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>
50 #define RTE_LOGTYPE_IPSEC RTE_LOGTYPE_USER1
52 #define MAX_JUMBO_PKT_LEN 9600
54 #define MEMPOOL_CACHE_SIZE 256
56 #define NB_MBUF (32000)
58 #define CDEV_QUEUE_DESC 2048
59 #define CDEV_MAP_ENTRIES 16384
60 #define CDEV_MP_NB_OBJS 1024
61 #define CDEV_MP_CACHE_SZ 64
62 #define MAX_QUEUE_PAIRS 1
64 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
68 /* Configure how many packets ahead to prefetch, when reading packets */
69 #define PREFETCH_OFFSET 3
71 #define MAX_RX_QUEUE_PER_LCORE 16
73 #define MAX_LCORE_PARAMS 1024
75 #define UNPROTECTED_PORT(port) (unprotected_port_mask & (1 << portid))
78 * Configurable number of RX/TX ring descriptors
80 #define IPSEC_SECGW_RX_DESC_DEFAULT 1024
81 #define IPSEC_SECGW_TX_DESC_DEFAULT 1024
82 static uint16_t nb_rxd = IPSEC_SECGW_RX_DESC_DEFAULT;
83 static uint16_t nb_txd = IPSEC_SECGW_TX_DESC_DEFAULT;
85 #if RTE_BYTE_ORDER != RTE_LITTLE_ENDIAN
86 #define __BYTES_TO_UINT64(a, b, c, d, e, f, g, h) \
87 (((uint64_t)((a) & 0xff) << 56) | \
88 ((uint64_t)((b) & 0xff) << 48) | \
89 ((uint64_t)((c) & 0xff) << 40) | \
90 ((uint64_t)((d) & 0xff) << 32) | \
91 ((uint64_t)((e) & 0xff) << 24) | \
92 ((uint64_t)((f) & 0xff) << 16) | \
93 ((uint64_t)((g) & 0xff) << 8) | \
94 ((uint64_t)(h) & 0xff))
96 #define __BYTES_TO_UINT64(a, b, c, d, e, f, g, h) \
97 (((uint64_t)((h) & 0xff) << 56) | \
98 ((uint64_t)((g) & 0xff) << 48) | \
99 ((uint64_t)((f) & 0xff) << 40) | \
100 ((uint64_t)((e) & 0xff) << 32) | \
101 ((uint64_t)((d) & 0xff) << 24) | \
102 ((uint64_t)((c) & 0xff) << 16) | \
103 ((uint64_t)((b) & 0xff) << 8) | \
104 ((uint64_t)(a) & 0xff))
106 #define ETHADDR(a, b, c, d, e, f) (__BYTES_TO_UINT64(a, b, c, d, e, f, 0, 0))
108 #define ETHADDR_TO_UINT64(addr) __BYTES_TO_UINT64( \
109 (addr)->addr_bytes[0], (addr)->addr_bytes[1], \
110 (addr)->addr_bytes[2], (addr)->addr_bytes[3], \
111 (addr)->addr_bytes[4], (addr)->addr_bytes[5], \
114 #define FRAG_TBL_BUCKET_ENTRIES 4
115 #define MAX_FRAG_TTL_NS (10LL * NS_PER_S)
117 #define MTU_TO_FRAMELEN(x) ((x) + RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN)
119 /* port/source ethernet addr and destination ethernet addr */
120 struct ethaddr_info {
124 struct ethaddr_info ethaddr_tbl[RTE_MAX_ETHPORTS] = {
125 { 0, ETHADDR(0x00, 0x16, 0x3e, 0x7e, 0x94, 0x9a) },
126 { 0, ETHADDR(0x00, 0x16, 0x3e, 0x22, 0xa1, 0xd9) },
127 { 0, ETHADDR(0x00, 0x16, 0x3e, 0x08, 0x69, 0x26) },
128 { 0, ETHADDR(0x00, 0x16, 0x3e, 0x49, 0x9e, 0xdd) }
131 #define CMD_LINE_OPT_CONFIG "config"
132 #define CMD_LINE_OPT_SINGLE_SA "single-sa"
133 #define CMD_LINE_OPT_CRYPTODEV_MASK "cryptodev_mask"
134 #define CMD_LINE_OPT_RX_OFFLOAD "rxoffload"
135 #define CMD_LINE_OPT_TX_OFFLOAD "txoffload"
136 #define CMD_LINE_OPT_REASSEMBLE "reassemble"
137 #define CMD_LINE_OPT_MTU "mtu"
138 #define CMD_LINE_OPT_FRAG_TTL "frag-ttl"
141 /* long options mapped to a short option */
143 /* first long only option value must be >= 256, so that we won't
144 * conflict with short options
146 CMD_LINE_OPT_MIN_NUM = 256,
147 CMD_LINE_OPT_CONFIG_NUM,
148 CMD_LINE_OPT_SINGLE_SA_NUM,
149 CMD_LINE_OPT_CRYPTODEV_MASK_NUM,
150 CMD_LINE_OPT_RX_OFFLOAD_NUM,
151 CMD_LINE_OPT_TX_OFFLOAD_NUM,
152 CMD_LINE_OPT_REASSEMBLE_NUM,
153 CMD_LINE_OPT_MTU_NUM,
154 CMD_LINE_OPT_FRAG_TTL_NUM,
157 static const struct option lgopts[] = {
158 {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
159 {CMD_LINE_OPT_SINGLE_SA, 1, 0, CMD_LINE_OPT_SINGLE_SA_NUM},
160 {CMD_LINE_OPT_CRYPTODEV_MASK, 1, 0, CMD_LINE_OPT_CRYPTODEV_MASK_NUM},
161 {CMD_LINE_OPT_RX_OFFLOAD, 1, 0, CMD_LINE_OPT_RX_OFFLOAD_NUM},
162 {CMD_LINE_OPT_TX_OFFLOAD, 1, 0, CMD_LINE_OPT_TX_OFFLOAD_NUM},
163 {CMD_LINE_OPT_REASSEMBLE, 1, 0, CMD_LINE_OPT_REASSEMBLE_NUM},
164 {CMD_LINE_OPT_MTU, 1, 0, CMD_LINE_OPT_MTU_NUM},
165 {CMD_LINE_OPT_FRAG_TTL, 1, 0, CMD_LINE_OPT_FRAG_TTL_NUM},
169 /* mask of enabled ports */
170 static uint32_t enabled_port_mask;
171 static uint64_t enabled_cryptodev_mask = UINT64_MAX;
172 static uint32_t unprotected_port_mask;
173 static int32_t promiscuous_on = 1;
174 static int32_t numa_on = 1; /**< NUMA is enabled by default. */
175 static uint32_t nb_lcores;
176 static uint32_t single_sa;
177 static uint32_t single_sa_idx;
180 * RX/TX HW offload capabilities to enable/use on ethernet ports.
181 * By default all capabilities are enabled.
183 static uint64_t dev_rx_offload = UINT64_MAX;
184 static uint64_t dev_tx_offload = UINT64_MAX;
187 * global values that determine multi-seg policy
189 static uint32_t frag_tbl_sz;
190 static uint32_t frame_buf_size = RTE_MBUF_DEFAULT_BUF_SIZE;
191 static uint32_t mtu_size = RTE_ETHER_MTU;
192 static uint64_t frag_ttl_ns = MAX_FRAG_TTL_NS;
194 /* application wide librte_ipsec/SA parameters */
195 struct app_sa_prm app_sa_prm = {.enable = 0};
196 static const char *cfgfile;
198 struct lcore_rx_queue {
201 } __rte_cache_aligned;
203 struct lcore_params {
207 } __rte_cache_aligned;
209 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
211 static struct lcore_params *lcore_params;
212 static uint16_t nb_lcore_params;
214 static struct rte_hash *cdev_map_in;
215 static struct rte_hash *cdev_map_out;
219 struct rte_mbuf *m_table[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
223 uint16_t nb_rx_queue;
224 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
225 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
226 struct buffer tx_mbufs[RTE_MAX_ETHPORTS];
227 struct ipsec_ctx inbound;
228 struct ipsec_ctx outbound;
229 struct rt_ctx *rt4_ctx;
230 struct rt_ctx *rt6_ctx;
232 struct rte_ip_frag_tbl *tbl;
233 struct rte_mempool *pool_dir;
234 struct rte_mempool *pool_indir;
235 struct rte_ip_frag_death_row dr;
237 } __rte_cache_aligned;
239 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
241 static struct rte_eth_conf port_conf = {
243 .mq_mode = ETH_MQ_RX_RSS,
244 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
246 .offloads = DEV_RX_OFFLOAD_CHECKSUM,
251 .rss_hf = ETH_RSS_IP | ETH_RSS_UDP |
252 ETH_RSS_TCP | ETH_RSS_SCTP,
256 .mq_mode = ETH_MQ_TX_NONE,
260 static struct socket_ctx socket_ctx[NB_SOCKETS];
263 * Determine is multi-segment support required:
264 * - either frame buffer size is smaller then mtu
265 * - or reassmeble support is requested
268 multi_seg_required(void)
270 return (MTU_TO_FRAMELEN(mtu_size) + RTE_PKTMBUF_HEADROOM >
271 frame_buf_size || frag_tbl_sz != 0);
275 adjust_ipv4_pktlen(struct rte_mbuf *m, const struct rte_ipv4_hdr *iph,
280 plen = rte_be_to_cpu_16(iph->total_length) + l2_len;
281 if (plen < m->pkt_len) {
282 trim = m->pkt_len - plen;
283 rte_pktmbuf_trim(m, trim);
288 adjust_ipv6_pktlen(struct rte_mbuf *m, const struct rte_ipv6_hdr *iph,
293 plen = rte_be_to_cpu_16(iph->payload_len) + sizeof(*iph) + l2_len;
294 if (plen < m->pkt_len) {
295 trim = m->pkt_len - plen;
296 rte_pktmbuf_trim(m, trim);
301 prepare_one_packet(struct rte_mbuf *pkt, struct ipsec_traffic *t)
303 const struct rte_ether_hdr *eth;
304 const struct rte_ipv4_hdr *iph4;
305 const struct rte_ipv6_hdr *iph6;
307 eth = rte_pktmbuf_mtod(pkt, const struct rte_ether_hdr *);
308 if (eth->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
310 iph4 = (const struct rte_ipv4_hdr *)rte_pktmbuf_adj(pkt,
312 adjust_ipv4_pktlen(pkt, iph4, 0);
314 if (iph4->next_proto_id == IPPROTO_ESP)
315 t->ipsec.pkts[(t->ipsec.num)++] = pkt;
317 t->ip4.data[t->ip4.num] = &iph4->next_proto_id;
318 t->ip4.pkts[(t->ip4.num)++] = pkt;
321 pkt->l3_len = sizeof(*iph4);
322 } else if (eth->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
324 size_t l3len, ext_len;
327 /* get protocol type */
328 iph6 = (const struct rte_ipv6_hdr *)rte_pktmbuf_adj(pkt,
330 adjust_ipv6_pktlen(pkt, iph6, 0);
332 next_proto = iph6->proto;
334 /* determine l3 header size up to ESP extension */
335 l3len = sizeof(struct ip6_hdr);
336 p = rte_pktmbuf_mtod(pkt, uint8_t *);
337 while (next_proto != IPPROTO_ESP && l3len < pkt->data_len &&
338 (next_proto = rte_ipv6_get_next_ext(p + l3len,
339 next_proto, &ext_len)) >= 0)
342 /* drop packet when IPv6 header exceeds first segment length */
343 if (unlikely(l3len > pkt->data_len)) {
344 rte_pktmbuf_free(pkt);
348 if (next_proto == IPPROTO_ESP)
349 t->ipsec.pkts[(t->ipsec.num)++] = pkt;
351 t->ip6.data[t->ip6.num] = &iph6->proto;
352 t->ip6.pkts[(t->ip6.num)++] = pkt;
357 /* Unknown/Unsupported type, drop the packet */
358 RTE_LOG(ERR, IPSEC, "Unsupported packet type 0x%x\n",
359 rte_be_to_cpu_16(eth->ether_type));
360 rte_pktmbuf_free(pkt);
364 /* Check if the packet has been processed inline. For inline protocol
365 * processed packets, the metadata in the mbuf can be used to identify
366 * the security processing done on the packet. The metadata will be
367 * used to retrieve the application registered userdata associated
368 * with the security session.
371 if (pkt->ol_flags & PKT_RX_SEC_OFFLOAD) {
373 struct ipsec_mbuf_metadata *priv;
374 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
375 rte_eth_dev_get_sec_ctx(
378 /* Retrieve the userdata registered. Here, the userdata
379 * registered is the SA pointer.
382 sa = (struct ipsec_sa *)
383 rte_security_get_userdata(ctx, pkt->udata64);
386 /* userdata could not be retrieved */
390 /* Save SA as priv member in mbuf. This will be used in the
391 * IPsec selector(SP-SA) check.
394 priv = get_priv(pkt);
400 prepare_traffic(struct rte_mbuf **pkts, struct ipsec_traffic *t,
409 for (i = 0; i < (nb_pkts - PREFETCH_OFFSET); i++) {
410 rte_prefetch0(rte_pktmbuf_mtod(pkts[i + PREFETCH_OFFSET],
412 prepare_one_packet(pkts[i], t);
414 /* Process left packets */
415 for (; i < nb_pkts; i++)
416 prepare_one_packet(pkts[i], t);
420 prepare_tx_pkt(struct rte_mbuf *pkt, uint16_t port,
421 const struct lcore_conf *qconf)
424 struct rte_ether_hdr *ethhdr;
426 ip = rte_pktmbuf_mtod(pkt, struct ip *);
428 ethhdr = (struct rte_ether_hdr *)
429 rte_pktmbuf_prepend(pkt, RTE_ETHER_HDR_LEN);
431 if (ip->ip_v == IPVERSION) {
432 pkt->ol_flags |= qconf->outbound.ipv4_offloads;
433 pkt->l3_len = sizeof(struct ip);
434 pkt->l2_len = RTE_ETHER_HDR_LEN;
438 /* calculate IPv4 cksum in SW */
439 if ((pkt->ol_flags & PKT_TX_IP_CKSUM) == 0)
440 ip->ip_sum = rte_ipv4_cksum((struct rte_ipv4_hdr *)ip);
442 ethhdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
444 pkt->ol_flags |= qconf->outbound.ipv6_offloads;
445 pkt->l3_len = sizeof(struct ip6_hdr);
446 pkt->l2_len = RTE_ETHER_HDR_LEN;
448 ethhdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
451 memcpy(ðhdr->s_addr, ðaddr_tbl[port].src,
452 sizeof(struct rte_ether_addr));
453 memcpy(ðhdr->d_addr, ðaddr_tbl[port].dst,
454 sizeof(struct rte_ether_addr));
458 prepare_tx_burst(struct rte_mbuf *pkts[], uint16_t nb_pkts, uint16_t port,
459 const struct lcore_conf *qconf)
462 const int32_t prefetch_offset = 2;
464 for (i = 0; i < (nb_pkts - prefetch_offset); i++) {
465 rte_mbuf_prefetch_part2(pkts[i + prefetch_offset]);
466 prepare_tx_pkt(pkts[i], port, qconf);
468 /* Process left packets */
469 for (; i < nb_pkts; i++)
470 prepare_tx_pkt(pkts[i], port, qconf);
473 /* Send burst of packets on an output interface */
474 static inline int32_t
475 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
477 struct rte_mbuf **m_table;
481 queueid = qconf->tx_queue_id[port];
482 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
484 prepare_tx_burst(m_table, n, port, qconf);
486 ret = rte_eth_tx_burst(port, queueid, m_table, n);
487 if (unlikely(ret < n)) {
489 rte_pktmbuf_free(m_table[ret]);
497 * Helper function to fragment and queue for TX one packet.
499 static inline uint32_t
500 send_fragment_packet(struct lcore_conf *qconf, struct rte_mbuf *m,
501 uint16_t port, uint8_t proto)
507 tbl = qconf->tx_mbufs + port;
510 /* free space for new fragments */
511 if (len + RTE_LIBRTE_IP_FRAG_MAX_FRAG >= RTE_DIM(tbl->m_table)) {
512 send_burst(qconf, len, port);
516 n = RTE_DIM(tbl->m_table) - len;
518 if (proto == IPPROTO_IP)
519 rc = rte_ipv4_fragment_packet(m, tbl->m_table + len,
520 n, mtu_size, qconf->frag.pool_dir,
521 qconf->frag.pool_indir);
523 rc = rte_ipv6_fragment_packet(m, tbl->m_table + len,
524 n, mtu_size, qconf->frag.pool_dir,
525 qconf->frag.pool_indir);
531 "%s: failed to fragment packet with size %u, "
533 __func__, m->pkt_len, rte_errno);
539 /* Enqueue a single packet, and send burst if queue is filled */
540 static inline int32_t
541 send_single_packet(struct rte_mbuf *m, uint16_t port, uint8_t proto)
545 struct lcore_conf *qconf;
547 lcore_id = rte_lcore_id();
549 qconf = &lcore_conf[lcore_id];
550 len = qconf->tx_mbufs[port].len;
552 if (m->pkt_len <= mtu_size) {
553 qconf->tx_mbufs[port].m_table[len] = m;
556 /* need to fragment the packet */
557 } else if (frag_tbl_sz > 0)
558 len = send_fragment_packet(qconf, m, port, proto);
562 /* enough pkts to be sent */
563 if (unlikely(len == MAX_PKT_BURST)) {
564 send_burst(qconf, MAX_PKT_BURST, port);
568 qconf->tx_mbufs[port].len = len;
573 inbound_sp_sa(struct sp_ctx *sp, struct sa_ctx *sa, struct traffic_type *ip,
577 uint32_t i, j, res, sa_idx;
579 if (ip->num == 0 || sp == NULL)
582 rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
583 ip->num, DEFAULT_MAX_CATEGORIES);
586 for (i = 0; i < ip->num; i++) {
593 if (res == DISCARD) {
598 /* Only check SPI match for processed IPSec packets */
599 if (i < lim && ((m->ol_flags & PKT_RX_SEC_OFFLOAD) == 0)) {
604 sa_idx = SPI2IDX(res);
605 if (!inbound_sa_check(sa, m, sa_idx)) {
615 split46_traffic(struct ipsec_traffic *trf, struct rte_mbuf *mb[], uint32_t num)
624 for (i = 0; i < num; i++) {
627 ip = rte_pktmbuf_mtod(m, struct ip *);
629 if (ip->ip_v == IPVERSION) {
630 trf->ip4.pkts[n4] = m;
631 trf->ip4.data[n4] = rte_pktmbuf_mtod_offset(m,
632 uint8_t *, offsetof(struct ip, ip_p));
634 } else if (ip->ip_v == IP6_VERSION) {
635 trf->ip6.pkts[n6] = m;
636 trf->ip6.data[n6] = rte_pktmbuf_mtod_offset(m,
638 offsetof(struct ip6_hdr, ip6_nxt));
650 process_pkts_inbound(struct ipsec_ctx *ipsec_ctx,
651 struct ipsec_traffic *traffic)
653 uint16_t nb_pkts_in, n_ip4, n_ip6;
655 n_ip4 = traffic->ip4.num;
656 n_ip6 = traffic->ip6.num;
658 if (app_sa_prm.enable == 0) {
659 nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
660 traffic->ipsec.num, MAX_PKT_BURST);
661 split46_traffic(traffic, traffic->ipsec.pkts, nb_pkts_in);
663 inbound_sa_lookup(ipsec_ctx->sa_ctx, traffic->ipsec.pkts,
664 traffic->ipsec.saptr, traffic->ipsec.num);
665 ipsec_process(ipsec_ctx, traffic);
668 inbound_sp_sa(ipsec_ctx->sp4_ctx, ipsec_ctx->sa_ctx, &traffic->ip4,
671 inbound_sp_sa(ipsec_ctx->sp6_ctx, ipsec_ctx->sa_ctx, &traffic->ip6,
676 outbound_sp(struct sp_ctx *sp, struct traffic_type *ip,
677 struct traffic_type *ipsec)
680 uint32_t i, j, sa_idx;
682 if (ip->num == 0 || sp == NULL)
685 rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
686 ip->num, DEFAULT_MAX_CATEGORIES);
689 for (i = 0; i < ip->num; i++) {
691 sa_idx = SPI2IDX(ip->res[i]);
692 if (ip->res[i] == DISCARD)
694 else if (ip->res[i] == BYPASS)
697 ipsec->res[ipsec->num] = sa_idx;
698 ipsec->pkts[ipsec->num++] = m;
705 process_pkts_outbound(struct ipsec_ctx *ipsec_ctx,
706 struct ipsec_traffic *traffic)
709 uint16_t idx, nb_pkts_out, i;
711 /* Drop any IPsec traffic from protected ports */
712 for (i = 0; i < traffic->ipsec.num; i++)
713 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
715 traffic->ipsec.num = 0;
717 outbound_sp(ipsec_ctx->sp4_ctx, &traffic->ip4, &traffic->ipsec);
719 outbound_sp(ipsec_ctx->sp6_ctx, &traffic->ip6, &traffic->ipsec);
721 if (app_sa_prm.enable == 0) {
723 nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ipsec.pkts,
724 traffic->ipsec.res, traffic->ipsec.num,
727 for (i = 0; i < nb_pkts_out; i++) {
728 m = traffic->ipsec.pkts[i];
729 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
730 if (ip->ip_v == IPVERSION) {
731 idx = traffic->ip4.num++;
732 traffic->ip4.pkts[idx] = m;
734 idx = traffic->ip6.num++;
735 traffic->ip6.pkts[idx] = m;
739 outbound_sa_lookup(ipsec_ctx->sa_ctx, traffic->ipsec.res,
740 traffic->ipsec.saptr, traffic->ipsec.num);
741 ipsec_process(ipsec_ctx, traffic);
746 process_pkts_inbound_nosp(struct ipsec_ctx *ipsec_ctx,
747 struct ipsec_traffic *traffic)
750 uint32_t nb_pkts_in, i, idx;
752 /* Drop any IPv4 traffic from unprotected ports */
753 for (i = 0; i < traffic->ip4.num; i++)
754 rte_pktmbuf_free(traffic->ip4.pkts[i]);
756 traffic->ip4.num = 0;
758 /* Drop any IPv6 traffic from unprotected ports */
759 for (i = 0; i < traffic->ip6.num; i++)
760 rte_pktmbuf_free(traffic->ip6.pkts[i]);
762 traffic->ip6.num = 0;
764 if (app_sa_prm.enable == 0) {
766 nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
767 traffic->ipsec.num, MAX_PKT_BURST);
769 for (i = 0; i < nb_pkts_in; i++) {
770 m = traffic->ipsec.pkts[i];
771 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
772 if (ip->ip_v == IPVERSION) {
773 idx = traffic->ip4.num++;
774 traffic->ip4.pkts[idx] = m;
776 idx = traffic->ip6.num++;
777 traffic->ip6.pkts[idx] = m;
781 inbound_sa_lookup(ipsec_ctx->sa_ctx, traffic->ipsec.pkts,
782 traffic->ipsec.saptr, traffic->ipsec.num);
783 ipsec_process(ipsec_ctx, traffic);
788 process_pkts_outbound_nosp(struct ipsec_ctx *ipsec_ctx,
789 struct ipsec_traffic *traffic)
792 uint32_t nb_pkts_out, i, n;
795 /* Drop any IPsec traffic from protected ports */
796 for (i = 0; i < traffic->ipsec.num; i++)
797 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
801 for (i = 0; i < traffic->ip4.num; i++) {
802 traffic->ipsec.pkts[n] = traffic->ip4.pkts[i];
803 traffic->ipsec.res[n++] = single_sa_idx;
806 for (i = 0; i < traffic->ip6.num; i++) {
807 traffic->ipsec.pkts[n] = traffic->ip6.pkts[i];
808 traffic->ipsec.res[n++] = single_sa_idx;
811 traffic->ip4.num = 0;
812 traffic->ip6.num = 0;
813 traffic->ipsec.num = n;
815 if (app_sa_prm.enable == 0) {
817 nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ipsec.pkts,
818 traffic->ipsec.res, traffic->ipsec.num,
821 /* They all sue the same SA (ip4 or ip6 tunnel) */
822 m = traffic->ipsec.pkts[0];
823 ip = rte_pktmbuf_mtod(m, struct ip *);
824 if (ip->ip_v == IPVERSION) {
825 traffic->ip4.num = nb_pkts_out;
826 for (i = 0; i < nb_pkts_out; i++)
827 traffic->ip4.pkts[i] = traffic->ipsec.pkts[i];
829 traffic->ip6.num = nb_pkts_out;
830 for (i = 0; i < nb_pkts_out; i++)
831 traffic->ip6.pkts[i] = traffic->ipsec.pkts[i];
834 outbound_sa_lookup(ipsec_ctx->sa_ctx, traffic->ipsec.res,
835 traffic->ipsec.saptr, traffic->ipsec.num);
836 ipsec_process(ipsec_ctx, traffic);
840 static inline int32_t
841 get_hop_for_offload_pkt(struct rte_mbuf *pkt, int is_ipv6)
843 struct ipsec_mbuf_metadata *priv;
846 priv = get_priv(pkt);
849 if (unlikely(sa == NULL)) {
850 RTE_LOG(ERR, IPSEC, "SA not saved in private data\n");
858 return (sa->portid | RTE_LPM_LOOKUP_SUCCESS);
869 route4_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
871 uint32_t hop[MAX_PKT_BURST * 2];
872 uint32_t dst_ip[MAX_PKT_BURST * 2];
875 uint16_t lpm_pkts = 0;
880 /* Need to do an LPM lookup for non-inline packets. Inline packets will
881 * have port ID in the SA
884 for (i = 0; i < nb_pkts; i++) {
885 if (!(pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD)) {
886 /* Security offload not enabled. So an LPM lookup is
887 * required to get the hop
889 offset = offsetof(struct ip, ip_dst);
890 dst_ip[lpm_pkts] = *rte_pktmbuf_mtod_offset(pkts[i],
892 dst_ip[lpm_pkts] = rte_be_to_cpu_32(dst_ip[lpm_pkts]);
897 rte_lpm_lookup_bulk((struct rte_lpm *)rt_ctx, dst_ip, hop, lpm_pkts);
901 for (i = 0; i < nb_pkts; i++) {
902 if (pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD) {
903 /* Read hop from the SA */
904 pkt_hop = get_hop_for_offload_pkt(pkts[i], 0);
906 /* Need to use hop returned by lookup */
907 pkt_hop = hop[lpm_pkts++];
910 if ((pkt_hop & RTE_LPM_LOOKUP_SUCCESS) == 0) {
911 rte_pktmbuf_free(pkts[i]);
914 send_single_packet(pkts[i], pkt_hop & 0xff, IPPROTO_IP);
919 route6_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
921 int32_t hop[MAX_PKT_BURST * 2];
922 uint8_t dst_ip[MAX_PKT_BURST * 2][16];
926 uint16_t lpm_pkts = 0;
931 /* Need to do an LPM lookup for non-inline packets. Inline packets will
932 * have port ID in the SA
935 for (i = 0; i < nb_pkts; i++) {
936 if (!(pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD)) {
937 /* Security offload not enabled. So an LPM lookup is
938 * required to get the hop
940 offset = offsetof(struct ip6_hdr, ip6_dst);
941 ip6_dst = rte_pktmbuf_mtod_offset(pkts[i], uint8_t *,
943 memcpy(&dst_ip[lpm_pkts][0], ip6_dst, 16);
948 rte_lpm6_lookup_bulk_func((struct rte_lpm6 *)rt_ctx, dst_ip, hop,
953 for (i = 0; i < nb_pkts; i++) {
954 if (pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD) {
955 /* Read hop from the SA */
956 pkt_hop = get_hop_for_offload_pkt(pkts[i], 1);
958 /* Need to use hop returned by lookup */
959 pkt_hop = hop[lpm_pkts++];
963 rte_pktmbuf_free(pkts[i]);
966 send_single_packet(pkts[i], pkt_hop & 0xff, IPPROTO_IPV6);
971 process_pkts(struct lcore_conf *qconf, struct rte_mbuf **pkts,
972 uint8_t nb_pkts, uint16_t portid)
974 struct ipsec_traffic traffic;
976 prepare_traffic(pkts, &traffic, nb_pkts);
978 if (unlikely(single_sa)) {
979 if (UNPROTECTED_PORT(portid))
980 process_pkts_inbound_nosp(&qconf->inbound, &traffic);
982 process_pkts_outbound_nosp(&qconf->outbound, &traffic);
984 if (UNPROTECTED_PORT(portid))
985 process_pkts_inbound(&qconf->inbound, &traffic);
987 process_pkts_outbound(&qconf->outbound, &traffic);
990 route4_pkts(qconf->rt4_ctx, traffic.ip4.pkts, traffic.ip4.num);
991 route6_pkts(qconf->rt6_ctx, traffic.ip6.pkts, traffic.ip6.num);
995 drain_tx_buffers(struct lcore_conf *qconf)
1000 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
1001 buf = &qconf->tx_mbufs[portid];
1004 send_burst(qconf, buf->len, portid);
1010 drain_crypto_buffers(struct lcore_conf *qconf)
1013 struct ipsec_ctx *ctx;
1015 /* drain inbound buffers*/
1016 ctx = &qconf->inbound;
1017 for (i = 0; i != ctx->nb_qps; i++) {
1018 if (ctx->tbl[i].len != 0)
1019 enqueue_cop_burst(ctx->tbl + i);
1022 /* drain outbound buffers*/
1023 ctx = &qconf->outbound;
1024 for (i = 0; i != ctx->nb_qps; i++) {
1025 if (ctx->tbl[i].len != 0)
1026 enqueue_cop_burst(ctx->tbl + i);
1031 drain_inbound_crypto_queues(const struct lcore_conf *qconf,
1032 struct ipsec_ctx *ctx)
1035 struct ipsec_traffic trf;
1037 if (app_sa_prm.enable == 0) {
1039 /* dequeue packets from crypto-queue */
1040 n = ipsec_inbound_cqp_dequeue(ctx, trf.ipsec.pkts,
1041 RTE_DIM(trf.ipsec.pkts));
1046 /* split traffic by ipv4-ipv6 */
1047 split46_traffic(&trf, trf.ipsec.pkts, n);
1049 ipsec_cqp_process(ctx, &trf);
1051 /* process ipv4 packets */
1052 if (trf.ip4.num != 0) {
1053 inbound_sp_sa(ctx->sp4_ctx, ctx->sa_ctx, &trf.ip4, 0);
1054 route4_pkts(qconf->rt4_ctx, trf.ip4.pkts, trf.ip4.num);
1057 /* process ipv6 packets */
1058 if (trf.ip6.num != 0) {
1059 inbound_sp_sa(ctx->sp6_ctx, ctx->sa_ctx, &trf.ip6, 0);
1060 route6_pkts(qconf->rt6_ctx, trf.ip6.pkts, trf.ip6.num);
1065 drain_outbound_crypto_queues(const struct lcore_conf *qconf,
1066 struct ipsec_ctx *ctx)
1069 struct ipsec_traffic trf;
1071 if (app_sa_prm.enable == 0) {
1073 /* dequeue packets from crypto-queue */
1074 n = ipsec_outbound_cqp_dequeue(ctx, trf.ipsec.pkts,
1075 RTE_DIM(trf.ipsec.pkts));
1080 /* split traffic by ipv4-ipv6 */
1081 split46_traffic(&trf, trf.ipsec.pkts, n);
1083 ipsec_cqp_process(ctx, &trf);
1085 /* process ipv4 packets */
1086 if (trf.ip4.num != 0)
1087 route4_pkts(qconf->rt4_ctx, trf.ip4.pkts, trf.ip4.num);
1089 /* process ipv6 packets */
1090 if (trf.ip6.num != 0)
1091 route6_pkts(qconf->rt6_ctx, trf.ip6.pkts, trf.ip6.num);
1094 /* main processing loop */
1096 main_loop(__attribute__((unused)) void *dummy)
1098 struct rte_mbuf *pkts[MAX_PKT_BURST];
1100 uint64_t prev_tsc, diff_tsc, cur_tsc;
1104 struct lcore_conf *qconf;
1106 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
1107 / US_PER_S * BURST_TX_DRAIN_US;
1108 struct lcore_rx_queue *rxql;
1111 lcore_id = rte_lcore_id();
1112 qconf = &lcore_conf[lcore_id];
1113 rxql = qconf->rx_queue_list;
1114 socket_id = rte_lcore_to_socket_id(lcore_id);
1116 qconf->rt4_ctx = socket_ctx[socket_id].rt_ip4;
1117 qconf->rt6_ctx = socket_ctx[socket_id].rt_ip6;
1118 qconf->inbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_in;
1119 qconf->inbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_in;
1120 qconf->inbound.sa_ctx = socket_ctx[socket_id].sa_in;
1121 qconf->inbound.cdev_map = cdev_map_in;
1122 qconf->inbound.session_pool = socket_ctx[socket_id].session_pool;
1123 qconf->inbound.session_priv_pool =
1124 socket_ctx[socket_id].session_priv_pool;
1125 qconf->outbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_out;
1126 qconf->outbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_out;
1127 qconf->outbound.sa_ctx = socket_ctx[socket_id].sa_out;
1128 qconf->outbound.cdev_map = cdev_map_out;
1129 qconf->outbound.session_pool = socket_ctx[socket_id].session_pool;
1130 qconf->outbound.session_priv_pool =
1131 socket_ctx[socket_id].session_priv_pool;
1132 qconf->frag.pool_dir = socket_ctx[socket_id].mbuf_pool;
1133 qconf->frag.pool_indir = socket_ctx[socket_id].mbuf_pool_indir;
1135 if (qconf->nb_rx_queue == 0) {
1136 RTE_LOG(DEBUG, IPSEC, "lcore %u has nothing to do\n",
1141 RTE_LOG(INFO, IPSEC, "entering main loop on lcore %u\n", lcore_id);
1143 for (i = 0; i < qconf->nb_rx_queue; i++) {
1144 portid = rxql[i].port_id;
1145 queueid = rxql[i].queue_id;
1146 RTE_LOG(INFO, IPSEC,
1147 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
1148 lcore_id, portid, queueid);
1152 cur_tsc = rte_rdtsc();
1154 /* TX queue buffer drain */
1155 diff_tsc = cur_tsc - prev_tsc;
1157 if (unlikely(diff_tsc > drain_tsc)) {
1158 drain_tx_buffers(qconf);
1159 drain_crypto_buffers(qconf);
1163 for (i = 0; i < qconf->nb_rx_queue; ++i) {
1165 /* Read packets from RX queues */
1166 portid = rxql[i].port_id;
1167 queueid = rxql[i].queue_id;
1168 nb_rx = rte_eth_rx_burst(portid, queueid,
1169 pkts, MAX_PKT_BURST);
1172 process_pkts(qconf, pkts, nb_rx, portid);
1174 /* dequeue and process completed crypto-ops */
1175 if (UNPROTECTED_PORT(portid))
1176 drain_inbound_crypto_queues(qconf,
1179 drain_outbound_crypto_queues(qconf,
1193 if (lcore_params == NULL) {
1194 printf("Error: No port/queue/core mappings\n");
1198 for (i = 0; i < nb_lcore_params; ++i) {
1199 lcore = lcore_params[i].lcore_id;
1200 if (!rte_lcore_is_enabled(lcore)) {
1201 printf("error: lcore %hhu is not enabled in "
1202 "lcore mask\n", lcore);
1205 socket_id = rte_lcore_to_socket_id(lcore);
1206 if (socket_id != 0 && numa_on == 0) {
1207 printf("warning: lcore %hhu is on socket %d "
1211 portid = lcore_params[i].port_id;
1212 if ((enabled_port_mask & (1 << portid)) == 0) {
1213 printf("port %u is not enabled in port mask\n", portid);
1216 if (!rte_eth_dev_is_valid_port(portid)) {
1217 printf("port %u is not present on the board\n", portid);
1225 get_port_nb_rx_queues(const uint16_t port)
1230 for (i = 0; i < nb_lcore_params; ++i) {
1231 if (lcore_params[i].port_id == port &&
1232 lcore_params[i].queue_id > queue)
1233 queue = lcore_params[i].queue_id;
1235 return (uint8_t)(++queue);
1239 init_lcore_rx_queues(void)
1241 uint16_t i, nb_rx_queue;
1244 for (i = 0; i < nb_lcore_params; ++i) {
1245 lcore = lcore_params[i].lcore_id;
1246 nb_rx_queue = lcore_conf[lcore].nb_rx_queue;
1247 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1248 printf("error: too many queues (%u) for lcore: %u\n",
1249 nb_rx_queue + 1, lcore);
1252 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1253 lcore_params[i].port_id;
1254 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1255 lcore_params[i].queue_id;
1256 lcore_conf[lcore].nb_rx_queue++;
1263 print_usage(const char *prgname)
1265 fprintf(stderr, "%s [EAL options] --"
1271 " [-w REPLAY_WINDOW_SIZE]"
1275 " --config (port,queue,lcore)[,(port,queue,lcore)]"
1276 " [--single-sa SAIDX]"
1277 " [--cryptodev_mask MASK]"
1278 " [--" CMD_LINE_OPT_RX_OFFLOAD " RX_OFFLOAD_MASK]"
1279 " [--" CMD_LINE_OPT_TX_OFFLOAD " TX_OFFLOAD_MASK]"
1280 " [--" CMD_LINE_OPT_REASSEMBLE " REASSEMBLE_TABLE_SIZE]"
1281 " [--" CMD_LINE_OPT_MTU " MTU]"
1283 " -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
1284 " -P : Enable promiscuous mode\n"
1285 " -u PORTMASK: Hexadecimal bitmask of unprotected ports\n"
1286 " -j FRAMESIZE: Data buffer size, minimum (and default)\n"
1287 " value: RTE_MBUF_DEFAULT_BUF_SIZE\n"
1288 " -l enables code-path that uses librte_ipsec\n"
1289 " -w REPLAY_WINDOW_SIZE specifies IPsec SQN replay window\n"
1290 " size for each SA\n"
1292 " -a enables SA SQN atomic behaviour\n"
1293 " -f CONFIG_FILE: Configuration file\n"
1294 " --config (port,queue,lcore): Rx queue configuration\n"
1295 " --single-sa SAIDX: Use single SA index for outbound traffic,\n"
1296 " bypassing the SP\n"
1297 " --cryptodev_mask MASK: Hexadecimal bitmask of the crypto\n"
1298 " devices to configure\n"
1299 " --" CMD_LINE_OPT_RX_OFFLOAD
1300 ": bitmask of the RX HW offload capabilities to enable/use\n"
1301 " (DEV_RX_OFFLOAD_*)\n"
1302 " --" CMD_LINE_OPT_TX_OFFLOAD
1303 ": bitmask of the TX HW offload capabilities to enable/use\n"
1304 " (DEV_TX_OFFLOAD_*)\n"
1305 " --" CMD_LINE_OPT_REASSEMBLE " NUM"
1306 ": max number of entries in reassemble(fragment) table\n"
1307 " (zero (default value) disables reassembly)\n"
1308 " --" CMD_LINE_OPT_MTU " MTU"
1309 ": MTU value on all ports (default value: 1500)\n"
1310 " outgoing packets with bigger size will be fragmented\n"
1311 " incoming packets with bigger size will be discarded\n"
1312 " --" CMD_LINE_OPT_FRAG_TTL " FRAG_TTL_NS"
1313 ": fragments lifetime in nanoseconds, default\n"
1314 " and maximum value is 10.000.000.000 ns (10 s)\n"
1320 parse_mask(const char *str, uint64_t *val)
1326 t = strtoul(str, &end, 0);
1327 if (errno != 0 || end[0] != 0)
1335 parse_portmask(const char *portmask)
1340 /* parse hexadecimal string */
1341 pm = strtoul(portmask, &end, 16);
1342 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1345 if ((pm == 0) && errno)
1352 parse_decimal(const char *str)
1357 num = strtoull(str, &end, 10);
1358 if ((str[0] == '\0') || (end == NULL) || (*end != '\0')
1366 parse_config(const char *q_arg)
1369 const char *p, *p0 = q_arg;
1377 unsigned long int_fld[_NUM_FLD];
1378 char *str_fld[_NUM_FLD];
1382 nb_lcore_params = 0;
1384 while ((p = strchr(p0, '(')) != NULL) {
1386 p0 = strchr(p, ')');
1391 if (size >= sizeof(s))
1394 snprintf(s, sizeof(s), "%.*s", size, p);
1395 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1398 for (i = 0; i < _NUM_FLD; i++) {
1400 int_fld[i] = strtoul(str_fld[i], &end, 0);
1401 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
1404 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1405 printf("exceeded max number of lcore params: %hu\n",
1409 lcore_params_array[nb_lcore_params].port_id =
1410 (uint8_t)int_fld[FLD_PORT];
1411 lcore_params_array[nb_lcore_params].queue_id =
1412 (uint8_t)int_fld[FLD_QUEUE];
1413 lcore_params_array[nb_lcore_params].lcore_id =
1414 (uint8_t)int_fld[FLD_LCORE];
1417 lcore_params = lcore_params_array;
1422 print_app_sa_prm(const struct app_sa_prm *prm)
1424 printf("librte_ipsec usage: %s\n",
1425 (prm->enable == 0) ? "disabled" : "enabled");
1427 if (prm->enable == 0)
1430 printf("replay window size: %u\n", prm->window_size);
1431 printf("ESN: %s\n", (prm->enable_esn == 0) ? "disabled" : "enabled");
1432 printf("SA flags: %#" PRIx64 "\n", prm->flags);
1433 printf("Frag TTL: %" PRIu64 " ns\n", frag_ttl_ns);
1437 parse_args(int32_t argc, char **argv)
1442 int32_t option_index;
1443 char *prgname = argv[0];
1444 int32_t f_present = 0;
1448 while ((opt = getopt_long(argc, argvopt, "aelp:Pu:f:j:w:",
1449 lgopts, &option_index)) != EOF) {
1453 enabled_port_mask = parse_portmask(optarg);
1454 if (enabled_port_mask == 0) {
1455 printf("invalid portmask\n");
1456 print_usage(prgname);
1461 printf("Promiscuous mode selected\n");
1465 unprotected_port_mask = parse_portmask(optarg);
1466 if (unprotected_port_mask == 0) {
1467 printf("invalid unprotected portmask\n");
1468 print_usage(prgname);
1473 if (f_present == 1) {
1474 printf("\"-f\" option present more than "
1476 print_usage(prgname);
1483 ret = parse_decimal(optarg);
1484 if (ret < RTE_MBUF_DEFAULT_BUF_SIZE ||
1486 printf("Invalid frame buffer size value: %s\n",
1488 print_usage(prgname);
1491 frame_buf_size = ret;
1492 printf("Custom frame buffer size %u\n", frame_buf_size);
1495 app_sa_prm.enable = 1;
1498 app_sa_prm.enable = 1;
1499 app_sa_prm.window_size = parse_decimal(optarg);
1502 app_sa_prm.enable = 1;
1503 app_sa_prm.enable_esn = 1;
1506 app_sa_prm.enable = 1;
1507 app_sa_prm.flags |= RTE_IPSEC_SAFLAG_SQN_ATOM;
1509 case CMD_LINE_OPT_CONFIG_NUM:
1510 ret = parse_config(optarg);
1512 printf("Invalid config\n");
1513 print_usage(prgname);
1517 case CMD_LINE_OPT_SINGLE_SA_NUM:
1518 ret = parse_decimal(optarg);
1519 if (ret == -1 || ret > UINT32_MAX) {
1520 printf("Invalid argument[sa_idx]\n");
1521 print_usage(prgname);
1527 single_sa_idx = ret;
1528 printf("Configured with single SA index %u\n",
1531 case CMD_LINE_OPT_CRYPTODEV_MASK_NUM:
1532 ret = parse_portmask(optarg);
1534 printf("Invalid argument[portmask]\n");
1535 print_usage(prgname);
1540 enabled_cryptodev_mask = ret;
1542 case CMD_LINE_OPT_RX_OFFLOAD_NUM:
1543 ret = parse_mask(optarg, &dev_rx_offload);
1545 printf("Invalid argument for \'%s\': %s\n",
1546 CMD_LINE_OPT_RX_OFFLOAD, optarg);
1547 print_usage(prgname);
1551 case CMD_LINE_OPT_TX_OFFLOAD_NUM:
1552 ret = parse_mask(optarg, &dev_tx_offload);
1554 printf("Invalid argument for \'%s\': %s\n",
1555 CMD_LINE_OPT_TX_OFFLOAD, optarg);
1556 print_usage(prgname);
1560 case CMD_LINE_OPT_REASSEMBLE_NUM:
1561 ret = parse_decimal(optarg);
1562 if (ret < 0 || ret > UINT32_MAX) {
1563 printf("Invalid argument for \'%s\': %s\n",
1564 CMD_LINE_OPT_REASSEMBLE, optarg);
1565 print_usage(prgname);
1570 case CMD_LINE_OPT_MTU_NUM:
1571 ret = parse_decimal(optarg);
1572 if (ret < 0 || ret > RTE_IPV4_MAX_PKT_LEN) {
1573 printf("Invalid argument for \'%s\': %s\n",
1574 CMD_LINE_OPT_MTU, optarg);
1575 print_usage(prgname);
1580 case CMD_LINE_OPT_FRAG_TTL_NUM:
1581 ret = parse_decimal(optarg);
1582 if (ret < 0 || ret > MAX_FRAG_TTL_NS) {
1583 printf("Invalid argument for \'%s\': %s\n",
1584 CMD_LINE_OPT_MTU, optarg);
1585 print_usage(prgname);
1591 print_usage(prgname);
1596 if (f_present == 0) {
1597 printf("Mandatory option \"-f\" not present\n");
1601 /* check do we need to enable multi-seg support */
1602 if (multi_seg_required()) {
1603 /* legacy mode doesn't support multi-seg */
1604 app_sa_prm.enable = 1;
1605 printf("frame buf size: %u, mtu: %u, "
1606 "number of reassemble entries: %u\n"
1607 "multi-segment support is required\n",
1608 frame_buf_size, mtu_size, frag_tbl_sz);
1611 print_app_sa_prm(&app_sa_prm);
1614 argv[optind-1] = prgname;
1617 optind = 1; /* reset getopt lib */
1622 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
1624 char buf[RTE_ETHER_ADDR_FMT_SIZE];
1625 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
1626 printf("%s%s", name, buf);
1630 * Update destination ethaddr for the port.
1633 add_dst_ethaddr(uint16_t port, const struct rte_ether_addr *addr)
1635 if (port >= RTE_DIM(ethaddr_tbl))
1638 ethaddr_tbl[port].dst = ETHADDR_TO_UINT64(addr);
1642 /* Check the link status of all ports in up to 9s, and print them finally */
1644 check_all_ports_link_status(uint32_t port_mask)
1646 #define CHECK_INTERVAL 100 /* 100ms */
1647 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1649 uint8_t count, all_ports_up, print_flag = 0;
1650 struct rte_eth_link link;
1653 printf("\nChecking link status");
1655 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1657 RTE_ETH_FOREACH_DEV(portid) {
1658 if ((port_mask & (1 << portid)) == 0)
1660 memset(&link, 0, sizeof(link));
1661 ret = rte_eth_link_get_nowait(portid, &link);
1664 if (print_flag == 1)
1665 printf("Port %u link get failed: %s\n",
1666 portid, rte_strerror(-ret));
1669 /* print link status if flag set */
1670 if (print_flag == 1) {
1671 if (link.link_status)
1673 "Port%d Link Up - speed %u Mbps -%s\n",
1674 portid, link.link_speed,
1675 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1676 ("full-duplex") : ("half-duplex\n"));
1678 printf("Port %d Link Down\n", portid);
1681 /* clear all_ports_up flag if any link down */
1682 if (link.link_status == ETH_LINK_DOWN) {
1687 /* after finally printing all link status, get out */
1688 if (print_flag == 1)
1691 if (all_ports_up == 0) {
1694 rte_delay_ms(CHECK_INTERVAL);
1697 /* set the print_flag if all ports up or timeout */
1698 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1706 add_mapping(struct rte_hash *map, const char *str, uint16_t cdev_id,
1707 uint16_t qp, struct lcore_params *params,
1708 struct ipsec_ctx *ipsec_ctx,
1709 const struct rte_cryptodev_capabilities *cipher,
1710 const struct rte_cryptodev_capabilities *auth,
1711 const struct rte_cryptodev_capabilities *aead)
1715 struct cdev_key key = { 0 };
1717 key.lcore_id = params->lcore_id;
1719 key.cipher_algo = cipher->sym.cipher.algo;
1721 key.auth_algo = auth->sym.auth.algo;
1723 key.aead_algo = aead->sym.aead.algo;
1725 ret = rte_hash_lookup(map, &key);
1729 for (i = 0; i < ipsec_ctx->nb_qps; i++)
1730 if (ipsec_ctx->tbl[i].id == cdev_id)
1733 if (i == ipsec_ctx->nb_qps) {
1734 if (ipsec_ctx->nb_qps == MAX_QP_PER_LCORE) {
1735 printf("Maximum number of crypto devices assigned to "
1736 "a core, increase MAX_QP_PER_LCORE value\n");
1739 ipsec_ctx->tbl[i].id = cdev_id;
1740 ipsec_ctx->tbl[i].qp = qp;
1741 ipsec_ctx->nb_qps++;
1742 printf("%s cdev mapping: lcore %u using cdev %u qp %u "
1743 "(cdev_id_qp %lu)\n", str, key.lcore_id,
1747 ret = rte_hash_add_key_data(map, &key, (void *)i);
1749 printf("Faled to insert cdev mapping for (lcore %u, "
1750 "cdev %u, qp %u), errno %d\n",
1751 key.lcore_id, ipsec_ctx->tbl[i].id,
1752 ipsec_ctx->tbl[i].qp, ret);
1760 add_cdev_mapping(struct rte_cryptodev_info *dev_info, uint16_t cdev_id,
1761 uint16_t qp, struct lcore_params *params)
1764 const struct rte_cryptodev_capabilities *i, *j;
1765 struct rte_hash *map;
1766 struct lcore_conf *qconf;
1767 struct ipsec_ctx *ipsec_ctx;
1770 qconf = &lcore_conf[params->lcore_id];
1772 if ((unprotected_port_mask & (1 << params->port_id)) == 0) {
1774 ipsec_ctx = &qconf->outbound;
1778 ipsec_ctx = &qconf->inbound;
1782 /* Required cryptodevs with operation chainning */
1783 if (!(dev_info->feature_flags &
1784 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING))
1787 for (i = dev_info->capabilities;
1788 i->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; i++) {
1789 if (i->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1792 if (i->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1793 ret |= add_mapping(map, str, cdev_id, qp, params,
1794 ipsec_ctx, NULL, NULL, i);
1798 if (i->sym.xform_type != RTE_CRYPTO_SYM_XFORM_CIPHER)
1801 for (j = dev_info->capabilities;
1802 j->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; j++) {
1803 if (j->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1806 if (j->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH)
1809 ret |= add_mapping(map, str, cdev_id, qp, params,
1810 ipsec_ctx, i, j, NULL);
1817 /* Check if the device is enabled by cryptodev_mask */
1819 check_cryptodev_mask(uint8_t cdev_id)
1821 if (enabled_cryptodev_mask & (1 << cdev_id))
1828 cryptodevs_init(void)
1830 struct rte_cryptodev_config dev_conf;
1831 struct rte_cryptodev_qp_conf qp_conf;
1832 uint16_t idx, max_nb_qps, qp, i;
1834 struct rte_hash_parameters params = { 0 };
1836 const uint64_t mseg_flag = multi_seg_required() ?
1837 RTE_CRYPTODEV_FF_IN_PLACE_SGL : 0;
1839 params.entries = CDEV_MAP_ENTRIES;
1840 params.key_len = sizeof(struct cdev_key);
1841 params.hash_func = rte_jhash;
1842 params.hash_func_init_val = 0;
1843 params.socket_id = rte_socket_id();
1845 params.name = "cdev_map_in";
1846 cdev_map_in = rte_hash_create(¶ms);
1847 if (cdev_map_in == NULL)
1848 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1851 params.name = "cdev_map_out";
1852 cdev_map_out = rte_hash_create(¶ms);
1853 if (cdev_map_out == NULL)
1854 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1857 printf("lcore/cryptodev/qp mappings:\n");
1860 for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1861 struct rte_cryptodev_info cdev_info;
1863 if (check_cryptodev_mask((uint8_t)cdev_id))
1866 rte_cryptodev_info_get(cdev_id, &cdev_info);
1868 if ((mseg_flag & cdev_info.feature_flags) != mseg_flag)
1869 rte_exit(EXIT_FAILURE,
1870 "Device %hd does not support \'%s\' feature\n",
1872 rte_cryptodev_get_feature_name(mseg_flag));
1874 if (nb_lcore_params > cdev_info.max_nb_queue_pairs)
1875 max_nb_qps = cdev_info.max_nb_queue_pairs;
1877 max_nb_qps = nb_lcore_params;
1881 while (qp < max_nb_qps && i < nb_lcore_params) {
1882 if (add_cdev_mapping(&cdev_info, cdev_id, qp,
1883 &lcore_params[idx]))
1886 idx = idx % nb_lcore_params;
1893 dev_conf.socket_id = rte_cryptodev_socket_id(cdev_id);
1894 dev_conf.nb_queue_pairs = qp;
1895 dev_conf.ff_disable = RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO;
1897 uint32_t dev_max_sess = cdev_info.sym.max_nb_sessions;
1898 if (dev_max_sess != 0 && dev_max_sess < CDEV_MP_NB_OBJS)
1899 rte_exit(EXIT_FAILURE,
1900 "Device does not support at least %u "
1901 "sessions", CDEV_MP_NB_OBJS);
1903 if (rte_cryptodev_configure(cdev_id, &dev_conf))
1904 rte_panic("Failed to initialize cryptodev %u\n",
1907 qp_conf.nb_descriptors = CDEV_QUEUE_DESC;
1908 qp_conf.mp_session =
1909 socket_ctx[dev_conf.socket_id].session_pool;
1910 qp_conf.mp_session_private =
1911 socket_ctx[dev_conf.socket_id].session_priv_pool;
1912 for (qp = 0; qp < dev_conf.nb_queue_pairs; qp++)
1913 if (rte_cryptodev_queue_pair_setup(cdev_id, qp,
1914 &qp_conf, dev_conf.socket_id))
1915 rte_panic("Failed to setup queue %u for "
1916 "cdev_id %u\n", 0, cdev_id);
1918 if (rte_cryptodev_start(cdev_id))
1919 rte_panic("Failed to start cryptodev %u\n",
1929 port_init(uint16_t portid, uint64_t req_rx_offloads, uint64_t req_tx_offloads)
1931 uint32_t frame_size;
1932 struct rte_eth_dev_info dev_info;
1933 struct rte_eth_txconf *txconf;
1934 uint16_t nb_tx_queue, nb_rx_queue;
1935 uint16_t tx_queueid, rx_queueid, queue, lcore_id;
1936 int32_t ret, socket_id;
1937 struct lcore_conf *qconf;
1938 struct rte_ether_addr ethaddr;
1939 struct rte_eth_conf local_port_conf = port_conf;
1941 ret = rte_eth_dev_info_get(portid, &dev_info);
1943 rte_exit(EXIT_FAILURE,
1944 "Error during getting device (port %u) info: %s\n",
1945 portid, strerror(-ret));
1947 /* limit allowed HW offloafs, as user requested */
1948 dev_info.rx_offload_capa &= dev_rx_offload;
1949 dev_info.tx_offload_capa &= dev_tx_offload;
1951 printf("Configuring device port %u:\n", portid);
1953 ret = rte_eth_macaddr_get(portid, ðaddr);
1955 rte_exit(EXIT_FAILURE,
1956 "Error getting MAC address (port %u): %s\n",
1957 portid, rte_strerror(-ret));
1959 ethaddr_tbl[portid].src = ETHADDR_TO_UINT64(ðaddr);
1960 print_ethaddr("Address: ", ðaddr);
1963 nb_rx_queue = get_port_nb_rx_queues(portid);
1964 nb_tx_queue = nb_lcores;
1966 if (nb_rx_queue > dev_info.max_rx_queues)
1967 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1968 "(max rx queue is %u)\n",
1969 nb_rx_queue, dev_info.max_rx_queues);
1971 if (nb_tx_queue > dev_info.max_tx_queues)
1972 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1973 "(max tx queue is %u)\n",
1974 nb_tx_queue, dev_info.max_tx_queues);
1976 printf("Creating queues: nb_rx_queue=%d nb_tx_queue=%u...\n",
1977 nb_rx_queue, nb_tx_queue);
1979 frame_size = MTU_TO_FRAMELEN(mtu_size);
1980 if (frame_size > local_port_conf.rxmode.max_rx_pkt_len)
1981 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
1982 local_port_conf.rxmode.max_rx_pkt_len = frame_size;
1984 if (multi_seg_required()) {
1985 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_SCATTER;
1986 local_port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
1989 local_port_conf.rxmode.offloads |= req_rx_offloads;
1990 local_port_conf.txmode.offloads |= req_tx_offloads;
1992 /* Check that all required capabilities are supported */
1993 if ((local_port_conf.rxmode.offloads & dev_info.rx_offload_capa) !=
1994 local_port_conf.rxmode.offloads)
1995 rte_exit(EXIT_FAILURE,
1996 "Error: port %u required RX offloads: 0x%" PRIx64
1997 ", avaialbe RX offloads: 0x%" PRIx64 "\n",
1998 portid, local_port_conf.rxmode.offloads,
1999 dev_info.rx_offload_capa);
2001 if ((local_port_conf.txmode.offloads & dev_info.tx_offload_capa) !=
2002 local_port_conf.txmode.offloads)
2003 rte_exit(EXIT_FAILURE,
2004 "Error: port %u required TX offloads: 0x%" PRIx64
2005 ", avaialbe TX offloads: 0x%" PRIx64 "\n",
2006 portid, local_port_conf.txmode.offloads,
2007 dev_info.tx_offload_capa);
2009 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2010 local_port_conf.txmode.offloads |=
2011 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2013 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_IPV4_CKSUM)
2014 local_port_conf.txmode.offloads |= DEV_TX_OFFLOAD_IPV4_CKSUM;
2016 printf("port %u configurng rx_offloads=0x%" PRIx64
2017 ", tx_offloads=0x%" PRIx64 "\n",
2018 portid, local_port_conf.rxmode.offloads,
2019 local_port_conf.txmode.offloads);
2021 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
2022 dev_info.flow_type_rss_offloads;
2023 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
2024 port_conf.rx_adv_conf.rss_conf.rss_hf) {
2025 printf("Port %u modified RSS hash function based on hardware support,"
2026 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
2028 port_conf.rx_adv_conf.rss_conf.rss_hf,
2029 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
2032 ret = rte_eth_dev_configure(portid, nb_rx_queue, nb_tx_queue,
2035 rte_exit(EXIT_FAILURE, "Cannot configure device: "
2036 "err=%d, port=%d\n", ret, portid);
2038 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
2040 rte_exit(EXIT_FAILURE, "Cannot adjust number of descriptors: "
2041 "err=%d, port=%d\n", ret, portid);
2043 /* init one TX queue per lcore */
2045 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2046 if (rte_lcore_is_enabled(lcore_id) == 0)
2050 socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2055 printf("Setup txq=%u,%d,%d\n", lcore_id, tx_queueid, socket_id);
2057 txconf = &dev_info.default_txconf;
2058 txconf->offloads = local_port_conf.txmode.offloads;
2060 ret = rte_eth_tx_queue_setup(portid, tx_queueid, nb_txd,
2063 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
2064 "err=%d, port=%d\n", ret, portid);
2066 qconf = &lcore_conf[lcore_id];
2067 qconf->tx_queue_id[portid] = tx_queueid;
2069 /* Pre-populate pkt offloads based on capabilities */
2070 qconf->outbound.ipv4_offloads = PKT_TX_IPV4;
2071 qconf->outbound.ipv6_offloads = PKT_TX_IPV6;
2072 if (local_port_conf.txmode.offloads & DEV_TX_OFFLOAD_IPV4_CKSUM)
2073 qconf->outbound.ipv4_offloads |= PKT_TX_IP_CKSUM;
2077 /* init RX queues */
2078 for (queue = 0; queue < qconf->nb_rx_queue; ++queue) {
2079 struct rte_eth_rxconf rxq_conf;
2081 if (portid != qconf->rx_queue_list[queue].port_id)
2084 rx_queueid = qconf->rx_queue_list[queue].queue_id;
2086 printf("Setup rxq=%d,%d,%d\n", portid, rx_queueid,
2089 rxq_conf = dev_info.default_rxconf;
2090 rxq_conf.offloads = local_port_conf.rxmode.offloads;
2091 ret = rte_eth_rx_queue_setup(portid, rx_queueid,
2092 nb_rxd, socket_id, &rxq_conf,
2093 socket_ctx[socket_id].mbuf_pool);
2095 rte_exit(EXIT_FAILURE,
2096 "rte_eth_rx_queue_setup: err=%d, "
2097 "port=%d\n", ret, portid);
2104 max_session_size(void)
2108 int16_t cdev_id, port_id, n;
2111 n = rte_cryptodev_count();
2112 for (cdev_id = 0; cdev_id != n; cdev_id++) {
2113 sz = rte_cryptodev_sym_get_private_session_size(cdev_id);
2117 * If crypto device is security capable, need to check the
2118 * size of security session as well.
2121 /* Get security context of the crypto device */
2122 sec_ctx = rte_cryptodev_get_sec_ctx(cdev_id);
2123 if (sec_ctx == NULL)
2126 /* Get size of security session */
2127 sz = rte_security_session_get_size(sec_ctx);
2132 RTE_ETH_FOREACH_DEV(port_id) {
2133 if ((enabled_port_mask & (1 << port_id)) == 0)
2136 sec_ctx = rte_eth_dev_get_sec_ctx(port_id);
2137 if (sec_ctx == NULL)
2140 sz = rte_security_session_get_size(sec_ctx);
2149 session_pool_init(struct socket_ctx *ctx, int32_t socket_id, size_t sess_sz)
2151 char mp_name[RTE_MEMPOOL_NAMESIZE];
2152 struct rte_mempool *sess_mp;
2154 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2155 "sess_mp_%u", socket_id);
2156 sess_mp = rte_cryptodev_sym_session_pool_create(
2157 mp_name, CDEV_MP_NB_OBJS,
2158 sess_sz, CDEV_MP_CACHE_SZ, 0,
2160 ctx->session_pool = sess_mp;
2162 if (ctx->session_pool == NULL)
2163 rte_exit(EXIT_FAILURE,
2164 "Cannot init session pool on socket %d\n", socket_id);
2166 printf("Allocated session pool on socket %d\n", socket_id);
2170 session_priv_pool_init(struct socket_ctx *ctx, int32_t socket_id,
2173 char mp_name[RTE_MEMPOOL_NAMESIZE];
2174 struct rte_mempool *sess_mp;
2176 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2177 "sess_mp_priv_%u", socket_id);
2178 sess_mp = rte_mempool_create(mp_name,
2182 0, NULL, NULL, NULL,
2185 ctx->session_priv_pool = sess_mp;
2187 if (ctx->session_priv_pool == NULL)
2188 rte_exit(EXIT_FAILURE,
2189 "Cannot init session priv pool on socket %d\n",
2192 printf("Allocated session priv pool on socket %d\n",
2197 pool_init(struct socket_ctx *ctx, int32_t socket_id, uint32_t nb_mbuf)
2202 snprintf(s, sizeof(s), "mbuf_pool_%d", socket_id);
2203 ctx->mbuf_pool = rte_pktmbuf_pool_create(s, nb_mbuf,
2204 MEMPOOL_CACHE_SIZE, ipsec_metadata_size(),
2205 frame_buf_size, socket_id);
2208 * if multi-segment support is enabled, then create a pool
2209 * for indirect mbufs.
2211 ms = multi_seg_required();
2213 snprintf(s, sizeof(s), "mbuf_pool_indir_%d", socket_id);
2214 ctx->mbuf_pool_indir = rte_pktmbuf_pool_create(s, nb_mbuf,
2215 MEMPOOL_CACHE_SIZE, 0, 0, socket_id);
2218 if (ctx->mbuf_pool == NULL || (ms != 0 && ctx->mbuf_pool_indir == NULL))
2219 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n",
2222 printf("Allocated mbuf pool on socket %d\n", socket_id);
2226 inline_ipsec_event_esn_overflow(struct rte_security_ctx *ctx, uint64_t md)
2228 struct ipsec_sa *sa;
2230 /* For inline protocol processing, the metadata in the event will
2231 * uniquely identify the security session which raised the event.
2232 * Application would then need the userdata it had registered with the
2233 * security session to process the event.
2236 sa = (struct ipsec_sa *)rte_security_get_userdata(ctx, md);
2239 /* userdata could not be retrieved */
2243 /* Sequence number over flow. SA need to be re-established */
2249 inline_ipsec_event_callback(uint16_t port_id, enum rte_eth_event_type type,
2250 void *param, void *ret_param)
2253 struct rte_eth_event_ipsec_desc *event_desc = NULL;
2254 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
2255 rte_eth_dev_get_sec_ctx(port_id);
2257 RTE_SET_USED(param);
2259 if (type != RTE_ETH_EVENT_IPSEC)
2262 event_desc = ret_param;
2263 if (event_desc == NULL) {
2264 printf("Event descriptor not set\n");
2268 md = event_desc->metadata;
2270 if (event_desc->subtype == RTE_ETH_EVENT_IPSEC_ESN_OVERFLOW)
2271 return inline_ipsec_event_esn_overflow(ctx, md);
2272 else if (event_desc->subtype >= RTE_ETH_EVENT_IPSEC_MAX) {
2273 printf("Invalid IPsec event reported\n");
2281 rx_callback(__rte_unused uint16_t port, __rte_unused uint16_t queue,
2282 struct rte_mbuf *pkt[], uint16_t nb_pkts,
2283 __rte_unused uint16_t max_pkts, void *user_param)
2287 struct lcore_conf *lc;
2288 struct rte_mbuf *mb;
2289 struct rte_ether_hdr *eth;
2295 for (i = 0; i != nb_pkts; i++) {
2298 eth = rte_pktmbuf_mtod(mb, struct rte_ether_hdr *);
2299 if (eth->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
2301 struct rte_ipv4_hdr *iph;
2303 iph = (struct rte_ipv4_hdr *)(eth + 1);
2304 if (rte_ipv4_frag_pkt_is_fragmented(iph)) {
2306 mb->l2_len = sizeof(*eth);
2307 mb->l3_len = sizeof(*iph);
2308 tm = (tm != 0) ? tm : rte_rdtsc();
2309 mb = rte_ipv4_frag_reassemble_packet(
2310 lc->frag.tbl, &lc->frag.dr,
2314 /* fix ip cksum after reassemble. */
2315 iph = rte_pktmbuf_mtod_offset(mb,
2316 struct rte_ipv4_hdr *,
2318 iph->hdr_checksum = 0;
2319 iph->hdr_checksum = rte_ipv4_cksum(iph);
2322 } else if (eth->ether_type ==
2323 rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
2325 struct rte_ipv6_hdr *iph;
2326 struct ipv6_extension_fragment *fh;
2328 iph = (struct rte_ipv6_hdr *)(eth + 1);
2329 fh = rte_ipv6_frag_get_ipv6_fragment_header(iph);
2331 mb->l2_len = sizeof(*eth);
2332 mb->l3_len = (uintptr_t)fh - (uintptr_t)iph +
2334 tm = (tm != 0) ? tm : rte_rdtsc();
2335 mb = rte_ipv6_frag_reassemble_packet(
2336 lc->frag.tbl, &lc->frag.dr,
2339 /* fix l3_len after reassemble. */
2340 mb->l3_len = mb->l3_len - sizeof(*fh);
2348 /* some fragments were encountered, drain death row */
2350 rte_ip_frag_free_death_row(&lc->frag.dr, 0);
2357 reassemble_lcore_init(struct lcore_conf *lc, uint32_t cid)
2361 uint64_t frag_cycles;
2362 const struct lcore_rx_queue *rxq;
2363 const struct rte_eth_rxtx_callback *cb;
2365 /* create fragment table */
2366 sid = rte_lcore_to_socket_id(cid);
2367 frag_cycles = (rte_get_tsc_hz() + NS_PER_S - 1) /
2368 NS_PER_S * frag_ttl_ns;
2370 lc->frag.tbl = rte_ip_frag_table_create(frag_tbl_sz,
2371 FRAG_TBL_BUCKET_ENTRIES, frag_tbl_sz, frag_cycles, sid);
2372 if (lc->frag.tbl == NULL) {
2373 printf("%s(%u): failed to create fragment table of size: %u, "
2375 __func__, cid, frag_tbl_sz, rte_errno);
2379 /* setup reassemble RX callbacks for all queues */
2380 for (i = 0; i != lc->nb_rx_queue; i++) {
2382 rxq = lc->rx_queue_list + i;
2383 cb = rte_eth_add_rx_callback(rxq->port_id, rxq->queue_id,
2386 printf("%s(%u): failed to install RX callback for "
2387 "portid=%u, queueid=%u, error code: %d\n",
2389 rxq->port_id, rxq->queue_id, rte_errno);
2398 reassemble_init(void)
2404 for (i = 0; i != nb_lcore_params; i++) {
2405 lc = lcore_params[i].lcore_id;
2406 rc = reassemble_lcore_init(lcore_conf + lc, lc);
2415 main(int32_t argc, char **argv)
2422 uint64_t req_rx_offloads, req_tx_offloads;
2426 ret = rte_eal_init(argc, argv);
2428 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
2432 /* parse application arguments (after the EAL ones) */
2433 ret = parse_args(argc, argv);
2435 rte_exit(EXIT_FAILURE, "Invalid parameters\n");
2437 /* parse configuration file */
2438 if (parse_cfg_file(cfgfile) < 0) {
2439 printf("parsing file \"%s\" failed\n",
2441 print_usage(argv[0]);
2445 if ((unprotected_port_mask & enabled_port_mask) !=
2446 unprotected_port_mask)
2447 rte_exit(EXIT_FAILURE, "Invalid unprotected portmask 0x%x\n",
2448 unprotected_port_mask);
2450 if (check_params() < 0)
2451 rte_exit(EXIT_FAILURE, "check_params failed\n");
2453 ret = init_lcore_rx_queues();
2455 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2457 nb_lcores = rte_lcore_count();
2459 sess_sz = max_session_size();
2461 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2462 if (rte_lcore_is_enabled(lcore_id) == 0)
2466 socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2470 /* mbuf_pool is initialised by the pool_init() function*/
2471 if (socket_ctx[socket_id].mbuf_pool)
2474 pool_init(&socket_ctx[socket_id], socket_id, NB_MBUF);
2475 session_pool_init(&socket_ctx[socket_id], socket_id, sess_sz);
2476 session_priv_pool_init(&socket_ctx[socket_id], socket_id,
2480 RTE_ETH_FOREACH_DEV(portid) {
2481 if ((enabled_port_mask & (1 << portid)) == 0)
2484 sa_check_offloads(portid, &req_rx_offloads, &req_tx_offloads);
2485 port_init(portid, req_rx_offloads, req_tx_offloads);
2491 RTE_ETH_FOREACH_DEV(portid) {
2492 if ((enabled_port_mask & (1 << portid)) == 0)
2497 * note: device must be started before a flow rule
2500 ret = rte_eth_dev_start(portid);
2502 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: "
2503 "err=%d, port=%d\n", ret, portid);
2505 * If enabled, put device in promiscuous mode.
2506 * This allows IO forwarding mode to forward packets
2507 * to itself through 2 cross-connected ports of the
2510 if (promiscuous_on) {
2511 ret = rte_eth_promiscuous_enable(portid);
2513 rte_exit(EXIT_FAILURE,
2514 "rte_eth_promiscuous_enable: err=%s, port=%d\n",
2515 rte_strerror(-ret), portid);
2518 rte_eth_dev_callback_register(portid,
2519 RTE_ETH_EVENT_IPSEC, inline_ipsec_event_callback, NULL);
2522 /* fragment reassemble is enabled */
2523 if (frag_tbl_sz != 0) {
2524 ret = reassemble_init();
2526 rte_exit(EXIT_FAILURE, "failed at reassemble init");
2529 /* Replicate each context per socket */
2530 for (i = 0; i < NB_SOCKETS && i < rte_socket_count(); i++) {
2531 socket_id = rte_socket_id_by_idx(i);
2532 if ((socket_ctx[socket_id].mbuf_pool != NULL) &&
2533 (socket_ctx[socket_id].sa_in == NULL) &&
2534 (socket_ctx[socket_id].sa_out == NULL)) {
2535 sa_init(&socket_ctx[socket_id], socket_id);
2536 sp4_init(&socket_ctx[socket_id], socket_id);
2537 sp6_init(&socket_ctx[socket_id], socket_id);
2538 rt_init(&socket_ctx[socket_id], socket_id);
2542 check_all_ports_link_status(enabled_port_mask);
2544 /* launch per-lcore init on every lcore */
2545 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2546 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2547 if (rte_eal_wait_lcore(lcore_id) < 0)