blob: 39f71438ab0a3470cf9d583758c1b35ae456f9c1 [file] [log] [blame]
Damjan Marionf56b77a2016-10-03 19:44:57 +02001#!/usr/bin/env python
2
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -08003import abc
4import six
Damjan Marionf56b77a2016-10-03 19:44:57 +02005
6from scapy.layers.l2 import Ether, Raw
7from scapy.layers.inet import IP, UDP
8
Eyal Barid81da8c2017-01-11 13:39:54 +02009from util import ip4_range
10
Damjan Marionf56b77a2016-10-03 19:44:57 +020011
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080012@six.add_metaclass(abc.ABCMeta)
Damjan Marionf56b77a2016-10-03 19:44:57 +020013class BridgeDomain(object):
Klement Sekeraf62ae122016-10-11 11:47:09 +020014 """ Bridge domain abstraction """
Damjan Marionf56b77a2016-10-03 19:44:57 +020015
Klement Sekeraf62ae122016-10-11 11:47:09 +020016 @property
Eyal Baric4aaee12016-12-20 18:36:46 +020017 def frame_request(self):
18 """ Ethernet frame modeling a generic request """
Klement Sekeraf62ae122016-10-11 11:47:09 +020019 return (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))
Damjan Marionf56b77a2016-10-03 19:44:57 +020023
Klement Sekeraf62ae122016-10-11 11:47:09 +020024 @property
Eyal Baric4aaee12016-12-20 18:36:46 +020025 def frame_reply(self):
26 """ Ethernet frame modeling a generic reply """
Klement Sekeraf62ae122016-10-11 11:47:09 +020027 return (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
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080032 @abc.abstractmethod
Eyal Baricef1e2a2018-06-18 13:01:59 +030033 def ip_range(self, start, end):
34 """ range of remote ip's """
35 pass
36
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080037 @abc.abstractmethod
Eyal Baric4aaee12016-12-20 18:36:46 +020038 def encap_mcast(self, pkt, src_ip, src_mac, vni):
39 """ Encapsulate mcast packet """
40 pass
41
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080042 @abc.abstractmethod
Eyal Baric4aaee12016-12-20 18:36:46 +020043 def encapsulate(self, pkt, vni):
Klement Sekeraf62ae122016-10-11 11:47:09 +020044 """ Encapsulate packet """
Damjan Marionf56b77a2016-10-03 19:44:57 +020045 pass
46
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080047 @abc.abstractmethod
Damjan Marionf56b77a2016-10-03 19:44:57 +020048 def decapsulate(self, pkt):
Klement Sekeraf62ae122016-10-11 11:47:09 +020049 """ Decapsulate packet """
Damjan Marionf56b77a2016-10-03 19:44:57 +020050 pass
51
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080052 @abc.abstractmethod
Eyal Baric4aaee12016-12-20 18:36:46 +020053 def check_encapsulation(self, pkt, vni, local_only=False):
Klement Sekeraf62ae122016-10-11 11:47:09 +020054 """ Verify the encapsulation """
Damjan Marionf56b77a2016-10-03 19:44:57 +020055 pass
56
Eyal Baric4aaee12016-12-20 18:36:46 +020057 def assert_eq_pkts(self, pkt1, pkt2):
58 """ Verify the Ether, IP, UDP, payload are equal in both
59 packets
60 """
61 self.assertEqual(pkt1[Ether].src, pkt2[Ether].src)
62 self.assertEqual(pkt1[Ether].dst, pkt2[Ether].dst)
63 self.assertEqual(pkt1[IP].src, pkt2[IP].src)
64 self.assertEqual(pkt1[IP].dst, pkt2[IP].dst)
65 self.assertEqual(pkt1[UDP].sport, pkt2[UDP].sport)
66 self.assertEqual(pkt1[UDP].dport, pkt2[UDP].dport)
67 self.assertEqual(pkt1[Raw], pkt2[Raw])
68
Damjan Marionf56b77a2016-10-03 19:44:57 +020069 def test_decap(self):
Klement Sekeraf62ae122016-10-11 11:47:09 +020070 """ Decapsulation test
71 Send encapsulated frames from pg0
72 Verify receipt of decapsulated frames on pg1
73 """
Damjan Marionf56b77a2016-10-03 19:44:57 +020074
Eyal Baric4aaee12016-12-20 18:36:46 +020075 encapsulated_pkt = self.encapsulate(self.frame_request,
76 self.single_tunnel_bd)
Damjan Marionf56b77a2016-10-03 19:44:57 +020077
Klement Sekeraf62ae122016-10-11 11:47:09 +020078 self.pg0.add_stream([encapsulated_pkt, ])
Damjan Marionf56b77a2016-10-03 19:44:57 +020079
Klement Sekeraf62ae122016-10-11 11:47:09 +020080 self.pg1.enable_capture()
81
Damjan Marionf56b77a2016-10-03 19:44:57 +020082 self.pg_start()
83
Klement Sekerada505f62017-01-04 12:58:53 +010084 # Pick first received frame and check if it's the non-encapsulated
85 # frame
Klement Sekeradab231a2016-12-21 08:50:14 +010086 out = self.pg1.get_capture(1)
Damjan Marionf56b77a2016-10-03 19:44:57 +020087 pkt = out[0]
Eyal Baric4aaee12016-12-20 18:36:46 +020088 self.assert_eq_pkts(pkt, self.frame_request)
Damjan Marionf56b77a2016-10-03 19:44:57 +020089
Damjan Marionf56b77a2016-10-03 19:44:57 +020090 def test_encap(self):
Klement Sekeraf62ae122016-10-11 11:47:09 +020091 """ Encapsulation test
92 Send frames from pg1
93 Verify receipt of encapsulated frames on pg0
94 """
Eyal Baric4aaee12016-12-20 18:36:46 +020095 self.pg1.add_stream([self.frame_reply])
Damjan Marionf56b77a2016-10-03 19:44:57 +020096
Klement Sekeraf62ae122016-10-11 11:47:09 +020097 self.pg0.enable_capture()
Damjan Marionf56b77a2016-10-03 19:44:57 +020098
Damjan Marionf56b77a2016-10-03 19:44:57 +020099 self.pg_start()
100
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700101 # Pick first received frame and check if it's correctly encapsulated.
Klement Sekeradab231a2016-12-21 08:50:14 +0100102 out = self.pg0.get_capture(1)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200103 pkt = out[0]
Eyal Baric4aaee12016-12-20 18:36:46 +0200104 self.check_encapsulation(pkt, self.single_tunnel_bd)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200105
Klement Sekeraf62ae122016-10-11 11:47:09 +0200106 payload = self.decapsulate(pkt)
Eyal Baric4aaee12016-12-20 18:36:46 +0200107 self.assert_eq_pkts(payload, self.frame_reply)
108
109 def test_ucast_flood(self):
110 """ Unicast flood test
111 Send frames from pg3
112 Verify receipt of encapsulated frames on pg0
113 """
114 self.pg3.add_stream([self.frame_reply])
115
116 self.pg0.enable_capture()
117
118 self.pg_start()
119
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700120 # Get packet from each tunnel and assert it's correctly encapsulated.
Eyal Barid81da8c2017-01-11 13:39:54 +0200121 out = self.pg0.get_capture(self.n_ucast_tunnels)
Eyal Baric4aaee12016-12-20 18:36:46 +0200122 for pkt in out:
123 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
124 payload = self.decapsulate(pkt)
125 self.assert_eq_pkts(payload, self.frame_reply)
126
127 def test_mcast_flood(self):
128 """ Multicast flood test
129 Send frames from pg2
130 Verify receipt of encapsulated frames on pg0
131 """
132 self.pg2.add_stream([self.frame_reply])
133
134 self.pg0.enable_capture()
135
136 self.pg_start()
137
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700138 # Pick first received frame and check if it's correctly encapsulated.
Eyal Baric4aaee12016-12-20 18:36:46 +0200139 out = self.pg0.get_capture(1)
140 pkt = out[0]
Eyal Bari6ae5ee72017-03-23 09:53:51 +0200141 self.check_encapsulation(pkt, self.mcast_flood_bd,
142 local_only=False, mcast_pkt=True)
Eyal Baric4aaee12016-12-20 18:36:46 +0200143
144 payload = self.decapsulate(pkt)
145 self.assert_eq_pkts(payload, self.frame_reply)
146
Eyal Baric4aaee12016-12-20 18:36:46 +0200147 def test_mcast_rcv(self):
148 """ Multicast receive test
149 Send 20 encapsulated frames from pg0 only 10 match unicast tunnels
150 Verify receipt of 10 decap frames on pg2
151 """
152 mac = self.pg0.remote_mac
153 ip_range_start = 10
154 ip_range_end = 30
155 mcast_stream = [
Eyal Barid81da8c2017-01-11 13:39:54 +0200156 self.encap_mcast(self.frame_request, ip, mac, self.mcast_flood_bd)
Eyal Baricef1e2a2018-06-18 13:01:59 +0300157 for ip in self.ip_range(ip_range_start, ip_range_end)]
Eyal Baric4aaee12016-12-20 18:36:46 +0200158 self.pg0.add_stream(mcast_stream)
159 self.pg2.enable_capture()
160 self.pg_start()
161 out = self.pg2.get_capture(10)
162 for pkt in out:
163 self.assert_eq_pkts(pkt, self.frame_request)