Cisco Discovery Protocol, initial working attempt

There are multiple enhancement opportunities...

Change-Id: I976772dc3802f8284e8c6457c001d68184831e25
Signed-off-by: Dave Barach <dave@barachs.net>
This commit is contained in:
Dave Barach
2016-02-08 15:57:35 -05:00
parent 3e7deb11b5
commit ced48e7cbb
9 changed files with 1495 additions and 1 deletions

View File

@ -191,9 +191,20 @@ libvnet_la_SOURCES += \
nobase_include_HEADERS += \
vnet/vxlan/vxlan.h \
vnet/vxlan/vxlan_packet.h \
vnet/vxlan/vxlan_packet.h \
vnet/vxlan/vxlan_error.def
########################################
# Layer 2 / CDP
########################################
libvnet_la_SOURCES += \
vnet/cdp/cdp_input.c \
vnet/cdp/cdp_node.c \
vnet/cdp/cdp_periodic.c
nobase_include_HEADERS += \
vnet/cdp/cdp_protocol.h
########################################
# Layer 2/3 "classify"
########################################

7
vnet/vnet/cdp/cdp.pg Normal file
View File

@ -0,0 +1,7 @@
packet-generator new {
name cdp
limit 1
node cdp-input
size 374-374
data { hex 0x02b46b96000100096978676265000500bf436973636f20494f5320536f6674776172652c2043333735304520536f66747761726520284333373530452d554e4956455253414c2d4d292c2056657273696f6e2031322e32283335295345352c2052454c4541534520534f4654574152452028666331290a436f707972696768742028632920313938362d3230303720627920436973636f2053797374656d732c20496e632e0a436f6d70696c6564205468752031392d4a756c2d30372031363a3137206279206e616368656e00060018636973636f2057532d4333373530452d3234544400020011000000010101cc0004000000000003001b54656e4769676162697445746865726e6574312f302f3100040008000000280008002400000c011200000000ffffffff010221ff000000000000001e7a50f000ff000000090004000a00060001000b0005010012000500001300050000160011000000010101cc000400000000001a00100000000100000000ffffffff }
}

476
vnet/vnet/cdp/cdp_input.c Normal file

File diff suppressed because it is too large Load Diff

194
vnet/vnet/cdp/cdp_node.c Normal file
View File

@ -0,0 +1,194 @@
/*
* Copyright (c) 2011-2016 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <vnet/cdp/cdp_node.h>
#include <vnet/ethernet/packet.h>
static vlib_node_registration_t cdp_process_node;
/** \file
2 x CDP graph nodes: an "interior" node to process
incoming announcements, and a "process" node to periodically
send announcements.
The interior node is neither pipelined nor dual-looped, because
it would be very unusual to see more than one CDP packet in
a given input frame. So, it's a very simple / straighforward
example.
*/
/*
* packet counter strings
* Dump these counters via the "show error" CLI command
*/
static char * cdp_error_strings[] = {
#define _(sym,string) string,
foreach_cdp_error
#undef _
};
/*
* We actually send all cdp pkts to the "error" node after scanning
* them, so the graph node has only one next-index. The "error-drop"
* node automatically bumps our per-node packet counters for us.
*/
typedef enum {
CDP_INPUT_NEXT_NORMAL,
CDP_INPUT_N_NEXT,
} cdp_next_t;
/*
* Process a frame of cdp packets
* Expect 1 packet / frame
*/
static uword
cdp_node_fn (vlib_main_t * vm,
vlib_node_runtime_t * node,
vlib_frame_t * frame)
{
u32 n_left_from, * from;
cdp_input_trace_t * t0;
from = vlib_frame_vector_args (frame); /* array of buffer indices */
n_left_from = frame->n_vectors; /* number of buffer indices */
while (n_left_from > 0)
{
u32 bi0;
vlib_buffer_t * b0;
u32 next0, error0;
bi0 = from[0];
b0 = vlib_get_buffer (vm, bi0);
next0 = CDP_INPUT_NEXT_NORMAL;
/* scan this cdp pkt. error0 is the counter index to bump */
error0 = cdp_input (vm, b0, bi0);
b0->error = node->errors[error0];
/* If this pkt is traced, snapshoot the data */
if (b0->flags & VLIB_BUFFER_IS_TRACED) {
int len;
t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
len = (b0->current_length < sizeof (t0->data))
? b0->current_length : sizeof (t0->data);
t0->len = len;
memcpy (t0->data, vlib_buffer_get_current (b0), len);
}
/* push this pkt to the next graph node, always error-drop */
vlib_set_next_frame_buffer (vm, node, next0, bi0);
from += 1;
n_left_from -= 1;
}
return frame->n_vectors;
}
/*
* cdp input graph node declaration
*/
VLIB_REGISTER_NODE (cdp_input_node, static) = {
.function = cdp_node_fn,
.name = "cdp-input",
.vector_size = sizeof (u32),
.type = VLIB_NODE_TYPE_INTERNAL,
.n_errors = CDP_N_ERROR,
.error_strings = cdp_error_strings,
.format_trace = cdp_input_format_trace,
.n_next_nodes = CDP_INPUT_N_NEXT,
.next_nodes = {
[CDP_INPUT_NEXT_NORMAL] = "error-drop",
},
};
/*
* cdp periodic function
*/
static uword
cdp_process (vlib_main_t * vm,
vlib_node_runtime_t * rt,
vlib_frame_t * f)
{
cdp_main_t * cm = &cdp_main;
f64 poll_time_remaining;
uword event_type, * event_data = 0;
/* So we can send events to the cdp process */
cm->cdp_process_node_index = cdp_process_node.index;
/* Dynamically register the cdp input node with the snap classifier */
snap_register_input_protocol (vm, "cdp-input",
0xC /* ieee_oui, Cisco */,
0x2000 /* protocol CDP */,
cdp_input_node.index);
snap_register_input_protocol (vm, "cdp-input",
0xC /* ieee_oui, Cisco */,
0x2004 /* protocol CDP */,
cdp_input_node.index);
#if 0 /* retain for reference */
/* with the hdlc classifier */
hdlc_register_input_protocol (vm, HDLC_PROTOCOL_cdp,
cdp_input_node.index);
#endif
/* with ethernet input (for SRP) */
ethernet_register_input_type (vm, ETHERNET_TYPE_CDP /* CDP */,
cdp_input_node.index);
poll_time_remaining = 10.0 /* seconds */;
while (1) {
/* sleep until next poll time, or msg serialize event occurs */
poll_time_remaining =
vlib_process_wait_for_event_or_clock (vm, poll_time_remaining);
event_type = vlib_process_get_events (vm, &event_data);
switch (event_type) {
case ~0: /* no events => timeout */
break;
default:
clib_warning ("BUG: event type 0x%wx", event_type);
break;
}
if (event_data)
_vec_len (event_data) = 0;
/* peer timeout scan, send announcements */
if (vlib_process_suspend_time_is_zero (poll_time_remaining)) {
cdp_periodic (vm);
poll_time_remaining = 10.0;
}
}
return 0;
}
/*
* cdp periodic node declaration
*/
VLIB_REGISTER_NODE (cdp_process_node, static) = {
.function = cdp_process,
.type = VLIB_NODE_TYPE_PROCESS,
.name = "cdp-process",
};
void vnet_cdp_node_reference(void) { }

133
vnet/vnet/cdp/cdp_node.h Normal file
View File

@ -0,0 +1,133 @@
/*
* Copyright (c) 2011-2016 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __included_cdp_node_h__
#define __included_cdp_node_h__
#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
#include <vnet/snap/snap.h>
#include <vnet/hdlc/hdlc.h>
#include <vnet/hdlc/packet.h>
#include <vppinfra/format.h>
#include <vppinfra/hash.h>
#include <vnet/cdp/cdp_protocol.h>
typedef enum {
CDP_PACKET_TEMPLATE_ETHERNET,
CDP_PACKET_TEMPLATE_HDLC,
CDP_PACKET_TEMPLATE_SRP,
CDP_N_PACKET_TEMPLATES,
} cdp_packet_template_id_t;
typedef struct {
/* neighbor's vlib software interface index */
u32 sw_if_index;
/* Timers */
f64 last_heard;
f64 last_sent;
/* Neighbor time-to-live (usually 180s) */
u8 ttl_in_seconds;
/* "no cdp run" or similar */
u8 disabled;
/* tx packet template id for this neighbor */
u8 packet_template_index;
/* Jenkins hash optimization: avoid tlv scan, send short keepalive msg */
u8 last_packet_signature_valid;
uword last_packet_signature;
/* Info we actually keep about each neighbor */
u8 *device_name;
u8 *version;
u8 *port_id;
u8 *platform;
/* last received packet, for the J-hash optimization */
u8 *last_rx_pkt;
} cdp_neighbor_t;
#define foreach_neighbor_string_field \
_(device_name) \
_(version) \
_(port_id) \
_(platform)
typedef struct {
/* pool of cdp neighbors */
cdp_neighbor_t *neighbors;
/* tx pcap debug enable */
u8 tx_pcap_debug;
/* rapidly find a neighbor by vlib software interface index */
uword *neighbor_by_sw_if_index;
/* Background process node index */
u32 cdp_process_node_index;
/* Packet templates for different encap types */
vlib_packet_template_t packet_templates [CDP_N_PACKET_TEMPLATES];
/* convenience variables */
vlib_main_t *vlib_main;
vnet_main_t *vnet_main;
} cdp_main_t;
cdp_main_t cdp_main;
/* Packet counters */
#define foreach_cdp_error \
_ (NONE, "good cdp packets (processed)") \
_ (CACHE_HIT, "good cdp packets (cache hit)") \
_ (BAD_TLV, "cdp packets with bad TLVs") \
_ (PROTOCOL_VERSION, "cdp packets with bad protocol versions") \
_ (CHECKSUM, "cdp packets with bad checksums") \
_ (DISABLED, "cdp packets received on disabled interfaces")
typedef enum {
#define _(sym,str) CDP_ERROR_##sym,
foreach_cdp_error
#undef _
CDP_N_ERROR,
} cdp_error_t;
/* cdp packet trace capture */
typedef struct {
u32 len;
u8 data[400];
} cdp_input_trace_t;
typedef enum {
CDP_EVENT_SEND_NEIGHBOR,
CDP_EVENT_SEND_KEEPALIVE,
} cdp_process_event_t;
cdp_error_t cdp_input (vlib_main_t * vm, vlib_buffer_t * b0, u32 bi0);
void cdp_periodic (vlib_main_t * vm);
void cdp_keepalive (cdp_main_t * cm, cdp_neighbor_t * n);
u16 cdp_checksum (void *p, int count);
u8 * cdp_input_format_trace (u8 * s, va_list * args);
serialize_function_t serialize_cdp_main, unserialize_cdp_main;
#endif /* __included_cdp_node_h__ */

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,175 @@
/*
* Copyright (c) 2011-2016 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __included_cdp_protocol_h__
#define __included_cdp_protocol_h__
#include <vnet/ethernet/ethernet.h> /* for ethernet_header_t */
#include <vnet/llc/llc.h>
#include <vnet/snap/snap.h>
#include <vnet/srp/packet.h>
typedef CLIB_PACKED (struct {
u8 version;
u8 ttl;
u16 checksum; /* 1's complement of the 1's complement sum */
u8 data[0];
}) cdp_hdr_t;
typedef struct {
u8 dst_address[6];
u8 src_address[6];
u16 len;
} ethernet_802_3_header_t;
typedef CLIB_PACKED (struct {
ethernet_802_3_header_t ethernet;
llc_header_t llc;
snap_header_t snap;
cdp_hdr_t cdp;
}) ethernet_llc_snap_and_cdp_header_t;
typedef CLIB_PACKED (struct {
hdlc_header_t hdlc;
cdp_hdr_t cdp;
}) hdlc_and_cdp_header_t;
typedef CLIB_PACKED (struct {
srp_header_t srp;
ethernet_header_t ethernet;
cdp_hdr_t cdp;
}) srp_and_cdp_header_t;
typedef CLIB_PACKED (struct {
u16 t;
u16 l;
u8 v[0];
}) cdp_tlv_t;
/*
* TLV codes.
*/
#define foreach_cdp_tlv_type \
_(unused) \
_(device_name) /* uniquely identifies the device */ \
_(address) /* list of addresses this device has */ \
_(port_id) /* port CDP packet was sent out on */ \
_(capabilities) /* funct. capabilities of the device */ \
_(version) /* version */ \
_(platform) /* hardware platform of this device */ \
_(ipprefix) /* An IP network prefix */ \
_(hello) /* Pprotocol piggyback hello msg */ \
_(vtp_domain) /* VTP management domain */ \
_(native_vlan) /* Native VLAN number */ \
_(duplex) /* The interface duplex mode */ \
_(appl_vlan) /* Appliance VLAN-ID TLV */ \
_(trigger) /* For sending trigger TLV msgs. */ \
_(power) /* Power consumption of that device */ \
_(mtu) /* MTU defined for sending intf. */ \
_(trust) /* Extended trust TLV */ \
_(cos) /* COS for Untrusted Port TLV */ \
_(sysname) /* System name (FQDN of device) */ \
_(sysobject) /* OID of sysObjectID MIB object */ \
_(mgmt_addr) /* SNMP manageable addrs. of device */ \
_(physical_loc) /* Physical Location of the device */ \
_(mgmt_addr2) /* External Port-ID */ \
_(power_requested) \
_(power_available) \
_(port_unidirectional) \
_(unknown_28) \
_(energywise) \
_(unknown_30) \
_(spare_poe)
typedef enum {
#define _(t) CDP_TLV_##t,
foreach_cdp_tlv_type
#undef _
} cdp_tlv_code_t;
/*
The address TLV looks as follows:
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Number of addresses |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| IDRP encoded address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
An address is encoded in IDRP format:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| PT | PT Length | Protocol (variable) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Address length | Address (variable) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
PT: Protocol type
1 = NLPID format
2 = 802.2 format
PT Length:
Length of protocol field, 1 for PT = 1, and either 3 or 8 for
802.2 format depending if SNAP is used for PT = 2.
The encodings for the other protocols have the following format:
field: <SSAP><DSAP><CTRL><-------OUI------><protocl_TYPE>
| | | | | | | | |
bytes: 0 1 2 3 4 5 6 7 8
where the first 3 bytes are 0xAAAA03 for SNAP encoded addresses.
The OUI is 000000 for ethernet and <protocl_TYPE>
is the assigned Ethernet type code for the particular protocol.
e.g. for DECnet the encoding is AAAA03 000000 6003.
for IPv6 the encoding is AAAA03 000000 86DD
*/
/*
* Capabilities.
*/
#define CDP_ROUTER_DEVICE 0x0001
#define CDP_TB_DEVICE 0x0002
#define CDP_SRB_DEVICE 0x0004
#define CDP_SWITCH_DEVICE 0x0008
#define CDP_HOST_DEVICE 0x0010
#define CDP_IGMP_DEVICE 0x0020
#define CDP_REPEATER_DEVICE 0x0040
/*
The protocol-hello TLV looks as follows:
0 1 2 3
012345678901234567890123456789012345678
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type | Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| OUI |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Protocol ID |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| up to 27 bytes of message |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
/*
* These macros define the valid values for the Duplex TLV.
*/
#define CDP_DUPLEX_TLV_HALF 0x0
#define CDP_DUPLEX_TLV_FULL 0x1
#endif /* __included_cdp_protocol_h__ */

View File

@ -296,6 +296,10 @@ llc_register_input_protocol (vlib_main_t * vm,
clib_error_t * error = vlib_call_init_function (vm, llc_input_init);
if (error)
clib_error_report (error);
/* Otherwise, osi_input_init will wipe out e.g. the snap init */
error = vlib_call_init_function (vm, osi_input_init);
if (error)
clib_error_report (error);
}
pi = llc_get_protocol_info (lm, protocol);

View File

@ -56,6 +56,12 @@ vpe_main_init (vlib_main_t * vm)
return error;
if ((error = vlib_call_init_function (vm, sixrd_init)))
return error;
if ((error = vlib_call_init_function (vm, llc_init)))
return error;
if ((error = vlib_call_init_function (vm, snap_init)))
return error;
if ((error = vlib_call_init_function (vm, cdp_init)))
return error;
if ((error = vlib_call_init_function (vm, nsh_gre_init)))
return error;
if ((error = vlib_call_init_function (vm, nsh_vxlan_gpe_init)))