FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
cdp_node.c
Go to the documentation of this file.
1 /*
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:
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 <cdp/cdp.h>
16 #include <vnet/ethernet/packet.h>
17 
18 /** \file
19 
20  2 x CDP graph nodes: an "interior" node to process
21  incoming announcements, and a "process" node to periodically
22  send announcements.
23 
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
27  example.
28 */
29 
30 /*
31  * packet counter strings
32  * Dump these counters via the "show error" CLI command
33  */
34 static char *cdp_error_strings[] = {
35 #define _(sym,string) string,
37 #undef _
38 };
39 
40 /*
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.
44  */
45 typedef enum
46 {
49 } cdp_next_t;
50 
51 /*
52  * Process a frame of cdp packets
53  * Expect 1 packet / frame
54  */
55 static uword
58 {
61 
62  from = vlib_frame_vector_args (frame); /* array of buffer indices */
63  n_left_from = frame->n_vectors; /* number of buffer indices */
64 
65  while (n_left_from > 0)
66  {
67  u32 bi0;
68  vlib_buffer_t *b0;
69  u32 next0, error0;
70 
71  bi0 = from[0];
72  b0 = vlib_get_buffer (vm, bi0);
73 
74  next0 = CDP_INPUT_NEXT_NORMAL;
75 
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];
79 
80  /* If this pkt is traced, snapshoot the data */
81  if (b0->flags & VLIB_BUFFER_IS_TRACED)
82  {
83  int len;
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);
87  t0->len = len;
89  }
90  /* push this pkt to the next graph node, always error-drop */
91  vlib_set_next_frame_buffer (vm, node, next0, bi0);
92 
93  from += 1;
94  n_left_from -= 1;
95  }
96 
97  return frame->n_vectors;
98 }
99 
100 /*
101  * cdp input graph node declaration
102  */
103 /* *INDENT-OFF* */
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 /* *INDENT-ON* */
121 
122 /*
123  * cdp periodic function
124  */
125 static uword
127 {
128  cdp_main_t *cm = &cdp_main;
129  f64 poll_time_remaining;
130  uword event_type, *event_data = 0;
131 
132  /* Start w/ cdp disabled */
133  poll_time_remaining = 86400.0;
134 
135  while (1)
136  {
137  /* sleep until next poll time, or msg serialize event occurs */
138  poll_time_remaining =
139  vlib_process_wait_for_event_or_clock (vm, poll_time_remaining);
140 
141  event_type = vlib_process_get_events (vm, &event_data);
142  switch (event_type)
143  {
144  case ~0: /* no events => timeout */
145  break;
146 
147  case CDP_EVENT_ENABLE:
148  if (!cm->cdp_protocol_registered)
149  {
150  /*
151  * Dynamically register the cdp input node
152  * with the snap classifier
153  */
154  snap_register_input_protocol (vm, "cdp-input",
155  0xC /* ieee_oui, Cisco */ ,
156  0x2000 /* protocol CDP */ ,
157  cdp_input_node.index);
158 
159  snap_register_input_protocol (vm, "cdp-input",
160  0xC /* ieee_oui, Cisco */ ,
161  0x2004 /* protocol CDP */ ,
162  cdp_input_node.index);
163 #if 0
164  /*
165  * Keep this code for reference...
166  * Register with the hdlc classifier
167  */
168  hdlc_register_input_protocol (vm, HDLC_PROTOCOL_cdp,
169  cdp_input_node.index);
170 #endif
171  /* with ethernet input (for SRP) */
172  ethernet_register_input_type (vm, ETHERNET_TYPE_CDP /* CDP */ ,
173  cdp_input_node.index);
174  cm->cdp_protocol_registered = 1;
175  }
176  poll_time_remaining = 10.0;
177  break;
178 
179  case CDP_EVENT_DISABLE:
180  poll_time_remaining = 86400.0;
181  break;
182 
183  default:
184  clib_warning ("BUG: event type 0x%wx", event_type);
185  break;
186  }
187  vec_reset_length (event_data);
188 
189  /* peer timeout scan, send announcements */
190  if (vlib_process_suspend_time_is_zero (poll_time_remaining))
191  {
192  cdp_periodic (vm);
193  poll_time_remaining = 10.0;
194  }
195  }
196 
197  return 0;
198 }
199 
200 void
202 {
203  /* Already created the process node? */
204  if (cmp->cdp_process_node_index > 0)
205  return;
206 
207  /* No, create it now and make a note of the node index */
209  (cmp->vlib_main, "cdp-process",
210  cdp_process, 16 /* log2_n_stack_bytes */ );
211 }
212 
213 /*
214  * fd.io coding-style-patch-verification: ON
215  *
216  * Local Variables:
217  * eval: (c-set-style "gnu")
218  * End:
219  */
vec_reset_length
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
Definition: vec_bootstrap.h:194
frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: nat44_ei.c:3048
clib_memcpy
#define clib_memcpy(d, s, n)
Definition: string.h:197
cdp_main_t::cdp_process_node_index
u32 cdp_process_node_index
Definition: cdp.h:94
vlib_get_buffer
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:111
f
vlib_frame_t * f
Definition: interface_output.c:1098
foreach_cdp_error
#define foreach_cdp_error
Definition: cdp.h:112
VLIB_NODE_TYPE_INTERNAL
@ VLIB_NODE_TYPE_INTERNAL
Definition: node.h:72
CDP_INPUT_N_NEXT
@ CDP_INPUT_N_NEXT
Definition: cdp_node.c:48
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
vnet_cdp_create_periodic_process
void vnet_cdp_create_periodic_process(cdp_main_t *cmp)
Definition: cdp_node.c:201
cdp_process
static uword cdp_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: cdp_node.c:126
cdp_main
cdp_main_t cdp_main
Definition: cdp_input.c:17
cdp_input
cdp_error_t cdp_input(vlib_main_t *vm, vlib_buffer_t *b0, u32 bi0)
Definition: cdp_input.c:323
vlib_frame_t
Definition: node.h:372
hdlc_register_input_protocol
void hdlc_register_input_protocol(vlib_main_t *vm, hdlc_protocol_t protocol, u32 node_index)
Definition: node.c:358
CDP_INPUT_NEXT_NORMAL
@ CDP_INPUT_NEXT_NORMAL
Definition: cdp_node.c:47
vlib_process_get_events
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type,...
Definition: node_funcs.h:583
cdp_input_trace_t::len
u32 len
Definition: cdp.h:131
CDP_EVENT_ENABLE
@ CDP_EVENT_ENABLE
Definition: cdp.h:137
cdp_next_t
cdp_next_t
Definition: cdp_node.c:45
vlib_buffer_t::error
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:145
packet.h
len
u8 len
Definition: ip_types.api:103
cdp_periodic
void cdp_periodic(vlib_main_t *vm)
Definition: cdp_periodic.c:350
snap_register_input_protocol
void snap_register_input_protocol(vlib_main_t *vm, char *name, u32 ieee_oui, u16 protocol, u32 node_index)
Definition: node.c:317
vlib_process_create
u32 vlib_process_create(vlib_main_t *vm, char *name, vlib_node_function_t *f, u32 log2_n_stack_bytes)
Create a vlib process.
Definition: node.c:795
vlib_frame_vector_args
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:301
uword
u64 uword
Definition: types.h:112
cdp_input_trace_t::data
u8 data[400]
Definition: cdp.h:132
CDP_EVENT_DISABLE
@ CDP_EVENT_DISABLE
Definition: cdp.h:138
f64
double f64
Definition: types.h:142
cm
vnet_feature_config_main_t * cm
Definition: nat44_ei_hairpinning.c:594
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
cdp.h
vlib_buffer_t::current_length
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:122
cdp_node_fn
static uword cdp_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: cdp_node.c:56
vlib_process_wait_for_event_or_clock
static f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds...
Definition: node_funcs.h:755
vlib_set_next_frame_buffer
static 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:428
u32
unsigned int u32
Definition: types.h:88
vlib_main_t
Definition: main.h:102
cdp_error_strings
static char * cdp_error_strings[]
Definition: cdp_node.c:34
vlib_add_trace
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:628
cdp_main_t::vlib_main
vlib_main_t * vlib_main
Definition: cdp.h:104
vlib_buffer_get_current
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:257
rt
vnet_interface_output_runtime_t * rt
Definition: interface_output.c:419
cdp_input_node
static vlib_node_registration_t cdp_input_node
(constructor) VLIB_REGISTER_NODE (cdp_input_node)
Definition: cdp_node.c:104
clib_warning
#define clib_warning(format, args...)
Definition: error.h:59
vlib_process_suspend_time_is_zero
static uword vlib_process_suspend_time_is_zero(f64 dt)
Returns TRUE if a process suspend time is less than 10us.
Definition: node_funcs.h:474
cdp_input_format_trace
u8 * cdp_input_format_trace(u8 *s, va_list *args)
Definition: cdp_input.c:484
cdp_input_trace_t
Definition: cdp.h:129
vlib_node_runtime_t
Definition: node.h:454
cdp_main_t
Definition: cdp.h:79
from
from
Definition: nat44_ei_hairpinning.c:415
CDP_N_ERROR
@ CDP_N_ERROR
Definition: cdp.h:125
n_left_from
n_left_from
Definition: nat44_ei_hairpinning.c:416
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
ethernet_register_input_type
void ethernet_register_input_type(vlib_main_t *vm, ethernet_type_t type, u32 node_index)
Definition: node.c:2274
vlib_buffer_t::flags
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,...
Definition: buffer.h:133
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169