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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/ip/ip.h>
17 #include <vnet/ip/ip_punt_drop.h>
18 #include <vnet/policer/policer.h>
19 #include <vnet/policer/police_inlines.h>
22 VNET_FEATURE_ARC_INIT (ip4_punt) =
24 .arc_name = "ip4-punt",
25 .start_nodes = VNET_FEATURES ("ip4-punt"),
28 VNET_FEATURE_ARC_INIT (ip4_drop) =
30 .arc_name = "ip4-drop",
31 .start_nodes = VNET_FEATURES ("ip4-drop"),
36 format_ip_punt_policer_trace (u8 * s, va_list * args)
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 *);
42 s = format (s, "policer_index %d next %d", t->policer_index, t->next);
46 ip_punt_policer_t ip4_punt_policer_cfg = {
50 static char *ip4_punt_policer_error_strings[] = {
51 #define _(sym,string) string,
52 foreach_ip_punt_policer_error
57 ip4_punt_policer (vlib_main_t * vm,
58 vlib_node_runtime_t * node, vlib_frame_t * frame)
60 return (ip_punt_policer (vm, node, frame,
61 vnet_feat_arc_ip4_punt.feature_arc_index,
62 ip4_punt_policer_cfg.policer_index));
66 VLIB_REGISTER_NODE (ip4_punt_policer_node, static) = {
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,
72 .n_errors = ARRAY_LEN(ip4_punt_policer_error_strings),
73 .error_strings = ip4_punt_policer_error_strings,
76 [IP_PUNT_POLICER_NEXT_DROP] = "ip4-drop",
80 VLIB_NODE_FUNCTION_MULTIARCH (ip4_punt_policer_node,
83 VNET_FEATURE_INIT (ip4_punt_policer_node, static) = {
84 .arc_name = "ip4-punt",
85 .node_name = "ip4-punt-policer",
86 .runs_before = VNET_FEATURES("ip4-punt-redirect"),
91 format_ip_punt_redirect_trace (u8 * s, va_list * args)
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 *);
95 ip_punt_redirect_trace_t *t = va_arg (*args, ip_punt_redirect_trace_t *);
96 vnet_main_t *vnm = vnet_get_main ();
97 vnet_sw_interface_t *si;
99 si = vnet_get_sw_interface_safe (vnm, t->redirect.tx_sw_if_index);
102 s = format (s, "via %U on %U using adj:%d",
103 format_ip46_address, &t->redirect.nh, IP46_TYPE_ANY,
104 format_vnet_sw_interface_name, vnm, si,
105 t->redirect.adj_index);
107 s = format (s, "via %U on %d using adj:%d",
108 format_ip46_address, &t->redirect.nh, IP46_TYPE_ANY,
109 t->redirect.tx_sw_if_index, t->redirect.adj_index);
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,
124 #define foreach_ip4_punt_redirect_error \
125 _(DROP, "ip4 punt redirect drop")
129 #define _(sym,str) IP4_PUNT_REDIRECT_ERROR_##sym,
130 foreach_ip4_punt_redirect_error
132 IP4_PUNT_REDIRECT_N_ERROR,
133 } ip4_punt_redirect_error_t;
135 static char *ip4_punt_redirect_error_strings[] = {
136 #define _(sym,string) string,
137 foreach_ip4_punt_redirect_error
142 ip4_punt_redirect (vlib_main_t * vm,
143 vlib_node_runtime_t * node, vlib_frame_t * frame)
145 return (ip_punt_redirect (vm, node, frame,
146 vnet_feat_arc_ip4_punt.feature_arc_index,
147 &ip4_punt_redirect_cfg));
151 VLIB_REGISTER_NODE (ip4_punt_redirect_node, static) = {
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,
157 .n_errors = ARRAY_LEN(ip4_punt_redirect_error_strings),
158 .error_strings = ip4_punt_redirect_error_strings,
160 /* edit / add dispositions here */
162 [IP_PUNT_REDIRECT_NEXT_DROP] = "ip4-drop",
163 [IP_PUNT_REDIRECT_NEXT_TX] = "ip4-rewrite",
164 [IP_PUNT_REDIRECT_NEXT_ARP] = "ip4-arp",
168 VLIB_NODE_FUNCTION_MULTIARCH (ip4_punt_redirect_node,
171 VNET_FEATURE_INIT (ip4_punt_redirect_node, static) = {
172 .arc_name = "ip4-punt",
173 .node_name = "ip4-punt-redirect",
174 .runs_before = VNET_FEATURES("error-punt"),
179 ip4_drop (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
181 if (node->flags & VLIB_NODE_FLAG_TRACE)
182 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
184 return ip_drop_or_punt (vm, node, frame,
185 vnet_feat_arc_ip4_drop.feature_arc_index);
190 ip4_punt (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
192 if (node->flags & VLIB_NODE_FLAG_TRACE)
193 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
195 return ip_drop_or_punt (vm, node, frame,
196 vnet_feat_arc_ip4_punt.feature_arc_index);
200 VLIB_REGISTER_NODE (ip4_drop_node, static) =
202 .function = ip4_drop,
204 .vector_size = sizeof (u32),
205 .format_trace = format_ip4_forward_next_trace,
212 VLIB_NODE_FUNCTION_MULTIARCH (ip4_drop_node, ip4_drop);
214 VLIB_REGISTER_NODE (ip4_punt_node, static) =
216 .function = ip4_punt,
218 .vector_size = sizeof (u32),
219 .format_trace = format_ip4_forward_next_trace,
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 */
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 */
240 ip4_punt_policer_add_del (u8 is_add, u32 policer_index)
242 ip4_punt_policer_cfg.policer_index = policer_index;
244 vnet_feature_enable_disable ("ip4-punt", "ip4-punt-policer",
248 static clib_error_t *
249 ip4_punt_police_cmd (vlib_main_t * vm,
250 unformat_input_t * main_input,
251 vlib_cli_command_t * cmd)
253 unformat_input_t _line_input, *line_input = &_line_input;
254 clib_error_t *error = 0;
260 if (!unformat_user (main_input, unformat_line_input, line_input))
263 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
265 if (unformat (line_input, "%d", &policer_index))
267 else if (unformat (line_input, "del"))
269 else if (unformat (line_input, "add"))
273 error = unformat_parse_error (line_input);
278 if (is_add && ~0 == policer_index)
280 error = clib_error_return (0, "expected policer index `%U'",
281 format_unformat_error, line_input);
287 ip4_punt_policer_add_del(is_add, policer_index);
290 unformat_free (line_input);
297 * @cliexcmd{set ip punt policer <INDEX>}
300 VLIB_CLI_COMMAND (ip4_punt_policer_command, static) =
302 .path = "ip punt policer",
303 .function = ip4_punt_police_cmd,
304 .short_help = "ip punt policer [add|del] <index>",
309 * an uninitalised rx-redirect strcut used to pad the vector
311 ip_punt_redirect_rx_t uninit_rx_redirect = {
312 .tx_sw_if_index = ~0,
313 .adj_index = ADJ_INDEX_INVALID,
317 ip_punt_redirect_add (ip_punt_redirect_t * cfg,
319 ip_punt_redirect_rx_t * redirect,
320 fib_protocol_t fproto, vnet_link_t linkt)
322 ip_punt_redirect_rx_t *new;
324 if (~0 == rx_sw_if_index)
326 cfg->any_rx_sw_if_index = *redirect;
327 new = &cfg->any_rx_sw_if_index;
331 vec_validate_init_empty (cfg->redirect_by_rx_sw_if_index,
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];
337 new->adj_index = adj_nbr_add_or_lock (fproto, linkt,
339 redirect->tx_sw_if_index);
343 ip_punt_redirect_del (ip_punt_redirect_t * cfg, u32 rx_sw_if_index)
345 ip_punt_redirect_rx_t *old;
347 if (~0 == rx_sw_if_index)
349 old = &cfg->any_rx_sw_if_index;
353 old = &cfg->redirect_by_rx_sw_if_index[rx_sw_if_index];
356 if ((old == NULL) || (old->adj_index == ADJ_INDEX_INVALID))
359 adj_unlock (old->adj_index);
360 *old = uninit_rx_redirect;
364 ip4_punt_redirect_add (u32 rx_sw_if_index,
365 u32 tx_sw_if_index, ip46_address_t * nh)
367 ip_punt_redirect_rx_t rx = {
368 .tx_sw_if_index = tx_sw_if_index,
372 ip_punt_redirect_add (&ip4_punt_redirect_cfg,
373 rx_sw_if_index, &rx, FIB_PROTOCOL_IP4, VNET_LINK_IP4);
375 vnet_feature_enable_disable ("ip4-punt", "ip4-punt-redirect", 0, 1, 0, 0);
379 ip4_punt_redirect_del (u32 rx_sw_if_index)
381 vnet_feature_enable_disable ("ip4-punt", "ip4-punt-redirect", 0, 0, 0, 0);
383 ip_punt_redirect_del (&ip4_punt_redirect_cfg, rx_sw_if_index);
386 static clib_error_t *
387 ip4_punt_redirect_cmd (vlib_main_t * vm,
388 unformat_input_t * main_input,
389 vlib_cli_command_t * cmd)
391 unformat_input_t _line_input, *line_input = &_line_input;
392 clib_error_t *error = 0;
400 vnm = vnet_get_main ();
402 if (!unformat_user (main_input, unformat_line_input, line_input))
405 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
407 if (unformat (line_input, "del"))
409 else if (unformat (line_input, "add"))
411 else if (unformat (line_input, "rx all"))
413 else if (unformat (line_input, "rx %U",
414 unformat_vnet_sw_interface, vnm, &rx_sw_if_index))
416 else if (unformat (line_input, "via %U %U",
417 unformat_ip4_address,
419 unformat_vnet_sw_interface, vnm, &tx_sw_if_index))
421 else if (unformat (line_input, "via %U",
422 unformat_vnet_sw_interface, vnm, &tx_sw_if_index))
423 memset (&nh, 0, sizeof (nh));
426 error = unformat_parse_error (line_input);
432 ip4_punt_redirect_add (rx_sw_if_index, tx_sw_if_index, &nh);
434 ip4_punt_redirect_del (rx_sw_if_index);
437 unformat_free (line_input);
444 * @cliexcmd{set ip punt policer}
447 VLIB_CLI_COMMAND (ip4_punt_redirect_command, static) =
449 .path = "ip punt redirect",
450 .function = ip4_punt_redirect_cmd,
451 .short_help = "ip punt redirect [add|del] rx [<interface>|all] via [<nh>] <tx_interface>",
456 format_ip_punt_redirect (u8 * s, va_list * args)
458 ip_punt_redirect_t *cfg = va_arg (*args, ip_punt_redirect_t *);
459 ip_punt_redirect_rx_t *rx;
461 vnet_main_t *vnm = vnet_get_main ();
463 vec_foreach_index (rx_sw_if_index, cfg->redirect_by_rx_sw_if_index)
465 rx = &cfg->redirect_by_rx_sw_if_index[rx_sw_if_index];
466 if (~0 != rx->tx_sw_if_index)
468 s = format (s, " rx %U redirect via %U %U\n",
469 format_vnet_sw_interface_name, vnm,
470 vnet_get_sw_interface (vnm, rx_sw_if_index),
471 format_ip46_address, &rx->nh, IP46_TYPE_ANY,
472 format_vnet_sw_interface_name, vnm,
473 vnet_get_sw_interface (vnm, rx->tx_sw_if_index));
476 if (~0 != cfg->any_rx_sw_if_index.tx_sw_if_index)
478 s = format (s, " rx all redirect via %U %U\n",
479 format_ip46_address, &cfg->any_rx_sw_if_index.nh,
480 IP46_TYPE_ANY, format_vnet_sw_interface_name, vnm,
481 vnet_get_sw_interface (vnm,
483 any_rx_sw_if_index.tx_sw_if_index));
489 static clib_error_t *
490 ip4_punt_redirect_show_cmd (vlib_main_t * vm,
491 unformat_input_t * main_input,
492 vlib_cli_command_t * cmd)
494 vlib_cli_output (vm, "%U", format_ip_punt_redirect, &ip4_punt_redirect_cfg);
502 * @cliexcmd{set ip punt redierect}
505 VLIB_CLI_COMMAND (show_ip4_punt_redirect_command, static) =
507 .path = "show ip punt redirect",
508 .function = ip4_punt_redirect_show_cmd,
509 .short_help = "show ip punt redirect",
515 * fd.io coding-style-patch-verification: ON
518 * eval: (c-set-style "gnu")