2019-10-31 13:31:07 -05:00
|
|
|
#!/usr/bin/env python3
|
2016-10-03 19:44:57 +02:00
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
import abc
|
2016-10-03 19:44:57 +02:00
|
|
|
|
2019-12-13 23:39:35 +00:00
|
|
|
from scapy.layers.l2 import Ether
|
|
|
|
from scapy.packet import Raw
|
2016-10-03 19:44:57 +02:00
|
|
|
from scapy.layers.inet import IP, UDP
|
2024-01-24 20:33:12 +05:00
|
|
|
from scapy.layers.inet6 import IPv6
|
|
|
|
from scapy.contrib.mpls import MPLS
|
2016-10-03 19:44:57 +02:00
|
|
|
|
2017-01-11 13:39:54 +02:00
|
|
|
|
2020-12-03 00:42:46 -05:00
|
|
|
class BridgeDomain(metaclass=abc.ABCMeta):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Bridge domain abstraction"""
|
2016-10-11 11:47:09 +02:00
|
|
|
|
|
|
|
@property
|
2016-12-20 18:36:46 +02:00
|
|
|
def frame_request(self):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Ethernet frame modeling a generic request"""
|
|
|
|
return (
|
|
|
|
Ether(src="00:00:00:00:00:01", dst="00:00:00:00:00:02")
|
|
|
|
/ IP(src="1.2.3.4", dst="4.3.2.1")
|
|
|
|
/ UDP(sport=10000, dport=20000)
|
|
|
|
/ Raw("\xa5" * 100)
|
|
|
|
)
|
2016-10-11 11:47:09 +02:00
|
|
|
|
|
|
|
@property
|
2016-12-20 18:36:46 +02:00
|
|
|
def frame_reply(self):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Ethernet frame modeling a generic reply"""
|
|
|
|
return (
|
|
|
|
Ether(src="00:00:00:00:00:02", dst="00:00:00:00:00:01")
|
|
|
|
/ IP(src="4.3.2.1", dst="1.2.3.4")
|
|
|
|
/ UDP(sport=20000, dport=10000)
|
|
|
|
/ Raw("\xa5" * 100)
|
|
|
|
)
|
2016-10-11 11:47:09 +02:00
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
@abc.abstractmethod
|
2018-06-18 13:01:59 +03:00
|
|
|
def ip_range(self, start, end):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""range of remote ip's"""
|
2018-06-18 13:01:59 +03:00
|
|
|
pass
|
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
@abc.abstractmethod
|
2016-12-20 18:36:46 +02:00
|
|
|
def encap_mcast(self, pkt, src_ip, src_mac, vni):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Encapsulate mcast packet"""
|
2016-12-20 18:36:46 +02:00
|
|
|
pass
|
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
@abc.abstractmethod
|
2016-12-20 18:36:46 +02:00
|
|
|
def encapsulate(self, pkt, vni):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Encapsulate packet"""
|
2016-10-03 19:44:57 +02:00
|
|
|
pass
|
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
@abc.abstractmethod
|
2016-10-03 19:44:57 +02:00
|
|
|
def decapsulate(self, pkt):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Decapsulate packet"""
|
2016-10-03 19:44:57 +02:00
|
|
|
pass
|
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
@abc.abstractmethod
|
2016-12-20 18:36:46 +02:00
|
|
|
def check_encapsulation(self, pkt, vni, local_only=False):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Verify the encapsulation"""
|
2016-10-03 19:44:57 +02:00
|
|
|
pass
|
|
|
|
|
2016-12-20 18:36:46 +02:00
|
|
|
def assert_eq_pkts(self, pkt1, pkt2):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Verify the Ether, IP, UDP, payload are equal in both
|
2016-12-20 18:36:46 +02:00
|
|
|
packets
|
|
|
|
"""
|
|
|
|
self.assertEqual(pkt1[Ether].src, pkt2[Ether].src)
|
|
|
|
self.assertEqual(pkt1[Ether].dst, pkt2[Ether].dst)
|
2024-01-24 20:33:12 +05:00
|
|
|
if MPLS in pkt1 or MPLS in pkt2:
|
|
|
|
self.assertEqual(pkt1[MPLS].label, pkt2[MPLS].label)
|
|
|
|
self.assertEqual(pkt1[MPLS].cos, pkt2[MPLS].cos)
|
|
|
|
self.assertEqual(pkt1[MPLS].ttl, pkt2[MPLS].ttl)
|
|
|
|
if IP in pkt1 or IP in pkt2:
|
|
|
|
self.assertEqual(pkt1[IP].src, pkt2[IP].src)
|
|
|
|
self.assertEqual(pkt1[IP].dst, pkt2[IP].dst)
|
|
|
|
elif IPv6 in pkt1 or IPv6 in pkt2:
|
|
|
|
self.assertEqual(pkt1[IPv6].src, pkt2[IPv6].src)
|
|
|
|
self.assertEqual(pkt1[IPv6].dst, pkt2[IPv6].dst)
|
2016-12-20 18:36:46 +02:00
|
|
|
self.assertEqual(pkt1[UDP].sport, pkt2[UDP].sport)
|
|
|
|
self.assertEqual(pkt1[UDP].dport, pkt2[UDP].dport)
|
|
|
|
self.assertEqual(pkt1[Raw], pkt2[Raw])
|
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
def test_decap(self):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Decapsulation test
|
2016-10-11 11:47:09 +02:00
|
|
|
Send encapsulated frames from pg0
|
|
|
|
Verify receipt of decapsulated frames on pg1
|
|
|
|
"""
|
|
|
|
|
2022-04-26 19:02:15 +02:00
|
|
|
encapsulated_pkt = self.encapsulate(self.frame_request, self.single_tunnel_vni)
|
2016-10-03 19:44:57 +02:00
|
|
|
|
2022-04-26 19:02:15 +02:00
|
|
|
self.pg0.add_stream(
|
|
|
|
[
|
|
|
|
encapsulated_pkt,
|
|
|
|
]
|
|
|
|
)
|
2016-10-03 19:44:57 +02:00
|
|
|
|
2016-10-11 11:47:09 +02:00
|
|
|
self.pg1.enable_capture()
|
2016-10-03 19:44:57 +02:00
|
|
|
|
|
|
|
self.pg_start()
|
|
|
|
|
2017-01-04 12:58:53 +01:00
|
|
|
# Pick first received frame and check if it's the non-encapsulated
|
|
|
|
# frame
|
2016-12-21 08:50:14 +01:00
|
|
|
out = self.pg1.get_capture(1)
|
2016-10-03 19:44:57 +02:00
|
|
|
pkt = out[0]
|
2016-12-20 18:36:46 +02:00
|
|
|
self.assert_eq_pkts(pkt, self.frame_request)
|
2016-10-11 11:47:09 +02:00
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
def test_encap(self):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Encapsulation test
|
2016-10-11 11:47:09 +02:00
|
|
|
Send frames from pg1
|
|
|
|
Verify receipt of encapsulated frames on pg0
|
|
|
|
"""
|
2016-12-20 18:36:46 +02:00
|
|
|
self.pg1.add_stream([self.frame_reply])
|
2016-10-03 19:44:57 +02:00
|
|
|
|
2016-10-11 11:47:09 +02:00
|
|
|
self.pg0.enable_capture()
|
2016-10-03 19:44:57 +02:00
|
|
|
|
|
|
|
self.pg_start()
|
|
|
|
|
2019-03-27 11:25:48 -07:00
|
|
|
# Pick first received frame and check if it's correctly encapsulated.
|
2016-12-21 08:50:14 +01:00
|
|
|
out = self.pg0.get_capture(1)
|
2016-10-11 11:47:09 +02:00
|
|
|
pkt = out[0]
|
2020-04-03 07:46:28 +00:00
|
|
|
self.check_encapsulation(pkt, self.single_tunnel_vni)
|
2016-10-03 19:44:57 +02:00
|
|
|
|
2016-10-11 11:47:09 +02:00
|
|
|
payload = self.decapsulate(pkt)
|
2016-12-20 18:36:46 +02:00
|
|
|
self.assert_eq_pkts(payload, self.frame_reply)
|
|
|
|
|
|
|
|
def test_ucast_flood(self):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Unicast flood test
|
2016-12-20 18:36:46 +02:00
|
|
|
Send frames from pg3
|
|
|
|
Verify receipt of encapsulated frames on pg0
|
|
|
|
"""
|
|
|
|
self.pg3.add_stream([self.frame_reply])
|
|
|
|
|
|
|
|
self.pg0.enable_capture()
|
|
|
|
|
|
|
|
self.pg_start()
|
|
|
|
|
2019-03-27 11:25:48 -07:00
|
|
|
# Get packet from each tunnel and assert it's correctly encapsulated.
|
2017-01-11 13:39:54 +02:00
|
|
|
out = self.pg0.get_capture(self.n_ucast_tunnels)
|
2016-12-20 18:36:46 +02:00
|
|
|
for pkt in out:
|
|
|
|
self.check_encapsulation(pkt, self.ucast_flood_bd, True)
|
|
|
|
payload = self.decapsulate(pkt)
|
|
|
|
self.assert_eq_pkts(payload, self.frame_reply)
|
|
|
|
|
|
|
|
def test_mcast_flood(self):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Multicast flood test
|
2016-12-20 18:36:46 +02:00
|
|
|
Send frames from pg2
|
|
|
|
Verify receipt of encapsulated frames on pg0
|
|
|
|
"""
|
|
|
|
self.pg2.add_stream([self.frame_reply])
|
|
|
|
|
|
|
|
self.pg0.enable_capture()
|
|
|
|
|
|
|
|
self.pg_start()
|
|
|
|
|
2019-03-27 11:25:48 -07:00
|
|
|
# Pick first received frame and check if it's correctly encapsulated.
|
2016-12-20 18:36:46 +02:00
|
|
|
out = self.pg0.get_capture(1)
|
|
|
|
pkt = out[0]
|
2022-04-26 19:02:15 +02:00
|
|
|
self.check_encapsulation(
|
|
|
|
pkt, self.mcast_flood_bd, local_only=False, mcast_pkt=True
|
|
|
|
)
|
2016-12-20 18:36:46 +02:00
|
|
|
|
|
|
|
payload = self.decapsulate(pkt)
|
|
|
|
self.assert_eq_pkts(payload, self.frame_reply)
|
|
|
|
|
|
|
|
def test_mcast_rcv(self):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Multicast receive test
|
2016-12-20 18:36:46 +02:00
|
|
|
Send 20 encapsulated frames from pg0 only 10 match unicast tunnels
|
|
|
|
Verify receipt of 10 decap frames on pg2
|
|
|
|
"""
|
|
|
|
mac = self.pg0.remote_mac
|
|
|
|
ip_range_start = 10
|
|
|
|
ip_range_end = 30
|
|
|
|
mcast_stream = [
|
2017-01-11 13:39:54 +02:00
|
|
|
self.encap_mcast(self.frame_request, ip, mac, self.mcast_flood_bd)
|
2022-04-26 19:02:15 +02:00
|
|
|
for ip in self.ip_range(ip_range_start, ip_range_end)
|
|
|
|
]
|
2016-12-20 18:36:46 +02:00
|
|
|
self.pg0.add_stream(mcast_stream)
|
|
|
|
self.pg2.enable_capture()
|
|
|
|
self.pg_start()
|
|
|
|
out = self.pg2.get_capture(10)
|
|
|
|
for pkt in out:
|
|
|
|
self.assert_eq_pkts(pkt, self.frame_request)
|