netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Matteo Croce <mcroce@redhat.com>
To: netdev@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>
Subject: [PATCH bpf-next 2/2] samples: bpf: get ifindex from ifname
Date: Thu, 18 Oct 2018 22:47:09 +0200	[thread overview]
Message-ID: <20181018204709.29547-3-mcroce@redhat.com> (raw)
In-Reply-To: <20181018204709.29547-2-mcroce@redhat.com>

Find the ifindex via ioctl(SIOCGIFINDEX) instead of requiring the
numeric ifindex.

Signed-off-by: Matteo Croce <mcroce@redhat.com>
---
 samples/bpf/xdp1_user.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/samples/bpf/xdp1_user.c b/samples/bpf/xdp1_user.c
index 4f3d824fc044..a1d0c5dcee9c 100644
--- a/samples/bpf/xdp1_user.c
+++ b/samples/bpf/xdp1_user.c
@@ -15,6 +15,9 @@
 #include <unistd.h>
 #include <libgen.h>
 #include <sys/resource.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <linux/if.h>
 
 #include "bpf_util.h"
 #include "bpf/bpf.h"
@@ -59,7 +62,7 @@ static void poll_stats(int map_fd, int interval)
 static void usage(const char *prog)
 {
 	fprintf(stderr,
-		"usage: %s [OPTS] IFINDEX\n\n"
+		"usage: %s [OPTS] IFACE\n\n"
 		"OPTS:\n"
 		"    -S    use skb-mode\n"
 		"    -N    enforce native mode\n",
@@ -74,9 +77,11 @@ int main(int argc, char **argv)
 	};
 	const char *optstr = "SN";
 	int prog_fd, map_fd, opt;
+	struct ifreq ifr = { 0 };
 	struct bpf_object *obj;
 	struct bpf_map *map;
 	char filename[256];
+	int sock;
 
 	while ((opt = getopt(argc, argv, optstr)) != -1) {
 		switch (opt) {
@@ -102,7 +107,24 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
-	ifindex = strtoul(argv[optind], NULL, 0);
+	sock = socket(AF_UNIX, SOCK_DGRAM, 0);
+	if (sock == -1) {
+		perror("socket");
+		return 1;
+	}
+
+	if (strlen(argv[optind]) >= IFNAMSIZ) {
+		printf("invalid ifname '%s'\n", argv[optind]);
+		return 1;
+	}
+
+	strcpy(ifr.ifr_name, argv[optind]);
+	if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) {
+		perror("SIOCGIFINDEX");
+		return 1;
+	}
+	close(sock);
+	ifindex = ifr.ifr_ifindex;
 
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 	prog_load_attr.file = filename;
-- 
2.19.1

  reply	other threads:[~2018-10-19  4:50 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-18 20:47 [PATCH bpf-next 0/2] sample: xdp1 improvements Matteo Croce
2018-10-18 20:47 ` [PATCH bpf-next 1/2] samples: bpf: improve xdp1 example Matteo Croce
2018-10-18 20:47   ` Matteo Croce [this message]
2018-10-18 21:30     ` [PATCH bpf-next 2/2] samples: bpf: get ifindex from ifname John Fastabend
2018-10-19  5:34     ` Y Song
2018-10-19  8:42       ` Matteo Croce
2018-10-19  5:32   ` [PATCH bpf-next 1/2] samples: bpf: improve xdp1 example Y Song

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=20181018204709.29547-3-mcroce@redhat.com \
    --to=mcroce@redhat.com \
    --cc=ast@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=netdev@vger.kernel.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;
as well as URLs for NNTP newsgroup(s).