blob: d1145fbf82e2cbaad24cfa9fbd5ab71651c7e99b [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 (ip6_punt) =
23{
24 .arc_name = "ip6-punt",
25 .start_nodes = VNET_FEATURES ("ip6-punt"),
26};
27
28VNET_FEATURE_ARC_INIT (ip6_drop) =
29{
30 .arc_name = "ip6-drop",
Neale Ranns8269d3d2018-01-30 09:02:20 -080031 .start_nodes = VNET_FEATURES ("ip6-drop", "ip6-not-enabled"),
Neale Rannsd91c1db2017-07-31 02:30:50 -070032};
33/* *INDENT-ON* */
34
Filip Tehlar26ea14e2019-03-11 05:30:21 -070035extern ip_punt_policer_t ip6_punt_policer_cfg;
36
37#ifndef CLIB_MARCH_VARIANT
Neale Rannsd91c1db2017-07-31 02:30:50 -070038ip_punt_policer_t ip6_punt_policer_cfg;
Filip Tehlar26ea14e2019-03-11 05:30:21 -070039#endif /* CLIB_MARCH_VARIANT */
Neale Rannsd91c1db2017-07-31 02:30:50 -070040
41static char *ip6_punt_policer_error_strings[] = {
42#define _(sym,string) string,
43 foreach_ip_punt_policer_error
44#undef _
45};
46
Filip Tehlar26ea14e2019-03-11 05:30:21 -070047VLIB_NODE_FN (ip6_punt_policer_node) (vlib_main_t * vm,
48 vlib_node_runtime_t * node,
49 vlib_frame_t * frame)
Neale Rannsd91c1db2017-07-31 02:30:50 -070050{
51 return (ip_punt_policer (vm, node, frame,
52 vnet_feat_arc_ip6_punt.feature_arc_index,
53 ip6_punt_policer_cfg.policer_index));
54}
55
56
57/* *INDENT-OFF* */
58
Filip Tehlar26ea14e2019-03-11 05:30:21 -070059VLIB_REGISTER_NODE (ip6_punt_policer_node) = {
Neale Rannsd91c1db2017-07-31 02:30:50 -070060 .name = "ip6-punt-policer",
61 .vector_size = sizeof (u32),
62 .n_next_nodes = IP_PUNT_POLICER_N_NEXT,
63 .format_trace = format_ip_punt_policer_trace,
64 .n_errors = ARRAY_LEN(ip6_punt_policer_error_strings),
65 .error_strings = ip6_punt_policer_error_strings,
66
67 /* edit / add dispositions here */
68 .next_nodes = {
69 [IP_PUNT_POLICER_NEXT_DROP] = "ip6-drop",
70 },
71};
72
Neale Rannsd91c1db2017-07-31 02:30:50 -070073VNET_FEATURE_INIT (ip6_punt_policer_node, static) = {
74 .arc_name = "ip6-punt",
75 .node_name = "ip6-punt-policer",
76 .runs_before = VNET_FEATURES("ip6-punt-redirect")
77};
78/* *INDENT-ON* */
79
Filip Tehlar26ea14e2019-03-11 05:30:21 -070080VLIB_NODE_FN (ip6_drop_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
81 vlib_frame_t * frame)
Neale Rannsd91c1db2017-07-31 02:30:50 -070082{
83 if (node->flags & VLIB_NODE_FLAG_TRACE)
84 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
85
86 return ip_drop_or_punt (vm, node, frame,
87 vnet_feat_arc_ip6_drop.feature_arc_index);
88
89}
90
Filip Tehlar26ea14e2019-03-11 05:30:21 -070091VLIB_NODE_FN (ip6_not_enabled_node) (vlib_main_t * vm,
92 vlib_node_runtime_t * node,
93 vlib_frame_t * frame)
Neale Ranns8269d3d2018-01-30 09:02:20 -080094{
95 if (node->flags & VLIB_NODE_FLAG_TRACE)
96 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
97
98 return ip_drop_or_punt (vm, node, frame,
99 vnet_feat_arc_ip6_drop.feature_arc_index);
100
101}
102
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700103VLIB_NODE_FN (ip6_punt_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
104 vlib_frame_t * frame)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700105{
106 if (node->flags & VLIB_NODE_FLAG_TRACE)
107 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
108
109 return ip_drop_or_punt (vm, node, frame,
110 vnet_feat_arc_ip6_punt.feature_arc_index);
111}
112
113/* *INDENT-OFF* */
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700114VLIB_REGISTER_NODE (ip6_drop_node) =
Neale Rannsd91c1db2017-07-31 02:30:50 -0700115{
Neale Rannsd91c1db2017-07-31 02:30:50 -0700116 .name = "ip6-drop",
117 .vector_size = sizeof (u32),
118 .format_trace = format_ip6_forward_next_trace,
119 .n_next_nodes = 1,
120 .next_nodes = {
121 [0] = "error-drop",
122 },
123};
124
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700125VLIB_REGISTER_NODE (ip6_not_enabled_node) =
Neale Ranns8269d3d2018-01-30 09:02:20 -0800126{
Neale Ranns8269d3d2018-01-30 09:02:20 -0800127 .name = "ip6-not-enabled",
128 .vector_size = sizeof (u32),
129 .format_trace = format_ip6_forward_next_trace,
130 .n_next_nodes = 1,
131 .next_nodes = {
132 [0] = "error-drop",
133 },
134};
135
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700136VLIB_REGISTER_NODE (ip6_punt_node) =
Neale Rannsd91c1db2017-07-31 02:30:50 -0700137{
Neale Rannsd91c1db2017-07-31 02:30:50 -0700138 .name = "ip6-punt",
139 .vector_size = sizeof (u32),
140 .format_trace = format_ip6_forward_next_trace,
141 .n_next_nodes = 1,
142 .next_nodes = {
143 [0] = "error-punt",
144 },
145};
146
Neale Rannsd91c1db2017-07-31 02:30:50 -0700147VNET_FEATURE_INIT (ip6_punt_end_of_arc, static) = {
148 .arc_name = "ip6-punt",
149 .node_name = "error-punt",
150 .runs_before = 0, /* not before any other features */
151};
152
153VNET_FEATURE_INIT (ip6_drop_end_of_arc, static) = {
154 .arc_name = "ip6-drop",
155 .node_name = "error-drop",
156 .runs_before = 0, /* not before any other features */
157};
158/* *INDENT-ON */
159
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700160#ifndef CLIB_MARCH_VARIANT
Neale Rannsd91c1db2017-07-31 02:30:50 -0700161void
162ip6_punt_policer_add_del (u8 is_add, u32 policer_index)
163{
164 ip6_punt_policer_cfg.policer_index = policer_index;
165
166 vnet_feature_enable_disable ("ip6-punt", "ip6-punt-policer",
167 0, is_add, 0, 0);
168}
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700169#endif /* CLIB_MARCH_VARIANT */
Neale Rannsd91c1db2017-07-31 02:30:50 -0700170
171static clib_error_t *
172ip6_punt_police_cmd (vlib_main_t * vm,
173 unformat_input_t * main_input,
174 vlib_cli_command_t * cmd)
175{
176 unformat_input_t _line_input, *line_input = &_line_input;
177 clib_error_t *error = 0;
178 u32 policer_index;
179 u8 is_add = 1;
180
181 policer_index = ~0;
182
183 if (!unformat_user (main_input, unformat_line_input, line_input))
184 return 0;
185
186 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
187 {
188 if (unformat (line_input, "%d", &policer_index))
189 ;
190 else if (unformat (line_input, "del"))
191 is_add = 0;
192 else if (unformat (line_input, "add"))
193 is_add = 1;
194 else
195 {
196 error = unformat_parse_error (line_input);
197 goto done;
198 }
199 }
200
201 if (is_add && ~0 == policer_index)
202 {
203 error = clib_error_return (0, "expected policer index `%U'",
204 format_unformat_error, line_input);
205 goto done;
206 }
207 if (!is_add)
208 policer_index = ~0;
209
210 ip6_punt_policer_add_del(is_add, policer_index);
211
212done:
213 unformat_free (line_input);
214 return (error);
215}
216
217/*?
218 *
219 * @cliexpar
220 * @cliexcmd{set ip punt policer <INDEX>}
221 ?*/
222/* *INDENT-OFF* */
223VLIB_CLI_COMMAND (ip6_punt_policer_command, static) =
224{
225 .path = "ip6 punt policer",
226 .function = ip6_punt_police_cmd,
227 .short_help = "ip6 punt policer [add|del] <index>",
228};
Swarup Nayak4112e382017-12-11 17:36:54 +0530229
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700230extern ip_punt_redirect_t ip6_punt_redirect_cfg;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700231
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700232#ifndef CLIB_MARCH_VARIANT
Neale Rannsd91c1db2017-07-31 02:30:50 -0700233ip_punt_redirect_t ip6_punt_redirect_cfg = {
234 .any_rx_sw_if_index = {
Swarup Nayak4112e382017-12-11 17:36:54 +0530235 .tx_sw_if_index = ~0,
236 .adj_index = ADJ_INDEX_INVALID,
237 },
Neale Rannsd91c1db2017-07-31 02:30:50 -0700238};
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700239#endif /* CLIB_MARCH_VARIANT */
Swarup Nayak4112e382017-12-11 17:36:54 +0530240/* *INDENT-ON* */
Neale Rannsd91c1db2017-07-31 02:30:50 -0700241
242#define foreach_ip6_punt_redirect_error \
243_(DROP, "ip6 punt redirect drop")
244
245typedef enum
246{
247#define _(sym,str) IP6_PUNT_REDIRECT_ERROR_##sym,
248 foreach_ip6_punt_redirect_error
249#undef _
250 IP6_PUNT_REDIRECT_N_ERROR,
251} ip6_punt_redirect_error_t;
252
253static char *ip6_punt_redirect_error_strings[] = {
254#define _(sym,string) string,
255 foreach_ip6_punt_redirect_error
256#undef _
257};
258
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700259VLIB_NODE_FN (ip6_punt_redirect_node) (vlib_main_t * vm,
260 vlib_node_runtime_t * node,
261 vlib_frame_t * frame)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700262{
263 return (ip_punt_redirect (vm, node, frame,
264 vnet_feat_arc_ip6_punt.feature_arc_index,
265 &ip6_punt_redirect_cfg));
266}
267
268/* *INDENT-OFF* */
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700269VLIB_REGISTER_NODE (ip6_punt_redirect_node) = {
Neale Rannsd91c1db2017-07-31 02:30:50 -0700270 .name = "ip6-punt-redirect",
271 .vector_size = sizeof (u32),
272 .n_next_nodes = IP_PUNT_REDIRECT_N_NEXT,
273 .format_trace = format_ip_punt_redirect_trace,
274 .n_errors = ARRAY_LEN(ip6_punt_redirect_error_strings),
275 .error_strings = ip6_punt_redirect_error_strings,
276
277 /* edit / add dispositions here */
278 .next_nodes = {
279 [IP_PUNT_REDIRECT_NEXT_DROP] = "ip6-drop",
280 [IP_PUNT_REDIRECT_NEXT_TX] = "ip6-rewrite",
281 [IP_PUNT_REDIRECT_NEXT_ARP] = "ip6-discover-neighbor",
282 },
283};
284
Neale Rannsd91c1db2017-07-31 02:30:50 -0700285VNET_FEATURE_INIT (ip6_punt_redirect_node, static) = {
286 .arc_name = "ip6-punt",
287 .node_name = "ip6-punt-redirect",
288 .runs_before = VNET_FEATURES("error-punt")
289};
290/* *INDENT-ON* */
291
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700292#ifndef CLIB_MARCH_VARIANT
Neale Rannsd91c1db2017-07-31 02:30:50 -0700293void
294ip6_punt_redirect_add (u32 rx_sw_if_index,
295 u32 tx_sw_if_index, ip46_address_t * nh)
296{
297 ip_punt_redirect_rx_t rx = {
298 .tx_sw_if_index = tx_sw_if_index,
299 .nh = *nh,
300 };
301
302 ip_punt_redirect_add (&ip6_punt_redirect_cfg,
303 rx_sw_if_index, &rx, FIB_PROTOCOL_IP6, VNET_LINK_IP6);
304
305 vnet_feature_enable_disable ("ip6-punt", "ip6-punt-redirect", 0, 1, 0, 0);
306}
307
308void
309ip6_punt_redirect_del (u32 rx_sw_if_index)
310{
311 vnet_feature_enable_disable ("ip6-punt", "ip6-punt-redirect", 0, 0, 0, 0);
312
313 ip_punt_redirect_del (&ip6_punt_redirect_cfg, rx_sw_if_index);
314}
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700315#endif /* CLIB_MARCH_VARIANT */
Neale Rannsd91c1db2017-07-31 02:30:50 -0700316
317static clib_error_t *
318ip6_punt_redirect_cmd (vlib_main_t * vm,
319 unformat_input_t * main_input,
320 vlib_cli_command_t * cmd)
321{
322 unformat_input_t _line_input, *line_input = &_line_input;
323 clib_error_t *error = 0;
Swarup Nayakecf844c2017-12-11 13:52:44 +0530324 u32 rx_sw_if_index = 0;
325 u32 tx_sw_if_index = 0;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700326 ip46_address_t nh;
327 vnet_main_t *vnm;
328 u8 is_add;
329
330 is_add = 1;
331 vnm = vnet_get_main ();
332
333 if (!unformat_user (main_input, unformat_line_input, line_input))
334 return 0;
335
336 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
337 {
338 if (unformat (line_input, "del"))
339 is_add = 0;
340 else if (unformat (line_input, "add"))
341 is_add = 1;
342 else if (unformat (line_input, "rx all"))
343 rx_sw_if_index = ~0;
344 else if (unformat (line_input, "rx %U",
345 unformat_vnet_sw_interface, vnm, &rx_sw_if_index))
346 ;
347 else if (unformat (line_input, "via %U %U",
348 unformat_ip6_address,
349 &nh.ip6,
350 unformat_vnet_sw_interface, vnm, &tx_sw_if_index))
351 ;
352 else if (unformat (line_input, "via %U",
353 unformat_vnet_sw_interface, vnm, &tx_sw_if_index))
Dave Barachb7b92992018-10-17 10:38:51 -0400354 clib_memset (&nh, 0, sizeof (nh));
Neale Rannsd91c1db2017-07-31 02:30:50 -0700355 else
356 {
357 error = unformat_parse_error (line_input);
358 goto done;
359 }
360 }
361
362 if (is_add)
Swarup Nayakecf844c2017-12-11 13:52:44 +0530363 {
364 if (rx_sw_if_index && tx_sw_if_index)
365 {
366 ip6_punt_redirect_add (rx_sw_if_index, tx_sw_if_index, &nh);
367 }
368 }
Neale Rannsd91c1db2017-07-31 02:30:50 -0700369 else
Swarup Nayakecf844c2017-12-11 13:52:44 +0530370 {
371 if (rx_sw_if_index)
372 {
373 ip6_punt_redirect_del (rx_sw_if_index);
374 }
375 }
Neale Rannsd91c1db2017-07-31 02:30:50 -0700376
377done:
378 unformat_free (line_input);
379 return (error);
380}
381
382/*?
383 *
384 * @cliexpar
385 * @cliexcmd{set ip punt policer <INDEX>}
386 ?*/
387/* *INDENT-OFF* */
388VLIB_CLI_COMMAND (ip6_punt_redirect_command, static) =
389{
390 .path = "ip6 punt redirect",
391 .function = ip6_punt_redirect_cmd,
392 .short_help = "ip6 punt redirect [add|del] rx [<interface>|all] via [<nh>] <tx_interface>",
393};
394/* *INDENT-ON* */
395
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700396#ifndef CLIB_MARCH_VARIANT
Pavel Kotucek609e1212018-11-27 09:59:44 +0100397ip_punt_redirect_detail_t *
398ip6_punt_redirect_entries (u32 sw_if_index)
399{
400 ip_punt_redirect_rx_t *pr;
401 ip_punt_redirect_detail_t *prs = 0;
402 u32 rx_sw_if_index;
403
404 vec_foreach_index (rx_sw_if_index,
405 ip6_punt_redirect_cfg.redirect_by_rx_sw_if_index)
406 {
407 if (sw_if_index == ~0 || sw_if_index == rx_sw_if_index)
408 {
409 pr =
410 &ip6_punt_redirect_cfg.redirect_by_rx_sw_if_index[rx_sw_if_index];
411 if (NULL != pr && ~0 != pr->tx_sw_if_index)
412 {
413 ip_punt_redirect_detail_t detail = {.rx_sw_if_index =
414 rx_sw_if_index,
415 .punt_redirect = *pr
416 };
417 vec_add1 (prs, detail);
418 }
419 }
420 }
421 if (~0 != ip6_punt_redirect_cfg.any_rx_sw_if_index.tx_sw_if_index)
422 {
423 pr = &ip6_punt_redirect_cfg.any_rx_sw_if_index;
424 if (NULL != pr)
425 {
426 ip_punt_redirect_detail_t detail = {.rx_sw_if_index =
427 rx_sw_if_index,
428 .punt_redirect = *pr
429 };
430 vec_add1 (prs, detail);
431 }
432 }
433
434 return prs;
435}
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700436#endif /* CLIB_MARCH_VARIANT */
Pavel Kotucek609e1212018-11-27 09:59:44 +0100437
Neale Rannsd91c1db2017-07-31 02:30:50 -0700438static clib_error_t *
439ip6_punt_redirect_show_cmd (vlib_main_t * vm,
440 unformat_input_t * main_input,
441 vlib_cli_command_t * cmd)
442{
443 vlib_cli_output (vm, "%U", format_ip_punt_redirect, &ip6_punt_redirect_cfg);
444
445 return (NULL);
446}
447
448/*?
449 *
450 * @cliexpar
451 * @cliexcmd{set ip punt policer <INDEX>}
452 ?*/
453/* *INDENT-OFF* */
454VLIB_CLI_COMMAND (show_ip6_punt_redirect_command, static) =
455{
456 .path = "show ip6 punt redirect",
457 .function = ip6_punt_redirect_show_cmd,
Swarup Nayaka3611a72017-12-06 18:55:43 +0530458 .short_help = "show ip6 punt redirect",
Neale Rannsd91c1db2017-07-31 02:30:50 -0700459 .is_mp_safe = 1,
460};
461/* *INDENT-ON* */
462
463/*
464 * fd.io coding-style-patch-verification: ON
465 *
466 * Local Variables:
467 * eval: (c-set-style "gnu")
468 * End:
469 */