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>
43 #include <rte_security.h>
48 #define RTE_LOGTYPE_IPSEC RTE_LOGTYPE_USER1
50 #define MAX_JUMBO_PKT_LEN 9600
52 #define MEMPOOL_CACHE_SIZE 256
54 #define NB_MBUF (32000)
56 #define CDEV_QUEUE_DESC 2048
57 #define CDEV_MAP_ENTRIES 16384
58 #define CDEV_MP_NB_OBJS 1024
59 #define CDEV_MP_CACHE_SZ 64
60 #define MAX_QUEUE_PAIRS 1
62 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
66 /* Configure how many packets ahead to prefetch, when reading packets */
67 #define PREFETCH_OFFSET 3
69 #define MAX_RX_QUEUE_PER_LCORE 16
71 #define MAX_LCORE_PARAMS 1024
73 #define UNPROTECTED_PORT(port) (unprotected_port_mask & (1 << portid))
76 * Configurable number of RX/TX ring descriptors
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;
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))
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))
104 #define ETHADDR(a, b, c, d, e, f) (__BYTES_TO_UINT64(a, b, c, d, e, f, 0, 0))
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], \
112 /* port/source ethernet addr and destination ethernet addr */
113 struct ethaddr_info {
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) }
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 #define CMD_LINE_OPT_RX_OFFLOAD "rxoffload"
128 #define CMD_LINE_OPT_TX_OFFLOAD "txoffload"
131 /* long options mapped to a short option */
133 /* first long only option value must be >= 256, so that we won't
134 * conflict with short options
136 CMD_LINE_OPT_MIN_NUM = 256,
137 CMD_LINE_OPT_CONFIG_NUM,
138 CMD_LINE_OPT_SINGLE_SA_NUM,
139 CMD_LINE_OPT_CRYPTODEV_MASK_NUM,
140 CMD_LINE_OPT_RX_OFFLOAD_NUM,
141 CMD_LINE_OPT_TX_OFFLOAD_NUM,
144 static const struct option lgopts[] = {
145 {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
146 {CMD_LINE_OPT_SINGLE_SA, 1, 0, CMD_LINE_OPT_SINGLE_SA_NUM},
147 {CMD_LINE_OPT_CRYPTODEV_MASK, 1, 0, CMD_LINE_OPT_CRYPTODEV_MASK_NUM},
148 {CMD_LINE_OPT_RX_OFFLOAD, 1, 0, CMD_LINE_OPT_RX_OFFLOAD_NUM},
149 {CMD_LINE_OPT_TX_OFFLOAD, 1, 0, CMD_LINE_OPT_TX_OFFLOAD_NUM},
153 /* mask of enabled ports */
154 static uint32_t enabled_port_mask;
155 static uint64_t enabled_cryptodev_mask = UINT64_MAX;
156 static uint32_t unprotected_port_mask;
157 static int32_t promiscuous_on = 1;
158 static int32_t numa_on = 1; /**< NUMA is enabled by default. */
159 static uint32_t nb_lcores;
160 static uint32_t single_sa;
161 static uint32_t single_sa_idx;
162 static uint32_t frame_size;
165 * RX/TX HW offload capabilities to enable/use on ethernet ports.
166 * By default all capabilities are enabled.
168 static uint64_t dev_rx_offload = UINT64_MAX;
169 static uint64_t dev_tx_offload = UINT64_MAX;
171 struct lcore_rx_queue {
174 } __rte_cache_aligned;
176 struct lcore_params {
180 } __rte_cache_aligned;
182 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
184 static struct lcore_params *lcore_params;
185 static uint16_t nb_lcore_params;
187 static struct rte_hash *cdev_map_in;
188 static struct rte_hash *cdev_map_out;
192 struct rte_mbuf *m_table[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
196 uint16_t nb_rx_queue;
197 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
198 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
199 struct buffer tx_mbufs[RTE_MAX_ETHPORTS];
200 struct ipsec_ctx inbound;
201 struct ipsec_ctx outbound;
202 struct rt_ctx *rt4_ctx;
203 struct rt_ctx *rt6_ctx;
204 } __rte_cache_aligned;
206 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
208 static struct rte_eth_conf port_conf = {
210 .mq_mode = ETH_MQ_RX_RSS,
211 .max_rx_pkt_len = ETHER_MAX_LEN,
213 .offloads = DEV_RX_OFFLOAD_CHECKSUM,
218 .rss_hf = ETH_RSS_IP | ETH_RSS_UDP |
219 ETH_RSS_TCP | ETH_RSS_SCTP,
223 .mq_mode = ETH_MQ_TX_NONE,
227 static struct socket_ctx socket_ctx[NB_SOCKETS];
229 struct traffic_type {
230 const uint8_t *data[MAX_PKT_BURST * 2];
231 struct rte_mbuf *pkts[MAX_PKT_BURST * 2];
232 uint32_t res[MAX_PKT_BURST * 2];
236 struct ipsec_traffic {
237 struct traffic_type ipsec;
238 struct traffic_type ip4;
239 struct traffic_type ip6;
243 prepare_one_packet(struct rte_mbuf *pkt, struct ipsec_traffic *t)
246 struct ether_hdr *eth;
248 eth = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
249 if (eth->ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4)) {
250 nlp = (uint8_t *)rte_pktmbuf_adj(pkt, ETHER_HDR_LEN);
251 nlp = RTE_PTR_ADD(nlp, offsetof(struct ip, ip_p));
252 if (*nlp == IPPROTO_ESP)
253 t->ipsec.pkts[(t->ipsec.num)++] = pkt;
255 t->ip4.data[t->ip4.num] = nlp;
256 t->ip4.pkts[(t->ip4.num)++] = pkt;
258 } else if (eth->ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6)) {
259 nlp = (uint8_t *)rte_pktmbuf_adj(pkt, ETHER_HDR_LEN);
260 nlp = RTE_PTR_ADD(nlp, offsetof(struct ip6_hdr, ip6_nxt));
261 if (*nlp == IPPROTO_ESP)
262 t->ipsec.pkts[(t->ipsec.num)++] = pkt;
264 t->ip6.data[t->ip6.num] = nlp;
265 t->ip6.pkts[(t->ip6.num)++] = pkt;
268 /* Unknown/Unsupported type, drop the packet */
269 RTE_LOG(ERR, IPSEC, "Unsupported packet type\n");
270 rte_pktmbuf_free(pkt);
273 /* Check if the packet has been processed inline. For inline protocol
274 * processed packets, the metadata in the mbuf can be used to identify
275 * the security processing done on the packet. The metadata will be
276 * used to retrieve the application registered userdata associated
277 * with the security session.
280 if (pkt->ol_flags & PKT_RX_SEC_OFFLOAD) {
282 struct ipsec_mbuf_metadata *priv;
283 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
284 rte_eth_dev_get_sec_ctx(
287 /* Retrieve the userdata registered. Here, the userdata
288 * registered is the SA pointer.
291 sa = (struct ipsec_sa *)
292 rte_security_get_userdata(ctx, pkt->udata64);
295 /* userdata could not be retrieved */
299 /* Save SA as priv member in mbuf. This will be used in the
300 * IPsec selector(SP-SA) check.
303 priv = get_priv(pkt);
309 prepare_traffic(struct rte_mbuf **pkts, struct ipsec_traffic *t,
318 for (i = 0; i < (nb_pkts - PREFETCH_OFFSET); i++) {
319 rte_prefetch0(rte_pktmbuf_mtod(pkts[i + PREFETCH_OFFSET],
321 prepare_one_packet(pkts[i], t);
323 /* Process left packets */
324 for (; i < nb_pkts; i++)
325 prepare_one_packet(pkts[i], t);
329 prepare_tx_pkt(struct rte_mbuf *pkt, uint16_t port,
330 const struct lcore_conf *qconf)
333 struct ether_hdr *ethhdr;
335 ip = rte_pktmbuf_mtod(pkt, struct ip *);
337 ethhdr = (struct ether_hdr *)rte_pktmbuf_prepend(pkt, ETHER_HDR_LEN);
339 if (ip->ip_v == IPVERSION) {
340 pkt->ol_flags |= qconf->outbound.ipv4_offloads;
341 pkt->l3_len = sizeof(struct ip);
342 pkt->l2_len = ETHER_HDR_LEN;
346 /* calculate IPv4 cksum in SW */
347 if ((pkt->ol_flags & PKT_TX_IP_CKSUM) == 0)
348 ip->ip_sum = rte_ipv4_cksum((struct ipv4_hdr *)ip);
350 ethhdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
352 pkt->ol_flags |= qconf->outbound.ipv6_offloads;
353 pkt->l3_len = sizeof(struct ip6_hdr);
354 pkt->l2_len = ETHER_HDR_LEN;
356 ethhdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv6);
359 memcpy(ðhdr->s_addr, ðaddr_tbl[port].src,
360 sizeof(struct ether_addr));
361 memcpy(ðhdr->d_addr, ðaddr_tbl[port].dst,
362 sizeof(struct ether_addr));
366 prepare_tx_burst(struct rte_mbuf *pkts[], uint16_t nb_pkts, uint16_t port,
367 const struct lcore_conf *qconf)
370 const int32_t prefetch_offset = 2;
372 for (i = 0; i < (nb_pkts - prefetch_offset); i++) {
373 rte_mbuf_prefetch_part2(pkts[i + prefetch_offset]);
374 prepare_tx_pkt(pkts[i], port, qconf);
376 /* Process left packets */
377 for (; i < nb_pkts; i++)
378 prepare_tx_pkt(pkts[i], port, qconf);
381 /* Send burst of packets on an output interface */
382 static inline int32_t
383 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
385 struct rte_mbuf **m_table;
389 queueid = qconf->tx_queue_id[port];
390 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
392 prepare_tx_burst(m_table, n, port, qconf);
394 ret = rte_eth_tx_burst(port, queueid, m_table, n);
395 if (unlikely(ret < n)) {
397 rte_pktmbuf_free(m_table[ret]);
404 /* Enqueue a single packet, and send burst if queue is filled */
405 static inline int32_t
406 send_single_packet(struct rte_mbuf *m, uint16_t port)
410 struct lcore_conf *qconf;
412 lcore_id = rte_lcore_id();
414 qconf = &lcore_conf[lcore_id];
415 len = qconf->tx_mbufs[port].len;
416 qconf->tx_mbufs[port].m_table[len] = m;
419 /* enough pkts to be sent */
420 if (unlikely(len == MAX_PKT_BURST)) {
421 send_burst(qconf, MAX_PKT_BURST, port);
425 qconf->tx_mbufs[port].len = len;
430 inbound_sp_sa(struct sp_ctx *sp, struct sa_ctx *sa, struct traffic_type *ip,
434 uint32_t i, j, res, 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++) {
455 /* Only check SPI match for processed IPSec packets */
456 if (i < lim && ((m->ol_flags & PKT_RX_SEC_OFFLOAD) == 0)) {
461 sa_idx = ip->res[i] & PROTECT_MASK;
462 if (sa_idx >= IPSEC_SA_MAX_ENTRIES ||
463 !inbound_sa_check(sa, m, sa_idx)) {
473 split46_traffic(struct ipsec_traffic *trf, struct rte_mbuf *mb[], uint32_t num)
482 for (i = 0; i < num; i++) {
485 ip = rte_pktmbuf_mtod(m, struct ip *);
487 if (ip->ip_v == IPVERSION) {
488 trf->ip4.pkts[n4] = m;
489 trf->ip4.data[n4] = rte_pktmbuf_mtod_offset(m,
490 uint8_t *, offsetof(struct ip, ip_p));
492 } else if (ip->ip_v == IP6_VERSION) {
493 trf->ip6.pkts[n6] = m;
494 trf->ip6.data[n6] = rte_pktmbuf_mtod_offset(m,
496 offsetof(struct ip6_hdr, ip6_nxt));
508 process_pkts_inbound(struct ipsec_ctx *ipsec_ctx,
509 struct ipsec_traffic *traffic)
511 uint16_t nb_pkts_in, n_ip4, n_ip6;
513 n_ip4 = traffic->ip4.num;
514 n_ip6 = traffic->ip6.num;
516 nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
517 traffic->ipsec.num, MAX_PKT_BURST);
519 split46_traffic(traffic, traffic->ipsec.pkts, nb_pkts_in);
521 inbound_sp_sa(ipsec_ctx->sp4_ctx, ipsec_ctx->sa_ctx, &traffic->ip4,
524 inbound_sp_sa(ipsec_ctx->sp6_ctx, ipsec_ctx->sa_ctx, &traffic->ip6,
529 outbound_sp(struct sp_ctx *sp, struct traffic_type *ip,
530 struct traffic_type *ipsec)
533 uint32_t i, j, sa_idx;
535 if (ip->num == 0 || sp == NULL)
538 rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
539 ip->num, DEFAULT_MAX_CATEGORIES);
542 for (i = 0; i < ip->num; i++) {
544 sa_idx = ip->res[i] & PROTECT_MASK;
545 if (ip->res[i] & DISCARD)
547 else if (ip->res[i] & BYPASS)
549 else if (sa_idx < IPSEC_SA_MAX_ENTRIES) {
550 ipsec->res[ipsec->num] = sa_idx;
551 ipsec->pkts[ipsec->num++] = m;
552 } else /* invalid SA idx */
559 process_pkts_outbound(struct ipsec_ctx *ipsec_ctx,
560 struct ipsec_traffic *traffic)
563 uint16_t idx, nb_pkts_out, i;
565 /* Drop any IPsec traffic from protected ports */
566 for (i = 0; i < traffic->ipsec.num; i++)
567 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
569 traffic->ipsec.num = 0;
571 outbound_sp(ipsec_ctx->sp4_ctx, &traffic->ip4, &traffic->ipsec);
573 outbound_sp(ipsec_ctx->sp6_ctx, &traffic->ip6, &traffic->ipsec);
575 nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ipsec.pkts,
576 traffic->ipsec.res, traffic->ipsec.num,
579 for (i = 0; i < nb_pkts_out; i++) {
580 m = traffic->ipsec.pkts[i];
581 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
582 if (ip->ip_v == IPVERSION) {
583 idx = traffic->ip4.num++;
584 traffic->ip4.pkts[idx] = m;
586 idx = traffic->ip6.num++;
587 traffic->ip6.pkts[idx] = m;
593 process_pkts_inbound_nosp(struct ipsec_ctx *ipsec_ctx,
594 struct ipsec_traffic *traffic)
597 uint32_t nb_pkts_in, i, idx;
599 /* Drop any IPv4 traffic from unprotected ports */
600 for (i = 0; i < traffic->ip4.num; i++)
601 rte_pktmbuf_free(traffic->ip4.pkts[i]);
603 traffic->ip4.num = 0;
605 /* Drop any IPv6 traffic from unprotected ports */
606 for (i = 0; i < traffic->ip6.num; i++)
607 rte_pktmbuf_free(traffic->ip6.pkts[i]);
609 traffic->ip6.num = 0;
611 nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
612 traffic->ipsec.num, MAX_PKT_BURST);
614 for (i = 0; i < nb_pkts_in; i++) {
615 m = traffic->ipsec.pkts[i];
616 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
617 if (ip->ip_v == IPVERSION) {
618 idx = traffic->ip4.num++;
619 traffic->ip4.pkts[idx] = m;
621 idx = traffic->ip6.num++;
622 traffic->ip6.pkts[idx] = m;
628 process_pkts_outbound_nosp(struct ipsec_ctx *ipsec_ctx,
629 struct ipsec_traffic *traffic)
632 uint32_t nb_pkts_out, i;
635 /* Drop any IPsec traffic from protected ports */
636 for (i = 0; i < traffic->ipsec.num; i++)
637 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
639 traffic->ipsec.num = 0;
641 for (i = 0; i < traffic->ip4.num; i++)
642 traffic->ip4.res[i] = single_sa_idx;
644 for (i = 0; i < traffic->ip6.num; i++)
645 traffic->ip6.res[i] = single_sa_idx;
647 nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ip4.pkts,
648 traffic->ip4.res, traffic->ip4.num,
651 /* They all sue the same SA (ip4 or ip6 tunnel) */
652 m = traffic->ipsec.pkts[i];
653 ip = rte_pktmbuf_mtod(m, struct ip *);
654 if (ip->ip_v == IPVERSION)
655 traffic->ip4.num = nb_pkts_out;
657 traffic->ip6.num = nb_pkts_out;
660 static inline int32_t
661 get_hop_for_offload_pkt(struct rte_mbuf *pkt, int is_ipv6)
663 struct ipsec_mbuf_metadata *priv;
666 priv = get_priv(pkt);
669 if (unlikely(sa == NULL)) {
670 RTE_LOG(ERR, IPSEC, "SA not saved in private data\n");
678 return (sa->portid | RTE_LPM_LOOKUP_SUCCESS);
689 route4_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
691 uint32_t hop[MAX_PKT_BURST * 2];
692 uint32_t dst_ip[MAX_PKT_BURST * 2];
695 uint16_t lpm_pkts = 0;
700 /* Need to do an LPM lookup for non-inline packets. Inline packets will
701 * have port ID in the SA
704 for (i = 0; i < nb_pkts; i++) {
705 if (!(pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD)) {
706 /* Security offload not enabled. So an LPM lookup is
707 * required to get the hop
709 offset = offsetof(struct ip, ip_dst);
710 dst_ip[lpm_pkts] = *rte_pktmbuf_mtod_offset(pkts[i],
712 dst_ip[lpm_pkts] = rte_be_to_cpu_32(dst_ip[lpm_pkts]);
717 rte_lpm_lookup_bulk((struct rte_lpm *)rt_ctx, dst_ip, hop, lpm_pkts);
721 for (i = 0; i < nb_pkts; i++) {
722 if (pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD) {
723 /* Read hop from the SA */
724 pkt_hop = get_hop_for_offload_pkt(pkts[i], 0);
726 /* Need to use hop returned by lookup */
727 pkt_hop = hop[lpm_pkts++];
730 if ((pkt_hop & RTE_LPM_LOOKUP_SUCCESS) == 0) {
731 rte_pktmbuf_free(pkts[i]);
734 send_single_packet(pkts[i], pkt_hop & 0xff);
739 route6_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
741 int32_t hop[MAX_PKT_BURST * 2];
742 uint8_t dst_ip[MAX_PKT_BURST * 2][16];
746 uint16_t lpm_pkts = 0;
751 /* Need to do an LPM lookup for non-inline packets. Inline packets will
752 * have port ID in the SA
755 for (i = 0; i < nb_pkts; i++) {
756 if (!(pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD)) {
757 /* Security offload not enabled. So an LPM lookup is
758 * required to get the hop
760 offset = offsetof(struct ip6_hdr, ip6_dst);
761 ip6_dst = rte_pktmbuf_mtod_offset(pkts[i], uint8_t *,
763 memcpy(&dst_ip[lpm_pkts][0], ip6_dst, 16);
768 rte_lpm6_lookup_bulk_func((struct rte_lpm6 *)rt_ctx, dst_ip, hop,
773 for (i = 0; i < nb_pkts; i++) {
774 if (pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD) {
775 /* Read hop from the SA */
776 pkt_hop = get_hop_for_offload_pkt(pkts[i], 1);
778 /* Need to use hop returned by lookup */
779 pkt_hop = hop[lpm_pkts++];
783 rte_pktmbuf_free(pkts[i]);
786 send_single_packet(pkts[i], pkt_hop & 0xff);
791 process_pkts(struct lcore_conf *qconf, struct rte_mbuf **pkts,
792 uint8_t nb_pkts, uint16_t portid)
794 struct ipsec_traffic traffic;
796 prepare_traffic(pkts, &traffic, nb_pkts);
798 if (unlikely(single_sa)) {
799 if (UNPROTECTED_PORT(portid))
800 process_pkts_inbound_nosp(&qconf->inbound, &traffic);
802 process_pkts_outbound_nosp(&qconf->outbound, &traffic);
804 if (UNPROTECTED_PORT(portid))
805 process_pkts_inbound(&qconf->inbound, &traffic);
807 process_pkts_outbound(&qconf->outbound, &traffic);
810 route4_pkts(qconf->rt4_ctx, traffic.ip4.pkts, traffic.ip4.num);
811 route6_pkts(qconf->rt6_ctx, traffic.ip6.pkts, traffic.ip6.num);
815 drain_tx_buffers(struct lcore_conf *qconf)
820 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
821 buf = &qconf->tx_mbufs[portid];
824 send_burst(qconf, buf->len, portid);
830 drain_crypto_buffers(struct lcore_conf *qconf)
833 struct ipsec_ctx *ctx;
835 /* drain inbound buffers*/
836 ctx = &qconf->inbound;
837 for (i = 0; i != ctx->nb_qps; i++) {
838 if (ctx->tbl[i].len != 0)
839 enqueue_cop_burst(ctx->tbl + i);
842 /* drain outbound buffers*/
843 ctx = &qconf->outbound;
844 for (i = 0; i != ctx->nb_qps; i++) {
845 if (ctx->tbl[i].len != 0)
846 enqueue_cop_burst(ctx->tbl + i);
851 drain_inbound_crypto_queues(const struct lcore_conf *qconf,
852 struct ipsec_ctx *ctx)
855 struct ipsec_traffic trf;
857 /* dequeue packets from crypto-queue */
858 n = ipsec_inbound_cqp_dequeue(ctx, trf.ipsec.pkts,
859 RTE_DIM(trf.ipsec.pkts));
866 /* split traffic by ipv4-ipv6 */
867 split46_traffic(&trf, trf.ipsec.pkts, n);
869 /* process ipv4 packets */
870 inbound_sp_sa(ctx->sp4_ctx, ctx->sa_ctx, &trf.ip4, 0);
871 route4_pkts(qconf->rt4_ctx, trf.ip4.pkts, trf.ip4.num);
873 /* process ipv6 packets */
874 inbound_sp_sa(ctx->sp6_ctx, ctx->sa_ctx, &trf.ip6, 0);
875 route6_pkts(qconf->rt6_ctx, trf.ip6.pkts, trf.ip6.num);
879 drain_outbound_crypto_queues(const struct lcore_conf *qconf,
880 struct ipsec_ctx *ctx)
883 struct ipsec_traffic trf;
885 /* dequeue packets from crypto-queue */
886 n = ipsec_outbound_cqp_dequeue(ctx, trf.ipsec.pkts,
887 RTE_DIM(trf.ipsec.pkts));
894 /* split traffic by ipv4-ipv6 */
895 split46_traffic(&trf, trf.ipsec.pkts, n);
897 /* process ipv4 packets */
898 route4_pkts(qconf->rt4_ctx, trf.ip4.pkts, trf.ip4.num);
900 /* process ipv6 packets */
901 route6_pkts(qconf->rt6_ctx, trf.ip6.pkts, trf.ip6.num);
904 /* main processing loop */
906 main_loop(__attribute__((unused)) void *dummy)
908 struct rte_mbuf *pkts[MAX_PKT_BURST];
910 uint64_t prev_tsc, diff_tsc, cur_tsc;
914 struct lcore_conf *qconf;
916 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
917 / US_PER_S * BURST_TX_DRAIN_US;
918 struct lcore_rx_queue *rxql;
921 lcore_id = rte_lcore_id();
922 qconf = &lcore_conf[lcore_id];
923 rxql = qconf->rx_queue_list;
924 socket_id = rte_lcore_to_socket_id(lcore_id);
926 qconf->rt4_ctx = socket_ctx[socket_id].rt_ip4;
927 qconf->rt6_ctx = socket_ctx[socket_id].rt_ip6;
928 qconf->inbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_in;
929 qconf->inbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_in;
930 qconf->inbound.sa_ctx = socket_ctx[socket_id].sa_in;
931 qconf->inbound.cdev_map = cdev_map_in;
932 qconf->inbound.session_pool = socket_ctx[socket_id].session_pool;
933 qconf->inbound.session_priv_pool =
934 socket_ctx[socket_id].session_priv_pool;
935 qconf->outbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_out;
936 qconf->outbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_out;
937 qconf->outbound.sa_ctx = socket_ctx[socket_id].sa_out;
938 qconf->outbound.cdev_map = cdev_map_out;
939 qconf->outbound.session_pool = socket_ctx[socket_id].session_pool;
940 qconf->outbound.session_priv_pool =
941 socket_ctx[socket_id].session_priv_pool;
943 if (qconf->nb_rx_queue == 0) {
944 RTE_LOG(INFO, IPSEC, "lcore %u has nothing to do\n", lcore_id);
948 RTE_LOG(INFO, IPSEC, "entering main loop on lcore %u\n", lcore_id);
950 for (i = 0; i < qconf->nb_rx_queue; i++) {
951 portid = rxql[i].port_id;
952 queueid = rxql[i].queue_id;
954 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
955 lcore_id, portid, queueid);
959 cur_tsc = rte_rdtsc();
961 /* TX queue buffer drain */
962 diff_tsc = cur_tsc - prev_tsc;
964 if (unlikely(diff_tsc > drain_tsc)) {
965 drain_tx_buffers(qconf);
966 drain_crypto_buffers(qconf);
970 for (i = 0; i < qconf->nb_rx_queue; ++i) {
972 /* Read packets from RX queues */
973 portid = rxql[i].port_id;
974 queueid = rxql[i].queue_id;
975 nb_rx = rte_eth_rx_burst(portid, queueid,
976 pkts, MAX_PKT_BURST);
979 process_pkts(qconf, pkts, nb_rx, portid);
981 /* dequeue and process completed crypto-ops */
982 if (UNPROTECTED_PORT(portid))
983 drain_inbound_crypto_queues(qconf,
986 drain_outbound_crypto_queues(qconf,
1000 if (lcore_params == NULL) {
1001 printf("Error: No port/queue/core mappings\n");
1005 for (i = 0; i < nb_lcore_params; ++i) {
1006 lcore = lcore_params[i].lcore_id;
1007 if (!rte_lcore_is_enabled(lcore)) {
1008 printf("error: lcore %hhu is not enabled in "
1009 "lcore mask\n", lcore);
1012 socket_id = rte_lcore_to_socket_id(lcore);
1013 if (socket_id != 0 && numa_on == 0) {
1014 printf("warning: lcore %hhu is on socket %d "
1018 portid = lcore_params[i].port_id;
1019 if ((enabled_port_mask & (1 << portid)) == 0) {
1020 printf("port %u is not enabled in port mask\n", portid);
1023 if (!rte_eth_dev_is_valid_port(portid)) {
1024 printf("port %u is not present on the board\n", portid);
1032 get_port_nb_rx_queues(const uint16_t port)
1037 for (i = 0; i < nb_lcore_params; ++i) {
1038 if (lcore_params[i].port_id == port &&
1039 lcore_params[i].queue_id > queue)
1040 queue = lcore_params[i].queue_id;
1042 return (uint8_t)(++queue);
1046 init_lcore_rx_queues(void)
1048 uint16_t i, nb_rx_queue;
1051 for (i = 0; i < nb_lcore_params; ++i) {
1052 lcore = lcore_params[i].lcore_id;
1053 nb_rx_queue = lcore_conf[lcore].nb_rx_queue;
1054 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1055 printf("error: too many queues (%u) for lcore: %u\n",
1056 nb_rx_queue + 1, lcore);
1059 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1060 lcore_params[i].port_id;
1061 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1062 lcore_params[i].queue_id;
1063 lcore_conf[lcore].nb_rx_queue++;
1070 print_usage(const char *prgname)
1072 fprintf(stderr, "%s [EAL options] --"
1078 " --config (port,queue,lcore)[,(port,queue,lcore)]"
1079 " [--single-sa SAIDX]"
1080 " [--cryptodev_mask MASK]"
1081 " [--" CMD_LINE_OPT_RX_OFFLOAD " RX_OFFLOAD_MASK]"
1082 " [--" CMD_LINE_OPT_TX_OFFLOAD " TX_OFFLOAD_MASK]"
1084 " -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
1085 " -P : Enable promiscuous mode\n"
1086 " -u PORTMASK: Hexadecimal bitmask of unprotected ports\n"
1087 " -j FRAMESIZE: Enable jumbo frame with 'FRAMESIZE' as maximum\n"
1089 " -f CONFIG_FILE: Configuration file\n"
1090 " --config (port,queue,lcore): Rx queue configuration\n"
1091 " --single-sa SAIDX: Use single SA index for outbound traffic,\n"
1092 " bypassing the SP\n"
1093 " --cryptodev_mask MASK: Hexadecimal bitmask of the crypto\n"
1094 " devices to configure\n"
1095 " --" CMD_LINE_OPT_RX_OFFLOAD
1096 ": bitmask of the RX HW offload capabilities to enable/use\n"
1097 " (DEV_RX_OFFLOAD_*)\n"
1098 " --" CMD_LINE_OPT_TX_OFFLOAD
1099 ": bitmask of the TX HW offload capabilities to enable/use\n"
1100 " (DEV_TX_OFFLOAD_*)\n"
1106 parse_mask(const char *str, uint64_t *val)
1112 t = strtoul(str, &end, 0);
1113 if (errno != 0 || end[0] != 0)
1121 parse_portmask(const char *portmask)
1126 /* parse hexadecimal string */
1127 pm = strtoul(portmask, &end, 16);
1128 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1131 if ((pm == 0) && errno)
1138 parse_decimal(const char *str)
1143 num = strtoul(str, &end, 10);
1144 if ((str[0] == '\0') || (end == NULL) || (*end != '\0'))
1151 parse_config(const char *q_arg)
1154 const char *p, *p0 = q_arg;
1162 unsigned long int_fld[_NUM_FLD];
1163 char *str_fld[_NUM_FLD];
1167 nb_lcore_params = 0;
1169 while ((p = strchr(p0, '(')) != NULL) {
1171 p0 = strchr(p, ')');
1176 if (size >= sizeof(s))
1179 snprintf(s, sizeof(s), "%.*s", size, p);
1180 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1183 for (i = 0; i < _NUM_FLD; i++) {
1185 int_fld[i] = strtoul(str_fld[i], &end, 0);
1186 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
1189 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1190 printf("exceeded max number of lcore params: %hu\n",
1194 lcore_params_array[nb_lcore_params].port_id =
1195 (uint8_t)int_fld[FLD_PORT];
1196 lcore_params_array[nb_lcore_params].queue_id =
1197 (uint8_t)int_fld[FLD_QUEUE];
1198 lcore_params_array[nb_lcore_params].lcore_id =
1199 (uint8_t)int_fld[FLD_LCORE];
1202 lcore_params = lcore_params_array;
1207 parse_args(int32_t argc, char **argv)
1211 int32_t option_index;
1212 char *prgname = argv[0];
1213 int32_t f_present = 0;
1217 while ((opt = getopt_long(argc, argvopt, "p:Pu:f:j:",
1218 lgopts, &option_index)) != EOF) {
1222 enabled_port_mask = parse_portmask(optarg);
1223 if (enabled_port_mask == 0) {
1224 printf("invalid portmask\n");
1225 print_usage(prgname);
1230 printf("Promiscuous mode selected\n");
1234 unprotected_port_mask = parse_portmask(optarg);
1235 if (unprotected_port_mask == 0) {
1236 printf("invalid unprotected portmask\n");
1237 print_usage(prgname);
1242 if (f_present == 1) {
1243 printf("\"-f\" option present more than "
1245 print_usage(prgname);
1248 if (parse_cfg_file(optarg) < 0) {
1249 printf("parsing file \"%s\" failed\n",
1251 print_usage(prgname);
1258 int32_t size = parse_decimal(optarg);
1260 printf("Invalid jumbo frame size\n");
1262 print_usage(prgname);
1265 printf("Using default value 9000\n");
1271 printf("Enabled jumbo frames size %u\n", frame_size);
1273 case CMD_LINE_OPT_CONFIG_NUM:
1274 ret = parse_config(optarg);
1276 printf("Invalid config\n");
1277 print_usage(prgname);
1281 case CMD_LINE_OPT_SINGLE_SA_NUM:
1282 ret = parse_decimal(optarg);
1284 printf("Invalid argument[sa_idx]\n");
1285 print_usage(prgname);
1291 single_sa_idx = ret;
1292 printf("Configured with single SA index %u\n",
1295 case CMD_LINE_OPT_CRYPTODEV_MASK_NUM:
1296 ret = parse_portmask(optarg);
1298 printf("Invalid argument[portmask]\n");
1299 print_usage(prgname);
1304 enabled_cryptodev_mask = ret;
1306 case CMD_LINE_OPT_RX_OFFLOAD_NUM:
1307 ret = parse_mask(optarg, &dev_rx_offload);
1309 printf("Invalid argument for \'%s\': %s\n",
1310 CMD_LINE_OPT_RX_OFFLOAD, optarg);
1311 print_usage(prgname);
1315 case CMD_LINE_OPT_TX_OFFLOAD_NUM:
1316 ret = parse_mask(optarg, &dev_tx_offload);
1318 printf("Invalid argument for \'%s\': %s\n",
1319 CMD_LINE_OPT_TX_OFFLOAD, optarg);
1320 print_usage(prgname);
1325 print_usage(prgname);
1330 if (f_present == 0) {
1331 printf("Mandatory option \"-f\" not present\n");
1336 argv[optind-1] = prgname;
1339 optind = 1; /* reset getopt lib */
1344 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
1346 char buf[ETHER_ADDR_FMT_SIZE];
1347 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
1348 printf("%s%s", name, buf);
1352 * Update destination ethaddr for the port.
1355 add_dst_ethaddr(uint16_t port, const struct ether_addr *addr)
1357 if (port > RTE_DIM(ethaddr_tbl))
1360 ethaddr_tbl[port].dst = ETHADDR_TO_UINT64(addr);
1364 /* Check the link status of all ports in up to 9s, and print them finally */
1366 check_all_ports_link_status(uint32_t port_mask)
1368 #define CHECK_INTERVAL 100 /* 100ms */
1369 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1371 uint8_t count, all_ports_up, print_flag = 0;
1372 struct rte_eth_link link;
1374 printf("\nChecking link status");
1376 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1378 RTE_ETH_FOREACH_DEV(portid) {
1379 if ((port_mask & (1 << portid)) == 0)
1381 memset(&link, 0, sizeof(link));
1382 rte_eth_link_get_nowait(portid, &link);
1383 /* print link status if flag set */
1384 if (print_flag == 1) {
1385 if (link.link_status)
1387 "Port%d Link Up - speed %u Mbps -%s\n",
1388 portid, link.link_speed,
1389 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1390 ("full-duplex") : ("half-duplex\n"));
1392 printf("Port %d Link Down\n", portid);
1395 /* clear all_ports_up flag if any link down */
1396 if (link.link_status == ETH_LINK_DOWN) {
1401 /* after finally printing all link status, get out */
1402 if (print_flag == 1)
1405 if (all_ports_up == 0) {
1408 rte_delay_ms(CHECK_INTERVAL);
1411 /* set the print_flag if all ports up or timeout */
1412 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1420 add_mapping(struct rte_hash *map, const char *str, uint16_t cdev_id,
1421 uint16_t qp, struct lcore_params *params,
1422 struct ipsec_ctx *ipsec_ctx,
1423 const struct rte_cryptodev_capabilities *cipher,
1424 const struct rte_cryptodev_capabilities *auth,
1425 const struct rte_cryptodev_capabilities *aead)
1429 struct cdev_key key = { 0 };
1431 key.lcore_id = params->lcore_id;
1433 key.cipher_algo = cipher->sym.cipher.algo;
1435 key.auth_algo = auth->sym.auth.algo;
1437 key.aead_algo = aead->sym.aead.algo;
1439 ret = rte_hash_lookup(map, &key);
1443 for (i = 0; i < ipsec_ctx->nb_qps; i++)
1444 if (ipsec_ctx->tbl[i].id == cdev_id)
1447 if (i == ipsec_ctx->nb_qps) {
1448 if (ipsec_ctx->nb_qps == MAX_QP_PER_LCORE) {
1449 printf("Maximum number of crypto devices assigned to "
1450 "a core, increase MAX_QP_PER_LCORE value\n");
1453 ipsec_ctx->tbl[i].id = cdev_id;
1454 ipsec_ctx->tbl[i].qp = qp;
1455 ipsec_ctx->nb_qps++;
1456 printf("%s cdev mapping: lcore %u using cdev %u qp %u "
1457 "(cdev_id_qp %lu)\n", str, key.lcore_id,
1461 ret = rte_hash_add_key_data(map, &key, (void *)i);
1463 printf("Faled to insert cdev mapping for (lcore %u, "
1464 "cdev %u, qp %u), errno %d\n",
1465 key.lcore_id, ipsec_ctx->tbl[i].id,
1466 ipsec_ctx->tbl[i].qp, ret);
1474 add_cdev_mapping(struct rte_cryptodev_info *dev_info, uint16_t cdev_id,
1475 uint16_t qp, struct lcore_params *params)
1478 const struct rte_cryptodev_capabilities *i, *j;
1479 struct rte_hash *map;
1480 struct lcore_conf *qconf;
1481 struct ipsec_ctx *ipsec_ctx;
1484 qconf = &lcore_conf[params->lcore_id];
1486 if ((unprotected_port_mask & (1 << params->port_id)) == 0) {
1488 ipsec_ctx = &qconf->outbound;
1492 ipsec_ctx = &qconf->inbound;
1496 /* Required cryptodevs with operation chainning */
1497 if (!(dev_info->feature_flags &
1498 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING))
1501 for (i = dev_info->capabilities;
1502 i->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; i++) {
1503 if (i->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1506 if (i->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1507 ret |= add_mapping(map, str, cdev_id, qp, params,
1508 ipsec_ctx, NULL, NULL, i);
1512 if (i->sym.xform_type != RTE_CRYPTO_SYM_XFORM_CIPHER)
1515 for (j = dev_info->capabilities;
1516 j->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; j++) {
1517 if (j->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1520 if (j->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH)
1523 ret |= add_mapping(map, str, cdev_id, qp, params,
1524 ipsec_ctx, i, j, NULL);
1531 /* Check if the device is enabled by cryptodev_mask */
1533 check_cryptodev_mask(uint8_t cdev_id)
1535 if (enabled_cryptodev_mask & (1 << cdev_id))
1542 cryptodevs_init(void)
1544 struct rte_cryptodev_config dev_conf;
1545 struct rte_cryptodev_qp_conf qp_conf;
1546 uint16_t idx, max_nb_qps, qp, i;
1547 int16_t cdev_id, port_id;
1548 struct rte_hash_parameters params = { 0 };
1550 params.entries = CDEV_MAP_ENTRIES;
1551 params.key_len = sizeof(struct cdev_key);
1552 params.hash_func = rte_jhash;
1553 params.hash_func_init_val = 0;
1554 params.socket_id = rte_socket_id();
1556 params.name = "cdev_map_in";
1557 cdev_map_in = rte_hash_create(¶ms);
1558 if (cdev_map_in == NULL)
1559 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1562 params.name = "cdev_map_out";
1563 cdev_map_out = rte_hash_create(¶ms);
1564 if (cdev_map_out == NULL)
1565 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1568 printf("lcore/cryptodev/qp mappings:\n");
1570 uint32_t max_sess_sz = 0, sess_sz;
1571 for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1574 /* Get crypto priv session size */
1575 sess_sz = rte_cryptodev_sym_get_private_session_size(cdev_id);
1576 if (sess_sz > max_sess_sz)
1577 max_sess_sz = sess_sz;
1580 * If crypto device is security capable, need to check the
1581 * size of security session as well.
1584 /* Get security context of the crypto device */
1585 sec_ctx = rte_cryptodev_get_sec_ctx(cdev_id);
1586 if (sec_ctx == NULL)
1589 /* Get size of security session */
1590 sess_sz = rte_security_session_get_size(sec_ctx);
1591 if (sess_sz > max_sess_sz)
1592 max_sess_sz = sess_sz;
1594 RTE_ETH_FOREACH_DEV(port_id) {
1597 if ((enabled_port_mask & (1 << port_id)) == 0)
1600 sec_ctx = rte_eth_dev_get_sec_ctx(port_id);
1601 if (sec_ctx == NULL)
1604 sess_sz = rte_security_session_get_size(sec_ctx);
1605 if (sess_sz > max_sess_sz)
1606 max_sess_sz = sess_sz;
1610 for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1611 struct rte_cryptodev_info cdev_info;
1613 if (check_cryptodev_mask((uint8_t)cdev_id))
1616 rte_cryptodev_info_get(cdev_id, &cdev_info);
1618 if (nb_lcore_params > cdev_info.max_nb_queue_pairs)
1619 max_nb_qps = cdev_info.max_nb_queue_pairs;
1621 max_nb_qps = nb_lcore_params;
1625 while (qp < max_nb_qps && i < nb_lcore_params) {
1626 if (add_cdev_mapping(&cdev_info, cdev_id, qp,
1627 &lcore_params[idx]))
1630 idx = idx % nb_lcore_params;
1637 dev_conf.socket_id = rte_cryptodev_socket_id(cdev_id);
1638 dev_conf.nb_queue_pairs = qp;
1640 uint32_t dev_max_sess = cdev_info.sym.max_nb_sessions;
1641 if (dev_max_sess != 0 && dev_max_sess < CDEV_MP_NB_OBJS)
1642 rte_exit(EXIT_FAILURE,
1643 "Device does not support at least %u "
1644 "sessions", CDEV_MP_NB_OBJS);
1646 if (!socket_ctx[dev_conf.socket_id].session_pool) {
1647 char mp_name[RTE_MEMPOOL_NAMESIZE];
1648 struct rte_mempool *sess_mp;
1650 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1651 "sess_mp_%u", dev_conf.socket_id);
1652 sess_mp = rte_cryptodev_sym_session_pool_create(
1653 mp_name, CDEV_MP_NB_OBJS,
1654 0, CDEV_MP_CACHE_SZ, 0,
1655 dev_conf.socket_id);
1656 socket_ctx[dev_conf.socket_id].session_pool = sess_mp;
1659 if (!socket_ctx[dev_conf.socket_id].session_priv_pool) {
1660 char mp_name[RTE_MEMPOOL_NAMESIZE];
1661 struct rte_mempool *sess_mp;
1663 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1664 "sess_mp_priv_%u", dev_conf.socket_id);
1665 sess_mp = rte_mempool_create(mp_name,
1669 0, NULL, NULL, NULL,
1670 NULL, dev_conf.socket_id,
1672 socket_ctx[dev_conf.socket_id].session_priv_pool =
1676 if (!socket_ctx[dev_conf.socket_id].session_priv_pool ||
1677 !socket_ctx[dev_conf.socket_id].session_pool)
1678 rte_exit(EXIT_FAILURE,
1679 "Cannot create session pool on socket %d\n",
1680 dev_conf.socket_id);
1682 printf("Allocated session pool on socket %d\n",
1683 dev_conf.socket_id);
1685 if (rte_cryptodev_configure(cdev_id, &dev_conf))
1686 rte_panic("Failed to initialize cryptodev %u\n",
1689 qp_conf.nb_descriptors = CDEV_QUEUE_DESC;
1690 qp_conf.mp_session =
1691 socket_ctx[dev_conf.socket_id].session_pool;
1692 qp_conf.mp_session_private =
1693 socket_ctx[dev_conf.socket_id].session_priv_pool;
1694 for (qp = 0; qp < dev_conf.nb_queue_pairs; qp++)
1695 if (rte_cryptodev_queue_pair_setup(cdev_id, qp,
1696 &qp_conf, dev_conf.socket_id))
1697 rte_panic("Failed to setup queue %u for "
1698 "cdev_id %u\n", 0, cdev_id);
1700 if (rte_cryptodev_start(cdev_id))
1701 rte_panic("Failed to start cryptodev %u\n",
1705 /* create session pools for eth devices that implement security */
1706 RTE_ETH_FOREACH_DEV(port_id) {
1707 if ((enabled_port_mask & (1 << port_id)) &&
1708 rte_eth_dev_get_sec_ctx(port_id)) {
1709 int socket_id = rte_eth_dev_socket_id(port_id);
1711 if (!socket_ctx[socket_id].session_pool) {
1712 char mp_name[RTE_MEMPOOL_NAMESIZE];
1713 struct rte_mempool *sess_mp;
1715 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1716 "sess_mp_%u", socket_id);
1717 sess_mp = rte_mempool_create(mp_name,
1718 (CDEV_MP_NB_OBJS * 2),
1721 0, NULL, NULL, NULL,
1724 if (sess_mp == NULL)
1725 rte_exit(EXIT_FAILURE,
1726 "Cannot create session pool "
1727 "on socket %d\n", socket_id);
1729 printf("Allocated session pool "
1730 "on socket %d\n", socket_id);
1731 socket_ctx[socket_id].session_pool = sess_mp;
1743 port_init(uint16_t portid, uint64_t req_rx_offloads, uint64_t req_tx_offloads)
1745 struct rte_eth_dev_info dev_info;
1746 struct rte_eth_txconf *txconf;
1747 uint16_t nb_tx_queue, nb_rx_queue;
1748 uint16_t tx_queueid, rx_queueid, queue, lcore_id;
1749 int32_t ret, socket_id;
1750 struct lcore_conf *qconf;
1751 struct ether_addr ethaddr;
1752 struct rte_eth_conf local_port_conf = port_conf;
1754 rte_eth_dev_info_get(portid, &dev_info);
1756 /* limit allowed HW offloafs, as user requested */
1757 dev_info.rx_offload_capa &= dev_rx_offload;
1758 dev_info.tx_offload_capa &= dev_tx_offload;
1760 printf("Configuring device port %u:\n", portid);
1762 rte_eth_macaddr_get(portid, ðaddr);
1763 ethaddr_tbl[portid].src = ETHADDR_TO_UINT64(ðaddr);
1764 print_ethaddr("Address: ", ðaddr);
1767 nb_rx_queue = get_port_nb_rx_queues(portid);
1768 nb_tx_queue = nb_lcores;
1770 if (nb_rx_queue > dev_info.max_rx_queues)
1771 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1772 "(max rx queue is %u)\n",
1773 nb_rx_queue, dev_info.max_rx_queues);
1775 if (nb_tx_queue > dev_info.max_tx_queues)
1776 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1777 "(max tx queue is %u)\n",
1778 nb_tx_queue, dev_info.max_tx_queues);
1780 printf("Creating queues: nb_rx_queue=%d nb_tx_queue=%u...\n",
1781 nb_rx_queue, nb_tx_queue);
1784 local_port_conf.rxmode.max_rx_pkt_len = frame_size;
1785 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
1788 local_port_conf.rxmode.offloads |= req_rx_offloads;
1789 local_port_conf.txmode.offloads |= req_tx_offloads;
1791 /* Check that all required capabilities are supported */
1792 if ((local_port_conf.rxmode.offloads & dev_info.rx_offload_capa) !=
1793 local_port_conf.rxmode.offloads)
1794 rte_exit(EXIT_FAILURE,
1795 "Error: port %u required RX offloads: 0x%" PRIx64
1796 ", avaialbe RX offloads: 0x%" PRIx64 "\n",
1797 portid, local_port_conf.rxmode.offloads,
1798 dev_info.rx_offload_capa);
1800 if ((local_port_conf.txmode.offloads & dev_info.tx_offload_capa) !=
1801 local_port_conf.txmode.offloads)
1802 rte_exit(EXIT_FAILURE,
1803 "Error: port %u required TX offloads: 0x%" PRIx64
1804 ", avaialbe TX offloads: 0x%" PRIx64 "\n",
1805 portid, local_port_conf.txmode.offloads,
1806 dev_info.tx_offload_capa);
1808 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
1809 local_port_conf.txmode.offloads |=
1810 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
1812 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_IPV4_CKSUM)
1813 local_port_conf.txmode.offloads |= DEV_TX_OFFLOAD_IPV4_CKSUM;
1815 printf("port %u configurng rx_offloads=0x%" PRIx64
1816 ", tx_offloads=0x%" PRIx64 "\n",
1817 portid, local_port_conf.rxmode.offloads,
1818 local_port_conf.txmode.offloads);
1820 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
1821 dev_info.flow_type_rss_offloads;
1822 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
1823 port_conf.rx_adv_conf.rss_conf.rss_hf) {
1824 printf("Port %u modified RSS hash function based on hardware support,"
1825 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
1827 port_conf.rx_adv_conf.rss_conf.rss_hf,
1828 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
1831 ret = rte_eth_dev_configure(portid, nb_rx_queue, nb_tx_queue,
1834 rte_exit(EXIT_FAILURE, "Cannot configure device: "
1835 "err=%d, port=%d\n", ret, portid);
1837 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
1839 rte_exit(EXIT_FAILURE, "Cannot adjust number of descriptors: "
1840 "err=%d, port=%d\n", ret, portid);
1842 /* init one TX queue per lcore */
1844 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1845 if (rte_lcore_is_enabled(lcore_id) == 0)
1849 socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1854 printf("Setup txq=%u,%d,%d\n", lcore_id, tx_queueid, socket_id);
1856 txconf = &dev_info.default_txconf;
1857 txconf->offloads = local_port_conf.txmode.offloads;
1859 ret = rte_eth_tx_queue_setup(portid, tx_queueid, nb_txd,
1862 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
1863 "err=%d, port=%d\n", ret, portid);
1865 qconf = &lcore_conf[lcore_id];
1866 qconf->tx_queue_id[portid] = tx_queueid;
1868 /* Pre-populate pkt offloads based on capabilities */
1869 qconf->outbound.ipv4_offloads = PKT_TX_IPV4;
1870 qconf->outbound.ipv6_offloads = PKT_TX_IPV6;
1871 if (local_port_conf.txmode.offloads & DEV_TX_OFFLOAD_IPV4_CKSUM)
1872 qconf->outbound.ipv4_offloads |= PKT_TX_IP_CKSUM;
1876 /* init RX queues */
1877 for (queue = 0; queue < qconf->nb_rx_queue; ++queue) {
1878 struct rte_eth_rxconf rxq_conf;
1880 if (portid != qconf->rx_queue_list[queue].port_id)
1883 rx_queueid = qconf->rx_queue_list[queue].queue_id;
1885 printf("Setup rxq=%d,%d,%d\n", portid, rx_queueid,
1888 rxq_conf = dev_info.default_rxconf;
1889 rxq_conf.offloads = local_port_conf.rxmode.offloads;
1890 ret = rte_eth_rx_queue_setup(portid, rx_queueid,
1891 nb_rxd, socket_id, &rxq_conf,
1892 socket_ctx[socket_id].mbuf_pool);
1894 rte_exit(EXIT_FAILURE,
1895 "rte_eth_rx_queue_setup: err=%d, "
1896 "port=%d\n", ret, portid);
1903 pool_init(struct socket_ctx *ctx, int32_t socket_id, uint32_t nb_mbuf)
1906 uint32_t buff_size = frame_size ? (frame_size + RTE_PKTMBUF_HEADROOM) :
1907 RTE_MBUF_DEFAULT_BUF_SIZE;
1910 snprintf(s, sizeof(s), "mbuf_pool_%d", socket_id);
1911 ctx->mbuf_pool = rte_pktmbuf_pool_create(s, nb_mbuf,
1912 MEMPOOL_CACHE_SIZE, ipsec_metadata_size(),
1915 if (ctx->mbuf_pool == NULL)
1916 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n",
1919 printf("Allocated mbuf pool on socket %d\n", socket_id);
1923 inline_ipsec_event_esn_overflow(struct rte_security_ctx *ctx, uint64_t md)
1925 struct ipsec_sa *sa;
1927 /* For inline protocol processing, the metadata in the event will
1928 * uniquely identify the security session which raised the event.
1929 * Application would then need the userdata it had registered with the
1930 * security session to process the event.
1933 sa = (struct ipsec_sa *)rte_security_get_userdata(ctx, md);
1936 /* userdata could not be retrieved */
1940 /* Sequence number over flow. SA need to be re-established */
1946 inline_ipsec_event_callback(uint16_t port_id, enum rte_eth_event_type type,
1947 void *param, void *ret_param)
1950 struct rte_eth_event_ipsec_desc *event_desc = NULL;
1951 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
1952 rte_eth_dev_get_sec_ctx(port_id);
1954 RTE_SET_USED(param);
1956 if (type != RTE_ETH_EVENT_IPSEC)
1959 event_desc = ret_param;
1960 if (event_desc == NULL) {
1961 printf("Event descriptor not set\n");
1965 md = event_desc->metadata;
1967 if (event_desc->subtype == RTE_ETH_EVENT_IPSEC_ESN_OVERFLOW)
1968 return inline_ipsec_event_esn_overflow(ctx, md);
1969 else if (event_desc->subtype >= RTE_ETH_EVENT_IPSEC_MAX) {
1970 printf("Invalid IPsec event reported\n");
1978 main(int32_t argc, char **argv)
1984 uint64_t req_rx_offloads, req_tx_offloads;
1987 ret = rte_eal_init(argc, argv);
1989 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1993 /* parse application arguments (after the EAL ones) */
1994 ret = parse_args(argc, argv);
1996 rte_exit(EXIT_FAILURE, "Invalid parameters\n");
1998 if ((unprotected_port_mask & enabled_port_mask) !=
1999 unprotected_port_mask)
2000 rte_exit(EXIT_FAILURE, "Invalid unprotected portmask 0x%x\n",
2001 unprotected_port_mask);
2003 if (check_params() < 0)
2004 rte_exit(EXIT_FAILURE, "check_params failed\n");
2006 ret = init_lcore_rx_queues();
2008 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2010 nb_lcores = rte_lcore_count();
2012 /* Replicate each context per socket */
2013 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2014 if (rte_lcore_is_enabled(lcore_id) == 0)
2018 socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2022 if (socket_ctx[socket_id].mbuf_pool)
2025 sa_init(&socket_ctx[socket_id], socket_id);
2027 sp4_init(&socket_ctx[socket_id], socket_id);
2029 sp6_init(&socket_ctx[socket_id], socket_id);
2031 rt_init(&socket_ctx[socket_id], socket_id);
2033 pool_init(&socket_ctx[socket_id], socket_id, NB_MBUF);
2036 RTE_ETH_FOREACH_DEV(portid) {
2037 if ((enabled_port_mask & (1 << portid)) == 0)
2040 sa_check_offloads(portid, &req_rx_offloads, &req_tx_offloads);
2041 port_init(portid, req_rx_offloads, req_tx_offloads);
2047 RTE_ETH_FOREACH_DEV(portid) {
2048 if ((enabled_port_mask & (1 << portid)) == 0)
2052 ret = rte_eth_dev_start(portid);
2054 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: "
2055 "err=%d, port=%d\n", ret, portid);
2057 * If enabled, put device in promiscuous mode.
2058 * This allows IO forwarding mode to forward packets
2059 * to itself through 2 cross-connected ports of the
2063 rte_eth_promiscuous_enable(portid);
2065 rte_eth_dev_callback_register(portid,
2066 RTE_ETH_EVENT_IPSEC, inline_ipsec_event_callback, NULL);
2069 check_all_ports_link_status(enabled_port_mask);
2071 /* launch per-lcore init on every lcore */
2072 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2073 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2074 if (rte_eal_wait_lcore(lcore_id) < 0)