tcp_offload.c 7.5 KB

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