From mboxrd@z Thu Jan 1 00:00:00 1970 From: Song Liu Subject: Re: [PATCH] samples/bpf: Add xdp_sample_pkts example Date: Wed, 30 May 2018 22:03:41 -0700 Message-ID: References: <20180530164524.21760-1-toke@toke.dk> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Cc: Networking To: =?UTF-8?B?VG9rZSBIw7hpbGFuZC1Kw7hyZ2Vuc2Vu?= Return-path: Received: from mail-qk0-f194.google.com ([209.85.220.194]:34875 "EHLO mail-qk0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753906AbeEaFDm (ORCPT ); Thu, 31 May 2018 01:03:42 -0400 Received: by mail-qk0-f194.google.com with SMTP id d130-v6so10906014qkc.2 for ; Wed, 30 May 2018 22:03:42 -0700 (PDT) In-Reply-To: <20180530164524.21760-1-toke@toke.dk> Sender: netdev-owner@vger.kernel.org List-ID: On Wed, May 30, 2018 at 9:45 AM, Toke H=C3=B8iland-J=C3=B8rgensen wrote: > This adds an example program showing how to sample packets from XDP using > the perf event buffer. The example userspace program just prints the > ethernet header for every packet sampled. > > Most of the userspace code is borrowed from other examples, most notably > trace_output. > > Note that the example only works when everything runs on CPU0; so > suitable smp_affinity needs to be set on the device. Some drivers seem > to reset smp_affinity when loading an XDP program, so it may be > necessary to change it after starting the example userspace program. Why does this only works when everything runs on CPU0? Is this something we can improve? Thanks, Song > > Signed-off-by: Toke H=C3=B8iland-J=C3=B8rgensen > --- > samples/bpf/Makefile | 4 + > samples/bpf/xdp_sample_pkts_kern.c | 48 ++++++++++++ > samples/bpf/xdp_sample_pkts_user.c | 147 +++++++++++++++++++++++++++++++= ++++++ > 3 files changed, 199 insertions(+) > create mode 100644 samples/bpf/xdp_sample_pkts_kern.c > create mode 100644 samples/bpf/xdp_sample_pkts_user.c > > diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile > index 1303af1..6f0c6d2 100644 > --- a/samples/bpf/Makefile > +++ b/samples/bpf/Makefile > @@ -52,6 +52,7 @@ hostprogs-y +=3D xdp_adjust_tail > hostprogs-y +=3D xdpsock > hostprogs-y +=3D xdp_fwd > hostprogs-y +=3D task_fd_query > +hostprogs-y +=3D xdp_sample_pkts > > # Libbpf dependencies > LIBBPF =3D $(TOOLS_PATH)/lib/bpf/libbpf.a > @@ -107,6 +108,7 @@ xdp_adjust_tail-objs :=3D xdp_adjust_tail_user.o > xdpsock-objs :=3D bpf_load.o xdpsock_user.o > xdp_fwd-objs :=3D bpf_load.o xdp_fwd_user.o > task_fd_query-objs :=3D bpf_load.o task_fd_query_user.o $(TRACE_HELPERS) > +xdp_sample_pkts-objs :=3D bpf_load.o xdp_sample_pkts_user.o $(TRACE_HELP= ERS) > > # Tell kbuild to always build the programs > always :=3D $(hostprogs-y) > @@ -163,6 +165,7 @@ always +=3D xdp_adjust_tail_kern.o > always +=3D xdpsock_kern.o > always +=3D xdp_fwd_kern.o > always +=3D task_fd_query_kern.o > +always +=3D xdp_sample_pkts_kern.o > > HOSTCFLAGS +=3D -I$(objtree)/usr/include > HOSTCFLAGS +=3D -I$(srctree)/tools/lib/ > @@ -179,6 +182,7 @@ HOSTCFLAGS_spintest_user.o +=3D -I$(srctree)/tools/li= b/bpf/ > HOSTCFLAGS_trace_event_user.o +=3D -I$(srctree)/tools/lib/bpf/ > HOSTCFLAGS_sampleip_user.o +=3D -I$(srctree)/tools/lib/bpf/ > HOSTCFLAGS_task_fd_query_user.o +=3D -I$(srctree)/tools/lib/bpf/ > +HOSTCFLAGS_xdp_sample_pkts_user.o +=3D -I$(srctree)/tools/lib/bpf/ > > HOST_LOADLIBES +=3D $(LIBBPF) -lelf > HOSTLOADLIBES_tracex4 +=3D -lrt > diff --git a/samples/bpf/xdp_sample_pkts_kern.c b/samples/bpf/xdp_sample_= pkts_kern.c > new file mode 100644 > index 0000000..c58183a > --- /dev/null > +++ b/samples/bpf/xdp_sample_pkts_kern.c > @@ -0,0 +1,48 @@ > +#include > +#include > +#include > +#include "bpf_helpers.h" > + > +#define SAMPLE_SIZE 64ul > + > +struct bpf_map_def SEC("maps") my_map =3D { > + .type =3D BPF_MAP_TYPE_PERF_EVENT_ARRAY, > + .key_size =3D sizeof(int), > + .value_size =3D sizeof(u32), > + .max_entries =3D 2, > +}; > + > +SEC("xdp_sample") > +int xdp_sample_prog(struct xdp_md *ctx) > +{ > + void *data_end =3D (void *)(long)ctx->data_end; > + void *data =3D (void *)(long)ctx->data; > + > + /* Metadata will be in the perf event before the packet data. */ > + struct S { > + u16 cookie; > + u16 pkt_len; > + } __attribute__((packed)) metadata; > + > + if (data + SAMPLE_SIZE < data_end) { > + /* The XDP perf_event_output handler will use the upper 3= 2 bits > + * of the flags argument as a number of bytes to include = of the > + * packet payload in the event data. If the size is too b= ig, the > + * call to bpf_perf_event_output will fail and return -EF= AULT. > + * > + * See bpf_xdp_event_output in net/core/filter.c. > + */ > + u64 flags =3D SAMPLE_SIZE << 32; > + > + metadata.cookie =3D 0xdead; > + metadata.pkt_len =3D (u16)(data_end - data); > + > + bpf_perf_event_output(ctx, &my_map, flags, > + &metadata, sizeof(metadata)); > + } > + > + return XDP_PASS; > +} > + > +char _license[] SEC("license") =3D "GPL"; > +u32 _version SEC("version") =3D LINUX_VERSION_CODE; > diff --git a/samples/bpf/xdp_sample_pkts_user.c b/samples/bpf/xdp_sample_= pkts_user.c > new file mode 100644 > index 0000000..f996917 > --- /dev/null > +++ b/samples/bpf/xdp_sample_pkts_user.c > @@ -0,0 +1,147 @@ > +/* This program is free software; you can redistribute it and/or > + * modify it under the terms of version 2 of the GNU General Public > + * License as published by the Free Software Foundation. > + */ > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include "bpf_load.h" > +#include "bpf_util.h" > +#include > + > +#include "perf-sys.h" > +#include "trace_helpers.h" > + > +static int pmu_fd, if_idx =3D 0; > +static char *if_name; > + > +static int do_attach(int idx, int fd, const char *name) > +{ > + int err; > + > + err =3D bpf_set_link_xdp_fd(idx, fd, 0); > + if (err < 0) > + printf("ERROR: failed to attach program to %s\n", name); > + > + return err; > +} > + > +static int do_detach(int idx, const char *name) > +{ > + int err; > + > + err =3D bpf_set_link_xdp_fd(idx, -1, 0); > + if (err < 0) > + printf("ERROR: failed to detach program from %s\n", name)= ; > + > + return err; > +} > + > +#define SAMPLE_SIZE 64 > + > +static int print_bpf_output(void *data, int size) > +{ > + struct { > + __u16 cookie; > + __u16 pkt_len; > + __u8 pkt_data[SAMPLE_SIZE]; > + } __attribute__((packed)) *e =3D data; > + int i; > + > + if (e->cookie !=3D 0xdead) { > + printf("BUG cookie %x sized %d\n", > + e->cookie, size); > + return LIBBPF_PERF_EVENT_ERROR; > + } > + > + printf("Pkt len: %-5d bytes. Ethernet hdr: ", e->pkt_len); > + for (i =3D 0; i < 14 && i < e->pkt_len; i++) > + printf("%02x ", e->pkt_data[i]); > + printf("\n"); > + > + return LIBBPF_PERF_EVENT_CONT; > +} > + > +static void test_bpf_perf_event(void) > +{ > + struct perf_event_attr attr =3D { > + .sample_type =3D PERF_SAMPLE_RAW, > + .type =3D PERF_TYPE_SOFTWARE, > + .config =3D PERF_COUNT_SW_BPF_OUTPUT, > + }; > + int key =3D 0; > + > + pmu_fd =3D sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*gr= oup_fd*/, 0); > + > + assert(pmu_fd >=3D 0); > + assert(bpf_map_update_elem(map_fd[0], &key, &pmu_fd, BPF_ANY) =3D= =3D 0); > + ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0); > +} > + > +static void sig_handler(int signo) > +{ > + do_detach(if_idx, if_name); > + exit(0); > +} > + > +int main(int argc, char **argv) > +{ > + char filename[256]; > + int ret, err; > + > + if (argc < 2) { > + printf("Usage: %s \n", argv[0]); > + return 1; > + } > + > + snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); > + > + if (load_bpf_file(filename)) { > + printf("%s", bpf_log_buf); > + return 1; > + } > + > + if_idx =3D if_nametoindex(argv[1]); > + if (!if_idx) > + if_idx =3D strtoul(argv[1], NULL, 0); > + > + if (!if_idx) { > + fprintf(stderr, "Invalid ifname\n"); > + return 1; > + } > + if_name =3D argv[1]; > + err =3D do_attach(if_idx, prog_fd[0], argv[1]); > + if (err) > + return err; > + > + if (signal(SIGINT, sig_handler) || > + signal(SIGHUP, sig_handler) || > + signal(SIGTERM, sig_handler)) { > + perror("signal"); > + return 1; > + } > + > + test_bpf_perf_event(); > + > + if (perf_event_mmap(pmu_fd) < 0) > + return 1; > + > + ret =3D perf_event_poller(pmu_fd, print_bpf_output); > + kill(0, SIGINT); > + return ret; > +} > -- > 2.7.4 >