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