Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 3 | from abc import abstractmethod, ABCMeta |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 4 | |
| 5 | from scapy.layers.l2 import Ether, Raw |
| 6 | from scapy.layers.inet import IP, UDP |
| 7 | |
Eyal Bari | d81da8c | 2017-01-11 13:39:54 +0200 | [diff] [blame] | 8 | from util import ip4_range |
| 9 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 10 | |
| 11 | class BridgeDomain(object): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 12 | """ Bridge domain abstraction """ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 13 | __metaclass__ = ABCMeta |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 14 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 15 | @property |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 16 | def frame_request(self): |
| 17 | """ Ethernet frame modeling a generic request """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 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) / |
| 21 | Raw('\xa5' * 100)) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 22 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 23 | @property |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 24 | def frame_reply(self): |
| 25 | """ Ethernet frame modeling a generic reply """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 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) / |
| 29 | Raw('\xa5' * 100)) |
| 30 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 31 | @abstractmethod |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 32 | 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 Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 38 | """ Encapsulate packet """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 39 | pass |
| 40 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 41 | @abstractmethod |
| 42 | def decapsulate(self, pkt): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 43 | """ Decapsulate packet """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 44 | pass |
| 45 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 46 | @abstractmethod |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 47 | def check_encapsulation(self, pkt, vni, local_only=False): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 48 | """ Verify the encapsulation """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 49 | pass |
| 50 | |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 51 | 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 Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 63 | def test_decap(self): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 64 | """ Decapsulation test |
| 65 | Send encapsulated frames from pg0 |
| 66 | Verify receipt of decapsulated frames on pg1 |
| 67 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 68 | |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 69 | encapsulated_pkt = self.encapsulate(self.frame_request, |
| 70 | self.single_tunnel_bd) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 71 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 72 | self.pg0.add_stream([encapsulated_pkt, ]) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 73 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 74 | self.pg1.enable_capture() |
| 75 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 76 | self.pg_start() |
| 77 | |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 78 | # Pick first received frame and check if it's the non-encapsulated |
| 79 | # frame |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 80 | out = self.pg1.get_capture(1) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 81 | pkt = out[0] |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 82 | self.assert_eq_pkts(pkt, self.frame_request) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 83 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 84 | def test_encap(self): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 85 | """ Encapsulation test |
| 86 | Send frames from pg1 |
| 87 | Verify receipt of encapsulated frames on pg0 |
| 88 | """ |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 89 | self.pg1.add_stream([self.frame_reply]) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 90 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 91 | self.pg0.enable_capture() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 92 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 93 | self.pg_start() |
| 94 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 95 | # Pick first received frame and check if it's corectly encapsulated. |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 96 | out = self.pg0.get_capture(1) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 97 | pkt = out[0] |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 98 | self.check_encapsulation(pkt, self.single_tunnel_bd) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 99 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 100 | payload = self.decapsulate(pkt) |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 101 | 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 Bari | d81da8c | 2017-01-11 13:39:54 +0200 | [diff] [blame] | 115 | out = self.pg0.get_capture(self.n_ucast_tunnels) |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 116 | 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 Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 140 | 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 Bari | d81da8c | 2017-01-11 13:39:54 +0200 | [diff] [blame] | 149 | 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 Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 152 | 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) |