1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2015-2016 Intel Corporation 3 */ 4 5 #include <time.h> 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <string.h> 9 #include <stdint.h> 10 #include <inttypes.h> 11 #include <sys/types.h> 12 #include <sys/queue.h> 13 #include <netinet/in.h> 14 #include <setjmp.h> 15 #include <stdarg.h> 16 #include <ctype.h> 17 #include <errno.h> 18 #include <getopt.h> 19 #include <fcntl.h> 20 #include <unistd.h> 21 22 #include <rte_string_fns.h> 23 #include <rte_atomic.h> 24 #include <rte_branch_prediction.h> 25 #include <rte_common.h> 26 #include <rte_cryptodev.h> 27 #include <rte_cycles.h> 28 #include <rte_debug.h> 29 #include <rte_eal.h> 30 #include <rte_ether.h> 31 #include <rte_ethdev.h> 32 #include <rte_interrupts.h> 33 #include <rte_ip.h> 34 #include <rte_launch.h> 35 #include <rte_lcore.h> 36 #include <rte_log.h> 37 #include <rte_malloc.h> 38 #include <rte_mbuf.h> 39 #include <rte_memcpy.h> 40 #include <rte_memory.h> 41 #include <rte_mempool.h> 42 #include <rte_per_lcore.h> 43 #include <rte_prefetch.h> 44 #include <rte_random.h> 45 #include <rte_hexdump.h> 46 #ifdef RTE_CRYPTO_SCHEDULER 47 #include <rte_cryptodev_scheduler.h> 48 #endif 49 50 enum cdev_type { 51 CDEV_TYPE_ANY, 52 CDEV_TYPE_HW, 53 CDEV_TYPE_SW 54 }; 55 56 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1 57 58 #define NB_MBUF 8192 59 60 #define MAX_STR_LEN 32 61 #define MAX_KEY_SIZE 128 62 #define MAX_IV_SIZE 16 63 #define MAX_AAD_SIZE 65535 64 #define MAX_PKT_BURST 32 65 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */ 66 #define SESSION_POOL_CACHE_SIZE 0 67 68 #define MAXIMUM_IV_LENGTH 16 69 #define IV_OFFSET (sizeof(struct rte_crypto_op) + \ 70 sizeof(struct rte_crypto_sym_op)) 71 72 /* 73 * Configurable number of RX/TX ring descriptors 74 */ 75 #define RTE_TEST_RX_DESC_DEFAULT 1024 76 #define RTE_TEST_TX_DESC_DEFAULT 1024 77 78 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT; 79 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT; 80 81 /* ethernet addresses of ports */ 82 static struct rte_ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS]; 83 84 /* mask of enabled ports */ 85 static uint64_t l2fwd_enabled_port_mask; 86 static uint64_t l2fwd_enabled_crypto_mask; 87 88 /* list of enabled ports */ 89 static uint16_t l2fwd_dst_ports[RTE_MAX_ETHPORTS]; 90 91 92 struct pkt_buffer { 93 unsigned len; 94 struct rte_mbuf *buffer[MAX_PKT_BURST]; 95 }; 96 97 struct op_buffer { 98 unsigned len; 99 struct rte_crypto_op *buffer[MAX_PKT_BURST]; 100 }; 101 102 #define MAX_RX_QUEUE_PER_LCORE 16 103 #define MAX_TX_QUEUE_PER_PORT 16 104 105 enum l2fwd_crypto_xform_chain { 106 L2FWD_CRYPTO_CIPHER_HASH, 107 L2FWD_CRYPTO_HASH_CIPHER, 108 L2FWD_CRYPTO_CIPHER_ONLY, 109 L2FWD_CRYPTO_HASH_ONLY, 110 L2FWD_CRYPTO_AEAD 111 }; 112 113 struct l2fwd_key { 114 uint8_t *data; 115 uint32_t length; 116 rte_iova_t phys_addr; 117 }; 118 119 struct l2fwd_iv { 120 uint8_t *data; 121 uint16_t length; 122 }; 123 124 /** l2fwd crypto application command line options */ 125 struct l2fwd_crypto_options { 126 unsigned portmask; 127 unsigned nb_ports_per_lcore; 128 unsigned refresh_period; 129 unsigned single_lcore:1; 130 131 enum cdev_type type; 132 unsigned sessionless:1; 133 134 enum l2fwd_crypto_xform_chain xform_chain; 135 136 struct rte_crypto_sym_xform cipher_xform; 137 unsigned ckey_param; 138 int ckey_random_size; 139 uint8_t cipher_key[MAX_KEY_SIZE]; 140 141 struct l2fwd_iv cipher_iv; 142 unsigned int cipher_iv_param; 143 int cipher_iv_random_size; 144 145 struct rte_crypto_sym_xform auth_xform; 146 uint8_t akey_param; 147 int akey_random_size; 148 uint8_t auth_key[MAX_KEY_SIZE]; 149 150 struct l2fwd_iv auth_iv; 151 unsigned int auth_iv_param; 152 int auth_iv_random_size; 153 154 struct rte_crypto_sym_xform aead_xform; 155 unsigned int aead_key_param; 156 int aead_key_random_size; 157 uint8_t aead_key[MAX_KEY_SIZE]; 158 159 struct l2fwd_iv aead_iv; 160 unsigned int aead_iv_param; 161 int aead_iv_random_size; 162 163 struct l2fwd_key aad; 164 unsigned aad_param; 165 int aad_random_size; 166 167 int digest_size; 168 169 uint16_t block_size; 170 char string_type[MAX_STR_LEN]; 171 172 uint64_t cryptodev_mask; 173 174 unsigned int mac_updating; 175 }; 176 177 /** l2fwd crypto lcore params */ 178 struct l2fwd_crypto_params { 179 uint8_t dev_id; 180 uint8_t qp_id; 181 182 unsigned digest_length; 183 unsigned block_size; 184 185 struct l2fwd_iv cipher_iv; 186 struct l2fwd_iv auth_iv; 187 struct l2fwd_iv aead_iv; 188 struct l2fwd_key aad; 189 struct rte_cryptodev_sym_session *session; 190 191 uint8_t do_cipher; 192 uint8_t do_hash; 193 uint8_t do_aead; 194 uint8_t hash_verify; 195 196 enum rte_crypto_cipher_algorithm cipher_algo; 197 enum rte_crypto_auth_algorithm auth_algo; 198 enum rte_crypto_aead_algorithm aead_algo; 199 }; 200 201 /** lcore configuration */ 202 struct lcore_queue_conf { 203 unsigned nb_rx_ports; 204 uint16_t rx_port_list[MAX_RX_QUEUE_PER_LCORE]; 205 206 unsigned nb_crypto_devs; 207 unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE]; 208 209 struct op_buffer op_buf[RTE_CRYPTO_MAX_DEVS]; 210 struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS]; 211 } __rte_cache_aligned; 212 213 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE]; 214 215 static struct rte_eth_conf port_conf = { 216 .rxmode = { 217 .mq_mode = ETH_MQ_RX_NONE, 218 .max_rx_pkt_len = RTE_ETHER_MAX_LEN, 219 .split_hdr_size = 0, 220 }, 221 .txmode = { 222 .mq_mode = ETH_MQ_TX_NONE, 223 }, 224 }; 225 226 struct rte_mempool *l2fwd_pktmbuf_pool; 227 struct rte_mempool *l2fwd_crypto_op_pool; 228 static struct { 229 struct rte_mempool *sess_mp; 230 struct rte_mempool *priv_mp; 231 } session_pool_socket[RTE_MAX_NUMA_NODES]; 232 233 /* Per-port statistics struct */ 234 struct l2fwd_port_statistics { 235 uint64_t tx; 236 uint64_t rx; 237 238 uint64_t crypto_enqueued; 239 uint64_t crypto_dequeued; 240 241 uint64_t dropped; 242 } __rte_cache_aligned; 243 244 struct l2fwd_crypto_statistics { 245 uint64_t enqueued; 246 uint64_t dequeued; 247 248 uint64_t errors; 249 } __rte_cache_aligned; 250 251 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS]; 252 struct l2fwd_crypto_statistics crypto_statistics[RTE_CRYPTO_MAX_DEVS]; 253 254 /* A tsc-based timer responsible for triggering statistics printout */ 255 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */ 256 #define MAX_TIMER_PERIOD 86400UL /* 1 day max */ 257 258 /* default period is 10 seconds */ 259 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000; 260 261 /* Print out statistics on packets dropped */ 262 static void 263 print_stats(void) 264 { 265 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx; 266 uint64_t total_packets_enqueued, total_packets_dequeued, 267 total_packets_errors; 268 uint16_t portid; 269 uint64_t cdevid; 270 271 total_packets_dropped = 0; 272 total_packets_tx = 0; 273 total_packets_rx = 0; 274 total_packets_enqueued = 0; 275 total_packets_dequeued = 0; 276 total_packets_errors = 0; 277 278 const char clr[] = { 27, '[', '2', 'J', '\0' }; 279 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' }; 280 281 /* Clear screen and move to top left */ 282 printf("%s%s", clr, topLeft); 283 284 printf("\nPort statistics ===================================="); 285 286 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) { 287 /* skip disabled ports */ 288 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) 289 continue; 290 printf("\nStatistics for port %u ------------------------------" 291 "\nPackets sent: %32"PRIu64 292 "\nPackets received: %28"PRIu64 293 "\nPackets dropped: %29"PRIu64, 294 portid, 295 port_statistics[portid].tx, 296 port_statistics[portid].rx, 297 port_statistics[portid].dropped); 298 299 total_packets_dropped += port_statistics[portid].dropped; 300 total_packets_tx += port_statistics[portid].tx; 301 total_packets_rx += port_statistics[portid].rx; 302 } 303 printf("\nCrypto statistics =================================="); 304 305 for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) { 306 /* skip disabled ports */ 307 if ((l2fwd_enabled_crypto_mask & (((uint64_t)1) << cdevid)) == 0) 308 continue; 309 printf("\nStatistics for cryptodev %"PRIu64 310 " -------------------------" 311 "\nPackets enqueued: %28"PRIu64 312 "\nPackets dequeued: %28"PRIu64 313 "\nPackets errors: %30"PRIu64, 314 cdevid, 315 crypto_statistics[cdevid].enqueued, 316 crypto_statistics[cdevid].dequeued, 317 crypto_statistics[cdevid].errors); 318 319 total_packets_enqueued += crypto_statistics[cdevid].enqueued; 320 total_packets_dequeued += crypto_statistics[cdevid].dequeued; 321 total_packets_errors += crypto_statistics[cdevid].errors; 322 } 323 printf("\nAggregate statistics ===============================" 324 "\nTotal packets received: %22"PRIu64 325 "\nTotal packets enqueued: %22"PRIu64 326 "\nTotal packets dequeued: %22"PRIu64 327 "\nTotal packets sent: %26"PRIu64 328 "\nTotal packets dropped: %23"PRIu64 329 "\nTotal packets crypto errors: %17"PRIu64, 330 total_packets_rx, 331 total_packets_enqueued, 332 total_packets_dequeued, 333 total_packets_tx, 334 total_packets_dropped, 335 total_packets_errors); 336 printf("\n====================================================\n"); 337 338 fflush(stdout); 339 } 340 341 static int 342 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n, 343 struct l2fwd_crypto_params *cparams) 344 { 345 struct rte_crypto_op **op_buffer; 346 unsigned ret; 347 348 op_buffer = (struct rte_crypto_op **) 349 qconf->op_buf[cparams->dev_id].buffer; 350 351 ret = rte_cryptodev_enqueue_burst(cparams->dev_id, 352 cparams->qp_id, op_buffer, (uint16_t) n); 353 354 crypto_statistics[cparams->dev_id].enqueued += ret; 355 if (unlikely(ret < n)) { 356 crypto_statistics[cparams->dev_id].errors += (n - ret); 357 do { 358 rte_pktmbuf_free(op_buffer[ret]->sym->m_src); 359 rte_crypto_op_free(op_buffer[ret]); 360 } while (++ret < n); 361 } 362 363 return 0; 364 } 365 366 static int 367 l2fwd_crypto_enqueue(struct rte_crypto_op *op, 368 struct l2fwd_crypto_params *cparams) 369 { 370 unsigned lcore_id, len; 371 struct lcore_queue_conf *qconf; 372 373 lcore_id = rte_lcore_id(); 374 375 qconf = &lcore_queue_conf[lcore_id]; 376 len = qconf->op_buf[cparams->dev_id].len; 377 qconf->op_buf[cparams->dev_id].buffer[len] = op; 378 len++; 379 380 /* enough ops to be sent */ 381 if (len == MAX_PKT_BURST) { 382 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams); 383 len = 0; 384 } 385 386 qconf->op_buf[cparams->dev_id].len = len; 387 return 0; 388 } 389 390 static int 391 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m, 392 struct rte_crypto_op *op, 393 struct l2fwd_crypto_params *cparams) 394 { 395 struct rte_ether_hdr *eth_hdr; 396 struct rte_ipv4_hdr *ip_hdr; 397 398 uint32_t ipdata_offset, data_len; 399 uint32_t pad_len = 0; 400 char *padding; 401 402 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *); 403 404 if (eth_hdr->ether_type != rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) 405 return -1; 406 407 ipdata_offset = sizeof(struct rte_ether_hdr); 408 409 ip_hdr = (struct rte_ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) + 410 ipdata_offset); 411 412 ipdata_offset += (ip_hdr->version_ihl & RTE_IPV4_HDR_IHL_MASK) 413 * RTE_IPV4_IHL_MULTIPLIER; 414 415 416 /* Zero pad data to be crypto'd so it is block aligned */ 417 data_len = rte_pktmbuf_data_len(m) - ipdata_offset; 418 419 if ((cparams->do_hash || cparams->do_aead) && cparams->hash_verify) 420 data_len -= cparams->digest_length; 421 422 if (cparams->do_cipher) { 423 /* 424 * Following algorithms are block cipher algorithms, 425 * and might need padding 426 */ 427 switch (cparams->cipher_algo) { 428 case RTE_CRYPTO_CIPHER_AES_CBC: 429 case RTE_CRYPTO_CIPHER_AES_ECB: 430 case RTE_CRYPTO_CIPHER_DES_CBC: 431 case RTE_CRYPTO_CIPHER_3DES_CBC: 432 case RTE_CRYPTO_CIPHER_3DES_ECB: 433 if (data_len % cparams->block_size) 434 pad_len = cparams->block_size - 435 (data_len % cparams->block_size); 436 break; 437 default: 438 pad_len = 0; 439 } 440 441 if (pad_len) { 442 padding = rte_pktmbuf_append(m, pad_len); 443 if (unlikely(!padding)) 444 return -1; 445 446 data_len += pad_len; 447 memset(padding, 0, pad_len); 448 } 449 } 450 451 /* Set crypto operation data parameters */ 452 rte_crypto_op_attach_sym_session(op, cparams->session); 453 454 if (cparams->do_hash) { 455 if (cparams->auth_iv.length) { 456 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, 457 uint8_t *, 458 IV_OFFSET + 459 cparams->cipher_iv.length); 460 /* 461 * Copy IV at the end of the crypto operation, 462 * after the cipher IV, if added 463 */ 464 rte_memcpy(iv_ptr, cparams->auth_iv.data, 465 cparams->auth_iv.length); 466 } 467 if (!cparams->hash_verify) { 468 /* Append space for digest to end of packet */ 469 op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m, 470 cparams->digest_length); 471 } else { 472 op->sym->auth.digest.data = rte_pktmbuf_mtod(m, 473 uint8_t *) + ipdata_offset + data_len; 474 } 475 476 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m, 477 rte_pktmbuf_pkt_len(m) - cparams->digest_length); 478 479 /* For wireless algorithms, offset/length must be in bits */ 480 if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2 || 481 cparams->auth_algo == RTE_CRYPTO_AUTH_KASUMI_F9 || 482 cparams->auth_algo == RTE_CRYPTO_AUTH_ZUC_EIA3) { 483 op->sym->auth.data.offset = ipdata_offset << 3; 484 op->sym->auth.data.length = data_len << 3; 485 } else { 486 op->sym->auth.data.offset = ipdata_offset; 487 op->sym->auth.data.length = data_len; 488 } 489 } 490 491 if (cparams->do_cipher) { 492 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *, 493 IV_OFFSET); 494 /* Copy IV at the end of the crypto operation */ 495 rte_memcpy(iv_ptr, cparams->cipher_iv.data, 496 cparams->cipher_iv.length); 497 498 /* For wireless algorithms, offset/length must be in bits */ 499 if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 || 500 cparams->cipher_algo == RTE_CRYPTO_CIPHER_KASUMI_F8 || 501 cparams->cipher_algo == RTE_CRYPTO_CIPHER_ZUC_EEA3) { 502 op->sym->cipher.data.offset = ipdata_offset << 3; 503 op->sym->cipher.data.length = data_len << 3; 504 } else { 505 op->sym->cipher.data.offset = ipdata_offset; 506 op->sym->cipher.data.length = data_len; 507 } 508 } 509 510 if (cparams->do_aead) { 511 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *, 512 IV_OFFSET); 513 /* Copy IV at the end of the crypto operation */ 514 /* 515 * If doing AES-CCM, nonce is copied one byte 516 * after the start of IV field 517 */ 518 if (cparams->aead_algo == RTE_CRYPTO_AEAD_AES_CCM) 519 rte_memcpy(iv_ptr + 1, cparams->aead_iv.data, 520 cparams->aead_iv.length); 521 else 522 rte_memcpy(iv_ptr, cparams->aead_iv.data, 523 cparams->aead_iv.length); 524 525 op->sym->aead.data.offset = ipdata_offset; 526 op->sym->aead.data.length = data_len; 527 528 if (!cparams->hash_verify) { 529 /* Append space for digest to end of packet */ 530 op->sym->aead.digest.data = (uint8_t *)rte_pktmbuf_append(m, 531 cparams->digest_length); 532 } else { 533 op->sym->aead.digest.data = rte_pktmbuf_mtod(m, 534 uint8_t *) + ipdata_offset + data_len; 535 } 536 537 op->sym->aead.digest.phys_addr = rte_pktmbuf_iova_offset(m, 538 rte_pktmbuf_pkt_len(m) - cparams->digest_length); 539 540 if (cparams->aad.length) { 541 op->sym->aead.aad.data = cparams->aad.data; 542 op->sym->aead.aad.phys_addr = cparams->aad.phys_addr; 543 } 544 } 545 546 op->sym->m_src = m; 547 548 return l2fwd_crypto_enqueue(op, cparams); 549 } 550 551 552 /* Send the burst of packets on an output interface */ 553 static int 554 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n, 555 uint16_t port) 556 { 557 struct rte_mbuf **pkt_buffer; 558 unsigned ret; 559 560 pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer; 561 562 ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n); 563 port_statistics[port].tx += ret; 564 if (unlikely(ret < n)) { 565 port_statistics[port].dropped += (n - ret); 566 do { 567 rte_pktmbuf_free(pkt_buffer[ret]); 568 } while (++ret < n); 569 } 570 571 return 0; 572 } 573 574 /* Enqueue packets for TX and prepare them to be sent */ 575 static int 576 l2fwd_send_packet(struct rte_mbuf *m, uint16_t port) 577 { 578 unsigned lcore_id, len; 579 struct lcore_queue_conf *qconf; 580 581 lcore_id = rte_lcore_id(); 582 583 qconf = &lcore_queue_conf[lcore_id]; 584 len = qconf->pkt_buf[port].len; 585 qconf->pkt_buf[port].buffer[len] = m; 586 len++; 587 588 /* enough pkts to be sent */ 589 if (unlikely(len == MAX_PKT_BURST)) { 590 l2fwd_send_burst(qconf, MAX_PKT_BURST, port); 591 len = 0; 592 } 593 594 qconf->pkt_buf[port].len = len; 595 return 0; 596 } 597 598 static void 599 l2fwd_mac_updating(struct rte_mbuf *m, uint16_t dest_portid) 600 { 601 struct rte_ether_hdr *eth; 602 void *tmp; 603 604 eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *); 605 606 /* 02:00:00:00:00:xx */ 607 tmp = ð->d_addr.addr_bytes[0]; 608 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40); 609 610 /* src addr */ 611 rte_ether_addr_copy(&l2fwd_ports_eth_addr[dest_portid], ð->s_addr); 612 } 613 614 static void 615 l2fwd_simple_forward(struct rte_mbuf *m, uint16_t portid, 616 struct l2fwd_crypto_options *options) 617 { 618 uint16_t dst_port; 619 uint32_t pad_len; 620 struct rte_ipv4_hdr *ip_hdr; 621 uint32_t ipdata_offset = sizeof(struct rte_ether_hdr); 622 623 ip_hdr = (struct rte_ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) + 624 ipdata_offset); 625 dst_port = l2fwd_dst_ports[portid]; 626 627 if (options->mac_updating) 628 l2fwd_mac_updating(m, dst_port); 629 630 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY) 631 rte_pktmbuf_trim(m, options->auth_xform.auth.digest_length); 632 633 if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) { 634 pad_len = m->pkt_len - rte_be_to_cpu_16(ip_hdr->total_length) - 635 ipdata_offset; 636 rte_pktmbuf_trim(m, pad_len); 637 } 638 639 l2fwd_send_packet(m, dst_port); 640 } 641 642 /** Generate random key */ 643 static void 644 generate_random_key(uint8_t *key, unsigned length) 645 { 646 int fd; 647 int ret; 648 649 fd = open("/dev/urandom", O_RDONLY); 650 if (fd < 0) 651 rte_exit(EXIT_FAILURE, "Failed to generate random key\n"); 652 653 ret = read(fd, key, length); 654 close(fd); 655 656 if (ret != (signed)length) 657 rte_exit(EXIT_FAILURE, "Failed to generate random key\n"); 658 } 659 660 static struct rte_cryptodev_sym_session * 661 initialize_crypto_session(struct l2fwd_crypto_options *options, uint8_t cdev_id) 662 { 663 struct rte_crypto_sym_xform *first_xform; 664 struct rte_cryptodev_sym_session *session; 665 int retval = rte_cryptodev_socket_id(cdev_id); 666 667 if (retval < 0) 668 return NULL; 669 670 uint8_t socket_id = (uint8_t) retval; 671 672 if (options->xform_chain == L2FWD_CRYPTO_AEAD) { 673 first_xform = &options->aead_xform; 674 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) { 675 first_xform = &options->cipher_xform; 676 first_xform->next = &options->auth_xform; 677 } else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) { 678 first_xform = &options->auth_xform; 679 first_xform->next = &options->cipher_xform; 680 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) { 681 first_xform = &options->cipher_xform; 682 } else { 683 first_xform = &options->auth_xform; 684 } 685 686 session = rte_cryptodev_sym_session_create( 687 session_pool_socket[socket_id].sess_mp); 688 if (session == NULL) 689 return NULL; 690 691 if (rte_cryptodev_sym_session_init(cdev_id, session, 692 first_xform, 693 session_pool_socket[socket_id].priv_mp) < 0) 694 return NULL; 695 696 return session; 697 } 698 699 static void 700 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options); 701 702 /* main processing loop */ 703 static void 704 l2fwd_main_loop(struct l2fwd_crypto_options *options) 705 { 706 struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST]; 707 struct rte_crypto_op *ops_burst[MAX_PKT_BURST]; 708 709 unsigned lcore_id = rte_lcore_id(); 710 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0; 711 unsigned int i, j, nb_rx, len; 712 uint16_t portid; 713 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id]; 714 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / 715 US_PER_S * BURST_TX_DRAIN_US; 716 struct l2fwd_crypto_params *cparams; 717 struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs]; 718 struct rte_cryptodev_sym_session *session; 719 720 if (qconf->nb_rx_ports == 0) { 721 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id); 722 return; 723 } 724 725 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id); 726 727 for (i = 0; i < qconf->nb_rx_ports; i++) { 728 729 portid = qconf->rx_port_list[i]; 730 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id, 731 portid); 732 } 733 734 for (i = 0; i < qconf->nb_crypto_devs; i++) { 735 port_cparams[i].do_cipher = 0; 736 port_cparams[i].do_hash = 0; 737 port_cparams[i].do_aead = 0; 738 739 switch (options->xform_chain) { 740 case L2FWD_CRYPTO_AEAD: 741 port_cparams[i].do_aead = 1; 742 break; 743 case L2FWD_CRYPTO_CIPHER_HASH: 744 case L2FWD_CRYPTO_HASH_CIPHER: 745 port_cparams[i].do_cipher = 1; 746 port_cparams[i].do_hash = 1; 747 break; 748 case L2FWD_CRYPTO_HASH_ONLY: 749 port_cparams[i].do_hash = 1; 750 break; 751 case L2FWD_CRYPTO_CIPHER_ONLY: 752 port_cparams[i].do_cipher = 1; 753 break; 754 } 755 756 port_cparams[i].dev_id = qconf->cryptodev_list[i]; 757 port_cparams[i].qp_id = 0; 758 759 port_cparams[i].block_size = options->block_size; 760 761 if (port_cparams[i].do_hash) { 762 port_cparams[i].auth_iv.data = options->auth_iv.data; 763 port_cparams[i].auth_iv.length = options->auth_iv.length; 764 if (!options->auth_iv_param) 765 generate_random_key(port_cparams[i].auth_iv.data, 766 port_cparams[i].auth_iv.length); 767 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY) 768 port_cparams[i].hash_verify = 1; 769 else 770 port_cparams[i].hash_verify = 0; 771 772 port_cparams[i].auth_algo = options->auth_xform.auth.algo; 773 port_cparams[i].digest_length = 774 options->auth_xform.auth.digest_length; 775 /* Set IV parameters */ 776 if (options->auth_iv.length) { 777 options->auth_xform.auth.iv.offset = 778 IV_OFFSET + options->cipher_iv.length; 779 options->auth_xform.auth.iv.length = 780 options->auth_iv.length; 781 } 782 } 783 784 if (port_cparams[i].do_aead) { 785 port_cparams[i].aead_iv.data = options->aead_iv.data; 786 port_cparams[i].aead_iv.length = options->aead_iv.length; 787 if (!options->aead_iv_param) 788 generate_random_key(port_cparams[i].aead_iv.data, 789 port_cparams[i].aead_iv.length); 790 port_cparams[i].aead_algo = options->aead_xform.aead.algo; 791 port_cparams[i].digest_length = 792 options->aead_xform.aead.digest_length; 793 if (options->aead_xform.aead.aad_length) { 794 port_cparams[i].aad.data = options->aad.data; 795 port_cparams[i].aad.phys_addr = options->aad.phys_addr; 796 port_cparams[i].aad.length = options->aad.length; 797 if (!options->aad_param) 798 generate_random_key(port_cparams[i].aad.data, 799 port_cparams[i].aad.length); 800 /* 801 * If doing AES-CCM, first 18 bytes has to be reserved, 802 * and actual AAD should start from byte 18 803 */ 804 if (port_cparams[i].aead_algo == RTE_CRYPTO_AEAD_AES_CCM) 805 memmove(port_cparams[i].aad.data + 18, 806 port_cparams[i].aad.data, 807 port_cparams[i].aad.length); 808 809 } else 810 port_cparams[i].aad.length = 0; 811 812 if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_DECRYPT) 813 port_cparams[i].hash_verify = 1; 814 else 815 port_cparams[i].hash_verify = 0; 816 817 /* Set IV parameters */ 818 options->aead_xform.aead.iv.offset = IV_OFFSET; 819 options->aead_xform.aead.iv.length = options->aead_iv.length; 820 } 821 822 if (port_cparams[i].do_cipher) { 823 port_cparams[i].cipher_iv.data = options->cipher_iv.data; 824 port_cparams[i].cipher_iv.length = options->cipher_iv.length; 825 if (!options->cipher_iv_param) 826 generate_random_key(port_cparams[i].cipher_iv.data, 827 port_cparams[i].cipher_iv.length); 828 829 port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo; 830 /* Set IV parameters */ 831 options->cipher_xform.cipher.iv.offset = IV_OFFSET; 832 options->cipher_xform.cipher.iv.length = 833 options->cipher_iv.length; 834 } 835 836 session = initialize_crypto_session(options, 837 port_cparams[i].dev_id); 838 if (session == NULL) 839 rte_exit(EXIT_FAILURE, "Failed to initialize crypto session\n"); 840 841 port_cparams[i].session = session; 842 843 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id, 844 port_cparams[i].dev_id); 845 } 846 847 l2fwd_crypto_options_print(options); 848 849 /* 850 * Initialize previous tsc timestamp before the loop, 851 * to avoid showing the port statistics immediately, 852 * so user can see the crypto information. 853 */ 854 prev_tsc = rte_rdtsc(); 855 while (1) { 856 857 cur_tsc = rte_rdtsc(); 858 859 /* 860 * Crypto device/TX burst queue drain 861 */ 862 diff_tsc = cur_tsc - prev_tsc; 863 if (unlikely(diff_tsc > drain_tsc)) { 864 /* Enqueue all crypto ops remaining in buffers */ 865 for (i = 0; i < qconf->nb_crypto_devs; i++) { 866 cparams = &port_cparams[i]; 867 len = qconf->op_buf[cparams->dev_id].len; 868 l2fwd_crypto_send_burst(qconf, len, cparams); 869 qconf->op_buf[cparams->dev_id].len = 0; 870 } 871 /* Transmit all packets remaining in buffers */ 872 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) { 873 if (qconf->pkt_buf[portid].len == 0) 874 continue; 875 l2fwd_send_burst(&lcore_queue_conf[lcore_id], 876 qconf->pkt_buf[portid].len, 877 portid); 878 qconf->pkt_buf[portid].len = 0; 879 } 880 881 /* if timer is enabled */ 882 if (timer_period > 0) { 883 884 /* advance the timer */ 885 timer_tsc += diff_tsc; 886 887 /* if timer has reached its timeout */ 888 if (unlikely(timer_tsc >= 889 (uint64_t)timer_period)) { 890 891 /* do this only on main core */ 892 if (lcore_id == rte_get_main_lcore() 893 && options->refresh_period) { 894 print_stats(); 895 timer_tsc = 0; 896 } 897 } 898 } 899 900 prev_tsc = cur_tsc; 901 } 902 903 /* 904 * Read packet from RX queues 905 */ 906 for (i = 0; i < qconf->nb_rx_ports; i++) { 907 portid = qconf->rx_port_list[i]; 908 909 cparams = &port_cparams[i]; 910 911 nb_rx = rte_eth_rx_burst(portid, 0, 912 pkts_burst, MAX_PKT_BURST); 913 914 port_statistics[portid].rx += nb_rx; 915 916 if (nb_rx) { 917 /* 918 * If we can't allocate a crypto_ops, then drop 919 * the rest of the burst and dequeue and 920 * process the packets to free offload structs 921 */ 922 if (rte_crypto_op_bulk_alloc( 923 l2fwd_crypto_op_pool, 924 RTE_CRYPTO_OP_TYPE_SYMMETRIC, 925 ops_burst, nb_rx) != 926 nb_rx) { 927 for (j = 0; j < nb_rx; j++) 928 rte_pktmbuf_free(pkts_burst[j]); 929 930 nb_rx = 0; 931 } 932 933 /* Enqueue packets from Crypto device*/ 934 for (j = 0; j < nb_rx; j++) { 935 m = pkts_burst[j]; 936 937 l2fwd_simple_crypto_enqueue(m, 938 ops_burst[j], cparams); 939 } 940 } 941 942 /* Dequeue packets from Crypto device */ 943 do { 944 nb_rx = rte_cryptodev_dequeue_burst( 945 cparams->dev_id, cparams->qp_id, 946 ops_burst, MAX_PKT_BURST); 947 948 crypto_statistics[cparams->dev_id].dequeued += 949 nb_rx; 950 951 /* Forward crypto'd packets */ 952 for (j = 0; j < nb_rx; j++) { 953 m = ops_burst[j]->sym->m_src; 954 955 rte_crypto_op_free(ops_burst[j]); 956 l2fwd_simple_forward(m, portid, 957 options); 958 } 959 } while (nb_rx == MAX_PKT_BURST); 960 } 961 } 962 } 963 964 static int 965 l2fwd_launch_one_lcore(void *arg) 966 { 967 l2fwd_main_loop((struct l2fwd_crypto_options *)arg); 968 return 0; 969 } 970 971 /* Display command line arguments usage */ 972 static void 973 l2fwd_crypto_usage(const char *prgname) 974 { 975 printf("%s [EAL options] --\n" 976 " -p PORTMASK: hexadecimal bitmask of ports to configure\n" 977 " -q NQ: number of queue (=ports) per lcore (default is 1)\n" 978 " -s manage all ports from single lcore\n" 979 " -T PERIOD: statistics will be refreshed each PERIOD seconds" 980 " (0 to disable, 10 default, 86400 maximum)\n" 981 982 " --cdev_type HW / SW / ANY\n" 983 " --chain HASH_CIPHER / CIPHER_HASH / CIPHER_ONLY /" 984 " HASH_ONLY / AEAD\n" 985 986 " --cipher_algo ALGO\n" 987 " --cipher_op ENCRYPT / DECRYPT\n" 988 " --cipher_key KEY (bytes separated with \":\")\n" 989 " --cipher_key_random_size SIZE: size of cipher key when generated randomly\n" 990 " --cipher_iv IV (bytes separated with \":\")\n" 991 " --cipher_iv_random_size SIZE: size of cipher IV when generated randomly\n" 992 993 " --auth_algo ALGO\n" 994 " --auth_op GENERATE / VERIFY\n" 995 " --auth_key KEY (bytes separated with \":\")\n" 996 " --auth_key_random_size SIZE: size of auth key when generated randomly\n" 997 " --auth_iv IV (bytes separated with \":\")\n" 998 " --auth_iv_random_size SIZE: size of auth IV when generated randomly\n" 999 1000 " --aead_algo ALGO\n" 1001 " --aead_op ENCRYPT / DECRYPT\n" 1002 " --aead_key KEY (bytes separated with \":\")\n" 1003 " --aead_key_random_size SIZE: size of AEAD key when generated randomly\n" 1004 " --aead_iv IV (bytes separated with \":\")\n" 1005 " --aead_iv_random_size SIZE: size of AEAD IV when generated randomly\n" 1006 " --aad AAD (bytes separated with \":\")\n" 1007 " --aad_random_size SIZE: size of AAD when generated randomly\n" 1008 1009 " --digest_size SIZE: size of digest to be generated/verified\n" 1010 1011 " --sessionless\n" 1012 " --cryptodev_mask MASK: hexadecimal bitmask of crypto devices to configure\n" 1013 1014 " --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n" 1015 " When enabled:\n" 1016 " - The source MAC address is replaced by the TX port MAC address\n" 1017 " - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n", 1018 prgname); 1019 } 1020 1021 /** Parse crypto device type command line argument */ 1022 static int 1023 parse_cryptodev_type(enum cdev_type *type, char *optarg) 1024 { 1025 if (strcmp("HW", optarg) == 0) { 1026 *type = CDEV_TYPE_HW; 1027 return 0; 1028 } else if (strcmp("SW", optarg) == 0) { 1029 *type = CDEV_TYPE_SW; 1030 return 0; 1031 } else if (strcmp("ANY", optarg) == 0) { 1032 *type = CDEV_TYPE_ANY; 1033 return 0; 1034 } 1035 1036 return -1; 1037 } 1038 1039 /** Parse crypto chain xform command line argument */ 1040 static int 1041 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg) 1042 { 1043 if (strcmp("CIPHER_HASH", optarg) == 0) { 1044 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH; 1045 return 0; 1046 } else if (strcmp("HASH_CIPHER", optarg) == 0) { 1047 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER; 1048 return 0; 1049 } else if (strcmp("CIPHER_ONLY", optarg) == 0) { 1050 options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY; 1051 return 0; 1052 } else if (strcmp("HASH_ONLY", optarg) == 0) { 1053 options->xform_chain = L2FWD_CRYPTO_HASH_ONLY; 1054 return 0; 1055 } else if (strcmp("AEAD", optarg) == 0) { 1056 options->xform_chain = L2FWD_CRYPTO_AEAD; 1057 return 0; 1058 } 1059 1060 return -1; 1061 } 1062 1063 /** Parse crypto cipher algo option command line argument */ 1064 static int 1065 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg) 1066 { 1067 1068 if (rte_cryptodev_get_cipher_algo_enum(algo, optarg) < 0) { 1069 RTE_LOG(ERR, USER1, "Cipher algorithm specified " 1070 "not supported!\n"); 1071 return -1; 1072 } 1073 1074 return 0; 1075 } 1076 1077 /** Parse crypto cipher operation command line argument */ 1078 static int 1079 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg) 1080 { 1081 if (strcmp("ENCRYPT", optarg) == 0) { 1082 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT; 1083 return 0; 1084 } else if (strcmp("DECRYPT", optarg) == 0) { 1085 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT; 1086 return 0; 1087 } 1088 1089 printf("Cipher operation not supported!\n"); 1090 return -1; 1091 } 1092 1093 /** Parse bytes from command line argument */ 1094 static int 1095 parse_bytes(uint8_t *data, char *input_arg, uint16_t max_size) 1096 { 1097 unsigned byte_count; 1098 char *token; 1099 1100 errno = 0; 1101 for (byte_count = 0, token = strtok(input_arg, ":"); 1102 (byte_count < max_size) && (token != NULL); 1103 token = strtok(NULL, ":")) { 1104 1105 int number = (int)strtol(token, NULL, 16); 1106 1107 if (errno == EINVAL || errno == ERANGE || number > 0xFF) 1108 return -1; 1109 1110 data[byte_count++] = (uint8_t)number; 1111 } 1112 1113 return byte_count; 1114 } 1115 1116 /** Parse size param*/ 1117 static int 1118 parse_size(int *size, const char *q_arg) 1119 { 1120 char *end = NULL; 1121 unsigned long n; 1122 1123 /* parse hexadecimal string */ 1124 n = strtoul(q_arg, &end, 10); 1125 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0')) 1126 n = 0; 1127 1128 if (n == 0) { 1129 printf("invalid size\n"); 1130 return -1; 1131 } 1132 1133 *size = n; 1134 return 0; 1135 } 1136 1137 /** Parse crypto cipher operation command line argument */ 1138 static int 1139 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg) 1140 { 1141 if (rte_cryptodev_get_auth_algo_enum(algo, optarg) < 0) { 1142 RTE_LOG(ERR, USER1, "Authentication algorithm specified " 1143 "not supported!\n"); 1144 return -1; 1145 } 1146 1147 return 0; 1148 } 1149 1150 static int 1151 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg) 1152 { 1153 if (strcmp("VERIFY", optarg) == 0) { 1154 *op = RTE_CRYPTO_AUTH_OP_VERIFY; 1155 return 0; 1156 } else if (strcmp("GENERATE", optarg) == 0) { 1157 *op = RTE_CRYPTO_AUTH_OP_GENERATE; 1158 return 0; 1159 } 1160 1161 printf("Authentication operation specified not supported!\n"); 1162 return -1; 1163 } 1164 1165 static int 1166 parse_aead_algo(enum rte_crypto_aead_algorithm *algo, char *optarg) 1167 { 1168 if (rte_cryptodev_get_aead_algo_enum(algo, optarg) < 0) { 1169 RTE_LOG(ERR, USER1, "AEAD algorithm specified " 1170 "not supported!\n"); 1171 return -1; 1172 } 1173 1174 return 0; 1175 } 1176 1177 static int 1178 parse_aead_op(enum rte_crypto_aead_operation *op, char *optarg) 1179 { 1180 if (strcmp("ENCRYPT", optarg) == 0) { 1181 *op = RTE_CRYPTO_AEAD_OP_ENCRYPT; 1182 return 0; 1183 } else if (strcmp("DECRYPT", optarg) == 0) { 1184 *op = RTE_CRYPTO_AEAD_OP_DECRYPT; 1185 return 0; 1186 } 1187 1188 printf("AEAD operation specified not supported!\n"); 1189 return -1; 1190 } 1191 static int 1192 parse_cryptodev_mask(struct l2fwd_crypto_options *options, 1193 const char *q_arg) 1194 { 1195 char *end = NULL; 1196 uint64_t pm; 1197 1198 /* parse hexadecimal string */ 1199 pm = strtoul(q_arg, &end, 16); 1200 if ((pm == '\0') || (end == NULL) || (*end != '\0')) 1201 pm = 0; 1202 1203 options->cryptodev_mask = pm; 1204 if (options->cryptodev_mask == 0) { 1205 printf("invalid cryptodev_mask specified\n"); 1206 return -1; 1207 } 1208 1209 return 0; 1210 } 1211 1212 /** Parse long options */ 1213 static int 1214 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options, 1215 struct option *lgopts, int option_index) 1216 { 1217 int retval; 1218 1219 if (strcmp(lgopts[option_index].name, "cdev_type") == 0) { 1220 retval = parse_cryptodev_type(&options->type, optarg); 1221 if (retval == 0) 1222 strlcpy(options->string_type, optarg, MAX_STR_LEN); 1223 return retval; 1224 } 1225 1226 else if (strcmp(lgopts[option_index].name, "chain") == 0) 1227 return parse_crypto_opt_chain(options, optarg); 1228 1229 /* Cipher options */ 1230 else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0) 1231 return parse_cipher_algo(&options->cipher_xform.cipher.algo, 1232 optarg); 1233 1234 else if (strcmp(lgopts[option_index].name, "cipher_op") == 0) 1235 return parse_cipher_op(&options->cipher_xform.cipher.op, 1236 optarg); 1237 1238 else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) { 1239 options->ckey_param = 1; 1240 options->cipher_xform.cipher.key.length = 1241 parse_bytes(options->cipher_key, optarg, MAX_KEY_SIZE); 1242 if (options->cipher_xform.cipher.key.length > 0) 1243 return 0; 1244 else 1245 return -1; 1246 } 1247 1248 else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0) 1249 return parse_size(&options->ckey_random_size, optarg); 1250 1251 else if (strcmp(lgopts[option_index].name, "cipher_iv") == 0) { 1252 options->cipher_iv_param = 1; 1253 options->cipher_iv.length = 1254 parse_bytes(options->cipher_iv.data, optarg, MAX_IV_SIZE); 1255 if (options->cipher_iv.length > 0) 1256 return 0; 1257 else 1258 return -1; 1259 } 1260 1261 else if (strcmp(lgopts[option_index].name, "cipher_iv_random_size") == 0) 1262 return parse_size(&options->cipher_iv_random_size, optarg); 1263 1264 /* Authentication options */ 1265 else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) { 1266 return parse_auth_algo(&options->auth_xform.auth.algo, 1267 optarg); 1268 } 1269 1270 else if (strcmp(lgopts[option_index].name, "auth_op") == 0) 1271 return parse_auth_op(&options->auth_xform.auth.op, 1272 optarg); 1273 1274 else if (strcmp(lgopts[option_index].name, "auth_key") == 0) { 1275 options->akey_param = 1; 1276 options->auth_xform.auth.key.length = 1277 parse_bytes(options->auth_key, optarg, MAX_KEY_SIZE); 1278 if (options->auth_xform.auth.key.length > 0) 1279 return 0; 1280 else 1281 return -1; 1282 } 1283 1284 else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) { 1285 return parse_size(&options->akey_random_size, optarg); 1286 } 1287 1288 else if (strcmp(lgopts[option_index].name, "auth_iv") == 0) { 1289 options->auth_iv_param = 1; 1290 options->auth_iv.length = 1291 parse_bytes(options->auth_iv.data, optarg, MAX_IV_SIZE); 1292 if (options->auth_iv.length > 0) 1293 return 0; 1294 else 1295 return -1; 1296 } 1297 1298 else if (strcmp(lgopts[option_index].name, "auth_iv_random_size") == 0) 1299 return parse_size(&options->auth_iv_random_size, optarg); 1300 1301 /* AEAD options */ 1302 else if (strcmp(lgopts[option_index].name, "aead_algo") == 0) { 1303 return parse_aead_algo(&options->aead_xform.aead.algo, 1304 optarg); 1305 } 1306 1307 else if (strcmp(lgopts[option_index].name, "aead_op") == 0) 1308 return parse_aead_op(&options->aead_xform.aead.op, 1309 optarg); 1310 1311 else if (strcmp(lgopts[option_index].name, "aead_key") == 0) { 1312 options->aead_key_param = 1; 1313 options->aead_xform.aead.key.length = 1314 parse_bytes(options->aead_key, optarg, MAX_KEY_SIZE); 1315 if (options->aead_xform.aead.key.length > 0) 1316 return 0; 1317 else 1318 return -1; 1319 } 1320 1321 else if (strcmp(lgopts[option_index].name, "aead_key_random_size") == 0) 1322 return parse_size(&options->aead_key_random_size, optarg); 1323 1324 1325 else if (strcmp(lgopts[option_index].name, "aead_iv") == 0) { 1326 options->aead_iv_param = 1; 1327 options->aead_iv.length = 1328 parse_bytes(options->aead_iv.data, optarg, MAX_IV_SIZE); 1329 if (options->aead_iv.length > 0) 1330 return 0; 1331 else 1332 return -1; 1333 } 1334 1335 else if (strcmp(lgopts[option_index].name, "aead_iv_random_size") == 0) 1336 return parse_size(&options->aead_iv_random_size, optarg); 1337 1338 else if (strcmp(lgopts[option_index].name, "aad") == 0) { 1339 options->aad_param = 1; 1340 options->aad.length = 1341 parse_bytes(options->aad.data, optarg, MAX_AAD_SIZE); 1342 if (options->aad.length > 0) 1343 return 0; 1344 else 1345 return -1; 1346 } 1347 1348 else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) { 1349 return parse_size(&options->aad_random_size, optarg); 1350 } 1351 1352 else if (strcmp(lgopts[option_index].name, "digest_size") == 0) { 1353 return parse_size(&options->digest_size, optarg); 1354 } 1355 1356 else if (strcmp(lgopts[option_index].name, "sessionless") == 0) { 1357 options->sessionless = 1; 1358 return 0; 1359 } 1360 1361 else if (strcmp(lgopts[option_index].name, "cryptodev_mask") == 0) 1362 return parse_cryptodev_mask(options, optarg); 1363 1364 else if (strcmp(lgopts[option_index].name, "mac-updating") == 0) { 1365 options->mac_updating = 1; 1366 return 0; 1367 } 1368 1369 else if (strcmp(lgopts[option_index].name, "no-mac-updating") == 0) { 1370 options->mac_updating = 0; 1371 return 0; 1372 } 1373 1374 return -1; 1375 } 1376 1377 /** Parse port mask */ 1378 static int 1379 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options, 1380 const char *q_arg) 1381 { 1382 char *end = NULL; 1383 unsigned long pm; 1384 1385 /* parse hexadecimal string */ 1386 pm = strtoul(q_arg, &end, 16); 1387 if ((pm == '\0') || (end == NULL) || (*end != '\0')) 1388 pm = 0; 1389 1390 options->portmask = pm; 1391 if (options->portmask == 0) { 1392 printf("invalid portmask specified\n"); 1393 return -1; 1394 } 1395 1396 return pm; 1397 } 1398 1399 /** Parse number of queues */ 1400 static int 1401 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options, 1402 const char *q_arg) 1403 { 1404 char *end = NULL; 1405 unsigned long n; 1406 1407 /* parse hexadecimal string */ 1408 n = strtoul(q_arg, &end, 10); 1409 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0')) 1410 n = 0; 1411 else if (n >= MAX_RX_QUEUE_PER_LCORE) 1412 n = 0; 1413 1414 options->nb_ports_per_lcore = n; 1415 if (options->nb_ports_per_lcore == 0) { 1416 printf("invalid number of ports selected\n"); 1417 return -1; 1418 } 1419 1420 return 0; 1421 } 1422 1423 /** Parse timer period */ 1424 static int 1425 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options, 1426 const char *q_arg) 1427 { 1428 char *end = NULL; 1429 unsigned long n; 1430 1431 /* parse number string */ 1432 n = (unsigned)strtol(q_arg, &end, 10); 1433 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0')) 1434 n = 0; 1435 1436 if (n >= MAX_TIMER_PERIOD) { 1437 printf("Warning refresh period specified %lu is greater than " 1438 "max value %lu! using max value", 1439 n, MAX_TIMER_PERIOD); 1440 n = MAX_TIMER_PERIOD; 1441 } 1442 1443 options->refresh_period = n * 1000 * TIMER_MILLISECOND; 1444 1445 return 0; 1446 } 1447 1448 /** Generate default options for application */ 1449 static void 1450 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options) 1451 { 1452 options->portmask = 0xffffffff; 1453 options->nb_ports_per_lcore = 1; 1454 options->refresh_period = 10000; 1455 options->single_lcore = 0; 1456 options->sessionless = 0; 1457 1458 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH; 1459 1460 /* Cipher Data */ 1461 options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER; 1462 options->cipher_xform.next = NULL; 1463 options->ckey_param = 0; 1464 options->ckey_random_size = -1; 1465 options->cipher_xform.cipher.key.length = 0; 1466 options->cipher_iv_param = 0; 1467 options->cipher_iv_random_size = -1; 1468 options->cipher_iv.length = 0; 1469 1470 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC; 1471 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT; 1472 1473 /* Authentication Data */ 1474 options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH; 1475 options->auth_xform.next = NULL; 1476 options->akey_param = 0; 1477 options->akey_random_size = -1; 1478 options->auth_xform.auth.key.length = 0; 1479 options->auth_iv_param = 0; 1480 options->auth_iv_random_size = -1; 1481 options->auth_iv.length = 0; 1482 1483 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC; 1484 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE; 1485 1486 /* AEAD Data */ 1487 options->aead_xform.type = RTE_CRYPTO_SYM_XFORM_AEAD; 1488 options->aead_xform.next = NULL; 1489 options->aead_key_param = 0; 1490 options->aead_key_random_size = -1; 1491 options->aead_xform.aead.key.length = 0; 1492 options->aead_iv_param = 0; 1493 options->aead_iv_random_size = -1; 1494 options->aead_iv.length = 0; 1495 1496 options->aead_xform.aead.algo = RTE_CRYPTO_AEAD_AES_GCM; 1497 options->aead_xform.aead.op = RTE_CRYPTO_AEAD_OP_ENCRYPT; 1498 1499 options->aad_param = 0; 1500 options->aad_random_size = -1; 1501 options->aad.length = 0; 1502 1503 options->digest_size = -1; 1504 1505 options->type = CDEV_TYPE_ANY; 1506 options->cryptodev_mask = UINT64_MAX; 1507 1508 options->mac_updating = 1; 1509 } 1510 1511 static void 1512 display_cipher_info(struct l2fwd_crypto_options *options) 1513 { 1514 printf("\n---- Cipher information ---\n"); 1515 printf("Algorithm: %s\n", 1516 rte_crypto_cipher_algorithm_strings[options->cipher_xform.cipher.algo]); 1517 rte_hexdump(stdout, "Cipher key:", 1518 options->cipher_xform.cipher.key.data, 1519 options->cipher_xform.cipher.key.length); 1520 rte_hexdump(stdout, "IV:", options->cipher_iv.data, options->cipher_iv.length); 1521 } 1522 1523 static void 1524 display_auth_info(struct l2fwd_crypto_options *options) 1525 { 1526 printf("\n---- Authentication information ---\n"); 1527 printf("Algorithm: %s\n", 1528 rte_crypto_auth_algorithm_strings[options->auth_xform.auth.algo]); 1529 rte_hexdump(stdout, "Auth key:", 1530 options->auth_xform.auth.key.data, 1531 options->auth_xform.auth.key.length); 1532 rte_hexdump(stdout, "IV:", options->auth_iv.data, options->auth_iv.length); 1533 } 1534 1535 static void 1536 display_aead_info(struct l2fwd_crypto_options *options) 1537 { 1538 printf("\n---- AEAD information ---\n"); 1539 printf("Algorithm: %s\n", 1540 rte_crypto_aead_algorithm_strings[options->aead_xform.aead.algo]); 1541 rte_hexdump(stdout, "AEAD key:", 1542 options->aead_xform.aead.key.data, 1543 options->aead_xform.aead.key.length); 1544 rte_hexdump(stdout, "IV:", options->aead_iv.data, options->aead_iv.length); 1545 rte_hexdump(stdout, "AAD:", options->aad.data, options->aad.length); 1546 } 1547 1548 static void 1549 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options) 1550 { 1551 char string_cipher_op[MAX_STR_LEN]; 1552 char string_auth_op[MAX_STR_LEN]; 1553 char string_aead_op[MAX_STR_LEN]; 1554 1555 if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) 1556 strcpy(string_cipher_op, "Encrypt"); 1557 else 1558 strcpy(string_cipher_op, "Decrypt"); 1559 1560 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) 1561 strcpy(string_auth_op, "Auth generate"); 1562 else 1563 strcpy(string_auth_op, "Auth verify"); 1564 1565 if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) 1566 strcpy(string_aead_op, "Authenticated encryption"); 1567 else 1568 strcpy(string_aead_op, "Authenticated decryption"); 1569 1570 1571 printf("Options:-\nn"); 1572 printf("portmask: %x\n", options->portmask); 1573 printf("ports per lcore: %u\n", options->nb_ports_per_lcore); 1574 printf("refresh period : %u\n", options->refresh_period); 1575 printf("single lcore mode: %s\n", 1576 options->single_lcore ? "enabled" : "disabled"); 1577 printf("stats_printing: %s\n", 1578 options->refresh_period == 0 ? "disabled" : "enabled"); 1579 1580 printf("sessionless crypto: %s\n", 1581 options->sessionless ? "enabled" : "disabled"); 1582 1583 if (options->ckey_param && (options->ckey_random_size != -1)) 1584 printf("Cipher key already parsed, ignoring size of random key\n"); 1585 1586 if (options->akey_param && (options->akey_random_size != -1)) 1587 printf("Auth key already parsed, ignoring size of random key\n"); 1588 1589 if (options->cipher_iv_param && (options->cipher_iv_random_size != -1)) 1590 printf("Cipher IV already parsed, ignoring size of random IV\n"); 1591 1592 if (options->auth_iv_param && (options->auth_iv_random_size != -1)) 1593 printf("Auth IV already parsed, ignoring size of random IV\n"); 1594 1595 if (options->aad_param && (options->aad_random_size != -1)) 1596 printf("AAD already parsed, ignoring size of random AAD\n"); 1597 1598 printf("\nCrypto chain: "); 1599 switch (options->xform_chain) { 1600 case L2FWD_CRYPTO_AEAD: 1601 printf("Input --> %s --> Output\n", string_aead_op); 1602 display_aead_info(options); 1603 break; 1604 case L2FWD_CRYPTO_CIPHER_HASH: 1605 printf("Input --> %s --> %s --> Output\n", 1606 string_cipher_op, string_auth_op); 1607 display_cipher_info(options); 1608 display_auth_info(options); 1609 break; 1610 case L2FWD_CRYPTO_HASH_CIPHER: 1611 printf("Input --> %s --> %s --> Output\n", 1612 string_auth_op, string_cipher_op); 1613 display_cipher_info(options); 1614 display_auth_info(options); 1615 break; 1616 case L2FWD_CRYPTO_HASH_ONLY: 1617 printf("Input --> %s --> Output\n", string_auth_op); 1618 display_auth_info(options); 1619 break; 1620 case L2FWD_CRYPTO_CIPHER_ONLY: 1621 printf("Input --> %s --> Output\n", string_cipher_op); 1622 display_cipher_info(options); 1623 break; 1624 } 1625 } 1626 1627 /* Parse the argument given in the command line of the application */ 1628 static int 1629 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options, 1630 int argc, char **argv) 1631 { 1632 int opt, retval, option_index; 1633 char **argvopt = argv, *prgname = argv[0]; 1634 1635 static struct option lgopts[] = { 1636 { "sessionless", no_argument, 0, 0 }, 1637 1638 { "cdev_type", required_argument, 0, 0 }, 1639 { "chain", required_argument, 0, 0 }, 1640 1641 { "cipher_algo", required_argument, 0, 0 }, 1642 { "cipher_op", required_argument, 0, 0 }, 1643 { "cipher_key", required_argument, 0, 0 }, 1644 { "cipher_key_random_size", required_argument, 0, 0 }, 1645 { "cipher_iv", required_argument, 0, 0 }, 1646 { "cipher_iv_random_size", required_argument, 0, 0 }, 1647 1648 { "auth_algo", required_argument, 0, 0 }, 1649 { "auth_op", required_argument, 0, 0 }, 1650 { "auth_key", required_argument, 0, 0 }, 1651 { "auth_key_random_size", required_argument, 0, 0 }, 1652 { "auth_iv", required_argument, 0, 0 }, 1653 { "auth_iv_random_size", required_argument, 0, 0 }, 1654 1655 { "aead_algo", required_argument, 0, 0 }, 1656 { "aead_op", required_argument, 0, 0 }, 1657 { "aead_key", required_argument, 0, 0 }, 1658 { "aead_key_random_size", required_argument, 0, 0 }, 1659 { "aead_iv", required_argument, 0, 0 }, 1660 { "aead_iv_random_size", required_argument, 0, 0 }, 1661 1662 { "aad", required_argument, 0, 0 }, 1663 { "aad_random_size", required_argument, 0, 0 }, 1664 1665 { "digest_size", required_argument, 0, 0 }, 1666 1667 { "sessionless", no_argument, 0, 0 }, 1668 { "cryptodev_mask", required_argument, 0, 0}, 1669 1670 { "mac-updating", no_argument, 0, 0}, 1671 { "no-mac-updating", no_argument, 0, 0}, 1672 1673 { NULL, 0, 0, 0 } 1674 }; 1675 1676 l2fwd_crypto_default_options(options); 1677 1678 while ((opt = getopt_long(argc, argvopt, "p:q:sT:", lgopts, 1679 &option_index)) != EOF) { 1680 switch (opt) { 1681 /* long options */ 1682 case 0: 1683 retval = l2fwd_crypto_parse_args_long_options(options, 1684 lgopts, option_index); 1685 if (retval < 0) { 1686 l2fwd_crypto_usage(prgname); 1687 return -1; 1688 } 1689 break; 1690 1691 /* portmask */ 1692 case 'p': 1693 retval = l2fwd_crypto_parse_portmask(options, optarg); 1694 if (retval < 0) { 1695 l2fwd_crypto_usage(prgname); 1696 return -1; 1697 } 1698 break; 1699 1700 /* nqueue */ 1701 case 'q': 1702 retval = l2fwd_crypto_parse_nqueue(options, optarg); 1703 if (retval < 0) { 1704 l2fwd_crypto_usage(prgname); 1705 return -1; 1706 } 1707 break; 1708 1709 /* single */ 1710 case 's': 1711 options->single_lcore = 1; 1712 1713 break; 1714 1715 /* timer period */ 1716 case 'T': 1717 retval = l2fwd_crypto_parse_timer_period(options, 1718 optarg); 1719 if (retval < 0) { 1720 l2fwd_crypto_usage(prgname); 1721 return -1; 1722 } 1723 break; 1724 1725 default: 1726 l2fwd_crypto_usage(prgname); 1727 return -1; 1728 } 1729 } 1730 1731 1732 if (optind >= 0) 1733 argv[optind-1] = prgname; 1734 1735 retval = optind-1; 1736 optind = 1; /* reset getopt lib */ 1737 1738 return retval; 1739 } 1740 1741 /* Check the link status of all ports in up to 9s, and print them finally */ 1742 static void 1743 check_all_ports_link_status(uint32_t port_mask) 1744 { 1745 #define CHECK_INTERVAL 100 /* 100ms */ 1746 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */ 1747 uint16_t portid; 1748 uint8_t count, all_ports_up, print_flag = 0; 1749 struct rte_eth_link link; 1750 int ret; 1751 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN]; 1752 1753 printf("\nChecking link status"); 1754 fflush(stdout); 1755 for (count = 0; count <= MAX_CHECK_TIME; count++) { 1756 all_ports_up = 1; 1757 RTE_ETH_FOREACH_DEV(portid) { 1758 if ((port_mask & (1 << portid)) == 0) 1759 continue; 1760 memset(&link, 0, sizeof(link)); 1761 ret = rte_eth_link_get_nowait(portid, &link); 1762 if (ret < 0) { 1763 all_ports_up = 0; 1764 if (print_flag == 1) 1765 printf("Port %u link get failed: %s\n", 1766 portid, rte_strerror(-ret)); 1767 continue; 1768 } 1769 /* print link status if flag set */ 1770 if (print_flag == 1) { 1771 rte_eth_link_to_str(link_status_text, 1772 sizeof(link_status_text), &link); 1773 printf("Port %d %s\n", portid, 1774 link_status_text); 1775 continue; 1776 } 1777 /* clear all_ports_up flag if any link down */ 1778 if (link.link_status == ETH_LINK_DOWN) { 1779 all_ports_up = 0; 1780 break; 1781 } 1782 } 1783 /* after finally printing all link status, get out */ 1784 if (print_flag == 1) 1785 break; 1786 1787 if (all_ports_up == 0) { 1788 printf("."); 1789 fflush(stdout); 1790 rte_delay_ms(CHECK_INTERVAL); 1791 } 1792 1793 /* set the print_flag if all ports up or timeout */ 1794 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) { 1795 print_flag = 1; 1796 printf("done\n"); 1797 } 1798 } 1799 } 1800 1801 /* Check if device has to be HW/SW or any */ 1802 static int 1803 check_type(const struct l2fwd_crypto_options *options, 1804 const struct rte_cryptodev_info *dev_info) 1805 { 1806 if (options->type == CDEV_TYPE_HW && 1807 (dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED)) 1808 return 0; 1809 if (options->type == CDEV_TYPE_SW && 1810 !(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED)) 1811 return 0; 1812 if (options->type == CDEV_TYPE_ANY) 1813 return 0; 1814 1815 return -1; 1816 } 1817 1818 static const struct rte_cryptodev_capabilities * 1819 check_device_support_cipher_algo(const struct l2fwd_crypto_options *options, 1820 const struct rte_cryptodev_info *dev_info, 1821 uint8_t cdev_id) 1822 { 1823 unsigned int i = 0; 1824 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0]; 1825 enum rte_crypto_cipher_algorithm cap_cipher_algo; 1826 enum rte_crypto_cipher_algorithm opt_cipher_algo = 1827 options->cipher_xform.cipher.algo; 1828 1829 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) { 1830 cap_cipher_algo = cap->sym.cipher.algo; 1831 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_CIPHER) { 1832 if (cap_cipher_algo == opt_cipher_algo) { 1833 if (check_type(options, dev_info) == 0) 1834 break; 1835 } 1836 } 1837 cap = &dev_info->capabilities[++i]; 1838 } 1839 1840 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) { 1841 printf("Algorithm %s not supported by cryptodev %u" 1842 " or device not of preferred type (%s)\n", 1843 rte_crypto_cipher_algorithm_strings[opt_cipher_algo], 1844 cdev_id, 1845 options->string_type); 1846 return NULL; 1847 } 1848 1849 return cap; 1850 } 1851 1852 static const struct rte_cryptodev_capabilities * 1853 check_device_support_auth_algo(const struct l2fwd_crypto_options *options, 1854 const struct rte_cryptodev_info *dev_info, 1855 uint8_t cdev_id) 1856 { 1857 unsigned int i = 0; 1858 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0]; 1859 enum rte_crypto_auth_algorithm cap_auth_algo; 1860 enum rte_crypto_auth_algorithm opt_auth_algo = 1861 options->auth_xform.auth.algo; 1862 1863 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) { 1864 cap_auth_algo = cap->sym.auth.algo; 1865 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) { 1866 if (cap_auth_algo == opt_auth_algo) { 1867 if (check_type(options, dev_info) == 0) 1868 break; 1869 } 1870 } 1871 cap = &dev_info->capabilities[++i]; 1872 } 1873 1874 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) { 1875 printf("Algorithm %s not supported by cryptodev %u" 1876 " or device not of preferred type (%s)\n", 1877 rte_crypto_auth_algorithm_strings[opt_auth_algo], 1878 cdev_id, 1879 options->string_type); 1880 return NULL; 1881 } 1882 1883 return cap; 1884 } 1885 1886 static const struct rte_cryptodev_capabilities * 1887 check_device_support_aead_algo(const struct l2fwd_crypto_options *options, 1888 const struct rte_cryptodev_info *dev_info, 1889 uint8_t cdev_id) 1890 { 1891 unsigned int i = 0; 1892 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0]; 1893 enum rte_crypto_aead_algorithm cap_aead_algo; 1894 enum rte_crypto_aead_algorithm opt_aead_algo = 1895 options->aead_xform.aead.algo; 1896 1897 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) { 1898 cap_aead_algo = cap->sym.aead.algo; 1899 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) { 1900 if (cap_aead_algo == opt_aead_algo) { 1901 if (check_type(options, dev_info) == 0) 1902 break; 1903 } 1904 } 1905 cap = &dev_info->capabilities[++i]; 1906 } 1907 1908 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) { 1909 printf("Algorithm %s not supported by cryptodev %u" 1910 " or device not of preferred type (%s)\n", 1911 rte_crypto_aead_algorithm_strings[opt_aead_algo], 1912 cdev_id, 1913 options->string_type); 1914 return NULL; 1915 } 1916 1917 return cap; 1918 } 1919 1920 /* Check if the device is enabled by cryptodev_mask */ 1921 static int 1922 check_cryptodev_mask(struct l2fwd_crypto_options *options, 1923 uint8_t cdev_id) 1924 { 1925 if (options->cryptodev_mask & (1 << cdev_id)) 1926 return 0; 1927 1928 return -1; 1929 } 1930 1931 static inline int 1932 check_supported_size(uint16_t length, uint16_t min, uint16_t max, 1933 uint16_t increment) 1934 { 1935 uint16_t supp_size; 1936 1937 /* Single value */ 1938 if (increment == 0) { 1939 if (length == min) 1940 return 0; 1941 else 1942 return -1; 1943 } 1944 1945 /* Range of values */ 1946 for (supp_size = min; supp_size <= max; supp_size += increment) { 1947 if (length == supp_size) 1948 return 0; 1949 } 1950 1951 return -1; 1952 } 1953 1954 static int 1955 check_iv_param(const struct rte_crypto_param_range *iv_range_size, 1956 unsigned int iv_param, int iv_random_size, 1957 uint16_t iv_length) 1958 { 1959 /* 1960 * Check if length of provided IV is supported 1961 * by the algorithm chosen. 1962 */ 1963 if (iv_param) { 1964 if (check_supported_size(iv_length, 1965 iv_range_size->min, 1966 iv_range_size->max, 1967 iv_range_size->increment) 1968 != 0) 1969 return -1; 1970 /* 1971 * Check if length of IV to be randomly generated 1972 * is supported by the algorithm chosen. 1973 */ 1974 } else if (iv_random_size != -1) { 1975 if (check_supported_size(iv_random_size, 1976 iv_range_size->min, 1977 iv_range_size->max, 1978 iv_range_size->increment) 1979 != 0) 1980 return -1; 1981 } 1982 1983 return 0; 1984 } 1985 1986 static int 1987 check_capabilities(struct l2fwd_crypto_options *options, uint8_t cdev_id) 1988 { 1989 struct rte_cryptodev_info dev_info; 1990 const struct rte_cryptodev_capabilities *cap; 1991 1992 rte_cryptodev_info_get(cdev_id, &dev_info); 1993 1994 /* Set AEAD parameters */ 1995 if (options->xform_chain == L2FWD_CRYPTO_AEAD) { 1996 /* Check if device supports AEAD algo */ 1997 cap = check_device_support_aead_algo(options, &dev_info, 1998 cdev_id); 1999 if (cap == NULL) 2000 return -1; 2001 2002 if (check_iv_param(&cap->sym.aead.iv_size, 2003 options->aead_iv_param, 2004 options->aead_iv_random_size, 2005 options->aead_iv.length) != 0) { 2006 RTE_LOG(DEBUG, USER1, 2007 "Device %u does not support IV length\n", 2008 cdev_id); 2009 return -1; 2010 } 2011 2012 /* 2013 * Check if length of provided AEAD key is supported 2014 * by the algorithm chosen. 2015 */ 2016 if (options->aead_key_param) { 2017 if (check_supported_size( 2018 options->aead_xform.aead.key.length, 2019 cap->sym.aead.key_size.min, 2020 cap->sym.aead.key_size.max, 2021 cap->sym.aead.key_size.increment) 2022 != 0) { 2023 RTE_LOG(DEBUG, USER1, 2024 "Device %u does not support " 2025 "AEAD key length\n", 2026 cdev_id); 2027 return -1; 2028 } 2029 /* 2030 * Check if length of the aead key to be randomly generated 2031 * is supported by the algorithm chosen. 2032 */ 2033 } else if (options->aead_key_random_size != -1) { 2034 if (check_supported_size(options->aead_key_random_size, 2035 cap->sym.aead.key_size.min, 2036 cap->sym.aead.key_size.max, 2037 cap->sym.aead.key_size.increment) 2038 != 0) { 2039 RTE_LOG(DEBUG, USER1, 2040 "Device %u does not support " 2041 "AEAD key length\n", 2042 cdev_id); 2043 return -1; 2044 } 2045 } 2046 2047 2048 /* 2049 * Check if length of provided AAD is supported 2050 * by the algorithm chosen. 2051 */ 2052 if (options->aad_param) { 2053 if (check_supported_size(options->aad.length, 2054 cap->sym.aead.aad_size.min, 2055 cap->sym.aead.aad_size.max, 2056 cap->sym.aead.aad_size.increment) 2057 != 0) { 2058 RTE_LOG(DEBUG, USER1, 2059 "Device %u does not support " 2060 "AAD length\n", 2061 cdev_id); 2062 return -1; 2063 } 2064 /* 2065 * Check if length of AAD to be randomly generated 2066 * is supported by the algorithm chosen. 2067 */ 2068 } else if (options->aad_random_size != -1) { 2069 if (check_supported_size(options->aad_random_size, 2070 cap->sym.aead.aad_size.min, 2071 cap->sym.aead.aad_size.max, 2072 cap->sym.aead.aad_size.increment) 2073 != 0) { 2074 RTE_LOG(DEBUG, USER1, 2075 "Device %u does not support " 2076 "AAD length\n", 2077 cdev_id); 2078 return -1; 2079 } 2080 } 2081 2082 /* Check if digest size is supported by the algorithm. */ 2083 if (options->digest_size != -1) { 2084 if (check_supported_size(options->digest_size, 2085 cap->sym.aead.digest_size.min, 2086 cap->sym.aead.digest_size.max, 2087 cap->sym.aead.digest_size.increment) 2088 != 0) { 2089 RTE_LOG(DEBUG, USER1, 2090 "Device %u does not support " 2091 "digest length\n", 2092 cdev_id); 2093 return -1; 2094 } 2095 } 2096 } 2097 2098 /* Set cipher parameters */ 2099 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH || 2100 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER || 2101 options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) { 2102 /* Check if device supports cipher algo */ 2103 cap = check_device_support_cipher_algo(options, &dev_info, 2104 cdev_id); 2105 if (cap == NULL) 2106 return -1; 2107 2108 if (check_iv_param(&cap->sym.cipher.iv_size, 2109 options->cipher_iv_param, 2110 options->cipher_iv_random_size, 2111 options->cipher_iv.length) != 0) { 2112 RTE_LOG(DEBUG, USER1, 2113 "Device %u does not support IV length\n", 2114 cdev_id); 2115 return -1; 2116 } 2117 2118 /* 2119 * Check if length of provided cipher key is supported 2120 * by the algorithm chosen. 2121 */ 2122 if (options->ckey_param) { 2123 if (check_supported_size( 2124 options->cipher_xform.cipher.key.length, 2125 cap->sym.cipher.key_size.min, 2126 cap->sym.cipher.key_size.max, 2127 cap->sym.cipher.key_size.increment) 2128 != 0) { 2129 RTE_LOG(DEBUG, USER1, 2130 "Device %u does not support cipher " 2131 "key length\n", 2132 cdev_id); 2133 return -1; 2134 } 2135 /* 2136 * Check if length of the cipher key to be randomly generated 2137 * is supported by the algorithm chosen. 2138 */ 2139 } else if (options->ckey_random_size != -1) { 2140 if (check_supported_size(options->ckey_random_size, 2141 cap->sym.cipher.key_size.min, 2142 cap->sym.cipher.key_size.max, 2143 cap->sym.cipher.key_size.increment) 2144 != 0) { 2145 RTE_LOG(DEBUG, USER1, 2146 "Device %u does not support cipher " 2147 "key length\n", 2148 cdev_id); 2149 return -1; 2150 } 2151 } 2152 } 2153 2154 /* Set auth parameters */ 2155 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH || 2156 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER || 2157 options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) { 2158 /* Check if device supports auth algo */ 2159 cap = check_device_support_auth_algo(options, &dev_info, 2160 cdev_id); 2161 if (cap == NULL) 2162 return -1; 2163 2164 if (check_iv_param(&cap->sym.auth.iv_size, 2165 options->auth_iv_param, 2166 options->auth_iv_random_size, 2167 options->auth_iv.length) != 0) { 2168 RTE_LOG(DEBUG, USER1, 2169 "Device %u does not support IV length\n", 2170 cdev_id); 2171 return -1; 2172 } 2173 /* 2174 * Check if length of provided auth key is supported 2175 * by the algorithm chosen. 2176 */ 2177 if (options->akey_param) { 2178 if (check_supported_size( 2179 options->auth_xform.auth.key.length, 2180 cap->sym.auth.key_size.min, 2181 cap->sym.auth.key_size.max, 2182 cap->sym.auth.key_size.increment) 2183 != 0) { 2184 RTE_LOG(DEBUG, USER1, 2185 "Device %u does not support auth " 2186 "key length\n", 2187 cdev_id); 2188 return -1; 2189 } 2190 /* 2191 * Check if length of the auth key to be randomly generated 2192 * is supported by the algorithm chosen. 2193 */ 2194 } else if (options->akey_random_size != -1) { 2195 if (check_supported_size(options->akey_random_size, 2196 cap->sym.auth.key_size.min, 2197 cap->sym.auth.key_size.max, 2198 cap->sym.auth.key_size.increment) 2199 != 0) { 2200 RTE_LOG(DEBUG, USER1, 2201 "Device %u does not support auth " 2202 "key length\n", 2203 cdev_id); 2204 return -1; 2205 } 2206 } 2207 2208 /* Check if digest size is supported by the algorithm. */ 2209 if (options->digest_size != -1) { 2210 if (check_supported_size(options->digest_size, 2211 cap->sym.auth.digest_size.min, 2212 cap->sym.auth.digest_size.max, 2213 cap->sym.auth.digest_size.increment) 2214 != 0) { 2215 RTE_LOG(DEBUG, USER1, 2216 "Device %u does not support " 2217 "digest length\n", 2218 cdev_id); 2219 return -1; 2220 } 2221 } 2222 } 2223 2224 return 0; 2225 } 2226 2227 static int 2228 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports, 2229 uint8_t *enabled_cdevs) 2230 { 2231 uint8_t cdev_id, cdev_count, enabled_cdev_count = 0; 2232 const struct rte_cryptodev_capabilities *cap; 2233 unsigned int sess_sz, max_sess_sz = 0; 2234 uint32_t sessions_needed = 0; 2235 int retval; 2236 2237 cdev_count = rte_cryptodev_count(); 2238 if (cdev_count == 0) { 2239 printf("No crypto devices available\n"); 2240 return -1; 2241 } 2242 2243 for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports; 2244 cdev_id++) { 2245 if (check_cryptodev_mask(options, cdev_id) < 0) 2246 continue; 2247 2248 if (check_capabilities(options, cdev_id) < 0) 2249 continue; 2250 2251 sess_sz = rte_cryptodev_sym_get_private_session_size(cdev_id); 2252 if (sess_sz > max_sess_sz) 2253 max_sess_sz = sess_sz; 2254 2255 l2fwd_enabled_crypto_mask |= (((uint64_t)1) << cdev_id); 2256 2257 enabled_cdevs[cdev_id] = 1; 2258 enabled_cdev_count++; 2259 } 2260 2261 for (cdev_id = 0; cdev_id < cdev_count; cdev_id++) { 2262 struct rte_cryptodev_qp_conf qp_conf; 2263 struct rte_cryptodev_info dev_info; 2264 2265 if (enabled_cdevs[cdev_id] == 0) 2266 continue; 2267 2268 if (check_cryptodev_mask(options, cdev_id) < 0) 2269 continue; 2270 2271 if (check_capabilities(options, cdev_id) < 0) 2272 continue; 2273 2274 retval = rte_cryptodev_socket_id(cdev_id); 2275 2276 if (retval < 0) { 2277 printf("Invalid crypto device id used\n"); 2278 return -1; 2279 } 2280 2281 uint8_t socket_id = (uint8_t) retval; 2282 2283 struct rte_cryptodev_config conf = { 2284 .nb_queue_pairs = 1, 2285 .socket_id = socket_id, 2286 .ff_disable = RTE_CRYPTODEV_FF_SECURITY, 2287 }; 2288 2289 rte_cryptodev_info_get(cdev_id, &dev_info); 2290 2291 /* 2292 * Two sessions objects are required for each session 2293 * (one for the header, one for the private data) 2294 */ 2295 if (!strcmp(dev_info.driver_name, "crypto_scheduler")) { 2296 #ifdef RTE_CRYPTO_SCHEDULER 2297 uint32_t nb_workers = 2298 rte_cryptodev_scheduler_workers_get(cdev_id, 2299 NULL); 2300 2301 sessions_needed = enabled_cdev_count * nb_workers; 2302 #endif 2303 } else 2304 sessions_needed = enabled_cdev_count; 2305 2306 if (session_pool_socket[socket_id].priv_mp == NULL) { 2307 char mp_name[RTE_MEMPOOL_NAMESIZE]; 2308 2309 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE, 2310 "priv_sess_mp_%u", socket_id); 2311 2312 session_pool_socket[socket_id].priv_mp = 2313 rte_mempool_create(mp_name, 2314 sessions_needed, 2315 max_sess_sz, 2316 0, 0, NULL, NULL, NULL, 2317 NULL, socket_id, 2318 0); 2319 2320 if (session_pool_socket[socket_id].priv_mp == NULL) { 2321 printf("Cannot create pool on socket %d\n", 2322 socket_id); 2323 return -ENOMEM; 2324 } 2325 2326 printf("Allocated pool \"%s\" on socket %d\n", 2327 mp_name, socket_id); 2328 } 2329 2330 if (session_pool_socket[socket_id].sess_mp == NULL) { 2331 char mp_name[RTE_MEMPOOL_NAMESIZE]; 2332 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE, 2333 "sess_mp_%u", socket_id); 2334 2335 session_pool_socket[socket_id].sess_mp = 2336 rte_cryptodev_sym_session_pool_create( 2337 mp_name, 2338 sessions_needed, 2339 0, 0, 0, socket_id); 2340 2341 if (session_pool_socket[socket_id].sess_mp == NULL) { 2342 printf("Cannot create pool on socket %d\n", 2343 socket_id); 2344 return -ENOMEM; 2345 } 2346 2347 printf("Allocated pool \"%s\" on socket %d\n", 2348 mp_name, socket_id); 2349 } 2350 2351 /* Set AEAD parameters */ 2352 if (options->xform_chain == L2FWD_CRYPTO_AEAD) { 2353 cap = check_device_support_aead_algo(options, &dev_info, 2354 cdev_id); 2355 2356 options->block_size = cap->sym.aead.block_size; 2357 2358 /* Set IV if not provided from command line */ 2359 if (options->aead_iv_param == 0) { 2360 if (options->aead_iv_random_size != -1) 2361 options->aead_iv.length = 2362 options->aead_iv_random_size; 2363 /* No size provided, use minimum size. */ 2364 else 2365 options->aead_iv.length = 2366 cap->sym.aead.iv_size.min; 2367 } 2368 2369 /* Set key if not provided from command line */ 2370 if (options->aead_key_param == 0) { 2371 if (options->aead_key_random_size != -1) 2372 options->aead_xform.aead.key.length = 2373 options->aead_key_random_size; 2374 /* No size provided, use minimum size. */ 2375 else 2376 options->aead_xform.aead.key.length = 2377 cap->sym.aead.key_size.min; 2378 2379 generate_random_key(options->aead_key, 2380 options->aead_xform.aead.key.length); 2381 } 2382 2383 /* Set AAD if not provided from command line */ 2384 if (options->aad_param == 0) { 2385 if (options->aad_random_size != -1) 2386 options->aad.length = 2387 options->aad_random_size; 2388 /* No size provided, use minimum size. */ 2389 else 2390 options->aad.length = 2391 cap->sym.auth.aad_size.min; 2392 } 2393 2394 options->aead_xform.aead.aad_length = 2395 options->aad.length; 2396 2397 /* Set digest size if not provided from command line */ 2398 if (options->digest_size != -1) 2399 options->aead_xform.aead.digest_length = 2400 options->digest_size; 2401 /* No size provided, use minimum size. */ 2402 else 2403 options->aead_xform.aead.digest_length = 2404 cap->sym.aead.digest_size.min; 2405 } 2406 2407 /* Set cipher parameters */ 2408 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH || 2409 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER || 2410 options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) { 2411 cap = check_device_support_cipher_algo(options, &dev_info, 2412 cdev_id); 2413 options->block_size = cap->sym.cipher.block_size; 2414 2415 /* Set IV if not provided from command line */ 2416 if (options->cipher_iv_param == 0) { 2417 if (options->cipher_iv_random_size != -1) 2418 options->cipher_iv.length = 2419 options->cipher_iv_random_size; 2420 /* No size provided, use minimum size. */ 2421 else 2422 options->cipher_iv.length = 2423 cap->sym.cipher.iv_size.min; 2424 } 2425 2426 /* Set key if not provided from command line */ 2427 if (options->ckey_param == 0) { 2428 if (options->ckey_random_size != -1) 2429 options->cipher_xform.cipher.key.length = 2430 options->ckey_random_size; 2431 /* No size provided, use minimum size. */ 2432 else 2433 options->cipher_xform.cipher.key.length = 2434 cap->sym.cipher.key_size.min; 2435 2436 generate_random_key(options->cipher_key, 2437 options->cipher_xform.cipher.key.length); 2438 } 2439 } 2440 2441 /* Set auth parameters */ 2442 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH || 2443 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER || 2444 options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) { 2445 cap = check_device_support_auth_algo(options, &dev_info, 2446 cdev_id); 2447 2448 /* Set IV if not provided from command line */ 2449 if (options->auth_iv_param == 0) { 2450 if (options->auth_iv_random_size != -1) 2451 options->auth_iv.length = 2452 options->auth_iv_random_size; 2453 /* No size provided, use minimum size. */ 2454 else 2455 options->auth_iv.length = 2456 cap->sym.auth.iv_size.min; 2457 } 2458 2459 /* Set key if not provided from command line */ 2460 if (options->akey_param == 0) { 2461 if (options->akey_random_size != -1) 2462 options->auth_xform.auth.key.length = 2463 options->akey_random_size; 2464 /* No size provided, use minimum size. */ 2465 else 2466 options->auth_xform.auth.key.length = 2467 cap->sym.auth.key_size.min; 2468 2469 generate_random_key(options->auth_key, 2470 options->auth_xform.auth.key.length); 2471 } 2472 2473 /* Set digest size if not provided from command line */ 2474 if (options->digest_size != -1) 2475 options->auth_xform.auth.digest_length = 2476 options->digest_size; 2477 /* No size provided, use minimum size. */ 2478 else 2479 options->auth_xform.auth.digest_length = 2480 cap->sym.auth.digest_size.min; 2481 } 2482 2483 retval = rte_cryptodev_configure(cdev_id, &conf); 2484 if (retval < 0) { 2485 printf("Failed to configure cryptodev %u", cdev_id); 2486 return -1; 2487 } 2488 2489 qp_conf.nb_descriptors = 2048; 2490 qp_conf.mp_session = session_pool_socket[socket_id].sess_mp; 2491 qp_conf.mp_session_private = 2492 session_pool_socket[socket_id].priv_mp; 2493 2494 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf, 2495 socket_id); 2496 if (retval < 0) { 2497 printf("Failed to setup queue pair %u on cryptodev %u", 2498 0, cdev_id); 2499 return -1; 2500 } 2501 2502 retval = rte_cryptodev_start(cdev_id); 2503 if (retval < 0) { 2504 printf("Failed to start device %u: error %d\n", 2505 cdev_id, retval); 2506 return -1; 2507 } 2508 } 2509 2510 return enabled_cdev_count; 2511 } 2512 2513 static int 2514 initialize_ports(struct l2fwd_crypto_options *options) 2515 { 2516 uint16_t last_portid = 0, portid; 2517 unsigned enabled_portcount = 0; 2518 unsigned nb_ports = rte_eth_dev_count_avail(); 2519 2520 if (nb_ports == 0) { 2521 printf("No Ethernet ports - bye\n"); 2522 return -1; 2523 } 2524 2525 /* Reset l2fwd_dst_ports */ 2526 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) 2527 l2fwd_dst_ports[portid] = 0; 2528 2529 RTE_ETH_FOREACH_DEV(portid) { 2530 int retval; 2531 struct rte_eth_dev_info dev_info; 2532 struct rte_eth_rxconf rxq_conf; 2533 struct rte_eth_txconf txq_conf; 2534 struct rte_eth_conf local_port_conf = port_conf; 2535 2536 /* Skip ports that are not enabled */ 2537 if ((options->portmask & (1 << portid)) == 0) 2538 continue; 2539 2540 /* init port */ 2541 printf("Initializing port %u... ", portid); 2542 fflush(stdout); 2543 2544 retval = rte_eth_dev_info_get(portid, &dev_info); 2545 if (retval != 0) { 2546 printf("Error during getting device (port %u) info: %s\n", 2547 portid, strerror(-retval)); 2548 return retval; 2549 } 2550 2551 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE) 2552 local_port_conf.txmode.offloads |= 2553 DEV_TX_OFFLOAD_MBUF_FAST_FREE; 2554 retval = rte_eth_dev_configure(portid, 1, 1, &local_port_conf); 2555 if (retval < 0) { 2556 printf("Cannot configure device: err=%d, port=%u\n", 2557 retval, portid); 2558 return -1; 2559 } 2560 2561 retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, 2562 &nb_txd); 2563 if (retval < 0) { 2564 printf("Cannot adjust number of descriptors: err=%d, port=%u\n", 2565 retval, portid); 2566 return -1; 2567 } 2568 2569 /* init one RX queue */ 2570 fflush(stdout); 2571 rxq_conf = dev_info.default_rxconf; 2572 rxq_conf.offloads = local_port_conf.rxmode.offloads; 2573 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd, 2574 rte_eth_dev_socket_id(portid), 2575 &rxq_conf, l2fwd_pktmbuf_pool); 2576 if (retval < 0) { 2577 printf("rte_eth_rx_queue_setup:err=%d, port=%u\n", 2578 retval, portid); 2579 return -1; 2580 } 2581 2582 /* init one TX queue on each port */ 2583 fflush(stdout); 2584 txq_conf = dev_info.default_txconf; 2585 txq_conf.offloads = local_port_conf.txmode.offloads; 2586 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd, 2587 rte_eth_dev_socket_id(portid), 2588 &txq_conf); 2589 if (retval < 0) { 2590 printf("rte_eth_tx_queue_setup:err=%d, port=%u\n", 2591 retval, portid); 2592 2593 return -1; 2594 } 2595 2596 /* Start device */ 2597 retval = rte_eth_dev_start(portid); 2598 if (retval < 0) { 2599 printf("rte_eth_dev_start:err=%d, port=%u\n", 2600 retval, portid); 2601 return -1; 2602 } 2603 2604 retval = rte_eth_promiscuous_enable(portid); 2605 if (retval != 0) { 2606 printf("rte_eth_promiscuous_enable:err=%s, port=%u\n", 2607 rte_strerror(-retval), portid); 2608 return -1; 2609 } 2610 2611 retval = rte_eth_macaddr_get(portid, 2612 &l2fwd_ports_eth_addr[portid]); 2613 if (retval < 0) { 2614 printf("rte_eth_macaddr_get :err=%d, port=%u\n", 2615 retval, portid); 2616 return -1; 2617 } 2618 2619 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n", 2620 portid, 2621 l2fwd_ports_eth_addr[portid].addr_bytes[0], 2622 l2fwd_ports_eth_addr[portid].addr_bytes[1], 2623 l2fwd_ports_eth_addr[portid].addr_bytes[2], 2624 l2fwd_ports_eth_addr[portid].addr_bytes[3], 2625 l2fwd_ports_eth_addr[portid].addr_bytes[4], 2626 l2fwd_ports_eth_addr[portid].addr_bytes[5]); 2627 2628 /* initialize port stats */ 2629 memset(&port_statistics, 0, sizeof(port_statistics)); 2630 2631 /* Setup port forwarding table */ 2632 if (enabled_portcount % 2) { 2633 l2fwd_dst_ports[portid] = last_portid; 2634 l2fwd_dst_ports[last_portid] = portid; 2635 } else { 2636 last_portid = portid; 2637 } 2638 2639 l2fwd_enabled_port_mask |= (1 << portid); 2640 enabled_portcount++; 2641 } 2642 2643 if (enabled_portcount == 1) { 2644 l2fwd_dst_ports[last_portid] = last_portid; 2645 } else if (enabled_portcount % 2) { 2646 printf("odd number of ports in portmask- bye\n"); 2647 return -1; 2648 } 2649 2650 check_all_ports_link_status(l2fwd_enabled_port_mask); 2651 2652 return enabled_portcount; 2653 } 2654 2655 static void 2656 reserve_key_memory(struct l2fwd_crypto_options *options) 2657 { 2658 options->cipher_xform.cipher.key.data = options->cipher_key; 2659 2660 options->auth_xform.auth.key.data = options->auth_key; 2661 2662 options->aead_xform.aead.key.data = options->aead_key; 2663 2664 options->cipher_iv.data = rte_malloc("cipher iv", MAX_KEY_SIZE, 0); 2665 if (options->cipher_iv.data == NULL) 2666 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher IV"); 2667 2668 options->auth_iv.data = rte_malloc("auth iv", MAX_KEY_SIZE, 0); 2669 if (options->auth_iv.data == NULL) 2670 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth IV"); 2671 2672 options->aead_iv.data = rte_malloc("aead_iv", MAX_KEY_SIZE, 0); 2673 if (options->aead_iv.data == NULL) 2674 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD iv"); 2675 2676 options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0); 2677 if (options->aad.data == NULL) 2678 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD"); 2679 options->aad.phys_addr = rte_malloc_virt2iova(options->aad.data); 2680 } 2681 2682 int 2683 main(int argc, char **argv) 2684 { 2685 struct lcore_queue_conf *qconf = NULL; 2686 struct l2fwd_crypto_options options; 2687 2688 uint8_t nb_cryptodevs, cdev_id; 2689 uint16_t portid; 2690 unsigned lcore_id, rx_lcore_id = 0; 2691 int ret, enabled_cdevcount, enabled_portcount; 2692 uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0}; 2693 2694 /* init EAL */ 2695 ret = rte_eal_init(argc, argv); 2696 if (ret < 0) 2697 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n"); 2698 argc -= ret; 2699 argv += ret; 2700 2701 /* reserve memory for Cipher/Auth key and IV */ 2702 reserve_key_memory(&options); 2703 2704 /* parse application arguments (after the EAL ones) */ 2705 ret = l2fwd_crypto_parse_args(&options, argc, argv); 2706 if (ret < 0) 2707 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n"); 2708 2709 printf("MAC updating %s\n", 2710 options.mac_updating ? "enabled" : "disabled"); 2711 2712 /* create the mbuf pool */ 2713 l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512, 2714 RTE_ALIGN(sizeof(struct rte_crypto_op), 2715 RTE_CACHE_LINE_SIZE), 2716 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); 2717 if (l2fwd_pktmbuf_pool == NULL) 2718 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n"); 2719 2720 /* create crypto op pool */ 2721 l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool", 2722 RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, MAXIMUM_IV_LENGTH, 2723 rte_socket_id()); 2724 if (l2fwd_crypto_op_pool == NULL) 2725 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n"); 2726 2727 /* Enable Ethernet ports */ 2728 enabled_portcount = initialize_ports(&options); 2729 if (enabled_portcount < 1) 2730 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n"); 2731 2732 /* Initialize the port/queue configuration of each logical core */ 2733 RTE_ETH_FOREACH_DEV(portid) { 2734 2735 /* skip ports that are not enabled */ 2736 if ((options.portmask & (1 << portid)) == 0) 2737 continue; 2738 2739 if (options.single_lcore && qconf == NULL) { 2740 while (rte_lcore_is_enabled(rx_lcore_id) == 0) { 2741 rx_lcore_id++; 2742 if (rx_lcore_id >= RTE_MAX_LCORE) 2743 rte_exit(EXIT_FAILURE, 2744 "Not enough cores\n"); 2745 } 2746 } else if (!options.single_lcore) { 2747 /* get the lcore_id for this port */ 2748 while (rte_lcore_is_enabled(rx_lcore_id) == 0 || 2749 lcore_queue_conf[rx_lcore_id].nb_rx_ports == 2750 options.nb_ports_per_lcore) { 2751 rx_lcore_id++; 2752 if (rx_lcore_id >= RTE_MAX_LCORE) 2753 rte_exit(EXIT_FAILURE, 2754 "Not enough cores\n"); 2755 } 2756 } 2757 2758 /* Assigned a new logical core in the loop above. */ 2759 if (qconf != &lcore_queue_conf[rx_lcore_id]) 2760 qconf = &lcore_queue_conf[rx_lcore_id]; 2761 2762 qconf->rx_port_list[qconf->nb_rx_ports] = portid; 2763 qconf->nb_rx_ports++; 2764 2765 printf("Lcore %u: RX port %u\n", rx_lcore_id, portid); 2766 } 2767 2768 /* Enable Crypto devices */ 2769 enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount, 2770 enabled_cdevs); 2771 if (enabled_cdevcount < 0) 2772 rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n"); 2773 2774 if (enabled_cdevcount < enabled_portcount) 2775 rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) " 2776 "has to be more or equal to number of ports (%d)\n", 2777 enabled_cdevcount, enabled_portcount); 2778 2779 nb_cryptodevs = rte_cryptodev_count(); 2780 2781 /* Initialize the port/cryptodev configuration of each logical core */ 2782 for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0; 2783 cdev_id < nb_cryptodevs && enabled_cdevcount; 2784 cdev_id++) { 2785 /* Crypto op not supported by crypto device */ 2786 if (!enabled_cdevs[cdev_id]) 2787 continue; 2788 2789 if (options.single_lcore && qconf == NULL) { 2790 while (rte_lcore_is_enabled(rx_lcore_id) == 0) { 2791 rx_lcore_id++; 2792 if (rx_lcore_id >= RTE_MAX_LCORE) 2793 rte_exit(EXIT_FAILURE, 2794 "Not enough cores\n"); 2795 } 2796 } else if (!options.single_lcore) { 2797 /* get the lcore_id for this port */ 2798 while (rte_lcore_is_enabled(rx_lcore_id) == 0 || 2799 lcore_queue_conf[rx_lcore_id].nb_crypto_devs == 2800 options.nb_ports_per_lcore) { 2801 rx_lcore_id++; 2802 if (rx_lcore_id >= RTE_MAX_LCORE) 2803 rte_exit(EXIT_FAILURE, 2804 "Not enough cores\n"); 2805 } 2806 } 2807 2808 /* Assigned a new logical core in the loop above. */ 2809 if (qconf != &lcore_queue_conf[rx_lcore_id]) 2810 qconf = &lcore_queue_conf[rx_lcore_id]; 2811 2812 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id; 2813 qconf->nb_crypto_devs++; 2814 2815 enabled_cdevcount--; 2816 2817 printf("Lcore %u: cryptodev %u\n", rx_lcore_id, 2818 (unsigned)cdev_id); 2819 } 2820 2821 /* launch per-lcore init on every lcore */ 2822 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options, 2823 CALL_MAIN); 2824 RTE_LCORE_FOREACH_WORKER(lcore_id) { 2825 if (rte_eal_wait_lcore(lcore_id) < 0) 2826 return -1; 2827 } 2828 2829 return 0; 2830 } 2831