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