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