blob: 88527756966ccf79240621c97dd43018a26082e1 [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",
31 .start_nodes = VNET_FEATURES ("ip6-drop"),
32};
33/* *INDENT-ON* */
34
35ip_punt_policer_t ip6_punt_policer_cfg;
36
37static char *ip6_punt_policer_error_strings[] = {
38#define _(sym,string) string,
39 foreach_ip_punt_policer_error
40#undef _
41};
42
43static uword
44ip6_punt_policer (vlib_main_t * vm,
45 vlib_node_runtime_t * node, vlib_frame_t * frame)
46{
47 return (ip_punt_policer (vm, node, frame,
48 vnet_feat_arc_ip6_punt.feature_arc_index,
49 ip6_punt_policer_cfg.policer_index));
50}
51
52
53/* *INDENT-OFF* */
54
55VLIB_REGISTER_NODE (ip6_punt_policer_node, static) = {
56 .function = ip6_punt_policer,
57 .name = "ip6-punt-policer",
58 .vector_size = sizeof (u32),
59 .n_next_nodes = IP_PUNT_POLICER_N_NEXT,
60 .format_trace = format_ip_punt_policer_trace,
61 .n_errors = ARRAY_LEN(ip6_punt_policer_error_strings),
62 .error_strings = ip6_punt_policer_error_strings,
63
64 /* edit / add dispositions here */
65 .next_nodes = {
66 [IP_PUNT_POLICER_NEXT_DROP] = "ip6-drop",
67 },
68};
69
70VLIB_NODE_FUNCTION_MULTIARCH (ip6_punt_policer_node,
71 ip6_punt_policer);
72
73VNET_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
80static uword
81ip6_drop (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
82{
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
91static uword
92ip6_punt (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
93{
94 if (node->flags & VLIB_NODE_FLAG_TRACE)
95 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
96
97 return ip_drop_or_punt (vm, node, frame,
98 vnet_feat_arc_ip6_punt.feature_arc_index);
99}
100
101/* *INDENT-OFF* */
102VLIB_REGISTER_NODE (ip6_drop_node, static) =
103{
104 .function = ip6_drop,
105 .name = "ip6-drop",
106 .vector_size = sizeof (u32),
107 .format_trace = format_ip6_forward_next_trace,
108 .n_next_nodes = 1,
109 .next_nodes = {
110 [0] = "error-drop",
111 },
112};
113
114VLIB_NODE_FUNCTION_MULTIARCH (ip6_drop_node, ip6_drop);
115
116VLIB_REGISTER_NODE (ip6_punt_node, static) =
117{
118 .function = ip6_punt,
119 .name = "ip6-punt",
120 .vector_size = sizeof (u32),
121 .format_trace = format_ip6_forward_next_trace,
122 .n_next_nodes = 1,
123 .next_nodes = {
124 [0] = "error-punt",
125 },
126};
127
128VLIB_NODE_FUNCTION_MULTIARCH (ip6_punt_node, ip6_punt);
129
130VNET_FEATURE_INIT (ip6_punt_end_of_arc, static) = {
131 .arc_name = "ip6-punt",
132 .node_name = "error-punt",
133 .runs_before = 0, /* not before any other features */
134};
135
136VNET_FEATURE_INIT (ip6_drop_end_of_arc, static) = {
137 .arc_name = "ip6-drop",
138 .node_name = "error-drop",
139 .runs_before = 0, /* not before any other features */
140};
141/* *INDENT-ON */
142
143void
144ip6_punt_policer_add_del (u8 is_add, u32 policer_index)
145{
146 ip6_punt_policer_cfg.policer_index = policer_index;
147
148 vnet_feature_enable_disable ("ip6-punt", "ip6-punt-policer",
149 0, is_add, 0, 0);
150}
151
152static clib_error_t *
153ip6_punt_police_cmd (vlib_main_t * vm,
154 unformat_input_t * main_input,
155 vlib_cli_command_t * cmd)
156{
157 unformat_input_t _line_input, *line_input = &_line_input;
158 clib_error_t *error = 0;
159 u32 policer_index;
160 u8 is_add = 1;
161
162 policer_index = ~0;
163
164 if (!unformat_user (main_input, unformat_line_input, line_input))
165 return 0;
166
167 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
168 {
169 if (unformat (line_input, "%d", &policer_index))
170 ;
171 else if (unformat (line_input, "del"))
172 is_add = 0;
173 else if (unformat (line_input, "add"))
174 is_add = 1;
175 else
176 {
177 error = unformat_parse_error (line_input);
178 goto done;
179 }
180 }
181
182 if (is_add && ~0 == policer_index)
183 {
184 error = clib_error_return (0, "expected policer index `%U'",
185 format_unformat_error, line_input);
186 goto done;
187 }
188 if (!is_add)
189 policer_index = ~0;
190
191 ip6_punt_policer_add_del(is_add, policer_index);
192
193done:
194 unformat_free (line_input);
195 return (error);
196}
197
198/*?
199 *
200 * @cliexpar
201 * @cliexcmd{set ip punt policer <INDEX>}
202 ?*/
203/* *INDENT-OFF* */
204VLIB_CLI_COMMAND (ip6_punt_policer_command, static) =
205{
206 .path = "ip6 punt policer",
207 .function = ip6_punt_police_cmd,
208 .short_help = "ip6 punt policer [add|del] <index>",
209};
Swarup Nayak4112e382017-12-11 17:36:54 +0530210
Neale Rannsd91c1db2017-07-31 02:30:50 -0700211
212ip_punt_redirect_t ip6_punt_redirect_cfg = {
213 .any_rx_sw_if_index = {
Swarup Nayak4112e382017-12-11 17:36:54 +0530214 .tx_sw_if_index = ~0,
215 .adj_index = ADJ_INDEX_INVALID,
216 },
Neale Rannsd91c1db2017-07-31 02:30:50 -0700217};
Swarup Nayak4112e382017-12-11 17:36:54 +0530218/* *INDENT-ON* */
Neale Rannsd91c1db2017-07-31 02:30:50 -0700219
220#define foreach_ip6_punt_redirect_error \
221_(DROP, "ip6 punt redirect drop")
222
223typedef enum
224{
225#define _(sym,str) IP6_PUNT_REDIRECT_ERROR_##sym,
226 foreach_ip6_punt_redirect_error
227#undef _
228 IP6_PUNT_REDIRECT_N_ERROR,
229} ip6_punt_redirect_error_t;
230
231static char *ip6_punt_redirect_error_strings[] = {
232#define _(sym,string) string,
233 foreach_ip6_punt_redirect_error
234#undef _
235};
236
237static uword
238ip6_punt_redirect (vlib_main_t * vm,
239 vlib_node_runtime_t * node, vlib_frame_t * frame)
240{
241 return (ip_punt_redirect (vm, node, frame,
242 vnet_feat_arc_ip6_punt.feature_arc_index,
243 &ip6_punt_redirect_cfg));
244}
245
246/* *INDENT-OFF* */
247VLIB_REGISTER_NODE (ip6_punt_redirect_node, static) = {
248 .function = ip6_punt_redirect,
249 .name = "ip6-punt-redirect",
250 .vector_size = sizeof (u32),
251 .n_next_nodes = IP_PUNT_REDIRECT_N_NEXT,
252 .format_trace = format_ip_punt_redirect_trace,
253 .n_errors = ARRAY_LEN(ip6_punt_redirect_error_strings),
254 .error_strings = ip6_punt_redirect_error_strings,
255
256 /* edit / add dispositions here */
257 .next_nodes = {
258 [IP_PUNT_REDIRECT_NEXT_DROP] = "ip6-drop",
259 [IP_PUNT_REDIRECT_NEXT_TX] = "ip6-rewrite",
260 [IP_PUNT_REDIRECT_NEXT_ARP] = "ip6-discover-neighbor",
261 },
262};
263
264VLIB_NODE_FUNCTION_MULTIARCH (ip6_punt_redirect_node,
265 ip6_punt_redirect);
266
267VNET_FEATURE_INIT (ip6_punt_redirect_node, static) = {
268 .arc_name = "ip6-punt",
269 .node_name = "ip6-punt-redirect",
270 .runs_before = VNET_FEATURES("error-punt")
271};
272/* *INDENT-ON* */
273
274void
275ip6_punt_redirect_add (u32 rx_sw_if_index,
276 u32 tx_sw_if_index, ip46_address_t * nh)
277{
278 ip_punt_redirect_rx_t rx = {
279 .tx_sw_if_index = tx_sw_if_index,
280 .nh = *nh,
281 };
282
283 ip_punt_redirect_add (&ip6_punt_redirect_cfg,
284 rx_sw_if_index, &rx, FIB_PROTOCOL_IP6, VNET_LINK_IP6);
285
286 vnet_feature_enable_disable ("ip6-punt", "ip6-punt-redirect", 0, 1, 0, 0);
287}
288
289void
290ip6_punt_redirect_del (u32 rx_sw_if_index)
291{
292 vnet_feature_enable_disable ("ip6-punt", "ip6-punt-redirect", 0, 0, 0, 0);
293
294 ip_punt_redirect_del (&ip6_punt_redirect_cfg, rx_sw_if_index);
295}
296
297static clib_error_t *
298ip6_punt_redirect_cmd (vlib_main_t * vm,
299 unformat_input_t * main_input,
300 vlib_cli_command_t * cmd)
301{
302 unformat_input_t _line_input, *line_input = &_line_input;
303 clib_error_t *error = 0;
Swarup Nayakecf844c2017-12-11 13:52:44 +0530304 u32 rx_sw_if_index = 0;
305 u32 tx_sw_if_index = 0;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700306 ip46_address_t nh;
307 vnet_main_t *vnm;
308 u8 is_add;
309
310 is_add = 1;
311 vnm = vnet_get_main ();
312
313 if (!unformat_user (main_input, unformat_line_input, line_input))
314 return 0;
315
316 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
317 {
318 if (unformat (line_input, "del"))
319 is_add = 0;
320 else if (unformat (line_input, "add"))
321 is_add = 1;
322 else if (unformat (line_input, "rx all"))
323 rx_sw_if_index = ~0;
324 else if (unformat (line_input, "rx %U",
325 unformat_vnet_sw_interface, vnm, &rx_sw_if_index))
326 ;
327 else if (unformat (line_input, "via %U %U",
328 unformat_ip6_address,
329 &nh.ip6,
330 unformat_vnet_sw_interface, vnm, &tx_sw_if_index))
331 ;
332 else if (unformat (line_input, "via %U",
333 unformat_vnet_sw_interface, vnm, &tx_sw_if_index))
334 memset (&nh, 0, sizeof (nh));
335 else
336 {
337 error = unformat_parse_error (line_input);
338 goto done;
339 }
340 }
341
342 if (is_add)
Swarup Nayakecf844c2017-12-11 13:52:44 +0530343 {
344 if (rx_sw_if_index && tx_sw_if_index)
345 {
346 ip6_punt_redirect_add (rx_sw_if_index, tx_sw_if_index, &nh);
347 }
348 }
Neale Rannsd91c1db2017-07-31 02:30:50 -0700349 else
Swarup Nayakecf844c2017-12-11 13:52:44 +0530350 {
351 if (rx_sw_if_index)
352 {
353 ip6_punt_redirect_del (rx_sw_if_index);
354 }
355 }
Neale Rannsd91c1db2017-07-31 02:30:50 -0700356
357done:
358 unformat_free (line_input);
359 return (error);
360}
361
362/*?
363 *
364 * @cliexpar
365 * @cliexcmd{set ip punt policer <INDEX>}
366 ?*/
367/* *INDENT-OFF* */
368VLIB_CLI_COMMAND (ip6_punt_redirect_command, static) =
369{
370 .path = "ip6 punt redirect",
371 .function = ip6_punt_redirect_cmd,
372 .short_help = "ip6 punt redirect [add|del] rx [<interface>|all] via [<nh>] <tx_interface>",
373};
374/* *INDENT-ON* */
375
376static clib_error_t *
377ip6_punt_redirect_show_cmd (vlib_main_t * vm,
378 unformat_input_t * main_input,
379 vlib_cli_command_t * cmd)
380{
381 vlib_cli_output (vm, "%U", format_ip_punt_redirect, &ip6_punt_redirect_cfg);
382
383 return (NULL);
384}
385
386/*?
387 *
388 * @cliexpar
389 * @cliexcmd{set ip punt policer <INDEX>}
390 ?*/
391/* *INDENT-OFF* */
392VLIB_CLI_COMMAND (show_ip6_punt_redirect_command, static) =
393{
394 .path = "show ip6 punt redirect",
395 .function = ip6_punt_redirect_show_cmd,
Swarup Nayaka3611a72017-12-06 18:55:43 +0530396 .short_help = "show ip6 punt redirect",
Neale Rannsd91c1db2017-07-31 02:30:50 -0700397 .is_mp_safe = 1,
398};
399/* *INDENT-ON* */
400
401/*
402 * fd.io coding-style-patch-verification: ON
403 *
404 * Local Variables:
405 * eval: (c-set-style "gnu")
406 * End:
407 */