xref: /openbmc/linux/net/core/tso.c (revision d7b061b8)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2484611e5SSachin Kamat #include <linux/export.h>
38941faa1Semmanuel.grumbach@intel.com #include <linux/if_vlan.h>
4e876f208SEzequiel Garcia #include <net/ip.h>
5e876f208SEzequiel Garcia #include <net/tso.h>
6a63ba13eSKarl Beldan #include <asm/unaligned.h>
7e876f208SEzequiel Garcia 
tso_build_hdr(const struct sk_buff * skb,char * hdr,struct tso_t * tso,int size,bool is_last)8504b9121SEric Dumazet void tso_build_hdr(const struct sk_buff *skb, char *hdr, struct tso_t *tso,
9e876f208SEzequiel Garcia 		   int size, bool is_last)
10e876f208SEzequiel Garcia {
11761b331cSEric Dumazet 	int hdr_len = skb_transport_offset(skb) + tso->tlen;
12e876f208SEzequiel Garcia 	int mac_hdr_len = skb_network_offset(skb);
13e876f208SEzequiel Garcia 
14e876f208SEzequiel Garcia 	memcpy(hdr, skb->data, hdr_len);
158941faa1Semmanuel.grumbach@intel.com 	if (!tso->ipv6) {
168941faa1Semmanuel.grumbach@intel.com 		struct iphdr *iph = (void *)(hdr + mac_hdr_len);
178941faa1Semmanuel.grumbach@intel.com 
18e876f208SEzequiel Garcia 		iph->id = htons(tso->ip_id);
19e876f208SEzequiel Garcia 		iph->tot_len = htons(size + hdr_len - mac_hdr_len);
208941faa1Semmanuel.grumbach@intel.com 		tso->ip_id++;
218941faa1Semmanuel.grumbach@intel.com 	} else {
228941faa1Semmanuel.grumbach@intel.com 		struct ipv6hdr *iph = (void *)(hdr + mac_hdr_len);
238941faa1Semmanuel.grumbach@intel.com 
24761b331cSEric Dumazet 		iph->payload_len = htons(size + tso->tlen);
258941faa1Semmanuel.grumbach@intel.com 	}
263d5b459bSEric Dumazet 	hdr += skb_transport_offset(skb);
273d5b459bSEric Dumazet 	if (tso->tlen != sizeof(struct udphdr)) {
283d5b459bSEric Dumazet 		struct tcphdr *tcph = (struct tcphdr *)hdr;
293d5b459bSEric Dumazet 
30a63ba13eSKarl Beldan 		put_unaligned_be32(tso->tcp_seq, &tcph->seq);
31e876f208SEzequiel Garcia 
32e876f208SEzequiel Garcia 		if (!is_last) {
33e876f208SEzequiel Garcia 			/* Clear all special flags for not last packet */
34e876f208SEzequiel Garcia 			tcph->psh = 0;
35e876f208SEzequiel Garcia 			tcph->fin = 0;
36e876f208SEzequiel Garcia 			tcph->rst = 0;
37e876f208SEzequiel Garcia 		}
383d5b459bSEric Dumazet 	} else {
393d5b459bSEric Dumazet 		struct udphdr *uh = (struct udphdr *)hdr;
403d5b459bSEric Dumazet 
413d5b459bSEric Dumazet 		uh->len = htons(sizeof(*uh) + size);
423d5b459bSEric Dumazet 	}
43e876f208SEzequiel Garcia }
44484611e5SSachin Kamat EXPORT_SYMBOL(tso_build_hdr);
45e876f208SEzequiel Garcia 
tso_build_data(const struct sk_buff * skb,struct tso_t * tso,int size)46504b9121SEric Dumazet void tso_build_data(const struct sk_buff *skb, struct tso_t *tso, int size)
47e876f208SEzequiel Garcia {
483d5b459bSEric Dumazet 	tso->tcp_seq += size; /* not worth avoiding this operation for UDP */
49e876f208SEzequiel Garcia 	tso->size -= size;
50e876f208SEzequiel Garcia 	tso->data += size;
51e876f208SEzequiel Garcia 
52e876f208SEzequiel Garcia 	if ((tso->size == 0) &&
53e876f208SEzequiel Garcia 	    (tso->next_frag_idx < skb_shinfo(skb)->nr_frags)) {
54e876f208SEzequiel Garcia 		skb_frag_t *frag = &skb_shinfo(skb)->frags[tso->next_frag_idx];
55e876f208SEzequiel Garcia 
56e876f208SEzequiel Garcia 		/* Move to next segment */
57d8e18a51SMatthew Wilcox (Oracle) 		tso->size = skb_frag_size(frag);
58d8e18a51SMatthew Wilcox (Oracle) 		tso->data = skb_frag_address(frag);
59e876f208SEzequiel Garcia 		tso->next_frag_idx++;
60e876f208SEzequiel Garcia 	}
61e876f208SEzequiel Garcia }
62484611e5SSachin Kamat EXPORT_SYMBOL(tso_build_data);
63e876f208SEzequiel Garcia 
tso_start(struct sk_buff * skb,struct tso_t * tso)64761b331cSEric Dumazet int tso_start(struct sk_buff *skb, struct tso_t *tso)
65e876f208SEzequiel Garcia {
663d5b459bSEric Dumazet 	int tlen = skb_is_gso_tcp(skb) ? tcp_hdrlen(skb) : sizeof(struct udphdr);
67761b331cSEric Dumazet 	int hdr_len = skb_transport_offset(skb) + tlen;
68e876f208SEzequiel Garcia 
69761b331cSEric Dumazet 	tso->tlen = tlen;
70e876f208SEzequiel Garcia 	tso->ip_id = ntohs(ip_hdr(skb)->id);
713d5b459bSEric Dumazet 	tso->tcp_seq = (tlen != sizeof(struct udphdr)) ? ntohl(tcp_hdr(skb)->seq) : 0;
72e876f208SEzequiel Garcia 	tso->next_frag_idx = 0;
738941faa1Semmanuel.grumbach@intel.com 	tso->ipv6 = vlan_get_protocol(skb) == htons(ETH_P_IPV6);
74e876f208SEzequiel Garcia 
75e876f208SEzequiel Garcia 	/* Build first data */
76e876f208SEzequiel Garcia 	tso->size = skb_headlen(skb) - hdr_len;
77e876f208SEzequiel Garcia 	tso->data = skb->data + hdr_len;
78e876f208SEzequiel Garcia 	if ((tso->size == 0) &&
79e876f208SEzequiel Garcia 	    (tso->next_frag_idx < skb_shinfo(skb)->nr_frags)) {
80e876f208SEzequiel Garcia 		skb_frag_t *frag = &skb_shinfo(skb)->frags[tso->next_frag_idx];
81e876f208SEzequiel Garcia 
82e876f208SEzequiel Garcia 		/* Move to next segment */
83d8e18a51SMatthew Wilcox (Oracle) 		tso->size = skb_frag_size(frag);
84d8e18a51SMatthew Wilcox (Oracle) 		tso->data = skb_frag_address(frag);
85e876f208SEzequiel Garcia 		tso->next_frag_idx++;
86e876f208SEzequiel Garcia 	}
87761b331cSEric Dumazet 	return hdr_len;
88e876f208SEzequiel Garcia }
89484611e5SSachin Kamat EXPORT_SYMBOL(tso_start);
90