Sachin Kamat | 484611e | 2014-05-26 14:52:46 +0530 | [diff] [blame] | 1 | #include <linux/export.h> |
emmanuel.grumbach@intel.com | 8941faa | 2015-10-26 10:31:29 +0200 | [diff] [blame] | 2 | #include <linux/if_vlan.h> |
Ezequiel Garcia | e876f20 | 2014-05-19 13:59:52 -0300 | [diff] [blame] | 3 | #include <net/ip.h> |
| 4 | #include <net/tso.h> |
Karl Beldan | a63ba13 | 2014-10-21 16:06:05 +0200 | [diff] [blame] | 5 | #include <asm/unaligned.h> |
Ezequiel Garcia | e876f20 | 2014-05-19 13:59:52 -0300 | [diff] [blame] | 6 | |
| 7 | /* Calculate expected number of TX descriptors */ |
| 8 | int tso_count_descs(struct sk_buff *skb) |
| 9 | { |
| 10 | /* The Marvell Way */ |
| 11 | return skb_shinfo(skb)->gso_segs * 2 + skb_shinfo(skb)->nr_frags; |
| 12 | } |
Sachin Kamat | 484611e | 2014-05-26 14:52:46 +0530 | [diff] [blame] | 13 | EXPORT_SYMBOL(tso_count_descs); |
Ezequiel Garcia | e876f20 | 2014-05-19 13:59:52 -0300 | [diff] [blame] | 14 | |
| 15 | void tso_build_hdr(struct sk_buff *skb, char *hdr, struct tso_t *tso, |
| 16 | int size, bool is_last) |
| 17 | { |
Ezequiel Garcia | e876f20 | 2014-05-19 13:59:52 -0300 | [diff] [blame] | 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); |
emmanuel.grumbach@intel.com | 8941faa | 2015-10-26 10:31:29 +0200 | [diff] [blame] | 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 | } |
Ezequiel Garcia | e876f20 | 2014-05-19 13:59:52 -0300 | [diff] [blame] | 34 | tcph = (struct tcphdr *)(hdr + skb_transport_offset(skb)); |
Karl Beldan | a63ba13 | 2014-10-21 16:06:05 +0200 | [diff] [blame] | 35 | put_unaligned_be32(tso->tcp_seq, &tcph->seq); |
Ezequiel Garcia | e876f20 | 2014-05-19 13:59:52 -0300 | [diff] [blame] | 36 | |
| 37 | if (!is_last) { |
| 38 | /* Clear all special flags for not last packet */ |
| 39 | tcph->psh = 0; |
| 40 | tcph->fin = 0; |
| 41 | tcph->rst = 0; |
| 42 | } |
| 43 | } |
Sachin Kamat | 484611e | 2014-05-26 14:52:46 +0530 | [diff] [blame] | 44 | EXPORT_SYMBOL(tso_build_hdr); |
Ezequiel Garcia | e876f20 | 2014-05-19 13:59:52 -0300 | [diff] [blame] | 45 | |
| 46 | void 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 = &skb_shinfo(skb)->frags[tso->next_frag_idx]; |
| 55 | |
| 56 | /* Move to next segment */ |
| 57 | tso->size = frag->size; |
| 58 | tso->data = page_address(frag->page.p) + frag->page_offset; |
| 59 | tso->next_frag_idx++; |
| 60 | } |
| 61 | } |
Sachin Kamat | 484611e | 2014-05-26 14:52:46 +0530 | [diff] [blame] | 62 | EXPORT_SYMBOL(tso_build_data); |
Ezequiel Garcia | e876f20 | 2014-05-19 13:59:52 -0300 | [diff] [blame] | 63 | |
| 64 | void tso_start(struct sk_buff *skb, struct tso_t *tso) |
| 65 | { |
| 66 | int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); |
| 67 | |
| 68 | tso->ip_id = ntohs(ip_hdr(skb)->id); |
| 69 | tso->tcp_seq = ntohl(tcp_hdr(skb)->seq); |
| 70 | tso->next_frag_idx = 0; |
emmanuel.grumbach@intel.com | 8941faa | 2015-10-26 10:31:29 +0200 | [diff] [blame] | 71 | tso->ipv6 = vlan_get_protocol(skb) == htons(ETH_P_IPV6); |
Ezequiel Garcia | e876f20 | 2014-05-19 13:59:52 -0300 | [diff] [blame] | 72 | |
| 73 | /* Build first data */ |
| 74 | tso->size = skb_headlen(skb) - hdr_len; |
| 75 | tso->data = skb->data + hdr_len; |
| 76 | if ((tso->size == 0) && |
| 77 | (tso->next_frag_idx < skb_shinfo(skb)->nr_frags)) { |
| 78 | skb_frag_t *frag = &skb_shinfo(skb)->frags[tso->next_frag_idx]; |
| 79 | |
| 80 | /* Move to next segment */ |
| 81 | tso->size = frag->size; |
| 82 | tso->data = page_address(frag->page.p) + frag->page_offset; |
| 83 | tso->next_frag_idx++; |
| 84 | } |
| 85 | } |
Sachin Kamat | 484611e | 2014-05-26 14:52:46 +0530 | [diff] [blame] | 86 | EXPORT_SYMBOL(tso_start); |