From: alardam@gmail.com <alardam@gmail.com>
To: intel-wired-lan@osuosl.org
Subject: [Intel-wired-lan] [PATCH 8/8] samples/bpf/xdp: apply netdev XDP/XSK modes info
Date: Mon, 16 Nov 2020 10:34:52 +0100 [thread overview]
Message-ID: <20201116093452.7541-9-marekx.majtyka@intel.com> (raw)
In-Reply-To: <20201116093452.7541-1-marekx.majtyka@intel.com>
From: Marek Majtyka <marekx.majtyka@intel.com>
Update xdpsock sample so that it utilizes netlink ethtool interface
to get available XDP/XSK modes. This allows to automatically choose
the best available mode of operation, if these are not provided explicitly.
Signed-off-by: Marek Majtyka <marekx.majtyka@intel.com>
---
samples/bpf/xdpsock_user.c | 117 ++++++++++++++++++++++++++++++++++---
1 file changed, 108 insertions(+), 9 deletions(-)
diff --git a/samples/bpf/xdpsock_user.c b/samples/bpf/xdpsock_user.c
index 1149e94ca32f..780e5d1d73a0 100644
--- a/samples/bpf/xdpsock_user.c
+++ b/samples/bpf/xdpsock_user.c
@@ -53,6 +53,9 @@
#define DEBUG_HEXDUMP 0
+#define XDP_MODES (XDP_FLAGS_SKB_MODE | XDP_FLAGS_DRV_MODE)
+#define XSK_MODES (XDP_COPY | XDP_ZEROCOPY)
+
typedef __u64 u64;
typedef __u32 u32;
typedef __u16 u16;
@@ -86,7 +89,7 @@ static u32 irq_no;
static int irqs_at_init = -1;
static int opt_poll;
static int opt_interval = 1;
-static u32 opt_xdp_bind_flags = XDP_USE_NEED_WAKEUP;
+static u16 opt_xdp_bind_flags = XDP_USE_NEED_WAKEUP;
static u32 opt_umem_flags;
static int opt_unaligned_chunks;
static int opt_mmap_flags;
@@ -95,6 +98,8 @@ static int opt_timeout = 1000;
static bool opt_need_wakeup = true;
static u32 opt_num_xsks = 1;
static u32 prog_id;
+static u32 xdp_caps;
+static u16 bind_caps;
struct xsk_ring_stats {
unsigned long rx_npkts;
@@ -957,6 +962,26 @@ static void usage(const char *prog)
exit(EXIT_FAILURE);
}
+static inline void set_drv_mode(void)
+{
+ opt_xdp_flags |= XDP_FLAGS_DRV_MODE;
+}
+
+static inline void set_skb_mode(void)
+{
+ opt_xdp_flags |= XDP_FLAGS_SKB_MODE;
+}
+
+static inline void set_zc_mode(void)
+{
+ opt_xdp_bind_flags |= XDP_ZEROCOPY;
+}
+
+static inline void set_copy_mode(void)
+{
+ opt_xdp_bind_flags |= XDP_COPY;
+}
+
static void parse_command_line(int argc, char **argv)
{
int option_index, c;
@@ -989,20 +1014,19 @@ static void parse_command_line(int argc, char **argv)
opt_poll = 1;
break;
case 'S':
- opt_xdp_flags |= XDP_FLAGS_SKB_MODE;
- opt_xdp_bind_flags |= XDP_COPY;
+ set_skb_mode();
break;
case 'N':
- /* default, set below */
+ set_drv_mode();
break;
case 'n':
opt_interval = atoi(optarg);
break;
case 'z':
- opt_xdp_bind_flags |= XDP_ZEROCOPY;
+ set_zc_mode();
break;
case 'c':
- opt_xdp_bind_flags |= XDP_COPY;
+ set_copy_mode();
break;
case 'u':
opt_umem_flags |= XDP_UMEM_UNALIGNED_CHUNK_FLAG;
@@ -1069,9 +1093,6 @@ static void parse_command_line(int argc, char **argv)
}
}
- if (!(opt_xdp_flags & XDP_FLAGS_SKB_MODE))
- opt_xdp_flags |= XDP_FLAGS_DRV_MODE;
-
opt_ifindex = if_nametoindex(opt_if);
if (!opt_ifindex) {
fprintf(stderr, "ERROR: interface \"%s\" does not exist\n",
@@ -1461,6 +1482,76 @@ static void enter_xsks_into_map(struct bpf_object *obj)
}
}
+static inline u32 xdp_mode_not_set(void)
+{
+ return (opt_xdp_flags & XDP_MODES) == 0;
+}
+
+static inline u16 bind_mode_not_set(void)
+{
+ return (opt_xdp_bind_flags & XSK_MODES) == 0;
+}
+
+static inline u16 zc_mode_set(void)
+{
+ return opt_xdp_bind_flags & XDP_ZEROCOPY;
+}
+
+static inline u32 drv_mode_set(void)
+{
+ return opt_xdp_flags & XDP_FLAGS_DRV_MODE;
+}
+
+static inline u16 zc_mode_available(void)
+{
+ return bind_caps & XDP_ZEROCOPY;
+}
+
+static inline u32 drv_mode_available(void)
+{
+ return xdp_caps & XDP_FLAGS_DRV_MODE;
+}
+
+static void set_xsk_default_flags(void)
+{
+ if (drv_mode_available()) {
+ set_drv_mode();
+
+ if (zc_mode_available())
+ set_zc_mode();
+ else
+ set_copy_mode();
+ } else {
+ set_skb_mode();
+ set_copy_mode();
+ }
+}
+
+static void adjust_missing_flags(void)
+{
+ if (xdp_mode_not_set()) {
+ if (bind_mode_not_set()) {
+ set_xsk_default_flags();
+ } else {
+ if (zc_mode_set()) {
+ set_drv_mode();
+ } else {
+ if (drv_mode_available())
+ set_drv_mode();
+ else
+ set_skb_mode();
+ }
+ }
+ } else {
+ if (bind_mode_not_set()) {
+ if (drv_mode_set() && zc_mode_available())
+ set_zc_mode();
+ else
+ set_copy_mode();
+ }
+ }
+}
+
int main(int argc, char **argv)
{
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
@@ -1473,6 +1564,14 @@ int main(int argc, char **argv)
parse_command_line(argc, argv);
+ ret = xsk_socket__get_caps(opt_if, &xdp_caps, &bind_caps);
+ if (ret) {
+ fprintf(stderr, "ERROR: xsk_socket__get_caps\n");
+ exit(EXIT_FAILURE);
+ }
+
+ adjust_missing_flags();
+
if (setrlimit(RLIMIT_MEMLOCK, &r)) {
fprintf(stderr, "ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n",
strerror(errno));
--
2.20.1
next prev parent reply other threads:[~2020-11-16 9:34 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-11-16 9:34 [Intel-wired-lan] [PATCH 0/8] New netdev feature flags for XDP alardam
2020-11-16 9:34 ` [Intel-wired-lan] [PATCH 1/8] net: ethtool: extend netdev_features flag set alardam
2020-11-16 9:34 ` [Intel-wired-lan] [PATCH 2/8] drivers/net: turn XDP flags on alardam
2020-11-16 9:34 ` [Intel-wired-lan] [PATCH 3/8] xsk: add usage of xdp netdev_features flags alardam
2020-11-16 9:34 ` [Intel-wired-lan] [PATCH 4/8] xsk: add check for full support of XDP in bind alardam
2020-11-16 9:34 ` [Intel-wired-lan] [PATCH 5/8] libbpf: extend netlink attribute API alardam
2020-11-16 9:34 ` [Intel-wired-lan] [PATCH 6/8] libbpf: add functions to get XSK modes alardam
2020-11-16 9:34 ` [Intel-wired-lan] [PATCH 7/8] libbpf: add API to get XSK/XDP caps alardam
2020-11-16 9:34 ` alardam [this message]
2020-11-16 13:25 ` [Intel-wired-lan] [PATCH 0/8] New netdev feature flags for XDP Toke =?unknown-8bit?q?H=C3=B8iland-J=C3=B8rgensen?=
2020-11-17 7:37 ` Magnus Karlsson
2020-11-17 8:55 ` Marek Majtyka
2020-11-17 18:38 ` Toke =?unknown-8bit?q?H=C3=B8iland-J=C3=B8rgensen?=
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201116093452.7541-9-marekx.majtyka@intel.com \
--to=alardam@gmail.com \
--cc=intel-wired-lan@osuosl.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox