4 * Copyright(c) 2017 Cavium networks. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of Cavium networks nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <rte_atomic.h>
34 #include <rte_common.h>
35 #include <rte_cycles.h>
36 #include <rte_debug.h>
38 #include <rte_ethdev.h>
39 #include <rte_eventdev.h>
40 #include <rte_hexdump.h>
42 #include <rte_malloc.h>
43 #include <rte_memcpy.h>
44 #include <rte_launch.h>
45 #include <rte_lcore.h>
46 #include <rte_per_lcore.h>
47 #include <rte_random.h>
51 #define NUM_PACKETS (1 << 18)
52 #define MAX_EVENTS (16 * 1024)
55 static struct rte_mempool *eventdev_test_mempool;
60 uint8_t sub_event_type;
66 static uint32_t seqn_list_index;
67 static int seqn_list[NUM_PACKETS];
72 RTE_BUILD_BUG_ON(NUM_PACKETS < MAX_EVENTS);
73 memset(seqn_list, 0, sizeof(seqn_list));
78 struct test_core_param {
79 rte_atomic32_t *total_events;
80 uint64_t dequeue_tmo_ticks;
88 const char *eventdev_name = "event_octeontx";
90 evdev = rte_event_dev_get_dev_id(eventdev_name);
92 printf("%d: Eventdev %s not found - creating.\n",
93 __LINE__, eventdev_name);
94 if (rte_eal_vdev_init(eventdev_name, NULL) < 0) {
95 printf("Error creating eventdev %s\n", eventdev_name);
98 evdev = rte_event_dev_get_dev_id(eventdev_name);
100 printf("Error finding newly created eventdev\n");
109 testsuite_teardown(void)
111 rte_event_dev_close(evdev);
115 devconf_set_default_sane_values(struct rte_event_dev_config *dev_conf,
116 struct rte_event_dev_info *info)
118 memset(dev_conf, 0, sizeof(struct rte_event_dev_config));
119 dev_conf->dequeue_timeout_ns = info->min_dequeue_timeout_ns;
120 dev_conf->nb_event_ports = info->max_event_ports;
121 dev_conf->nb_event_queues = info->max_event_queues;
122 dev_conf->nb_event_queue_flows = info->max_event_queue_flows;
123 dev_conf->nb_event_port_dequeue_depth =
124 info->max_event_port_dequeue_depth;
125 dev_conf->nb_event_port_enqueue_depth =
126 info->max_event_port_enqueue_depth;
127 dev_conf->nb_event_port_enqueue_depth =
128 info->max_event_port_enqueue_depth;
129 dev_conf->nb_events_limit =
130 info->max_num_events;
134 TEST_EVENTDEV_SETUP_DEFAULT,
135 TEST_EVENTDEV_SETUP_PRIORITY,
136 TEST_EVENTDEV_SETUP_DEQUEUE_TIMEOUT,
140 _eventdev_setup(int mode)
143 struct rte_event_dev_config dev_conf;
144 struct rte_event_dev_info info;
145 const char *pool_name = "evdev_octeontx_test_pool";
147 /* Create and destrory pool for each test case to make it standalone */
148 eventdev_test_mempool = rte_pktmbuf_pool_create(pool_name,
150 0 /*MBUF_CACHE_SIZE*/,
152 512, /* Use very small mbufs */
154 if (!eventdev_test_mempool) {
155 printf("ERROR creating mempool\n");
159 ret = rte_event_dev_info_get(evdev, &info);
160 TEST_ASSERT_SUCCESS(ret, "Failed to get event dev info");
161 TEST_ASSERT(info.max_num_events >= (int32_t)MAX_EVENTS,
162 "max_num_events=%d < max_events=%d",
163 info.max_num_events, MAX_EVENTS);
165 devconf_set_default_sane_values(&dev_conf, &info);
166 if (mode == TEST_EVENTDEV_SETUP_DEQUEUE_TIMEOUT)
167 dev_conf.event_dev_cfg |= RTE_EVENT_DEV_CFG_PER_DEQUEUE_TIMEOUT;
169 ret = rte_event_dev_configure(evdev, &dev_conf);
170 TEST_ASSERT_SUCCESS(ret, "Failed to configure eventdev");
172 if (mode == TEST_EVENTDEV_SETUP_PRIORITY) {
173 /* Configure event queues(0 to n) with
174 * RTE_EVENT_DEV_PRIORITY_HIGHEST to
175 * RTE_EVENT_DEV_PRIORITY_LOWEST
177 uint8_t step = (RTE_EVENT_DEV_PRIORITY_LOWEST + 1) /
178 rte_event_queue_count(evdev);
179 for (i = 0; i < rte_event_queue_count(evdev); i++) {
180 struct rte_event_queue_conf queue_conf;
182 ret = rte_event_queue_default_conf_get(evdev, i,
184 TEST_ASSERT_SUCCESS(ret, "Failed to get def_conf%d", i);
185 queue_conf.priority = i * step;
186 ret = rte_event_queue_setup(evdev, i, &queue_conf);
187 TEST_ASSERT_SUCCESS(ret, "Failed to setup queue=%d", i);
191 /* Configure event queues with default priority */
192 for (i = 0; i < rte_event_queue_count(evdev); i++) {
193 ret = rte_event_queue_setup(evdev, i, NULL);
194 TEST_ASSERT_SUCCESS(ret, "Failed to setup queue=%d", i);
197 /* Configure event ports */
198 for (i = 0; i < rte_event_port_count(evdev); i++) {
199 ret = rte_event_port_setup(evdev, i, NULL);
200 TEST_ASSERT_SUCCESS(ret, "Failed to setup port=%d", i);
201 ret = rte_event_port_link(evdev, i, NULL, NULL, 0);
202 TEST_ASSERT(ret >= 0, "Failed to link all queues port=%d", i);
205 ret = rte_event_dev_start(evdev);
206 TEST_ASSERT_SUCCESS(ret, "Failed to start device");
214 return _eventdev_setup(TEST_EVENTDEV_SETUP_DEFAULT);
218 eventdev_setup_priority(void)
220 return _eventdev_setup(TEST_EVENTDEV_SETUP_PRIORITY);
224 eventdev_teardown(void)
226 rte_event_dev_stop(evdev);
227 rte_mempool_free(eventdev_test_mempool);
231 update_event_and_validation_attr(struct rte_mbuf *m, struct rte_event *ev,
232 uint32_t flow_id, uint8_t event_type,
233 uint8_t sub_event_type, uint8_t sched_type,
234 uint8_t queue, uint8_t port)
236 struct event_attr *attr;
238 /* Store the event attributes in mbuf for future reference */
239 attr = rte_pktmbuf_mtod(m, struct event_attr *);
240 attr->flow_id = flow_id;
241 attr->event_type = event_type;
242 attr->sub_event_type = sub_event_type;
243 attr->sched_type = sched_type;
247 ev->flow_id = flow_id;
248 ev->sub_event_type = sub_event_type;
249 ev->event_type = event_type;
250 /* Inject the new event */
251 ev->op = RTE_EVENT_OP_NEW;
252 ev->sched_type = sched_type;
253 ev->queue_id = queue;
258 inject_events(uint32_t flow_id, uint8_t event_type, uint8_t sub_event_type,
259 uint8_t sched_type, uint8_t queue, uint8_t port,
265 for (i = 0; i < events; i++) {
266 struct rte_event ev = {.event = 0, .u64 = 0};
268 m = rte_pktmbuf_alloc(eventdev_test_mempool);
269 TEST_ASSERT_NOT_NULL(m, "mempool alloc failed");
272 update_event_and_validation_attr(m, &ev, flow_id, event_type,
273 sub_event_type, sched_type, queue, port);
274 rte_event_enqueue_burst(evdev, port, &ev, 1);
280 check_excess_events(uint8_t port)
283 uint16_t valid_event;
286 /* Check for excess events, try for a few times and exit */
287 for (i = 0; i < 32; i++) {
288 valid_event = rte_event_dequeue_burst(evdev, port, &ev, 1, 0);
290 TEST_ASSERT_SUCCESS(valid_event, "Unexpected valid event=%d",
297 generate_random_events(const unsigned int total_events)
299 struct rte_event_dev_info info;
303 ret = rte_event_dev_info_get(evdev, &info);
304 TEST_ASSERT_SUCCESS(ret, "Failed to get event dev info");
305 for (i = 0; i < total_events; i++) {
307 rte_rand() % info.max_event_queue_flows /*flow_id */,
308 rte_rand() % (RTE_EVENT_TYPE_CPU + 1) /* event_type */,
309 rte_rand() % 256 /* sub_event_type */,
310 rte_rand() % (RTE_SCHED_TYPE_PARALLEL + 1),
311 rte_rand() % rte_event_queue_count(evdev) /* queue */,
322 validate_event(struct rte_event *ev)
324 struct event_attr *attr;
326 attr = rte_pktmbuf_mtod(ev->mbuf, struct event_attr *);
327 TEST_ASSERT_EQUAL(attr->flow_id, ev->flow_id,
328 "flow_id mismatch enq=%d deq =%d",
329 attr->flow_id, ev->flow_id);
330 TEST_ASSERT_EQUAL(attr->event_type, ev->event_type,
331 "event_type mismatch enq=%d deq =%d",
332 attr->event_type, ev->event_type);
333 TEST_ASSERT_EQUAL(attr->sub_event_type, ev->sub_event_type,
334 "sub_event_type mismatch enq=%d deq =%d",
335 attr->sub_event_type, ev->sub_event_type);
336 TEST_ASSERT_EQUAL(attr->sched_type, ev->sched_type,
337 "sched_type mismatch enq=%d deq =%d",
338 attr->sched_type, ev->sched_type);
339 TEST_ASSERT_EQUAL(attr->queue, ev->queue_id,
340 "queue mismatch enq=%d deq =%d",
341 attr->queue, ev->queue_id);
345 typedef int (*validate_event_cb)(uint32_t index, uint8_t port,
346 struct rte_event *ev);
349 consume_events(uint8_t port, const uint32_t total_events, validate_event_cb fn)
352 uint16_t valid_event;
353 uint32_t events = 0, forward_progress_cnt = 0, index = 0;
357 if (++forward_progress_cnt > UINT16_MAX) {
358 printf("Detected deadlock\n");
362 valid_event = rte_event_dequeue_burst(evdev, port, &ev, 1, 0);
366 forward_progress_cnt = 0;
367 ret = validate_event(&ev);
372 ret = fn(index, port, &ev);
373 TEST_ASSERT_SUCCESS(ret,
374 "Failed to validate test specific event");
379 rte_pktmbuf_free(ev.mbuf);
380 if (++events >= total_events)
384 return check_excess_events(port);
388 validate_simple_enqdeq(uint32_t index, uint8_t port, struct rte_event *ev)
391 TEST_ASSERT_EQUAL(index, ev->mbuf->seqn, "index=%d != seqn=%d", index,
397 test_simple_enqdeq(uint8_t sched_type)
401 ret = inject_events(0 /*flow_id */,
402 RTE_EVENT_TYPE_CPU /* event_type */,
403 0 /* sub_event_type */,
411 return consume_events(0 /* port */, MAX_EVENTS, validate_simple_enqdeq);
415 test_simple_enqdeq_ordered(void)
417 return test_simple_enqdeq(RTE_SCHED_TYPE_ORDERED);
421 test_simple_enqdeq_atomic(void)
423 return test_simple_enqdeq(RTE_SCHED_TYPE_ATOMIC);
427 test_simple_enqdeq_parallel(void)
429 return test_simple_enqdeq(RTE_SCHED_TYPE_PARALLEL);
433 * Generate a prescribed number of events and spread them across available
434 * queues. On dequeue, using single event port(port 0) verify the enqueued
438 test_multi_queue_enq_single_port_deq(void)
442 ret = generate_random_events(MAX_EVENTS);
446 return consume_events(0 /* port */, MAX_EVENTS, NULL);
450 * Inject 0..MAX_EVENTS events over 0..rte_event_queue_count() with modulus
453 * For example, Inject 32 events over 0..7 queues
454 * enqueue events 0, 8, 16, 24 in queue 0
455 * enqueue events 1, 9, 17, 25 in queue 1
458 * enqueue events 7, 15, 23, 31 in queue 7
460 * On dequeue, Validate the events comes in 0,8,16,24,1,9,17,25..,7,15,23,31
461 * order from queue0(highest priority) to queue7(lowest_priority)
464 validate_queue_priority(uint32_t index, uint8_t port, struct rte_event *ev)
466 uint32_t range = MAX_EVENTS / rte_event_queue_count(evdev);
467 uint32_t expected_val = (index % range) * rte_event_queue_count(evdev);
469 expected_val += ev->queue_id;
471 TEST_ASSERT_EQUAL(ev->mbuf->seqn, expected_val,
472 "seqn=%d index=%d expected=%d range=%d nb_queues=%d max_event=%d",
473 ev->mbuf->seqn, index, expected_val, range,
474 rte_event_queue_count(evdev), MAX_EVENTS);
479 test_multi_queue_priority(void)
483 int i, max_evts_roundoff;
485 /* See validate_queue_priority() comments for priority validate logic */
486 max_evts_roundoff = MAX_EVENTS / rte_event_queue_count(evdev);
487 max_evts_roundoff *= rte_event_queue_count(evdev);
489 for (i = 0; i < max_evts_roundoff; i++) {
490 struct rte_event ev = {.event = 0, .u64 = 0};
492 m = rte_pktmbuf_alloc(eventdev_test_mempool);
493 TEST_ASSERT_NOT_NULL(m, "mempool alloc failed");
496 queue = i % rte_event_queue_count(evdev);
497 update_event_and_validation_attr(m, &ev, 0, RTE_EVENT_TYPE_CPU,
498 0, RTE_SCHED_TYPE_PARALLEL, queue, 0);
499 rte_event_enqueue_burst(evdev, 0, &ev, 1);
502 return consume_events(0, max_evts_roundoff, validate_queue_priority);
506 worker_multi_port_fn(void *arg)
508 struct test_core_param *param = arg;
510 uint16_t valid_event;
511 uint8_t port = param->port;
512 rte_atomic32_t *total_events = param->total_events;
515 while (rte_atomic32_read(total_events) > 0) {
516 valid_event = rte_event_dequeue_burst(evdev, port, &ev, 1, 0);
520 ret = validate_event(&ev);
521 TEST_ASSERT_SUCCESS(ret, "Failed to validate event");
522 rte_pktmbuf_free(ev.mbuf);
523 rte_atomic32_sub(total_events, 1);
529 wait_workers_to_join(int lcore, const rte_atomic32_t *count)
531 uint64_t cycles, print_cycles;
533 print_cycles = cycles = rte_get_timer_cycles();
534 while (rte_eal_get_lcore_state(lcore) != FINISHED) {
535 uint64_t new_cycles = rte_get_timer_cycles();
537 if (new_cycles - print_cycles > rte_get_timer_hz()) {
538 printf("\r%s: events %d\n", __func__,
539 rte_atomic32_read(count));
540 print_cycles = new_cycles;
542 if (new_cycles - cycles > rte_get_timer_hz() * 10) {
543 printf("%s: No schedules for seconds, deadlock (%d)\n",
545 rte_atomic32_read(count));
546 rte_event_dev_dump(evdev, stdout);
551 rte_eal_mp_wait_lcore();
557 launch_workers_and_wait(int (*master_worker)(void *),
558 int (*slave_workers)(void *), uint32_t total_events,
559 uint8_t nb_workers, uint8_t sched_type)
564 struct test_core_param *param;
565 rte_atomic32_t atomic_total_events;
566 uint64_t dequeue_tmo_ticks;
571 rte_atomic32_set(&atomic_total_events, total_events);
574 param = malloc(sizeof(struct test_core_param) * nb_workers);
578 ret = rte_event_dequeue_timeout_ticks(evdev,
579 rte_rand() % 10000000/* 10ms */, &dequeue_tmo_ticks);
583 param[0].total_events = &atomic_total_events;
584 param[0].sched_type = sched_type;
586 param[0].dequeue_tmo_ticks = dequeue_tmo_ticks;
589 w_lcore = rte_get_next_lcore(
593 rte_eal_remote_launch(master_worker, ¶m[0], w_lcore);
595 for (port = 1; port < nb_workers; port++) {
596 param[port].total_events = &atomic_total_events;
597 param[port].sched_type = sched_type;
598 param[port].port = port;
599 param[port].dequeue_tmo_ticks = dequeue_tmo_ticks;
601 w_lcore = rte_get_next_lcore(w_lcore, 1, 0);
602 rte_eal_remote_launch(slave_workers, ¶m[port], w_lcore);
605 ret = wait_workers_to_join(w_lcore, &atomic_total_events);
611 * Generate a prescribed number of events and spread them across available
612 * queues. Dequeue the events through multiple ports and verify the enqueued
616 test_multi_queue_enq_multi_port_deq(void)
618 const unsigned int total_events = MAX_EVENTS;
622 ret = generate_random_events(total_events);
626 nr_ports = RTE_MIN(rte_event_port_count(evdev), rte_lcore_count() - 1);
629 printf("%s: Not enough ports=%d or workers=%d\n", __func__,
630 rte_event_port_count(evdev), rte_lcore_count() - 1);
634 return launch_workers_and_wait(worker_multi_port_fn,
635 worker_multi_port_fn, total_events,
636 nr_ports, 0xff /* invalid */);
639 static struct unit_test_suite eventdev_octeontx_testsuite = {
640 .suite_name = "eventdev octeontx unit test suite",
641 .setup = testsuite_setup,
642 .teardown = testsuite_teardown,
644 TEST_CASE_ST(eventdev_setup, eventdev_teardown,
645 test_simple_enqdeq_ordered),
646 TEST_CASE_ST(eventdev_setup, eventdev_teardown,
647 test_simple_enqdeq_atomic),
648 TEST_CASE_ST(eventdev_setup, eventdev_teardown,
649 test_simple_enqdeq_parallel),
650 TEST_CASE_ST(eventdev_setup, eventdev_teardown,
651 test_multi_queue_enq_single_port_deq),
652 TEST_CASE_ST(eventdev_setup_priority, eventdev_teardown,
653 test_multi_queue_priority),
654 TEST_CASE_ST(eventdev_setup, eventdev_teardown,
655 test_multi_queue_enq_multi_port_deq),
656 TEST_CASES_END() /**< NULL terminate unit test array */
661 test_eventdev_octeontx(void)
663 return unit_test_suite_runner(&eventdev_octeontx_testsuite);
666 REGISTER_TEST_COMMAND(eventdev_octeontx_autotest, test_eventdev_octeontx);