blob: 55aaa5a8f4c69d21f993abccfee49524e2b526ba [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
8
Eyal Barid81da8c2017-01-11 13:39:54 +02009
Paul Vinciguerra090096b2020-12-03 00:42:46 -050010class BridgeDomain(metaclass=abc.ABCMeta):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020011 """Bridge domain abstraction"""
Damjan Marionf56b77a2016-10-03 19:44:57 +020012
Klement Sekeraf62ae122016-10-11 11:47:09 +020013 @property
Eyal Baric4aaee12016-12-20 18:36:46 +020014 def frame_request(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020015 """Ethernet frame modeling a generic request"""
16 return (
17 Ether(src="00:00:00:00:00:01", dst="00:00:00:00:00:02")
18 / IP(src="1.2.3.4", dst="4.3.2.1")
19 / UDP(sport=10000, dport=20000)
20 / Raw("\xa5" * 100)
21 )
Damjan Marionf56b77a2016-10-03 19:44:57 +020022
Klement Sekeraf62ae122016-10-11 11:47:09 +020023 @property
Eyal Baric4aaee12016-12-20 18:36:46 +020024 def frame_reply(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020025 """Ethernet frame modeling a generic reply"""
26 return (
27 Ether(src="00:00:00:00:00:02", dst="00:00:00:00:00:01")
28 / IP(src="4.3.2.1", dst="1.2.3.4")
29 / UDP(sport=20000, dport=10000)
30 / Raw("\xa5" * 100)
31 )
Klement Sekeraf62ae122016-10-11 11:47:09 +020032
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080033 @abc.abstractmethod
Eyal Baricef1e2a2018-06-18 13:01:59 +030034 def ip_range(self, start, end):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020035 """range of remote ip's"""
Eyal Baricef1e2a2018-06-18 13:01:59 +030036 pass
37
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080038 @abc.abstractmethod
Eyal Baric4aaee12016-12-20 18:36:46 +020039 def encap_mcast(self, pkt, src_ip, src_mac, vni):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020040 """Encapsulate mcast packet"""
Eyal Baric4aaee12016-12-20 18:36:46 +020041 pass
42
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080043 @abc.abstractmethod
Eyal Baric4aaee12016-12-20 18:36:46 +020044 def encapsulate(self, pkt, vni):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020045 """Encapsulate packet"""
Damjan Marionf56b77a2016-10-03 19:44:57 +020046 pass
47
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080048 @abc.abstractmethod
Damjan Marionf56b77a2016-10-03 19:44:57 +020049 def decapsulate(self, pkt):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020050 """Decapsulate packet"""
Damjan Marionf56b77a2016-10-03 19:44:57 +020051 pass
52
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080053 @abc.abstractmethod
Eyal Baric4aaee12016-12-20 18:36:46 +020054 def check_encapsulation(self, pkt, vni, local_only=False):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020055 """Verify the encapsulation"""
Damjan Marionf56b77a2016-10-03 19:44:57 +020056 pass
57
Eyal Baric4aaee12016-12-20 18:36:46 +020058 def assert_eq_pkts(self, pkt1, pkt2):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020059 """Verify the Ether, IP, UDP, payload are equal in both
Eyal Baric4aaee12016-12-20 18:36:46 +020060 packets
61 """
62 self.assertEqual(pkt1[Ether].src, pkt2[Ether].src)
63 self.assertEqual(pkt1[Ether].dst, pkt2[Ether].dst)
64 self.assertEqual(pkt1[IP].src, pkt2[IP].src)
65 self.assertEqual(pkt1[IP].dst, pkt2[IP].dst)
66 self.assertEqual(pkt1[UDP].sport, pkt2[UDP].sport)
67 self.assertEqual(pkt1[UDP].dport, pkt2[UDP].dport)
68 self.assertEqual(pkt1[Raw], pkt2[Raw])
69
Damjan Marionf56b77a2016-10-03 19:44:57 +020070 def test_decap(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020071 """Decapsulation test
Klement Sekeraf62ae122016-10-11 11:47:09 +020072 Send encapsulated frames from pg0
73 Verify receipt of decapsulated frames on pg1
74 """
Damjan Marionf56b77a2016-10-03 19:44:57 +020075
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020076 encapsulated_pkt = self.encapsulate(self.frame_request, self.single_tunnel_vni)
Damjan Marionf56b77a2016-10-03 19:44:57 +020077
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020078 self.pg0.add_stream(
79 [
80 encapsulated_pkt,
81 ]
82 )
Damjan Marionf56b77a2016-10-03 19:44:57 +020083
Klement Sekeraf62ae122016-10-11 11:47:09 +020084 self.pg1.enable_capture()
85
Damjan Marionf56b77a2016-10-03 19:44:57 +020086 self.pg_start()
87
Klement Sekerada505f62017-01-04 12:58:53 +010088 # Pick first received frame and check if it's the non-encapsulated
89 # frame
Klement Sekeradab231a2016-12-21 08:50:14 +010090 out = self.pg1.get_capture(1)
Damjan Marionf56b77a2016-10-03 19:44:57 +020091 pkt = out[0]
Eyal Baric4aaee12016-12-20 18:36:46 +020092 self.assert_eq_pkts(pkt, self.frame_request)
Damjan Marionf56b77a2016-10-03 19:44:57 +020093
Damjan Marionf56b77a2016-10-03 19:44:57 +020094 def test_encap(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020095 """Encapsulation test
Klement Sekeraf62ae122016-10-11 11:47:09 +020096 Send frames from pg1
97 Verify receipt of encapsulated frames on pg0
98 """
Eyal Baric4aaee12016-12-20 18:36:46 +020099 self.pg1.add_stream([self.frame_reply])
Damjan Marionf56b77a2016-10-03 19:44:57 +0200100
Klement Sekeraf62ae122016-10-11 11:47:09 +0200101 self.pg0.enable_capture()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200102
Damjan Marionf56b77a2016-10-03 19:44:57 +0200103 self.pg_start()
104
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700105 # Pick first received frame and check if it's correctly encapsulated.
Klement Sekeradab231a2016-12-21 08:50:14 +0100106 out = self.pg0.get_capture(1)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200107 pkt = out[0]
Neale Ranns91fd9102020-04-03 07:46:28 +0000108 self.check_encapsulation(pkt, self.single_tunnel_vni)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200109
Klement Sekeraf62ae122016-10-11 11:47:09 +0200110 payload = self.decapsulate(pkt)
Eyal Baric4aaee12016-12-20 18:36:46 +0200111 self.assert_eq_pkts(payload, self.frame_reply)
112
113 def test_ucast_flood(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200114 """Unicast flood test
Eyal Baric4aaee12016-12-20 18:36:46 +0200115 Send frames from pg3
116 Verify receipt of encapsulated frames on pg0
117 """
118 self.pg3.add_stream([self.frame_reply])
119
120 self.pg0.enable_capture()
121
122 self.pg_start()
123
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700124 # Get packet from each tunnel and assert it's correctly encapsulated.
Eyal Barid81da8c2017-01-11 13:39:54 +0200125 out = self.pg0.get_capture(self.n_ucast_tunnels)
Eyal Baric4aaee12016-12-20 18:36:46 +0200126 for pkt in out:
127 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
128 payload = self.decapsulate(pkt)
129 self.assert_eq_pkts(payload, self.frame_reply)
130
131 def test_mcast_flood(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200132 """Multicast flood test
Eyal Baric4aaee12016-12-20 18:36:46 +0200133 Send frames from pg2
134 Verify receipt of encapsulated frames on pg0
135 """
136 self.pg2.add_stream([self.frame_reply])
137
138 self.pg0.enable_capture()
139
140 self.pg_start()
141
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700142 # Pick first received frame and check if it's correctly encapsulated.
Eyal Baric4aaee12016-12-20 18:36:46 +0200143 out = self.pg0.get_capture(1)
144 pkt = out[0]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200145 self.check_encapsulation(
146 pkt, self.mcast_flood_bd, local_only=False, mcast_pkt=True
147 )
Eyal Baric4aaee12016-12-20 18:36:46 +0200148
149 payload = self.decapsulate(pkt)
150 self.assert_eq_pkts(payload, self.frame_reply)
151
Eyal Baric4aaee12016-12-20 18:36:46 +0200152 def test_mcast_rcv(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200153 """Multicast receive test
Eyal Baric4aaee12016-12-20 18:36:46 +0200154 Send 20 encapsulated frames from pg0 only 10 match unicast tunnels
155 Verify receipt of 10 decap frames on pg2
156 """
157 mac = self.pg0.remote_mac
158 ip_range_start = 10
159 ip_range_end = 30
160 mcast_stream = [
Eyal Barid81da8c2017-01-11 13:39:54 +0200161 self.encap_mcast(self.frame_request, ip, mac, self.mcast_flood_bd)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200162 for ip in self.ip_range(ip_range_start, ip_range_end)
163 ]
Eyal Baric4aaee12016-12-20 18:36:46 +0200164 self.pg0.add_stream(mcast_stream)
165 self.pg2.enable_capture()
166 self.pg_start()
167 out = self.pg2.get_capture(10)
168 for pkt in out:
169 self.assert_eq_pkts(pkt, self.frame_request)