tso.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /* Calculate expected number of TX descriptors */
  7. int tso_count_descs(struct sk_buff *skb)
  8. {
  9. /* The Marvell Way */
  10. return skb_shinfo(skb)->gso_segs * 2 + skb_shinfo(skb)->nr_frags;
  11. }
  12. EXPORT_SYMBOL(tso_count_descs);
  13. void tso_build_hdr(struct sk_buff *skb, char *hdr, struct tso_t *tso,
  14. int size, bool is_last)
  15. {
  16. struct tcphdr *tcph;
  17. int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  18. int mac_hdr_len = skb_network_offset(skb);
  19. memcpy(hdr, skb->data, hdr_len);
  20. if (!tso->ipv6) {
  21. struct iphdr *iph = (void *)(hdr + mac_hdr_len);
  22. iph->id = htons(tso->ip_id);
  23. iph->tot_len = htons(size + hdr_len - mac_hdr_len);
  24. tso->ip_id++;
  25. } else {
  26. struct ipv6hdr *iph = (void *)(hdr + mac_hdr_len);
  27. iph->payload_len = htons(size + tcp_hdrlen(skb));
  28. }
  29. tcph = (struct tcphdr *)(hdr + skb_transport_offset(skb));
  30. put_unaligned_be32(tso->tcp_seq, &tcph->seq);
  31. if (!is_last) {
  32. /* Clear all special flags for not last packet */
  33. tcph->psh = 0;
  34. tcph->fin = 0;
  35. tcph->rst = 0;
  36. }
  37. }
  38. EXPORT_SYMBOL(tso_build_hdr);
  39. void tso_build_data(struct sk_buff *skb, struct tso_t *tso, int size)
  40. {
  41. tso->tcp_seq += size;
  42. tso->size -= size;
  43. tso->data += size;
  44. if ((tso->size == 0) &&
  45. (tso->next_frag_idx < skb_shinfo(skb)->nr_frags)) {
  46. skb_frag_t *frag = &skb_shinfo(skb)->frags[tso->next_frag_idx];
  47. /* Move to next segment */
  48. tso->size = frag->size;
  49. tso->data = page_address(frag->page.p) + frag->page_offset;
  50. tso->next_frag_idx++;
  51. }
  52. }
  53. EXPORT_SYMBOL(tso_build_data);
  54. void tso_start(struct sk_buff *skb, struct tso_t *tso)
  55. {
  56. int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  57. tso->ip_id = ntohs(ip_hdr(skb)->id);
  58. tso->tcp_seq = ntohl(tcp_hdr(skb)->seq);
  59. tso->next_frag_idx = 0;
  60. tso->ipv6 = vlan_get_protocol(skb) == htons(ETH_P_IPV6);
  61. /* Build first data */
  62. tso->size = skb_headlen(skb) - hdr_len;
  63. tso->data = skb->data + hdr_len;
  64. if ((tso->size == 0) &&
  65. (tso->next_frag_idx < skb_shinfo(skb)->nr_frags)) {
  66. skb_frag_t *frag = &skb_shinfo(skb)->frags[tso->next_frag_idx];
  67. /* Move to next segment */
  68. tso->size = frag->size;
  69. tso->data = page_address(frag->page.p) + frag->page_offset;
  70. tso->next_frag_idx++;
  71. }
  72. }
  73. EXPORT_SYMBOL(tso_start);