blob: bebe76d765ddc74906145340b5c1291af6048bc9 [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 Sekeraf62ae122016-10-11 11:47:09 +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):
15 """ Ethernet frame modeling a generic request """
Klement Sekeraf62ae122016-10-11 11:47:09 +020016 return (Ether(src='00:00:00:00:00:01', dst='00:00:00:00:00:02') /
17 IP(src='1.2.3.4', dst='4.3.2.1') /
18 UDP(sport=10000, dport=20000) /
19 Raw('\xa5' * 100))
Damjan Marionf56b77a2016-10-03 19:44:57 +020020
Klement Sekeraf62ae122016-10-11 11:47:09 +020021 @property
Eyal Baric4aaee12016-12-20 18:36:46 +020022 def frame_reply(self):
23 """ Ethernet frame modeling a generic reply """
Klement Sekeraf62ae122016-10-11 11:47:09 +020024 return (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
25 IP(src='4.3.2.1', dst='1.2.3.4') /
26 UDP(sport=20000, dport=10000) /
27 Raw('\xa5' * 100))
28
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080029 @abc.abstractmethod
Eyal Baricef1e2a2018-06-18 13:01:59 +030030 def ip_range(self, start, end):
31 """ range of remote ip's """
32 pass
33
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080034 @abc.abstractmethod
Eyal Baric4aaee12016-12-20 18:36:46 +020035 def encap_mcast(self, pkt, src_ip, src_mac, vni):
36 """ Encapsulate mcast packet """
37 pass
38
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080039 @abc.abstractmethod
Eyal Baric4aaee12016-12-20 18:36:46 +020040 def encapsulate(self, pkt, vni):
Klement Sekeraf62ae122016-10-11 11:47:09 +020041 """ Encapsulate packet """
Damjan Marionf56b77a2016-10-03 19:44:57 +020042 pass
43
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080044 @abc.abstractmethod
Damjan Marionf56b77a2016-10-03 19:44:57 +020045 def decapsulate(self, pkt):
Klement Sekeraf62ae122016-10-11 11:47:09 +020046 """ Decapsulate packet """
Damjan Marionf56b77a2016-10-03 19:44:57 +020047 pass
48
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080049 @abc.abstractmethod
Eyal Baric4aaee12016-12-20 18:36:46 +020050 def check_encapsulation(self, pkt, vni, local_only=False):
Klement Sekeraf62ae122016-10-11 11:47:09 +020051 """ Verify the encapsulation """
Damjan Marionf56b77a2016-10-03 19:44:57 +020052 pass
53
Eyal Baric4aaee12016-12-20 18:36:46 +020054 def assert_eq_pkts(self, pkt1, pkt2):
55 """ Verify the Ether, IP, UDP, payload are equal in both
56 packets
57 """
58 self.assertEqual(pkt1[Ether].src, pkt2[Ether].src)
59 self.assertEqual(pkt1[Ether].dst, pkt2[Ether].dst)
60 self.assertEqual(pkt1[IP].src, pkt2[IP].src)
61 self.assertEqual(pkt1[IP].dst, pkt2[IP].dst)
62 self.assertEqual(pkt1[UDP].sport, pkt2[UDP].sport)
63 self.assertEqual(pkt1[UDP].dport, pkt2[UDP].dport)
64 self.assertEqual(pkt1[Raw], pkt2[Raw])
65
Damjan Marionf56b77a2016-10-03 19:44:57 +020066 def test_decap(self):
Klement Sekeraf62ae122016-10-11 11:47:09 +020067 """ Decapsulation test
68 Send encapsulated frames from pg0
69 Verify receipt of decapsulated frames on pg1
70 """
Damjan Marionf56b77a2016-10-03 19:44:57 +020071
Eyal Baric4aaee12016-12-20 18:36:46 +020072 encapsulated_pkt = self.encapsulate(self.frame_request,
Neale Ranns91fd9102020-04-03 07:46:28 +000073 self.single_tunnel_vni)
Damjan Marionf56b77a2016-10-03 19:44:57 +020074
Klement Sekeraf62ae122016-10-11 11:47:09 +020075 self.pg0.add_stream([encapsulated_pkt, ])
Damjan Marionf56b77a2016-10-03 19:44:57 +020076
Klement Sekeraf62ae122016-10-11 11:47:09 +020077 self.pg1.enable_capture()
78
Damjan Marionf56b77a2016-10-03 19:44:57 +020079 self.pg_start()
80
Klement Sekerada505f62017-01-04 12:58:53 +010081 # Pick first received frame and check if it's the non-encapsulated
82 # frame
Klement Sekeradab231a2016-12-21 08:50:14 +010083 out = self.pg1.get_capture(1)
Damjan Marionf56b77a2016-10-03 19:44:57 +020084 pkt = out[0]
Eyal Baric4aaee12016-12-20 18:36:46 +020085 self.assert_eq_pkts(pkt, self.frame_request)
Damjan Marionf56b77a2016-10-03 19:44:57 +020086
Damjan Marionf56b77a2016-10-03 19:44:57 +020087 def test_encap(self):
Klement Sekeraf62ae122016-10-11 11:47:09 +020088 """ Encapsulation test
89 Send frames from pg1
90 Verify receipt of encapsulated frames on pg0
91 """
Eyal Baric4aaee12016-12-20 18:36:46 +020092 self.pg1.add_stream([self.frame_reply])
Damjan Marionf56b77a2016-10-03 19:44:57 +020093
Klement Sekeraf62ae122016-10-11 11:47:09 +020094 self.pg0.enable_capture()
Damjan Marionf56b77a2016-10-03 19:44:57 +020095
Damjan Marionf56b77a2016-10-03 19:44:57 +020096 self.pg_start()
97
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -070098 # Pick first received frame and check if it's correctly encapsulated.
Klement Sekeradab231a2016-12-21 08:50:14 +010099 out = self.pg0.get_capture(1)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200100 pkt = out[0]
Neale Ranns91fd9102020-04-03 07:46:28 +0000101 self.check_encapsulation(pkt, self.single_tunnel_vni)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200102
Klement Sekeraf62ae122016-10-11 11:47:09 +0200103 payload = self.decapsulate(pkt)
Eyal Baric4aaee12016-12-20 18:36:46 +0200104 self.assert_eq_pkts(payload, self.frame_reply)
105
106 def test_ucast_flood(self):
107 """ Unicast flood test
108 Send frames from pg3
109 Verify receipt of encapsulated frames on pg0
110 """
111 self.pg3.add_stream([self.frame_reply])
112
113 self.pg0.enable_capture()
114
115 self.pg_start()
116
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700117 # Get packet from each tunnel and assert it's correctly encapsulated.
Eyal Barid81da8c2017-01-11 13:39:54 +0200118 out = self.pg0.get_capture(self.n_ucast_tunnels)
Eyal Baric4aaee12016-12-20 18:36:46 +0200119 for pkt in out:
120 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
121 payload = self.decapsulate(pkt)
122 self.assert_eq_pkts(payload, self.frame_reply)
123
124 def test_mcast_flood(self):
125 """ Multicast flood test
126 Send frames from pg2
127 Verify receipt of encapsulated frames on pg0
128 """
129 self.pg2.add_stream([self.frame_reply])
130
131 self.pg0.enable_capture()
132
133 self.pg_start()
134
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700135 # Pick first received frame and check if it's correctly encapsulated.
Eyal Baric4aaee12016-12-20 18:36:46 +0200136 out = self.pg0.get_capture(1)
137 pkt = out[0]
Eyal Bari6ae5ee72017-03-23 09:53:51 +0200138 self.check_encapsulation(pkt, self.mcast_flood_bd,
139 local_only=False, mcast_pkt=True)
Eyal Baric4aaee12016-12-20 18:36:46 +0200140
141 payload = self.decapsulate(pkt)
142 self.assert_eq_pkts(payload, self.frame_reply)
143
Eyal Baric4aaee12016-12-20 18:36:46 +0200144 def test_mcast_rcv(self):
145 """ Multicast receive test
146 Send 20 encapsulated frames from pg0 only 10 match unicast tunnels
147 Verify receipt of 10 decap frames on pg2
148 """
149 mac = self.pg0.remote_mac
150 ip_range_start = 10
151 ip_range_end = 30
152 mcast_stream = [
Eyal Barid81da8c2017-01-11 13:39:54 +0200153 self.encap_mcast(self.frame_request, ip, mac, self.mcast_flood_bd)
Eyal Baricef1e2a2018-06-18 13:01:59 +0300154 for ip in self.ip_range(ip_range_start, ip_range_end)]
Eyal Baric4aaee12016-12-20 18:36:46 +0200155 self.pg0.add_stream(mcast_stream)
156 self.pg2.enable_capture()
157 self.pg_start()
158 out = self.pg2.get_capture(10)
159 for pkt in out:
160 self.assert_eq_pkts(pkt, self.frame_request)