blob: 3600d7cb9d3cddb2f5aabb601f14d9e856b84307 [file] [log] [blame]
Neale Rannsd91c1db2017-07-31 02:30:50 -07001/*
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>
19#include <vnet/policer/police_inlines.h>
20
21/* *INDENT-OFF* */
22VNET_FEATURE_ARC_INIT (ip4_punt) =
23{
24 .arc_name = "ip4-punt",
25 .start_nodes = VNET_FEATURES ("ip4-punt"),
26};
27
28VNET_FEATURE_ARC_INIT (ip4_drop) =
29{
30 .arc_name = "ip4-drop",
Neale Ranns8269d3d2018-01-30 09:02:20 -080031 .start_nodes = VNET_FEATURES ("ip4-drop", "ip4-not-enabled"),
Neale Rannsd91c1db2017-07-31 02:30:50 -070032};
33/* *INDENT-ON* */
34
35u8 *
36format_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
46ip_punt_policer_t ip4_punt_policer_cfg = {
47 .policer_index = ~0,
48};
49
50static char *ip4_punt_policer_error_strings[] = {
51#define _(sym,string) string,
52 foreach_ip_punt_policer_error
53#undef _
54};
55
56static uword
57ip4_punt_policer (vlib_main_t * vm,
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* */
66VLIB_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,
74
75 .next_nodes = {
76 [IP_PUNT_POLICER_NEXT_DROP] = "ip4-drop",
77 },
78};
79
80VLIB_NODE_FUNCTION_MULTIARCH (ip4_punt_policer_node,
81 ip4_punt_policer);
82
83VNET_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"),
87};
88/* *INDENT-ON* */
89
90u8 *
91format_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 *);
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;
98
99 si = vnet_get_sw_interface_safe (vnm, t->redirect.tx_sw_if_index);
100
101 if (NULL != si)
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);
106 else
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);
110
111 return s;
112}
113
114/* *INDENT-OFF* */
115ip_punt_redirect_t ip4_punt_redirect_cfg = {
116 .any_rx_sw_if_index = {
117 .tx_sw_if_index = ~0,
Swarup Nayak4112e382017-12-11 17:36:54 +0530118 .adj_index = ADJ_INDEX_INVALID,
Neale Rannsd91c1db2017-07-31 02:30:50 -0700119 },
120};
121/* *INDENT-ON* */
122
123
124#define foreach_ip4_punt_redirect_error \
125_(DROP, "ip4 punt redirect drop")
126
127typedef enum
128{
129#define _(sym,str) IP4_PUNT_REDIRECT_ERROR_##sym,
130 foreach_ip4_punt_redirect_error
131#undef _
132 IP4_PUNT_REDIRECT_N_ERROR,
133} ip4_punt_redirect_error_t;
134
135static char *ip4_punt_redirect_error_strings[] = {
136#define _(sym,string) string,
137 foreach_ip4_punt_redirect_error
138#undef _
139};
140
141static uword
142ip4_punt_redirect (vlib_main_t * vm,
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* */
151VLIB_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,
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
168VLIB_NODE_FUNCTION_MULTIARCH (ip4_punt_redirect_node,
169 ip4_punt_redirect);
170
171VNET_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"),
175};
176/* *INDENT-ON* */
177
178static uword
179ip4_drop (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
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
189static uword
Neale Ranns8269d3d2018-01-30 09:02:20 -0800190ip4_not_enabled (vlib_main_t * vm,
191 vlib_node_runtime_t * node, vlib_frame_t * frame)
192{
193 if (node->flags & VLIB_NODE_FLAG_TRACE)
194 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
195
196 return ip_drop_or_punt (vm, node, frame,
197 vnet_feat_arc_ip4_drop.feature_arc_index);
198}
199
200static uword
Neale Rannsd91c1db2017-07-31 02:30:50 -0700201ip4_punt (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
202{
203 if (node->flags & VLIB_NODE_FLAG_TRACE)
204 ip4_forward_next_trace (vm, node, frame, VLIB_TX);
205
206 return ip_drop_or_punt (vm, node, frame,
207 vnet_feat_arc_ip4_punt.feature_arc_index);
208}
209
210/* *INDENT-OFF* */
211VLIB_REGISTER_NODE (ip4_drop_node, static) =
212{
213 .function = ip4_drop,
214 .name = "ip4-drop",
215 .vector_size = sizeof (u32),
216 .format_trace = format_ip4_forward_next_trace,
217 .n_next_nodes = 1,
218 .next_nodes = {
219 [0] = "error-drop",
220 },
221};
222
223VLIB_NODE_FUNCTION_MULTIARCH (ip4_drop_node, ip4_drop);
224
Neale Ranns8269d3d2018-01-30 09:02:20 -0800225VLIB_REGISTER_NODE (ip4_not_enabled_node, static) =
226{
227 .function = ip4_not_enabled,
228 .name = "ip4-not-enabled",
229 .vector_size = sizeof (u32),
230 .format_trace = format_ip4_forward_next_trace,
231 .n_next_nodes = 1,
232 .next_nodes = {
233 [0] = "error-drop",
234 },
235};
236
237VLIB_NODE_FUNCTION_MULTIARCH (ip4_not_enabled_node, ip4_not_enabled);
238
Neale Rannsd91c1db2017-07-31 02:30:50 -0700239VLIB_REGISTER_NODE (ip4_punt_node, static) =
240{
241 .function = ip4_punt,
242 .name = "ip4-punt",
243 .vector_size = sizeof (u32),
244 .format_trace = format_ip4_forward_next_trace,
245 .n_next_nodes = 1,
246 .next_nodes = {
247 [0] = "error-punt",
248 },
249};
250
251VNET_FEATURE_INIT (ip4_punt_end_of_arc, static) = {
252 .arc_name = "ip4-punt",
253 .node_name = "error-punt",
254 .runs_before = 0, /* not before any other features */
255};
256
257VNET_FEATURE_INIT (ip4_drop_end_of_arc, static) = {
258 .arc_name = "ip4-drop",
259 .node_name = "error-drop",
260 .runs_before = 0, /* not before any other features */
261};
262/* *INDENT-ON */
263
264void
265ip4_punt_policer_add_del (u8 is_add, u32 policer_index)
266{
267 ip4_punt_policer_cfg.policer_index = policer_index;
268
269 vnet_feature_enable_disable ("ip4-punt", "ip4-punt-policer",
270 0, is_add, 0, 0);
271}
272
273static clib_error_t *
274ip4_punt_police_cmd (vlib_main_t * vm,
275 unformat_input_t * main_input,
276 vlib_cli_command_t * cmd)
277{
278 unformat_input_t _line_input, *line_input = &_line_input;
279 clib_error_t *error = 0;
280 u32 policer_index;
281 u8 is_add = 1;
282
283 policer_index = ~0;
284
285 if (!unformat_user (main_input, unformat_line_input, line_input))
286 return 0;
287
288 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
289 {
290 if (unformat (line_input, "%d", &policer_index))
291 ;
292 else if (unformat (line_input, "del"))
293 is_add = 0;
294 else if (unformat (line_input, "add"))
295 is_add = 1;
296 else
297 {
298 error = unformat_parse_error (line_input);
299 goto done;
300 }
301 }
302
303 if (is_add && ~0 == policer_index)
304 {
305 error = clib_error_return (0, "expected policer index `%U'",
306 format_unformat_error, line_input);
307 goto done;
308 }
309 if (!is_add)
310 policer_index = ~0;
311
312 ip4_punt_policer_add_del(is_add, policer_index);
313
314done:
315 unformat_free (line_input);
316 return (error);
317}
318
319/*?
320 *
321 * @cliexpar
322 * @cliexcmd{set ip punt policer <INDEX>}
323 ?*/
324/* *INDENT-OFF* */
325VLIB_CLI_COMMAND (ip4_punt_policer_command, static) =
326{
327 .path = "ip punt policer",
328 .function = ip4_punt_police_cmd,
329 .short_help = "ip punt policer [add|del] <index>",
330};
331/* *INDENT-ON* */
332
333/*
334 * an uninitalised rx-redirect strcut used to pad the vector
335 */
336ip_punt_redirect_rx_t uninit_rx_redirect = {
337 .tx_sw_if_index = ~0,
Swarup Nayak4112e382017-12-11 17:36:54 +0530338 .adj_index = ADJ_INDEX_INVALID,
Neale Rannsd91c1db2017-07-31 02:30:50 -0700339};
340
341void
342ip_punt_redirect_add (ip_punt_redirect_t * cfg,
343 u32 rx_sw_if_index,
344 ip_punt_redirect_rx_t * redirect,
345 fib_protocol_t fproto, vnet_link_t linkt)
346{
347 ip_punt_redirect_rx_t *new;
348
349 if (~0 == rx_sw_if_index)
350 {
351 cfg->any_rx_sw_if_index = *redirect;
352 new = &cfg->any_rx_sw_if_index;
353 }
354 else
355 {
356 vec_validate_init_empty (cfg->redirect_by_rx_sw_if_index,
357 rx_sw_if_index, uninit_rx_redirect);
358 cfg->redirect_by_rx_sw_if_index[rx_sw_if_index] = *redirect;
359 new = &cfg->redirect_by_rx_sw_if_index[rx_sw_if_index];
360 }
361
362 new->adj_index = adj_nbr_add_or_lock (fproto, linkt,
363 &redirect->nh,
364 redirect->tx_sw_if_index);
365}
366
367void
368ip_punt_redirect_del (ip_punt_redirect_t * cfg, u32 rx_sw_if_index)
369{
370 ip_punt_redirect_rx_t *old;
371
372 if (~0 == rx_sw_if_index)
373 {
374 old = &cfg->any_rx_sw_if_index;
375 }
376 else
377 {
378 old = &cfg->redirect_by_rx_sw_if_index[rx_sw_if_index];
379 }
380
Swarup Nayak4112e382017-12-11 17:36:54 +0530381 if ((old == NULL) || (old->adj_index == ADJ_INDEX_INVALID))
382 return;
383
Neale Rannsd91c1db2017-07-31 02:30:50 -0700384 adj_unlock (old->adj_index);
385 *old = uninit_rx_redirect;
386}
387
388void
389ip4_punt_redirect_add (u32 rx_sw_if_index,
390 u32 tx_sw_if_index, ip46_address_t * nh)
391{
392 ip_punt_redirect_rx_t rx = {
393 .tx_sw_if_index = tx_sw_if_index,
394 .nh = *nh,
395 };
396
397 ip_punt_redirect_add (&ip4_punt_redirect_cfg,
398 rx_sw_if_index, &rx, FIB_PROTOCOL_IP4, VNET_LINK_IP4);
399
400 vnet_feature_enable_disable ("ip4-punt", "ip4-punt-redirect", 0, 1, 0, 0);
401}
402
403void
404ip4_punt_redirect_del (u32 rx_sw_if_index)
405{
406 vnet_feature_enable_disable ("ip4-punt", "ip4-punt-redirect", 0, 0, 0, 0);
407
408 ip_punt_redirect_del (&ip4_punt_redirect_cfg, rx_sw_if_index);
409}
410
411static clib_error_t *
412ip4_punt_redirect_cmd (vlib_main_t * vm,
413 unformat_input_t * main_input,
414 vlib_cli_command_t * cmd)
415{
416 unformat_input_t _line_input, *line_input = &_line_input;
417 clib_error_t *error = 0;
Swarup Nayakecf844c2017-12-11 13:52:44 +0530418 u32 rx_sw_if_index = 0;
419 u32 tx_sw_if_index = 0;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700420 ip46_address_t nh;
421 vnet_main_t *vnm;
422 u8 is_add;
423
424 is_add = 1;
425 vnm = vnet_get_main ();
426
427 if (!unformat_user (main_input, unformat_line_input, line_input))
428 return 0;
429
430 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
431 {
432 if (unformat (line_input, "del"))
433 is_add = 0;
434 else if (unformat (line_input, "add"))
435 is_add = 1;
436 else if (unformat (line_input, "rx all"))
437 rx_sw_if_index = ~0;
438 else if (unformat (line_input, "rx %U",
439 unformat_vnet_sw_interface, vnm, &rx_sw_if_index))
440 ;
441 else if (unformat (line_input, "via %U %U",
442 unformat_ip4_address,
443 &nh.ip4,
444 unformat_vnet_sw_interface, vnm, &tx_sw_if_index))
445 ;
446 else if (unformat (line_input, "via %U",
447 unformat_vnet_sw_interface, vnm, &tx_sw_if_index))
448 memset (&nh, 0, sizeof (nh));
449 else
450 {
451 error = unformat_parse_error (line_input);
452 goto done;
453 }
454 }
455
456 if (is_add)
Swarup Nayakecf844c2017-12-11 13:52:44 +0530457 {
458 if (rx_sw_if_index && tx_sw_if_index)
459 {
460 ip4_punt_redirect_add (rx_sw_if_index, tx_sw_if_index, &nh);
461 }
462 }
Neale Rannsd91c1db2017-07-31 02:30:50 -0700463 else
Swarup Nayakecf844c2017-12-11 13:52:44 +0530464 {
465 if (rx_sw_if_index)
466 {
467 ip4_punt_redirect_del (rx_sw_if_index);
468 }
469 }
Neale Rannsd91c1db2017-07-31 02:30:50 -0700470
471done:
472 unformat_free (line_input);
473 return (error);
474}
475
476/*?
477 *
478 * @cliexpar
479 * @cliexcmd{set ip punt policer}
480 ?*/
481/* *INDENT-OFF* */
482VLIB_CLI_COMMAND (ip4_punt_redirect_command, static) =
483{
484 .path = "ip punt redirect",
485 .function = ip4_punt_redirect_cmd,
486 .short_help = "ip punt redirect [add|del] rx [<interface>|all] via [<nh>] <tx_interface>",
487};
488/* *INDENT-ON* */
489
490u8 *
491format_ip_punt_redirect (u8 * s, va_list * args)
492{
493 ip_punt_redirect_t *cfg = va_arg (*args, ip_punt_redirect_t *);
494 ip_punt_redirect_rx_t *rx;
495 u32 rx_sw_if_index;
496 vnet_main_t *vnm = vnet_get_main ();
497
498 vec_foreach_index (rx_sw_if_index, cfg->redirect_by_rx_sw_if_index)
499 {
500 rx = &cfg->redirect_by_rx_sw_if_index[rx_sw_if_index];
501 if (~0 != rx->tx_sw_if_index)
502 {
503 s = format (s, " rx %U redirect via %U %U\n",
504 format_vnet_sw_interface_name, vnm,
505 vnet_get_sw_interface (vnm, rx_sw_if_index),
506 format_ip46_address, &rx->nh, IP46_TYPE_ANY,
507 format_vnet_sw_interface_name, vnm,
508 vnet_get_sw_interface (vnm, rx->tx_sw_if_index));
509 }
510 }
511 if (~0 != cfg->any_rx_sw_if_index.tx_sw_if_index)
512 {
513 s = format (s, " rx all redirect via %U %U\n",
514 format_ip46_address, &cfg->any_rx_sw_if_index.nh,
515 IP46_TYPE_ANY, format_vnet_sw_interface_name, vnm,
516 vnet_get_sw_interface (vnm,
517 cfg->
518 any_rx_sw_if_index.tx_sw_if_index));
519 }
520
521 return (s);
522}
523
524static clib_error_t *
525ip4_punt_redirect_show_cmd (vlib_main_t * vm,
526 unformat_input_t * main_input,
527 vlib_cli_command_t * cmd)
528{
529 vlib_cli_output (vm, "%U", format_ip_punt_redirect, &ip4_punt_redirect_cfg);
530
531 return (NULL);
532}
533
534/*?
535 *
536 * @cliexpar
537 * @cliexcmd{set ip punt redierect}
538 ?*/
539/* *INDENT-OFF* */
540VLIB_CLI_COMMAND (show_ip4_punt_redirect_command, static) =
541{
542 .path = "show ip punt redirect",
543 .function = ip4_punt_redirect_show_cmd,
Swarup Nayaka3611a72017-12-06 18:55:43 +0530544 .short_help = "show ip punt redirect",
Neale Rannsd91c1db2017-07-31 02:30:50 -0700545 .is_mp_safe = 1,
546};
547/* *INDENT-ON* */
548
549/*
550 * fd.io coding-style-patch-verification: ON
551 *
552 * Local Variables:
553 * eval: (c-set-style "gnu")
554 * End:
555 */