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.
17 #include <vnet/vnet.h>
18 #include <vnet/devices/devices.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vnet/ethernet/packet.h>
22 #include <vnet/flow/flow.h>
24 static format_function_t format_flow;
27 unformat_ip_port_and_mask (unformat_input_t * input, va_list * args)
29 ip_port_and_mask_t *pm = va_arg (*args, ip_port_and_mask_t *);
30 u32 port = 0, mask = 0;
32 if (unformat (input, "any"))
34 else if (unformat (input, "%u/%u", &port, &mask))
36 else if (unformat (input, "%u/0x%x", &port, &mask))
38 else if (unformat (input, "%u", &port))
43 if (port > 0xffff || mask > 0xffff)
52 format_ip_port_and_mask (u8 * s, va_list * args)
54 ip_port_and_mask_t *pm = va_arg (*args, ip_port_and_mask_t *);
56 if (pm->port == 0 && pm->mask == 0)
57 return format (s, "any");
59 if (pm->mask == 0xffff)
60 return format (s, "%u", pm->port);
62 return format (s, "%u/0x%x", pm->port, pm->mask);
66 unformat_ip_protocol_and_mask (unformat_input_t * input, va_list * args)
68 ip_prot_and_mask_t *pm = va_arg (*args, ip_prot_and_mask_t *);
69 u32 prot = 0, mask = 0;
71 if (unformat (input, "any"))
73 else if (unformat (input, "%U", unformat_ip_protocol, &prot))
75 else if (unformat (input, "%u", &prot))
80 if (prot > 0XFF || mask > 0xFF)
89 format_ip_protocol_and_mask (u8 * s, va_list * args)
91 ip_prot_and_mask_t *pm = va_arg (*args, ip_prot_and_mask_t *);
93 if (pm->prot == 0 && pm->mask == 0)
94 return format (s, "any");
96 return format (s, "%U", format_ip_protocol, pm->prot);
100 format_flow_error (u8 * s, va_list * args)
102 int error = va_arg (*args, int);
105 return format (s, "no error");
107 #define _(v,n,str) if (error == v) return format (s, #str);
111 return format (s, "unknown error (%d)", error);
115 format_flow_actions (u8 * s, va_list * args)
117 u32 actions = va_arg (*args, u32);
120 #define _(a, b, c) if (actions & (1 << a)) \
121 t = format (t, "%s%s", t ? " ":"", c);
124 s = format (s, "%v", t);
130 format_flow_enabled_hw (u8 * s, va_list * args)
132 u32 flow_index = va_arg (*args, u32);
133 vnet_flow_t *f = vnet_get_flow (flow_index);
135 return format (s, "not found");
140 vnet_main_t *vnm = vnet_get_main ();
142 hash_foreach (hw_if_index, private_data, f->private_data,
144 t = format (t, "%s%U", t ? ", " : "",
145 format_vnet_hw_if_index_name, vnm, hw_if_index);
148 s = format (s, "%v", t);
154 format_rss_function (u8 * s, va_list * args)
156 vnet_rss_function_t func = va_arg (*args, vnet_rss_function_t);
162 else if (func == VNET_RSS_FUNC_##f) \
163 return format (s, n);
167 return format (s, "unknown");
171 format_rss_types (u8 * s, va_list * args)
173 u64 type = va_arg (*args, u64);
177 if (type & (1UL<<a)) \
178 s = format (s, "%s ", c);
180 foreach_flow_rss_types
185 static const char *flow_type_strings[] = { 0,
191 static clib_error_t *
192 show_flow_entry (vlib_main_t * vm, unformat_input_t * input,
193 vlib_cli_command_t * cmd_arg)
195 vnet_main_t *vnm = vnet_get_main ();
196 vnet_flow_main_t *fm = &flow_main;
197 unformat_input_t _line_input, *line_input = &_line_input;
198 vnet_hw_interface_t *hi;
199 vnet_device_class_t *dev_class;
202 u32 index = ~0, hw_if_index;
204 if (!unformat_user (input, unformat_line_input, line_input))
207 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
209 if (unformat (line_input, "index %u", &index))
212 return clib_error_return (0, "parse error: '%U'",
213 format_unformat_error, line_input);
216 unformat_free (line_input);
220 if ((f = vnet_get_flow (index)) == 0)
221 return clib_error_return (0, "no such flow");
223 vlib_cli_output (vm, "%-10s: %u", "index", f->index);
224 vlib_cli_output (vm, "%-10s: %s", "type", flow_type_strings[f->type]);
225 vlib_cli_output (vm, "%-10s: %U", "match", format_flow, f);
226 if (f->type == VNET_FLOW_TYPE_GENERIC)
228 vlib_cli_output (vm, "%s: %s", "spec", f->generic.pattern.spec);
229 vlib_cli_output (vm, "%s: %s", "mask", f->generic.pattern.mask);
232 hash_foreach (hw_if_index, private_data, f->private_data,
234 hi = vnet_get_hw_interface (vnm, hw_if_index);
235 dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
236 vlib_cli_output (vm, "interface %U\n",
237 format_vnet_hw_if_index_name, vnm, hw_if_index);
238 if (dev_class->format_flow)
239 vlib_cli_output (vm, " %U\n", dev_class->format_flow,
240 hi->dev_instance, f->index, private_data);
248 pool_foreach (f, fm->global_flow_pool)
250 vlib_cli_output (vm, "%U\n", format_flow, f);
251 if (f->type == VNET_FLOW_TYPE_GENERIC)
253 vlib_cli_output (vm, "%s: %s", "spec", f->generic.pattern.spec);
254 vlib_cli_output (vm, "%s: %s", "mask", f->generic.pattern.mask);
263 VLIB_CLI_COMMAND (show_flow_entry_command, static) = {
264 .path = "show flow entry",
265 .short_help = "show flow entry [index <index>]",
266 .function = show_flow_entry,
270 static clib_error_t *
271 show_flow_ranges (vlib_main_t * vm, unformat_input_t * input,
272 vlib_cli_command_t * cmd_arg)
274 vnet_flow_main_t *fm = &flow_main;
275 vnet_flow_range_t *r = 0;
277 vlib_cli_output (vm, "%8s %8s %s", "Start", "Count", "Owner");
280 vec_foreach (r, fm->ranges)
282 vlib_cli_output (vm, "%8u %8u %s", r->start, r->count, r->owner);
289 VLIB_CLI_COMMAND (show_flow_ranges_command, static) = {
290 .path = "show flow ranges",
291 .short_help = "show flow ranges",
292 .function = show_flow_ranges,
296 static clib_error_t *
297 show_flow_interface (vlib_main_t * vm, unformat_input_t * input,
298 vlib_cli_command_t * cmd_arg)
300 vnet_main_t *vnm = vnet_get_main ();
301 vnet_hw_interface_t *hi;
302 vnet_device_class_t *dev_class;
303 unformat_input_t _line_input, *line_input = &_line_input;
304 u32 hw_if_index = ~0;
306 if (unformat_user (input, unformat_line_input, line_input))
308 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
310 if (unformat (line_input, "%U",
311 unformat_vnet_hw_interface, vnm, &hw_if_index))
314 return clib_error_return (0, "parse error: '%U'",
315 format_unformat_error, line_input);
317 unformat_free (line_input);
320 if (hw_if_index == ~0)
321 return clib_error_return (0, "please specify interface");
323 hi = vnet_get_hw_interface (vnm, hw_if_index);
324 dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
325 if (dev_class->format_flow == 0)
326 return clib_error_return (0, "not supported");
328 vlib_cli_output (vm, "%U", dev_class->format_flow, hi->dev_instance, ~0, 0);
333 VLIB_CLI_COMMAND (show_flow_interface_command, static) = {
334 .path = "show flow interface",
335 .short_help = "show flow interface <interface name>",
336 .function = show_flow_interface,
340 static clib_error_t *
341 test_flow (vlib_main_t * vm, unformat_input_t * input,
342 vlib_cli_command_t * cmd_arg)
345 vnet_main_t *vnm = vnet_get_main ();
346 unformat_input_t _line_input, *line_input = &_line_input;
354 } action = FLOW_UNKNOWN_ACTION;
361 } flow_class = FLOW_UNKNOWN_CLASS;
363 u32 hw_if_index = ~0, flow_index = ~0;
365 u32 teid = 0, session_id = 0, spi = 0;
367 u32 queue_start = 0, queue_end = 0;
368 vnet_flow_type_t type = VNET_FLOW_TYPE_UNKNOWN;
369 ip4_address_and_mask_t ip4s = {}, in_ip4s = {};
370 ip4_address_and_mask_t ip4d = {}, in_ip4d = {};
371 ip6_address_and_mask_t ip6s = {}, in_ip6s = {};
372 ip6_address_and_mask_t ip6d = {}, in_ip6d = {};
373 ip_port_and_mask_t sport = {}, in_sport = {};
374 ip_port_and_mask_t dport = {}, in_dport = {};
375 ip_prot_and_mask_t protocol = {}, in_proto = {};
377 bool inner_ip4_set = false, inner_ip6_set = false;
378 bool tcp_udp_port_set = false, inner_port_set = false;
379 bool gtpc_set = false;
380 bool gtpu_set = false;
381 bool vni_set = false;
382 bool l2tpv3oip_set = false;
383 bool ipsec_esp_set = false, ipsec_ah_set = false;
384 u8 *rss_type[3] = { };
389 clib_memset (&flow, 0, sizeof (vnet_flow_t));
393 if (!unformat_user (input, unformat_line_input, line_input))
396 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
398 if (unformat (line_input, "add"))
400 else if (unformat (line_input, "del"))
402 else if (unformat (line_input, "enable"))
403 action = FLOW_ENABLE;
404 else if (unformat (line_input, "disable"))
405 action = FLOW_DISABLE;
406 else if (unformat (line_input, "spec %s", &spec))
408 else if (unformat (line_input, "mask %s", &mask))
410 else if (unformat (line_input, "eth-type %U",
411 unformat_ethernet_type_host_byte_order, ð_type))
412 flow_class = FLOW_ETHERNET_CLASS;
413 else if (unformat (line_input, "src-ip %U",
414 unformat_ip4_address_and_mask, &ip4s))
415 flow_class = FLOW_IPV4_CLASS;
416 else if (unformat (line_input, "dst-ip %U",
417 unformat_ip4_address_and_mask, &ip4d))
418 flow_class = FLOW_IPV4_CLASS;
419 else if (unformat (line_input, "in-src-ip %U",
420 unformat_ip4_address_and_mask, &in_ip4s))
421 inner_ip4_set = true;
422 else if (unformat (line_input, "in-dst-ip %U",
423 unformat_ip4_address_and_mask, &in_ip4d))
424 inner_ip4_set = true;
425 else if (unformat (line_input, "ip6-src-ip %U",
426 unformat_ip6_address_and_mask, &ip6s))
427 flow_class = FLOW_IPV6_CLASS;
428 else if (unformat (line_input, "ip6-dst-ip %U",
429 unformat_ip6_address_and_mask, &ip6d))
430 flow_class = FLOW_IPV6_CLASS;
431 else if (unformat (line_input, "in-ip6-src-ip %U",
432 unformat_ip6_address_and_mask, &in_ip6s))
433 inner_ip6_set = true;
434 else if (unformat (line_input, "in-ip6-dst-ip %U",
435 unformat_ip6_address_and_mask, &in_ip6d))
436 inner_ip6_set = true;
437 else if (unformat (line_input, "src-port %U", unformat_ip_port_and_mask,
439 tcp_udp_port_set = true;
440 else if (unformat (line_input, "dst-port %U", unformat_ip_port_and_mask,
442 tcp_udp_port_set = true;
445 (line_input, "proto %U", unformat_ip_protocol_and_mask,
448 else if (unformat (line_input, "in-src-port %U",
449 unformat_ip_port_and_mask, &in_sport))
450 inner_port_set = true;
451 else if (unformat (line_input, "in-dst-port %U",
452 unformat_ip_port_and_mask, &in_dport))
453 inner_port_set = true;
454 else if (unformat (line_input, "in-proto %U",
455 unformat_ip_protocol_and_mask, &in_proto))
457 else if (unformat (line_input, "gtpc teid %u", &teid))
459 else if (unformat (line_input, "gtpu teid %u", &teid))
461 else if (unformat (line_input, "vxlan vni %u", &vni))
463 else if (unformat (line_input, "session id %u", &session_id))
465 if (protocol.prot == IP_PROTOCOL_L2TP)
466 l2tpv3oip_set = true;
468 else if (unformat (line_input, "spi %u", &spi))
470 if (protocol.prot == IP_PROTOCOL_IPSEC_ESP)
471 ipsec_esp_set = true;
472 else if (protocol.prot == IP_PROTOCOL_IPSEC_AH)
475 else if (unformat (line_input, "index %u", &flow_index))
477 else if (unformat (line_input, "next-node %U", unformat_vlib_node, vm,
478 &flow.redirect_node_index))
479 flow.actions |= VNET_FLOW_ACTION_REDIRECT_TO_NODE;
480 else if (unformat (line_input, "mark %d", &flow.mark_flow_id))
481 flow.actions |= VNET_FLOW_ACTION_MARK;
482 else if (unformat (line_input, "buffer-advance %d",
483 &flow.buffer_advance))
484 flow.actions |= VNET_FLOW_ACTION_BUFFER_ADVANCE;
485 else if (unformat (line_input, "redirect-to-queue %d",
486 &flow.redirect_queue))
487 flow.actions |= VNET_FLOW_ACTION_REDIRECT_TO_QUEUE;
488 else if (unformat (line_input, "drop"))
489 flow.actions |= VNET_FLOW_ACTION_DROP;
490 else if (unformat (line_input, "rss function"))
496 else if (unformat (line_input, s)) \
497 flow.rss_fun = VNET_RSS_FUNC_##f;
503 return clib_error_return (0, "unknown input `%U'",
504 format_unformat_error, line_input);
507 flow.actions |= VNET_FLOW_ACTION_RSS;
509 else if (unformat (line_input, "rss types"))
516 if (unformat (line_input, "%s use %s and %s",
517 &rss_type[0], &rss_type[1], &rss_type[2]))
520 (line_input, "%s use %s", &rss_type[0], &rss_type[1]))
522 else if (unformat (line_input, "%s", &rss_type[0]))
527 else if (!clib_strcmp(c, (const char *)type_str)) \
528 flow.rss_types |= (1ULL<<a);
530 #define check_rss_types(_str) \
535 foreach_flow_rss_types \
538 return clib_error_return (0, "parse error: '%U'", \
539 format_unformat_error, line_input); \
543 check_rss_types (rss_type[0])
544 check_rss_types (rss_type[1]) check_rss_types (rss_type[2])
546 flow.actions |= VNET_FLOW_ACTION_RSS;
548 else if (unformat (line_input, "rss queues"))
550 if (unformat (line_input, "%d to %d", &queue_start, &queue_end))
554 return clib_error_return (0, "unknown input `%U'",
555 format_unformat_error, line_input);
558 flow.queue_index = queue_start;
559 flow.queue_num = queue_end - queue_start + 1;
561 flow.actions |= VNET_FLOW_ACTION_RSS;
563 else if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
567 return clib_error_return (0, "parse error: '%U'",
568 format_unformat_error, line_input);
571 unformat_free (line_input);
573 if (hw_if_index == ~0 && (action == FLOW_ENABLE || action == FLOW_DISABLE))
574 return clib_error_return (0, "Please specify interface name");
576 if (flow_index == ~0 && (action == FLOW_ENABLE || action == FLOW_DISABLE ||
578 return clib_error_return (0, "Please specify flow index");
583 if (flow.actions == 0)
584 return clib_error_return (0, "Please specify at least one action");
586 /* Adjust the flow type */
589 case FLOW_ETHERNET_CLASS:
590 type = VNET_FLOW_TYPE_ETHERNET;
593 case FLOW_IPV4_CLASS:
596 type = VNET_FLOW_TYPE_IP4_GTPC;
597 protocol.prot = IP_PROTOCOL_UDP;
601 type = VNET_FLOW_TYPE_IP4_GTPU;
602 protocol.prot = IP_PROTOCOL_UDP;
606 type = VNET_FLOW_TYPE_IP4_VXLAN;
607 protocol.prot = IP_PROTOCOL_UDP;
609 else if (l2tpv3oip_set)
610 type = VNET_FLOW_TYPE_IP4_L2TPV3OIP;
611 else if (ipsec_esp_set)
612 type = VNET_FLOW_TYPE_IP4_IPSEC_ESP;
613 else if (ipsec_ah_set)
614 type = VNET_FLOW_TYPE_IP4_IPSEC_AH;
615 else if (tcp_udp_port_set)
616 type = VNET_FLOW_TYPE_IP4_N_TUPLE;
617 else if (inner_ip4_set)
620 type = VNET_FLOW_TYPE_IP4_IP4_N_TUPLE;
622 type = VNET_FLOW_TYPE_IP4_IP4;
623 protocol.prot = IP_PROTOCOL_IP_IN_IP;
625 else if (inner_ip6_set)
628 type = VNET_FLOW_TYPE_IP4_IP6_N_TUPLE;
630 type = VNET_FLOW_TYPE_IP4_IP6;
631 protocol.prot = IP_PROTOCOL_IPV6;
634 type = VNET_FLOW_TYPE_IP4;
636 case FLOW_IPV6_CLASS:
637 if (tcp_udp_port_set)
638 type = VNET_FLOW_TYPE_IP6_N_TUPLE;
640 type = VNET_FLOW_TYPE_IP6_VXLAN;
641 else if (inner_ip4_set)
644 type = VNET_FLOW_TYPE_IP6_IP4_N_TUPLE;
646 type = VNET_FLOW_TYPE_IP6_IP4;
647 protocol.prot = IP_PROTOCOL_IP_IN_IP;
649 else if (inner_ip6_set)
652 type = VNET_FLOW_TYPE_IP6_IP6_N_TUPLE;
654 type = VNET_FLOW_TYPE_IP6_IP6;
655 protocol.prot = IP_PROTOCOL_IPV6;
658 type = VNET_FLOW_TYPE_IP6;
664 type = VNET_FLOW_TYPE_GENERIC;
667 return clib_error_return (0,
668 "Please specify a supported flow type");
671 /* Assign specific field values per flow type */
672 if (flow_class == FLOW_ETHERNET_CLASS)
674 flow.ethernet.eth_hdr.type = eth_type;
676 else if (flow_class == FLOW_IPV4_CLASS)
678 vnet_flow_ip4_t *ip4_ptr = &flow.ip4;
680 clib_memcpy (&ip4_ptr->src_addr, &ip4s,
681 sizeof (ip4_address_and_mask_t));
682 clib_memcpy (&ip4_ptr->dst_addr, &ip4d,
683 sizeof (ip4_address_and_mask_t));
684 ip4_ptr->protocol.prot = protocol.prot;
686 /* In this cli, we use the protocol.mask only when the flow type is
687 * VNET_FLOW_TYPE_IP4/IP6. For other cases, the IP protocol is just
688 * used to identify the next layer type: e.g. UDP/TCP or IPSEC_ESP
690 if (type == VNET_FLOW_TYPE_IP4)
691 ip4_ptr->protocol.mask = protocol.mask;
693 switch (protocol.prot)
696 case IP_PROTOCOL_TCP:
697 case IP_PROTOCOL_UDP:
698 flow.ip4_n_tuple.src_port = sport;
699 flow.ip4_n_tuple.dst_port = dport;
701 if (type == VNET_FLOW_TYPE_IP4_GTPC)
702 flow.ip4_gtpc.teid = teid;
703 else if (type == VNET_FLOW_TYPE_IP4_GTPU)
704 flow.ip4_gtpu.teid = teid;
705 else if (type == VNET_FLOW_TYPE_IP4_VXLAN)
706 flow.ip4_vxlan.vni = vni;
708 case IP_PROTOCOL_L2TP:
709 flow.ip4_l2tpv3oip.session_id = session_id;
711 case IP_PROTOCOL_IPSEC_ESP:
712 flow.ip4_ipsec_esp.spi = spi;
714 case IP_PROTOCOL_IPSEC_AH:
715 flow.ip4_ipsec_esp.spi = spi;
717 case IP_PROTOCOL_IP_IN_IP:
718 clib_memcpy (&flow.ip4_ip4.in_src_addr, &in_ip4s,
719 sizeof (ip4_address_and_mask_t));
720 clib_memcpy (&flow.ip4_ip4.in_dst_addr, &in_ip4d,
721 sizeof (ip4_address_and_mask_t));
722 if (type == VNET_FLOW_TYPE_IP4_IP4_N_TUPLE)
724 flow.ip4_ip4.in_protocol.prot = in_proto.prot;
725 flow.ip4_ip4_n_tuple.in_src_port = in_sport;
726 flow.ip4_ip4_n_tuple.in_dst_port = in_dport;
729 case IP_PROTOCOL_IPV6:
730 clib_memcpy (&flow.ip4_ip6.in_src_addr, &in_ip6s,
731 sizeof (ip6_address_and_mask_t));
732 clib_memcpy (&flow.ip4_ip6.in_dst_addr, &in_ip6d,
733 sizeof (ip6_address_and_mask_t));
734 if (type == VNET_FLOW_TYPE_IP4_IP6_N_TUPLE)
736 flow.ip4_ip6.in_protocol.prot = in_proto.prot;
737 flow.ip4_ip6_n_tuple.in_src_port = in_sport;
738 flow.ip4_ip6_n_tuple.in_dst_port = in_dport;
745 else if (flow_class == FLOW_IPV6_CLASS)
747 vnet_flow_ip6_t *ip6_ptr = &flow.ip6;
749 clib_memcpy (&flow.ip6_n_tuple.src_addr, &ip6s,
750 sizeof (ip6_address_and_mask_t));
751 clib_memcpy (&flow.ip6_n_tuple.dst_addr, &ip6d,
752 sizeof (ip6_address_and_mask_t));
754 ip6_ptr->protocol.prot = protocol.prot;
756 /* In this cli, we use the protocol.mask only when the flow type is
757 * VNET_FLOW_TYPE_IP4/IP6. For other cases, the IP protocol is just
758 * used to identify the next layer type: e.g. UDP/TCP or IPSEC_ESP
760 if (type == VNET_FLOW_TYPE_IP6)
761 ip6_ptr->protocol.mask = protocol.mask;
763 switch (protocol.prot)
766 case IP_PROTOCOL_TCP:
767 case IP_PROTOCOL_UDP:
768 flow.ip6_n_tuple.src_port = sport;
769 flow.ip6_n_tuple.dst_port = dport;
771 if (type == VNET_FLOW_TYPE_IP6_VXLAN)
772 flow.ip6_vxlan.vni = vni;
774 case IP_PROTOCOL_IP_IN_IP:
775 clib_memcpy (&flow.ip6_ip4.in_src_addr, &in_ip4s,
776 sizeof (ip4_address_and_mask_t));
777 clib_memcpy (&flow.ip6_ip4.in_dst_addr, &in_ip4d,
778 sizeof (ip4_address_and_mask_t));
779 if (type == VNET_FLOW_TYPE_IP6_IP4_N_TUPLE)
781 flow.ip6_ip4.in_protocol.prot = in_proto.prot;
782 flow.ip6_ip4_n_tuple.in_src_port = in_sport;
783 flow.ip6_ip4_n_tuple.in_dst_port = in_dport;
786 case IP_PROTOCOL_IPV6:
787 clib_memcpy (&flow.ip6_ip6.in_src_addr, &in_ip6s,
788 sizeof (ip6_address_and_mask_t));
789 clib_memcpy (&flow.ip6_ip6.in_dst_addr, &in_ip6d,
790 sizeof (ip6_address_and_mask_t));
791 if (type == VNET_FLOW_TYPE_IP6_IP6_N_TUPLE)
793 flow.ip6_ip6.in_protocol.prot = in_proto.prot;
794 flow.ip6_ip6_n_tuple.in_src_port = in_sport;
795 flow.ip6_ip6_n_tuple.in_dst_port = in_dport;
802 if (type == VNET_FLOW_TYPE_GENERIC)
804 clib_memcpy (flow.generic.pattern.spec, spec,
805 sizeof (flow.generic.pattern.spec));
806 clib_memcpy (flow.generic.pattern.mask, mask,
807 sizeof (flow.generic.pattern.mask));
811 rv = vnet_flow_add (vnm, &flow, &flow_index);
813 vlib_cli_output (vm, "flow %u added", flow_index);
817 rv = vnet_flow_del (vnm, flow_index);
820 rv = vnet_flow_enable (vnm, flow_index, hw_if_index);
823 rv = vnet_flow_disable (vnm, flow_index, hw_if_index);
826 return clib_error_return (0, "please specify action (add, del, enable,"
831 return clib_error_return (0, "flow error: %U", format_flow_error, rv);
837 VLIB_CLI_COMMAND (test_flow_command, static) = {
839 .short_help = "test flow [add|del|enable|disable] [index <id>] "
840 "[src-ip <ip-addr/mask>] [dst-ip <ip-addr/mask>] "
841 "[ip6-src-ip <ip-addr/mask>] [ip6-dst-ip <ip-addr/mask>] "
842 "[src-port <port/mask>] [dst-port <port/mask>] "
843 "[proto <ip-proto>] "
844 "[gtpc teid <teid>] [gtpu teid <teid>] [vxlan <vni>] "
845 "[session id <session>] [spi <spi>]"
846 "[spec <spec string>] [mask <mask string>]"
847 "[next-node <node>] [mark <id>] [buffer-advance <len>] "
848 "[redirect-to-queue <queue>] [drop] "
849 "[rss function <name>] [rss types <flow type>]"
850 "[rss queues <queue_start> to <queue_end>]",
851 .function = test_flow,
856 format_flow_match_element (u8 * s, va_list * args)
858 char *type = va_arg (*args, char *);
859 void *ptr = va_arg (*args, void *);
861 if (strncmp (type, "u8", 2) == 0)
862 return format (s, "%d", *(u8 *) ptr);
864 if (strncmp (type, "u16", 3) == 0)
865 return format (s, "%d", *(u16 *) ptr);
867 if (strncmp (type, "u32", 3) == 0)
868 return format (s, "%d", *(u32 *) ptr);
870 if (strncmp (type, "ethernet_header_t", 13) == 0)
872 ethernet_max_header_t m;
873 memset (&m, 0, sizeof (m));
874 m.ethernet = *(ethernet_header_t *) ptr;
875 /* convert the ethernet type to net order */
876 m.ethernet.type = clib_host_to_net_u16 (m.ethernet.type);
877 return format (s, "%U", format_ethernet_header, &m);
880 if (strncmp (type, "ip4_address_t", 13) == 0)
881 return format (s, "%U", format_ip4_address, ptr);
883 if (strncmp (type, "ip4_address_and_mask_t", 13) == 0)
884 return format (s, "%U", format_ip4_address_and_mask, ptr);
886 if (strncmp (type, "ip6_address_t", 13) == 0)
887 return format (s, "%U", format_ip6_address, ptr);
889 if (strncmp (type, "ip6_address_and_mask_t", 13) == 0)
890 return format (s, "%U", format_ip6_address_and_mask, ptr);
892 if (strncmp (type, "ip_prot_and_mask_t", 13) == 0)
893 return format (s, "%U", format_ip_protocol_and_mask, ptr);
895 if (strncmp (type, "ip_port_and_mask_t", 18) == 0)
896 return format (s, "%U", format_ip_port_and_mask, ptr);
898 s = format (s, "unknown type '%s'", type);
902 #define _fe(a,b) s2 = format (s2, "%s%s %U", s2 ? ", ":"", #b, \
903 format_flow_match_element, #a, &f->b);
905 u8 * format_flow_match_##b (u8 * s, va_list * args) \
907 vnet_flow_##b##_t *f = __builtin_va_arg (*args, vnet_flow_##b##_t *); \
909 foreach_flow_entry_##b \
910 s = format (s, "%v", s2);; \
918 format_flow_match (u8 * s, va_list * args)
920 vnet_flow_t *f = va_arg (*args, vnet_flow_t *);
923 if (f->type == VNET_FLOW_TYPE_##a) \
924 return format (s, "%U", format_flow_match_##b, &f->b);
932 format_flow (u8 * s, va_list * args)
934 vlib_main_t *vm = vlib_get_main ();
935 vnet_flow_t *f = va_arg (*args, vnet_flow_t *);
936 u32 indent = format_get_indent (s);
939 s = format (s, "flow-index %u type %s active %u",
940 f->index, flow_type_strings[f->type],
941 hash_elts (f->private_data)),
942 s = format (s, "\n%Umatch: %U", format_white_space, indent + 2,
943 format_flow_match, f);
944 s = format (s, "\n%Uaction: %U", format_white_space, indent + 2,
945 format_flow_actions, f->actions);
947 if (f->actions & VNET_FLOW_ACTION_DROP)
948 t = format (t, "%sdrop", t ? ", " : "");
950 if (f->actions & VNET_FLOW_ACTION_MARK)
951 t = format (t, "%smark %u", t ? ", " : "", f->mark_flow_id);
953 if (f->actions & VNET_FLOW_ACTION_REDIRECT_TO_QUEUE)
955 format (t, "%sredirect-to-queue %u", t ? ", " : "", f->redirect_queue);
957 if (f->actions & VNET_FLOW_ACTION_REDIRECT_TO_NODE)
958 t = format (t, "%snext-node %U", t ? ", " : "",
959 format_vlib_node_name, vm, f->redirect_node_index);
961 if (f->actions & VNET_FLOW_ACTION_BUFFER_ADVANCE)
962 t = format (t, "%sbuffer-advance %d", t ? ", " : "", f->buffer_advance);
964 if (f->actions & VNET_FLOW_ACTION_RSS)
966 t = format (t, "%srss function %U", t ? ", " : "",
967 format_rss_function, f->rss_fun);
968 t = format (t, "%srss types %U", t ? ", " : "",
969 format_rss_types, f->rss_types);
974 s = format (s, "\n%U%v", format_white_space, indent + 4, t);
982 * fd.io coding-style-patch-verification: ON
985 * eval: (c-set-style "gnu")