tso.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/export.h>
  3. #include <linux/if_vlan.h>
  4. #include <net/ip.h>
  5. #include <net/tso.h>
  6. #include <asm/unaligned.h>
  7. /* Calculate expected number of TX descriptors */
  8. int tso_count_descs(const struct sk_buff *skb)
  9. {
  10. /* The Marvell Way */
  11. return skb_shinfo(skb)->gso_segs * 2 + skb_shinfo(skb)->nr_frags;
  12. }
  13. EXPORT_SYMBOL(tso_count_descs);
  14. void tso_build_hdr(const struct sk_buff *skb, char *hdr, struct tso_t *tso,
  15. int size, bool is_last)
  16. {
  17. int hdr_len = skb_transport_offset(skb) + tso->tlen;
  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 + tso->tlen);
  28. }
  29. hdr += skb_transport_offset(skb);
  30. if (tso->tlen != sizeof(struct udphdr)) {
  31. struct tcphdr *tcph = (struct tcphdr *)hdr;
  32. put_unaligned_be32(tso->tcp_seq, &tcph->seq);
  33. if (!is_last) {
  34. /* Clear all special flags for not last packet */
  35. tcph->psh = 0;
  36. tcph->fin = 0;
  37. tcph->rst = 0;
  38. }
  39. } else {
  40. struct udphdr *uh = (struct udphdr *)hdr;
  41. uh->len = htons(sizeof(*uh) + size);
  42. }
  43. }
  44. EXPORT_SYMBOL(tso_build_hdr);
  45. void tso_build_data(const struct sk_buff *skb, struct tso_t *tso, int size)
  46. {
  47. tso->tcp_seq += size; /* not worth avoiding this operation for UDP */
  48. tso->size -= size;
  49. tso->data += size;
  50. if ((tso->size == 0) &&
  51. (tso->next_frag_idx < skb_shinfo(skb)->nr_frags)) {
  52. skb_frag_t *frag = &skb_shinfo(skb)->frags[tso->next_frag_idx];
  53. /* Move to next segment */
  54. tso->size = skb_frag_size(frag);
  55. tso->data = skb_frag_address(frag);
  56. tso->next_frag_idx++;
  57. }
  58. }
  59. EXPORT_SYMBOL(tso_build_data);
  60. int tso_start(struct sk_buff *skb, struct tso_t *tso)
  61. {
  62. int tlen = skb_is_gso_tcp(skb) ? tcp_hdrlen(skb) : sizeof(struct udphdr);
  63. int hdr_len = skb_transport_offset(skb) + tlen;
  64. tso->tlen = tlen;
  65. tso->ip_id = ntohs(ip_hdr(skb)->id);
  66. tso->tcp_seq = (tlen != sizeof(struct udphdr)) ? ntohl(tcp_hdr(skb)->seq) : 0;
  67. tso->next_frag_idx = 0;
  68. tso->ipv6 = vlan_get_protocol(skb) == htons(ETH_P_IPV6);
  69. /* Build first data */
  70. tso->size = skb_headlen(skb) - hdr_len;
  71. tso->data = skb->data + hdr_len;
  72. if ((tso->size == 0) &&
  73. (tso->next_frag_idx < skb_shinfo(skb)->nr_frags)) {
  74. skb_frag_t *frag = &skb_shinfo(skb)->frags[tso->next_frag_idx];
  75. /* Move to next segment */
  76. tso->size = skb_frag_size(frag);
  77. tso->data = skb_frag_address(frag);
  78. tso->next_frag_idx++;
  79. }
  80. return hdr_len;
  81. }
  82. EXPORT_SYMBOL(tso_start);