Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
484611e5 | 2 | #include <linux/export.h> |
8941faa1 | 3 | #include <linux/if_vlan.h> |
e876f208 EG |
4 | #include <net/ip.h> |
5 | #include <net/tso.h> | |
a63ba13e | 6 | #include <asm/unaligned.h> |
e876f208 | 7 | |
504b9121 | 8 | void tso_build_hdr(const struct sk_buff *skb, char *hdr, struct tso_t *tso, |
e876f208 EG |
9 | int size, bool is_last) |
10 | { | |
761b331c | 11 | int hdr_len = skb_transport_offset(skb) + tso->tlen; |
e876f208 EG |
12 | int mac_hdr_len = skb_network_offset(skb); |
13 | ||
14 | memcpy(hdr, skb->data, hdr_len); | |
8941faa1 | 15 | if (!tso->ipv6) { |
16 | struct iphdr *iph = (void *)(hdr + mac_hdr_len); | |
17 | ||
18 | iph->id = htons(tso->ip_id); | |
19 | iph->tot_len = htons(size + hdr_len - mac_hdr_len); | |
20 | tso->ip_id++; | |
21 | } else { | |
22 | struct ipv6hdr *iph = (void *)(hdr + mac_hdr_len); | |
23 | ||
761b331c | 24 | iph->payload_len = htons(size + tso->tlen); |
8941faa1 | 25 | } |
3d5b459b ED |
26 | hdr += skb_transport_offset(skb); |
27 | if (tso->tlen != sizeof(struct udphdr)) { | |
28 | struct tcphdr *tcph = (struct tcphdr *)hdr; | |
e876f208 | 29 | |
3d5b459b ED |
30 | put_unaligned_be32(tso->tcp_seq, &tcph->seq); |
31 | ||
32 | if (!is_last) { | |
33 | /* Clear all special flags for not last packet */ | |
34 | tcph->psh = 0; | |
35 | tcph->fin = 0; | |
36 | tcph->rst = 0; | |
37 | } | |
38 | } else { | |
39 | struct udphdr *uh = (struct udphdr *)hdr; | |
40 | ||
41 | uh->len = htons(sizeof(*uh) + size); | |
e876f208 EG |
42 | } |
43 | } | |
484611e5 | 44 | EXPORT_SYMBOL(tso_build_hdr); |
e876f208 | 45 | |
504b9121 | 46 | void tso_build_data(const struct sk_buff *skb, struct tso_t *tso, int size) |
e876f208 | 47 | { |
3d5b459b | 48 | tso->tcp_seq += size; /* not worth avoiding this operation for UDP */ |
e876f208 EG |
49 | tso->size -= size; |
50 | tso->data += size; | |
51 | ||
52 | if ((tso->size == 0) && | |
53 | (tso->next_frag_idx < skb_shinfo(skb)->nr_frags)) { | |
54 | skb_frag_t *frag = &skb_shinfo(skb)->frags[tso->next_frag_idx]; | |
55 | ||
56 | /* Move to next segment */ | |
d8e18a51 MWO |
57 | tso->size = skb_frag_size(frag); |
58 | tso->data = skb_frag_address(frag); | |
e876f208 EG |
59 | tso->next_frag_idx++; |
60 | } | |
61 | } | |
484611e5 | 62 | EXPORT_SYMBOL(tso_build_data); |
e876f208 | 63 | |
761b331c | 64 | int tso_start(struct sk_buff *skb, struct tso_t *tso) |
e876f208 | 65 | { |
3d5b459b | 66 | int tlen = skb_is_gso_tcp(skb) ? tcp_hdrlen(skb) : sizeof(struct udphdr); |
761b331c | 67 | int hdr_len = skb_transport_offset(skb) + tlen; |
e876f208 | 68 | |
761b331c | 69 | tso->tlen = tlen; |
e876f208 | 70 | tso->ip_id = ntohs(ip_hdr(skb)->id); |
3d5b459b | 71 | tso->tcp_seq = (tlen != sizeof(struct udphdr)) ? ntohl(tcp_hdr(skb)->seq) : 0; |
e876f208 | 72 | tso->next_frag_idx = 0; |
8941faa1 | 73 | tso->ipv6 = vlan_get_protocol(skb) == htons(ETH_P_IPV6); |
e876f208 EG |
74 | |
75 | /* Build first data */ | |
76 | tso->size = skb_headlen(skb) - hdr_len; | |
77 | tso->data = skb->data + hdr_len; | |
78 | if ((tso->size == 0) && | |
79 | (tso->next_frag_idx < skb_shinfo(skb)->nr_frags)) { | |
80 | skb_frag_t *frag = &skb_shinfo(skb)->frags[tso->next_frag_idx]; | |
81 | ||
82 | /* Move to next segment */ | |
d8e18a51 MWO |
83 | tso->size = skb_frag_size(frag); |
84 | tso->data = skb_frag_address(frag); | |
e876f208 EG |
85 | tso->next_frag_idx++; |
86 | } | |
761b331c | 87 | return hdr_len; |
e876f208 | 88 | } |
484611e5 | 89 | EXPORT_SYMBOL(tso_start); |