1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2016 Intel Corporation 3 */ 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <stdint.h> 8 #include <inttypes.h> 9 #include <sys/types.h> 10 #include <netinet/in.h> 11 #include <netinet/ip.h> 12 #include <netinet/ip6.h> 13 #include <string.h> 14 #include <sys/queue.h> 15 #include <stdarg.h> 16 #include <errno.h> 17 #include <getopt.h> 18 19 #include <rte_common.h> 20 #include <rte_byteorder.h> 21 #include <rte_log.h> 22 #include <rte_eal.h> 23 #include <rte_launch.h> 24 #include <rte_atomic.h> 25 #include <rte_cycles.h> 26 #include <rte_prefetch.h> 27 #include <rte_lcore.h> 28 #include <rte_per_lcore.h> 29 #include <rte_branch_prediction.h> 30 #include <rte_interrupts.h> 31 #include <rte_random.h> 32 #include <rte_debug.h> 33 #include <rte_ether.h> 34 #include <rte_ethdev.h> 35 #include <rte_mempool.h> 36 #include <rte_mbuf.h> 37 #include <rte_acl.h> 38 #include <rte_lpm.h> 39 #include <rte_lpm6.h> 40 #include <rte_hash.h> 41 #include <rte_jhash.h> 42 #include <rte_cryptodev.h> 43 #include <rte_security.h> 44 45 #include "ipsec.h" 46 #include "parser.h" 47 48 #define RTE_LOGTYPE_IPSEC RTE_LOGTYPE_USER1 49 50 #define MAX_JUMBO_PKT_LEN 9600 51 52 #define MEMPOOL_CACHE_SIZE 256 53 54 #define NB_MBUF (32000) 55 56 #define CDEV_QUEUE_DESC 2048 57 #define CDEV_MAP_ENTRIES 1024 58 #define CDEV_MP_NB_OBJS 2048 59 #define CDEV_MP_CACHE_SZ 64 60 #define MAX_QUEUE_PAIRS 1 61 62 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */ 63 64 #define NB_SOCKETS 4 65 66 /* Configure how many packets ahead to prefetch, when reading packets */ 67 #define PREFETCH_OFFSET 3 68 69 #define MAX_RX_QUEUE_PER_LCORE 16 70 71 #define MAX_LCORE_PARAMS 1024 72 73 #define UNPROTECTED_PORT(port) (unprotected_port_mask & (1 << portid)) 74 75 /* 76 * Configurable number of RX/TX ring descriptors 77 */ 78 #define IPSEC_SECGW_RX_DESC_DEFAULT 1024 79 #define IPSEC_SECGW_TX_DESC_DEFAULT 1024 80 static uint16_t nb_rxd = IPSEC_SECGW_RX_DESC_DEFAULT; 81 static uint16_t nb_txd = IPSEC_SECGW_TX_DESC_DEFAULT; 82 83 #if RTE_BYTE_ORDER != RTE_LITTLE_ENDIAN 84 #define __BYTES_TO_UINT64(a, b, c, d, e, f, g, h) \ 85 (((uint64_t)((a) & 0xff) << 56) | \ 86 ((uint64_t)((b) & 0xff) << 48) | \ 87 ((uint64_t)((c) & 0xff) << 40) | \ 88 ((uint64_t)((d) & 0xff) << 32) | \ 89 ((uint64_t)((e) & 0xff) << 24) | \ 90 ((uint64_t)((f) & 0xff) << 16) | \ 91 ((uint64_t)((g) & 0xff) << 8) | \ 92 ((uint64_t)(h) & 0xff)) 93 #else 94 #define __BYTES_TO_UINT64(a, b, c, d, e, f, g, h) \ 95 (((uint64_t)((h) & 0xff) << 56) | \ 96 ((uint64_t)((g) & 0xff) << 48) | \ 97 ((uint64_t)((f) & 0xff) << 40) | \ 98 ((uint64_t)((e) & 0xff) << 32) | \ 99 ((uint64_t)((d) & 0xff) << 24) | \ 100 ((uint64_t)((c) & 0xff) << 16) | \ 101 ((uint64_t)((b) & 0xff) << 8) | \ 102 ((uint64_t)(a) & 0xff)) 103 #endif 104 #define ETHADDR(a, b, c, d, e, f) (__BYTES_TO_UINT64(a, b, c, d, e, f, 0, 0)) 105 106 #define ETHADDR_TO_UINT64(addr) __BYTES_TO_UINT64( \ 107 addr.addr_bytes[0], addr.addr_bytes[1], \ 108 addr.addr_bytes[2], addr.addr_bytes[3], \ 109 addr.addr_bytes[4], addr.addr_bytes[5], \ 110 0, 0) 111 112 /* port/source ethernet addr and destination ethernet addr */ 113 struct ethaddr_info { 114 uint64_t src, dst; 115 }; 116 117 struct ethaddr_info ethaddr_tbl[RTE_MAX_ETHPORTS] = { 118 { 0, ETHADDR(0x00, 0x16, 0x3e, 0x7e, 0x94, 0x9a) }, 119 { 0, ETHADDR(0x00, 0x16, 0x3e, 0x22, 0xa1, 0xd9) }, 120 { 0, ETHADDR(0x00, 0x16, 0x3e, 0x08, 0x69, 0x26) }, 121 { 0, ETHADDR(0x00, 0x16, 0x3e, 0x49, 0x9e, 0xdd) } 122 }; 123 124 #define CMD_LINE_OPT_CONFIG "config" 125 #define CMD_LINE_OPT_SINGLE_SA "single-sa" 126 #define CMD_LINE_OPT_CRYPTODEV_MASK "cryptodev_mask" 127 128 enum { 129 /* long options mapped to a short option */ 130 131 /* first long only option value must be >= 256, so that we won't 132 * conflict with short options 133 */ 134 CMD_LINE_OPT_MIN_NUM = 256, 135 CMD_LINE_OPT_CONFIG_NUM, 136 CMD_LINE_OPT_SINGLE_SA_NUM, 137 CMD_LINE_OPT_CRYPTODEV_MASK_NUM, 138 }; 139 140 static const struct option lgopts[] = { 141 {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM}, 142 {CMD_LINE_OPT_SINGLE_SA, 1, 0, CMD_LINE_OPT_SINGLE_SA_NUM}, 143 {CMD_LINE_OPT_CRYPTODEV_MASK, 1, 0, CMD_LINE_OPT_CRYPTODEV_MASK_NUM}, 144 {NULL, 0, 0, 0} 145 }; 146 147 /* mask of enabled ports */ 148 static uint32_t enabled_port_mask; 149 static uint64_t enabled_cryptodev_mask = UINT64_MAX; 150 static uint32_t unprotected_port_mask; 151 static int32_t promiscuous_on = 1; 152 static int32_t numa_on = 1; /**< NUMA is enabled by default. */ 153 static uint32_t nb_lcores; 154 static uint32_t single_sa; 155 static uint32_t single_sa_idx; 156 static uint32_t frame_size; 157 158 struct lcore_rx_queue { 159 uint16_t port_id; 160 uint8_t queue_id; 161 } __rte_cache_aligned; 162 163 struct lcore_params { 164 uint16_t port_id; 165 uint8_t queue_id; 166 uint8_t lcore_id; 167 } __rte_cache_aligned; 168 169 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS]; 170 171 static struct lcore_params *lcore_params; 172 static uint16_t nb_lcore_params; 173 174 static struct rte_hash *cdev_map_in; 175 static struct rte_hash *cdev_map_out; 176 177 struct buffer { 178 uint16_t len; 179 struct rte_mbuf *m_table[MAX_PKT_BURST] __rte_aligned(sizeof(void *)); 180 }; 181 182 struct lcore_conf { 183 uint16_t nb_rx_queue; 184 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE]; 185 uint16_t tx_queue_id[RTE_MAX_ETHPORTS]; 186 struct buffer tx_mbufs[RTE_MAX_ETHPORTS]; 187 struct ipsec_ctx inbound; 188 struct ipsec_ctx outbound; 189 struct rt_ctx *rt4_ctx; 190 struct rt_ctx *rt6_ctx; 191 } __rte_cache_aligned; 192 193 static struct lcore_conf lcore_conf[RTE_MAX_LCORE]; 194 195 static struct rte_eth_conf port_conf = { 196 .rxmode = { 197 .mq_mode = ETH_MQ_RX_RSS, 198 .max_rx_pkt_len = ETHER_MAX_LEN, 199 .split_hdr_size = 0, 200 .offloads = DEV_RX_OFFLOAD_CHECKSUM | 201 DEV_RX_OFFLOAD_CRC_STRIP, 202 }, 203 .rx_adv_conf = { 204 .rss_conf = { 205 .rss_key = NULL, 206 .rss_hf = ETH_RSS_IP | ETH_RSS_UDP | 207 ETH_RSS_TCP | ETH_RSS_SCTP, 208 }, 209 }, 210 .txmode = { 211 .mq_mode = ETH_MQ_TX_NONE, 212 .offloads = (DEV_TX_OFFLOAD_IPV4_CKSUM | 213 DEV_TX_OFFLOAD_MULTI_SEGS), 214 }, 215 }; 216 217 static struct socket_ctx socket_ctx[NB_SOCKETS]; 218 219 struct traffic_type { 220 const uint8_t *data[MAX_PKT_BURST * 2]; 221 struct rte_mbuf *pkts[MAX_PKT_BURST * 2]; 222 uint32_t res[MAX_PKT_BURST * 2]; 223 uint32_t num; 224 }; 225 226 struct ipsec_traffic { 227 struct traffic_type ipsec; 228 struct traffic_type ip4; 229 struct traffic_type ip6; 230 }; 231 232 static inline void 233 prepare_one_packet(struct rte_mbuf *pkt, struct ipsec_traffic *t) 234 { 235 uint8_t *nlp; 236 struct ether_hdr *eth; 237 238 eth = rte_pktmbuf_mtod(pkt, struct ether_hdr *); 239 if (eth->ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4)) { 240 nlp = (uint8_t *)rte_pktmbuf_adj(pkt, ETHER_HDR_LEN); 241 nlp = RTE_PTR_ADD(nlp, offsetof(struct ip, ip_p)); 242 if (*nlp == IPPROTO_ESP) 243 t->ipsec.pkts[(t->ipsec.num)++] = pkt; 244 else { 245 t->ip4.data[t->ip4.num] = nlp; 246 t->ip4.pkts[(t->ip4.num)++] = pkt; 247 } 248 } else if (eth->ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6)) { 249 nlp = (uint8_t *)rte_pktmbuf_adj(pkt, ETHER_HDR_LEN); 250 nlp = RTE_PTR_ADD(nlp, offsetof(struct ip6_hdr, ip6_nxt)); 251 if (*nlp == IPPROTO_ESP) 252 t->ipsec.pkts[(t->ipsec.num)++] = pkt; 253 else { 254 t->ip6.data[t->ip6.num] = nlp; 255 t->ip6.pkts[(t->ip6.num)++] = pkt; 256 } 257 } else { 258 /* Unknown/Unsupported type, drop the packet */ 259 RTE_LOG(ERR, IPSEC, "Unsupported packet type\n"); 260 rte_pktmbuf_free(pkt); 261 } 262 263 /* Check if the packet has been processed inline. For inline protocol 264 * processed packets, the metadata in the mbuf can be used to identify 265 * the security processing done on the packet. The metadata will be 266 * used to retrieve the application registered userdata associated 267 * with the security session. 268 */ 269 270 if (pkt->ol_flags & PKT_RX_SEC_OFFLOAD) { 271 struct ipsec_sa *sa; 272 struct ipsec_mbuf_metadata *priv; 273 struct rte_security_ctx *ctx = (struct rte_security_ctx *) 274 rte_eth_dev_get_sec_ctx( 275 pkt->port); 276 277 /* Retrieve the userdata registered. Here, the userdata 278 * registered is the SA pointer. 279 */ 280 281 sa = (struct ipsec_sa *) 282 rte_security_get_userdata(ctx, pkt->udata64); 283 284 if (sa == NULL) { 285 /* userdata could not be retrieved */ 286 return; 287 } 288 289 /* Save SA as priv member in mbuf. This will be used in the 290 * IPsec selector(SP-SA) check. 291 */ 292 293 priv = get_priv(pkt); 294 priv->sa = sa; 295 } 296 } 297 298 static inline void 299 prepare_traffic(struct rte_mbuf **pkts, struct ipsec_traffic *t, 300 uint16_t nb_pkts) 301 { 302 int32_t i; 303 304 t->ipsec.num = 0; 305 t->ip4.num = 0; 306 t->ip6.num = 0; 307 308 for (i = 0; i < (nb_pkts - PREFETCH_OFFSET); i++) { 309 rte_prefetch0(rte_pktmbuf_mtod(pkts[i + PREFETCH_OFFSET], 310 void *)); 311 prepare_one_packet(pkts[i], t); 312 } 313 /* Process left packets */ 314 for (; i < nb_pkts; i++) 315 prepare_one_packet(pkts[i], t); 316 } 317 318 static inline void 319 prepare_tx_pkt(struct rte_mbuf *pkt, uint16_t port) 320 { 321 struct ip *ip; 322 struct ether_hdr *ethhdr; 323 324 ip = rte_pktmbuf_mtod(pkt, struct ip *); 325 326 ethhdr = (struct ether_hdr *)rte_pktmbuf_prepend(pkt, ETHER_HDR_LEN); 327 328 if (ip->ip_v == IPVERSION) { 329 pkt->ol_flags |= PKT_TX_IP_CKSUM | PKT_TX_IPV4; 330 pkt->l3_len = sizeof(struct ip); 331 pkt->l2_len = ETHER_HDR_LEN; 332 333 ethhdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv4); 334 } else { 335 pkt->ol_flags |= PKT_TX_IPV6; 336 pkt->l3_len = sizeof(struct ip6_hdr); 337 pkt->l2_len = ETHER_HDR_LEN; 338 339 ethhdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv6); 340 } 341 342 memcpy(ðhdr->s_addr, ðaddr_tbl[port].src, 343 sizeof(struct ether_addr)); 344 memcpy(ðhdr->d_addr, ðaddr_tbl[port].dst, 345 sizeof(struct ether_addr)); 346 } 347 348 static inline void 349 prepare_tx_burst(struct rte_mbuf *pkts[], uint16_t nb_pkts, uint16_t port) 350 { 351 int32_t i; 352 const int32_t prefetch_offset = 2; 353 354 for (i = 0; i < (nb_pkts - prefetch_offset); i++) { 355 rte_mbuf_prefetch_part2(pkts[i + prefetch_offset]); 356 prepare_tx_pkt(pkts[i], port); 357 } 358 /* Process left packets */ 359 for (; i < nb_pkts; i++) 360 prepare_tx_pkt(pkts[i], port); 361 } 362 363 /* Send burst of packets on an output interface */ 364 static inline int32_t 365 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port) 366 { 367 struct rte_mbuf **m_table; 368 int32_t ret; 369 uint16_t queueid; 370 371 queueid = qconf->tx_queue_id[port]; 372 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table; 373 374 prepare_tx_burst(m_table, n, port); 375 376 ret = rte_eth_tx_burst(port, queueid, m_table, n); 377 if (unlikely(ret < n)) { 378 do { 379 rte_pktmbuf_free(m_table[ret]); 380 } while (++ret < n); 381 } 382 383 return 0; 384 } 385 386 /* Enqueue a single packet, and send burst if queue is filled */ 387 static inline int32_t 388 send_single_packet(struct rte_mbuf *m, uint16_t port) 389 { 390 uint32_t lcore_id; 391 uint16_t len; 392 struct lcore_conf *qconf; 393 394 lcore_id = rte_lcore_id(); 395 396 qconf = &lcore_conf[lcore_id]; 397 len = qconf->tx_mbufs[port].len; 398 qconf->tx_mbufs[port].m_table[len] = m; 399 len++; 400 401 /* enough pkts to be sent */ 402 if (unlikely(len == MAX_PKT_BURST)) { 403 send_burst(qconf, MAX_PKT_BURST, port); 404 len = 0; 405 } 406 407 qconf->tx_mbufs[port].len = len; 408 return 0; 409 } 410 411 static inline void 412 inbound_sp_sa(struct sp_ctx *sp, struct sa_ctx *sa, struct traffic_type *ip, 413 uint16_t lim) 414 { 415 struct rte_mbuf *m; 416 uint32_t i, j, res, sa_idx; 417 418 if (ip->num == 0 || sp == NULL) 419 return; 420 421 rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res, 422 ip->num, DEFAULT_MAX_CATEGORIES); 423 424 j = 0; 425 for (i = 0; i < ip->num; i++) { 426 m = ip->pkts[i]; 427 res = ip->res[i]; 428 if (res & BYPASS) { 429 ip->pkts[j++] = m; 430 continue; 431 } 432 if (res & DISCARD) { 433 rte_pktmbuf_free(m); 434 continue; 435 } 436 437 /* Only check SPI match for processed IPSec packets */ 438 if (i < lim && ((m->ol_flags & PKT_RX_SEC_OFFLOAD) == 0)) { 439 rte_pktmbuf_free(m); 440 continue; 441 } 442 443 sa_idx = ip->res[i] & PROTECT_MASK; 444 if (sa_idx >= IPSEC_SA_MAX_ENTRIES || 445 !inbound_sa_check(sa, m, sa_idx)) { 446 rte_pktmbuf_free(m); 447 continue; 448 } 449 ip->pkts[j++] = m; 450 } 451 ip->num = j; 452 } 453 454 static inline void 455 process_pkts_inbound(struct ipsec_ctx *ipsec_ctx, 456 struct ipsec_traffic *traffic) 457 { 458 struct rte_mbuf *m; 459 uint16_t idx, nb_pkts_in, i, n_ip4, n_ip6; 460 461 nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts, 462 traffic->ipsec.num, MAX_PKT_BURST); 463 464 n_ip4 = traffic->ip4.num; 465 n_ip6 = traffic->ip6.num; 466 467 /* SP/ACL Inbound check ipsec and ip4 */ 468 for (i = 0; i < nb_pkts_in; i++) { 469 m = traffic->ipsec.pkts[i]; 470 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *); 471 if (ip->ip_v == IPVERSION) { 472 idx = traffic->ip4.num++; 473 traffic->ip4.pkts[idx] = m; 474 traffic->ip4.data[idx] = rte_pktmbuf_mtod_offset(m, 475 uint8_t *, offsetof(struct ip, ip_p)); 476 } else if (ip->ip_v == IP6_VERSION) { 477 idx = traffic->ip6.num++; 478 traffic->ip6.pkts[idx] = m; 479 traffic->ip6.data[idx] = rte_pktmbuf_mtod_offset(m, 480 uint8_t *, 481 offsetof(struct ip6_hdr, ip6_nxt)); 482 } else 483 rte_pktmbuf_free(m); 484 } 485 486 inbound_sp_sa(ipsec_ctx->sp4_ctx, ipsec_ctx->sa_ctx, &traffic->ip4, 487 n_ip4); 488 489 inbound_sp_sa(ipsec_ctx->sp6_ctx, ipsec_ctx->sa_ctx, &traffic->ip6, 490 n_ip6); 491 } 492 493 static inline void 494 outbound_sp(struct sp_ctx *sp, struct traffic_type *ip, 495 struct traffic_type *ipsec) 496 { 497 struct rte_mbuf *m; 498 uint32_t i, j, sa_idx; 499 500 if (ip->num == 0 || sp == NULL) 501 return; 502 503 rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res, 504 ip->num, DEFAULT_MAX_CATEGORIES); 505 506 j = 0; 507 for (i = 0; i < ip->num; i++) { 508 m = ip->pkts[i]; 509 sa_idx = ip->res[i] & PROTECT_MASK; 510 if (ip->res[i] & DISCARD) 511 rte_pktmbuf_free(m); 512 else if (sa_idx < IPSEC_SA_MAX_ENTRIES) { 513 ipsec->res[ipsec->num] = sa_idx; 514 ipsec->pkts[ipsec->num++] = m; 515 } else /* BYPASS */ 516 ip->pkts[j++] = m; 517 } 518 ip->num = j; 519 } 520 521 static inline void 522 process_pkts_outbound(struct ipsec_ctx *ipsec_ctx, 523 struct ipsec_traffic *traffic) 524 { 525 struct rte_mbuf *m; 526 uint16_t idx, nb_pkts_out, i; 527 528 /* Drop any IPsec traffic from protected ports */ 529 for (i = 0; i < traffic->ipsec.num; i++) 530 rte_pktmbuf_free(traffic->ipsec.pkts[i]); 531 532 traffic->ipsec.num = 0; 533 534 outbound_sp(ipsec_ctx->sp4_ctx, &traffic->ip4, &traffic->ipsec); 535 536 outbound_sp(ipsec_ctx->sp6_ctx, &traffic->ip6, &traffic->ipsec); 537 538 nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ipsec.pkts, 539 traffic->ipsec.res, traffic->ipsec.num, 540 MAX_PKT_BURST); 541 542 for (i = 0; i < nb_pkts_out; i++) { 543 m = traffic->ipsec.pkts[i]; 544 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *); 545 if (ip->ip_v == IPVERSION) { 546 idx = traffic->ip4.num++; 547 traffic->ip4.pkts[idx] = m; 548 } else { 549 idx = traffic->ip6.num++; 550 traffic->ip6.pkts[idx] = m; 551 } 552 } 553 } 554 555 static inline void 556 process_pkts_inbound_nosp(struct ipsec_ctx *ipsec_ctx, 557 struct ipsec_traffic *traffic) 558 { 559 struct rte_mbuf *m; 560 uint32_t nb_pkts_in, i, idx; 561 562 /* Drop any IPv4 traffic from unprotected ports */ 563 for (i = 0; i < traffic->ip4.num; i++) 564 rte_pktmbuf_free(traffic->ip4.pkts[i]); 565 566 traffic->ip4.num = 0; 567 568 /* Drop any IPv6 traffic from unprotected ports */ 569 for (i = 0; i < traffic->ip6.num; i++) 570 rte_pktmbuf_free(traffic->ip6.pkts[i]); 571 572 traffic->ip6.num = 0; 573 574 nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts, 575 traffic->ipsec.num, MAX_PKT_BURST); 576 577 for (i = 0; i < nb_pkts_in; i++) { 578 m = traffic->ipsec.pkts[i]; 579 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *); 580 if (ip->ip_v == IPVERSION) { 581 idx = traffic->ip4.num++; 582 traffic->ip4.pkts[idx] = m; 583 } else { 584 idx = traffic->ip6.num++; 585 traffic->ip6.pkts[idx] = m; 586 } 587 } 588 } 589 590 static inline void 591 process_pkts_outbound_nosp(struct ipsec_ctx *ipsec_ctx, 592 struct ipsec_traffic *traffic) 593 { 594 struct rte_mbuf *m; 595 uint32_t nb_pkts_out, i; 596 struct ip *ip; 597 598 /* Drop any IPsec traffic from protected ports */ 599 for (i = 0; i < traffic->ipsec.num; i++) 600 rte_pktmbuf_free(traffic->ipsec.pkts[i]); 601 602 traffic->ipsec.num = 0; 603 604 for (i = 0; i < traffic->ip4.num; i++) 605 traffic->ip4.res[i] = single_sa_idx; 606 607 for (i = 0; i < traffic->ip6.num; i++) 608 traffic->ip6.res[i] = single_sa_idx; 609 610 nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ip4.pkts, 611 traffic->ip4.res, traffic->ip4.num, 612 MAX_PKT_BURST); 613 614 /* They all sue the same SA (ip4 or ip6 tunnel) */ 615 m = traffic->ipsec.pkts[i]; 616 ip = rte_pktmbuf_mtod(m, struct ip *); 617 if (ip->ip_v == IPVERSION) 618 traffic->ip4.num = nb_pkts_out; 619 else 620 traffic->ip6.num = nb_pkts_out; 621 } 622 623 static inline int32_t 624 get_hop_for_offload_pkt(struct rte_mbuf *pkt, int is_ipv6) 625 { 626 struct ipsec_mbuf_metadata *priv; 627 struct ipsec_sa *sa; 628 629 priv = get_priv(pkt); 630 631 sa = priv->sa; 632 if (unlikely(sa == NULL)) { 633 RTE_LOG(ERR, IPSEC, "SA not saved in private data\n"); 634 goto fail; 635 } 636 637 if (is_ipv6) 638 return sa->portid; 639 640 /* else */ 641 return (sa->portid | RTE_LPM_LOOKUP_SUCCESS); 642 643 fail: 644 if (is_ipv6) 645 return -1; 646 647 /* else */ 648 return 0; 649 } 650 651 static inline void 652 route4_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts) 653 { 654 uint32_t hop[MAX_PKT_BURST * 2]; 655 uint32_t dst_ip[MAX_PKT_BURST * 2]; 656 int32_t pkt_hop = 0; 657 uint16_t i, offset; 658 uint16_t lpm_pkts = 0; 659 660 if (nb_pkts == 0) 661 return; 662 663 /* Need to do an LPM lookup for non-inline packets. Inline packets will 664 * have port ID in the SA 665 */ 666 667 for (i = 0; i < nb_pkts; i++) { 668 if (!(pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD)) { 669 /* Security offload not enabled. So an LPM lookup is 670 * required to get the hop 671 */ 672 offset = offsetof(struct ip, ip_dst); 673 dst_ip[lpm_pkts] = *rte_pktmbuf_mtod_offset(pkts[i], 674 uint32_t *, offset); 675 dst_ip[lpm_pkts] = rte_be_to_cpu_32(dst_ip[lpm_pkts]); 676 lpm_pkts++; 677 } 678 } 679 680 rte_lpm_lookup_bulk((struct rte_lpm *)rt_ctx, dst_ip, hop, lpm_pkts); 681 682 lpm_pkts = 0; 683 684 for (i = 0; i < nb_pkts; i++) { 685 if (pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD) { 686 /* Read hop from the SA */ 687 pkt_hop = get_hop_for_offload_pkt(pkts[i], 0); 688 } else { 689 /* Need to use hop returned by lookup */ 690 pkt_hop = hop[lpm_pkts++]; 691 } 692 693 if ((pkt_hop & RTE_LPM_LOOKUP_SUCCESS) == 0) { 694 rte_pktmbuf_free(pkts[i]); 695 continue; 696 } 697 send_single_packet(pkts[i], pkt_hop & 0xff); 698 } 699 } 700 701 static inline void 702 route6_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts) 703 { 704 int32_t hop[MAX_PKT_BURST * 2]; 705 uint8_t dst_ip[MAX_PKT_BURST * 2][16]; 706 uint8_t *ip6_dst; 707 int32_t pkt_hop = 0; 708 uint16_t i, offset; 709 uint16_t lpm_pkts = 0; 710 711 if (nb_pkts == 0) 712 return; 713 714 /* Need to do an LPM lookup for non-inline packets. Inline packets will 715 * have port ID in the SA 716 */ 717 718 for (i = 0; i < nb_pkts; i++) { 719 if (!(pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD)) { 720 /* Security offload not enabled. So an LPM lookup is 721 * required to get the hop 722 */ 723 offset = offsetof(struct ip6_hdr, ip6_dst); 724 ip6_dst = rte_pktmbuf_mtod_offset(pkts[i], uint8_t *, 725 offset); 726 memcpy(&dst_ip[lpm_pkts][0], ip6_dst, 16); 727 lpm_pkts++; 728 } 729 } 730 731 rte_lpm6_lookup_bulk_func((struct rte_lpm6 *)rt_ctx, dst_ip, hop, 732 lpm_pkts); 733 734 lpm_pkts = 0; 735 736 for (i = 0; i < nb_pkts; i++) { 737 if (pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD) { 738 /* Read hop from the SA */ 739 pkt_hop = get_hop_for_offload_pkt(pkts[i], 1); 740 } else { 741 /* Need to use hop returned by lookup */ 742 pkt_hop = hop[lpm_pkts++]; 743 } 744 745 if (pkt_hop == -1) { 746 rte_pktmbuf_free(pkts[i]); 747 continue; 748 } 749 send_single_packet(pkts[i], pkt_hop & 0xff); 750 } 751 } 752 753 static inline void 754 process_pkts(struct lcore_conf *qconf, struct rte_mbuf **pkts, 755 uint8_t nb_pkts, uint16_t portid) 756 { 757 struct ipsec_traffic traffic; 758 759 prepare_traffic(pkts, &traffic, nb_pkts); 760 761 if (unlikely(single_sa)) { 762 if (UNPROTECTED_PORT(portid)) 763 process_pkts_inbound_nosp(&qconf->inbound, &traffic); 764 else 765 process_pkts_outbound_nosp(&qconf->outbound, &traffic); 766 } else { 767 if (UNPROTECTED_PORT(portid)) 768 process_pkts_inbound(&qconf->inbound, &traffic); 769 else 770 process_pkts_outbound(&qconf->outbound, &traffic); 771 } 772 773 route4_pkts(qconf->rt4_ctx, traffic.ip4.pkts, traffic.ip4.num); 774 route6_pkts(qconf->rt6_ctx, traffic.ip6.pkts, traffic.ip6.num); 775 } 776 777 static inline void 778 drain_buffers(struct lcore_conf *qconf) 779 { 780 struct buffer *buf; 781 uint32_t portid; 782 783 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) { 784 buf = &qconf->tx_mbufs[portid]; 785 if (buf->len == 0) 786 continue; 787 send_burst(qconf, buf->len, portid); 788 buf->len = 0; 789 } 790 } 791 792 /* main processing loop */ 793 static int32_t 794 main_loop(__attribute__((unused)) void *dummy) 795 { 796 struct rte_mbuf *pkts[MAX_PKT_BURST]; 797 uint32_t lcore_id; 798 uint64_t prev_tsc, diff_tsc, cur_tsc; 799 int32_t i, nb_rx; 800 uint16_t portid; 801 uint8_t queueid; 802 struct lcore_conf *qconf; 803 int32_t socket_id; 804 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) 805 / US_PER_S * BURST_TX_DRAIN_US; 806 struct lcore_rx_queue *rxql; 807 808 prev_tsc = 0; 809 lcore_id = rte_lcore_id(); 810 qconf = &lcore_conf[lcore_id]; 811 rxql = qconf->rx_queue_list; 812 socket_id = rte_lcore_to_socket_id(lcore_id); 813 814 qconf->rt4_ctx = socket_ctx[socket_id].rt_ip4; 815 qconf->rt6_ctx = socket_ctx[socket_id].rt_ip6; 816 qconf->inbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_in; 817 qconf->inbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_in; 818 qconf->inbound.sa_ctx = socket_ctx[socket_id].sa_in; 819 qconf->inbound.cdev_map = cdev_map_in; 820 qconf->inbound.session_pool = socket_ctx[socket_id].session_pool; 821 qconf->outbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_out; 822 qconf->outbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_out; 823 qconf->outbound.sa_ctx = socket_ctx[socket_id].sa_out; 824 qconf->outbound.cdev_map = cdev_map_out; 825 qconf->outbound.session_pool = socket_ctx[socket_id].session_pool; 826 827 if (qconf->nb_rx_queue == 0) { 828 RTE_LOG(INFO, IPSEC, "lcore %u has nothing to do\n", lcore_id); 829 return 0; 830 } 831 832 RTE_LOG(INFO, IPSEC, "entering main loop on lcore %u\n", lcore_id); 833 834 for (i = 0; i < qconf->nb_rx_queue; i++) { 835 portid = rxql[i].port_id; 836 queueid = rxql[i].queue_id; 837 RTE_LOG(INFO, IPSEC, 838 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n", 839 lcore_id, portid, queueid); 840 } 841 842 while (1) { 843 cur_tsc = rte_rdtsc(); 844 845 /* TX queue buffer drain */ 846 diff_tsc = cur_tsc - prev_tsc; 847 848 if (unlikely(diff_tsc > drain_tsc)) { 849 drain_buffers(qconf); 850 prev_tsc = cur_tsc; 851 } 852 853 /* Read packet from RX queues */ 854 for (i = 0; i < qconf->nb_rx_queue; ++i) { 855 portid = rxql[i].port_id; 856 queueid = rxql[i].queue_id; 857 nb_rx = rte_eth_rx_burst(portid, queueid, 858 pkts, MAX_PKT_BURST); 859 860 if (nb_rx > 0) 861 process_pkts(qconf, pkts, nb_rx, portid); 862 } 863 } 864 } 865 866 static int32_t 867 check_params(void) 868 { 869 uint8_t lcore; 870 uint16_t portid; 871 uint16_t i; 872 int32_t socket_id; 873 874 if (lcore_params == NULL) { 875 printf("Error: No port/queue/core mappings\n"); 876 return -1; 877 } 878 879 for (i = 0; i < nb_lcore_params; ++i) { 880 lcore = lcore_params[i].lcore_id; 881 if (!rte_lcore_is_enabled(lcore)) { 882 printf("error: lcore %hhu is not enabled in " 883 "lcore mask\n", lcore); 884 return -1; 885 } 886 socket_id = rte_lcore_to_socket_id(lcore); 887 if (socket_id != 0 && numa_on == 0) { 888 printf("warning: lcore %hhu is on socket %d " 889 "with numa off\n", 890 lcore, socket_id); 891 } 892 portid = lcore_params[i].port_id; 893 if ((enabled_port_mask & (1 << portid)) == 0) { 894 printf("port %u is not enabled in port mask\n", portid); 895 return -1; 896 } 897 if (!rte_eth_dev_is_valid_port(portid)) { 898 printf("port %u is not present on the board\n", portid); 899 return -1; 900 } 901 } 902 return 0; 903 } 904 905 static uint8_t 906 get_port_nb_rx_queues(const uint16_t port) 907 { 908 int32_t queue = -1; 909 uint16_t i; 910 911 for (i = 0; i < nb_lcore_params; ++i) { 912 if (lcore_params[i].port_id == port && 913 lcore_params[i].queue_id > queue) 914 queue = lcore_params[i].queue_id; 915 } 916 return (uint8_t)(++queue); 917 } 918 919 static int32_t 920 init_lcore_rx_queues(void) 921 { 922 uint16_t i, nb_rx_queue; 923 uint8_t lcore; 924 925 for (i = 0; i < nb_lcore_params; ++i) { 926 lcore = lcore_params[i].lcore_id; 927 nb_rx_queue = lcore_conf[lcore].nb_rx_queue; 928 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) { 929 printf("error: too many queues (%u) for lcore: %u\n", 930 nb_rx_queue + 1, lcore); 931 return -1; 932 } 933 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id = 934 lcore_params[i].port_id; 935 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id = 936 lcore_params[i].queue_id; 937 lcore_conf[lcore].nb_rx_queue++; 938 } 939 return 0; 940 } 941 942 /* display usage */ 943 static void 944 print_usage(const char *prgname) 945 { 946 fprintf(stderr, "%s [EAL options] --" 947 " -p PORTMASK" 948 " [-P]" 949 " [-u PORTMASK]" 950 " [-j FRAMESIZE]" 951 " -f CONFIG_FILE" 952 " --config (port,queue,lcore)[,(port,queue,lcore)]" 953 " [--single-sa SAIDX]" 954 " [--cryptodev_mask MASK]" 955 "\n\n" 956 " -p PORTMASK: Hexadecimal bitmask of ports to configure\n" 957 " -P : Enable promiscuous mode\n" 958 " -u PORTMASK: Hexadecimal bitmask of unprotected ports\n" 959 " -j FRAMESIZE: Enable jumbo frame with 'FRAMESIZE' as maximum\n" 960 " packet size\n" 961 " -f CONFIG_FILE: Configuration file\n" 962 " --config (port,queue,lcore): Rx queue configuration\n" 963 " --single-sa SAIDX: Use single SA index for outbound traffic,\n" 964 " bypassing the SP\n" 965 " --cryptodev_mask MASK: Hexadecimal bitmask of the crypto\n" 966 " devices to configure\n" 967 "\n", 968 prgname); 969 } 970 971 static int32_t 972 parse_portmask(const char *portmask) 973 { 974 char *end = NULL; 975 unsigned long pm; 976 977 /* parse hexadecimal string */ 978 pm = strtoul(portmask, &end, 16); 979 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0')) 980 return -1; 981 982 if ((pm == 0) && errno) 983 return -1; 984 985 return pm; 986 } 987 988 static int32_t 989 parse_decimal(const char *str) 990 { 991 char *end = NULL; 992 unsigned long num; 993 994 num = strtoul(str, &end, 10); 995 if ((str[0] == '\0') || (end == NULL) || (*end != '\0')) 996 return -1; 997 998 return num; 999 } 1000 1001 static int32_t 1002 parse_config(const char *q_arg) 1003 { 1004 char s[256]; 1005 const char *p, *p0 = q_arg; 1006 char *end; 1007 enum fieldnames { 1008 FLD_PORT = 0, 1009 FLD_QUEUE, 1010 FLD_LCORE, 1011 _NUM_FLD 1012 }; 1013 unsigned long int_fld[_NUM_FLD]; 1014 char *str_fld[_NUM_FLD]; 1015 int32_t i; 1016 uint32_t size; 1017 1018 nb_lcore_params = 0; 1019 1020 while ((p = strchr(p0, '(')) != NULL) { 1021 ++p; 1022 p0 = strchr(p, ')'); 1023 if (p0 == NULL) 1024 return -1; 1025 1026 size = p0 - p; 1027 if (size >= sizeof(s)) 1028 return -1; 1029 1030 snprintf(s, sizeof(s), "%.*s", size, p); 1031 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != 1032 _NUM_FLD) 1033 return -1; 1034 for (i = 0; i < _NUM_FLD; i++) { 1035 errno = 0; 1036 int_fld[i] = strtoul(str_fld[i], &end, 0); 1037 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255) 1038 return -1; 1039 } 1040 if (nb_lcore_params >= MAX_LCORE_PARAMS) { 1041 printf("exceeded max number of lcore params: %hu\n", 1042 nb_lcore_params); 1043 return -1; 1044 } 1045 lcore_params_array[nb_lcore_params].port_id = 1046 (uint8_t)int_fld[FLD_PORT]; 1047 lcore_params_array[nb_lcore_params].queue_id = 1048 (uint8_t)int_fld[FLD_QUEUE]; 1049 lcore_params_array[nb_lcore_params].lcore_id = 1050 (uint8_t)int_fld[FLD_LCORE]; 1051 ++nb_lcore_params; 1052 } 1053 lcore_params = lcore_params_array; 1054 return 0; 1055 } 1056 1057 static int32_t 1058 parse_args(int32_t argc, char **argv) 1059 { 1060 int32_t opt, ret; 1061 char **argvopt; 1062 int32_t option_index; 1063 char *prgname = argv[0]; 1064 int32_t f_present = 0; 1065 1066 argvopt = argv; 1067 1068 while ((opt = getopt_long(argc, argvopt, "p:Pu:f:j:", 1069 lgopts, &option_index)) != EOF) { 1070 1071 switch (opt) { 1072 case 'p': 1073 enabled_port_mask = parse_portmask(optarg); 1074 if (enabled_port_mask == 0) { 1075 printf("invalid portmask\n"); 1076 print_usage(prgname); 1077 return -1; 1078 } 1079 break; 1080 case 'P': 1081 printf("Promiscuous mode selected\n"); 1082 promiscuous_on = 1; 1083 break; 1084 case 'u': 1085 unprotected_port_mask = parse_portmask(optarg); 1086 if (unprotected_port_mask == 0) { 1087 printf("invalid unprotected portmask\n"); 1088 print_usage(prgname); 1089 return -1; 1090 } 1091 break; 1092 case 'f': 1093 if (f_present == 1) { 1094 printf("\"-f\" option present more than " 1095 "once!\n"); 1096 print_usage(prgname); 1097 return -1; 1098 } 1099 if (parse_cfg_file(optarg) < 0) { 1100 printf("parsing file \"%s\" failed\n", 1101 optarg); 1102 print_usage(prgname); 1103 return -1; 1104 } 1105 f_present = 1; 1106 break; 1107 case 'j': 1108 { 1109 int32_t size = parse_decimal(optarg); 1110 if (size <= 1518) { 1111 printf("Invalid jumbo frame size\n"); 1112 if (size < 0) { 1113 print_usage(prgname); 1114 return -1; 1115 } 1116 printf("Using default value 9000\n"); 1117 frame_size = 9000; 1118 } else { 1119 frame_size = size; 1120 } 1121 } 1122 printf("Enabled jumbo frames size %u\n", frame_size); 1123 break; 1124 case CMD_LINE_OPT_CONFIG_NUM: 1125 ret = parse_config(optarg); 1126 if (ret) { 1127 printf("Invalid config\n"); 1128 print_usage(prgname); 1129 return -1; 1130 } 1131 break; 1132 case CMD_LINE_OPT_SINGLE_SA_NUM: 1133 ret = parse_decimal(optarg); 1134 if (ret == -1) { 1135 printf("Invalid argument[sa_idx]\n"); 1136 print_usage(prgname); 1137 return -1; 1138 } 1139 1140 /* else */ 1141 single_sa = 1; 1142 single_sa_idx = ret; 1143 printf("Configured with single SA index %u\n", 1144 single_sa_idx); 1145 break; 1146 case CMD_LINE_OPT_CRYPTODEV_MASK_NUM: 1147 ret = parse_portmask(optarg); 1148 if (ret == -1) { 1149 printf("Invalid argument[portmask]\n"); 1150 print_usage(prgname); 1151 return -1; 1152 } 1153 1154 /* else */ 1155 enabled_cryptodev_mask = ret; 1156 break; 1157 default: 1158 print_usage(prgname); 1159 return -1; 1160 } 1161 } 1162 1163 if (f_present == 0) { 1164 printf("Mandatory option \"-f\" not present\n"); 1165 return -1; 1166 } 1167 1168 if (optind >= 0) 1169 argv[optind-1] = prgname; 1170 1171 ret = optind-1; 1172 optind = 1; /* reset getopt lib */ 1173 return ret; 1174 } 1175 1176 static void 1177 print_ethaddr(const char *name, const struct ether_addr *eth_addr) 1178 { 1179 char buf[ETHER_ADDR_FMT_SIZE]; 1180 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr); 1181 printf("%s%s", name, buf); 1182 } 1183 1184 /* Check the link status of all ports in up to 9s, and print them finally */ 1185 static void 1186 check_all_ports_link_status(uint32_t port_mask) 1187 { 1188 #define CHECK_INTERVAL 100 /* 100ms */ 1189 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */ 1190 uint16_t portid; 1191 uint8_t count, all_ports_up, print_flag = 0; 1192 struct rte_eth_link link; 1193 1194 printf("\nChecking link status"); 1195 fflush(stdout); 1196 for (count = 0; count <= MAX_CHECK_TIME; count++) { 1197 all_ports_up = 1; 1198 RTE_ETH_FOREACH_DEV(portid) { 1199 if ((port_mask & (1 << portid)) == 0) 1200 continue; 1201 memset(&link, 0, sizeof(link)); 1202 rte_eth_link_get_nowait(portid, &link); 1203 /* print link status if flag set */ 1204 if (print_flag == 1) { 1205 if (link.link_status) 1206 printf( 1207 "Port%d Link Up - speed %u Mbps -%s\n", 1208 portid, link.link_speed, 1209 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ? 1210 ("full-duplex") : ("half-duplex\n")); 1211 else 1212 printf("Port %d Link Down\n", portid); 1213 continue; 1214 } 1215 /* clear all_ports_up flag if any link down */ 1216 if (link.link_status == ETH_LINK_DOWN) { 1217 all_ports_up = 0; 1218 break; 1219 } 1220 } 1221 /* after finally printing all link status, get out */ 1222 if (print_flag == 1) 1223 break; 1224 1225 if (all_ports_up == 0) { 1226 printf("."); 1227 fflush(stdout); 1228 rte_delay_ms(CHECK_INTERVAL); 1229 } 1230 1231 /* set the print_flag if all ports up or timeout */ 1232 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) { 1233 print_flag = 1; 1234 printf("done\n"); 1235 } 1236 } 1237 } 1238 1239 static int32_t 1240 add_mapping(struct rte_hash *map, const char *str, uint16_t cdev_id, 1241 uint16_t qp, struct lcore_params *params, 1242 struct ipsec_ctx *ipsec_ctx, 1243 const struct rte_cryptodev_capabilities *cipher, 1244 const struct rte_cryptodev_capabilities *auth, 1245 const struct rte_cryptodev_capabilities *aead) 1246 { 1247 int32_t ret = 0; 1248 unsigned long i; 1249 struct cdev_key key = { 0 }; 1250 1251 key.lcore_id = params->lcore_id; 1252 if (cipher) 1253 key.cipher_algo = cipher->sym.cipher.algo; 1254 if (auth) 1255 key.auth_algo = auth->sym.auth.algo; 1256 if (aead) 1257 key.aead_algo = aead->sym.aead.algo; 1258 1259 ret = rte_hash_lookup(map, &key); 1260 if (ret != -ENOENT) 1261 return 0; 1262 1263 for (i = 0; i < ipsec_ctx->nb_qps; i++) 1264 if (ipsec_ctx->tbl[i].id == cdev_id) 1265 break; 1266 1267 if (i == ipsec_ctx->nb_qps) { 1268 if (ipsec_ctx->nb_qps == MAX_QP_PER_LCORE) { 1269 printf("Maximum number of crypto devices assigned to " 1270 "a core, increase MAX_QP_PER_LCORE value\n"); 1271 return 0; 1272 } 1273 ipsec_ctx->tbl[i].id = cdev_id; 1274 ipsec_ctx->tbl[i].qp = qp; 1275 ipsec_ctx->nb_qps++; 1276 printf("%s cdev mapping: lcore %u using cdev %u qp %u " 1277 "(cdev_id_qp %lu)\n", str, key.lcore_id, 1278 cdev_id, qp, i); 1279 } 1280 1281 ret = rte_hash_add_key_data(map, &key, (void *)i); 1282 if (ret < 0) { 1283 printf("Faled to insert cdev mapping for (lcore %u, " 1284 "cdev %u, qp %u), errno %d\n", 1285 key.lcore_id, ipsec_ctx->tbl[i].id, 1286 ipsec_ctx->tbl[i].qp, ret); 1287 return 0; 1288 } 1289 1290 return 1; 1291 } 1292 1293 static int32_t 1294 add_cdev_mapping(struct rte_cryptodev_info *dev_info, uint16_t cdev_id, 1295 uint16_t qp, struct lcore_params *params) 1296 { 1297 int32_t ret = 0; 1298 const struct rte_cryptodev_capabilities *i, *j; 1299 struct rte_hash *map; 1300 struct lcore_conf *qconf; 1301 struct ipsec_ctx *ipsec_ctx; 1302 const char *str; 1303 1304 qconf = &lcore_conf[params->lcore_id]; 1305 1306 if ((unprotected_port_mask & (1 << params->port_id)) == 0) { 1307 map = cdev_map_out; 1308 ipsec_ctx = &qconf->outbound; 1309 str = "Outbound"; 1310 } else { 1311 map = cdev_map_in; 1312 ipsec_ctx = &qconf->inbound; 1313 str = "Inbound"; 1314 } 1315 1316 /* Required cryptodevs with operation chainning */ 1317 if (!(dev_info->feature_flags & 1318 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING)) 1319 return ret; 1320 1321 for (i = dev_info->capabilities; 1322 i->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; i++) { 1323 if (i->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC) 1324 continue; 1325 1326 if (i->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) { 1327 ret |= add_mapping(map, str, cdev_id, qp, params, 1328 ipsec_ctx, NULL, NULL, i); 1329 continue; 1330 } 1331 1332 if (i->sym.xform_type != RTE_CRYPTO_SYM_XFORM_CIPHER) 1333 continue; 1334 1335 for (j = dev_info->capabilities; 1336 j->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; j++) { 1337 if (j->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC) 1338 continue; 1339 1340 if (j->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH) 1341 continue; 1342 1343 ret |= add_mapping(map, str, cdev_id, qp, params, 1344 ipsec_ctx, i, j, NULL); 1345 } 1346 } 1347 1348 return ret; 1349 } 1350 1351 /* Check if the device is enabled by cryptodev_mask */ 1352 static int 1353 check_cryptodev_mask(uint8_t cdev_id) 1354 { 1355 if (enabled_cryptodev_mask & (1 << cdev_id)) 1356 return 0; 1357 1358 return -1; 1359 } 1360 1361 static int32_t 1362 cryptodevs_init(void) 1363 { 1364 struct rte_cryptodev_config dev_conf; 1365 struct rte_cryptodev_qp_conf qp_conf; 1366 uint16_t idx, max_nb_qps, qp, i; 1367 int16_t cdev_id, port_id; 1368 struct rte_hash_parameters params = { 0 }; 1369 1370 params.entries = CDEV_MAP_ENTRIES; 1371 params.key_len = sizeof(struct cdev_key); 1372 params.hash_func = rte_jhash; 1373 params.hash_func_init_val = 0; 1374 params.socket_id = rte_socket_id(); 1375 1376 params.name = "cdev_map_in"; 1377 cdev_map_in = rte_hash_create(¶ms); 1378 if (cdev_map_in == NULL) 1379 rte_panic("Failed to create cdev_map hash table, errno = %d\n", 1380 rte_errno); 1381 1382 params.name = "cdev_map_out"; 1383 cdev_map_out = rte_hash_create(¶ms); 1384 if (cdev_map_out == NULL) 1385 rte_panic("Failed to create cdev_map hash table, errno = %d\n", 1386 rte_errno); 1387 1388 printf("lcore/cryptodev/qp mappings:\n"); 1389 1390 uint32_t max_sess_sz = 0, sess_sz; 1391 for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) { 1392 sess_sz = rte_cryptodev_sym_get_private_session_size(cdev_id); 1393 if (sess_sz > max_sess_sz) 1394 max_sess_sz = sess_sz; 1395 } 1396 RTE_ETH_FOREACH_DEV(port_id) { 1397 void *sec_ctx; 1398 1399 if ((enabled_port_mask & (1 << port_id)) == 0) 1400 continue; 1401 1402 sec_ctx = rte_eth_dev_get_sec_ctx(port_id); 1403 if (sec_ctx == NULL) 1404 continue; 1405 1406 sess_sz = rte_security_session_get_size(sec_ctx); 1407 if (sess_sz > max_sess_sz) 1408 max_sess_sz = sess_sz; 1409 } 1410 1411 idx = 0; 1412 for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) { 1413 struct rte_cryptodev_info cdev_info; 1414 1415 if (check_cryptodev_mask((uint8_t)cdev_id)) 1416 continue; 1417 1418 rte_cryptodev_info_get(cdev_id, &cdev_info); 1419 1420 if (nb_lcore_params > cdev_info.max_nb_queue_pairs) 1421 max_nb_qps = cdev_info.max_nb_queue_pairs; 1422 else 1423 max_nb_qps = nb_lcore_params; 1424 1425 qp = 0; 1426 i = 0; 1427 while (qp < max_nb_qps && i < nb_lcore_params) { 1428 if (add_cdev_mapping(&cdev_info, cdev_id, qp, 1429 &lcore_params[idx])) 1430 qp++; 1431 idx++; 1432 idx = idx % nb_lcore_params; 1433 i++; 1434 } 1435 1436 if (qp == 0) 1437 continue; 1438 1439 dev_conf.socket_id = rte_cryptodev_socket_id(cdev_id); 1440 dev_conf.nb_queue_pairs = qp; 1441 1442 uint32_t dev_max_sess = cdev_info.sym.max_nb_sessions; 1443 if (dev_max_sess != 0 && dev_max_sess < (CDEV_MP_NB_OBJS / 2)) 1444 rte_exit(EXIT_FAILURE, 1445 "Device does not support at least %u " 1446 "sessions", CDEV_MP_NB_OBJS / 2); 1447 1448 if (!socket_ctx[dev_conf.socket_id].session_pool) { 1449 char mp_name[RTE_MEMPOOL_NAMESIZE]; 1450 struct rte_mempool *sess_mp; 1451 1452 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE, 1453 "sess_mp_%u", dev_conf.socket_id); 1454 sess_mp = rte_mempool_create(mp_name, 1455 CDEV_MP_NB_OBJS, 1456 max_sess_sz, 1457 CDEV_MP_CACHE_SZ, 1458 0, NULL, NULL, NULL, 1459 NULL, dev_conf.socket_id, 1460 0); 1461 if (sess_mp == NULL) 1462 rte_exit(EXIT_FAILURE, 1463 "Cannot create session pool on socket %d\n", 1464 dev_conf.socket_id); 1465 else 1466 printf("Allocated session pool on socket %d\n", 1467 dev_conf.socket_id); 1468 socket_ctx[dev_conf.socket_id].session_pool = sess_mp; 1469 } 1470 1471 if (rte_cryptodev_configure(cdev_id, &dev_conf)) 1472 rte_panic("Failed to initialize cryptodev %u\n", 1473 cdev_id); 1474 1475 qp_conf.nb_descriptors = CDEV_QUEUE_DESC; 1476 for (qp = 0; qp < dev_conf.nb_queue_pairs; qp++) 1477 if (rte_cryptodev_queue_pair_setup(cdev_id, qp, 1478 &qp_conf, dev_conf.socket_id, 1479 socket_ctx[dev_conf.socket_id].session_pool)) 1480 rte_panic("Failed to setup queue %u for " 1481 "cdev_id %u\n", 0, cdev_id); 1482 1483 if (rte_cryptodev_start(cdev_id)) 1484 rte_panic("Failed to start cryptodev %u\n", 1485 cdev_id); 1486 } 1487 1488 /* create session pools for eth devices that implement security */ 1489 RTE_ETH_FOREACH_DEV(port_id) { 1490 if ((enabled_port_mask & (1 << port_id)) && 1491 rte_eth_dev_get_sec_ctx(port_id)) { 1492 int socket_id = rte_eth_dev_socket_id(port_id); 1493 1494 if (!socket_ctx[socket_id].session_pool) { 1495 char mp_name[RTE_MEMPOOL_NAMESIZE]; 1496 struct rte_mempool *sess_mp; 1497 1498 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE, 1499 "sess_mp_%u", socket_id); 1500 sess_mp = rte_mempool_create(mp_name, 1501 CDEV_MP_NB_OBJS, 1502 max_sess_sz, 1503 CDEV_MP_CACHE_SZ, 1504 0, NULL, NULL, NULL, 1505 NULL, socket_id, 1506 0); 1507 if (sess_mp == NULL) 1508 rte_exit(EXIT_FAILURE, 1509 "Cannot create session pool " 1510 "on socket %d\n", socket_id); 1511 else 1512 printf("Allocated session pool " 1513 "on socket %d\n", socket_id); 1514 socket_ctx[socket_id].session_pool = sess_mp; 1515 } 1516 } 1517 } 1518 1519 1520 printf("\n"); 1521 1522 return 0; 1523 } 1524 1525 static void 1526 port_init(uint16_t portid) 1527 { 1528 struct rte_eth_dev_info dev_info; 1529 struct rte_eth_txconf *txconf; 1530 uint16_t nb_tx_queue, nb_rx_queue; 1531 uint16_t tx_queueid, rx_queueid, queue, lcore_id; 1532 int32_t ret, socket_id; 1533 struct lcore_conf *qconf; 1534 struct ether_addr ethaddr; 1535 struct rte_eth_conf local_port_conf = port_conf; 1536 1537 rte_eth_dev_info_get(portid, &dev_info); 1538 1539 printf("Configuring device port %u:\n", portid); 1540 1541 rte_eth_macaddr_get(portid, ðaddr); 1542 ethaddr_tbl[portid].src = ETHADDR_TO_UINT64(ethaddr); 1543 print_ethaddr("Address: ", ðaddr); 1544 printf("\n"); 1545 1546 nb_rx_queue = get_port_nb_rx_queues(portid); 1547 nb_tx_queue = nb_lcores; 1548 1549 if (nb_rx_queue > dev_info.max_rx_queues) 1550 rte_exit(EXIT_FAILURE, "Error: queue %u not available " 1551 "(max rx queue is %u)\n", 1552 nb_rx_queue, dev_info.max_rx_queues); 1553 1554 if (nb_tx_queue > dev_info.max_tx_queues) 1555 rte_exit(EXIT_FAILURE, "Error: queue %u not available " 1556 "(max tx queue is %u)\n", 1557 nb_tx_queue, dev_info.max_tx_queues); 1558 1559 printf("Creating queues: nb_rx_queue=%d nb_tx_queue=%u...\n", 1560 nb_rx_queue, nb_tx_queue); 1561 1562 if (frame_size) { 1563 local_port_conf.rxmode.max_rx_pkt_len = frame_size; 1564 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME; 1565 } 1566 1567 if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_SECURITY) 1568 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_SECURITY; 1569 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_SECURITY) 1570 local_port_conf.txmode.offloads |= DEV_TX_OFFLOAD_SECURITY; 1571 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE) 1572 local_port_conf.txmode.offloads |= 1573 DEV_TX_OFFLOAD_MBUF_FAST_FREE; 1574 1575 local_port_conf.rx_adv_conf.rss_conf.rss_hf &= 1576 dev_info.flow_type_rss_offloads; 1577 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf != 1578 port_conf.rx_adv_conf.rss_conf.rss_hf) { 1579 printf("Port %u modified RSS hash function based on hardware support," 1580 "requested:%#"PRIx64" configured:%#"PRIx64"\n", 1581 portid, 1582 port_conf.rx_adv_conf.rss_conf.rss_hf, 1583 local_port_conf.rx_adv_conf.rss_conf.rss_hf); 1584 } 1585 1586 ret = rte_eth_dev_configure(portid, nb_rx_queue, nb_tx_queue, 1587 &local_port_conf); 1588 if (ret < 0) 1589 rte_exit(EXIT_FAILURE, "Cannot configure device: " 1590 "err=%d, port=%d\n", ret, portid); 1591 1592 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd); 1593 if (ret < 0) 1594 rte_exit(EXIT_FAILURE, "Cannot adjust number of descriptors: " 1595 "err=%d, port=%d\n", ret, portid); 1596 1597 /* init one TX queue per lcore */ 1598 tx_queueid = 0; 1599 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { 1600 if (rte_lcore_is_enabled(lcore_id) == 0) 1601 continue; 1602 1603 if (numa_on) 1604 socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id); 1605 else 1606 socket_id = 0; 1607 1608 /* init TX queue */ 1609 printf("Setup txq=%u,%d,%d\n", lcore_id, tx_queueid, socket_id); 1610 1611 txconf = &dev_info.default_txconf; 1612 txconf->offloads = local_port_conf.txmode.offloads; 1613 1614 ret = rte_eth_tx_queue_setup(portid, tx_queueid, nb_txd, 1615 socket_id, txconf); 1616 if (ret < 0) 1617 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: " 1618 "err=%d, port=%d\n", ret, portid); 1619 1620 qconf = &lcore_conf[lcore_id]; 1621 qconf->tx_queue_id[portid] = tx_queueid; 1622 tx_queueid++; 1623 1624 /* init RX queues */ 1625 for (queue = 0; queue < qconf->nb_rx_queue; ++queue) { 1626 struct rte_eth_rxconf rxq_conf; 1627 1628 if (portid != qconf->rx_queue_list[queue].port_id) 1629 continue; 1630 1631 rx_queueid = qconf->rx_queue_list[queue].queue_id; 1632 1633 printf("Setup rxq=%d,%d,%d\n", portid, rx_queueid, 1634 socket_id); 1635 1636 rxq_conf = dev_info.default_rxconf; 1637 rxq_conf.offloads = local_port_conf.rxmode.offloads; 1638 ret = rte_eth_rx_queue_setup(portid, rx_queueid, 1639 nb_rxd, socket_id, &rxq_conf, 1640 socket_ctx[socket_id].mbuf_pool); 1641 if (ret < 0) 1642 rte_exit(EXIT_FAILURE, 1643 "rte_eth_rx_queue_setup: err=%d, " 1644 "port=%d\n", ret, portid); 1645 } 1646 } 1647 printf("\n"); 1648 } 1649 1650 static void 1651 pool_init(struct socket_ctx *ctx, int32_t socket_id, uint32_t nb_mbuf) 1652 { 1653 char s[64]; 1654 uint32_t buff_size = frame_size ? (frame_size + RTE_PKTMBUF_HEADROOM) : 1655 RTE_MBUF_DEFAULT_BUF_SIZE; 1656 1657 1658 snprintf(s, sizeof(s), "mbuf_pool_%d", socket_id); 1659 ctx->mbuf_pool = rte_pktmbuf_pool_create(s, nb_mbuf, 1660 MEMPOOL_CACHE_SIZE, ipsec_metadata_size(), 1661 buff_size, 1662 socket_id); 1663 if (ctx->mbuf_pool == NULL) 1664 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n", 1665 socket_id); 1666 else 1667 printf("Allocated mbuf pool on socket %d\n", socket_id); 1668 } 1669 1670 static inline int 1671 inline_ipsec_event_esn_overflow(struct rte_security_ctx *ctx, uint64_t md) 1672 { 1673 struct ipsec_sa *sa; 1674 1675 /* For inline protocol processing, the metadata in the event will 1676 * uniquely identify the security session which raised the event. 1677 * Application would then need the userdata it had registered with the 1678 * security session to process the event. 1679 */ 1680 1681 sa = (struct ipsec_sa *)rte_security_get_userdata(ctx, md); 1682 1683 if (sa == NULL) { 1684 /* userdata could not be retrieved */ 1685 return -1; 1686 } 1687 1688 /* Sequence number over flow. SA need to be re-established */ 1689 RTE_SET_USED(sa); 1690 return 0; 1691 } 1692 1693 static int 1694 inline_ipsec_event_callback(uint16_t port_id, enum rte_eth_event_type type, 1695 void *param, void *ret_param) 1696 { 1697 uint64_t md; 1698 struct rte_eth_event_ipsec_desc *event_desc = NULL; 1699 struct rte_security_ctx *ctx = (struct rte_security_ctx *) 1700 rte_eth_dev_get_sec_ctx(port_id); 1701 1702 RTE_SET_USED(param); 1703 1704 if (type != RTE_ETH_EVENT_IPSEC) 1705 return -1; 1706 1707 event_desc = ret_param; 1708 if (event_desc == NULL) { 1709 printf("Event descriptor not set\n"); 1710 return -1; 1711 } 1712 1713 md = event_desc->metadata; 1714 1715 if (event_desc->subtype == RTE_ETH_EVENT_IPSEC_ESN_OVERFLOW) 1716 return inline_ipsec_event_esn_overflow(ctx, md); 1717 else if (event_desc->subtype >= RTE_ETH_EVENT_IPSEC_MAX) { 1718 printf("Invalid IPsec event reported\n"); 1719 return -1; 1720 } 1721 1722 return -1; 1723 } 1724 1725 int32_t 1726 main(int32_t argc, char **argv) 1727 { 1728 int32_t ret; 1729 uint32_t lcore_id; 1730 uint8_t socket_id; 1731 uint16_t portid; 1732 1733 /* init EAL */ 1734 ret = rte_eal_init(argc, argv); 1735 if (ret < 0) 1736 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n"); 1737 argc -= ret; 1738 argv += ret; 1739 1740 /* parse application arguments (after the EAL ones) */ 1741 ret = parse_args(argc, argv); 1742 if (ret < 0) 1743 rte_exit(EXIT_FAILURE, "Invalid parameters\n"); 1744 1745 if ((unprotected_port_mask & enabled_port_mask) != 1746 unprotected_port_mask) 1747 rte_exit(EXIT_FAILURE, "Invalid unprotected portmask 0x%x\n", 1748 unprotected_port_mask); 1749 1750 if (check_params() < 0) 1751 rte_exit(EXIT_FAILURE, "check_params failed\n"); 1752 1753 ret = init_lcore_rx_queues(); 1754 if (ret < 0) 1755 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n"); 1756 1757 nb_lcores = rte_lcore_count(); 1758 1759 /* Replicate each context per socket */ 1760 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { 1761 if (rte_lcore_is_enabled(lcore_id) == 0) 1762 continue; 1763 1764 if (numa_on) 1765 socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id); 1766 else 1767 socket_id = 0; 1768 1769 if (socket_ctx[socket_id].mbuf_pool) 1770 continue; 1771 1772 sa_init(&socket_ctx[socket_id], socket_id); 1773 1774 sp4_init(&socket_ctx[socket_id], socket_id); 1775 1776 sp6_init(&socket_ctx[socket_id], socket_id); 1777 1778 rt_init(&socket_ctx[socket_id], socket_id); 1779 1780 pool_init(&socket_ctx[socket_id], socket_id, NB_MBUF); 1781 } 1782 1783 RTE_ETH_FOREACH_DEV(portid) { 1784 if ((enabled_port_mask & (1 << portid)) == 0) 1785 continue; 1786 1787 port_init(portid); 1788 } 1789 1790 cryptodevs_init(); 1791 1792 /* start ports */ 1793 RTE_ETH_FOREACH_DEV(portid) { 1794 if ((enabled_port_mask & (1 << portid)) == 0) 1795 continue; 1796 1797 /* Start device */ 1798 ret = rte_eth_dev_start(portid); 1799 if (ret < 0) 1800 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: " 1801 "err=%d, port=%d\n", ret, portid); 1802 /* 1803 * If enabled, put device in promiscuous mode. 1804 * This allows IO forwarding mode to forward packets 1805 * to itself through 2 cross-connected ports of the 1806 * target machine. 1807 */ 1808 if (promiscuous_on) 1809 rte_eth_promiscuous_enable(portid); 1810 1811 rte_eth_dev_callback_register(portid, 1812 RTE_ETH_EVENT_IPSEC, inline_ipsec_event_callback, NULL); 1813 } 1814 1815 check_all_ports_link_status(enabled_port_mask); 1816 1817 /* launch per-lcore init on every lcore */ 1818 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER); 1819 RTE_LCORE_FOREACH_SLAVE(lcore_id) { 1820 if (rte_eal_wait_lcore(lcore_id) < 0) 1821 return -1; 1822 } 1823 1824 return 0; 1825 } 1826