2 * Copyright (c) 2011-2016 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.
17 * @brief LLDP nodes implementation
19 #include <lldp/lldp_node.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vnet/ethernet/packet.h>
23 /* set this to 1 to turn on debug prints via clib_warning() */
24 #define LLDP_DEBUG (0)
26 static vlib_node_registration_t lldp_process_node;
28 #define F(sym, string) static char LLDP_ERR_##sym##_STR[] = string;
29 foreach_lldp_error (F);
33 * packet counter strings
34 * Dump these counters via the "show error" CLI command
36 static char *lldp_error_strings[] = {
37 #define F(sym, string) LLDP_ERR_##sym##_STR,
38 foreach_lldp_error (F)
43 * We actually send all lldp pkts to the "error" node after scanning
44 * them, so the graph node has only one next-index. The "error-drop"
45 * node automatically bumps our per-node packet counters for us.
49 LLDP_INPUT_NEXT_NORMAL,
54 * Process a frame of lldp packets
55 * Expect 1 packet / frame
58 lldp_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
61 u32 n_left_from, *from;
62 lldp_input_trace_t *t0;
64 from = vlib_frame_vector_args (frame); /* array of buffer indices */
65 n_left_from = frame->n_vectors; /* number of buffer indices */
67 while (n_left_from > 0)
74 b0 = vlib_get_buffer (vm, bi0);
76 next0 = LLDP_INPUT_NEXT_NORMAL;
78 /* scan this lldp pkt. error0 is the counter index to bump */
79 error0 = lldp_input (vm, b0, bi0);
80 b0->error = node->errors[error0];
82 /* If this pkt is traced, snapshot the data */
83 if (b0->flags & VLIB_BUFFER_IS_TRACED)
86 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
87 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
90 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0), len);
92 /* push this pkt to the next graph node, always error-drop */
93 vlib_set_next_frame_buffer (vm, node, next0, bi0);
99 return frame->n_vectors;
103 * lldp input graph node declaration
105 VLIB_REGISTER_NODE(lldp_input_node, static) = {
106 .function = lldp_node_fn,
107 .name = "lldp-input",
108 .vector_size = sizeof(u32),
109 .type = VLIB_NODE_TYPE_INTERNAL,
111 .n_errors = LLDP_N_ERROR,
112 .error_strings = lldp_error_strings,
114 .format_trace = lldp_input_format_trace,
116 .n_next_nodes = LLDP_INPUT_N_NEXT,
119 [LLDP_INPUT_NEXT_NORMAL] = "error-drop",
124 * lldp process node function
127 lldp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
129 lldp_main_t *lm = &lldp_main;
131 uword event_type, *event_data = 0;
133 /* So we can send events to the lldp process */
134 lm->lldp_process_node_index = lldp_process_node.index;
136 /* with ethernet input */
137 ethernet_register_input_type (vm, ETHERNET_TYPE_802_1_LLDP /* LLDP */ ,
138 lldp_input_node.index);
142 if (vec_len (lm->intfs_timeouts))
145 clib_warning ("DEBUG: wait for event with timeout %f", timeout);
147 (void) vlib_process_wait_for_event_or_clock (vm, timeout);
152 clib_warning ("DEBUG: wait for event without timeout");
154 (void) vlib_process_wait_for_event (vm);
156 event_type = vlib_process_get_events (vm, &event_data);
159 case ~0: /* no events => timeout */
160 /* nothing to do here */
162 case LLDP_EVENT_RESCHEDULE:
163 /* nothing to do here - reschedule is done automatically after
164 * each event or timeout */
167 clib_warning ("BUG: event type 0x%wx", event_type);
170 if (!vec_len (lm->intfs_timeouts))
174 /* send packet(s) and schedule another timeout */
175 const f64 now = vlib_time_now (lm->vlib_main);
178 lldp_intf_t *n = pool_elt_at_index (lm->intfs,
180 [lm->intfs_timeouts_idx]);
181 if (n->last_sent < 0.01 || now > n->last_sent + lm->msg_tx_interval)
184 clib_warning ("send packet to lldp %p, if idx %d", n,
187 lldp_send_ethernet (lm, n, 0);
188 ++lm->intfs_timeouts_idx;
189 if (lm->intfs_timeouts_idx >= vec_len (lm->intfs_timeouts))
191 lm->intfs_timeouts_idx = 0;
197 timeout = n->last_sent + lm->msg_tx_interval - now;
202 clib_warning ("DEBUG: timeout set to %f", timeout);
205 vec_foreach_index (i, lm->intfs_timeouts)
207 if (i == lm->intfs_timeouts_idx)
209 s = format (s, " [%d]", lm->intfs_timeouts[i]);
213 s = format (s, " %d", lm->intfs_timeouts[i]);
216 clib_warning ("DEBUG: timeout schedule: %s", s);
221 vec_set_len (event_data, 0);
229 * lldp process node declaration
231 VLIB_REGISTER_NODE(lldp_process_node, static) = {
232 .function = lldp_process,
233 .type = VLIB_NODE_TYPE_PROCESS,
234 .name = "lldp-process",
238 lldp_schedule_intf (lldp_main_t * lm, lldp_intf_t * n)
240 const int idx = n - lm->intfs;
242 vec_foreach_index (v, lm->intfs_timeouts)
244 if (lm->intfs_timeouts[v] == idx)
246 /* already scheduled */
250 n->last_sent = 0; /* ensure that a packet is sent out immediately */
251 /* put the interface at the current position in the timeouts - it
252 * will timeout immediately */
253 vec_insert (lm->intfs_timeouts, 1, lm->intfs_timeouts_idx);
254 lm->intfs_timeouts[lm->intfs_timeouts_idx] = n - lm->intfs;
255 vlib_process_signal_event (lm->vlib_main, lm->lldp_process_node_index,
256 LLDP_EVENT_RESCHEDULE, 0);
258 clib_warning ("DEBUG: schedule interface %p, if idx %d", n, n->hw_if_index);
263 lldp_unschedule_intf (lldp_main_t * lm, lldp_intf_t * n)
270 clib_warning ("DEBUG: unschedule interface %p, if idx %d", n,
273 const int idx = n - lm->intfs;
275 /* remove intf index from timeouts vector */
276 vec_foreach_index (v, lm->intfs_timeouts)
278 if (lm->intfs_timeouts[v] == idx)
280 vec_delete (lm->intfs_timeouts, 1, v);
284 /* wrap current timeout index to first element if needed */
285 if (lm->intfs_timeouts_idx >= vec_len (lm->intfs_timeouts))
287 lm->intfs_timeouts_idx = 0;
289 vlib_process_signal_event (lm->vlib_main, lm->lldp_process_node_index,
290 LLDP_EVENT_RESCHEDULE, 0);
293 static clib_error_t *
294 lldp_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
296 lldp_main_t *lm = &lldp_main;
297 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
298 lldp_intf_t *n = lldp_get_intf (lm, hi->hw_if_index);
301 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
303 /* FIXME - the packet sent here isn't send properly - need to find a
304 * way to send the packet before interface goes down */
305 lldp_send_ethernet (lm, n, 1);
306 lldp_unschedule_intf (lm, n);
312 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (lldp_sw_interface_up_down);
314 static clib_error_t *
315 lldp_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
317 lldp_main_t *lm = &lldp_main;
318 lldp_intf_t *n = lldp_get_intf (lm, hw_if_index);
321 if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
323 lldp_schedule_intf (lm, n);
329 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (lldp_hw_interface_up_down);
332 * fd.io coding-style-patch-verification: ON
335 * eval: (c-set-style "gnu")