1 /* 2 * SPDX-License-Identifier: BSD-3-Clause 3 * Copyright 2016 Intel Corporation. 4 * Copyright 2017 Cavium, Inc. 5 */ 6 7 #include <stdlib.h> 8 9 #include "pipeline_common.h" 10 11 static __rte_always_inline int 12 worker_generic(void *arg) 13 { 14 struct rte_event ev; 15 16 struct worker_data *data = (struct worker_data *)arg; 17 uint8_t dev_id = data->dev_id; 18 uint8_t port_id = data->port_id; 19 size_t sent = 0, received = 0; 20 unsigned int lcore_id = rte_lcore_id(); 21 uint16_t nb_rx = 0, nb_tx = 0; 22 23 while (!fdata->done) { 24 25 if (fdata->cap.scheduler) 26 fdata->cap.scheduler(lcore_id); 27 28 if (!fdata->worker_core[lcore_id]) { 29 rte_pause(); 30 continue; 31 } 32 33 nb_rx = rte_event_dequeue_burst(dev_id, port_id, &ev, 1, 0); 34 35 if (nb_rx == 0) { 36 rte_pause(); 37 continue; 38 } 39 received++; 40 41 /* The first worker stage does classification and sets txq. */ 42 if (ev.queue_id == cdata.qid[0]) { 43 ev.flow_id = ev.mbuf->hash.rss 44 % cdata.num_fids; 45 rte_event_eth_tx_adapter_txq_set(ev.mbuf, 0); 46 } 47 48 ev.queue_id = cdata.next_qid[ev.queue_id]; 49 ev.op = RTE_EVENT_OP_FORWARD; 50 ev.sched_type = cdata.queue_type; 51 52 work(); 53 54 do { 55 nb_tx = rte_event_enqueue_burst(dev_id, port_id, &ev, 56 1); 57 } while (!nb_tx && !fdata->done); 58 sent++; 59 } 60 61 worker_cleanup(dev_id, port_id, &ev, nb_tx, nb_rx); 62 if (!cdata.quiet) 63 printf(" worker %u thread done. RX=%zu TX=%zu\n", 64 rte_lcore_id(), received, sent); 65 66 return 0; 67 } 68 69 static int 70 worker_generic_burst(void *arg) 71 { 72 struct rte_event events[BATCH_SIZE]; 73 74 struct worker_data *data = (struct worker_data *)arg; 75 uint8_t dev_id = data->dev_id; 76 uint8_t port_id = data->port_id; 77 size_t sent = 0, received = 0; 78 unsigned int lcore_id = rte_lcore_id(); 79 uint16_t i, nb_rx = 0, nb_tx = 0; 80 81 while (!fdata->done) { 82 if (fdata->cap.scheduler) 83 fdata->cap.scheduler(lcore_id); 84 85 if (!fdata->worker_core[lcore_id]) { 86 rte_pause(); 87 continue; 88 } 89 90 nb_rx = rte_event_dequeue_burst(dev_id, port_id, events, 91 RTE_DIM(events), 0); 92 93 if (nb_rx == 0) { 94 rte_pause(); 95 continue; 96 } 97 received += nb_rx; 98 99 for (i = 0; i < nb_rx; i++) { 100 101 /* The first worker stage does classification and sets txq. */ 102 if (events[i].queue_id == cdata.qid[0]) { 103 events[i].flow_id = events[i].mbuf->hash.rss 104 % cdata.num_fids; 105 rte_event_eth_tx_adapter_txq_set(events[i].mbuf, 0); 106 } 107 108 events[i].queue_id = cdata.next_qid[events[i].queue_id]; 109 events[i].op = RTE_EVENT_OP_FORWARD; 110 events[i].sched_type = cdata.queue_type; 111 112 work(); 113 } 114 nb_tx = rte_event_enqueue_burst(dev_id, port_id, events, nb_rx); 115 while (nb_tx < nb_rx && !fdata->done) 116 nb_tx += rte_event_enqueue_burst(dev_id, port_id, 117 events + nb_tx, 118 nb_rx - nb_tx); 119 sent += nb_tx; 120 } 121 122 worker_cleanup(dev_id, port_id, events, nb_tx, nb_rx); 123 124 if (!cdata.quiet) 125 printf(" worker %u thread done. RX=%zu TX=%zu\n", 126 rte_lcore_id(), received, sent); 127 128 return 0; 129 } 130 131 static int 132 setup_eventdev_generic(struct worker_data *worker_data) 133 { 134 const uint8_t dev_id = 0; 135 /* +1 stages is for a SINGLE_LINK TX stage */ 136 const uint8_t nb_queues = cdata.num_stages + 1; 137 const uint8_t nb_ports = cdata.num_workers; 138 struct rte_event_dev_config config = { 139 .nb_event_queues = nb_queues, 140 .nb_event_ports = nb_ports, 141 .nb_single_link_event_port_queues = 1, 142 .nb_events_limit = 4096, 143 .nb_event_queue_flows = 1024, 144 .nb_event_port_dequeue_depth = 128, 145 .nb_event_port_enqueue_depth = 128, 146 }; 147 struct rte_event_port_conf wkr_p_conf = { 148 .dequeue_depth = cdata.worker_cq_depth, 149 .enqueue_depth = 64, 150 .new_event_threshold = 4096, 151 .event_port_cfg = RTE_EVENT_PORT_CFG_HINT_WORKER, 152 }; 153 struct rte_event_queue_conf wkr_q_conf = { 154 .schedule_type = cdata.queue_type, 155 .priority = RTE_EVENT_DEV_PRIORITY_NORMAL, 156 .nb_atomic_flows = 1024, 157 .nb_atomic_order_sequences = 1024, 158 }; 159 struct rte_event_queue_conf tx_q_conf = { 160 .priority = RTE_EVENT_DEV_PRIORITY_HIGHEST, 161 .event_queue_cfg = RTE_EVENT_QUEUE_CFG_SINGLE_LINK, 162 }; 163 164 struct port_link worker_queues[MAX_NUM_STAGES]; 165 uint8_t disable_implicit_release; 166 unsigned int i; 167 168 int ret, ndev = rte_event_dev_count(); 169 if (ndev < 1) { 170 printf("%d: No Eventdev Devices Found\n", __LINE__); 171 return -1; 172 } 173 174 struct rte_event_dev_info dev_info; 175 ret = rte_event_dev_info_get(dev_id, &dev_info); 176 printf("\tEventdev %d: %s\n", dev_id, dev_info.driver_name); 177 178 disable_implicit_release = (dev_info.event_dev_cap & 179 RTE_EVENT_DEV_CAP_IMPLICIT_RELEASE_DISABLE); 180 181 wkr_p_conf.event_port_cfg = disable_implicit_release ? 182 RTE_EVENT_PORT_CFG_DISABLE_IMPL_REL : 0; 183 184 if (dev_info.max_num_events < config.nb_events_limit) 185 config.nb_events_limit = dev_info.max_num_events; 186 if (dev_info.max_event_port_dequeue_depth < 187 config.nb_event_port_dequeue_depth) 188 config.nb_event_port_dequeue_depth = 189 dev_info.max_event_port_dequeue_depth; 190 if (dev_info.max_event_port_enqueue_depth < 191 config.nb_event_port_enqueue_depth) 192 config.nb_event_port_enqueue_depth = 193 dev_info.max_event_port_enqueue_depth; 194 195 if (dev_info.event_dev_cap & RTE_EVENT_DEV_CAP_EVENT_PRESCHEDULE) 196 config.preschedule_type = RTE_EVENT_PRESCHEDULE; 197 198 if (dev_info.event_dev_cap & RTE_EVENT_DEV_CAP_EVENT_PRESCHEDULE_ADAPTIVE) 199 config.preschedule_type = RTE_EVENT_PRESCHEDULE_ADAPTIVE; 200 201 ret = rte_event_dev_configure(dev_id, &config); 202 if (ret < 0) { 203 printf("%d: Error configuring device\n", __LINE__); 204 return -1; 205 } 206 207 /* Q creation - one load balanced per pipeline stage*/ 208 printf(" Stages:\n"); 209 for (i = 0; i < cdata.num_stages; i++) { 210 if (rte_event_queue_setup(dev_id, i, &wkr_q_conf) < 0) { 211 printf("%d: error creating qid %d\n", __LINE__, i); 212 return -1; 213 } 214 cdata.qid[i] = i; 215 cdata.next_qid[i] = i+1; 216 worker_queues[i].queue_id = i; 217 if (cdata.enable_queue_priorities) { 218 /* calculate priority stepping for each stage, leaving 219 * headroom of 1 for the SINGLE_LINK TX below 220 */ 221 const uint32_t prio_delta = 222 (RTE_EVENT_DEV_PRIORITY_LOWEST-1) / nb_queues; 223 224 /* higher priority for queues closer to tx */ 225 wkr_q_conf.priority = 226 RTE_EVENT_DEV_PRIORITY_LOWEST - prio_delta * i; 227 } 228 229 const char *type_str = "Atomic"; 230 switch (wkr_q_conf.schedule_type) { 231 case RTE_SCHED_TYPE_ORDERED: 232 type_str = "Ordered"; 233 break; 234 case RTE_SCHED_TYPE_PARALLEL: 235 type_str = "Parallel"; 236 break; 237 } 238 printf("\tStage %d, Type %s\tPriority = %d\n", i, type_str, 239 wkr_q_conf.priority); 240 } 241 printf("\n"); 242 243 /* final queue for sending to TX core */ 244 if (rte_event_queue_setup(dev_id, i, &tx_q_conf) < 0) { 245 printf("%d: error creating qid %d\n", __LINE__, i); 246 return -1; 247 } 248 cdata.tx_queue_id = i; 249 250 if (wkr_p_conf.new_event_threshold > config.nb_events_limit) 251 wkr_p_conf.new_event_threshold = config.nb_events_limit; 252 if (wkr_p_conf.dequeue_depth > config.nb_event_port_dequeue_depth) 253 wkr_p_conf.dequeue_depth = config.nb_event_port_dequeue_depth; 254 if (wkr_p_conf.enqueue_depth > config.nb_event_port_enqueue_depth) 255 wkr_p_conf.enqueue_depth = config.nb_event_port_enqueue_depth; 256 257 /* set up one port per worker, linking to all stage queues */ 258 for (i = 0; i < cdata.num_workers; i++) { 259 struct worker_data *w = &worker_data[i]; 260 w->dev_id = dev_id; 261 if (rte_event_port_setup(dev_id, i, &wkr_p_conf) < 0) { 262 printf("Error setting up port %d\n", i); 263 return -1; 264 } 265 266 uint32_t s; 267 for (s = 0; s < cdata.num_stages; s++) { 268 if (rte_event_port_link(dev_id, i, 269 &worker_queues[s].queue_id, 270 &worker_queues[s].priority, 271 1) != 1) { 272 printf("%d: error creating link for port %d\n", 273 __LINE__, i); 274 return -1; 275 } 276 } 277 w->port_id = i; 278 } 279 280 ret = rte_event_dev_service_id_get(dev_id, 281 &fdata->evdev_service_id); 282 if (ret != -ESRCH && ret != 0) { 283 printf("Error getting the service ID for sw eventdev\n"); 284 return -1; 285 } 286 rte_service_runstate_set(fdata->evdev_service_id, 1); 287 rte_service_set_runstate_mapped_check(fdata->evdev_service_id, 0); 288 289 return dev_id; 290 } 291 292 /* 293 * Initializes a given port using global settings and with the RX buffers 294 * coming from the mbuf_pool passed as a parameter. 295 */ 296 static inline int 297 port_init(uint8_t port, struct rte_mempool *mbuf_pool) 298 { 299 struct rte_eth_rxconf rx_conf; 300 static const struct rte_eth_conf port_conf_default = { 301 .rxmode = { 302 .mq_mode = RTE_ETH_MQ_RX_RSS, 303 }, 304 .rx_adv_conf = { 305 .rss_conf = { 306 .rss_hf = RTE_ETH_RSS_IP | 307 RTE_ETH_RSS_TCP | 308 RTE_ETH_RSS_UDP, 309 } 310 } 311 }; 312 const uint16_t rx_rings = 1, tx_rings = 1; 313 const uint16_t rx_ring_size = 512, tx_ring_size = 512; 314 struct rte_eth_conf port_conf = port_conf_default; 315 int retval; 316 uint16_t q; 317 struct rte_eth_dev_info dev_info; 318 struct rte_eth_txconf txconf; 319 320 if (!rte_eth_dev_is_valid_port(port)) 321 return -1; 322 323 retval = rte_eth_dev_info_get(port, &dev_info); 324 if (retval != 0) { 325 printf("Error during getting device (port %u) info: %s\n", 326 port, strerror(-retval)); 327 return retval; 328 } 329 330 if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE) 331 port_conf.txmode.offloads |= 332 RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE; 333 334 if (dev_info.rx_offload_capa & RTE_ETH_RX_OFFLOAD_RSS_HASH) 335 port_conf.rxmode.offloads |= RTE_ETH_RX_OFFLOAD_RSS_HASH; 336 337 rx_conf = dev_info.default_rxconf; 338 rx_conf.offloads = port_conf.rxmode.offloads; 339 340 port_conf.rx_adv_conf.rss_conf.rss_hf &= 341 dev_info.flow_type_rss_offloads; 342 if (port_conf.rx_adv_conf.rss_conf.rss_hf != 343 port_conf_default.rx_adv_conf.rss_conf.rss_hf) { 344 printf("Port %u modified RSS hash function based on hardware support," 345 "requested:%#"PRIx64" configured:%#"PRIx64"\n", 346 port, 347 port_conf_default.rx_adv_conf.rss_conf.rss_hf, 348 port_conf.rx_adv_conf.rss_conf.rss_hf); 349 } 350 351 /* Configure the Ethernet device. */ 352 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf); 353 if (retval != 0) 354 return retval; 355 356 /* Allocate and set up 1 RX queue per Ethernet port. */ 357 for (q = 0; q < rx_rings; q++) { 358 retval = rte_eth_rx_queue_setup(port, q, rx_ring_size, 359 rte_eth_dev_socket_id(port), &rx_conf, 360 mbuf_pool); 361 if (retval < 0) 362 return retval; 363 } 364 365 txconf = dev_info.default_txconf; 366 txconf.offloads = port_conf_default.txmode.offloads; 367 /* Allocate and set up 1 TX queue per Ethernet port. */ 368 for (q = 0; q < tx_rings; q++) { 369 retval = rte_eth_tx_queue_setup(port, q, tx_ring_size, 370 rte_eth_dev_socket_id(port), &txconf); 371 if (retval < 0) 372 return retval; 373 } 374 375 /* Display the port MAC address. */ 376 struct rte_ether_addr addr; 377 retval = rte_eth_macaddr_get(port, &addr); 378 if (retval != 0) { 379 printf("Failed to get MAC address (port %u): %s\n", 380 port, rte_strerror(-retval)); 381 return retval; 382 } 383 384 printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8 385 " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n", 386 (unsigned int)port, RTE_ETHER_ADDR_BYTES(&addr)); 387 388 /* Enable RX in promiscuous mode for the Ethernet device. */ 389 retval = rte_eth_promiscuous_enable(port); 390 if (retval != 0) 391 return retval; 392 393 return 0; 394 } 395 396 static int 397 init_ports(uint16_t num_ports) 398 { 399 uint16_t portid; 400 401 if (!cdata.num_mbuf) 402 cdata.num_mbuf = 16384 * num_ports; 403 404 struct rte_mempool *mp = rte_pktmbuf_pool_create("packet_pool", 405 /* mbufs */ cdata.num_mbuf, 406 /* cache_size */ 512, 407 /* priv_size*/ 0, 408 /* data_room_size */ RTE_MBUF_DEFAULT_BUF_SIZE, 409 rte_socket_id()); 410 411 RTE_ETH_FOREACH_DEV(portid) 412 if (port_init(portid, mp) != 0) 413 rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu16 "\n", 414 portid); 415 416 return 0; 417 } 418 419 static void 420 init_adapters(uint16_t nb_ports) 421 { 422 int i; 423 int ret; 424 uint8_t tx_port_id = 0; 425 uint8_t evdev_id = 0; 426 struct rte_event_dev_info dev_info; 427 428 ret = rte_event_dev_info_get(evdev_id, &dev_info); 429 430 struct rte_event_port_conf adptr_p_conf = { 431 .dequeue_depth = cdata.worker_cq_depth, 432 .enqueue_depth = 64, 433 .new_event_threshold = 4096, 434 .event_port_cfg = RTE_EVENT_PORT_CFG_HINT_PRODUCER, 435 }; 436 437 if (adptr_p_conf.new_event_threshold > dev_info.max_num_events) 438 adptr_p_conf.new_event_threshold = dev_info.max_num_events; 439 if (adptr_p_conf.dequeue_depth > dev_info.max_event_port_dequeue_depth) 440 adptr_p_conf.dequeue_depth = 441 dev_info.max_event_port_dequeue_depth; 442 if (adptr_p_conf.enqueue_depth > dev_info.max_event_port_enqueue_depth) 443 adptr_p_conf.enqueue_depth = 444 dev_info.max_event_port_enqueue_depth; 445 446 init_ports(nb_ports); 447 /* Create one adapter for all the ethernet ports. */ 448 ret = rte_event_eth_rx_adapter_create(cdata.rx_adapter_id, evdev_id, 449 &adptr_p_conf); 450 if (ret) 451 rte_exit(EXIT_FAILURE, "failed to create rx adapter[%d]", 452 cdata.rx_adapter_id); 453 454 ret = rte_event_eth_tx_adapter_create(cdata.tx_adapter_id, evdev_id, 455 &adptr_p_conf); 456 if (ret) 457 rte_exit(EXIT_FAILURE, "failed to create tx adapter[%d]", 458 cdata.tx_adapter_id); 459 460 struct rte_event_eth_rx_adapter_queue_conf queue_conf; 461 memset(&queue_conf, 0, sizeof(queue_conf)); 462 queue_conf.ev.sched_type = cdata.queue_type; 463 queue_conf.ev.queue_id = cdata.qid[0]; 464 465 for (i = 0; i < nb_ports; i++) { 466 ret = rte_event_eth_rx_adapter_queue_add(cdata.rx_adapter_id, i, 467 -1, &queue_conf); 468 if (ret) 469 rte_exit(EXIT_FAILURE, 470 "Failed to add queues to Rx adapter"); 471 472 ret = rte_event_eth_tx_adapter_queue_add(cdata.tx_adapter_id, i, 473 -1); 474 if (ret) 475 rte_exit(EXIT_FAILURE, 476 "Failed to add queues to Tx adapter"); 477 } 478 479 ret = rte_event_eth_tx_adapter_event_port_get(cdata.tx_adapter_id, 480 &tx_port_id); 481 if (ret) 482 rte_exit(EXIT_FAILURE, 483 "Failed to get Tx adapter port id"); 484 ret = rte_event_port_link(evdev_id, tx_port_id, &cdata.tx_queue_id, 485 NULL, 1); 486 if (ret != 1) 487 rte_exit(EXIT_FAILURE, 488 "Unable to link Tx adapter port to Tx queue"); 489 490 ret = rte_event_eth_rx_adapter_service_id_get(cdata.rx_adapter_id, 491 &fdata->rxadptr_service_id); 492 if (ret != -ESRCH && ret != 0) { 493 rte_exit(EXIT_FAILURE, 494 "Error getting the service ID for Rx adapter\n"); 495 } 496 rte_service_runstate_set(fdata->rxadptr_service_id, 1); 497 rte_service_set_runstate_mapped_check(fdata->rxadptr_service_id, 0); 498 499 ret = rte_event_eth_tx_adapter_service_id_get(cdata.tx_adapter_id, 500 &fdata->txadptr_service_id); 501 if (ret != -ESRCH && ret != 0) { 502 rte_exit(EXIT_FAILURE, 503 "Error getting the service ID for Tx adapter\n"); 504 } 505 rte_service_runstate_set(fdata->txadptr_service_id, 1); 506 rte_service_set_runstate_mapped_check(fdata->txadptr_service_id, 0); 507 508 ret = rte_event_eth_rx_adapter_start(cdata.rx_adapter_id); 509 if (ret) 510 rte_exit(EXIT_FAILURE, "Rx adapter[%d] start failed", 511 cdata.rx_adapter_id); 512 513 ret = rte_event_eth_tx_adapter_start(cdata.tx_adapter_id); 514 if (ret) 515 rte_exit(EXIT_FAILURE, "Tx adapter[%d] start failed", 516 cdata.tx_adapter_id); 517 518 if (rte_event_dev_start(evdev_id) < 0) 519 rte_exit(EXIT_FAILURE, "Error starting eventdev"); 520 } 521 522 static void 523 generic_opt_check(void) 524 { 525 int i; 526 int ret; 527 uint32_t cap = 0; 528 uint8_t rx_needed = 0; 529 uint8_t sched_needed = 0; 530 struct rte_event_dev_info eventdev_info; 531 532 memset(&eventdev_info, 0, sizeof(struct rte_event_dev_info)); 533 rte_event_dev_info_get(0, &eventdev_info); 534 535 if (cdata.all_type_queues && !(eventdev_info.event_dev_cap & 536 RTE_EVENT_DEV_CAP_QUEUE_ALL_TYPES)) 537 rte_exit(EXIT_FAILURE, 538 "Event dev doesn't support all type queues\n"); 539 sched_needed = !(eventdev_info.event_dev_cap & 540 RTE_EVENT_DEV_CAP_DISTRIBUTED_SCHED); 541 542 RTE_ETH_FOREACH_DEV(i) { 543 ret = rte_event_eth_rx_adapter_caps_get(0, i, &cap); 544 if (ret) 545 rte_exit(EXIT_FAILURE, 546 "failed to get event rx adapter capabilities"); 547 rx_needed |= 548 !(cap & RTE_EVENT_ETH_RX_ADAPTER_CAP_INTERNAL_PORT); 549 } 550 551 if (cdata.worker_lcore_mask == 0 || 552 (rx_needed && cdata.rx_lcore_mask == 0) || 553 (cdata.tx_lcore_mask == 0) || 554 (sched_needed && cdata.sched_lcore_mask == 0)) { 555 printf("Core part of pipeline was not assigned any cores. " 556 "This will stall the pipeline, please check core masks " 557 "(use -h for details on setting core masks):\n" 558 "\trx: %"PRIu64"\n\ttx: %"PRIu64"\n\tsched: %"PRIu64 559 "\n\tworkers: %"PRIu64"\n", 560 cdata.rx_lcore_mask, cdata.tx_lcore_mask, 561 cdata.sched_lcore_mask, 562 cdata.worker_lcore_mask); 563 rte_exit(-1, "Fix core masks\n"); 564 } 565 566 if (!sched_needed) 567 memset(fdata->sched_core, 0, 568 sizeof(unsigned int) * MAX_NUM_CORE); 569 if (!rx_needed) 570 memset(fdata->rx_core, 0, 571 sizeof(unsigned int) * MAX_NUM_CORE); 572 } 573 574 void 575 set_worker_generic_setup_data(struct setup_data *caps, bool burst) 576 { 577 if (burst) { 578 caps->worker = worker_generic_burst; 579 } else { 580 caps->worker = worker_generic; 581 } 582 583 caps->adptr_setup = init_adapters; 584 caps->scheduler = schedule_devices; 585 caps->evdev_setup = setup_eventdev_generic; 586 caps->check_opt = generic_opt_check; 587 } 588