1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (C) 2020 Marvell International Ltd.
4 #ifndef _IPSEC_WORKER_H_
5 #define _IPSEC_WORKER_H_
8 #include <rte_ethdev.h>
14 /* Configure how many packets ahead to prefetch, when reading packets */
15 #define PREFETCH_OFFSET 3
16 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
19 PKT_TYPE_PLAIN_IPV4 = 1,
29 PKT_POSTED /* for lookaside case */
33 struct rt_ctx *rt4_ctx;
34 struct rt_ctx *rt6_ctx;
38 * Conf required by event mode worker with tx internal port
40 struct lcore_conf_ev_tx_int_port_wrkr {
41 struct ipsec_ctx inbound;
42 struct ipsec_ctx outbound;
43 struct route_table rt;
44 } __rte_cache_aligned;
46 void ipsec_poll_mode_worker(void);
47 void ipsec_poll_mode_wrkr_inl_pr(void);
48 void ipsec_poll_mode_wrkr_inl_pr_ss(void);
50 int ipsec_launch_one_lcore(void *args);
53 * helper routine for inline and cpu(synchronous) processing
54 * this is just to satisfy inbound_sa_check() and get_hop_for_offload_pkt().
55 * Should be removed in future.
58 prep_process_group(void *sa, struct rte_mbuf *mb[], uint32_t cnt)
61 struct ipsec_mbuf_metadata *priv;
63 for (j = 0; j != cnt; j++) {
64 priv = get_priv(mb[j]);
66 /* setup TSO related fields if TSO enabled*/
68 uint32_t ptype = mb[j]->packet_type;
69 /* only TCP is supported */
70 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP) {
71 mb[j]->tso_segsz = priv->sa->mss;
72 if ((IS_TUNNEL(priv->sa->flags))) {
73 mb[j]->outer_l3_len = mb[j]->l3_len;
74 mb[j]->outer_l2_len = mb[j]->l2_len;
76 RTE_MBUF_F_TX_TUNNEL_ESP;
77 if (RTE_ETH_IS_IPV4_HDR(ptype))
79 RTE_MBUF_F_TX_OUTER_IP_CKSUM;
81 mb[j]->l4_len = sizeof(struct rte_tcp_hdr);
82 mb[j]->ol_flags |= (RTE_MBUF_F_TX_TCP_SEG |
83 RTE_MBUF_F_TX_TCP_CKSUM);
84 if (RTE_ETH_IS_IPV4_HDR(ptype))
86 RTE_MBUF_F_TX_OUTER_IPV4;
89 RTE_MBUF_F_TX_OUTER_IPV6;
95 static __rte_always_inline void
96 adjust_ipv4_pktlen(struct rte_mbuf *m, const struct rte_ipv4_hdr *iph,
101 plen = rte_be_to_cpu_16(iph->total_length) + l2_len;
102 if (plen < m->pkt_len) {
103 trim = m->pkt_len - plen;
104 rte_pktmbuf_trim(m, trim);
108 static __rte_always_inline void
109 adjust_ipv6_pktlen(struct rte_mbuf *m, const struct rte_ipv6_hdr *iph,
114 plen = rte_be_to_cpu_16(iph->payload_len) + sizeof(*iph) + l2_len;
115 if (plen < m->pkt_len) {
116 trim = m->pkt_len - plen;
117 rte_pktmbuf_trim(m, trim);
121 static __rte_always_inline void
122 prepare_one_packet(struct rte_security_ctx *ctx, struct rte_mbuf *pkt,
123 struct ipsec_traffic *t)
125 uint32_t ptype = pkt->packet_type;
126 const struct rte_ether_hdr *eth;
127 const struct rte_ipv4_hdr *iph4;
128 const struct rte_ipv6_hdr *iph6;
129 uint32_t tun_type, l3_type;
133 tun_type = ptype & RTE_PTYPE_TUNNEL_MASK;
134 l3_type = ptype & RTE_PTYPE_L3_MASK;
136 eth = rte_pktmbuf_mtod(pkt, const struct rte_ether_hdr *);
137 if (RTE_ETH_IS_IPV4_HDR(l3_type)) {
138 iph4 = (const struct rte_ipv4_hdr *)rte_pktmbuf_adj(pkt,
140 adjust_ipv4_pktlen(pkt, iph4, 0);
142 if (tun_type == RTE_PTYPE_TUNNEL_ESP) {
143 t->ipsec.pkts[(t->ipsec.num)++] = pkt;
145 t->ip4.data[t->ip4.num] = &iph4->next_proto_id;
146 t->ip4.pkts[(t->ip4.num)++] = pkt;
148 tx_offload = sizeof(*iph4) << RTE_MBUF_L2_LEN_BITS;
149 } else if (RTE_ETH_IS_IPV6_HDR(l3_type)) {
154 /* get protocol type */
155 iph6 = (const struct rte_ipv6_hdr *)rte_pktmbuf_adj(pkt,
157 adjust_ipv6_pktlen(pkt, iph6, 0);
159 l3len = sizeof(struct ip6_hdr);
161 if (tun_type == RTE_PTYPE_TUNNEL_ESP) {
162 t->ipsec.pkts[(t->ipsec.num)++] = pkt;
164 t->ip6.data[t->ip6.num] = &iph6->proto;
165 t->ip6.pkts[(t->ip6.num)++] = pkt;
168 /* Determine l3 header size up to ESP extension by walking
169 * through extension headers.
171 if (l3_type == RTE_PTYPE_L3_IPV6_EXT ||
172 l3_type == RTE_PTYPE_L3_IPV6_EXT_UNKNOWN) {
173 p = rte_pktmbuf_mtod(pkt, uint8_t *);
174 next_proto = iph6->proto;
175 while (next_proto != IPPROTO_ESP &&
176 l3len < pkt->data_len &&
177 (next_proto = rte_ipv6_get_next_ext(p + l3len,
178 next_proto, &ext_len)) >= 0)
181 /* Drop pkt when IPv6 header exceeds first seg size */
182 if (unlikely(l3len > pkt->data_len)) {
187 tx_offload = l3len << RTE_MBUF_L2_LEN_BITS;
189 /* Unknown/Unsupported type, drop the packet */
190 RTE_LOG(ERR, IPSEC, "Unsupported packet type 0x%x\n",
191 rte_be_to_cpu_16(eth->ether_type));
196 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP)
197 tx_offload |= (sizeof(struct rte_tcp_hdr) <<
198 (RTE_MBUF_L2_LEN_BITS + RTE_MBUF_L3_LEN_BITS));
199 else if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP)
200 tx_offload |= (sizeof(struct rte_udp_hdr) <<
201 (RTE_MBUF_L2_LEN_BITS + RTE_MBUF_L3_LEN_BITS));
202 pkt->tx_offload = tx_offload;
204 /* Check if the packet has been processed inline. For inline protocol
205 * processed packets, the metadata in the mbuf can be used to identify
206 * the security processing done on the packet. The metadata will be
207 * used to retrieve the application registered userdata associated
208 * with the security session.
211 if (ctx && pkt->ol_flags & RTE_MBUF_F_RX_SEC_OFFLOAD) {
213 struct ipsec_mbuf_metadata *priv;
215 /* Retrieve the userdata registered. Here, the userdata
216 * registered is the SA pointer.
218 sa = (struct ipsec_sa *)rte_security_get_userdata(ctx,
219 *rte_security_dynfield(pkt));
221 /* userdata could not be retrieved */
225 /* Save SA as priv member in mbuf. This will be used in the
226 * IPsec selector(SP-SA) check.
229 priv = get_priv(pkt);
234 static __rte_always_inline void
235 prepare_traffic(struct rte_security_ctx *ctx, struct rte_mbuf **pkts,
236 struct ipsec_traffic *t, uint16_t nb_pkts)
244 for (i = 0; i < (nb_pkts - PREFETCH_OFFSET); i++) {
245 rte_prefetch0(rte_pktmbuf_mtod(pkts[i + PREFETCH_OFFSET],
247 prepare_one_packet(ctx, pkts[i], t);
249 /* Process left packets */
250 for (; i < nb_pkts; i++)
251 prepare_one_packet(ctx, pkts[i], t);
254 /* Send burst of packets on an output interface */
255 static __rte_always_inline int32_t
256 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
258 struct rte_mbuf **m_table;
262 queueid = qconf->tx_queue_id[port];
263 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
265 ret = rte_eth_tx_burst(port, queueid, m_table, n);
267 core_stats_update_tx(ret);
269 if (unlikely(ret < n)) {
271 free_pkts(&m_table[ret], 1);
279 * Helper function to fragment and queue for TX one packet.
281 static __rte_always_inline uint32_t
282 send_fragment_packet(struct lcore_conf *qconf, struct rte_mbuf *m,
283 uint16_t port, uint8_t proto)
285 struct rte_ether_hdr *ethhdr;
286 struct rte_ipv4_hdr *ip;
287 struct rte_mbuf *pkt;
292 tbl = qconf->tx_mbufs + port;
295 /* free space for new fragments */
296 if (len + RTE_LIBRTE_IP_FRAG_MAX_FRAG >= RTE_DIM(tbl->m_table)) {
297 send_burst(qconf, len, port);
301 n = RTE_DIM(tbl->m_table) - len;
303 /* Strip the ethernet header that was prepended earlier */
304 rte_pktmbuf_adj(m, RTE_ETHER_HDR_LEN);
306 if (proto == IPPROTO_IP)
307 rc = rte_ipv4_fragment_packet(m, tbl->m_table + len,
308 n, mtu_size, m->pool, qconf->frag.pool_indir);
310 rc = rte_ipv6_fragment_packet(m, tbl->m_table + len,
311 n, mtu_size, m->pool, qconf->frag.pool_indir);
315 "%s: failed to fragment packet with size %u, "
317 __func__, m->pkt_len, rte_errno);
323 for (; i < len; i++) {
324 pkt = tbl->m_table[i];
326 /* Update Ethernet header */
327 ethhdr = (struct rte_ether_hdr *)
328 rte_pktmbuf_prepend(pkt, RTE_ETHER_HDR_LEN);
329 pkt->l2_len = RTE_ETHER_HDR_LEN;
331 if (proto == IPPROTO_IP) {
333 rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
334 /* Update minimum offload data */
335 pkt->l3_len = sizeof(struct rte_ipv4_hdr);
336 pkt->ol_flags |= qconf->outbound.ipv4_offloads;
338 ip = (struct rte_ipv4_hdr *)(ethhdr + 1);
339 ip->hdr_checksum = 0;
341 /* calculate IPv4 cksum in SW */
342 if ((pkt->ol_flags & RTE_MBUF_F_TX_IP_CKSUM) == 0)
343 ip->hdr_checksum = rte_ipv4_cksum(ip);
346 rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
348 /* Update minimum offload data */
349 pkt->l3_len = sizeof(struct rte_ipv6_hdr);
350 pkt->ol_flags |= qconf->outbound.ipv6_offloads;
353 memcpy(ðhdr->src_addr, ðaddr_tbl[port].src,
354 sizeof(struct rte_ether_addr));
355 memcpy(ðhdr->dst_addr, ðaddr_tbl[port].dst,
356 sizeof(struct rte_ether_addr));
363 /* Enqueue a single packet, and send burst if queue is filled */
364 static __rte_always_inline int32_t
365 send_single_packet(struct rte_mbuf *m, uint16_t port, uint8_t proto)
369 struct lcore_conf *qconf;
371 lcore_id = rte_lcore_id();
373 qconf = &lcore_conf[lcore_id];
374 len = qconf->tx_mbufs[port].len;
376 /* L2 header is already part of packet */
377 if (m->pkt_len - RTE_ETHER_HDR_LEN <= mtu_size) {
378 qconf->tx_mbufs[port].m_table[len] = m;
381 /* need to fragment the packet */
382 } else if (frag_tbl_sz > 0)
383 len = send_fragment_packet(qconf, m, port, proto);
387 /* enough pkts to be sent */
388 if (unlikely(len == MAX_PKT_BURST)) {
389 send_burst(qconf, MAX_PKT_BURST, port);
393 qconf->tx_mbufs[port].len = len;
397 static __rte_always_inline void
398 inbound_sp_sa(struct sp_ctx *sp, struct sa_ctx *sa, struct traffic_type *ip,
399 uint16_t lim, struct ipsec_spd_stats *stats)
402 uint32_t i, j, res, sa_idx;
404 if (ip->num == 0 || sp == NULL)
407 rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
408 ip->num, DEFAULT_MAX_CATEGORIES);
411 for (i = 0; i < ip->num; i++) {
419 if (res == DISCARD) {
425 /* Only check SPI match for processed IPSec packets */
426 if (i < lim && ((m->ol_flags & RTE_MBUF_F_RX_SEC_OFFLOAD) == 0)) {
433 if (!inbound_sa_check(sa, m, sa_idx)) {
444 static __rte_always_inline int32_t
445 get_hop_for_offload_pkt(struct rte_mbuf *pkt, int is_ipv6)
447 struct ipsec_mbuf_metadata *priv;
450 priv = get_priv(pkt);
453 if (unlikely(sa == NULL)) {
454 RTE_LOG(ERR, IPSEC, "SA not saved in private data\n");
462 return (sa->portid | RTE_LPM_LOOKUP_SUCCESS);
472 static __rte_always_inline void
473 route4_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[],
474 uint8_t nb_pkts, uint64_t tx_offloads, bool ip_cksum)
476 uint32_t hop[MAX_PKT_BURST * 2];
477 uint32_t dst_ip[MAX_PKT_BURST * 2];
478 struct rte_ether_hdr *ethhdr;
481 uint16_t lpm_pkts = 0;
482 unsigned int lcoreid = rte_lcore_id();
483 struct rte_mbuf *pkt;
489 /* Need to do an LPM lookup for non-inline packets. Inline packets will
490 * have port ID in the SA
493 for (i = 0; i < nb_pkts; i++) {
495 if (!(pkt->ol_flags & RTE_MBUF_F_TX_SEC_OFFLOAD)) {
496 /* Security offload not enabled. So an LPM lookup is
497 * required to get the hop
499 offset = offsetof(struct ip, ip_dst);
500 dst_ip[lpm_pkts] = *rte_pktmbuf_mtod_offset(pkt,
502 dst_ip[lpm_pkts] = rte_be_to_cpu_32(dst_ip[lpm_pkts]);
507 rte_lpm_lookup_bulk((struct rte_lpm *)rt_ctx, dst_ip, hop, lpm_pkts);
511 for (i = 0; i < nb_pkts; i++) {
513 if (pkt->ol_flags & RTE_MBUF_F_TX_SEC_OFFLOAD) {
514 /* Read hop from the SA */
515 pkt_hop = get_hop_for_offload_pkt(pkt, 0);
517 /* Need to use hop returned by lookup */
518 pkt_hop = hop[lpm_pkts++];
521 if ((pkt_hop & RTE_LPM_LOOKUP_SUCCESS) == 0) {
522 core_statistics[lcoreid].lpm4.miss++;
527 port = pkt_hop & 0xff;
529 /* Update minimum offload data */
530 pkt->l3_len = sizeof(struct rte_ipv4_hdr);
531 pkt->l2_len = RTE_ETHER_HDR_LEN;
532 pkt->ol_flags |= RTE_MBUF_F_TX_IPV4;
534 /* Update Ethernet header */
535 ethhdr = (struct rte_ether_hdr *)
536 rte_pktmbuf_prepend(pkt, RTE_ETHER_HDR_LEN);
539 struct rte_ipv4_hdr *ip;
541 pkt->ol_flags |= tx_offloads;
543 ip = (struct rte_ipv4_hdr *)(ethhdr + 1);
544 ip->hdr_checksum = 0;
546 /* calculate IPv4 cksum in SW */
547 if ((pkt->ol_flags & RTE_MBUF_F_TX_IP_CKSUM) == 0)
548 ip->hdr_checksum = rte_ipv4_cksum(ip);
551 ethhdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
552 memcpy(ðhdr->src_addr, ðaddr_tbl[port].src,
553 sizeof(struct rte_ether_addr));
554 memcpy(ðhdr->dst_addr, ðaddr_tbl[port].dst,
555 sizeof(struct rte_ether_addr));
557 send_single_packet(pkt, port, IPPROTO_IP);
561 static __rte_always_inline void
562 route6_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
564 int32_t hop[MAX_PKT_BURST * 2];
565 uint8_t dst_ip[MAX_PKT_BURST * 2][16];
566 struct rte_ether_hdr *ethhdr;
570 uint16_t lpm_pkts = 0;
571 unsigned int lcoreid = rte_lcore_id();
572 struct rte_mbuf *pkt;
578 /* Need to do an LPM lookup for non-inline packets. Inline packets will
579 * have port ID in the SA
582 for (i = 0; i < nb_pkts; i++) {
584 if (!(pkt->ol_flags & RTE_MBUF_F_TX_SEC_OFFLOAD)) {
585 /* Security offload not enabled. So an LPM lookup is
586 * required to get the hop
588 offset = offsetof(struct ip6_hdr, ip6_dst);
589 ip6_dst = rte_pktmbuf_mtod_offset(pkt, uint8_t *,
591 memcpy(&dst_ip[lpm_pkts][0], ip6_dst, 16);
596 rte_lpm6_lookup_bulk_func((struct rte_lpm6 *)rt_ctx, dst_ip, hop,
601 for (i = 0; i < nb_pkts; i++) {
603 if (pkt->ol_flags & RTE_MBUF_F_TX_SEC_OFFLOAD) {
604 /* Read hop from the SA */
605 pkt_hop = get_hop_for_offload_pkt(pkt, 1);
607 /* Need to use hop returned by lookup */
608 pkt_hop = hop[lpm_pkts++];
612 core_statistics[lcoreid].lpm6.miss++;
617 port = pkt_hop & 0xff;
619 /* Update minimum offload data */
620 pkt->ol_flags |= RTE_MBUF_F_TX_IPV6;
621 pkt->l3_len = sizeof(struct ip6_hdr);
622 pkt->l2_len = RTE_ETHER_HDR_LEN;
624 /* Update Ethernet header */
625 ethhdr = (struct rte_ether_hdr *)
626 rte_pktmbuf_prepend(pkt, RTE_ETHER_HDR_LEN);
628 ethhdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
629 memcpy(ðhdr->src_addr, ðaddr_tbl[port].src,
630 sizeof(struct rte_ether_addr));
631 memcpy(ðhdr->dst_addr, ðaddr_tbl[port].dst,
632 sizeof(struct rte_ether_addr));
634 send_single_packet(pkt, port, IPPROTO_IPV6);
638 static __rte_always_inline void
639 drain_tx_buffers(struct lcore_conf *qconf)
644 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
645 buf = &qconf->tx_mbufs[portid];
648 send_burst(qconf, buf->len, portid);
653 #endif /* _IPSEC_WORKER_H_ */