2 * Copyright (c) 2018 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/vnet.h>
17 #include <vnet/devices/devices.h>
18 #include <vnet/ip/ip.h>
19 #include <vnet/ethernet/ethernet.h>
20 #include <vnet/flow/flow.h>
22 static format_function_t format_flow;
25 unformat_ip_port_and_mask (unformat_input_t * input, va_list * args)
27 ip_port_and_mask_t *pm = va_arg (*args, ip_port_and_mask_t *);
28 u32 port = 0, mask = 0;
30 if (unformat (input, "any"))
32 else if (unformat (input, "%u/%u", &port, &mask))
34 else if (unformat (input, "%u/0x%x", &port, &mask))
36 else if (unformat (input, "%u", &port))
41 if (port > 0xffff || mask > 0xffff)
50 format_ip_port_and_mask (u8 * s, va_list * args)
52 ip_port_and_mask_t *pm = va_arg (*args, ip_port_and_mask_t *);
54 if (pm->port == 0 && pm->mask == 0)
55 return format (s, "any");
57 if (pm->mask == 0xffff)
58 return format (s, "%u", pm->port);
60 return format (s, "%u/0x%x", pm->port, pm->mask);
64 format_flow_error (u8 * s, va_list * args)
66 int error = va_arg (*args, int);
69 return format (s, "no error");
71 #define _(v,n,str) if (error == v) return format (s, #str);
75 return format (s, "unknown error (%d)", error);
79 format_flow_actions (u8 * s, va_list * args)
81 u32 actions = va_arg (*args, u32);
84 #define _(a, b, c) if (actions & (1 << a)) \
85 t = format (t, "%s%s", t ? " ":"", c);
88 s = format (s, "%v", t);
94 format_flow_enabled_hw (u8 * s, va_list * args)
96 u32 flow_index = va_arg (*args, u32);
97 vnet_flow_t *f = vnet_get_flow (flow_index);
99 return format (s, "not found");
104 vnet_main_t *vnm = vnet_get_main ();
106 hash_foreach (hw_if_index, private_data, f->private_data,
108 t = format (t, "%s%U", t ? ", " : "",
109 format_vnet_hw_if_index_name, vnm, hw_if_index);
112 s = format (s, "%v", t);
117 static const char *flow_type_strings[] = { 0,
123 static clib_error_t *
124 show_flow_entry (vlib_main_t * vm, unformat_input_t * input,
125 vlib_cli_command_t * cmd_arg)
127 vnet_main_t *vnm = vnet_get_main ();
128 vnet_flow_main_t *fm = &flow_main;
129 unformat_input_t _line_input, *line_input = &_line_input;
130 vnet_hw_interface_t *hi;
131 vnet_device_class_t *dev_class;
134 u32 index = ~0, hw_if_index;
136 if (!unformat_user (input, unformat_line_input, line_input))
139 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
141 if (unformat (line_input, "index %u", &index))
144 return clib_error_return (0, "parse error: '%U'",
145 format_unformat_error, line_input);
148 unformat_free (line_input);
152 if ((f = vnet_get_flow (index)) == 0)
153 return clib_error_return (0, "no such flow");
155 vlib_cli_output (vm, "%-10s: %u", "index", f->index);
156 vlib_cli_output (vm, "%-10s: %s", "type", flow_type_strings[f->type]);
157 vlib_cli_output (vm, "%-10s: %U", "match", format_flow, f);
159 hash_foreach (hw_if_index, private_data, f->private_data,
161 hi = vnet_get_hw_interface (vnm, hw_if_index);
162 dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
163 vlib_cli_output (vm, "interface %U\n",
164 format_vnet_hw_if_index_name, vnm, hw_if_index);
165 if (dev_class->format_flow)
166 vlib_cli_output (vm, " %U\n", dev_class->format_flow,
167 hi->dev_instance, f->index, private_data);
175 pool_foreach (f, fm->global_flow_pool,
177 vlib_cli_output (vm, "%U\n", format_flow, f);
185 VLIB_CLI_COMMAND (show_flow_entry_command, static) = {
186 .path = "show flow entry",
187 .short_help = "show flow entry [index <index>]",
188 .function = show_flow_entry,
192 static clib_error_t *
193 show_flow_ranges (vlib_main_t * vm, unformat_input_t * input,
194 vlib_cli_command_t * cmd_arg)
196 vnet_flow_main_t *fm = &flow_main;
197 vnet_flow_range_t *r = 0;
199 vlib_cli_output (vm, "%8s %8s %s", "Start", "Count", "Owner");
202 vec_foreach (r, fm->ranges)
204 vlib_cli_output (vm, "%8u %8u %s", r->start, r->count, r->owner);
211 VLIB_CLI_COMMAND (show_flow_ranges_command, static) = {
212 .path = "show flow ranges",
213 .short_help = "show flow ranges",
214 .function = show_flow_ranges,
218 static clib_error_t *
219 show_flow_interface (vlib_main_t * vm, unformat_input_t * input,
220 vlib_cli_command_t * cmd_arg)
222 vnet_main_t *vnm = vnet_get_main ();
223 vnet_hw_interface_t *hi;
224 vnet_device_class_t *dev_class;
225 unformat_input_t _line_input, *line_input = &_line_input;
226 u32 hw_if_index = ~0;
228 if (unformat_user (input, unformat_line_input, line_input))
230 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
232 if (unformat (line_input, "%U",
233 unformat_vnet_hw_interface, vnm, &hw_if_index))
236 return clib_error_return (0, "parse error: '%U'",
237 format_unformat_error, line_input);
239 unformat_free (line_input);
242 if (hw_if_index == ~0)
243 return clib_error_return (0, "please specify interface");
245 hi = vnet_get_hw_interface (vnm, hw_if_index);
246 dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
247 if (dev_class->format_flow == 0)
248 return clib_error_return (0, "not supported");
250 vlib_cli_output (vm, "%U", dev_class->format_flow, hi->dev_instance, ~0, 0);
255 VLIB_CLI_COMMAND (show_flow_interface_command, static) = {
256 .path = "show flow interface",
257 .short_help = "show flow interface <interface name>",
258 .function = show_flow_interface,
262 static clib_error_t *
263 test_flow (vlib_main_t * vm, unformat_input_t * input,
264 vlib_cli_command_t * cmd_arg)
267 vnet_main_t *vnm = vnet_get_main ();
268 unformat_input_t _line_input, *line_input = &_line_input;
276 } action = FLOW_UNKNOWN_ACTION;
277 u32 hw_if_index = ~0, flow_index = ~0;
279 u32 prot = 0, teid = 0;
280 vnet_flow_type_t type = VNET_FLOW_TYPE_IP4_N_TUPLE;
281 bool is_gtpc_set = false;
282 bool is_gtpu_set = false;
283 vnet_flow_type_t outer_type = VNET_FLOW_TYPE_UNKNOWN;
284 vnet_flow_type_t inner_type = VNET_FLOW_TYPE_UNKNOWN;
285 bool outer_ip4_set = false, inner_ip4_set = false;
286 bool outer_ip6_set = false, inner_ip6_set = false;
287 ip4_address_and_mask_t ip4s = { };
288 ip4_address_and_mask_t ip4d = { };
289 ip4_address_and_mask_t inner_ip4s = { };
290 ip4_address_and_mask_t inner_ip4d = { };
291 ip6_address_and_mask_t ip6s = { };
292 ip6_address_and_mask_t ip6d = { };
293 ip6_address_and_mask_t inner_ip6s = { };
294 ip6_address_and_mask_t inner_ip6d = { };
295 ip_port_and_mask_t sport = { };
296 ip_port_and_mask_t dport = { };
298 bool ethernet_set = false;
300 clib_memset (&flow, 0, sizeof (vnet_flow_t));
303 flow.ip4_n_tuple.protocol = ~0;
304 if (!unformat_user (input, unformat_line_input, line_input))
307 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
309 if (unformat (line_input, "add"))
311 else if (unformat (line_input, "del"))
313 else if (unformat (line_input, "enable"))
314 action = FLOW_ENABLE;
315 else if (unformat (line_input, "disable"))
316 action = FLOW_DISABLE;
317 else if (unformat (line_input, "eth-type %U",
318 unformat_ethernet_type_host_byte_order, ð_type))
320 else if (unformat (line_input, "src-ip %U",
321 unformat_ip4_address_and_mask, &ip4s))
322 outer_ip4_set = true;
323 else if (unformat (line_input, "dst-ip %U",
324 unformat_ip4_address_and_mask, &ip4d))
325 outer_ip4_set = true;
326 else if (unformat (line_input, "ip6-src-ip %U",
327 unformat_ip6_address_and_mask, &ip6s))
328 outer_ip6_set = true;
329 else if (unformat (line_input, "ip6-dst-ip %U",
330 unformat_ip6_address_and_mask, &ip6d))
331 outer_ip6_set = true;
332 else if (unformat (line_input, "inner-src-ip %U",
333 unformat_ip4_address_and_mask, &inner_ip4s))
334 inner_ip4_set = true;
335 else if (unformat (line_input, "inner-dst-ip %U",
336 unformat_ip4_address_and_mask, &inner_ip4d))
337 inner_ip4_set = true;
338 else if (unformat (line_input, "inner-ip6-src-ip %U",
339 unformat_ip6_address_and_mask, &inner_ip6s))
340 inner_ip6_set = true;
341 else if (unformat (line_input, "inner-ip6-dst-ip %U",
342 unformat_ip6_address_and_mask, &inner_ip6d))
343 inner_ip6_set = true;
344 else if (unformat (line_input, "src-port %U", unformat_ip_port_and_mask,
347 else if (unformat (line_input, "dst-port %U", unformat_ip_port_and_mask,
350 else if (unformat (line_input, "proto %U", unformat_ip_protocol, &prot))
352 else if (unformat (line_input, "proto %u", &prot))
354 else if (unformat (line_input, "gtpc teid %u", &teid))
356 else if (unformat (line_input, "gtpu teid %u", &teid))
358 else if (unformat (line_input, "index %u", &flow_index))
360 else if (unformat (line_input, "next-node %U", unformat_vlib_node, vm,
361 &flow.redirect_node_index))
362 flow.actions |= VNET_FLOW_ACTION_REDIRECT_TO_NODE;
363 else if (unformat (line_input, "mark %d", &flow.mark_flow_id))
364 flow.actions |= VNET_FLOW_ACTION_MARK;
365 else if (unformat (line_input, "buffer-advance %d",
366 &flow.buffer_advance))
367 flow.actions |= VNET_FLOW_ACTION_BUFFER_ADVANCE;
368 else if (unformat (line_input, "redirect-to-queue %d",
369 &flow.redirect_queue))
370 flow.actions |= VNET_FLOW_ACTION_REDIRECT_TO_QUEUE;
371 else if (unformat (line_input, "drop"))
372 flow.actions |= VNET_FLOW_ACTION_DROP;
373 else if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
377 return clib_error_return (0, "parse error: '%U'",
378 format_unformat_error, line_input);
381 unformat_free (line_input);
383 if (hw_if_index == ~0 && (action == FLOW_ENABLE || action == FLOW_DISABLE))
384 return clib_error_return (0, "Please specify interface name");
386 if (flow_index == ~0 && (action == FLOW_ENABLE || action == FLOW_DISABLE ||
388 return clib_error_return (0, "Please specify flow index");
393 if (flow.actions == 0)
394 return clib_error_return (0, "Please specify at least one action");
396 /* Adjust the flow type */
397 if (ethernet_set == true)
398 outer_type = VNET_FLOW_TYPE_ETHERNET;
399 if (outer_ip4_set == true)
400 outer_type = VNET_FLOW_TYPE_IP4_N_TUPLE;
401 else if (outer_ip6_set == true)
402 outer_type = VNET_FLOW_TYPE_IP6_N_TUPLE;
403 if (inner_ip4_set == true)
404 inner_type = VNET_FLOW_TYPE_IP4_N_TUPLE;
405 else if (inner_ip6_set == true)
406 inner_type = VNET_FLOW_TYPE_IP6_N_TUPLE;
408 if (outer_type == VNET_FLOW_TYPE_UNKNOWN)
409 return clib_error_return (0, "Please specify a supported flow type");
411 if (outer_type == VNET_FLOW_TYPE_ETHERNET)
412 type = VNET_FLOW_TYPE_ETHERNET;
413 else if (outer_type == VNET_FLOW_TYPE_IP4_N_TUPLE)
415 type = VNET_FLOW_TYPE_IP4_N_TUPLE;
417 if (inner_type == VNET_FLOW_TYPE_UNKNOWN)
420 type = VNET_FLOW_TYPE_IP4_GTPC;
421 else if (is_gtpu_set)
422 type = VNET_FLOW_TYPE_IP4_GTPU;
424 else if (inner_type == VNET_FLOW_TYPE_IP4_N_TUPLE)
427 type = VNET_FLOW_TYPE_IP4_GTPU_IP4;
429 else if (inner_type == VNET_FLOW_TYPE_IP6_N_TUPLE)
432 type = VNET_FLOW_TYPE_IP4_GTPU_IP6;
435 else if (outer_type == VNET_FLOW_TYPE_IP6_N_TUPLE)
437 type = VNET_FLOW_TYPE_IP6_N_TUPLE;
439 if (inner_type == VNET_FLOW_TYPE_UNKNOWN)
442 type = VNET_FLOW_TYPE_IP6_GTPC;
443 else if (is_gtpu_set)
444 type = VNET_FLOW_TYPE_IP6_GTPU;
446 else if (inner_type == VNET_FLOW_TYPE_IP4_N_TUPLE)
449 type = VNET_FLOW_TYPE_IP6_GTPU_IP4;
451 else if (inner_type == VNET_FLOW_TYPE_IP6_N_TUPLE)
454 type = VNET_FLOW_TYPE_IP6_GTPU_IP6;
458 //assign specific field values per flow type
461 case VNET_FLOW_TYPE_ETHERNET:
462 memset (&flow.ethernet, 0, sizeof (flow.ethernet));
463 flow.ethernet.eth_hdr.type = eth_type;
466 case VNET_FLOW_TYPE_IP4_N_TUPLE:
467 case VNET_FLOW_TYPE_IP4_GTPC:
468 case VNET_FLOW_TYPE_IP4_GTPU:
469 case VNET_FLOW_TYPE_IP4_GTPU_IP4:
470 case VNET_FLOW_TYPE_IP4_GTPU_IP6:
471 clib_memcpy (&flow.ip4_n_tuple.src_addr, &ip4s,
472 sizeof (ip4_address_and_mask_t));
473 clib_memcpy (&flow.ip4_n_tuple.dst_addr, &ip4d,
474 sizeof (ip4_address_and_mask_t));
475 clib_memcpy (&flow.ip4_n_tuple.src_port, &sport,
476 sizeof (ip_port_and_mask_t));
477 clib_memcpy (&flow.ip4_n_tuple.dst_port, &dport,
478 sizeof (ip_port_and_mask_t));
479 flow.ip4_n_tuple.protocol = prot;
481 if (type == VNET_FLOW_TYPE_IP4_GTPC)
482 flow.ip4_gtpc.teid = teid;
483 else if (type == VNET_FLOW_TYPE_IP4_GTPU)
484 flow.ip4_gtpu.teid = teid;
485 else if (type == VNET_FLOW_TYPE_IP4_GTPU_IP4)
487 flow.ip4_gtpu_ip4.teid = teid;
488 clib_memcpy (&flow.ip4_gtpu_ip4.inner_src_addr, &inner_ip4s,
489 sizeof (ip4_address_and_mask_t));
490 clib_memcpy (&flow.ip4_gtpu_ip4.inner_dst_addr, &inner_ip4d,
491 sizeof (ip4_address_and_mask_t));
493 else if (type == VNET_FLOW_TYPE_IP4_GTPU_IP6)
495 flow.ip4_gtpu_ip6.teid = teid;
496 clib_memcpy (&flow.ip4_gtpu_ip6.inner_src_addr, &inner_ip6s,
497 sizeof (ip6_address_and_mask_t));
498 clib_memcpy (&flow.ip4_gtpu_ip6.inner_dst_addr, &inner_ip6d,
499 sizeof (ip6_address_and_mask_t));
502 if (flow.ip4_n_tuple.protocol == (ip_protocol_t) ~ 0)
503 return clib_error_return (0, "Please specify ip protocol");
504 if ((type != VNET_FLOW_TYPE_IP4_N_TUPLE) &&
505 (flow.ip4_n_tuple.protocol != IP_PROTOCOL_UDP))
506 return clib_error_return (0,
507 "For GTP related flow, ip protocol must be UDP");
510 case VNET_FLOW_TYPE_IP6_N_TUPLE:
511 case VNET_FLOW_TYPE_IP6_GTPC:
512 case VNET_FLOW_TYPE_IP6_GTPU:
513 case VNET_FLOW_TYPE_IP6_GTPU_IP4:
514 case VNET_FLOW_TYPE_IP6_GTPU_IP6:
515 clib_memcpy (&flow.ip6_n_tuple.src_addr, &ip6s,
516 sizeof (ip6_address_and_mask_t));
517 clib_memcpy (&flow.ip6_n_tuple.dst_addr, &ip6d,
518 sizeof (ip6_address_and_mask_t));
519 clib_memcpy (&flow.ip6_n_tuple.src_port, &sport,
520 sizeof (ip_port_and_mask_t));
521 clib_memcpy (&flow.ip6_n_tuple.dst_port, &dport,
522 sizeof (ip_port_and_mask_t));
523 flow.ip6_n_tuple.protocol = prot;
525 if (type == VNET_FLOW_TYPE_IP6_GTPC)
526 flow.ip6_gtpc.teid = teid;
527 else if (type == VNET_FLOW_TYPE_IP6_GTPU)
528 flow.ip6_gtpu.teid = teid;
529 else if (type == VNET_FLOW_TYPE_IP6_GTPU_IP4)
531 flow.ip6_gtpu_ip4.teid = teid;
532 clib_memcpy (&flow.ip6_gtpu_ip4.inner_src_addr, &inner_ip4s,
533 sizeof (ip4_address_and_mask_t));
534 clib_memcpy (&flow.ip6_gtpu_ip4.inner_dst_addr, &inner_ip4d,
535 sizeof (ip4_address_and_mask_t));
537 else if (type == VNET_FLOW_TYPE_IP6_GTPU_IP6)
539 flow.ip6_gtpu_ip6.teid = teid;
540 clib_memcpy (&flow.ip6_gtpu_ip6.inner_src_addr, &inner_ip6s,
541 sizeof (ip6_address_and_mask_t));
542 clib_memcpy (&flow.ip6_gtpu_ip6.inner_dst_addr, &inner_ip6d,
543 sizeof (ip6_address_and_mask_t));
546 if (flow.ip6_n_tuple.protocol == (ip_protocol_t) ~ 0)
547 return clib_error_return (0, "Please specify ip protocol");
548 if ((type != VNET_FLOW_TYPE_IP4_N_TUPLE) &&
549 (flow.ip6_n_tuple.protocol != IP_PROTOCOL_UDP))
550 return clib_error_return (0,
551 "For GTP related flow, ip protocol must be UDP");
559 rv = vnet_flow_add (vnm, &flow, &flow_index);
561 printf ("flow %u added\n", flow_index);
565 rv = vnet_flow_del (vnm, flow_index);
568 rv = vnet_flow_enable (vnm, flow_index, hw_if_index);
571 rv = vnet_flow_disable (vnm, flow_index, hw_if_index);
574 return clib_error_return (0, "please specify action (add, del, enable,"
579 return clib_error_return (0, "flow error: %U", format_flow_error, rv);
584 VLIB_CLI_COMMAND (test_flow_command, static) = {
586 .short_help = "test flow add [src-ip <ip-addr/mask>] [dst-ip "
587 "<ip-addr/mask>] [src-port <port/mask>] [dst-port <port/mask>] "
589 .function = test_flow,
595 format_flow_match_element (u8 * s, va_list * args)
597 char *type = va_arg (*args, char *);
598 void *ptr = va_arg (*args, void *);
600 if (strncmp (type, "u8", 2) == 0)
601 return format (s, "%d", *(u8 *) ptr);
603 if (strncmp (type, "u16", 3) == 0)
604 return format (s, "%d", *(u16 *) ptr);
606 if (strncmp (type, "u32", 3) == 0)
607 return format (s, "%d", *(u32 *) ptr);
609 if (strncmp (type, "ip4_address_t", 13) == 0)
610 return format (s, "%U", format_ip4_address, ptr);
612 if (strncmp (type, "ip4_address_and_mask_t", 13) == 0)
613 return format (s, "%U", format_ip4_address_and_mask, ptr);
615 if (strncmp (type, "ip6_address_t", 13) == 0)
616 return format (s, "%U", format_ip6_address, ptr);
618 if (strncmp (type, "ip6_address_and_mask_t", 13) == 0)
619 return format (s, "%U", format_ip6_address_and_mask, ptr);
621 if (strncmp (type, "ip_protocol_t", 13) == 0)
622 return format (s, "%U", format_ip_protocol, *(ip_protocol_t *) ptr);
624 if (strncmp (type, "ip_port_and_mask_t", 18) == 0)
625 return format (s, "%U", format_ip_port_and_mask, ptr);
627 s = format (s, "unknown type '%s'", type);
631 #define _fe(a,b) s2 = format (s2, "%s%s %U", s2 ? ", ":"", #b, \
632 format_flow_match_element, #a, &f->b);
634 u8 * format_flow_match_##b (u8 * s, va_list * args) \
636 vnet_flow_##b##_t *f = __builtin_va_arg (*args, vnet_flow_##b##_t *); \
638 foreach_flow_entry_##b \
639 s = format (s, "%v", s2);; \
647 format_flow_match (u8 * s, va_list * args)
649 vnet_flow_t *f = va_arg (*args, vnet_flow_t *);
652 if (f->type == VNET_FLOW_TYPE_##a) \
653 return format (s, "%U", format_flow_match_##b, &f->b);
661 format_flow (u8 * s, va_list * args)
663 vlib_main_t *vm = vlib_get_main ();
664 vnet_flow_t *f = va_arg (*args, vnet_flow_t *);
665 u32 indent = format_get_indent (s);
668 s = format (s, "flow-index %u type %s active %u",
669 f->index, flow_type_strings[f->type],
670 hash_elts (f->private_data)),
671 s = format (s, "\n%Umatch: %U", format_white_space, indent + 2,
672 format_flow_match, f);
673 s = format (s, "\n%Uaction: %U", format_white_space, indent + 2,
674 format_flow_actions, f->actions);
676 if (f->actions & VNET_FLOW_ACTION_MARK)
677 t = format (t, "%smark %u", t ? ", " : "", f->mark_flow_id);
679 if (f->actions & VNET_FLOW_ACTION_REDIRECT_TO_NODE)
680 t = format (t, "%snext-node %U", t ? ", " : "",
681 format_vlib_node_name, vm, f->redirect_node_index);
683 if (f->actions & VNET_FLOW_ACTION_BUFFER_ADVANCE)
684 t = format (t, "%sbuffer-advance %d", t ? ", " : "", f->buffer_advance);
688 s = format (s, "\n%U%v", format_white_space, indent + 4, t);
696 * fd.io coding-style-patch-verification: ON
699 * eval: (c-set-style "gnu")