blob: 8d72e88aa2434947b938b48de83c0f07482e31ca [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Filip Vargaeb601242018-10-31 07:58:05 +01002""" CDP tests """
3
4from scapy.packet import Packet
5from scapy.all import ShortField, StrField
6from scapy.layers.l2 import Dot3, LLC, SNAP
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02007from scapy.contrib.cdp import (
8 CDPMsgDeviceID,
9 CDPMsgSoftwareVersion,
10 CDPMsgPlatform,
11 CDPMsgPortID,
12 CDPv2_HDR,
13)
Filip Vargaeb601242018-10-31 07:58:05 +010014
15from framework import VppTestCase
16from scapy.all import raw
17from re import compile
18from time import sleep
19from util import ppp
Dmitry Valter34fa0ce2024-03-11 10:38:46 +000020from config import config
Filip Vargaeb601242018-10-31 07:58:05 +010021import platform
Ole Troanbd38f7f2019-10-22 08:57:31 +020022import sys
23import unittest
Filip Vargaeb601242018-10-31 07:58:05 +010024
25
26""" TestCDP is a subclass of VPPTestCase classes.
27
28CDP test.
29
30"""
31
32
33class CustomTLV(Packet):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020034 """Custom TLV protocol layer for scapy"""
Filip Vargaeb601242018-10-31 07:58:05 +010035
36 fields_desc = [
37 ShortField("type", 0),
38 ShortField("length", 4),
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020039 StrField("value", ""),
Filip Vargaeb601242018-10-31 07:58:05 +010040 ]
41
42
Dmitry Valter34fa0ce2024-03-11 10:38:46 +000043@unittest.skipIf("cdp" in config.excluded_plugins, "Exclude CDP plugin tests")
Filip Vargaeb601242018-10-31 07:58:05 +010044class TestCDP(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020045 """CDP Test Case"""
Filip Vargaeb601242018-10-31 07:58:05 +010046
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 Vinciguerraa7427ec2019-03-10 10:04:23 -070082 @classmethod
83 def tearDownClass(cls):
84 super(TestCDP, cls).tearDownClass()
85
Filip Vargaeb601242018-10-31 07:58:05 +010086 def test_enable_cdp(self):
Paul Vinciguerra76ef6092019-03-19 05:44:16 -070087 self.logger.info(self.vapi.cdp_enable_disable(enable_disable=1))
Filip Vargaeb601242018-10-31 07:58:05 +010088 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 Vinciguerra76ef6092019-03-19 05:44:16 -070094 self.logger.info(self.vapi.cdp_enable_disable(enable_disable=1))
Filip Vargaeb601242018-10-31 07:58:05 +010095 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.Zhangca7f53a2018-12-10 02:08:02 -0800101 length = min(len(system), len(self.device_id))
Filip Vargaeb601242018-10-31 07:58:05 +0100102
103 self.assert_equal(port, self.port_id, "CDP received invalid port id")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200104 self.assert_equal(
105 system[:length], self.device_id[:length], "CDP received invalid device id"
106 )
Filip Vargaeb601242018-10-31 07:58:05 +0100107
Filip Varga3206bb12018-11-05 09:41:56 +0100108 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 Vinciguerra76ef6092019-03-19 05:44:16 -0700115 self.logger.info(self.vapi.cdp_enable_disable(enable_disable=1))
Filip Varga3206bb12018-11-05 09:41:56 +0100116 self.send_packet(self.create_bad_packet(l, v))
Filip Vargaeb601242018-10-31 07:58:05 +0100117
Ole Troan148c7b72020-10-07 18:05:37 +0200118 err = self.statistics.get_err_counter(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200119 "/err/cdp-input/cdp packets with bad TLVs"
120 )
Ole Troan148c7b72020-10-07 18:05:37 +0200121 self.assertTrue(err >= 1, "CDP didn't drop bad packet")
Filip Vargaeb601242018-10-31 07:58:05 +0100122
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 Sekerad9b0c6f2022-04-26 19:02:15 +0200129 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 Vargaeb601242018-10-31 07:58:05 +0100135 return packet
136
137 def create_packet(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200138 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 Vargaeb601242018-10-31 07:58:05 +0100145 return packet
146
147 def create_bad_packet(self, tl=4, tv=""):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200148 packet = self.create_base_packet() / CustomTLV(type=1, length=tl, value=tv)
Filip Vargaeb601242018-10-31 07:58:05 +0100149 return packet
150
151 def process_cli(self, exp, ptr):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200152 for line in self.vapi.cli(exp).split("\n")[1:]:
Filip Vargaeb601242018-10-31 07:58:05 +0100153 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