examples/ipsec-secgw: allow larger burst size for vectors
[dpdk.git] / examples / ipsec-secgw / ipsec_worker.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (C) 2020 Marvell International Ltd.
3  */
4 #ifndef _IPSEC_WORKER_H_
5 #define _IPSEC_WORKER_H_
6
7 #include <rte_acl.h>
8 #include <rte_ethdev.h>
9 #include <rte_lpm.h>
10 #include <rte_lpm6.h>
11
12 #include "ipsec.h"
13
14 /* Configure how many packets ahead to prefetch, when reading packets */
15 #define PREFETCH_OFFSET 3
16 enum pkt_type {
17         PKT_TYPE_PLAIN_IPV4 = 1,
18         PKT_TYPE_IPSEC_IPV4,
19         PKT_TYPE_PLAIN_IPV6,
20         PKT_TYPE_IPSEC_IPV6,
21         PKT_TYPE_INVALID
22 };
23
24 enum {
25         PKT_DROPPED = 0,
26         PKT_FORWARDED,
27         PKT_POSTED      /* for lookaside case */
28 };
29
30 struct route_table {
31         struct rt_ctx *rt4_ctx;
32         struct rt_ctx *rt6_ctx;
33 };
34
35 /*
36  * Conf required by event mode worker with tx internal port
37  */
38 struct lcore_conf_ev_tx_int_port_wrkr {
39         struct ipsec_ctx inbound;
40         struct ipsec_ctx outbound;
41         struct route_table rt;
42 } __rte_cache_aligned;
43
44 void ipsec_poll_mode_worker(void);
45
46 int ipsec_launch_one_lcore(void *args);
47
48 /*
49  * helper routine for inline and cpu(synchronous) processing
50  * this is just to satisfy inbound_sa_check() and get_hop_for_offload_pkt().
51  * Should be removed in future.
52  */
53 static inline void
54 prep_process_group(void *sa, struct rte_mbuf *mb[], uint32_t cnt)
55 {
56         uint32_t j;
57         struct ipsec_mbuf_metadata *priv;
58
59         for (j = 0; j != cnt; j++) {
60                 priv = get_priv(mb[j]);
61                 priv->sa = sa;
62                 /* setup TSO related fields if TSO enabled*/
63                 if (priv->sa->mss) {
64                         uint32_t ptype = mb[j]->packet_type;
65                         /* only TCP is supported */
66                         if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP) {
67                                 mb[j]->tso_segsz = priv->sa->mss;
68                                 if ((IS_TUNNEL(priv->sa->flags))) {
69                                         mb[j]->outer_l3_len = mb[j]->l3_len;
70                                         mb[j]->outer_l2_len = mb[j]->l2_len;
71                                         mb[j]->ol_flags |=
72                                                 RTE_MBUF_F_TX_TUNNEL_ESP;
73                                         if (RTE_ETH_IS_IPV4_HDR(ptype))
74                                                 mb[j]->ol_flags |=
75                                                 RTE_MBUF_F_TX_OUTER_IP_CKSUM;
76                                 }
77                                 mb[j]->l4_len = sizeof(struct rte_tcp_hdr);
78                                 mb[j]->ol_flags |= (RTE_MBUF_F_TX_TCP_SEG |
79                                                 RTE_MBUF_F_TX_TCP_CKSUM);
80                                 if (RTE_ETH_IS_IPV4_HDR(ptype))
81                                         mb[j]->ol_flags |=
82                                                 RTE_MBUF_F_TX_OUTER_IPV4;
83                                 else
84                                         mb[j]->ol_flags |=
85                                                 RTE_MBUF_F_TX_OUTER_IPV6;
86                         }
87                 }
88         }
89 }
90
91 static inline void
92 adjust_ipv4_pktlen(struct rte_mbuf *m, const struct rte_ipv4_hdr *iph,
93         uint32_t l2_len)
94 {
95         uint32_t plen, trim;
96
97         plen = rte_be_to_cpu_16(iph->total_length) + l2_len;
98         if (plen < m->pkt_len) {
99                 trim = m->pkt_len - plen;
100                 rte_pktmbuf_trim(m, trim);
101         }
102 }
103
104 static inline void
105 adjust_ipv6_pktlen(struct rte_mbuf *m, const struct rte_ipv6_hdr *iph,
106         uint32_t l2_len)
107 {
108         uint32_t plen, trim;
109
110         plen = rte_be_to_cpu_16(iph->payload_len) + sizeof(*iph) + l2_len;
111         if (plen < m->pkt_len) {
112                 trim = m->pkt_len - plen;
113                 rte_pktmbuf_trim(m, trim);
114         }
115 }
116
117 static inline void
118 prepare_one_packet(struct rte_mbuf *pkt, struct ipsec_traffic *t)
119 {
120         uint32_t ptype = pkt->packet_type;
121         const struct rte_ether_hdr *eth;
122         const struct rte_ipv4_hdr *iph4;
123         const struct rte_ipv6_hdr *iph6;
124         uint32_t tun_type, l3_type;
125         uint64_t tx_offload;
126         uint16_t l3len;
127
128         tun_type = ptype & RTE_PTYPE_TUNNEL_MASK;
129         l3_type = ptype & RTE_PTYPE_L3_MASK;
130
131         eth = rte_pktmbuf_mtod(pkt, const struct rte_ether_hdr *);
132         if (RTE_ETH_IS_IPV4_HDR(l3_type)) {
133                 iph4 = (const struct rte_ipv4_hdr *)rte_pktmbuf_adj(pkt,
134                         RTE_ETHER_HDR_LEN);
135                 adjust_ipv4_pktlen(pkt, iph4, 0);
136
137                 if (tun_type == RTE_PTYPE_TUNNEL_ESP) {
138                         t->ipsec.pkts[(t->ipsec.num)++] = pkt;
139                 } else {
140                         t->ip4.data[t->ip4.num] = &iph4->next_proto_id;
141                         t->ip4.pkts[(t->ip4.num)++] = pkt;
142                 }
143                 tx_offload = sizeof(*iph4) << RTE_MBUF_L2_LEN_BITS;
144         } else if (RTE_ETH_IS_IPV6_HDR(l3_type)) {
145                 int next_proto;
146                 size_t ext_len;
147                 uint8_t *p;
148
149                 /* get protocol type */
150                 iph6 = (const struct rte_ipv6_hdr *)rte_pktmbuf_adj(pkt,
151                         RTE_ETHER_HDR_LEN);
152                 adjust_ipv6_pktlen(pkt, iph6, 0);
153
154                 l3len = sizeof(struct ip6_hdr);
155
156                 if (tun_type == RTE_PTYPE_TUNNEL_ESP) {
157                         t->ipsec.pkts[(t->ipsec.num)++] = pkt;
158                 } else {
159                         t->ip6.data[t->ip6.num] = &iph6->proto;
160                         t->ip6.pkts[(t->ip6.num)++] = pkt;
161                 }
162
163                 /* Determine l3 header size up to ESP extension by walking
164                  * through extension headers.
165                  */
166                 if (l3_type == RTE_PTYPE_L3_IPV6_EXT ||
167                      l3_type == RTE_PTYPE_L3_IPV6_EXT_UNKNOWN) {
168                         p = rte_pktmbuf_mtod(pkt, uint8_t *);
169                         next_proto = iph6->proto;
170                         while (next_proto != IPPROTO_ESP &&
171                                l3len < pkt->data_len &&
172                                (next_proto = rte_ipv6_get_next_ext(p + l3len,
173                                                 next_proto, &ext_len)) >= 0)
174                                 l3len += ext_len;
175
176                         /* Drop pkt when IPv6 header exceeds first seg size */
177                         if (unlikely(l3len > pkt->data_len)) {
178                                 free_pkts(&pkt, 1);
179                                 return;
180                         }
181                 }
182                 tx_offload = l3len << RTE_MBUF_L2_LEN_BITS;
183         } else {
184                 /* Unknown/Unsupported type, drop the packet */
185                 RTE_LOG(ERR, IPSEC, "Unsupported packet type 0x%x\n",
186                         rte_be_to_cpu_16(eth->ether_type));
187                 free_pkts(&pkt, 1);
188                 return;
189         }
190
191         if  ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP)
192                 tx_offload |= (sizeof(struct rte_tcp_hdr) <<
193                                (RTE_MBUF_L2_LEN_BITS + RTE_MBUF_L3_LEN_BITS));
194         else if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP)
195                 tx_offload |= (sizeof(struct rte_udp_hdr) <<
196                                (RTE_MBUF_L2_LEN_BITS + RTE_MBUF_L3_LEN_BITS));
197         pkt->tx_offload = tx_offload;
198
199         /* Check if the packet has been processed inline. For inline protocol
200          * processed packets, the metadata in the mbuf can be used to identify
201          * the security processing done on the packet. The metadata will be
202          * used to retrieve the application registered userdata associated
203          * with the security session.
204          */
205
206         if (pkt->ol_flags & RTE_MBUF_F_RX_SEC_OFFLOAD &&
207                         rte_security_dynfield_is_registered()) {
208                 struct ipsec_sa *sa;
209                 struct ipsec_mbuf_metadata *priv;
210                 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
211                                                 rte_eth_dev_get_sec_ctx(
212                                                 pkt->port);
213
214                 /* Retrieve the userdata registered. Here, the userdata
215                  * registered is the SA pointer.
216                  */
217                 sa = (struct ipsec_sa *)rte_security_get_userdata(ctx,
218                                 *rte_security_dynfield(pkt));
219                 if (sa == NULL) {
220                         /* userdata could not be retrieved */
221                         return;
222                 }
223
224                 /* Save SA as priv member in mbuf. This will be used in the
225                  * IPsec selector(SP-SA) check.
226                  */
227
228                 priv = get_priv(pkt);
229                 priv->sa = sa;
230         }
231 }
232
233 static inline void
234 prepare_traffic(struct rte_mbuf **pkts, struct ipsec_traffic *t,
235                 uint16_t nb_pkts)
236 {
237         int32_t i;
238
239         t->ipsec.num = 0;
240         t->ip4.num = 0;
241         t->ip6.num = 0;
242
243         for (i = 0; i < (nb_pkts - PREFETCH_OFFSET); i++) {
244                 rte_prefetch0(rte_pktmbuf_mtod(pkts[i + PREFETCH_OFFSET],
245                                         void *));
246                 prepare_one_packet(pkts[i], t);
247         }
248         /* Process left packets */
249         for (; i < nb_pkts; i++)
250                 prepare_one_packet(pkts[i], t);
251 }
252
253 static inline void
254 prepare_tx_pkt(struct rte_mbuf *pkt, uint16_t port,
255                 const struct lcore_conf *qconf)
256 {
257         struct ip *ip;
258         struct rte_ether_hdr *ethhdr;
259
260         ip = rte_pktmbuf_mtod(pkt, struct ip *);
261
262         ethhdr = (struct rte_ether_hdr *)
263                 rte_pktmbuf_prepend(pkt, RTE_ETHER_HDR_LEN);
264
265         if (ip->ip_v == IPVERSION) {
266                 pkt->ol_flags |= qconf->outbound.ipv4_offloads;
267                 pkt->l3_len = sizeof(struct ip);
268                 pkt->l2_len = RTE_ETHER_HDR_LEN;
269
270                 ip->ip_sum = 0;
271
272                 /* calculate IPv4 cksum in SW */
273                 if ((pkt->ol_flags & RTE_MBUF_F_TX_IP_CKSUM) == 0)
274                         ip->ip_sum = rte_ipv4_cksum((struct rte_ipv4_hdr *)ip);
275
276                 ethhdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
277         } else {
278                 pkt->ol_flags |= qconf->outbound.ipv6_offloads;
279                 pkt->l3_len = sizeof(struct ip6_hdr);
280                 pkt->l2_len = RTE_ETHER_HDR_LEN;
281
282                 ethhdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
283         }
284
285         memcpy(&ethhdr->src_addr, &ethaddr_tbl[port].src,
286                         sizeof(struct rte_ether_addr));
287         memcpy(&ethhdr->dst_addr, &ethaddr_tbl[port].dst,
288                         sizeof(struct rte_ether_addr));
289 }
290
291 static inline void
292 prepare_tx_burst(struct rte_mbuf *pkts[], uint16_t nb_pkts, uint16_t port,
293                 const struct lcore_conf *qconf)
294 {
295         int32_t i;
296         const int32_t prefetch_offset = 2;
297
298         for (i = 0; i < (nb_pkts - prefetch_offset); i++) {
299                 rte_mbuf_prefetch_part2(pkts[i + prefetch_offset]);
300                 prepare_tx_pkt(pkts[i], port, qconf);
301         }
302         /* Process left packets */
303         for (; i < nb_pkts; i++)
304                 prepare_tx_pkt(pkts[i], port, qconf);
305 }
306
307 /* Send burst of packets on an output interface */
308 static inline int32_t
309 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
310 {
311         struct rte_mbuf **m_table;
312         int32_t ret;
313         uint16_t queueid;
314
315         queueid = qconf->tx_queue_id[port];
316         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
317
318         prepare_tx_burst(m_table, n, port, qconf);
319
320         ret = rte_eth_tx_burst(port, queueid, m_table, n);
321
322         core_stats_update_tx(ret);
323
324         if (unlikely(ret < n)) {
325                 do {
326                         free_pkts(&m_table[ret], 1);
327                 } while (++ret < n);
328         }
329
330         return 0;
331 }
332
333 /*
334  * Helper function to fragment and queue for TX one packet.
335  */
336 static inline uint32_t
337 send_fragment_packet(struct lcore_conf *qconf, struct rte_mbuf *m,
338         uint16_t port, uint8_t proto)
339 {
340         struct buffer *tbl;
341         uint32_t len, n;
342         int32_t rc;
343
344         tbl =  qconf->tx_mbufs + port;
345         len = tbl->len;
346
347         /* free space for new fragments */
348         if (len + RTE_LIBRTE_IP_FRAG_MAX_FRAG >=  RTE_DIM(tbl->m_table)) {
349                 send_burst(qconf, len, port);
350                 len = 0;
351         }
352
353         n = RTE_DIM(tbl->m_table) - len;
354
355         if (proto == IPPROTO_IP)
356                 rc = rte_ipv4_fragment_packet(m, tbl->m_table + len,
357                         n, mtu_size, m->pool, qconf->frag.pool_indir);
358         else
359                 rc = rte_ipv6_fragment_packet(m, tbl->m_table + len,
360                         n, mtu_size, m->pool, qconf->frag.pool_indir);
361
362         if (rc >= 0)
363                 len += rc;
364         else
365                 RTE_LOG(ERR, IPSEC,
366                         "%s: failed to fragment packet with size %u, "
367                         "error code: %d\n",
368                         __func__, m->pkt_len, rte_errno);
369
370         free_pkts(&m, 1);
371         return len;
372 }
373
374 /* Enqueue a single packet, and send burst if queue is filled */
375 static inline int32_t
376 send_single_packet(struct rte_mbuf *m, uint16_t port, uint8_t proto)
377 {
378         uint32_t lcore_id;
379         uint16_t len;
380         struct lcore_conf *qconf;
381
382         lcore_id = rte_lcore_id();
383
384         qconf = &lcore_conf[lcore_id];
385         len = qconf->tx_mbufs[port].len;
386
387         if (m->pkt_len <= mtu_size) {
388                 qconf->tx_mbufs[port].m_table[len] = m;
389                 len++;
390
391         /* need to fragment the packet */
392         } else if (frag_tbl_sz > 0)
393                 len = send_fragment_packet(qconf, m, port, proto);
394         else
395                 free_pkts(&m, 1);
396
397         /* enough pkts to be sent */
398         if (unlikely(len == MAX_PKT_BURST)) {
399                 send_burst(qconf, MAX_PKT_BURST, port);
400                 len = 0;
401         }
402
403         qconf->tx_mbufs[port].len = len;
404         return 0;
405 }
406
407 static inline void
408 inbound_sp_sa(struct sp_ctx *sp, struct sa_ctx *sa, struct traffic_type *ip,
409                 uint16_t lim, struct ipsec_spd_stats *stats)
410 {
411         struct rte_mbuf *m;
412         uint32_t i, j, res, sa_idx;
413
414         if (ip->num == 0 || sp == NULL)
415                 return;
416
417         rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
418                         ip->num, DEFAULT_MAX_CATEGORIES);
419
420         j = 0;
421         for (i = 0; i < ip->num; i++) {
422                 m = ip->pkts[i];
423                 res = ip->res[i];
424                 if (res == BYPASS) {
425                         ip->pkts[j++] = m;
426                         stats->bypass++;
427                         continue;
428                 }
429                 if (res == DISCARD) {
430                         free_pkts(&m, 1);
431                         stats->discard++;
432                         continue;
433                 }
434
435                 /* Only check SPI match for processed IPSec packets */
436                 if (i < lim && ((m->ol_flags & RTE_MBUF_F_RX_SEC_OFFLOAD) == 0)) {
437                         stats->discard++;
438                         free_pkts(&m, 1);
439                         continue;
440                 }
441
442                 sa_idx = res - 1;
443                 if (!inbound_sa_check(sa, m, sa_idx)) {
444                         stats->discard++;
445                         free_pkts(&m, 1);
446                         continue;
447                 }
448                 ip->pkts[j++] = m;
449                 stats->protect++;
450         }
451         ip->num = j;
452 }
453
454 static inline int32_t
455 get_hop_for_offload_pkt(struct rte_mbuf *pkt, int is_ipv6)
456 {
457         struct ipsec_mbuf_metadata *priv;
458         struct ipsec_sa *sa;
459
460         priv = get_priv(pkt);
461
462         sa = priv->sa;
463         if (unlikely(sa == NULL)) {
464                 RTE_LOG(ERR, IPSEC, "SA not saved in private data\n");
465                 goto fail;
466         }
467
468         if (is_ipv6)
469                 return sa->portid;
470
471         /* else */
472         return (sa->portid | RTE_LPM_LOOKUP_SUCCESS);
473
474 fail:
475         if (is_ipv6)
476                 return -1;
477
478         /* else */
479         return 0;
480 }
481
482 static inline void
483 route4_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
484 {
485         uint32_t hop[MAX_PKT_BURST * 2];
486         uint32_t dst_ip[MAX_PKT_BURST * 2];
487         int32_t pkt_hop = 0;
488         uint16_t i, offset;
489         uint16_t lpm_pkts = 0;
490         unsigned int lcoreid = rte_lcore_id();
491
492         if (nb_pkts == 0)
493                 return;
494
495         /* Need to do an LPM lookup for non-inline packets. Inline packets will
496          * have port ID in the SA
497          */
498
499         for (i = 0; i < nb_pkts; i++) {
500                 if (!(pkts[i]->ol_flags & RTE_MBUF_F_TX_SEC_OFFLOAD)) {
501                         /* Security offload not enabled. So an LPM lookup is
502                          * required to get the hop
503                          */
504                         offset = offsetof(struct ip, ip_dst);
505                         dst_ip[lpm_pkts] = *rte_pktmbuf_mtod_offset(pkts[i],
506                                         uint32_t *, offset);
507                         dst_ip[lpm_pkts] = rte_be_to_cpu_32(dst_ip[lpm_pkts]);
508                         lpm_pkts++;
509                 }
510         }
511
512         rte_lpm_lookup_bulk((struct rte_lpm *)rt_ctx, dst_ip, hop, lpm_pkts);
513
514         lpm_pkts = 0;
515
516         for (i = 0; i < nb_pkts; i++) {
517                 if (pkts[i]->ol_flags & RTE_MBUF_F_TX_SEC_OFFLOAD) {
518                         /* Read hop from the SA */
519                         pkt_hop = get_hop_for_offload_pkt(pkts[i], 0);
520                 } else {
521                         /* Need to use hop returned by lookup */
522                         pkt_hop = hop[lpm_pkts++];
523                 }
524
525                 if ((pkt_hop & RTE_LPM_LOOKUP_SUCCESS) == 0) {
526                         core_statistics[lcoreid].lpm4.miss++;
527                         free_pkts(&pkts[i], 1);
528                         continue;
529                 }
530                 send_single_packet(pkts[i], pkt_hop & 0xff, IPPROTO_IP);
531         }
532 }
533
534 static inline void
535 route6_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
536 {
537         int32_t hop[MAX_PKT_BURST * 2];
538         uint8_t dst_ip[MAX_PKT_BURST * 2][16];
539         uint8_t *ip6_dst;
540         int32_t pkt_hop = 0;
541         uint16_t i, offset;
542         uint16_t lpm_pkts = 0;
543         unsigned int lcoreid = rte_lcore_id();
544
545         if (nb_pkts == 0)
546                 return;
547
548         /* Need to do an LPM lookup for non-inline packets. Inline packets will
549          * have port ID in the SA
550          */
551
552         for (i = 0; i < nb_pkts; i++) {
553                 if (!(pkts[i]->ol_flags & RTE_MBUF_F_TX_SEC_OFFLOAD)) {
554                         /* Security offload not enabled. So an LPM lookup is
555                          * required to get the hop
556                          */
557                         offset = offsetof(struct ip6_hdr, ip6_dst);
558                         ip6_dst = rte_pktmbuf_mtod_offset(pkts[i], uint8_t *,
559                                         offset);
560                         memcpy(&dst_ip[lpm_pkts][0], ip6_dst, 16);
561                         lpm_pkts++;
562                 }
563         }
564
565         rte_lpm6_lookup_bulk_func((struct rte_lpm6 *)rt_ctx, dst_ip, hop,
566                         lpm_pkts);
567
568         lpm_pkts = 0;
569
570         for (i = 0; i < nb_pkts; i++) {
571                 if (pkts[i]->ol_flags & RTE_MBUF_F_TX_SEC_OFFLOAD) {
572                         /* Read hop from the SA */
573                         pkt_hop = get_hop_for_offload_pkt(pkts[i], 1);
574                 } else {
575                         /* Need to use hop returned by lookup */
576                         pkt_hop = hop[lpm_pkts++];
577                 }
578
579                 if (pkt_hop == -1) {
580                         core_statistics[lcoreid].lpm6.miss++;
581                         free_pkts(&pkts[i], 1);
582                         continue;
583                 }
584                 send_single_packet(pkts[i], pkt_hop & 0xff, IPPROTO_IPV6);
585         }
586 }
587
588 static inline void
589 drain_tx_buffers(struct lcore_conf *qconf)
590 {
591         struct buffer *buf;
592         uint32_t portid;
593
594         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
595                 buf = &qconf->tx_mbufs[portid];
596                 if (buf->len == 0)
597                         continue;
598                 send_burst(qconf, buf->len, portid);
599                 buf->len = 0;
600         }
601 }
602
603 #endif /* _IPSEC_WORKER_H_ */