selftests: xsk: Add test for unaligned mode
[linux-2.6-microblaze.git] / tools / testing / selftests / bpf / xdpxceiver.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2020 Intel Corporation. */
3
4 /*
5  * Some functions in this program are taken from
6  * Linux kernel samples/bpf/xdpsock* and modified
7  * for use.
8  *
9  * See test_xsk.sh for detailed information on test topology
10  * and prerequisite network setup.
11  *
12  * This test program contains two threads, each thread is single socket with
13  * a unique UMEM. It validates in-order packet delivery and packet content
14  * by sending packets to each other.
15  *
16  * Tests Information:
17  * ------------------
18  * These selftests test AF_XDP SKB and Native/DRV modes using veth
19  * Virtual Ethernet interfaces.
20  *
21  * For each mode, the following tests are run:
22  *    a. nopoll - soft-irq processing in run-to-completion mode
23  *    b. poll - using poll() syscall
24  *    c. Socket Teardown
25  *       Create a Tx and a Rx socket, Tx from one socket, Rx on another. Destroy
26  *       both sockets, then repeat multiple times. Only nopoll mode is used
27  *    d. Bi-directional sockets
28  *       Configure sockets as bi-directional tx/rx sockets, sets up fill and
29  *       completion rings on each socket, tx/rx in both directions. Only nopoll
30  *       mode is used
31  *    e. Statistics
32  *       Trigger some error conditions and ensure that the appropriate statistics
33  *       are incremented. Within this test, the following statistics are tested:
34  *       i.   rx dropped
35  *            Increase the UMEM frame headroom to a value which results in
36  *            insufficient space in the rx buffer for both the packet and the headroom.
37  *       ii.  tx invalid
38  *            Set the 'len' field of tx descriptors to an invalid value (umem frame
39  *            size + 1).
40  *       iii. rx ring full
41  *            Reduce the size of the RX ring to a fraction of the fill ring size.
42  *       iv.  fill queue empty
43  *            Do not populate the fill queue and then try to receive pkts.
44  *    f. bpf_link resource persistence
45  *       Configure sockets at indexes 0 and 1, run a traffic on queue ids 0,
46  *       then remove xsk sockets from queue 0 on both veth interfaces and
47  *       finally run a traffic on queues ids 1
48  *    g. unaligned mode
49  *
50  * Total tests: 12
51  *
52  * Flow:
53  * -----
54  * - Single process spawns two threads: Tx and Rx
55  * - Each of these two threads attach to a veth interface within their assigned
56  *   namespaces
57  * - Each thread Creates one AF_XDP socket connected to a unique umem for each
58  *   veth interface
59  * - Tx thread Transmits 10k packets from veth<xxxx> to veth<yyyy>
60  * - Rx thread verifies if all 10k packets were received and delivered in-order,
61  *   and have the right content
62  *
63  * Enable/disable packet dump mode:
64  * --------------------------
65  * To enable L2 - L4 headers and payload dump of each packet on STDOUT, add
66  * parameter -D to params array in test_xsk.sh, i.e. params=("-S" "-D")
67  */
68
69 #define _GNU_SOURCE
70 #include <fcntl.h>
71 #include <errno.h>
72 #include <getopt.h>
73 #include <asm/barrier.h>
74 #include <linux/if_link.h>
75 #include <linux/if_ether.h>
76 #include <linux/ip.h>
77 #include <linux/udp.h>
78 #include <arpa/inet.h>
79 #include <net/if.h>
80 #include <locale.h>
81 #include <poll.h>
82 #include <pthread.h>
83 #include <signal.h>
84 #include <stdbool.h>
85 #include <stdio.h>
86 #include <stdlib.h>
87 #include <string.h>
88 #include <stddef.h>
89 #include <sys/mman.h>
90 #include <sys/resource.h>
91 #include <sys/types.h>
92 #include <sys/queue.h>
93 #include <time.h>
94 #include <unistd.h>
95 #include <stdatomic.h>
96 #include <bpf/xsk.h>
97 #include "xdpxceiver.h"
98 #include "../kselftest.h"
99
100 static const char *MAC1 = "\x00\x0A\x56\x9E\xEE\x62";
101 static const char *MAC2 = "\x00\x0A\x56\x9E\xEE\x61";
102 static const char *IP1 = "192.168.100.162";
103 static const char *IP2 = "192.168.100.161";
104 static const u16 UDP_PORT1 = 2020;
105 static const u16 UDP_PORT2 = 2121;
106
107 static void __exit_with_error(int error, const char *file, const char *func, int line)
108 {
109         ksft_test_result_fail("[%s:%s:%i]: ERROR: %d/\"%s\"\n", file, func, line, error,
110                               strerror(error));
111         ksft_exit_xfail();
112 }
113
114 #define exit_with_error(error) __exit_with_error(error, __FILE__, __func__, __LINE__)
115
116 #define mode_string(test) (test)->ifobj_tx->xdp_flags & XDP_FLAGS_SKB_MODE ? "SKB" : "DRV"
117
118 #define print_ksft_result(test)                                         \
119         (ksft_test_result_pass("PASS: %s %s\n", mode_string(test), (test)->name))
120
121 static void memset32_htonl(void *dest, u32 val, u32 size)
122 {
123         u32 *ptr = (u32 *)dest;
124         int i;
125
126         val = htonl(val);
127
128         for (i = 0; i < (size & (~0x3)); i += 4)
129                 ptr[i >> 2] = val;
130 }
131
132 /*
133  * Fold a partial checksum
134  * This function code has been taken from
135  * Linux kernel include/asm-generic/checksum.h
136  */
137 static __u16 csum_fold(__u32 csum)
138 {
139         u32 sum = (__force u32)csum;
140
141         sum = (sum & 0xffff) + (sum >> 16);
142         sum = (sum & 0xffff) + (sum >> 16);
143         return (__force __u16)~sum;
144 }
145
146 /*
147  * This function code has been taken from
148  * Linux kernel lib/checksum.c
149  */
150 static u32 from64to32(u64 x)
151 {
152         /* add up 32-bit and 32-bit for 32+c bit */
153         x = (x & 0xffffffff) + (x >> 32);
154         /* add up carry.. */
155         x = (x & 0xffffffff) + (x >> 32);
156         return (u32)x;
157 }
158
159 /*
160  * This function code has been taken from
161  * Linux kernel lib/checksum.c
162  */
163 static __u32 csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len, __u8 proto, __u32 sum)
164 {
165         unsigned long long s = (__force u32)sum;
166
167         s += (__force u32)saddr;
168         s += (__force u32)daddr;
169 #ifdef __BIG_ENDIAN__
170         s += proto + len;
171 #else
172         s += (proto + len) << 8;
173 #endif
174         return (__force __u32)from64to32(s);
175 }
176
177 /*
178  * This function has been taken from
179  * Linux kernel include/asm-generic/checksum.h
180  */
181 static __u16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len, __u8 proto, __u32 sum)
182 {
183         return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
184 }
185
186 static u16 udp_csum(u32 saddr, u32 daddr, u32 len, u8 proto, u16 *udp_pkt)
187 {
188         u32 csum = 0;
189         u32 cnt = 0;
190
191         /* udp hdr and data */
192         for (; cnt < len; cnt += 2)
193                 csum += udp_pkt[cnt >> 1];
194
195         return csum_tcpudp_magic(saddr, daddr, len, proto, csum);
196 }
197
198 static void gen_eth_hdr(struct ifobject *ifobject, struct ethhdr *eth_hdr)
199 {
200         memcpy(eth_hdr->h_dest, ifobject->dst_mac, ETH_ALEN);
201         memcpy(eth_hdr->h_source, ifobject->src_mac, ETH_ALEN);
202         eth_hdr->h_proto = htons(ETH_P_IP);
203 }
204
205 static void gen_ip_hdr(struct ifobject *ifobject, struct iphdr *ip_hdr)
206 {
207         ip_hdr->version = IP_PKT_VER;
208         ip_hdr->ihl = 0x5;
209         ip_hdr->tos = IP_PKT_TOS;
210         ip_hdr->tot_len = htons(IP_PKT_SIZE);
211         ip_hdr->id = 0;
212         ip_hdr->frag_off = 0;
213         ip_hdr->ttl = IPDEFTTL;
214         ip_hdr->protocol = IPPROTO_UDP;
215         ip_hdr->saddr = ifobject->src_ip;
216         ip_hdr->daddr = ifobject->dst_ip;
217         ip_hdr->check = 0;
218 }
219
220 static void gen_udp_hdr(u32 payload, void *pkt, struct ifobject *ifobject,
221                         struct udphdr *udp_hdr)
222 {
223         udp_hdr->source = htons(ifobject->src_port);
224         udp_hdr->dest = htons(ifobject->dst_port);
225         udp_hdr->len = htons(UDP_PKT_SIZE);
226         memset32_htonl(pkt + PKT_HDR_SIZE, payload, UDP_PKT_DATA_SIZE);
227 }
228
229 static void gen_udp_csum(struct udphdr *udp_hdr, struct iphdr *ip_hdr)
230 {
231         udp_hdr->check = 0;
232         udp_hdr->check =
233             udp_csum(ip_hdr->saddr, ip_hdr->daddr, UDP_PKT_SIZE, IPPROTO_UDP, (u16 *)udp_hdr);
234 }
235
236 static int xsk_configure_umem(struct xsk_umem_info *umem, void *buffer, u64 size)
237 {
238         struct xsk_umem_config cfg = {
239                 .fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS,
240                 .comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS,
241                 .frame_size = umem->frame_size,
242                 .frame_headroom = umem->frame_headroom,
243                 .flags = XSK_UMEM__DEFAULT_FLAGS
244         };
245         int ret;
246
247         if (umem->unaligned_mode)
248                 cfg.flags |= XDP_UMEM_UNALIGNED_CHUNK_FLAG;
249
250         ret = xsk_umem__create(&umem->umem, buffer, size,
251                                &umem->fq, &umem->cq, &cfg);
252         if (ret)
253                 return ret;
254
255         umem->buffer = buffer;
256         return 0;
257 }
258
259 static int xsk_configure_socket(struct xsk_socket_info *xsk, struct xsk_umem_info *umem,
260                                 struct ifobject *ifobject, u32 qid)
261 {
262         struct xsk_socket_config cfg;
263         struct xsk_ring_cons *rxr;
264         struct xsk_ring_prod *txr;
265
266         xsk->umem = umem;
267         cfg.rx_size = xsk->rxqsize;
268         cfg.tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
269         cfg.libbpf_flags = 0;
270         cfg.xdp_flags = ifobject->xdp_flags;
271         cfg.bind_flags = ifobject->bind_flags;
272
273         txr = ifobject->tx_on ? &xsk->tx : NULL;
274         rxr = ifobject->rx_on ? &xsk->rx : NULL;
275         return xsk_socket__create(&xsk->xsk, ifobject->ifname, qid, umem->umem, rxr, txr, &cfg);
276 }
277
278 static struct option long_options[] = {
279         {"interface", required_argument, 0, 'i'},
280         {"queue", optional_argument, 0, 'q'},
281         {"dump-pkts", optional_argument, 0, 'D'},
282         {"verbose", no_argument, 0, 'v'},
283         {0, 0, 0, 0}
284 };
285
286 static void usage(const char *prog)
287 {
288         const char *str =
289                 "  Usage: %s [OPTIONS]\n"
290                 "  Options:\n"
291                 "  -i, --interface      Use interface\n"
292                 "  -q, --queue=n        Use queue n (default 0)\n"
293                 "  -D, --dump-pkts      Dump packets L2 - L5\n"
294                 "  -v, --verbose        Verbose output\n";
295
296         ksft_print_msg(str, prog);
297 }
298
299 static int switch_namespace(const char *nsname)
300 {
301         char fqns[26] = "/var/run/netns/";
302         int nsfd;
303
304         if (!nsname || strlen(nsname) == 0)
305                 return -1;
306
307         strncat(fqns, nsname, sizeof(fqns) - strlen(fqns) - 1);
308         nsfd = open(fqns, O_RDONLY);
309
310         if (nsfd == -1)
311                 exit_with_error(errno);
312
313         if (setns(nsfd, 0) == -1)
314                 exit_with_error(errno);
315
316         print_verbose("NS switched: %s\n", nsname);
317
318         return nsfd;
319 }
320
321 static bool validate_interface(struct ifobject *ifobj)
322 {
323         if (!strcmp(ifobj->ifname, ""))
324                 return false;
325         return true;
326 }
327
328 static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj_rx, int argc,
329                                char **argv)
330 {
331         struct ifobject *ifobj;
332         u32 interface_nb = 0;
333         int option_index, c;
334
335         opterr = 0;
336
337         for (;;) {
338                 char *sptr, *token;
339
340                 c = getopt_long(argc, argv, "i:Dv", long_options, &option_index);
341                 if (c == -1)
342                         break;
343
344                 switch (c) {
345                 case 'i':
346                         if (interface_nb == 0)
347                                 ifobj = ifobj_tx;
348                         else if (interface_nb == 1)
349                                 ifobj = ifobj_rx;
350                         else
351                                 break;
352
353                         sptr = strndupa(optarg, strlen(optarg));
354                         memcpy(ifobj->ifname, strsep(&sptr, ","), MAX_INTERFACE_NAME_CHARS);
355                         token = strsep(&sptr, ",");
356                         if (token)
357                                 memcpy(ifobj->nsname, token, MAX_INTERFACES_NAMESPACE_CHARS);
358                         interface_nb++;
359                         break;
360                 case 'D':
361                         opt_pkt_dump = true;
362                         break;
363                 case 'v':
364                         opt_verbose = true;
365                         break;
366                 default:
367                         usage(basename(argv[0]));
368                         ksft_exit_xfail();
369                 }
370         }
371 }
372
373 static void __test_spec_init(struct test_spec *test, struct ifobject *ifobj_tx,
374                              struct ifobject *ifobj_rx)
375 {
376         u32 i, j;
377
378         for (i = 0; i < MAX_INTERFACES; i++) {
379                 struct ifobject *ifobj = i ? ifobj_rx : ifobj_tx;
380
381                 ifobj->umem = &ifobj->umem_arr[0];
382                 ifobj->xsk = &ifobj->xsk_arr[0];
383                 ifobj->use_poll = false;
384                 ifobj->pkt_stream = test->pkt_stream_default;
385
386                 if (i == 0) {
387                         ifobj->rx_on = false;
388                         ifobj->tx_on = true;
389                 } else {
390                         ifobj->rx_on = true;
391                         ifobj->tx_on = false;
392                 }
393
394                 for (j = 0; j < MAX_SOCKETS; j++) {
395                         memset(&ifobj->umem_arr[j], 0, sizeof(ifobj->umem_arr[j]));
396                         memset(&ifobj->xsk_arr[j], 0, sizeof(ifobj->xsk_arr[j]));
397                         ifobj->umem_arr[j].num_frames = DEFAULT_PKT_CNT / 4;
398                         ifobj->umem_arr[j].frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE;
399                         ifobj->xsk_arr[j].rxqsize = XSK_RING_CONS__DEFAULT_NUM_DESCS;
400                 }
401         }
402
403         test->ifobj_tx = ifobj_tx;
404         test->ifobj_rx = ifobj_rx;
405         test->current_step = 0;
406         test->total_steps = 1;
407         test->nb_sockets = 1;
408 }
409
410 static void test_spec_init(struct test_spec *test, struct ifobject *ifobj_tx,
411                            struct ifobject *ifobj_rx, enum test_mode mode)
412 {
413         struct pkt_stream *pkt_stream;
414         u32 i;
415
416         pkt_stream = test->pkt_stream_default;
417         memset(test, 0, sizeof(*test));
418         test->pkt_stream_default = pkt_stream;
419
420         for (i = 0; i < MAX_INTERFACES; i++) {
421                 struct ifobject *ifobj = i ? ifobj_rx : ifobj_tx;
422
423                 ifobj->xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
424                 if (mode == TEST_MODE_SKB)
425                         ifobj->xdp_flags |= XDP_FLAGS_SKB_MODE;
426                 else
427                         ifobj->xdp_flags |= XDP_FLAGS_DRV_MODE;
428
429                 ifobj->bind_flags = XDP_USE_NEED_WAKEUP | XDP_COPY;
430         }
431
432         __test_spec_init(test, ifobj_tx, ifobj_rx);
433 }
434
435 static void test_spec_reset(struct test_spec *test)
436 {
437         __test_spec_init(test, test->ifobj_tx, test->ifobj_rx);
438 }
439
440 static void test_spec_set_name(struct test_spec *test, const char *name)
441 {
442         strncpy(test->name, name, MAX_TEST_NAME_SIZE);
443 }
444
445 static struct pkt *pkt_stream_get_pkt(struct pkt_stream *pkt_stream, u32 pkt_nb)
446 {
447         if (pkt_nb >= pkt_stream->nb_pkts)
448                 return NULL;
449
450         return &pkt_stream->pkts[pkt_nb];
451 }
452
453 static void pkt_stream_delete(struct pkt_stream *pkt_stream)
454 {
455         free(pkt_stream->pkts);
456         free(pkt_stream);
457 }
458
459 static void pkt_stream_restore_default(struct test_spec *test)
460 {
461         pkt_stream_delete(test->ifobj_tx->pkt_stream);
462         test->ifobj_tx->pkt_stream = test->pkt_stream_default;
463         test->ifobj_rx->pkt_stream = test->pkt_stream_default;
464 }
465
466 static struct pkt_stream *pkt_stream_generate(struct xsk_umem_info *umem, u32 nb_pkts, u32 pkt_len)
467 {
468         struct pkt_stream *pkt_stream;
469         u32 i;
470
471         pkt_stream = calloc(1, sizeof(*pkt_stream));
472         if (!pkt_stream)
473                 exit_with_error(ENOMEM);
474
475         pkt_stream->pkts = calloc(nb_pkts, sizeof(*pkt_stream->pkts));
476         if (!pkt_stream->pkts)
477                 exit_with_error(ENOMEM);
478
479         pkt_stream->nb_pkts = nb_pkts;
480         for (i = 0; i < nb_pkts; i++) {
481                 pkt_stream->pkts[i].addr = (i % umem->num_frames) * umem->frame_size +
482                         DEFAULT_OFFSET;
483                 pkt_stream->pkts[i].len = pkt_len;
484                 pkt_stream->pkts[i].payload = i;
485
486                 if (pkt_len > umem->frame_size)
487                         pkt_stream->pkts[i].valid = false;
488                 else
489                         pkt_stream->pkts[i].valid = true;
490         }
491
492         return pkt_stream;
493 }
494
495 static struct pkt_stream *pkt_stream_clone(struct xsk_umem_info *umem,
496                                            struct pkt_stream *pkt_stream)
497 {
498         return pkt_stream_generate(umem, pkt_stream->nb_pkts, pkt_stream->pkts[0].len);
499 }
500
501 static void pkt_stream_replace(struct test_spec *test, u32 nb_pkts, u32 pkt_len)
502 {
503         struct pkt_stream *pkt_stream;
504
505         pkt_stream = pkt_stream_generate(test->ifobj_tx->umem, nb_pkts, pkt_len);
506         test->ifobj_tx->pkt_stream = pkt_stream;
507         test->ifobj_rx->pkt_stream = pkt_stream;
508 }
509
510 static void pkt_stream_replace_half(struct test_spec *test, u32 pkt_len, u32 offset)
511 {
512         struct xsk_umem_info *umem = test->ifobj_tx->umem;
513         struct pkt_stream *pkt_stream;
514         u32 i;
515
516         pkt_stream = pkt_stream_clone(umem, test->pkt_stream_default);
517         for (i = 0; i < test->pkt_stream_default->nb_pkts; i += 2) {
518                 pkt_stream->pkts[i].addr = (i % umem->num_frames) * umem->frame_size + offset;
519                 pkt_stream->pkts[i].len = pkt_len;
520         }
521
522         test->ifobj_tx->pkt_stream = pkt_stream;
523         test->ifobj_rx->pkt_stream = pkt_stream;
524 }
525
526 static struct pkt *pkt_generate(struct ifobject *ifobject, u32 pkt_nb)
527 {
528         struct pkt *pkt = pkt_stream_get_pkt(ifobject->pkt_stream, pkt_nb);
529         struct udphdr *udp_hdr;
530         struct ethhdr *eth_hdr;
531         struct iphdr *ip_hdr;
532         void *data;
533
534         if (!pkt)
535                 return NULL;
536
537         data = xsk_umem__get_data(ifobject->umem->buffer, pkt->addr);
538         udp_hdr = (struct udphdr *)(data + sizeof(struct ethhdr) + sizeof(struct iphdr));
539         ip_hdr = (struct iphdr *)(data + sizeof(struct ethhdr));
540         eth_hdr = (struct ethhdr *)data;
541
542         gen_udp_hdr(pkt_nb, data, ifobject, udp_hdr);
543         gen_ip_hdr(ifobject, ip_hdr);
544         gen_udp_csum(udp_hdr, ip_hdr);
545         gen_eth_hdr(ifobject, eth_hdr);
546
547         return pkt;
548 }
549
550 static void pkt_dump(void *pkt, u32 len)
551 {
552         char s[INET_ADDRSTRLEN];
553         struct ethhdr *ethhdr;
554         struct udphdr *udphdr;
555         struct iphdr *iphdr;
556         int payload, i;
557
558         ethhdr = pkt;
559         iphdr = pkt + sizeof(*ethhdr);
560         udphdr = pkt + sizeof(*ethhdr) + sizeof(*iphdr);
561
562         /*extract L2 frame */
563         fprintf(stdout, "DEBUG>> L2: dst mac: ");
564         for (i = 0; i < ETH_ALEN; i++)
565                 fprintf(stdout, "%02X", ethhdr->h_dest[i]);
566
567         fprintf(stdout, "\nDEBUG>> L2: src mac: ");
568         for (i = 0; i < ETH_ALEN; i++)
569                 fprintf(stdout, "%02X", ethhdr->h_source[i]);
570
571         /*extract L3 frame */
572         fprintf(stdout, "\nDEBUG>> L3: ip_hdr->ihl: %02X\n", iphdr->ihl);
573         fprintf(stdout, "DEBUG>> L3: ip_hdr->saddr: %s\n",
574                 inet_ntop(AF_INET, &iphdr->saddr, s, sizeof(s)));
575         fprintf(stdout, "DEBUG>> L3: ip_hdr->daddr: %s\n",
576                 inet_ntop(AF_INET, &iphdr->daddr, s, sizeof(s)));
577         /*extract L4 frame */
578         fprintf(stdout, "DEBUG>> L4: udp_hdr->src: %d\n", ntohs(udphdr->source));
579         fprintf(stdout, "DEBUG>> L4: udp_hdr->dst: %d\n", ntohs(udphdr->dest));
580         /*extract L5 frame */
581         payload = *((uint32_t *)(pkt + PKT_HDR_SIZE));
582
583         fprintf(stdout, "DEBUG>> L5: payload: %d\n", payload);
584         fprintf(stdout, "---------------------------------------\n");
585 }
586
587 static bool is_pkt_valid(struct pkt *pkt, void *buffer, u64 addr, u32 len)
588 {
589         void *data = xsk_umem__get_data(buffer, addr);
590         struct iphdr *iphdr = (struct iphdr *)(data + sizeof(struct ethhdr));
591
592         if (!pkt) {
593                 ksft_test_result_fail("ERROR: [%s] too many packets received\n", __func__);
594                 return false;
595         }
596
597         if (iphdr->version == IP_PKT_VER && iphdr->tos == IP_PKT_TOS) {
598                 u32 seqnum = ntohl(*((u32 *)(data + PKT_HDR_SIZE)));
599
600                 if (opt_pkt_dump)
601                         pkt_dump(data, PKT_SIZE);
602
603                 if (pkt->len != len) {
604                         ksft_test_result_fail
605                                 ("ERROR: [%s] expected length [%d], got length [%d]\n",
606                                         __func__, pkt->len, len);
607                         return false;
608                 }
609
610                 if (pkt->payload != seqnum) {
611                         ksft_test_result_fail
612                                 ("ERROR: [%s] expected seqnum [%d], got seqnum [%d]\n",
613                                         __func__, pkt->payload, seqnum);
614                         return false;
615                 }
616         } else {
617                 ksft_print_msg("Invalid frame received: ");
618                 ksft_print_msg("[IP_PKT_VER: %02X], [IP_PKT_TOS: %02X]\n", iphdr->version,
619                                iphdr->tos);
620                 return false;
621         }
622
623         return true;
624 }
625
626 static void kick_tx(struct xsk_socket_info *xsk)
627 {
628         int ret;
629
630         ret = sendto(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, 0);
631         if (ret >= 0 || errno == ENOBUFS || errno == EAGAIN || errno == EBUSY || errno == ENETDOWN)
632                 return;
633         exit_with_error(errno);
634 }
635
636 static void complete_pkts(struct xsk_socket_info *xsk, int batch_size)
637 {
638         unsigned int rcvd;
639         u32 idx;
640
641         if (xsk_ring_prod__needs_wakeup(&xsk->tx))
642                 kick_tx(xsk);
643
644         rcvd = xsk_ring_cons__peek(&xsk->umem->cq, batch_size, &idx);
645         if (rcvd) {
646                 xsk_ring_cons__release(&xsk->umem->cq, rcvd);
647                 xsk->outstanding_tx -= rcvd;
648         }
649 }
650
651 static void receive_pkts(struct pkt_stream *pkt_stream, struct xsk_socket_info *xsk,
652                          struct pollfd *fds)
653 {
654         u32 idx_rx = 0, idx_fq = 0, rcvd, i, pkt_count = 0;
655         struct pkt *pkt;
656         int ret;
657
658         pkt = pkt_stream_get_pkt(pkt_stream, pkt_count++);
659         while (pkt) {
660                 rcvd = xsk_ring_cons__peek(&xsk->rx, BATCH_SIZE, &idx_rx);
661                 if (!rcvd) {
662                         if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) {
663                                 ret = poll(fds, 1, POLL_TMOUT);
664                                 if (ret < 0)
665                                         exit_with_error(-ret);
666                         }
667                         continue;
668                 }
669
670                 ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq);
671                 while (ret != rcvd) {
672                         if (ret < 0)
673                                 exit_with_error(-ret);
674                         if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) {
675                                 ret = poll(fds, 1, POLL_TMOUT);
676                                 if (ret < 0)
677                                         exit_with_error(-ret);
678                         }
679                         ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq);
680                 }
681
682                 for (i = 0; i < rcvd; i++) {
683                         const struct xdp_desc *desc = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++);
684                         u64 addr = desc->addr, orig;
685
686                         orig = xsk_umem__extract_addr(addr);
687                         addr = xsk_umem__add_offset_to_addr(addr);
688                         if (!is_pkt_valid(pkt, xsk->umem->buffer, addr, desc->len))
689                                 return;
690
691                         *xsk_ring_prod__fill_addr(&xsk->umem->fq, idx_fq++) = orig;
692                         pkt = pkt_stream_get_pkt(pkt_stream, pkt_count++);
693                 }
694
695                 xsk_ring_prod__submit(&xsk->umem->fq, rcvd);
696                 xsk_ring_cons__release(&xsk->rx, rcvd);
697         }
698 }
699
700 static u32 __send_pkts(struct ifobject *ifobject, u32 pkt_nb)
701 {
702         struct xsk_socket_info *xsk = ifobject->xsk;
703         u32 i, idx, valid_pkts = 0;
704
705         while (xsk_ring_prod__reserve(&xsk->tx, BATCH_SIZE, &idx) < BATCH_SIZE)
706                 complete_pkts(xsk, BATCH_SIZE);
707
708         for (i = 0; i < BATCH_SIZE; i++) {
709                 struct xdp_desc *tx_desc = xsk_ring_prod__tx_desc(&xsk->tx, idx + i);
710                 struct pkt *pkt = pkt_generate(ifobject, pkt_nb);
711
712                 if (!pkt)
713                         break;
714
715                 tx_desc->addr = pkt->addr;
716                 tx_desc->len = pkt->len;
717                 pkt_nb++;
718                 if (pkt->valid)
719                         valid_pkts++;
720         }
721
722         xsk_ring_prod__submit(&xsk->tx, i);
723         xsk->outstanding_tx += valid_pkts;
724         complete_pkts(xsk, BATCH_SIZE);
725
726         return i;
727 }
728
729 static void wait_for_tx_completion(struct xsk_socket_info *xsk)
730 {
731         while (xsk->outstanding_tx)
732                 complete_pkts(xsk, BATCH_SIZE);
733 }
734
735 static void send_pkts(struct ifobject *ifobject)
736 {
737         struct pollfd fds = { };
738         u32 pkt_cnt = 0;
739
740         fds.fd = xsk_socket__fd(ifobject->xsk->xsk);
741         fds.events = POLLOUT;
742
743         while (pkt_cnt < ifobject->pkt_stream->nb_pkts) {
744                 u32 sent;
745
746                 if (ifobject->use_poll) {
747                         int ret;
748
749                         ret = poll(&fds, 1, POLL_TMOUT);
750                         if (ret <= 0)
751                                 continue;
752
753                         if (!(fds.revents & POLLOUT))
754                                 continue;
755                 }
756
757                 sent = __send_pkts(ifobject, pkt_cnt);
758                 pkt_cnt += sent;
759                 usleep(10);
760         }
761
762         wait_for_tx_completion(ifobject->xsk);
763 }
764
765 static bool rx_stats_are_valid(struct ifobject *ifobject)
766 {
767         u32 xsk_stat = 0, expected_stat = ifobject->pkt_stream->nb_pkts;
768         struct xsk_socket *xsk = ifobject->xsk->xsk;
769         int fd = xsk_socket__fd(xsk);
770         struct xdp_statistics stats;
771         socklen_t optlen;
772         int err;
773
774         optlen = sizeof(stats);
775         err = getsockopt(fd, SOL_XDP, XDP_STATISTICS, &stats, &optlen);
776         if (err) {
777                 ksft_test_result_fail("ERROR Rx: [%s] getsockopt(XDP_STATISTICS) error %u %s\n",
778                                       __func__, -err, strerror(-err));
779                 return true;
780         }
781
782         if (optlen == sizeof(struct xdp_statistics)) {
783                 switch (stat_test_type) {
784                 case STAT_TEST_RX_DROPPED:
785                         xsk_stat = stats.rx_dropped;
786                         break;
787                 case STAT_TEST_TX_INVALID:
788                         return true;
789                 case STAT_TEST_RX_FULL:
790                         xsk_stat = stats.rx_ring_full;
791                         expected_stat -= RX_FULL_RXQSIZE;
792                         break;
793                 case STAT_TEST_RX_FILL_EMPTY:
794                         xsk_stat = stats.rx_fill_ring_empty_descs;
795                         break;
796                 default:
797                         break;
798                 }
799
800                 if (xsk_stat == expected_stat)
801                         return true;
802         }
803
804         return false;
805 }
806
807 static void tx_stats_validate(struct ifobject *ifobject)
808 {
809         struct xsk_socket *xsk = ifobject->xsk->xsk;
810         int fd = xsk_socket__fd(xsk);
811         struct xdp_statistics stats;
812         socklen_t optlen;
813         int err;
814
815         optlen = sizeof(stats);
816         err = getsockopt(fd, SOL_XDP, XDP_STATISTICS, &stats, &optlen);
817         if (err) {
818                 ksft_test_result_fail("ERROR Tx: [%s] getsockopt(XDP_STATISTICS) error %u %s\n",
819                                       __func__, -err, strerror(-err));
820                 return;
821         }
822
823         if (stats.tx_invalid_descs == ifobject->pkt_stream->nb_pkts)
824                 return;
825
826         ksft_test_result_fail("ERROR: [%s] tx_invalid_descs incorrect. Got [%u] expected [%u]\n",
827                               __func__, stats.tx_invalid_descs, ifobject->pkt_stream->nb_pkts);
828 }
829
830 static void thread_common_ops(struct test_spec *test, struct ifobject *ifobject)
831 {
832         int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
833         u32 i;
834
835         ifobject->ns_fd = switch_namespace(ifobject->nsname);
836
837         if (ifobject->umem->unaligned_mode)
838                 mmap_flags |= MAP_HUGETLB;
839
840         for (i = 0; i < test->nb_sockets; i++) {
841                 u64 umem_sz = ifobject->umem->num_frames * ifobject->umem->frame_size;
842                 u32 ctr = 0;
843                 void *bufs;
844
845                 bufs = mmap(NULL, umem_sz, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
846                 if (bufs == MAP_FAILED)
847                         exit_with_error(errno);
848
849                 while (ctr++ < SOCK_RECONF_CTR) {
850                         int ret;
851
852                         ret = xsk_configure_umem(&ifobject->umem_arr[i], bufs, umem_sz);
853                         if (ret)
854                                 exit_with_error(-ret);
855
856                         ret = xsk_configure_socket(&ifobject->xsk_arr[i], &ifobject->umem_arr[i],
857                                                    ifobject, i);
858                         if (!ret)
859                                 break;
860
861                         /* Retry if it fails as xsk_socket__create() is asynchronous */
862                         if (ctr >= SOCK_RECONF_CTR)
863                                 exit_with_error(-ret);
864                         usleep(USLEEP_MAX);
865                 }
866         }
867
868         ifobject->umem = &ifobject->umem_arr[0];
869         ifobject->xsk = &ifobject->xsk_arr[0];
870 }
871
872 static void testapp_cleanup_xsk_res(struct ifobject *ifobj)
873 {
874         print_verbose("Destroying socket\n");
875         xsk_socket__delete(ifobj->xsk->xsk);
876         xsk_umem__delete(ifobj->umem->umem);
877 }
878
879 static void *worker_testapp_validate_tx(void *arg)
880 {
881         struct test_spec *test = (struct test_spec *)arg;
882         struct ifobject *ifobject = test->ifobj_tx;
883
884         if (test->current_step == 1)
885                 thread_common_ops(test, ifobject);
886
887         print_verbose("Sending %d packets on interface %s\n", ifobject->pkt_stream->nb_pkts,
888                       ifobject->ifname);
889         send_pkts(ifobject);
890
891         if (stat_test_type == STAT_TEST_TX_INVALID)
892                 tx_stats_validate(ifobject);
893
894         if (test->total_steps == test->current_step)
895                 testapp_cleanup_xsk_res(ifobject);
896         pthread_exit(NULL);
897 }
898
899 static void xsk_populate_fill_ring(struct xsk_umem_info *umem, struct pkt_stream *pkt_stream)
900 {
901         u32 idx = 0, i;
902         int ret;
903
904         ret = xsk_ring_prod__reserve(&umem->fq, XSK_RING_PROD__DEFAULT_NUM_DESCS, &idx);
905         if (ret != XSK_RING_PROD__DEFAULT_NUM_DESCS)
906                 exit_with_error(ENOSPC);
907         for (i = 0; i < XSK_RING_PROD__DEFAULT_NUM_DESCS; i++) {
908                 u64 addr;
909
910                 if (pkt_stream->use_addr_for_fill) {
911                         struct pkt *pkt = pkt_stream_get_pkt(pkt_stream, i);
912
913                         if (!pkt)
914                                 break;
915                         addr = pkt->addr;
916                 } else {
917                         addr = (i % umem->num_frames) * umem->frame_size + DEFAULT_OFFSET;
918                 }
919
920                 *xsk_ring_prod__fill_addr(&umem->fq, idx++) = addr;
921         }
922         xsk_ring_prod__submit(&umem->fq, XSK_RING_PROD__DEFAULT_NUM_DESCS);
923 }
924
925 static void *worker_testapp_validate_rx(void *arg)
926 {
927         struct test_spec *test = (struct test_spec *)arg;
928         struct ifobject *ifobject = test->ifobj_rx;
929         struct pollfd fds = { };
930
931         if (test->current_step == 1)
932                 thread_common_ops(test, ifobject);
933
934         if (stat_test_type != STAT_TEST_RX_FILL_EMPTY)
935                 xsk_populate_fill_ring(ifobject->umem, ifobject->pkt_stream);
936
937         fds.fd = xsk_socket__fd(ifobject->xsk->xsk);
938         fds.events = POLLIN;
939
940         pthread_barrier_wait(&barr);
941
942         if (test_type == TEST_TYPE_STATS)
943                 while (!rx_stats_are_valid(ifobject))
944                         continue;
945         else
946                 receive_pkts(ifobject->pkt_stream, ifobject->xsk, &fds);
947
948         if (test->total_steps == test->current_step)
949                 testapp_cleanup_xsk_res(ifobject);
950         pthread_exit(NULL);
951 }
952
953 static void testapp_validate_traffic(struct test_spec *test)
954 {
955         struct ifobject *ifobj_tx = test->ifobj_tx;
956         struct ifobject *ifobj_rx = test->ifobj_rx;
957         pthread_t t0, t1;
958
959         if (pthread_barrier_init(&barr, NULL, 2))
960                 exit_with_error(errno);
961
962         test->current_step++;
963
964         /*Spawn RX thread */
965         pthread_create(&t0, NULL, ifobj_rx->func_ptr, test);
966
967         pthread_barrier_wait(&barr);
968         if (pthread_barrier_destroy(&barr))
969                 exit_with_error(errno);
970
971         /*Spawn TX thread */
972         pthread_create(&t1, NULL, ifobj_tx->func_ptr, test);
973
974         pthread_join(t1, NULL);
975         pthread_join(t0, NULL);
976 }
977
978 static void testapp_teardown(struct test_spec *test)
979 {
980         int i;
981
982         test_spec_set_name(test, "TEARDOWN");
983         for (i = 0; i < MAX_TEARDOWN_ITER; i++) {
984                 testapp_validate_traffic(test);
985                 test_spec_reset(test);
986         }
987 }
988
989 static void swap_directions(struct ifobject **ifobj1, struct ifobject **ifobj2)
990 {
991         thread_func_t tmp_func_ptr = (*ifobj1)->func_ptr;
992         struct ifobject *tmp_ifobj = (*ifobj1);
993
994         (*ifobj1)->func_ptr = (*ifobj2)->func_ptr;
995         (*ifobj2)->func_ptr = tmp_func_ptr;
996
997         *ifobj1 = *ifobj2;
998         *ifobj2 = tmp_ifobj;
999 }
1000
1001 static void testapp_bidi(struct test_spec *test)
1002 {
1003         test_spec_set_name(test, "BIDIRECTIONAL");
1004         test->ifobj_tx->rx_on = true;
1005         test->ifobj_rx->tx_on = true;
1006         test->total_steps = 2;
1007         testapp_validate_traffic(test);
1008
1009         print_verbose("Switching Tx/Rx vectors\n");
1010         swap_directions(&test->ifobj_rx, &test->ifobj_tx);
1011         testapp_validate_traffic(test);
1012
1013         swap_directions(&test->ifobj_rx, &test->ifobj_tx);
1014 }
1015
1016 static void swap_xsk_resources(struct ifobject *ifobj_tx, struct ifobject *ifobj_rx)
1017 {
1018         xsk_socket__delete(ifobj_tx->xsk->xsk);
1019         xsk_umem__delete(ifobj_tx->umem->umem);
1020         xsk_socket__delete(ifobj_rx->xsk->xsk);
1021         xsk_umem__delete(ifobj_rx->umem->umem);
1022         ifobj_tx->umem = &ifobj_tx->umem_arr[1];
1023         ifobj_tx->xsk = &ifobj_tx->xsk_arr[1];
1024         ifobj_rx->umem = &ifobj_rx->umem_arr[1];
1025         ifobj_rx->xsk = &ifobj_rx->xsk_arr[1];
1026 }
1027
1028 static void testapp_bpf_res(struct test_spec *test)
1029 {
1030         test_spec_set_name(test, "BPF_RES");
1031         test->total_steps = 2;
1032         test->nb_sockets = 2;
1033         testapp_validate_traffic(test);
1034
1035         swap_xsk_resources(test->ifobj_tx, test->ifobj_rx);
1036         testapp_validate_traffic(test);
1037 }
1038
1039 static void testapp_stats(struct test_spec *test)
1040 {
1041         int i;
1042
1043         for (i = 0; i < STAT_TEST_TYPE_MAX; i++) {
1044                 test_spec_reset(test);
1045                 stat_test_type = i;
1046
1047                 switch (stat_test_type) {
1048                 case STAT_TEST_RX_DROPPED:
1049                         test_spec_set_name(test, "STAT_RX_DROPPED");
1050                         test->ifobj_rx->umem->frame_headroom = test->ifobj_rx->umem->frame_size -
1051                                 XDP_PACKET_HEADROOM - 1;
1052                         testapp_validate_traffic(test);
1053                         break;
1054                 case STAT_TEST_RX_FULL:
1055                         test_spec_set_name(test, "STAT_RX_FULL");
1056                         test->ifobj_rx->xsk->rxqsize = RX_FULL_RXQSIZE;
1057                         testapp_validate_traffic(test);
1058                         break;
1059                 case STAT_TEST_TX_INVALID:
1060                         test_spec_set_name(test, "STAT_TX_INVALID");
1061                         pkt_stream_replace(test, DEFAULT_PKT_CNT, XSK_UMEM__INVALID_FRAME_SIZE);
1062                         testapp_validate_traffic(test);
1063
1064                         pkt_stream_restore_default(test);
1065                         break;
1066                 case STAT_TEST_RX_FILL_EMPTY:
1067                         test_spec_set_name(test, "STAT_RX_FILL_EMPTY");
1068                         testapp_validate_traffic(test);
1069                         break;
1070                 default:
1071                         break;
1072                 }
1073         }
1074
1075         /* To only see the whole stat set being completed unless an individual test fails. */
1076         test_spec_set_name(test, "STATS");
1077 }
1078
1079 /* Simple test */
1080 static bool hugepages_present(struct ifobject *ifobject)
1081 {
1082         const size_t mmap_sz = 2 * ifobject->umem->num_frames * ifobject->umem->frame_size;
1083         void *bufs;
1084
1085         bufs = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE,
1086                     MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_HUGETLB, -1, 0);
1087         if (bufs == MAP_FAILED)
1088                 return false;
1089
1090         munmap(bufs, mmap_sz);
1091         return true;
1092 }
1093
1094 static bool testapp_unaligned(struct test_spec *test)
1095 {
1096         if (!hugepages_present(test->ifobj_tx)) {
1097                 ksft_test_result_skip("No 2M huge pages present.\n");
1098                 return false;
1099         }
1100
1101         test_spec_set_name(test, "UNALIGNED_MODE");
1102         test->ifobj_tx->umem->unaligned_mode = true;
1103         test->ifobj_rx->umem->unaligned_mode = true;
1104         /* Let half of the packets straddle a buffer boundrary */
1105         pkt_stream_replace_half(test, PKT_SIZE, test->ifobj_tx->umem->frame_size - 32);
1106         test->ifobj_rx->pkt_stream->use_addr_for_fill = true;
1107         testapp_validate_traffic(test);
1108
1109         pkt_stream_restore_default(test);
1110         return true;
1111 }
1112
1113 static void init_iface(struct ifobject *ifobj, const char *dst_mac, const char *src_mac,
1114                        const char *dst_ip, const char *src_ip, const u16 dst_port,
1115                        const u16 src_port, thread_func_t func_ptr)
1116 {
1117         struct in_addr ip;
1118
1119         memcpy(ifobj->dst_mac, dst_mac, ETH_ALEN);
1120         memcpy(ifobj->src_mac, src_mac, ETH_ALEN);
1121
1122         inet_aton(dst_ip, &ip);
1123         ifobj->dst_ip = ip.s_addr;
1124
1125         inet_aton(src_ip, &ip);
1126         ifobj->src_ip = ip.s_addr;
1127
1128         ifobj->dst_port = dst_port;
1129         ifobj->src_port = src_port;
1130
1131         ifobj->func_ptr = func_ptr;
1132 }
1133
1134 static void run_pkt_test(struct test_spec *test, enum test_mode mode, enum test_type type)
1135 {
1136         test_type = type;
1137
1138         /* reset defaults after potential previous test */
1139         stat_test_type = -1;
1140
1141         switch (test_type) {
1142         case TEST_TYPE_STATS:
1143                 testapp_stats(test);
1144                 break;
1145         case TEST_TYPE_TEARDOWN:
1146                 testapp_teardown(test);
1147                 break;
1148         case TEST_TYPE_BIDI:
1149                 testapp_bidi(test);
1150                 break;
1151         case TEST_TYPE_BPF_RES:
1152                 testapp_bpf_res(test);
1153                 break;
1154         case TEST_TYPE_NOPOLL:
1155                 test_spec_set_name(test, "RUN_TO_COMPLETION");
1156                 testapp_validate_traffic(test);
1157                 break;
1158         case TEST_TYPE_POLL:
1159                 test->ifobj_tx->use_poll = true;
1160                 test->ifobj_rx->use_poll = true;
1161                 test_spec_set_name(test, "POLL");
1162                 testapp_validate_traffic(test);
1163                 break;
1164         case TEST_TYPE_UNALIGNED:
1165                 if (!testapp_unaligned(test))
1166                         return;
1167                 break;
1168         default:
1169                 break;
1170         }
1171
1172         print_ksft_result(test);
1173 }
1174
1175 static struct ifobject *ifobject_create(void)
1176 {
1177         struct ifobject *ifobj;
1178
1179         ifobj = calloc(1, sizeof(struct ifobject));
1180         if (!ifobj)
1181                 return NULL;
1182
1183         ifobj->xsk_arr = calloc(MAX_SOCKETS, sizeof(*ifobj->xsk_arr));
1184         if (!ifobj->xsk_arr)
1185                 goto out_xsk_arr;
1186
1187         ifobj->umem_arr = calloc(MAX_SOCKETS, sizeof(*ifobj->umem_arr));
1188         if (!ifobj->umem_arr)
1189                 goto out_umem_arr;
1190
1191         return ifobj;
1192
1193 out_umem_arr:
1194         free(ifobj->xsk_arr);
1195 out_xsk_arr:
1196         free(ifobj);
1197         return NULL;
1198 }
1199
1200 static void ifobject_delete(struct ifobject *ifobj)
1201 {
1202         free(ifobj->umem_arr);
1203         free(ifobj->xsk_arr);
1204         free(ifobj);
1205 }
1206
1207 int main(int argc, char **argv)
1208 {
1209         struct rlimit _rlim = { RLIM_INFINITY, RLIM_INFINITY };
1210         struct pkt_stream *pkt_stream_default;
1211         struct ifobject *ifobj_tx, *ifobj_rx;
1212         struct test_spec test;
1213         u32 i, j;
1214
1215         if (setrlimit(RLIMIT_MEMLOCK, &_rlim))
1216                 exit_with_error(errno);
1217
1218         ifobj_tx = ifobject_create();
1219         if (!ifobj_tx)
1220                 exit_with_error(ENOMEM);
1221         ifobj_rx = ifobject_create();
1222         if (!ifobj_rx)
1223                 exit_with_error(ENOMEM);
1224
1225         setlocale(LC_ALL, "");
1226
1227         parse_command_line(ifobj_tx, ifobj_rx, argc, argv);
1228
1229         if (!validate_interface(ifobj_tx) || !validate_interface(ifobj_rx)) {
1230                 usage(basename(argv[0]));
1231                 ksft_exit_xfail();
1232         }
1233
1234         init_iface(ifobj_tx, MAC1, MAC2, IP1, IP2, UDP_PORT1, UDP_PORT2,
1235                    worker_testapp_validate_tx);
1236         init_iface(ifobj_rx, MAC2, MAC1, IP2, IP1, UDP_PORT2, UDP_PORT1,
1237                    worker_testapp_validate_rx);
1238
1239         test_spec_init(&test, ifobj_tx, ifobj_rx, 0);
1240         pkt_stream_default = pkt_stream_generate(ifobj_tx->umem, DEFAULT_PKT_CNT, PKT_SIZE);
1241         if (!pkt_stream_default)
1242                 exit_with_error(ENOMEM);
1243         test.pkt_stream_default = pkt_stream_default;
1244
1245         ksft_set_plan(TEST_MODE_MAX * TEST_TYPE_MAX);
1246
1247         for (i = 0; i < TEST_MODE_MAX; i++)
1248                 for (j = 0; j < TEST_TYPE_MAX; j++) {
1249                         test_spec_init(&test, ifobj_tx, ifobj_rx, i);
1250                         run_pkt_test(&test, i, j);
1251                         usleep(USLEEP_MAX);
1252                 }
1253
1254         pkt_stream_delete(pkt_stream_default);
1255         ifobject_delete(ifobj_tx);
1256         ifobject_delete(ifobj_rx);
1257
1258         ksft_exit_pass();
1259         return 0;
1260 }