1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2020 Intel Corporation. */
13 #include <sys/resource.h>
14 #include <sys/socket.h>
15 #include <sys/types.h>
19 #include <netinet/ether.h>
22 #include <linux/bpf.h>
23 #include <linux/if_link.h>
24 #include <linux/if_xdp.h>
26 #include <bpf/libbpf.h>
30 /* libbpf APIs for AF_XDP are deprecated starting from v0.7 */
31 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
33 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
40 /* This program illustrates the packet forwarding between multiple AF_XDP
41 * sockets in multi-threaded environment. All threads are sharing a common
42 * buffer pool, with each socket having its own private buffer cache.
44 * Example 1: Single thread handling two sockets. The packets received by socket
45 * A (interface IFA, queue QA) are forwarded to socket B (interface IFB, queue
46 * QB), while the packets received by socket B are forwarded to socket A. The
47 * thread is running on CPU core X:
49 * ./xsk_fwd -i IFA -q QA -i IFB -q QB -c X
51 * Example 2: Two threads, each handling two sockets. The thread running on CPU
52 * core X forwards all the packets received by socket A to socket B, and all the
53 * packets received by socket B to socket A. The thread running on CPU core Y is
54 * performing the same packet forwarding between sockets C and D:
56 * ./xsk_fwd -i IFA -q QA -i IFB -q QB -i IFC -q QC -i IFD -q QD
61 * Buffer pool and buffer cache
63 * For packet forwarding, the packet buffers are typically allocated from the
64 * pool for packet reception and freed back to the pool for further reuse once
65 * the packet transmission is completed.
67 * The buffer pool is shared between multiple threads. In order to minimize the
68 * access latency to the shared buffer pool, each thread creates one (or
69 * several) buffer caches, which, unlike the buffer pool, are private to the
70 * thread that creates them and therefore cannot be shared with other threads.
71 * The access to the shared pool is only needed either (A) when the cache gets
72 * empty due to repeated buffer allocations and it needs to be replenished from
73 * the pool, or (B) when the cache gets full due to repeated buffer free and it
74 * needs to be flushed back to the pull.
76 * In a packet forwarding system, a packet received on any input port can
77 * potentially be transmitted on any output port, depending on the forwarding
78 * configuration. For AF_XDP sockets, for this to work with zero-copy of the
79 * packet buffers when, it is required that the buffer pool memory fits into the
80 * UMEM area shared by all the sockets.
89 u32 n_buffers_per_slab;
92 /* This buffer pool implementation organizes the buffers into equally sized
93 * slabs of *n_buffers_per_slab*. Initially, there are *n_slabs* slabs in the
94 * pool that are completely filled with buffer pointers (full slabs).
96 * Each buffer cache has a slab for buffer allocation and a slab for buffer
97 * free, with both of these slabs initially empty. When the cache's allocation
98 * slab goes empty, it is swapped with one of the available full slabs from the
99 * pool, if any is available. When the cache's free slab goes full, it is
100 * swapped for one of the empty slabs from the pool, which is guaranteed to
103 * Partially filled slabs never get traded between the cache and the pool
104 * (except when the cache itself is destroyed), which enables fast operation
105 * through pointer swapping.
108 struct bpool_params params;
109 pthread_mutex_t lock;
113 u64 **slabs_reserved;
115 u64 *buffers_reserved;
118 u64 n_slabs_reserved;
121 u64 n_slabs_available;
122 u64 n_slabs_reserved_available;
124 struct xsk_umem_config umem_cfg;
125 struct xsk_ring_prod umem_fq;
126 struct xsk_ring_cons umem_cq;
127 struct xsk_umem *umem;
130 static struct bpool *
131 bpool_init(struct bpool_params *params,
132 struct xsk_umem_config *umem_cfg)
134 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
135 u64 n_slabs, n_slabs_reserved, n_buffers, n_buffers_reserved;
136 u64 slabs_size, slabs_reserved_size;
137 u64 buffers_size, buffers_reserved_size;
144 if (setrlimit(RLIMIT_MEMLOCK, &r))
147 /* bpool internals dimensioning. */
148 n_slabs = (params->n_buffers + params->n_buffers_per_slab - 1) /
149 params->n_buffers_per_slab;
150 n_slabs_reserved = params->n_users_max * 2;
151 n_buffers = n_slabs * params->n_buffers_per_slab;
152 n_buffers_reserved = n_slabs_reserved * params->n_buffers_per_slab;
154 slabs_size = n_slabs * sizeof(u64 *);
155 slabs_reserved_size = n_slabs_reserved * sizeof(u64 *);
156 buffers_size = n_buffers * sizeof(u64);
157 buffers_reserved_size = n_buffers_reserved * sizeof(u64);
159 total_size = sizeof(struct bpool) +
160 slabs_size + slabs_reserved_size +
161 buffers_size + buffers_reserved_size;
163 /* bpool memory allocation. */
164 p = calloc(total_size, sizeof(u8));
168 /* bpool memory initialization. */
169 bp = (struct bpool *)p;
170 memcpy(&bp->params, params, sizeof(*params));
171 bp->params.n_buffers = n_buffers;
173 bp->slabs = (u64 **)&p[sizeof(struct bpool)];
174 bp->slabs_reserved = (u64 **)&p[sizeof(struct bpool) +
176 bp->buffers = (u64 *)&p[sizeof(struct bpool) +
177 slabs_size + slabs_reserved_size];
178 bp->buffers_reserved = (u64 *)&p[sizeof(struct bpool) +
179 slabs_size + slabs_reserved_size + buffers_size];
181 bp->n_slabs = n_slabs;
182 bp->n_slabs_reserved = n_slabs_reserved;
183 bp->n_buffers = n_buffers;
185 for (i = 0; i < n_slabs; i++)
186 bp->slabs[i] = &bp->buffers[i * params->n_buffers_per_slab];
187 bp->n_slabs_available = n_slabs;
189 for (i = 0; i < n_slabs_reserved; i++)
190 bp->slabs_reserved[i] = &bp->buffers_reserved[i *
191 params->n_buffers_per_slab];
192 bp->n_slabs_reserved_available = n_slabs_reserved;
194 for (i = 0; i < n_buffers; i++)
195 bp->buffers[i] = i * params->buffer_size;
198 status = pthread_mutex_init(&bp->lock, NULL);
205 bp->addr = mmap(NULL,
206 n_buffers * params->buffer_size,
207 PROT_READ | PROT_WRITE,
208 MAP_PRIVATE | MAP_ANONYMOUS | params->mmap_flags,
211 if (bp->addr == MAP_FAILED) {
212 pthread_mutex_destroy(&bp->lock);
218 status = xsk_umem__create(&bp->umem,
220 bp->params.n_buffers * bp->params.buffer_size,
225 munmap(bp->addr, bp->params.n_buffers * bp->params.buffer_size);
226 pthread_mutex_destroy(&bp->lock);
230 memcpy(&bp->umem_cfg, umem_cfg, sizeof(*umem_cfg));
236 bpool_free(struct bpool *bp)
241 xsk_umem__delete(bp->umem);
242 munmap(bp->addr, bp->params.n_buffers * bp->params.buffer_size);
243 pthread_mutex_destroy(&bp->lock);
258 bcache_slab_size(struct bcache *bc)
260 struct bpool *bp = bc->bp;
262 return bp->params.n_buffers_per_slab;
265 static struct bcache *
266 bcache_init(struct bpool *bp)
270 bc = calloc(1, sizeof(struct bcache));
275 bc->n_buffers_cons = 0;
276 bc->n_buffers_prod = 0;
278 pthread_mutex_lock(&bp->lock);
279 if (bp->n_slabs_reserved_available == 0) {
280 pthread_mutex_unlock(&bp->lock);
285 bc->slab_cons = bp->slabs_reserved[bp->n_slabs_reserved_available - 1];
286 bc->slab_prod = bp->slabs_reserved[bp->n_slabs_reserved_available - 2];
287 bp->n_slabs_reserved_available -= 2;
288 pthread_mutex_unlock(&bp->lock);
294 bcache_free(struct bcache *bc)
301 /* In order to keep this example simple, the case of freeing any
302 * existing buffers from the cache back to the pool is ignored.
306 pthread_mutex_lock(&bp->lock);
307 bp->slabs_reserved[bp->n_slabs_reserved_available] = bc->slab_prod;
308 bp->slabs_reserved[bp->n_slabs_reserved_available + 1] = bc->slab_cons;
309 bp->n_slabs_reserved_available += 2;
310 pthread_mutex_unlock(&bp->lock);
315 /* To work correctly, the implementation requires that the *n_buffers* input
316 * argument is never greater than the buffer pool's *n_buffers_per_slab*. This
317 * is typically the case, with one exception taking place when large number of
318 * buffers are allocated at init time (e.g. for the UMEM fill queue setup).
321 bcache_cons_check(struct bcache *bc, u32 n_buffers)
323 struct bpool *bp = bc->bp;
324 u64 n_buffers_per_slab = bp->params.n_buffers_per_slab;
325 u64 n_buffers_cons = bc->n_buffers_cons;
326 u64 n_slabs_available;
330 * Consumer slab is not empty: Use what's available locally. Do not
331 * look for more buffers from the pool when the ask can only be
332 * partially satisfied.
335 return (n_buffers_cons < n_buffers) ?
340 * Consumer slab is empty: look to trade the current consumer slab
341 * (full) for a full slab from the pool, if any is available.
343 pthread_mutex_lock(&bp->lock);
344 n_slabs_available = bp->n_slabs_available;
345 if (!n_slabs_available) {
346 pthread_mutex_unlock(&bp->lock);
351 slab_full = bp->slabs[n_slabs_available];
352 bp->slabs[n_slabs_available] = bc->slab_cons;
353 bp->n_slabs_available = n_slabs_available;
354 pthread_mutex_unlock(&bp->lock);
356 bc->slab_cons = slab_full;
357 bc->n_buffers_cons = n_buffers_per_slab;
362 bcache_cons(struct bcache *bc)
364 u64 n_buffers_cons = bc->n_buffers_cons - 1;
367 buffer = bc->slab_cons[n_buffers_cons];
368 bc->n_buffers_cons = n_buffers_cons;
373 bcache_prod(struct bcache *bc, u64 buffer)
375 struct bpool *bp = bc->bp;
376 u64 n_buffers_per_slab = bp->params.n_buffers_per_slab;
377 u64 n_buffers_prod = bc->n_buffers_prod;
378 u64 n_slabs_available;
382 * Producer slab is not yet full: store the current buffer to it.
384 if (n_buffers_prod < n_buffers_per_slab) {
385 bc->slab_prod[n_buffers_prod] = buffer;
386 bc->n_buffers_prod = n_buffers_prod + 1;
391 * Producer slab is full: trade the cache's current producer slab
392 * (full) for an empty slab from the pool, then store the current
393 * buffer to the new producer slab. As one full slab exists in the
394 * cache, it is guaranteed that there is at least one empty slab
395 * available in the pool.
397 pthread_mutex_lock(&bp->lock);
398 n_slabs_available = bp->n_slabs_available;
399 slab_empty = bp->slabs[n_slabs_available];
400 bp->slabs[n_slabs_available] = bc->slab_prod;
401 bp->n_slabs_available = n_slabs_available + 1;
402 pthread_mutex_unlock(&bp->lock);
404 slab_empty[0] = buffer;
405 bc->slab_prod = slab_empty;
406 bc->n_buffers_prod = 1;
412 * Each of the forwarding ports sits on top of an AF_XDP socket. In order for
413 * packet forwarding to happen with no packet buffer copy, all the sockets need
414 * to share the same UMEM area, which is used as the buffer pool memory.
417 #define MAX_BURST_RX 64
421 #define MAX_BURST_TX 64
425 u64 addr[MAX_BURST_RX];
426 u32 len[MAX_BURST_RX];
430 u64 addr[MAX_BURST_TX];
431 u32 len[MAX_BURST_TX];
436 struct xsk_socket_config xsk_cfg;
443 struct port_params params;
447 struct xsk_ring_cons rxq;
448 struct xsk_ring_prod txq;
449 struct xsk_ring_prod umem_fq;
450 struct xsk_ring_cons umem_cq;
451 struct xsk_socket *xsk;
452 int umem_fq_initialized;
459 port_free(struct port *p)
464 /* To keep this example simple, the code to free the buffers from the
465 * socket's receive and transmit queues, as well as from the UMEM fill
466 * and completion queues, is not included.
470 xsk_socket__delete(p->xsk);
478 port_init(struct port_params *params)
481 u32 umem_fq_size, pos = 0;
484 /* Memory allocation and initialization. */
485 p = calloc(sizeof(struct port), 1);
489 memcpy(&p->params, params, sizeof(p->params));
490 umem_fq_size = params->bp->umem_cfg.fill_size;
493 p->bc = bcache_init(params->bp);
495 (bcache_slab_size(p->bc) < umem_fq_size) ||
496 (bcache_cons_check(p->bc, umem_fq_size) < umem_fq_size)) {
502 status = xsk_socket__create_shared(&p->xsk,
517 xsk_ring_prod__reserve(&p->umem_fq, umem_fq_size, &pos);
519 for (i = 0; i < umem_fq_size; i++)
520 *xsk_ring_prod__fill_addr(&p->umem_fq, pos + i) =
523 xsk_ring_prod__submit(&p->umem_fq, umem_fq_size);
524 p->umem_fq_initialized = 1;
530 port_rx_burst(struct port *p, struct burst_rx *b)
534 /* Free buffers for FQ replenish. */
535 n_pkts = ARRAY_SIZE(b->addr);
537 n_pkts = bcache_cons_check(p->bc, n_pkts);
542 n_pkts = xsk_ring_cons__peek(&p->rxq, n_pkts, &pos);
544 if (xsk_ring_prod__needs_wakeup(&p->umem_fq)) {
545 struct pollfd pollfd = {
546 .fd = xsk_socket__fd(p->xsk),
555 for (i = 0; i < n_pkts; i++) {
556 b->addr[i] = xsk_ring_cons__rx_desc(&p->rxq, pos + i)->addr;
557 b->len[i] = xsk_ring_cons__rx_desc(&p->rxq, pos + i)->len;
560 xsk_ring_cons__release(&p->rxq, n_pkts);
561 p->n_pkts_rx += n_pkts;
567 status = xsk_ring_prod__reserve(&p->umem_fq, n_pkts, &pos);
568 if (status == n_pkts)
571 if (xsk_ring_prod__needs_wakeup(&p->umem_fq)) {
572 struct pollfd pollfd = {
573 .fd = xsk_socket__fd(p->xsk),
581 for (i = 0; i < n_pkts; i++)
582 *xsk_ring_prod__fill_addr(&p->umem_fq, pos + i) =
585 xsk_ring_prod__submit(&p->umem_fq, n_pkts);
591 port_tx_burst(struct port *p, struct burst_tx *b)
597 n_pkts = p->params.bp->umem_cfg.comp_size;
599 n_pkts = xsk_ring_cons__peek(&p->umem_cq, n_pkts, &pos);
601 for (i = 0; i < n_pkts; i++) {
602 u64 addr = *xsk_ring_cons__comp_addr(&p->umem_cq, pos + i);
604 bcache_prod(p->bc, addr);
607 xsk_ring_cons__release(&p->umem_cq, n_pkts);
613 status = xsk_ring_prod__reserve(&p->txq, n_pkts, &pos);
614 if (status == n_pkts)
617 if (xsk_ring_prod__needs_wakeup(&p->txq))
618 sendto(xsk_socket__fd(p->xsk), NULL, 0, MSG_DONTWAIT,
622 for (i = 0; i < n_pkts; i++) {
623 xsk_ring_prod__tx_desc(&p->txq, pos + i)->addr = b->addr[i];
624 xsk_ring_prod__tx_desc(&p->txq, pos + i)->len = b->len[i];
627 xsk_ring_prod__submit(&p->txq, n_pkts);
628 if (xsk_ring_prod__needs_wakeup(&p->txq))
629 sendto(xsk_socket__fd(p->xsk), NULL, 0, MSG_DONTWAIT, NULL, 0);
630 p->n_pkts_tx += n_pkts;
636 * Packet forwarding threads.
638 #ifndef MAX_PORTS_PER_THREAD
639 #define MAX_PORTS_PER_THREAD 16
643 struct port *ports_rx[MAX_PORTS_PER_THREAD];
644 struct port *ports_tx[MAX_PORTS_PER_THREAD];
646 struct burst_rx burst_rx;
647 struct burst_tx burst_tx[MAX_PORTS_PER_THREAD];
652 static void swap_mac_addresses(void *data)
654 struct ether_header *eth = (struct ether_header *)data;
655 struct ether_addr *src_addr = (struct ether_addr *)ð->ether_shost;
656 struct ether_addr *dst_addr = (struct ether_addr *)ð->ether_dhost;
657 struct ether_addr tmp;
660 *src_addr = *dst_addr;
665 thread_func(void *arg)
667 struct thread_data *t = arg;
671 CPU_ZERO(&cpu_cores);
672 CPU_SET(t->cpu_core_id, &cpu_cores);
673 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpu_cores);
675 for (i = 0; !t->quit; i = (i + 1) & (t->n_ports_rx - 1)) {
676 struct port *port_rx = t->ports_rx[i];
677 struct port *port_tx = t->ports_tx[i];
678 struct burst_rx *brx = &t->burst_rx;
679 struct burst_tx *btx = &t->burst_tx[i];
683 n_pkts = port_rx_burst(port_rx, brx);
688 for (j = 0; j < n_pkts; j++) {
689 u64 addr = xsk_umem__add_offset_to_addr(brx->addr[j]);
690 u8 *pkt = xsk_umem__get_data(port_rx->params.bp->addr,
693 swap_mac_addresses(pkt);
695 btx->addr[btx->n_pkts] = brx->addr[j];
696 btx->len[btx->n_pkts] = brx->len[j];
699 if (btx->n_pkts == MAX_BURST_TX) {
700 port_tx_burst(port_tx, btx);
712 static const struct bpool_params bpool_params_default = {
713 .n_buffers = 64 * 1024,
714 .buffer_size = XSK_UMEM__DEFAULT_FRAME_SIZE,
718 .n_buffers_per_slab = XSK_RING_PROD__DEFAULT_NUM_DESCS * 2,
721 static const struct xsk_umem_config umem_cfg_default = {
722 .fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS * 2,
723 .comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS,
724 .frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE,
725 .frame_headroom = XSK_UMEM__DEFAULT_FRAME_HEADROOM,
729 static const struct port_params port_params_default = {
731 .rx_size = XSK_RING_CONS__DEFAULT_NUM_DESCS,
732 .tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS,
734 .xdp_flags = XDP_FLAGS_DRV_MODE,
735 .bind_flags = XDP_USE_NEED_WAKEUP | XDP_ZEROCOPY,
748 #define MAX_THREADS 64
751 static struct bpool_params bpool_params;
752 static struct xsk_umem_config umem_cfg;
753 static struct bpool *bp;
755 static struct port_params port_params[MAX_PORTS];
756 static struct port *ports[MAX_PORTS];
757 static u64 n_pkts_rx[MAX_PORTS];
758 static u64 n_pkts_tx[MAX_PORTS];
761 static pthread_t threads[MAX_THREADS];
762 static struct thread_data thread_data[MAX_THREADS];
763 static int n_threads;
766 print_usage(char *prog_name)
770 "\t%s [ -b SIZE ] -c CORE -i INTERFACE [ -q QUEUE ]\n"
772 "-c CORE CPU core to run a packet forwarding thread\n"
773 " on. May be invoked multiple times.\n"
775 "-b SIZE Number of buffers in the buffer pool shared\n"
776 " by all the forwarding threads. Default: %u.\n"
778 "-i INTERFACE Network interface. Each (INTERFACE, QUEUE)\n"
779 " pair specifies one forwarding port. May be\n"
780 " invoked multiple times.\n"
782 "-q QUEUE Network interface queue for RX and TX. Each\n"
783 " (INTERFACE, QUEUE) pair specified one\n"
784 " forwarding port. Default: %u. May be invoked\n"
789 bpool_params_default.n_buffers,
790 port_params_default.iface_queue);
794 parse_args(int argc, char **argv)
796 struct option lgopts[] = {
799 int opt, option_index;
801 /* Parse the input arguments. */
803 opt = getopt_long(argc, argv, "c:i:q:", lgopts, &option_index);
809 bpool_params.n_buffers = atoi(optarg);
813 if (n_threads == MAX_THREADS) {
814 printf("Max number of threads (%d) reached.\n",
819 thread_data[n_threads].cpu_core_id = atoi(optarg);
824 if (n_ports == MAX_PORTS) {
825 printf("Max number of ports (%d) reached.\n",
830 port_params[n_ports].iface = optarg;
831 port_params[n_ports].iface_queue = 0;
837 printf("No port specified for queue.\n");
840 port_params[n_ports - 1].iface_queue = atoi(optarg);
844 printf("Illegal argument.\n");
849 optind = 1; /* reset getopt lib */
851 /* Check the input arguments. */
853 printf("No ports specified.\n");
858 printf("No threads specified.\n");
862 if (n_ports % n_threads) {
863 printf("Ports cannot be evenly distributed to threads.\n");
871 print_port(u32 port_id)
873 struct port *port = ports[port_id];
875 printf("Port %u: interface = %s, queue = %u\n",
876 port_id, port->params.iface, port->params.iface_queue);
880 print_thread(u32 thread_id)
882 struct thread_data *t = &thread_data[thread_id];
885 printf("Thread %u (CPU core %u): ",
886 thread_id, t->cpu_core_id);
888 for (i = 0; i < t->n_ports_rx; i++) {
889 struct port *port_rx = t->ports_rx[i];
890 struct port *port_tx = t->ports_tx[i];
892 printf("(%s, %u) -> (%s, %u), ",
893 port_rx->params.iface,
894 port_rx->params.iface_queue,
895 port_tx->params.iface,
896 port_tx->params.iface_queue);
903 print_port_stats_separator(void)
905 printf("+-%4s-+-%12s-+-%13s-+-%12s-+-%13s-+\n",
914 print_port_stats_header(void)
916 print_port_stats_separator();
917 printf("| %4s | %12s | %13s | %12s | %13s |\n",
923 print_port_stats_separator();
927 print_port_stats_trailer(void)
929 print_port_stats_separator();
934 print_port_stats(int port_id, u64 ns_diff)
936 struct port *p = ports[port_id];
937 double rx_pps, tx_pps;
939 rx_pps = (p->n_pkts_rx - n_pkts_rx[port_id]) * 1000000000. / ns_diff;
940 tx_pps = (p->n_pkts_tx - n_pkts_tx[port_id]) * 1000000000. / ns_diff;
942 printf("| %4d | %12llu | %13.0f | %12llu | %13.0f |\n",
949 n_pkts_rx[port_id] = p->n_pkts_rx;
950 n_pkts_tx[port_id] = p->n_pkts_tx;
954 print_port_stats_all(u64 ns_diff)
958 print_port_stats_header();
959 for (i = 0; i < n_ports; i++)
960 print_port_stats(i, ns_diff);
961 print_port_stats_trailer();
967 signal_handler(int sig)
972 static void remove_xdp_program(void)
976 for (i = 0 ; i < n_ports; i++)
977 bpf_set_link_xdp_fd(if_nametoindex(port_params[i].iface), -1,
978 port_params[i].xsk_cfg.xdp_flags);
981 int main(int argc, char **argv)
983 struct timespec time;
988 memcpy(&bpool_params, &bpool_params_default,
989 sizeof(struct bpool_params));
990 memcpy(&umem_cfg, &umem_cfg_default,
991 sizeof(struct xsk_umem_config));
992 for (i = 0; i < MAX_PORTS; i++)
993 memcpy(&port_params[i], &port_params_default,
994 sizeof(struct port_params));
996 if (parse_args(argc, argv)) {
997 print_usage(argv[0]);
1001 /* Buffer pool initialization. */
1002 bp = bpool_init(&bpool_params, &umem_cfg);
1004 printf("Buffer pool initialization failed.\n");
1007 printf("Buffer pool created successfully.\n");
1009 /* Ports initialization. */
1010 for (i = 0; i < MAX_PORTS; i++)
1011 port_params[i].bp = bp;
1013 for (i = 0; i < n_ports; i++) {
1014 ports[i] = port_init(&port_params[i]);
1016 printf("Port %d initialization failed.\n", i);
1021 printf("All ports created successfully.\n");
1024 for (i = 0; i < n_threads; i++) {
1025 struct thread_data *t = &thread_data[i];
1026 u32 n_ports_per_thread = n_ports / n_threads, j;
1028 for (j = 0; j < n_ports_per_thread; j++) {
1029 t->ports_rx[j] = ports[i * n_ports_per_thread + j];
1030 t->ports_tx[j] = ports[i * n_ports_per_thread +
1031 (j + 1) % n_ports_per_thread];
1034 t->n_ports_rx = n_ports_per_thread;
1039 for (i = 0; i < n_threads; i++) {
1042 status = pthread_create(&threads[i],
1047 printf("Thread %d creation failed.\n", i);
1051 printf("All threads created successfully.\n");
1053 /* Print statistics. */
1054 signal(SIGINT, signal_handler);
1055 signal(SIGTERM, signal_handler);
1056 signal(SIGABRT, signal_handler);
1058 clock_gettime(CLOCK_MONOTONIC, &time);
1059 ns0 = time.tv_sec * 1000000000UL + time.tv_nsec;
1064 clock_gettime(CLOCK_MONOTONIC, &time);
1065 ns1 = time.tv_sec * 1000000000UL + time.tv_nsec;
1066 ns_diff = ns1 - ns0;
1069 print_port_stats_all(ns_diff);
1072 /* Threads completion. */
1074 for (i = 0; i < n_threads; i++)
1075 thread_data[i].quit = 1;
1077 for (i = 0; i < n_threads; i++)
1078 pthread_join(threads[i], NULL);
1080 for (i = 0; i < n_ports; i++)
1081 port_free(ports[i]);
1085 remove_xdp_program();