blob: 198cc2258b8d5e25687896b11b5f47079dd49ef0 [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
4import six
Damjan Marionf56b77a2016-10-03 19:44:57 +02005
snaramre5d4b8912019-12-13 23:39:35 +00006from scapy.layers.l2 import Ether
7from scapy.packet import Raw
Damjan Marionf56b77a2016-10-03 19:44:57 +02008from scapy.layers.inet import IP, UDP
9
Eyal Barid81da8c2017-01-11 13:39:54 +020010from util import ip4_range
11
Damjan Marionf56b77a2016-10-03 19:44:57 +020012
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080013@six.add_metaclass(abc.ABCMeta)
Damjan Marionf56b77a2016-10-03 19:44:57 +020014class BridgeDomain(object):
Klement Sekeraf62ae122016-10-11 11:47:09 +020015 """ Bridge domain abstraction """
Damjan Marionf56b77a2016-10-03 19:44:57 +020016
Klement Sekeraf62ae122016-10-11 11:47:09 +020017 @property
Eyal Baric4aaee12016-12-20 18:36:46 +020018 def frame_request(self):
19 """ Ethernet frame modeling a generic request """
Klement Sekeraf62ae122016-10-11 11:47:09 +020020 return (Ether(src='00:00:00:00:00:01', dst='00:00:00:00:00:02') /
21 IP(src='1.2.3.4', dst='4.3.2.1') /
22 UDP(sport=10000, dport=20000) /
23 Raw('\xa5' * 100))
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):
27 """ Ethernet frame modeling a generic reply """
Klement Sekeraf62ae122016-10-11 11:47:09 +020028 return (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
29 IP(src='4.3.2.1', dst='1.2.3.4') /
30 UDP(sport=20000, dport=10000) /
31 Raw('\xa5' * 100))
32
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080033 @abc.abstractmethod
Eyal Baricef1e2a2018-06-18 13:01:59 +030034 def ip_range(self, start, end):
35 """ range of remote ip's """
36 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):
40 """ Encapsulate mcast packet """
41 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 Sekeraf62ae122016-10-11 11:47:09 +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 Sekeraf62ae122016-10-11 11:47:09 +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 Sekeraf62ae122016-10-11 11:47:09 +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):
59 """ Verify the Ether, IP, UDP, payload are equal in both
60 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 Sekeraf62ae122016-10-11 11:47:09 +020071 """ Decapsulation test
72 Send encapsulated frames from pg0
73 Verify receipt of decapsulated frames on pg1
74 """
Damjan Marionf56b77a2016-10-03 19:44:57 +020075
Eyal Baric4aaee12016-12-20 18:36:46 +020076 encapsulated_pkt = self.encapsulate(self.frame_request,
Neale Ranns91fd9102020-04-03 07:46:28 +000077 self.single_tunnel_vni)
Damjan Marionf56b77a2016-10-03 19:44:57 +020078
Klement Sekeraf62ae122016-10-11 11:47:09 +020079 self.pg0.add_stream([encapsulated_pkt, ])
Damjan Marionf56b77a2016-10-03 19:44:57 +020080
Klement Sekeraf62ae122016-10-11 11:47:09 +020081 self.pg1.enable_capture()
82
Damjan Marionf56b77a2016-10-03 19:44:57 +020083 self.pg_start()
84
Klement Sekerada505f62017-01-04 12:58:53 +010085 # Pick first received frame and check if it's the non-encapsulated
86 # frame
Klement Sekeradab231a2016-12-21 08:50:14 +010087 out = self.pg1.get_capture(1)
Damjan Marionf56b77a2016-10-03 19:44:57 +020088 pkt = out[0]
Eyal Baric4aaee12016-12-20 18:36:46 +020089 self.assert_eq_pkts(pkt, self.frame_request)
Damjan Marionf56b77a2016-10-03 19:44:57 +020090
Damjan Marionf56b77a2016-10-03 19:44:57 +020091 def test_encap(self):
Klement Sekeraf62ae122016-10-11 11:47:09 +020092 """ Encapsulation test
93 Send frames from pg1
94 Verify receipt of encapsulated frames on pg0
95 """
Eyal Baric4aaee12016-12-20 18:36:46 +020096 self.pg1.add_stream([self.frame_reply])
Damjan Marionf56b77a2016-10-03 19:44:57 +020097
Klement Sekeraf62ae122016-10-11 11:47:09 +020098 self.pg0.enable_capture()
Damjan Marionf56b77a2016-10-03 19:44:57 +020099
Damjan Marionf56b77a2016-10-03 19:44:57 +0200100 self.pg_start()
101
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700102 # Pick first received frame and check if it's correctly encapsulated.
Klement Sekeradab231a2016-12-21 08:50:14 +0100103 out = self.pg0.get_capture(1)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200104 pkt = out[0]
Neale Ranns91fd9102020-04-03 07:46:28 +0000105 self.check_encapsulation(pkt, self.single_tunnel_vni)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200106
Klement Sekeraf62ae122016-10-11 11:47:09 +0200107 payload = self.decapsulate(pkt)
Eyal Baric4aaee12016-12-20 18:36:46 +0200108 self.assert_eq_pkts(payload, self.frame_reply)
109
110 def test_ucast_flood(self):
111 """ Unicast flood test
112 Send frames from pg3
113 Verify receipt of encapsulated frames on pg0
114 """
115 self.pg3.add_stream([self.frame_reply])
116
117 self.pg0.enable_capture()
118
119 self.pg_start()
120
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700121 # Get packet from each tunnel and assert it's correctly encapsulated.
Eyal Barid81da8c2017-01-11 13:39:54 +0200122 out = self.pg0.get_capture(self.n_ucast_tunnels)
Eyal Baric4aaee12016-12-20 18:36:46 +0200123 for pkt in out:
124 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
125 payload = self.decapsulate(pkt)
126 self.assert_eq_pkts(payload, self.frame_reply)
127
128 def test_mcast_flood(self):
129 """ Multicast flood test
130 Send frames from pg2
131 Verify receipt of encapsulated frames on pg0
132 """
133 self.pg2.add_stream([self.frame_reply])
134
135 self.pg0.enable_capture()
136
137 self.pg_start()
138
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700139 # Pick first received frame and check if it's correctly encapsulated.
Eyal Baric4aaee12016-12-20 18:36:46 +0200140 out = self.pg0.get_capture(1)
141 pkt = out[0]
Eyal Bari6ae5ee72017-03-23 09:53:51 +0200142 self.check_encapsulation(pkt, self.mcast_flood_bd,
143 local_only=False, mcast_pkt=True)
Eyal Baric4aaee12016-12-20 18:36:46 +0200144
145 payload = self.decapsulate(pkt)
146 self.assert_eq_pkts(payload, self.frame_reply)
147
Eyal Baric4aaee12016-12-20 18:36:46 +0200148 def test_mcast_rcv(self):
149 """ Multicast receive test
150 Send 20 encapsulated frames from pg0 only 10 match unicast tunnels
151 Verify receipt of 10 decap frames on pg2
152 """
153 mac = self.pg0.remote_mac
154 ip_range_start = 10
155 ip_range_end = 30
156 mcast_stream = [
Eyal Barid81da8c2017-01-11 13:39:54 +0200157 self.encap_mcast(self.frame_request, ip, mac, self.mcast_flood_bd)
Eyal Baricef1e2a2018-06-18 13:01:59 +0300158 for ip in self.ip_range(ip_range_start, ip_range_end)]
Eyal Baric4aaee12016-12-20 18:36:46 +0200159 self.pg0.add_stream(mcast_stream)
160 self.pg2.enable_capture()
161 self.pg_start()
162 out = self.pg2.get_capture(10)
163 for pkt in out:
164 self.assert_eq_pkts(pkt, self.frame_request)