examples/flow_filtering: add re-route to kernel snippet
PATCH 1/1 examples/flow_filtering: add re-route to kernel snippet
The snippet enables direct packet re-routing to kernel by using the new RTE_FLOW_ACTION_TYPE_SEND_TO_KERNEL action.
Added support for snippet-specific configurations:
* port configuration: set promiscuous mode / flow isolation mode
* Flow configuration: Setup beyond basic ingress flows
Signed-off-by Shani Peretz <[email protected]>
--- doc/guides/sample_app_ug/flow_filtering.rst | 19 +++- examples/flow_filtering/common.h | 12 ++- examples/flow_filtering/flow_skeleton.c | 28 ++--- examples/flow_filtering/main.c | 31 ++++-- examples/flow_filtering/meson.build | 1 + .../snippets/snippet_match_gre.c | 16 +++ .../snippets/snippet_match_gre.h | 17 ++- .../snippets/snippet_match_ipv4.c | 24 +++-- .../snippets/snippet_match_ipv4.h | 12 ++- .../snippets/snippet_match_mpls.c | 7 ++ .../snippets/snippet_match_mpls.h | 14 ++- .../snippets/snippet_re_route_to_kernel.c | 100 ++++++++++++++++++ .../snippets/snippet_re_route_to_kernel.h | 34 ++++++ 13 files changed, 264 insertions(+), 51 deletions(-) create mode 100644 examples/flow_filtering/snippets/snippet_re_route_to_kernel.c create mode 100644 examples/flow_filtering/snippets/snippet_re_route_to_kernel.h
doc/guides/sample_app_ug/flow_filtering.rst +17 -2
@@ -78,6 +78,15 @@ Allocate a memory pool for managing mbufs used within the application: :end-before: >8 End of allocating a mempool to hold the mbufs. :dedent: 1 +Some snippets may require different configuration of the port and flow attributes, +those configuration are defined in the snippet file. + +.. literalinclude:: ../../../examples/flow_filtering/main.c + :language: c + :start-after: Add snippet-specific configuration. 8< + :end-before: >8 End of snippet-specific configuration. + :dedent: 1 + Initialize the ports using the user-defined ``init_port()`` function, configuring Ethernet ports with default settings, including both Rx and Tx queues for a single port:
@@ -183,5 +192,11 @@ For example, within ``snippet_match_ipv4_flow.c``, developers can find the funct - ``snippet_ipv4_flow_create_patterns()`` for setting packet matching patterns, - ``snippet_ipv4_flow_create_table()`` for creating the patterns and actions template table. -These functions can simply be called in the appropriate place -in ``flow_skeleton.c`` to change the default created flow. +To use a different snippet, simply update the include statement in "flow_skeleton.c" to point to +the desired snippet file, this will change the default created flow. + +some snippets may require different configuration, those configuration are defined in the snippet file: + +- ``snippet_init_ipv4`` for configuration of the port and flow attributes. + +In order to use them the developer should include the snippet header file in main.c
examples/flow_filtering/common.h +9 -3
@@ -5,10 +5,16 @@#ifndef COMMON_H #define COMMON_H -#define QUEUE_ID 1 +extern bool enable_promiscuous_mode; +extern bool enable_flow_isolation; +extern struct rte_flow_attr flow_attr; -extern struct rte_flow_attr attr; -extern struct rte_flow_op_attr ops_attr; +static inline void init_default_snippet (void ) +{ + enable_promiscuous_mode = true; + enable_flow_isolation = false; + flow_attr.ingress =1 ; +}/** * Skeleton for creation of a flow rule using template and non template API.
examples/flow_filtering/flow_skeleton.c +9 -19
@@ -10,12 +10,11 @@#include "common.h" #include "snippets/snippet_match_ipv4.h" - -struct rte_flow_attr attr = { .ingress =1 }; +struct rte_flow_attr flow_attr;struct rte_flow_op_attr ops_attr = { .postpone =0 };static struct rte_flow * -create_flow_non_template (uint16_t port_id,struct rte_flow_attr *attr, +create_flow_non_template (uint16_t port_id,struct rte_flow_attr *flow_attr,struct rte_flow_item *patterns,struct rte_flow_action *actions,struct rte_flow_error *error)
@@ -23,8 +22,8 @@ create_flow_non_template(uint16_t port_id, struct rte_flow_attr *attr,struct rte_flow *flow =NULL ;/* Validate the rule and create it. */ -if (rte_flow_validate (port_id, attr, patterns, actions, error) ==0 ) - flow =rte_flow_create (port_id, attr, patterns, actions, error); +if (rte_flow_validate (port_id, flow_attr, patterns, actions, error) ==0 ) + flow =rte_flow_create (port_id, flow_attr, patterns, actions, error);return flow; }
@@ -34,10 +33,7 @@ create_flow_template(uint16_t port_id, struct rte_flow_op_attr *ops_attr,struct rte_flow_action *actions, struct rte_flow_error *error) { -/* Replace this function call with -* snippet_*_create_table() function from the snippets directory. -*/ -struct rte_flow_template_table *table =snippet_ipv4_flow_create_table (port_id, error); + struct rte_flow_template_table *table = snippet_skeleton_flow_create_table(port_id, error);if (table ==NULL ) {printf ("Failed to create table: %s (%s)\n" , error->message,rte_strerror (rte_errno));
@@ -45,7 +41,7 @@ create_flow_template(uint16_t port_id, struct rte_flow_op_attr *ops_attr, } return rte_flow_async_create(port_id, - QUEUE_ID, /* Flow queue used to insert the rule. */ + 1, /* Flow queue used to insert the rule. */ ops_attr, table, patterns,
@@ -63,15 +59,9 @@ generate_flow_skeleton(uint16_t port_id, struct rte_flow_error *error, int use_tstruct rte_flow_action actions[MAX_ACTION_NUM ] = {0 };struct rte_flow_item patterns[MAX_PATTERN_NUM ] = {0 }; -/* Replace this function call with -* snippet_*_create_actions() function from the snippets directory -*/ -snippet_ipv4_flow_create_actions (actions); +snippet_skeleton_flow_create_actions (actions); -/* Replace this function call with -* snippet_*_create_patterns() function from the snippets directory -*/ -snippet_ipv4_flow_create_patterns (patterns); +snippet_skeleton_flow_create_patterns (patterns);/* >8 End of setting the common action and pattern structures. */ /* Create a flow rule using template API 8< */
@@ -80,6 +70,6 @@ generate_flow_skeleton(uint16_t port_id, struct rte_flow_error *error, int use_t/* >8 End of creating a flow rule using template API. */ /* Validate and create the rule 8< */ -return create_flow_non_template (port_id, &attr, patterns, actions, error); +return create_flow_non_template (port_id, &flow_attr, patterns, actions, error);/* >8 End of validating and creating the rule. */ }
examples/flow_filtering/main.c +25 -6
@@ -30,6 +30,10 @@#include <rte_argparse.h> #include "common.h" +#include "snippets/snippet_match_ipv4.h" + +bool enable_promiscuous_mode = true;/* default configuration of promiscuous mode */ +bool enable_flow_isolation;/* some snippet may need isolation mode instead of promiscuous mode */ /* Template API enabled by default. */ static int use_template_api =1 ;
@@ -212,12 +216,23 @@ init_port(void) } } -/* Setting the RX port to promiscuous mode. */ - ret =rte_eth_promiscuous_enable (port_id); -if (ret !=0 ) -rte_exit (EXIT_FAILURE , -":: promiscuous mode enable failed: err=%s, port=%u\n" , -rte_strerror (-ret), port_id); +if (enable_promiscuous_mode) { +/* Setting the RX port to promiscuous mode. */ + ret =rte_eth_promiscuous_enable (port_id); +printf (":: promiscuous mode enabled\n" ); +if (ret !=0 ) +rte_exit (EXIT_FAILURE , +":: promiscuous mode enable failed: err=%s, port=%u\n" , +rte_strerror (-ret), port_id); + }else if (enable_flow_isolation) { +/* Setting the RX port to isolate mode. */ + ret =rte_flow_isolate (port_id,1 ,NULL ); +printf (":: isolate mode enabled\n" ); +if (ret !=0 ) +rte_exit (EXIT_FAILURE , +":: isolate mode enable failed: err=%s, port=%u\n" , +rte_strerror (-ret), port_id); + } ret =rte_eth_dev_start (port_id);if (ret <0 ) {
@@ -328,6 +343,10 @@ main(int argc, char **argv)if (mbuf_pool ==NULL )rte_exit (EXIT_FAILURE ,"Cannot init mbuf pool\n" ); +/* Add snippet-specific configuration. 8< */ +snippet_init (); +/* >8 End of snippet-specific configuration. */ +/* Initializes all the ports using the user defined init_port(). 8< */ init_port ();/* >8 End of Initializing the ports using user defined init_port(). */
examples/flow_filtering/meson.build +1 -0
@@ -15,6 +15,7 @@ sources = files( 'snippets/snippet_match_ipv4.c', 'snippets/snippet_match_gre.c', 'snippets/snippet_match_mpls.c', + 'snippets/snippet_re_route_to_kernel.c', ) # The code snippets are not utilized.
examples/flow_filtering/snippets/snippet_match_gre.c +16 -0
@@ -5,8 +5,16 @@#include <stdlib.h> #include <rte_flow.h> +#include "common.h" +#include "rte_common.h" #include "snippet_match_gre.h" +void +snippet_init_gre (void ) +{ +init_default_snippet (); +} +static void snippet_match_gre_create_actions (struct rte_flow_action *action) {
@@ -52,3 +60,11 @@ snippet_match_gre_create_patterns(struct rte_flow_item *pattern) pattern[3 ].mask = gre_opt_spec; pattern[4 ].type =RTE_FLOW_ITEM_TYPE_END ; } + +static struct rte_flow_template_table * +snippet_gre_flow_create_table ( + __rte_unused uint16_t port_id, + __rte_unused struct rte_flow_error *error) +{ +return NULL ; +}
examples/flow_filtering/snippets/snippet_match_gre.h +13 -4
@@ -11,15 +11,24 @@* c_bit/k_bit/s_bit in the GRE pattern. */ -#define MAX_PATTERN_NUM 5/* Maximal number of patterns for this example. */ -#define MAX_ACTION_NUM 2/* Maximal number of actions for this example. */ +#define MAX_PATTERN_NUM 5/* Maximal number of patterns for this example. */ +#define MAX_ACTION_NUM 2/* Maximal number of actions for this example. */ + +static void +snippet_init_gre (void ); +#define snippet_init snippet_init_gre -/* Replace this function with the snippet_*_create_actions() function in flow_skeleton.c. */ static void snippet_match_gre_create_actions (struct rte_flow_action *action); +#define snippet_skeleton_flow_create_actions snippet_match_gre_create_actions -/* Replace this function with the snippet_*_create_patterns() function in flow_skeleton.c. */ static void snippet_match_gre_create_patterns (struct rte_flow_item *pattern); +#define snippet_skeleton_flow_create_patterns snippet_match_gre_create_patterns + +static struct rte_flow_template_table * +snippet_gre_flow_create_table (uint16_t port_id,struct rte_flow_error *error); +#define snippet_skeleton_flow_create_table snippet_gre_flow_create_table +#endif /* SNIPPET_MATCH_GRE_H */
examples/flow_filtering/snippets/snippet_match_ipv4.c +17 -7
@@ -10,6 +10,12 @@#include "snippet_match_ipv4.h" #include "../common.h" +void +snippet_init_ipv4 (void ) +{ +init_default_snippet (); +} +void snippet_ipv4_flow_create_actions (struct rte_flow_action *action) {
@@ -20,7 +26,7 @@ snippet_ipv4_flow_create_actions(struct rte_flow_action *action)struct rte_flow_action_queue *queue =calloc (1 ,sizeof (struct rte_flow_action_queue ));if (queue ==NULL )fprintf (stderr,"Failed to allocate memory for queue\n" ); - queue->index =QUEUE_ID ;/* The selected target queue.*/ + queue->index =1 ;/* The selected target queue.*/ action[0 ].type =RTE_FLOW_ACTION_TYPE_QUEUE ; action[0 ].conf = queue; action[1 ].type =RTE_FLOW_ACTION_TYPE_END ;
@@ -54,10 +60,14 @@ snippet_ipv4_flow_create_patterns(struct rte_flow_item *patterns)if (ip_mask ==NULL )fprintf (stderr,"Failed to allocate memory for ip_mask\n" ); - ip_spec->hdr.dst_addr =htonl (DEST_IP );/* The dest ip value to match the input packet. */ - ip_mask->hdr.dst_addr =FULL_MASK ;/* The mask to apply to the dest ip. */ - ip_spec->hdr.src_addr =htonl (SRC_IP );/* The src ip value to match the input packet. */ - ip_mask->hdr.src_addr =EMPTY_MASK ;/* The mask to apply to the src ip. */ +/* Match destination IP 192.168.1.1 with full mask */ + ip_spec->hdr.dst_addr =htonl (((192 <<24 ) + (168 <<16 ) + (1 <<8 ) +1 )); + ip_mask->hdr.dst_addr =0xffffffff ; + +/* Match any source IP by using empty mask */ + ip_spec->hdr.src_addr =htonl (((0 <<24 ) + (0 <<16 ) + (0 <<8 ) +0 )); + ip_mask->hdr.src_addr =0x0 ; + patterns[1 ].spec = ip_spec; patterns[1 ].mask = ip_mask;
@@ -102,8 +112,8 @@ snippet_ipv4_flow_create_pattern_template(uint16_t port_id, struct rte_flow_erro titems[0 ].type =RTE_FLOW_ITEM_TYPE_ETH ; titems[1 ].type =RTE_FLOW_ITEM_TYPE_IPV4 ; - ip_mask.hdr.src_addr =EMPTY_MASK ; - ip_mask.hdr.dst_addr =FULL_MASK ; + ip_mask.hdr.src_addr =0x0 ;/* empty mask */ + ip_mask.hdr.dst_addr =0xffffffff ;/* full mask */ titems[1 ].mask = &ip_mask; titems[2 ].type =RTE_FLOW_ITEM_TYPE_END ;
examples/flow_filtering/snippets/snippet_match_ipv4.h +7 -5
@@ -9,22 +9,24 @@* sends packets with matching src and dest ip to selected queue. */ -#define SRC_IP ((0<<24) + (0<<16) + (0<<8) + 0)/* src ip = 0.0.0.0 */ -#define DEST_IP ((192<<24) + (168<<16) + (1<<8) + 1)/* dest ip = 192.168.1.1 */ -#define FULL_MASK 0xffffffff/* full mask */ -#define EMPTY_MASK 0x0/* empty mask */ -#define MAX_PATTERN_NUM 3/* Maximal number of patterns for this example. */ #define MAX_ACTION_NUM 2/* Maximal number of actions for this example. */ +void +snippet_init_ipv4 (void ); +#define snippet_init snippet_init_ipv4 +void snippet_ipv4_flow_create_actions (struct rte_flow_action *action); +#define snippet_skeleton_flow_create_actions snippet_ipv4_flow_create_actionsvoid snippet_ipv4_flow_create_patterns (struct rte_flow_item *patterns); +#define snippet_skeleton_flow_create_patterns snippet_ipv4_flow_create_patternsstruct rte_flow_template_table *snippet_ipv4_flow_create_table (uint16_t port_id,struct rte_flow_error *error); +#define snippet_skeleton_flow_create_table snippet_ipv4_flow_create_table#endif /* SNIPPET_MATCH_IPV4_H */
examples/flow_filtering/snippets/snippet_match_mpls.c +7 -0
@@ -11,6 +11,13 @@#include "../common.h" #include "snippet_match_mpls.h" + +static void +snippet_init_mpls (void ) +{ +init_default_snippet (); +} +static void snippet_mpls_create_actions (struct rte_flow_action *actions) {
examples/flow_filtering/snippets/snippet_match_mpls.h +9 -5
@@ -11,19 +11,23 @@* The maximum supported MPLS headers is 5. */ -#define MAX_PATTERN_NUM 6/* Maximal number of patterns for this example. */ -#define MAX_ACTION_NUM 2/* Maximal number of actions for this example. */ +#define MAX_PATTERN_NUM 6/* Maximal number of patterns for this example. */ +#define MAX_ACTION_NUM 2/* Maximal number of actions for this example. */ + +static void +snippet_init_mpls (void ); +#define snippet_init snippet_init_mpls -/* Replace this function with the snippet_*_create_actions() function in flow_skeleton.c. */ static void snippet_mpls_create_actions (struct rte_flow_action *actions); +#define snippet_skeleton_flow_create_actions snippet_mpls_create_actions -/* Replace this function with the snippet_*_create_patterns() function in flow_skeleton.c. */ static void snippet_mpls_create_patterns (struct rte_flow_item *pattern); +#define snippet_skeleton_flow_create_patterns snippet_mpls_create_patterns -/* Replace this function with the snippet_*_create_table() function in flow_skeleton.c. */ static struct rte_flow_template_table *snippet_mpls_create_table (uint16_t port_id,struct rte_flow_error *error); +#define snippet_skeleton_flow_create_table snippet_mpls_create_table#endif /* SNIPPET_MATCH_MPLS_H */
examples/flow_filtering/snippets/snippet_re_route_to_kernel.c new file +100 -0
100 lines not on this page · raw message
examples/flow_filtering/snippets/snippet_re_route_to_kernel.h new file +34 -0
@@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright (c) 2022 NVIDIA Corporation & Affiliates +*/ + +#ifndef SNIPPET_REROUTE_TO_KERNEL_H +#define SNIPPET_REROUTE_TO_KERNEL_H + +/* Packet Re-Routing to the Kernel +* This feature introduces a new rte_flow action (RTE_FLOW_ACTION_TYPE_SEND_TO_KERNEL) +* which allows application to re-route packets directly to the kernel without software involvement. +* The packets will be received by the kernel sharing the same device as the DPDK port +* on which this action is configured. +*/ + +#define MAX_PATTERN_NUM 2/* Maximal number of patterns for this example. */ +#define MAX_ACTION_NUM 2/* Maximal number of actions for this example. */ + +static void +snippet_init_re_route_to_kernel (void ); +#define snippet_init snippet_init_re_route_to_kernel + +static void +snippet_re_route_to_kernel_create_actions (struct rte_flow_action *action); +#define snippet_skeleton_flow_create_actions snippet_re_route_to_kernel_create_actions + +static void +snippet_re_route_to_kernel_create_patterns (struct rte_flow_item *pattern); +#define snippet_skeleton_flow_create_patterns snippet_re_route_to_kernel_create_patterns + +static struct rte_flow_template_table * +snippet_re_route_to_kernel_create_table (uint16_t port_id,struct rte_flow_error *error); +#define snippet_skeleton_flow_create_table snippet_re_route_to_kernel_create_table + +#endif /* SNIPPET_REROUTE_TO_KERNEL_H */
Signature · 1 line
2.34.1
Thomas Monjalon
Applied21/05/2025 09:27, Shani Peretz:
Quoted 8 lines
The snippet enables direct packet re-routing to kernel by using the new
RTE_FLOW_ACTION_TYPE_SEND_TO_KERNELaction.Added support for snippet-specific configurations:
* port configuration: set promiscuous mode / flow isolation mode
* Flow configuration: Setup beyond basic ingress flowsSigned-off-by Shani Peretz <[email protected]>
Applied, thanks.