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>
45 #include <rte_ip_frag.h>
51 #define RTE_LOGTYPE_IPSEC RTE_LOGTYPE_USER1
53 #define MAX_JUMBO_PKT_LEN 9600
55 #define MEMPOOL_CACHE_SIZE 256
57 #define NB_MBUF (32000)
59 #define CDEV_QUEUE_DESC 2048
60 #define CDEV_MAP_ENTRIES 16384
61 #define CDEV_MP_NB_OBJS 1024
62 #define CDEV_MP_CACHE_SZ 64
63 #define MAX_QUEUE_PAIRS 1
65 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
69 /* Configure how many packets ahead to prefetch, when reading packets */
70 #define PREFETCH_OFFSET 3
72 #define MAX_RX_QUEUE_PER_LCORE 16
74 #define MAX_LCORE_PARAMS 1024
76 #define UNPROTECTED_PORT(port) (unprotected_port_mask & (1 << portid))
79 * Configurable number of RX/TX ring descriptors
81 #define IPSEC_SECGW_RX_DESC_DEFAULT 1024
82 #define IPSEC_SECGW_TX_DESC_DEFAULT 1024
83 static uint16_t nb_rxd = IPSEC_SECGW_RX_DESC_DEFAULT;
84 static uint16_t nb_txd = IPSEC_SECGW_TX_DESC_DEFAULT;
86 #if RTE_BYTE_ORDER != RTE_LITTLE_ENDIAN
87 #define __BYTES_TO_UINT64(a, b, c, d, e, f, g, h) \
88 (((uint64_t)((a) & 0xff) << 56) | \
89 ((uint64_t)((b) & 0xff) << 48) | \
90 ((uint64_t)((c) & 0xff) << 40) | \
91 ((uint64_t)((d) & 0xff) << 32) | \
92 ((uint64_t)((e) & 0xff) << 24) | \
93 ((uint64_t)((f) & 0xff) << 16) | \
94 ((uint64_t)((g) & 0xff) << 8) | \
95 ((uint64_t)(h) & 0xff))
97 #define __BYTES_TO_UINT64(a, b, c, d, e, f, g, h) \
98 (((uint64_t)((h) & 0xff) << 56) | \
99 ((uint64_t)((g) & 0xff) << 48) | \
100 ((uint64_t)((f) & 0xff) << 40) | \
101 ((uint64_t)((e) & 0xff) << 32) | \
102 ((uint64_t)((d) & 0xff) << 24) | \
103 ((uint64_t)((c) & 0xff) << 16) | \
104 ((uint64_t)((b) & 0xff) << 8) | \
105 ((uint64_t)(a) & 0xff))
107 #define ETHADDR(a, b, c, d, e, f) (__BYTES_TO_UINT64(a, b, c, d, e, f, 0, 0))
109 #define ETHADDR_TO_UINT64(addr) __BYTES_TO_UINT64( \
110 (addr)->addr_bytes[0], (addr)->addr_bytes[1], \
111 (addr)->addr_bytes[2], (addr)->addr_bytes[3], \
112 (addr)->addr_bytes[4], (addr)->addr_bytes[5], \
115 #define FRAG_TBL_BUCKET_ENTRIES 4
116 #define MAX_FRAG_TTL_NS (10LL * NS_PER_S)
118 #define MTU_TO_FRAMELEN(x) ((x) + RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN)
120 /* port/source ethernet addr and destination ethernet addr */
121 struct ethaddr_info {
125 struct ethaddr_info ethaddr_tbl[RTE_MAX_ETHPORTS] = {
126 { 0, ETHADDR(0x00, 0x16, 0x3e, 0x7e, 0x94, 0x9a) },
127 { 0, ETHADDR(0x00, 0x16, 0x3e, 0x22, 0xa1, 0xd9) },
128 { 0, ETHADDR(0x00, 0x16, 0x3e, 0x08, 0x69, 0x26) },
129 { 0, ETHADDR(0x00, 0x16, 0x3e, 0x49, 0x9e, 0xdd) }
132 struct flow_info flow_info_tbl[RTE_MAX_ETHPORTS];
134 #define CMD_LINE_OPT_CONFIG "config"
135 #define CMD_LINE_OPT_SINGLE_SA "single-sa"
136 #define CMD_LINE_OPT_CRYPTODEV_MASK "cryptodev_mask"
137 #define CMD_LINE_OPT_RX_OFFLOAD "rxoffload"
138 #define CMD_LINE_OPT_TX_OFFLOAD "txoffload"
139 #define CMD_LINE_OPT_REASSEMBLE "reassemble"
140 #define CMD_LINE_OPT_MTU "mtu"
141 #define CMD_LINE_OPT_FRAG_TTL "frag-ttl"
144 /* long options mapped to a short option */
146 /* first long only option value must be >= 256, so that we won't
147 * conflict with short options
149 CMD_LINE_OPT_MIN_NUM = 256,
150 CMD_LINE_OPT_CONFIG_NUM,
151 CMD_LINE_OPT_SINGLE_SA_NUM,
152 CMD_LINE_OPT_CRYPTODEV_MASK_NUM,
153 CMD_LINE_OPT_RX_OFFLOAD_NUM,
154 CMD_LINE_OPT_TX_OFFLOAD_NUM,
155 CMD_LINE_OPT_REASSEMBLE_NUM,
156 CMD_LINE_OPT_MTU_NUM,
157 CMD_LINE_OPT_FRAG_TTL_NUM,
160 static const struct option lgopts[] = {
161 {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
162 {CMD_LINE_OPT_SINGLE_SA, 1, 0, CMD_LINE_OPT_SINGLE_SA_NUM},
163 {CMD_LINE_OPT_CRYPTODEV_MASK, 1, 0, CMD_LINE_OPT_CRYPTODEV_MASK_NUM},
164 {CMD_LINE_OPT_RX_OFFLOAD, 1, 0, CMD_LINE_OPT_RX_OFFLOAD_NUM},
165 {CMD_LINE_OPT_TX_OFFLOAD, 1, 0, CMD_LINE_OPT_TX_OFFLOAD_NUM},
166 {CMD_LINE_OPT_REASSEMBLE, 1, 0, CMD_LINE_OPT_REASSEMBLE_NUM},
167 {CMD_LINE_OPT_MTU, 1, 0, CMD_LINE_OPT_MTU_NUM},
168 {CMD_LINE_OPT_FRAG_TTL, 1, 0, CMD_LINE_OPT_FRAG_TTL_NUM},
172 /* mask of enabled ports */
173 static uint32_t enabled_port_mask;
174 static uint64_t enabled_cryptodev_mask = UINT64_MAX;
175 static uint32_t unprotected_port_mask;
176 static int32_t promiscuous_on = 1;
177 static int32_t numa_on = 1; /**< NUMA is enabled by default. */
178 static uint32_t nb_lcores;
179 static uint32_t single_sa;
180 static uint32_t single_sa_idx;
183 * RX/TX HW offload capabilities to enable/use on ethernet ports.
184 * By default all capabilities are enabled.
186 static uint64_t dev_rx_offload = UINT64_MAX;
187 static uint64_t dev_tx_offload = UINT64_MAX;
190 * global values that determine multi-seg policy
192 static uint32_t frag_tbl_sz;
193 static uint32_t frame_buf_size = RTE_MBUF_DEFAULT_BUF_SIZE;
194 static uint32_t mtu_size = RTE_ETHER_MTU;
195 static uint64_t frag_ttl_ns = MAX_FRAG_TTL_NS;
197 /* application wide librte_ipsec/SA parameters */
198 struct app_sa_prm app_sa_prm = {
200 .cache_sz = SA_CACHE_SZ
202 static const char *cfgfile;
204 struct lcore_rx_queue {
207 } __rte_cache_aligned;
209 struct lcore_params {
213 } __rte_cache_aligned;
215 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
217 static struct lcore_params *lcore_params;
218 static uint16_t nb_lcore_params;
220 static struct rte_hash *cdev_map_in;
221 static struct rte_hash *cdev_map_out;
225 struct rte_mbuf *m_table[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
229 uint16_t nb_rx_queue;
230 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
231 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
232 struct buffer tx_mbufs[RTE_MAX_ETHPORTS];
233 struct ipsec_ctx inbound;
234 struct ipsec_ctx outbound;
235 struct rt_ctx *rt4_ctx;
236 struct rt_ctx *rt6_ctx;
238 struct rte_ip_frag_tbl *tbl;
239 struct rte_mempool *pool_dir;
240 struct rte_mempool *pool_indir;
241 struct rte_ip_frag_death_row dr;
243 } __rte_cache_aligned;
245 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
247 static struct rte_eth_conf port_conf = {
249 .mq_mode = ETH_MQ_RX_RSS,
250 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
252 .offloads = DEV_RX_OFFLOAD_CHECKSUM,
257 .rss_hf = ETH_RSS_IP | ETH_RSS_UDP |
258 ETH_RSS_TCP | ETH_RSS_SCTP,
262 .mq_mode = ETH_MQ_TX_NONE,
266 static struct socket_ctx socket_ctx[NB_SOCKETS];
269 * Determine is multi-segment support required:
270 * - either frame buffer size is smaller then mtu
271 * - or reassmeble support is requested
274 multi_seg_required(void)
276 return (MTU_TO_FRAMELEN(mtu_size) + RTE_PKTMBUF_HEADROOM >
277 frame_buf_size || frag_tbl_sz != 0);
281 adjust_ipv4_pktlen(struct rte_mbuf *m, const struct rte_ipv4_hdr *iph,
286 plen = rte_be_to_cpu_16(iph->total_length) + l2_len;
287 if (plen < m->pkt_len) {
288 trim = m->pkt_len - plen;
289 rte_pktmbuf_trim(m, trim);
294 adjust_ipv6_pktlen(struct rte_mbuf *m, const struct rte_ipv6_hdr *iph,
299 plen = rte_be_to_cpu_16(iph->payload_len) + sizeof(*iph) + l2_len;
300 if (plen < m->pkt_len) {
301 trim = m->pkt_len - plen;
302 rte_pktmbuf_trim(m, trim);
307 prepare_one_packet(struct rte_mbuf *pkt, struct ipsec_traffic *t)
309 const struct rte_ether_hdr *eth;
310 const struct rte_ipv4_hdr *iph4;
311 const struct rte_ipv6_hdr *iph6;
313 eth = rte_pktmbuf_mtod(pkt, const struct rte_ether_hdr *);
314 if (eth->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
316 iph4 = (const struct rte_ipv4_hdr *)rte_pktmbuf_adj(pkt,
318 adjust_ipv4_pktlen(pkt, iph4, 0);
320 if (iph4->next_proto_id == IPPROTO_ESP)
321 t->ipsec.pkts[(t->ipsec.num)++] = pkt;
323 t->ip4.data[t->ip4.num] = &iph4->next_proto_id;
324 t->ip4.pkts[(t->ip4.num)++] = pkt;
327 pkt->l3_len = sizeof(*iph4);
328 pkt->packet_type |= RTE_PTYPE_L3_IPV4;
329 } else if (eth->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
331 size_t l3len, ext_len;
334 /* get protocol type */
335 iph6 = (const struct rte_ipv6_hdr *)rte_pktmbuf_adj(pkt,
337 adjust_ipv6_pktlen(pkt, iph6, 0);
339 next_proto = iph6->proto;
341 /* determine l3 header size up to ESP extension */
342 l3len = sizeof(struct ip6_hdr);
343 p = rte_pktmbuf_mtod(pkt, uint8_t *);
344 while (next_proto != IPPROTO_ESP && l3len < pkt->data_len &&
345 (next_proto = rte_ipv6_get_next_ext(p + l3len,
346 next_proto, &ext_len)) >= 0)
349 /* drop packet when IPv6 header exceeds first segment length */
350 if (unlikely(l3len > pkt->data_len)) {
351 rte_pktmbuf_free(pkt);
355 if (next_proto == IPPROTO_ESP)
356 t->ipsec.pkts[(t->ipsec.num)++] = pkt;
358 t->ip6.data[t->ip6.num] = &iph6->proto;
359 t->ip6.pkts[(t->ip6.num)++] = pkt;
363 pkt->packet_type |= RTE_PTYPE_L3_IPV6;
365 /* Unknown/Unsupported type, drop the packet */
366 RTE_LOG(ERR, IPSEC, "Unsupported packet type 0x%x\n",
367 rte_be_to_cpu_16(eth->ether_type));
368 rte_pktmbuf_free(pkt);
372 /* Check if the packet has been processed inline. For inline protocol
373 * processed packets, the metadata in the mbuf can be used to identify
374 * the security processing done on the packet. The metadata will be
375 * used to retrieve the application registered userdata associated
376 * with the security session.
379 if (pkt->ol_flags & PKT_RX_SEC_OFFLOAD) {
381 struct ipsec_mbuf_metadata *priv;
382 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
383 rte_eth_dev_get_sec_ctx(
386 /* Retrieve the userdata registered. Here, the userdata
387 * registered is the SA pointer.
390 sa = (struct ipsec_sa *)
391 rte_security_get_userdata(ctx, pkt->udata64);
394 /* userdata could not be retrieved */
398 /* Save SA as priv member in mbuf. This will be used in the
399 * IPsec selector(SP-SA) check.
402 priv = get_priv(pkt);
408 prepare_traffic(struct rte_mbuf **pkts, struct ipsec_traffic *t,
417 for (i = 0; i < (nb_pkts - PREFETCH_OFFSET); i++) {
418 rte_prefetch0(rte_pktmbuf_mtod(pkts[i + PREFETCH_OFFSET],
420 prepare_one_packet(pkts[i], t);
422 /* Process left packets */
423 for (; i < nb_pkts; i++)
424 prepare_one_packet(pkts[i], t);
428 prepare_tx_pkt(struct rte_mbuf *pkt, uint16_t port,
429 const struct lcore_conf *qconf)
432 struct rte_ether_hdr *ethhdr;
434 ip = rte_pktmbuf_mtod(pkt, struct ip *);
436 ethhdr = (struct rte_ether_hdr *)
437 rte_pktmbuf_prepend(pkt, RTE_ETHER_HDR_LEN);
439 if (ip->ip_v == IPVERSION) {
440 pkt->ol_flags |= qconf->outbound.ipv4_offloads;
441 pkt->l3_len = sizeof(struct ip);
442 pkt->l2_len = RTE_ETHER_HDR_LEN;
446 /* calculate IPv4 cksum in SW */
447 if ((pkt->ol_flags & PKT_TX_IP_CKSUM) == 0)
448 ip->ip_sum = rte_ipv4_cksum((struct rte_ipv4_hdr *)ip);
450 ethhdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
452 pkt->ol_flags |= qconf->outbound.ipv6_offloads;
453 pkt->l3_len = sizeof(struct ip6_hdr);
454 pkt->l2_len = RTE_ETHER_HDR_LEN;
456 ethhdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
459 memcpy(ðhdr->s_addr, ðaddr_tbl[port].src,
460 sizeof(struct rte_ether_addr));
461 memcpy(ðhdr->d_addr, ðaddr_tbl[port].dst,
462 sizeof(struct rte_ether_addr));
466 prepare_tx_burst(struct rte_mbuf *pkts[], uint16_t nb_pkts, uint16_t port,
467 const struct lcore_conf *qconf)
470 const int32_t prefetch_offset = 2;
472 for (i = 0; i < (nb_pkts - prefetch_offset); i++) {
473 rte_mbuf_prefetch_part2(pkts[i + prefetch_offset]);
474 prepare_tx_pkt(pkts[i], port, qconf);
476 /* Process left packets */
477 for (; i < nb_pkts; i++)
478 prepare_tx_pkt(pkts[i], port, qconf);
481 /* Send burst of packets on an output interface */
482 static inline int32_t
483 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
485 struct rte_mbuf **m_table;
489 queueid = qconf->tx_queue_id[port];
490 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
492 prepare_tx_burst(m_table, n, port, qconf);
494 ret = rte_eth_tx_burst(port, queueid, m_table, n);
495 if (unlikely(ret < n)) {
497 rte_pktmbuf_free(m_table[ret]);
505 * Helper function to fragment and queue for TX one packet.
507 static inline uint32_t
508 send_fragment_packet(struct lcore_conf *qconf, struct rte_mbuf *m,
509 uint16_t port, uint8_t proto)
515 tbl = qconf->tx_mbufs + port;
518 /* free space for new fragments */
519 if (len + RTE_LIBRTE_IP_FRAG_MAX_FRAG >= RTE_DIM(tbl->m_table)) {
520 send_burst(qconf, len, port);
524 n = RTE_DIM(tbl->m_table) - len;
526 if (proto == IPPROTO_IP)
527 rc = rte_ipv4_fragment_packet(m, tbl->m_table + len,
528 n, mtu_size, qconf->frag.pool_dir,
529 qconf->frag.pool_indir);
531 rc = rte_ipv6_fragment_packet(m, tbl->m_table + len,
532 n, mtu_size, qconf->frag.pool_dir,
533 qconf->frag.pool_indir);
539 "%s: failed to fragment packet with size %u, "
541 __func__, m->pkt_len, rte_errno);
547 /* Enqueue a single packet, and send burst if queue is filled */
548 static inline int32_t
549 send_single_packet(struct rte_mbuf *m, uint16_t port, uint8_t proto)
553 struct lcore_conf *qconf;
555 lcore_id = rte_lcore_id();
557 qconf = &lcore_conf[lcore_id];
558 len = qconf->tx_mbufs[port].len;
560 if (m->pkt_len <= mtu_size) {
561 qconf->tx_mbufs[port].m_table[len] = m;
564 /* need to fragment the packet */
565 } else if (frag_tbl_sz > 0)
566 len = send_fragment_packet(qconf, m, port, proto);
570 /* enough pkts to be sent */
571 if (unlikely(len == MAX_PKT_BURST)) {
572 send_burst(qconf, MAX_PKT_BURST, port);
576 qconf->tx_mbufs[port].len = len;
581 inbound_sp_sa(struct sp_ctx *sp, struct sa_ctx *sa, struct traffic_type *ip,
585 uint32_t i, j, res, sa_idx;
587 if (ip->num == 0 || sp == NULL)
590 rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
591 ip->num, DEFAULT_MAX_CATEGORIES);
594 for (i = 0; i < ip->num; i++) {
601 if (res == DISCARD) {
606 /* Only check SPI match for processed IPSec packets */
607 if (i < lim && ((m->ol_flags & PKT_RX_SEC_OFFLOAD) == 0)) {
613 if (!inbound_sa_check(sa, m, sa_idx)) {
623 split46_traffic(struct ipsec_traffic *trf, struct rte_mbuf *mb[], uint32_t num)
632 for (i = 0; i < num; i++) {
635 ip = rte_pktmbuf_mtod(m, struct ip *);
637 if (ip->ip_v == IPVERSION) {
638 trf->ip4.pkts[n4] = m;
639 trf->ip4.data[n4] = rte_pktmbuf_mtod_offset(m,
640 uint8_t *, offsetof(struct ip, ip_p));
642 } else if (ip->ip_v == IP6_VERSION) {
643 trf->ip6.pkts[n6] = m;
644 trf->ip6.data[n6] = rte_pktmbuf_mtod_offset(m,
646 offsetof(struct ip6_hdr, ip6_nxt));
658 process_pkts_inbound(struct ipsec_ctx *ipsec_ctx,
659 struct ipsec_traffic *traffic)
661 uint16_t nb_pkts_in, n_ip4, n_ip6;
663 n_ip4 = traffic->ip4.num;
664 n_ip6 = traffic->ip6.num;
666 if (app_sa_prm.enable == 0) {
667 nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
668 traffic->ipsec.num, MAX_PKT_BURST);
669 split46_traffic(traffic, traffic->ipsec.pkts, nb_pkts_in);
671 inbound_sa_lookup(ipsec_ctx->sa_ctx, traffic->ipsec.pkts,
672 traffic->ipsec.saptr, traffic->ipsec.num);
673 ipsec_process(ipsec_ctx, traffic);
676 inbound_sp_sa(ipsec_ctx->sp4_ctx, ipsec_ctx->sa_ctx, &traffic->ip4,
679 inbound_sp_sa(ipsec_ctx->sp6_ctx, ipsec_ctx->sa_ctx, &traffic->ip6,
684 outbound_sp(struct sp_ctx *sp, struct traffic_type *ip,
685 struct traffic_type *ipsec)
688 uint32_t i, j, sa_idx;
690 if (ip->num == 0 || sp == NULL)
693 rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
694 ip->num, DEFAULT_MAX_CATEGORIES);
697 for (i = 0; i < ip->num; i++) {
699 sa_idx = ip->res[i] - 1;
700 if (ip->res[i] == DISCARD)
702 else if (ip->res[i] == BYPASS)
705 ipsec->res[ipsec->num] = sa_idx;
706 ipsec->pkts[ipsec->num++] = m;
713 process_pkts_outbound(struct ipsec_ctx *ipsec_ctx,
714 struct ipsec_traffic *traffic)
717 uint16_t idx, nb_pkts_out, i;
719 /* Drop any IPsec traffic from protected ports */
720 for (i = 0; i < traffic->ipsec.num; i++)
721 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
723 traffic->ipsec.num = 0;
725 outbound_sp(ipsec_ctx->sp4_ctx, &traffic->ip4, &traffic->ipsec);
727 outbound_sp(ipsec_ctx->sp6_ctx, &traffic->ip6, &traffic->ipsec);
729 if (app_sa_prm.enable == 0) {
731 nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ipsec.pkts,
732 traffic->ipsec.res, traffic->ipsec.num,
735 for (i = 0; i < nb_pkts_out; i++) {
736 m = traffic->ipsec.pkts[i];
737 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
738 if (ip->ip_v == IPVERSION) {
739 idx = traffic->ip4.num++;
740 traffic->ip4.pkts[idx] = m;
742 idx = traffic->ip6.num++;
743 traffic->ip6.pkts[idx] = m;
747 outbound_sa_lookup(ipsec_ctx->sa_ctx, traffic->ipsec.res,
748 traffic->ipsec.saptr, traffic->ipsec.num);
749 ipsec_process(ipsec_ctx, traffic);
754 process_pkts_inbound_nosp(struct ipsec_ctx *ipsec_ctx,
755 struct ipsec_traffic *traffic)
758 uint32_t nb_pkts_in, i, idx;
760 /* Drop any IPv4 traffic from unprotected ports */
761 for (i = 0; i < traffic->ip4.num; i++)
762 rte_pktmbuf_free(traffic->ip4.pkts[i]);
764 traffic->ip4.num = 0;
766 /* Drop any IPv6 traffic from unprotected ports */
767 for (i = 0; i < traffic->ip6.num; i++)
768 rte_pktmbuf_free(traffic->ip6.pkts[i]);
770 traffic->ip6.num = 0;
772 if (app_sa_prm.enable == 0) {
774 nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
775 traffic->ipsec.num, MAX_PKT_BURST);
777 for (i = 0; i < nb_pkts_in; i++) {
778 m = traffic->ipsec.pkts[i];
779 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
780 if (ip->ip_v == IPVERSION) {
781 idx = traffic->ip4.num++;
782 traffic->ip4.pkts[idx] = m;
784 idx = traffic->ip6.num++;
785 traffic->ip6.pkts[idx] = m;
789 inbound_sa_lookup(ipsec_ctx->sa_ctx, traffic->ipsec.pkts,
790 traffic->ipsec.saptr, traffic->ipsec.num);
791 ipsec_process(ipsec_ctx, traffic);
796 process_pkts_outbound_nosp(struct ipsec_ctx *ipsec_ctx,
797 struct ipsec_traffic *traffic)
800 uint32_t nb_pkts_out, i, n;
803 /* Drop any IPsec traffic from protected ports */
804 for (i = 0; i < traffic->ipsec.num; i++)
805 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
809 for (i = 0; i < traffic->ip4.num; i++) {
810 traffic->ipsec.pkts[n] = traffic->ip4.pkts[i];
811 traffic->ipsec.res[n++] = single_sa_idx;
814 for (i = 0; i < traffic->ip6.num; i++) {
815 traffic->ipsec.pkts[n] = traffic->ip6.pkts[i];
816 traffic->ipsec.res[n++] = single_sa_idx;
819 traffic->ip4.num = 0;
820 traffic->ip6.num = 0;
821 traffic->ipsec.num = n;
823 if (app_sa_prm.enable == 0) {
825 nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ipsec.pkts,
826 traffic->ipsec.res, traffic->ipsec.num,
829 /* They all sue the same SA (ip4 or ip6 tunnel) */
830 m = traffic->ipsec.pkts[0];
831 ip = rte_pktmbuf_mtod(m, struct ip *);
832 if (ip->ip_v == IPVERSION) {
833 traffic->ip4.num = nb_pkts_out;
834 for (i = 0; i < nb_pkts_out; i++)
835 traffic->ip4.pkts[i] = traffic->ipsec.pkts[i];
837 traffic->ip6.num = nb_pkts_out;
838 for (i = 0; i < nb_pkts_out; i++)
839 traffic->ip6.pkts[i] = traffic->ipsec.pkts[i];
842 outbound_sa_lookup(ipsec_ctx->sa_ctx, traffic->ipsec.res,
843 traffic->ipsec.saptr, traffic->ipsec.num);
844 ipsec_process(ipsec_ctx, traffic);
848 static inline int32_t
849 get_hop_for_offload_pkt(struct rte_mbuf *pkt, int is_ipv6)
851 struct ipsec_mbuf_metadata *priv;
854 priv = get_priv(pkt);
857 if (unlikely(sa == NULL)) {
858 RTE_LOG(ERR, IPSEC, "SA not saved in private data\n");
866 return (sa->portid | RTE_LPM_LOOKUP_SUCCESS);
877 route4_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
879 uint32_t hop[MAX_PKT_BURST * 2];
880 uint32_t dst_ip[MAX_PKT_BURST * 2];
883 uint16_t lpm_pkts = 0;
888 /* Need to do an LPM lookup for non-inline packets. Inline packets will
889 * have port ID in the SA
892 for (i = 0; i < nb_pkts; i++) {
893 if (!(pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD)) {
894 /* Security offload not enabled. So an LPM lookup is
895 * required to get the hop
897 offset = offsetof(struct ip, ip_dst);
898 dst_ip[lpm_pkts] = *rte_pktmbuf_mtod_offset(pkts[i],
900 dst_ip[lpm_pkts] = rte_be_to_cpu_32(dst_ip[lpm_pkts]);
905 rte_lpm_lookup_bulk((struct rte_lpm *)rt_ctx, dst_ip, hop, lpm_pkts);
909 for (i = 0; i < nb_pkts; i++) {
910 if (pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD) {
911 /* Read hop from the SA */
912 pkt_hop = get_hop_for_offload_pkt(pkts[i], 0);
914 /* Need to use hop returned by lookup */
915 pkt_hop = hop[lpm_pkts++];
918 if ((pkt_hop & RTE_LPM_LOOKUP_SUCCESS) == 0) {
919 rte_pktmbuf_free(pkts[i]);
922 send_single_packet(pkts[i], pkt_hop & 0xff, IPPROTO_IP);
927 route6_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
929 int32_t hop[MAX_PKT_BURST * 2];
930 uint8_t dst_ip[MAX_PKT_BURST * 2][16];
934 uint16_t lpm_pkts = 0;
939 /* Need to do an LPM lookup for non-inline packets. Inline packets will
940 * have port ID in the SA
943 for (i = 0; i < nb_pkts; i++) {
944 if (!(pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD)) {
945 /* Security offload not enabled. So an LPM lookup is
946 * required to get the hop
948 offset = offsetof(struct ip6_hdr, ip6_dst);
949 ip6_dst = rte_pktmbuf_mtod_offset(pkts[i], uint8_t *,
951 memcpy(&dst_ip[lpm_pkts][0], ip6_dst, 16);
956 rte_lpm6_lookup_bulk_func((struct rte_lpm6 *)rt_ctx, dst_ip, hop,
961 for (i = 0; i < nb_pkts; i++) {
962 if (pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD) {
963 /* Read hop from the SA */
964 pkt_hop = get_hop_for_offload_pkt(pkts[i], 1);
966 /* Need to use hop returned by lookup */
967 pkt_hop = hop[lpm_pkts++];
971 rte_pktmbuf_free(pkts[i]);
974 send_single_packet(pkts[i], pkt_hop & 0xff, IPPROTO_IPV6);
979 process_pkts(struct lcore_conf *qconf, struct rte_mbuf **pkts,
980 uint8_t nb_pkts, uint16_t portid)
982 struct ipsec_traffic traffic;
984 prepare_traffic(pkts, &traffic, nb_pkts);
986 if (unlikely(single_sa)) {
987 if (UNPROTECTED_PORT(portid))
988 process_pkts_inbound_nosp(&qconf->inbound, &traffic);
990 process_pkts_outbound_nosp(&qconf->outbound, &traffic);
992 if (UNPROTECTED_PORT(portid))
993 process_pkts_inbound(&qconf->inbound, &traffic);
995 process_pkts_outbound(&qconf->outbound, &traffic);
998 route4_pkts(qconf->rt4_ctx, traffic.ip4.pkts, traffic.ip4.num);
999 route6_pkts(qconf->rt6_ctx, traffic.ip6.pkts, traffic.ip6.num);
1003 drain_tx_buffers(struct lcore_conf *qconf)
1008 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
1009 buf = &qconf->tx_mbufs[portid];
1012 send_burst(qconf, buf->len, portid);
1018 drain_crypto_buffers(struct lcore_conf *qconf)
1021 struct ipsec_ctx *ctx;
1023 /* drain inbound buffers*/
1024 ctx = &qconf->inbound;
1025 for (i = 0; i != ctx->nb_qps; i++) {
1026 if (ctx->tbl[i].len != 0)
1027 enqueue_cop_burst(ctx->tbl + i);
1030 /* drain outbound buffers*/
1031 ctx = &qconf->outbound;
1032 for (i = 0; i != ctx->nb_qps; i++) {
1033 if (ctx->tbl[i].len != 0)
1034 enqueue_cop_burst(ctx->tbl + i);
1039 drain_inbound_crypto_queues(const struct lcore_conf *qconf,
1040 struct ipsec_ctx *ctx)
1043 struct ipsec_traffic trf;
1045 if (app_sa_prm.enable == 0) {
1047 /* dequeue packets from crypto-queue */
1048 n = ipsec_inbound_cqp_dequeue(ctx, trf.ipsec.pkts,
1049 RTE_DIM(trf.ipsec.pkts));
1054 /* split traffic by ipv4-ipv6 */
1055 split46_traffic(&trf, trf.ipsec.pkts, n);
1057 ipsec_cqp_process(ctx, &trf);
1059 /* process ipv4 packets */
1060 if (trf.ip4.num != 0) {
1061 inbound_sp_sa(ctx->sp4_ctx, ctx->sa_ctx, &trf.ip4, 0);
1062 route4_pkts(qconf->rt4_ctx, trf.ip4.pkts, trf.ip4.num);
1065 /* process ipv6 packets */
1066 if (trf.ip6.num != 0) {
1067 inbound_sp_sa(ctx->sp6_ctx, ctx->sa_ctx, &trf.ip6, 0);
1068 route6_pkts(qconf->rt6_ctx, trf.ip6.pkts, trf.ip6.num);
1073 drain_outbound_crypto_queues(const struct lcore_conf *qconf,
1074 struct ipsec_ctx *ctx)
1077 struct ipsec_traffic trf;
1079 if (app_sa_prm.enable == 0) {
1081 /* dequeue packets from crypto-queue */
1082 n = ipsec_outbound_cqp_dequeue(ctx, trf.ipsec.pkts,
1083 RTE_DIM(trf.ipsec.pkts));
1088 /* split traffic by ipv4-ipv6 */
1089 split46_traffic(&trf, trf.ipsec.pkts, n);
1091 ipsec_cqp_process(ctx, &trf);
1093 /* process ipv4 packets */
1094 if (trf.ip4.num != 0)
1095 route4_pkts(qconf->rt4_ctx, trf.ip4.pkts, trf.ip4.num);
1097 /* process ipv6 packets */
1098 if (trf.ip6.num != 0)
1099 route6_pkts(qconf->rt6_ctx, trf.ip6.pkts, trf.ip6.num);
1102 /* main processing loop */
1104 main_loop(__attribute__((unused)) void *dummy)
1106 struct rte_mbuf *pkts[MAX_PKT_BURST];
1108 uint64_t prev_tsc, diff_tsc, cur_tsc;
1112 struct lcore_conf *qconf;
1113 int32_t rc, socket_id;
1114 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
1115 / US_PER_S * BURST_TX_DRAIN_US;
1116 struct lcore_rx_queue *rxql;
1119 lcore_id = rte_lcore_id();
1120 qconf = &lcore_conf[lcore_id];
1121 rxql = qconf->rx_queue_list;
1122 socket_id = rte_lcore_to_socket_id(lcore_id);
1124 qconf->rt4_ctx = socket_ctx[socket_id].rt_ip4;
1125 qconf->rt6_ctx = socket_ctx[socket_id].rt_ip6;
1126 qconf->inbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_in;
1127 qconf->inbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_in;
1128 qconf->inbound.sa_ctx = socket_ctx[socket_id].sa_in;
1129 qconf->inbound.cdev_map = cdev_map_in;
1130 qconf->inbound.session_pool = socket_ctx[socket_id].session_pool;
1131 qconf->inbound.session_priv_pool =
1132 socket_ctx[socket_id].session_priv_pool;
1133 qconf->outbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_out;
1134 qconf->outbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_out;
1135 qconf->outbound.sa_ctx = socket_ctx[socket_id].sa_out;
1136 qconf->outbound.cdev_map = cdev_map_out;
1137 qconf->outbound.session_pool = socket_ctx[socket_id].session_pool;
1138 qconf->outbound.session_priv_pool =
1139 socket_ctx[socket_id].session_priv_pool;
1140 qconf->frag.pool_dir = socket_ctx[socket_id].mbuf_pool;
1141 qconf->frag.pool_indir = socket_ctx[socket_id].mbuf_pool_indir;
1143 rc = ipsec_sad_lcore_cache_init(app_sa_prm.cache_sz);
1146 "SAD cache init on lcore %u, failed with code: %d\n",
1151 if (qconf->nb_rx_queue == 0) {
1152 RTE_LOG(DEBUG, IPSEC, "lcore %u has nothing to do\n",
1157 RTE_LOG(INFO, IPSEC, "entering main loop on lcore %u\n", lcore_id);
1159 for (i = 0; i < qconf->nb_rx_queue; i++) {
1160 portid = rxql[i].port_id;
1161 queueid = rxql[i].queue_id;
1162 RTE_LOG(INFO, IPSEC,
1163 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
1164 lcore_id, portid, queueid);
1168 cur_tsc = rte_rdtsc();
1170 /* TX queue buffer drain */
1171 diff_tsc = cur_tsc - prev_tsc;
1173 if (unlikely(diff_tsc > drain_tsc)) {
1174 drain_tx_buffers(qconf);
1175 drain_crypto_buffers(qconf);
1179 for (i = 0; i < qconf->nb_rx_queue; ++i) {
1181 /* Read packets from RX queues */
1182 portid = rxql[i].port_id;
1183 queueid = rxql[i].queue_id;
1184 nb_rx = rte_eth_rx_burst(portid, queueid,
1185 pkts, MAX_PKT_BURST);
1188 process_pkts(qconf, pkts, nb_rx, portid);
1190 /* dequeue and process completed crypto-ops */
1191 if (UNPROTECTED_PORT(portid))
1192 drain_inbound_crypto_queues(qconf,
1195 drain_outbound_crypto_queues(qconf,
1209 if (lcore_params == NULL) {
1210 printf("Error: No port/queue/core mappings\n");
1214 for (i = 0; i < nb_lcore_params; ++i) {
1215 lcore = lcore_params[i].lcore_id;
1216 if (!rte_lcore_is_enabled(lcore)) {
1217 printf("error: lcore %hhu is not enabled in "
1218 "lcore mask\n", lcore);
1221 socket_id = rte_lcore_to_socket_id(lcore);
1222 if (socket_id != 0 && numa_on == 0) {
1223 printf("warning: lcore %hhu is on socket %d "
1227 portid = lcore_params[i].port_id;
1228 if ((enabled_port_mask & (1 << portid)) == 0) {
1229 printf("port %u is not enabled in port mask\n", portid);
1232 if (!rte_eth_dev_is_valid_port(portid)) {
1233 printf("port %u is not present on the board\n", portid);
1241 get_port_nb_rx_queues(const uint16_t port)
1246 for (i = 0; i < nb_lcore_params; ++i) {
1247 if (lcore_params[i].port_id == port &&
1248 lcore_params[i].queue_id > queue)
1249 queue = lcore_params[i].queue_id;
1251 return (uint8_t)(++queue);
1255 init_lcore_rx_queues(void)
1257 uint16_t i, nb_rx_queue;
1260 for (i = 0; i < nb_lcore_params; ++i) {
1261 lcore = lcore_params[i].lcore_id;
1262 nb_rx_queue = lcore_conf[lcore].nb_rx_queue;
1263 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1264 printf("error: too many queues (%u) for lcore: %u\n",
1265 nb_rx_queue + 1, lcore);
1268 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1269 lcore_params[i].port_id;
1270 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1271 lcore_params[i].queue_id;
1272 lcore_conf[lcore].nb_rx_queue++;
1279 print_usage(const char *prgname)
1281 fprintf(stderr, "%s [EAL options] --"
1287 " [-w REPLAY_WINDOW_SIZE]"
1292 " --config (port,queue,lcore)[,(port,queue,lcore)]"
1293 " [--single-sa SAIDX]"
1294 " [--cryptodev_mask MASK]"
1295 " [--" CMD_LINE_OPT_RX_OFFLOAD " RX_OFFLOAD_MASK]"
1296 " [--" CMD_LINE_OPT_TX_OFFLOAD " TX_OFFLOAD_MASK]"
1297 " [--" CMD_LINE_OPT_REASSEMBLE " REASSEMBLE_TABLE_SIZE]"
1298 " [--" CMD_LINE_OPT_MTU " MTU]"
1300 " -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
1301 " -P : Enable promiscuous mode\n"
1302 " -u PORTMASK: Hexadecimal bitmask of unprotected ports\n"
1303 " -j FRAMESIZE: Data buffer size, minimum (and default)\n"
1304 " value: RTE_MBUF_DEFAULT_BUF_SIZE\n"
1305 " -l enables code-path that uses librte_ipsec\n"
1306 " -w REPLAY_WINDOW_SIZE specifies IPsec SQN replay window\n"
1307 " size for each SA\n"
1309 " -a enables SA SQN atomic behaviour\n"
1310 " -c specifies inbound SAD cache size,\n"
1311 " zero value disables the cache (default value: 128)\n"
1312 " -f CONFIG_FILE: Configuration file\n"
1313 " --config (port,queue,lcore): Rx queue configuration\n"
1314 " --single-sa SAIDX: Use single SA index for outbound traffic,\n"
1315 " bypassing the SP\n"
1316 " --cryptodev_mask MASK: Hexadecimal bitmask of the crypto\n"
1317 " devices to configure\n"
1318 " --" CMD_LINE_OPT_RX_OFFLOAD
1319 ": bitmask of the RX HW offload capabilities to enable/use\n"
1320 " (DEV_RX_OFFLOAD_*)\n"
1321 " --" CMD_LINE_OPT_TX_OFFLOAD
1322 ": bitmask of the TX HW offload capabilities to enable/use\n"
1323 " (DEV_TX_OFFLOAD_*)\n"
1324 " --" CMD_LINE_OPT_REASSEMBLE " NUM"
1325 ": max number of entries in reassemble(fragment) table\n"
1326 " (zero (default value) disables reassembly)\n"
1327 " --" CMD_LINE_OPT_MTU " MTU"
1328 ": MTU value on all ports (default value: 1500)\n"
1329 " outgoing packets with bigger size will be fragmented\n"
1330 " incoming packets with bigger size will be discarded\n"
1331 " --" CMD_LINE_OPT_FRAG_TTL " FRAG_TTL_NS"
1332 ": fragments lifetime in nanoseconds, default\n"
1333 " and maximum value is 10.000.000.000 ns (10 s)\n"
1339 parse_mask(const char *str, uint64_t *val)
1345 t = strtoul(str, &end, 0);
1346 if (errno != 0 || end[0] != 0)
1354 parse_portmask(const char *portmask)
1359 /* parse hexadecimal string */
1360 pm = strtoul(portmask, &end, 16);
1361 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1364 if ((pm == 0) && errno)
1371 parse_decimal(const char *str)
1376 num = strtoull(str, &end, 10);
1377 if ((str[0] == '\0') || (end == NULL) || (*end != '\0')
1385 parse_config(const char *q_arg)
1388 const char *p, *p0 = q_arg;
1396 unsigned long int_fld[_NUM_FLD];
1397 char *str_fld[_NUM_FLD];
1401 nb_lcore_params = 0;
1403 while ((p = strchr(p0, '(')) != NULL) {
1405 p0 = strchr(p, ')');
1410 if (size >= sizeof(s))
1413 snprintf(s, sizeof(s), "%.*s", size, p);
1414 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1417 for (i = 0; i < _NUM_FLD; i++) {
1419 int_fld[i] = strtoul(str_fld[i], &end, 0);
1420 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
1423 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1424 printf("exceeded max number of lcore params: %hu\n",
1428 lcore_params_array[nb_lcore_params].port_id =
1429 (uint8_t)int_fld[FLD_PORT];
1430 lcore_params_array[nb_lcore_params].queue_id =
1431 (uint8_t)int_fld[FLD_QUEUE];
1432 lcore_params_array[nb_lcore_params].lcore_id =
1433 (uint8_t)int_fld[FLD_LCORE];
1436 lcore_params = lcore_params_array;
1441 print_app_sa_prm(const struct app_sa_prm *prm)
1443 printf("librte_ipsec usage: %s\n",
1444 (prm->enable == 0) ? "disabled" : "enabled");
1446 printf("replay window size: %u\n", prm->window_size);
1447 printf("ESN: %s\n", (prm->enable_esn == 0) ? "disabled" : "enabled");
1448 printf("SA flags: %#" PRIx64 "\n", prm->flags);
1449 printf("Frag TTL: %" PRIu64 " ns\n", frag_ttl_ns);
1453 parse_args(int32_t argc, char **argv)
1458 int32_t option_index;
1459 char *prgname = argv[0];
1460 int32_t f_present = 0;
1464 while ((opt = getopt_long(argc, argvopt, "aelp:Pu:f:j:w:c:",
1465 lgopts, &option_index)) != EOF) {
1469 enabled_port_mask = parse_portmask(optarg);
1470 if (enabled_port_mask == 0) {
1471 printf("invalid portmask\n");
1472 print_usage(prgname);
1477 printf("Promiscuous mode selected\n");
1481 unprotected_port_mask = parse_portmask(optarg);
1482 if (unprotected_port_mask == 0) {
1483 printf("invalid unprotected portmask\n");
1484 print_usage(prgname);
1489 if (f_present == 1) {
1490 printf("\"-f\" option present more than "
1492 print_usage(prgname);
1499 ret = parse_decimal(optarg);
1500 if (ret < RTE_MBUF_DEFAULT_BUF_SIZE ||
1502 printf("Invalid frame buffer size value: %s\n",
1504 print_usage(prgname);
1507 frame_buf_size = ret;
1508 printf("Custom frame buffer size %u\n", frame_buf_size);
1511 app_sa_prm.enable = 1;
1514 app_sa_prm.window_size = parse_decimal(optarg);
1517 app_sa_prm.enable_esn = 1;
1520 app_sa_prm.enable = 1;
1521 app_sa_prm.flags |= RTE_IPSEC_SAFLAG_SQN_ATOM;
1524 ret = parse_decimal(optarg);
1526 printf("Invalid SA cache size: %s\n", optarg);
1527 print_usage(prgname);
1530 app_sa_prm.cache_sz = ret;
1532 case CMD_LINE_OPT_CONFIG_NUM:
1533 ret = parse_config(optarg);
1535 printf("Invalid config\n");
1536 print_usage(prgname);
1540 case CMD_LINE_OPT_SINGLE_SA_NUM:
1541 ret = parse_decimal(optarg);
1542 if (ret == -1 || ret > UINT32_MAX) {
1543 printf("Invalid argument[sa_idx]\n");
1544 print_usage(prgname);
1550 single_sa_idx = ret;
1551 printf("Configured with single SA index %u\n",
1554 case CMD_LINE_OPT_CRYPTODEV_MASK_NUM:
1555 ret = parse_portmask(optarg);
1557 printf("Invalid argument[portmask]\n");
1558 print_usage(prgname);
1563 enabled_cryptodev_mask = ret;
1565 case CMD_LINE_OPT_RX_OFFLOAD_NUM:
1566 ret = parse_mask(optarg, &dev_rx_offload);
1568 printf("Invalid argument for \'%s\': %s\n",
1569 CMD_LINE_OPT_RX_OFFLOAD, optarg);
1570 print_usage(prgname);
1574 case CMD_LINE_OPT_TX_OFFLOAD_NUM:
1575 ret = parse_mask(optarg, &dev_tx_offload);
1577 printf("Invalid argument for \'%s\': %s\n",
1578 CMD_LINE_OPT_TX_OFFLOAD, optarg);
1579 print_usage(prgname);
1583 case CMD_LINE_OPT_REASSEMBLE_NUM:
1584 ret = parse_decimal(optarg);
1585 if (ret < 0 || ret > UINT32_MAX) {
1586 printf("Invalid argument for \'%s\': %s\n",
1587 CMD_LINE_OPT_REASSEMBLE, optarg);
1588 print_usage(prgname);
1593 case CMD_LINE_OPT_MTU_NUM:
1594 ret = parse_decimal(optarg);
1595 if (ret < 0 || ret > RTE_IPV4_MAX_PKT_LEN) {
1596 printf("Invalid argument for \'%s\': %s\n",
1597 CMD_LINE_OPT_MTU, optarg);
1598 print_usage(prgname);
1603 case CMD_LINE_OPT_FRAG_TTL_NUM:
1604 ret = parse_decimal(optarg);
1605 if (ret < 0 || ret > MAX_FRAG_TTL_NS) {
1606 printf("Invalid argument for \'%s\': %s\n",
1607 CMD_LINE_OPT_MTU, optarg);
1608 print_usage(prgname);
1614 print_usage(prgname);
1619 if (f_present == 0) {
1620 printf("Mandatory option \"-f\" not present\n");
1624 /* check do we need to enable multi-seg support */
1625 if (multi_seg_required()) {
1626 /* legacy mode doesn't support multi-seg */
1627 app_sa_prm.enable = 1;
1628 printf("frame buf size: %u, mtu: %u, "
1629 "number of reassemble entries: %u\n"
1630 "multi-segment support is required\n",
1631 frame_buf_size, mtu_size, frag_tbl_sz);
1634 print_app_sa_prm(&app_sa_prm);
1637 argv[optind-1] = prgname;
1640 optind = 1; /* reset getopt lib */
1645 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
1647 char buf[RTE_ETHER_ADDR_FMT_SIZE];
1648 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
1649 printf("%s%s", name, buf);
1653 * Update destination ethaddr for the port.
1656 add_dst_ethaddr(uint16_t port, const struct rte_ether_addr *addr)
1658 if (port >= RTE_DIM(ethaddr_tbl))
1661 ethaddr_tbl[port].dst = ETHADDR_TO_UINT64(addr);
1665 /* Check the link status of all ports in up to 9s, and print them finally */
1667 check_all_ports_link_status(uint32_t port_mask)
1669 #define CHECK_INTERVAL 100 /* 100ms */
1670 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1672 uint8_t count, all_ports_up, print_flag = 0;
1673 struct rte_eth_link link;
1676 printf("\nChecking link status");
1678 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1680 RTE_ETH_FOREACH_DEV(portid) {
1681 if ((port_mask & (1 << portid)) == 0)
1683 memset(&link, 0, sizeof(link));
1684 ret = rte_eth_link_get_nowait(portid, &link);
1687 if (print_flag == 1)
1688 printf("Port %u link get failed: %s\n",
1689 portid, rte_strerror(-ret));
1692 /* print link status if flag set */
1693 if (print_flag == 1) {
1694 if (link.link_status)
1696 "Port%d Link Up - speed %u Mbps -%s\n",
1697 portid, link.link_speed,
1698 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1699 ("full-duplex") : ("half-duplex\n"));
1701 printf("Port %d Link Down\n", portid);
1704 /* clear all_ports_up flag if any link down */
1705 if (link.link_status == ETH_LINK_DOWN) {
1710 /* after finally printing all link status, get out */
1711 if (print_flag == 1)
1714 if (all_ports_up == 0) {
1717 rte_delay_ms(CHECK_INTERVAL);
1720 /* set the print_flag if all ports up or timeout */
1721 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1729 add_mapping(struct rte_hash *map, const char *str, uint16_t cdev_id,
1730 uint16_t qp, struct lcore_params *params,
1731 struct ipsec_ctx *ipsec_ctx,
1732 const struct rte_cryptodev_capabilities *cipher,
1733 const struct rte_cryptodev_capabilities *auth,
1734 const struct rte_cryptodev_capabilities *aead)
1738 struct cdev_key key = { 0 };
1740 key.lcore_id = params->lcore_id;
1742 key.cipher_algo = cipher->sym.cipher.algo;
1744 key.auth_algo = auth->sym.auth.algo;
1746 key.aead_algo = aead->sym.aead.algo;
1748 ret = rte_hash_lookup(map, &key);
1752 for (i = 0; i < ipsec_ctx->nb_qps; i++)
1753 if (ipsec_ctx->tbl[i].id == cdev_id)
1756 if (i == ipsec_ctx->nb_qps) {
1757 if (ipsec_ctx->nb_qps == MAX_QP_PER_LCORE) {
1758 printf("Maximum number of crypto devices assigned to "
1759 "a core, increase MAX_QP_PER_LCORE value\n");
1762 ipsec_ctx->tbl[i].id = cdev_id;
1763 ipsec_ctx->tbl[i].qp = qp;
1764 ipsec_ctx->nb_qps++;
1765 printf("%s cdev mapping: lcore %u using cdev %u qp %u "
1766 "(cdev_id_qp %lu)\n", str, key.lcore_id,
1770 ret = rte_hash_add_key_data(map, &key, (void *)i);
1772 printf("Faled to insert cdev mapping for (lcore %u, "
1773 "cdev %u, qp %u), errno %d\n",
1774 key.lcore_id, ipsec_ctx->tbl[i].id,
1775 ipsec_ctx->tbl[i].qp, ret);
1783 add_cdev_mapping(struct rte_cryptodev_info *dev_info, uint16_t cdev_id,
1784 uint16_t qp, struct lcore_params *params)
1787 const struct rte_cryptodev_capabilities *i, *j;
1788 struct rte_hash *map;
1789 struct lcore_conf *qconf;
1790 struct ipsec_ctx *ipsec_ctx;
1793 qconf = &lcore_conf[params->lcore_id];
1795 if ((unprotected_port_mask & (1 << params->port_id)) == 0) {
1797 ipsec_ctx = &qconf->outbound;
1801 ipsec_ctx = &qconf->inbound;
1805 /* Required cryptodevs with operation chainning */
1806 if (!(dev_info->feature_flags &
1807 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING))
1810 for (i = dev_info->capabilities;
1811 i->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; i++) {
1812 if (i->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1815 if (i->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1816 ret |= add_mapping(map, str, cdev_id, qp, params,
1817 ipsec_ctx, NULL, NULL, i);
1821 if (i->sym.xform_type != RTE_CRYPTO_SYM_XFORM_CIPHER)
1824 for (j = dev_info->capabilities;
1825 j->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; j++) {
1826 if (j->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1829 if (j->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH)
1832 ret |= add_mapping(map, str, cdev_id, qp, params,
1833 ipsec_ctx, i, j, NULL);
1840 /* Check if the device is enabled by cryptodev_mask */
1842 check_cryptodev_mask(uint8_t cdev_id)
1844 if (enabled_cryptodev_mask & (1 << cdev_id))
1851 cryptodevs_init(void)
1853 struct rte_cryptodev_config dev_conf;
1854 struct rte_cryptodev_qp_conf qp_conf;
1855 uint16_t idx, max_nb_qps, qp, i;
1857 struct rte_hash_parameters params = { 0 };
1859 const uint64_t mseg_flag = multi_seg_required() ?
1860 RTE_CRYPTODEV_FF_IN_PLACE_SGL : 0;
1862 params.entries = CDEV_MAP_ENTRIES;
1863 params.key_len = sizeof(struct cdev_key);
1864 params.hash_func = rte_jhash;
1865 params.hash_func_init_val = 0;
1866 params.socket_id = rte_socket_id();
1868 params.name = "cdev_map_in";
1869 cdev_map_in = rte_hash_create(¶ms);
1870 if (cdev_map_in == NULL)
1871 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1874 params.name = "cdev_map_out";
1875 cdev_map_out = rte_hash_create(¶ms);
1876 if (cdev_map_out == NULL)
1877 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1880 printf("lcore/cryptodev/qp mappings:\n");
1883 for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1884 struct rte_cryptodev_info cdev_info;
1886 if (check_cryptodev_mask((uint8_t)cdev_id))
1889 rte_cryptodev_info_get(cdev_id, &cdev_info);
1891 if ((mseg_flag & cdev_info.feature_flags) != mseg_flag)
1892 rte_exit(EXIT_FAILURE,
1893 "Device %hd does not support \'%s\' feature\n",
1895 rte_cryptodev_get_feature_name(mseg_flag));
1897 if (nb_lcore_params > cdev_info.max_nb_queue_pairs)
1898 max_nb_qps = cdev_info.max_nb_queue_pairs;
1900 max_nb_qps = nb_lcore_params;
1904 while (qp < max_nb_qps && i < nb_lcore_params) {
1905 if (add_cdev_mapping(&cdev_info, cdev_id, qp,
1906 &lcore_params[idx]))
1909 idx = idx % nb_lcore_params;
1916 dev_conf.socket_id = rte_cryptodev_socket_id(cdev_id);
1917 dev_conf.nb_queue_pairs = qp;
1918 dev_conf.ff_disable = RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO;
1920 uint32_t dev_max_sess = cdev_info.sym.max_nb_sessions;
1921 if (dev_max_sess != 0 && dev_max_sess < CDEV_MP_NB_OBJS)
1922 rte_exit(EXIT_FAILURE,
1923 "Device does not support at least %u "
1924 "sessions", CDEV_MP_NB_OBJS);
1926 if (rte_cryptodev_configure(cdev_id, &dev_conf))
1927 rte_panic("Failed to initialize cryptodev %u\n",
1930 qp_conf.nb_descriptors = CDEV_QUEUE_DESC;
1931 qp_conf.mp_session =
1932 socket_ctx[dev_conf.socket_id].session_pool;
1933 qp_conf.mp_session_private =
1934 socket_ctx[dev_conf.socket_id].session_priv_pool;
1935 for (qp = 0; qp < dev_conf.nb_queue_pairs; qp++)
1936 if (rte_cryptodev_queue_pair_setup(cdev_id, qp,
1937 &qp_conf, dev_conf.socket_id))
1938 rte_panic("Failed to setup queue %u for "
1939 "cdev_id %u\n", 0, cdev_id);
1941 if (rte_cryptodev_start(cdev_id))
1942 rte_panic("Failed to start cryptodev %u\n",
1952 port_init(uint16_t portid, uint64_t req_rx_offloads, uint64_t req_tx_offloads)
1954 uint32_t frame_size;
1955 struct rte_eth_dev_info dev_info;
1956 struct rte_eth_txconf *txconf;
1957 uint16_t nb_tx_queue, nb_rx_queue;
1958 uint16_t tx_queueid, rx_queueid, queue, lcore_id;
1959 int32_t ret, socket_id;
1960 struct lcore_conf *qconf;
1961 struct rte_ether_addr ethaddr;
1962 struct rte_eth_conf local_port_conf = port_conf;
1964 ret = rte_eth_dev_info_get(portid, &dev_info);
1966 rte_exit(EXIT_FAILURE,
1967 "Error during getting device (port %u) info: %s\n",
1968 portid, strerror(-ret));
1970 /* limit allowed HW offloafs, as user requested */
1971 dev_info.rx_offload_capa &= dev_rx_offload;
1972 dev_info.tx_offload_capa &= dev_tx_offload;
1974 printf("Configuring device port %u:\n", portid);
1976 ret = rte_eth_macaddr_get(portid, ðaddr);
1978 rte_exit(EXIT_FAILURE,
1979 "Error getting MAC address (port %u): %s\n",
1980 portid, rte_strerror(-ret));
1982 ethaddr_tbl[portid].src = ETHADDR_TO_UINT64(ðaddr);
1983 print_ethaddr("Address: ", ðaddr);
1986 nb_rx_queue = get_port_nb_rx_queues(portid);
1987 nb_tx_queue = nb_lcores;
1989 if (nb_rx_queue > dev_info.max_rx_queues)
1990 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1991 "(max rx queue is %u)\n",
1992 nb_rx_queue, dev_info.max_rx_queues);
1994 if (nb_tx_queue > dev_info.max_tx_queues)
1995 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1996 "(max tx queue is %u)\n",
1997 nb_tx_queue, dev_info.max_tx_queues);
1999 printf("Creating queues: nb_rx_queue=%d nb_tx_queue=%u...\n",
2000 nb_rx_queue, nb_tx_queue);
2002 frame_size = MTU_TO_FRAMELEN(mtu_size);
2003 if (frame_size > local_port_conf.rxmode.max_rx_pkt_len)
2004 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
2005 local_port_conf.rxmode.max_rx_pkt_len = frame_size;
2007 if (multi_seg_required()) {
2008 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_SCATTER;
2009 local_port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
2012 local_port_conf.rxmode.offloads |= req_rx_offloads;
2013 local_port_conf.txmode.offloads |= req_tx_offloads;
2015 /* Check that all required capabilities are supported */
2016 if ((local_port_conf.rxmode.offloads & dev_info.rx_offload_capa) !=
2017 local_port_conf.rxmode.offloads)
2018 rte_exit(EXIT_FAILURE,
2019 "Error: port %u required RX offloads: 0x%" PRIx64
2020 ", avaialbe RX offloads: 0x%" PRIx64 "\n",
2021 portid, local_port_conf.rxmode.offloads,
2022 dev_info.rx_offload_capa);
2024 if ((local_port_conf.txmode.offloads & dev_info.tx_offload_capa) !=
2025 local_port_conf.txmode.offloads)
2026 rte_exit(EXIT_FAILURE,
2027 "Error: port %u required TX offloads: 0x%" PRIx64
2028 ", avaialbe TX offloads: 0x%" PRIx64 "\n",
2029 portid, local_port_conf.txmode.offloads,
2030 dev_info.tx_offload_capa);
2032 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2033 local_port_conf.txmode.offloads |=
2034 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2036 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_IPV4_CKSUM)
2037 local_port_conf.txmode.offloads |= DEV_TX_OFFLOAD_IPV4_CKSUM;
2039 printf("port %u configurng rx_offloads=0x%" PRIx64
2040 ", tx_offloads=0x%" PRIx64 "\n",
2041 portid, local_port_conf.rxmode.offloads,
2042 local_port_conf.txmode.offloads);
2044 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
2045 dev_info.flow_type_rss_offloads;
2046 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
2047 port_conf.rx_adv_conf.rss_conf.rss_hf) {
2048 printf("Port %u modified RSS hash function based on hardware support,"
2049 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
2051 port_conf.rx_adv_conf.rss_conf.rss_hf,
2052 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
2055 ret = rte_eth_dev_configure(portid, nb_rx_queue, nb_tx_queue,
2058 rte_exit(EXIT_FAILURE, "Cannot configure device: "
2059 "err=%d, port=%d\n", ret, portid);
2061 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
2063 rte_exit(EXIT_FAILURE, "Cannot adjust number of descriptors: "
2064 "err=%d, port=%d\n", ret, portid);
2066 /* init one TX queue per lcore */
2068 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2069 if (rte_lcore_is_enabled(lcore_id) == 0)
2073 socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2078 printf("Setup txq=%u,%d,%d\n", lcore_id, tx_queueid, socket_id);
2080 txconf = &dev_info.default_txconf;
2081 txconf->offloads = local_port_conf.txmode.offloads;
2083 ret = rte_eth_tx_queue_setup(portid, tx_queueid, nb_txd,
2086 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
2087 "err=%d, port=%d\n", ret, portid);
2089 qconf = &lcore_conf[lcore_id];
2090 qconf->tx_queue_id[portid] = tx_queueid;
2092 /* Pre-populate pkt offloads based on capabilities */
2093 qconf->outbound.ipv4_offloads = PKT_TX_IPV4;
2094 qconf->outbound.ipv6_offloads = PKT_TX_IPV6;
2095 if (local_port_conf.txmode.offloads & DEV_TX_OFFLOAD_IPV4_CKSUM)
2096 qconf->outbound.ipv4_offloads |= PKT_TX_IP_CKSUM;
2100 /* init RX queues */
2101 for (queue = 0; queue < qconf->nb_rx_queue; ++queue) {
2102 struct rte_eth_rxconf rxq_conf;
2104 if (portid != qconf->rx_queue_list[queue].port_id)
2107 rx_queueid = qconf->rx_queue_list[queue].queue_id;
2109 printf("Setup rxq=%d,%d,%d\n", portid, rx_queueid,
2112 rxq_conf = dev_info.default_rxconf;
2113 rxq_conf.offloads = local_port_conf.rxmode.offloads;
2114 ret = rte_eth_rx_queue_setup(portid, rx_queueid,
2115 nb_rxd, socket_id, &rxq_conf,
2116 socket_ctx[socket_id].mbuf_pool);
2118 rte_exit(EXIT_FAILURE,
2119 "rte_eth_rx_queue_setup: err=%d, "
2120 "port=%d\n", ret, portid);
2127 max_session_size(void)
2131 int16_t cdev_id, port_id, n;
2134 n = rte_cryptodev_count();
2135 for (cdev_id = 0; cdev_id != n; cdev_id++) {
2136 sz = rte_cryptodev_sym_get_private_session_size(cdev_id);
2140 * If crypto device is security capable, need to check the
2141 * size of security session as well.
2144 /* Get security context of the crypto device */
2145 sec_ctx = rte_cryptodev_get_sec_ctx(cdev_id);
2146 if (sec_ctx == NULL)
2149 /* Get size of security session */
2150 sz = rte_security_session_get_size(sec_ctx);
2155 RTE_ETH_FOREACH_DEV(port_id) {
2156 if ((enabled_port_mask & (1 << port_id)) == 0)
2159 sec_ctx = rte_eth_dev_get_sec_ctx(port_id);
2160 if (sec_ctx == NULL)
2163 sz = rte_security_session_get_size(sec_ctx);
2172 session_pool_init(struct socket_ctx *ctx, int32_t socket_id, size_t sess_sz)
2174 char mp_name[RTE_MEMPOOL_NAMESIZE];
2175 struct rte_mempool *sess_mp;
2177 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2178 "sess_mp_%u", socket_id);
2179 sess_mp = rte_cryptodev_sym_session_pool_create(
2180 mp_name, CDEV_MP_NB_OBJS,
2181 sess_sz, CDEV_MP_CACHE_SZ, 0,
2183 ctx->session_pool = sess_mp;
2185 if (ctx->session_pool == NULL)
2186 rte_exit(EXIT_FAILURE,
2187 "Cannot init session pool on socket %d\n", socket_id);
2189 printf("Allocated session pool on socket %d\n", socket_id);
2193 session_priv_pool_init(struct socket_ctx *ctx, int32_t socket_id,
2196 char mp_name[RTE_MEMPOOL_NAMESIZE];
2197 struct rte_mempool *sess_mp;
2199 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2200 "sess_mp_priv_%u", socket_id);
2201 sess_mp = rte_mempool_create(mp_name,
2205 0, NULL, NULL, NULL,
2208 ctx->session_priv_pool = sess_mp;
2210 if (ctx->session_priv_pool == NULL)
2211 rte_exit(EXIT_FAILURE,
2212 "Cannot init session priv pool on socket %d\n",
2215 printf("Allocated session priv pool on socket %d\n",
2220 pool_init(struct socket_ctx *ctx, int32_t socket_id, uint32_t nb_mbuf)
2225 snprintf(s, sizeof(s), "mbuf_pool_%d", socket_id);
2226 ctx->mbuf_pool = rte_pktmbuf_pool_create(s, nb_mbuf,
2227 MEMPOOL_CACHE_SIZE, ipsec_metadata_size(),
2228 frame_buf_size, socket_id);
2231 * if multi-segment support is enabled, then create a pool
2232 * for indirect mbufs.
2234 ms = multi_seg_required();
2236 snprintf(s, sizeof(s), "mbuf_pool_indir_%d", socket_id);
2237 ctx->mbuf_pool_indir = rte_pktmbuf_pool_create(s, nb_mbuf,
2238 MEMPOOL_CACHE_SIZE, 0, 0, socket_id);
2241 if (ctx->mbuf_pool == NULL || (ms != 0 && ctx->mbuf_pool_indir == NULL))
2242 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n",
2245 printf("Allocated mbuf pool on socket %d\n", socket_id);
2249 inline_ipsec_event_esn_overflow(struct rte_security_ctx *ctx, uint64_t md)
2251 struct ipsec_sa *sa;
2253 /* For inline protocol processing, the metadata in the event will
2254 * uniquely identify the security session which raised the event.
2255 * Application would then need the userdata it had registered with the
2256 * security session to process the event.
2259 sa = (struct ipsec_sa *)rte_security_get_userdata(ctx, md);
2262 /* userdata could not be retrieved */
2266 /* Sequence number over flow. SA need to be re-established */
2272 inline_ipsec_event_callback(uint16_t port_id, enum rte_eth_event_type type,
2273 void *param, void *ret_param)
2276 struct rte_eth_event_ipsec_desc *event_desc = NULL;
2277 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
2278 rte_eth_dev_get_sec_ctx(port_id);
2280 RTE_SET_USED(param);
2282 if (type != RTE_ETH_EVENT_IPSEC)
2285 event_desc = ret_param;
2286 if (event_desc == NULL) {
2287 printf("Event descriptor not set\n");
2291 md = event_desc->metadata;
2293 if (event_desc->subtype == RTE_ETH_EVENT_IPSEC_ESN_OVERFLOW)
2294 return inline_ipsec_event_esn_overflow(ctx, md);
2295 else if (event_desc->subtype >= RTE_ETH_EVENT_IPSEC_MAX) {
2296 printf("Invalid IPsec event reported\n");
2304 rx_callback(__rte_unused uint16_t port, __rte_unused uint16_t queue,
2305 struct rte_mbuf *pkt[], uint16_t nb_pkts,
2306 __rte_unused uint16_t max_pkts, void *user_param)
2310 struct lcore_conf *lc;
2311 struct rte_mbuf *mb;
2312 struct rte_ether_hdr *eth;
2318 for (i = 0; i != nb_pkts; i++) {
2321 eth = rte_pktmbuf_mtod(mb, struct rte_ether_hdr *);
2322 if (eth->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
2324 struct rte_ipv4_hdr *iph;
2326 iph = (struct rte_ipv4_hdr *)(eth + 1);
2327 if (rte_ipv4_frag_pkt_is_fragmented(iph)) {
2329 mb->l2_len = sizeof(*eth);
2330 mb->l3_len = sizeof(*iph);
2331 tm = (tm != 0) ? tm : rte_rdtsc();
2332 mb = rte_ipv4_frag_reassemble_packet(
2333 lc->frag.tbl, &lc->frag.dr,
2337 /* fix ip cksum after reassemble. */
2338 iph = rte_pktmbuf_mtod_offset(mb,
2339 struct rte_ipv4_hdr *,
2341 iph->hdr_checksum = 0;
2342 iph->hdr_checksum = rte_ipv4_cksum(iph);
2345 } else if (eth->ether_type ==
2346 rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
2348 struct rte_ipv6_hdr *iph;
2349 struct ipv6_extension_fragment *fh;
2351 iph = (struct rte_ipv6_hdr *)(eth + 1);
2352 fh = rte_ipv6_frag_get_ipv6_fragment_header(iph);
2354 mb->l2_len = sizeof(*eth);
2355 mb->l3_len = (uintptr_t)fh - (uintptr_t)iph +
2357 tm = (tm != 0) ? tm : rte_rdtsc();
2358 mb = rte_ipv6_frag_reassemble_packet(
2359 lc->frag.tbl, &lc->frag.dr,
2362 /* fix l3_len after reassemble. */
2363 mb->l3_len = mb->l3_len - sizeof(*fh);
2371 /* some fragments were encountered, drain death row */
2373 rte_ip_frag_free_death_row(&lc->frag.dr, 0);
2380 reassemble_lcore_init(struct lcore_conf *lc, uint32_t cid)
2384 uint64_t frag_cycles;
2385 const struct lcore_rx_queue *rxq;
2386 const struct rte_eth_rxtx_callback *cb;
2388 /* create fragment table */
2389 sid = rte_lcore_to_socket_id(cid);
2390 frag_cycles = (rte_get_tsc_hz() + NS_PER_S - 1) /
2391 NS_PER_S * frag_ttl_ns;
2393 lc->frag.tbl = rte_ip_frag_table_create(frag_tbl_sz,
2394 FRAG_TBL_BUCKET_ENTRIES, frag_tbl_sz, frag_cycles, sid);
2395 if (lc->frag.tbl == NULL) {
2396 printf("%s(%u): failed to create fragment table of size: %u, "
2398 __func__, cid, frag_tbl_sz, rte_errno);
2402 /* setup reassemble RX callbacks for all queues */
2403 for (i = 0; i != lc->nb_rx_queue; i++) {
2405 rxq = lc->rx_queue_list + i;
2406 cb = rte_eth_add_rx_callback(rxq->port_id, rxq->queue_id,
2409 printf("%s(%u): failed to install RX callback for "
2410 "portid=%u, queueid=%u, error code: %d\n",
2412 rxq->port_id, rxq->queue_id, rte_errno);
2421 reassemble_init(void)
2427 for (i = 0; i != nb_lcore_params; i++) {
2428 lc = lcore_params[i].lcore_id;
2429 rc = reassemble_lcore_init(lcore_conf + lc, lc);
2438 create_default_ipsec_flow(uint16_t port_id, uint64_t rx_offloads)
2440 struct rte_flow_action action[2];
2441 struct rte_flow_item pattern[2];
2442 struct rte_flow_attr attr = {0};
2443 struct rte_flow_error err;
2444 struct rte_flow *flow;
2447 if (!(rx_offloads & DEV_RX_OFFLOAD_SECURITY))
2450 /* Add the default rte_flow to enable SECURITY for all ESP packets */
2452 pattern[0].type = RTE_FLOW_ITEM_TYPE_ESP;
2453 pattern[0].spec = NULL;
2454 pattern[0].mask = NULL;
2455 pattern[0].last = NULL;
2456 pattern[1].type = RTE_FLOW_ITEM_TYPE_END;
2458 action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY;
2459 action[0].conf = NULL;
2460 action[1].type = RTE_FLOW_ACTION_TYPE_END;
2461 action[1].conf = NULL;
2465 ret = rte_flow_validate(port_id, &attr, pattern, action, &err);
2469 flow = rte_flow_create(port_id, &attr, pattern, action, &err);
2473 flow_info_tbl[port_id].rx_def_flow = flow;
2474 RTE_LOG(INFO, IPSEC,
2475 "Created default flow enabling SECURITY for all ESP traffic on port %d\n",
2480 main(int32_t argc, char **argv)
2487 uint64_t req_rx_offloads[RTE_MAX_ETHPORTS];
2488 uint64_t req_tx_offloads[RTE_MAX_ETHPORTS];
2492 ret = rte_eal_init(argc, argv);
2494 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
2498 /* parse application arguments (after the EAL ones) */
2499 ret = parse_args(argc, argv);
2501 rte_exit(EXIT_FAILURE, "Invalid parameters\n");
2503 /* parse configuration file */
2504 if (parse_cfg_file(cfgfile) < 0) {
2505 printf("parsing file \"%s\" failed\n",
2507 print_usage(argv[0]);
2511 if ((unprotected_port_mask & enabled_port_mask) !=
2512 unprotected_port_mask)
2513 rte_exit(EXIT_FAILURE, "Invalid unprotected portmask 0x%x\n",
2514 unprotected_port_mask);
2516 if (check_params() < 0)
2517 rte_exit(EXIT_FAILURE, "check_params failed\n");
2519 ret = init_lcore_rx_queues();
2521 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2523 nb_lcores = rte_lcore_count();
2525 sess_sz = max_session_size();
2527 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2528 if (rte_lcore_is_enabled(lcore_id) == 0)
2532 socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2536 /* mbuf_pool is initialised by the pool_init() function*/
2537 if (socket_ctx[socket_id].mbuf_pool)
2540 pool_init(&socket_ctx[socket_id], socket_id, NB_MBUF);
2541 session_pool_init(&socket_ctx[socket_id], socket_id, sess_sz);
2542 session_priv_pool_init(&socket_ctx[socket_id], socket_id,
2546 RTE_ETH_FOREACH_DEV(portid) {
2547 if ((enabled_port_mask & (1 << portid)) == 0)
2550 sa_check_offloads(portid, &req_rx_offloads[portid],
2551 &req_tx_offloads[portid]);
2552 port_init(portid, req_rx_offloads[portid],
2553 req_tx_offloads[portid]);
2559 RTE_ETH_FOREACH_DEV(portid) {
2560 if ((enabled_port_mask & (1 << portid)) == 0)
2563 /* Create flow before starting the device */
2564 create_default_ipsec_flow(portid, req_rx_offloads[portid]);
2566 ret = rte_eth_dev_start(portid);
2568 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: "
2569 "err=%d, port=%d\n", ret, portid);
2571 * If enabled, put device in promiscuous mode.
2572 * This allows IO forwarding mode to forward packets
2573 * to itself through 2 cross-connected ports of the
2576 if (promiscuous_on) {
2577 ret = rte_eth_promiscuous_enable(portid);
2579 rte_exit(EXIT_FAILURE,
2580 "rte_eth_promiscuous_enable: err=%s, port=%d\n",
2581 rte_strerror(-ret), portid);
2584 rte_eth_dev_callback_register(portid,
2585 RTE_ETH_EVENT_IPSEC, inline_ipsec_event_callback, NULL);
2588 /* fragment reassemble is enabled */
2589 if (frag_tbl_sz != 0) {
2590 ret = reassemble_init();
2592 rte_exit(EXIT_FAILURE, "failed at reassemble init");
2595 /* Replicate each context per socket */
2596 for (i = 0; i < NB_SOCKETS && i < rte_socket_count(); i++) {
2597 socket_id = rte_socket_id_by_idx(i);
2598 if ((socket_ctx[socket_id].mbuf_pool != NULL) &&
2599 (socket_ctx[socket_id].sa_in == NULL) &&
2600 (socket_ctx[socket_id].sa_out == NULL)) {
2601 sa_init(&socket_ctx[socket_id], socket_id);
2602 sp4_init(&socket_ctx[socket_id], socket_id);
2603 sp6_init(&socket_ctx[socket_id], socket_id);
2604 rt_init(&socket_ctx[socket_id], socket_id);
2608 check_all_ports_link_status(enabled_port_mask);
2610 /* launch per-lcore init on every lcore */
2611 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2612 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2613 if (rte_eal_wait_lcore(lcore_id) < 0)