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 printf("replay window size: %u\n", prm->window_size);
1428 printf("ESN: %s\n", (prm->enable_esn == 0) ? "disabled" : "enabled");
1429 printf("SA flags: %#" PRIx64 "\n", prm->flags);
1430 printf("Frag TTL: %" PRIu64 " ns\n", frag_ttl_ns);
1434 parse_args(int32_t argc, char **argv)
1439 int32_t option_index;
1440 char *prgname = argv[0];
1441 int32_t f_present = 0;
1445 while ((opt = getopt_long(argc, argvopt, "aelp:Pu:f:j:w:",
1446 lgopts, &option_index)) != EOF) {
1450 enabled_port_mask = parse_portmask(optarg);
1451 if (enabled_port_mask == 0) {
1452 printf("invalid portmask\n");
1453 print_usage(prgname);
1458 printf("Promiscuous mode selected\n");
1462 unprotected_port_mask = parse_portmask(optarg);
1463 if (unprotected_port_mask == 0) {
1464 printf("invalid unprotected portmask\n");
1465 print_usage(prgname);
1470 if (f_present == 1) {
1471 printf("\"-f\" option present more than "
1473 print_usage(prgname);
1480 ret = parse_decimal(optarg);
1481 if (ret < RTE_MBUF_DEFAULT_BUF_SIZE ||
1483 printf("Invalid frame buffer size value: %s\n",
1485 print_usage(prgname);
1488 frame_buf_size = ret;
1489 printf("Custom frame buffer size %u\n", frame_buf_size);
1492 app_sa_prm.enable = 1;
1495 app_sa_prm.window_size = parse_decimal(optarg);
1498 app_sa_prm.enable_esn = 1;
1501 app_sa_prm.enable = 1;
1502 app_sa_prm.flags |= RTE_IPSEC_SAFLAG_SQN_ATOM;
1504 case CMD_LINE_OPT_CONFIG_NUM:
1505 ret = parse_config(optarg);
1507 printf("Invalid config\n");
1508 print_usage(prgname);
1512 case CMD_LINE_OPT_SINGLE_SA_NUM:
1513 ret = parse_decimal(optarg);
1514 if (ret == -1 || ret > UINT32_MAX) {
1515 printf("Invalid argument[sa_idx]\n");
1516 print_usage(prgname);
1522 single_sa_idx = ret;
1523 printf("Configured with single SA index %u\n",
1526 case CMD_LINE_OPT_CRYPTODEV_MASK_NUM:
1527 ret = parse_portmask(optarg);
1529 printf("Invalid argument[portmask]\n");
1530 print_usage(prgname);
1535 enabled_cryptodev_mask = ret;
1537 case CMD_LINE_OPT_RX_OFFLOAD_NUM:
1538 ret = parse_mask(optarg, &dev_rx_offload);
1540 printf("Invalid argument for \'%s\': %s\n",
1541 CMD_LINE_OPT_RX_OFFLOAD, optarg);
1542 print_usage(prgname);
1546 case CMD_LINE_OPT_TX_OFFLOAD_NUM:
1547 ret = parse_mask(optarg, &dev_tx_offload);
1549 printf("Invalid argument for \'%s\': %s\n",
1550 CMD_LINE_OPT_TX_OFFLOAD, optarg);
1551 print_usage(prgname);
1555 case CMD_LINE_OPT_REASSEMBLE_NUM:
1556 ret = parse_decimal(optarg);
1557 if (ret < 0 || ret > UINT32_MAX) {
1558 printf("Invalid argument for \'%s\': %s\n",
1559 CMD_LINE_OPT_REASSEMBLE, optarg);
1560 print_usage(prgname);
1565 case CMD_LINE_OPT_MTU_NUM:
1566 ret = parse_decimal(optarg);
1567 if (ret < 0 || ret > RTE_IPV4_MAX_PKT_LEN) {
1568 printf("Invalid argument for \'%s\': %s\n",
1569 CMD_LINE_OPT_MTU, optarg);
1570 print_usage(prgname);
1575 case CMD_LINE_OPT_FRAG_TTL_NUM:
1576 ret = parse_decimal(optarg);
1577 if (ret < 0 || ret > MAX_FRAG_TTL_NS) {
1578 printf("Invalid argument for \'%s\': %s\n",
1579 CMD_LINE_OPT_MTU, optarg);
1580 print_usage(prgname);
1586 print_usage(prgname);
1591 if (f_present == 0) {
1592 printf("Mandatory option \"-f\" not present\n");
1596 /* check do we need to enable multi-seg support */
1597 if (multi_seg_required()) {
1598 /* legacy mode doesn't support multi-seg */
1599 app_sa_prm.enable = 1;
1600 printf("frame buf size: %u, mtu: %u, "
1601 "number of reassemble entries: %u\n"
1602 "multi-segment support is required\n",
1603 frame_buf_size, mtu_size, frag_tbl_sz);
1606 print_app_sa_prm(&app_sa_prm);
1609 argv[optind-1] = prgname;
1612 optind = 1; /* reset getopt lib */
1617 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
1619 char buf[RTE_ETHER_ADDR_FMT_SIZE];
1620 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
1621 printf("%s%s", name, buf);
1625 * Update destination ethaddr for the port.
1628 add_dst_ethaddr(uint16_t port, const struct rte_ether_addr *addr)
1630 if (port >= RTE_DIM(ethaddr_tbl))
1633 ethaddr_tbl[port].dst = ETHADDR_TO_UINT64(addr);
1637 /* Check the link status of all ports in up to 9s, and print them finally */
1639 check_all_ports_link_status(uint32_t port_mask)
1641 #define CHECK_INTERVAL 100 /* 100ms */
1642 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1644 uint8_t count, all_ports_up, print_flag = 0;
1645 struct rte_eth_link link;
1648 printf("\nChecking link status");
1650 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1652 RTE_ETH_FOREACH_DEV(portid) {
1653 if ((port_mask & (1 << portid)) == 0)
1655 memset(&link, 0, sizeof(link));
1656 ret = rte_eth_link_get_nowait(portid, &link);
1659 if (print_flag == 1)
1660 printf("Port %u link get failed: %s\n",
1661 portid, rte_strerror(-ret));
1664 /* print link status if flag set */
1665 if (print_flag == 1) {
1666 if (link.link_status)
1668 "Port%d Link Up - speed %u Mbps -%s\n",
1669 portid, link.link_speed,
1670 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1671 ("full-duplex") : ("half-duplex\n"));
1673 printf("Port %d Link Down\n", portid);
1676 /* clear all_ports_up flag if any link down */
1677 if (link.link_status == ETH_LINK_DOWN) {
1682 /* after finally printing all link status, get out */
1683 if (print_flag == 1)
1686 if (all_ports_up == 0) {
1689 rte_delay_ms(CHECK_INTERVAL);
1692 /* set the print_flag if all ports up or timeout */
1693 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1701 add_mapping(struct rte_hash *map, const char *str, uint16_t cdev_id,
1702 uint16_t qp, struct lcore_params *params,
1703 struct ipsec_ctx *ipsec_ctx,
1704 const struct rte_cryptodev_capabilities *cipher,
1705 const struct rte_cryptodev_capabilities *auth,
1706 const struct rte_cryptodev_capabilities *aead)
1710 struct cdev_key key = { 0 };
1712 key.lcore_id = params->lcore_id;
1714 key.cipher_algo = cipher->sym.cipher.algo;
1716 key.auth_algo = auth->sym.auth.algo;
1718 key.aead_algo = aead->sym.aead.algo;
1720 ret = rte_hash_lookup(map, &key);
1724 for (i = 0; i < ipsec_ctx->nb_qps; i++)
1725 if (ipsec_ctx->tbl[i].id == cdev_id)
1728 if (i == ipsec_ctx->nb_qps) {
1729 if (ipsec_ctx->nb_qps == MAX_QP_PER_LCORE) {
1730 printf("Maximum number of crypto devices assigned to "
1731 "a core, increase MAX_QP_PER_LCORE value\n");
1734 ipsec_ctx->tbl[i].id = cdev_id;
1735 ipsec_ctx->tbl[i].qp = qp;
1736 ipsec_ctx->nb_qps++;
1737 printf("%s cdev mapping: lcore %u using cdev %u qp %u "
1738 "(cdev_id_qp %lu)\n", str, key.lcore_id,
1742 ret = rte_hash_add_key_data(map, &key, (void *)i);
1744 printf("Faled to insert cdev mapping for (lcore %u, "
1745 "cdev %u, qp %u), errno %d\n",
1746 key.lcore_id, ipsec_ctx->tbl[i].id,
1747 ipsec_ctx->tbl[i].qp, ret);
1755 add_cdev_mapping(struct rte_cryptodev_info *dev_info, uint16_t cdev_id,
1756 uint16_t qp, struct lcore_params *params)
1759 const struct rte_cryptodev_capabilities *i, *j;
1760 struct rte_hash *map;
1761 struct lcore_conf *qconf;
1762 struct ipsec_ctx *ipsec_ctx;
1765 qconf = &lcore_conf[params->lcore_id];
1767 if ((unprotected_port_mask & (1 << params->port_id)) == 0) {
1769 ipsec_ctx = &qconf->outbound;
1773 ipsec_ctx = &qconf->inbound;
1777 /* Required cryptodevs with operation chainning */
1778 if (!(dev_info->feature_flags &
1779 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING))
1782 for (i = dev_info->capabilities;
1783 i->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; i++) {
1784 if (i->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1787 if (i->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1788 ret |= add_mapping(map, str, cdev_id, qp, params,
1789 ipsec_ctx, NULL, NULL, i);
1793 if (i->sym.xform_type != RTE_CRYPTO_SYM_XFORM_CIPHER)
1796 for (j = dev_info->capabilities;
1797 j->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; j++) {
1798 if (j->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1801 if (j->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH)
1804 ret |= add_mapping(map, str, cdev_id, qp, params,
1805 ipsec_ctx, i, j, NULL);
1812 /* Check if the device is enabled by cryptodev_mask */
1814 check_cryptodev_mask(uint8_t cdev_id)
1816 if (enabled_cryptodev_mask & (1 << cdev_id))
1823 cryptodevs_init(void)
1825 struct rte_cryptodev_config dev_conf;
1826 struct rte_cryptodev_qp_conf qp_conf;
1827 uint16_t idx, max_nb_qps, qp, i;
1829 struct rte_hash_parameters params = { 0 };
1831 const uint64_t mseg_flag = multi_seg_required() ?
1832 RTE_CRYPTODEV_FF_IN_PLACE_SGL : 0;
1834 params.entries = CDEV_MAP_ENTRIES;
1835 params.key_len = sizeof(struct cdev_key);
1836 params.hash_func = rte_jhash;
1837 params.hash_func_init_val = 0;
1838 params.socket_id = rte_socket_id();
1840 params.name = "cdev_map_in";
1841 cdev_map_in = rte_hash_create(¶ms);
1842 if (cdev_map_in == NULL)
1843 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1846 params.name = "cdev_map_out";
1847 cdev_map_out = rte_hash_create(¶ms);
1848 if (cdev_map_out == NULL)
1849 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1852 printf("lcore/cryptodev/qp mappings:\n");
1855 for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1856 struct rte_cryptodev_info cdev_info;
1858 if (check_cryptodev_mask((uint8_t)cdev_id))
1861 rte_cryptodev_info_get(cdev_id, &cdev_info);
1863 if ((mseg_flag & cdev_info.feature_flags) != mseg_flag)
1864 rte_exit(EXIT_FAILURE,
1865 "Device %hd does not support \'%s\' feature\n",
1867 rte_cryptodev_get_feature_name(mseg_flag));
1869 if (nb_lcore_params > cdev_info.max_nb_queue_pairs)
1870 max_nb_qps = cdev_info.max_nb_queue_pairs;
1872 max_nb_qps = nb_lcore_params;
1876 while (qp < max_nb_qps && i < nb_lcore_params) {
1877 if (add_cdev_mapping(&cdev_info, cdev_id, qp,
1878 &lcore_params[idx]))
1881 idx = idx % nb_lcore_params;
1888 dev_conf.socket_id = rte_cryptodev_socket_id(cdev_id);
1889 dev_conf.nb_queue_pairs = qp;
1890 dev_conf.ff_disable = RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO;
1892 uint32_t dev_max_sess = cdev_info.sym.max_nb_sessions;
1893 if (dev_max_sess != 0 && dev_max_sess < CDEV_MP_NB_OBJS)
1894 rte_exit(EXIT_FAILURE,
1895 "Device does not support at least %u "
1896 "sessions", CDEV_MP_NB_OBJS);
1898 if (rte_cryptodev_configure(cdev_id, &dev_conf))
1899 rte_panic("Failed to initialize cryptodev %u\n",
1902 qp_conf.nb_descriptors = CDEV_QUEUE_DESC;
1903 qp_conf.mp_session =
1904 socket_ctx[dev_conf.socket_id].session_pool;
1905 qp_conf.mp_session_private =
1906 socket_ctx[dev_conf.socket_id].session_priv_pool;
1907 for (qp = 0; qp < dev_conf.nb_queue_pairs; qp++)
1908 if (rte_cryptodev_queue_pair_setup(cdev_id, qp,
1909 &qp_conf, dev_conf.socket_id))
1910 rte_panic("Failed to setup queue %u for "
1911 "cdev_id %u\n", 0, cdev_id);
1913 if (rte_cryptodev_start(cdev_id))
1914 rte_panic("Failed to start cryptodev %u\n",
1924 port_init(uint16_t portid, uint64_t req_rx_offloads, uint64_t req_tx_offloads)
1926 uint32_t frame_size;
1927 struct rte_eth_dev_info dev_info;
1928 struct rte_eth_txconf *txconf;
1929 uint16_t nb_tx_queue, nb_rx_queue;
1930 uint16_t tx_queueid, rx_queueid, queue, lcore_id;
1931 int32_t ret, socket_id;
1932 struct lcore_conf *qconf;
1933 struct rte_ether_addr ethaddr;
1934 struct rte_eth_conf local_port_conf = port_conf;
1936 ret = rte_eth_dev_info_get(portid, &dev_info);
1938 rte_exit(EXIT_FAILURE,
1939 "Error during getting device (port %u) info: %s\n",
1940 portid, strerror(-ret));
1942 /* limit allowed HW offloafs, as user requested */
1943 dev_info.rx_offload_capa &= dev_rx_offload;
1944 dev_info.tx_offload_capa &= dev_tx_offload;
1946 printf("Configuring device port %u:\n", portid);
1948 ret = rte_eth_macaddr_get(portid, ðaddr);
1950 rte_exit(EXIT_FAILURE,
1951 "Error getting MAC address (port %u): %s\n",
1952 portid, rte_strerror(-ret));
1954 ethaddr_tbl[portid].src = ETHADDR_TO_UINT64(ðaddr);
1955 print_ethaddr("Address: ", ðaddr);
1958 nb_rx_queue = get_port_nb_rx_queues(portid);
1959 nb_tx_queue = nb_lcores;
1961 if (nb_rx_queue > dev_info.max_rx_queues)
1962 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1963 "(max rx queue is %u)\n",
1964 nb_rx_queue, dev_info.max_rx_queues);
1966 if (nb_tx_queue > dev_info.max_tx_queues)
1967 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1968 "(max tx queue is %u)\n",
1969 nb_tx_queue, dev_info.max_tx_queues);
1971 printf("Creating queues: nb_rx_queue=%d nb_tx_queue=%u...\n",
1972 nb_rx_queue, nb_tx_queue);
1974 frame_size = MTU_TO_FRAMELEN(mtu_size);
1975 if (frame_size > local_port_conf.rxmode.max_rx_pkt_len)
1976 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
1977 local_port_conf.rxmode.max_rx_pkt_len = frame_size;
1979 if (multi_seg_required()) {
1980 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_SCATTER;
1981 local_port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
1984 local_port_conf.rxmode.offloads |= req_rx_offloads;
1985 local_port_conf.txmode.offloads |= req_tx_offloads;
1987 /* Check that all required capabilities are supported */
1988 if ((local_port_conf.rxmode.offloads & dev_info.rx_offload_capa) !=
1989 local_port_conf.rxmode.offloads)
1990 rte_exit(EXIT_FAILURE,
1991 "Error: port %u required RX offloads: 0x%" PRIx64
1992 ", avaialbe RX offloads: 0x%" PRIx64 "\n",
1993 portid, local_port_conf.rxmode.offloads,
1994 dev_info.rx_offload_capa);
1996 if ((local_port_conf.txmode.offloads & dev_info.tx_offload_capa) !=
1997 local_port_conf.txmode.offloads)
1998 rte_exit(EXIT_FAILURE,
1999 "Error: port %u required TX offloads: 0x%" PRIx64
2000 ", avaialbe TX offloads: 0x%" PRIx64 "\n",
2001 portid, local_port_conf.txmode.offloads,
2002 dev_info.tx_offload_capa);
2004 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2005 local_port_conf.txmode.offloads |=
2006 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2008 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_IPV4_CKSUM)
2009 local_port_conf.txmode.offloads |= DEV_TX_OFFLOAD_IPV4_CKSUM;
2011 printf("port %u configurng rx_offloads=0x%" PRIx64
2012 ", tx_offloads=0x%" PRIx64 "\n",
2013 portid, local_port_conf.rxmode.offloads,
2014 local_port_conf.txmode.offloads);
2016 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
2017 dev_info.flow_type_rss_offloads;
2018 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
2019 port_conf.rx_adv_conf.rss_conf.rss_hf) {
2020 printf("Port %u modified RSS hash function based on hardware support,"
2021 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
2023 port_conf.rx_adv_conf.rss_conf.rss_hf,
2024 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
2027 ret = rte_eth_dev_configure(portid, nb_rx_queue, nb_tx_queue,
2030 rte_exit(EXIT_FAILURE, "Cannot configure device: "
2031 "err=%d, port=%d\n", ret, portid);
2033 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
2035 rte_exit(EXIT_FAILURE, "Cannot adjust number of descriptors: "
2036 "err=%d, port=%d\n", ret, portid);
2038 /* init one TX queue per lcore */
2040 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2041 if (rte_lcore_is_enabled(lcore_id) == 0)
2045 socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2050 printf("Setup txq=%u,%d,%d\n", lcore_id, tx_queueid, socket_id);
2052 txconf = &dev_info.default_txconf;
2053 txconf->offloads = local_port_conf.txmode.offloads;
2055 ret = rte_eth_tx_queue_setup(portid, tx_queueid, nb_txd,
2058 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
2059 "err=%d, port=%d\n", ret, portid);
2061 qconf = &lcore_conf[lcore_id];
2062 qconf->tx_queue_id[portid] = tx_queueid;
2064 /* Pre-populate pkt offloads based on capabilities */
2065 qconf->outbound.ipv4_offloads = PKT_TX_IPV4;
2066 qconf->outbound.ipv6_offloads = PKT_TX_IPV6;
2067 if (local_port_conf.txmode.offloads & DEV_TX_OFFLOAD_IPV4_CKSUM)
2068 qconf->outbound.ipv4_offloads |= PKT_TX_IP_CKSUM;
2072 /* init RX queues */
2073 for (queue = 0; queue < qconf->nb_rx_queue; ++queue) {
2074 struct rte_eth_rxconf rxq_conf;
2076 if (portid != qconf->rx_queue_list[queue].port_id)
2079 rx_queueid = qconf->rx_queue_list[queue].queue_id;
2081 printf("Setup rxq=%d,%d,%d\n", portid, rx_queueid,
2084 rxq_conf = dev_info.default_rxconf;
2085 rxq_conf.offloads = local_port_conf.rxmode.offloads;
2086 ret = rte_eth_rx_queue_setup(portid, rx_queueid,
2087 nb_rxd, socket_id, &rxq_conf,
2088 socket_ctx[socket_id].mbuf_pool);
2090 rte_exit(EXIT_FAILURE,
2091 "rte_eth_rx_queue_setup: err=%d, "
2092 "port=%d\n", ret, portid);
2099 max_session_size(void)
2103 int16_t cdev_id, port_id, n;
2106 n = rte_cryptodev_count();
2107 for (cdev_id = 0; cdev_id != n; cdev_id++) {
2108 sz = rte_cryptodev_sym_get_private_session_size(cdev_id);
2112 * If crypto device is security capable, need to check the
2113 * size of security session as well.
2116 /* Get security context of the crypto device */
2117 sec_ctx = rte_cryptodev_get_sec_ctx(cdev_id);
2118 if (sec_ctx == NULL)
2121 /* Get size of security session */
2122 sz = rte_security_session_get_size(sec_ctx);
2127 RTE_ETH_FOREACH_DEV(port_id) {
2128 if ((enabled_port_mask & (1 << port_id)) == 0)
2131 sec_ctx = rte_eth_dev_get_sec_ctx(port_id);
2132 if (sec_ctx == NULL)
2135 sz = rte_security_session_get_size(sec_ctx);
2144 session_pool_init(struct socket_ctx *ctx, int32_t socket_id, size_t sess_sz)
2146 char mp_name[RTE_MEMPOOL_NAMESIZE];
2147 struct rte_mempool *sess_mp;
2149 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2150 "sess_mp_%u", socket_id);
2151 sess_mp = rte_cryptodev_sym_session_pool_create(
2152 mp_name, CDEV_MP_NB_OBJS,
2153 sess_sz, CDEV_MP_CACHE_SZ, 0,
2155 ctx->session_pool = sess_mp;
2157 if (ctx->session_pool == NULL)
2158 rte_exit(EXIT_FAILURE,
2159 "Cannot init session pool on socket %d\n", socket_id);
2161 printf("Allocated session pool on socket %d\n", socket_id);
2165 session_priv_pool_init(struct socket_ctx *ctx, int32_t socket_id,
2168 char mp_name[RTE_MEMPOOL_NAMESIZE];
2169 struct rte_mempool *sess_mp;
2171 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2172 "sess_mp_priv_%u", socket_id);
2173 sess_mp = rte_mempool_create(mp_name,
2177 0, NULL, NULL, NULL,
2180 ctx->session_priv_pool = sess_mp;
2182 if (ctx->session_priv_pool == NULL)
2183 rte_exit(EXIT_FAILURE,
2184 "Cannot init session priv pool on socket %d\n",
2187 printf("Allocated session priv pool on socket %d\n",
2192 pool_init(struct socket_ctx *ctx, int32_t socket_id, uint32_t nb_mbuf)
2197 snprintf(s, sizeof(s), "mbuf_pool_%d", socket_id);
2198 ctx->mbuf_pool = rte_pktmbuf_pool_create(s, nb_mbuf,
2199 MEMPOOL_CACHE_SIZE, ipsec_metadata_size(),
2200 frame_buf_size, socket_id);
2203 * if multi-segment support is enabled, then create a pool
2204 * for indirect mbufs.
2206 ms = multi_seg_required();
2208 snprintf(s, sizeof(s), "mbuf_pool_indir_%d", socket_id);
2209 ctx->mbuf_pool_indir = rte_pktmbuf_pool_create(s, nb_mbuf,
2210 MEMPOOL_CACHE_SIZE, 0, 0, socket_id);
2213 if (ctx->mbuf_pool == NULL || (ms != 0 && ctx->mbuf_pool_indir == NULL))
2214 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n",
2217 printf("Allocated mbuf pool on socket %d\n", socket_id);
2221 inline_ipsec_event_esn_overflow(struct rte_security_ctx *ctx, uint64_t md)
2223 struct ipsec_sa *sa;
2225 /* For inline protocol processing, the metadata in the event will
2226 * uniquely identify the security session which raised the event.
2227 * Application would then need the userdata it had registered with the
2228 * security session to process the event.
2231 sa = (struct ipsec_sa *)rte_security_get_userdata(ctx, md);
2234 /* userdata could not be retrieved */
2238 /* Sequence number over flow. SA need to be re-established */
2244 inline_ipsec_event_callback(uint16_t port_id, enum rte_eth_event_type type,
2245 void *param, void *ret_param)
2248 struct rte_eth_event_ipsec_desc *event_desc = NULL;
2249 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
2250 rte_eth_dev_get_sec_ctx(port_id);
2252 RTE_SET_USED(param);
2254 if (type != RTE_ETH_EVENT_IPSEC)
2257 event_desc = ret_param;
2258 if (event_desc == NULL) {
2259 printf("Event descriptor not set\n");
2263 md = event_desc->metadata;
2265 if (event_desc->subtype == RTE_ETH_EVENT_IPSEC_ESN_OVERFLOW)
2266 return inline_ipsec_event_esn_overflow(ctx, md);
2267 else if (event_desc->subtype >= RTE_ETH_EVENT_IPSEC_MAX) {
2268 printf("Invalid IPsec event reported\n");
2276 rx_callback(__rte_unused uint16_t port, __rte_unused uint16_t queue,
2277 struct rte_mbuf *pkt[], uint16_t nb_pkts,
2278 __rte_unused uint16_t max_pkts, void *user_param)
2282 struct lcore_conf *lc;
2283 struct rte_mbuf *mb;
2284 struct rte_ether_hdr *eth;
2290 for (i = 0; i != nb_pkts; i++) {
2293 eth = rte_pktmbuf_mtod(mb, struct rte_ether_hdr *);
2294 if (eth->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
2296 struct rte_ipv4_hdr *iph;
2298 iph = (struct rte_ipv4_hdr *)(eth + 1);
2299 if (rte_ipv4_frag_pkt_is_fragmented(iph)) {
2301 mb->l2_len = sizeof(*eth);
2302 mb->l3_len = sizeof(*iph);
2303 tm = (tm != 0) ? tm : rte_rdtsc();
2304 mb = rte_ipv4_frag_reassemble_packet(
2305 lc->frag.tbl, &lc->frag.dr,
2309 /* fix ip cksum after reassemble. */
2310 iph = rte_pktmbuf_mtod_offset(mb,
2311 struct rte_ipv4_hdr *,
2313 iph->hdr_checksum = 0;
2314 iph->hdr_checksum = rte_ipv4_cksum(iph);
2317 } else if (eth->ether_type ==
2318 rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
2320 struct rte_ipv6_hdr *iph;
2321 struct ipv6_extension_fragment *fh;
2323 iph = (struct rte_ipv6_hdr *)(eth + 1);
2324 fh = rte_ipv6_frag_get_ipv6_fragment_header(iph);
2326 mb->l2_len = sizeof(*eth);
2327 mb->l3_len = (uintptr_t)fh - (uintptr_t)iph +
2329 tm = (tm != 0) ? tm : rte_rdtsc();
2330 mb = rte_ipv6_frag_reassemble_packet(
2331 lc->frag.tbl, &lc->frag.dr,
2334 /* fix l3_len after reassemble. */
2335 mb->l3_len = mb->l3_len - sizeof(*fh);
2343 /* some fragments were encountered, drain death row */
2345 rte_ip_frag_free_death_row(&lc->frag.dr, 0);
2352 reassemble_lcore_init(struct lcore_conf *lc, uint32_t cid)
2356 uint64_t frag_cycles;
2357 const struct lcore_rx_queue *rxq;
2358 const struct rte_eth_rxtx_callback *cb;
2360 /* create fragment table */
2361 sid = rte_lcore_to_socket_id(cid);
2362 frag_cycles = (rte_get_tsc_hz() + NS_PER_S - 1) /
2363 NS_PER_S * frag_ttl_ns;
2365 lc->frag.tbl = rte_ip_frag_table_create(frag_tbl_sz,
2366 FRAG_TBL_BUCKET_ENTRIES, frag_tbl_sz, frag_cycles, sid);
2367 if (lc->frag.tbl == NULL) {
2368 printf("%s(%u): failed to create fragment table of size: %u, "
2370 __func__, cid, frag_tbl_sz, rte_errno);
2374 /* setup reassemble RX callbacks for all queues */
2375 for (i = 0; i != lc->nb_rx_queue; i++) {
2377 rxq = lc->rx_queue_list + i;
2378 cb = rte_eth_add_rx_callback(rxq->port_id, rxq->queue_id,
2381 printf("%s(%u): failed to install RX callback for "
2382 "portid=%u, queueid=%u, error code: %d\n",
2384 rxq->port_id, rxq->queue_id, rte_errno);
2393 reassemble_init(void)
2399 for (i = 0; i != nb_lcore_params; i++) {
2400 lc = lcore_params[i].lcore_id;
2401 rc = reassemble_lcore_init(lcore_conf + lc, lc);
2410 main(int32_t argc, char **argv)
2417 uint64_t req_rx_offloads, req_tx_offloads;
2421 ret = rte_eal_init(argc, argv);
2423 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
2427 /* parse application arguments (after the EAL ones) */
2428 ret = parse_args(argc, argv);
2430 rte_exit(EXIT_FAILURE, "Invalid parameters\n");
2432 /* parse configuration file */
2433 if (parse_cfg_file(cfgfile) < 0) {
2434 printf("parsing file \"%s\" failed\n",
2436 print_usage(argv[0]);
2440 if ((unprotected_port_mask & enabled_port_mask) !=
2441 unprotected_port_mask)
2442 rte_exit(EXIT_FAILURE, "Invalid unprotected portmask 0x%x\n",
2443 unprotected_port_mask);
2445 if (check_params() < 0)
2446 rte_exit(EXIT_FAILURE, "check_params failed\n");
2448 ret = init_lcore_rx_queues();
2450 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2452 nb_lcores = rte_lcore_count();
2454 sess_sz = max_session_size();
2456 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2457 if (rte_lcore_is_enabled(lcore_id) == 0)
2461 socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2465 /* mbuf_pool is initialised by the pool_init() function*/
2466 if (socket_ctx[socket_id].mbuf_pool)
2469 pool_init(&socket_ctx[socket_id], socket_id, NB_MBUF);
2470 session_pool_init(&socket_ctx[socket_id], socket_id, sess_sz);
2471 session_priv_pool_init(&socket_ctx[socket_id], socket_id,
2475 RTE_ETH_FOREACH_DEV(portid) {
2476 if ((enabled_port_mask & (1 << portid)) == 0)
2479 sa_check_offloads(portid, &req_rx_offloads, &req_tx_offloads);
2480 port_init(portid, req_rx_offloads, req_tx_offloads);
2486 RTE_ETH_FOREACH_DEV(portid) {
2487 if ((enabled_port_mask & (1 << portid)) == 0)
2492 * note: device must be started before a flow rule
2495 ret = rte_eth_dev_start(portid);
2497 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: "
2498 "err=%d, port=%d\n", ret, portid);
2500 * If enabled, put device in promiscuous mode.
2501 * This allows IO forwarding mode to forward packets
2502 * to itself through 2 cross-connected ports of the
2505 if (promiscuous_on) {
2506 ret = rte_eth_promiscuous_enable(portid);
2508 rte_exit(EXIT_FAILURE,
2509 "rte_eth_promiscuous_enable: err=%s, port=%d\n",
2510 rte_strerror(-ret), portid);
2513 rte_eth_dev_callback_register(portid,
2514 RTE_ETH_EVENT_IPSEC, inline_ipsec_event_callback, NULL);
2517 /* fragment reassemble is enabled */
2518 if (frag_tbl_sz != 0) {
2519 ret = reassemble_init();
2521 rte_exit(EXIT_FAILURE, "failed at reassemble init");
2524 /* Replicate each context per socket */
2525 for (i = 0; i < NB_SOCKETS && i < rte_socket_count(); i++) {
2526 socket_id = rte_socket_id_by_idx(i);
2527 if ((socket_ctx[socket_id].mbuf_pool != NULL) &&
2528 (socket_ctx[socket_id].sa_in == NULL) &&
2529 (socket_ctx[socket_id].sa_out == NULL)) {
2530 sa_init(&socket_ctx[socket_id], socket_id);
2531 sp4_init(&socket_ctx[socket_id], socket_id);
2532 sp6_init(&socket_ctx[socket_id], socket_id);
2533 rt_init(&socket_ctx[socket_id], socket_id);
2537 check_all_ports_link_status(enabled_port_mask);
2539 /* launch per-lcore init on every lcore */
2540 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2541 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2542 if (rte_eal_wait_lcore(lcore_id) < 0)