History

samples/bpf: adapted to new uapi

Here, the xdpsock sample application is adjusted to the new descriptor format.

Björn Töpel committed 8 years ago

Commit a412ef51 parent bbff2f31 file · +36 −48Browse files

Unified · Split

samples/bpf/xdpsock_user.c+36−48
@@ -46,6 +46,7 @@ · 45 unmodified lines
4646
4747 #define NUM_FRAMES 131072
4848 #define FRAME_HEADROOM 0
49+#define FRAME_SHIFT 11
4950 #define FRAME_SIZE 2048
5051 #define NUM_DESCS 1024
5152 #define BATCH_SIZE 16
@@ -55,6 +56,7 @@ · 3 unmodified lines
5556
5657 #define DEBUG_HEXDUMP 0
5758
59+typedef __u64 u64;
5860 typedef __u32 u32;
5961
6062 static unsigned long prev_time;
@@ -81,12 +83,12 @@ struct xdp_umem_uqueue { · 20 unmodified lines
8183 u32 size;
8284 u32 *producer;
8385 u32 *consumer;
84− u32 *ring;
86+ u64 *ring;
8587 void *map;
8688 };
8789
8890 struct xdp_umem {
89− char (*frames)[FRAME_SIZE];
91+ char *frames;
9092 struct xdp_umem_uqueue fq;
9193 struct xdp_umem_uqueue cq;
9294 int fd;
@@ -214,7 +216,7 @@ static inline int umem_fill_to_kernel_ex(struct xdp_umem_uqueue *fq, · 121 unmodified lines
214216 for (i = 0; i < nb; i++) {
215217 u32 idx = fq->cached_prod++ & fq->mask;
216218
217− fq->ring[idx] = d[i].idx;
219+ fq->ring[idx] = d[i].addr;
218220 }
219221
220222 u_smp_wmb();
@@ -224,7 +226,7 @@ static inline int umem_fill_to_kernel_ex(struct xdp_umem_uqueue *fq, · 3 unmodified lines
224226 return 0;
225227 }
226228
227−static inline int umem_fill_to_kernel(struct xdp_umem_uqueue *fq, u32 *d,
229+static inline int umem_fill_to_kernel(struct xdp_umem_uqueue *fq, u64 *d,
228230 size_t nb)
229231 {
230232 u32 i;
@@ -246,7 +248,7 @@ static inline int umem_fill_to_kernel(struct xdp_umem_uqueue *fq, u32 *d, · 15 unmodified lines
246248 }
247249
248250 static inline size_t umem_complete_from_kernel(struct xdp_umem_uqueue *cq,
249− u32 *d, size_t nb)
251+ u64 *d, size_t nb)
250252 {
251253 u32 idx, i, entries = umem_nb_avail(cq, nb);
252254
@@ -266,10 +268,9 @@ static inline size_t umem_complete_from_kernel(struct xdp_umem_uqueue *cq, · 13 unmodified lines
266268 return entries;
267269 }
268270
269−static inline void *xq_get_data(struct xdpsock *xsk, __u32 idx, __u32 off)
271+static inline void *xq_get_data(struct xdpsock *xsk, u64 addr)
270272 {
271− lassert(idx < NUM_FRAMES);
272− return &xsk->umem->frames[idx][off];
273+ return &xsk->umem->frames[addr];
273274 }
274275
275276 static inline int xq_enq(struct xdp_uqueue *uq,
@@ -285,9 +286,8 @@ static inline int xq_enq(struct xdp_uqueue *uq, · 9 unmodified lines
285286 for (i = 0; i < ndescs; i++) {
286287 u32 idx = uq->cached_prod++ & uq->mask;
287288
288− r[idx].idx = descs[i].idx;
289+ r[idx].addr = descs[i].addr;
289290 r[idx].len = descs[i].len;
290− r[idx].offset = descs[i].offset;
291291 }
292292
293293 u_smp_wmb();
@@ -297,7 +297,7 @@ static inline int xq_enq(struct xdp_uqueue *uq, · 3 unmodified lines
297297 }
298298
299299 static inline int xq_enq_tx_only(struct xdp_uqueue *uq,
300− __u32 idx, unsigned int ndescs)
300+ unsigned int id, unsigned int ndescs)
301301 {
302302 struct xdp_desc *r = uq->ring;
303303 unsigned int i;
@@ -308,9 +308,8 @@ static inline int xq_enq_tx_only(struct xdp_uqueue *uq, · 4 unmodified lines
308308 for (i = 0; i < ndescs; i++) {
309309 u32 idx = uq->cached_prod++ & uq->mask;
310310
311− r[idx].idx = idx + i;
311+ r[idx].addr = (id + i) << FRAME_SHIFT;
312312 r[idx].len = sizeof(pkt_data) - 1;
313− r[idx].offset = 0;
314313 }
315314
316315 u_smp_wmb();
@@ -357,17 +356,21 @@ static void swap_mac_addresses(void *data) · 40 unmodified lines
357356 *dst_addr = tmp;
358357 }
359358
360−#if DEBUG_HEXDUMP
361−static void hex_dump(void *pkt, size_t length, const char *prefix)
359+static void hex_dump(void *pkt, size_t length, u64 addr)
362360 {
363− int i = 0;
364361 const unsigned char *address = (unsigned char *)pkt;
365362 const unsigned char *line = address;
366363 size_t line_size = 32;
367364 unsigned char c;
365+ char buf[32];
366+ int i = 0;
368367
368+ if (!DEBUG_HEXDUMP)
369+ return;
370+
371+ sprintf(buf, "addr=%llu", addr);
369372 printf("length = %zu\n", length);
370− printf("%s | ", prefix);
373+ printf("%s | ", buf);
371374 while (length-- > 0) {
372375 printf("%02X ", *address++);
373376 if (!(++i % line_size) || (length == 0 && i % line_size)) {
@@ -382,12 +385,11 @@ static void hex_dump(void *pkt, size_t length, const char *prefix) · 8 unmodified lines
382385 }
383386 printf("\n");
384387 if (length > 0)
385− printf("%s | ", prefix);
388+ printf("%s | ", buf);
386389 }
387390 }
388391 printf("\n");
389392 }
390−#endif
391393
392394 static size_t gen_eth_frame(char *frame)
393395 {
@@ -412,8 +414,8 @@ static struct xdp_umem *xdp_umem_configure(int sfd) · 18 unmodified lines
412414
413415 mr.addr = (__u64)bufs;
414416 mr.len = NUM_FRAMES * FRAME_SIZE;
415− mr.frame_size = FRAME_SIZE;
416− mr.frame_headroom = FRAME_HEADROOM;
417+ mr.chunk_size = FRAME_SIZE;
418+ mr.headroom = FRAME_HEADROOM;
417419
418420 lassert(setsockopt(sfd, SOL_XDP, XDP_UMEM_REG, &mr, sizeof(mr)) == 0);
419421 lassert(setsockopt(sfd, SOL_XDP, XDP_UMEM_FILL_RING, &fq_size,
@@ -426,7 +428,7 @@ static struct xdp_umem *xdp_umem_configure(int sfd) · 6 unmodified lines
426428 &optlen) == 0);
427429
428430 umem->fq.map = mmap(0, off.fr.desc +
429− FQ_NUM_DESCS * sizeof(u32),
431+ FQ_NUM_DESCS * sizeof(u64),
430432 PROT_READ | PROT_WRITE,
431433 MAP_SHARED | MAP_POPULATE, sfd,
432434 XDP_UMEM_PGOFF_FILL_RING);
@@ -439,7 +441,7 @@ static struct xdp_umem *xdp_umem_configure(int sfd) · 6 unmodified lines
439441 umem->fq.ring = umem->fq.map + off.fr.desc;
440442
441443 umem->cq.map = mmap(0, off.cr.desc +
442− CQ_NUM_DESCS * sizeof(u32),
444+ CQ_NUM_DESCS * sizeof(u64),
443445 PROT_READ | PROT_WRITE,
444446 MAP_SHARED | MAP_POPULATE, sfd,
445447 XDP_UMEM_PGOFF_COMPLETION_RING);
@@ -451,14 +453,14 @@ static struct xdp_umem *xdp_umem_configure(int sfd) · 5 unmodified lines
451453 umem->cq.consumer = umem->cq.map + off.cr.consumer;
452454 umem->cq.ring = umem->cq.map + off.cr.desc;
453455
454− umem->frames = (char (*)[FRAME_SIZE])bufs;
456+ umem->frames = bufs;
455457 umem->fd = sfd;
456458
457459 if (opt_bench == BENCH_TXONLY) {
458460 int i;
459461
460− for (i = 0; i < NUM_FRAMES; i++)
461− (void)gen_eth_frame(&umem->frames[i][0]);
462+ for (i = 0; i < NUM_FRAMES * FRAME_SIZE; i += FRAME_SIZE)
463+ (void)gen_eth_frame(&umem->frames[i]);
462464 }
463465
464466 return umem;
@@ -472,7 +474,7 @@ static struct xdpsock *xsk_configure(struct xdp_umem *umem) · 7 unmodified lines
472474 struct xdpsock *xsk;
473475 bool shared = true;
474476 socklen_t optlen;
475− u32 i;
477+ u64 i;
476478
477479 sfd = socket(PF_XDP, SOCK_RAW, 0);
478480 lassert(sfd >= 0);
@@ -508,7 +510,7 @@ static struct xdpsock *xsk_configure(struct xdp_umem *umem) · 29 unmodified lines
508510 lassert(xsk->rx.map != MAP_FAILED);
509511
510512 if (!shared) {
511− for (i = 0; i < NUM_DESCS / 2; i++)
513+ for (i = 0; i < NUM_DESCS * FRAME_SIZE; i += FRAME_SIZE)
512514 lassert(umem_fill_to_kernel(&xsk->umem->fq, &i, 1)
513515 == 0);
514516 }
@@ -727,7 +729,7 @@ static void kick_tx(int fd) · 212 unmodified lines
727729
728730 static inline void complete_tx_l2fwd(struct xdpsock *xsk)
729731 {
730− u32 descs[BATCH_SIZE];
732+ u64 descs[BATCH_SIZE];
731733 unsigned int rcvd;
732734 size_t ndescs;
733735
@@ -749,7 +751,7 @@ static inline void complete_tx_l2fwd(struct xdpsock *xsk) · 15 unmodified lines
749751
750752 static inline void complete_tx_only(struct xdpsock *xsk)
751753 {
752− u32 descs[BATCH_SIZE];
754+ u64 descs[BATCH_SIZE];
753755 unsigned int rcvd;
754756
755757 if (!xsk->outstanding_tx)
@@ -774,17 +776,9 @@ static void rx_drop(struct xdpsock *xsk) · 18 unmodified lines
774776 return;
775777
776778 for (i = 0; i < rcvd; i++) {
777− u32 idx = descs[i].idx;
778−
779− lassert(idx < NUM_FRAMES);
780−#if DEBUG_HEXDUMP
781− char *pkt;
782− char buf[32];
779+ char *pkt = xq_get_data(xsk, descs[i].addr);
783780
784− pkt = xq_get_data(xsk, idx, descs[i].offset);
785− sprintf(buf, "idx=%d", idx);
786− hex_dump(pkt, descs[i].len, buf);
787−#endif
781+ hex_dump(pkt, descs[i].len, descs[i].addr);
788782 }
789783
790784 xsk->rx_npkts += rcvd;
@@ -867,17 +861,11 @@ static void l2fwd(struct xdpsock *xsk) · 76 unmodified lines
867861 }
868862
869863 for (i = 0; i < rcvd; i++) {
870− char *pkt = xq_get_data(xsk, descs[i].idx,
871− descs[i].offset);
864+ char *pkt = xq_get_data(xsk, descs[i].addr);
872865
873866 swap_mac_addresses(pkt);
874−#if DEBUG_HEXDUMP
875− char buf[32];
876− u32 idx = descs[i].idx;
877867
878− sprintf(buf, "idx=%d", idx);
879− hex_dump(pkt, descs[i].len, buf);
880−#endif
868+ hex_dump(pkt, descs[i].len, descs[i].addr);
881869 }
882870
883871 xsk->rx_npkts += rcvd;