1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
|
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2023 Ericsson AB
*/
#include <rte_bus_vdev.h>
#include <rte_dispatcher.h>
#include <rte_eventdev.h>
#include <rte_random.h>
#include <rte_service.h>
#include <rte_stdatomic.h>
#include "test.h"
#define NUM_WORKERS 3
#define NUM_PORTS (NUM_WORKERS + 1)
#define WORKER_PORT_ID(worker_idx) (worker_idx)
#define DRIVER_PORT_ID (NUM_PORTS - 1)
#define NUM_SERVICE_CORES NUM_WORKERS
#define MIN_LCORES (NUM_SERVICE_CORES + 1)
/* Eventdev */
#define NUM_QUEUES 8
#define LAST_QUEUE_ID (NUM_QUEUES - 1)
#define MAX_EVENTS 4096
#define NEW_EVENT_THRESHOLD (MAX_EVENTS / 2)
#define DEQUEUE_BURST_SIZE 32
#define ENQUEUE_BURST_SIZE 32
#define NUM_EVENTS 10000000
#define NUM_FLOWS 16
#define DSW_VDEV "event_dsw0"
struct app_queue {
uint8_t queue_id;
uint64_t sn[NUM_FLOWS];
int dispatcher_reg_id;
};
struct cb_count {
uint8_t expected_event_dev_id;
uint8_t expected_event_port_id[RTE_MAX_LCORE];
RTE_ATOMIC(int) count;
};
struct test_app {
uint8_t event_dev_id;
struct rte_dispatcher *dispatcher;
uint32_t dispatcher_service_id;
unsigned int service_lcores[NUM_SERVICE_CORES];
int never_match_reg_id;
uint64_t never_match_count;
struct cb_count never_process_count;
struct app_queue queues[NUM_QUEUES];
int finalize_reg_id;
struct cb_count finalize_count;
bool running;
RTE_ATOMIC(int) completed_events;
RTE_ATOMIC(int) errors;
};
static struct test_app *
test_app_create(void)
{
int i;
struct test_app *app;
app = calloc(1, sizeof(struct test_app));
if (app == NULL)
return NULL;
for (i = 0; i < NUM_QUEUES; i++)
app->queues[i].queue_id = i;
return app;
}
static void
test_app_free(struct test_app *app)
{
free(app);
}
static int
test_app_create_vdev(struct test_app *app)
{
int rc;
rc = rte_vdev_init(DSW_VDEV, NULL);
if (rc < 0)
return TEST_SKIPPED;
rc = rte_event_dev_get_dev_id(DSW_VDEV);
app->event_dev_id = (uint8_t)rc;
return TEST_SUCCESS;
}
static int
test_app_destroy_vdev(struct test_app *app)
{
int rc;
rc = rte_event_dev_close(app->event_dev_id);
TEST_ASSERT_SUCCESS(rc, "Error while closing event device");
rc = rte_vdev_uninit(DSW_VDEV);
TEST_ASSERT_SUCCESS(rc, "Error while uninitializing virtual device");
return TEST_SUCCESS;
}
static int
test_app_setup_event_dev(struct test_app *app)
{
int rc;
int i;
rc = test_app_create_vdev(app);
if (rc != TEST_SUCCESS)
return rc;
struct rte_event_dev_config config = {
.nb_event_queues = NUM_QUEUES,
.nb_event_ports = NUM_PORTS,
.nb_events_limit = MAX_EVENTS,
.nb_event_queue_flows = 64,
.nb_event_port_dequeue_depth = DEQUEUE_BURST_SIZE,
.nb_event_port_enqueue_depth = ENQUEUE_BURST_SIZE
};
rc = rte_event_dev_configure(app->event_dev_id, &config);
TEST_ASSERT_SUCCESS(rc, "Unable to configure event device");
struct rte_event_queue_conf queue_config = {
.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
.schedule_type = RTE_SCHED_TYPE_ATOMIC,
.nb_atomic_flows = 64
};
for (i = 0; i < NUM_QUEUES; i++) {
uint8_t queue_id = i;
rc = rte_event_queue_setup(app->event_dev_id, queue_id,
&queue_config);
TEST_ASSERT_SUCCESS(rc, "Unable to setup queue %d", queue_id);
}
struct rte_event_port_conf port_config = {
.new_event_threshold = NEW_EVENT_THRESHOLD,
.dequeue_depth = DEQUEUE_BURST_SIZE,
.enqueue_depth = ENQUEUE_BURST_SIZE
};
for (i = 0; i < NUM_PORTS; i++) {
uint8_t event_port_id = i;
rc = rte_event_port_setup(app->event_dev_id, event_port_id,
&port_config);
TEST_ASSERT_SUCCESS(rc, "Failed to create event port %d",
event_port_id);
if (event_port_id == DRIVER_PORT_ID)
continue;
rc = rte_event_port_link(app->event_dev_id, event_port_id,
NULL, NULL, 0);
TEST_ASSERT_EQUAL(rc, NUM_QUEUES, "Failed to link port %d",
event_port_id);
}
return TEST_SUCCESS;
}
static int
test_app_teardown_event_dev(struct test_app *app)
{
return test_app_destroy_vdev(app);
}
static int
test_app_start_event_dev(struct test_app *app)
{
int rc;
rc = rte_event_dev_start(app->event_dev_id);
TEST_ASSERT_SUCCESS(rc, "Unable to start event device");
return TEST_SUCCESS;
}
static void
test_app_stop_event_dev(struct test_app *app)
{
rte_event_dev_stop(app->event_dev_id);
}
static int
test_app_create_dispatcher(struct test_app *app)
{
int rc;
app->dispatcher = rte_dispatcher_create(app->event_dev_id);
TEST_ASSERT(app->dispatcher != NULL, "Unable to create event "
"dispatcher");
app->dispatcher_service_id =
rte_dispatcher_service_id_get(app->dispatcher);
rc = rte_service_set_stats_enable(app->dispatcher_service_id, 1);
TEST_ASSERT_SUCCESS(rc, "Unable to enable event dispatcher service "
"stats");
rc = rte_service_runstate_set(app->dispatcher_service_id, 1);
TEST_ASSERT_SUCCESS(rc, "Unable to set dispatcher service runstate");
return TEST_SUCCESS;
}
static int
test_app_free_dispatcher(struct test_app *app)
{
int rc;
rc = rte_service_runstate_set(app->dispatcher_service_id, 0);
TEST_ASSERT_SUCCESS(rc, "Error disabling dispatcher service");
rc = rte_dispatcher_free(app->dispatcher);
TEST_ASSERT_SUCCESS(rc, "Error freeing dispatcher");
return TEST_SUCCESS;
}
static int
test_app_bind_ports(struct test_app *app)
{
int i;
app->never_process_count.expected_event_dev_id =
app->event_dev_id;
app->finalize_count.expected_event_dev_id =
app->event_dev_id;
for (i = 0; i < NUM_WORKERS; i++) {
unsigned int lcore_id = app->service_lcores[i];
uint8_t port_id = WORKER_PORT_ID(i);
int rc = rte_dispatcher_bind_port_to_lcore(
app->dispatcher, port_id, DEQUEUE_BURST_SIZE, 0,
lcore_id
);
TEST_ASSERT_SUCCESS(rc, "Unable to bind event device port %d "
"to lcore %d", port_id, lcore_id);
app->never_process_count.expected_event_port_id[lcore_id] =
port_id;
app->finalize_count.expected_event_port_id[lcore_id] = port_id;
}
return TEST_SUCCESS;
}
static int
test_app_unbind_ports(struct test_app *app)
{
int i;
for (i = 0; i < NUM_WORKERS; i++) {
unsigned int lcore_id = app->service_lcores[i];
int rc = rte_dispatcher_unbind_port_from_lcore(
app->dispatcher,
WORKER_PORT_ID(i),
lcore_id
);
TEST_ASSERT_SUCCESS(rc, "Unable to unbind event device port %d "
"from lcore %d", WORKER_PORT_ID(i),
lcore_id);
}
return TEST_SUCCESS;
}
static bool
match_queue(const struct rte_event *event, void *cb_data)
{
uintptr_t queue_id = (uintptr_t)cb_data;
return event->queue_id == queue_id;
}
static int
test_app_get_worker_index(struct test_app *app, unsigned int lcore_id)
{
int i;
for (i = 0; i < NUM_SERVICE_CORES; i++)
if (app->service_lcores[i] == lcore_id)
return i;
return -1;
}
static int
test_app_get_worker_port(struct test_app *app, unsigned int lcore_id)
{
int worker;
worker = test_app_get_worker_index(app, lcore_id);
if (worker < 0)
return -1;
return WORKER_PORT_ID(worker);
}
static void
test_app_queue_note_error(struct test_app *app)
{
rte_atomic_fetch_add_explicit(&app->errors, 1, rte_memory_order_relaxed);
}
static void
test_app_process_queue(uint8_t p_event_dev_id, uint8_t p_event_port_id,
struct rte_event *in_events, uint16_t num,
void *cb_data)
{
struct app_queue *app_queue = cb_data;
struct test_app *app = container_of(app_queue, struct test_app,
queues[app_queue->queue_id]);
unsigned int lcore_id = rte_lcore_id();
bool intermediate_queue = app_queue->queue_id != LAST_QUEUE_ID;
int event_port_id;
uint16_t i;
struct rte_event out_events[num];
event_port_id = test_app_get_worker_port(app, lcore_id);
if (event_port_id < 0 || p_event_dev_id != app->event_dev_id ||
p_event_port_id != event_port_id) {
test_app_queue_note_error(app);
return;
}
for (i = 0; i < num; i++) {
const struct rte_event *in_event = &in_events[i];
struct rte_event *out_event = &out_events[i];
uint64_t sn = in_event->u64;
uint64_t expected_sn;
if (in_event->queue_id != app_queue->queue_id) {
test_app_queue_note_error(app);
return;
}
expected_sn = app_queue->sn[in_event->flow_id]++;
if (expected_sn != sn) {
test_app_queue_note_error(app);
return;
}
if (intermediate_queue)
*out_event = (struct rte_event) {
.queue_id = in_event->queue_id + 1,
.flow_id = in_event->flow_id,
.sched_type = RTE_SCHED_TYPE_ATOMIC,
.op = RTE_EVENT_OP_FORWARD,
.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
.u64 = sn
};
}
if (intermediate_queue) {
uint16_t n = 0;
do {
n += rte_event_enqueue_forward_burst(p_event_dev_id,
p_event_port_id,
out_events + n,
num - n);
} while (n != num);
} else
rte_atomic_fetch_add_explicit(&app->completed_events, num,
rte_memory_order_relaxed);
}
static bool
never_match(const struct rte_event *event __rte_unused, void *cb_data)
{
uint64_t *count = cb_data;
(*count)++;
return false;
}
static void
test_app_never_process(uint8_t event_dev_id, uint8_t event_port_id,
struct rte_event *in_events __rte_unused, uint16_t num, void *cb_data)
{
struct cb_count *count = cb_data;
unsigned int lcore_id = rte_lcore_id();
if (event_dev_id == count->expected_event_dev_id &&
event_port_id == count->expected_event_port_id[lcore_id])
rte_atomic_fetch_add_explicit(&count->count, num,
rte_memory_order_relaxed);
}
static void
finalize(uint8_t event_dev_id, uint8_t event_port_id, void *cb_data)
{
struct cb_count *count = cb_data;
unsigned int lcore_id = rte_lcore_id();
if (event_dev_id == count->expected_event_dev_id &&
event_port_id == count->expected_event_port_id[lcore_id])
rte_atomic_fetch_add_explicit(&count->count, 1,
rte_memory_order_relaxed);
}
static int
test_app_register_callbacks(struct test_app *app)
{
int i;
app->never_match_reg_id =
rte_dispatcher_register(app->dispatcher, never_match,
&app->never_match_count,
test_app_never_process,
&app->never_process_count);
TEST_ASSERT(app->never_match_reg_id >= 0, "Unable to register "
"never-match handler");
for (i = 0; i < NUM_QUEUES; i++) {
struct app_queue *app_queue = &app->queues[i];
uintptr_t queue_id = app_queue->queue_id;
int reg_id;
reg_id = rte_dispatcher_register(app->dispatcher,
match_queue, (void *)queue_id,
test_app_process_queue,
app_queue);
TEST_ASSERT(reg_id >= 0, "Unable to register consumer "
"callback for queue %d", i);
app_queue->dispatcher_reg_id = reg_id;
}
app->finalize_reg_id =
rte_dispatcher_finalize_register(app->dispatcher,
finalize,
&app->finalize_count);
TEST_ASSERT_SUCCESS(app->finalize_reg_id, "Error registering "
"finalize callback");
return TEST_SUCCESS;
}
static int
test_app_unregister_callback(struct test_app *app, uint8_t queue_id)
{
int reg_id = app->queues[queue_id].dispatcher_reg_id;
int rc;
if (reg_id < 0) /* unregistered already */
return 0;
rc = rte_dispatcher_unregister(app->dispatcher, reg_id);
TEST_ASSERT_SUCCESS(rc, "Unable to unregister consumer "
"callback for queue %d", queue_id);
app->queues[queue_id].dispatcher_reg_id = -1;
return TEST_SUCCESS;
}
static int
test_app_unregister_callbacks(struct test_app *app)
{
int i;
int rc;
if (app->never_match_reg_id >= 0) {
rc = rte_dispatcher_unregister(app->dispatcher,
app->never_match_reg_id);
TEST_ASSERT_SUCCESS(rc, "Unable to unregister never-match "
"handler");
app->never_match_reg_id = -1;
}
for (i = 0; i < NUM_QUEUES; i++) {
rc = test_app_unregister_callback(app, i);
if (rc != TEST_SUCCESS)
return rc;
}
if (app->finalize_reg_id >= 0) {
rc = rte_dispatcher_finalize_unregister(
app->dispatcher, app->finalize_reg_id
);
app->finalize_reg_id = -1;
}
return TEST_SUCCESS;
}
static void
test_app_start_dispatcher(struct test_app *app)
{
rte_dispatcher_start(app->dispatcher);
}
static void
test_app_stop_dispatcher(struct test_app *app)
{
rte_dispatcher_stop(app->dispatcher);
}
static int
test_app_reset_dispatcher_stats(struct test_app *app)
{
struct rte_dispatcher_stats stats;
rte_dispatcher_stats_reset(app->dispatcher);
memset(&stats, 0xff, sizeof(stats));
rte_dispatcher_stats_get(app->dispatcher, &stats);
TEST_ASSERT_EQUAL(stats.poll_count, 0, "Poll count not zero");
TEST_ASSERT_EQUAL(stats.ev_batch_count, 0, "Batch count not zero");
TEST_ASSERT_EQUAL(stats.ev_dispatch_count, 0, "Dispatch count "
"not zero");
TEST_ASSERT_EQUAL(stats.ev_drop_count, 0, "Drop count not zero");
return TEST_SUCCESS;
}
static int
test_app_setup_service_core(struct test_app *app, unsigned int lcore_id)
{
int rc;
rc = rte_service_lcore_add(lcore_id);
TEST_ASSERT_SUCCESS(rc, "Unable to make lcore %d an event dispatcher "
"service core", lcore_id);
rc = rte_service_map_lcore_set(app->dispatcher_service_id, lcore_id, 1);
TEST_ASSERT_SUCCESS(rc, "Unable to map event dispatcher service");
return TEST_SUCCESS;
}
static int
test_app_setup_service_cores(struct test_app *app)
{
int i;
int lcore_id = -1;
for (i = 0; i < NUM_SERVICE_CORES; i++) {
lcore_id = rte_get_next_lcore(lcore_id, 1, 0);
app->service_lcores[i] = lcore_id;
}
for (i = 0; i < NUM_SERVICE_CORES; i++) {
int rc;
rc = test_app_setup_service_core(app, app->service_lcores[i]);
if (rc != TEST_SUCCESS)
return rc;
}
return TEST_SUCCESS;
}
static int
test_app_teardown_service_core(struct test_app *app, unsigned int lcore_id)
{
int rc;
rc = rte_service_map_lcore_set(app->dispatcher_service_id, lcore_id, 0);
TEST_ASSERT_SUCCESS(rc, "Unable to unmap event dispatcher service");
rc = rte_service_lcore_del(lcore_id);
TEST_ASSERT_SUCCESS(rc, "Unable change role of service lcore %d",
lcore_id);
return TEST_SUCCESS;
}
static int
test_app_teardown_service_cores(struct test_app *app)
{
int i;
for (i = 0; i < NUM_SERVICE_CORES; i++) {
unsigned int lcore_id = app->service_lcores[i];
int rc;
rc = test_app_teardown_service_core(app, lcore_id);
if (rc != TEST_SUCCESS)
return rc;
}
return TEST_SUCCESS;
}
static int
test_app_start_service_cores(struct test_app *app)
{
int i;
for (i = 0; i < NUM_SERVICE_CORES; i++) {
unsigned int lcore_id = app->service_lcores[i];
int rc;
rc = rte_service_lcore_start(lcore_id);
TEST_ASSERT_SUCCESS(rc, "Unable to start service lcore %d",
lcore_id);
}
return TEST_SUCCESS;
}
static int
test_app_stop_service_cores(struct test_app *app)
{
int i;
for (i = 0; i < NUM_SERVICE_CORES; i++) {
unsigned int lcore_id = app->service_lcores[i];
int rc;
rc = rte_service_lcore_stop(lcore_id);
TEST_ASSERT_SUCCESS(rc, "Unable to stop service lcore %d",
lcore_id);
}
return TEST_SUCCESS;
}
static int
test_app_start(struct test_app *app)
{
int rc;
rc = test_app_start_event_dev(app);
if (rc != TEST_SUCCESS)
return rc;
rc = test_app_start_service_cores(app);
if (rc != TEST_SUCCESS)
return rc;
test_app_start_dispatcher(app);
app->running = true;
return TEST_SUCCESS;
}
static int
test_app_stop(struct test_app *app)
{
int rc;
test_app_stop_dispatcher(app);
rc = test_app_stop_service_cores(app);
if (rc != TEST_SUCCESS)
return rc;
test_app_stop_event_dev(app);
app->running = false;
return TEST_SUCCESS;
}
struct test_app *test_app;
static int
test_setup(void)
{
int rc;
if (rte_lcore_count() < MIN_LCORES) {
printf("Not enough cores for dispatcher_autotest; expecting at "
"least %d.\n", MIN_LCORES);
return TEST_SKIPPED;
}
test_app = test_app_create();
TEST_ASSERT(test_app != NULL, "Unable to allocate memory");
rc = test_app_setup_event_dev(test_app);
if (rc != TEST_SUCCESS)
goto err_free_app;
rc = test_app_create_dispatcher(test_app);
if (rc != TEST_SUCCESS)
goto err_teardown_event_dev;
rc = test_app_setup_service_cores(test_app);
if (rc != TEST_SUCCESS)
goto err_free_dispatcher;
rc = test_app_register_callbacks(test_app);
if (rc != TEST_SUCCESS)
goto err_teardown_service_cores;
rc = test_app_bind_ports(test_app);
if (rc != TEST_SUCCESS)
goto err_unregister_callbacks;
return TEST_SUCCESS;
err_unregister_callbacks:
test_app_unregister_callbacks(test_app);
err_teardown_service_cores:
test_app_teardown_service_cores(test_app);
err_free_dispatcher:
test_app_free_dispatcher(test_app);
err_teardown_event_dev:
test_app_teardown_event_dev(test_app);
err_free_app:
test_app_free(test_app);
test_app = NULL;
return rc;
}
static void test_teardown(void)
{
if (test_app == NULL)
return;
if (test_app->running)
test_app_stop(test_app);
test_app_teardown_service_cores(test_app);
test_app_unregister_callbacks(test_app);
test_app_unbind_ports(test_app);
test_app_free_dispatcher(test_app);
test_app_teardown_event_dev(test_app);
test_app_free(test_app);
test_app = NULL;
}
static int
test_app_get_completed_events(struct test_app *app)
{
return rte_atomic_load_explicit(&app->completed_events,
rte_memory_order_relaxed);
}
static int
test_app_get_errors(struct test_app *app)
{
return rte_atomic_load_explicit(&app->errors, rte_memory_order_relaxed);
}
static int
test_basic(void)
{
int rc;
int i;
rc = test_app_start(test_app);
if (rc != TEST_SUCCESS)
return rc;
uint64_t sns[NUM_FLOWS] = { 0 };
for (i = 0; i < NUM_EVENTS;) {
struct rte_event events[ENQUEUE_BURST_SIZE];
int left;
int batch_size;
int j;
uint16_t n = 0;
batch_size = 1 + rte_rand_max(ENQUEUE_BURST_SIZE);
left = NUM_EVENTS - i;
batch_size = RTE_MIN(left, batch_size);
for (j = 0; j < batch_size; j++) {
struct rte_event *event = &events[j];
uint64_t sn;
uint32_t flow_id;
flow_id = rte_rand_max(NUM_FLOWS);
sn = sns[flow_id]++;
*event = (struct rte_event) {
.queue_id = 0,
.flow_id = flow_id,
.sched_type = RTE_SCHED_TYPE_ATOMIC,
.op = RTE_EVENT_OP_NEW,
.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
.u64 = sn
};
}
while (n < batch_size)
n += rte_event_enqueue_new_burst(test_app->event_dev_id,
DRIVER_PORT_ID,
events + n,
batch_size - n);
i += batch_size;
}
while (test_app_get_completed_events(test_app) != NUM_EVENTS)
rte_event_maintain(test_app->event_dev_id, DRIVER_PORT_ID, 0);
rc = test_app_get_errors(test_app);
TEST_ASSERT(rc == 0, "%d errors occurred", rc);
rc = test_app_stop(test_app);
if (rc != TEST_SUCCESS)
return rc;
struct rte_dispatcher_stats stats;
rte_dispatcher_stats_get(test_app->dispatcher, &stats);
TEST_ASSERT_EQUAL(stats.ev_drop_count, 0, "Drop count is not zero");
TEST_ASSERT_EQUAL(stats.ev_dispatch_count, NUM_EVENTS * NUM_QUEUES,
"Invalid dispatch count");
TEST_ASSERT(stats.poll_count > 0, "Poll count is zero");
TEST_ASSERT_EQUAL(test_app->never_process_count.count, 0,
"Never-match handler's process function has "
"been called");
int finalize_count =
rte_atomic_load_explicit(&test_app->finalize_count.count,
rte_memory_order_relaxed);
TEST_ASSERT(finalize_count > 0, "Finalize count is zero");
TEST_ASSERT(finalize_count <= (int)stats.ev_dispatch_count,
"Finalize count larger than event count");
TEST_ASSERT_EQUAL(finalize_count, (int)stats.ev_batch_count,
"%"PRIu64" batches dequeued, but finalize called %d "
"times", stats.ev_batch_count, finalize_count);
/*
* The event dispatcher should call often-matching match functions
* more often, and thus this never-matching match function should
* be called relatively infrequently.
*/
TEST_ASSERT(test_app->never_match_count <
(stats.ev_dispatch_count / 4),
"Never-matching match function called suspiciously often");
rc = test_app_reset_dispatcher_stats(test_app);
if (rc != TEST_SUCCESS)
return rc;
return TEST_SUCCESS;
}
static int
test_drop(void)
{
int rc;
uint8_t unhandled_queue;
struct rte_dispatcher_stats stats;
unhandled_queue = (uint8_t)rte_rand_max(NUM_QUEUES);
rc = test_app_start(test_app);
if (rc != TEST_SUCCESS)
return rc;
rc = test_app_unregister_callback(test_app, unhandled_queue);
if (rc != TEST_SUCCESS)
return rc;
struct rte_event event = {
.queue_id = unhandled_queue,
.flow_id = 0,
.sched_type = RTE_SCHED_TYPE_ATOMIC,
.op = RTE_EVENT_OP_NEW,
.priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
.u64 = 0
};
do {
rc = rte_event_enqueue_burst(test_app->event_dev_id,
DRIVER_PORT_ID, &event, 1);
} while (rc == 0);
do {
rte_dispatcher_stats_get(test_app->dispatcher, &stats);
rte_event_maintain(test_app->event_dev_id, DRIVER_PORT_ID, 0);
} while (stats.ev_drop_count == 0 && stats.ev_dispatch_count == 0);
rc = test_app_stop(test_app);
if (rc != TEST_SUCCESS)
return rc;
TEST_ASSERT_EQUAL(stats.ev_drop_count, 1, "Drop count is not one");
TEST_ASSERT_EQUAL(stats.ev_dispatch_count, 0,
"Dispatch count is not zero");
TEST_ASSERT(stats.poll_count > 0, "Poll count is zero");
return TEST_SUCCESS;
}
#define MORE_THAN_MAX_HANDLERS 1000
#define MIN_HANDLERS 32
static int
test_many_handler_registrations(void)
{
int rc;
int num_regs = 0;
int reg_ids[MORE_THAN_MAX_HANDLERS];
int reg_id;
int i;
rc = test_app_unregister_callbacks(test_app);
if (rc != TEST_SUCCESS)
return rc;
for (i = 0; i < MORE_THAN_MAX_HANDLERS; i++) {
reg_id = rte_dispatcher_register(test_app->dispatcher,
never_match, NULL,
test_app_never_process, NULL);
if (reg_id < 0)
break;
reg_ids[num_regs++] = reg_id;
}
TEST_ASSERT_EQUAL(reg_id, -ENOMEM, "Incorrect return code. Expected "
"%d but was %d", -ENOMEM, reg_id);
TEST_ASSERT(num_regs >= MIN_HANDLERS, "Registration failed already "
"after %d handler registrations.", num_regs);
for (i = 0; i < num_regs; i++) {
rc = rte_dispatcher_unregister(test_app->dispatcher,
reg_ids[i]);
TEST_ASSERT_SUCCESS(rc, "Unable to unregister handler %d",
reg_ids[i]);
}
return TEST_SUCCESS;
}
static void
dummy_finalize(uint8_t event_dev_id __rte_unused,
uint8_t event_port_id __rte_unused,
void *cb_data __rte_unused)
{
}
#define MORE_THAN_MAX_FINALIZERS 1000
#define MIN_FINALIZERS 16
static int
test_many_finalize_registrations(void)
{
int rc;
int num_regs = 0;
int reg_ids[MORE_THAN_MAX_FINALIZERS];
int reg_id;
int i;
rc = test_app_unregister_callbacks(test_app);
if (rc != TEST_SUCCESS)
return rc;
for (i = 0; i < MORE_THAN_MAX_FINALIZERS; i++) {
reg_id = rte_dispatcher_finalize_register(
test_app->dispatcher, dummy_finalize, NULL
);
if (reg_id < 0)
break;
reg_ids[num_regs++] = reg_id;
}
TEST_ASSERT_EQUAL(reg_id, -ENOMEM, "Incorrect return code. Expected "
"%d but was %d", -ENOMEM, reg_id);
TEST_ASSERT(num_regs >= MIN_FINALIZERS, "Finalize registration failed "
"already after %d registrations.", num_regs);
for (i = 0; i < num_regs; i++) {
rc = rte_dispatcher_finalize_unregister(
test_app->dispatcher, reg_ids[i]
);
TEST_ASSERT_SUCCESS(rc, "Unable to unregister finalizer %d",
reg_ids[i]);
}
return TEST_SUCCESS;
}
static struct unit_test_suite test_suite = {
.suite_name = "Event dispatcher test suite",
.unit_test_cases = {
TEST_CASE_ST(test_setup, test_teardown, test_basic),
TEST_CASE_ST(test_setup, test_teardown, test_drop),
TEST_CASE_ST(test_setup, test_teardown,
test_many_handler_registrations),
TEST_CASE_ST(test_setup, test_teardown,
test_many_finalize_registrations),
TEST_CASES_END()
}
};
static int
test_dispatcher(void)
{
return unit_test_suite_runner(&test_suite);
}
REGISTER_FAST_TEST(dispatcher_autotest, false, true, test_dispatcher);
|