FD.io VPP  v16.06
Vector Packet Processing
node_funcs.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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 
16 #include <stdint.h>
17 
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vnet/policer/policer.h>
21 
22 /* Dispatch functions meant to be instantiated elsewhere */
23 
24 typedef struct {
29 
30 /* packet trace format function */
31 static u8 * format_policer_trace (u8 * s, va_list * args)
32 {
33  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
34  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
35  vnet_policer_trace_t * t = va_arg (*args, vnet_policer_trace_t *);
36 
37  s = format (s, "VNET_POLICER: sw_if_index %d policer_index %d next %d",
39  return s;
40 }
41 
42 #define foreach_vnet_policer_error \
43 _(TRANSMIT, "Packets Transmitted") \
44 _(DROP, "Packets Dropped")
45 
46 typedef enum {
47 #define _(sym,str) VNET_POLICER_ERROR_##sym,
49 #undef _
52 
53 static char * vnet_policer_error_strings[] = {
54 #define _(sym,string) string,
56 #undef _
57 };
58 
59 static inline
61  vlib_node_runtime_t * node,
62  vlib_frame_t * frame,
64 {
65  u32 n_left_from, * from, * to_next;
66  vnet_policer_next_t next_index;
68  u64 time_in_policer_periods;
69  u32 transmitted = 0;
70 
71  time_in_policer_periods =
73 
74  from = vlib_frame_vector_args (frame);
75  n_left_from = frame->n_vectors;
76  next_index = node->cached_next_index;
77 
78  while (n_left_from > 0)
79  {
80  u32 n_left_to_next;
81 
82  vlib_get_next_frame (vm, node, next_index,
83  to_next, n_left_to_next);
84 
85  while (n_left_from >= 4 && n_left_to_next >= 2)
86  {
87  u32 bi0, bi1;
88  vlib_buffer_t * b0, * b1;
89  u32 next0, next1;
90  u32 sw_if_index0, sw_if_index1;
91  u32 pi0 = 0, pi1 = 0;
92  u32 len0, len1;
93  u32 col0, col1;
94  policer_read_response_type_st * pol0, * pol1;
95 
96  /* Prefetch next iteration. */
97  {
98  vlib_buffer_t * b2, * b3;
99 
100  b2 = vlib_get_buffer (vm, from[2]);
101  b3 = vlib_get_buffer (vm, from[3]);
102 
103  vlib_prefetch_buffer_header (b2, LOAD);
104  vlib_prefetch_buffer_header (b3, LOAD);
105  }
106 
107  /* speculatively enqueue b0 and b1 to the current next frame */
108  to_next[0] = bi0 = from[0];
109  to_next[1] = bi1 = from[1];
110  from += 2;
111  to_next += 2;
112  n_left_from -= 2;
113  n_left_to_next -= 2;
114 
115  b0 = vlib_get_buffer (vm, bi0);
116  b1 = vlib_get_buffer (vm, bi1);
117 
118  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
120 
121  sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
123 
124 
126  {
127  pi0 = pm->policer_index_by_sw_if_index[sw_if_index0];
128  pi1 = pm->policer_index_by_sw_if_index[sw_if_index1];
129  }
130 
131  if (which == VNET_POLICER_INDEX_BY_OPAQUE)
132  {
133  pi0 = vnet_buffer(b0)->policer.index;
134  pi1 = vnet_buffer(b1)->policer.index;
135  }
136 
137  if (which == VNET_POLICER_INDEX_BY_EITHER)
138  {
139  pi0 = vnet_buffer(b0)->policer.index;
140  pi0 = (pi0 != ~0) ? pi0 :
141  pm->policer_index_by_sw_if_index [sw_if_index0];
142  pi1 = vnet_buffer(b1)->policer.index;
143  pi1 = (pi1 != ~0) ? pi1 :
144  pm->policer_index_by_sw_if_index [sw_if_index1];
145  }
146 
147  len0 = vlib_buffer_length_in_chain (vm, b0);
148  pol0 = &pm->policers [pi0];
149  col0 = vnet_police_packet (pol0, len0,
150  POLICE_CONFORM /* no chaining */,
151  time_in_policer_periods);
152 
153  len1 = vlib_buffer_length_in_chain (vm, b1);
154  pol1 = &pm->policers [pi1];
155  col1 = vnet_police_packet (pol1, len1,
156  POLICE_CONFORM /* no chaining */,
157  time_in_policer_periods);
158 
159  if (PREDICT_FALSE(col0 > 0))
160  {
161  next0 = VNET_POLICER_NEXT_DROP;
162  b0->error = node->errors[VNET_POLICER_ERROR_DROP];
163  }
164  else
165  transmitted++;
166 
167  if (PREDICT_FALSE(col1 > 0))
168  {
169  next1 = VNET_POLICER_NEXT_DROP;
170  b1->error = node->errors[VNET_POLICER_ERROR_DROP];
171  }
172  else
173  transmitted++;
174 
175 
177  {
178  if (b0->flags & VLIB_BUFFER_IS_TRACED)
179  {
181  vlib_add_trace (vm, node, b0, sizeof (*t));
182  t->sw_if_index = sw_if_index0;
183  t->next_index = next0;
184  }
185  if (b1->flags & VLIB_BUFFER_IS_TRACED)
186  {
188  vlib_add_trace (vm, node, b1, sizeof (*t));
189  t->sw_if_index = sw_if_index1;
190  t->next_index = next1;
191  }
192  }
193 
194  /* verify speculative enqueues, maybe switch current next frame */
195  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
196  to_next, n_left_to_next,
197  bi0, bi1, next0, next1);
198  }
199 
200  while (n_left_from > 0 && n_left_to_next > 0)
201  {
202  u32 bi0;
203  vlib_buffer_t * b0;
204  u32 next0;
205  u32 sw_if_index0;
206  u32 pi0 = 0;
207  u32 len0;
208  u32 col0;
210 
211  bi0 = from[0];
212  to_next[0] = bi0;
213  from += 1;
214  to_next += 1;
215  n_left_from -= 1;
216  n_left_to_next -= 1;
217 
218  b0 = vlib_get_buffer (vm, bi0);
219 
220  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
222 
224  pi0 = pm->policer_index_by_sw_if_index[sw_if_index0];
225 
226  if (which == VNET_POLICER_INDEX_BY_OPAQUE)
227  pi0 = vnet_buffer(b0)->policer.index;
228 
229  if (which == VNET_POLICER_INDEX_BY_EITHER)
230  {
231  pi0 = vnet_buffer(b0)->policer.index;
232  pi0 = (pi0 != ~0) ? pi0 :
233  pm->policer_index_by_sw_if_index [sw_if_index0];
234  }
235 
236  len0 = vlib_buffer_length_in_chain (vm, b0);
237  pol0 = &pm->policers [pi0];
238  col0 = vnet_police_packet (pol0, len0,
239  POLICE_CONFORM /* no chaining */,
240  time_in_policer_periods);
241 
242  if (PREDICT_FALSE(col0 > 0))
243  {
244  next0 = VNET_POLICER_NEXT_DROP;
245  b0->error = node->errors[VNET_POLICER_ERROR_DROP];
246  }
247  else
248  {
249  transmitted++;
250  }
251 
253  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
254  {
256  vlib_add_trace (vm, node, b0, sizeof (*t));
257  t->sw_if_index = sw_if_index0;
258  t->next_index = next0;
259  t->policer_index = pi0;
260  }
261 
262  /* verify speculative enqueue, maybe switch current next frame */
263  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
264  to_next, n_left_to_next,
265  bi0, next0);
266  }
267 
268  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
269  }
270 
272  VNET_POLICER_ERROR_TRANSMIT,
273  transmitted);
274  return frame->n_vectors;
275 }
276 
278  vlib_node_runtime_t * node,
279  vlib_frame_t * frame)
280 {
281  return vnet_policer_inline (vm, node, frame,
283 }
284 
286  vlib_node_runtime_t * node,
287  vlib_frame_t * frame)
288 {
289  return vnet_policer_inline (vm, node, frame,
291 }
292 
294  vlib_node_runtime_t * node,
295  vlib_frame_t * frame)
296 {
297  return vnet_policer_inline (vm, node, frame,
299 }
300 
302 
303 
304 #define TEST_CODE 1
305 
306 #ifdef TEST_CODE
307 
309  .function = vnet_policer_by_sw_if_index,
310  .name = "policer-by-sw-if-index",
311  .vector_size = sizeof (u32),
312  .format_trace = format_policer_trace,
314 
316  .error_strings = vnet_policer_error_strings,
317 
318  .n_next_nodes = VNET_POLICER_N_NEXT,
319 
320  /* edit / add dispositions here */
321  .next_nodes = {
322  [VNET_POLICER_NEXT_TRANSMIT] = "ethernet-input",
323  [VNET_POLICER_NEXT_DROP] = "error-drop",
324  },
325 };
326 
327 
328 int test_policer_add_del (u32 rx_sw_if_index, u8 *config_name,
329  int is_add)
330 {
334  vnet_hw_interface_t * rxhi;
335  uword * p;
336 
337  rxhi = vnet_get_sup_hw_interface (pm->vnet_main, rx_sw_if_index);
338 
339  /* Make sure caller didn't pass a vlan subif, etc. */
340  if (rxhi->sw_if_index != rx_sw_if_index)
341  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
342 
343  if (is_add)
344  {
345 
346  p = hash_get_mem (pm->policer_config_by_name, config_name);
347 
348  if (p == 0)
349  return -2;
350 
351  template = pool_elt_at_index (pm->policer_templates, p[0]);
352 
354  (pm->vnet_main,
355  rxhi->hw_if_index,
357 
359 
360  policer[0] = template[0];
361 
362  vec_validate (pm->policer_index_by_sw_if_index, rx_sw_if_index);
363  pm->policer_index_by_sw_if_index[rx_sw_if_index]
364  = policer - pm->policers;
365  }
366  else
367  {
368  u32 pi;
370  rxhi->hw_if_index,
371  ~0 /* disable */);
372 
373  pi = pm->policer_index_by_sw_if_index[rx_sw_if_index];
374  pm->policer_index_by_sw_if_index[rx_sw_if_index] = ~0;
375  pool_put_index (pm->policers, pi);
376  }
377 
378  return 0;
379 }
380 
381 static clib_error_t *
383  unformat_input_t * input,
384  vlib_cli_command_t * cmd)
385 {
387  unformat_input_t _line_input, * line_input = &_line_input;
388  u32 rx_sw_if_index;
389  int rv;
390  u8 * config_name = 0;
391  int rx_set = 0;
392  int is_add = 1;
393  int is_show = 0;
394 
395  /* Get a line of input. */
396  if (! unformat_user (input, unformat_line_input, line_input))
397  return 0;
398 
399  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
400  {
401  if (unformat (line_input, "intfc %U", unformat_vnet_sw_interface,
402  pm->vnet_main, &rx_sw_if_index))
403  rx_set = 1;
404  else if (unformat (line_input, "show"))
405  is_show=1;
406  else if (unformat (line_input, "policer %s", &config_name))
407  ;
408  else if (unformat (line_input, "del"))
409  is_add = 0;
410  else break;
411  }
412 
413  if (rx_set == 0)
414  return clib_error_return (0, "interface not set");
415 
416  if (is_show)
417  {
418  u32 pi = pm->policer_index_by_sw_if_index[rx_sw_if_index];
420  policer = pool_elt_at_index (pm->policers, pi);
421 
422  vlib_cli_output (vm, "%U", format_policer_instance, policer);
423  return 0;
424  }
425 
426  if (is_add && config_name == 0)
427  {
428  return clib_error_return (0, "policer config name required");
429  }
430 
431  rv = test_policer_add_del (rx_sw_if_index, config_name, is_add);
432 
433  switch (rv)
434  {
435  case 0:
436  break;
437 
438  default:
439  return clib_error_return
440  (0, "WARNING: vnet_vnet_policer_add_del returned %d", rv);
441  }
442 
443  return 0;
444 }
445 
446 VLIB_CLI_COMMAND (test_patch_command, static) = {
447  .path = "test policer",
448  .short_help =
449  "intfc <intfc> policer <policer-config-name> [del]",
450  .function = test_policer_command_fn,
451 };
452 
453 
454 #endif /* TEST_CODE */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:394
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Definition: main.c:459
#define foreach_vnet_policer_error
Definition: node_funcs.c:42
#define CLIB_UNUSED(x)
Definition: clib.h:79
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
#define POLICER_TICKS_PER_PERIOD_SHIFT
Definition: police.h:56
vnet_policer_error_t
Definition: node_funcs.c:46
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
uword * policer_config_by_name
Definition: policer.h:33
int vnet_hw_interface_rx_redirect_to_node(vnet_main_t *vnm, u32 hw_if_index, u32 node_index)
Definition: interface.c:901
#define UNFORMAT_END_OF_INPUT
Definition: format.h:142
vnet_policer_next_t
Definition: policer.h:51
static uword vnet_policer_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, vnet_policer_index_t which)
Definition: node_funcs.c:60
int test_policer_add_del(u32 rx_sw_if_index, u8 *config_name, int is_add)
Definition: node_funcs.c:328
vnet_main_t * vnet_main
Definition: policer.h:40
unformat_function_t unformat_vnet_sw_interface
vlib_error_t * errors
Definition: node.h:378
u8 * format_policer_instance(u8 *s, va_list *va)
Definition: policer.c:68
always_inline uword unformat_check_input(unformat_input_t *i)
Definition: format.h:168
vlib_node_registration_t policer_by_sw_if_index_node
(constructor) VLIB_REGISTER_NODE (policer_by_sw_if_index_node)
Definition: node_funcs.c:308
uword vnet_policer_by_opaque(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node_funcs.c:285
static char * vnet_policer_error_strings[]
Definition: node_funcs.c:53
static policer_result_e vnet_police_packet(policer_read_response_type_st *policer, uint32_t packet_length, policer_result_e packet_color, uint64_t time)
Definition: police.h:86
always_inline uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:112
unsigned long u64
Definition: types.h:89
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:953
always_inline void * vlib_frame_vector_args(vlib_frame_t *f)
Definition: node_funcs.h:202
#define pool_elt_at_index(p, i)
Definition: pool.h:346
#define PREDICT_FALSE(x)
Definition: clib.h:97
always_inline void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:970
#define vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next, n_left_to_next, bi0, bi1, next0, next1)
Definition: buffer_node.h:43
policer_read_response_type_st * policers
Definition: policer.h:26
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Definition: buffer_node.h:83
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Definition: node_funcs.h:265
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:538
void vnet_policer_node_funcs_reference(void)
Definition: node_funcs.c:301
uword vnet_policer_by_sw_if_index(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node_funcs.c:277
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:129
uword vnet_policer_by_either(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node_funcs.c:293
u32 * policer_index_by_sw_if_index
Definition: policer.h:36
#define pool_get_aligned(P, E, A)
Definition: pool.h:155
u16 n_vectors
Definition: node.h:307
always_inline vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
#define ARRAY_LEN(x)
Definition: clib.h:59
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:150
u16 cached_next_index
Definition: node.h:422
#define pool_put_index(p, i)
Definition: pool.h:214
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:300
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:225
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:91
u64 uword
Definition: types.h:112
unsigned char u8
Definition: types.h:56
static clib_error_t * test_policer_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: node_funcs.c:382
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 hash_get_mem(h, key)
Definition: hash.h:251
policer_read_response_type_st * policer_templates
Definition: policer.h:30
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:162
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:140
#define clib_error_return(e, args...)
Definition: error.h:112
struct _unformat_input_t unformat_input_t
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:84
always_inline u64 clib_cpu_time_now(void)
Definition: time.h:71
unformat_function_t unformat_line_input
Definition: format.h:279
vnet_policer_main_t vnet_policer_main
Definition: policer.h:43
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 u8 * format_policer_trace(u8 *s, va_list *args)
Definition: node_funcs.c:31
vnet_policer_index_t
Definition: policer.h:45
Definition: defs.h:45