Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 2 | |
Paul Vinciguerra | 3bce8eb | 2018-11-24 21:46:05 -0800 | [diff] [blame] | 3 | import abc |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 4 | |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 5 | from scapy.layers.l2 import Ether |
| 6 | from scapy.packet import Raw |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 7 | from scapy.layers.inet import IP, UDP |
| 8 | |
Eyal Bari | d81da8c | 2017-01-11 13:39:54 +0200 | [diff] [blame] | 9 | |
Paul Vinciguerra | 090096b | 2020-12-03 00:42:46 -0500 | [diff] [blame] | 10 | class BridgeDomain(metaclass=abc.ABCMeta): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 11 | """ Bridge domain abstraction """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 12 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 13 | @property |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 14 | def frame_request(self): |
| 15 | """ Ethernet frame modeling a generic request """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 16 | return (Ether(src='00:00:00:00:00:01', dst='00:00:00:00:00:02') / |
| 17 | IP(src='1.2.3.4', dst='4.3.2.1') / |
| 18 | UDP(sport=10000, dport=20000) / |
| 19 | Raw('\xa5' * 100)) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 20 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 21 | @property |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 22 | def frame_reply(self): |
| 23 | """ Ethernet frame modeling a generic reply """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 24 | return (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') / |
| 25 | IP(src='4.3.2.1', dst='1.2.3.4') / |
| 26 | UDP(sport=20000, dport=10000) / |
| 27 | Raw('\xa5' * 100)) |
| 28 | |
Paul Vinciguerra | 3bce8eb | 2018-11-24 21:46:05 -0800 | [diff] [blame] | 29 | @abc.abstractmethod |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 30 | def ip_range(self, start, end): |
| 31 | """ range of remote ip's """ |
| 32 | pass |
| 33 | |
Paul Vinciguerra | 3bce8eb | 2018-11-24 21:46:05 -0800 | [diff] [blame] | 34 | @abc.abstractmethod |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 35 | def encap_mcast(self, pkt, src_ip, src_mac, vni): |
| 36 | """ Encapsulate mcast packet """ |
| 37 | pass |
| 38 | |
Paul Vinciguerra | 3bce8eb | 2018-11-24 21:46:05 -0800 | [diff] [blame] | 39 | @abc.abstractmethod |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 40 | def encapsulate(self, pkt, vni): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 41 | """ Encapsulate packet """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 42 | pass |
| 43 | |
Paul Vinciguerra | 3bce8eb | 2018-11-24 21:46:05 -0800 | [diff] [blame] | 44 | @abc.abstractmethod |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 45 | def decapsulate(self, pkt): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 46 | """ Decapsulate packet """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 47 | pass |
| 48 | |
Paul Vinciguerra | 3bce8eb | 2018-11-24 21:46:05 -0800 | [diff] [blame] | 49 | @abc.abstractmethod |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 50 | def check_encapsulation(self, pkt, vni, local_only=False): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 51 | """ Verify the encapsulation """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 52 | pass |
| 53 | |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 54 | def assert_eq_pkts(self, pkt1, pkt2): |
| 55 | """ Verify the Ether, IP, UDP, payload are equal in both |
| 56 | packets |
| 57 | """ |
| 58 | self.assertEqual(pkt1[Ether].src, pkt2[Ether].src) |
| 59 | self.assertEqual(pkt1[Ether].dst, pkt2[Ether].dst) |
| 60 | self.assertEqual(pkt1[IP].src, pkt2[IP].src) |
| 61 | self.assertEqual(pkt1[IP].dst, pkt2[IP].dst) |
| 62 | self.assertEqual(pkt1[UDP].sport, pkt2[UDP].sport) |
| 63 | self.assertEqual(pkt1[UDP].dport, pkt2[UDP].dport) |
| 64 | self.assertEqual(pkt1[Raw], pkt2[Raw]) |
| 65 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 66 | def test_decap(self): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 67 | """ Decapsulation test |
| 68 | Send encapsulated frames from pg0 |
| 69 | Verify receipt of decapsulated frames on pg1 |
| 70 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 71 | |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 72 | encapsulated_pkt = self.encapsulate(self.frame_request, |
Neale Ranns | 91fd910 | 2020-04-03 07:46:28 +0000 | [diff] [blame] | 73 | self.single_tunnel_vni) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 74 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 75 | self.pg0.add_stream([encapsulated_pkt, ]) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 76 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 77 | self.pg1.enable_capture() |
| 78 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 79 | self.pg_start() |
| 80 | |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 81 | # Pick first received frame and check if it's the non-encapsulated |
| 82 | # frame |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 83 | out = self.pg1.get_capture(1) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 84 | pkt = out[0] |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 85 | self.assert_eq_pkts(pkt, self.frame_request) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 86 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 87 | def test_encap(self): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 88 | """ Encapsulation test |
| 89 | Send frames from pg1 |
| 90 | Verify receipt of encapsulated frames on pg0 |
| 91 | """ |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 92 | self.pg1.add_stream([self.frame_reply]) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 93 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 94 | self.pg0.enable_capture() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 95 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 96 | self.pg_start() |
| 97 | |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 98 | # Pick first received frame and check if it's correctly encapsulated. |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 99 | out = self.pg0.get_capture(1) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 100 | pkt = out[0] |
Neale Ranns | 91fd910 | 2020-04-03 07:46:28 +0000 | [diff] [blame] | 101 | self.check_encapsulation(pkt, self.single_tunnel_vni) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 102 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 103 | payload = self.decapsulate(pkt) |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 104 | self.assert_eq_pkts(payload, self.frame_reply) |
| 105 | |
| 106 | def test_ucast_flood(self): |
| 107 | """ Unicast flood test |
| 108 | Send frames from pg3 |
| 109 | Verify receipt of encapsulated frames on pg0 |
| 110 | """ |
| 111 | self.pg3.add_stream([self.frame_reply]) |
| 112 | |
| 113 | self.pg0.enable_capture() |
| 114 | |
| 115 | self.pg_start() |
| 116 | |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 117 | # Get packet from each tunnel and assert it's correctly encapsulated. |
Eyal Bari | d81da8c | 2017-01-11 13:39:54 +0200 | [diff] [blame] | 118 | out = self.pg0.get_capture(self.n_ucast_tunnels) |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 119 | for pkt in out: |
| 120 | self.check_encapsulation(pkt, self.ucast_flood_bd, True) |
| 121 | payload = self.decapsulate(pkt) |
| 122 | self.assert_eq_pkts(payload, self.frame_reply) |
| 123 | |
| 124 | def test_mcast_flood(self): |
| 125 | """ Multicast flood test |
| 126 | Send frames from pg2 |
| 127 | Verify receipt of encapsulated frames on pg0 |
| 128 | """ |
| 129 | self.pg2.add_stream([self.frame_reply]) |
| 130 | |
| 131 | self.pg0.enable_capture() |
| 132 | |
| 133 | self.pg_start() |
| 134 | |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 135 | # Pick first received frame and check if it's correctly encapsulated. |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 136 | out = self.pg0.get_capture(1) |
| 137 | pkt = out[0] |
Eyal Bari | 6ae5ee7 | 2017-03-23 09:53:51 +0200 | [diff] [blame] | 138 | self.check_encapsulation(pkt, self.mcast_flood_bd, |
| 139 | local_only=False, mcast_pkt=True) |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 140 | |
| 141 | payload = self.decapsulate(pkt) |
| 142 | self.assert_eq_pkts(payload, self.frame_reply) |
| 143 | |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 144 | def test_mcast_rcv(self): |
| 145 | """ Multicast receive test |
| 146 | Send 20 encapsulated frames from pg0 only 10 match unicast tunnels |
| 147 | Verify receipt of 10 decap frames on pg2 |
| 148 | """ |
| 149 | mac = self.pg0.remote_mac |
| 150 | ip_range_start = 10 |
| 151 | ip_range_end = 30 |
| 152 | mcast_stream = [ |
Eyal Bari | d81da8c | 2017-01-11 13:39:54 +0200 | [diff] [blame] | 153 | self.encap_mcast(self.frame_request, ip, mac, self.mcast_flood_bd) |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 154 | for ip in self.ip_range(ip_range_start, ip_range_end)] |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 155 | self.pg0.add_stream(mcast_stream) |
| 156 | self.pg2.enable_capture() |
| 157 | self.pg_start() |
| 158 | out = self.pg2.get_capture(10) |
| 159 | for pkt in out: |
| 160 | self.assert_eq_pkts(pkt, self.frame_request) |