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 <vnet/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
106 VLIB_REGISTER_NODE(lldp_input_node, static) = {
107 .function = lldp_node_fn,
108 .name = "lldp-input",
109 .vector_size = sizeof(u32),
110 .type = VLIB_NODE_TYPE_INTERNAL,
112 .n_errors = LLDP_N_ERROR,
113 .error_strings = lldp_error_strings,
115 .format_trace = lldp_input_format_trace,
117 .n_next_nodes = LLDP_INPUT_N_NEXT,
120 [LLDP_INPUT_NEXT_NORMAL] = "error-drop",
126 * lldp process node function
129 lldp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
131 lldp_main_t *lm = &lldp_main;
133 uword event_type, *event_data = 0;
135 /* So we can send events to the lldp process */
136 lm->lldp_process_node_index = lldp_process_node.index;
138 /* with ethernet input */
139 ethernet_register_input_type (vm, ETHERNET_TYPE_802_1_LLDP /* LLDP */ ,
140 lldp_input_node.index);
144 if (vec_len (lm->intfs_timeouts))
147 clib_warning ("DEBUG: wait for event with timeout %f", timeout);
149 (void) vlib_process_wait_for_event_or_clock (vm, timeout);
154 clib_warning ("DEBUG: wait for event without timeout");
156 (void) vlib_process_wait_for_event (vm);
158 event_type = vlib_process_get_events (vm, &event_data);
161 case ~0: /* no events => timeout */
162 /* nothing to do here */
164 case LLDP_EVENT_RESCHEDULE:
165 /* nothing to do here - reschedule is done automatically after
166 * each event or timeout */
169 clib_warning ("BUG: event type 0x%wx", event_type);
172 if (!vec_len (lm->intfs_timeouts))
176 /* send packet(s) and schedule another timeout */
177 const f64 now = vlib_time_now (lm->vlib_main);
180 lldp_intf_t *n = pool_elt_at_index (lm->intfs,
182 [lm->intfs_timeouts_idx]);
183 if (n->last_sent < 0.01 || now > n->last_sent + lm->msg_tx_interval)
186 clib_warning ("send packet to lldp %p, if idx %d", n,
189 lldp_send_ethernet (lm, n, 0);
190 ++lm->intfs_timeouts_idx;
191 if (lm->intfs_timeouts_idx >= vec_len (lm->intfs_timeouts))
193 lm->intfs_timeouts_idx = 0;
199 timeout = n->last_sent + lm->msg_tx_interval - now;
204 clib_warning ("DEBUG: timeout set to %f", timeout);
207 vec_foreach_index (i, lm->intfs_timeouts)
209 if (i == lm->intfs_timeouts_idx)
211 s = format (s, " [%d]", lm->intfs_timeouts[i]);
215 s = format (s, " %d", lm->intfs_timeouts[i]);
218 clib_warning ("DEBUG: timeout schedule: %s", s);
223 _vec_len (event_data) = 0;
231 * lldp process node declaration
234 VLIB_REGISTER_NODE(lldp_process_node, static) = {
235 .function = lldp_process,
236 .type = VLIB_NODE_TYPE_PROCESS,
237 .name = "lldp-process",
242 lldp_schedule_intf (lldp_main_t * lm, lldp_intf_t * n)
244 const int idx = n - lm->intfs;
246 vec_foreach_index (v, lm->intfs_timeouts)
248 if (lm->intfs_timeouts[v] == idx)
250 /* already scheduled */
254 n->last_sent = 0; /* ensure that a packet is sent out immediately */
255 /* put the interface at the current position in the timeouts - it
256 * will timeout immediately */
257 vec_insert (lm->intfs_timeouts, 1, lm->intfs_timeouts_idx);
258 lm->intfs_timeouts[lm->intfs_timeouts_idx] = n - lm->intfs;
259 vlib_process_signal_event (lm->vlib_main, lm->lldp_process_node_index,
260 LLDP_EVENT_RESCHEDULE, 0);
262 clib_warning ("DEBUG: schedule interface %p, if idx %d", n, n->hw_if_index);
267 lldp_unschedule_intf (lldp_main_t * lm, lldp_intf_t * n)
274 clib_warning ("DEBUG: unschedule interface %p, if idx %d", n,
277 const int idx = n - lm->intfs;
279 /* remove intf index from timeouts vector */
280 vec_foreach_index (v, lm->intfs_timeouts)
282 if (lm->intfs_timeouts[v] == idx)
284 vec_delete (lm->intfs_timeouts, 1, v);
288 /* wrap current timeout index to first element if needed */
289 if (lm->intfs_timeouts_idx >= vec_len (lm->intfs_timeouts))
291 lm->intfs_timeouts_idx = 0;
293 vlib_process_signal_event (lm->vlib_main, lm->lldp_process_node_index,
294 LLDP_EVENT_RESCHEDULE, 0);
297 static clib_error_t *
298 lldp_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
300 lldp_main_t *lm = &lldp_main;
301 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
302 lldp_intf_t *n = lldp_get_intf (lm, hi->hw_if_index);
305 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
307 /* FIXME - the packet sent here isn't send properly - need to find a
308 * way to send the packet before interface goes down */
309 lldp_send_ethernet (lm, n, 1);
310 lldp_unschedule_intf (lm, n);
316 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (lldp_sw_interface_up_down);
318 static clib_error_t *
319 lldp_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
321 lldp_main_t *lm = &lldp_main;
322 lldp_intf_t *n = lldp_get_intf (lm, hw_if_index);
325 if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
327 lldp_schedule_intf (lm, n);
333 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (lldp_hw_interface_up_down);
336 * fd.io coding-style-patch-verification: ON
339 * eval: (c-set-style "gnu")