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 FRAG_TTL_MS (10 * MS_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"
140 /* long options mapped to a short option */
142 /* first long only option value must be >= 256, so that we won't
143 * conflict with short options
145 CMD_LINE_OPT_MIN_NUM = 256,
146 CMD_LINE_OPT_CONFIG_NUM,
147 CMD_LINE_OPT_SINGLE_SA_NUM,
148 CMD_LINE_OPT_CRYPTODEV_MASK_NUM,
149 CMD_LINE_OPT_RX_OFFLOAD_NUM,
150 CMD_LINE_OPT_TX_OFFLOAD_NUM,
151 CMD_LINE_OPT_REASSEMBLE_NUM,
152 CMD_LINE_OPT_MTU_NUM,
155 static const struct option lgopts[] = {
156 {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
157 {CMD_LINE_OPT_SINGLE_SA, 1, 0, CMD_LINE_OPT_SINGLE_SA_NUM},
158 {CMD_LINE_OPT_CRYPTODEV_MASK, 1, 0, CMD_LINE_OPT_CRYPTODEV_MASK_NUM},
159 {CMD_LINE_OPT_RX_OFFLOAD, 1, 0, CMD_LINE_OPT_RX_OFFLOAD_NUM},
160 {CMD_LINE_OPT_TX_OFFLOAD, 1, 0, CMD_LINE_OPT_TX_OFFLOAD_NUM},
161 {CMD_LINE_OPT_REASSEMBLE, 1, 0, CMD_LINE_OPT_REASSEMBLE_NUM},
165 /* mask of enabled ports */
166 static uint32_t enabled_port_mask;
167 static uint64_t enabled_cryptodev_mask = UINT64_MAX;
168 static uint32_t unprotected_port_mask;
169 static int32_t promiscuous_on = 1;
170 static int32_t numa_on = 1; /**< NUMA is enabled by default. */
171 static uint32_t nb_lcores;
172 static uint32_t single_sa;
173 static uint32_t single_sa_idx;
176 * RX/TX HW offload capabilities to enable/use on ethernet ports.
177 * By default all capabilities are enabled.
179 static uint64_t dev_rx_offload = UINT64_MAX;
180 static uint64_t dev_tx_offload = UINT64_MAX;
183 * global values that determine multi-seg policy
185 static uint32_t frag_tbl_sz;
186 static uint32_t frame_buf_size = RTE_MBUF_DEFAULT_BUF_SIZE;
187 static uint32_t mtu_size = RTE_ETHER_MTU;
189 /* application wide librte_ipsec/SA parameters */
190 struct app_sa_prm app_sa_prm = {.enable = 0};
192 struct lcore_rx_queue {
195 } __rte_cache_aligned;
197 struct lcore_params {
201 } __rte_cache_aligned;
203 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
205 static struct lcore_params *lcore_params;
206 static uint16_t nb_lcore_params;
208 static struct rte_hash *cdev_map_in;
209 static struct rte_hash *cdev_map_out;
213 struct rte_mbuf *m_table[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
217 uint16_t nb_rx_queue;
218 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
219 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
220 struct buffer tx_mbufs[RTE_MAX_ETHPORTS];
221 struct ipsec_ctx inbound;
222 struct ipsec_ctx outbound;
223 struct rt_ctx *rt4_ctx;
224 struct rt_ctx *rt6_ctx;
226 struct rte_ip_frag_tbl *tbl;
227 struct rte_mempool *pool_dir;
228 struct rte_mempool *pool_indir;
229 struct rte_ip_frag_death_row dr;
231 } __rte_cache_aligned;
233 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
235 static struct rte_eth_conf port_conf = {
237 .mq_mode = ETH_MQ_RX_RSS,
238 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
240 .offloads = DEV_RX_OFFLOAD_CHECKSUM,
245 .rss_hf = ETH_RSS_IP | ETH_RSS_UDP |
246 ETH_RSS_TCP | ETH_RSS_SCTP,
250 .mq_mode = ETH_MQ_TX_NONE,
254 static struct socket_ctx socket_ctx[NB_SOCKETS];
257 * Determine is multi-segment support required:
258 * - either frame buffer size is smaller then mtu
259 * - or reassmeble support is requested
262 multi_seg_required(void)
264 return (MTU_TO_FRAMELEN(mtu_size) + RTE_PKTMBUF_HEADROOM >
265 frame_buf_size || frag_tbl_sz != 0);
269 adjust_ipv4_pktlen(struct rte_mbuf *m, const struct rte_ipv4_hdr *iph,
274 plen = rte_be_to_cpu_16(iph->total_length) + l2_len;
275 if (plen < m->pkt_len) {
276 trim = m->pkt_len - plen;
277 rte_pktmbuf_trim(m, trim);
282 adjust_ipv6_pktlen(struct rte_mbuf *m, const struct rte_ipv6_hdr *iph,
287 plen = rte_be_to_cpu_16(iph->payload_len) + sizeof(*iph) + l2_len;
288 if (plen < m->pkt_len) {
289 trim = m->pkt_len - plen;
290 rte_pktmbuf_trim(m, trim);
295 prepare_one_packet(struct rte_mbuf *pkt, struct ipsec_traffic *t)
297 const struct rte_ether_hdr *eth;
298 const struct rte_ipv4_hdr *iph4;
299 const struct rte_ipv6_hdr *iph6;
301 eth = rte_pktmbuf_mtod(pkt, const struct rte_ether_hdr *);
302 if (eth->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
304 iph4 = (const struct rte_ipv4_hdr *)rte_pktmbuf_adj(pkt,
306 adjust_ipv4_pktlen(pkt, iph4, 0);
308 if (iph4->next_proto_id == IPPROTO_ESP)
309 t->ipsec.pkts[(t->ipsec.num)++] = pkt;
311 t->ip4.data[t->ip4.num] = &iph4->next_proto_id;
312 t->ip4.pkts[(t->ip4.num)++] = pkt;
315 pkt->l3_len = sizeof(*iph4);
316 } else if (eth->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
318 size_t l3len, ext_len;
321 /* get protocol type */
322 iph6 = (const struct rte_ipv6_hdr *)rte_pktmbuf_adj(pkt,
324 adjust_ipv6_pktlen(pkt, iph6, 0);
326 next_proto = iph6->proto;
328 /* determine l3 header size up to ESP extension */
329 l3len = sizeof(struct ip6_hdr);
330 p = rte_pktmbuf_mtod(pkt, uint8_t *);
331 while (next_proto != IPPROTO_ESP && l3len < pkt->data_len &&
332 (next_proto = rte_ipv6_get_next_ext(p + l3len,
333 next_proto, &ext_len)) >= 0)
336 /* drop packet when IPv6 header exceeds first segment length */
337 if (unlikely(l3len > pkt->data_len)) {
338 rte_pktmbuf_free(pkt);
342 if (next_proto == IPPROTO_ESP)
343 t->ipsec.pkts[(t->ipsec.num)++] = pkt;
345 t->ip6.data[t->ip6.num] = &iph6->proto;
346 t->ip6.pkts[(t->ip6.num)++] = pkt;
351 /* Unknown/Unsupported type, drop the packet */
352 RTE_LOG(ERR, IPSEC, "Unsupported packet type 0x%x\n",
353 rte_be_to_cpu_16(eth->ether_type));
354 rte_pktmbuf_free(pkt);
357 /* Check if the packet has been processed inline. For inline protocol
358 * processed packets, the metadata in the mbuf can be used to identify
359 * the security processing done on the packet. The metadata will be
360 * used to retrieve the application registered userdata associated
361 * with the security session.
364 if (pkt->ol_flags & PKT_RX_SEC_OFFLOAD) {
366 struct ipsec_mbuf_metadata *priv;
367 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
368 rte_eth_dev_get_sec_ctx(
371 /* Retrieve the userdata registered. Here, the userdata
372 * registered is the SA pointer.
375 sa = (struct ipsec_sa *)
376 rte_security_get_userdata(ctx, pkt->udata64);
379 /* userdata could not be retrieved */
383 /* Save SA as priv member in mbuf. This will be used in the
384 * IPsec selector(SP-SA) check.
387 priv = get_priv(pkt);
393 prepare_traffic(struct rte_mbuf **pkts, struct ipsec_traffic *t,
402 for (i = 0; i < (nb_pkts - PREFETCH_OFFSET); i++) {
403 rte_prefetch0(rte_pktmbuf_mtod(pkts[i + PREFETCH_OFFSET],
405 prepare_one_packet(pkts[i], t);
407 /* Process left packets */
408 for (; i < nb_pkts; i++)
409 prepare_one_packet(pkts[i], t);
413 prepare_tx_pkt(struct rte_mbuf *pkt, uint16_t port,
414 const struct lcore_conf *qconf)
417 struct rte_ether_hdr *ethhdr;
419 ip = rte_pktmbuf_mtod(pkt, struct ip *);
421 ethhdr = (struct rte_ether_hdr *)
422 rte_pktmbuf_prepend(pkt, RTE_ETHER_HDR_LEN);
424 if (ip->ip_v == IPVERSION) {
425 pkt->ol_flags |= qconf->outbound.ipv4_offloads;
426 pkt->l3_len = sizeof(struct ip);
427 pkt->l2_len = RTE_ETHER_HDR_LEN;
431 /* calculate IPv4 cksum in SW */
432 if ((pkt->ol_flags & PKT_TX_IP_CKSUM) == 0)
433 ip->ip_sum = rte_ipv4_cksum((struct rte_ipv4_hdr *)ip);
435 ethhdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
437 pkt->ol_flags |= qconf->outbound.ipv6_offloads;
438 pkt->l3_len = sizeof(struct ip6_hdr);
439 pkt->l2_len = RTE_ETHER_HDR_LEN;
441 ethhdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
444 memcpy(ðhdr->s_addr, ðaddr_tbl[port].src,
445 sizeof(struct rte_ether_addr));
446 memcpy(ðhdr->d_addr, ðaddr_tbl[port].dst,
447 sizeof(struct rte_ether_addr));
451 prepare_tx_burst(struct rte_mbuf *pkts[], uint16_t nb_pkts, uint16_t port,
452 const struct lcore_conf *qconf)
455 const int32_t prefetch_offset = 2;
457 for (i = 0; i < (nb_pkts - prefetch_offset); i++) {
458 rte_mbuf_prefetch_part2(pkts[i + prefetch_offset]);
459 prepare_tx_pkt(pkts[i], port, qconf);
461 /* Process left packets */
462 for (; i < nb_pkts; i++)
463 prepare_tx_pkt(pkts[i], port, qconf);
466 /* Send burst of packets on an output interface */
467 static inline int32_t
468 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
470 struct rte_mbuf **m_table;
474 queueid = qconf->tx_queue_id[port];
475 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
477 prepare_tx_burst(m_table, n, port, qconf);
479 ret = rte_eth_tx_burst(port, queueid, m_table, n);
480 if (unlikely(ret < n)) {
482 rte_pktmbuf_free(m_table[ret]);
490 * Helper function to fragment and queue for TX one packet.
492 static inline uint32_t
493 send_fragment_packet(struct lcore_conf *qconf, struct rte_mbuf *m,
494 uint16_t port, uint8_t proto)
500 tbl = qconf->tx_mbufs + port;
503 /* free space for new fragments */
504 if (len + RTE_LIBRTE_IP_FRAG_MAX_FRAG >= RTE_DIM(tbl->m_table)) {
505 send_burst(qconf, len, port);
509 n = RTE_DIM(tbl->m_table) - len;
511 if (proto == IPPROTO_IP)
512 rc = rte_ipv4_fragment_packet(m, tbl->m_table + len,
513 n, mtu_size, qconf->frag.pool_dir,
514 qconf->frag.pool_indir);
516 rc = rte_ipv6_fragment_packet(m, tbl->m_table + len,
517 n, mtu_size, qconf->frag.pool_dir,
518 qconf->frag.pool_indir);
524 "%s: failed to fragment packet with size %u, "
526 __func__, m->pkt_len, rte_errno);
532 /* Enqueue a single packet, and send burst if queue is filled */
533 static inline int32_t
534 send_single_packet(struct rte_mbuf *m, uint16_t port, uint8_t proto)
538 struct lcore_conf *qconf;
540 lcore_id = rte_lcore_id();
542 qconf = &lcore_conf[lcore_id];
543 len = qconf->tx_mbufs[port].len;
545 if (m->pkt_len <= mtu_size) {
546 qconf->tx_mbufs[port].m_table[len] = m;
549 /* need to fragment the packet */
551 len = send_fragment_packet(qconf, m, port, proto);
553 /* enough pkts to be sent */
554 if (unlikely(len == MAX_PKT_BURST)) {
555 send_burst(qconf, MAX_PKT_BURST, port);
559 qconf->tx_mbufs[port].len = len;
564 inbound_sp_sa(struct sp_ctx *sp, struct sa_ctx *sa, struct traffic_type *ip,
568 uint32_t i, j, res, sa_idx;
570 if (ip->num == 0 || sp == NULL)
573 rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
574 ip->num, DEFAULT_MAX_CATEGORIES);
577 for (i = 0; i < ip->num; i++) {
584 if (res == DISCARD) {
589 /* Only check SPI match for processed IPSec packets */
590 if (i < lim && ((m->ol_flags & PKT_RX_SEC_OFFLOAD) == 0)) {
595 sa_idx = SPI2IDX(res);
596 if (!inbound_sa_check(sa, m, sa_idx)) {
606 split46_traffic(struct ipsec_traffic *trf, struct rte_mbuf *mb[], uint32_t num)
615 for (i = 0; i < num; i++) {
618 ip = rte_pktmbuf_mtod(m, struct ip *);
620 if (ip->ip_v == IPVERSION) {
621 trf->ip4.pkts[n4] = m;
622 trf->ip4.data[n4] = rte_pktmbuf_mtod_offset(m,
623 uint8_t *, offsetof(struct ip, ip_p));
625 } else if (ip->ip_v == IP6_VERSION) {
626 trf->ip6.pkts[n6] = m;
627 trf->ip6.data[n6] = rte_pktmbuf_mtod_offset(m,
629 offsetof(struct ip6_hdr, ip6_nxt));
641 process_pkts_inbound(struct ipsec_ctx *ipsec_ctx,
642 struct ipsec_traffic *traffic)
644 uint16_t nb_pkts_in, n_ip4, n_ip6;
646 n_ip4 = traffic->ip4.num;
647 n_ip6 = traffic->ip6.num;
649 if (app_sa_prm.enable == 0) {
650 nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
651 traffic->ipsec.num, MAX_PKT_BURST);
652 split46_traffic(traffic, traffic->ipsec.pkts, nb_pkts_in);
654 inbound_sa_lookup(ipsec_ctx->sa_ctx, traffic->ipsec.pkts,
655 traffic->ipsec.saptr, traffic->ipsec.num);
656 ipsec_process(ipsec_ctx, traffic);
659 inbound_sp_sa(ipsec_ctx->sp4_ctx, ipsec_ctx->sa_ctx, &traffic->ip4,
662 inbound_sp_sa(ipsec_ctx->sp6_ctx, ipsec_ctx->sa_ctx, &traffic->ip6,
667 outbound_sp(struct sp_ctx *sp, struct traffic_type *ip,
668 struct traffic_type *ipsec)
671 uint32_t i, j, sa_idx;
673 if (ip->num == 0 || sp == NULL)
676 rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
677 ip->num, DEFAULT_MAX_CATEGORIES);
680 for (i = 0; i < ip->num; i++) {
682 sa_idx = SPI2IDX(ip->res[i]);
683 if (ip->res[i] == DISCARD)
685 else if (ip->res[i] == BYPASS)
688 ipsec->res[ipsec->num] = sa_idx;
689 ipsec->pkts[ipsec->num++] = m;
696 process_pkts_outbound(struct ipsec_ctx *ipsec_ctx,
697 struct ipsec_traffic *traffic)
700 uint16_t idx, nb_pkts_out, i;
702 /* Drop any IPsec traffic from protected ports */
703 for (i = 0; i < traffic->ipsec.num; i++)
704 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
706 traffic->ipsec.num = 0;
708 outbound_sp(ipsec_ctx->sp4_ctx, &traffic->ip4, &traffic->ipsec);
710 outbound_sp(ipsec_ctx->sp6_ctx, &traffic->ip6, &traffic->ipsec);
712 if (app_sa_prm.enable == 0) {
714 nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ipsec.pkts,
715 traffic->ipsec.res, traffic->ipsec.num,
718 for (i = 0; i < nb_pkts_out; i++) {
719 m = traffic->ipsec.pkts[i];
720 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
721 if (ip->ip_v == IPVERSION) {
722 idx = traffic->ip4.num++;
723 traffic->ip4.pkts[idx] = m;
725 idx = traffic->ip6.num++;
726 traffic->ip6.pkts[idx] = m;
730 outbound_sa_lookup(ipsec_ctx->sa_ctx, traffic->ipsec.res,
731 traffic->ipsec.saptr, traffic->ipsec.num);
732 ipsec_process(ipsec_ctx, traffic);
737 process_pkts_inbound_nosp(struct ipsec_ctx *ipsec_ctx,
738 struct ipsec_traffic *traffic)
741 uint32_t nb_pkts_in, i, idx;
743 /* Drop any IPv4 traffic from unprotected ports */
744 for (i = 0; i < traffic->ip4.num; i++)
745 rte_pktmbuf_free(traffic->ip4.pkts[i]);
747 traffic->ip4.num = 0;
749 /* Drop any IPv6 traffic from unprotected ports */
750 for (i = 0; i < traffic->ip6.num; i++)
751 rte_pktmbuf_free(traffic->ip6.pkts[i]);
753 traffic->ip6.num = 0;
755 if (app_sa_prm.enable == 0) {
757 nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
758 traffic->ipsec.num, MAX_PKT_BURST);
760 for (i = 0; i < nb_pkts_in; i++) {
761 m = traffic->ipsec.pkts[i];
762 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
763 if (ip->ip_v == IPVERSION) {
764 idx = traffic->ip4.num++;
765 traffic->ip4.pkts[idx] = m;
767 idx = traffic->ip6.num++;
768 traffic->ip6.pkts[idx] = m;
772 inbound_sa_lookup(ipsec_ctx->sa_ctx, traffic->ipsec.pkts,
773 traffic->ipsec.saptr, traffic->ipsec.num);
774 ipsec_process(ipsec_ctx, traffic);
779 process_pkts_outbound_nosp(struct ipsec_ctx *ipsec_ctx,
780 struct ipsec_traffic *traffic)
783 uint32_t nb_pkts_out, i, n;
786 /* Drop any IPsec traffic from protected ports */
787 for (i = 0; i < traffic->ipsec.num; i++)
788 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
792 for (i = 0; i < traffic->ip4.num; i++) {
793 traffic->ipsec.pkts[n] = traffic->ip4.pkts[i];
794 traffic->ipsec.res[n++] = single_sa_idx;
797 for (i = 0; i < traffic->ip6.num; i++) {
798 traffic->ipsec.pkts[n] = traffic->ip6.pkts[i];
799 traffic->ipsec.res[n++] = single_sa_idx;
802 traffic->ip4.num = 0;
803 traffic->ip6.num = 0;
804 traffic->ipsec.num = n;
806 if (app_sa_prm.enable == 0) {
808 nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ipsec.pkts,
809 traffic->ipsec.res, traffic->ipsec.num,
812 /* They all sue the same SA (ip4 or ip6 tunnel) */
813 m = traffic->ipsec.pkts[0];
814 ip = rte_pktmbuf_mtod(m, struct ip *);
815 if (ip->ip_v == IPVERSION) {
816 traffic->ip4.num = nb_pkts_out;
817 for (i = 0; i < nb_pkts_out; i++)
818 traffic->ip4.pkts[i] = traffic->ipsec.pkts[i];
820 traffic->ip6.num = nb_pkts_out;
821 for (i = 0; i < nb_pkts_out; i++)
822 traffic->ip6.pkts[i] = traffic->ipsec.pkts[i];
825 outbound_sa_lookup(ipsec_ctx->sa_ctx, traffic->ipsec.res,
826 traffic->ipsec.saptr, traffic->ipsec.num);
827 ipsec_process(ipsec_ctx, traffic);
831 static inline int32_t
832 get_hop_for_offload_pkt(struct rte_mbuf *pkt, int is_ipv6)
834 struct ipsec_mbuf_metadata *priv;
837 priv = get_priv(pkt);
840 if (unlikely(sa == NULL)) {
841 RTE_LOG(ERR, IPSEC, "SA not saved in private data\n");
849 return (sa->portid | RTE_LPM_LOOKUP_SUCCESS);
860 route4_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
862 uint32_t hop[MAX_PKT_BURST * 2];
863 uint32_t dst_ip[MAX_PKT_BURST * 2];
866 uint16_t lpm_pkts = 0;
871 /* Need to do an LPM lookup for non-inline packets. Inline packets will
872 * have port ID in the SA
875 for (i = 0; i < nb_pkts; i++) {
876 if (!(pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD)) {
877 /* Security offload not enabled. So an LPM lookup is
878 * required to get the hop
880 offset = offsetof(struct ip, ip_dst);
881 dst_ip[lpm_pkts] = *rte_pktmbuf_mtod_offset(pkts[i],
883 dst_ip[lpm_pkts] = rte_be_to_cpu_32(dst_ip[lpm_pkts]);
888 rte_lpm_lookup_bulk((struct rte_lpm *)rt_ctx, dst_ip, hop, lpm_pkts);
892 for (i = 0; i < nb_pkts; i++) {
893 if (pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD) {
894 /* Read hop from the SA */
895 pkt_hop = get_hop_for_offload_pkt(pkts[i], 0);
897 /* Need to use hop returned by lookup */
898 pkt_hop = hop[lpm_pkts++];
901 if ((pkt_hop & RTE_LPM_LOOKUP_SUCCESS) == 0) {
902 rte_pktmbuf_free(pkts[i]);
905 send_single_packet(pkts[i], pkt_hop & 0xff, IPPROTO_IP);
910 route6_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
912 int32_t hop[MAX_PKT_BURST * 2];
913 uint8_t dst_ip[MAX_PKT_BURST * 2][16];
917 uint16_t lpm_pkts = 0;
922 /* Need to do an LPM lookup for non-inline packets. Inline packets will
923 * have port ID in the SA
926 for (i = 0; i < nb_pkts; i++) {
927 if (!(pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD)) {
928 /* Security offload not enabled. So an LPM lookup is
929 * required to get the hop
931 offset = offsetof(struct ip6_hdr, ip6_dst);
932 ip6_dst = rte_pktmbuf_mtod_offset(pkts[i], uint8_t *,
934 memcpy(&dst_ip[lpm_pkts][0], ip6_dst, 16);
939 rte_lpm6_lookup_bulk_func((struct rte_lpm6 *)rt_ctx, dst_ip, hop,
944 for (i = 0; i < nb_pkts; i++) {
945 if (pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD) {
946 /* Read hop from the SA */
947 pkt_hop = get_hop_for_offload_pkt(pkts[i], 1);
949 /* Need to use hop returned by lookup */
950 pkt_hop = hop[lpm_pkts++];
954 rte_pktmbuf_free(pkts[i]);
957 send_single_packet(pkts[i], pkt_hop & 0xff, IPPROTO_IPV6);
962 process_pkts(struct lcore_conf *qconf, struct rte_mbuf **pkts,
963 uint8_t nb_pkts, uint16_t portid)
965 struct ipsec_traffic traffic;
967 prepare_traffic(pkts, &traffic, nb_pkts);
969 if (unlikely(single_sa)) {
970 if (UNPROTECTED_PORT(portid))
971 process_pkts_inbound_nosp(&qconf->inbound, &traffic);
973 process_pkts_outbound_nosp(&qconf->outbound, &traffic);
975 if (UNPROTECTED_PORT(portid))
976 process_pkts_inbound(&qconf->inbound, &traffic);
978 process_pkts_outbound(&qconf->outbound, &traffic);
981 route4_pkts(qconf->rt4_ctx, traffic.ip4.pkts, traffic.ip4.num);
982 route6_pkts(qconf->rt6_ctx, traffic.ip6.pkts, traffic.ip6.num);
986 drain_tx_buffers(struct lcore_conf *qconf)
991 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
992 buf = &qconf->tx_mbufs[portid];
995 send_burst(qconf, buf->len, portid);
1001 drain_crypto_buffers(struct lcore_conf *qconf)
1004 struct ipsec_ctx *ctx;
1006 /* drain inbound buffers*/
1007 ctx = &qconf->inbound;
1008 for (i = 0; i != ctx->nb_qps; i++) {
1009 if (ctx->tbl[i].len != 0)
1010 enqueue_cop_burst(ctx->tbl + i);
1013 /* drain outbound buffers*/
1014 ctx = &qconf->outbound;
1015 for (i = 0; i != ctx->nb_qps; i++) {
1016 if (ctx->tbl[i].len != 0)
1017 enqueue_cop_burst(ctx->tbl + i);
1022 drain_inbound_crypto_queues(const struct lcore_conf *qconf,
1023 struct ipsec_ctx *ctx)
1026 struct ipsec_traffic trf;
1028 if (app_sa_prm.enable == 0) {
1030 /* dequeue packets from crypto-queue */
1031 n = ipsec_inbound_cqp_dequeue(ctx, trf.ipsec.pkts,
1032 RTE_DIM(trf.ipsec.pkts));
1037 /* split traffic by ipv4-ipv6 */
1038 split46_traffic(&trf, trf.ipsec.pkts, n);
1040 ipsec_cqp_process(ctx, &trf);
1042 /* process ipv4 packets */
1043 if (trf.ip4.num != 0) {
1044 inbound_sp_sa(ctx->sp4_ctx, ctx->sa_ctx, &trf.ip4, 0);
1045 route4_pkts(qconf->rt4_ctx, trf.ip4.pkts, trf.ip4.num);
1048 /* process ipv6 packets */
1049 if (trf.ip6.num != 0) {
1050 inbound_sp_sa(ctx->sp6_ctx, ctx->sa_ctx, &trf.ip6, 0);
1051 route6_pkts(qconf->rt6_ctx, trf.ip6.pkts, trf.ip6.num);
1056 drain_outbound_crypto_queues(const struct lcore_conf *qconf,
1057 struct ipsec_ctx *ctx)
1060 struct ipsec_traffic trf;
1062 if (app_sa_prm.enable == 0) {
1064 /* dequeue packets from crypto-queue */
1065 n = ipsec_outbound_cqp_dequeue(ctx, trf.ipsec.pkts,
1066 RTE_DIM(trf.ipsec.pkts));
1071 /* split traffic by ipv4-ipv6 */
1072 split46_traffic(&trf, trf.ipsec.pkts, n);
1074 ipsec_cqp_process(ctx, &trf);
1076 /* process ipv4 packets */
1077 if (trf.ip4.num != 0)
1078 route4_pkts(qconf->rt4_ctx, trf.ip4.pkts, trf.ip4.num);
1080 /* process ipv6 packets */
1081 if (trf.ip6.num != 0)
1082 route6_pkts(qconf->rt6_ctx, trf.ip6.pkts, trf.ip6.num);
1085 /* main processing loop */
1087 main_loop(__attribute__((unused)) void *dummy)
1089 struct rte_mbuf *pkts[MAX_PKT_BURST];
1091 uint64_t prev_tsc, diff_tsc, cur_tsc;
1095 struct lcore_conf *qconf;
1097 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
1098 / US_PER_S * BURST_TX_DRAIN_US;
1099 struct lcore_rx_queue *rxql;
1102 lcore_id = rte_lcore_id();
1103 qconf = &lcore_conf[lcore_id];
1104 rxql = qconf->rx_queue_list;
1105 socket_id = rte_lcore_to_socket_id(lcore_id);
1107 qconf->rt4_ctx = socket_ctx[socket_id].rt_ip4;
1108 qconf->rt6_ctx = socket_ctx[socket_id].rt_ip6;
1109 qconf->inbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_in;
1110 qconf->inbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_in;
1111 qconf->inbound.sa_ctx = socket_ctx[socket_id].sa_in;
1112 qconf->inbound.cdev_map = cdev_map_in;
1113 qconf->inbound.session_pool = socket_ctx[socket_id].session_pool;
1114 qconf->inbound.session_priv_pool =
1115 socket_ctx[socket_id].session_priv_pool;
1116 qconf->outbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_out;
1117 qconf->outbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_out;
1118 qconf->outbound.sa_ctx = socket_ctx[socket_id].sa_out;
1119 qconf->outbound.cdev_map = cdev_map_out;
1120 qconf->outbound.session_pool = socket_ctx[socket_id].session_pool;
1121 qconf->outbound.session_priv_pool =
1122 socket_ctx[socket_id].session_priv_pool;
1123 qconf->frag.pool_dir = socket_ctx[socket_id].mbuf_pool;
1124 qconf->frag.pool_indir = socket_ctx[socket_id].mbuf_pool_indir;
1126 if (qconf->nb_rx_queue == 0) {
1127 RTE_LOG(DEBUG, IPSEC, "lcore %u has nothing to do\n",
1132 RTE_LOG(INFO, IPSEC, "entering main loop on lcore %u\n", lcore_id);
1134 for (i = 0; i < qconf->nb_rx_queue; i++) {
1135 portid = rxql[i].port_id;
1136 queueid = rxql[i].queue_id;
1137 RTE_LOG(INFO, IPSEC,
1138 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
1139 lcore_id, portid, queueid);
1143 cur_tsc = rte_rdtsc();
1145 /* TX queue buffer drain */
1146 diff_tsc = cur_tsc - prev_tsc;
1148 if (unlikely(diff_tsc > drain_tsc)) {
1149 drain_tx_buffers(qconf);
1150 drain_crypto_buffers(qconf);
1154 for (i = 0; i < qconf->nb_rx_queue; ++i) {
1156 /* Read packets from RX queues */
1157 portid = rxql[i].port_id;
1158 queueid = rxql[i].queue_id;
1159 nb_rx = rte_eth_rx_burst(portid, queueid,
1160 pkts, MAX_PKT_BURST);
1163 process_pkts(qconf, pkts, nb_rx, portid);
1165 /* dequeue and process completed crypto-ops */
1166 if (UNPROTECTED_PORT(portid))
1167 drain_inbound_crypto_queues(qconf,
1170 drain_outbound_crypto_queues(qconf,
1184 if (lcore_params == NULL) {
1185 printf("Error: No port/queue/core mappings\n");
1189 for (i = 0; i < nb_lcore_params; ++i) {
1190 lcore = lcore_params[i].lcore_id;
1191 if (!rte_lcore_is_enabled(lcore)) {
1192 printf("error: lcore %hhu is not enabled in "
1193 "lcore mask\n", lcore);
1196 socket_id = rte_lcore_to_socket_id(lcore);
1197 if (socket_id != 0 && numa_on == 0) {
1198 printf("warning: lcore %hhu is on socket %d "
1202 portid = lcore_params[i].port_id;
1203 if ((enabled_port_mask & (1 << portid)) == 0) {
1204 printf("port %u is not enabled in port mask\n", portid);
1207 if (!rte_eth_dev_is_valid_port(portid)) {
1208 printf("port %u is not present on the board\n", portid);
1216 get_port_nb_rx_queues(const uint16_t port)
1221 for (i = 0; i < nb_lcore_params; ++i) {
1222 if (lcore_params[i].port_id == port &&
1223 lcore_params[i].queue_id > queue)
1224 queue = lcore_params[i].queue_id;
1226 return (uint8_t)(++queue);
1230 init_lcore_rx_queues(void)
1232 uint16_t i, nb_rx_queue;
1235 for (i = 0; i < nb_lcore_params; ++i) {
1236 lcore = lcore_params[i].lcore_id;
1237 nb_rx_queue = lcore_conf[lcore].nb_rx_queue;
1238 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1239 printf("error: too many queues (%u) for lcore: %u\n",
1240 nb_rx_queue + 1, lcore);
1243 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1244 lcore_params[i].port_id;
1245 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1246 lcore_params[i].queue_id;
1247 lcore_conf[lcore].nb_rx_queue++;
1254 print_usage(const char *prgname)
1256 fprintf(stderr, "%s [EAL options] --"
1262 " [-w REPLAY_WINDOW_SIZE]"
1266 " --config (port,queue,lcore)[,(port,queue,lcore)]"
1267 " [--single-sa SAIDX]"
1268 " [--cryptodev_mask MASK]"
1269 " [--" CMD_LINE_OPT_RX_OFFLOAD " RX_OFFLOAD_MASK]"
1270 " [--" CMD_LINE_OPT_TX_OFFLOAD " TX_OFFLOAD_MASK]"
1271 " [--" CMD_LINE_OPT_REASSEMBLE " REASSEMBLE_TABLE_SIZE]"
1272 " [--" CMD_LINE_OPT_MTU " MTU]"
1274 " -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
1275 " -P : Enable promiscuous mode\n"
1276 " -u PORTMASK: Hexadecimal bitmask of unprotected ports\n"
1277 " -j FRAMESIZE: Data buffer size, minimum (and default)\n"
1278 " value: RTE_MBUF_DEFAULT_BUF_SIZE\n"
1279 " -l enables code-path that uses librte_ipsec\n"
1280 " -w REPLAY_WINDOW_SIZE specifies IPsec SQN replay window\n"
1281 " size for each SA\n"
1283 " -a enables SA SQN atomic behaviour\n"
1284 " -f CONFIG_FILE: Configuration file\n"
1285 " --config (port,queue,lcore): Rx queue configuration\n"
1286 " --single-sa SAIDX: Use single SA index for outbound traffic,\n"
1287 " bypassing the SP\n"
1288 " --cryptodev_mask MASK: Hexadecimal bitmask of the crypto\n"
1289 " devices to configure\n"
1290 " --" CMD_LINE_OPT_RX_OFFLOAD
1291 ": bitmask of the RX HW offload capabilities to enable/use\n"
1292 " (DEV_RX_OFFLOAD_*)\n"
1293 " --" CMD_LINE_OPT_TX_OFFLOAD
1294 ": bitmask of the TX HW offload capabilities to enable/use\n"
1295 " (DEV_TX_OFFLOAD_*)\n"
1296 " --" CMD_LINE_OPT_REASSEMBLE " NUM"
1297 ": max number of entries in reassemble(fragment) table\n"
1298 " (zero (default value) disables reassembly)\n"
1299 " --" CMD_LINE_OPT_MTU " MTU"
1300 ": MTU value on all ports (default value: 1500)\n"
1301 " outgoing packets with bigger size will be fragmented\n"
1302 " incoming packets with bigger size will be discarded\n"
1308 parse_mask(const char *str, uint64_t *val)
1314 t = strtoul(str, &end, 0);
1315 if (errno != 0 || end[0] != 0)
1323 parse_portmask(const char *portmask)
1328 /* parse hexadecimal string */
1329 pm = strtoul(portmask, &end, 16);
1330 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1333 if ((pm == 0) && errno)
1340 parse_decimal(const char *str)
1345 num = strtoul(str, &end, 10);
1346 if ((str[0] == '\0') || (end == NULL) || (*end != '\0'))
1353 parse_config(const char *q_arg)
1356 const char *p, *p0 = q_arg;
1364 unsigned long int_fld[_NUM_FLD];
1365 char *str_fld[_NUM_FLD];
1369 nb_lcore_params = 0;
1371 while ((p = strchr(p0, '(')) != NULL) {
1373 p0 = strchr(p, ')');
1378 if (size >= sizeof(s))
1381 snprintf(s, sizeof(s), "%.*s", size, p);
1382 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1385 for (i = 0; i < _NUM_FLD; i++) {
1387 int_fld[i] = strtoul(str_fld[i], &end, 0);
1388 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
1391 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1392 printf("exceeded max number of lcore params: %hu\n",
1396 lcore_params_array[nb_lcore_params].port_id =
1397 (uint8_t)int_fld[FLD_PORT];
1398 lcore_params_array[nb_lcore_params].queue_id =
1399 (uint8_t)int_fld[FLD_QUEUE];
1400 lcore_params_array[nb_lcore_params].lcore_id =
1401 (uint8_t)int_fld[FLD_LCORE];
1404 lcore_params = lcore_params_array;
1409 print_app_sa_prm(const struct app_sa_prm *prm)
1411 printf("librte_ipsec usage: %s\n",
1412 (prm->enable == 0) ? "disabled" : "enabled");
1414 if (prm->enable == 0)
1417 printf("replay window size: %u\n", prm->window_size);
1418 printf("ESN: %s\n", (prm->enable_esn == 0) ? "disabled" : "enabled");
1419 printf("SA flags: %#" PRIx64 "\n", prm->flags);
1423 parse_args(int32_t argc, char **argv)
1427 int32_t option_index;
1428 char *prgname = argv[0];
1429 int32_t f_present = 0;
1433 while ((opt = getopt_long(argc, argvopt, "aelp:Pu:f:j:w:",
1434 lgopts, &option_index)) != EOF) {
1438 enabled_port_mask = parse_portmask(optarg);
1439 if (enabled_port_mask == 0) {
1440 printf("invalid portmask\n");
1441 print_usage(prgname);
1446 printf("Promiscuous mode selected\n");
1450 unprotected_port_mask = parse_portmask(optarg);
1451 if (unprotected_port_mask == 0) {
1452 printf("invalid unprotected portmask\n");
1453 print_usage(prgname);
1458 if (f_present == 1) {
1459 printf("\"-f\" option present more than "
1461 print_usage(prgname);
1464 if (parse_cfg_file(optarg) < 0) {
1465 printf("parsing file \"%s\" failed\n",
1467 print_usage(prgname);
1473 ret = parse_decimal(optarg);
1474 if (ret < RTE_MBUF_DEFAULT_BUF_SIZE ||
1476 printf("Invalid frame buffer size value: %s\n",
1478 print_usage(prgname);
1481 frame_buf_size = ret;
1482 printf("Custom frame buffer size %u\n", frame_buf_size);
1485 app_sa_prm.enable = 1;
1488 app_sa_prm.enable = 1;
1489 app_sa_prm.window_size = parse_decimal(optarg);
1492 app_sa_prm.enable = 1;
1493 app_sa_prm.enable_esn = 1;
1496 app_sa_prm.enable = 1;
1497 app_sa_prm.flags |= RTE_IPSEC_SAFLAG_SQN_ATOM;
1499 case CMD_LINE_OPT_CONFIG_NUM:
1500 ret = parse_config(optarg);
1502 printf("Invalid config\n");
1503 print_usage(prgname);
1507 case CMD_LINE_OPT_SINGLE_SA_NUM:
1508 ret = parse_decimal(optarg);
1510 printf("Invalid argument[sa_idx]\n");
1511 print_usage(prgname);
1517 single_sa_idx = ret;
1518 printf("Configured with single SA index %u\n",
1521 case CMD_LINE_OPT_CRYPTODEV_MASK_NUM:
1522 ret = parse_portmask(optarg);
1524 printf("Invalid argument[portmask]\n");
1525 print_usage(prgname);
1530 enabled_cryptodev_mask = ret;
1532 case CMD_LINE_OPT_RX_OFFLOAD_NUM:
1533 ret = parse_mask(optarg, &dev_rx_offload);
1535 printf("Invalid argument for \'%s\': %s\n",
1536 CMD_LINE_OPT_RX_OFFLOAD, optarg);
1537 print_usage(prgname);
1541 case CMD_LINE_OPT_TX_OFFLOAD_NUM:
1542 ret = parse_mask(optarg, &dev_tx_offload);
1544 printf("Invalid argument for \'%s\': %s\n",
1545 CMD_LINE_OPT_TX_OFFLOAD, optarg);
1546 print_usage(prgname);
1550 case CMD_LINE_OPT_REASSEMBLE_NUM:
1551 ret = parse_decimal(optarg);
1553 printf("Invalid argument for \'%s\': %s\n",
1554 CMD_LINE_OPT_REASSEMBLE, optarg);
1555 print_usage(prgname);
1560 case CMD_LINE_OPT_MTU_NUM:
1561 ret = parse_decimal(optarg);
1562 if (ret < 0 || ret > RTE_IPV4_MAX_PKT_LEN) {
1563 printf("Invalid argument for \'%s\': %s\n",
1564 CMD_LINE_OPT_MTU, optarg);
1565 print_usage(prgname);
1571 print_usage(prgname);
1576 if (f_present == 0) {
1577 printf("Mandatory option \"-f\" not present\n");
1581 /* check do we need to enable multi-seg support */
1582 if (multi_seg_required()) {
1583 /* legacy mode doesn't support multi-seg */
1584 app_sa_prm.enable = 1;
1585 printf("frame buf size: %u, mtu: %u, "
1586 "number of reassemble entries: %u\n"
1587 "multi-segment support is required\n",
1588 frame_buf_size, mtu_size, frag_tbl_sz);
1591 print_app_sa_prm(&app_sa_prm);
1594 argv[optind-1] = prgname;
1597 optind = 1; /* reset getopt lib */
1602 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
1604 char buf[RTE_ETHER_ADDR_FMT_SIZE];
1605 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
1606 printf("%s%s", name, buf);
1610 * Update destination ethaddr for the port.
1613 add_dst_ethaddr(uint16_t port, const struct rte_ether_addr *addr)
1615 if (port >= RTE_DIM(ethaddr_tbl))
1618 ethaddr_tbl[port].dst = ETHADDR_TO_UINT64(addr);
1622 /* Check the link status of all ports in up to 9s, and print them finally */
1624 check_all_ports_link_status(uint32_t port_mask)
1626 #define CHECK_INTERVAL 100 /* 100ms */
1627 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1629 uint8_t count, all_ports_up, print_flag = 0;
1630 struct rte_eth_link link;
1632 printf("\nChecking link status");
1634 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1636 RTE_ETH_FOREACH_DEV(portid) {
1637 if ((port_mask & (1 << portid)) == 0)
1639 memset(&link, 0, sizeof(link));
1640 rte_eth_link_get_nowait(portid, &link);
1641 /* print link status if flag set */
1642 if (print_flag == 1) {
1643 if (link.link_status)
1645 "Port%d Link Up - speed %u Mbps -%s\n",
1646 portid, link.link_speed,
1647 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1648 ("full-duplex") : ("half-duplex\n"));
1650 printf("Port %d Link Down\n", portid);
1653 /* clear all_ports_up flag if any link down */
1654 if (link.link_status == ETH_LINK_DOWN) {
1659 /* after finally printing all link status, get out */
1660 if (print_flag == 1)
1663 if (all_ports_up == 0) {
1666 rte_delay_ms(CHECK_INTERVAL);
1669 /* set the print_flag if all ports up or timeout */
1670 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1678 add_mapping(struct rte_hash *map, const char *str, uint16_t cdev_id,
1679 uint16_t qp, struct lcore_params *params,
1680 struct ipsec_ctx *ipsec_ctx,
1681 const struct rte_cryptodev_capabilities *cipher,
1682 const struct rte_cryptodev_capabilities *auth,
1683 const struct rte_cryptodev_capabilities *aead)
1687 struct cdev_key key = { 0 };
1689 key.lcore_id = params->lcore_id;
1691 key.cipher_algo = cipher->sym.cipher.algo;
1693 key.auth_algo = auth->sym.auth.algo;
1695 key.aead_algo = aead->sym.aead.algo;
1697 ret = rte_hash_lookup(map, &key);
1701 for (i = 0; i < ipsec_ctx->nb_qps; i++)
1702 if (ipsec_ctx->tbl[i].id == cdev_id)
1705 if (i == ipsec_ctx->nb_qps) {
1706 if (ipsec_ctx->nb_qps == MAX_QP_PER_LCORE) {
1707 printf("Maximum number of crypto devices assigned to "
1708 "a core, increase MAX_QP_PER_LCORE value\n");
1711 ipsec_ctx->tbl[i].id = cdev_id;
1712 ipsec_ctx->tbl[i].qp = qp;
1713 ipsec_ctx->nb_qps++;
1714 printf("%s cdev mapping: lcore %u using cdev %u qp %u "
1715 "(cdev_id_qp %lu)\n", str, key.lcore_id,
1719 ret = rte_hash_add_key_data(map, &key, (void *)i);
1721 printf("Faled to insert cdev mapping for (lcore %u, "
1722 "cdev %u, qp %u), errno %d\n",
1723 key.lcore_id, ipsec_ctx->tbl[i].id,
1724 ipsec_ctx->tbl[i].qp, ret);
1732 add_cdev_mapping(struct rte_cryptodev_info *dev_info, uint16_t cdev_id,
1733 uint16_t qp, struct lcore_params *params)
1736 const struct rte_cryptodev_capabilities *i, *j;
1737 struct rte_hash *map;
1738 struct lcore_conf *qconf;
1739 struct ipsec_ctx *ipsec_ctx;
1742 qconf = &lcore_conf[params->lcore_id];
1744 if ((unprotected_port_mask & (1 << params->port_id)) == 0) {
1746 ipsec_ctx = &qconf->outbound;
1750 ipsec_ctx = &qconf->inbound;
1754 /* Required cryptodevs with operation chainning */
1755 if (!(dev_info->feature_flags &
1756 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING))
1759 for (i = dev_info->capabilities;
1760 i->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; i++) {
1761 if (i->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1764 if (i->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1765 ret |= add_mapping(map, str, cdev_id, qp, params,
1766 ipsec_ctx, NULL, NULL, i);
1770 if (i->sym.xform_type != RTE_CRYPTO_SYM_XFORM_CIPHER)
1773 for (j = dev_info->capabilities;
1774 j->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; j++) {
1775 if (j->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1778 if (j->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH)
1781 ret |= add_mapping(map, str, cdev_id, qp, params,
1782 ipsec_ctx, i, j, NULL);
1789 /* Check if the device is enabled by cryptodev_mask */
1791 check_cryptodev_mask(uint8_t cdev_id)
1793 if (enabled_cryptodev_mask & (1 << cdev_id))
1800 cryptodevs_init(void)
1802 struct rte_cryptodev_config dev_conf;
1803 struct rte_cryptodev_qp_conf qp_conf;
1804 uint16_t idx, max_nb_qps, qp, i;
1805 int16_t cdev_id, port_id;
1806 struct rte_hash_parameters params = { 0 };
1808 const uint64_t mseg_flag = multi_seg_required() ?
1809 RTE_CRYPTODEV_FF_IN_PLACE_SGL : 0;
1811 params.entries = CDEV_MAP_ENTRIES;
1812 params.key_len = sizeof(struct cdev_key);
1813 params.hash_func = rte_jhash;
1814 params.hash_func_init_val = 0;
1815 params.socket_id = rte_socket_id();
1817 params.name = "cdev_map_in";
1818 cdev_map_in = rte_hash_create(¶ms);
1819 if (cdev_map_in == NULL)
1820 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1823 params.name = "cdev_map_out";
1824 cdev_map_out = rte_hash_create(¶ms);
1825 if (cdev_map_out == NULL)
1826 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1829 printf("lcore/cryptodev/qp mappings:\n");
1831 uint32_t max_sess_sz = 0, sess_sz;
1832 for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1835 /* Get crypto priv session size */
1836 sess_sz = rte_cryptodev_sym_get_private_session_size(cdev_id);
1837 if (sess_sz > max_sess_sz)
1838 max_sess_sz = sess_sz;
1841 * If crypto device is security capable, need to check the
1842 * size of security session as well.
1845 /* Get security context of the crypto device */
1846 sec_ctx = rte_cryptodev_get_sec_ctx(cdev_id);
1847 if (sec_ctx == NULL)
1850 /* Get size of security session */
1851 sess_sz = rte_security_session_get_size(sec_ctx);
1852 if (sess_sz > max_sess_sz)
1853 max_sess_sz = sess_sz;
1855 RTE_ETH_FOREACH_DEV(port_id) {
1858 if ((enabled_port_mask & (1 << port_id)) == 0)
1861 sec_ctx = rte_eth_dev_get_sec_ctx(port_id);
1862 if (sec_ctx == NULL)
1865 sess_sz = rte_security_session_get_size(sec_ctx);
1866 if (sess_sz > max_sess_sz)
1867 max_sess_sz = sess_sz;
1871 for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1872 struct rte_cryptodev_info cdev_info;
1874 if (check_cryptodev_mask((uint8_t)cdev_id))
1877 rte_cryptodev_info_get(cdev_id, &cdev_info);
1879 if ((mseg_flag & cdev_info.feature_flags) != mseg_flag)
1880 rte_exit(EXIT_FAILURE,
1881 "Device %hd does not support \'%s\' feature\n",
1883 rte_cryptodev_get_feature_name(mseg_flag));
1885 if (nb_lcore_params > cdev_info.max_nb_queue_pairs)
1886 max_nb_qps = cdev_info.max_nb_queue_pairs;
1888 max_nb_qps = nb_lcore_params;
1892 while (qp < max_nb_qps && i < nb_lcore_params) {
1893 if (add_cdev_mapping(&cdev_info, cdev_id, qp,
1894 &lcore_params[idx]))
1897 idx = idx % nb_lcore_params;
1904 dev_conf.socket_id = rte_cryptodev_socket_id(cdev_id);
1905 dev_conf.nb_queue_pairs = qp;
1906 dev_conf.ff_disable = RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO;
1908 uint32_t dev_max_sess = cdev_info.sym.max_nb_sessions;
1909 if (dev_max_sess != 0 && dev_max_sess < CDEV_MP_NB_OBJS)
1910 rte_exit(EXIT_FAILURE,
1911 "Device does not support at least %u "
1912 "sessions", CDEV_MP_NB_OBJS);
1914 if (!socket_ctx[dev_conf.socket_id].session_pool) {
1915 char mp_name[RTE_MEMPOOL_NAMESIZE];
1916 struct rte_mempool *sess_mp;
1918 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1919 "sess_mp_%u", dev_conf.socket_id);
1920 sess_mp = rte_cryptodev_sym_session_pool_create(
1921 mp_name, CDEV_MP_NB_OBJS,
1922 0, CDEV_MP_CACHE_SZ, 0,
1923 dev_conf.socket_id);
1924 socket_ctx[dev_conf.socket_id].session_pool = sess_mp;
1927 if (!socket_ctx[dev_conf.socket_id].session_priv_pool) {
1928 char mp_name[RTE_MEMPOOL_NAMESIZE];
1929 struct rte_mempool *sess_mp;
1931 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1932 "sess_mp_priv_%u", dev_conf.socket_id);
1933 sess_mp = rte_mempool_create(mp_name,
1937 0, NULL, NULL, NULL,
1938 NULL, dev_conf.socket_id,
1940 socket_ctx[dev_conf.socket_id].session_priv_pool =
1944 if (!socket_ctx[dev_conf.socket_id].session_priv_pool ||
1945 !socket_ctx[dev_conf.socket_id].session_pool)
1946 rte_exit(EXIT_FAILURE,
1947 "Cannot create session pool on socket %d\n",
1948 dev_conf.socket_id);
1950 printf("Allocated session pool on socket %d\n",
1951 dev_conf.socket_id);
1953 if (rte_cryptodev_configure(cdev_id, &dev_conf))
1954 rte_panic("Failed to initialize cryptodev %u\n",
1957 qp_conf.nb_descriptors = CDEV_QUEUE_DESC;
1958 qp_conf.mp_session =
1959 socket_ctx[dev_conf.socket_id].session_pool;
1960 qp_conf.mp_session_private =
1961 socket_ctx[dev_conf.socket_id].session_priv_pool;
1962 for (qp = 0; qp < dev_conf.nb_queue_pairs; qp++)
1963 if (rte_cryptodev_queue_pair_setup(cdev_id, qp,
1964 &qp_conf, dev_conf.socket_id))
1965 rte_panic("Failed to setup queue %u for "
1966 "cdev_id %u\n", 0, cdev_id);
1968 if (rte_cryptodev_start(cdev_id))
1969 rte_panic("Failed to start cryptodev %u\n",
1973 /* create session pools for eth devices that implement security */
1974 RTE_ETH_FOREACH_DEV(port_id) {
1975 if ((enabled_port_mask & (1 << port_id)) &&
1976 rte_eth_dev_get_sec_ctx(port_id)) {
1977 int socket_id = rte_eth_dev_socket_id(port_id);
1979 if (!socket_ctx[socket_id].session_priv_pool) {
1980 char mp_name[RTE_MEMPOOL_NAMESIZE];
1981 struct rte_mempool *sess_mp;
1983 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1984 "sess_mp_%u", socket_id);
1985 sess_mp = rte_mempool_create(mp_name,
1986 (CDEV_MP_NB_OBJS * 2),
1989 0, NULL, NULL, NULL,
1992 if (sess_mp == NULL)
1993 rte_exit(EXIT_FAILURE,
1994 "Cannot create session pool "
1995 "on socket %d\n", socket_id);
1997 printf("Allocated session pool "
1998 "on socket %d\n", socket_id);
1999 socket_ctx[socket_id].session_priv_pool =
2012 port_init(uint16_t portid, uint64_t req_rx_offloads, uint64_t req_tx_offloads)
2014 uint32_t frame_size;
2015 struct rte_eth_dev_info dev_info;
2016 struct rte_eth_txconf *txconf;
2017 uint16_t nb_tx_queue, nb_rx_queue;
2018 uint16_t tx_queueid, rx_queueid, queue, lcore_id;
2019 int32_t ret, socket_id;
2020 struct lcore_conf *qconf;
2021 struct rte_ether_addr ethaddr;
2022 struct rte_eth_conf local_port_conf = port_conf;
2024 rte_eth_dev_info_get(portid, &dev_info);
2026 /* limit allowed HW offloafs, as user requested */
2027 dev_info.rx_offload_capa &= dev_rx_offload;
2028 dev_info.tx_offload_capa &= dev_tx_offload;
2030 printf("Configuring device port %u:\n", portid);
2032 rte_eth_macaddr_get(portid, ðaddr);
2033 ethaddr_tbl[portid].src = ETHADDR_TO_UINT64(ðaddr);
2034 print_ethaddr("Address: ", ðaddr);
2037 nb_rx_queue = get_port_nb_rx_queues(portid);
2038 nb_tx_queue = nb_lcores;
2040 if (nb_rx_queue > dev_info.max_rx_queues)
2041 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
2042 "(max rx queue is %u)\n",
2043 nb_rx_queue, dev_info.max_rx_queues);
2045 if (nb_tx_queue > dev_info.max_tx_queues)
2046 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
2047 "(max tx queue is %u)\n",
2048 nb_tx_queue, dev_info.max_tx_queues);
2050 printf("Creating queues: nb_rx_queue=%d nb_tx_queue=%u...\n",
2051 nb_rx_queue, nb_tx_queue);
2053 frame_size = MTU_TO_FRAMELEN(mtu_size);
2054 if (frame_size > local_port_conf.rxmode.max_rx_pkt_len)
2055 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
2056 local_port_conf.rxmode.max_rx_pkt_len = frame_size;
2058 if (multi_seg_required()) {
2059 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_SCATTER;
2060 local_port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
2063 local_port_conf.rxmode.offloads |= req_rx_offloads;
2064 local_port_conf.txmode.offloads |= req_tx_offloads;
2066 /* Check that all required capabilities are supported */
2067 if ((local_port_conf.rxmode.offloads & dev_info.rx_offload_capa) !=
2068 local_port_conf.rxmode.offloads)
2069 rte_exit(EXIT_FAILURE,
2070 "Error: port %u required RX offloads: 0x%" PRIx64
2071 ", avaialbe RX offloads: 0x%" PRIx64 "\n",
2072 portid, local_port_conf.rxmode.offloads,
2073 dev_info.rx_offload_capa);
2075 if ((local_port_conf.txmode.offloads & dev_info.tx_offload_capa) !=
2076 local_port_conf.txmode.offloads)
2077 rte_exit(EXIT_FAILURE,
2078 "Error: port %u required TX offloads: 0x%" PRIx64
2079 ", avaialbe TX offloads: 0x%" PRIx64 "\n",
2080 portid, local_port_conf.txmode.offloads,
2081 dev_info.tx_offload_capa);
2083 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2084 local_port_conf.txmode.offloads |=
2085 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2087 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_IPV4_CKSUM)
2088 local_port_conf.txmode.offloads |= DEV_TX_OFFLOAD_IPV4_CKSUM;
2090 printf("port %u configurng rx_offloads=0x%" PRIx64
2091 ", tx_offloads=0x%" PRIx64 "\n",
2092 portid, local_port_conf.rxmode.offloads,
2093 local_port_conf.txmode.offloads);
2095 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
2096 dev_info.flow_type_rss_offloads;
2097 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
2098 port_conf.rx_adv_conf.rss_conf.rss_hf) {
2099 printf("Port %u modified RSS hash function based on hardware support,"
2100 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
2102 port_conf.rx_adv_conf.rss_conf.rss_hf,
2103 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
2106 ret = rte_eth_dev_configure(portid, nb_rx_queue, nb_tx_queue,
2109 rte_exit(EXIT_FAILURE, "Cannot configure device: "
2110 "err=%d, port=%d\n", ret, portid);
2112 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
2114 rte_exit(EXIT_FAILURE, "Cannot adjust number of descriptors: "
2115 "err=%d, port=%d\n", ret, portid);
2117 /* init one TX queue per lcore */
2119 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2120 if (rte_lcore_is_enabled(lcore_id) == 0)
2124 socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2129 printf("Setup txq=%u,%d,%d\n", lcore_id, tx_queueid, socket_id);
2131 txconf = &dev_info.default_txconf;
2132 txconf->offloads = local_port_conf.txmode.offloads;
2134 ret = rte_eth_tx_queue_setup(portid, tx_queueid, nb_txd,
2137 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
2138 "err=%d, port=%d\n", ret, portid);
2140 qconf = &lcore_conf[lcore_id];
2141 qconf->tx_queue_id[portid] = tx_queueid;
2143 /* Pre-populate pkt offloads based on capabilities */
2144 qconf->outbound.ipv4_offloads = PKT_TX_IPV4;
2145 qconf->outbound.ipv6_offloads = PKT_TX_IPV6;
2146 if (local_port_conf.txmode.offloads & DEV_TX_OFFLOAD_IPV4_CKSUM)
2147 qconf->outbound.ipv4_offloads |= PKT_TX_IP_CKSUM;
2151 /* init RX queues */
2152 for (queue = 0; queue < qconf->nb_rx_queue; ++queue) {
2153 struct rte_eth_rxconf rxq_conf;
2155 if (portid != qconf->rx_queue_list[queue].port_id)
2158 rx_queueid = qconf->rx_queue_list[queue].queue_id;
2160 printf("Setup rxq=%d,%d,%d\n", portid, rx_queueid,
2163 rxq_conf = dev_info.default_rxconf;
2164 rxq_conf.offloads = local_port_conf.rxmode.offloads;
2165 ret = rte_eth_rx_queue_setup(portid, rx_queueid,
2166 nb_rxd, socket_id, &rxq_conf,
2167 socket_ctx[socket_id].mbuf_pool);
2169 rte_exit(EXIT_FAILURE,
2170 "rte_eth_rx_queue_setup: err=%d, "
2171 "port=%d\n", ret, portid);
2178 pool_init(struct socket_ctx *ctx, int32_t socket_id, uint32_t nb_mbuf)
2183 snprintf(s, sizeof(s), "mbuf_pool_%d", socket_id);
2184 ctx->mbuf_pool = rte_pktmbuf_pool_create(s, nb_mbuf,
2185 MEMPOOL_CACHE_SIZE, ipsec_metadata_size(),
2186 frame_buf_size, socket_id);
2189 * if multi-segment support is enabled, then create a pool
2190 * for indirect mbufs.
2192 ms = multi_seg_required();
2194 snprintf(s, sizeof(s), "mbuf_pool_indir_%d", socket_id);
2195 ctx->mbuf_pool_indir = rte_pktmbuf_pool_create(s, nb_mbuf,
2196 MEMPOOL_CACHE_SIZE, 0, 0, socket_id);
2199 if (ctx->mbuf_pool == NULL || (ms != 0 && ctx->mbuf_pool_indir == NULL))
2200 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n",
2203 printf("Allocated mbuf pool on socket %d\n", socket_id);
2207 inline_ipsec_event_esn_overflow(struct rte_security_ctx *ctx, uint64_t md)
2209 struct ipsec_sa *sa;
2211 /* For inline protocol processing, the metadata in the event will
2212 * uniquely identify the security session which raised the event.
2213 * Application would then need the userdata it had registered with the
2214 * security session to process the event.
2217 sa = (struct ipsec_sa *)rte_security_get_userdata(ctx, md);
2220 /* userdata could not be retrieved */
2224 /* Sequence number over flow. SA need to be re-established */
2230 inline_ipsec_event_callback(uint16_t port_id, enum rte_eth_event_type type,
2231 void *param, void *ret_param)
2234 struct rte_eth_event_ipsec_desc *event_desc = NULL;
2235 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
2236 rte_eth_dev_get_sec_ctx(port_id);
2238 RTE_SET_USED(param);
2240 if (type != RTE_ETH_EVENT_IPSEC)
2243 event_desc = ret_param;
2244 if (event_desc == NULL) {
2245 printf("Event descriptor not set\n");
2249 md = event_desc->metadata;
2251 if (event_desc->subtype == RTE_ETH_EVENT_IPSEC_ESN_OVERFLOW)
2252 return inline_ipsec_event_esn_overflow(ctx, md);
2253 else if (event_desc->subtype >= RTE_ETH_EVENT_IPSEC_MAX) {
2254 printf("Invalid IPsec event reported\n");
2262 rx_callback(__rte_unused uint16_t port, __rte_unused uint16_t queue,
2263 struct rte_mbuf *pkt[], uint16_t nb_pkts,
2264 __rte_unused uint16_t max_pkts, void *user_param)
2268 struct lcore_conf *lc;
2269 struct rte_mbuf *mb;
2270 struct rte_ether_hdr *eth;
2276 for (i = 0; i != nb_pkts; i++) {
2279 eth = rte_pktmbuf_mtod(mb, struct rte_ether_hdr *);
2280 if (eth->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
2282 struct rte_ipv4_hdr *iph;
2284 iph = (struct rte_ipv4_hdr *)(eth + 1);
2285 if (rte_ipv4_frag_pkt_is_fragmented(iph)) {
2287 mb->l2_len = sizeof(*eth);
2288 mb->l3_len = sizeof(*iph);
2289 tm = (tm != 0) ? tm : rte_rdtsc();
2290 mb = rte_ipv4_frag_reassemble_packet(
2291 lc->frag.tbl, &lc->frag.dr,
2295 /* fix ip cksum after reassemble. */
2296 iph = rte_pktmbuf_mtod_offset(mb,
2297 struct rte_ipv4_hdr *,
2299 iph->hdr_checksum = 0;
2300 iph->hdr_checksum = rte_ipv4_cksum(iph);
2303 } else if (eth->ether_type ==
2304 rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
2306 struct rte_ipv6_hdr *iph;
2307 struct ipv6_extension_fragment *fh;
2309 iph = (struct rte_ipv6_hdr *)(eth + 1);
2310 fh = rte_ipv6_frag_get_ipv6_fragment_header(iph);
2312 mb->l2_len = sizeof(*eth);
2313 mb->l3_len = (uintptr_t)fh - (uintptr_t)iph +
2315 tm = (tm != 0) ? tm : rte_rdtsc();
2316 mb = rte_ipv6_frag_reassemble_packet(
2317 lc->frag.tbl, &lc->frag.dr,
2320 /* fix l3_len after reassemble. */
2321 mb->l3_len = mb->l3_len - sizeof(*fh);
2329 /* some fragments were encountered, drain death row */
2331 rte_ip_frag_free_death_row(&lc->frag.dr, 0);
2338 reassemble_lcore_init(struct lcore_conf *lc, uint32_t cid)
2342 uint64_t frag_cycles;
2343 const struct lcore_rx_queue *rxq;
2344 const struct rte_eth_rxtx_callback *cb;
2346 /* create fragment table */
2347 sid = rte_lcore_to_socket_id(cid);
2348 frag_cycles = (rte_get_tsc_hz() + MS_PER_S - 1) /
2349 MS_PER_S * FRAG_TTL_MS;
2351 lc->frag.tbl = rte_ip_frag_table_create(frag_tbl_sz,
2352 FRAG_TBL_BUCKET_ENTRIES, frag_tbl_sz, frag_cycles, sid);
2353 if (lc->frag.tbl == NULL) {
2354 printf("%s(%u): failed to create fragment table of size: %u, "
2356 __func__, cid, frag_tbl_sz, rte_errno);
2360 /* setup reassemble RX callbacks for all queues */
2361 for (i = 0; i != lc->nb_rx_queue; i++) {
2363 rxq = lc->rx_queue_list + i;
2364 cb = rte_eth_add_rx_callback(rxq->port_id, rxq->queue_id,
2367 printf("%s(%u): failed to install RX callback for "
2368 "portid=%u, queueid=%u, error code: %d\n",
2370 rxq->port_id, rxq->queue_id, rte_errno);
2379 reassemble_init(void)
2385 for (i = 0; i != nb_lcore_params; i++) {
2386 lc = lcore_params[i].lcore_id;
2387 rc = reassemble_lcore_init(lcore_conf + lc, lc);
2396 main(int32_t argc, char **argv)
2402 uint64_t req_rx_offloads, req_tx_offloads;
2405 ret = rte_eal_init(argc, argv);
2407 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
2411 /* parse application arguments (after the EAL ones) */
2412 ret = parse_args(argc, argv);
2414 rte_exit(EXIT_FAILURE, "Invalid parameters\n");
2416 if ((unprotected_port_mask & enabled_port_mask) !=
2417 unprotected_port_mask)
2418 rte_exit(EXIT_FAILURE, "Invalid unprotected portmask 0x%x\n",
2419 unprotected_port_mask);
2421 if (check_params() < 0)
2422 rte_exit(EXIT_FAILURE, "check_params failed\n");
2424 ret = init_lcore_rx_queues();
2426 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2428 nb_lcores = rte_lcore_count();
2430 /* Replicate each context per socket */
2431 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2432 if (rte_lcore_is_enabled(lcore_id) == 0)
2436 socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2440 if (socket_ctx[socket_id].mbuf_pool)
2444 sp4_init(&socket_ctx[socket_id], socket_id);
2446 sp6_init(&socket_ctx[socket_id], socket_id);
2449 sa_init(&socket_ctx[socket_id], socket_id);
2451 rt_init(&socket_ctx[socket_id], socket_id);
2453 pool_init(&socket_ctx[socket_id], socket_id, NB_MBUF);
2456 RTE_ETH_FOREACH_DEV(portid) {
2457 if ((enabled_port_mask & (1 << portid)) == 0)
2460 sa_check_offloads(portid, &req_rx_offloads, &req_tx_offloads);
2461 port_init(portid, req_rx_offloads, req_tx_offloads);
2467 RTE_ETH_FOREACH_DEV(portid) {
2468 if ((enabled_port_mask & (1 << portid)) == 0)
2472 ret = rte_eth_dev_start(portid);
2474 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: "
2475 "err=%d, port=%d\n", ret, portid);
2477 * If enabled, put device in promiscuous mode.
2478 * This allows IO forwarding mode to forward packets
2479 * to itself through 2 cross-connected ports of the
2483 rte_eth_promiscuous_enable(portid);
2485 rte_eth_dev_callback_register(portid,
2486 RTE_ETH_EVENT_IPSEC, inline_ipsec_event_callback, NULL);
2489 /* fragment reassemble is enabled */
2490 if (frag_tbl_sz != 0) {
2491 ret = reassemble_init();
2493 rte_exit(EXIT_FAILURE, "failed at reassemble init");
2496 check_all_ports_link_status(enabled_port_mask);
2498 /* launch per-lcore init on every lcore */
2499 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2500 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2501 if (rte_eal_wait_lcore(lcore_id) < 0)