Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2017 Intel Corporation
3 : : */
4 : :
5 : : #include "test.h"
6 : :
7 : : #include <unistd.h>
8 : : #include <string.h>
9 : : #include <rte_cycles.h>
10 : : #include <rte_errno.h>
11 : : #include <rte_mempool.h>
12 : : #include <rte_mbuf.h>
13 : : #include <rte_mbuf_dyn.h>
14 : :
15 : : #ifdef RTE_EXEC_ENV_WINDOWS
16 : : static int
17 : : test_distributor(void)
18 : : {
19 : : printf("distributor not supported on Windows, skipping test\n");
20 : : return TEST_SKIPPED;
21 : : }
22 : :
23 : : #else
24 : :
25 : : #include <rte_distributor.h>
26 : : #include <rte_string_fns.h>
27 : :
28 : : #define ITER_POWER 20 /* log 2 of how many iterations we do when timing. */
29 : : #define BURST 32
30 : : #define BIG_BATCH 1024
31 : :
32 : : typedef uint32_t seq_dynfield_t;
33 : : static int seq_dynfield_offset = -1;
34 : :
35 : : static inline seq_dynfield_t *
36 : : seq_field(struct rte_mbuf *mbuf)
37 : : {
38 : 0 : return RTE_MBUF_DYNFIELD(mbuf, seq_dynfield_offset, seq_dynfield_t *);
39 : : }
40 : :
41 : : struct worker_params {
42 : : char name[64];
43 : : struct rte_distributor *dist;
44 : : };
45 : :
46 : : struct worker_params worker_params;
47 : :
48 : : /* statics - all zero-initialized by default */
49 : : static volatile int quit; /**< general quit variable for all threads */
50 : : static volatile int zero_quit; /**< var for when we just want thr0 to quit*/
51 : : static volatile int zero_sleep; /**< thr0 has quit basic loop and is sleeping*/
52 : : static volatile unsigned worker_idx;
53 : : static volatile unsigned zero_idx;
54 : :
55 : : struct worker_stats {
56 : : volatile unsigned handled_packets;
57 : : } __rte_cache_aligned;
58 : : struct worker_stats worker_stats[RTE_MAX_LCORE];
59 : :
60 : : /* returns the total count of the number of packets handled by the worker
61 : : * functions given below.
62 : : */
63 : : static inline unsigned
64 : : total_packet_count(void)
65 : : {
66 : : unsigned i, count = 0;
67 [ - - - - : 12 : for (i = 0; i < worker_idx; i++)
- - - - +
+ - - + +
- - - - -
- + + -
- ]
68 : 6 : count += __atomic_load_n(&worker_stats[i].handled_packets,
69 : : __ATOMIC_RELAXED);
70 : : return count;
71 : : }
72 : :
73 : : /* resets the packet counts for a new test */
74 : : static inline void
75 : : clear_packet_count(void)
76 : : {
77 : : unsigned int i;
78 [ - - - - : 1032 : for (i = 0; i < RTE_MAX_LCORE; i++)
- - + + +
+ - - + +
+ + ]
79 : 1024 : __atomic_store_n(&worker_stats[i].handled_packets, 0,
80 : : __ATOMIC_RELAXED);
81 : : }
82 : :
83 : : /* this is the basic worker function for sanity test
84 : : * it does nothing but return packets and count them.
85 : : */
86 : : static int
87 : 2 : handle_work(void *arg)
88 : : {
89 : : struct rte_mbuf *buf[8] __rte_cache_aligned;
90 : : struct worker_params *wp = arg;
91 : 2 : struct rte_distributor *db = wp->dist;
92 : : unsigned int num;
93 : 2 : unsigned int id = __atomic_fetch_add(&worker_idx, 1, __ATOMIC_RELAXED);
94 : :
95 : 2 : num = rte_distributor_get_pkt(db, id, buf, NULL, 0);
96 [ + + ]: 1297 : while (!quit) {
97 : 1295 : __atomic_fetch_add(&worker_stats[id].handled_packets, num,
98 : : __ATOMIC_RELAXED);
99 : 1295 : num = rte_distributor_get_pkt(db, id,
100 : : buf, buf, num);
101 : : }
102 : 2 : __atomic_fetch_add(&worker_stats[id].handled_packets, num,
103 : : __ATOMIC_RELAXED);
104 : 2 : rte_distributor_return_pkt(db, id, buf, num);
105 : 2 : return 0;
106 : : }
107 : :
108 : : /* do basic sanity testing of the distributor. This test tests the following:
109 : : * - send 32 packets through distributor with the same tag and ensure they
110 : : * all go to the one worker
111 : : * - send 32 packets through the distributor with two different tags and
112 : : * verify that they go equally to two different workers.
113 : : * - send 32 packets with different tags through the distributors and
114 : : * just verify we get all packets back.
115 : : * - send 1024 packets through the distributor, gathering the returned packets
116 : : * as we go. Then verify that we correctly got all 1024 pointers back again,
117 : : * not necessarily in the same order (as different flows).
118 : : */
119 : : static int
120 : 2 : sanity_test(struct worker_params *wp, struct rte_mempool *p)
121 : : {
122 : 2 : struct rte_distributor *db = wp->dist;
123 : : struct rte_mbuf *bufs[BURST];
124 : : struct rte_mbuf *returns[BURST*2];
125 : : unsigned int i, count;
126 : : unsigned int retries;
127 : : unsigned int processed;
128 : :
129 : : printf("=== Basic distributor sanity tests ===\n");
130 : : clear_packet_count();
131 [ + - ]: 1 : if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
132 : : printf("line %d: Error getting mbufs from pool\n", __LINE__);
133 : 0 : return -1;
134 : : }
135 : :
136 : : /* now set all hash values in all buffers to zero, so all pkts go to the
137 : : * one worker thread */
138 [ + + ]: 66 : for (i = 0; i < BURST; i++)
139 : 64 : bufs[i]->hash.usr = 0;
140 : :
141 : : processed = 0;
142 [ + + ]: 4 : while (processed < BURST)
143 : 2 : processed += rte_distributor_process(db, &bufs[processed],
144 : : BURST - processed);
145 : :
146 : : count = 0;
147 : : do {
148 : :
149 : 2 : rte_distributor_flush(db);
150 : 2 : count += rte_distributor_returned_pkts(db,
151 : : returns, BURST*2);
152 [ - + ]: 2 : } while (count < BURST);
153 : :
154 [ - + ]: 2 : if (total_packet_count() != BURST) {
155 : : printf("Line %d: Error, not all packets flushed. "
156 : : "Expected %u, got %u\n",
157 : : __LINE__, BURST, total_packet_count());
158 : : rte_mempool_put_bulk(p, (void *)bufs, BURST);
159 : 0 : return -1;
160 : : }
161 : :
162 [ + + ]: 4 : for (i = 0; i < rte_lcore_count() - 1; i++)
163 : 2 : printf("Worker %u handled %u packets\n", i,
164 : 2 : __atomic_load_n(&worker_stats[i].handled_packets,
165 : : __ATOMIC_RELAXED));
166 : : printf("Sanity test with all zero hashes done.\n");
167 : :
168 : : /* pick two flows and check they go correctly */
169 [ - + ]: 2 : if (rte_lcore_count() >= 3) {
170 : : clear_packet_count();
171 [ # # ]: 0 : for (i = 0; i < BURST; i++)
172 : 0 : bufs[i]->hash.usr = (i & 1) << 8;
173 : :
174 : 0 : rte_distributor_process(db, bufs, BURST);
175 : : count = 0;
176 : : do {
177 : 0 : rte_distributor_flush(db);
178 : 0 : count += rte_distributor_returned_pkts(db,
179 : : returns, BURST*2);
180 [ # # ]: 0 : } while (count < BURST);
181 [ # # ]: 0 : if (total_packet_count() != BURST) {
182 : : printf("Line %d: Error, not all packets flushed. "
183 : : "Expected %u, got %u\n",
184 : : __LINE__, BURST, total_packet_count());
185 : : rte_mempool_put_bulk(p, (void *)bufs, BURST);
186 : 0 : return -1;
187 : : }
188 : :
189 [ # # ]: 0 : for (i = 0; i < rte_lcore_count() - 1; i++)
190 : 0 : printf("Worker %u handled %u packets\n", i,
191 : : __atomic_load_n(
192 : 0 : &worker_stats[i].handled_packets,
193 : : __ATOMIC_RELAXED));
194 : : printf("Sanity test with two hash values done\n");
195 : : }
196 : :
197 : : /* give a different hash value to each packet,
198 : : * so load gets distributed */
199 : : clear_packet_count();
200 [ + + ]: 66 : for (i = 0; i < BURST; i++)
201 : 64 : bufs[i]->hash.usr = i+1;
202 : :
203 : 2 : rte_distributor_process(db, bufs, BURST);
204 : : count = 0;
205 : : do {
206 : 2 : rte_distributor_flush(db);
207 : 2 : count += rte_distributor_returned_pkts(db,
208 : : returns, BURST*2);
209 [ - + ]: 2 : } while (count < BURST);
210 [ - + ]: 2 : if (total_packet_count() != BURST) {
211 : : printf("Line %d: Error, not all packets flushed. "
212 : : "Expected %u, got %u\n",
213 : : __LINE__, BURST, total_packet_count());
214 : : rte_mempool_put_bulk(p, (void *)bufs, BURST);
215 : 0 : return -1;
216 : : }
217 : :
218 [ + + ]: 4 : for (i = 0; i < rte_lcore_count() - 1; i++)
219 : 2 : printf("Worker %u handled %u packets\n", i,
220 : 2 : __atomic_load_n(&worker_stats[i].handled_packets,
221 : : __ATOMIC_RELAXED));
222 : : printf("Sanity test with non-zero hashes done\n");
223 : :
224 : : rte_mempool_put_bulk(p, (void *)bufs, BURST);
225 : :
226 : : /* sanity test with BIG_BATCH packets to ensure they all arrived back
227 : : * from the returned packets function */
228 : : clear_packet_count();
229 : : struct rte_mbuf *many_bufs[BIG_BATCH], *return_bufs[BIG_BATCH];
230 : : unsigned num_returned = 0;
231 : : unsigned int num_being_processed = 0;
232 : : unsigned int return_buffer_capacity = 127;/* RTE_DISTRIB_RETURNS_MASK */
233 : :
234 : : /* flush out any remaining packets */
235 : 2 : rte_distributor_flush(db);
236 : 2 : rte_distributor_clear_returns(db);
237 : :
238 [ + - ]: 2 : if (rte_mempool_get_bulk(p, (void *)many_bufs, BIG_BATCH) != 0) {
239 : : printf("line %d: Error getting mbufs from pool\n", __LINE__);
240 : 0 : return -1;
241 : : }
242 [ + + ]: 2050 : for (i = 0; i < BIG_BATCH; i++)
243 : 2048 : many_bufs[i]->hash.usr = i << 2;
244 : :
245 : 2 : printf("=== testing big burst (%s) ===\n", wp->name);
246 [ + + ]: 66 : for (i = 0; i < BIG_BATCH/BURST; i++) {
247 : 64 : rte_distributor_process(db,
248 : 64 : &many_bufs[i*BURST], BURST);
249 : 64 : num_being_processed += BURST;
250 : : do {
251 : 64 : count = rte_distributor_returned_pkts(db,
252 : : &return_bufs[num_returned],
253 : : BIG_BATCH - num_returned);
254 : 64 : num_being_processed -= count;
255 : 64 : num_returned += count;
256 : 64 : rte_distributor_flush(db);
257 [ - + ]: 64 : } while (num_being_processed + BURST > return_buffer_capacity);
258 : : }
259 : : retries = 0;
260 : : do {
261 : 2 : rte_distributor_flush(db);
262 : 2 : count = rte_distributor_returned_pkts(db,
263 : : &return_bufs[num_returned],
264 : : BIG_BATCH - num_returned);
265 : 2 : num_returned += count;
266 : 2 : retries++;
267 [ - + ]: 2 : } while ((num_returned < BIG_BATCH) && (retries < 100));
268 : :
269 [ - + ]: 2 : if (num_returned != BIG_BATCH) {
270 : : printf("line %d: Missing packets, expected %d\n",
271 : : __LINE__, num_returned);
272 : : rte_mempool_put_bulk(p, (void *)many_bufs, BIG_BATCH);
273 : 0 : return -1;
274 : : }
275 : :
276 : : /* big check - make sure all packets made it back!! */
277 [ + + ]: 2050 : for (i = 0; i < BIG_BATCH; i++) {
278 : : unsigned j;
279 : 2048 : struct rte_mbuf *src = many_bufs[i];
280 [ + - ]: 1049600 : for (j = 0; j < BIG_BATCH; j++) {
281 [ + + ]: 1049600 : if (return_bufs[j] == src)
282 : : break;
283 : : }
284 : :
285 [ - + ]: 2048 : if (j == BIG_BATCH) {
286 : : printf("Error: could not find source packet #%u\n", i);
287 : : rte_mempool_put_bulk(p, (void *)many_bufs, BIG_BATCH);
288 : 0 : return -1;
289 : : }
290 : : }
291 : : printf("Sanity test of returned packets done\n");
292 : :
293 : : rte_mempool_put_bulk(p, (void *)many_bufs, BIG_BATCH);
294 : :
295 : : printf("\n");
296 : 2 : return 0;
297 : : }
298 : :
299 : :
300 : : /* to test that the distributor does not lose packets, we use this worker
301 : : * function which frees mbufs when it gets them. The distributor thread does
302 : : * the mbuf allocation. If distributor drops packets we'll eventually run out
303 : : * of mbufs.
304 : : */
305 : : static int
306 : 2 : handle_work_with_free_mbufs(void *arg)
307 : : {
308 : : struct rte_mbuf *buf[8] __rte_cache_aligned;
309 : : struct worker_params *wp = arg;
310 : 2 : struct rte_distributor *d = wp->dist;
311 : : unsigned int i;
312 : : unsigned int num;
313 : 2 : unsigned int id = __atomic_fetch_add(&worker_idx, 1, __ATOMIC_RELAXED);
314 : :
315 : 2 : num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
316 [ + + ]: 1179652 : while (!quit) {
317 : 1179650 : __atomic_fetch_add(&worker_stats[id].handled_packets, num,
318 : : __ATOMIC_RELAXED);
319 [ + + ]: 3276802 : for (i = 0; i < num; i++)
320 : 2097152 : rte_pktmbuf_free(buf[i]);
321 : 1179650 : num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
322 : : }
323 : 2 : __atomic_fetch_add(&worker_stats[id].handled_packets, num,
324 : : __ATOMIC_RELAXED);
325 : 2 : rte_distributor_return_pkt(d, id, buf, num);
326 : 2 : return 0;
327 : : }
328 : :
329 : : /* Perform a sanity test of the distributor with a large number of packets,
330 : : * where we allocate a new set of mbufs for each burst. The workers then
331 : : * free the mbufs. This ensures that we don't have any packet leaks in the
332 : : * library.
333 : : */
334 : : static int
335 : 2 : sanity_test_with_mbuf_alloc(struct worker_params *wp, struct rte_mempool *p)
336 : : {
337 : 2 : struct rte_distributor *d = wp->dist;
338 : : unsigned i;
339 : : struct rte_mbuf *bufs[BURST];
340 : : unsigned int processed;
341 : :
342 : 2 : printf("=== Sanity test with mbuf alloc/free (%s) ===\n", wp->name);
343 : :
344 : : clear_packet_count();
345 [ + + ]: 65538 : for (i = 0; i < ((1<<ITER_POWER)); i += BURST) {
346 : : unsigned j;
347 [ - + ]: 32768 : while (rte_mempool_get_bulk(p, (void *)bufs, BURST) < 0)
348 : 0 : rte_distributor_process(d, NULL, 0);
349 [ + + ]: 2162688 : for (j = 0; j < BURST; j++) {
350 : 2097152 : bufs[j]->hash.usr = (i+j) << 1;
351 : : }
352 : :
353 : : processed = 0;
354 [ + + ]: 131072 : while (processed < BURST)
355 : 65536 : processed += rte_distributor_process(d,
356 : : &bufs[processed], BURST - processed);
357 : : }
358 : :
359 : 2 : rte_distributor_flush(d);
360 : :
361 : 2 : rte_delay_us(10000);
362 : :
363 [ - + ]: 2 : if (total_packet_count() < (1<<ITER_POWER)) {
364 : : printf("Line %u: Packet count is incorrect, %u, expected %u\n",
365 : : __LINE__, total_packet_count(),
366 : : (1<<ITER_POWER));
367 : 0 : return -1;
368 : : }
369 : :
370 : : printf("Sanity test with mbuf alloc/free passed\n\n");
371 : 2 : return 0;
372 : : }
373 : :
374 : : static int
375 : 0 : handle_work_for_shutdown_test(void *arg)
376 : : {
377 : : struct rte_mbuf *buf[8] __rte_cache_aligned;
378 : : struct worker_params *wp = arg;
379 : 0 : struct rte_distributor *d = wp->dist;
380 : : unsigned int num;
381 : : unsigned int zero_id = 0;
382 : : unsigned int zero_unset;
383 : 0 : const unsigned int id = __atomic_fetch_add(&worker_idx, 1,
384 : : __ATOMIC_RELAXED);
385 : :
386 : 0 : num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
387 : :
388 [ # # ]: 0 : if (num > 0) {
389 : : zero_unset = RTE_MAX_LCORE;
390 : 0 : __atomic_compare_exchange_n(&zero_idx, &zero_unset, id,
391 : : false, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
392 : : }
393 : 0 : zero_id = __atomic_load_n(&zero_idx, __ATOMIC_ACQUIRE);
394 : :
395 : : /* wait for quit single globally, or for worker zero, wait
396 : : * for zero_quit */
397 [ # # # # : 0 : while (!quit && !(id == zero_id && zero_quit)) {
# # ]
398 : 0 : __atomic_fetch_add(&worker_stats[id].handled_packets, num,
399 : : __ATOMIC_RELAXED);
400 : 0 : num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
401 : :
402 [ # # ]: 0 : if (num > 0) {
403 : : zero_unset = RTE_MAX_LCORE;
404 : 0 : __atomic_compare_exchange_n(&zero_idx, &zero_unset, id,
405 : : false, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
406 : : }
407 : 0 : zero_id = __atomic_load_n(&zero_idx, __ATOMIC_ACQUIRE);
408 : : }
409 : :
410 : 0 : __atomic_fetch_add(&worker_stats[id].handled_packets, num,
411 : : __ATOMIC_RELAXED);
412 [ # # ]: 0 : if (id == zero_id) {
413 : 0 : rte_distributor_return_pkt(d, id, NULL, 0);
414 : :
415 : : /* for worker zero, allow it to restart to pick up last packet
416 : : * when all workers are shutting down.
417 : : */
418 : 0 : __atomic_store_n(&zero_sleep, 1, __ATOMIC_RELEASE);
419 [ # # ]: 0 : while (zero_quit)
420 : 0 : usleep(100);
421 : 0 : __atomic_store_n(&zero_sleep, 0, __ATOMIC_RELEASE);
422 : :
423 : 0 : num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
424 : :
425 [ # # ]: 0 : while (!quit) {
426 : 0 : __atomic_fetch_add(&worker_stats[id].handled_packets,
427 : : num, __ATOMIC_RELAXED);
428 : 0 : num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
429 : : }
430 : : }
431 : 0 : rte_distributor_return_pkt(d, id, buf, num);
432 : 0 : return 0;
433 : : }
434 : :
435 : :
436 : : /* Perform a sanity test of the distributor with a large number of packets,
437 : : * where we allocate a new set of mbufs for each burst. The workers then
438 : : * free the mbufs. This ensures that we don't have any packet leaks in the
439 : : * library.
440 : : */
441 : : static int
442 : 0 : sanity_test_with_worker_shutdown(struct worker_params *wp,
443 : : struct rte_mempool *p)
444 : : {
445 : 0 : struct rte_distributor *d = wp->dist;
446 : : struct rte_mbuf *bufs[BURST];
447 : : struct rte_mbuf *bufs2[BURST];
448 : : unsigned int i;
449 : : unsigned int failed = 0;
450 : : unsigned int processed = 0;
451 : :
452 : : printf("=== Sanity test of worker shutdown ===\n");
453 : :
454 : : clear_packet_count();
455 : :
456 [ # # ]: 0 : if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
457 : : printf("line %d: Error getting mbufs from pool\n", __LINE__);
458 : 0 : return -1;
459 : : }
460 : :
461 : : /*
462 : : * Now set all hash values in all buffers to same value so all
463 : : * pkts go to the one worker thread
464 : : */
465 [ # # ]: 0 : for (i = 0; i < BURST; i++)
466 : 0 : bufs[i]->hash.usr = 1;
467 : :
468 : : processed = 0;
469 [ # # ]: 0 : while (processed < BURST)
470 : 0 : processed += rte_distributor_process(d, &bufs[processed],
471 : : BURST - processed);
472 : 0 : rte_distributor_flush(d);
473 : :
474 : : /* at this point, we will have processed some packets and have a full
475 : : * backlog for the other ones at worker 0.
476 : : */
477 : :
478 : : /* get more buffers to queue up, again setting them to the same flow */
479 [ # # ]: 0 : if (rte_mempool_get_bulk(p, (void *)bufs2, BURST) != 0) {
480 : : printf("line %d: Error getting mbufs from pool\n", __LINE__);
481 : : rte_mempool_put_bulk(p, (void *)bufs, BURST);
482 : 0 : return -1;
483 : : }
484 [ # # ]: 0 : for (i = 0; i < BURST; i++)
485 : 0 : bufs2[i]->hash.usr = 1;
486 : :
487 : : /* get worker zero to quit */
488 : 0 : zero_quit = 1;
489 : 0 : rte_distributor_process(d, bufs2, BURST);
490 : :
491 : : /* flush the distributor */
492 : 0 : rte_distributor_flush(d);
493 [ # # ]: 0 : while (!__atomic_load_n(&zero_sleep, __ATOMIC_ACQUIRE))
494 : 0 : rte_distributor_flush(d);
495 : :
496 : 0 : zero_quit = 0;
497 [ # # ]: 0 : while (__atomic_load_n(&zero_sleep, __ATOMIC_ACQUIRE))
498 : 0 : rte_delay_us(100);
499 : :
500 [ # # ]: 0 : for (i = 0; i < rte_lcore_count() - 1; i++)
501 : 0 : printf("Worker %u handled %u packets\n", i,
502 : 0 : __atomic_load_n(&worker_stats[i].handled_packets,
503 : : __ATOMIC_RELAXED));
504 : :
505 [ # # ]: 0 : if (total_packet_count() != BURST * 2) {
506 : : printf("Line %d: Error, not all packets flushed. "
507 : : "Expected %u, got %u\n",
508 : : __LINE__, BURST * 2, total_packet_count());
509 : : failed = 1;
510 : : }
511 : :
512 : : rte_mempool_put_bulk(p, (void *)bufs, BURST);
513 : : rte_mempool_put_bulk(p, (void *)bufs2, BURST);
514 : :
515 [ # # ]: 0 : if (failed)
516 : : return -1;
517 : :
518 : : printf("Sanity test with worker shutdown passed\n\n");
519 : 0 : return 0;
520 : : }
521 : :
522 : : /* Test that the flush function is able to move packets between workers when
523 : : * one worker shuts down..
524 : : */
525 : : static int
526 : 0 : test_flush_with_worker_shutdown(struct worker_params *wp,
527 : : struct rte_mempool *p)
528 : : {
529 : 0 : struct rte_distributor *d = wp->dist;
530 : : struct rte_mbuf *bufs[BURST];
531 : : unsigned int i;
532 : : unsigned int failed = 0;
533 : : unsigned int processed;
534 : :
535 : 0 : printf("=== Test flush fn with worker shutdown (%s) ===\n", wp->name);
536 : :
537 : : clear_packet_count();
538 [ # # ]: 0 : if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
539 : : printf("line %d: Error getting mbufs from pool\n", __LINE__);
540 : 0 : return -1;
541 : : }
542 : :
543 : : /* now set all hash values in all buffers to zero, so all pkts go to the
544 : : * one worker thread */
545 [ # # ]: 0 : for (i = 0; i < BURST; i++)
546 : 0 : bufs[i]->hash.usr = 0;
547 : :
548 : : processed = 0;
549 [ # # ]: 0 : while (processed < BURST)
550 : 0 : processed += rte_distributor_process(d, &bufs[processed],
551 : : BURST - processed);
552 : : /* at this point, we will have processed some packets and have a full
553 : : * backlog for the other ones at worker 0.
554 : : */
555 : :
556 : : /* get worker zero to quit */
557 : 0 : zero_quit = 1;
558 : :
559 : : /* flush the distributor */
560 : 0 : rte_distributor_flush(d);
561 : :
562 [ # # ]: 0 : while (!__atomic_load_n(&zero_sleep, __ATOMIC_ACQUIRE))
563 : 0 : rte_distributor_flush(d);
564 : :
565 : 0 : zero_quit = 0;
566 : :
567 [ # # ]: 0 : while (__atomic_load_n(&zero_sleep, __ATOMIC_ACQUIRE))
568 : 0 : rte_delay_us(100);
569 : :
570 [ # # ]: 0 : for (i = 0; i < rte_lcore_count() - 1; i++)
571 : 0 : printf("Worker %u handled %u packets\n", i,
572 : 0 : __atomic_load_n(&worker_stats[i].handled_packets,
573 : : __ATOMIC_RELAXED));
574 : :
575 [ # # ]: 0 : if (total_packet_count() != BURST) {
576 : : printf("Line %d: Error, not all packets flushed. "
577 : : "Expected %u, got %u\n",
578 : : __LINE__, BURST, total_packet_count());
579 : : failed = 1;
580 : : }
581 : :
582 : : rte_mempool_put_bulk(p, (void *)bufs, BURST);
583 : :
584 [ # # ]: 0 : if (failed)
585 : : return -1;
586 : :
587 : : printf("Flush test with worker shutdown passed\n\n");
588 : 0 : return 0;
589 : : }
590 : :
591 : : static int
592 : 0 : handle_and_mark_work(void *arg)
593 : : {
594 : : struct rte_mbuf *buf[8] __rte_cache_aligned;
595 : : struct worker_params *wp = arg;
596 : 0 : struct rte_distributor *db = wp->dist;
597 : : unsigned int num, i;
598 : 0 : unsigned int id = __atomic_fetch_add(&worker_idx, 1, __ATOMIC_RELAXED);
599 : 0 : num = rte_distributor_get_pkt(db, id, buf, NULL, 0);
600 [ # # ]: 0 : while (!quit) {
601 : 0 : __atomic_fetch_add(&worker_stats[id].handled_packets, num,
602 : : __ATOMIC_RELAXED);
603 [ # # ]: 0 : for (i = 0; i < num; i++)
604 : 0 : *seq_field(buf[i]) += id + 1;
605 : 0 : num = rte_distributor_get_pkt(db, id,
606 : : buf, buf, num);
607 : : }
608 : 0 : __atomic_fetch_add(&worker_stats[id].handled_packets, num,
609 : : __ATOMIC_RELAXED);
610 : 0 : rte_distributor_return_pkt(db, id, buf, num);
611 : 0 : return 0;
612 : : }
613 : :
614 : : /* sanity_mark_test sends packets to workers which mark them.
615 : : * Every packet has also encoded sequence number.
616 : : * The returned packets are sorted and verified if they were handled
617 : : * by proper workers.
618 : : */
619 : : static int
620 : 0 : sanity_mark_test(struct worker_params *wp, struct rte_mempool *p)
621 : 0 : {
622 : : const unsigned int buf_count = 24;
623 : : const unsigned int burst = 8;
624 : : const unsigned int shift = 12;
625 : : const unsigned int seq_shift = 10;
626 : :
627 : 0 : struct rte_distributor *db = wp->dist;
628 : 0 : struct rte_mbuf *bufs[buf_count];
629 : 0 : struct rte_mbuf *returns[buf_count];
630 : : unsigned int i, count, id;
631 : 0 : unsigned int sorted[buf_count], seq;
632 : : unsigned int failed = 0;
633 : : unsigned int processed;
634 : :
635 : : printf("=== Marked packets test ===\n");
636 : : clear_packet_count();
637 [ # # ]: 0 : if (rte_mempool_get_bulk(p, (void *)bufs, buf_count) != 0) {
638 : : printf("line %d: Error getting mbufs from pool\n", __LINE__);
639 : 0 : return -1;
640 : : }
641 : :
642 : : /* bufs' hashes will be like these below, but shifted left.
643 : : * The shifting is for avoiding collisions with backlogs
644 : : * and in-flight tags left by previous tests.
645 : : * [1, 1, 1, 1, 1, 1, 1, 1
646 : : * 1, 1, 1, 1, 2, 2, 2, 2
647 : : * 2, 2, 2, 2, 1, 1, 1, 1]
648 : : */
649 [ # # ]: 0 : for (i = 0; i < burst; i++) {
650 : 0 : bufs[0 * burst + i]->hash.usr = 1 << shift;
651 : 0 : bufs[1 * burst + i]->hash.usr = ((i < burst / 2) ? 1 : 2)
652 [ # # ]: 0 : << shift;
653 : 0 : bufs[2 * burst + i]->hash.usr = ((i < burst / 2) ? 2 : 1)
654 [ # # ]: 0 : << shift;
655 : : }
656 : : /* Assign a sequence number to each packet. The sequence is shifted,
657 : : * so that lower bits will hold mark from worker.
658 : : */
659 [ # # ]: 0 : for (i = 0; i < buf_count; i++)
660 : 0 : *seq_field(bufs[i]) = i << seq_shift;
661 : :
662 : : count = 0;
663 [ # # ]: 0 : for (i = 0; i < buf_count/burst; i++) {
664 : : processed = 0;
665 [ # # ]: 0 : while (processed < burst)
666 : 0 : processed += rte_distributor_process(db,
667 : 0 : &bufs[i * burst + processed],
668 : : burst - processed);
669 : 0 : count += rte_distributor_returned_pkts(db, &returns[count],
670 : : buf_count - count);
671 : : }
672 : :
673 : : do {
674 : 0 : rte_distributor_flush(db);
675 : 0 : count += rte_distributor_returned_pkts(db, &returns[count],
676 : : buf_count - count);
677 [ # # ]: 0 : } while (count < buf_count);
678 : :
679 [ # # ]: 0 : for (i = 0; i < rte_lcore_count() - 1; i++)
680 : 0 : printf("Worker %u handled %u packets\n", i,
681 : 0 : __atomic_load_n(&worker_stats[i].handled_packets,
682 : : __ATOMIC_RELAXED));
683 : :
684 : : /* Sort returned packets by sent order (sequence numbers). */
685 [ # # ]: 0 : for (i = 0; i < buf_count; i++) {
686 : 0 : seq = *seq_field(returns[i]) >> seq_shift;
687 : 0 : id = *seq_field(returns[i]) - (seq << seq_shift);
688 : 0 : sorted[seq] = id;
689 : : }
690 : :
691 : : /* Verify that packets [0-11] and [20-23] were processed
692 : : * by the same worker
693 : : */
694 [ # # ]: 0 : for (i = 1; i < 12; i++) {
695 [ # # ]: 0 : if (sorted[i] != sorted[0]) {
696 : : printf("Packet number %u processed by worker %u,"
697 : : " but should be processes by worker %u\n",
698 : : i, sorted[i], sorted[0]);
699 : : failed = 1;
700 : : }
701 : : }
702 [ # # ]: 0 : for (i = 20; i < 24; i++) {
703 [ # # ]: 0 : if (sorted[i] != sorted[0]) {
704 : : printf("Packet number %u processed by worker %u,"
705 : : " but should be processes by worker %u\n",
706 : : i, sorted[i], sorted[0]);
707 : : failed = 1;
708 : : }
709 : : }
710 : : /* And verify that packets [12-19] were processed
711 : : * by the another worker
712 : : */
713 [ # # ]: 0 : for (i = 13; i < 20; i++) {
714 [ # # ]: 0 : if (sorted[i] != sorted[12]) {
715 : : printf("Packet number %u processed by worker %u,"
716 : : " but should be processes by worker %u\n",
717 : : i, sorted[i], sorted[12]);
718 : : failed = 1;
719 : : }
720 : : }
721 : :
722 : : rte_mempool_put_bulk(p, (void *)bufs, buf_count);
723 : :
724 [ # # ]: 0 : if (failed)
725 : : return -1;
726 : :
727 : : printf("Marked packets test passed\n");
728 : 0 : return 0;
729 : : }
730 : :
731 : : static
732 : 1 : int test_error_distributor_create_name(void)
733 : : {
734 : : struct rte_distributor *d = NULL;
735 : : struct rte_distributor *db = NULL;
736 : : char *name = NULL;
737 : :
738 : 1 : d = rte_distributor_create(name, rte_socket_id(),
739 : 1 : rte_lcore_count() - 1,
740 : : RTE_DIST_ALG_SINGLE);
741 [ + - - + ]: 1 : if (d != NULL || rte_errno != EINVAL) {
742 : : printf("ERROR: No error on create() with NULL name param\n");
743 : 0 : return -1;
744 : : }
745 : :
746 : 1 : db = rte_distributor_create(name, rte_socket_id(),
747 : 1 : rte_lcore_count() - 1,
748 : : RTE_DIST_ALG_BURST);
749 [ + - - + ]: 1 : if (db != NULL || rte_errno != EINVAL) {
750 : : printf("ERROR: No error on create() with NULL param\n");
751 : 0 : return -1;
752 : : }
753 : :
754 : : return 0;
755 : : }
756 : :
757 : :
758 : : static
759 : 1 : int test_error_distributor_create_numworkers(void)
760 : : {
761 : : struct rte_distributor *ds = NULL;
762 : : struct rte_distributor *db = NULL;
763 : :
764 : 1 : ds = rte_distributor_create("test_numworkers", rte_socket_id(),
765 : : RTE_MAX_LCORE + 10,
766 : : RTE_DIST_ALG_SINGLE);
767 [ + - - + ]: 1 : if (ds != NULL || rte_errno != EINVAL) {
768 : : printf("ERROR: No error on create() with num_workers > MAX\n");
769 : 0 : return -1;
770 : : }
771 : :
772 : 1 : db = rte_distributor_create("test_numworkers", rte_socket_id(),
773 : : RTE_MAX_LCORE + 10,
774 : : RTE_DIST_ALG_BURST);
775 [ + - - + ]: 1 : if (db != NULL || rte_errno != EINVAL) {
776 : : printf("ERROR: No error on create() num_workers > MAX\n");
777 : 0 : return -1;
778 : : }
779 : :
780 : : return 0;
781 : : }
782 : :
783 : :
784 : : /* Useful function which ensures that all worker functions terminate */
785 : : static void
786 : 4 : quit_workers(struct worker_params *wp, struct rte_mempool *p)
787 : : {
788 : 4 : struct rte_distributor *d = wp->dist;
789 [ + - ]: 4 : const unsigned num_workers = rte_lcore_count() - 1;
790 : : unsigned i;
791 : : struct rte_mbuf *bufs[RTE_MAX_LCORE];
792 : : struct rte_mbuf *returns[RTE_MAX_LCORE];
793 [ # # ]: 0 : if (rte_mempool_get_bulk(p, (void *)bufs, num_workers) != 0) {
794 : : printf("line %d: Error getting mbufs from pool\n", __LINE__);
795 : 0 : return;
796 : : }
797 : :
798 : 4 : zero_quit = 0;
799 : 4 : quit = 1;
800 [ + + ]: 8 : for (i = 0; i < num_workers; i++) {
801 : 4 : bufs[i]->hash.usr = i << 1;
802 : 4 : rte_distributor_process(d, &bufs[i], 1);
803 : : }
804 : :
805 : 4 : rte_distributor_process(d, NULL, 0);
806 : 4 : rte_distributor_flush(d);
807 : 4 : rte_eal_mp_wait_lcore();
808 : :
809 [ + + ]: 8 : while (rte_distributor_returned_pkts(d, returns, RTE_MAX_LCORE))
810 : : ;
811 : :
812 : 4 : rte_distributor_clear_returns(d);
813 : : rte_mempool_put_bulk(p, (void *)bufs, num_workers);
814 : :
815 : 4 : quit = 0;
816 : 4 : worker_idx = 0;
817 : 4 : zero_idx = RTE_MAX_LCORE;
818 : 4 : zero_quit = 0;
819 : 4 : zero_sleep = 0;
820 : : }
821 : :
822 : : static int
823 : 1 : test_distributor(void)
824 : : {
825 : : static struct rte_distributor *ds;
826 : : static struct rte_distributor *db;
827 : : static struct rte_distributor *dist[2];
828 : : static struct rte_mempool *p;
829 : : int i;
830 : :
831 : : static const struct rte_mbuf_dynfield seq_dynfield_desc = {
832 : : .name = "test_distributor_dynfield_seq",
833 : : .size = sizeof(seq_dynfield_t),
834 : : .align = __alignof__(seq_dynfield_t),
835 : : };
836 : 1 : seq_dynfield_offset =
837 : 1 : rte_mbuf_dynfield_register(&seq_dynfield_desc);
838 [ - + ]: 1 : if (seq_dynfield_offset < 0) {
839 : : printf("Error registering mbuf field\n");
840 : 0 : return TEST_FAILED;
841 : : }
842 : :
843 [ - + ]: 1 : if (rte_lcore_count() < 2) {
844 : : printf("Not enough cores for distributor_autotest, expecting at least 2\n");
845 : 0 : return TEST_SKIPPED;
846 : : }
847 : :
848 [ + - ]: 1 : if (db == NULL) {
849 : 1 : db = rte_distributor_create("Test_dist_burst", rte_socket_id(),
850 : 1 : rte_lcore_count() - 1,
851 : : RTE_DIST_ALG_BURST);
852 [ - + ]: 1 : if (db == NULL) {
853 : : printf("Error creating burst distributor\n");
854 : 0 : return -1;
855 : : }
856 : : } else {
857 : 0 : rte_distributor_flush(db);
858 : 0 : rte_distributor_clear_returns(db);
859 : : }
860 : :
861 [ + - ]: 1 : if (ds == NULL) {
862 : 1 : ds = rte_distributor_create("Test_dist_single",
863 : : rte_socket_id(),
864 : 1 : rte_lcore_count() - 1,
865 : : RTE_DIST_ALG_SINGLE);
866 [ - + ]: 1 : if (ds == NULL) {
867 : : printf("Error creating single distributor\n");
868 : 0 : return -1;
869 : : }
870 : : } else {
871 : 0 : rte_distributor_flush(ds);
872 : 0 : rte_distributor_clear_returns(ds);
873 : : }
874 : :
875 : 1 : const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
876 [ - + ]: 1 : (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
877 [ + - ]: 1 : if (p == NULL) {
878 : 1 : p = rte_pktmbuf_pool_create("DT_MBUF_POOL", nb_bufs, BURST,
879 : 1 : 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
880 [ - + ]: 1 : if (p == NULL) {
881 : : printf("Error creating mempool\n");
882 : 0 : return -1;
883 : : }
884 : : }
885 : :
886 : 1 : dist[0] = ds;
887 : 1 : dist[1] = db;
888 : :
889 [ + + ]: 3 : for (i = 0; i < 2; i++) {
890 : :
891 : 2 : worker_params.dist = dist[i];
892 [ + + ]: 2 : if (i)
893 : : strlcpy(worker_params.name, "burst",
894 : : sizeof(worker_params.name));
895 : : else
896 : : strlcpy(worker_params.name, "single",
897 : : sizeof(worker_params.name));
898 : :
899 : 2 : rte_eal_mp_remote_launch(handle_work,
900 : : &worker_params, SKIP_MAIN);
901 [ - + ]: 2 : if (sanity_test(&worker_params, p) < 0)
902 : 0 : goto err;
903 : 2 : quit_workers(&worker_params, p);
904 : :
905 : 2 : rte_eal_mp_remote_launch(handle_work_with_free_mbufs,
906 : : &worker_params, SKIP_MAIN);
907 [ - + ]: 2 : if (sanity_test_with_mbuf_alloc(&worker_params, p) < 0)
908 : 0 : goto err;
909 : 2 : quit_workers(&worker_params, p);
910 : :
911 [ - + ]: 2 : if (rte_lcore_count() > 2) {
912 : 0 : rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
913 : : &worker_params,
914 : : SKIP_MAIN);
915 [ # # ]: 0 : if (sanity_test_with_worker_shutdown(&worker_params,
916 : : p) < 0)
917 : 0 : goto err;
918 : 0 : quit_workers(&worker_params, p);
919 : :
920 : 0 : rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
921 : : &worker_params,
922 : : SKIP_MAIN);
923 [ # # ]: 0 : if (test_flush_with_worker_shutdown(&worker_params,
924 : : p) < 0)
925 : 0 : goto err;
926 : 0 : quit_workers(&worker_params, p);
927 : :
928 : 0 : rte_eal_mp_remote_launch(handle_and_mark_work,
929 : : &worker_params, SKIP_MAIN);
930 [ # # ]: 0 : if (sanity_mark_test(&worker_params, p) < 0)
931 : 0 : goto err;
932 : 0 : quit_workers(&worker_params, p);
933 : :
934 : : } else {
935 : : printf("Too few cores to run worker shutdown test\n");
936 : : }
937 : :
938 : : }
939 : :
940 [ + - - + ]: 2 : if (test_error_distributor_create_numworkers() == -1 ||
941 : 1 : test_error_distributor_create_name() == -1) {
942 : : printf("rte_distributor_create parameter check tests failed");
943 : 0 : return -1;
944 : : }
945 : :
946 : : return 0;
947 : :
948 : 0 : err:
949 : 0 : quit_workers(&worker_params, p);
950 : 0 : return -1;
951 : : }
952 : :
953 : : #endif /* !RTE_EXEC_ENV_WINDOWS */
954 : :
955 : 235 : REGISTER_FAST_TEST(distributor_autotest, false, true, test_distributor);
|