examples/ipsec-secgw: disable Tx checksum for inline
[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         const struct rte_ether_hdr *eth;
121         const struct rte_ipv4_hdr *iph4;
122         const struct rte_ipv6_hdr *iph6;
123         const struct rte_udp_hdr *udp;
124         uint16_t ip4_hdr_len;
125         uint16_t nat_port;
126
127         eth = rte_pktmbuf_mtod(pkt, const struct rte_ether_hdr *);
128         if (eth->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
129
130                 iph4 = (const struct rte_ipv4_hdr *)rte_pktmbuf_adj(pkt,
131                         RTE_ETHER_HDR_LEN);
132                 adjust_ipv4_pktlen(pkt, iph4, 0);
133
134                 switch (iph4->next_proto_id) {
135                 case IPPROTO_ESP:
136                         t->ipsec.pkts[(t->ipsec.num)++] = pkt;
137                         break;
138                 case IPPROTO_UDP:
139                         if (app_sa_prm.udp_encap == 1) {
140                                 ip4_hdr_len = ((iph4->version_ihl &
141                                         RTE_IPV4_HDR_IHL_MASK) *
142                                         RTE_IPV4_IHL_MULTIPLIER);
143                                 udp = rte_pktmbuf_mtod_offset(pkt,
144                                         struct rte_udp_hdr *, ip4_hdr_len);
145                                 nat_port = rte_cpu_to_be_16(IPSEC_NAT_T_PORT);
146                                 if (udp->src_port == nat_port ||
147                                         udp->dst_port == nat_port){
148                                         t->ipsec.pkts[(t->ipsec.num)++] = pkt;
149                                         pkt->packet_type |=
150                                                 MBUF_PTYPE_TUNNEL_ESP_IN_UDP;
151                                         break;
152                                 }
153                         }
154                 /* Fall through */
155                 default:
156                         t->ip4.data[t->ip4.num] = &iph4->next_proto_id;
157                         t->ip4.pkts[(t->ip4.num)++] = pkt;
158                 }
159                 pkt->l2_len = 0;
160                 pkt->l3_len = sizeof(*iph4);
161                 pkt->packet_type |= RTE_PTYPE_L3_IPV4;
162                 if  (pkt->packet_type & RTE_PTYPE_L4_TCP)
163                         pkt->l4_len = sizeof(struct rte_tcp_hdr);
164                 else if (pkt->packet_type & RTE_PTYPE_L4_UDP)
165                         pkt->l4_len = sizeof(struct rte_udp_hdr);
166         } else if (eth->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
167                 int next_proto;
168                 size_t l3len, ext_len;
169                 uint8_t *p;
170
171                 /* get protocol type */
172                 iph6 = (const struct rte_ipv6_hdr *)rte_pktmbuf_adj(pkt,
173                         RTE_ETHER_HDR_LEN);
174                 adjust_ipv6_pktlen(pkt, iph6, 0);
175
176                 next_proto = iph6->proto;
177
178                 /* determine l3 header size up to ESP extension */
179                 l3len = sizeof(struct ip6_hdr);
180                 p = rte_pktmbuf_mtod(pkt, uint8_t *);
181                 while (next_proto != IPPROTO_ESP && l3len < pkt->data_len &&
182                         (next_proto = rte_ipv6_get_next_ext(p + l3len,
183                                                 next_proto, &ext_len)) >= 0)
184                         l3len += ext_len;
185
186                 /* drop packet when IPv6 header exceeds first segment length */
187                 if (unlikely(l3len > pkt->data_len)) {
188                         free_pkts(&pkt, 1);
189                         return;
190                 }
191
192                 switch (next_proto) {
193                 case IPPROTO_ESP:
194                         t->ipsec.pkts[(t->ipsec.num)++] = pkt;
195                         break;
196                 case IPPROTO_UDP:
197                         if (app_sa_prm.udp_encap == 1) {
198                                 udp = rte_pktmbuf_mtod_offset(pkt,
199                                         struct rte_udp_hdr *, l3len);
200                                 nat_port = rte_cpu_to_be_16(IPSEC_NAT_T_PORT);
201                                 if (udp->src_port == nat_port ||
202                                         udp->dst_port == nat_port){
203                                         t->ipsec.pkts[(t->ipsec.num)++] = pkt;
204                                         pkt->packet_type |=
205                                                 MBUF_PTYPE_TUNNEL_ESP_IN_UDP;
206                                         break;
207                                 }
208                         }
209                 /* Fall through */
210                 default:
211                         t->ip6.data[t->ip6.num] = &iph6->proto;
212                         t->ip6.pkts[(t->ip6.num)++] = pkt;
213                 }
214                 pkt->l2_len = 0;
215                 pkt->l3_len = l3len;
216                 pkt->packet_type |= RTE_PTYPE_L3_IPV6;
217         } else {
218                 /* Unknown/Unsupported type, drop the packet */
219                 RTE_LOG(ERR, IPSEC, "Unsupported packet type 0x%x\n",
220                         rte_be_to_cpu_16(eth->ether_type));
221                 free_pkts(&pkt, 1);
222                 return;
223         }
224
225         /* Check if the packet has been processed inline. For inline protocol
226          * processed packets, the metadata in the mbuf can be used to identify
227          * the security processing done on the packet. The metadata will be
228          * used to retrieve the application registered userdata associated
229          * with the security session.
230          */
231
232         if (pkt->ol_flags & RTE_MBUF_F_RX_SEC_OFFLOAD &&
233                         rte_security_dynfield_is_registered()) {
234                 struct ipsec_sa *sa;
235                 struct ipsec_mbuf_metadata *priv;
236                 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
237                                                 rte_eth_dev_get_sec_ctx(
238                                                 pkt->port);
239
240                 /* Retrieve the userdata registered. Here, the userdata
241                  * registered is the SA pointer.
242                  */
243                 sa = (struct ipsec_sa *)rte_security_get_userdata(ctx,
244                                 *rte_security_dynfield(pkt));
245                 if (sa == NULL) {
246                         /* userdata could not be retrieved */
247                         return;
248                 }
249
250                 /* Save SA as priv member in mbuf. This will be used in the
251                  * IPsec selector(SP-SA) check.
252                  */
253
254                 priv = get_priv(pkt);
255                 priv->sa = sa;
256         }
257 }
258
259 static inline void
260 prepare_traffic(struct rte_mbuf **pkts, struct ipsec_traffic *t,
261                 uint16_t nb_pkts)
262 {
263         int32_t i;
264
265         t->ipsec.num = 0;
266         t->ip4.num = 0;
267         t->ip6.num = 0;
268
269         for (i = 0; i < (nb_pkts - PREFETCH_OFFSET); i++) {
270                 rte_prefetch0(rte_pktmbuf_mtod(pkts[i + PREFETCH_OFFSET],
271                                         void *));
272                 prepare_one_packet(pkts[i], t);
273         }
274         /* Process left packets */
275         for (; i < nb_pkts; i++)
276                 prepare_one_packet(pkts[i], t);
277 }
278
279 static inline void
280 prepare_tx_pkt(struct rte_mbuf *pkt, uint16_t port,
281                 const struct lcore_conf *qconf)
282 {
283         struct ip *ip;
284         struct rte_ether_hdr *ethhdr;
285
286         ip = rte_pktmbuf_mtod(pkt, struct ip *);
287
288         ethhdr = (struct rte_ether_hdr *)
289                 rte_pktmbuf_prepend(pkt, RTE_ETHER_HDR_LEN);
290
291         if (ip->ip_v == IPVERSION) {
292                 pkt->ol_flags |= qconf->outbound.ipv4_offloads;
293                 pkt->l3_len = sizeof(struct ip);
294                 pkt->l2_len = RTE_ETHER_HDR_LEN;
295
296                 ip->ip_sum = 0;
297
298                 /* calculate IPv4 cksum in SW */
299                 if ((pkt->ol_flags & RTE_MBUF_F_TX_IP_CKSUM) == 0)
300                         ip->ip_sum = rte_ipv4_cksum((struct rte_ipv4_hdr *)ip);
301
302                 ethhdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
303         } else {
304                 pkt->ol_flags |= qconf->outbound.ipv6_offloads;
305                 pkt->l3_len = sizeof(struct ip6_hdr);
306                 pkt->l2_len = RTE_ETHER_HDR_LEN;
307
308                 ethhdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
309         }
310
311         memcpy(&ethhdr->src_addr, &ethaddr_tbl[port].src,
312                         sizeof(struct rte_ether_addr));
313         memcpy(&ethhdr->dst_addr, &ethaddr_tbl[port].dst,
314                         sizeof(struct rte_ether_addr));
315 }
316
317 static inline void
318 prepare_tx_burst(struct rte_mbuf *pkts[], uint16_t nb_pkts, uint16_t port,
319                 const struct lcore_conf *qconf)
320 {
321         int32_t i;
322         const int32_t prefetch_offset = 2;
323
324         for (i = 0; i < (nb_pkts - prefetch_offset); i++) {
325                 rte_mbuf_prefetch_part2(pkts[i + prefetch_offset]);
326                 prepare_tx_pkt(pkts[i], port, qconf);
327         }
328         /* Process left packets */
329         for (; i < nb_pkts; i++)
330                 prepare_tx_pkt(pkts[i], port, qconf);
331 }
332
333 /* Send burst of packets on an output interface */
334 static inline int32_t
335 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
336 {
337         struct rte_mbuf **m_table;
338         int32_t ret;
339         uint16_t queueid;
340
341         queueid = qconf->tx_queue_id[port];
342         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
343
344         prepare_tx_burst(m_table, n, port, qconf);
345
346         ret = rte_eth_tx_burst(port, queueid, m_table, n);
347
348         core_stats_update_tx(ret);
349
350         if (unlikely(ret < n)) {
351                 do {
352                         free_pkts(&m_table[ret], 1);
353                 } while (++ret < n);
354         }
355
356         return 0;
357 }
358
359 /*
360  * Helper function to fragment and queue for TX one packet.
361  */
362 static inline uint32_t
363 send_fragment_packet(struct lcore_conf *qconf, struct rte_mbuf *m,
364         uint16_t port, uint8_t proto)
365 {
366         struct buffer *tbl;
367         uint32_t len, n;
368         int32_t rc;
369
370         tbl =  qconf->tx_mbufs + port;
371         len = tbl->len;
372
373         /* free space for new fragments */
374         if (len + RTE_LIBRTE_IP_FRAG_MAX_FRAG >=  RTE_DIM(tbl->m_table)) {
375                 send_burst(qconf, len, port);
376                 len = 0;
377         }
378
379         n = RTE_DIM(tbl->m_table) - len;
380
381         if (proto == IPPROTO_IP)
382                 rc = rte_ipv4_fragment_packet(m, tbl->m_table + len,
383                         n, mtu_size, m->pool, qconf->frag.pool_indir);
384         else
385                 rc = rte_ipv6_fragment_packet(m, tbl->m_table + len,
386                         n, mtu_size, m->pool, qconf->frag.pool_indir);
387
388         if (rc >= 0)
389                 len += rc;
390         else
391                 RTE_LOG(ERR, IPSEC,
392                         "%s: failed to fragment packet with size %u, "
393                         "error code: %d\n",
394                         __func__, m->pkt_len, rte_errno);
395
396         free_pkts(&m, 1);
397         return len;
398 }
399
400 /* Enqueue a single packet, and send burst if queue is filled */
401 static inline int32_t
402 send_single_packet(struct rte_mbuf *m, uint16_t port, uint8_t proto)
403 {
404         uint32_t lcore_id;
405         uint16_t len;
406         struct lcore_conf *qconf;
407
408         lcore_id = rte_lcore_id();
409
410         qconf = &lcore_conf[lcore_id];
411         len = qconf->tx_mbufs[port].len;
412
413         if (m->pkt_len <= mtu_size) {
414                 qconf->tx_mbufs[port].m_table[len] = m;
415                 len++;
416
417         /* need to fragment the packet */
418         } else if (frag_tbl_sz > 0)
419                 len = send_fragment_packet(qconf, m, port, proto);
420         else
421                 free_pkts(&m, 1);
422
423         /* enough pkts to be sent */
424         if (unlikely(len == MAX_PKT_BURST)) {
425                 send_burst(qconf, MAX_PKT_BURST, port);
426                 len = 0;
427         }
428
429         qconf->tx_mbufs[port].len = len;
430         return 0;
431 }
432
433 static inline void
434 inbound_sp_sa(struct sp_ctx *sp, struct sa_ctx *sa, struct traffic_type *ip,
435                 uint16_t lim, struct ipsec_spd_stats *stats)
436 {
437         struct rte_mbuf *m;
438         uint32_t i, j, res, sa_idx;
439
440         if (ip->num == 0 || sp == NULL)
441                 return;
442
443         rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
444                         ip->num, DEFAULT_MAX_CATEGORIES);
445
446         j = 0;
447         for (i = 0; i < ip->num; i++) {
448                 m = ip->pkts[i];
449                 res = ip->res[i];
450                 if (res == BYPASS) {
451                         ip->pkts[j++] = m;
452                         stats->bypass++;
453                         continue;
454                 }
455                 if (res == DISCARD) {
456                         free_pkts(&m, 1);
457                         stats->discard++;
458                         continue;
459                 }
460
461                 /* Only check SPI match for processed IPSec packets */
462                 if (i < lim && ((m->ol_flags & RTE_MBUF_F_RX_SEC_OFFLOAD) == 0)) {
463                         stats->discard++;
464                         free_pkts(&m, 1);
465                         continue;
466                 }
467
468                 sa_idx = res - 1;
469                 if (!inbound_sa_check(sa, m, sa_idx)) {
470                         stats->discard++;
471                         free_pkts(&m, 1);
472                         continue;
473                 }
474                 ip->pkts[j++] = m;
475                 stats->protect++;
476         }
477         ip->num = j;
478 }
479
480 static inline int32_t
481 get_hop_for_offload_pkt(struct rte_mbuf *pkt, int is_ipv6)
482 {
483         struct ipsec_mbuf_metadata *priv;
484         struct ipsec_sa *sa;
485
486         priv = get_priv(pkt);
487
488         sa = priv->sa;
489         if (unlikely(sa == NULL)) {
490                 RTE_LOG(ERR, IPSEC, "SA not saved in private data\n");
491                 goto fail;
492         }
493
494         if (is_ipv6)
495                 return sa->portid;
496
497         /* else */
498         return (sa->portid | RTE_LPM_LOOKUP_SUCCESS);
499
500 fail:
501         if (is_ipv6)
502                 return -1;
503
504         /* else */
505         return 0;
506 }
507
508 static inline void
509 route4_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
510 {
511         uint32_t hop[MAX_PKT_BURST * 2];
512         uint32_t dst_ip[MAX_PKT_BURST * 2];
513         int32_t pkt_hop = 0;
514         uint16_t i, offset;
515         uint16_t lpm_pkts = 0;
516         unsigned int lcoreid = rte_lcore_id();
517
518         if (nb_pkts == 0)
519                 return;
520
521         /* Need to do an LPM lookup for non-inline packets. Inline packets will
522          * have port ID in the SA
523          */
524
525         for (i = 0; i < nb_pkts; i++) {
526                 if (!(pkts[i]->ol_flags & RTE_MBUF_F_TX_SEC_OFFLOAD)) {
527                         /* Security offload not enabled. So an LPM lookup is
528                          * required to get the hop
529                          */
530                         offset = offsetof(struct ip, ip_dst);
531                         dst_ip[lpm_pkts] = *rte_pktmbuf_mtod_offset(pkts[i],
532                                         uint32_t *, offset);
533                         dst_ip[lpm_pkts] = rte_be_to_cpu_32(dst_ip[lpm_pkts]);
534                         lpm_pkts++;
535                 }
536         }
537
538         rte_lpm_lookup_bulk((struct rte_lpm *)rt_ctx, dst_ip, hop, lpm_pkts);
539
540         lpm_pkts = 0;
541
542         for (i = 0; i < nb_pkts; i++) {
543                 if (pkts[i]->ol_flags & RTE_MBUF_F_TX_SEC_OFFLOAD) {
544                         /* Read hop from the SA */
545                         pkt_hop = get_hop_for_offload_pkt(pkts[i], 0);
546                 } else {
547                         /* Need to use hop returned by lookup */
548                         pkt_hop = hop[lpm_pkts++];
549                 }
550
551                 if ((pkt_hop & RTE_LPM_LOOKUP_SUCCESS) == 0) {
552                         core_statistics[lcoreid].lpm4.miss++;
553                         free_pkts(&pkts[i], 1);
554                         continue;
555                 }
556                 send_single_packet(pkts[i], pkt_hop & 0xff, IPPROTO_IP);
557         }
558 }
559
560 static inline void
561 route6_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
562 {
563         int32_t hop[MAX_PKT_BURST * 2];
564         uint8_t dst_ip[MAX_PKT_BURST * 2][16];
565         uint8_t *ip6_dst;
566         int32_t pkt_hop = 0;
567         uint16_t i, offset;
568         uint16_t lpm_pkts = 0;
569         unsigned int lcoreid = rte_lcore_id();
570
571         if (nb_pkts == 0)
572                 return;
573
574         /* Need to do an LPM lookup for non-inline packets. Inline packets will
575          * have port ID in the SA
576          */
577
578         for (i = 0; i < nb_pkts; i++) {
579                 if (!(pkts[i]->ol_flags & RTE_MBUF_F_TX_SEC_OFFLOAD)) {
580                         /* Security offload not enabled. So an LPM lookup is
581                          * required to get the hop
582                          */
583                         offset = offsetof(struct ip6_hdr, ip6_dst);
584                         ip6_dst = rte_pktmbuf_mtod_offset(pkts[i], uint8_t *,
585                                         offset);
586                         memcpy(&dst_ip[lpm_pkts][0], ip6_dst, 16);
587                         lpm_pkts++;
588                 }
589         }
590
591         rte_lpm6_lookup_bulk_func((struct rte_lpm6 *)rt_ctx, dst_ip, hop,
592                         lpm_pkts);
593
594         lpm_pkts = 0;
595
596         for (i = 0; i < nb_pkts; i++) {
597                 if (pkts[i]->ol_flags & RTE_MBUF_F_TX_SEC_OFFLOAD) {
598                         /* Read hop from the SA */
599                         pkt_hop = get_hop_for_offload_pkt(pkts[i], 1);
600                 } else {
601                         /* Need to use hop returned by lookup */
602                         pkt_hop = hop[lpm_pkts++];
603                 }
604
605                 if (pkt_hop == -1) {
606                         core_statistics[lcoreid].lpm6.miss++;
607                         free_pkts(&pkts[i], 1);
608                         continue;
609                 }
610                 send_single_packet(pkts[i], pkt_hop & 0xff, IPPROTO_IPV6);
611         }
612 }
613
614 static inline void
615 drain_tx_buffers(struct lcore_conf *qconf)
616 {
617         struct buffer *buf;
618         uint32_t portid;
619
620         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
621                 buf = &qconf->tx_mbufs[portid];
622                 if (buf->len == 0)
623                         continue;
624                 send_burst(qconf, buf->len, portid);
625                 buf->len = 0;
626         }
627 }
628
629 #endif /* _IPSEC_WORKER_H_ */