blob: 291f4684db9b332697f712ca93e8105aa49f5f79 [file] [log] [blame]
Damjan Marionf56b77a2016-10-03 19:44:57 +02001#!/usr/bin/env python
2
Klement Sekera0e3c0de2016-09-29 14:43:44 +02003from abc import abstractmethod, ABCMeta
Damjan Marionf56b77a2016-10-03 19:44:57 +02004
5from scapy.layers.l2 import Ether, Raw
6from scapy.layers.inet import IP, UDP
7
Eyal Barid81da8c2017-01-11 13:39:54 +02008from util import ip4_range
9
Damjan Marionf56b77a2016-10-03 19:44:57 +020010
11class BridgeDomain(object):
Klement Sekeraf62ae122016-10-11 11:47:09 +020012 """ Bridge domain abstraction """
Klement Sekera0e3c0de2016-09-29 14:43:44 +020013 __metaclass__ = ABCMeta
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):
17 """ Ethernet frame modeling a generic request """
Klement Sekeraf62ae122016-10-11 11:47:09 +020018 return (Ether(src='00:00:00:00:00:01', dst='00:00:00:00:00:02') /
19 IP(src='1.2.3.4', dst='4.3.2.1') /
20 UDP(sport=10000, dport=20000) /
21 Raw('\xa5' * 100))
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):
25 """ Ethernet frame modeling a generic reply """
Klement Sekeraf62ae122016-10-11 11:47:09 +020026 return (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
27 IP(src='4.3.2.1', dst='1.2.3.4') /
28 UDP(sport=20000, dport=10000) /
29 Raw('\xa5' * 100))
30
Damjan Marionf56b77a2016-10-03 19:44:57 +020031 @abstractmethod
Eyal Baricef1e2a2018-06-18 13:01:59 +030032 def ip_range(self, start, end):
33 """ range of remote ip's """
34 pass
35
36 @abstractmethod
Eyal Baric4aaee12016-12-20 18:36:46 +020037 def encap_mcast(self, pkt, src_ip, src_mac, vni):
38 """ Encapsulate mcast packet """
39 pass
40
41 @abstractmethod
42 def encapsulate(self, pkt, vni):
Klement Sekeraf62ae122016-10-11 11:47:09 +020043 """ Encapsulate packet """
Damjan Marionf56b77a2016-10-03 19:44:57 +020044 pass
45
Damjan Marionf56b77a2016-10-03 19:44:57 +020046 @abstractmethod
47 def decapsulate(self, pkt):
Klement Sekeraf62ae122016-10-11 11:47:09 +020048 """ Decapsulate packet """
Damjan Marionf56b77a2016-10-03 19:44:57 +020049 pass
50
Damjan Marionf56b77a2016-10-03 19:44:57 +020051 @abstractmethod
Eyal Baric4aaee12016-12-20 18:36:46 +020052 def check_encapsulation(self, pkt, vni, local_only=False):
Klement Sekeraf62ae122016-10-11 11:47:09 +020053 """ Verify the encapsulation """
Damjan Marionf56b77a2016-10-03 19:44:57 +020054 pass
55
Eyal Baric4aaee12016-12-20 18:36:46 +020056 def assert_eq_pkts(self, pkt1, pkt2):
57 """ Verify the Ether, IP, UDP, payload are equal in both
58 packets
59 """
60 self.assertEqual(pkt1[Ether].src, pkt2[Ether].src)
61 self.assertEqual(pkt1[Ether].dst, pkt2[Ether].dst)
62 self.assertEqual(pkt1[IP].src, pkt2[IP].src)
63 self.assertEqual(pkt1[IP].dst, pkt2[IP].dst)
64 self.assertEqual(pkt1[UDP].sport, pkt2[UDP].sport)
65 self.assertEqual(pkt1[UDP].dport, pkt2[UDP].dport)
66 self.assertEqual(pkt1[Raw], pkt2[Raw])
67
Damjan Marionf56b77a2016-10-03 19:44:57 +020068 def test_decap(self):
Klement Sekeraf62ae122016-10-11 11:47:09 +020069 """ Decapsulation test
70 Send encapsulated frames from pg0
71 Verify receipt of decapsulated frames on pg1
72 """
Damjan Marionf56b77a2016-10-03 19:44:57 +020073
Eyal Baric4aaee12016-12-20 18:36:46 +020074 encapsulated_pkt = self.encapsulate(self.frame_request,
75 self.single_tunnel_bd)
Damjan Marionf56b77a2016-10-03 19:44:57 +020076
Klement Sekeraf62ae122016-10-11 11:47:09 +020077 self.pg0.add_stream([encapsulated_pkt, ])
Damjan Marionf56b77a2016-10-03 19:44:57 +020078
Klement Sekeraf62ae122016-10-11 11:47:09 +020079 self.pg1.enable_capture()
80
Damjan Marionf56b77a2016-10-03 19:44:57 +020081 self.pg_start()
82
Klement Sekerada505f62017-01-04 12:58:53 +010083 # Pick first received frame and check if it's the non-encapsulated
84 # frame
Klement Sekeradab231a2016-12-21 08:50:14 +010085 out = self.pg1.get_capture(1)
Damjan Marionf56b77a2016-10-03 19:44:57 +020086 pkt = out[0]
Eyal Baric4aaee12016-12-20 18:36:46 +020087 self.assert_eq_pkts(pkt, self.frame_request)
Damjan Marionf56b77a2016-10-03 19:44:57 +020088
Damjan Marionf56b77a2016-10-03 19:44:57 +020089 def test_encap(self):
Klement Sekeraf62ae122016-10-11 11:47:09 +020090 """ Encapsulation test
91 Send frames from pg1
92 Verify receipt of encapsulated frames on pg0
93 """
Eyal Baric4aaee12016-12-20 18:36:46 +020094 self.pg1.add_stream([self.frame_reply])
Damjan Marionf56b77a2016-10-03 19:44:57 +020095
Klement Sekeraf62ae122016-10-11 11:47:09 +020096 self.pg0.enable_capture()
Damjan Marionf56b77a2016-10-03 19:44:57 +020097
Damjan Marionf56b77a2016-10-03 19:44:57 +020098 self.pg_start()
99
Klement Sekeraf62ae122016-10-11 11:47:09 +0200100 # Pick first received frame and check if it's corectly encapsulated.
Klement Sekeradab231a2016-12-21 08:50:14 +0100101 out = self.pg0.get_capture(1)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200102 pkt = out[0]
Eyal Baric4aaee12016-12-20 18:36:46 +0200103 self.check_encapsulation(pkt, self.single_tunnel_bd)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200104
Klement Sekeraf62ae122016-10-11 11:47:09 +0200105 payload = self.decapsulate(pkt)
Eyal Baric4aaee12016-12-20 18:36:46 +0200106 self.assert_eq_pkts(payload, self.frame_reply)
107
108 def test_ucast_flood(self):
109 """ Unicast flood test
110 Send frames from pg3
111 Verify receipt of encapsulated frames on pg0
112 """
113 self.pg3.add_stream([self.frame_reply])
114
115 self.pg0.enable_capture()
116
117 self.pg_start()
118
119 # Get packet from each tunnel and assert it's corectly encapsulated.
Eyal Barid81da8c2017-01-11 13:39:54 +0200120 out = self.pg0.get_capture(self.n_ucast_tunnels)
Eyal Baric4aaee12016-12-20 18:36:46 +0200121 for pkt in out:
122 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
123 payload = self.decapsulate(pkt)
124 self.assert_eq_pkts(payload, self.frame_reply)
125
126 def test_mcast_flood(self):
127 """ Multicast flood test
128 Send frames from pg2
129 Verify receipt of encapsulated frames on pg0
130 """
131 self.pg2.add_stream([self.frame_reply])
132
133 self.pg0.enable_capture()
134
135 self.pg_start()
136
137 # Pick first received frame and check if it's corectly encapsulated.
138 out = self.pg0.get_capture(1)
139 pkt = out[0]
Eyal Bari6ae5ee72017-03-23 09:53:51 +0200140 self.check_encapsulation(pkt, self.mcast_flood_bd,
141 local_only=False, mcast_pkt=True)
Eyal Baric4aaee12016-12-20 18:36:46 +0200142
143 payload = self.decapsulate(pkt)
144 self.assert_eq_pkts(payload, self.frame_reply)
145
Eyal Baric4aaee12016-12-20 18:36:46 +0200146 def test_mcast_rcv(self):
147 """ Multicast receive test
148 Send 20 encapsulated frames from pg0 only 10 match unicast tunnels
149 Verify receipt of 10 decap frames on pg2
150 """
151 mac = self.pg0.remote_mac
152 ip_range_start = 10
153 ip_range_end = 30
154 mcast_stream = [
Eyal Barid81da8c2017-01-11 13:39:54 +0200155 self.encap_mcast(self.frame_request, ip, mac, self.mcast_flood_bd)
Eyal Baricef1e2a2018-06-18 13:01:59 +0300156 for ip in self.ip_range(ip_range_start, ip_range_end)]
Eyal Baric4aaee12016-12-20 18:36:46 +0200157 self.pg0.add_stream(mcast_stream)
158 self.pg2.enable_capture()
159 self.pg_start()
160 out = self.pg2.get_capture(10)
161 for pkt in out:
162 self.assert_eq_pkts(pkt, self.frame_request)