1#include <linux/export.h>
2#include <linux/if_vlan.h>
3#include <net/ip.h>
4#include <net/tso.h>
5#include <asm/unaligned.h>
6
7
8int tso_count_descs(struct sk_buff *skb)
9{
10
11 return skb_shinfo(skb)->gso_segs * 2 + skb_shinfo(skb)->nr_frags;
12}
13EXPORT_SYMBOL(tso_count_descs);
14
15void tso_build_hdr(struct sk_buff *skb, char *hdr, struct tso_t *tso,
16 int size, bool is_last)
17{
18 struct tcphdr *tcph;
19 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
20 int mac_hdr_len = skb_network_offset(skb);
21
22 memcpy(hdr, skb->data, hdr_len);
23 if (!tso->ipv6) {
24 struct iphdr *iph = (void *)(hdr + mac_hdr_len);
25
26 iph->id = htons(tso->ip_id);
27 iph->tot_len = htons(size + hdr_len - mac_hdr_len);
28 tso->ip_id++;
29 } else {
30 struct ipv6hdr *iph = (void *)(hdr + mac_hdr_len);
31
32 iph->payload_len = htons(size + tcp_hdrlen(skb));
33 }
34 tcph = (struct tcphdr *)(hdr + skb_transport_offset(skb));
35 put_unaligned_be32(tso->tcp_seq, &tcph->seq);
36
37 if (!is_last) {
38
39 tcph->psh = 0;
40 tcph->fin = 0;
41 tcph->rst = 0;
42 }
43}
44EXPORT_SYMBOL(tso_build_hdr);
45
46void tso_build_data(struct sk_buff *skb, struct tso_t *tso, int size)
47{
48 tso->tcp_seq += size;
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;
55
56 gmb();
57 frag = &skb_shinfo(skb)->frags[tso->next_frag_idx];
58
59
60 tso->size = frag->size;
61 tso->data = page_address(frag->page.p) + frag->page_offset;
62 tso->next_frag_idx++;
63 }
64}
65EXPORT_SYMBOL(tso_build_data);
66
67void tso_start(struct sk_buff *skb, struct tso_t *tso)
68{
69 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
70
71 tso->ip_id = ntohs(ip_hdr(skb)->id);
72 tso->tcp_seq = ntohl(tcp_hdr(skb)->seq);
73 tso->next_frag_idx = 0;
74 tso->ipv6 = vlan_get_protocol(skb) == htons(ETH_P_IPV6);
75
76
77 tso->size = skb_headlen(skb) - hdr_len;
78 tso->data = skb->data + hdr_len;
79 if ((tso->size == 0) &&
80 (tso->next_frag_idx < skb_shinfo(skb)->nr_frags)) {
81 skb_frag_t *frag;
82
83 gmb();
84 frag = &skb_shinfo(skb)->frags[tso->next_frag_idx];
85
86 tso->size = frag->size;
87 tso->data = page_address(frag->page.p) + frag->page_offset;
88 tso->next_frag_idx++;
89 }
90}
91EXPORT_SYMBOL(tso_start);
92