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