14418919fSjohnjiang /* SPDX-License-Identifier: BSD-3-Clause
24418919fSjohnjiang * Copyright(c) 2010-2017 Intel Corporation
34418919fSjohnjiang */
44418919fSjohnjiang
54418919fSjohnjiang #include "test.h"
64418919fSjohnjiang
74418919fSjohnjiang #include <unistd.h>
84418919fSjohnjiang #include <string.h>
94418919fSjohnjiang #include <rte_cycles.h>
104418919fSjohnjiang #include <rte_errno.h>
114418919fSjohnjiang #include <rte_mempool.h>
124418919fSjohnjiang #include <rte_mbuf.h>
13*2d9fd380Sjfb8856606 #include <rte_mbuf_dyn.h>
144418919fSjohnjiang #include <rte_distributor.h>
154418919fSjohnjiang #include <rte_string_fns.h>
164418919fSjohnjiang
174418919fSjohnjiang #define ITER_POWER 20 /* log 2 of how many iterations we do when timing. */
184418919fSjohnjiang #define BURST 32
194418919fSjohnjiang #define BIG_BATCH 1024
204418919fSjohnjiang
21*2d9fd380Sjfb8856606 typedef uint32_t seq_dynfield_t;
22*2d9fd380Sjfb8856606 static int seq_dynfield_offset = -1;
23*2d9fd380Sjfb8856606
24*2d9fd380Sjfb8856606 static inline seq_dynfield_t *
seq_field(struct rte_mbuf * mbuf)25*2d9fd380Sjfb8856606 seq_field(struct rte_mbuf *mbuf)
26*2d9fd380Sjfb8856606 {
27*2d9fd380Sjfb8856606 return RTE_MBUF_DYNFIELD(mbuf, seq_dynfield_offset, seq_dynfield_t *);
28*2d9fd380Sjfb8856606 }
29*2d9fd380Sjfb8856606
304418919fSjohnjiang struct worker_params {
314418919fSjohnjiang char name[64];
324418919fSjohnjiang struct rte_distributor *dist;
334418919fSjohnjiang };
344418919fSjohnjiang
354418919fSjohnjiang struct worker_params worker_params;
364418919fSjohnjiang
374418919fSjohnjiang /* statics - all zero-initialized by default */
384418919fSjohnjiang static volatile int quit; /**< general quit variable for all threads */
394418919fSjohnjiang static volatile int zero_quit; /**< var for when we just want thr0 to quit*/
400c6bd470Sfengbojiang static volatile int zero_sleep; /**< thr0 has quit basic loop and is sleeping*/
414418919fSjohnjiang static volatile unsigned worker_idx;
420c6bd470Sfengbojiang static volatile unsigned zero_idx;
434418919fSjohnjiang
444418919fSjohnjiang struct worker_stats {
454418919fSjohnjiang volatile unsigned handled_packets;
464418919fSjohnjiang } __rte_cache_aligned;
474418919fSjohnjiang struct worker_stats worker_stats[RTE_MAX_LCORE];
484418919fSjohnjiang
494418919fSjohnjiang /* returns the total count of the number of packets handled by the worker
504418919fSjohnjiang * functions given below.
514418919fSjohnjiang */
524418919fSjohnjiang static inline unsigned
total_packet_count(void)534418919fSjohnjiang total_packet_count(void)
544418919fSjohnjiang {
554418919fSjohnjiang unsigned i, count = 0;
564418919fSjohnjiang for (i = 0; i < worker_idx; i++)
570c6bd470Sfengbojiang count += __atomic_load_n(&worker_stats[i].handled_packets,
580c6bd470Sfengbojiang __ATOMIC_RELAXED);
594418919fSjohnjiang return count;
604418919fSjohnjiang }
614418919fSjohnjiang
624418919fSjohnjiang /* resets the packet counts for a new test */
634418919fSjohnjiang static inline void
clear_packet_count(void)644418919fSjohnjiang clear_packet_count(void)
654418919fSjohnjiang {
660c6bd470Sfengbojiang unsigned int i;
670c6bd470Sfengbojiang for (i = 0; i < RTE_MAX_LCORE; i++)
680c6bd470Sfengbojiang __atomic_store_n(&worker_stats[i].handled_packets, 0,
690c6bd470Sfengbojiang __ATOMIC_RELAXED);
704418919fSjohnjiang }
714418919fSjohnjiang
724418919fSjohnjiang /* this is the basic worker function for sanity test
734418919fSjohnjiang * it does nothing but return packets and count them.
744418919fSjohnjiang */
754418919fSjohnjiang static int
handle_work(void * arg)764418919fSjohnjiang handle_work(void *arg)
774418919fSjohnjiang {
784418919fSjohnjiang struct rte_mbuf *buf[8] __rte_cache_aligned;
794418919fSjohnjiang struct worker_params *wp = arg;
804418919fSjohnjiang struct rte_distributor *db = wp->dist;
810c6bd470Sfengbojiang unsigned int num;
824418919fSjohnjiang unsigned int id = __atomic_fetch_add(&worker_idx, 1, __ATOMIC_RELAXED);
834418919fSjohnjiang
840c6bd470Sfengbojiang num = rte_distributor_get_pkt(db, id, buf, NULL, 0);
854418919fSjohnjiang while (!quit) {
864418919fSjohnjiang __atomic_fetch_add(&worker_stats[id].handled_packets, num,
874418919fSjohnjiang __ATOMIC_RELAXED);
884418919fSjohnjiang num = rte_distributor_get_pkt(db, id,
894418919fSjohnjiang buf, buf, num);
904418919fSjohnjiang }
914418919fSjohnjiang __atomic_fetch_add(&worker_stats[id].handled_packets, num,
924418919fSjohnjiang __ATOMIC_RELAXED);
934418919fSjohnjiang rte_distributor_return_pkt(db, id, buf, num);
944418919fSjohnjiang return 0;
954418919fSjohnjiang }
964418919fSjohnjiang
974418919fSjohnjiang /* do basic sanity testing of the distributor. This test tests the following:
984418919fSjohnjiang * - send 32 packets through distributor with the same tag and ensure they
994418919fSjohnjiang * all go to the one worker
1004418919fSjohnjiang * - send 32 packets through the distributor with two different tags and
1014418919fSjohnjiang * verify that they go equally to two different workers.
1024418919fSjohnjiang * - send 32 packets with different tags through the distributors and
1034418919fSjohnjiang * just verify we get all packets back.
1044418919fSjohnjiang * - send 1024 packets through the distributor, gathering the returned packets
1054418919fSjohnjiang * as we go. Then verify that we correctly got all 1024 pointers back again,
1064418919fSjohnjiang * not necessarily in the same order (as different flows).
1074418919fSjohnjiang */
1084418919fSjohnjiang static int
sanity_test(struct worker_params * wp,struct rte_mempool * p)1094418919fSjohnjiang sanity_test(struct worker_params *wp, struct rte_mempool *p)
1104418919fSjohnjiang {
1114418919fSjohnjiang struct rte_distributor *db = wp->dist;
1124418919fSjohnjiang struct rte_mbuf *bufs[BURST];
1134418919fSjohnjiang struct rte_mbuf *returns[BURST*2];
1144418919fSjohnjiang unsigned int i, count;
1154418919fSjohnjiang unsigned int retries;
1160c6bd470Sfengbojiang unsigned int processed;
1174418919fSjohnjiang
1184418919fSjohnjiang printf("=== Basic distributor sanity tests ===\n");
1194418919fSjohnjiang clear_packet_count();
1204418919fSjohnjiang if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
1214418919fSjohnjiang printf("line %d: Error getting mbufs from pool\n", __LINE__);
1224418919fSjohnjiang return -1;
1234418919fSjohnjiang }
1244418919fSjohnjiang
1254418919fSjohnjiang /* now set all hash values in all buffers to zero, so all pkts go to the
1264418919fSjohnjiang * one worker thread */
1274418919fSjohnjiang for (i = 0; i < BURST; i++)
1284418919fSjohnjiang bufs[i]->hash.usr = 0;
1294418919fSjohnjiang
1300c6bd470Sfengbojiang processed = 0;
1310c6bd470Sfengbojiang while (processed < BURST)
1320c6bd470Sfengbojiang processed += rte_distributor_process(db, &bufs[processed],
1330c6bd470Sfengbojiang BURST - processed);
1340c6bd470Sfengbojiang
1354418919fSjohnjiang count = 0;
1364418919fSjohnjiang do {
1374418919fSjohnjiang
1384418919fSjohnjiang rte_distributor_flush(db);
1394418919fSjohnjiang count += rte_distributor_returned_pkts(db,
1404418919fSjohnjiang returns, BURST*2);
1414418919fSjohnjiang } while (count < BURST);
1424418919fSjohnjiang
1434418919fSjohnjiang if (total_packet_count() != BURST) {
1444418919fSjohnjiang printf("Line %d: Error, not all packets flushed. "
1454418919fSjohnjiang "Expected %u, got %u\n",
1464418919fSjohnjiang __LINE__, BURST, total_packet_count());
1470c6bd470Sfengbojiang rte_mempool_put_bulk(p, (void *)bufs, BURST);
1484418919fSjohnjiang return -1;
1494418919fSjohnjiang }
1504418919fSjohnjiang
1514418919fSjohnjiang for (i = 0; i < rte_lcore_count() - 1; i++)
1524418919fSjohnjiang printf("Worker %u handled %u packets\n", i,
1530c6bd470Sfengbojiang __atomic_load_n(&worker_stats[i].handled_packets,
1540c6bd470Sfengbojiang __ATOMIC_RELAXED));
1554418919fSjohnjiang printf("Sanity test with all zero hashes done.\n");
1564418919fSjohnjiang
1574418919fSjohnjiang /* pick two flows and check they go correctly */
1584418919fSjohnjiang if (rte_lcore_count() >= 3) {
1594418919fSjohnjiang clear_packet_count();
1604418919fSjohnjiang for (i = 0; i < BURST; i++)
1614418919fSjohnjiang bufs[i]->hash.usr = (i & 1) << 8;
1624418919fSjohnjiang
1634418919fSjohnjiang rte_distributor_process(db, bufs, BURST);
1644418919fSjohnjiang count = 0;
1654418919fSjohnjiang do {
1664418919fSjohnjiang rte_distributor_flush(db);
1674418919fSjohnjiang count += rte_distributor_returned_pkts(db,
1684418919fSjohnjiang returns, BURST*2);
1694418919fSjohnjiang } while (count < BURST);
1704418919fSjohnjiang if (total_packet_count() != BURST) {
1714418919fSjohnjiang printf("Line %d: Error, not all packets flushed. "
1724418919fSjohnjiang "Expected %u, got %u\n",
1734418919fSjohnjiang __LINE__, BURST, total_packet_count());
1740c6bd470Sfengbojiang rte_mempool_put_bulk(p, (void *)bufs, BURST);
1754418919fSjohnjiang return -1;
1764418919fSjohnjiang }
1774418919fSjohnjiang
1784418919fSjohnjiang for (i = 0; i < rte_lcore_count() - 1; i++)
1794418919fSjohnjiang printf("Worker %u handled %u packets\n", i,
1800c6bd470Sfengbojiang __atomic_load_n(
1810c6bd470Sfengbojiang &worker_stats[i].handled_packets,
1820c6bd470Sfengbojiang __ATOMIC_RELAXED));
1834418919fSjohnjiang printf("Sanity test with two hash values done\n");
1844418919fSjohnjiang }
1854418919fSjohnjiang
1864418919fSjohnjiang /* give a different hash value to each packet,
1874418919fSjohnjiang * so load gets distributed */
1884418919fSjohnjiang clear_packet_count();
1894418919fSjohnjiang for (i = 0; i < BURST; i++)
1904418919fSjohnjiang bufs[i]->hash.usr = i+1;
1914418919fSjohnjiang
1924418919fSjohnjiang rte_distributor_process(db, bufs, BURST);
1934418919fSjohnjiang count = 0;
1944418919fSjohnjiang do {
1954418919fSjohnjiang rte_distributor_flush(db);
1964418919fSjohnjiang count += rte_distributor_returned_pkts(db,
1974418919fSjohnjiang returns, BURST*2);
1984418919fSjohnjiang } while (count < BURST);
1994418919fSjohnjiang if (total_packet_count() != BURST) {
2004418919fSjohnjiang printf("Line %d: Error, not all packets flushed. "
2014418919fSjohnjiang "Expected %u, got %u\n",
2024418919fSjohnjiang __LINE__, BURST, total_packet_count());
2030c6bd470Sfengbojiang rte_mempool_put_bulk(p, (void *)bufs, BURST);
2044418919fSjohnjiang return -1;
2054418919fSjohnjiang }
2064418919fSjohnjiang
2074418919fSjohnjiang for (i = 0; i < rte_lcore_count() - 1; i++)
2084418919fSjohnjiang printf("Worker %u handled %u packets\n", i,
2090c6bd470Sfengbojiang __atomic_load_n(&worker_stats[i].handled_packets,
2100c6bd470Sfengbojiang __ATOMIC_RELAXED));
2114418919fSjohnjiang printf("Sanity test with non-zero hashes done\n");
2124418919fSjohnjiang
2134418919fSjohnjiang rte_mempool_put_bulk(p, (void *)bufs, BURST);
2144418919fSjohnjiang
2154418919fSjohnjiang /* sanity test with BIG_BATCH packets to ensure they all arrived back
2164418919fSjohnjiang * from the returned packets function */
2174418919fSjohnjiang clear_packet_count();
2184418919fSjohnjiang struct rte_mbuf *many_bufs[BIG_BATCH], *return_bufs[BIG_BATCH];
2194418919fSjohnjiang unsigned num_returned = 0;
2204418919fSjohnjiang
2214418919fSjohnjiang /* flush out any remaining packets */
2224418919fSjohnjiang rte_distributor_flush(db);
2234418919fSjohnjiang rte_distributor_clear_returns(db);
2244418919fSjohnjiang
2254418919fSjohnjiang if (rte_mempool_get_bulk(p, (void *)many_bufs, BIG_BATCH) != 0) {
2264418919fSjohnjiang printf("line %d: Error getting mbufs from pool\n", __LINE__);
2274418919fSjohnjiang return -1;
2284418919fSjohnjiang }
2294418919fSjohnjiang for (i = 0; i < BIG_BATCH; i++)
2304418919fSjohnjiang many_bufs[i]->hash.usr = i << 2;
2314418919fSjohnjiang
2324418919fSjohnjiang printf("=== testing big burst (%s) ===\n", wp->name);
2334418919fSjohnjiang for (i = 0; i < BIG_BATCH/BURST; i++) {
2344418919fSjohnjiang rte_distributor_process(db,
2354418919fSjohnjiang &many_bufs[i*BURST], BURST);
2364418919fSjohnjiang count = rte_distributor_returned_pkts(db,
2374418919fSjohnjiang &return_bufs[num_returned],
2384418919fSjohnjiang BIG_BATCH - num_returned);
2394418919fSjohnjiang num_returned += count;
2404418919fSjohnjiang }
2414418919fSjohnjiang rte_distributor_flush(db);
2424418919fSjohnjiang count = rte_distributor_returned_pkts(db,
2434418919fSjohnjiang &return_bufs[num_returned],
2444418919fSjohnjiang BIG_BATCH - num_returned);
2454418919fSjohnjiang num_returned += count;
2464418919fSjohnjiang retries = 0;
2474418919fSjohnjiang do {
2484418919fSjohnjiang rte_distributor_flush(db);
2494418919fSjohnjiang count = rte_distributor_returned_pkts(db,
2504418919fSjohnjiang &return_bufs[num_returned],
2514418919fSjohnjiang BIG_BATCH - num_returned);
2524418919fSjohnjiang num_returned += count;
2534418919fSjohnjiang retries++;
2544418919fSjohnjiang } while ((num_returned < BIG_BATCH) && (retries < 100));
2554418919fSjohnjiang
2564418919fSjohnjiang if (num_returned != BIG_BATCH) {
2574418919fSjohnjiang printf("line %d: Missing packets, expected %d\n",
2584418919fSjohnjiang __LINE__, num_returned);
2590c6bd470Sfengbojiang rte_mempool_put_bulk(p, (void *)many_bufs, BIG_BATCH);
2604418919fSjohnjiang return -1;
2614418919fSjohnjiang }
2624418919fSjohnjiang
2634418919fSjohnjiang /* big check - make sure all packets made it back!! */
2644418919fSjohnjiang for (i = 0; i < BIG_BATCH; i++) {
2654418919fSjohnjiang unsigned j;
2664418919fSjohnjiang struct rte_mbuf *src = many_bufs[i];
2674418919fSjohnjiang for (j = 0; j < BIG_BATCH; j++) {
2684418919fSjohnjiang if (return_bufs[j] == src)
2694418919fSjohnjiang break;
2704418919fSjohnjiang }
2714418919fSjohnjiang
2724418919fSjohnjiang if (j == BIG_BATCH) {
2734418919fSjohnjiang printf("Error: could not find source packet #%u\n", i);
2740c6bd470Sfengbojiang rte_mempool_put_bulk(p, (void *)many_bufs, BIG_BATCH);
2754418919fSjohnjiang return -1;
2764418919fSjohnjiang }
2774418919fSjohnjiang }
2784418919fSjohnjiang printf("Sanity test of returned packets done\n");
2794418919fSjohnjiang
2804418919fSjohnjiang rte_mempool_put_bulk(p, (void *)many_bufs, BIG_BATCH);
2814418919fSjohnjiang
2824418919fSjohnjiang printf("\n");
2834418919fSjohnjiang return 0;
2844418919fSjohnjiang }
2854418919fSjohnjiang
2864418919fSjohnjiang
2874418919fSjohnjiang /* to test that the distributor does not lose packets, we use this worker
2884418919fSjohnjiang * function which frees mbufs when it gets them. The distributor thread does
2894418919fSjohnjiang * the mbuf allocation. If distributor drops packets we'll eventually run out
2904418919fSjohnjiang * of mbufs.
2914418919fSjohnjiang */
2924418919fSjohnjiang static int
handle_work_with_free_mbufs(void * arg)2934418919fSjohnjiang handle_work_with_free_mbufs(void *arg)
2944418919fSjohnjiang {
2954418919fSjohnjiang struct rte_mbuf *buf[8] __rte_cache_aligned;
2964418919fSjohnjiang struct worker_params *wp = arg;
2974418919fSjohnjiang struct rte_distributor *d = wp->dist;
2984418919fSjohnjiang unsigned int i;
2990c6bd470Sfengbojiang unsigned int num;
3004418919fSjohnjiang unsigned int id = __atomic_fetch_add(&worker_idx, 1, __ATOMIC_RELAXED);
3014418919fSjohnjiang
3020c6bd470Sfengbojiang num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
3034418919fSjohnjiang while (!quit) {
3040c6bd470Sfengbojiang __atomic_fetch_add(&worker_stats[id].handled_packets, num,
3050c6bd470Sfengbojiang __ATOMIC_RELAXED);
3064418919fSjohnjiang for (i = 0; i < num; i++)
3074418919fSjohnjiang rte_pktmbuf_free(buf[i]);
3080c6bd470Sfengbojiang num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
3094418919fSjohnjiang }
3100c6bd470Sfengbojiang __atomic_fetch_add(&worker_stats[id].handled_packets, num,
3110c6bd470Sfengbojiang __ATOMIC_RELAXED);
3124418919fSjohnjiang rte_distributor_return_pkt(d, id, buf, num);
3134418919fSjohnjiang return 0;
3144418919fSjohnjiang }
3154418919fSjohnjiang
3164418919fSjohnjiang /* Perform a sanity test of the distributor with a large number of packets,
3174418919fSjohnjiang * where we allocate a new set of mbufs for each burst. The workers then
3184418919fSjohnjiang * free the mbufs. This ensures that we don't have any packet leaks in the
3194418919fSjohnjiang * library.
3204418919fSjohnjiang */
3214418919fSjohnjiang static int
sanity_test_with_mbuf_alloc(struct worker_params * wp,struct rte_mempool * p)3224418919fSjohnjiang sanity_test_with_mbuf_alloc(struct worker_params *wp, struct rte_mempool *p)
3234418919fSjohnjiang {
3244418919fSjohnjiang struct rte_distributor *d = wp->dist;
3254418919fSjohnjiang unsigned i;
3264418919fSjohnjiang struct rte_mbuf *bufs[BURST];
3270c6bd470Sfengbojiang unsigned int processed;
3284418919fSjohnjiang
3294418919fSjohnjiang printf("=== Sanity test with mbuf alloc/free (%s) ===\n", wp->name);
3304418919fSjohnjiang
3314418919fSjohnjiang clear_packet_count();
3324418919fSjohnjiang for (i = 0; i < ((1<<ITER_POWER)); i += BURST) {
3334418919fSjohnjiang unsigned j;
3344418919fSjohnjiang while (rte_mempool_get_bulk(p, (void *)bufs, BURST) < 0)
3354418919fSjohnjiang rte_distributor_process(d, NULL, 0);
3364418919fSjohnjiang for (j = 0; j < BURST; j++) {
3374418919fSjohnjiang bufs[j]->hash.usr = (i+j) << 1;
3384418919fSjohnjiang }
3394418919fSjohnjiang
3400c6bd470Sfengbojiang processed = 0;
3410c6bd470Sfengbojiang while (processed < BURST)
3420c6bd470Sfengbojiang processed += rte_distributor_process(d,
3430c6bd470Sfengbojiang &bufs[processed], BURST - processed);
3444418919fSjohnjiang }
3454418919fSjohnjiang
3464418919fSjohnjiang rte_distributor_flush(d);
3474418919fSjohnjiang
3484418919fSjohnjiang rte_delay_us(10000);
3494418919fSjohnjiang
3504418919fSjohnjiang if (total_packet_count() < (1<<ITER_POWER)) {
3514418919fSjohnjiang printf("Line %u: Packet count is incorrect, %u, expected %u\n",
3524418919fSjohnjiang __LINE__, total_packet_count(),
3534418919fSjohnjiang (1<<ITER_POWER));
3544418919fSjohnjiang return -1;
3554418919fSjohnjiang }
3564418919fSjohnjiang
3574418919fSjohnjiang printf("Sanity test with mbuf alloc/free passed\n\n");
3584418919fSjohnjiang return 0;
3594418919fSjohnjiang }
3604418919fSjohnjiang
3614418919fSjohnjiang static int
handle_work_for_shutdown_test(void * arg)3624418919fSjohnjiang handle_work_for_shutdown_test(void *arg)
3634418919fSjohnjiang {
3644418919fSjohnjiang struct rte_mbuf *buf[8] __rte_cache_aligned;
3654418919fSjohnjiang struct worker_params *wp = arg;
3664418919fSjohnjiang struct rte_distributor *d = wp->dist;
3670c6bd470Sfengbojiang unsigned int num;
3680c6bd470Sfengbojiang unsigned int zero_id = 0;
3690c6bd470Sfengbojiang unsigned int zero_unset;
3704418919fSjohnjiang const unsigned int id = __atomic_fetch_add(&worker_idx, 1,
3714418919fSjohnjiang __ATOMIC_RELAXED);
3724418919fSjohnjiang
3730c6bd470Sfengbojiang num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
3740c6bd470Sfengbojiang
3750c6bd470Sfengbojiang if (num > 0) {
3760c6bd470Sfengbojiang zero_unset = RTE_MAX_LCORE;
3770c6bd470Sfengbojiang __atomic_compare_exchange_n(&zero_idx, &zero_unset, id,
378*2d9fd380Sjfb8856606 false, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
3790c6bd470Sfengbojiang }
3800c6bd470Sfengbojiang zero_id = __atomic_load_n(&zero_idx, __ATOMIC_ACQUIRE);
3814418919fSjohnjiang
3824418919fSjohnjiang /* wait for quit single globally, or for worker zero, wait
3834418919fSjohnjiang * for zero_quit */
3840c6bd470Sfengbojiang while (!quit && !(id == zero_id && zero_quit)) {
3850c6bd470Sfengbojiang __atomic_fetch_add(&worker_stats[id].handled_packets, num,
3860c6bd470Sfengbojiang __ATOMIC_RELAXED);
3870c6bd470Sfengbojiang num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
3884418919fSjohnjiang
3890c6bd470Sfengbojiang if (num > 0) {
3900c6bd470Sfengbojiang zero_unset = RTE_MAX_LCORE;
3910c6bd470Sfengbojiang __atomic_compare_exchange_n(&zero_idx, &zero_unset, id,
392*2d9fd380Sjfb8856606 false, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
3930c6bd470Sfengbojiang }
3940c6bd470Sfengbojiang zero_id = __atomic_load_n(&zero_idx, __ATOMIC_ACQUIRE);
3950c6bd470Sfengbojiang }
3960c6bd470Sfengbojiang
3970c6bd470Sfengbojiang __atomic_fetch_add(&worker_stats[id].handled_packets, num,
3980c6bd470Sfengbojiang __ATOMIC_RELAXED);
3990c6bd470Sfengbojiang if (id == zero_id) {
4000c6bd470Sfengbojiang rte_distributor_return_pkt(d, id, NULL, 0);
4010c6bd470Sfengbojiang
4024418919fSjohnjiang /* for worker zero, allow it to restart to pick up last packet
4034418919fSjohnjiang * when all workers are shutting down.
4044418919fSjohnjiang */
4050c6bd470Sfengbojiang __atomic_store_n(&zero_sleep, 1, __ATOMIC_RELEASE);
4064418919fSjohnjiang while (zero_quit)
4074418919fSjohnjiang usleep(100);
4080c6bd470Sfengbojiang __atomic_store_n(&zero_sleep, 0, __ATOMIC_RELEASE);
4094418919fSjohnjiang
4100c6bd470Sfengbojiang num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
4114418919fSjohnjiang
4124418919fSjohnjiang while (!quit) {
4130c6bd470Sfengbojiang __atomic_fetch_add(&worker_stats[id].handled_packets,
4140c6bd470Sfengbojiang num, __ATOMIC_RELAXED);
4150c6bd470Sfengbojiang num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
4164418919fSjohnjiang }
4174418919fSjohnjiang }
4180c6bd470Sfengbojiang rte_distributor_return_pkt(d, id, buf, num);
4194418919fSjohnjiang return 0;
4204418919fSjohnjiang }
4214418919fSjohnjiang
4224418919fSjohnjiang
4234418919fSjohnjiang /* Perform a sanity test of the distributor with a large number of packets,
4244418919fSjohnjiang * where we allocate a new set of mbufs for each burst. The workers then
4254418919fSjohnjiang * free the mbufs. This ensures that we don't have any packet leaks in the
4264418919fSjohnjiang * library.
4274418919fSjohnjiang */
4284418919fSjohnjiang static int
sanity_test_with_worker_shutdown(struct worker_params * wp,struct rte_mempool * p)4294418919fSjohnjiang sanity_test_with_worker_shutdown(struct worker_params *wp,
4304418919fSjohnjiang struct rte_mempool *p)
4314418919fSjohnjiang {
4324418919fSjohnjiang struct rte_distributor *d = wp->dist;
4334418919fSjohnjiang struct rte_mbuf *bufs[BURST];
4340c6bd470Sfengbojiang struct rte_mbuf *bufs2[BURST];
4350c6bd470Sfengbojiang unsigned int i;
4360c6bd470Sfengbojiang unsigned int failed = 0;
4370c6bd470Sfengbojiang unsigned int processed = 0;
4384418919fSjohnjiang
4394418919fSjohnjiang printf("=== Sanity test of worker shutdown ===\n");
4404418919fSjohnjiang
4414418919fSjohnjiang clear_packet_count();
4424418919fSjohnjiang
4434418919fSjohnjiang if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
4444418919fSjohnjiang printf("line %d: Error getting mbufs from pool\n", __LINE__);
4454418919fSjohnjiang return -1;
4464418919fSjohnjiang }
4474418919fSjohnjiang
4484418919fSjohnjiang /*
4494418919fSjohnjiang * Now set all hash values in all buffers to same value so all
4504418919fSjohnjiang * pkts go to the one worker thread
4514418919fSjohnjiang */
4524418919fSjohnjiang for (i = 0; i < BURST; i++)
4534418919fSjohnjiang bufs[i]->hash.usr = 1;
4544418919fSjohnjiang
4550c6bd470Sfengbojiang processed = 0;
4560c6bd470Sfengbojiang while (processed < BURST)
4570c6bd470Sfengbojiang processed += rte_distributor_process(d, &bufs[processed],
4580c6bd470Sfengbojiang BURST - processed);
4594418919fSjohnjiang rte_distributor_flush(d);
4604418919fSjohnjiang
4614418919fSjohnjiang /* at this point, we will have processed some packets and have a full
4624418919fSjohnjiang * backlog for the other ones at worker 0.
4634418919fSjohnjiang */
4644418919fSjohnjiang
4654418919fSjohnjiang /* get more buffers to queue up, again setting them to the same flow */
4660c6bd470Sfengbojiang if (rte_mempool_get_bulk(p, (void *)bufs2, BURST) != 0) {
4674418919fSjohnjiang printf("line %d: Error getting mbufs from pool\n", __LINE__);
4680c6bd470Sfengbojiang rte_mempool_put_bulk(p, (void *)bufs, BURST);
4694418919fSjohnjiang return -1;
4704418919fSjohnjiang }
4714418919fSjohnjiang for (i = 0; i < BURST; i++)
4720c6bd470Sfengbojiang bufs2[i]->hash.usr = 1;
4734418919fSjohnjiang
4744418919fSjohnjiang /* get worker zero to quit */
4754418919fSjohnjiang zero_quit = 1;
4760c6bd470Sfengbojiang rte_distributor_process(d, bufs2, BURST);
4774418919fSjohnjiang
4784418919fSjohnjiang /* flush the distributor */
4794418919fSjohnjiang rte_distributor_flush(d);
4800c6bd470Sfengbojiang while (!__atomic_load_n(&zero_sleep, __ATOMIC_ACQUIRE))
4810c6bd470Sfengbojiang rte_distributor_flush(d);
4820c6bd470Sfengbojiang
4830c6bd470Sfengbojiang zero_quit = 0;
4840c6bd470Sfengbojiang while (__atomic_load_n(&zero_sleep, __ATOMIC_ACQUIRE))
4850c6bd470Sfengbojiang rte_delay_us(100);
4864418919fSjohnjiang
4874418919fSjohnjiang for (i = 0; i < rte_lcore_count() - 1; i++)
4884418919fSjohnjiang printf("Worker %u handled %u packets\n", i,
4890c6bd470Sfengbojiang __atomic_load_n(&worker_stats[i].handled_packets,
4900c6bd470Sfengbojiang __ATOMIC_RELAXED));
4914418919fSjohnjiang
4924418919fSjohnjiang if (total_packet_count() != BURST * 2) {
4934418919fSjohnjiang printf("Line %d: Error, not all packets flushed. "
4944418919fSjohnjiang "Expected %u, got %u\n",
4954418919fSjohnjiang __LINE__, BURST * 2, total_packet_count());
4960c6bd470Sfengbojiang failed = 1;
4974418919fSjohnjiang }
4984418919fSjohnjiang
4990c6bd470Sfengbojiang rte_mempool_put_bulk(p, (void *)bufs, BURST);
5000c6bd470Sfengbojiang rte_mempool_put_bulk(p, (void *)bufs2, BURST);
5010c6bd470Sfengbojiang
5020c6bd470Sfengbojiang if (failed)
5030c6bd470Sfengbojiang return -1;
5040c6bd470Sfengbojiang
5054418919fSjohnjiang printf("Sanity test with worker shutdown passed\n\n");
5064418919fSjohnjiang return 0;
5074418919fSjohnjiang }
5084418919fSjohnjiang
5094418919fSjohnjiang /* Test that the flush function is able to move packets between workers when
5104418919fSjohnjiang * one worker shuts down..
5114418919fSjohnjiang */
5124418919fSjohnjiang static int
test_flush_with_worker_shutdown(struct worker_params * wp,struct rte_mempool * p)5134418919fSjohnjiang test_flush_with_worker_shutdown(struct worker_params *wp,
5144418919fSjohnjiang struct rte_mempool *p)
5154418919fSjohnjiang {
5164418919fSjohnjiang struct rte_distributor *d = wp->dist;
5174418919fSjohnjiang struct rte_mbuf *bufs[BURST];
5180c6bd470Sfengbojiang unsigned int i;
5190c6bd470Sfengbojiang unsigned int failed = 0;
5200c6bd470Sfengbojiang unsigned int processed;
5214418919fSjohnjiang
5224418919fSjohnjiang printf("=== Test flush fn with worker shutdown (%s) ===\n", wp->name);
5234418919fSjohnjiang
5244418919fSjohnjiang clear_packet_count();
5254418919fSjohnjiang if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
5264418919fSjohnjiang printf("line %d: Error getting mbufs from pool\n", __LINE__);
5274418919fSjohnjiang return -1;
5284418919fSjohnjiang }
5294418919fSjohnjiang
5304418919fSjohnjiang /* now set all hash values in all buffers to zero, so all pkts go to the
5314418919fSjohnjiang * one worker thread */
5324418919fSjohnjiang for (i = 0; i < BURST; i++)
5334418919fSjohnjiang bufs[i]->hash.usr = 0;
5344418919fSjohnjiang
5350c6bd470Sfengbojiang processed = 0;
5360c6bd470Sfengbojiang while (processed < BURST)
5370c6bd470Sfengbojiang processed += rte_distributor_process(d, &bufs[processed],
5380c6bd470Sfengbojiang BURST - processed);
5394418919fSjohnjiang /* at this point, we will have processed some packets and have a full
5404418919fSjohnjiang * backlog for the other ones at worker 0.
5414418919fSjohnjiang */
5424418919fSjohnjiang
5434418919fSjohnjiang /* get worker zero to quit */
5444418919fSjohnjiang zero_quit = 1;
5454418919fSjohnjiang
5464418919fSjohnjiang /* flush the distributor */
5474418919fSjohnjiang rte_distributor_flush(d);
5484418919fSjohnjiang
5490c6bd470Sfengbojiang while (!__atomic_load_n(&zero_sleep, __ATOMIC_ACQUIRE))
5500c6bd470Sfengbojiang rte_distributor_flush(d);
5514418919fSjohnjiang
5524418919fSjohnjiang zero_quit = 0;
5530c6bd470Sfengbojiang
5540c6bd470Sfengbojiang while (__atomic_load_n(&zero_sleep, __ATOMIC_ACQUIRE))
5550c6bd470Sfengbojiang rte_delay_us(100);
5560c6bd470Sfengbojiang
5574418919fSjohnjiang for (i = 0; i < rte_lcore_count() - 1; i++)
5584418919fSjohnjiang printf("Worker %u handled %u packets\n", i,
5590c6bd470Sfengbojiang __atomic_load_n(&worker_stats[i].handled_packets,
5600c6bd470Sfengbojiang __ATOMIC_RELAXED));
5614418919fSjohnjiang
5624418919fSjohnjiang if (total_packet_count() != BURST) {
5634418919fSjohnjiang printf("Line %d: Error, not all packets flushed. "
5644418919fSjohnjiang "Expected %u, got %u\n",
5654418919fSjohnjiang __LINE__, BURST, total_packet_count());
5660c6bd470Sfengbojiang failed = 1;
5674418919fSjohnjiang }
5684418919fSjohnjiang
5690c6bd470Sfengbojiang rte_mempool_put_bulk(p, (void *)bufs, BURST);
5700c6bd470Sfengbojiang
5710c6bd470Sfengbojiang if (failed)
5720c6bd470Sfengbojiang return -1;
5730c6bd470Sfengbojiang
5744418919fSjohnjiang printf("Flush test with worker shutdown passed\n\n");
5754418919fSjohnjiang return 0;
5764418919fSjohnjiang }
5774418919fSjohnjiang
578*2d9fd380Sjfb8856606 static int
handle_and_mark_work(void * arg)579*2d9fd380Sjfb8856606 handle_and_mark_work(void *arg)
580*2d9fd380Sjfb8856606 {
581*2d9fd380Sjfb8856606 struct rte_mbuf *buf[8] __rte_cache_aligned;
582*2d9fd380Sjfb8856606 struct worker_params *wp = arg;
583*2d9fd380Sjfb8856606 struct rte_distributor *db = wp->dist;
584*2d9fd380Sjfb8856606 unsigned int num, i;
585*2d9fd380Sjfb8856606 unsigned int id = __atomic_fetch_add(&worker_idx, 1, __ATOMIC_RELAXED);
586*2d9fd380Sjfb8856606 num = rte_distributor_get_pkt(db, id, buf, NULL, 0);
587*2d9fd380Sjfb8856606 while (!quit) {
588*2d9fd380Sjfb8856606 __atomic_fetch_add(&worker_stats[id].handled_packets, num,
589*2d9fd380Sjfb8856606 __ATOMIC_RELAXED);
590*2d9fd380Sjfb8856606 for (i = 0; i < num; i++)
591*2d9fd380Sjfb8856606 *seq_field(buf[i]) += id + 1;
592*2d9fd380Sjfb8856606 num = rte_distributor_get_pkt(db, id,
593*2d9fd380Sjfb8856606 buf, buf, num);
594*2d9fd380Sjfb8856606 }
595*2d9fd380Sjfb8856606 __atomic_fetch_add(&worker_stats[id].handled_packets, num,
596*2d9fd380Sjfb8856606 __ATOMIC_RELAXED);
597*2d9fd380Sjfb8856606 rte_distributor_return_pkt(db, id, buf, num);
598*2d9fd380Sjfb8856606 return 0;
599*2d9fd380Sjfb8856606 }
600*2d9fd380Sjfb8856606
601*2d9fd380Sjfb8856606 /* sanity_mark_test sends packets to workers which mark them.
602*2d9fd380Sjfb8856606 * Every packet has also encoded sequence number.
603*2d9fd380Sjfb8856606 * The returned packets are sorted and verified if they were handled
604*2d9fd380Sjfb8856606 * by proper workers.
605*2d9fd380Sjfb8856606 */
606*2d9fd380Sjfb8856606 static int
sanity_mark_test(struct worker_params * wp,struct rte_mempool * p)607*2d9fd380Sjfb8856606 sanity_mark_test(struct worker_params *wp, struct rte_mempool *p)
608*2d9fd380Sjfb8856606 {
609*2d9fd380Sjfb8856606 const unsigned int buf_count = 24;
610*2d9fd380Sjfb8856606 const unsigned int burst = 8;
611*2d9fd380Sjfb8856606 const unsigned int shift = 12;
612*2d9fd380Sjfb8856606 const unsigned int seq_shift = 10;
613*2d9fd380Sjfb8856606
614*2d9fd380Sjfb8856606 struct rte_distributor *db = wp->dist;
615*2d9fd380Sjfb8856606 struct rte_mbuf *bufs[buf_count];
616*2d9fd380Sjfb8856606 struct rte_mbuf *returns[buf_count];
617*2d9fd380Sjfb8856606 unsigned int i, count, id;
618*2d9fd380Sjfb8856606 unsigned int sorted[buf_count], seq;
619*2d9fd380Sjfb8856606 unsigned int failed = 0;
620*2d9fd380Sjfb8856606 unsigned int processed;
621*2d9fd380Sjfb8856606
622*2d9fd380Sjfb8856606 printf("=== Marked packets test ===\n");
623*2d9fd380Sjfb8856606 clear_packet_count();
624*2d9fd380Sjfb8856606 if (rte_mempool_get_bulk(p, (void *)bufs, buf_count) != 0) {
625*2d9fd380Sjfb8856606 printf("line %d: Error getting mbufs from pool\n", __LINE__);
626*2d9fd380Sjfb8856606 return -1;
627*2d9fd380Sjfb8856606 }
628*2d9fd380Sjfb8856606
629*2d9fd380Sjfb8856606 /* bufs' hashes will be like these below, but shifted left.
630*2d9fd380Sjfb8856606 * The shifting is for avoiding collisions with backlogs
631*2d9fd380Sjfb8856606 * and in-flight tags left by previous tests.
632*2d9fd380Sjfb8856606 * [1, 1, 1, 1, 1, 1, 1, 1
633*2d9fd380Sjfb8856606 * 1, 1, 1, 1, 2, 2, 2, 2
634*2d9fd380Sjfb8856606 * 2, 2, 2, 2, 1, 1, 1, 1]
635*2d9fd380Sjfb8856606 */
636*2d9fd380Sjfb8856606 for (i = 0; i < burst; i++) {
637*2d9fd380Sjfb8856606 bufs[0 * burst + i]->hash.usr = 1 << shift;
638*2d9fd380Sjfb8856606 bufs[1 * burst + i]->hash.usr = ((i < burst / 2) ? 1 : 2)
639*2d9fd380Sjfb8856606 << shift;
640*2d9fd380Sjfb8856606 bufs[2 * burst + i]->hash.usr = ((i < burst / 2) ? 2 : 1)
641*2d9fd380Sjfb8856606 << shift;
642*2d9fd380Sjfb8856606 }
643*2d9fd380Sjfb8856606 /* Assign a sequence number to each packet. The sequence is shifted,
644*2d9fd380Sjfb8856606 * so that lower bits will hold mark from worker.
645*2d9fd380Sjfb8856606 */
646*2d9fd380Sjfb8856606 for (i = 0; i < buf_count; i++)
647*2d9fd380Sjfb8856606 *seq_field(bufs[i]) = i << seq_shift;
648*2d9fd380Sjfb8856606
649*2d9fd380Sjfb8856606 count = 0;
650*2d9fd380Sjfb8856606 for (i = 0; i < buf_count/burst; i++) {
651*2d9fd380Sjfb8856606 processed = 0;
652*2d9fd380Sjfb8856606 while (processed < burst)
653*2d9fd380Sjfb8856606 processed += rte_distributor_process(db,
654*2d9fd380Sjfb8856606 &bufs[i * burst + processed],
655*2d9fd380Sjfb8856606 burst - processed);
656*2d9fd380Sjfb8856606 count += rte_distributor_returned_pkts(db, &returns[count],
657*2d9fd380Sjfb8856606 buf_count - count);
658*2d9fd380Sjfb8856606 }
659*2d9fd380Sjfb8856606
660*2d9fd380Sjfb8856606 do {
661*2d9fd380Sjfb8856606 rte_distributor_flush(db);
662*2d9fd380Sjfb8856606 count += rte_distributor_returned_pkts(db, &returns[count],
663*2d9fd380Sjfb8856606 buf_count - count);
664*2d9fd380Sjfb8856606 } while (count < buf_count);
665*2d9fd380Sjfb8856606
666*2d9fd380Sjfb8856606 for (i = 0; i < rte_lcore_count() - 1; i++)
667*2d9fd380Sjfb8856606 printf("Worker %u handled %u packets\n", i,
668*2d9fd380Sjfb8856606 __atomic_load_n(&worker_stats[i].handled_packets,
669*2d9fd380Sjfb8856606 __ATOMIC_RELAXED));
670*2d9fd380Sjfb8856606
671*2d9fd380Sjfb8856606 /* Sort returned packets by sent order (sequence numbers). */
672*2d9fd380Sjfb8856606 for (i = 0; i < buf_count; i++) {
673*2d9fd380Sjfb8856606 seq = *seq_field(returns[i]) >> seq_shift;
674*2d9fd380Sjfb8856606 id = *seq_field(returns[i]) - (seq << seq_shift);
675*2d9fd380Sjfb8856606 sorted[seq] = id;
676*2d9fd380Sjfb8856606 }
677*2d9fd380Sjfb8856606
678*2d9fd380Sjfb8856606 /* Verify that packets [0-11] and [20-23] were processed
679*2d9fd380Sjfb8856606 * by the same worker
680*2d9fd380Sjfb8856606 */
681*2d9fd380Sjfb8856606 for (i = 1; i < 12; i++) {
682*2d9fd380Sjfb8856606 if (sorted[i] != sorted[0]) {
683*2d9fd380Sjfb8856606 printf("Packet number %u processed by worker %u,"
684*2d9fd380Sjfb8856606 " but should be processes by worker %u\n",
685*2d9fd380Sjfb8856606 i, sorted[i], sorted[0]);
686*2d9fd380Sjfb8856606 failed = 1;
687*2d9fd380Sjfb8856606 }
688*2d9fd380Sjfb8856606 }
689*2d9fd380Sjfb8856606 for (i = 20; i < 24; i++) {
690*2d9fd380Sjfb8856606 if (sorted[i] != sorted[0]) {
691*2d9fd380Sjfb8856606 printf("Packet number %u processed by worker %u,"
692*2d9fd380Sjfb8856606 " but should be processes by worker %u\n",
693*2d9fd380Sjfb8856606 i, sorted[i], sorted[0]);
694*2d9fd380Sjfb8856606 failed = 1;
695*2d9fd380Sjfb8856606 }
696*2d9fd380Sjfb8856606 }
697*2d9fd380Sjfb8856606 /* And verify that packets [12-19] were processed
698*2d9fd380Sjfb8856606 * by the another worker
699*2d9fd380Sjfb8856606 */
700*2d9fd380Sjfb8856606 for (i = 13; i < 20; i++) {
701*2d9fd380Sjfb8856606 if (sorted[i] != sorted[12]) {
702*2d9fd380Sjfb8856606 printf("Packet number %u processed by worker %u,"
703*2d9fd380Sjfb8856606 " but should be processes by worker %u\n",
704*2d9fd380Sjfb8856606 i, sorted[i], sorted[12]);
705*2d9fd380Sjfb8856606 failed = 1;
706*2d9fd380Sjfb8856606 }
707*2d9fd380Sjfb8856606 }
708*2d9fd380Sjfb8856606
709*2d9fd380Sjfb8856606 rte_mempool_put_bulk(p, (void *)bufs, buf_count);
710*2d9fd380Sjfb8856606
711*2d9fd380Sjfb8856606 if (failed)
712*2d9fd380Sjfb8856606 return -1;
713*2d9fd380Sjfb8856606
714*2d9fd380Sjfb8856606 printf("Marked packets test passed\n");
715*2d9fd380Sjfb8856606 return 0;
716*2d9fd380Sjfb8856606 }
717*2d9fd380Sjfb8856606
7184418919fSjohnjiang static
test_error_distributor_create_name(void)7194418919fSjohnjiang int test_error_distributor_create_name(void)
7204418919fSjohnjiang {
7214418919fSjohnjiang struct rte_distributor *d = NULL;
7224418919fSjohnjiang struct rte_distributor *db = NULL;
7234418919fSjohnjiang char *name = NULL;
7244418919fSjohnjiang
7254418919fSjohnjiang d = rte_distributor_create(name, rte_socket_id(),
7264418919fSjohnjiang rte_lcore_count() - 1,
7274418919fSjohnjiang RTE_DIST_ALG_SINGLE);
7284418919fSjohnjiang if (d != NULL || rte_errno != EINVAL) {
7294418919fSjohnjiang printf("ERROR: No error on create() with NULL name param\n");
7304418919fSjohnjiang return -1;
7314418919fSjohnjiang }
7324418919fSjohnjiang
7334418919fSjohnjiang db = rte_distributor_create(name, rte_socket_id(),
7344418919fSjohnjiang rte_lcore_count() - 1,
7354418919fSjohnjiang RTE_DIST_ALG_BURST);
7364418919fSjohnjiang if (db != NULL || rte_errno != EINVAL) {
7374418919fSjohnjiang printf("ERROR: No error on create() with NULL param\n");
7384418919fSjohnjiang return -1;
7394418919fSjohnjiang }
7404418919fSjohnjiang
7414418919fSjohnjiang return 0;
7424418919fSjohnjiang }
7434418919fSjohnjiang
7444418919fSjohnjiang
7454418919fSjohnjiang static
test_error_distributor_create_numworkers(void)7464418919fSjohnjiang int test_error_distributor_create_numworkers(void)
7474418919fSjohnjiang {
7484418919fSjohnjiang struct rte_distributor *ds = NULL;
7494418919fSjohnjiang struct rte_distributor *db = NULL;
7504418919fSjohnjiang
7514418919fSjohnjiang ds = rte_distributor_create("test_numworkers", rte_socket_id(),
7524418919fSjohnjiang RTE_MAX_LCORE + 10,
7534418919fSjohnjiang RTE_DIST_ALG_SINGLE);
7544418919fSjohnjiang if (ds != NULL || rte_errno != EINVAL) {
7554418919fSjohnjiang printf("ERROR: No error on create() with num_workers > MAX\n");
7564418919fSjohnjiang return -1;
7574418919fSjohnjiang }
7584418919fSjohnjiang
7594418919fSjohnjiang db = rte_distributor_create("test_numworkers", rte_socket_id(),
7604418919fSjohnjiang RTE_MAX_LCORE + 10,
7614418919fSjohnjiang RTE_DIST_ALG_BURST);
7624418919fSjohnjiang if (db != NULL || rte_errno != EINVAL) {
7634418919fSjohnjiang printf("ERROR: No error on create() num_workers > MAX\n");
7644418919fSjohnjiang return -1;
7654418919fSjohnjiang }
7664418919fSjohnjiang
7674418919fSjohnjiang return 0;
7684418919fSjohnjiang }
7694418919fSjohnjiang
7704418919fSjohnjiang
7714418919fSjohnjiang /* Useful function which ensures that all worker functions terminate */
7724418919fSjohnjiang static void
quit_workers(struct worker_params * wp,struct rte_mempool * p)7734418919fSjohnjiang quit_workers(struct worker_params *wp, struct rte_mempool *p)
7744418919fSjohnjiang {
7754418919fSjohnjiang struct rte_distributor *d = wp->dist;
7764418919fSjohnjiang const unsigned num_workers = rte_lcore_count() - 1;
7774418919fSjohnjiang unsigned i;
7784418919fSjohnjiang struct rte_mbuf *bufs[RTE_MAX_LCORE];
7790c6bd470Sfengbojiang struct rte_mbuf *returns[RTE_MAX_LCORE];
7800c6bd470Sfengbojiang if (rte_mempool_get_bulk(p, (void *)bufs, num_workers) != 0) {
7810c6bd470Sfengbojiang printf("line %d: Error getting mbufs from pool\n", __LINE__);
7820c6bd470Sfengbojiang return;
7830c6bd470Sfengbojiang }
7844418919fSjohnjiang
7854418919fSjohnjiang zero_quit = 0;
7864418919fSjohnjiang quit = 1;
7870c6bd470Sfengbojiang for (i = 0; i < num_workers; i++) {
7884418919fSjohnjiang bufs[i]->hash.usr = i << 1;
7890c6bd470Sfengbojiang rte_distributor_process(d, &bufs[i], 1);
7900c6bd470Sfengbojiang }
7914418919fSjohnjiang
7924418919fSjohnjiang rte_distributor_process(d, NULL, 0);
7934418919fSjohnjiang rte_distributor_flush(d);
7944418919fSjohnjiang rte_eal_mp_wait_lcore();
7950c6bd470Sfengbojiang
7960c6bd470Sfengbojiang while (rte_distributor_returned_pkts(d, returns, RTE_MAX_LCORE))
7970c6bd470Sfengbojiang ;
7980c6bd470Sfengbojiang
7990c6bd470Sfengbojiang rte_distributor_clear_returns(d);
8000c6bd470Sfengbojiang rte_mempool_put_bulk(p, (void *)bufs, num_workers);
8010c6bd470Sfengbojiang
8024418919fSjohnjiang quit = 0;
8034418919fSjohnjiang worker_idx = 0;
8040c6bd470Sfengbojiang zero_idx = RTE_MAX_LCORE;
8050c6bd470Sfengbojiang zero_quit = 0;
8060c6bd470Sfengbojiang zero_sleep = 0;
8074418919fSjohnjiang }
8084418919fSjohnjiang
8094418919fSjohnjiang static int
test_distributor(void)8104418919fSjohnjiang test_distributor(void)
8114418919fSjohnjiang {
8124418919fSjohnjiang static struct rte_distributor *ds;
8134418919fSjohnjiang static struct rte_distributor *db;
8144418919fSjohnjiang static struct rte_distributor *dist[2];
8154418919fSjohnjiang static struct rte_mempool *p;
8164418919fSjohnjiang int i;
8174418919fSjohnjiang
818*2d9fd380Sjfb8856606 static const struct rte_mbuf_dynfield seq_dynfield_desc = {
819*2d9fd380Sjfb8856606 .name = "test_distributor_dynfield_seq",
820*2d9fd380Sjfb8856606 .size = sizeof(seq_dynfield_t),
821*2d9fd380Sjfb8856606 .align = __alignof__(seq_dynfield_t),
822*2d9fd380Sjfb8856606 };
823*2d9fd380Sjfb8856606 seq_dynfield_offset =
824*2d9fd380Sjfb8856606 rte_mbuf_dynfield_register(&seq_dynfield_desc);
825*2d9fd380Sjfb8856606 if (seq_dynfield_offset < 0) {
826*2d9fd380Sjfb8856606 printf("Error registering mbuf field\n");
827*2d9fd380Sjfb8856606 return TEST_FAILED;
828*2d9fd380Sjfb8856606 }
829*2d9fd380Sjfb8856606
8304418919fSjohnjiang if (rte_lcore_count() < 2) {
8314418919fSjohnjiang printf("Not enough cores for distributor_autotest, expecting at least 2\n");
8324418919fSjohnjiang return TEST_SKIPPED;
8334418919fSjohnjiang }
8344418919fSjohnjiang
8354418919fSjohnjiang if (db == NULL) {
8364418919fSjohnjiang db = rte_distributor_create("Test_dist_burst", rte_socket_id(),
8374418919fSjohnjiang rte_lcore_count() - 1,
8384418919fSjohnjiang RTE_DIST_ALG_BURST);
8394418919fSjohnjiang if (db == NULL) {
8404418919fSjohnjiang printf("Error creating burst distributor\n");
8414418919fSjohnjiang return -1;
8424418919fSjohnjiang }
8434418919fSjohnjiang } else {
8444418919fSjohnjiang rte_distributor_flush(db);
8454418919fSjohnjiang rte_distributor_clear_returns(db);
8464418919fSjohnjiang }
8474418919fSjohnjiang
8484418919fSjohnjiang if (ds == NULL) {
8494418919fSjohnjiang ds = rte_distributor_create("Test_dist_single",
8504418919fSjohnjiang rte_socket_id(),
8514418919fSjohnjiang rte_lcore_count() - 1,
8524418919fSjohnjiang RTE_DIST_ALG_SINGLE);
8534418919fSjohnjiang if (ds == NULL) {
8544418919fSjohnjiang printf("Error creating single distributor\n");
8554418919fSjohnjiang return -1;
8564418919fSjohnjiang }
8574418919fSjohnjiang } else {
8584418919fSjohnjiang rte_distributor_flush(ds);
8594418919fSjohnjiang rte_distributor_clear_returns(ds);
8604418919fSjohnjiang }
8614418919fSjohnjiang
8624418919fSjohnjiang const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
8634418919fSjohnjiang (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
8644418919fSjohnjiang if (p == NULL) {
8654418919fSjohnjiang p = rte_pktmbuf_pool_create("DT_MBUF_POOL", nb_bufs, BURST,
8664418919fSjohnjiang 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
8674418919fSjohnjiang if (p == NULL) {
8684418919fSjohnjiang printf("Error creating mempool\n");
8694418919fSjohnjiang return -1;
8704418919fSjohnjiang }
8714418919fSjohnjiang }
8724418919fSjohnjiang
8734418919fSjohnjiang dist[0] = ds;
8744418919fSjohnjiang dist[1] = db;
8754418919fSjohnjiang
8764418919fSjohnjiang for (i = 0; i < 2; i++) {
8774418919fSjohnjiang
8784418919fSjohnjiang worker_params.dist = dist[i];
8794418919fSjohnjiang if (i)
8804418919fSjohnjiang strlcpy(worker_params.name, "burst",
8814418919fSjohnjiang sizeof(worker_params.name));
8824418919fSjohnjiang else
8834418919fSjohnjiang strlcpy(worker_params.name, "single",
8844418919fSjohnjiang sizeof(worker_params.name));
8854418919fSjohnjiang
8864418919fSjohnjiang rte_eal_mp_remote_launch(handle_work,
887*2d9fd380Sjfb8856606 &worker_params, SKIP_MAIN);
8884418919fSjohnjiang if (sanity_test(&worker_params, p) < 0)
8894418919fSjohnjiang goto err;
8904418919fSjohnjiang quit_workers(&worker_params, p);
8914418919fSjohnjiang
8924418919fSjohnjiang rte_eal_mp_remote_launch(handle_work_with_free_mbufs,
893*2d9fd380Sjfb8856606 &worker_params, SKIP_MAIN);
8944418919fSjohnjiang if (sanity_test_with_mbuf_alloc(&worker_params, p) < 0)
8954418919fSjohnjiang goto err;
8964418919fSjohnjiang quit_workers(&worker_params, p);
8974418919fSjohnjiang
8984418919fSjohnjiang if (rte_lcore_count() > 2) {
8994418919fSjohnjiang rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
9004418919fSjohnjiang &worker_params,
901*2d9fd380Sjfb8856606 SKIP_MAIN);
9024418919fSjohnjiang if (sanity_test_with_worker_shutdown(&worker_params,
9034418919fSjohnjiang p) < 0)
9044418919fSjohnjiang goto err;
9054418919fSjohnjiang quit_workers(&worker_params, p);
9064418919fSjohnjiang
9074418919fSjohnjiang rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
9084418919fSjohnjiang &worker_params,
909*2d9fd380Sjfb8856606 SKIP_MAIN);
9104418919fSjohnjiang if (test_flush_with_worker_shutdown(&worker_params,
9114418919fSjohnjiang p) < 0)
9124418919fSjohnjiang goto err;
9134418919fSjohnjiang quit_workers(&worker_params, p);
9144418919fSjohnjiang
915*2d9fd380Sjfb8856606 rte_eal_mp_remote_launch(handle_and_mark_work,
916*2d9fd380Sjfb8856606 &worker_params, SKIP_MAIN);
917*2d9fd380Sjfb8856606 if (sanity_mark_test(&worker_params, p) < 0)
918*2d9fd380Sjfb8856606 goto err;
919*2d9fd380Sjfb8856606 quit_workers(&worker_params, p);
920*2d9fd380Sjfb8856606
9214418919fSjohnjiang } else {
9224418919fSjohnjiang printf("Too few cores to run worker shutdown test\n");
9234418919fSjohnjiang }
9244418919fSjohnjiang
9254418919fSjohnjiang }
9264418919fSjohnjiang
9274418919fSjohnjiang if (test_error_distributor_create_numworkers() == -1 ||
9284418919fSjohnjiang test_error_distributor_create_name() == -1) {
9294418919fSjohnjiang printf("rte_distributor_create parameter check tests failed");
9304418919fSjohnjiang return -1;
9314418919fSjohnjiang }
9324418919fSjohnjiang
9334418919fSjohnjiang return 0;
9344418919fSjohnjiang
9354418919fSjohnjiang err:
9364418919fSjohnjiang quit_workers(&worker_params, p);
9374418919fSjohnjiang return -1;
9384418919fSjohnjiang }
9394418919fSjohnjiang
9404418919fSjohnjiang REGISTER_TEST_COMMAND(distributor_autotest, test_distributor);
941