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 | |
| 8 | |
| 9 | class BridgeDomain(object): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 10 | """ Bridge domain abstraction """ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 11 | __metaclass__ = ABCMeta |
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 |
| 14 | def frame_pg0_to_pg1(self): |
| 15 | """ Ethernet frame sent from pg0 and expected to arrive at pg1 """ |
| 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 |
| 22 | def frame_pg1_to_pg0(self): |
| 23 | """ Ethernet frame sent from pg1 and expected to arrive at pg0 """ |
| 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 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 29 | @abstractmethod |
| 30 | def encapsulate(self, pkt): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 31 | """ Encapsulate packet """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 32 | pass |
| 33 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 34 | @abstractmethod |
| 35 | def decapsulate(self, pkt): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 36 | """ Decapsulate packet """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 37 | pass |
| 38 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 39 | @abstractmethod |
| 40 | def check_encapsulation(self, pkt): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 41 | """ Verify the encapsulation """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 42 | pass |
| 43 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 44 | def test_decap(self): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 45 | """ Decapsulation test |
| 46 | Send encapsulated frames from pg0 |
| 47 | Verify receipt of decapsulated frames on pg1 |
| 48 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 49 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 50 | encapsulated_pkt = self.encapsulate(self.frame_pg0_to_pg1) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 51 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 52 | self.pg0.add_stream([encapsulated_pkt, ]) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 53 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 54 | self.pg1.enable_capture() |
| 55 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 56 | self.pg_start() |
| 57 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 58 | # Pick first received frame and check if it's the non-encapsulated frame |
| 59 | out = self.pg1.get_capture() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 60 | self.assertEqual(len(out), 1, |
| 61 | 'Invalid number of packets on ' |
| 62 | 'output: {}'.format(len(out))) |
| 63 | pkt = out[0] |
| 64 | |
| 65 | # TODO: add error messages |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 66 | self.assertEqual(pkt[Ether].src, self.frame_pg0_to_pg1[Ether].src) |
| 67 | self.assertEqual(pkt[Ether].dst, self.frame_pg0_to_pg1[Ether].dst) |
| 68 | self.assertEqual(pkt[IP].src, self.frame_pg0_to_pg1[IP].src) |
| 69 | self.assertEqual(pkt[IP].dst, self.frame_pg0_to_pg1[IP].dst) |
| 70 | self.assertEqual(pkt[UDP].sport, self.frame_pg0_to_pg1[UDP].sport) |
| 71 | self.assertEqual(pkt[UDP].dport, self.frame_pg0_to_pg1[UDP].dport) |
| 72 | self.assertEqual(pkt[Raw], self.frame_pg0_to_pg1[Raw]) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 73 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 74 | def test_encap(self): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 75 | """ Encapsulation test |
| 76 | Send frames from pg1 |
| 77 | Verify receipt of encapsulated frames on pg0 |
| 78 | """ |
| 79 | self.pg1.add_stream([self.frame_pg1_to_pg0]) |
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.pg0.enable_capture() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 82 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 83 | self.pg_start() |
| 84 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 85 | # Pick first received frame and check if it's corectly encapsulated. |
| 86 | out = self.pg0.get_capture() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 87 | self.assertEqual(len(out), 1, |
| 88 | 'Invalid number of packets on ' |
| 89 | 'output: {}'.format(len(out))) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 90 | pkt = out[0] |
| 91 | self.check_encapsulation(pkt) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 92 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 93 | payload = self.decapsulate(pkt) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 94 | # TODO: add error messages |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 95 | self.assertEqual(payload[Ether].src, self.frame_pg1_to_pg0[Ether].src) |
| 96 | self.assertEqual(payload[Ether].dst, self.frame_pg1_to_pg0[Ether].dst) |
| 97 | self.assertEqual(payload[IP].src, self.frame_pg1_to_pg0[IP].src) |
| 98 | self.assertEqual(payload[IP].dst, self.frame_pg1_to_pg0[IP].dst) |
| 99 | self.assertEqual(payload[UDP].sport, self.frame_pg1_to_pg0[UDP].sport) |
| 100 | self.assertEqual(payload[UDP].dport, self.frame_pg1_to_pg0[UDP].dport) |
| 101 | self.assertEqual(payload[Raw], self.frame_pg1_to_pg0[Raw]) |