aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Leblond <eric@regit.org>2018-01-30 15:55:04 -0500
committerAlexei Starovoitov <ast@kernel.org>2018-02-02 20:53:48 -0500
commitb259c2ffd9b4812aa42a1d502eabd8c62a32d063 (patch)
tree83671596b4c2f36d19f38f1312756022c037dbc5
parent6061a3d6720600c976b877c3ac1402b3ef0a8a55 (diff)
samples/bpf: use bpf_set_link_xdp_fd
Use bpf_set_link_xdp_fd instead of set_link_xdp_fd to remove some code duplication and benefit of netlink ext ack errors message. Signed-off-by: Eric Leblond <eric@regit.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
-rw-r--r--samples/bpf/bpf_load.c102
-rw-r--r--samples/bpf/bpf_load.h2
-rw-r--r--samples/bpf/xdp1_user.c4
-rw-r--r--samples/bpf/xdp_redirect_cpu_user.c6
-rw-r--r--samples/bpf/xdp_redirect_map_user.c8
-rw-r--r--samples/bpf/xdp_redirect_user.c8
-rw-r--r--samples/bpf/xdp_router_ipv4_user.c10
-rw-r--r--samples/bpf/xdp_rxq_info_user.c4
-rw-r--r--samples/bpf/xdp_tx_iptunnel_user.c6
9 files changed, 24 insertions, 126 deletions
diff --git a/samples/bpf/bpf_load.c b/samples/bpf/bpf_load.c
index 242631aa4ea2..69806d74fa53 100644
--- a/samples/bpf/bpf_load.c
+++ b/samples/bpf/bpf_load.c
@@ -695,105 +695,3 @@ struct ksym *ksym_search(long key)
695 return &syms[0]; 695 return &syms[0];
696} 696}
697 697
698int set_link_xdp_fd(int ifindex, int fd, __u32 flags)
699{
700 struct sockaddr_nl sa;
701 int sock, seq = 0, len, ret = -1;
702 char buf[4096];
703 struct nlattr *nla, *nla_xdp;
704 struct {
705 struct nlmsghdr nh;
706 struct ifinfomsg ifinfo;
707 char attrbuf[64];
708 } req;
709 struct nlmsghdr *nh;
710 struct nlmsgerr *err;
711
712 memset(&sa, 0, sizeof(sa));
713 sa.nl_family = AF_NETLINK;
714
715 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
716 if (sock < 0) {
717 printf("open netlink socket: %s\n", strerror(errno));
718 return -1;
719 }
720
721 if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
722 printf("bind to netlink: %s\n", strerror(errno));
723 goto cleanup;
724 }
725
726 memset(&req, 0, sizeof(req));
727 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
728 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
729 req.nh.nlmsg_type = RTM_SETLINK;
730 req.nh.nlmsg_pid = 0;
731 req.nh.nlmsg_seq = ++seq;
732 req.ifinfo.ifi_family = AF_UNSPEC;
733 req.ifinfo.ifi_index = ifindex;
734
735 /* started nested attribute for XDP */
736 nla = (struct nlattr *)(((char *)&req)
737 + NLMSG_ALIGN(req.nh.nlmsg_len));
738 nla->nla_type = NLA_F_NESTED | 43/*IFLA_XDP*/;
739 nla->nla_len = NLA_HDRLEN;
740
741 /* add XDP fd */
742 nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
743 nla_xdp->nla_type = 1/*IFLA_XDP_FD*/;
744 nla_xdp->nla_len = NLA_HDRLEN + sizeof(int);
745 memcpy((char *)nla_xdp + NLA_HDRLEN, &fd, sizeof(fd));
746 nla->nla_len += nla_xdp->nla_len;
747
748 /* if user passed in any flags, add those too */
749 if (flags) {
750 nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
751 nla_xdp->nla_type = 3/*IFLA_XDP_FLAGS*/;
752 nla_xdp->nla_len = NLA_HDRLEN + sizeof(flags);
753 memcpy((char *)nla_xdp + NLA_HDRLEN, &flags, sizeof(flags));
754 nla->nla_len += nla_xdp->nla_len;
755 }
756
757 req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len);
758
759 if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) {
760 printf("send to netlink: %s\n", strerror(errno));
761 goto cleanup;
762 }
763
764 len = recv(sock, buf, sizeof(buf), 0);
765 if (len < 0) {
766 printf("recv from netlink: %s\n", strerror(errno));
767 goto cleanup;
768 }
769
770 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len);
771 nh = NLMSG_NEXT(nh, len)) {
772 if (nh->nlmsg_pid != getpid()) {
773 printf("Wrong pid %d, expected %d\n",
774 nh->nlmsg_pid, getpid());
775 goto cleanup;
776 }
777 if (nh->nlmsg_seq != seq) {
778 printf("Wrong seq %d, expected %d\n",
779 nh->nlmsg_seq, seq);
780 goto cleanup;
781 }
782 switch (nh->nlmsg_type) {
783 case NLMSG_ERROR:
784 err = (struct nlmsgerr *)NLMSG_DATA(nh);
785 if (!err->error)
786 continue;
787 printf("nlmsg error %s\n", strerror(-err->error));
788 goto cleanup;
789 case NLMSG_DONE:
790 break;
791 }
792 }
793
794 ret = 0;
795
796cleanup:
797 close(sock);
798 return ret;
799}
diff --git a/samples/bpf/bpf_load.h b/samples/bpf/bpf_load.h
index 7d57a4248893..453c200b389b 100644
--- a/samples/bpf/bpf_load.h
+++ b/samples/bpf/bpf_load.h
@@ -61,5 +61,5 @@ struct ksym {
61 61
62int load_kallsyms(void); 62int load_kallsyms(void);
63struct ksym *ksym_search(long key); 63struct ksym *ksym_search(long key);
64int set_link_xdp_fd(int ifindex, int fd, __u32 flags); 64int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
65#endif 65#endif
diff --git a/samples/bpf/xdp1_user.c b/samples/bpf/xdp1_user.c
index fdaefe91801d..b901ee2b3336 100644
--- a/samples/bpf/xdp1_user.c
+++ b/samples/bpf/xdp1_user.c
@@ -25,7 +25,7 @@ static __u32 xdp_flags;
25 25
26static void int_exit(int sig) 26static void int_exit(int sig)
27{ 27{
28 set_link_xdp_fd(ifindex, -1, xdp_flags); 28 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
29 exit(0); 29 exit(0);
30} 30}
31 31
@@ -116,7 +116,7 @@ int main(int argc, char **argv)
116 signal(SIGINT, int_exit); 116 signal(SIGINT, int_exit);
117 signal(SIGTERM, int_exit); 117 signal(SIGTERM, int_exit);
118 118
119 if (set_link_xdp_fd(ifindex, prog_fd[0], xdp_flags) < 0) { 119 if (bpf_set_link_xdp_fd(ifindex, prog_fd[0], xdp_flags) < 0) {
120 printf("link set xdp fd failed\n"); 120 printf("link set xdp fd failed\n");
121 return 1; 121 return 1;
122 } 122 }
diff --git a/samples/bpf/xdp_redirect_cpu_user.c b/samples/bpf/xdp_redirect_cpu_user.c
index 35fec9fecb57..23744a8aaf21 100644
--- a/samples/bpf/xdp_redirect_cpu_user.c
+++ b/samples/bpf/xdp_redirect_cpu_user.c
@@ -26,7 +26,7 @@ static const char *__doc__ =
26 26
27/* Wanted to get rid of bpf_load.h and fake-"libbpf.h" (and instead 27/* Wanted to get rid of bpf_load.h and fake-"libbpf.h" (and instead
28 * use bpf/libbpf.h), but cannot as (currently) needed for XDP 28 * use bpf/libbpf.h), but cannot as (currently) needed for XDP
29 * attaching to a device via set_link_xdp_fd() 29 * attaching to a device via bpf_set_link_xdp_fd()
30 */ 30 */
31#include "libbpf.h" 31#include "libbpf.h"
32#include "bpf_load.h" 32#include "bpf_load.h"
@@ -67,7 +67,7 @@ static void int_exit(int sig)
67 "Interrupted: Removing XDP program on ifindex:%d device:%s\n", 67 "Interrupted: Removing XDP program on ifindex:%d device:%s\n",
68 ifindex, ifname); 68 ifindex, ifname);
69 if (ifindex > -1) 69 if (ifindex > -1)
70 set_link_xdp_fd(ifindex, -1, xdp_flags); 70 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
71 exit(EXIT_OK); 71 exit(EXIT_OK);
72} 72}
73 73
@@ -682,7 +682,7 @@ int main(int argc, char **argv)
682 /* Remove XDP program when program is interrupted */ 682 /* Remove XDP program when program is interrupted */
683 signal(SIGINT, int_exit); 683 signal(SIGINT, int_exit);
684 684
685 if (set_link_xdp_fd(ifindex, prog_fd[prog_num], xdp_flags) < 0) { 685 if (bpf_set_link_xdp_fd(ifindex, prog_fd[prog_num], xdp_flags) < 0) {
686 fprintf(stderr, "link set xdp fd failed\n"); 686 fprintf(stderr, "link set xdp fd failed\n");
687 return EXIT_FAIL_XDP; 687 return EXIT_FAIL_XDP;
688 } 688 }
diff --git a/samples/bpf/xdp_redirect_map_user.c b/samples/bpf/xdp_redirect_map_user.c
index 978a532f0748..7eae07d7293e 100644
--- a/samples/bpf/xdp_redirect_map_user.c
+++ b/samples/bpf/xdp_redirect_map_user.c
@@ -34,9 +34,9 @@ static __u32 xdp_flags;
34 34
35static void int_exit(int sig) 35static void int_exit(int sig)
36{ 36{
37 set_link_xdp_fd(ifindex_in, -1, xdp_flags); 37 bpf_set_link_xdp_fd(ifindex_in, -1, xdp_flags);
38 if (ifindex_out_xdp_dummy_attached) 38 if (ifindex_out_xdp_dummy_attached)
39 set_link_xdp_fd(ifindex_out, -1, xdp_flags); 39 bpf_set_link_xdp_fd(ifindex_out, -1, xdp_flags);
40 exit(0); 40 exit(0);
41} 41}
42 42
@@ -120,13 +120,13 @@ int main(int argc, char **argv)
120 return 1; 120 return 1;
121 } 121 }
122 122
123 if (set_link_xdp_fd(ifindex_in, prog_fd[0], xdp_flags) < 0) { 123 if (bpf_set_link_xdp_fd(ifindex_in, prog_fd[0], xdp_flags) < 0) {
124 printf("ERROR: link set xdp fd failed on %d\n", ifindex_in); 124 printf("ERROR: link set xdp fd failed on %d\n", ifindex_in);
125 return 1; 125 return 1;
126 } 126 }
127 127
128 /* Loading dummy XDP prog on out-device */ 128 /* Loading dummy XDP prog on out-device */
129 if (set_link_xdp_fd(ifindex_out, prog_fd[1], 129 if (bpf_set_link_xdp_fd(ifindex_out, prog_fd[1],
130 (xdp_flags | XDP_FLAGS_UPDATE_IF_NOEXIST)) < 0) { 130 (xdp_flags | XDP_FLAGS_UPDATE_IF_NOEXIST)) < 0) {
131 printf("WARN: link set xdp fd failed on %d\n", ifindex_out); 131 printf("WARN: link set xdp fd failed on %d\n", ifindex_out);
132 ifindex_out_xdp_dummy_attached = false; 132 ifindex_out_xdp_dummy_attached = false;
diff --git a/samples/bpf/xdp_redirect_user.c b/samples/bpf/xdp_redirect_user.c
index 4475d837bf2c..d54e91eb6cbf 100644
--- a/samples/bpf/xdp_redirect_user.c
+++ b/samples/bpf/xdp_redirect_user.c
@@ -33,9 +33,9 @@ static __u32 xdp_flags;
33 33
34static void int_exit(int sig) 34static void int_exit(int sig)
35{ 35{
36 set_link_xdp_fd(ifindex_in, -1, xdp_flags); 36 bpf_set_link_xdp_fd(ifindex_in, -1, xdp_flags);
37 if (ifindex_out_xdp_dummy_attached) 37 if (ifindex_out_xdp_dummy_attached)
38 set_link_xdp_fd(ifindex_out, -1, xdp_flags); 38 bpf_set_link_xdp_fd(ifindex_out, -1, xdp_flags);
39 exit(0); 39 exit(0);
40} 40}
41 41
@@ -114,13 +114,13 @@ int main(int argc, char **argv)
114 return 1; 114 return 1;
115 } 115 }
116 116
117 if (set_link_xdp_fd(ifindex_in, prog_fd[0], xdp_flags) < 0) { 117 if (bpf_set_link_xdp_fd(ifindex_in, prog_fd[0], xdp_flags) < 0) {
118 printf("ERROR: link set xdp fd failed on %d\n", ifindex_in); 118 printf("ERROR: link set xdp fd failed on %d\n", ifindex_in);
119 return 1; 119 return 1;
120 } 120 }
121 121
122 /* Loading dummy XDP prog on out-device */ 122 /* Loading dummy XDP prog on out-device */
123 if (set_link_xdp_fd(ifindex_out, prog_fd[1], 123 if (bpf_set_link_xdp_fd(ifindex_out, prog_fd[1],
124 (xdp_flags | XDP_FLAGS_UPDATE_IF_NOEXIST)) < 0) { 124 (xdp_flags | XDP_FLAGS_UPDATE_IF_NOEXIST)) < 0) {
125 printf("WARN: link set xdp fd failed on %d\n", ifindex_out); 125 printf("WARN: link set xdp fd failed on %d\n", ifindex_out);
126 ifindex_out_xdp_dummy_attached = false; 126 ifindex_out_xdp_dummy_attached = false;
diff --git a/samples/bpf/xdp_router_ipv4_user.c b/samples/bpf/xdp_router_ipv4_user.c
index 916462112d55..6296741c1fbd 100644
--- a/samples/bpf/xdp_router_ipv4_user.c
+++ b/samples/bpf/xdp_router_ipv4_user.c
@@ -37,7 +37,7 @@ static void int_exit(int sig)
37 int i = 0; 37 int i = 0;
38 38
39 for (i = 0; i < total_ifindex; i++) 39 for (i = 0; i < total_ifindex; i++)
40 set_link_xdp_fd(ifindex_list[i], -1, flags); 40 bpf_set_link_xdp_fd(ifindex_list[i], -1, flags);
41 exit(0); 41 exit(0);
42} 42}
43 43
@@ -49,7 +49,7 @@ static void close_and_exit(int sig)
49 close(sock_arp); 49 close(sock_arp);
50 50
51 for (i = 0; i < total_ifindex; i++) 51 for (i = 0; i < total_ifindex; i++)
52 set_link_xdp_fd(ifindex_list[i], -1, flags); 52 bpf_set_link_xdp_fd(ifindex_list[i], -1, flags);
53 exit(0); 53 exit(0);
54} 54}
55 55
@@ -183,7 +183,7 @@ static void read_route(struct nlmsghdr *nh, int nll)
183 int i = 0; 183 int i = 0;
184 184
185 for (i = 0; i < total_ifindex; i++) 185 for (i = 0; i < total_ifindex; i++)
186 set_link_xdp_fd(ifindex_list[i], -1, flags); 186 bpf_set_link_xdp_fd(ifindex_list[i], -1, flags);
187 exit(0); 187 exit(0);
188 } 188 }
189 assert(bpf_map_update_elem(map_fd[4], &route.iface, &route.iface, 0) == 0); 189 assert(bpf_map_update_elem(map_fd[4], &route.iface, &route.iface, 0) == 0);
@@ -633,12 +633,12 @@ int main(int ac, char **argv)
633 } 633 }
634 } 634 }
635 for (i = 0; i < total_ifindex; i++) { 635 for (i = 0; i < total_ifindex; i++) {
636 if (set_link_xdp_fd(ifindex_list[i], prog_fd[0], flags) < 0) { 636 if (bpf_set_link_xdp_fd(ifindex_list[i], prog_fd[0], flags) < 0) {
637 printf("link set xdp fd failed\n"); 637 printf("link set xdp fd failed\n");
638 int recovery_index = i; 638 int recovery_index = i;
639 639
640 for (i = 0; i < recovery_index; i++) 640 for (i = 0; i < recovery_index; i++)
641 set_link_xdp_fd(ifindex_list[i], -1, flags); 641 bpf_set_link_xdp_fd(ifindex_list[i], -1, flags);
642 642
643 return 1; 643 return 1;
644 } 644 }
diff --git a/samples/bpf/xdp_rxq_info_user.c b/samples/bpf/xdp_rxq_info_user.c
index 32430e8b3a6a..478d95412de4 100644
--- a/samples/bpf/xdp_rxq_info_user.c
+++ b/samples/bpf/xdp_rxq_info_user.c
@@ -56,7 +56,7 @@ static void int_exit(int sig)
56 "Interrupted: Removing XDP program on ifindex:%d device:%s\n", 56 "Interrupted: Removing XDP program on ifindex:%d device:%s\n",
57 ifindex, ifname); 57 ifindex, ifname);
58 if (ifindex > -1) 58 if (ifindex > -1)
59 set_link_xdp_fd(ifindex, -1, xdp_flags); 59 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
60 exit(EXIT_OK); 60 exit(EXIT_OK);
61} 61}
62 62
@@ -521,7 +521,7 @@ int main(int argc, char **argv)
521 /* Remove XDP program when program is interrupted */ 521 /* Remove XDP program when program is interrupted */
522 signal(SIGINT, int_exit); 522 signal(SIGINT, int_exit);
523 523
524 if (set_link_xdp_fd(ifindex, prog_fd[0], xdp_flags) < 0) { 524 if (bpf_set_link_xdp_fd(ifindex, prog_fd[0], xdp_flags) < 0) {
525 fprintf(stderr, "link set xdp fd failed\n"); 525 fprintf(stderr, "link set xdp fd failed\n");
526 return EXIT_FAIL_XDP; 526 return EXIT_FAIL_XDP;
527 } 527 }
diff --git a/samples/bpf/xdp_tx_iptunnel_user.c b/samples/bpf/xdp_tx_iptunnel_user.c
index 715cd12eaca5..f0a787268a87 100644
--- a/samples/bpf/xdp_tx_iptunnel_user.c
+++ b/samples/bpf/xdp_tx_iptunnel_user.c
@@ -30,7 +30,7 @@ static __u32 xdp_flags = 0;
30static void int_exit(int sig) 30static void int_exit(int sig)
31{ 31{
32 if (ifindex > -1) 32 if (ifindex > -1)
33 set_link_xdp_fd(ifindex, -1, xdp_flags); 33 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
34 exit(0); 34 exit(0);
35} 35}
36 36
@@ -254,14 +254,14 @@ int main(int argc, char **argv)
254 } 254 }
255 } 255 }
256 256
257 if (set_link_xdp_fd(ifindex, prog_fd[0], xdp_flags) < 0) { 257 if (bpf_set_link_xdp_fd(ifindex, prog_fd[0], xdp_flags) < 0) {
258 printf("link set xdp fd failed\n"); 258 printf("link set xdp fd failed\n");
259 return 1; 259 return 1;
260 } 260 }
261 261
262 poll_stats(kill_after_s); 262 poll_stats(kill_after_s);
263 263
264 set_link_xdp_fd(ifindex, -1, xdp_flags); 264 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
265 265
266 return 0; 266 return 0;
267} 267}