Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Filip Varga | eb60124 | 2018-10-31 07:58:05 +0100 | [diff] [blame] | 2 | """ CDP tests """ |
| 3 | |
| 4 | from scapy.packet import Packet |
| 5 | from scapy.all import ShortField, StrField |
| 6 | from scapy.layers.l2 import Dot3, LLC, SNAP |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 7 | from scapy.contrib.cdp import ( |
| 8 | CDPMsgDeviceID, |
| 9 | CDPMsgSoftwareVersion, |
| 10 | CDPMsgPlatform, |
| 11 | CDPMsgPortID, |
| 12 | CDPv2_HDR, |
| 13 | ) |
Filip Varga | eb60124 | 2018-10-31 07:58:05 +0100 | [diff] [blame] | 14 | |
| 15 | from framework import VppTestCase |
| 16 | from scapy.all import raw |
| 17 | from re import compile |
| 18 | from time import sleep |
| 19 | from util import ppp |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame^] | 20 | from config import config |
Filip Varga | eb60124 | 2018-10-31 07:58:05 +0100 | [diff] [blame] | 21 | import platform |
Ole Troan | bd38f7f | 2019-10-22 08:57:31 +0200 | [diff] [blame] | 22 | import sys |
| 23 | import unittest |
Filip Varga | eb60124 | 2018-10-31 07:58:05 +0100 | [diff] [blame] | 24 | |
| 25 | |
| 26 | """ TestCDP is a subclass of VPPTestCase classes. |
| 27 | |
| 28 | CDP test. |
| 29 | |
| 30 | """ |
| 31 | |
| 32 | |
| 33 | class CustomTLV(Packet): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 34 | """Custom TLV protocol layer for scapy""" |
Filip Varga | eb60124 | 2018-10-31 07:58:05 +0100 | [diff] [blame] | 35 | |
| 36 | fields_desc = [ |
| 37 | ShortField("type", 0), |
| 38 | ShortField("length", 4), |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 39 | StrField("value", ""), |
Filip Varga | eb60124 | 2018-10-31 07:58:05 +0100 | [diff] [blame] | 40 | ] |
| 41 | |
| 42 | |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame^] | 43 | @unittest.skipIf("cdp" in config.excluded_plugins, "Exclude CDP plugin tests") |
Filip Varga | eb60124 | 2018-10-31 07:58:05 +0100 | [diff] [blame] | 44 | class TestCDP(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 45 | """CDP Test Case""" |
Filip Varga | eb60124 | 2018-10-31 07:58:05 +0100 | [diff] [blame] | 46 | |
| 47 | nen_ptr = compile(r"not enabled") |
| 48 | cdp_ptr = compile(r"^([-\.\w]+)\s+([-\.\w]+)\s+([-\.\w]+)\s+([-\.\w]+)$") |
| 49 | err_ptr = compile(r"^([\d]+)\s+([-\w]+)\s+([ -\.\w)(]+)$") |
| 50 | |
| 51 | @property |
| 52 | def device_id(self): |
| 53 | return platform.node() |
| 54 | |
| 55 | @property |
| 56 | def version(self): |
| 57 | return platform.release() |
| 58 | |
| 59 | @property |
| 60 | def port_id(self): |
| 61 | return self.interface.name |
| 62 | |
| 63 | @property |
| 64 | def platform(self): |
| 65 | return platform.system() |
| 66 | |
| 67 | @classmethod |
| 68 | def setUpClass(cls): |
| 69 | super(TestCDP, cls).setUpClass() |
| 70 | try: |
| 71 | cls.create_pg_interfaces(range(1)) |
| 72 | cls.interface = cls.pg_interfaces[0] |
| 73 | |
| 74 | cls.interface.admin_up() |
| 75 | cls.interface.config_ip4() |
| 76 | cls.interface.resolve_arp() |
| 77 | |
| 78 | except Exception: |
| 79 | super(TestCDP, cls).tearDownClass() |
| 80 | raise |
| 81 | |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 82 | @classmethod |
| 83 | def tearDownClass(cls): |
| 84 | super(TestCDP, cls).tearDownClass() |
| 85 | |
Filip Varga | eb60124 | 2018-10-31 07:58:05 +0100 | [diff] [blame] | 86 | def test_enable_cdp(self): |
Paul Vinciguerra | 76ef609 | 2019-03-19 05:44:16 -0700 | [diff] [blame] | 87 | self.logger.info(self.vapi.cdp_enable_disable(enable_disable=1)) |
Filip Varga | eb60124 | 2018-10-31 07:58:05 +0100 | [diff] [blame] | 88 | ret = self.vapi.cli("show cdp") |
| 89 | self.logger.info(ret) |
| 90 | not_enabled = self.nen_ptr.search(ret) |
| 91 | self.assertFalse(not_enabled, "CDP isn't enabled") |
| 92 | |
| 93 | def test_send_cdp_packet(self): |
Paul Vinciguerra | 76ef609 | 2019-03-19 05:44:16 -0700 | [diff] [blame] | 94 | self.logger.info(self.vapi.cdp_enable_disable(enable_disable=1)) |
Filip Varga | eb60124 | 2018-10-31 07:58:05 +0100 | [diff] [blame] | 95 | self.send_packet(self.create_packet()) |
| 96 | |
| 97 | neighbors = list(self.show_cdp()) |
| 98 | self.assertTrue(neighbors, "CDP didn't register neighbor") |
| 99 | |
| 100 | port, system = neighbors[0] |
Lijian.Zhang | ca7f53a | 2018-12-10 02:08:02 -0800 | [diff] [blame] | 101 | length = min(len(system), len(self.device_id)) |
Filip Varga | eb60124 | 2018-10-31 07:58:05 +0100 | [diff] [blame] | 102 | |
| 103 | self.assert_equal(port, self.port_id, "CDP received invalid port id") |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 104 | self.assert_equal( |
| 105 | system[:length], self.device_id[:length], "CDP received invalid device id" |
| 106 | ) |
Filip Varga | eb60124 | 2018-10-31 07:58:05 +0100 | [diff] [blame] | 107 | |
Filip Varga | 3206bb1 | 2018-11-05 09:41:56 +0100 | [diff] [blame] | 108 | def test_cdp_underflow_tlv(self): |
| 109 | self.send_bad_packet(3, ".") |
| 110 | |
| 111 | def test_cdp_overflow_tlv(self): |
| 112 | self.send_bad_packet(8, ".") |
| 113 | |
| 114 | def send_bad_packet(self, l, v): |
Paul Vinciguerra | 76ef609 | 2019-03-19 05:44:16 -0700 | [diff] [blame] | 115 | self.logger.info(self.vapi.cdp_enable_disable(enable_disable=1)) |
Filip Varga | 3206bb1 | 2018-11-05 09:41:56 +0100 | [diff] [blame] | 116 | self.send_packet(self.create_bad_packet(l, v)) |
Filip Varga | eb60124 | 2018-10-31 07:58:05 +0100 | [diff] [blame] | 117 | |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 118 | err = self.statistics.get_err_counter( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 119 | "/err/cdp-input/cdp packets with bad TLVs" |
| 120 | ) |
Ole Troan | 148c7b7 | 2020-10-07 18:05:37 +0200 | [diff] [blame] | 121 | self.assertTrue(err >= 1, "CDP didn't drop bad packet") |
Filip Varga | eb60124 | 2018-10-31 07:58:05 +0100 | [diff] [blame] | 122 | |
| 123 | def send_packet(self, packet): |
| 124 | self.logger.debug(ppp("Sending packet:", packet)) |
| 125 | self.interface.add_stream(packet) |
| 126 | self.pg_start() |
| 127 | |
| 128 | def create_base_packet(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 129 | packet = ( |
| 130 | Dot3(src=self.interface.remote_mac, dst="01:00:0c:cc:cc:cc") |
| 131 | / LLC(dsap=0xAA, ssap=0xAA, ctrl=0x03) |
| 132 | / SNAP() |
| 133 | / CDPv2_HDR() |
| 134 | ) |
Filip Varga | eb60124 | 2018-10-31 07:58:05 +0100 | [diff] [blame] | 135 | return packet |
| 136 | |
| 137 | def create_packet(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 138 | packet = ( |
| 139 | self.create_base_packet() |
| 140 | / CDPMsgDeviceID(val=self.device_id) |
| 141 | / CDPMsgSoftwareVersion(val=self.version) |
| 142 | / CDPMsgPortID(iface=self.port_id) |
| 143 | / CDPMsgPlatform(val=self.platform) |
| 144 | ) |
Filip Varga | eb60124 | 2018-10-31 07:58:05 +0100 | [diff] [blame] | 145 | return packet |
| 146 | |
| 147 | def create_bad_packet(self, tl=4, tv=""): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 148 | packet = self.create_base_packet() / CustomTLV(type=1, length=tl, value=tv) |
Filip Varga | eb60124 | 2018-10-31 07:58:05 +0100 | [diff] [blame] | 149 | return packet |
| 150 | |
| 151 | def process_cli(self, exp, ptr): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 152 | for line in self.vapi.cli(exp).split("\n")[1:]: |
Filip Varga | eb60124 | 2018-10-31 07:58:05 +0100 | [diff] [blame] | 153 | m = ptr.match(line.strip()) |
| 154 | if m: |
| 155 | yield m.groups() |
| 156 | |
| 157 | def show_cdp(self): |
| 158 | for pack in self.process_cli("show cdp", self.cdp_ptr): |
| 159 | try: |
| 160 | port, system, _, _ = pack |
| 161 | except ValueError: |
| 162 | pass |
| 163 | else: |
| 164 | yield port, system |