3 from abc import abstractmethod, ABCMeta
5 from scapy.layers.l2 import Ether, Raw
6 from scapy.layers.inet import IP, UDP
8 from util import ip4_range
11 class BridgeDomain(object):
12 """ Bridge domain abstraction """
13 __metaclass__ = ABCMeta
16 def frame_request(self):
17 """ Ethernet frame modeling a generic request """
18 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) /
24 def frame_reply(self):
25 """ Ethernet frame modeling a generic reply """
26 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) /
32 def encap_mcast(self, pkt, src_ip, src_mac, vni):
33 """ Encapsulate mcast packet """
37 def encapsulate(self, pkt, vni):
38 """ Encapsulate packet """
42 def decapsulate(self, pkt):
43 """ Decapsulate packet """
47 def check_encapsulation(self, pkt, vni, local_only=False):
48 """ Verify the encapsulation """
51 def assert_eq_pkts(self, pkt1, pkt2):
52 """ Verify the Ether, IP, UDP, payload are equal in both
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])
64 """ Decapsulation test
65 Send encapsulated frames from pg0
66 Verify receipt of decapsulated frames on pg1
69 encapsulated_pkt = self.encapsulate(self.frame_request,
70 self.single_tunnel_bd)
72 self.pg0.add_stream([encapsulated_pkt, ])
74 self.pg1.enable_capture()
78 # Pick first received frame and check if it's the non-encapsulated
80 out = self.pg1.get_capture(1)
82 self.assert_eq_pkts(pkt, self.frame_request)
85 """ Encapsulation test
87 Verify receipt of encapsulated frames on pg0
89 self.pg1.add_stream([self.frame_reply])
91 self.pg0.enable_capture()
95 # Pick first received frame and check if it's corectly encapsulated.
96 out = self.pg0.get_capture(1)
98 self.check_encapsulation(pkt, self.single_tunnel_bd)
100 payload = self.decapsulate(pkt)
101 self.assert_eq_pkts(payload, self.frame_reply)
103 def test_ucast_flood(self):
104 """ Unicast flood test
106 Verify receipt of encapsulated frames on pg0
108 self.pg3.add_stream([self.frame_reply])
110 self.pg0.enable_capture()
114 # Get packet from each tunnel and assert it's corectly encapsulated.
115 out = self.pg0.get_capture(self.n_ucast_tunnels)
117 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
118 payload = self.decapsulate(pkt)
119 self.assert_eq_pkts(payload, self.frame_reply)
121 def test_mcast_flood(self):
122 """ Multicast flood test
124 Verify receipt of encapsulated frames on pg0
126 self.pg2.add_stream([self.frame_reply])
128 self.pg0.enable_capture()
132 # Pick first received frame and check if it's corectly encapsulated.
133 out = self.pg0.get_capture(1)
135 self.check_encapsulation(pkt, self.mcast_flood_bd,
136 local_only=False, mcast_pkt=True)
138 payload = self.decapsulate(pkt)
139 self.assert_eq_pkts(payload, self.frame_reply)
141 def test_mcast_rcv(self):
142 """ Multicast receive test
143 Send 20 encapsulated frames from pg0 only 10 match unicast tunnels
144 Verify receipt of 10 decap frames on pg2
146 mac = self.pg0.remote_mac
150 self.encap_mcast(self.frame_request, ip, mac, self.mcast_flood_bd)
151 for ip in ip4_range(self.pg0.remote_ip4,
152 ip_range_start, ip_range_end)]
153 self.pg0.add_stream(mcast_stream)
154 self.pg2.enable_capture()
156 out = self.pg2.get_capture(10)
158 self.assert_eq_pkts(pkt, self.frame_request)