[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20181217102501.22019-7-bjorn.topel@gmail.com>
Date: Mon, 17 Dec 2018 11:25:00 +0100
From: Björn Töpel <bjorn.topel@...il.com>
To: bjorn.topel@...il.com, magnus.karlsson@...el.com,
magnus.karlsson@...il.com, ast@...nel.org, daniel@...earbox.net,
netdev@...r.kernel.org
Cc: Björn Töpel <bjorn.topel@...el.com>,
brouer@...hat.com, u9012063@...il.com, qi.z.zhang@...el.com,
jakub.kicinski@...ronome.com, andrew@...n.ch
Subject: [PATCH bpf-next v2 6/7] samples: bpf: simplify/cleanup xdpsock
From: Björn Töpel <bjorn.topel@...el.com>
Break out all map and XDP program setup into a function.
Signed-off-by: Björn Töpel <bjorn.topel@...el.com>
---
samples/bpf/xdpsock_user.c | 91 +++++++++++++++++++++-----------------
1 file changed, 50 insertions(+), 41 deletions(-)
diff --git a/samples/bpf/xdpsock_user.c b/samples/bpf/xdpsock_user.c
index 57ecadc58403..c72face0da05 100644
--- a/samples/bpf/xdpsock_user.c
+++ b/samples/bpf/xdpsock_user.c
@@ -119,7 +119,7 @@ struct xdpsock {
};
static int num_socks;
-struct xdpsock *xsks[MAX_SOCKS];
+static struct xdpsock *xsks[MAX_SOCKS];
static unsigned long get_nsecs(void)
{
@@ -897,37 +897,21 @@ static void l2fwd(struct xdpsock *xsk)
}
}
-int main(int argc, char **argv)
+static void setup_xdp_xskmap(const char *pathprefix)
{
- struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
- struct bpf_prog_load_attr prog_load_attr = {
- .prog_type = BPF_PROG_TYPE_XDP,
- };
- int prog_fd, qidconf_map, xsks_map;
+ int prog_fd, qidconf_map, xsks_map, i, key = 0, err;
+ struct bpf_prog_load_attr prog_load_attr = {};
+ char bpf_objs_filename[256];
+ struct bpf_program *prog;
struct bpf_object *obj;
- char xdp_filename[256];
struct bpf_map *map;
- int i, ret, key = 0;
- pthread_t pt;
- parse_command_line(argc, argv);
-
- if (setrlimit(RLIMIT_MEMLOCK, &r)) {
- fprintf(stderr, "ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n",
- strerror(errno));
- exit(EXIT_FAILURE);
- }
-
- snprintf(xdp_filename, sizeof(xdp_filename), "%s_kern.o", argv[0]);
- prog_load_attr.file = xdp_filename;
+ snprintf(bpf_objs_filename, sizeof(bpf_objs_filename), "%s_kern.o",
+ pathprefix);
+ prog_load_attr.file = bpf_objs_filename;
if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
exit(EXIT_FAILURE);
- if (prog_fd < 0) {
- fprintf(stderr, "ERROR: no program found: %s\n",
- strerror(prog_fd));
- exit(EXIT_FAILURE);
- }
map = bpf_object__find_map_by_name(obj, "qidconf_map");
qidconf_map = bpf_map__fd(map);
@@ -945,14 +929,51 @@ int main(int argc, char **argv)
exit(EXIT_FAILURE);
}
+ err = bpf_map_update_elem(qidconf_map, &key, &opt_queue, 0);
+ if (err) {
+ fprintf(stderr, "ERROR: bpf_map_update_elem qidconf\n");
+ exit(EXIT_FAILURE);
+ }
+
+ /* Insert sockets into into the XSKMAP. */
+ for (i = 0; i < num_socks; i++) {
+ key = i;
+ err = bpf_map_update_elem(xsks_map, &key, &xsks[i]->sfd, 0);
+ if (err) {
+ fprintf(stderr, "ERROR: bpf_map_update_elem %d\n", i);
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ prog = bpf_object__find_program_by_title(obj, "xdp_sock");
+ prog_fd = bpf_program__fd(prog);
+ if (prog_fd < 0) {
+ fprintf(stderr, "ERROR: no xdp_sock program found: %s\n",
+ strerror(prog_fd));
+ exit(EXIT_FAILURE);
+ }
+
if (bpf_set_link_xdp_fd(opt_ifindex, prog_fd, opt_xdp_flags) < 0) {
fprintf(stderr, "ERROR: link set xdp fd failed\n");
exit(EXIT_FAILURE);
}
+}
- ret = bpf_map_update_elem(qidconf_map, &key, &opt_queue, 0);
- if (ret) {
- fprintf(stderr, "ERROR: bpf_map_update_elem qidconf\n");
+int main(int argc, char **argv)
+{
+ struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
+ pthread_t pt;
+ int ret;
+
+ signal(SIGINT, int_exit);
+ signal(SIGTERM, int_exit);
+ signal(SIGABRT, int_exit);
+
+ parse_command_line(argc, argv);
+
+ if (setrlimit(RLIMIT_MEMLOCK, &r)) {
+ fprintf(stderr, "ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n",
+ strerror(errno));
exit(EXIT_FAILURE);
}
@@ -964,19 +985,7 @@ int main(int argc, char **argv)
xsks[num_socks++] = xsk_configure(xsks[0]->umem);
#endif
- /* ...and insert them into the map. */
- for (i = 0; i < num_socks; i++) {
- key = i;
- ret = bpf_map_update_elem(xsks_map, &key, &xsks[i]->sfd, 0);
- if (ret) {
- fprintf(stderr, "ERROR: bpf_map_update_elem %d\n", i);
- exit(EXIT_FAILURE);
- }
- }
-
- signal(SIGINT, int_exit);
- signal(SIGTERM, int_exit);
- signal(SIGABRT, int_exit);
+ setup_xdp_xskmap(argv[0]);
setlocale(LC_ALL, "");
--
2.19.1
Powered by blists - more mailing lists