tcp_offload.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * IPV4 GSO/GRO offload support
  3. * Linux INET implementation
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. *
  10. * TCPv4 GSO/GRO support
  11. */
  12. #include <linux/skbuff.h>
  13. #include <net/tcp.h>
  14. #include <net/protocol.h>
  15. struct sk_buff *tcp_tso_segment(struct sk_buff *skb,
  16. netdev_features_t features)
  17. {
  18. struct sk_buff *segs = ERR_PTR(-EINVAL);
  19. struct tcphdr *th;
  20. unsigned int thlen;
  21. unsigned int seq;
  22. __be32 delta;
  23. unsigned int oldlen;
  24. unsigned int mss;
  25. struct sk_buff *gso_skb = skb;
  26. __sum16 newcheck;
  27. bool ooo_okay, copy_destructor;
  28. if (!pskb_may_pull(skb, sizeof(*th)))
  29. goto out;
  30. th = tcp_hdr(skb);
  31. thlen = th->doff * 4;
  32. if (thlen < sizeof(*th))
  33. goto out;
  34. if (!pskb_may_pull(skb, thlen))
  35. goto out;
  36. oldlen = (u16)~skb->len;
  37. __skb_pull(skb, thlen);
  38. mss = tcp_skb_mss(skb);
  39. if (unlikely(skb->len <= mss))
  40. goto out;
  41. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  42. /* Packet is from an untrusted source, reset gso_segs. */
  43. int type = skb_shinfo(skb)->gso_type;
  44. if (unlikely(type &
  45. ~(SKB_GSO_TCPV4 |
  46. SKB_GSO_DODGY |
  47. SKB_GSO_TCP_ECN |
  48. SKB_GSO_TCPV6 |
  49. SKB_GSO_GRE |
  50. SKB_GSO_MPLS |
  51. SKB_GSO_UDP_TUNNEL |
  52. 0) ||
  53. !(type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))))
  54. goto out;
  55. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
  56. segs = NULL;
  57. goto out;
  58. }
  59. copy_destructor = gso_skb->destructor == tcp_wfree;
  60. ooo_okay = gso_skb->ooo_okay;
  61. /* All segments but the first should have ooo_okay cleared */
  62. skb->ooo_okay = 0;
  63. segs = skb_segment(skb, features);
  64. if (IS_ERR(segs))
  65. goto out;
  66. /* Only first segment might have ooo_okay set */
  67. segs->ooo_okay = ooo_okay;
  68. delta = htonl(oldlen + (thlen + mss));
  69. skb = segs;
  70. th = tcp_hdr(skb);
  71. seq = ntohl(th->seq);
  72. newcheck = ~csum_fold((__force __wsum)((__force u32)th->check +
  73. (__force u32)delta));
  74. do {
  75. th->fin = th->psh = 0;
  76. th->check = newcheck;
  77. if (skb->ip_summed != CHECKSUM_PARTIAL)
  78. th->check =
  79. csum_fold(csum_partial(skb_transport_header(skb),
  80. thlen, skb->csum));
  81. seq += mss;
  82. if (copy_destructor) {
  83. skb->destructor = gso_skb->destructor;
  84. skb->sk = gso_skb->sk;
  85. /* {tcp|sock}_wfree() use exact truesize accounting :
  86. * sum(skb->truesize) MUST be exactly be gso_skb->truesize
  87. * So we account mss bytes of 'true size' for each segment.
  88. * The last segment will contain the remaining.
  89. */
  90. skb->truesize = mss;
  91. gso_skb->truesize -= mss;
  92. }
  93. skb = skb->next;
  94. th = tcp_hdr(skb);
  95. th->seq = htonl(seq);
  96. th->cwr = 0;
  97. } while (skb->next);
  98. /* Following permits TCP Small Queues to work well with GSO :
  99. * The callback to TCP stack will be called at the time last frag
  100. * is freed at TX completion, and not right now when gso_skb
  101. * is freed by GSO engine
  102. */
  103. if (copy_destructor) {
  104. swap(gso_skb->sk, skb->sk);
  105. swap(gso_skb->destructor, skb->destructor);
  106. swap(gso_skb->truesize, skb->truesize);
  107. }
  108. delta = htonl(oldlen + (skb_tail_pointer(skb) -
  109. skb_transport_header(skb)) +
  110. skb->data_len);
  111. th->check = ~csum_fold((__force __wsum)((__force u32)th->check +
  112. (__force u32)delta));
  113. if (skb->ip_summed != CHECKSUM_PARTIAL)
  114. th->check = csum_fold(csum_partial(skb_transport_header(skb),
  115. thlen, skb->csum));
  116. out:
  117. return segs;
  118. }
  119. EXPORT_SYMBOL(tcp_tso_segment);
  120. struct sk_buff **tcp_gro_receive(struct sk_buff **head, struct sk_buff *skb)
  121. {
  122. struct sk_buff **pp = NULL;
  123. struct sk_buff *p;
  124. struct tcphdr *th;
  125. struct tcphdr *th2;
  126. unsigned int len;
  127. unsigned int thlen;
  128. __be32 flags;
  129. unsigned int mss = 1;
  130. unsigned int hlen;
  131. unsigned int off;
  132. int flush = 1;
  133. int i;
  134. off = skb_gro_offset(skb);
  135. hlen = off + sizeof(*th);
  136. th = skb_gro_header_fast(skb, off);
  137. if (skb_gro_header_hard(skb, hlen)) {
  138. th = skb_gro_header_slow(skb, hlen, off);
  139. if (unlikely(!th))
  140. goto out;
  141. }
  142. thlen = th->doff * 4;
  143. if (thlen < sizeof(*th))
  144. goto out;
  145. hlen = off + thlen;
  146. if (skb_gro_header_hard(skb, hlen)) {
  147. th = skb_gro_header_slow(skb, hlen, off);
  148. if (unlikely(!th))
  149. goto out;
  150. }
  151. skb_gro_pull(skb, thlen);
  152. len = skb_gro_len(skb);
  153. flags = tcp_flag_word(th);
  154. for (; (p = *head); head = &p->next) {
  155. if (!NAPI_GRO_CB(p)->same_flow)
  156. continue;
  157. th2 = tcp_hdr(p);
  158. if (*(u32 *)&th->source ^ *(u32 *)&th2->source) {
  159. NAPI_GRO_CB(p)->same_flow = 0;
  160. continue;
  161. }
  162. goto found;
  163. }
  164. goto out_check_final;
  165. found:
  166. flush = NAPI_GRO_CB(p)->flush;
  167. flush |= (__force int)(flags & TCP_FLAG_CWR);
  168. flush |= (__force int)((flags ^ tcp_flag_word(th2)) &
  169. ~(TCP_FLAG_CWR | TCP_FLAG_FIN | TCP_FLAG_PSH));
  170. flush |= (__force int)(th->ack_seq ^ th2->ack_seq);
  171. for (i = sizeof(*th); i < thlen; i += 4)
  172. flush |= *(u32 *)((u8 *)th + i) ^
  173. *(u32 *)((u8 *)th2 + i);
  174. mss = tcp_skb_mss(p);
  175. flush |= (len - 1) >= mss;
  176. flush |= (ntohl(th2->seq) + skb_gro_len(p)) ^ ntohl(th->seq);
  177. if (flush || skb_gro_receive(head, skb)) {
  178. mss = 1;
  179. goto out_check_final;
  180. }
  181. p = *head;
  182. th2 = tcp_hdr(p);
  183. tcp_flag_word(th2) |= flags & (TCP_FLAG_FIN | TCP_FLAG_PSH);
  184. out_check_final:
  185. flush = len < mss;
  186. flush |= (__force int)(flags & (TCP_FLAG_URG | TCP_FLAG_PSH |
  187. TCP_FLAG_RST | TCP_FLAG_SYN |
  188. TCP_FLAG_FIN));
  189. if (p && (!NAPI_GRO_CB(skb)->same_flow || flush))
  190. pp = head;
  191. out:
  192. NAPI_GRO_CB(skb)->flush |= flush;
  193. return pp;
  194. }
  195. EXPORT_SYMBOL(tcp_gro_receive);
  196. int tcp_gro_complete(struct sk_buff *skb)
  197. {
  198. struct tcphdr *th = tcp_hdr(skb);
  199. skb->csum_start = skb_transport_header(skb) - skb->head;
  200. skb->csum_offset = offsetof(struct tcphdr, check);
  201. skb->ip_summed = CHECKSUM_PARTIAL;
  202. skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
  203. if (th->cwr)
  204. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  205. return 0;
  206. }
  207. EXPORT_SYMBOL(tcp_gro_complete);
  208. static int tcp_v4_gso_send_check(struct sk_buff *skb)
  209. {
  210. const struct iphdr *iph;
  211. struct tcphdr *th;
  212. if (!pskb_may_pull(skb, sizeof(*th)))
  213. return -EINVAL;
  214. iph = ip_hdr(skb);
  215. th = tcp_hdr(skb);
  216. th->check = 0;
  217. skb->ip_summed = CHECKSUM_PARTIAL;
  218. __tcp_v4_send_check(skb, iph->saddr, iph->daddr);
  219. return 0;
  220. }
  221. static struct sk_buff **tcp4_gro_receive(struct sk_buff **head, struct sk_buff *skb)
  222. {
  223. const struct iphdr *iph = skb_gro_network_header(skb);
  224. __wsum wsum;
  225. __sum16 sum;
  226. switch (skb->ip_summed) {
  227. case CHECKSUM_COMPLETE:
  228. if (!tcp_v4_check(skb_gro_len(skb), iph->saddr, iph->daddr,
  229. skb->csum)) {
  230. skb->ip_summed = CHECKSUM_UNNECESSARY;
  231. break;
  232. }
  233. flush:
  234. NAPI_GRO_CB(skb)->flush = 1;
  235. return NULL;
  236. case CHECKSUM_NONE:
  237. wsum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
  238. skb_gro_len(skb), IPPROTO_TCP, 0);
  239. sum = csum_fold(skb_checksum(skb,
  240. skb_gro_offset(skb),
  241. skb_gro_len(skb),
  242. wsum));
  243. if (sum)
  244. goto flush;
  245. skb->ip_summed = CHECKSUM_UNNECESSARY;
  246. break;
  247. }
  248. return tcp_gro_receive(head, skb);
  249. }
  250. static int tcp4_gro_complete(struct sk_buff *skb)
  251. {
  252. const struct iphdr *iph = ip_hdr(skb);
  253. struct tcphdr *th = tcp_hdr(skb);
  254. th->check = ~tcp_v4_check(skb->len - skb_transport_offset(skb),
  255. iph->saddr, iph->daddr, 0);
  256. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  257. return tcp_gro_complete(skb);
  258. }
  259. static const struct net_offload tcpv4_offload = {
  260. .callbacks = {
  261. .gso_send_check = tcp_v4_gso_send_check,
  262. .gso_segment = tcp_tso_segment,
  263. .gro_receive = tcp4_gro_receive,
  264. .gro_complete = tcp4_gro_complete,
  265. },
  266. };
  267. int __init tcpv4_offload_init(void)
  268. {
  269. return inet_add_offload(&tcpv4_offload, IPPROTO_TCP);
  270. }