blob: ae17135198462cad9b627025240e4384d9557290 [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 Baric4aaee12016-12-20 18:36:46 +020032 def encap_mcast(self, pkt, src_ip, src_mac, vni):
33 """ Encapsulate mcast packet """
34 pass
35
36 @abstractmethod
37 def encapsulate(self, pkt, vni):
Klement Sekeraf62ae122016-10-11 11:47:09 +020038 """ Encapsulate packet """
Damjan Marionf56b77a2016-10-03 19:44:57 +020039 pass
40
Damjan Marionf56b77a2016-10-03 19:44:57 +020041 @abstractmethod
42 def decapsulate(self, pkt):
Klement Sekeraf62ae122016-10-11 11:47:09 +020043 """ Decapsulate packet """
Damjan Marionf56b77a2016-10-03 19:44:57 +020044 pass
45
Damjan Marionf56b77a2016-10-03 19:44:57 +020046 @abstractmethod
Eyal Baric4aaee12016-12-20 18:36:46 +020047 def check_encapsulation(self, pkt, vni, local_only=False):
Klement Sekeraf62ae122016-10-11 11:47:09 +020048 """ Verify the encapsulation """
Damjan Marionf56b77a2016-10-03 19:44:57 +020049 pass
50
Eyal Baric4aaee12016-12-20 18:36:46 +020051 def assert_eq_pkts(self, pkt1, pkt2):
52 """ Verify the Ether, IP, UDP, payload are equal in both
53 packets
54 """
55 self.assertEqual(pkt1[Ether].src, pkt2[Ether].src)
56 self.assertEqual(pkt1[Ether].dst, pkt2[Ether].dst)
57 self.assertEqual(pkt1[IP].src, pkt2[IP].src)
58 self.assertEqual(pkt1[IP].dst, pkt2[IP].dst)
59 self.assertEqual(pkt1[UDP].sport, pkt2[UDP].sport)
60 self.assertEqual(pkt1[UDP].dport, pkt2[UDP].dport)
61 self.assertEqual(pkt1[Raw], pkt2[Raw])
62
Damjan Marionf56b77a2016-10-03 19:44:57 +020063 def test_decap(self):
Klement Sekeraf62ae122016-10-11 11:47:09 +020064 """ Decapsulation test
65 Send encapsulated frames from pg0
66 Verify receipt of decapsulated frames on pg1
67 """
Damjan Marionf56b77a2016-10-03 19:44:57 +020068
Eyal Baric4aaee12016-12-20 18:36:46 +020069 encapsulated_pkt = self.encapsulate(self.frame_request,
70 self.single_tunnel_bd)
Damjan Marionf56b77a2016-10-03 19:44:57 +020071
Klement Sekeraf62ae122016-10-11 11:47:09 +020072 self.pg0.add_stream([encapsulated_pkt, ])
Damjan Marionf56b77a2016-10-03 19:44:57 +020073
Klement Sekeraf62ae122016-10-11 11:47:09 +020074 self.pg1.enable_capture()
75
Damjan Marionf56b77a2016-10-03 19:44:57 +020076 self.pg_start()
77
Klement Sekerada505f62017-01-04 12:58:53 +010078 # Pick first received frame and check if it's the non-encapsulated
79 # frame
Klement Sekeradab231a2016-12-21 08:50:14 +010080 out = self.pg1.get_capture(1)
Damjan Marionf56b77a2016-10-03 19:44:57 +020081 pkt = out[0]
Eyal Baric4aaee12016-12-20 18:36:46 +020082 self.assert_eq_pkts(pkt, self.frame_request)
Damjan Marionf56b77a2016-10-03 19:44:57 +020083
Damjan Marionf56b77a2016-10-03 19:44:57 +020084 def test_encap(self):
Klement Sekeraf62ae122016-10-11 11:47:09 +020085 """ Encapsulation test
86 Send frames from pg1
87 Verify receipt of encapsulated frames on pg0
88 """
Eyal Baric4aaee12016-12-20 18:36:46 +020089 self.pg1.add_stream([self.frame_reply])
Damjan Marionf56b77a2016-10-03 19:44:57 +020090
Klement Sekeraf62ae122016-10-11 11:47:09 +020091 self.pg0.enable_capture()
Damjan Marionf56b77a2016-10-03 19:44:57 +020092
Damjan Marionf56b77a2016-10-03 19:44:57 +020093 self.pg_start()
94
Klement Sekeraf62ae122016-10-11 11:47:09 +020095 # Pick first received frame and check if it's corectly encapsulated.
Klement Sekeradab231a2016-12-21 08:50:14 +010096 out = self.pg0.get_capture(1)
Klement Sekeraf62ae122016-10-11 11:47:09 +020097 pkt = out[0]
Eyal Baric4aaee12016-12-20 18:36:46 +020098 self.check_encapsulation(pkt, self.single_tunnel_bd)
Damjan Marionf56b77a2016-10-03 19:44:57 +020099
Klement Sekeraf62ae122016-10-11 11:47:09 +0200100 payload = self.decapsulate(pkt)
Eyal Baric4aaee12016-12-20 18:36:46 +0200101 self.assert_eq_pkts(payload, self.frame_reply)
102
103 def test_ucast_flood(self):
104 """ Unicast flood test
105 Send frames from pg3
106 Verify receipt of encapsulated frames on pg0
107 """
108 self.pg3.add_stream([self.frame_reply])
109
110 self.pg0.enable_capture()
111
112 self.pg_start()
113
114 # Get packet from each tunnel and assert it's corectly encapsulated.
Eyal Barid81da8c2017-01-11 13:39:54 +0200115 out = self.pg0.get_capture(self.n_ucast_tunnels)
Eyal Baric4aaee12016-12-20 18:36:46 +0200116 for pkt in out:
117 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
118 payload = self.decapsulate(pkt)
119 self.assert_eq_pkts(payload, self.frame_reply)
120
121 def test_mcast_flood(self):
122 """ Multicast flood test
123 Send frames from pg2
124 Verify receipt of encapsulated frames on pg0
125 """
126 self.pg2.add_stream([self.frame_reply])
127
128 self.pg0.enable_capture()
129
130 self.pg_start()
131
132 # Pick first received frame and check if it's corectly encapsulated.
133 out = self.pg0.get_capture(1)
134 pkt = out[0]
135 self.check_encapsulation(pkt, self.mcast_flood_bd, True)
136
137 payload = self.decapsulate(pkt)
138 self.assert_eq_pkts(payload, self.frame_reply)
139
Eyal Baric4aaee12016-12-20 18:36:46 +0200140 def test_mcast_rcv(self):
141 """ Multicast receive test
142 Send 20 encapsulated frames from pg0 only 10 match unicast tunnels
143 Verify receipt of 10 decap frames on pg2
144 """
145 mac = self.pg0.remote_mac
146 ip_range_start = 10
147 ip_range_end = 30
148 mcast_stream = [
Eyal Barid81da8c2017-01-11 13:39:54 +0200149 self.encap_mcast(self.frame_request, ip, mac, self.mcast_flood_bd)
150 for ip in ip4_range(self.pg0.remote_ip4,
151 ip_range_start, ip_range_end)]
Eyal Baric4aaee12016-12-20 18:36:46 +0200152 self.pg0.add_stream(mcast_stream)
153 self.pg2.enable_capture()
154 self.pg_start()
155 out = self.pg2.get_capture(10)
156 for pkt in out:
157 self.assert_eq_pkts(pkt, self.frame_request)