2016-10-03 19:44:57 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
import abc
|
|
|
|
import six
|
2016-10-03 19:44:57 +02:00
|
|
|
|
|
|
|
from scapy.layers.l2 import Ether, Raw
|
|
|
|
from scapy.layers.inet import IP, UDP
|
|
|
|
|
2017-01-11 13:39:54 +02:00
|
|
|
from util import ip4_range
|
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
@six.add_metaclass(abc.ABCMeta)
|
2016-10-03 19:44:57 +02:00
|
|
|
class BridgeDomain(object):
|
2016-10-11 11:47:09 +02:00
|
|
|
""" Bridge domain abstraction """
|
|
|
|
|
|
|
|
@property
|
2016-12-20 18:36:46 +02:00
|
|
|
def frame_request(self):
|
|
|
|
""" Ethernet frame modeling a generic request """
|
2016-10-11 11:47:09 +02:00
|
|
|
return (Ether(src='00:00:00:00:00:01', dst='00:00:00:00:00:02') /
|
|
|
|
IP(src='1.2.3.4', dst='4.3.2.1') /
|
|
|
|
UDP(sport=10000, dport=20000) /
|
|
|
|
Raw('\xa5' * 100))
|
|
|
|
|
|
|
|
@property
|
2016-12-20 18:36:46 +02:00
|
|
|
def frame_reply(self):
|
|
|
|
""" Ethernet frame modeling a generic reply """
|
2016-10-11 11:47:09 +02:00
|
|
|
return (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
|
|
|
|
IP(src='4.3.2.1', dst='1.2.3.4') /
|
|
|
|
UDP(sport=20000, dport=10000) /
|
|
|
|
Raw('\xa5' * 100))
|
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
@abc.abstractmethod
|
2018-06-18 13:01:59 +03:00
|
|
|
def ip_range(self, start, end):
|
|
|
|
""" range of remote ip's """
|
|
|
|
pass
|
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
@abc.abstractmethod
|
2016-12-20 18:36:46 +02:00
|
|
|
def encap_mcast(self, pkt, src_ip, src_mac, vni):
|
|
|
|
""" Encapsulate mcast packet """
|
|
|
|
pass
|
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
@abc.abstractmethod
|
2016-12-20 18:36:46 +02:00
|
|
|
def encapsulate(self, pkt, vni):
|
2016-10-11 11:47:09 +02:00
|
|
|
""" Encapsulate packet """
|
2016-10-03 19:44:57 +02:00
|
|
|
pass
|
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
@abc.abstractmethod
|
2016-10-03 19:44:57 +02:00
|
|
|
def decapsulate(self, pkt):
|
2016-10-11 11:47:09 +02:00
|
|
|
""" Decapsulate packet """
|
2016-10-03 19:44:57 +02:00
|
|
|
pass
|
|
|
|
|
2018-11-24 21:46:05 -08:00
|
|
|
@abc.abstractmethod
|
2016-12-20 18:36:46 +02:00
|
|
|
def check_encapsulation(self, pkt, vni, local_only=False):
|
2016-10-11 11:47:09 +02:00
|
|
|
""" Verify the encapsulation """
|
2016-10-03 19:44:57 +02:00
|
|
|
pass
|
|
|
|
|
2016-12-20 18:36:46 +02:00
|
|
|
def assert_eq_pkts(self, pkt1, pkt2):
|
|
|
|
""" Verify the Ether, IP, UDP, payload are equal in both
|
|
|
|
packets
|
|
|
|
"""
|
|
|
|
self.assertEqual(pkt1[Ether].src, pkt2[Ether].src)
|
|
|
|
self.assertEqual(pkt1[Ether].dst, pkt2[Ether].dst)
|
|
|
|
self.assertEqual(pkt1[IP].src, pkt2[IP].src)
|
|
|
|
self.assertEqual(pkt1[IP].dst, pkt2[IP].dst)
|
|
|
|
self.assertEqual(pkt1[UDP].sport, pkt2[UDP].sport)
|
|
|
|
self.assertEqual(pkt1[UDP].dport, pkt2[UDP].dport)
|
|
|
|
self.assertEqual(pkt1[Raw], pkt2[Raw])
|
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
def test_decap(self):
|
2016-10-11 11:47:09 +02:00
|
|
|
""" Decapsulation test
|
|
|
|
Send encapsulated frames from pg0
|
|
|
|
Verify receipt of decapsulated frames on pg1
|
|
|
|
"""
|
|
|
|
|
2016-12-20 18:36:46 +02:00
|
|
|
encapsulated_pkt = self.encapsulate(self.frame_request,
|
|
|
|
self.single_tunnel_bd)
|
2016-10-03 19:44:57 +02:00
|
|
|
|
2016-10-11 11:47:09 +02:00
|
|
|
self.pg0.add_stream([encapsulated_pkt, ])
|
2016-10-03 19:44:57 +02:00
|
|
|
|
2016-10-11 11:47:09 +02:00
|
|
|
self.pg1.enable_capture()
|
2016-10-03 19:44:57 +02:00
|
|
|
|
|
|
|
self.pg_start()
|
|
|
|
|
2017-01-04 12:58:53 +01:00
|
|
|
# Pick first received frame and check if it's the non-encapsulated
|
|
|
|
# frame
|
2016-12-21 08:50:14 +01:00
|
|
|
out = self.pg1.get_capture(1)
|
2016-10-03 19:44:57 +02:00
|
|
|
pkt = out[0]
|
2016-12-20 18:36:46 +02:00
|
|
|
self.assert_eq_pkts(pkt, self.frame_request)
|
2016-10-11 11:47:09 +02:00
|
|
|
|
2016-10-03 19:44:57 +02:00
|
|
|
def test_encap(self):
|
2016-10-11 11:47:09 +02:00
|
|
|
""" Encapsulation test
|
|
|
|
Send frames from pg1
|
|
|
|
Verify receipt of encapsulated frames on pg0
|
|
|
|
"""
|
2016-12-20 18:36:46 +02:00
|
|
|
self.pg1.add_stream([self.frame_reply])
|
2016-10-03 19:44:57 +02:00
|
|
|
|
2016-10-11 11:47:09 +02:00
|
|
|
self.pg0.enable_capture()
|
2016-10-03 19:44:57 +02:00
|
|
|
|
|
|
|
self.pg_start()
|
|
|
|
|
2019-03-27 11:25:48 -07:00
|
|
|
# Pick first received frame and check if it's correctly encapsulated.
|
2016-12-21 08:50:14 +01:00
|
|
|
out = self.pg0.get_capture(1)
|
2016-10-11 11:47:09 +02:00
|
|
|
pkt = out[0]
|
2016-12-20 18:36:46 +02:00
|
|
|
self.check_encapsulation(pkt, self.single_tunnel_bd)
|
2016-10-03 19:44:57 +02:00
|
|
|
|
2016-10-11 11:47:09 +02:00
|
|
|
payload = self.decapsulate(pkt)
|
2016-12-20 18:36:46 +02:00
|
|
|
self.assert_eq_pkts(payload, self.frame_reply)
|
|
|
|
|
|
|
|
def test_ucast_flood(self):
|
|
|
|
""" Unicast flood test
|
|
|
|
Send frames from pg3
|
|
|
|
Verify receipt of encapsulated frames on pg0
|
|
|
|
"""
|
|
|
|
self.pg3.add_stream([self.frame_reply])
|
|
|
|
|
|
|
|
self.pg0.enable_capture()
|
|
|
|
|
|
|
|
self.pg_start()
|
|
|
|
|
2019-03-27 11:25:48 -07:00
|
|
|
# Get packet from each tunnel and assert it's correctly encapsulated.
|
2017-01-11 13:39:54 +02:00
|
|
|
out = self.pg0.get_capture(self.n_ucast_tunnels)
|
2016-12-20 18:36:46 +02:00
|
|
|
for pkt in out:
|
|
|
|
self.check_encapsulation(pkt, self.ucast_flood_bd, True)
|
|
|
|
payload = self.decapsulate(pkt)
|
|
|
|
self.assert_eq_pkts(payload, self.frame_reply)
|
|
|
|
|
|
|
|
def test_mcast_flood(self):
|
|
|
|
""" Multicast flood test
|
|
|
|
Send frames from pg2
|
|
|
|
Verify receipt of encapsulated frames on pg0
|
|
|
|
"""
|
|
|
|
self.pg2.add_stream([self.frame_reply])
|
|
|
|
|
|
|
|
self.pg0.enable_capture()
|
|
|
|
|
|
|
|
self.pg_start()
|
|
|
|
|
2019-03-27 11:25:48 -07:00
|
|
|
# Pick first received frame and check if it's correctly encapsulated.
|
2016-12-20 18:36:46 +02:00
|
|
|
out = self.pg0.get_capture(1)
|
|
|
|
pkt = out[0]
|
2017-03-23 09:53:51 +02:00
|
|
|
self.check_encapsulation(pkt, self.mcast_flood_bd,
|
|
|
|
local_only=False, mcast_pkt=True)
|
2016-12-20 18:36:46 +02:00
|
|
|
|
|
|
|
payload = self.decapsulate(pkt)
|
|
|
|
self.assert_eq_pkts(payload, self.frame_reply)
|
|
|
|
|
|
|
|
def test_mcast_rcv(self):
|
|
|
|
""" Multicast receive test
|
|
|
|
Send 20 encapsulated frames from pg0 only 10 match unicast tunnels
|
|
|
|
Verify receipt of 10 decap frames on pg2
|
|
|
|
"""
|
|
|
|
mac = self.pg0.remote_mac
|
|
|
|
ip_range_start = 10
|
|
|
|
ip_range_end = 30
|
|
|
|
mcast_stream = [
|
2017-01-11 13:39:54 +02:00
|
|
|
self.encap_mcast(self.frame_request, ip, mac, self.mcast_flood_bd)
|
2018-06-18 13:01:59 +03:00
|
|
|
for ip in self.ip_range(ip_range_start, ip_range_end)]
|
2016-12-20 18:36:46 +02:00
|
|
|
self.pg0.add_stream(mcast_stream)
|
|
|
|
self.pg2.enable_capture()
|
|
|
|
self.pg_start()
|
|
|
|
out = self.pg2.get_capture(10)
|
|
|
|
for pkt in out:
|
|
|
|
self.assert_eq_pkts(pkt, self.frame_request)
|