Skip to contentSkip to conversation

samples: bpf: make the use of xdp samples consistent

PATCH Superseded Daniel T. Lee 3 participants 4 messages Jun 24, 2019

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 samples: bpf: make the use of xdp samples consistent

Currently, each xdp samples are inconsistent in the use. Most of the samples fetch the interface with it's name. (ex. xdp1, xdp2skb, xdp_redirect, xdp_sample_pkts, etc.)

But only xdp_adjst_tail and xdp_tx_iptunnel fetch the interface with ifindex by command argument.

This commit enables those two samples to fetch interface with it's name without changing the original index interface fetching. (<ifname|ifindex> fetching in the same way as xdp_sample_pkts_user.c does.)

---
 samples/bpf/xdp_adjust_tail_user.c | 12 ++++++++++--
 samples/bpf/xdp_tx_iptunnel_user.c | 12 ++++++++++--
 2 files changed, 20 insertions(+), 4 deletions(-)
samples/bpf/xdp_adjust_tail_user.c +10 -2
index 586ff751aba9..a3596b617c4c 100644
@@ -13,6 +13,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <net/if.h>
 #include <sys/resource.h>
 #include <arpa/inet.h>
 #include <netinet/ether.h>
@@ -69,7 +70,7 @@ static void usage(const char *cmd)
 	printf("Start a XDP prog which send ICMP \"packet too big\" \n"
 		"messages if ingress packet is bigger then MAX_SIZE bytes\n");
 	printf("Usage: %s [...]\n", cmd);
-	printf("    -i <ifindex> Interface Index\n");
+	printf("    -i <ifname|ifindex> Interface\n");
 	printf("    -T <stop-after-X-seconds> Default: 0 (forever)\n");
 	printf("    -S use skb-mode\n");
 	printf("    -N enforce native mode\n");
@@ -102,7 +103,9 @@ int main(int argc, char **argv)
 
 		switch (opt) {
 		case 'i':
-			ifindex = atoi(optarg);
+			ifindex = if_nametoindex(optarg);
+			if (!ifindex)
+				ifindex = atoi(optarg);
 			break;
 		case 'T':
 			kill_after_s = atoi(optarg);
@@ -136,6 +139,11 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
+	if (!ifindex) {
+		fprintf(stderr, "Invalid ifname\n");
+		return 1;
+	}
+
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 	prog_load_attr.file = filename;
 
samples/bpf/xdp_tx_iptunnel_user.c +10 -2
index 394896430712..dfb68582e243 100644
@@ -9,6 +9,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <net/if.h>
 #include <sys/resource.h>
 #include <arpa/inet.h>
 #include <netinet/ether.h>
@@ -83,7 +84,7 @@ static void usage(const char *cmd)
 	       "in an IPv4/v6 header and XDP_TX it out.  The dst <VIP:PORT>\n"
 	       "is used to select packets to encapsulate\n\n");
 	printf("Usage: %s [...]\n", cmd);
-	printf("    -i <ifindex> Interface Index\n");
+	printf("    -i <ifname|ifindex> Interface\n");
 	printf("    -a <vip-service-address> IPv4 or IPv6\n");
 	printf("    -p <vip-service-port> A port range (e.g. 433-444) is also allowed\n");
 	printf("    -s <source-ip> Used in the IPTunnel header\n");
@@ -181,7 +182,9 @@ int main(int argc, char **argv)
 
 		switch (opt) {
 		case 'i':
-			ifindex = atoi(optarg);
+			ifindex = if_nametoindex(optarg);
+			if (!ifindex)
+				ifindex = atoi(optarg);
 			break;
 		case 'a':
 			vip.family = parse_ipstr(optarg, vip.daddr.v6);
@@ -253,6 +256,11 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
+	if (!ifindex) {
+		fprintf(stderr, "Invalid ifname\n");
+		return 1;
+	}
+
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 	prog_load_attr.file = filename;
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 3 lines · Daniel T. Lee

Currently, each xdp samples are inconsistent in the use. Most of the samples fetch the interface with it's name. (ex. xdp1, xdp2skb, xdp_redirect, xdp_sample_pkts, etc.)

The xdp_redirect and xdp_redirect_map also only accept ifindexes, not interface names. Care to fix those while you're at it? :)

-Toke

#2 lore raw

Will do right away! :)

Quoted 11 lines · Toke Høiland-Jørgensen
Quoted 3 lines · Daniel T. Lee

Currently, each xdp samples are inconsistent in the use. Most of the samples fetch the interface with it's name. (ex. xdp1, xdp2skb, xdp_redirect, xdp_sample_pkts, etc.)

The xdp_redirect and xdp_redirect_map also only accept ifindexes, not interface names. Care to fix those while you're at it? :)

-Toke

#3 lore raw
In reply to Daniel T. Lee
Quoted 5 lines · Daniel T. Lee

Currently, each xdp samples are inconsistent in the use. Most of the samples fetch the interface with it's name. (ex. xdp1, xdp2skb, xdp_redirect, xdp_sample_pkts, etc.)

Quoted 8 lines

But only xdp_adjst_tail and xdp_tx_iptunnel fetch the interface with ifindex by command argument.

This commit enables those two samples to fetch interface with it's name without changing the original index interface fetching. (<ifname|ifindex> fetching in the same way as xdp_sample_pkts_user.c does.)

From patchworks, I assume you will send v2.

Please also CC [email protected] in v2.

Thanks,
Song

Quoted 96 lines
---
 samples/bpf/xdp_adjust_tail_user.c | 12 ++++++++++--
 samples/bpf/xdp_tx_iptunnel_user.c | 12 ++++++++++--
 2 files changed, 20 insertions(+), 4 deletions(-)
samples/bpf/xdp_adjust_tail_user.c +10 -2
index 586ff751aba9..a3596b617c4c 100644
@@ -13,6 +13,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <net/if.h>
 #include <sys/resource.h>
 #include <arpa/inet.h>
 #include <netinet/ether.h>
@@ -69,7 +70,7 @@ static void usage(const char *cmd)
        printf("Start a XDP prog which send ICMP \"packet too big\" \n"
                "messages if ingress packet is bigger then MAX_SIZE bytes\n");
        printf("Usage: %s [...]\n", cmd);
-       printf("    -i <ifindex> Interface Index\n");
+       printf("    -i <ifname|ifindex> Interface\n");
        printf("    -T <stop-after-X-seconds> Default: 0 (forever)\n");
        printf("    -S use skb-mode\n");
        printf("    -N enforce native mode\n");
@@ -102,7 +103,9 @@ int main(int argc, char **argv)
 
                switch (opt) {
                case 'i':
-                       ifindex = atoi(optarg);
+                       ifindex = if_nametoindex(optarg);
+                       if (!ifindex)
+                               ifindex = atoi(optarg);
                        break;
                case 'T':
                        kill_after_s = atoi(optarg);
@@ -136,6 +139,11 @@ int main(int argc, char **argv)
                return 1;
        }
 
+       if (!ifindex) {
+               fprintf(stderr, "Invalid ifname\n");
+               return 1;
+       }
+
        snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
        prog_load_attr.file = filename;
 
samples/bpf/xdp_tx_iptunnel_user.c +10 -3
index 394896430712..dfb68582e243 100644
@@ -9,6 +9,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <net/if.h>
 #include <sys/resource.h>
 #include <arpa/inet.h>
 #include <netinet/ether.h>
@@ -83,7 +84,7 @@ static void usage(const char *cmd)
               "in an IPv4/v6 header and XDP_TX it out.  The dst <VIP:PORT>\n"
               "is used to select packets to encapsulate\n\n");
        printf("Usage: %s [...]\n", cmd);
-       printf("    -i <ifindex> Interface Index\n");
+       printf("    -i <ifname|ifindex> Interface\n");
        printf("    -a <vip-service-address> IPv4 or IPv6\n");
        printf("    -p <vip-service-port> A port range (e.g. 433-444) is also allowed\n");
        printf("    -s <source-ip> Used in the IPTunnel header\n");
@@ -181,7 +182,9 @@ int main(int argc, char **argv)
 
                switch (opt) {
                case 'i':
-                       ifindex = atoi(optarg);
+                       ifindex = if_nametoindex(optarg);
+                       if (!ifindex)
+                               ifindex = atoi(optarg);
                        break;
                case 'a':
                        vip.family = parse_ipstr(optarg, vip.daddr.v6);
@@ -253,6 +256,11 @@ int main(int argc, char **argv)
                return 1;
        }
 
+       if (!ifindex) {
+               fprintf(stderr, "Invalid ifname\n");
+               return 1;
+       }
+
        snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
        prog_load_attr.file = filename;
 
--

2.17.1