DPDK patches and discussions
 help / color / mirror / Atom feed
* [PATCH] net/af_xdp: enable AF_XDP program attachment mode
@ 2025-07-23  7:42 Shivaji Kant
  2025-07-23 12:30 ` Ivan Malov
  2025-07-29  8:24 ` [PATCH v2] " Shivaji Kant
  0 siblings, 2 replies; 8+ messages in thread
From: Shivaji Kant @ 2025-07-23  7:42 UTC (permalink / raw)
  To: Ciara Loftus, Maryam Tahhan; +Cc: dev, Joshua Washington, Shivaji Kant

Currently, the AF_XDP PMD attaches its XDP program using a fixed
set of flags,defaulting to `XDP_FLAGS_UPDATE_IF_NOEXIST`. This lacks
flexibility for users who might need to explicitly control the XDP
mode based on their specific hardware capabilities or performance
requirements.

This patch introduces a new vdev argument, `mode`, for the `net_af_xdp`
PMD. This argument allows users to explicitly specify the desired
XDP program attachment mode:
`drv` (XDP_FLAGS_DRV_MODE),
`skb` (XDP_FLAGS_SKB_MODE),
`hw` (XDP_FLAGS_HW_MODE).
(If needed more can be added)

This change provides greater control and flexibility for users to
fine-tune AF_XDP behavior, enabling them to leverage native driver
or hardware offload capabilities when available, or fall back to
generic SKB mode for broader compatibility.

Signed-off-by: Shivaji Kant <shivajikant@google.com>
---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 61 +++++++++++++++++++++++++----
 1 file changed, 54 insertions(+), 7 deletions(-)

diff --git a/drivers/net/af_xdp/rte_eth_af_xdp.c b/drivers/net/af_xdp/rte_eth_af_xdp.c
index 5f65850a27..e39f2b4935 100644
--- a/drivers/net/af_xdp/rte_eth_af_xdp.c
+++ b/drivers/net/af_xdp/rte_eth_af_xdp.c
@@ -104,6 +104,8 @@ RTE_LOG_REGISTER_DEFAULT(af_xdp_logtype, NOTICE);
 
 static int afxdp_dev_count;
 
+static __u32 mode_flag = XDP_FLAGS_UPDATE_IF_NOEXIST;
+
 /* Message header to synchronize fds via IPC */
 struct ipc_hdr {
 	char port_name[RTE_DEV_NAME_MAX_LEN];
@@ -198,6 +200,26 @@ struct pmd_process_private {
 #define ETH_AF_XDP_USE_CNI_ARG			"use_cni"
 #define ETH_AF_XDP_USE_PINNED_MAP_ARG	"use_pinned_map"
 #define ETH_AF_XDP_DP_PATH_ARG			"dp_path"
+#define ETH_AF_XDP_MODE_ARG				"mode"
+
+/* Define different modes for af_xdp prog to attach */
+#define ETH_AF_XDP_DRV_MODE_ARG			"drv"
+#define ETH_AF_XDP_SKB_MODE_ARG			"skb"
+#define ETH_AF_XDP_HW_MODE_ARG			"hw"
+#define ETH_AF_XDP_NUM_MODE_ARG			3
+
+static const char * const mode_arguments[] = {
+	ETH_AF_XDP_DRV_MODE_ARG,
+	ETH_AF_XDP_SKB_MODE_ARG,
+	ETH_AF_XDP_HW_MODE_ARG,
+	NULL
+};
+
+static const unsigned int mode_flags[] = {
+	XDP_FLAGS_DRV_MODE,
+	XDP_FLAGS_SKB_MODE,
+	XDP_FLAGS_HW_MODE
+};
 
 static const char * const valid_arguments[] = {
 	ETH_AF_XDP_IFACE_ARG,
@@ -210,6 +232,7 @@ static const char * const valid_arguments[] = {
 	ETH_AF_XDP_USE_CNI_ARG,
 	ETH_AF_XDP_USE_PINNED_MAP_ARG,
 	ETH_AF_XDP_DP_PATH_ARG,
+	ETH_AF_XDP_MODE_ARG,
 	NULL
 };
 
@@ -950,14 +973,14 @@ remove_xdp_program(struct pmd_internals *internals)
 	uint32_t curr_prog_id = 0;
 	int ret;
 
-	ret = bpf_xdp_query_id(internals->if_index, XDP_FLAGS_UPDATE_IF_NOEXIST,
+	ret = bpf_xdp_query_id(internals->if_index, mode_flag,
 			       &curr_prog_id);
 	if (ret != 0) {
 		AF_XDP_LOG_LINE(ERR, "bpf_xdp_query_id failed");
 		return ret;
 	}
 
-	ret = bpf_xdp_detach(internals->if_index, XDP_FLAGS_UPDATE_IF_NOEXIST,
+	ret = bpf_xdp_detach(internals->if_index, mode_flag,
 			     NULL);
 	if (ret != 0)
 		AF_XDP_LOG_LINE(ERR, "bpf_xdp_detach failed");
@@ -978,14 +1001,14 @@ remove_xdp_program(struct pmd_internals *internals)
 	int ret;
 
 	ret = bpf_get_link_xdp_id(internals->if_index, &curr_prog_id,
-				  XDP_FLAGS_UPDATE_IF_NOEXIST);
+				  mode_flag);
 	if (ret != 0) {
 		AF_XDP_LOG_LINE(ERR, "bpf_get_link_xdp_id failed");
 		return ret;
 	}
 
 	ret = bpf_set_link_xdp_fd(internals->if_index, -1,
-				  XDP_FLAGS_UPDATE_IF_NOEXIST);
+				  mode_flag);
 	if (ret != 0)
 		AF_XDP_LOG_LINE(ERR, "bpf_set_link_xdp_fd failed");
 	return ret;
@@ -1328,7 +1351,7 @@ load_custom_xdp_prog(const char *prog_path, int if_index, struct bpf_map **map)
 
 	/* Link the program with the given network device */
 	ret = link_xdp_prog_with_dev(if_index, prog_fd,
-					XDP_FLAGS_UPDATE_IF_NOEXIST);
+					mode_flag);
 	if (ret) {
 		AF_XDP_LOG_LINE(ERR, "Failed to set prog fd %d on interface",
 				prog_fd);
@@ -1679,7 +1702,7 @@ xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
 	cfg.rx_size = ring_size;
 	cfg.tx_size = ring_size;
 	cfg.libbpf_flags = 0;
-	cfg.xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
+	cfg.xdp_flags = mode_flag;
 	cfg.bind_flags = 0;
 
 	/* Force AF_XDP socket into copy mode when users want it */
@@ -2017,6 +2040,25 @@ parse_name_arg(const char *key __rte_unused,
 	return 0;
 }
 
+/** parse name argument */
+static int
+parse_mode_arg(const char *key __rte_unused,
+	       const char *value, void *extra_args)
+{
+	unsigned int *mode = extra_args;
+	unsigned int i;
+
+	for (i = 0; i < ETH_AF_XDP_NUM_MODE_ARG; i++) {
+		if (strcmp(value, mode_arguments[i]) == 0) {
+			*mode |= mode_flags[i];
+			return 0;
+		}
+	}
+
+	AF_XDP_LOG_LINE(ERR, "Invalid af_xdp mode, choose correct mode to attach af_xdp program.");
+	return -EINVAL;
+}
+
 /** parse xdp prog argument */
 static int
 parse_prog_arg(const char *key __rte_unused,
@@ -2147,6 +2189,10 @@ parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
 
 	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_DP_PATH_ARG,
 				 &parse_prog_arg, dp_path);
+
+	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_MODE_ARG,
+				 &parse_mode_arg, &mode_flag);
+
 	if (ret < 0)
 		goto free_kvlist;
 
@@ -2587,4 +2633,5 @@ RTE_PMD_REGISTER_PARAM_STRING(net_af_xdp,
 			      "force_copy=<int> "
 			      "use_cni=<int> "
 			      "use_pinned_map=<int> "
-			      "dp_path=<string> ");
+			      "dp_path=<string> "
+			      "mode=<string> ");
-- 
2.50.0.727.gbf7dc18ff4-goog


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] net/af_xdp: enable AF_XDP program attachment mode
  2025-07-23  7:42 [PATCH] net/af_xdp: enable AF_XDP program attachment mode Shivaji Kant
@ 2025-07-23 12:30 ` Ivan Malov
  2025-07-29  8:24 ` [PATCH v2] " Shivaji Kant
  1 sibling, 0 replies; 8+ messages in thread
From: Ivan Malov @ 2025-07-23 12:30 UTC (permalink / raw)
  To: Shivaji Kant; +Cc: Ciara Loftus, Maryam Tahhan, dev, Joshua Washington

Hi Shivaji,

(please see below)

On Wed, 23 Jul 2025, Shivaji Kant wrote:

> Currently, the AF_XDP PMD attaches its XDP program using a fixed
> set of flags,defaulting to `XDP_FLAGS_UPDATE_IF_NOEXIST`. This lacks
> flexibility for users who might need to explicitly control the XDP
> mode based on their specific hardware capabilities or performance
> requirements.
>
> This patch introduces a new vdev argument, `mode`, for the `net_af_xdp`
> PMD. This argument allows users to explicitly specify the desired
> XDP program attachment mode:
> `drv` (XDP_FLAGS_DRV_MODE),
> `skb` (XDP_FLAGS_SKB_MODE),
> `hw` (XDP_FLAGS_HW_MODE).
> (If needed more can be added)
>
> This change provides greater control and flexibility for users to
> fine-tune AF_XDP behavior, enabling them to leverage native driver
> or hardware offload capabilities when available, or fall back to
> generic SKB mode for broader compatibility.
>
> Signed-off-by: Shivaji Kant <shivajikant@google.com>
> ---
> drivers/net/af_xdp/rte_eth_af_xdp.c | 61 +++++++++++++++++++++++++----
> 1 file changed, 54 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/net/af_xdp/rte_eth_af_xdp.c b/drivers/net/af_xdp/rte_eth_af_xdp.c
> index 5f65850a27..e39f2b4935 100644
> --- a/drivers/net/af_xdp/rte_eth_af_xdp.c
> +++ b/drivers/net/af_xdp/rte_eth_af_xdp.c
> @@ -104,6 +104,8 @@ RTE_LOG_REGISTER_DEFAULT(af_xdp_logtype, NOTICE);
>
> static int afxdp_dev_count;
>
> +static __u32 mode_flag = XDP_FLAGS_UPDATE_IF_NOEXIST;

If one seeks to add a per-device argument, then why is this static/driver-wide?
This belongs in 'struct pmd_internals'.

Thank you.

> +
> /* Message header to synchronize fds via IPC */
> struct ipc_hdr {
> 	char port_name[RTE_DEV_NAME_MAX_LEN];
> @@ -198,6 +200,26 @@ struct pmd_process_private {
> #define ETH_AF_XDP_USE_CNI_ARG			"use_cni"
> #define ETH_AF_XDP_USE_PINNED_MAP_ARG	"use_pinned_map"
> #define ETH_AF_XDP_DP_PATH_ARG			"dp_path"
> +#define ETH_AF_XDP_MODE_ARG				"mode"
> +
> +/* Define different modes for af_xdp prog to attach */
> +#define ETH_AF_XDP_DRV_MODE_ARG			"drv"
> +#define ETH_AF_XDP_SKB_MODE_ARG			"skb"
> +#define ETH_AF_XDP_HW_MODE_ARG			"hw"
> +#define ETH_AF_XDP_NUM_MODE_ARG			3
> +
> +static const char * const mode_arguments[] = {
> +	ETH_AF_XDP_DRV_MODE_ARG,
> +	ETH_AF_XDP_SKB_MODE_ARG,
> +	ETH_AF_XDP_HW_MODE_ARG,
> +	NULL
> +};
> +
> +static const unsigned int mode_flags[] = {
> +	XDP_FLAGS_DRV_MODE,
> +	XDP_FLAGS_SKB_MODE,
> +	XDP_FLAGS_HW_MODE
> +};
>
> static const char * const valid_arguments[] = {
> 	ETH_AF_XDP_IFACE_ARG,
> @@ -210,6 +232,7 @@ static const char * const valid_arguments[] = {
> 	ETH_AF_XDP_USE_CNI_ARG,
> 	ETH_AF_XDP_USE_PINNED_MAP_ARG,
> 	ETH_AF_XDP_DP_PATH_ARG,
> +	ETH_AF_XDP_MODE_ARG,
> 	NULL
> };
>
> @@ -950,14 +973,14 @@ remove_xdp_program(struct pmd_internals *internals)
> 	uint32_t curr_prog_id = 0;
> 	int ret;
>
> -	ret = bpf_xdp_query_id(internals->if_index, XDP_FLAGS_UPDATE_IF_NOEXIST,
> +	ret = bpf_xdp_query_id(internals->if_index, mode_flag,
> 			       &curr_prog_id);
> 	if (ret != 0) {
> 		AF_XDP_LOG_LINE(ERR, "bpf_xdp_query_id failed");
> 		return ret;
> 	}
>
> -	ret = bpf_xdp_detach(internals->if_index, XDP_FLAGS_UPDATE_IF_NOEXIST,
> +	ret = bpf_xdp_detach(internals->if_index, mode_flag,
> 			     NULL);
> 	if (ret != 0)
> 		AF_XDP_LOG_LINE(ERR, "bpf_xdp_detach failed");
> @@ -978,14 +1001,14 @@ remove_xdp_program(struct pmd_internals *internals)
> 	int ret;
>
> 	ret = bpf_get_link_xdp_id(internals->if_index, &curr_prog_id,
> -				  XDP_FLAGS_UPDATE_IF_NOEXIST);
> +				  mode_flag);
> 	if (ret != 0) {
> 		AF_XDP_LOG_LINE(ERR, "bpf_get_link_xdp_id failed");
> 		return ret;
> 	}
>
> 	ret = bpf_set_link_xdp_fd(internals->if_index, -1,
> -				  XDP_FLAGS_UPDATE_IF_NOEXIST);
> +				  mode_flag);
> 	if (ret != 0)
> 		AF_XDP_LOG_LINE(ERR, "bpf_set_link_xdp_fd failed");
> 	return ret;
> @@ -1328,7 +1351,7 @@ load_custom_xdp_prog(const char *prog_path, int if_index, struct bpf_map **map)
>
> 	/* Link the program with the given network device */
> 	ret = link_xdp_prog_with_dev(if_index, prog_fd,
> -					XDP_FLAGS_UPDATE_IF_NOEXIST);
> +					mode_flag);
> 	if (ret) {
> 		AF_XDP_LOG_LINE(ERR, "Failed to set prog fd %d on interface",
> 				prog_fd);
> @@ -1679,7 +1702,7 @@ xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
> 	cfg.rx_size = ring_size;
> 	cfg.tx_size = ring_size;
> 	cfg.libbpf_flags = 0;
> -	cfg.xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
> +	cfg.xdp_flags = mode_flag;
> 	cfg.bind_flags = 0;
>
> 	/* Force AF_XDP socket into copy mode when users want it */
> @@ -2017,6 +2040,25 @@ parse_name_arg(const char *key __rte_unused,
> 	return 0;
> }
>
> +/** parse name argument */
> +static int
> +parse_mode_arg(const char *key __rte_unused,
> +	       const char *value, void *extra_args)
> +{
> +	unsigned int *mode = extra_args;
> +	unsigned int i;
> +
> +	for (i = 0; i < ETH_AF_XDP_NUM_MODE_ARG; i++) {
> +		if (strcmp(value, mode_arguments[i]) == 0) {
> +			*mode |= mode_flags[i];
> +			return 0;
> +		}
> +	}
> +
> +	AF_XDP_LOG_LINE(ERR, "Invalid af_xdp mode, choose correct mode to attach af_xdp program.");
> +	return -EINVAL;
> +}
> +
> /** parse xdp prog argument */
> static int
> parse_prog_arg(const char *key __rte_unused,
> @@ -2147,6 +2189,10 @@ parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
>
> 	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_DP_PATH_ARG,
> 				 &parse_prog_arg, dp_path);
> +
> +	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_MODE_ARG,
> +				 &parse_mode_arg, &mode_flag);
> +
> 	if (ret < 0)
> 		goto free_kvlist;
>
> @@ -2587,4 +2633,5 @@ RTE_PMD_REGISTER_PARAM_STRING(net_af_xdp,
> 			      "force_copy=<int> "
> 			      "use_cni=<int> "
> 			      "use_pinned_map=<int> "
> -			      "dp_path=<string> ");
> +			      "dp_path=<string> "
> +			      "mode=<string> ");
> -- 
> 2.50.0.727.gbf7dc18ff4-goog
>
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2] net/af_xdp: enable AF_XDP program attachment mode
  2025-07-23  7:42 [PATCH] net/af_xdp: enable AF_XDP program attachment mode Shivaji Kant
  2025-07-23 12:30 ` Ivan Malov
@ 2025-07-29  8:24 ` Shivaji Kant
  2025-07-29 10:08   ` [PATCH v3] " Shivaji Kant
  1 sibling, 1 reply; 8+ messages in thread
From: Shivaji Kant @ 2025-07-29  8:24 UTC (permalink / raw)
  To: Stephen Hemminger, Ivan Malov
  Cc: dev, Shivaji Kant, Ciara Loftus, Maryam Tahhan

Currently, the AF_XDP PMD attaches its XDP program using a fixed
set of flags, defaulting to `XDP_FLAGS_UPDATE_IF_NOEXIST`. This lacks
flexibility for users who might need to explicitly control the XDP
mode based on their specific hardware capabilities or performance
requirements.

This patch introduces a new vdev argument, `mode`, for the `net_af_xdp`
PMD. This argument allows users to explicitly specify the desired
XDP program attachment mode:
`drv` (XDP_FLAGS_DRV_MODE),
`skb` (XDP_FLAGS_SKB_MODE),
`hw` (XDP_FLAGS_HW_MODE).
(If needed more can be added)

This change provides greater control and flexibility for users to
fine-tune AF_XDP behavior, enabling them to leverage native driver
or hardware offload capabilities when available, or fall back to
generic SKB mode for broader compatibility.

Signed-off-by: Shivaji Kant <shivajikant@google.com>
---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 75 ++++++++++++++++++++++++-----
 1 file changed, 62 insertions(+), 13 deletions(-)

diff --git a/drivers/net/af_xdp/rte_eth_af_xdp.c b/drivers/net/af_xdp/rte_eth_af_xdp.c
index 5f65850a27..cf0077c62a 100644
--- a/drivers/net/af_xdp/rte_eth_af_xdp.c
+++ b/drivers/net/af_xdp/rte_eth_af_xdp.c
@@ -104,6 +104,7 @@ RTE_LOG_REGISTER_DEFAULT(af_xdp_logtype, NOTICE);
 
 static int afxdp_dev_count;
 
+
 /* Message header to synchronize fds via IPC */
 struct ipc_hdr {
 	char port_name[RTE_DEV_NAME_MAX_LEN];
@@ -169,6 +170,7 @@ struct pmd_internals {
 	int queue_cnt;
 	int max_queue_cnt;
 	int configured_queue_cnt;
+	uint mode_flag;
 	bool shared_umem;
 	char prog_path[PATH_MAX];
 	bool custom_prog_configured;
@@ -198,6 +200,26 @@ struct pmd_process_private {
 #define ETH_AF_XDP_USE_CNI_ARG			"use_cni"
 #define ETH_AF_XDP_USE_PINNED_MAP_ARG	"use_pinned_map"
 #define ETH_AF_XDP_DP_PATH_ARG			"dp_path"
+#define ETH_AF_XDP_MODE_ARG				"mode"
+
+/* Define different modes for af_xdp prog to attach */
+#define ETH_AF_XDP_DRV_MODE_ARG			"drv"
+#define ETH_AF_XDP_SKB_MODE_ARG			"skb"
+#define ETH_AF_XDP_HW_MODE_ARG			"hw"
+#define ETH_AF_XDP_NUM_MODE_ARG			3
+
+static const char * const mode_arguments[] = {
+	ETH_AF_XDP_DRV_MODE_ARG,
+	ETH_AF_XDP_SKB_MODE_ARG,
+	ETH_AF_XDP_HW_MODE_ARG,
+	NULL
+};
+
+static const unsigned int mode_flags[] = {
+	XDP_FLAGS_DRV_MODE,
+	XDP_FLAGS_SKB_MODE,
+	XDP_FLAGS_HW_MODE
+};
 
 static const char * const valid_arguments[] = {
 	ETH_AF_XDP_IFACE_ARG,
@@ -210,6 +232,7 @@ static const char * const valid_arguments[] = {
 	ETH_AF_XDP_USE_CNI_ARG,
 	ETH_AF_XDP_USE_PINNED_MAP_ARG,
 	ETH_AF_XDP_DP_PATH_ARG,
+	ETH_AF_XDP_MODE_ARG,
 	NULL
 };
 
@@ -950,14 +973,14 @@ remove_xdp_program(struct pmd_internals *internals)
 	uint32_t curr_prog_id = 0;
 	int ret;
 
-	ret = bpf_xdp_query_id(internals->if_index, XDP_FLAGS_UPDATE_IF_NOEXIST,
+	ret = bpf_xdp_query_id(internals->if_index, internals->mode_flag,
 			       &curr_prog_id);
 	if (ret != 0) {
 		AF_XDP_LOG_LINE(ERR, "bpf_xdp_query_id failed");
 		return ret;
 	}
 
-	ret = bpf_xdp_detach(internals->if_index, XDP_FLAGS_UPDATE_IF_NOEXIST,
+	ret = bpf_xdp_detach(internals->if_index, internals->mode_flag,
 			     NULL);
 	if (ret != 0)
 		AF_XDP_LOG_LINE(ERR, "bpf_xdp_detach failed");
@@ -978,14 +1001,14 @@ remove_xdp_program(struct pmd_internals *internals)
 	int ret;
 
 	ret = bpf_get_link_xdp_id(internals->if_index, &curr_prog_id,
-				  XDP_FLAGS_UPDATE_IF_NOEXIST);
+				  mode_flag);
 	if (ret != 0) {
 		AF_XDP_LOG_LINE(ERR, "bpf_get_link_xdp_id failed");
 		return ret;
 	}
 
 	ret = bpf_set_link_xdp_fd(internals->if_index, -1,
-				  XDP_FLAGS_UPDATE_IF_NOEXIST);
+				  mode_flag);
 	if (ret != 0)
 		AF_XDP_LOG_LINE(ERR, "bpf_set_link_xdp_fd failed");
 	return ret;
@@ -1305,7 +1328,7 @@ get_pinned_map(const char *dp_path, int *map_fd)
 }
 
 static int
-load_custom_xdp_prog(const char *prog_path, int if_index, struct bpf_map **map)
+load_custom_xdp_prog(const char *prog_path, int if_index, struct bpf_map **map, uint mode_flag)
 {
 	int ret, prog_fd;
 	struct bpf_object *obj;
@@ -1328,7 +1351,7 @@ load_custom_xdp_prog(const char *prog_path, int if_index, struct bpf_map **map)
 
 	/* Link the program with the given network device */
 	ret = link_xdp_prog_with_dev(if_index, prog_fd,
-					XDP_FLAGS_UPDATE_IF_NOEXIST);
+					mode_flag);
 	if (ret) {
 		AF_XDP_LOG_LINE(ERR, "Failed to set prog fd %d on interface",
 				prog_fd);
@@ -1679,7 +1702,7 @@ xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
 	cfg.rx_size = ring_size;
 	cfg.tx_size = ring_size;
 	cfg.libbpf_flags = 0;
-	cfg.xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
+	cfg.xdp_flags = internals->mode_flag;
 	cfg.bind_flags = 0;
 
 	/* Force AF_XDP socket into copy mode when users want it */
@@ -1698,7 +1721,7 @@ xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
 		if (!internals->custom_prog_configured) {
 			ret = load_custom_xdp_prog(internals->prog_path,
 							internals->if_index,
-							&internals->map);
+							&internals->map, internals->mode_flag);
 			if (ret) {
 				AF_XDP_LOG_LINE(ERR, "Failed to load custom XDP program %s",
 						internals->prog_path);
@@ -2017,6 +2040,25 @@ parse_name_arg(const char *key __rte_unused,
 	return 0;
 }
 
+/** parse name argument */
+static int
+parse_mode_arg(const char *key __rte_unused,
+	       const char *value, void *extra_args)
+{
+	unsigned int *mode = extra_args;
+	unsigned int i;
+
+	for (i = 0; i < ETH_AF_XDP_NUM_MODE_ARG; i++) {
+		if (strcmp(value, mode_arguments[i]) == 0) {
+			*mode |= mode_flags[i];
+			return 0;
+		}
+	}
+
+	AF_XDP_LOG_LINE(ERR, "Invalid af_xdp mode, choose correct mode to attach af_xdp program.");
+	return -EINVAL;
+}
+
 /** parse xdp prog argument */
 static int
 parse_prog_arg(const char *key __rte_unused,
@@ -2094,7 +2136,7 @@ static int
 parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
 		 int *queue_cnt, int *shared_umem, char *prog_path,
 		 int *busy_budget, int *force_copy, int *use_cni,
-		 int *use_pinned_map, char *dp_path)
+		 int *use_pinned_map, char *dp_path, uint *xdp_mode)
 {
 	int ret;
 
@@ -2147,6 +2189,10 @@ parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
 
 	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_DP_PATH_ARG,
 				 &parse_prog_arg, dp_path);
+
+	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_MODE_ARG,
+				 &parse_mode_arg, xdp_mode);
+
 	if (ret < 0)
 		goto free_kvlist;
 
@@ -2189,7 +2235,7 @@ static struct rte_eth_dev *
 init_internals(struct rte_vdev_device *dev, const char *if_name,
 	       int start_queue_idx, int queue_cnt, int shared_umem,
 	       const char *prog_path, int busy_budget, int force_copy,
-	       int use_cni, int use_pinned_map, const char *dp_path)
+	       int use_cni, int use_pinned_map, const char *dp_path, uint xdp_mode)
 {
 	const char *name = rte_vdev_device_name(dev);
 	const unsigned int numa_node = dev->device.numa_node;
@@ -2220,6 +2266,7 @@ init_internals(struct rte_vdev_device *dev, const char *if_name,
 	internals->force_copy = force_copy;
 	internals->use_cni = use_cni;
 	internals->use_pinned_map = use_pinned_map;
+	internals->mode_flag = XDP_FLAGS_UPDATE_IF_NOEXIST | xdp_mode;
 	strlcpy(internals->dp_path, dp_path, PATH_MAX);
 
 	if (xdp_get_channels_info(if_name, &internals->max_queue_cnt,
@@ -2412,6 +2459,7 @@ rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
 	int force_copy = 0;
 	int use_cni = 0;
 	int use_pinned_map = 0;
+	uint xdp_mode = 0;
 	char dp_path[PATH_MAX] = {'\0'};
 	struct rte_eth_dev *eth_dev = NULL;
 	const char *name = rte_vdev_device_name(dev);
@@ -2456,7 +2504,7 @@ rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
 	if (parse_parameters(kvlist, if_name, &xsk_start_queue_idx,
 			     &xsk_queue_cnt, &shared_umem, prog_path,
 			     &busy_budget, &force_copy, &use_cni, &use_pinned_map,
-			     dp_path) < 0) {
+			     dp_path, &xdp_mode) < 0) {
 		AF_XDP_LOG_LINE(ERR, "Invalid kvargs value");
 		return -EINVAL;
 	}
@@ -2524,7 +2572,7 @@ rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
 	eth_dev = init_internals(dev, if_name, xsk_start_queue_idx,
 				 xsk_queue_cnt, shared_umem, prog_path,
 				 busy_budget, force_copy, use_cni, use_pinned_map,
-				 dp_path);
+				 dp_path, xdp_mode);
 	if (eth_dev == NULL) {
 		AF_XDP_LOG_LINE(ERR, "Failed to init internals");
 		return -1;
@@ -2587,4 +2635,5 @@ RTE_PMD_REGISTER_PARAM_STRING(net_af_xdp,
 			      "force_copy=<int> "
 			      "use_cni=<int> "
 			      "use_pinned_map=<int> "
-			      "dp_path=<string> ");
+			      "dp_path=<string> "
+			      "mode=<string> ");
-- 
2.50.1.470.g6ba607880d-goog


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v3] net/af_xdp: enable AF_XDP program attachment mode
  2025-07-29  8:24 ` [PATCH v2] " Shivaji Kant
@ 2025-07-29 10:08   ` Shivaji Kant
  2025-07-29 14:48     ` Stephen Hemminger
                       ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Shivaji Kant @ 2025-07-29 10:08 UTC (permalink / raw)
  To: Stephen Hemminger, Ivan Malov
  Cc: dev, Shivaji Kant, Ciara Loftus, Maryam Tahhan

Currently, the AF_XDP PMD attaches its XDP program using a fixed
set of flags, defaulting to `XDP_FLAGS_UPDATE_IF_NOEXIST`. This lacks
flexibility for users who might need to explicitly control the XDP
mode based on their specific hardware capabilities or performance
requirements.

This patch introduces a new vdev argument, `mode`, for the `net_af_xdp`
PMD. This argument allows users to explicitly specify the desired
XDP program attachment mode:
`drv` (XDP_FLAGS_DRV_MODE),
`skb` (XDP_FLAGS_SKB_MODE),
`hw` (XDP_FLAGS_HW_MODE).
(If needed more can be added)

This change provides greater control and flexibility for users to
fine-tune AF_XDP behavior, enabling them to leverage native driver
or hardware offload capabilities when available, or fall back to
generic SKB mode for broader compatibility.

Signed-off-by: Shivaji Kant <shivajikant@google.com>

---
Changes in v2:
The attach mode flag was intended to be per device flag. So making
it part of the pmd_internals struct.
---
Changes in v3
Fix the build error for cases without RTE_NET_AF_XDP_LIBBPF_XDP_ATTACH
---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 75 ++++++++++++++++++++++++-----
 1 file changed, 62 insertions(+), 13 deletions(-)

diff --git a/drivers/net/af_xdp/rte_eth_af_xdp.c b/drivers/net/af_xdp/rte_eth_af_xdp.c
index 5f65850a27..fbd592c538 100644
--- a/drivers/net/af_xdp/rte_eth_af_xdp.c
+++ b/drivers/net/af_xdp/rte_eth_af_xdp.c
@@ -104,6 +104,7 @@ RTE_LOG_REGISTER_DEFAULT(af_xdp_logtype, NOTICE);
 
 static int afxdp_dev_count;
 
+
 /* Message header to synchronize fds via IPC */
 struct ipc_hdr {
 	char port_name[RTE_DEV_NAME_MAX_LEN];
@@ -169,6 +170,7 @@ struct pmd_internals {
 	int queue_cnt;
 	int max_queue_cnt;
 	int configured_queue_cnt;
+	uint mode_flag;
 	bool shared_umem;
 	char prog_path[PATH_MAX];
 	bool custom_prog_configured;
@@ -198,6 +200,26 @@ struct pmd_process_private {
 #define ETH_AF_XDP_USE_CNI_ARG			"use_cni"
 #define ETH_AF_XDP_USE_PINNED_MAP_ARG	"use_pinned_map"
 #define ETH_AF_XDP_DP_PATH_ARG			"dp_path"
+#define ETH_AF_XDP_MODE_ARG				"mode"
+
+/* Define different modes for af_xdp prog to attach */
+#define ETH_AF_XDP_DRV_MODE_ARG			"drv"
+#define ETH_AF_XDP_SKB_MODE_ARG			"skb"
+#define ETH_AF_XDP_HW_MODE_ARG			"hw"
+#define ETH_AF_XDP_NUM_MODE_ARG			3
+
+static const char * const mode_arguments[] = {
+	ETH_AF_XDP_DRV_MODE_ARG,
+	ETH_AF_XDP_SKB_MODE_ARG,
+	ETH_AF_XDP_HW_MODE_ARG,
+	NULL
+};
+
+static const unsigned int mode_flags[] = {
+	XDP_FLAGS_DRV_MODE,
+	XDP_FLAGS_SKB_MODE,
+	XDP_FLAGS_HW_MODE
+};
 
 static const char * const valid_arguments[] = {
 	ETH_AF_XDP_IFACE_ARG,
@@ -210,6 +232,7 @@ static const char * const valid_arguments[] = {
 	ETH_AF_XDP_USE_CNI_ARG,
 	ETH_AF_XDP_USE_PINNED_MAP_ARG,
 	ETH_AF_XDP_DP_PATH_ARG,
+	ETH_AF_XDP_MODE_ARG,
 	NULL
 };
 
@@ -950,14 +973,14 @@ remove_xdp_program(struct pmd_internals *internals)
 	uint32_t curr_prog_id = 0;
 	int ret;
 
-	ret = bpf_xdp_query_id(internals->if_index, XDP_FLAGS_UPDATE_IF_NOEXIST,
+	ret = bpf_xdp_query_id(internals->if_index, internals->mode_flag,
 			       &curr_prog_id);
 	if (ret != 0) {
 		AF_XDP_LOG_LINE(ERR, "bpf_xdp_query_id failed");
 		return ret;
 	}
 
-	ret = bpf_xdp_detach(internals->if_index, XDP_FLAGS_UPDATE_IF_NOEXIST,
+	ret = bpf_xdp_detach(internals->if_index, internals->mode_flag,
 			     NULL);
 	if (ret != 0)
 		AF_XDP_LOG_LINE(ERR, "bpf_xdp_detach failed");
@@ -978,14 +1001,14 @@ remove_xdp_program(struct pmd_internals *internals)
 	int ret;
 
 	ret = bpf_get_link_xdp_id(internals->if_index, &curr_prog_id,
-				  XDP_FLAGS_UPDATE_IF_NOEXIST);
+				  internals->mode_flag);
 	if (ret != 0) {
 		AF_XDP_LOG_LINE(ERR, "bpf_get_link_xdp_id failed");
 		return ret;
 	}
 
 	ret = bpf_set_link_xdp_fd(internals->if_index, -1,
-				  XDP_FLAGS_UPDATE_IF_NOEXIST);
+				  internals->mode_flag);
 	if (ret != 0)
 		AF_XDP_LOG_LINE(ERR, "bpf_set_link_xdp_fd failed");
 	return ret;
@@ -1305,7 +1328,7 @@ get_pinned_map(const char *dp_path, int *map_fd)
 }
 
 static int
-load_custom_xdp_prog(const char *prog_path, int if_index, struct bpf_map **map)
+load_custom_xdp_prog(const char *prog_path, int if_index, struct bpf_map **map, uint mode_flag)
 {
 	int ret, prog_fd;
 	struct bpf_object *obj;
@@ -1328,7 +1351,7 @@ load_custom_xdp_prog(const char *prog_path, int if_index, struct bpf_map **map)
 
 	/* Link the program with the given network device */
 	ret = link_xdp_prog_with_dev(if_index, prog_fd,
-					XDP_FLAGS_UPDATE_IF_NOEXIST);
+					mode_flag);
 	if (ret) {
 		AF_XDP_LOG_LINE(ERR, "Failed to set prog fd %d on interface",
 				prog_fd);
@@ -1679,7 +1702,7 @@ xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
 	cfg.rx_size = ring_size;
 	cfg.tx_size = ring_size;
 	cfg.libbpf_flags = 0;
-	cfg.xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
+	cfg.xdp_flags = internals->mode_flag;
 	cfg.bind_flags = 0;
 
 	/* Force AF_XDP socket into copy mode when users want it */
@@ -1698,7 +1721,7 @@ xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
 		if (!internals->custom_prog_configured) {
 			ret = load_custom_xdp_prog(internals->prog_path,
 							internals->if_index,
-							&internals->map);
+							&internals->map, internals->mode_flag);
 			if (ret) {
 				AF_XDP_LOG_LINE(ERR, "Failed to load custom XDP program %s",
 						internals->prog_path);
@@ -2017,6 +2040,25 @@ parse_name_arg(const char *key __rte_unused,
 	return 0;
 }
 
+/** parse name argument */
+static int
+parse_mode_arg(const char *key __rte_unused,
+	       const char *value, void *extra_args)
+{
+	unsigned int *mode = extra_args;
+	unsigned int i;
+
+	for (i = 0; i < ETH_AF_XDP_NUM_MODE_ARG; i++) {
+		if (strcmp(value, mode_arguments[i]) == 0) {
+			*mode |= mode_flags[i];
+			return 0;
+		}
+	}
+
+	AF_XDP_LOG_LINE(ERR, "Invalid af_xdp mode, choose correct mode to attach af_xdp program.");
+	return -EINVAL;
+}
+
 /** parse xdp prog argument */
 static int
 parse_prog_arg(const char *key __rte_unused,
@@ -2094,7 +2136,7 @@ static int
 parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
 		 int *queue_cnt, int *shared_umem, char *prog_path,
 		 int *busy_budget, int *force_copy, int *use_cni,
-		 int *use_pinned_map, char *dp_path)
+		 int *use_pinned_map, char *dp_path, uint *xdp_mode)
 {
 	int ret;
 
@@ -2147,6 +2189,10 @@ parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
 
 	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_DP_PATH_ARG,
 				 &parse_prog_arg, dp_path);
+
+	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_MODE_ARG,
+				 &parse_mode_arg, xdp_mode);
+
 	if (ret < 0)
 		goto free_kvlist;
 
@@ -2189,7 +2235,7 @@ static struct rte_eth_dev *
 init_internals(struct rte_vdev_device *dev, const char *if_name,
 	       int start_queue_idx, int queue_cnt, int shared_umem,
 	       const char *prog_path, int busy_budget, int force_copy,
-	       int use_cni, int use_pinned_map, const char *dp_path)
+	       int use_cni, int use_pinned_map, const char *dp_path, uint xdp_mode)
 {
 	const char *name = rte_vdev_device_name(dev);
 	const unsigned int numa_node = dev->device.numa_node;
@@ -2220,6 +2266,7 @@ init_internals(struct rte_vdev_device *dev, const char *if_name,
 	internals->force_copy = force_copy;
 	internals->use_cni = use_cni;
 	internals->use_pinned_map = use_pinned_map;
+	internals->mode_flag = XDP_FLAGS_UPDATE_IF_NOEXIST | xdp_mode;
 	strlcpy(internals->dp_path, dp_path, PATH_MAX);
 
 	if (xdp_get_channels_info(if_name, &internals->max_queue_cnt,
@@ -2412,6 +2459,7 @@ rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
 	int force_copy = 0;
 	int use_cni = 0;
 	int use_pinned_map = 0;
+	uint xdp_mode = 0;
 	char dp_path[PATH_MAX] = {'\0'};
 	struct rte_eth_dev *eth_dev = NULL;
 	const char *name = rte_vdev_device_name(dev);
@@ -2456,7 +2504,7 @@ rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
 	if (parse_parameters(kvlist, if_name, &xsk_start_queue_idx,
 			     &xsk_queue_cnt, &shared_umem, prog_path,
 			     &busy_budget, &force_copy, &use_cni, &use_pinned_map,
-			     dp_path) < 0) {
+			     dp_path, &xdp_mode) < 0) {
 		AF_XDP_LOG_LINE(ERR, "Invalid kvargs value");
 		return -EINVAL;
 	}
@@ -2524,7 +2572,7 @@ rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
 	eth_dev = init_internals(dev, if_name, xsk_start_queue_idx,
 				 xsk_queue_cnt, shared_umem, prog_path,
 				 busy_budget, force_copy, use_cni, use_pinned_map,
-				 dp_path);
+				 dp_path, xdp_mode);
 	if (eth_dev == NULL) {
 		AF_XDP_LOG_LINE(ERR, "Failed to init internals");
 		return -1;
@@ -2587,4 +2635,5 @@ RTE_PMD_REGISTER_PARAM_STRING(net_af_xdp,
 			      "force_copy=<int> "
 			      "use_cni=<int> "
 			      "use_pinned_map=<int> "
-			      "dp_path=<string> ");
+			      "dp_path=<string> "
+			      "mode=<string> ");
-- 
2.50.1.487.gc89ff58d15-goog


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v3] net/af_xdp: enable AF_XDP program attachment mode
  2025-07-29 10:08   ` [PATCH v3] " Shivaji Kant
@ 2025-07-29 14:48     ` Stephen Hemminger
  2025-07-29 14:50     ` Stephen Hemminger
  2025-07-29 16:17     ` [PATCH v4] " Shivaji Kant
  2 siblings, 0 replies; 8+ messages in thread
From: Stephen Hemminger @ 2025-07-29 14:48 UTC (permalink / raw)
  To: Shivaji Kant; +Cc: Ivan Malov, dev, Ciara Loftus, Maryam Tahhan

On Tue, 29 Jul 2025 10:08:45 +0000
Shivaji Kant <shivajikant@google.com> wrote:

> diff --git a/drivers/net/af_xdp/rte_eth_af_xdp.c b/drivers/net/af_xdp/rte_eth_af_xdp.c
> index 5f65850a27..fbd592c538 100644
> --- a/drivers/net/af_xdp/rte_eth_af_xdp.c
> +++ b/drivers/net/af_xdp/rte_eth_af_xdp.c
> @@ -104,6 +104,7 @@ RTE_LOG_REGISTER_DEFAULT(af_xdp_logtype, NOTICE);
>  
>  static int afxdp_dev_count;
>  
> +
>  /* Message header to synchronize fds via IPC */
>  struct ipc_hdr {
>  	char port_name[RTE_DEV_NAME_MAX_LEN];
> @@ -169,6 +170,7 @@ struct pmd_internals {

Looks like random line added while doing other edits, remove in next version.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v3] net/af_xdp: enable AF_XDP program attachment mode
  2025-07-29 10:08   ` [PATCH v3] " Shivaji Kant
  2025-07-29 14:48     ` Stephen Hemminger
@ 2025-07-29 14:50     ` Stephen Hemminger
  2025-07-29 16:17     ` [PATCH v4] " Shivaji Kant
  2 siblings, 0 replies; 8+ messages in thread
From: Stephen Hemminger @ 2025-07-29 14:50 UTC (permalink / raw)
  To: Shivaji Kant; +Cc: Ivan Malov, dev, Ciara Loftus, Maryam Tahhan

On Tue, 29 Jul 2025 10:08:45 +0000
Shivaji Kant <shivajikant@google.com> wrote:

> @@ -2147,6 +2189,10 @@ parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
>  
>  	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_DP_PATH_ARG,
>  				 &parse_prog_arg, dp_path);
> +
> +	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_MODE_ARG,
> +				 &parse_mode_arg, xdp_mode);
> +
>  	if (ret < 0)
>  		goto free_kvlist;

You lost the check for invalid path argument return.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v4] net/af_xdp: enable AF_XDP program attachment mode
  2025-07-29 10:08   ` [PATCH v3] " Shivaji Kant
  2025-07-29 14:48     ` Stephen Hemminger
  2025-07-29 14:50     ` Stephen Hemminger
@ 2025-07-29 16:17     ` Shivaji Kant
  2025-07-29 22:10       ` Stephen Hemminger
  2 siblings, 1 reply; 8+ messages in thread
From: Shivaji Kant @ 2025-07-29 16:17 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: dev, Shivaji Kant, Ciara Loftus, Maryam Tahhan

Currently, the AF_XDP PMD attaches its XDP program using a fixed
set of flags, defaulting to `XDP_FLAGS_UPDATE_IF_NOEXIST`. This lacks
flexibility for users who might need to explicitly control the XDP
mode based on their specific hardware capabilities or performance
requirements.

This patch introduces a new vdev argument, `mode`, for the `net_af_xdp`
PMD. This argument allows users to explicitly specify the desired
XDP program attachment mode:
`drv` (XDP_FLAGS_DRV_MODE),
`skb` (XDP_FLAGS_SKB_MODE),
`hw` (XDP_FLAGS_HW_MODE).
(If needed more can be added)

This change provides greater control and flexibility for users to
fine-tune AF_XDP behavior, enabling them to leverage native driver
or hardware offload capabilities when available, or fall back to
generic SKB mode for broader compatibility.

Signed-off-by: Shivaji Kant <shivajikant@google.com>

---
Changes in v2:
The attach mode flag was intended to be per device flag. So making
it part of the pmd_internals struct.
---
Changes in v3
Fix the build error for cases without RTE_NET_AF_XDP_LIBBPF_XDP_ATTACH
---
Changes in v4
Add check for invalid path argument return.
---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 77 ++++++++++++++++++++++++-----
 1 file changed, 64 insertions(+), 13 deletions(-)

diff --git a/drivers/net/af_xdp/rte_eth_af_xdp.c b/drivers/net/af_xdp/rte_eth_af_xdp.c
index 5f65850a27..9858ac24b2 100644
--- a/drivers/net/af_xdp/rte_eth_af_xdp.c
+++ b/drivers/net/af_xdp/rte_eth_af_xdp.c
@@ -169,6 +169,7 @@ struct pmd_internals {
 	int queue_cnt;
 	int max_queue_cnt;
 	int configured_queue_cnt;
+	uint mode_flag;
 	bool shared_umem;
 	char prog_path[PATH_MAX];
 	bool custom_prog_configured;
@@ -198,6 +199,26 @@ struct pmd_process_private {
 #define ETH_AF_XDP_USE_CNI_ARG			"use_cni"
 #define ETH_AF_XDP_USE_PINNED_MAP_ARG	"use_pinned_map"
 #define ETH_AF_XDP_DP_PATH_ARG			"dp_path"
+#define ETH_AF_XDP_MODE_ARG				"mode"
+
+/* Define different modes for af_xdp prog to attach */
+#define ETH_AF_XDP_DRV_MODE_ARG			"drv"
+#define ETH_AF_XDP_SKB_MODE_ARG			"skb"
+#define ETH_AF_XDP_HW_MODE_ARG			"hw"
+#define ETH_AF_XDP_NUM_MODE_ARG			3
+
+static const char * const mode_arguments[] = {
+	ETH_AF_XDP_DRV_MODE_ARG,
+	ETH_AF_XDP_SKB_MODE_ARG,
+	ETH_AF_XDP_HW_MODE_ARG,
+	NULL
+};
+
+static const unsigned int mode_flags[] = {
+	XDP_FLAGS_DRV_MODE,
+	XDP_FLAGS_SKB_MODE,
+	XDP_FLAGS_HW_MODE
+};
 
 static const char * const valid_arguments[] = {
 	ETH_AF_XDP_IFACE_ARG,
@@ -210,6 +231,7 @@ static const char * const valid_arguments[] = {
 	ETH_AF_XDP_USE_CNI_ARG,
 	ETH_AF_XDP_USE_PINNED_MAP_ARG,
 	ETH_AF_XDP_DP_PATH_ARG,
+	ETH_AF_XDP_MODE_ARG,
 	NULL
 };
 
@@ -950,14 +972,14 @@ remove_xdp_program(struct pmd_internals *internals)
 	uint32_t curr_prog_id = 0;
 	int ret;
 
-	ret = bpf_xdp_query_id(internals->if_index, XDP_FLAGS_UPDATE_IF_NOEXIST,
+	ret = bpf_xdp_query_id(internals->if_index, internals->mode_flag,
 			       &curr_prog_id);
 	if (ret != 0) {
 		AF_XDP_LOG_LINE(ERR, "bpf_xdp_query_id failed");
 		return ret;
 	}
 
-	ret = bpf_xdp_detach(internals->if_index, XDP_FLAGS_UPDATE_IF_NOEXIST,
+	ret = bpf_xdp_detach(internals->if_index, internals->mode_flag,
 			     NULL);
 	if (ret != 0)
 		AF_XDP_LOG_LINE(ERR, "bpf_xdp_detach failed");
@@ -978,14 +1000,14 @@ remove_xdp_program(struct pmd_internals *internals)
 	int ret;
 
 	ret = bpf_get_link_xdp_id(internals->if_index, &curr_prog_id,
-				  XDP_FLAGS_UPDATE_IF_NOEXIST);
+				  internals->mode_flag);
 	if (ret != 0) {
 		AF_XDP_LOG_LINE(ERR, "bpf_get_link_xdp_id failed");
 		return ret;
 	}
 
 	ret = bpf_set_link_xdp_fd(internals->if_index, -1,
-				  XDP_FLAGS_UPDATE_IF_NOEXIST);
+				  internals->mode_flag);
 	if (ret != 0)
 		AF_XDP_LOG_LINE(ERR, "bpf_set_link_xdp_fd failed");
 	return ret;
@@ -1305,7 +1327,7 @@ get_pinned_map(const char *dp_path, int *map_fd)
 }
 
 static int
-load_custom_xdp_prog(const char *prog_path, int if_index, struct bpf_map **map)
+load_custom_xdp_prog(const char *prog_path, int if_index, struct bpf_map **map, uint mode_flag)
 {
 	int ret, prog_fd;
 	struct bpf_object *obj;
@@ -1328,7 +1350,7 @@ load_custom_xdp_prog(const char *prog_path, int if_index, struct bpf_map **map)
 
 	/* Link the program with the given network device */
 	ret = link_xdp_prog_with_dev(if_index, prog_fd,
-					XDP_FLAGS_UPDATE_IF_NOEXIST);
+					mode_flag);
 	if (ret) {
 		AF_XDP_LOG_LINE(ERR, "Failed to set prog fd %d on interface",
 				prog_fd);
@@ -1679,7 +1701,7 @@ xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
 	cfg.rx_size = ring_size;
 	cfg.tx_size = ring_size;
 	cfg.libbpf_flags = 0;
-	cfg.xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
+	cfg.xdp_flags = internals->mode_flag;
 	cfg.bind_flags = 0;
 
 	/* Force AF_XDP socket into copy mode when users want it */
@@ -1698,7 +1720,7 @@ xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
 		if (!internals->custom_prog_configured) {
 			ret = load_custom_xdp_prog(internals->prog_path,
 							internals->if_index,
-							&internals->map);
+							&internals->map, internals->mode_flag);
 			if (ret) {
 				AF_XDP_LOG_LINE(ERR, "Failed to load custom XDP program %s",
 						internals->prog_path);
@@ -2017,6 +2039,25 @@ parse_name_arg(const char *key __rte_unused,
 	return 0;
 }
 
+/** parse name argument */
+static int
+parse_mode_arg(const char *key __rte_unused,
+	       const char *value, void *extra_args)
+{
+	unsigned int *mode = extra_args;
+	unsigned int i;
+
+	for (i = 0; i < ETH_AF_XDP_NUM_MODE_ARG; i++) {
+		if (strcmp(value, mode_arguments[i]) == 0) {
+			*mode |= mode_flags[i];
+			return 0;
+		}
+	}
+
+	AF_XDP_LOG_LINE(ERR, "Invalid af_xdp mode, choose correct mode to attach af_xdp program.");
+	return -EINVAL;
+}
+
 /** parse xdp prog argument */
 static int
 parse_prog_arg(const char *key __rte_unused,
@@ -2094,7 +2135,7 @@ static int
 parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
 		 int *queue_cnt, int *shared_umem, char *prog_path,
 		 int *busy_budget, int *force_copy, int *use_cni,
-		 int *use_pinned_map, char *dp_path)
+		 int *use_pinned_map, char *dp_path, uint *xdp_mode)
 {
 	int ret;
 
@@ -2147,6 +2188,13 @@ parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
 
 	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_DP_PATH_ARG,
 				 &parse_prog_arg, dp_path);
+
+	if (ret < 0)
+		goto free_kvlist;
+
+	ret = rte_kvargs_process(kvlist, ETH_AF_XDP_MODE_ARG,
+				 &parse_mode_arg, xdp_mode);
+
 	if (ret < 0)
 		goto free_kvlist;
 
@@ -2189,7 +2237,7 @@ static struct rte_eth_dev *
 init_internals(struct rte_vdev_device *dev, const char *if_name,
 	       int start_queue_idx, int queue_cnt, int shared_umem,
 	       const char *prog_path, int busy_budget, int force_copy,
-	       int use_cni, int use_pinned_map, const char *dp_path)
+	       int use_cni, int use_pinned_map, const char *dp_path, uint xdp_mode)
 {
 	const char *name = rte_vdev_device_name(dev);
 	const unsigned int numa_node = dev->device.numa_node;
@@ -2220,6 +2268,7 @@ init_internals(struct rte_vdev_device *dev, const char *if_name,
 	internals->force_copy = force_copy;
 	internals->use_cni = use_cni;
 	internals->use_pinned_map = use_pinned_map;
+	internals->mode_flag = XDP_FLAGS_UPDATE_IF_NOEXIST | xdp_mode;
 	strlcpy(internals->dp_path, dp_path, PATH_MAX);
 
 	if (xdp_get_channels_info(if_name, &internals->max_queue_cnt,
@@ -2412,6 +2461,7 @@ rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
 	int force_copy = 0;
 	int use_cni = 0;
 	int use_pinned_map = 0;
+	uint xdp_mode = 0;
 	char dp_path[PATH_MAX] = {'\0'};
 	struct rte_eth_dev *eth_dev = NULL;
 	const char *name = rte_vdev_device_name(dev);
@@ -2456,7 +2506,7 @@ rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
 	if (parse_parameters(kvlist, if_name, &xsk_start_queue_idx,
 			     &xsk_queue_cnt, &shared_umem, prog_path,
 			     &busy_budget, &force_copy, &use_cni, &use_pinned_map,
-			     dp_path) < 0) {
+			     dp_path, &xdp_mode) < 0) {
 		AF_XDP_LOG_LINE(ERR, "Invalid kvargs value");
 		return -EINVAL;
 	}
@@ -2524,7 +2574,7 @@ rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
 	eth_dev = init_internals(dev, if_name, xsk_start_queue_idx,
 				 xsk_queue_cnt, shared_umem, prog_path,
 				 busy_budget, force_copy, use_cni, use_pinned_map,
-				 dp_path);
+				 dp_path, xdp_mode);
 	if (eth_dev == NULL) {
 		AF_XDP_LOG_LINE(ERR, "Failed to init internals");
 		return -1;
@@ -2587,4 +2637,5 @@ RTE_PMD_REGISTER_PARAM_STRING(net_af_xdp,
 			      "force_copy=<int> "
 			      "use_cni=<int> "
 			      "use_pinned_map=<int> "
-			      "dp_path=<string> ");
+			      "dp_path=<string> "
+			      "mode=<string> ");
-- 
2.50.1.552.g942d659e1b-goog


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v4] net/af_xdp: enable AF_XDP program attachment mode
  2025-07-29 16:17     ` [PATCH v4] " Shivaji Kant
@ 2025-07-29 22:10       ` Stephen Hemminger
  0 siblings, 0 replies; 8+ messages in thread
From: Stephen Hemminger @ 2025-07-29 22:10 UTC (permalink / raw)
  To: Shivaji Kant; +Cc: dev, Ciara Loftus, Maryam Tahhan

On Tue, 29 Jul 2025 16:17:47 +0000
Shivaji Kant <shivajikant@google.com> wrote:

> diff --git a/drivers/net/af_xdp/rte_eth_af_xdp.c b/drivers/net/af_xdp/rte_eth_af_xdp.c
> index 5f65850a27..9858ac24b2 100644
> --- a/drivers/net/af_xdp/rte_eth_af_xdp.c
> +++ b/drivers/net/af_xdp/rte_eth_af_xdp.c
> @@ -169,6 +169,7 @@ struct pmd_internals {
>  	int queue_cnt;
>  	int max_queue_cnt;
>  	int configured_queue_cnt;
> +	uint mode_flag;
>  	bool shared_umem;
>  	char prog_path[PATH_MAX];
>  	bool custom_prog_configured;

Don't use the typedef uint, it is not what DPDK usually uses.
In types.h these are under "Old compatibility names for C types."

Better to use uint32_t which matches the __u32 arg for bpf_xdp_attach prototype.

I would also prefer that drivers avoid using int for things like
queue count because it can lead to overflow bugs. But that is a different problem.

The pmd_internals could also use some reordering to eliminate padding
and put hot elements in same cache.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2025-07-29 22:10 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-07-23  7:42 [PATCH] net/af_xdp: enable AF_XDP program attachment mode Shivaji Kant
2025-07-23 12:30 ` Ivan Malov
2025-07-29  8:24 ` [PATCH v2] " Shivaji Kant
2025-07-29 10:08   ` [PATCH v3] " Shivaji Kant
2025-07-29 14:48     ` Stephen Hemminger
2025-07-29 14:50     ` Stephen Hemminger
2025-07-29 16:17     ` [PATCH v4] " Shivaji Kant
2025-07-29 22:10       ` Stephen Hemminger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).