punt and drop features:
[vpp.git] / src / vnet / ip / ip4_punt_drop.c
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>
19 #include <vnet/policer/police_inlines.h>
20
21 /* *INDENT-OFF* */
22 VNET_FEATURE_ARC_INIT (ip4_punt) =
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"),
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
50 static char *ip4_punt_policer_error_strings[] = {
51 #define _(sym,string) string,
52   foreach_ip_punt_policer_error
53 #undef _
54 };
55
56 static uword
57 ip4_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* */
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,
74
75   .next_nodes = {
76     [IP_PUNT_POLICER_NEXT_DROP] = "ip4-drop",
77   },
78 };
79
80 VLIB_NODE_FUNCTION_MULTIARCH (ip4_punt_policer_node,
81                               ip4_punt_policer);
82
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"),
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 *);
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* */
115 ip_punt_redirect_t ip4_punt_redirect_cfg = {
116   .any_rx_sw_if_index = {
117     .tx_sw_if_index = ~0,
118   },
119 };
120 /* *INDENT-ON* */
121
122
123 #define foreach_ip4_punt_redirect_error         \
124 _(DROP, "ip4 punt redirect drop")
125
126 typedef enum
127 {
128 #define _(sym,str) IP4_PUNT_REDIRECT_ERROR_##sym,
129   foreach_ip4_punt_redirect_error
130 #undef _
131     IP4_PUNT_REDIRECT_N_ERROR,
132 } ip4_punt_redirect_error_t;
133
134 static char *ip4_punt_redirect_error_strings[] = {
135 #define _(sym,string) string,
136   foreach_ip4_punt_redirect_error
137 #undef _
138 };
139
140 static uword
141 ip4_punt_redirect (vlib_main_t * vm,
142                    vlib_node_runtime_t * node, vlib_frame_t * frame)
143 {
144   return (ip_punt_redirect (vm, node, frame,
145                             vnet_feat_arc_ip4_punt.feature_arc_index,
146                             &ip4_punt_redirect_cfg));
147 }
148
149 /* *INDENT-OFF* */
150 VLIB_REGISTER_NODE (ip4_punt_redirect_node, static) = {
151   .function = ip4_punt_redirect,
152   .name = "ip4-punt-redirect",
153   .vector_size = sizeof (u32),
154   .n_next_nodes = IP_PUNT_REDIRECT_N_NEXT,
155   .format_trace = format_ip_punt_redirect_trace,
156   .n_errors = ARRAY_LEN(ip4_punt_redirect_error_strings),
157   .error_strings = ip4_punt_redirect_error_strings,
158
159   /* edit / add dispositions here */
160   .next_nodes = {
161     [IP_PUNT_REDIRECT_NEXT_DROP] = "ip4-drop",
162     [IP_PUNT_REDIRECT_NEXT_TX] = "ip4-rewrite",
163     [IP_PUNT_REDIRECT_NEXT_ARP] = "ip4-arp",
164   },
165 };
166
167 VLIB_NODE_FUNCTION_MULTIARCH (ip4_punt_redirect_node,
168                               ip4_punt_redirect);
169
170 VNET_FEATURE_INIT (ip4_punt_redirect_node, static) = {
171   .arc_name = "ip4-punt",
172   .node_name = "ip4-punt-redirect",
173   .runs_before = VNET_FEATURES("error-punt"),
174 };
175 /* *INDENT-ON* */
176
177 static uword
178 ip4_drop (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
179 {
180   if (node->flags & VLIB_NODE_FLAG_TRACE)
181     ip4_forward_next_trace (vm, node, frame, VLIB_TX);
182
183   return ip_drop_or_punt (vm, node, frame,
184                           vnet_feat_arc_ip4_drop.feature_arc_index);
185
186 }
187
188 static uword
189 ip4_punt (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
190 {
191   if (node->flags & VLIB_NODE_FLAG_TRACE)
192     ip4_forward_next_trace (vm, node, frame, VLIB_TX);
193
194   return ip_drop_or_punt (vm, node, frame,
195                           vnet_feat_arc_ip4_punt.feature_arc_index);
196 }
197
198 /* *INDENT-OFF* */
199 VLIB_REGISTER_NODE (ip4_drop_node, static) =
200 {
201   .function = ip4_drop,
202   .name = "ip4-drop",
203   .vector_size = sizeof (u32),
204   .format_trace = format_ip4_forward_next_trace,
205   .n_next_nodes = 1,
206   .next_nodes = {
207     [0] = "error-drop",
208   },
209 };
210
211 VLIB_NODE_FUNCTION_MULTIARCH (ip4_drop_node, ip4_drop);
212
213 VLIB_REGISTER_NODE (ip4_punt_node, static) =
214 {
215   .function = ip4_punt,
216   .name = "ip4-punt",
217   .vector_size = sizeof (u32),
218   .format_trace = format_ip4_forward_next_trace,
219   .n_next_nodes = 1,
220   .next_nodes = {
221     [0] = "error-punt",
222   },
223 };
224
225 VNET_FEATURE_INIT (ip4_punt_end_of_arc, static) = {
226   .arc_name = "ip4-punt",
227   .node_name = "error-punt",
228   .runs_before = 0, /* not before any other features */
229 };
230
231 VNET_FEATURE_INIT (ip4_drop_end_of_arc, static) = {
232   .arc_name = "ip4-drop",
233   .node_name = "error-drop",
234   .runs_before = 0, /* not before any other features */
235 };
236 /* *INDENT-ON */
237
238 void
239 ip4_punt_policer_add_del (u8 is_add, u32 policer_index)
240 {
241   ip4_punt_policer_cfg.policer_index = policer_index;
242
243   vnet_feature_enable_disable ("ip4-punt", "ip4-punt-policer",
244                                0, is_add, 0, 0);
245 }
246
247 static clib_error_t *
248 ip4_punt_police_cmd (vlib_main_t * vm,
249                      unformat_input_t * main_input,
250                      vlib_cli_command_t * cmd)
251 {
252   unformat_input_t _line_input, *line_input = &_line_input;
253   clib_error_t *error = 0;
254   u32 policer_index;
255   u8 is_add = 1;
256
257   policer_index = ~0;
258
259   if (!unformat_user (main_input, unformat_line_input, line_input))
260     return 0;
261
262   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
263     {
264       if (unformat (line_input, "%d", &policer_index))
265         ;
266       else if (unformat (line_input, "del"))
267         is_add = 0;
268       else if (unformat (line_input, "add"))
269         is_add = 1;
270       else
271         {
272           error = unformat_parse_error (line_input);
273           goto done;
274         }
275     }
276
277   if (is_add && ~0 == policer_index)
278   {
279       error = clib_error_return (0, "expected policer index `%U'",
280                                  format_unformat_error, line_input);
281       goto done;
282   }
283   if (!is_add)
284       policer_index = ~0;
285
286   ip4_punt_policer_add_del(is_add, policer_index);
287
288 done:
289   unformat_free (line_input);
290   return (error);
291 }
292
293 /*?
294  *
295  * @cliexpar
296  * @cliexcmd{set ip punt policer <INDEX>}
297  ?*/
298 /* *INDENT-OFF* */
299 VLIB_CLI_COMMAND (ip4_punt_policer_command, static) =
300 {
301   .path = "ip punt policer",
302   .function = ip4_punt_police_cmd,
303   .short_help = "ip punt policer [add|del] <index>",
304 };
305 /* *INDENT-ON* */
306
307 /*
308  * an uninitalised rx-redirect strcut used to pad the vector
309  */
310 ip_punt_redirect_rx_t uninit_rx_redirect = {
311   .tx_sw_if_index = ~0,
312 };
313
314 void
315 ip_punt_redirect_add (ip_punt_redirect_t * cfg,
316                       u32 rx_sw_if_index,
317                       ip_punt_redirect_rx_t * redirect,
318                       fib_protocol_t fproto, vnet_link_t linkt)
319 {
320   ip_punt_redirect_rx_t *new;
321
322   if (~0 == rx_sw_if_index)
323     {
324       cfg->any_rx_sw_if_index = *redirect;
325       new = &cfg->any_rx_sw_if_index;
326     }
327   else
328     {
329       vec_validate_init_empty (cfg->redirect_by_rx_sw_if_index,
330                                rx_sw_if_index, uninit_rx_redirect);
331       cfg->redirect_by_rx_sw_if_index[rx_sw_if_index] = *redirect;
332       new = &cfg->redirect_by_rx_sw_if_index[rx_sw_if_index];
333     }
334
335   new->adj_index = adj_nbr_add_or_lock (fproto, linkt,
336                                         &redirect->nh,
337                                         redirect->tx_sw_if_index);
338 }
339
340 void
341 ip_punt_redirect_del (ip_punt_redirect_t * cfg, u32 rx_sw_if_index)
342 {
343   ip_punt_redirect_rx_t *old;
344
345   if (~0 == rx_sw_if_index)
346     {
347       old = &cfg->any_rx_sw_if_index;
348     }
349   else
350     {
351       old = &cfg->redirect_by_rx_sw_if_index[rx_sw_if_index];
352     }
353
354   adj_unlock (old->adj_index);
355   *old = uninit_rx_redirect;
356 }
357
358 void
359 ip4_punt_redirect_add (u32 rx_sw_if_index,
360                        u32 tx_sw_if_index, ip46_address_t * nh)
361 {
362   ip_punt_redirect_rx_t rx = {
363     .tx_sw_if_index = tx_sw_if_index,
364     .nh = *nh,
365   };
366
367   ip_punt_redirect_add (&ip4_punt_redirect_cfg,
368                         rx_sw_if_index, &rx, FIB_PROTOCOL_IP4, VNET_LINK_IP4);
369
370   vnet_feature_enable_disable ("ip4-punt", "ip4-punt-redirect", 0, 1, 0, 0);
371 }
372
373 void
374 ip4_punt_redirect_del (u32 rx_sw_if_index)
375 {
376   vnet_feature_enable_disable ("ip4-punt", "ip4-punt-redirect", 0, 0, 0, 0);
377
378   ip_punt_redirect_del (&ip4_punt_redirect_cfg, rx_sw_if_index);
379 }
380
381 static clib_error_t *
382 ip4_punt_redirect_cmd (vlib_main_t * vm,
383                        unformat_input_t * main_input,
384                        vlib_cli_command_t * cmd)
385 {
386   unformat_input_t _line_input, *line_input = &_line_input;
387   clib_error_t *error = 0;
388   u32 rx_sw_if_index;
389   u32 tx_sw_if_index;
390   ip46_address_t nh;
391   vnet_main_t *vnm;
392   u8 is_add;
393
394   is_add = 1;
395   vnm = vnet_get_main ();
396
397   if (!unformat_user (main_input, unformat_line_input, line_input))
398     return 0;
399
400   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
401     {
402       if (unformat (line_input, "del"))
403         is_add = 0;
404       else if (unformat (line_input, "add"))
405         is_add = 1;
406       else if (unformat (line_input, "rx all"))
407         rx_sw_if_index = ~0;
408       else if (unformat (line_input, "rx %U",
409                          unformat_vnet_sw_interface, vnm, &rx_sw_if_index))
410         ;
411       else if (unformat (line_input, "via %U %U",
412                          unformat_ip4_address,
413                          &nh.ip4,
414                          unformat_vnet_sw_interface, vnm, &tx_sw_if_index))
415         ;
416       else if (unformat (line_input, "via %U",
417                          unformat_vnet_sw_interface, vnm, &tx_sw_if_index))
418         memset (&nh, 0, sizeof (nh));
419       else
420         {
421           error = unformat_parse_error (line_input);
422           goto done;
423         }
424     }
425
426   if (is_add)
427     ip4_punt_redirect_add (rx_sw_if_index, tx_sw_if_index, &nh);
428   else
429     ip4_punt_redirect_del (rx_sw_if_index);
430
431 done:
432   unformat_free (line_input);
433   return (error);
434 }
435
436 /*?
437  *
438  * @cliexpar
439  * @cliexcmd{set ip punt policer}
440  ?*/
441 /* *INDENT-OFF* */
442 VLIB_CLI_COMMAND (ip4_punt_redirect_command, static) =
443 {
444   .path = "ip punt redirect",
445   .function = ip4_punt_redirect_cmd,
446   .short_help = "ip punt redirect [add|del] rx [<interface>|all] via [<nh>] <tx_interface>",
447 };
448 /* *INDENT-ON* */
449
450 u8 *
451 format_ip_punt_redirect (u8 * s, va_list * args)
452 {
453   ip_punt_redirect_t *cfg = va_arg (*args, ip_punt_redirect_t *);
454   ip_punt_redirect_rx_t *rx;
455   u32 rx_sw_if_index;
456   vnet_main_t *vnm = vnet_get_main ();
457
458   vec_foreach_index (rx_sw_if_index, cfg->redirect_by_rx_sw_if_index)
459   {
460     rx = &cfg->redirect_by_rx_sw_if_index[rx_sw_if_index];
461     if (~0 != rx->tx_sw_if_index)
462       {
463         s = format (s, " rx %U redirect via %U %U\n",
464                     format_vnet_sw_interface_name, vnm,
465                     vnet_get_sw_interface (vnm, rx_sw_if_index),
466                     format_ip46_address, &rx->nh, IP46_TYPE_ANY,
467                     format_vnet_sw_interface_name, vnm,
468                     vnet_get_sw_interface (vnm, rx->tx_sw_if_index));
469       }
470   }
471   if (~0 != cfg->any_rx_sw_if_index.tx_sw_if_index)
472     {
473       s = format (s, " rx all redirect via %U %U\n",
474                   format_ip46_address, &cfg->any_rx_sw_if_index.nh,
475                   IP46_TYPE_ANY, format_vnet_sw_interface_name, vnm,
476                   vnet_get_sw_interface (vnm,
477                                          cfg->
478                                          any_rx_sw_if_index.tx_sw_if_index));
479     }
480
481   return (s);
482 }
483
484 static clib_error_t *
485 ip4_punt_redirect_show_cmd (vlib_main_t * vm,
486                             unformat_input_t * main_input,
487                             vlib_cli_command_t * cmd)
488 {
489   vlib_cli_output (vm, "%U", format_ip_punt_redirect, &ip4_punt_redirect_cfg);
490
491   return (NULL);
492 }
493
494 /*?
495  *
496  * @cliexpar
497  * @cliexcmd{set ip punt redierect}
498  ?*/
499 /* *INDENT-OFF* */
500 VLIB_CLI_COMMAND (show_ip4_punt_redirect_command, static) =
501 {
502   .path = "show ip punt redirect",
503   .function = ip4_punt_redirect_show_cmd,
504   .short_help = "show ip punt redirect [add|del] rx [<interface>|all] via [<nh>] <tx_interface>",
505   .is_mp_safe = 1,
506 };
507 /* *INDENT-ON* */
508
509 /*
510  * fd.io coding-style-patch-verification: ON
511  *
512  * Local Variables:
513  * eval: (c-set-style "gnu")
514  * End:
515  */