FD.io VPP  v20.01-48-g3e0dafb74
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 
28 VNET_FEATURE_ARC_INIT (ip4_drop) =
29 {
30  .arc_name = "ip4-drop",
31  .start_nodes = VNET_FEATURES ("ip4-drop", "ip4-not-enabled"),
32 };
33 /* *INDENT-ON* */
34 
36 
37 #ifndef CLIB_MARCH_VARIANT
38 u8 *
39 format_ip_punt_policer_trace (u8 * s, va_list * args)
40 {
41  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
42  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
43  ip_punt_policer_trace_t *t = va_arg (*args, ip_punt_policer_trace_t *);
44 
45  s = format (s, "policer_index %d next %d", t->policer_index, t->next);
46  return s;
47 }
48 
49 ip_punt_policer_t ip4_punt_policer_cfg = {
50  .policer_index = ~0,
51 };
52 #endif /* CLIB_MARCH_VARIANT */
53 
55 #define _(sym,string) string,
57 #undef _
58 };
59 
63 {
64  return (ip_punt_policer (vm, node, frame,
65  vnet_feat_arc_ip4_punt.feature_arc_index,
66  ip4_punt_policer_cfg.policer_index));
67 }
68 
69 /* *INDENT-OFF* */
71  .name = "ip4-punt-policer",
72  .vector_size = sizeof (u32),
73  .n_next_nodes = IP_PUNT_POLICER_N_NEXT,
74  .format_trace = format_ip_punt_policer_trace,
76  .error_strings = ip4_punt_policer_error_strings,
77 
78  .next_nodes = {
79  [IP_PUNT_POLICER_NEXT_DROP] = "ip4-drop",
80  },
81 };
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 
91 #define foreach_ip4_punt_redirect_error \
92 _(DROP, "ip4 punt redirect drop")
93 
94 typedef enum
95 {
96 #define _(sym,str) IP4_PUNT_REDIRECT_ERROR_##sym,
98 #undef _
101 
103 #define _(sym,string) string,
105 #undef _
106 };
107 
111 {
112  return (ip_punt_redirect (vm, node, frame,
113  vnet_feat_arc_ip4_punt.feature_arc_index,
115 }
116 
117 /* *INDENT-OFF* */
119  .name = "ip4-punt-redirect",
120  .vector_size = sizeof (u32),
121  .n_next_nodes = IP_PUNT_REDIRECT_N_NEXT,
122  .format_trace = format_ip_punt_redirect_trace,
124  .error_strings = ip4_punt_redirect_error_strings,
125 
126  /* edit / add dispositions here */
127  .next_nodes = {
128  [IP_PUNT_REDIRECT_NEXT_DROP] = "ip4-drop",
129  [IP_PUNT_REDIRECT_NEXT_TX] = "ip4-rewrite",
130  [IP_PUNT_REDIRECT_NEXT_ARP] = "ip4-arp",
131  },
132 };
133 
135  .arc_name = "ip4-punt",
136  .node_name = "ip4-punt-redirect",
137  .runs_before = VNET_FEATURES("error-punt"),
138 };
139 /* *INDENT-ON* */
140 
143 {
144  if (node->flags & VLIB_NODE_FLAG_TRACE)
146 
147  return ip_drop_or_punt (vm, node, frame,
148  vnet_feat_arc_ip4_drop.feature_arc_index);
149 
150 }
151 
155 {
156  if (node->flags & VLIB_NODE_FLAG_TRACE)
158 
159  return ip_drop_or_punt (vm, node, frame,
160  vnet_feat_arc_ip4_drop.feature_arc_index);
161 }
162 
163 static uword
165 {
166  if (node->flags & VLIB_NODE_FLAG_TRACE)
167  ip4_forward_next_trace (vm, node, frame, VLIB_TX);
168 
169  return ip_drop_or_punt (vm, node, frame,
170  vnet_feat_arc_ip4_punt.feature_arc_index);
171 }
172 
173 /* *INDENT-OFF* */
175 {
176  .name = "ip4-drop",
177  .vector_size = sizeof (u32),
178  .format_trace = format_ip4_forward_next_trace,
179  .n_next_nodes = 1,
180  .next_nodes = {
181  [0] = "error-drop",
182  },
183 };
184 
186 {
187  .name = "ip4-not-enabled",
188  .vector_size = sizeof (u32),
189  .format_trace = format_ip4_forward_next_trace,
190  .sibling_of = "ip4-drop",
191 };
192 
194 {
195  .function = ip4_punt,
196  .name = "ip4-punt",
197  .vector_size = sizeof (u32),
198  .format_trace = format_ip4_forward_next_trace,
199  .n_next_nodes = 1,
200  .next_nodes = {
201  [0] = "error-punt",
202  },
203 };
204 
205 VNET_FEATURE_INIT (ip4_punt_end_of_arc, static) = {
206  .arc_name = "ip4-punt",
207  .node_name = "error-punt",
208  .runs_before = 0, /* not before any other features */
209 };
210 
211 VNET_FEATURE_INIT (ip4_drop_end_of_arc, static) = {
212  .arc_name = "ip4-drop",
213  .node_name = "error-drop",
214  .runs_before = 0, /* not before any other features */
215 };
216 /* *INDENT-ON */
217 
218 #ifndef CLIB_MARCH_VARIANT
219 void
220 ip4_punt_policer_add_del (u8 is_add, u32 policer_index)
221 {
222  ip4_punt_policer_cfg.policer_index = policer_index;
223 
224  vnet_feature_enable_disable ("ip4-punt", "ip4-punt-policer",
225  0, is_add, 0, 0);
226 }
227 #endif /* CLIB_MARCH_VARIANT */
228 
229 static clib_error_t *
231  unformat_input_t * main_input,
232  vlib_cli_command_t * cmd)
233 {
234  unformat_input_t _line_input, *line_input = &_line_input;
235  clib_error_t *error = 0;
236  u32 policer_index;
237  u8 is_add = 1;
238 
239  policer_index = ~0;
240 
241  if (!unformat_user (main_input, unformat_line_input, line_input))
242  return 0;
243 
244  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
245  {
246  if (unformat (line_input, "%d", &policer_index))
247  ;
248  else if (unformat (line_input, "del"))
249  is_add = 0;
250  else if (unformat (line_input, "add"))
251  is_add = 1;
252  else
253  {
254  error = unformat_parse_error (line_input);
255  goto done;
256  }
257  }
258 
259  if (is_add && ~0 == policer_index)
260  {
261  error = clib_error_return (0, "expected policer index `%U'",
262  format_unformat_error, line_input);
263  goto done;
264  }
265  if (!is_add)
266  policer_index = ~0;
267 
268  ip4_punt_policer_add_del(is_add, policer_index);
269 
270 done:
271  unformat_free (line_input);
272  return (error);
273 }
274 
275 /*?
276  *
277  * @cliexpar
278  * @cliexcmd{set ip punt policer <INDEX>}
279  ?*/
280 /* *INDENT-OFF* */
281 VLIB_CLI_COMMAND (ip4_punt_policer_command, static) =
282 {
283  .path = "ip punt policer",
284  .function = ip4_punt_police_cmd,
285  .short_help = "ip punt policer [add|del] <index>",
286 };
287 /* *INDENT-ON* */
288 
289 #ifndef CLIB_MARCH_VARIANT
290 
291 void
292 ip4_punt_redirect_add (u32 rx_sw_if_index,
293  u32 tx_sw_if_index, ip46_address_t * nh)
294 {
295  /* *INDENT-OFF* */
296  fib_route_path_t *rpaths = NULL, rpath = {
297  .frp_proto = DPO_PROTO_IP4,
298  .frp_addr = *nh,
299  .frp_sw_if_index = tx_sw_if_index,
300  .frp_weight = 1,
301  .frp_fib_index = ~0,
302  };
303  /* *INDENT-ON* */
304 
305  vec_add1 (rpaths, rpath);
306 
307  ip4_punt_redirect_add_paths (rx_sw_if_index, rpaths);
308 
309  vec_free (rpaths);
310 }
311 
312 void
314 {
316  rx_sw_if_index,
318 
319  vnet_feature_enable_disable ("ip4-punt", "ip4-punt-redirect", 0, 1, 0, 0);
320 }
321 
322 void
323 ip4_punt_redirect_del (u32 rx_sw_if_index)
324 {
325  vnet_feature_enable_disable ("ip4-punt", "ip4-punt-redirect", 0, 0, 0, 0);
326 
327  ip_punt_redirect_del (FIB_PROTOCOL_IP4, rx_sw_if_index);
328 }
329 #endif /* CLIB_MARCH_VARIANT */
330 
331 static clib_error_t *
333  unformat_input_t * main_input,
334  vlib_cli_command_t * cmd)
335 {
336  unformat_input_t _line_input, *line_input = &_line_input;
337  fib_route_path_t *rpaths = NULL, rpath;
338  dpo_proto_t payload_proto;
339  clib_error_t *error = 0;
340  u32 rx_sw_if_index = ~0;
341  vnet_main_t *vnm;
342  u8 is_add;
343 
344  is_add = 1;
345  vnm = vnet_get_main ();
346 
347  if (!unformat_user (main_input, unformat_line_input, line_input))
348  return 0;
349 
350  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
351  {
352  if (unformat (line_input, "del"))
353  is_add = 0;
354  else if (unformat (line_input, "add"))
355  is_add = 1;
356  else if (unformat (line_input, "rx all"))
357  rx_sw_if_index = ~0;
358  else if (unformat (line_input, "rx %U",
359  unformat_vnet_sw_interface, vnm, &rx_sw_if_index))
360  ;
361  else if (unformat (line_input, "via %U",
362  unformat_fib_route_path, &rpath, &payload_proto))
363  vec_add1 (rpaths, rpath);
364  else
365  {
366  error = unformat_parse_error (line_input);
367  goto done;
368  }
369  }
370 
371  if (~0 == rx_sw_if_index)
372  {
373  error = unformat_parse_error (line_input);
374  goto done;
375  }
376 
377  if (is_add)
378  {
379  if (vec_len (rpaths))
380  ip4_punt_redirect_add_paths (rx_sw_if_index, rpaths);
381  }
382  else
383  {
384  ip4_punt_redirect_del (rx_sw_if_index);
385  }
386 
387 done:
388  unformat_free (line_input);
389  return (error);
390 }
391 
392 /*?
393  *
394  * @cliexpar
395  * @cliexcmd{set ip punt policer}
396  ?*/
397 /* *INDENT-OFF* */
398 VLIB_CLI_COMMAND (ip4_punt_redirect_command, static) =
399 {
400  .path = "ip punt redirect",
401  .function = ip4_punt_redirect_cmd,
402  .short_help = "ip punt redirect [add|del] rx [<interface>|all] via [<nh>] <tx_interface>",
403 };
404 /* *INDENT-ON* */
405 
406 static clib_error_t *
408  unformat_input_t * main_input,
409  vlib_cli_command_t * cmd)
410 {
412 
413  return (NULL);
414 }
415 
416 /*?
417  *
418  * @cliexpar
419  * @cliexcmd{set ip punt redierect}
420  ?*/
421 /* *INDENT-OFF* */
422 VLIB_CLI_COMMAND (show_ip4_punt_redirect_command, static) =
423 {
424  .path = "show ip punt redirect",
425  .function = ip4_punt_redirect_show_cmd,
426  .short_help = "show ip punt redirect",
427  .is_mp_safe = 1,
428 };
429 /* *INDENT-ON* */
430 
431 /*
432  * fd.io coding-style-patch-verification: ON
433  *
434  * Local Variables:
435  * eval: (c-set-style "gnu")
436  * End:
437  */
#define CLIB_UNUSED(x)
Definition: clib.h:82
A representation of a path as described by a route producer.
Definition: fib_types.h:485
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:46
ip4_punt_redirect_error_t
Definition: ip4_punt_drop.c:94
vl_api_fib_path_nh_t nh
Definition: fib_types.api:126
uword unformat_fib_route_path(unformat_input_t *input, va_list *args)
Unformat a fib_route_path_t from CLI input.
Definition: fib_types.c:457
vlib_node_registration_t ip4_drop_node
(constructor) VLIB_REGISTER_NODE (ip4_drop_node)
#define NULL
Definition: clib.h:58
u8 * format_ip_punt_redirect_trace(u8 *s, va_list *args)
Definition: ip_punt_drop.c:25
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:523
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:358
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
Contribute an object that is to be used to forward IP4 packets.
Definition: fib_types.h:109
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
unformat_function_t unformat_vnet_sw_interface
#define VLIB_NODE_FN(node)
Definition: node.h:202
void ip_punt_redirect_add(fib_protocol_t fproto, u32 rx_sw_if_index, fib_forward_chain_type_t ct, fib_route_path_t *rpaths)
Add a punt redirect entry.
Definition: ip_punt_drop.c:74
void ip4_punt_policer_add_del(u8 is_add, u32 policer_index)
unsigned char u8
Definition: types.h:56
#define foreach_ip4_punt_redirect_error
Definition: ip4_punt_drop.c:91
u8 * format_ip_punt_redirect(u8 *s, va_list *args)
Definition: ip_punt_drop.c:131
vlib_node_registration_t ip4_punt_policer_node
(constructor) VLIB_REGISTER_NODE (ip4_punt_policer_node)
Definition: ip4_punt_drop.c:70
vlib_node_registration_t ip4_not_enabled_node
(constructor) VLIB_REGISTER_NODE (ip4_not_enabled_node)
ip_punt_policer_t ip4_punt_policer_cfg
Definition: ip4_punt_drop.c:49
#define clib_error_return(e, args...)
Definition: error.h:99
unsigned int u32
Definition: types.h:88
#define foreach_ip_punt_policer_error
Definition: ip_punt_drop.h:44
enum dpo_proto_t_ dpo_proto_t
Data path protocol.
static char * ip4_punt_redirect_error_strings[]
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:283
u8 * format_ip_punt_policer_trace(u8 *s, va_list *args)
Definition: ip4_punt_drop.c:39
static char * ip4_punt_policer_error_strings[]
Definition: ip4_punt_drop.c:54
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)
vlib_main_t * vm
Definition: in2out_ed.c:1810
vlib_node_registration_t ip4_punt_node
(constructor) VLIB_REGISTER_NODE (ip4_punt_node)
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:342
vlib_node_registration_t ip4_punt_redirect_node
(constructor) VLIB_REGISTER_NODE (ip4_punt_redirect_node)
#define ARRAY_LEN(x)
Definition: clib.h:62
VNET_FEATURE_INIT(ip4_punt_policer_node)
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1810
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:152
vl_api_interface_index_t tx_sw_if_index
Definition: ip.api:415
static uword ip4_punt(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
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 VNET_FEATURES(...)
Definition: feature.h:442
static uword ip_punt_redirect(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, u8 arc_index, fib_protocol_t fproto)
Definition: ip_punt_drop.h:277
#define unformat_parse_error(input)
Definition: format.h:269
Definition: defs.h:47
void ip_punt_redirect_del(fib_protocol_t fproto, u32 rx_sw_if_index)
Definition: ip_punt_drop.c:107
void ip4_punt_redirect_add(u32 rx_sw_if_index, u32 tx_sw_if_index, ip46_address_t *nh)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
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:1214
u64 uword
Definition: types.h:112
static void unformat_free(unformat_input_t *i)
Definition: format.h:163
u8 * format_ip4_forward_next_trace(u8 *s, va_list *args)
Definition: ip4_forward.c:1164
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1811
u16 flags
Copy of main node flags.
Definition: node.h:509
void ip4_punt_redirect_add_paths(u32 rx_sw_if_index, fib_route_path_t *rpaths)
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:302
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:689
VNET_FEATURE_ARC_INIT(ip4_punt)
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
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:304
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171