blob: a1f0ebe6901b7c9b3a20a676fa68236f4f79c6a9 [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};
210/* *INDENT-ON* */
211
212ip_punt_redirect_t ip6_punt_redirect_cfg = {
213 .any_rx_sw_if_index = {
214 .tx_sw_if_index = ~0,
215 }
216 ,
217};
218
219#define foreach_ip6_punt_redirect_error \
220_(DROP, "ip6 punt redirect drop")
221
222typedef enum
223{
224#define _(sym,str) IP6_PUNT_REDIRECT_ERROR_##sym,
225 foreach_ip6_punt_redirect_error
226#undef _
227 IP6_PUNT_REDIRECT_N_ERROR,
228} ip6_punt_redirect_error_t;
229
230static char *ip6_punt_redirect_error_strings[] = {
231#define _(sym,string) string,
232 foreach_ip6_punt_redirect_error
233#undef _
234};
235
236static uword
237ip6_punt_redirect (vlib_main_t * vm,
238 vlib_node_runtime_t * node, vlib_frame_t * frame)
239{
240 return (ip_punt_redirect (vm, node, frame,
241 vnet_feat_arc_ip6_punt.feature_arc_index,
242 &ip6_punt_redirect_cfg));
243}
244
245/* *INDENT-OFF* */
246VLIB_REGISTER_NODE (ip6_punt_redirect_node, static) = {
247 .function = ip6_punt_redirect,
248 .name = "ip6-punt-redirect",
249 .vector_size = sizeof (u32),
250 .n_next_nodes = IP_PUNT_REDIRECT_N_NEXT,
251 .format_trace = format_ip_punt_redirect_trace,
252 .n_errors = ARRAY_LEN(ip6_punt_redirect_error_strings),
253 .error_strings = ip6_punt_redirect_error_strings,
254
255 /* edit / add dispositions here */
256 .next_nodes = {
257 [IP_PUNT_REDIRECT_NEXT_DROP] = "ip6-drop",
258 [IP_PUNT_REDIRECT_NEXT_TX] = "ip6-rewrite",
259 [IP_PUNT_REDIRECT_NEXT_ARP] = "ip6-discover-neighbor",
260 },
261};
262
263VLIB_NODE_FUNCTION_MULTIARCH (ip6_punt_redirect_node,
264 ip6_punt_redirect);
265
266VNET_FEATURE_INIT (ip6_punt_redirect_node, static) = {
267 .arc_name = "ip6-punt",
268 .node_name = "ip6-punt-redirect",
269 .runs_before = VNET_FEATURES("error-punt")
270};
271/* *INDENT-ON* */
272
273void
274ip6_punt_redirect_add (u32 rx_sw_if_index,
275 u32 tx_sw_if_index, ip46_address_t * nh)
276{
277 ip_punt_redirect_rx_t rx = {
278 .tx_sw_if_index = tx_sw_if_index,
279 .nh = *nh,
280 };
281
282 ip_punt_redirect_add (&ip6_punt_redirect_cfg,
283 rx_sw_if_index, &rx, FIB_PROTOCOL_IP6, VNET_LINK_IP6);
284
285 vnet_feature_enable_disable ("ip6-punt", "ip6-punt-redirect", 0, 1, 0, 0);
286}
287
288void
289ip6_punt_redirect_del (u32 rx_sw_if_index)
290{
291 vnet_feature_enable_disable ("ip6-punt", "ip6-punt-redirect", 0, 0, 0, 0);
292
293 ip_punt_redirect_del (&ip6_punt_redirect_cfg, rx_sw_if_index);
294}
295
296static clib_error_t *
297ip6_punt_redirect_cmd (vlib_main_t * vm,
298 unformat_input_t * main_input,
299 vlib_cli_command_t * cmd)
300{
301 unformat_input_t _line_input, *line_input = &_line_input;
302 clib_error_t *error = 0;
303 u32 rx_sw_if_index;
304 u32 tx_sw_if_index;
305 ip46_address_t nh;
306 vnet_main_t *vnm;
307 u8 is_add;
308
309 is_add = 1;
310 vnm = vnet_get_main ();
311
312 if (!unformat_user (main_input, unformat_line_input, line_input))
313 return 0;
314
315 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
316 {
317 if (unformat (line_input, "del"))
318 is_add = 0;
319 else if (unformat (line_input, "add"))
320 is_add = 1;
321 else if (unformat (line_input, "rx all"))
322 rx_sw_if_index = ~0;
323 else if (unformat (line_input, "rx %U",
324 unformat_vnet_sw_interface, vnm, &rx_sw_if_index))
325 ;
326 else if (unformat (line_input, "via %U %U",
327 unformat_ip6_address,
328 &nh.ip6,
329 unformat_vnet_sw_interface, vnm, &tx_sw_if_index))
330 ;
331 else if (unformat (line_input, "via %U",
332 unformat_vnet_sw_interface, vnm, &tx_sw_if_index))
333 memset (&nh, 0, sizeof (nh));
334 else
335 {
336 error = unformat_parse_error (line_input);
337 goto done;
338 }
339 }
340
341 if (is_add)
342 ip6_punt_redirect_add (rx_sw_if_index, tx_sw_if_index, &nh);
343 else
344 ip6_punt_redirect_del (rx_sw_if_index);
345
346done:
347 unformat_free (line_input);
348 return (error);
349}
350
351/*?
352 *
353 * @cliexpar
354 * @cliexcmd{set ip punt policer <INDEX>}
355 ?*/
356/* *INDENT-OFF* */
357VLIB_CLI_COMMAND (ip6_punt_redirect_command, static) =
358{
359 .path = "ip6 punt redirect",
360 .function = ip6_punt_redirect_cmd,
361 .short_help = "ip6 punt redirect [add|del] rx [<interface>|all] via [<nh>] <tx_interface>",
362};
363/* *INDENT-ON* */
364
365static clib_error_t *
366ip6_punt_redirect_show_cmd (vlib_main_t * vm,
367 unformat_input_t * main_input,
368 vlib_cli_command_t * cmd)
369{
370 vlib_cli_output (vm, "%U", format_ip_punt_redirect, &ip6_punt_redirect_cfg);
371
372 return (NULL);
373}
374
375/*?
376 *
377 * @cliexpar
378 * @cliexcmd{set ip punt policer <INDEX>}
379 ?*/
380/* *INDENT-OFF* */
381VLIB_CLI_COMMAND (show_ip6_punt_redirect_command, static) =
382{
383 .path = "show ip6 punt redirect",
384 .function = ip6_punt_redirect_show_cmd,
385 .short_help = "show ip6 punt redirect [add|del] rx [<interface>|all] via [<nh>] <tx_interface>",
386 .is_mp_safe = 1,
387};
388/* *INDENT-ON* */
389
390/*
391 * fd.io coding-style-patch-verification: ON
392 *
393 * Local Variables:
394 * eval: (c-set-style "gnu")
395 * End:
396 */