1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation
10 #include <netinet/in.h>
11 #include <netinet/ip.h>
12 #include <netinet/ip6.h>
14 #include <sys/queue.h>
19 #include <rte_common.h>
20 #include <rte_byteorder.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>
41 #include <rte_jhash.h>
42 #include <rte_cryptodev.h>
47 #define RTE_LOGTYPE_IPSEC RTE_LOGTYPE_USER1
49 #define MAX_JUMBO_PKT_LEN 9600
51 #define MEMPOOL_CACHE_SIZE 256
53 #define NB_MBUF (32000)
55 #define CDEV_QUEUE_DESC 2048
56 #define CDEV_MAP_ENTRIES 1024
57 #define CDEV_MP_NB_OBJS 2048
58 #define CDEV_MP_CACHE_SZ 64
59 #define MAX_QUEUE_PAIRS 1
61 #define OPTION_CONFIG "config"
62 #define OPTION_SINGLE_SA "single-sa"
64 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
68 /* Configure how many packets ahead to prefetch, when reading packets */
69 #define PREFETCH_OFFSET 3
71 #define MAX_RX_QUEUE_PER_LCORE 16
73 #define MAX_LCORE_PARAMS 1024
75 #define UNPROTECTED_PORT(port) (unprotected_port_mask & (1 << portid))
78 * Configurable number of RX/TX ring descriptors
80 #define IPSEC_SECGW_RX_DESC_DEFAULT 128
81 #define IPSEC_SECGW_TX_DESC_DEFAULT 512
82 static uint16_t nb_rxd = IPSEC_SECGW_RX_DESC_DEFAULT;
83 static uint16_t nb_txd = IPSEC_SECGW_TX_DESC_DEFAULT;
85 #if RTE_BYTE_ORDER != RTE_LITTLE_ENDIAN
86 #define __BYTES_TO_UINT64(a, b, c, d, e, f, g, h) \
87 (((uint64_t)((a) & 0xff) << 56) | \
88 ((uint64_t)((b) & 0xff) << 48) | \
89 ((uint64_t)((c) & 0xff) << 40) | \
90 ((uint64_t)((d) & 0xff) << 32) | \
91 ((uint64_t)((e) & 0xff) << 24) | \
92 ((uint64_t)((f) & 0xff) << 16) | \
93 ((uint64_t)((g) & 0xff) << 8) | \
94 ((uint64_t)(h) & 0xff))
96 #define __BYTES_TO_UINT64(a, b, c, d, e, f, g, h) \
97 (((uint64_t)((h) & 0xff) << 56) | \
98 ((uint64_t)((g) & 0xff) << 48) | \
99 ((uint64_t)((f) & 0xff) << 40) | \
100 ((uint64_t)((e) & 0xff) << 32) | \
101 ((uint64_t)((d) & 0xff) << 24) | \
102 ((uint64_t)((c) & 0xff) << 16) | \
103 ((uint64_t)((b) & 0xff) << 8) | \
104 ((uint64_t)(a) & 0xff))
106 #define ETHADDR(a, b, c, d, e, f) (__BYTES_TO_UINT64(a, b, c, d, e, f, 0, 0))
108 #define ETHADDR_TO_UINT64(addr) __BYTES_TO_UINT64( \
109 addr.addr_bytes[0], addr.addr_bytes[1], \
110 addr.addr_bytes[2], addr.addr_bytes[3], \
111 addr.addr_bytes[4], addr.addr_bytes[5], \
114 /* port/source ethernet addr and destination ethernet addr */
115 struct ethaddr_info {
119 struct ethaddr_info ethaddr_tbl[RTE_MAX_ETHPORTS] = {
120 { 0, ETHADDR(0x00, 0x16, 0x3e, 0x7e, 0x94, 0x9a) },
121 { 0, ETHADDR(0x00, 0x16, 0x3e, 0x22, 0xa1, 0xd9) },
122 { 0, ETHADDR(0x00, 0x16, 0x3e, 0x08, 0x69, 0x26) },
123 { 0, ETHADDR(0x00, 0x16, 0x3e, 0x49, 0x9e, 0xdd) }
126 /* mask of enabled ports */
127 static uint32_t enabled_port_mask;
128 static uint32_t unprotected_port_mask;
129 static int32_t promiscuous_on = 1;
130 static int32_t numa_on = 1; /**< NUMA is enabled by default. */
131 static uint32_t nb_lcores;
132 static uint32_t single_sa;
133 static uint32_t single_sa_idx;
134 static uint32_t frame_size;
136 struct lcore_rx_queue {
139 } __rte_cache_aligned;
141 struct lcore_params {
145 } __rte_cache_aligned;
147 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
149 static struct lcore_params *lcore_params;
150 static uint16_t nb_lcore_params;
152 static struct rte_hash *cdev_map_in;
153 static struct rte_hash *cdev_map_out;
157 struct rte_mbuf *m_table[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
161 uint16_t nb_rx_queue;
162 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
163 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
164 struct buffer tx_mbufs[RTE_MAX_ETHPORTS];
165 struct ipsec_ctx inbound;
166 struct ipsec_ctx outbound;
167 struct rt_ctx *rt4_ctx;
168 struct rt_ctx *rt6_ctx;
169 } __rte_cache_aligned;
171 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
173 static struct rte_eth_conf port_conf = {
175 .mq_mode = ETH_MQ_RX_RSS,
176 .max_rx_pkt_len = ETHER_MAX_LEN,
178 .offloads = DEV_RX_OFFLOAD_CHECKSUM |
179 DEV_RX_OFFLOAD_CRC_STRIP,
180 .ignore_offload_bitfield = 1,
185 .rss_hf = ETH_RSS_IP | ETH_RSS_UDP |
186 ETH_RSS_TCP | ETH_RSS_SCTP,
190 .mq_mode = ETH_MQ_TX_NONE,
194 static struct socket_ctx socket_ctx[NB_SOCKETS];
196 struct traffic_type {
197 const uint8_t *data[MAX_PKT_BURST * 2];
198 struct rte_mbuf *pkts[MAX_PKT_BURST * 2];
199 uint32_t res[MAX_PKT_BURST * 2];
203 struct ipsec_traffic {
204 struct traffic_type ipsec;
205 struct traffic_type ip4;
206 struct traffic_type ip6;
210 prepare_one_packet(struct rte_mbuf *pkt, struct ipsec_traffic *t)
213 struct ether_hdr *eth;
215 eth = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
216 if (eth->ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4)) {
217 nlp = (uint8_t *)rte_pktmbuf_adj(pkt, ETHER_HDR_LEN);
218 nlp = RTE_PTR_ADD(nlp, offsetof(struct ip, ip_p));
219 if (*nlp == IPPROTO_ESP)
220 t->ipsec.pkts[(t->ipsec.num)++] = pkt;
222 t->ip4.data[t->ip4.num] = nlp;
223 t->ip4.pkts[(t->ip4.num)++] = pkt;
225 } else if (eth->ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6)) {
226 nlp = (uint8_t *)rte_pktmbuf_adj(pkt, ETHER_HDR_LEN);
227 nlp = RTE_PTR_ADD(nlp, offsetof(struct ip6_hdr, ip6_nxt));
228 if (*nlp == IPPROTO_ESP)
229 t->ipsec.pkts[(t->ipsec.num)++] = pkt;
231 t->ip6.data[t->ip6.num] = nlp;
232 t->ip6.pkts[(t->ip6.num)++] = pkt;
235 /* Unknown/Unsupported type, drop the packet */
236 RTE_LOG(ERR, IPSEC, "Unsupported packet type\n");
237 rte_pktmbuf_free(pkt);
242 prepare_traffic(struct rte_mbuf **pkts, struct ipsec_traffic *t,
251 for (i = 0; i < (nb_pkts - PREFETCH_OFFSET); i++) {
252 rte_prefetch0(rte_pktmbuf_mtod(pkts[i + PREFETCH_OFFSET],
254 prepare_one_packet(pkts[i], t);
256 /* Process left packets */
257 for (; i < nb_pkts; i++)
258 prepare_one_packet(pkts[i], t);
262 prepare_tx_pkt(struct rte_mbuf *pkt, uint16_t port)
265 struct ether_hdr *ethhdr;
267 ip = rte_pktmbuf_mtod(pkt, struct ip *);
269 ethhdr = (struct ether_hdr *)rte_pktmbuf_prepend(pkt, ETHER_HDR_LEN);
271 if (ip->ip_v == IPVERSION) {
272 pkt->ol_flags |= PKT_TX_IP_CKSUM | PKT_TX_IPV4;
273 pkt->l3_len = sizeof(struct ip);
274 pkt->l2_len = ETHER_HDR_LEN;
276 ethhdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
278 pkt->ol_flags |= PKT_TX_IPV6;
279 pkt->l3_len = sizeof(struct ip6_hdr);
280 pkt->l2_len = ETHER_HDR_LEN;
282 ethhdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv6);
285 memcpy(ðhdr->s_addr, ðaddr_tbl[port].src,
286 sizeof(struct ether_addr));
287 memcpy(ðhdr->d_addr, ðaddr_tbl[port].dst,
288 sizeof(struct ether_addr));
292 prepare_tx_burst(struct rte_mbuf *pkts[], uint16_t nb_pkts, uint16_t port)
295 const int32_t prefetch_offset = 2;
297 for (i = 0; i < (nb_pkts - prefetch_offset); i++) {
298 rte_mbuf_prefetch_part2(pkts[i + prefetch_offset]);
299 prepare_tx_pkt(pkts[i], port);
301 /* Process left packets */
302 for (; i < nb_pkts; i++)
303 prepare_tx_pkt(pkts[i], port);
306 /* Send burst of packets on an output interface */
307 static inline int32_t
308 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
310 struct rte_mbuf **m_table;
314 queueid = qconf->tx_queue_id[port];
315 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
317 prepare_tx_burst(m_table, n, port);
319 ret = rte_eth_tx_burst(port, queueid, m_table, n);
320 if (unlikely(ret < n)) {
322 rte_pktmbuf_free(m_table[ret]);
329 /* Enqueue a single packet, and send burst if queue is filled */
330 static inline int32_t
331 send_single_packet(struct rte_mbuf *m, uint16_t port)
335 struct lcore_conf *qconf;
337 lcore_id = rte_lcore_id();
339 qconf = &lcore_conf[lcore_id];
340 len = qconf->tx_mbufs[port].len;
341 qconf->tx_mbufs[port].m_table[len] = m;
344 /* enough pkts to be sent */
345 if (unlikely(len == MAX_PKT_BURST)) {
346 send_burst(qconf, MAX_PKT_BURST, port);
350 qconf->tx_mbufs[port].len = len;
355 inbound_sp_sa(struct sp_ctx *sp, struct sa_ctx *sa, struct traffic_type *ip,
359 uint32_t i, j, res, sa_idx;
361 if (ip->num == 0 || sp == NULL)
364 rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
365 ip->num, DEFAULT_MAX_CATEGORIES);
368 for (i = 0; i < ip->num; i++) {
375 if (res & DISCARD || i < lim) {
379 /* Only check SPI match for processed IPSec packets */
380 sa_idx = ip->res[i] & PROTECT_MASK;
381 if (sa_idx == 0 || !inbound_sa_check(sa, m, sa_idx)) {
391 process_pkts_inbound(struct ipsec_ctx *ipsec_ctx,
392 struct ipsec_traffic *traffic)
395 uint16_t idx, nb_pkts_in, i, n_ip4, n_ip6;
397 nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
398 traffic->ipsec.num, MAX_PKT_BURST);
400 n_ip4 = traffic->ip4.num;
401 n_ip6 = traffic->ip6.num;
403 /* SP/ACL Inbound check ipsec and ip4 */
404 for (i = 0; i < nb_pkts_in; i++) {
405 m = traffic->ipsec.pkts[i];
406 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
407 if (ip->ip_v == IPVERSION) {
408 idx = traffic->ip4.num++;
409 traffic->ip4.pkts[idx] = m;
410 traffic->ip4.data[idx] = rte_pktmbuf_mtod_offset(m,
411 uint8_t *, offsetof(struct ip, ip_p));
412 } else if (ip->ip_v == IP6_VERSION) {
413 idx = traffic->ip6.num++;
414 traffic->ip6.pkts[idx] = m;
415 traffic->ip6.data[idx] = rte_pktmbuf_mtod_offset(m,
417 offsetof(struct ip6_hdr, ip6_nxt));
422 inbound_sp_sa(ipsec_ctx->sp4_ctx, ipsec_ctx->sa_ctx, &traffic->ip4,
425 inbound_sp_sa(ipsec_ctx->sp6_ctx, ipsec_ctx->sa_ctx, &traffic->ip6,
430 outbound_sp(struct sp_ctx *sp, struct traffic_type *ip,
431 struct traffic_type *ipsec)
434 uint32_t i, j, sa_idx;
436 if (ip->num == 0 || sp == NULL)
439 rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
440 ip->num, DEFAULT_MAX_CATEGORIES);
443 for (i = 0; i < ip->num; i++) {
445 sa_idx = ip->res[i] & PROTECT_MASK;
446 if ((ip->res[i] == 0) || (ip->res[i] & DISCARD))
448 else if (sa_idx != 0) {
449 ipsec->res[ipsec->num] = sa_idx;
450 ipsec->pkts[ipsec->num++] = m;
458 process_pkts_outbound(struct ipsec_ctx *ipsec_ctx,
459 struct ipsec_traffic *traffic)
462 uint16_t idx, nb_pkts_out, i;
464 /* Drop any IPsec traffic from protected ports */
465 for (i = 0; i < traffic->ipsec.num; i++)
466 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
468 traffic->ipsec.num = 0;
470 outbound_sp(ipsec_ctx->sp4_ctx, &traffic->ip4, &traffic->ipsec);
472 outbound_sp(ipsec_ctx->sp6_ctx, &traffic->ip6, &traffic->ipsec);
474 nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ipsec.pkts,
475 traffic->ipsec.res, traffic->ipsec.num,
478 for (i = 0; i < nb_pkts_out; i++) {
479 m = traffic->ipsec.pkts[i];
480 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
481 if (ip->ip_v == IPVERSION) {
482 idx = traffic->ip4.num++;
483 traffic->ip4.pkts[idx] = m;
485 idx = traffic->ip6.num++;
486 traffic->ip6.pkts[idx] = m;
492 process_pkts_inbound_nosp(struct ipsec_ctx *ipsec_ctx,
493 struct ipsec_traffic *traffic)
496 uint32_t nb_pkts_in, i, idx;
498 /* Drop any IPv4 traffic from unprotected ports */
499 for (i = 0; i < traffic->ip4.num; i++)
500 rte_pktmbuf_free(traffic->ip4.pkts[i]);
502 traffic->ip4.num = 0;
504 /* Drop any IPv6 traffic from unprotected ports */
505 for (i = 0; i < traffic->ip6.num; i++)
506 rte_pktmbuf_free(traffic->ip6.pkts[i]);
508 traffic->ip6.num = 0;
510 nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
511 traffic->ipsec.num, MAX_PKT_BURST);
513 for (i = 0; i < nb_pkts_in; i++) {
514 m = traffic->ipsec.pkts[i];
515 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
516 if (ip->ip_v == IPVERSION) {
517 idx = traffic->ip4.num++;
518 traffic->ip4.pkts[idx] = m;
520 idx = traffic->ip6.num++;
521 traffic->ip6.pkts[idx] = m;
527 process_pkts_outbound_nosp(struct ipsec_ctx *ipsec_ctx,
528 struct ipsec_traffic *traffic)
531 uint32_t nb_pkts_out, i;
534 /* Drop any IPsec traffic from protected ports */
535 for (i = 0; i < traffic->ipsec.num; i++)
536 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
538 traffic->ipsec.num = 0;
540 for (i = 0; i < traffic->ip4.num; i++)
541 traffic->ip4.res[i] = single_sa_idx;
543 for (i = 0; i < traffic->ip6.num; i++)
544 traffic->ip6.res[i] = single_sa_idx;
546 nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ip4.pkts,
547 traffic->ip4.res, traffic->ip4.num,
550 /* They all sue the same SA (ip4 or ip6 tunnel) */
551 m = traffic->ipsec.pkts[i];
552 ip = rte_pktmbuf_mtod(m, struct ip *);
553 if (ip->ip_v == IPVERSION)
554 traffic->ip4.num = nb_pkts_out;
556 traffic->ip6.num = nb_pkts_out;
560 route4_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
562 uint32_t hop[MAX_PKT_BURST * 2];
563 uint32_t dst_ip[MAX_PKT_BURST * 2];
569 for (i = 0; i < nb_pkts; i++) {
570 offset = offsetof(struct ip, ip_dst);
571 dst_ip[i] = *rte_pktmbuf_mtod_offset(pkts[i],
573 dst_ip[i] = rte_be_to_cpu_32(dst_ip[i]);
576 rte_lpm_lookup_bulk((struct rte_lpm *)rt_ctx, dst_ip, hop, nb_pkts);
578 for (i = 0; i < nb_pkts; i++) {
579 if ((hop[i] & RTE_LPM_LOOKUP_SUCCESS) == 0) {
580 rte_pktmbuf_free(pkts[i]);
583 send_single_packet(pkts[i], hop[i] & 0xff);
588 route6_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
590 int32_t hop[MAX_PKT_BURST * 2];
591 uint8_t dst_ip[MAX_PKT_BURST * 2][16];
598 for (i = 0; i < nb_pkts; i++) {
599 offset = offsetof(struct ip6_hdr, ip6_dst);
600 ip6_dst = rte_pktmbuf_mtod_offset(pkts[i], uint8_t *, offset);
601 memcpy(&dst_ip[i][0], ip6_dst, 16);
604 rte_lpm6_lookup_bulk_func((struct rte_lpm6 *)rt_ctx, dst_ip,
607 for (i = 0; i < nb_pkts; i++) {
609 rte_pktmbuf_free(pkts[i]);
612 send_single_packet(pkts[i], hop[i] & 0xff);
617 process_pkts(struct lcore_conf *qconf, struct rte_mbuf **pkts,
618 uint8_t nb_pkts, uint16_t portid)
620 struct ipsec_traffic traffic;
622 prepare_traffic(pkts, &traffic, nb_pkts);
624 if (unlikely(single_sa)) {
625 if (UNPROTECTED_PORT(portid))
626 process_pkts_inbound_nosp(&qconf->inbound, &traffic);
628 process_pkts_outbound_nosp(&qconf->outbound, &traffic);
630 if (UNPROTECTED_PORT(portid))
631 process_pkts_inbound(&qconf->inbound, &traffic);
633 process_pkts_outbound(&qconf->outbound, &traffic);
636 route4_pkts(qconf->rt4_ctx, traffic.ip4.pkts, traffic.ip4.num);
637 route6_pkts(qconf->rt6_ctx, traffic.ip6.pkts, traffic.ip6.num);
641 drain_buffers(struct lcore_conf *qconf)
646 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
647 buf = &qconf->tx_mbufs[portid];
650 send_burst(qconf, buf->len, portid);
655 /* main processing loop */
657 main_loop(__attribute__((unused)) void *dummy)
659 struct rte_mbuf *pkts[MAX_PKT_BURST];
661 uint64_t prev_tsc, diff_tsc, cur_tsc;
665 struct lcore_conf *qconf;
667 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
668 / US_PER_S * BURST_TX_DRAIN_US;
669 struct lcore_rx_queue *rxql;
672 lcore_id = rte_lcore_id();
673 qconf = &lcore_conf[lcore_id];
674 rxql = qconf->rx_queue_list;
675 socket_id = rte_lcore_to_socket_id(lcore_id);
677 qconf->rt4_ctx = socket_ctx[socket_id].rt_ip4;
678 qconf->rt6_ctx = socket_ctx[socket_id].rt_ip6;
679 qconf->inbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_in;
680 qconf->inbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_in;
681 qconf->inbound.sa_ctx = socket_ctx[socket_id].sa_in;
682 qconf->inbound.cdev_map = cdev_map_in;
683 qconf->inbound.session_pool = socket_ctx[socket_id].session_pool;
684 qconf->outbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_out;
685 qconf->outbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_out;
686 qconf->outbound.sa_ctx = socket_ctx[socket_id].sa_out;
687 qconf->outbound.cdev_map = cdev_map_out;
688 qconf->outbound.session_pool = socket_ctx[socket_id].session_pool;
690 if (qconf->nb_rx_queue == 0) {
691 RTE_LOG(INFO, IPSEC, "lcore %u has nothing to do\n", lcore_id);
695 RTE_LOG(INFO, IPSEC, "entering main loop on lcore %u\n", lcore_id);
697 for (i = 0; i < qconf->nb_rx_queue; i++) {
698 portid = rxql[i].port_id;
699 queueid = rxql[i].queue_id;
701 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
702 lcore_id, portid, queueid);
706 cur_tsc = rte_rdtsc();
708 /* TX queue buffer drain */
709 diff_tsc = cur_tsc - prev_tsc;
711 if (unlikely(diff_tsc > drain_tsc)) {
712 drain_buffers(qconf);
716 /* Read packet from RX queues */
717 for (i = 0; i < qconf->nb_rx_queue; ++i) {
718 portid = rxql[i].port_id;
719 queueid = rxql[i].queue_id;
720 nb_rx = rte_eth_rx_burst(portid, queueid,
721 pkts, MAX_PKT_BURST);
724 process_pkts(qconf, pkts, nb_rx, portid);
733 uint16_t portid, nb_ports;
737 if (lcore_params == NULL) {
738 printf("Error: No port/queue/core mappings\n");
742 nb_ports = rte_eth_dev_count();
744 for (i = 0; i < nb_lcore_params; ++i) {
745 lcore = lcore_params[i].lcore_id;
746 if (!rte_lcore_is_enabled(lcore)) {
747 printf("error: lcore %hhu is not enabled in "
748 "lcore mask\n", lcore);
751 socket_id = rte_lcore_to_socket_id(lcore);
752 if (socket_id != 0 && numa_on == 0) {
753 printf("warning: lcore %hhu is on socket %d "
757 portid = lcore_params[i].port_id;
758 if ((enabled_port_mask & (1 << portid)) == 0) {
759 printf("port %u is not enabled in port mask\n", portid);
762 if (portid >= nb_ports) {
763 printf("port %u is not present on the board\n", portid);
771 get_port_nb_rx_queues(const uint16_t port)
776 for (i = 0; i < nb_lcore_params; ++i) {
777 if (lcore_params[i].port_id == port &&
778 lcore_params[i].queue_id > queue)
779 queue = lcore_params[i].queue_id;
781 return (uint8_t)(++queue);
785 init_lcore_rx_queues(void)
787 uint16_t i, nb_rx_queue;
790 for (i = 0; i < nb_lcore_params; ++i) {
791 lcore = lcore_params[i].lcore_id;
792 nb_rx_queue = lcore_conf[lcore].nb_rx_queue;
793 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
794 printf("error: too many queues (%u) for lcore: %u\n",
795 nb_rx_queue + 1, lcore);
798 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
799 lcore_params[i].port_id;
800 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
801 lcore_params[i].queue_id;
802 lcore_conf[lcore].nb_rx_queue++;
809 print_usage(const char *prgname)
811 printf("%s [EAL options] -- -p PORTMASK -P -u PORTMASK"
812 " --"OPTION_CONFIG" (port,queue,lcore)[,(port,queue,lcore]"
813 " --single-sa SAIDX -f CONFIG_FILE\n"
814 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
815 " -P : enable promiscuous mode\n"
816 " -u PORTMASK: hexadecimal bitmask of unprotected ports\n"
817 " -j FRAMESIZE: jumbo frame maximum size\n"
818 " --"OPTION_CONFIG": (port,queue,lcore): "
819 "rx queues configuration\n"
820 " --single-sa SAIDX: use single SA index for outbound, "
822 " -f CONFIG_FILE: Configuration file path\n",
827 parse_portmask(const char *portmask)
832 /* parse hexadecimal string */
833 pm = strtoul(portmask, &end, 16);
834 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
837 if ((pm == 0) && errno)
844 parse_decimal(const char *str)
849 num = strtoul(str, &end, 10);
850 if ((str[0] == '\0') || (end == NULL) || (*end != '\0'))
857 parse_config(const char *q_arg)
860 const char *p, *p0 = q_arg;
868 unsigned long int_fld[_NUM_FLD];
869 char *str_fld[_NUM_FLD];
875 while ((p = strchr(p0, '(')) != NULL) {
882 if (size >= sizeof(s))
885 snprintf(s, sizeof(s), "%.*s", size, p);
886 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
889 for (i = 0; i < _NUM_FLD; i++) {
891 int_fld[i] = strtoul(str_fld[i], &end, 0);
892 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
895 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
896 printf("exceeded max number of lcore params: %hu\n",
900 lcore_params_array[nb_lcore_params].port_id =
901 (uint8_t)int_fld[FLD_PORT];
902 lcore_params_array[nb_lcore_params].queue_id =
903 (uint8_t)int_fld[FLD_QUEUE];
904 lcore_params_array[nb_lcore_params].lcore_id =
905 (uint8_t)int_fld[FLD_LCORE];
908 lcore_params = lcore_params_array;
912 #define __STRNCMP(name, opt) (!strncmp(name, opt, sizeof(opt)))
914 parse_args_long_options(struct option *lgopts, int32_t option_index)
917 const char *optname = lgopts[option_index].name;
919 if (__STRNCMP(optname, OPTION_CONFIG)) {
920 ret = parse_config(optarg);
922 printf("invalid config\n");
925 if (__STRNCMP(optname, OPTION_SINGLE_SA)) {
926 ret = parse_decimal(optarg);
930 printf("Configured with single SA index %u\n",
941 parse_args(int32_t argc, char **argv)
945 int32_t option_index;
946 char *prgname = argv[0];
947 static struct option lgopts[] = {
948 {OPTION_CONFIG, 1, 0, 0},
949 {OPTION_SINGLE_SA, 1, 0, 0},
952 int32_t f_present = 0;
956 while ((opt = getopt_long(argc, argvopt, "p:Pu:f:j:",
957 lgopts, &option_index)) != EOF) {
961 enabled_port_mask = parse_portmask(optarg);
962 if (enabled_port_mask == 0) {
963 printf("invalid portmask\n");
964 print_usage(prgname);
969 printf("Promiscuous mode selected\n");
973 unprotected_port_mask = parse_portmask(optarg);
974 if (unprotected_port_mask == 0) {
975 printf("invalid unprotected portmask\n");
976 print_usage(prgname);
981 if (f_present == 1) {
982 printf("\"-f\" option present more than "
984 print_usage(prgname);
987 if (parse_cfg_file(optarg) < 0) {
988 printf("parsing file \"%s\" failed\n",
990 print_usage(prgname);
997 int32_t size = parse_decimal(optarg);
999 printf("Invalid jumbo frame size\n");
1001 print_usage(prgname);
1004 printf("Using default value 9000\n");
1010 printf("Enabled jumbo frames size %u\n", frame_size);
1013 if (parse_args_long_options(lgopts, option_index)) {
1014 print_usage(prgname);
1019 print_usage(prgname);
1024 if (f_present == 0) {
1025 printf("Mandatory option \"-f\" not present\n");
1030 argv[optind-1] = prgname;
1033 optind = 1; /* reset getopt lib */
1038 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
1040 char buf[ETHER_ADDR_FMT_SIZE];
1041 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
1042 printf("%s%s", name, buf);
1045 /* Check the link status of all ports in up to 9s, and print them finally */
1047 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
1049 #define CHECK_INTERVAL 100 /* 100ms */
1050 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1052 uint8_t count, all_ports_up, print_flag = 0;
1053 struct rte_eth_link link;
1055 printf("\nChecking link status");
1057 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1059 for (portid = 0; portid < port_num; portid++) {
1060 if ((port_mask & (1 << portid)) == 0)
1062 memset(&link, 0, sizeof(link));
1063 rte_eth_link_get_nowait(portid, &link);
1064 /* print link status if flag set */
1065 if (print_flag == 1) {
1066 if (link.link_status)
1068 "Port%d Link Up - speed %u Mbps -%s\n",
1069 portid, link.link_speed,
1070 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1071 ("full-duplex") : ("half-duplex\n"));
1073 printf("Port %d Link Down\n", portid);
1076 /* clear all_ports_up flag if any link down */
1077 if (link.link_status == ETH_LINK_DOWN) {
1082 /* after finally printing all link status, get out */
1083 if (print_flag == 1)
1086 if (all_ports_up == 0) {
1089 rte_delay_ms(CHECK_INTERVAL);
1092 /* set the print_flag if all ports up or timeout */
1093 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1101 add_mapping(struct rte_hash *map, const char *str, uint16_t cdev_id,
1102 uint16_t qp, struct lcore_params *params,
1103 struct ipsec_ctx *ipsec_ctx,
1104 const struct rte_cryptodev_capabilities *cipher,
1105 const struct rte_cryptodev_capabilities *auth,
1106 const struct rte_cryptodev_capabilities *aead)
1110 struct cdev_key key = { 0 };
1112 key.lcore_id = params->lcore_id;
1114 key.cipher_algo = cipher->sym.cipher.algo;
1116 key.auth_algo = auth->sym.auth.algo;
1118 key.aead_algo = aead->sym.aead.algo;
1120 ret = rte_hash_lookup(map, &key);
1124 for (i = 0; i < ipsec_ctx->nb_qps; i++)
1125 if (ipsec_ctx->tbl[i].id == cdev_id)
1128 if (i == ipsec_ctx->nb_qps) {
1129 if (ipsec_ctx->nb_qps == MAX_QP_PER_LCORE) {
1130 printf("Maximum number of crypto devices assigned to "
1131 "a core, increase MAX_QP_PER_LCORE value\n");
1134 ipsec_ctx->tbl[i].id = cdev_id;
1135 ipsec_ctx->tbl[i].qp = qp;
1136 ipsec_ctx->nb_qps++;
1137 printf("%s cdev mapping: lcore %u using cdev %u qp %u "
1138 "(cdev_id_qp %lu)\n", str, key.lcore_id,
1142 ret = rte_hash_add_key_data(map, &key, (void *)i);
1144 printf("Faled to insert cdev mapping for (lcore %u, "
1145 "cdev %u, qp %u), errno %d\n",
1146 key.lcore_id, ipsec_ctx->tbl[i].id,
1147 ipsec_ctx->tbl[i].qp, ret);
1155 add_cdev_mapping(struct rte_cryptodev_info *dev_info, uint16_t cdev_id,
1156 uint16_t qp, struct lcore_params *params)
1159 const struct rte_cryptodev_capabilities *i, *j;
1160 struct rte_hash *map;
1161 struct lcore_conf *qconf;
1162 struct ipsec_ctx *ipsec_ctx;
1165 qconf = &lcore_conf[params->lcore_id];
1167 if ((unprotected_port_mask & (1 << params->port_id)) == 0) {
1169 ipsec_ctx = &qconf->outbound;
1173 ipsec_ctx = &qconf->inbound;
1177 /* Required cryptodevs with operation chainning */
1178 if (!(dev_info->feature_flags &
1179 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING))
1182 for (i = dev_info->capabilities;
1183 i->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; i++) {
1184 if (i->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1187 if (i->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1188 ret |= add_mapping(map, str, cdev_id, qp, params,
1189 ipsec_ctx, NULL, NULL, i);
1193 if (i->sym.xform_type != RTE_CRYPTO_SYM_XFORM_CIPHER)
1196 for (j = dev_info->capabilities;
1197 j->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; j++) {
1198 if (j->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1201 if (j->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH)
1204 ret |= add_mapping(map, str, cdev_id, qp, params,
1205 ipsec_ctx, i, j, NULL);
1213 cryptodevs_init(void)
1215 struct rte_cryptodev_config dev_conf;
1216 struct rte_cryptodev_qp_conf qp_conf;
1217 uint16_t idx, max_nb_qps, qp, i;
1219 struct rte_hash_parameters params = { 0 };
1221 params.entries = CDEV_MAP_ENTRIES;
1222 params.key_len = sizeof(struct cdev_key);
1223 params.hash_func = rte_jhash;
1224 params.hash_func_init_val = 0;
1225 params.socket_id = rte_socket_id();
1227 params.name = "cdev_map_in";
1228 cdev_map_in = rte_hash_create(¶ms);
1229 if (cdev_map_in == NULL)
1230 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1233 params.name = "cdev_map_out";
1234 cdev_map_out = rte_hash_create(¶ms);
1235 if (cdev_map_out == NULL)
1236 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1239 printf("lcore/cryptodev/qp mappings:\n");
1241 uint32_t max_sess_sz = 0, sess_sz;
1242 for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1243 sess_sz = rte_cryptodev_get_private_session_size(cdev_id);
1244 if (sess_sz > max_sess_sz)
1245 max_sess_sz = sess_sz;
1249 /* Start from last cdev id to give HW priority */
1250 for (cdev_id = rte_cryptodev_count() - 1; cdev_id >= 0; cdev_id--) {
1251 struct rte_cryptodev_info cdev_info;
1253 rte_cryptodev_info_get(cdev_id, &cdev_info);
1255 if (nb_lcore_params > cdev_info.max_nb_queue_pairs)
1256 max_nb_qps = cdev_info.max_nb_queue_pairs;
1258 max_nb_qps = nb_lcore_params;
1262 while (qp < max_nb_qps && i < nb_lcore_params) {
1263 if (add_cdev_mapping(&cdev_info, cdev_id, qp,
1264 &lcore_params[idx]))
1267 idx = idx % nb_lcore_params;
1274 dev_conf.socket_id = rte_cryptodev_socket_id(cdev_id);
1275 dev_conf.nb_queue_pairs = qp;
1277 if (!socket_ctx[dev_conf.socket_id].session_pool) {
1278 char mp_name[RTE_MEMPOOL_NAMESIZE];
1279 struct rte_mempool *sess_mp;
1281 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1282 "sess_mp_%u", dev_conf.socket_id);
1283 sess_mp = rte_mempool_create(mp_name,
1287 0, NULL, NULL, NULL,
1288 NULL, dev_conf.socket_id,
1290 if (sess_mp == NULL)
1291 rte_exit(EXIT_FAILURE,
1292 "Cannot create session pool on socket %d\n",
1293 dev_conf.socket_id);
1295 printf("Allocated session pool on socket %d\n",
1296 dev_conf.socket_id);
1297 socket_ctx[dev_conf.socket_id].session_pool = sess_mp;
1300 if (rte_cryptodev_configure(cdev_id, &dev_conf))
1301 rte_panic("Failed to initialize cryptodev %u\n",
1304 qp_conf.nb_descriptors = CDEV_QUEUE_DESC;
1305 for (qp = 0; qp < dev_conf.nb_queue_pairs; qp++)
1306 if (rte_cryptodev_queue_pair_setup(cdev_id, qp,
1307 &qp_conf, dev_conf.socket_id,
1308 socket_ctx[dev_conf.socket_id].session_pool))
1309 rte_panic("Failed to setup queue %u for "
1310 "cdev_id %u\n", 0, cdev_id);
1312 if (rte_cryptodev_start(cdev_id))
1313 rte_panic("Failed to start cryptodev %u\n",
1323 port_init(uint16_t portid)
1325 struct rte_eth_dev_info dev_info;
1326 struct rte_eth_txconf *txconf;
1327 uint16_t nb_tx_queue, nb_rx_queue;
1328 uint16_t tx_queueid, rx_queueid, queue, lcore_id;
1329 int32_t ret, socket_id;
1330 struct lcore_conf *qconf;
1331 struct ether_addr ethaddr;
1333 rte_eth_dev_info_get(portid, &dev_info);
1335 printf("Configuring device port %u:\n", portid);
1337 rte_eth_macaddr_get(portid, ðaddr);
1338 ethaddr_tbl[portid].src = ETHADDR_TO_UINT64(ethaddr);
1339 print_ethaddr("Address: ", ðaddr);
1342 nb_rx_queue = get_port_nb_rx_queues(portid);
1343 nb_tx_queue = nb_lcores;
1345 if (nb_rx_queue > dev_info.max_rx_queues)
1346 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1347 "(max rx queue is %u)\n",
1348 nb_rx_queue, dev_info.max_rx_queues);
1350 if (nb_tx_queue > dev_info.max_tx_queues)
1351 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1352 "(max tx queue is %u)\n",
1353 nb_tx_queue, dev_info.max_tx_queues);
1355 printf("Creating queues: nb_rx_queue=%d nb_tx_queue=%u...\n",
1356 nb_rx_queue, nb_tx_queue);
1359 port_conf.rxmode.max_rx_pkt_len = frame_size;
1360 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
1363 if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_SECURITY)
1364 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_SECURITY;
1365 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_SECURITY)
1366 port_conf.txmode.offloads |= DEV_TX_OFFLOAD_SECURITY;
1368 ret = rte_eth_dev_configure(portid, nb_rx_queue, nb_tx_queue,
1371 rte_exit(EXIT_FAILURE, "Cannot configure device: "
1372 "err=%d, port=%d\n", ret, portid);
1374 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
1376 rte_exit(EXIT_FAILURE, "Cannot adjust number of descriptors: "
1377 "err=%d, port=%d\n", ret, portid);
1379 /* init one TX queue per lcore */
1381 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1382 if (rte_lcore_is_enabled(lcore_id) == 0)
1386 socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1391 printf("Setup txq=%u,%d,%d\n", lcore_id, tx_queueid, socket_id);
1393 txconf = &dev_info.default_txconf;
1394 txconf->txq_flags = 0;
1396 ret = rte_eth_tx_queue_setup(portid, tx_queueid, nb_txd,
1399 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
1400 "err=%d, port=%d\n", ret, portid);
1402 qconf = &lcore_conf[lcore_id];
1403 qconf->tx_queue_id[portid] = tx_queueid;
1406 /* init RX queues */
1407 for (queue = 0; queue < qconf->nb_rx_queue; ++queue) {
1408 if (portid != qconf->rx_queue_list[queue].port_id)
1411 rx_queueid = qconf->rx_queue_list[queue].queue_id;
1413 printf("Setup rxq=%d,%d,%d\n", portid, rx_queueid,
1416 ret = rte_eth_rx_queue_setup(portid, rx_queueid,
1417 nb_rxd, socket_id, NULL,
1418 socket_ctx[socket_id].mbuf_pool);
1420 rte_exit(EXIT_FAILURE,
1421 "rte_eth_rx_queue_setup: err=%d, "
1422 "port=%d\n", ret, portid);
1429 pool_init(struct socket_ctx *ctx, int32_t socket_id, uint32_t nb_mbuf)
1432 uint32_t buff_size = frame_size ? (frame_size + RTE_PKTMBUF_HEADROOM) :
1433 RTE_MBUF_DEFAULT_BUF_SIZE;
1436 snprintf(s, sizeof(s), "mbuf_pool_%d", socket_id);
1437 ctx->mbuf_pool = rte_pktmbuf_pool_create(s, nb_mbuf,
1438 MEMPOOL_CACHE_SIZE, ipsec_metadata_size(),
1441 if (ctx->mbuf_pool == NULL)
1442 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n",
1445 printf("Allocated mbuf pool on socket %d\n", socket_id);
1449 main(int32_t argc, char **argv)
1454 uint16_t portid, nb_ports;
1457 ret = rte_eal_init(argc, argv);
1459 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1463 /* parse application arguments (after the EAL ones) */
1464 ret = parse_args(argc, argv);
1466 rte_exit(EXIT_FAILURE, "Invalid parameters\n");
1468 if ((unprotected_port_mask & enabled_port_mask) !=
1469 unprotected_port_mask)
1470 rte_exit(EXIT_FAILURE, "Invalid unprotected portmask 0x%x\n",
1471 unprotected_port_mask);
1473 nb_ports = rte_eth_dev_count();
1475 if (check_params() < 0)
1476 rte_exit(EXIT_FAILURE, "check_params failed\n");
1478 ret = init_lcore_rx_queues();
1480 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
1482 nb_lcores = rte_lcore_count();
1484 /* Replicate each context per socket */
1485 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1486 if (rte_lcore_is_enabled(lcore_id) == 0)
1490 socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1494 if (socket_ctx[socket_id].mbuf_pool)
1497 sa_init(&socket_ctx[socket_id], socket_id);
1499 sp4_init(&socket_ctx[socket_id], socket_id);
1501 sp6_init(&socket_ctx[socket_id], socket_id);
1503 rt_init(&socket_ctx[socket_id], socket_id);
1505 pool_init(&socket_ctx[socket_id], socket_id, NB_MBUF);
1508 for (portid = 0; portid < nb_ports; portid++) {
1509 if ((enabled_port_mask & (1 << portid)) == 0)
1518 for (portid = 0; portid < nb_ports; portid++) {
1519 if ((enabled_port_mask & (1 << portid)) == 0)
1523 ret = rte_eth_dev_start(portid);
1525 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: "
1526 "err=%d, port=%d\n", ret, portid);
1528 * If enabled, put device in promiscuous mode.
1529 * This allows IO forwarding mode to forward packets
1530 * to itself through 2 cross-connected ports of the
1534 rte_eth_promiscuous_enable(portid);
1537 check_all_ports_link_status(nb_ports, enabled_port_mask);
1539 /* launch per-lcore init on every lcore */
1540 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1541 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1542 if (rte_eal_wait_lcore(lcore_id) < 0)