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