From 9899694a7f67714216665b87318eb367e2c5c901 Mon Sep 17 00:00:00 2001 From: Joe Stringer Date: Thu, 8 Dec 2016 18:46:20 -0800 Subject: samples/bpf: Move open_raw_sock to separate header This function was declared in libbpf.c and was the only remaining function in this library, but has nothing to do with BPF. Shift it out into a new header, sock_example.h, and include it from the relevant samples. Signed-off-by: Joe Stringer Cc: Alexei Starovoitov Cc: Daniel Borkmann Cc: Wang Nan Link: http://lkml.kernel.org/r/20161209024620.31660-8-joe@ovn.org Signed-off-by: Arnaldo Carvalho de Melo --- samples/bpf/Makefile | 2 +- samples/bpf/fds_example.c | 1 + samples/bpf/libbpf.c | 36 ------------------------------------ samples/bpf/libbpf.h | 3 --- samples/bpf/sock_example.c | 1 + samples/bpf/sock_example.h | 35 +++++++++++++++++++++++++++++++++++ samples/bpf/sockex1_user.c | 1 + samples/bpf/sockex2_user.c | 1 + samples/bpf/sockex3_user.c | 1 + 9 files changed, 41 insertions(+), 40 deletions(-) delete mode 100644 samples/bpf/libbpf.c create mode 100644 samples/bpf/sock_example.h (limited to 'samples') diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index 5a73f5a7ace1..f01b66f277b0 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile @@ -36,7 +36,7 @@ hostprogs-y += lwt_len_hist hostprogs-y += xdp_tx_iptunnel # Libbpf dependencies -LIBBPF := libbpf.o ../../tools/lib/bpf/bpf.o +LIBBPF := ../../tools/lib/bpf/bpf.o test_lru_dist-objs := test_lru_dist.o $(LIBBPF) sock_example-objs := sock_example.o $(LIBBPF) diff --git a/samples/bpf/fds_example.c b/samples/bpf/fds_example.c index a5cddc99cccd..e29bd52ff9e8 100644 --- a/samples/bpf/fds_example.c +++ b/samples/bpf/fds_example.c @@ -14,6 +14,7 @@ #include "bpf_load.h" #include "libbpf.h" +#include "sock_example.h" #define BPF_F_PIN (1 << 0) #define BPF_F_GET (1 << 1) diff --git a/samples/bpf/libbpf.c b/samples/bpf/libbpf.c deleted file mode 100644 index bee473a494f1..000000000000 --- a/samples/bpf/libbpf.c +++ /dev/null @@ -1,36 +0,0 @@ -/* eBPF mini library */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "libbpf.h" - -int open_raw_sock(const char *name) -{ - struct sockaddr_ll sll; - int sock; - - sock = socket(PF_PACKET, SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC, htons(ETH_P_ALL)); - if (sock < 0) { - printf("cannot create raw socket\n"); - return -1; - } - - memset(&sll, 0, sizeof(sll)); - sll.sll_family = AF_PACKET; - sll.sll_ifindex = if_nametoindex(name); - sll.sll_protocol = htons(ETH_P_ALL); - if (bind(sock, (struct sockaddr *)&sll, sizeof(sll)) < 0) { - printf("bind to %s: %s\n", name, strerror(errno)); - close(sock); - return -1; - } - - return sock; -} diff --git a/samples/bpf/libbpf.h b/samples/bpf/libbpf.h index 09aedc320009..3705fba453a0 100644 --- a/samples/bpf/libbpf.h +++ b/samples/bpf/libbpf.h @@ -185,7 +185,4 @@ struct bpf_insn; .off = 0, \ .imm = 0 }) -/* create RAW socket and bind to interface 'name' */ -int open_raw_sock(const char *name); - #endif diff --git a/samples/bpf/sock_example.c b/samples/bpf/sock_example.c index 5546f8aac37e..6fc6e193ef1b 100644 --- a/samples/bpf/sock_example.c +++ b/samples/bpf/sock_example.c @@ -27,6 +27,7 @@ #include #include #include "libbpf.h" +#include "sock_example.h" char bpf_log_buf[BPF_LOG_BUF_SIZE]; diff --git a/samples/bpf/sock_example.h b/samples/bpf/sock_example.h new file mode 100644 index 000000000000..09f7fe7e5fd7 --- /dev/null +++ b/samples/bpf/sock_example.h @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "libbpf.h" + +static inline int open_raw_sock(const char *name) +{ + struct sockaddr_ll sll; + int sock; + + sock = socket(PF_PACKET, SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC, htons(ETH_P_ALL)); + if (sock < 0) { + printf("cannot create raw socket\n"); + return -1; + } + + memset(&sll, 0, sizeof(sll)); + sll.sll_family = AF_PACKET; + sll.sll_ifindex = if_nametoindex(name); + sll.sll_protocol = htons(ETH_P_ALL); + if (bind(sock, (struct sockaddr *)&sll, sizeof(sll)) < 0) { + printf("bind to %s: %s\n", name, strerror(errno)); + close(sock); + return -1; + } + + return sock; +} diff --git a/samples/bpf/sockex1_user.c b/samples/bpf/sockex1_user.c index 9454448bf198..6cd2feb3e9b3 100644 --- a/samples/bpf/sockex1_user.c +++ b/samples/bpf/sockex1_user.c @@ -3,6 +3,7 @@ #include #include "libbpf.h" #include "bpf_load.h" +#include "sock_example.h" #include #include diff --git a/samples/bpf/sockex2_user.c b/samples/bpf/sockex2_user.c index 6a40600d5a83..0e0207c90841 100644 --- a/samples/bpf/sockex2_user.c +++ b/samples/bpf/sockex2_user.c @@ -3,6 +3,7 @@ #include #include "libbpf.h" #include "bpf_load.h" +#include "sock_example.h" #include #include #include diff --git a/samples/bpf/sockex3_user.c b/samples/bpf/sockex3_user.c index 9099c4255f23..b5524d417eb5 100644 --- a/samples/bpf/sockex3_user.c +++ b/samples/bpf/sockex3_user.c @@ -3,6 +3,7 @@ #include #include "libbpf.h" #include "bpf_load.h" +#include "sock_example.h" #include #include #include -- cgit v1.2.3