net: add rte prefix to SCTP structure
[dpdk.git] / app / test-pmd / csumonly.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation.
3  * Copyright 2014 6WIND S.A.
4  */
5
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <errno.h>
9 #include <stdint.h>
10 #include <unistd.h>
11 #include <inttypes.h>
12
13 #include <sys/queue.h>
14 #include <sys/stat.h>
15
16 #include <rte_common.h>
17 #include <rte_byteorder.h>
18 #include <rte_log.h>
19 #include <rte_debug.h>
20 #include <rte_cycles.h>
21 #include <rte_memory.h>
22 #include <rte_memcpy.h>
23 #include <rte_launch.h>
24 #include <rte_eal.h>
25 #include <rte_per_lcore.h>
26 #include <rte_lcore.h>
27 #include <rte_atomic.h>
28 #include <rte_branch_prediction.h>
29 #include <rte_mempool.h>
30 #include <rte_mbuf.h>
31 #include <rte_interrupts.h>
32 #include <rte_pci.h>
33 #include <rte_ether.h>
34 #include <rte_ethdev.h>
35 #include <rte_ip.h>
36 #include <rte_tcp.h>
37 #include <rte_udp.h>
38 #include <rte_sctp.h>
39 #include <rte_prefetch.h>
40 #include <rte_string_fns.h>
41 #include <rte_flow.h>
42 #include <rte_gro.h>
43 #include <rte_gso.h>
44
45 #include "testpmd.h"
46
47 #define IP_DEFTTL  64   /* from RFC 1340. */
48 #define IP_VERSION 0x40
49 #define IP_HDRLEN  0x05 /* default IP header length == five 32-bits words. */
50 #define IP_VHL_DEF (IP_VERSION | IP_HDRLEN)
51
52 #define GRE_CHECKSUM_PRESENT    0x8000
53 #define GRE_KEY_PRESENT         0x2000
54 #define GRE_SEQUENCE_PRESENT    0x1000
55 #define GRE_EXT_LEN             4
56 #define GRE_SUPPORTED_FIELDS    (GRE_CHECKSUM_PRESENT | GRE_KEY_PRESENT |\
57                                  GRE_SEQUENCE_PRESENT)
58
59 /* We cannot use rte_cpu_to_be_16() on a constant in a switch/case */
60 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
61 #define _htons(x) ((uint16_t)((((x) & 0x00ffU) << 8) | (((x) & 0xff00U) >> 8)))
62 #else
63 #define _htons(x) (x)
64 #endif
65
66 uint16_t vxlan_gpe_udp_port = 4790;
67
68 /* structure that caches offload info for the current packet */
69 struct testpmd_offload_info {
70         uint16_t ethertype;
71         uint8_t gso_enable;
72         uint16_t l2_len;
73         uint16_t l3_len;
74         uint16_t l4_len;
75         uint8_t l4_proto;
76         uint8_t is_tunnel;
77         uint16_t outer_ethertype;
78         uint16_t outer_l2_len;
79         uint16_t outer_l3_len;
80         uint8_t outer_l4_proto;
81         uint16_t tso_segsz;
82         uint16_t tunnel_tso_segsz;
83         uint32_t pkt_len;
84 };
85
86 /* simplified GRE header */
87 struct simple_gre_hdr {
88         uint16_t flags;
89         uint16_t proto;
90 } __attribute__((__packed__));
91
92 static uint16_t
93 get_udptcp_checksum(void *l3_hdr, void *l4_hdr, uint16_t ethertype)
94 {
95         if (ethertype == _htons(RTE_ETHER_TYPE_IPv4))
96                 return rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
97         else /* assume ethertype == RTE_ETHER_TYPE_IPv6 */
98                 return rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr);
99 }
100
101 /* Parse an IPv4 header to fill l3_len, l4_len, and l4_proto */
102 static void
103 parse_ipv4(struct rte_ipv4_hdr *ipv4_hdr, struct testpmd_offload_info *info)
104 {
105         struct tcp_hdr *tcp_hdr;
106
107         info->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
108         info->l4_proto = ipv4_hdr->next_proto_id;
109
110         /* only fill l4_len for TCP, it's useful for TSO */
111         if (info->l4_proto == IPPROTO_TCP) {
112                 tcp_hdr = (struct tcp_hdr *)((char *)ipv4_hdr + info->l3_len);
113                 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
114         } else if (info->l4_proto == IPPROTO_UDP)
115                 info->l4_len = sizeof(struct udp_hdr);
116         else
117                 info->l4_len = 0;
118 }
119
120 /* Parse an IPv6 header to fill l3_len, l4_len, and l4_proto */
121 static void
122 parse_ipv6(struct rte_ipv6_hdr *ipv6_hdr, struct testpmd_offload_info *info)
123 {
124         struct tcp_hdr *tcp_hdr;
125
126         info->l3_len = sizeof(struct rte_ipv6_hdr);
127         info->l4_proto = ipv6_hdr->proto;
128
129         /* only fill l4_len for TCP, it's useful for TSO */
130         if (info->l4_proto == IPPROTO_TCP) {
131                 tcp_hdr = (struct tcp_hdr *)((char *)ipv6_hdr + info->l3_len);
132                 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
133         } else if (info->l4_proto == IPPROTO_UDP)
134                 info->l4_len = sizeof(struct udp_hdr);
135         else
136                 info->l4_len = 0;
137 }
138
139 /*
140  * Parse an ethernet header to fill the ethertype, l2_len, l3_len and
141  * ipproto. This function is able to recognize IPv4/IPv6 with one optional vlan
142  * header. The l4_len argument is only set in case of TCP (useful for TSO).
143  */
144 static void
145 parse_ethernet(struct rte_ether_hdr *eth_hdr, struct testpmd_offload_info *info)
146 {
147         struct rte_ipv4_hdr *ipv4_hdr;
148         struct rte_ipv6_hdr *ipv6_hdr;
149
150         info->l2_len = sizeof(struct rte_ether_hdr);
151         info->ethertype = eth_hdr->ether_type;
152
153         if (info->ethertype == _htons(RTE_ETHER_TYPE_VLAN)) {
154                 struct rte_vlan_hdr *vlan_hdr = (
155                         struct rte_vlan_hdr *)(eth_hdr + 1);
156
157                 info->l2_len  += sizeof(struct rte_vlan_hdr);
158                 info->ethertype = vlan_hdr->eth_proto;
159         }
160
161         switch (info->ethertype) {
162         case _htons(RTE_ETHER_TYPE_IPv4):
163                 ipv4_hdr = (struct rte_ipv4_hdr *)
164                         ((char *)eth_hdr + info->l2_len);
165                 parse_ipv4(ipv4_hdr, info);
166                 break;
167         case _htons(RTE_ETHER_TYPE_IPv6):
168                 ipv6_hdr = (struct rte_ipv6_hdr *)
169                         ((char *)eth_hdr + info->l2_len);
170                 parse_ipv6(ipv6_hdr, info);
171                 break;
172         default:
173                 info->l4_len = 0;
174                 info->l3_len = 0;
175                 info->l4_proto = 0;
176                 break;
177         }
178 }
179
180 /* Parse a vxlan header */
181 static void
182 parse_vxlan(struct udp_hdr *udp_hdr,
183             struct testpmd_offload_info *info,
184             uint32_t pkt_type)
185 {
186         struct rte_ether_hdr *eth_hdr;
187
188         /* check udp destination port, 4789 is the default vxlan port
189          * (rfc7348) or that the rx offload flag is set (i40e only
190          * currently) */
191         if (udp_hdr->dst_port != _htons(4789) &&
192                 RTE_ETH_IS_TUNNEL_PKT(pkt_type) == 0)
193                 return;
194
195         info->is_tunnel = 1;
196         info->outer_ethertype = info->ethertype;
197         info->outer_l2_len = info->l2_len;
198         info->outer_l3_len = info->l3_len;
199         info->outer_l4_proto = info->l4_proto;
200
201         eth_hdr = (struct rte_ether_hdr *)((char *)udp_hdr +
202                 sizeof(struct udp_hdr) +
203                 sizeof(struct rte_vxlan_hdr));
204
205         parse_ethernet(eth_hdr, info);
206         info->l2_len += RTE_ETHER_VXLAN_HLEN; /* add udp + vxlan */
207 }
208
209 /* Parse a vxlan-gpe header */
210 static void
211 parse_vxlan_gpe(struct udp_hdr *udp_hdr,
212             struct testpmd_offload_info *info)
213 {
214         struct rte_ether_hdr *eth_hdr;
215         struct rte_ipv4_hdr *ipv4_hdr;
216         struct rte_ipv6_hdr *ipv6_hdr;
217         struct rte_vxlan_gpe_hdr *vxlan_gpe_hdr;
218         uint8_t vxlan_gpe_len = sizeof(*vxlan_gpe_hdr);
219
220         /* Check udp destination port. */
221         if (udp_hdr->dst_port != _htons(vxlan_gpe_udp_port))
222                 return;
223
224         vxlan_gpe_hdr = (struct rte_vxlan_gpe_hdr *)((char *)udp_hdr +
225                                 sizeof(struct udp_hdr));
226
227         if (!vxlan_gpe_hdr->proto || vxlan_gpe_hdr->proto ==
228             RTE_VXLAN_GPE_TYPE_IPV4) {
229                 info->is_tunnel = 1;
230                 info->outer_ethertype = info->ethertype;
231                 info->outer_l2_len = info->l2_len;
232                 info->outer_l3_len = info->l3_len;
233                 info->outer_l4_proto = info->l4_proto;
234
235                 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)vxlan_gpe_hdr +
236                            vxlan_gpe_len);
237
238                 parse_ipv4(ipv4_hdr, info);
239                 info->ethertype = _htons(RTE_ETHER_TYPE_IPv4);
240                 info->l2_len = 0;
241
242         } else if (vxlan_gpe_hdr->proto == RTE_VXLAN_GPE_TYPE_IPV6) {
243                 info->is_tunnel = 1;
244                 info->outer_ethertype = info->ethertype;
245                 info->outer_l2_len = info->l2_len;
246                 info->outer_l3_len = info->l3_len;
247                 info->outer_l4_proto = info->l4_proto;
248
249                 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)vxlan_gpe_hdr +
250                            vxlan_gpe_len);
251
252                 info->ethertype = _htons(RTE_ETHER_TYPE_IPv6);
253                 parse_ipv6(ipv6_hdr, info);
254                 info->l2_len = 0;
255
256         } else if (vxlan_gpe_hdr->proto == RTE_VXLAN_GPE_TYPE_ETH) {
257                 info->is_tunnel = 1;
258                 info->outer_ethertype = info->ethertype;
259                 info->outer_l2_len = info->l2_len;
260                 info->outer_l3_len = info->l3_len;
261                 info->outer_l4_proto = info->l4_proto;
262
263                 eth_hdr = (struct rte_ether_hdr *)((char *)vxlan_gpe_hdr +
264                           vxlan_gpe_len);
265
266                 parse_ethernet(eth_hdr, info);
267         } else
268                 return;
269
270         info->l2_len += RTE_ETHER_VXLAN_GPE_HLEN;
271 }
272
273 /* Parse a gre header */
274 static void
275 parse_gre(struct simple_gre_hdr *gre_hdr, struct testpmd_offload_info *info)
276 {
277         struct rte_ether_hdr *eth_hdr;
278         struct rte_ipv4_hdr *ipv4_hdr;
279         struct rte_ipv6_hdr *ipv6_hdr;
280         uint8_t gre_len = 0;
281
282         gre_len += sizeof(struct simple_gre_hdr);
283
284         if (gre_hdr->flags & _htons(GRE_KEY_PRESENT))
285                 gre_len += GRE_EXT_LEN;
286         if (gre_hdr->flags & _htons(GRE_SEQUENCE_PRESENT))
287                 gre_len += GRE_EXT_LEN;
288         if (gre_hdr->flags & _htons(GRE_CHECKSUM_PRESENT))
289                 gre_len += GRE_EXT_LEN;
290
291         if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_IPv4)) {
292                 info->is_tunnel = 1;
293                 info->outer_ethertype = info->ethertype;
294                 info->outer_l2_len = info->l2_len;
295                 info->outer_l3_len = info->l3_len;
296                 info->outer_l4_proto = info->l4_proto;
297
298                 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)gre_hdr + gre_len);
299
300                 parse_ipv4(ipv4_hdr, info);
301                 info->ethertype = _htons(RTE_ETHER_TYPE_IPv4);
302                 info->l2_len = 0;
303
304         } else if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_IPv6)) {
305                 info->is_tunnel = 1;
306                 info->outer_ethertype = info->ethertype;
307                 info->outer_l2_len = info->l2_len;
308                 info->outer_l3_len = info->l3_len;
309                 info->outer_l4_proto = info->l4_proto;
310
311                 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)gre_hdr + gre_len);
312
313                 info->ethertype = _htons(RTE_ETHER_TYPE_IPv6);
314                 parse_ipv6(ipv6_hdr, info);
315                 info->l2_len = 0;
316
317         } else if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_TEB)) {
318                 info->is_tunnel = 1;
319                 info->outer_ethertype = info->ethertype;
320                 info->outer_l2_len = info->l2_len;
321                 info->outer_l3_len = info->l3_len;
322                 info->outer_l4_proto = info->l4_proto;
323
324                 eth_hdr = (struct rte_ether_hdr *)((char *)gre_hdr + gre_len);
325
326                 parse_ethernet(eth_hdr, info);
327         } else
328                 return;
329
330         info->l2_len += gre_len;
331 }
332
333
334 /* Parse an encapsulated ip or ipv6 header */
335 static void
336 parse_encap_ip(void *encap_ip, struct testpmd_offload_info *info)
337 {
338         struct rte_ipv4_hdr *ipv4_hdr = encap_ip;
339         struct rte_ipv6_hdr *ipv6_hdr = encap_ip;
340         uint8_t ip_version;
341
342         ip_version = (ipv4_hdr->version_ihl & 0xf0) >> 4;
343
344         if (ip_version != 4 && ip_version != 6)
345                 return;
346
347         info->is_tunnel = 1;
348         info->outer_ethertype = info->ethertype;
349         info->outer_l2_len = info->l2_len;
350         info->outer_l3_len = info->l3_len;
351
352         if (ip_version == 4) {
353                 parse_ipv4(ipv4_hdr, info);
354                 info->ethertype = _htons(RTE_ETHER_TYPE_IPv4);
355         } else {
356                 parse_ipv6(ipv6_hdr, info);
357                 info->ethertype = _htons(RTE_ETHER_TYPE_IPv6);
358         }
359         info->l2_len = 0;
360 }
361
362 /* if possible, calculate the checksum of a packet in hw or sw,
363  * depending on the testpmd command line configuration */
364 static uint64_t
365 process_inner_cksums(void *l3_hdr, const struct testpmd_offload_info *info,
366         uint64_t tx_offloads)
367 {
368         struct rte_ipv4_hdr *ipv4_hdr = l3_hdr;
369         struct udp_hdr *udp_hdr;
370         struct tcp_hdr *tcp_hdr;
371         struct rte_sctp_hdr *sctp_hdr;
372         uint64_t ol_flags = 0;
373         uint32_t max_pkt_len, tso_segsz = 0;
374
375         /* ensure packet is large enough to require tso */
376         if (!info->is_tunnel) {
377                 max_pkt_len = info->l2_len + info->l3_len + info->l4_len +
378                         info->tso_segsz;
379                 if (info->tso_segsz != 0 && info->pkt_len > max_pkt_len)
380                         tso_segsz = info->tso_segsz;
381         } else {
382                 max_pkt_len = info->outer_l2_len + info->outer_l3_len +
383                         info->l2_len + info->l3_len + info->l4_len +
384                         info->tunnel_tso_segsz;
385                 if (info->tunnel_tso_segsz != 0 && info->pkt_len > max_pkt_len)
386                         tso_segsz = info->tunnel_tso_segsz;
387         }
388
389         if (info->ethertype == _htons(RTE_ETHER_TYPE_IPv4)) {
390                 ipv4_hdr = l3_hdr;
391                 ipv4_hdr->hdr_checksum = 0;
392
393                 ol_flags |= PKT_TX_IPV4;
394                 if (info->l4_proto == IPPROTO_TCP && tso_segsz) {
395                         ol_flags |= PKT_TX_IP_CKSUM;
396                 } else {
397                         if (tx_offloads & DEV_TX_OFFLOAD_IPV4_CKSUM)
398                                 ol_flags |= PKT_TX_IP_CKSUM;
399                         else
400                                 ipv4_hdr->hdr_checksum =
401                                         rte_ipv4_cksum(ipv4_hdr);
402                 }
403         } else if (info->ethertype == _htons(RTE_ETHER_TYPE_IPv6))
404                 ol_flags |= PKT_TX_IPV6;
405         else
406                 return 0; /* packet type not supported, nothing to do */
407
408         if (info->l4_proto == IPPROTO_UDP) {
409                 udp_hdr = (struct udp_hdr *)((char *)l3_hdr + info->l3_len);
410                 /* do not recalculate udp cksum if it was 0 */
411                 if (udp_hdr->dgram_cksum != 0) {
412                         udp_hdr->dgram_cksum = 0;
413                         if (tx_offloads & DEV_TX_OFFLOAD_UDP_CKSUM)
414                                 ol_flags |= PKT_TX_UDP_CKSUM;
415                         else {
416                                 udp_hdr->dgram_cksum =
417                                         get_udptcp_checksum(l3_hdr, udp_hdr,
418                                                 info->ethertype);
419                         }
420                 }
421                 if (info->gso_enable)
422                         ol_flags |= PKT_TX_UDP_SEG;
423         } else if (info->l4_proto == IPPROTO_TCP) {
424                 tcp_hdr = (struct tcp_hdr *)((char *)l3_hdr + info->l3_len);
425                 tcp_hdr->cksum = 0;
426                 if (tso_segsz)
427                         ol_flags |= PKT_TX_TCP_SEG;
428                 else if (tx_offloads & DEV_TX_OFFLOAD_TCP_CKSUM)
429                         ol_flags |= PKT_TX_TCP_CKSUM;
430                 else {
431                         tcp_hdr->cksum =
432                                 get_udptcp_checksum(l3_hdr, tcp_hdr,
433                                         info->ethertype);
434                 }
435                 if (info->gso_enable)
436                         ol_flags |= PKT_TX_TCP_SEG;
437         } else if (info->l4_proto == IPPROTO_SCTP) {
438                 sctp_hdr = (struct rte_sctp_hdr *)
439                         ((char *)l3_hdr + info->l3_len);
440                 sctp_hdr->cksum = 0;
441                 /* sctp payload must be a multiple of 4 to be
442                  * offloaded */
443                 if ((tx_offloads & DEV_TX_OFFLOAD_SCTP_CKSUM) &&
444                         ((ipv4_hdr->total_length & 0x3) == 0)) {
445                         ol_flags |= PKT_TX_SCTP_CKSUM;
446                 } else {
447                         /* XXX implement CRC32c, example available in
448                          * RFC3309 */
449                 }
450         }
451
452         return ol_flags;
453 }
454
455 /* Calculate the checksum of outer header */
456 static uint64_t
457 process_outer_cksums(void *outer_l3_hdr, struct testpmd_offload_info *info,
458         uint64_t tx_offloads, int tso_enabled)
459 {
460         struct rte_ipv4_hdr *ipv4_hdr = outer_l3_hdr;
461         struct rte_ipv6_hdr *ipv6_hdr = outer_l3_hdr;
462         struct udp_hdr *udp_hdr;
463         uint64_t ol_flags = 0;
464
465         if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPv4)) {
466                 ipv4_hdr->hdr_checksum = 0;
467                 ol_flags |= PKT_TX_OUTER_IPV4;
468
469                 if (tx_offloads & DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM)
470                         ol_flags |= PKT_TX_OUTER_IP_CKSUM;
471                 else
472                         ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
473         } else
474                 ol_flags |= PKT_TX_OUTER_IPV6;
475
476         if (info->outer_l4_proto != IPPROTO_UDP)
477                 return ol_flags;
478
479         /* Skip SW outer UDP checksum generation if HW supports it */
480         if (tx_offloads & DEV_TX_OFFLOAD_OUTER_UDP_CKSUM) {
481                 ol_flags |= PKT_TX_OUTER_UDP_CKSUM;
482                 return ol_flags;
483         }
484
485         udp_hdr = (struct udp_hdr *)((char *)outer_l3_hdr + info->outer_l3_len);
486
487         /* outer UDP checksum is done in software. In the other side, for
488          * UDP tunneling, like VXLAN or Geneve, outer UDP checksum can be
489          * set to zero.
490          *
491          * If a packet will be TSOed into small packets by NIC, we cannot
492          * set/calculate a non-zero checksum, because it will be a wrong
493          * value after the packet be split into several small packets.
494          */
495         if (tso_enabled)
496                 udp_hdr->dgram_cksum = 0;
497
498         /* do not recalculate udp cksum if it was 0 */
499         if (udp_hdr->dgram_cksum != 0) {
500                 udp_hdr->dgram_cksum = 0;
501                 if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPv4))
502                         udp_hdr->dgram_cksum =
503                                 rte_ipv4_udptcp_cksum(ipv4_hdr, udp_hdr);
504                 else
505                         udp_hdr->dgram_cksum =
506                                 rte_ipv6_udptcp_cksum(ipv6_hdr, udp_hdr);
507         }
508
509         return ol_flags;
510 }
511
512 /*
513  * Helper function.
514  * Performs actual copying.
515  * Returns number of segments in the destination mbuf on success,
516  * or negative error code on failure.
517  */
518 static int
519 mbuf_copy_split(const struct rte_mbuf *ms, struct rte_mbuf *md[],
520         uint16_t seglen[], uint8_t nb_seg)
521 {
522         uint32_t dlen, slen, tlen;
523         uint32_t i, len;
524         const struct rte_mbuf *m;
525         const uint8_t *src;
526         uint8_t *dst;
527
528         dlen = 0;
529         slen = 0;
530         tlen = 0;
531
532         dst = NULL;
533         src = NULL;
534
535         m = ms;
536         i = 0;
537         while (ms != NULL && i != nb_seg) {
538
539                 if (slen == 0) {
540                         slen = rte_pktmbuf_data_len(ms);
541                         src = rte_pktmbuf_mtod(ms, const uint8_t *);
542                 }
543
544                 if (dlen == 0) {
545                         dlen = RTE_MIN(seglen[i], slen);
546                         md[i]->data_len = dlen;
547                         md[i]->next = (i + 1 == nb_seg) ? NULL : md[i + 1];
548                         dst = rte_pktmbuf_mtod(md[i], uint8_t *);
549                 }
550
551                 len = RTE_MIN(slen, dlen);
552                 memcpy(dst, src, len);
553                 tlen += len;
554                 slen -= len;
555                 dlen -= len;
556                 src += len;
557                 dst += len;
558
559                 if (slen == 0)
560                         ms = ms->next;
561                 if (dlen == 0)
562                         i++;
563         }
564
565         if (ms != NULL)
566                 return -ENOBUFS;
567         else if (tlen != m->pkt_len)
568                 return -EINVAL;
569
570         md[0]->nb_segs = nb_seg;
571         md[0]->pkt_len = tlen;
572         md[0]->vlan_tci = m->vlan_tci;
573         md[0]->vlan_tci_outer = m->vlan_tci_outer;
574         md[0]->ol_flags = m->ol_flags;
575         md[0]->tx_offload = m->tx_offload;
576
577         return nb_seg;
578 }
579
580 /*
581  * Allocate a new mbuf with up to tx_pkt_nb_segs segments.
582  * Copy packet contents and offload information into the new segmented mbuf.
583  */
584 static struct rte_mbuf *
585 pkt_copy_split(const struct rte_mbuf *pkt)
586 {
587         int32_t n, rc;
588         uint32_t i, len, nb_seg;
589         struct rte_mempool *mp;
590         uint16_t seglen[RTE_MAX_SEGS_PER_PKT];
591         struct rte_mbuf *p, *md[RTE_MAX_SEGS_PER_PKT];
592
593         mp = current_fwd_lcore()->mbp;
594
595         if (tx_pkt_split == TX_PKT_SPLIT_RND)
596                 nb_seg = random() % tx_pkt_nb_segs + 1;
597         else
598                 nb_seg = tx_pkt_nb_segs;
599
600         memcpy(seglen, tx_pkt_seg_lengths, nb_seg * sizeof(seglen[0]));
601
602         /* calculate number of segments to use and their length. */
603         len = 0;
604         for (i = 0; i != nb_seg && len < pkt->pkt_len; i++) {
605                 len += seglen[i];
606                 md[i] = NULL;
607         }
608
609         n = pkt->pkt_len - len;
610
611         /* update size of the last segment to fit rest of the packet */
612         if (n >= 0) {
613                 seglen[i - 1] += n;
614                 len += n;
615         }
616
617         nb_seg = i;
618         while (i != 0) {
619                 p = rte_pktmbuf_alloc(mp);
620                 if (p == NULL) {
621                         TESTPMD_LOG(ERR,
622                                 "failed to allocate %u-th of %u mbuf "
623                                 "from mempool: %s\n",
624                                 nb_seg - i, nb_seg, mp->name);
625                         break;
626                 }
627
628                 md[--i] = p;
629                 if (rte_pktmbuf_tailroom(md[i]) < seglen[i]) {
630                         TESTPMD_LOG(ERR, "mempool %s, %u-th segment: "
631                                 "expected seglen: %u, "
632                                 "actual mbuf tailroom: %u\n",
633                                 mp->name, i, seglen[i],
634                                 rte_pktmbuf_tailroom(md[i]));
635                         break;
636                 }
637         }
638
639         /* all mbufs successfully allocated, do copy */
640         if (i == 0) {
641                 rc = mbuf_copy_split(pkt, md, seglen, nb_seg);
642                 if (rc < 0)
643                         TESTPMD_LOG(ERR,
644                                 "mbuf_copy_split for %p(len=%u, nb_seg=%u) "
645                                 "into %u segments failed with error code: %d\n",
646                                 pkt, pkt->pkt_len, pkt->nb_segs, nb_seg, rc);
647
648                 /* figure out how many mbufs to free. */
649                 i = RTE_MAX(rc, 0);
650         }
651
652         /* free unused mbufs */
653         for (; i != nb_seg; i++) {
654                 rte_pktmbuf_free_seg(md[i]);
655                 md[i] = NULL;
656         }
657
658         return md[0];
659 }
660
661 /*
662  * Receive a burst of packets, and for each packet:
663  *  - parse packet, and try to recognize a supported packet type (1)
664  *  - if it's not a supported packet type, don't touch the packet, else:
665  *  - reprocess the checksum of all supported layers. This is done in SW
666  *    or HW, depending on testpmd command line configuration
667  *  - if TSO is enabled in testpmd command line, also flag the mbuf for TCP
668  *    segmentation offload (this implies HW TCP checksum)
669  * Then transmit packets on the output port.
670  *
671  * (1) Supported packets are:
672  *   Ether / (vlan) / IP|IP6 / UDP|TCP|SCTP .
673  *   Ether / (vlan) / outer IP|IP6 / outer UDP / VxLAN / Ether / IP|IP6 /
674  *           UDP|TCP|SCTP
675  *   Ether / (vlan) / outer IP|IP6 / outer UDP / VXLAN-GPE / Ether / IP|IP6 /
676  *           UDP|TCP|SCTP
677  *   Ether / (vlan) / outer IP|IP6 / outer UDP / VXLAN-GPE / IP|IP6 /
678  *           UDP|TCP|SCTP
679  *   Ether / (vlan) / outer IP|IP6 / GRE / Ether / IP|IP6 / UDP|TCP|SCTP
680  *   Ether / (vlan) / outer IP|IP6 / GRE / IP|IP6 / UDP|TCP|SCTP
681  *   Ether / (vlan) / outer IP|IP6 / IP|IP6 / UDP|TCP|SCTP
682  *
683  * The testpmd command line for this forward engine sets the flags
684  * TESTPMD_TX_OFFLOAD_* in ports[tx_port].tx_ol_flags. They control
685  * wether a checksum must be calculated in software or in hardware. The
686  * IP, UDP, TCP and SCTP flags always concern the inner layer. The
687  * OUTER_IP is only useful for tunnel packets.
688  */
689 static void
690 pkt_burst_checksum_forward(struct fwd_stream *fs)
691 {
692         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
693         struct rte_mbuf *gso_segments[GSO_MAX_PKT_BURST];
694         struct rte_gso_ctx *gso_ctx;
695         struct rte_mbuf **tx_pkts_burst;
696         struct rte_port *txp;
697         struct rte_mbuf *m, *p;
698         struct rte_ether_hdr *eth_hdr;
699         void *l3_hdr = NULL, *outer_l3_hdr = NULL; /* can be IPv4 or IPv6 */
700         void **gro_ctx;
701         uint16_t gro_pkts_num;
702         uint8_t gro_enable;
703         uint16_t nb_rx;
704         uint16_t nb_tx;
705         uint16_t nb_prep;
706         uint16_t i;
707         uint64_t rx_ol_flags, tx_ol_flags;
708         uint64_t tx_offloads;
709         uint32_t retry;
710         uint32_t rx_bad_ip_csum;
711         uint32_t rx_bad_l4_csum;
712         uint32_t rx_bad_outer_l4_csum;
713         struct testpmd_offload_info info;
714         uint16_t nb_segments = 0;
715         int ret;
716
717 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
718         uint64_t start_tsc;
719         uint64_t end_tsc;
720         uint64_t core_cycles;
721 #endif
722
723 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
724         start_tsc = rte_rdtsc();
725 #endif
726
727         /* receive a burst of packet */
728         nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
729                                  nb_pkt_per_burst);
730         if (unlikely(nb_rx == 0))
731                 return;
732 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
733         fs->rx_burst_stats.pkt_burst_spread[nb_rx]++;
734 #endif
735         fs->rx_packets += nb_rx;
736         rx_bad_ip_csum = 0;
737         rx_bad_l4_csum = 0;
738         rx_bad_outer_l4_csum = 0;
739         gro_enable = gro_ports[fs->rx_port].enable;
740
741         txp = &ports[fs->tx_port];
742         tx_offloads = txp->dev_conf.txmode.offloads;
743         memset(&info, 0, sizeof(info));
744         info.tso_segsz = txp->tso_segsz;
745         info.tunnel_tso_segsz = txp->tunnel_tso_segsz;
746         if (gso_ports[fs->tx_port].enable)
747                 info.gso_enable = 1;
748
749         for (i = 0; i < nb_rx; i++) {
750                 if (likely(i < nb_rx - 1))
751                         rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[i + 1],
752                                                        void *));
753
754                 m = pkts_burst[i];
755                 info.is_tunnel = 0;
756                 info.pkt_len = rte_pktmbuf_pkt_len(m);
757                 tx_ol_flags = m->ol_flags &
758                               (IND_ATTACHED_MBUF | EXT_ATTACHED_MBUF);
759                 rx_ol_flags = m->ol_flags;
760
761                 /* Update the L3/L4 checksum error packet statistics */
762                 if ((rx_ol_flags & PKT_RX_IP_CKSUM_MASK) == PKT_RX_IP_CKSUM_BAD)
763                         rx_bad_ip_csum += 1;
764                 if ((rx_ol_flags & PKT_RX_L4_CKSUM_MASK) == PKT_RX_L4_CKSUM_BAD)
765                         rx_bad_l4_csum += 1;
766                 if (rx_ol_flags & PKT_RX_OUTER_L4_CKSUM_BAD)
767                         rx_bad_outer_l4_csum += 1;
768
769                 /* step 1: dissect packet, parsing optional vlan, ip4/ip6, vxlan
770                  * and inner headers */
771
772                 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
773                 rte_ether_addr_copy(&peer_eth_addrs[fs->peer_addr],
774                                 &eth_hdr->d_addr);
775                 rte_ether_addr_copy(&ports[fs->tx_port].eth_addr,
776                                 &eth_hdr->s_addr);
777                 parse_ethernet(eth_hdr, &info);
778                 l3_hdr = (char *)eth_hdr + info.l2_len;
779
780                 /* check if it's a supported tunnel */
781                 if (txp->parse_tunnel) {
782                         if (info.l4_proto == IPPROTO_UDP) {
783                                 struct udp_hdr *udp_hdr;
784
785                                 udp_hdr = (struct udp_hdr *)((char *)l3_hdr +
786                                         info.l3_len);
787                                 parse_vxlan_gpe(udp_hdr, &info);
788                                 if (info.is_tunnel) {
789                                         tx_ol_flags |= PKT_TX_TUNNEL_VXLAN_GPE;
790                                 } else {
791                                         parse_vxlan(udp_hdr, &info,
792                                                     m->packet_type);
793                                         if (info.is_tunnel)
794                                                 tx_ol_flags |=
795                                                         PKT_TX_TUNNEL_VXLAN;
796                                 }
797                         } else if (info.l4_proto == IPPROTO_GRE) {
798                                 struct simple_gre_hdr *gre_hdr;
799
800                                 gre_hdr = (struct simple_gre_hdr *)
801                                         ((char *)l3_hdr + info.l3_len);
802                                 parse_gre(gre_hdr, &info);
803                                 if (info.is_tunnel)
804                                         tx_ol_flags |= PKT_TX_TUNNEL_GRE;
805                         } else if (info.l4_proto == IPPROTO_IPIP) {
806                                 void *encap_ip_hdr;
807
808                                 encap_ip_hdr = (char *)l3_hdr + info.l3_len;
809                                 parse_encap_ip(encap_ip_hdr, &info);
810                                 if (info.is_tunnel)
811                                         tx_ol_flags |= PKT_TX_TUNNEL_IPIP;
812                         }
813                 }
814
815                 /* update l3_hdr and outer_l3_hdr if a tunnel was parsed */
816                 if (info.is_tunnel) {
817                         outer_l3_hdr = l3_hdr;
818                         l3_hdr = (char *)l3_hdr + info.outer_l3_len + info.l2_len;
819                 }
820
821                 /* step 2: depending on user command line configuration,
822                  * recompute checksum either in software or flag the
823                  * mbuf to offload the calculation to the NIC. If TSO
824                  * is configured, prepare the mbuf for TCP segmentation. */
825
826                 /* process checksums of inner headers first */
827                 tx_ol_flags |= process_inner_cksums(l3_hdr, &info,
828                         tx_offloads);
829
830                 /* Then process outer headers if any. Note that the software
831                  * checksum will be wrong if one of the inner checksums is
832                  * processed in hardware. */
833                 if (info.is_tunnel == 1) {
834                         tx_ol_flags |= process_outer_cksums(outer_l3_hdr, &info,
835                                         tx_offloads,
836                                         !!(tx_ol_flags & PKT_TX_TCP_SEG));
837                 }
838
839                 /* step 3: fill the mbuf meta data (flags and header lengths) */
840
841                 m->tx_offload = 0;
842                 if (info.is_tunnel == 1) {
843                         if (info.tunnel_tso_segsz ||
844                             (tx_offloads &
845                              DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM) ||
846                             (tx_offloads &
847                              DEV_TX_OFFLOAD_OUTER_UDP_CKSUM) ||
848                             (tx_ol_flags & PKT_TX_OUTER_IPV6)) {
849                                 m->outer_l2_len = info.outer_l2_len;
850                                 m->outer_l3_len = info.outer_l3_len;
851                                 m->l2_len = info.l2_len;
852                                 m->l3_len = info.l3_len;
853                                 m->l4_len = info.l4_len;
854                                 m->tso_segsz = info.tunnel_tso_segsz;
855                         }
856                         else {
857                                 /* if there is a outer UDP cksum
858                                    processed in sw and the inner in hw,
859                                    the outer checksum will be wrong as
860                                    the payload will be modified by the
861                                    hardware */
862                                 m->l2_len = info.outer_l2_len +
863                                         info.outer_l3_len + info.l2_len;
864                                 m->l3_len = info.l3_len;
865                                 m->l4_len = info.l4_len;
866                         }
867                 } else {
868                         /* this is only useful if an offload flag is
869                          * set, but it does not hurt to fill it in any
870                          * case */
871                         m->l2_len = info.l2_len;
872                         m->l3_len = info.l3_len;
873                         m->l4_len = info.l4_len;
874                         m->tso_segsz = info.tso_segsz;
875                 }
876                 m->ol_flags = tx_ol_flags;
877
878                 /* Do split & copy for the packet. */
879                 if (tx_pkt_split != TX_PKT_SPLIT_OFF) {
880                         p = pkt_copy_split(m);
881                         if (p != NULL) {
882                                 rte_pktmbuf_free(m);
883                                 m = p;
884                                 pkts_burst[i] = m;
885                         }
886                 }
887
888                 /* if verbose mode is enabled, dump debug info */
889                 if (verbose_level > 0) {
890                         char buf[256];
891
892                         printf("-----------------\n");
893                         printf("port=%u, mbuf=%p, pkt_len=%u, nb_segs=%u:\n",
894                                 fs->rx_port, m, m->pkt_len, m->nb_segs);
895                         /* dump rx parsed packet info */
896                         rte_get_rx_ol_flag_list(rx_ol_flags, buf, sizeof(buf));
897                         printf("rx: l2_len=%d ethertype=%x l3_len=%d "
898                                 "l4_proto=%d l4_len=%d flags=%s\n",
899                                 info.l2_len, rte_be_to_cpu_16(info.ethertype),
900                                 info.l3_len, info.l4_proto, info.l4_len, buf);
901                         if (rx_ol_flags & PKT_RX_LRO)
902                                 printf("rx: m->lro_segsz=%u\n", m->tso_segsz);
903                         if (info.is_tunnel == 1)
904                                 printf("rx: outer_l2_len=%d outer_ethertype=%x "
905                                         "outer_l3_len=%d\n", info.outer_l2_len,
906                                         rte_be_to_cpu_16(info.outer_ethertype),
907                                         info.outer_l3_len);
908                         /* dump tx packet info */
909                         if ((tx_offloads & (DEV_TX_OFFLOAD_IPV4_CKSUM |
910                                             DEV_TX_OFFLOAD_UDP_CKSUM |
911                                             DEV_TX_OFFLOAD_TCP_CKSUM |
912                                             DEV_TX_OFFLOAD_SCTP_CKSUM)) ||
913                                 info.tso_segsz != 0)
914                                 printf("tx: m->l2_len=%d m->l3_len=%d "
915                                         "m->l4_len=%d\n",
916                                         m->l2_len, m->l3_len, m->l4_len);
917                         if (info.is_tunnel == 1) {
918                                 if ((tx_offloads &
919                                     DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM) ||
920                                     (tx_offloads &
921                                     DEV_TX_OFFLOAD_OUTER_UDP_CKSUM) ||
922                                     (tx_ol_flags & PKT_TX_OUTER_IPV6))
923                                         printf("tx: m->outer_l2_len=%d "
924                                                 "m->outer_l3_len=%d\n",
925                                                 m->outer_l2_len,
926                                                 m->outer_l3_len);
927                                 if (info.tunnel_tso_segsz != 0 &&
928                                                 (m->ol_flags & PKT_TX_TCP_SEG))
929                                         printf("tx: m->tso_segsz=%d\n",
930                                                 m->tso_segsz);
931                         } else if (info.tso_segsz != 0 &&
932                                         (m->ol_flags & PKT_TX_TCP_SEG))
933                                 printf("tx: m->tso_segsz=%d\n", m->tso_segsz);
934                         rte_get_tx_ol_flag_list(m->ol_flags, buf, sizeof(buf));
935                         printf("tx: flags=%s", buf);
936                         printf("\n");
937                 }
938         }
939
940         if (unlikely(gro_enable)) {
941                 if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) {
942                         nb_rx = rte_gro_reassemble_burst(pkts_burst, nb_rx,
943                                         &(gro_ports[fs->rx_port].param));
944                 } else {
945                         gro_ctx = current_fwd_lcore()->gro_ctx;
946                         nb_rx = rte_gro_reassemble(pkts_burst, nb_rx, gro_ctx);
947
948                         if (++fs->gro_times >= gro_flush_cycles) {
949                                 gro_pkts_num = rte_gro_get_pkt_count(gro_ctx);
950                                 if (gro_pkts_num > MAX_PKT_BURST - nb_rx)
951                                         gro_pkts_num = MAX_PKT_BURST - nb_rx;
952
953                                 nb_rx += rte_gro_timeout_flush(gro_ctx, 0,
954                                                 RTE_GRO_TCP_IPV4,
955                                                 &pkts_burst[nb_rx],
956                                                 gro_pkts_num);
957                                 fs->gro_times = 0;
958                         }
959                 }
960         }
961
962         if (gso_ports[fs->tx_port].enable == 0)
963                 tx_pkts_burst = pkts_burst;
964         else {
965                 gso_ctx = &(current_fwd_lcore()->gso_ctx);
966                 gso_ctx->gso_size = gso_max_segment_size;
967                 for (i = 0; i < nb_rx; i++) {
968                         ret = rte_gso_segment(pkts_burst[i], gso_ctx,
969                                         &gso_segments[nb_segments],
970                                         GSO_MAX_PKT_BURST - nb_segments);
971                         if (ret >= 0)
972                                 nb_segments += ret;
973                         else {
974                                 TESTPMD_LOG(DEBUG, "Unable to segment packet");
975                                 rte_pktmbuf_free(pkts_burst[i]);
976                         }
977                 }
978
979                 tx_pkts_burst = gso_segments;
980                 nb_rx = nb_segments;
981         }
982
983         nb_prep = rte_eth_tx_prepare(fs->tx_port, fs->tx_queue,
984                         tx_pkts_burst, nb_rx);
985         if (nb_prep != nb_rx)
986                 printf("Preparing packet burst to transmit failed: %s\n",
987                                 rte_strerror(rte_errno));
988
989         nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, tx_pkts_burst,
990                         nb_prep);
991
992         /*
993          * Retry if necessary
994          */
995         if (unlikely(nb_tx < nb_rx) && fs->retry_enabled) {
996                 retry = 0;
997                 while (nb_tx < nb_rx && retry++ < burst_tx_retry_num) {
998                         rte_delay_us(burst_tx_delay_time);
999                         nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
1000                                         &tx_pkts_burst[nb_tx], nb_rx - nb_tx);
1001                 }
1002         }
1003         fs->tx_packets += nb_tx;
1004         fs->rx_bad_ip_csum += rx_bad_ip_csum;
1005         fs->rx_bad_l4_csum += rx_bad_l4_csum;
1006         fs->rx_bad_outer_l4_csum += rx_bad_outer_l4_csum;
1007
1008 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
1009         fs->tx_burst_stats.pkt_burst_spread[nb_tx]++;
1010 #endif
1011         if (unlikely(nb_tx < nb_rx)) {
1012                 fs->fwd_dropped += (nb_rx - nb_tx);
1013                 do {
1014                         rte_pktmbuf_free(tx_pkts_burst[nb_tx]);
1015                 } while (++nb_tx < nb_rx);
1016         }
1017
1018 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1019         end_tsc = rte_rdtsc();
1020         core_cycles = (end_tsc - start_tsc);
1021         fs->core_cycles = (uint64_t) (fs->core_cycles + core_cycles);
1022 #endif
1023 }
1024
1025 struct fwd_engine csum_fwd_engine = {
1026         .fwd_mode_name  = "csum",
1027         .port_fwd_begin = NULL,
1028         .port_fwd_end   = NULL,
1029         .packet_fwd     = pkt_burst_checksum_forward,
1030 };