diff mbox series

[bpf-next,2/2] samples: bpf: get ifindex from ifname

Message ID 20181018204709.29547-3-mcroce@redhat.com
State Changes Requested, archived
Delegated to: BPF Maintainers
Headers show
Series sample: xdp1 improvements | expand

Commit Message

Matteo Croce Oct. 18, 2018, 8:47 p.m. UTC
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(-)

Comments

John Fastabend Oct. 18, 2018, 9:30 p.m. UTC | #1
On 10/18/2018 01:47 PM, Matteo Croce wrote:
> Find the ifindex via ioctl(SIOCGIFINDEX) instead of requiring the
> numeric ifindex.
> 
> Signed-off-by: Matteo Croce <mcroce@redhat.com>
> ---

I don't think there are any expectation that samples have to be
stable as far as inputs over versions. And because I consistently
run this with the ifname before realizing its the ifindex not
string name I'll Ack it.

Acked-by: John Fastabend <john.fastabend@gmail.com>
Y Song Oct. 19, 2018, 5:34 a.m. UTC | #2
On Thu, Oct 18, 2018 at 1:48 PM Matteo Croce <mcroce@redhat.com> wrote:
>
> Find the ifindex via ioctl(SIOCGIFINDEX) instead of requiring the
> numeric ifindex.

Maybe use if_nametoindex which is simpler?

>
> 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
>
Matteo Croce Oct. 19, 2018, 8:42 a.m. UTC | #3
On Fri, Oct 19, 2018 at 5:35 AM Y Song <ys114321@gmail.com> wrote:
>
> On Thu, Oct 18, 2018 at 1:48 PM Matteo Croce <mcroce@redhat.com> wrote:
> >
> > Find the ifindex via ioctl(SIOCGIFINDEX) instead of requiring the
> > numeric ifindex.
>
> Maybe use if_nametoindex which is simpler?
>
> >
> > 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
> >

Right, even better. Will do a v2, thanks.
diff mbox series

Patch

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;