tcp_offload.c 7.4 KB

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