FD.io VPP  v16.06
Vector Packet Processing
cdp_node.c
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 #include <vnet/cdp/cdp_node.h>
16 #include <vnet/ethernet/packet.h>
17 
19 
20 /** \file
21 
22  2 x CDP graph nodes: an "interior" node to process
23  incoming announcements, and a "process" node to periodically
24  send announcements.
25 
26  The interior node is neither pipelined nor dual-looped, because
27  it would be very unusual to see more than one CDP packet in
28  a given input frame. So, it's a very simple / straighforward
29  example.
30 */
31 
32 /*
33  * packet counter strings
34  * Dump these counters via the "show error" CLI command
35  */
36 static char * cdp_error_strings[] = {
37 #define _(sym,string) string,
39 #undef _
40 };
41 
42 /*
43  * We actually send all cdp 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.
46  */
47 typedef enum {
50 } cdp_next_t;
51 
52 /*
53  * Process a frame of cdp packets
54  * Expect 1 packet / frame
55  */
56 static uword
58  vlib_node_runtime_t * node,
59  vlib_frame_t * frame)
60 {
61  u32 n_left_from, * from;
62  cdp_input_trace_t * t0;
63 
64  from = vlib_frame_vector_args (frame); /* array of buffer indices */
65  n_left_from = frame->n_vectors; /* number of buffer indices */
66 
67  while (n_left_from > 0)
68  {
69  u32 bi0;
70  vlib_buffer_t * b0;
71  u32 next0, error0;
72 
73  bi0 = from[0];
74  b0 = vlib_get_buffer (vm, bi0);
75 
76  next0 = CDP_INPUT_NEXT_NORMAL;
77 
78  /* scan this cdp pkt. error0 is the counter index to bump */
79  error0 = cdp_input (vm, b0, bi0);
80  b0->error = node->errors[error0];
81 
82  /* If this pkt is traced, snapshoot the data */
83  if (b0->flags & VLIB_BUFFER_IS_TRACED) {
84  int len;
85  t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
86  len = (b0->current_length < sizeof (t0->data))
87  ? b0->current_length : sizeof (t0->data);
88  t0->len = len;
89  clib_memcpy (t0->data, vlib_buffer_get_current (b0), len);
90  }
91  /* push this pkt to the next graph node, always error-drop */
92  vlib_set_next_frame_buffer (vm, node, next0, bi0);
93 
94  from += 1;
95  n_left_from -= 1;
96  }
97 
98  return frame->n_vectors;
99 }
100 
101 /*
102  * cdp input graph node declaration
103  */
105  .function = cdp_node_fn,
106  .name = "cdp-input",
107  .vector_size = sizeof (u32),
109 
110  .n_errors = CDP_N_ERROR,
111  .error_strings = cdp_error_strings,
112 
113  .format_trace = cdp_input_format_trace,
114 
115  .n_next_nodes = CDP_INPUT_N_NEXT,
116  .next_nodes = {
117  [CDP_INPUT_NEXT_NORMAL] = "error-drop",
118  },
119 };
120 
121 /*
122  * cdp periodic function
123  */
124 static uword
126  vlib_node_runtime_t * rt,
127  vlib_frame_t * f)
128 {
129  cdp_main_t * cm = &cdp_main;
130  f64 poll_time_remaining;
131  uword event_type, * event_data = 0;
132 
133  /* So we can send events to the cdp process */
135 
136  /* Dynamically register the cdp input node with the snap classifier */
137  snap_register_input_protocol (vm, "cdp-input",
138  0xC /* ieee_oui, Cisco */,
139  0x2000 /* protocol CDP */,
140  cdp_input_node.index);
141 
142  snap_register_input_protocol (vm, "cdp-input",
143  0xC /* ieee_oui, Cisco */,
144  0x2004 /* protocol CDP */,
145  cdp_input_node.index);
146 
147 #if 0 /* retain for reference */
148  /* with the hdlc classifier */
149  hdlc_register_input_protocol (vm, HDLC_PROTOCOL_cdp,
150  cdp_input_node.index);
151 #endif
152 
153  /* with ethernet input (for SRP) */
154  ethernet_register_input_type (vm, ETHERNET_TYPE_CDP /* CDP */,
155  cdp_input_node.index);
156 
157  poll_time_remaining = 10.0 /* seconds */;
158  while (1) {
159  /* sleep until next poll time, or msg serialize event occurs */
160  poll_time_remaining =
161  vlib_process_wait_for_event_or_clock (vm, poll_time_remaining);
162 
163  event_type = vlib_process_get_events (vm, &event_data);
164  switch (event_type) {
165  case ~0: /* no events => timeout */
166  break;
167 
168  default:
169  clib_warning ("BUG: event type 0x%wx", event_type);
170  break;
171  }
172  if (event_data)
173  _vec_len (event_data) = 0;
174 
175  /* peer timeout scan, send announcements */
176  if (vlib_process_suspend_time_is_zero (poll_time_remaining)) {
177  cdp_periodic (vm);
178  poll_time_remaining = 10.0;
179  }
180  }
181 
182  return 0;
183 }
184 
185 /*
186  * cdp periodic node declaration
187  */
189  .function = cdp_process,
190  .type = VLIB_NODE_TYPE_PROCESS,
191  .name = "cdp-process",
192 };
193 
always_inline uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Definition: node_funcs.h:410
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
static uword cdp_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: cdp_node.c:57
always_inline uword vlib_process_suspend_time_is_zero(f64 dt)
Definition: node_funcs.h:322
struct _vlib_node_registration vlib_node_registration_t
u32 cdp_process_node_index
Definition: cdp_node.h:85
vlib_error_t * errors
Definition: node.h:378
cdp_main_t cdp_main
Definition: cdp_input.c:17
always_inline void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:184
cdp_next_t
Definition: cdp_node.c:47
vlib_node_registration_t cdp_input_node
(constructor) VLIB_REGISTER_NODE (cdp_input_node)
Definition: cdp_node.c:104
#define clib_warning(format, args...)
Definition: error.h:59
always_inline void * vlib_frame_vector_args(vlib_frame_t *f)
Definition: node_funcs.h:202
#define foreach_cdp_error
Definition: cdp_node.h:98
cdp_error_t cdp_input(vlib_main_t *vm, vlib_buffer_t *b0, u32 bi0)
Definition: cdp_input.c:294
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:81
always_inline f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Definition: node_funcs.h:551
static char * cdp_error_strings[]
Definition: cdp_node.c:36
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:129
void vnet_cdp_node_reference(void)
Definition: cdp_node.c:194
void cdp_periodic(vlib_main_t *vm)
Definition: cdp_periodic.c:330
u16 n_vectors
Definition: node.h:307
#define clib_memcpy(a, b, c)
Definition: string.h:63
void hdlc_register_input_protocol(vlib_main_t *vm, hdlc_protocol_t protocol, u32 node_index)
Definition: node.c:319
unsigned int u32
Definition: types.h:88
static vlib_node_registration_t cdp_process_node
(constructor) VLIB_REGISTER_NODE (cdp_process_node)
Definition: cdp_node.c:18
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:91
void ethernet_register_input_type(vlib_main_t *vm, ethernet_type_t type, u32 node_index)
Definition: node.c:1078
u64 uword
Definition: types.h:112
double f64
Definition: types.h:140
always_inline void vlib_set_next_frame_buffer(vlib_main_t *vm, vlib_node_runtime_t *node, u32 next_index, u32 buffer_index)
Definition: node_funcs.h:292
always_inline void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:140
u8 * cdp_input_format_trace(u8 *s, va_list *args)
Definition: cdp_input.c:442
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:84
always_inline vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
static uword cdp_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: cdp_node.c:125
void snap_register_input_protocol(vlib_main_t *vm, char *name, u32 ieee_oui, u16 protocol, u32 node_index)
Definition: node.c:305