FD.io VPP  v18.01.2-1-g9b554f3
Vector Packet Processing
ip4_punt_drop.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 <vnet/ip/ip.h>
17 #include <vnet/ip/ip_punt_drop.h>
18 #include <vnet/policer/policer.h>
20 
21 /* *INDENT-OFF* */
23 {
24  .arc_name = "ip4-punt",
25  .start_nodes = VNET_FEATURES ("ip4-punt"),
26 };
27 
29 {
30  .arc_name = "ip4-drop",
31  .start_nodes = VNET_FEATURES ("ip4-drop"),
32 };
33 /* *INDENT-ON* */
34 
35 u8 *
36 format_ip_punt_policer_trace (u8 * s, va_list * args)
37 {
38  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
39  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
40  ip_punt_policer_trace_t *t = va_arg (*args, ip_punt_policer_trace_t *);
41 
42  s = format (s, "policer_index %d next %d", t->policer_index, t->next);
43  return s;
44 }
45 
46 ip_punt_policer_t ip4_punt_policer_cfg = {
47  .policer_index = ~0,
48 };
49 
51 #define _(sym,string) string,
53 #undef _
54 };
55 
56 static uword
58  vlib_node_runtime_t * node, vlib_frame_t * frame)
59 {
60  return (ip_punt_policer (vm, node, frame,
61  vnet_feat_arc_ip4_punt.feature_arc_index,
62  ip4_punt_policer_cfg.policer_index));
63 }
64 
65 /* *INDENT-OFF* */
67  .function = ip4_punt_policer,
68  .name = "ip4-punt-policer",
69  .vector_size = sizeof (u32),
70  .n_next_nodes = IP_PUNT_POLICER_N_NEXT,
71  .format_trace = format_ip_punt_policer_trace,
73  .error_strings = ip4_punt_policer_error_strings,
74 
75  .next_nodes = {
76  [IP_PUNT_POLICER_NEXT_DROP] = "ip4-drop",
77  },
78 };
79 
82 
84  .arc_name = "ip4-punt",
85  .node_name = "ip4-punt-policer",
86  .runs_before = VNET_FEATURES("ip4-punt-redirect"),
87 };
88 /* *INDENT-ON* */
89 
90 u8 *
91 format_ip_punt_redirect_trace (u8 * s, va_list * args)
92 {
93  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
94  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
96  vnet_main_t *vnm = vnet_get_main ();
98 
100 
101  if (NULL != si)
102  s = format (s, "via %U on %U using adj:%d",
105  t->redirect.adj_index);
106  else
107  s = format (s, "via %U on %d using adj:%d",
110 
111  return s;
112 }
113 
114 /* *INDENT-OFF* */
115 ip_punt_redirect_t ip4_punt_redirect_cfg = {
116  .any_rx_sw_if_index = {
117  .tx_sw_if_index = ~0,
118  .adj_index = ADJ_INDEX_INVALID,
119  },
120 };
121 /* *INDENT-ON* */
122 
123 
124 #define foreach_ip4_punt_redirect_error \
125 _(DROP, "ip4 punt redirect drop")
126 
127 typedef enum
128 {
129 #define _(sym,str) IP4_PUNT_REDIRECT_ERROR_##sym,
131 #undef _
134 
136 #define _(sym,string) string,
138 #undef _
139 };
140 
141 static uword
143  vlib_node_runtime_t * node, vlib_frame_t * frame)
144 {
145  return (ip_punt_redirect (vm, node, frame,
146  vnet_feat_arc_ip4_punt.feature_arc_index,
147  &ip4_punt_redirect_cfg));
148 }
149 
150 /* *INDENT-OFF* */
152  .function = ip4_punt_redirect,
153  .name = "ip4-punt-redirect",
154  .vector_size = sizeof (u32),
155  .n_next_nodes = IP_PUNT_REDIRECT_N_NEXT,
156  .format_trace = format_ip_punt_redirect_trace,
158  .error_strings = ip4_punt_redirect_error_strings,
159 
160  /* edit / add dispositions here */
161  .next_nodes = {
162  [IP_PUNT_REDIRECT_NEXT_DROP] = "ip4-drop",
163  [IP_PUNT_REDIRECT_NEXT_TX] = "ip4-rewrite",
164  [IP_PUNT_REDIRECT_NEXT_ARP] = "ip4-arp",
165  },
166 };
167 
170 
172  .arc_name = "ip4-punt",
173  .node_name = "ip4-punt-redirect",
174  .runs_before = VNET_FEATURES("error-punt"),
175 };
176 /* *INDENT-ON* */
177 
178 static uword
180 {
181  if (node->flags & VLIB_NODE_FLAG_TRACE)
182  ip4_forward_next_trace (vm, node, frame, VLIB_TX);
183 
184  return ip_drop_or_punt (vm, node, frame,
185  vnet_feat_arc_ip4_drop.feature_arc_index);
186 
187 }
188 
189 static uword
191 {
192  if (node->flags & VLIB_NODE_FLAG_TRACE)
193  ip4_forward_next_trace (vm, node, frame, VLIB_TX);
194 
195  return ip_drop_or_punt (vm, node, frame,
196  vnet_feat_arc_ip4_punt.feature_arc_index);
197 }
198 
199 /* *INDENT-OFF* */
201 {
202  .function = ip4_drop,
203  .name = "ip4-drop",
204  .vector_size = sizeof (u32),
205  .format_trace = format_ip4_forward_next_trace,
206  .n_next_nodes = 1,
207  .next_nodes = {
208  [0] = "error-drop",
209  },
210 };
211 
213 
215 {
216  .function = ip4_punt,
217  .name = "ip4-punt",
218  .vector_size = sizeof (u32),
219  .format_trace = format_ip4_forward_next_trace,
220  .n_next_nodes = 1,
221  .next_nodes = {
222  [0] = "error-punt",
223  },
224 };
225 
226 VNET_FEATURE_INIT (ip4_punt_end_of_arc, static) = {
227  .arc_name = "ip4-punt",
228  .node_name = "error-punt",
229  .runs_before = 0, /* not before any other features */
230 };
231 
232 VNET_FEATURE_INIT (ip4_drop_end_of_arc, static) = {
233  .arc_name = "ip4-drop",
234  .node_name = "error-drop",
235  .runs_before = 0, /* not before any other features */
236 };
237 /* *INDENT-ON */
238 
239 void
240 ip4_punt_policer_add_del (u8 is_add, u32 policer_index)
241 {
242  ip4_punt_policer_cfg.policer_index = policer_index;
243 
244  vnet_feature_enable_disable ("ip4-punt", "ip4-punt-policer",
245  0, is_add, 0, 0);
246 }
247 
248 static clib_error_t *
250  unformat_input_t * main_input,
251  vlib_cli_command_t * cmd)
252 {
253  unformat_input_t _line_input, *line_input = &_line_input;
254  clib_error_t *error = 0;
255  u32 policer_index;
256  u8 is_add = 1;
257 
258  policer_index = ~0;
259 
260  if (!unformat_user (main_input, unformat_line_input, line_input))
261  return 0;
262 
263  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
264  {
265  if (unformat (line_input, "%d", &policer_index))
266  ;
267  else if (unformat (line_input, "del"))
268  is_add = 0;
269  else if (unformat (line_input, "add"))
270  is_add = 1;
271  else
272  {
273  error = unformat_parse_error (line_input);
274  goto done;
275  }
276  }
277 
278  if (is_add && ~0 == policer_index)
279  {
280  error = clib_error_return (0, "expected policer index `%U'",
281  format_unformat_error, line_input);
282  goto done;
283  }
284  if (!is_add)
285  policer_index = ~0;
286 
287  ip4_punt_policer_add_del(is_add, policer_index);
288 
289 done:
290  unformat_free (line_input);
291  return (error);
292 }
293 
294 /*?
295  *
296  * @cliexpar
297  * @cliexcmd{set ip punt policer <INDEX>}
298  ?*/
299 /* *INDENT-OFF* */
300 VLIB_CLI_COMMAND (ip4_punt_policer_command, static) =
301 {
302  .path = "ip punt policer",
303  .function = ip4_punt_police_cmd,
304  .short_help = "ip punt policer [add|del] <index>",
305 };
306 /* *INDENT-ON* */
307 
308 /*
309  * an uninitalised rx-redirect strcut used to pad the vector
310  */
311 ip_punt_redirect_rx_t uninit_rx_redirect = {
312  .tx_sw_if_index = ~0,
313  .adj_index = ADJ_INDEX_INVALID,
314 };
315 
316 void
318  u32 rx_sw_if_index,
319  ip_punt_redirect_rx_t * redirect,
320  fib_protocol_t fproto, vnet_link_t linkt)
321 {
323 
324  if (~0 == rx_sw_if_index)
325  {
326  cfg->any_rx_sw_if_index = *redirect;
327  new = &cfg->any_rx_sw_if_index;
328  }
329  else
330  {
332  rx_sw_if_index, uninit_rx_redirect);
333  cfg->redirect_by_rx_sw_if_index[rx_sw_if_index] = *redirect;
334  new = &cfg->redirect_by_rx_sw_if_index[rx_sw_if_index];
335  }
336 
337  new->adj_index = adj_nbr_add_or_lock (fproto, linkt,
338  &redirect->nh,
339  redirect->tx_sw_if_index);
340 }
341 
342 void
344 {
346 
347  if (~0 == rx_sw_if_index)
348  {
349  old = &cfg->any_rx_sw_if_index;
350  }
351  else
352  {
353  old = &cfg->redirect_by_rx_sw_if_index[rx_sw_if_index];
354  }
355 
356  if ((old == NULL) || (old->adj_index == ADJ_INDEX_INVALID))
357  return;
358 
359  adj_unlock (old->adj_index);
360  *old = uninit_rx_redirect;
361 }
362 
363 void
364 ip4_punt_redirect_add (u32 rx_sw_if_index,
365  u32 tx_sw_if_index, ip46_address_t * nh)
366 {
367  ip_punt_redirect_rx_t rx = {
368  .tx_sw_if_index = tx_sw_if_index,
369  .nh = *nh,
370  };
371 
372  ip_punt_redirect_add (&ip4_punt_redirect_cfg,
373  rx_sw_if_index, &rx, FIB_PROTOCOL_IP4, VNET_LINK_IP4);
374 
375  vnet_feature_enable_disable ("ip4-punt", "ip4-punt-redirect", 0, 1, 0, 0);
376 }
377 
378 void
379 ip4_punt_redirect_del (u32 rx_sw_if_index)
380 {
381  vnet_feature_enable_disable ("ip4-punt", "ip4-punt-redirect", 0, 0, 0, 0);
382 
383  ip_punt_redirect_del (&ip4_punt_redirect_cfg, rx_sw_if_index);
384 }
385 
386 static clib_error_t *
388  unformat_input_t * main_input,
389  vlib_cli_command_t * cmd)
390 {
391  unformat_input_t _line_input, *line_input = &_line_input;
392  clib_error_t *error = 0;
393  u32 rx_sw_if_index = 0;
394  u32 tx_sw_if_index = 0;
395  ip46_address_t nh;
396  vnet_main_t *vnm;
397  u8 is_add;
398 
399  is_add = 1;
400  vnm = vnet_get_main ();
401 
402  if (!unformat_user (main_input, unformat_line_input, line_input))
403  return 0;
404 
405  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
406  {
407  if (unformat (line_input, "del"))
408  is_add = 0;
409  else if (unformat (line_input, "add"))
410  is_add = 1;
411  else if (unformat (line_input, "rx all"))
412  rx_sw_if_index = ~0;
413  else if (unformat (line_input, "rx %U",
414  unformat_vnet_sw_interface, vnm, &rx_sw_if_index))
415  ;
416  else if (unformat (line_input, "via %U %U",
418  &nh.ip4,
419  unformat_vnet_sw_interface, vnm, &tx_sw_if_index))
420  ;
421  else if (unformat (line_input, "via %U",
422  unformat_vnet_sw_interface, vnm, &tx_sw_if_index))
423  memset (&nh, 0, sizeof (nh));
424  else
425  {
426  error = unformat_parse_error (line_input);
427  goto done;
428  }
429  }
430 
431  if (is_add)
432  {
433  if (rx_sw_if_index && tx_sw_if_index)
434  {
435  ip4_punt_redirect_add (rx_sw_if_index, tx_sw_if_index, &nh);
436  }
437  }
438  else
439  {
440  if (rx_sw_if_index)
441  {
442  ip4_punt_redirect_del (rx_sw_if_index);
443  }
444  }
445 
446 done:
447  unformat_free (line_input);
448  return (error);
449 }
450 
451 /*?
452  *
453  * @cliexpar
454  * @cliexcmd{set ip punt policer}
455  ?*/
456 /* *INDENT-OFF* */
457 VLIB_CLI_COMMAND (ip4_punt_redirect_command, static) =
458 {
459  .path = "ip punt redirect",
460  .function = ip4_punt_redirect_cmd,
461  .short_help = "ip punt redirect [add|del] rx [<interface>|all] via [<nh>] <tx_interface>",
462 };
463 /* *INDENT-ON* */
464 
465 u8 *
466 format_ip_punt_redirect (u8 * s, va_list * args)
467 {
468  ip_punt_redirect_t *cfg = va_arg (*args, ip_punt_redirect_t *);
470  u32 rx_sw_if_index;
471  vnet_main_t *vnm = vnet_get_main ();
472 
473  vec_foreach_index (rx_sw_if_index, cfg->redirect_by_rx_sw_if_index)
474  {
475  rx = &cfg->redirect_by_rx_sw_if_index[rx_sw_if_index];
476  if (~0 != rx->tx_sw_if_index)
477  {
478  s = format (s, " rx %U redirect via %U %U\n",
480  vnet_get_sw_interface (vnm, rx_sw_if_index),
484  }
485  }
486  if (~0 != cfg->any_rx_sw_if_index.tx_sw_if_index)
487  {
488  s = format (s, " rx all redirect via %U %U\n",
492  cfg->
493  any_rx_sw_if_index.tx_sw_if_index));
494  }
495 
496  return (s);
497 }
498 
499 static clib_error_t *
501  unformat_input_t * main_input,
502  vlib_cli_command_t * cmd)
503 {
504  vlib_cli_output (vm, "%U", format_ip_punt_redirect, &ip4_punt_redirect_cfg);
505 
506  return (NULL);
507 }
508 
509 /*?
510  *
511  * @cliexpar
512  * @cliexcmd{set ip punt redierect}
513  ?*/
514 /* *INDENT-OFF* */
515 VLIB_CLI_COMMAND (show_ip4_punt_redirect_command, static) =
516 {
517  .path = "show ip punt redirect",
518  .function = ip4_punt_redirect_show_cmd,
519  .short_help = "show ip punt redirect",
520  .is_mp_safe = 1,
521 };
522 /* *INDENT-ON* */
523 
524 /*
525  * fd.io coding-style-patch-verification: ON
526  *
527  * Local Variables:
528  * eval: (c-set-style "gnu")
529  * End:
530  */
#define vec_foreach_index(var, v)
Iterate over vector indices.
#define CLIB_UNUSED(x)
Definition: clib.h:79
VLIB_NODE_FUNCTION_MULTIARCH(ip4_punt_policer_node, ip4_punt_policer)
static clib_error_t * ip4_punt_redirect_show_cmd(vlib_main_t *vm, unformat_input_t *main_input, vlib_cli_command_t *cmd)
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
ip4_punt_redirect_error_t
static vlib_node_registration_t ip4_drop_node
(constructor) VLIB_REGISTER_NODE (ip4_drop_node)
#define NULL
Definition: clib.h:55
ip_punt_redirect_rx_t uninit_rx_redirect
ip_punt_redirect_rx_t * redirect_by_rx_sw_if_index
per-RX interface configuration
Definition: ip_punt_drop.h:225
static uword ip4_punt_policer(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: ip4_punt_drop.c:57
static uword ip_drop_or_punt(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, u8 arc_index)
Definition: ip_punt_drop.h:367
void ip_punt_redirect_del(ip_punt_redirect_t *cfg, u32 rx_sw_if_index)
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:983
format_function_t format_ip46_address
Definition: format.h:61
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
unformat_function_t unformat_vnet_sw_interface
void ip4_punt_policer_add_del(u8 is_add, u32 policer_index)
#define foreach_ip4_punt_redirect_error
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
u8 * format_ip_punt_redirect_trace(u8 *s, va_list *args)
Definition: ip4_punt_drop.c:91
unformat_function_t unformat_ip4_address
Definition: format.h:76
static vlib_node_registration_t ip4_punt_policer_node
(constructor) VLIB_REGISTER_NODE (ip4_punt_policer_node)
Definition: ip4_punt_drop.c:66
#define clib_error_return(e, args...)
Definition: error.h:99
void adj_unlock(adj_index_t adj_index)
Release a reference counting lock on the adjacency.
Definition: adj.c:239
ip46_address_t nh
The next-hop to send redirected packets to.
Definition: ip_punt_drop.h:199
#define foreach_ip_punt_policer_error
Definition: ip_punt_drop.h:44
static char * ip4_punt_redirect_error_strings[]
IP punt redirect configuration.
Definition: ip_punt_drop.h:215
static uword ip_punt_policer(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, u8 arc_index, u32 policer_index)
IP punt policing node function.
Definition: ip_punt_drop.h:61
unformat_function_t unformat_line_input
Definition: format.h:281
u8 * format_ip_punt_policer_trace(u8 *s, va_list *args)
Definition: ip4_punt_drop.c:36
format_function_t format_vnet_sw_interface_name
ip_punt_redirect_rx_t redirect
Definition: ip_punt_drop.h:244
#define ADJ_INDEX_INVALID
Invalid ADJ index - used when no adj is known likewise blazoned capitals INVALID speak volumes where ...
Definition: adj_types.h:36
static char * ip4_punt_policer_error_strings[]
Definition: ip4_punt_drop.c:50
struct _unformat_input_t unformat_input_t
static clib_error_t * ip4_punt_redirect_cmd(vlib_main_t *vm, unformat_input_t *main_input, vlib_cli_command_t *cmd)
static uword ip_punt_redirect(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, u8 arc_index, ip_punt_redirect_t *redirect)
Definition: ip_punt_drop.h:286
IP Punt redirect trace.
Definition: ip_punt_drop.h:242
static vlib_node_registration_t ip4_punt_node
(constructor) VLIB_REGISTER_NODE (ip4_punt_node)
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
ip_punt_redirect_rx_t any_rx_sw_if_index
any RX interface redirect
Definition: ip_punt_drop.h:220
vlib_main_t * vm
Definition: buffer.c:283
static uword ip4_punt_redirect(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
IP4 punt redirect per-rx interface configuration redirect punted traffic to another location...
Definition: ip_punt_drop.h:194
static vnet_sw_interface_t * vnet_get_sw_interface_safe(vnet_main_t *vnm, u32 sw_if_index)
static vlib_node_registration_t ip4_punt_redirect_node
(constructor) VLIB_REGISTER_NODE (ip4_punt_redirect_node)
u32 tx_sw_if_index
the TX interface to send redirected packets
Definition: ip_punt_drop.h:204
#define ARRAY_LEN(x)
Definition: clib.h:59
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
static uword ip4_punt(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
unsigned int u32
Definition: types.h:88
static uword ip4_drop(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
enum vnet_link_t_ vnet_link_t
Link Type: A description of the protocol of packets on the link.
IP4 punt policer configuration we police the punt rate to prevent overloading the host...
Definition: ip_punt_drop.h:27
static clib_error_t * ip4_punt_police_cmd(vlib_main_t *vm, unformat_input_t *main_input, vlib_cli_command_t *cmd)
void ip4_punt_redirect_del(u32 rx_sw_if_index)
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:259
#define VNET_FEATURES(...)
Definition: feature.h:368
u64 uword
Definition: types.h:112
#define unformat_parse_error(input)
Definition: format.h:267
Definition: defs.h:47
void ip4_punt_redirect_add(u32 rx_sw_if_index, u32 tx_sw_if_index, ip46_address_t *nh)
unsigned char u8
Definition: types.h:56
void ip_punt_redirect_add(ip_punt_redirect_t *cfg, u32 rx_sw_if_index, ip_punt_redirect_rx_t *redirect, fib_protocol_t fproto, vnet_link_t linkt)
Add a punt redirect entry.
void ip4_forward_next_trace(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, vlib_rx_or_tx_t which_adj_index)
Definition: ip4_forward.c:1316
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
u8 * format_ip4_forward_next_trace(u8 *s, va_list *args)
Definition: ip4_forward.c:1268
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
adj_index_t adj_nbr_add_or_lock(fib_protocol_t nh_proto, vnet_link_t link_type, const ip46_address_t *nh_addr, u32 sw_if_index)
Neighbour Adjacency sub-type.
Definition: adj_nbr.c:214
u8 * format_ip_punt_redirect(u8 *s, va_list *args)
u16 flags
Copy of main node flags.
Definition: node.h:450
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
Definition: vec.h:481
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:680
VNET_FEATURE_INIT(ip4_punt_policer_node, static)
adj_index_t adj_index
redirect forwarding adjacency
Definition: ip_punt_drop.h:209
VNET_FEATURE_ARC_INIT(ip4_punt)
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
int vnet_feature_enable_disable(const char *arc_name, const char *node_name, u32 sw_if_index, int enable_disable, void *feature_config, u32 n_feature_config_bytes)
Definition: feature.c:229
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169