4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/types.h>
39 #include <netinet/in.h>
40 #include <netinet/ip.h>
41 #include <netinet/ip6.h>
43 #include <sys/queue.h>
48 #include <rte_common.h>
49 #include <rte_byteorder.h>
52 #include <rte_launch.h>
53 #include <rte_atomic.h>
54 #include <rte_cycles.h>
55 #include <rte_prefetch.h>
56 #include <rte_lcore.h>
57 #include <rte_per_lcore.h>
58 #include <rte_branch_prediction.h>
59 #include <rte_interrupts.h>
61 #include <rte_random.h>
62 #include <rte_debug.h>
63 #include <rte_ether.h>
64 #include <rte_ethdev.h>
65 #include <rte_mempool.h>
71 #include <rte_jhash.h>
72 #include <rte_cryptodev.h>
77 #define RTE_LOGTYPE_IPSEC RTE_LOGTYPE_USER1
79 #define MAX_JUMBO_PKT_LEN 9600
81 #define MEMPOOL_CACHE_SIZE 256
83 #define NB_MBUF (32000)
85 #define CDEV_QUEUE_DESC 2048
86 #define CDEV_MAP_ENTRIES 1024
87 #define CDEV_MP_NB_OBJS 2048
88 #define CDEV_MP_CACHE_SZ 64
89 #define MAX_QUEUE_PAIRS 1
91 #define OPTION_CONFIG "config"
92 #define OPTION_SINGLE_SA "single-sa"
94 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
98 /* Configure how many packets ahead to prefetch, when reading packets */
99 #define PREFETCH_OFFSET 3
101 #define MAX_RX_QUEUE_PER_LCORE 16
103 #define MAX_LCORE_PARAMS 1024
105 #define UNPROTECTED_PORT(port) (unprotected_port_mask & (1 << portid))
108 * Configurable number of RX/TX ring descriptors
110 #define IPSEC_SECGW_RX_DESC_DEFAULT 128
111 #define IPSEC_SECGW_TX_DESC_DEFAULT 512
112 static uint16_t nb_rxd = IPSEC_SECGW_RX_DESC_DEFAULT;
113 static uint16_t nb_txd = IPSEC_SECGW_TX_DESC_DEFAULT;
115 #if RTE_BYTE_ORDER != RTE_LITTLE_ENDIAN
116 #define __BYTES_TO_UINT64(a, b, c, d, e, f, g, h) \
117 (((uint64_t)((a) & 0xff) << 56) | \
118 ((uint64_t)((b) & 0xff) << 48) | \
119 ((uint64_t)((c) & 0xff) << 40) | \
120 ((uint64_t)((d) & 0xff) << 32) | \
121 ((uint64_t)((e) & 0xff) << 24) | \
122 ((uint64_t)((f) & 0xff) << 16) | \
123 ((uint64_t)((g) & 0xff) << 8) | \
124 ((uint64_t)(h) & 0xff))
126 #define __BYTES_TO_UINT64(a, b, c, d, e, f, g, h) \
127 (((uint64_t)((h) & 0xff) << 56) | \
128 ((uint64_t)((g) & 0xff) << 48) | \
129 ((uint64_t)((f) & 0xff) << 40) | \
130 ((uint64_t)((e) & 0xff) << 32) | \
131 ((uint64_t)((d) & 0xff) << 24) | \
132 ((uint64_t)((c) & 0xff) << 16) | \
133 ((uint64_t)((b) & 0xff) << 8) | \
134 ((uint64_t)(a) & 0xff))
136 #define ETHADDR(a, b, c, d, e, f) (__BYTES_TO_UINT64(a, b, c, d, e, f, 0, 0))
138 #define ETHADDR_TO_UINT64(addr) __BYTES_TO_UINT64( \
139 addr.addr_bytes[0], addr.addr_bytes[1], \
140 addr.addr_bytes[2], addr.addr_bytes[3], \
141 addr.addr_bytes[4], addr.addr_bytes[5], \
144 /* port/source ethernet addr and destination ethernet addr */
145 struct ethaddr_info {
149 struct ethaddr_info ethaddr_tbl[RTE_MAX_ETHPORTS] = {
150 { 0, ETHADDR(0x00, 0x16, 0x3e, 0x7e, 0x94, 0x9a) },
151 { 0, ETHADDR(0x00, 0x16, 0x3e, 0x22, 0xa1, 0xd9) },
152 { 0, ETHADDR(0x00, 0x16, 0x3e, 0x08, 0x69, 0x26) },
153 { 0, ETHADDR(0x00, 0x16, 0x3e, 0x49, 0x9e, 0xdd) }
156 /* mask of enabled ports */
157 static uint32_t enabled_port_mask;
158 static uint32_t unprotected_port_mask;
159 static int32_t promiscuous_on = 1;
160 static int32_t numa_on = 1; /**< NUMA is enabled by default. */
161 static uint32_t nb_lcores;
162 static uint32_t single_sa;
163 static uint32_t single_sa_idx;
165 struct lcore_rx_queue {
168 } __rte_cache_aligned;
170 struct lcore_params {
174 } __rte_cache_aligned;
176 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
178 static struct lcore_params *lcore_params;
179 static uint16_t nb_lcore_params;
181 static struct rte_hash *cdev_map_in;
182 static struct rte_hash *cdev_map_out;
186 struct rte_mbuf *m_table[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
190 uint16_t nb_rx_queue;
191 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
192 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
193 struct buffer tx_mbufs[RTE_MAX_ETHPORTS];
194 struct ipsec_ctx inbound;
195 struct ipsec_ctx outbound;
196 struct rt_ctx *rt4_ctx;
197 struct rt_ctx *rt6_ctx;
198 } __rte_cache_aligned;
200 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
202 static struct rte_eth_conf port_conf = {
204 .mq_mode = ETH_MQ_RX_RSS,
205 .max_rx_pkt_len = ETHER_MAX_LEN,
207 .header_split = 0, /**< Header Split disabled */
208 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
209 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
210 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
211 .hw_strip_crc = 1, /**< CRC stripped by hardware */
216 .rss_hf = ETH_RSS_IP | ETH_RSS_UDP |
217 ETH_RSS_TCP | ETH_RSS_SCTP,
221 .mq_mode = ETH_MQ_TX_NONE,
225 static struct socket_ctx socket_ctx[NB_SOCKETS];
227 struct traffic_type {
228 const uint8_t *data[MAX_PKT_BURST * 2];
229 struct rte_mbuf *pkts[MAX_PKT_BURST * 2];
230 uint32_t res[MAX_PKT_BURST * 2];
234 struct ipsec_traffic {
235 struct traffic_type ipsec;
236 struct traffic_type ip4;
237 struct traffic_type ip6;
241 prepare_one_packet(struct rte_mbuf *pkt, struct ipsec_traffic *t)
244 struct ether_hdr *eth;
246 eth = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
247 if (eth->ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4)) {
248 nlp = (uint8_t *)rte_pktmbuf_adj(pkt, ETHER_HDR_LEN);
249 nlp = RTE_PTR_ADD(nlp, offsetof(struct ip, ip_p));
250 if (*nlp == IPPROTO_ESP)
251 t->ipsec.pkts[(t->ipsec.num)++] = pkt;
253 t->ip4.data[t->ip4.num] = nlp;
254 t->ip4.pkts[(t->ip4.num)++] = pkt;
256 } else if (eth->ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6)) {
257 nlp = (uint8_t *)rte_pktmbuf_adj(pkt, ETHER_HDR_LEN);
258 nlp = RTE_PTR_ADD(nlp, offsetof(struct ip6_hdr, ip6_nxt));
259 if (*nlp == IPPROTO_ESP)
260 t->ipsec.pkts[(t->ipsec.num)++] = pkt;
262 t->ip6.data[t->ip6.num] = nlp;
263 t->ip6.pkts[(t->ip6.num)++] = pkt;
266 /* Unknown/Unsupported type, drop the packet */
267 RTE_LOG(ERR, IPSEC, "Unsupported packet type\n");
268 rte_pktmbuf_free(pkt);
273 prepare_traffic(struct rte_mbuf **pkts, struct ipsec_traffic *t,
282 for (i = 0; i < (nb_pkts - PREFETCH_OFFSET); i++) {
283 rte_prefetch0(rte_pktmbuf_mtod(pkts[i + PREFETCH_OFFSET],
285 prepare_one_packet(pkts[i], t);
287 /* Process left packets */
288 for (; i < nb_pkts; i++)
289 prepare_one_packet(pkts[i], t);
293 prepare_tx_pkt(struct rte_mbuf *pkt, uint16_t port)
296 struct ether_hdr *ethhdr;
298 ip = rte_pktmbuf_mtod(pkt, struct ip *);
300 ethhdr = (struct ether_hdr *)rte_pktmbuf_prepend(pkt, ETHER_HDR_LEN);
302 if (ip->ip_v == IPVERSION) {
303 pkt->ol_flags |= PKT_TX_IP_CKSUM | PKT_TX_IPV4;
304 pkt->l3_len = sizeof(struct ip);
305 pkt->l2_len = ETHER_HDR_LEN;
307 ethhdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
309 pkt->ol_flags |= PKT_TX_IPV6;
310 pkt->l3_len = sizeof(struct ip6_hdr);
311 pkt->l2_len = ETHER_HDR_LEN;
313 ethhdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv6);
316 memcpy(ðhdr->s_addr, ðaddr_tbl[port].src,
317 sizeof(struct ether_addr));
318 memcpy(ðhdr->d_addr, ðaddr_tbl[port].dst,
319 sizeof(struct ether_addr));
323 prepare_tx_burst(struct rte_mbuf *pkts[], uint16_t nb_pkts, uint16_t port)
326 const int32_t prefetch_offset = 2;
328 for (i = 0; i < (nb_pkts - prefetch_offset); i++) {
329 rte_mbuf_prefetch_part2(pkts[i + prefetch_offset]);
330 prepare_tx_pkt(pkts[i], port);
332 /* Process left packets */
333 for (; i < nb_pkts; i++)
334 prepare_tx_pkt(pkts[i], port);
337 /* Send burst of packets on an output interface */
338 static inline int32_t
339 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
341 struct rte_mbuf **m_table;
345 queueid = qconf->tx_queue_id[port];
346 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
348 prepare_tx_burst(m_table, n, port);
350 ret = rte_eth_tx_burst(port, queueid, m_table, n);
351 if (unlikely(ret < n)) {
353 rte_pktmbuf_free(m_table[ret]);
360 /* Enqueue a single packet, and send burst if queue is filled */
361 static inline int32_t
362 send_single_packet(struct rte_mbuf *m, uint16_t port)
366 struct lcore_conf *qconf;
368 lcore_id = rte_lcore_id();
370 qconf = &lcore_conf[lcore_id];
371 len = qconf->tx_mbufs[port].len;
372 qconf->tx_mbufs[port].m_table[len] = m;
375 /* enough pkts to be sent */
376 if (unlikely(len == MAX_PKT_BURST)) {
377 send_burst(qconf, MAX_PKT_BURST, port);
381 qconf->tx_mbufs[port].len = len;
386 inbound_sp_sa(struct sp_ctx *sp, struct sa_ctx *sa, struct traffic_type *ip,
390 uint32_t i, j, res, sa_idx;
392 if (ip->num == 0 || sp == NULL)
395 rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
396 ip->num, DEFAULT_MAX_CATEGORIES);
399 for (i = 0; i < ip->num; i++) {
406 if (res & DISCARD || i < lim) {
410 /* Only check SPI match for processed IPSec packets */
411 sa_idx = ip->res[i] & PROTECT_MASK;
412 if (sa_idx == 0 || !inbound_sa_check(sa, m, sa_idx)) {
422 process_pkts_inbound(struct ipsec_ctx *ipsec_ctx,
423 struct ipsec_traffic *traffic)
426 uint16_t idx, nb_pkts_in, i, n_ip4, n_ip6;
428 nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
429 traffic->ipsec.num, MAX_PKT_BURST);
431 n_ip4 = traffic->ip4.num;
432 n_ip6 = traffic->ip6.num;
434 /* SP/ACL Inbound check ipsec and ip4 */
435 for (i = 0; i < nb_pkts_in; i++) {
436 m = traffic->ipsec.pkts[i];
437 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
438 if (ip->ip_v == IPVERSION) {
439 idx = traffic->ip4.num++;
440 traffic->ip4.pkts[idx] = m;
441 traffic->ip4.data[idx] = rte_pktmbuf_mtod_offset(m,
442 uint8_t *, offsetof(struct ip, ip_p));
443 } else if (ip->ip_v == IP6_VERSION) {
444 idx = traffic->ip6.num++;
445 traffic->ip6.pkts[idx] = m;
446 traffic->ip6.data[idx] = rte_pktmbuf_mtod_offset(m,
448 offsetof(struct ip6_hdr, ip6_nxt));
453 inbound_sp_sa(ipsec_ctx->sp4_ctx, ipsec_ctx->sa_ctx, &traffic->ip4,
456 inbound_sp_sa(ipsec_ctx->sp6_ctx, ipsec_ctx->sa_ctx, &traffic->ip6,
461 outbound_sp(struct sp_ctx *sp, struct traffic_type *ip,
462 struct traffic_type *ipsec)
465 uint32_t i, j, sa_idx;
467 if (ip->num == 0 || sp == NULL)
470 rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
471 ip->num, DEFAULT_MAX_CATEGORIES);
474 for (i = 0; i < ip->num; i++) {
476 sa_idx = ip->res[i] & PROTECT_MASK;
477 if ((ip->res[i] == 0) || (ip->res[i] & DISCARD))
479 else if (sa_idx != 0) {
480 ipsec->res[ipsec->num] = sa_idx;
481 ipsec->pkts[ipsec->num++] = m;
489 process_pkts_outbound(struct ipsec_ctx *ipsec_ctx,
490 struct ipsec_traffic *traffic)
493 uint16_t idx, nb_pkts_out, i;
495 /* Drop any IPsec traffic from protected ports */
496 for (i = 0; i < traffic->ipsec.num; i++)
497 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
499 traffic->ipsec.num = 0;
501 outbound_sp(ipsec_ctx->sp4_ctx, &traffic->ip4, &traffic->ipsec);
503 outbound_sp(ipsec_ctx->sp6_ctx, &traffic->ip6, &traffic->ipsec);
505 nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ipsec.pkts,
506 traffic->ipsec.res, traffic->ipsec.num,
509 for (i = 0; i < nb_pkts_out; i++) {
510 m = traffic->ipsec.pkts[i];
511 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
512 if (ip->ip_v == IPVERSION) {
513 idx = traffic->ip4.num++;
514 traffic->ip4.pkts[idx] = m;
516 idx = traffic->ip6.num++;
517 traffic->ip6.pkts[idx] = m;
523 process_pkts_inbound_nosp(struct ipsec_ctx *ipsec_ctx,
524 struct ipsec_traffic *traffic)
527 uint32_t nb_pkts_in, i, idx;
529 /* Drop any IPv4 traffic from unprotected ports */
530 for (i = 0; i < traffic->ip4.num; i++)
531 rte_pktmbuf_free(traffic->ip4.pkts[i]);
533 traffic->ip4.num = 0;
535 /* Drop any IPv6 traffic from unprotected ports */
536 for (i = 0; i < traffic->ip6.num; i++)
537 rte_pktmbuf_free(traffic->ip6.pkts[i]);
539 traffic->ip6.num = 0;
541 nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
542 traffic->ipsec.num, MAX_PKT_BURST);
544 for (i = 0; i < nb_pkts_in; i++) {
545 m = traffic->ipsec.pkts[i];
546 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
547 if (ip->ip_v == IPVERSION) {
548 idx = traffic->ip4.num++;
549 traffic->ip4.pkts[idx] = m;
551 idx = traffic->ip6.num++;
552 traffic->ip6.pkts[idx] = m;
558 process_pkts_outbound_nosp(struct ipsec_ctx *ipsec_ctx,
559 struct ipsec_traffic *traffic)
562 uint32_t nb_pkts_out, i;
565 /* Drop any IPsec traffic from protected ports */
566 for (i = 0; i < traffic->ipsec.num; i++)
567 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
569 traffic->ipsec.num = 0;
571 for (i = 0; i < traffic->ip4.num; i++)
572 traffic->ip4.res[i] = single_sa_idx;
574 for (i = 0; i < traffic->ip6.num; i++)
575 traffic->ip6.res[i] = single_sa_idx;
577 nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ip4.pkts,
578 traffic->ip4.res, traffic->ip4.num,
581 /* They all sue the same SA (ip4 or ip6 tunnel) */
582 m = traffic->ipsec.pkts[i];
583 ip = rte_pktmbuf_mtod(m, struct ip *);
584 if (ip->ip_v == IPVERSION)
585 traffic->ip4.num = nb_pkts_out;
587 traffic->ip6.num = nb_pkts_out;
591 route4_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
593 uint32_t hop[MAX_PKT_BURST * 2];
594 uint32_t dst_ip[MAX_PKT_BURST * 2];
600 for (i = 0; i < nb_pkts; i++) {
601 offset = offsetof(struct ip, ip_dst);
602 dst_ip[i] = *rte_pktmbuf_mtod_offset(pkts[i],
604 dst_ip[i] = rte_be_to_cpu_32(dst_ip[i]);
607 rte_lpm_lookup_bulk((struct rte_lpm *)rt_ctx, dst_ip, hop, nb_pkts);
609 for (i = 0; i < nb_pkts; i++) {
610 if ((hop[i] & RTE_LPM_LOOKUP_SUCCESS) == 0) {
611 rte_pktmbuf_free(pkts[i]);
614 send_single_packet(pkts[i], hop[i] & 0xff);
619 route6_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
621 int32_t hop[MAX_PKT_BURST * 2];
622 uint8_t dst_ip[MAX_PKT_BURST * 2][16];
629 for (i = 0; i < nb_pkts; i++) {
630 offset = offsetof(struct ip6_hdr, ip6_dst);
631 ip6_dst = rte_pktmbuf_mtod_offset(pkts[i], uint8_t *, offset);
632 memcpy(&dst_ip[i][0], ip6_dst, 16);
635 rte_lpm6_lookup_bulk_func((struct rte_lpm6 *)rt_ctx, dst_ip,
638 for (i = 0; i < nb_pkts; i++) {
640 rte_pktmbuf_free(pkts[i]);
643 send_single_packet(pkts[i], hop[i] & 0xff);
648 process_pkts(struct lcore_conf *qconf, struct rte_mbuf **pkts,
649 uint8_t nb_pkts, uint16_t portid)
651 struct ipsec_traffic traffic;
653 prepare_traffic(pkts, &traffic, nb_pkts);
655 if (unlikely(single_sa)) {
656 if (UNPROTECTED_PORT(portid))
657 process_pkts_inbound_nosp(&qconf->inbound, &traffic);
659 process_pkts_outbound_nosp(&qconf->outbound, &traffic);
661 if (UNPROTECTED_PORT(portid))
662 process_pkts_inbound(&qconf->inbound, &traffic);
664 process_pkts_outbound(&qconf->outbound, &traffic);
667 route4_pkts(qconf->rt4_ctx, traffic.ip4.pkts, traffic.ip4.num);
668 route6_pkts(qconf->rt6_ctx, traffic.ip6.pkts, traffic.ip6.num);
672 drain_buffers(struct lcore_conf *qconf)
677 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
678 buf = &qconf->tx_mbufs[portid];
681 send_burst(qconf, buf->len, portid);
686 /* main processing loop */
688 main_loop(__attribute__((unused)) void *dummy)
690 struct rte_mbuf *pkts[MAX_PKT_BURST];
692 uint64_t prev_tsc, diff_tsc, cur_tsc;
696 struct lcore_conf *qconf;
698 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
699 / US_PER_S * BURST_TX_DRAIN_US;
700 struct lcore_rx_queue *rxql;
703 lcore_id = rte_lcore_id();
704 qconf = &lcore_conf[lcore_id];
705 rxql = qconf->rx_queue_list;
706 socket_id = rte_lcore_to_socket_id(lcore_id);
708 qconf->rt4_ctx = socket_ctx[socket_id].rt_ip4;
709 qconf->rt6_ctx = socket_ctx[socket_id].rt_ip6;
710 qconf->inbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_in;
711 qconf->inbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_in;
712 qconf->inbound.sa_ctx = socket_ctx[socket_id].sa_in;
713 qconf->inbound.cdev_map = cdev_map_in;
714 qconf->inbound.session_pool = socket_ctx[socket_id].session_pool;
715 qconf->outbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_out;
716 qconf->outbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_out;
717 qconf->outbound.sa_ctx = socket_ctx[socket_id].sa_out;
718 qconf->outbound.cdev_map = cdev_map_out;
719 qconf->outbound.session_pool = socket_ctx[socket_id].session_pool;
721 if (qconf->nb_rx_queue == 0) {
722 RTE_LOG(INFO, IPSEC, "lcore %u has nothing to do\n", lcore_id);
726 RTE_LOG(INFO, IPSEC, "entering main loop on lcore %u\n", lcore_id);
728 for (i = 0; i < qconf->nb_rx_queue; i++) {
729 portid = rxql[i].port_id;
730 queueid = rxql[i].queue_id;
732 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
733 lcore_id, portid, queueid);
737 cur_tsc = rte_rdtsc();
739 /* TX queue buffer drain */
740 diff_tsc = cur_tsc - prev_tsc;
742 if (unlikely(diff_tsc > drain_tsc)) {
743 drain_buffers(qconf);
747 /* Read packet from RX queues */
748 for (i = 0; i < qconf->nb_rx_queue; ++i) {
749 portid = rxql[i].port_id;
750 queueid = rxql[i].queue_id;
751 nb_rx = rte_eth_rx_burst(portid, queueid,
752 pkts, MAX_PKT_BURST);
755 process_pkts(qconf, pkts, nb_rx, portid);
764 uint16_t portid, nb_ports;
768 if (lcore_params == NULL) {
769 printf("Error: No port/queue/core mappings\n");
773 nb_ports = rte_eth_dev_count();
775 for (i = 0; i < nb_lcore_params; ++i) {
776 lcore = lcore_params[i].lcore_id;
777 if (!rte_lcore_is_enabled(lcore)) {
778 printf("error: lcore %hhu is not enabled in "
779 "lcore mask\n", lcore);
782 socket_id = rte_lcore_to_socket_id(lcore);
783 if (socket_id != 0 && numa_on == 0) {
784 printf("warning: lcore %hhu is on socket %d "
788 portid = lcore_params[i].port_id;
789 if ((enabled_port_mask & (1 << portid)) == 0) {
790 printf("port %u is not enabled in port mask\n", portid);
793 if (portid >= nb_ports) {
794 printf("port %u is not present on the board\n", portid);
802 get_port_nb_rx_queues(const uint16_t port)
807 for (i = 0; i < nb_lcore_params; ++i) {
808 if (lcore_params[i].port_id == port &&
809 lcore_params[i].queue_id > queue)
810 queue = lcore_params[i].queue_id;
812 return (uint8_t)(++queue);
816 init_lcore_rx_queues(void)
818 uint16_t i, nb_rx_queue;
821 for (i = 0; i < nb_lcore_params; ++i) {
822 lcore = lcore_params[i].lcore_id;
823 nb_rx_queue = lcore_conf[lcore].nb_rx_queue;
824 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
825 printf("error: too many queues (%u) for lcore: %u\n",
826 nb_rx_queue + 1, lcore);
829 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
830 lcore_params[i].port_id;
831 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
832 lcore_params[i].queue_id;
833 lcore_conf[lcore].nb_rx_queue++;
840 print_usage(const char *prgname)
842 printf("%s [EAL options] -- -p PORTMASK -P -u PORTMASK"
843 " --"OPTION_CONFIG" (port,queue,lcore)[,(port,queue,lcore]"
844 " --single-sa SAIDX -f CONFIG_FILE\n"
845 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
846 " -P : enable promiscuous mode\n"
847 " -u PORTMASK: hexadecimal bitmask of unprotected ports\n"
848 " --"OPTION_CONFIG": (port,queue,lcore): "
849 "rx queues configuration\n"
850 " --single-sa SAIDX: use single SA index for outbound, "
852 " -f CONFIG_FILE: Configuration file path\n",
857 parse_portmask(const char *portmask)
862 /* parse hexadecimal string */
863 pm = strtoul(portmask, &end, 16);
864 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
867 if ((pm == 0) && errno)
874 parse_decimal(const char *str)
879 num = strtoul(str, &end, 10);
880 if ((str[0] == '\0') || (end == NULL) || (*end != '\0'))
887 parse_config(const char *q_arg)
890 const char *p, *p0 = q_arg;
898 unsigned long int_fld[_NUM_FLD];
899 char *str_fld[_NUM_FLD];
905 while ((p = strchr(p0, '(')) != NULL) {
912 if (size >= sizeof(s))
915 snprintf(s, sizeof(s), "%.*s", size, p);
916 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
919 for (i = 0; i < _NUM_FLD; i++) {
921 int_fld[i] = strtoul(str_fld[i], &end, 0);
922 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
925 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
926 printf("exceeded max number of lcore params: %hu\n",
930 lcore_params_array[nb_lcore_params].port_id =
931 (uint8_t)int_fld[FLD_PORT];
932 lcore_params_array[nb_lcore_params].queue_id =
933 (uint8_t)int_fld[FLD_QUEUE];
934 lcore_params_array[nb_lcore_params].lcore_id =
935 (uint8_t)int_fld[FLD_LCORE];
938 lcore_params = lcore_params_array;
942 #define __STRNCMP(name, opt) (!strncmp(name, opt, sizeof(opt)))
944 parse_args_long_options(struct option *lgopts, int32_t option_index)
947 const char *optname = lgopts[option_index].name;
949 if (__STRNCMP(optname, OPTION_CONFIG)) {
950 ret = parse_config(optarg);
952 printf("invalid config\n");
955 if (__STRNCMP(optname, OPTION_SINGLE_SA)) {
956 ret = parse_decimal(optarg);
960 printf("Configured with single SA index %u\n",
971 parse_args(int32_t argc, char **argv)
975 int32_t option_index;
976 char *prgname = argv[0];
977 static struct option lgopts[] = {
978 {OPTION_CONFIG, 1, 0, 0},
979 {OPTION_SINGLE_SA, 1, 0, 0},
982 int32_t f_present = 0;
986 while ((opt = getopt_long(argc, argvopt, "p:Pu:f:",
987 lgopts, &option_index)) != EOF) {
991 enabled_port_mask = parse_portmask(optarg);
992 if (enabled_port_mask == 0) {
993 printf("invalid portmask\n");
994 print_usage(prgname);
999 printf("Promiscuous mode selected\n");
1003 unprotected_port_mask = parse_portmask(optarg);
1004 if (unprotected_port_mask == 0) {
1005 printf("invalid unprotected portmask\n");
1006 print_usage(prgname);
1011 if (f_present == 1) {
1012 printf("\"-f\" option present more than "
1014 print_usage(prgname);
1017 if (parse_cfg_file(optarg) < 0) {
1018 printf("parsing file \"%s\" failed\n",
1020 print_usage(prgname);
1026 if (parse_args_long_options(lgopts, option_index)) {
1027 print_usage(prgname);
1032 print_usage(prgname);
1037 if (f_present == 0) {
1038 printf("Mandatory option \"-f\" not present\n");
1043 argv[optind-1] = prgname;
1046 optind = 1; /* reset getopt lib */
1051 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
1053 char buf[ETHER_ADDR_FMT_SIZE];
1054 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
1055 printf("%s%s", name, buf);
1058 /* Check the link status of all ports in up to 9s, and print them finally */
1060 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
1062 #define CHECK_INTERVAL 100 /* 100ms */
1063 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1065 uint8_t count, all_ports_up, print_flag = 0;
1066 struct rte_eth_link link;
1068 printf("\nChecking link status");
1070 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1072 for (portid = 0; portid < port_num; portid++) {
1073 if ((port_mask & (1 << portid)) == 0)
1075 memset(&link, 0, sizeof(link));
1076 rte_eth_link_get_nowait(portid, &link);
1077 /* print link status if flag set */
1078 if (print_flag == 1) {
1079 if (link.link_status)
1081 "Port%d Link Up - speed %u Mbps -%s\n",
1082 portid, link.link_speed,
1083 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1084 ("full-duplex") : ("half-duplex\n"));
1086 printf("Port %d Link Down\n", portid);
1089 /* clear all_ports_up flag if any link down */
1090 if (link.link_status == ETH_LINK_DOWN) {
1095 /* after finally printing all link status, get out */
1096 if (print_flag == 1)
1099 if (all_ports_up == 0) {
1102 rte_delay_ms(CHECK_INTERVAL);
1105 /* set the print_flag if all ports up or timeout */
1106 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1114 add_mapping(struct rte_hash *map, const char *str, uint16_t cdev_id,
1115 uint16_t qp, struct lcore_params *params,
1116 struct ipsec_ctx *ipsec_ctx,
1117 const struct rte_cryptodev_capabilities *cipher,
1118 const struct rte_cryptodev_capabilities *auth)
1122 struct cdev_key key = { 0 };
1124 key.lcore_id = params->lcore_id;
1126 key.cipher_algo = cipher->sym.cipher.algo;
1128 key.auth_algo = auth->sym.auth.algo;
1130 ret = rte_hash_lookup(map, &key);
1134 for (i = 0; i < ipsec_ctx->nb_qps; i++)
1135 if (ipsec_ctx->tbl[i].id == cdev_id)
1138 if (i == ipsec_ctx->nb_qps) {
1139 if (ipsec_ctx->nb_qps == MAX_QP_PER_LCORE) {
1140 printf("Maximum number of crypto devices assigned to "
1141 "a core, increase MAX_QP_PER_LCORE value\n");
1144 ipsec_ctx->tbl[i].id = cdev_id;
1145 ipsec_ctx->tbl[i].qp = qp;
1146 ipsec_ctx->nb_qps++;
1147 printf("%s cdev mapping: lcore %u using cdev %u qp %u "
1148 "(cdev_id_qp %lu)\n", str, key.lcore_id,
1152 ret = rte_hash_add_key_data(map, &key, (void *)i);
1154 printf("Faled to insert cdev mapping for (lcore %u, "
1155 "cdev %u, qp %u), errno %d\n",
1156 key.lcore_id, ipsec_ctx->tbl[i].id,
1157 ipsec_ctx->tbl[i].qp, ret);
1165 add_cdev_mapping(struct rte_cryptodev_info *dev_info, uint16_t cdev_id,
1166 uint16_t qp, struct lcore_params *params)
1169 const struct rte_cryptodev_capabilities *i, *j;
1170 struct rte_hash *map;
1171 struct lcore_conf *qconf;
1172 struct ipsec_ctx *ipsec_ctx;
1175 qconf = &lcore_conf[params->lcore_id];
1177 if ((unprotected_port_mask & (1 << params->port_id)) == 0) {
1179 ipsec_ctx = &qconf->outbound;
1183 ipsec_ctx = &qconf->inbound;
1187 /* Required cryptodevs with operation chainning */
1188 if (!(dev_info->feature_flags &
1189 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING))
1192 for (i = dev_info->capabilities;
1193 i->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; i++) {
1194 if (i->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1197 if (i->sym.xform_type != RTE_CRYPTO_SYM_XFORM_CIPHER)
1200 for (j = dev_info->capabilities;
1201 j->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; j++) {
1202 if (j->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1205 if (j->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH)
1208 ret |= add_mapping(map, str, cdev_id, qp, params,
1217 cryptodevs_init(void)
1219 struct rte_cryptodev_config dev_conf;
1220 struct rte_cryptodev_qp_conf qp_conf;
1221 uint16_t idx, max_nb_qps, qp, i;
1223 struct rte_hash_parameters params = { 0 };
1225 params.entries = CDEV_MAP_ENTRIES;
1226 params.key_len = sizeof(struct cdev_key);
1227 params.hash_func = rte_jhash;
1228 params.hash_func_init_val = 0;
1229 params.socket_id = rte_socket_id();
1231 params.name = "cdev_map_in";
1232 cdev_map_in = rte_hash_create(¶ms);
1233 if (cdev_map_in == NULL)
1234 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1237 params.name = "cdev_map_out";
1238 cdev_map_out = rte_hash_create(¶ms);
1239 if (cdev_map_out == NULL)
1240 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1243 printf("lcore/cryptodev/qp mappings:\n");
1245 uint32_t max_sess_sz = 0, sess_sz;
1246 for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1247 sess_sz = rte_cryptodev_get_private_session_size(cdev_id);
1248 if (sess_sz > max_sess_sz)
1249 max_sess_sz = sess_sz;
1253 /* Start from last cdev id to give HW priority */
1254 for (cdev_id = rte_cryptodev_count() - 1; cdev_id >= 0; cdev_id--) {
1255 struct rte_cryptodev_info cdev_info;
1257 rte_cryptodev_info_get(cdev_id, &cdev_info);
1259 if (nb_lcore_params > cdev_info.max_nb_queue_pairs)
1260 max_nb_qps = cdev_info.max_nb_queue_pairs;
1262 max_nb_qps = nb_lcore_params;
1266 while (qp < max_nb_qps && i < nb_lcore_params) {
1267 if (add_cdev_mapping(&cdev_info, cdev_id, qp,
1268 &lcore_params[idx]))
1271 idx = idx % nb_lcore_params;
1278 dev_conf.socket_id = rte_cryptodev_socket_id(cdev_id);
1279 dev_conf.nb_queue_pairs = qp;
1281 if (!socket_ctx[dev_conf.socket_id].session_pool) {
1282 char mp_name[RTE_MEMPOOL_NAMESIZE];
1283 struct rte_mempool *sess_mp;
1285 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1286 "sess_mp_%u", dev_conf.socket_id);
1287 sess_mp = rte_mempool_create(mp_name,
1291 0, NULL, NULL, NULL,
1292 NULL, dev_conf.socket_id,
1294 if (sess_mp == NULL)
1295 rte_exit(EXIT_FAILURE,
1296 "Cannot create session pool on socket %d\n",
1297 dev_conf.socket_id);
1299 printf("Allocated session pool on socket %d\n",
1300 dev_conf.socket_id);
1301 socket_ctx[dev_conf.socket_id].session_pool = sess_mp;
1304 if (rte_cryptodev_configure(cdev_id, &dev_conf))
1305 rte_panic("Failed to initialize cryptodev %u\n",
1308 qp_conf.nb_descriptors = CDEV_QUEUE_DESC;
1309 for (qp = 0; qp < dev_conf.nb_queue_pairs; qp++)
1310 if (rte_cryptodev_queue_pair_setup(cdev_id, qp,
1311 &qp_conf, dev_conf.socket_id,
1312 socket_ctx[dev_conf.socket_id].session_pool))
1313 rte_panic("Failed to setup queue %u for "
1314 "cdev_id %u\n", 0, cdev_id);
1316 if (rte_cryptodev_start(cdev_id))
1317 rte_panic("Failed to start cryptodev %u\n",
1327 port_init(uint16_t portid)
1329 struct rte_eth_dev_info dev_info;
1330 struct rte_eth_txconf *txconf;
1331 uint16_t nb_tx_queue, nb_rx_queue;
1332 uint16_t tx_queueid, rx_queueid, queue, lcore_id;
1333 int32_t ret, socket_id;
1334 struct lcore_conf *qconf;
1335 struct ether_addr ethaddr;
1337 rte_eth_dev_info_get(portid, &dev_info);
1339 printf("Configuring device port %u:\n", portid);
1341 rte_eth_macaddr_get(portid, ðaddr);
1342 ethaddr_tbl[portid].src = ETHADDR_TO_UINT64(ethaddr);
1343 print_ethaddr("Address: ", ðaddr);
1346 nb_rx_queue = get_port_nb_rx_queues(portid);
1347 nb_tx_queue = nb_lcores;
1349 if (nb_rx_queue > dev_info.max_rx_queues)
1350 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1351 "(max rx queue is %u)\n",
1352 nb_rx_queue, dev_info.max_rx_queues);
1354 if (nb_tx_queue > dev_info.max_tx_queues)
1355 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1356 "(max tx queue is %u)\n",
1357 nb_tx_queue, dev_info.max_tx_queues);
1359 printf("Creating queues: nb_rx_queue=%d nb_tx_queue=%u...\n",
1360 nb_rx_queue, nb_tx_queue);
1362 ret = rte_eth_dev_configure(portid, nb_rx_queue, nb_tx_queue,
1365 rte_exit(EXIT_FAILURE, "Cannot configure device: "
1366 "err=%d, port=%d\n", ret, portid);
1368 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
1370 rte_exit(EXIT_FAILURE, "Cannot adjust number of descriptors: "
1371 "err=%d, port=%d\n", ret, portid);
1373 /* init one TX queue per lcore */
1375 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1376 if (rte_lcore_is_enabled(lcore_id) == 0)
1380 socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1385 printf("Setup txq=%u,%d,%d\n", lcore_id, tx_queueid, socket_id);
1387 txconf = &dev_info.default_txconf;
1388 txconf->txq_flags = 0;
1390 ret = rte_eth_tx_queue_setup(portid, tx_queueid, nb_txd,
1393 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
1394 "err=%d, port=%d\n", ret, portid);
1396 qconf = &lcore_conf[lcore_id];
1397 qconf->tx_queue_id[portid] = tx_queueid;
1400 /* init RX queues */
1401 for (queue = 0; queue < qconf->nb_rx_queue; ++queue) {
1402 if (portid != qconf->rx_queue_list[queue].port_id)
1405 rx_queueid = qconf->rx_queue_list[queue].queue_id;
1407 printf("Setup rxq=%d,%d,%d\n", portid, rx_queueid,
1410 ret = rte_eth_rx_queue_setup(portid, rx_queueid,
1411 nb_rxd, socket_id, NULL,
1412 socket_ctx[socket_id].mbuf_pool);
1414 rte_exit(EXIT_FAILURE,
1415 "rte_eth_rx_queue_setup: err=%d, "
1416 "port=%d\n", ret, portid);
1423 pool_init(struct socket_ctx *ctx, int32_t socket_id, uint32_t nb_mbuf)
1427 snprintf(s, sizeof(s), "mbuf_pool_%d", socket_id);
1428 ctx->mbuf_pool = rte_pktmbuf_pool_create(s, nb_mbuf,
1429 MEMPOOL_CACHE_SIZE, ipsec_metadata_size(),
1430 RTE_MBUF_DEFAULT_BUF_SIZE,
1432 if (ctx->mbuf_pool == NULL)
1433 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n",
1436 printf("Allocated mbuf pool on socket %d\n", socket_id);
1440 main(int32_t argc, char **argv)
1445 uint16_t portid, nb_ports;
1448 ret = rte_eal_init(argc, argv);
1450 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1454 /* parse application arguments (after the EAL ones) */
1455 ret = parse_args(argc, argv);
1457 rte_exit(EXIT_FAILURE, "Invalid parameters\n");
1459 if ((unprotected_port_mask & enabled_port_mask) !=
1460 unprotected_port_mask)
1461 rte_exit(EXIT_FAILURE, "Invalid unprotected portmask 0x%x\n",
1462 unprotected_port_mask);
1464 nb_ports = rte_eth_dev_count();
1466 if (check_params() < 0)
1467 rte_exit(EXIT_FAILURE, "check_params failed\n");
1469 ret = init_lcore_rx_queues();
1471 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
1473 nb_lcores = rte_lcore_count();
1475 /* Replicate each context per socket */
1476 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1477 if (rte_lcore_is_enabled(lcore_id) == 0)
1481 socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1485 if (socket_ctx[socket_id].mbuf_pool)
1488 sa_init(&socket_ctx[socket_id], socket_id);
1490 sp4_init(&socket_ctx[socket_id], socket_id);
1492 sp6_init(&socket_ctx[socket_id], socket_id);
1494 rt_init(&socket_ctx[socket_id], socket_id);
1496 pool_init(&socket_ctx[socket_id], socket_id, NB_MBUF);
1499 for (portid = 0; portid < nb_ports; portid++) {
1500 if ((enabled_port_mask & (1 << portid)) == 0)
1509 for (portid = 0; portid < nb_ports; portid++) {
1510 if ((enabled_port_mask & (1 << portid)) == 0)
1514 ret = rte_eth_dev_start(portid);
1516 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: "
1517 "err=%d, port=%d\n", ret, portid);
1519 * If enabled, put device in promiscuous mode.
1520 * This allows IO forwarding mode to forward packets
1521 * to itself through 2 cross-connected ports of the
1525 rte_eth_promiscuous_enable(portid);
1528 check_all_ports_link_status(nb_ports, enabled_port_mask);
1530 /* launch per-lcore init on every lcore */
1531 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1532 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1533 if (rte_eal_wait_lcore(lcore_id) < 0)