2 * Copyright (c) 2011-2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/ethernet/packet.h>
20 2 x CDP graph nodes: an "interior" node to process
21 incoming announcements, and a "process" node to periodically
24 The interior node is neither pipelined nor dual-looped, because
25 it would be very unusual to see more than one CDP packet in
26 a given input frame. So, it's a very simple / straightforward
31 * packet counter strings
32 * Dump these counters via the "show error" CLI command
34 static char *cdp_error_strings[] = {
35 #define _(sym,string) string,
41 * We actually send all cdp pkts to the "error" node after scanning
42 * them, so the graph node has only one next-index. The "error-drop"
43 * node automatically bumps our per-node packet counters for us.
47 CDP_INPUT_NEXT_NORMAL,
52 * Process a frame of cdp packets
53 * Expect 1 packet / frame
56 cdp_node_fn (vlib_main_t * vm,
57 vlib_node_runtime_t * node, vlib_frame_t * frame)
59 u32 n_left_from, *from;
60 cdp_input_trace_t *t0;
62 from = vlib_frame_vector_args (frame); /* array of buffer indices */
63 n_left_from = frame->n_vectors; /* number of buffer indices */
65 while (n_left_from > 0)
72 b0 = vlib_get_buffer (vm, bi0);
74 next0 = CDP_INPUT_NEXT_NORMAL;
76 /* scan this cdp pkt. error0 is the counter index to bump */
77 error0 = cdp_input (vm, b0, bi0);
78 b0->error = node->errors[error0];
80 /* If this pkt is traced, snapshoot the data */
81 if (b0->flags & VLIB_BUFFER_IS_TRACED)
84 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
85 len = (b0->current_length < sizeof (t0->data))
86 ? b0->current_length : sizeof (t0->data);
88 clib_memcpy (t0->data, vlib_buffer_get_current (b0), len);
90 /* push this pkt to the next graph node, always error-drop */
91 vlib_set_next_frame_buffer (vm, node, next0, bi0);
97 return frame->n_vectors;
101 * cdp input graph node declaration
103 VLIB_REGISTER_NODE (cdp_input_node, static) = {
104 .function = cdp_node_fn,
106 .vector_size = sizeof (u32),
107 .type = VLIB_NODE_TYPE_INTERNAL,
109 .n_errors = CDP_N_ERROR,
110 .error_strings = cdp_error_strings,
112 .format_trace = cdp_input_format_trace,
114 .n_next_nodes = CDP_INPUT_N_NEXT,
116 [CDP_INPUT_NEXT_NORMAL] = "error-drop",
121 * cdp periodic function
124 cdp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
126 cdp_main_t *cm = &cdp_main;
127 f64 poll_time_remaining;
128 uword event_type, *event_data = 0;
130 /* Start w/ cdp disabled */
131 poll_time_remaining = 86400.0;
135 /* sleep until next poll time, or msg serialize event occurs */
136 poll_time_remaining =
137 vlib_process_wait_for_event_or_clock (vm, poll_time_remaining);
139 event_type = vlib_process_get_events (vm, &event_data);
142 case ~0: /* no events => timeout */
145 case CDP_EVENT_ENABLE:
146 if (!cm->cdp_protocol_registered)
149 * Dynamically register the cdp input node
150 * with the snap classifier
152 snap_register_input_protocol (vm, "cdp-input",
153 0xC /* ieee_oui, Cisco */ ,
154 0x2000 /* protocol CDP */ ,
155 cdp_input_node.index);
157 snap_register_input_protocol (vm, "cdp-input",
158 0xC /* ieee_oui, Cisco */ ,
159 0x2004 /* protocol CDP */ ,
160 cdp_input_node.index);
163 * Keep this code for reference...
164 * Register with the hdlc classifier
166 hdlc_register_input_protocol (vm, HDLC_PROTOCOL_cdp,
167 cdp_input_node.index);
169 /* with ethernet input (for SRP) */
170 ethernet_register_input_type (vm, ETHERNET_TYPE_CDP /* CDP */ ,
171 cdp_input_node.index);
172 cm->cdp_protocol_registered = 1;
174 poll_time_remaining = 10.0;
177 case CDP_EVENT_DISABLE:
178 poll_time_remaining = 86400.0;
182 clib_warning ("BUG: event type 0x%wx", event_type);
185 vec_reset_length (event_data);
187 /* peer timeout scan, send announcements */
188 if (vlib_process_suspend_time_is_zero (poll_time_remaining))
191 poll_time_remaining = 10.0;
199 vnet_cdp_create_periodic_process (cdp_main_t * cmp)
201 /* Already created the process node? */
202 if (cmp->cdp_process_node_index > 0)
205 /* No, create it now and make a note of the node index */
206 cmp->cdp_process_node_index = vlib_process_create
207 (cmp->vlib_main, "cdp-process",
208 cdp_process, 16 /* log2_n_stack_bytes */ );
212 * fd.io coding-style-patch-verification: ON
215 * eval: (c-set-style "gnu")