2019-10-31 13:31:07 -05:00
|
|
|
#!/usr/bin/env python3
|
2018-10-31 07:58:05 +01:00
|
|
|
""" CDP tests """
|
|
|
|
|
|
|
|
from scapy.packet import Packet
|
|
|
|
from scapy.all import ShortField, StrField
|
|
|
|
from scapy.layers.l2 import Dot3, LLC, SNAP
|
2022-04-26 19:02:15 +02:00
|
|
|
from scapy.contrib.cdp import (
|
|
|
|
CDPMsgDeviceID,
|
|
|
|
CDPMsgSoftwareVersion,
|
|
|
|
CDPMsgPlatform,
|
|
|
|
CDPMsgPortID,
|
|
|
|
CDPv2_HDR,
|
|
|
|
)
|
2018-10-31 07:58:05 +01:00
|
|
|
|
|
|
|
from framework import VppTestCase
|
|
|
|
from scapy.all import raw
|
|
|
|
from re import compile
|
|
|
|
from time import sleep
|
|
|
|
from util import ppp
|
2024-03-11 10:38:46 +00:00
|
|
|
from config import config
|
2018-10-31 07:58:05 +01:00
|
|
|
import platform
|
2019-10-22 08:57:31 +02:00
|
|
|
import sys
|
|
|
|
import unittest
|
2018-10-31 07:58:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
""" TestCDP is a subclass of VPPTestCase classes.
|
|
|
|
|
|
|
|
CDP test.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class CustomTLV(Packet):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Custom TLV protocol layer for scapy"""
|
2018-10-31 07:58:05 +01:00
|
|
|
|
|
|
|
fields_desc = [
|
|
|
|
ShortField("type", 0),
|
|
|
|
ShortField("length", 4),
|
2022-04-26 19:02:15 +02:00
|
|
|
StrField("value", ""),
|
2018-10-31 07:58:05 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2024-03-11 10:38:46 +00:00
|
|
|
@unittest.skipIf("cdp" in config.excluded_plugins, "Exclude CDP plugin tests")
|
2018-10-31 07:58:05 +01:00
|
|
|
class TestCDP(VppTestCase):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""CDP Test Case"""
|
2018-10-31 07:58:05 +01:00
|
|
|
|
|
|
|
nen_ptr = compile(r"not enabled")
|
|
|
|
cdp_ptr = compile(r"^([-\.\w]+)\s+([-\.\w]+)\s+([-\.\w]+)\s+([-\.\w]+)$")
|
|
|
|
err_ptr = compile(r"^([\d]+)\s+([-\w]+)\s+([ -\.\w)(]+)$")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_id(self):
|
|
|
|
return platform.node()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def version(self):
|
|
|
|
return platform.release()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def port_id(self):
|
|
|
|
return self.interface.name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def platform(self):
|
|
|
|
return platform.system()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
super(TestCDP, cls).setUpClass()
|
|
|
|
try:
|
|
|
|
cls.create_pg_interfaces(range(1))
|
|
|
|
cls.interface = cls.pg_interfaces[0]
|
|
|
|
|
|
|
|
cls.interface.admin_up()
|
|
|
|
cls.interface.config_ip4()
|
|
|
|
cls.interface.resolve_arp()
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
super(TestCDP, cls).tearDownClass()
|
|
|
|
raise
|
|
|
|
|
2019-03-10 10:04:23 -07:00
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
super(TestCDP, cls).tearDownClass()
|
|
|
|
|
2018-10-31 07:58:05 +01:00
|
|
|
def test_enable_cdp(self):
|
2019-03-19 05:44:16 -07:00
|
|
|
self.logger.info(self.vapi.cdp_enable_disable(enable_disable=1))
|
2018-10-31 07:58:05 +01:00
|
|
|
ret = self.vapi.cli("show cdp")
|
|
|
|
self.logger.info(ret)
|
|
|
|
not_enabled = self.nen_ptr.search(ret)
|
|
|
|
self.assertFalse(not_enabled, "CDP isn't enabled")
|
|
|
|
|
|
|
|
def test_send_cdp_packet(self):
|
2019-03-19 05:44:16 -07:00
|
|
|
self.logger.info(self.vapi.cdp_enable_disable(enable_disable=1))
|
2018-10-31 07:58:05 +01:00
|
|
|
self.send_packet(self.create_packet())
|
|
|
|
|
|
|
|
neighbors = list(self.show_cdp())
|
|
|
|
self.assertTrue(neighbors, "CDP didn't register neighbor")
|
|
|
|
|
|
|
|
port, system = neighbors[0]
|
2018-12-10 02:08:02 -08:00
|
|
|
length = min(len(system), len(self.device_id))
|
2018-10-31 07:58:05 +01:00
|
|
|
|
|
|
|
self.assert_equal(port, self.port_id, "CDP received invalid port id")
|
2022-04-26 19:02:15 +02:00
|
|
|
self.assert_equal(
|
|
|
|
system[:length], self.device_id[:length], "CDP received invalid device id"
|
|
|
|
)
|
2018-10-31 07:58:05 +01:00
|
|
|
|
2018-11-05 09:41:56 +01:00
|
|
|
def test_cdp_underflow_tlv(self):
|
|
|
|
self.send_bad_packet(3, ".")
|
|
|
|
|
|
|
|
def test_cdp_overflow_tlv(self):
|
|
|
|
self.send_bad_packet(8, ".")
|
|
|
|
|
|
|
|
def send_bad_packet(self, l, v):
|
2019-03-19 05:44:16 -07:00
|
|
|
self.logger.info(self.vapi.cdp_enable_disable(enable_disable=1))
|
2018-11-05 09:41:56 +01:00
|
|
|
self.send_packet(self.create_bad_packet(l, v))
|
2018-10-31 07:58:05 +01:00
|
|
|
|
2020-10-07 18:05:37 +02:00
|
|
|
err = self.statistics.get_err_counter(
|
2022-04-26 19:02:15 +02:00
|
|
|
"/err/cdp-input/cdp packets with bad TLVs"
|
|
|
|
)
|
2020-10-07 18:05:37 +02:00
|
|
|
self.assertTrue(err >= 1, "CDP didn't drop bad packet")
|
2018-10-31 07:58:05 +01:00
|
|
|
|
|
|
|
def send_packet(self, packet):
|
|
|
|
self.logger.debug(ppp("Sending packet:", packet))
|
|
|
|
self.interface.add_stream(packet)
|
|
|
|
self.pg_start()
|
|
|
|
|
|
|
|
def create_base_packet(self):
|
2022-04-26 19:02:15 +02:00
|
|
|
packet = (
|
|
|
|
Dot3(src=self.interface.remote_mac, dst="01:00:0c:cc:cc:cc")
|
|
|
|
/ LLC(dsap=0xAA, ssap=0xAA, ctrl=0x03)
|
|
|
|
/ SNAP()
|
|
|
|
/ CDPv2_HDR()
|
|
|
|
)
|
2018-10-31 07:58:05 +01:00
|
|
|
return packet
|
|
|
|
|
|
|
|
def create_packet(self):
|
2022-04-26 19:02:15 +02:00
|
|
|
packet = (
|
|
|
|
self.create_base_packet()
|
|
|
|
/ CDPMsgDeviceID(val=self.device_id)
|
|
|
|
/ CDPMsgSoftwareVersion(val=self.version)
|
|
|
|
/ CDPMsgPortID(iface=self.port_id)
|
|
|
|
/ CDPMsgPlatform(val=self.platform)
|
|
|
|
)
|
2018-10-31 07:58:05 +01:00
|
|
|
return packet
|
|
|
|
|
|
|
|
def create_bad_packet(self, tl=4, tv=""):
|
2022-04-26 19:02:15 +02:00
|
|
|
packet = self.create_base_packet() / CustomTLV(type=1, length=tl, value=tv)
|
2018-10-31 07:58:05 +01:00
|
|
|
return packet
|
|
|
|
|
|
|
|
def process_cli(self, exp, ptr):
|
2022-04-26 19:02:15 +02:00
|
|
|
for line in self.vapi.cli(exp).split("\n")[1:]:
|
2018-10-31 07:58:05 +01:00
|
|
|
m = ptr.match(line.strip())
|
|
|
|
if m:
|
|
|
|
yield m.groups()
|
|
|
|
|
|
|
|
def show_cdp(self):
|
|
|
|
for pack in self.process_cli("show cdp", self.cdp_ptr):
|
|
|
|
try:
|
|
|
|
port, system, _, _ = pack
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
yield port, system
|