blob: 07e824af5a882b84ac4628299f41d57074352b77 [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Damjan Marionf56b77a2016-10-03 19:44:57 +02002
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -08003import abc
Damjan Marionf56b77a2016-10-03 19:44:57 +02004
snaramre5d4b8912019-12-13 23:39:35 +00005from scapy.layers.l2 import Ether
6from scapy.packet import Raw
Damjan Marionf56b77a2016-10-03 19:44:57 +02007from scapy.layers.inet import IP, UDP
Vladislav Grishenkof2fc97a2024-01-24 20:33:12 +05008from scapy.layers.inet6 import IPv6
9from scapy.contrib.mpls import MPLS
Damjan Marionf56b77a2016-10-03 19:44:57 +020010
Eyal Barid81da8c2017-01-11 13:39:54 +020011
Paul Vinciguerra090096b2020-12-03 00:42:46 -050012class BridgeDomain(metaclass=abc.ABCMeta):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020013 """Bridge domain abstraction"""
Damjan Marionf56b77a2016-10-03 19:44:57 +020014
Klement Sekeraf62ae122016-10-11 11:47:09 +020015 @property
Eyal Baric4aaee12016-12-20 18:36:46 +020016 def frame_request(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020017 """Ethernet frame modeling a generic request"""
18 return (
19 Ether(src="00:00:00:00:00:01", dst="00:00:00:00:00:02")
20 / IP(src="1.2.3.4", dst="4.3.2.1")
21 / UDP(sport=10000, dport=20000)
22 / Raw("\xa5" * 100)
23 )
Damjan Marionf56b77a2016-10-03 19:44:57 +020024
Klement Sekeraf62ae122016-10-11 11:47:09 +020025 @property
Eyal Baric4aaee12016-12-20 18:36:46 +020026 def frame_reply(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020027 """Ethernet frame modeling a generic reply"""
28 return (
29 Ether(src="00:00:00:00:00:02", dst="00:00:00:00:00:01")
30 / IP(src="4.3.2.1", dst="1.2.3.4")
31 / UDP(sport=20000, dport=10000)
32 / Raw("\xa5" * 100)
33 )
Klement Sekeraf62ae122016-10-11 11:47:09 +020034
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080035 @abc.abstractmethod
Eyal Baricef1e2a2018-06-18 13:01:59 +030036 def ip_range(self, start, end):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020037 """range of remote ip's"""
Eyal Baricef1e2a2018-06-18 13:01:59 +030038 pass
39
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080040 @abc.abstractmethod
Eyal Baric4aaee12016-12-20 18:36:46 +020041 def encap_mcast(self, pkt, src_ip, src_mac, vni):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020042 """Encapsulate mcast packet"""
Eyal Baric4aaee12016-12-20 18:36:46 +020043 pass
44
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080045 @abc.abstractmethod
Eyal Baric4aaee12016-12-20 18:36:46 +020046 def encapsulate(self, pkt, vni):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020047 """Encapsulate packet"""
Damjan Marionf56b77a2016-10-03 19:44:57 +020048 pass
49
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080050 @abc.abstractmethod
Damjan Marionf56b77a2016-10-03 19:44:57 +020051 def decapsulate(self, pkt):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020052 """Decapsulate packet"""
Damjan Marionf56b77a2016-10-03 19:44:57 +020053 pass
54
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080055 @abc.abstractmethod
Eyal Baric4aaee12016-12-20 18:36:46 +020056 def check_encapsulation(self, pkt, vni, local_only=False):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020057 """Verify the encapsulation"""
Damjan Marionf56b77a2016-10-03 19:44:57 +020058 pass
59
Eyal Baric4aaee12016-12-20 18:36:46 +020060 def assert_eq_pkts(self, pkt1, pkt2):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020061 """Verify the Ether, IP, UDP, payload are equal in both
Eyal Baric4aaee12016-12-20 18:36:46 +020062 packets
63 """
64 self.assertEqual(pkt1[Ether].src, pkt2[Ether].src)
65 self.assertEqual(pkt1[Ether].dst, pkt2[Ether].dst)
Vladislav Grishenkof2fc97a2024-01-24 20:33:12 +050066 if MPLS in pkt1 or MPLS in pkt2:
67 self.assertEqual(pkt1[MPLS].label, pkt2[MPLS].label)
68 self.assertEqual(pkt1[MPLS].cos, pkt2[MPLS].cos)
69 self.assertEqual(pkt1[MPLS].ttl, pkt2[MPLS].ttl)
70 if IP in pkt1 or IP in pkt2:
71 self.assertEqual(pkt1[IP].src, pkt2[IP].src)
72 self.assertEqual(pkt1[IP].dst, pkt2[IP].dst)
73 elif IPv6 in pkt1 or IPv6 in pkt2:
74 self.assertEqual(pkt1[IPv6].src, pkt2[IPv6].src)
75 self.assertEqual(pkt1[IPv6].dst, pkt2[IPv6].dst)
Eyal Baric4aaee12016-12-20 18:36:46 +020076 self.assertEqual(pkt1[UDP].sport, pkt2[UDP].sport)
77 self.assertEqual(pkt1[UDP].dport, pkt2[UDP].dport)
78 self.assertEqual(pkt1[Raw], pkt2[Raw])
79
Damjan Marionf56b77a2016-10-03 19:44:57 +020080 def test_decap(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020081 """Decapsulation test
Klement Sekeraf62ae122016-10-11 11:47:09 +020082 Send encapsulated frames from pg0
83 Verify receipt of decapsulated frames on pg1
84 """
Damjan Marionf56b77a2016-10-03 19:44:57 +020085
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020086 encapsulated_pkt = self.encapsulate(self.frame_request, self.single_tunnel_vni)
Damjan Marionf56b77a2016-10-03 19:44:57 +020087
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020088 self.pg0.add_stream(
89 [
90 encapsulated_pkt,
91 ]
92 )
Damjan Marionf56b77a2016-10-03 19:44:57 +020093
Klement Sekeraf62ae122016-10-11 11:47:09 +020094 self.pg1.enable_capture()
95
Damjan Marionf56b77a2016-10-03 19:44:57 +020096 self.pg_start()
97
Klement Sekerada505f62017-01-04 12:58:53 +010098 # Pick first received frame and check if it's the non-encapsulated
99 # frame
Klement Sekeradab231a2016-12-21 08:50:14 +0100100 out = self.pg1.get_capture(1)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200101 pkt = out[0]
Eyal Baric4aaee12016-12-20 18:36:46 +0200102 self.assert_eq_pkts(pkt, self.frame_request)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200103
Damjan Marionf56b77a2016-10-03 19:44:57 +0200104 def test_encap(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200105 """Encapsulation test
Klement Sekeraf62ae122016-10-11 11:47:09 +0200106 Send frames from pg1
107 Verify receipt of encapsulated frames on pg0
108 """
Eyal Baric4aaee12016-12-20 18:36:46 +0200109 self.pg1.add_stream([self.frame_reply])
Damjan Marionf56b77a2016-10-03 19:44:57 +0200110
Klement Sekeraf62ae122016-10-11 11:47:09 +0200111 self.pg0.enable_capture()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200112
Damjan Marionf56b77a2016-10-03 19:44:57 +0200113 self.pg_start()
114
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700115 # Pick first received frame and check if it's correctly encapsulated.
Klement Sekeradab231a2016-12-21 08:50:14 +0100116 out = self.pg0.get_capture(1)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200117 pkt = out[0]
Neale Ranns91fd9102020-04-03 07:46:28 +0000118 self.check_encapsulation(pkt, self.single_tunnel_vni)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200119
Klement Sekeraf62ae122016-10-11 11:47:09 +0200120 payload = self.decapsulate(pkt)
Eyal Baric4aaee12016-12-20 18:36:46 +0200121 self.assert_eq_pkts(payload, self.frame_reply)
122
123 def test_ucast_flood(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200124 """Unicast flood test
Eyal Baric4aaee12016-12-20 18:36:46 +0200125 Send frames from pg3
126 Verify receipt of encapsulated frames on pg0
127 """
128 self.pg3.add_stream([self.frame_reply])
129
130 self.pg0.enable_capture()
131
132 self.pg_start()
133
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700134 # Get packet from each tunnel and assert it's correctly encapsulated.
Eyal Barid81da8c2017-01-11 13:39:54 +0200135 out = self.pg0.get_capture(self.n_ucast_tunnels)
Eyal Baric4aaee12016-12-20 18:36:46 +0200136 for pkt in out:
137 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
138 payload = self.decapsulate(pkt)
139 self.assert_eq_pkts(payload, self.frame_reply)
140
141 def test_mcast_flood(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200142 """Multicast flood test
Eyal Baric4aaee12016-12-20 18:36:46 +0200143 Send frames from pg2
144 Verify receipt of encapsulated frames on pg0
145 """
146 self.pg2.add_stream([self.frame_reply])
147
148 self.pg0.enable_capture()
149
150 self.pg_start()
151
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700152 # Pick first received frame and check if it's correctly encapsulated.
Eyal Baric4aaee12016-12-20 18:36:46 +0200153 out = self.pg0.get_capture(1)
154 pkt = out[0]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200155 self.check_encapsulation(
156 pkt, self.mcast_flood_bd, local_only=False, mcast_pkt=True
157 )
Eyal Baric4aaee12016-12-20 18:36:46 +0200158
159 payload = self.decapsulate(pkt)
160 self.assert_eq_pkts(payload, self.frame_reply)
161
Eyal Baric4aaee12016-12-20 18:36:46 +0200162 def test_mcast_rcv(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200163 """Multicast receive test
Eyal Baric4aaee12016-12-20 18:36:46 +0200164 Send 20 encapsulated frames from pg0 only 10 match unicast tunnels
165 Verify receipt of 10 decap frames on pg2
166 """
167 mac = self.pg0.remote_mac
168 ip_range_start = 10
169 ip_range_end = 30
170 mcast_stream = [
Eyal Barid81da8c2017-01-11 13:39:54 +0200171 self.encap_mcast(self.frame_request, ip, mac, self.mcast_flood_bd)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200172 for ip in self.ip_range(ip_range_start, ip_range_end)
173 ]
Eyal Baric4aaee12016-12-20 18:36:46 +0200174 self.pg0.add_stream(mcast_stream)
175 self.pg2.enable_capture()
176 self.pg_start()
177 out = self.pg2.get_capture(10)
178 for pkt in out:
179 self.assert_eq_pkts(pkt, self.frame_request)