tcp_offload.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. static void tcp_gso_tstamp(struct sk_buff *skb, unsigned int ts_seq,
  16. unsigned int seq, unsigned int mss)
  17. {
  18. while (skb) {
  19. if (before(ts_seq, seq + mss)) {
  20. skb_shinfo(skb)->tx_flags |= SKBTX_SW_TSTAMP;
  21. skb_shinfo(skb)->tskey = ts_seq;
  22. return;
  23. }
  24. skb = skb->next;
  25. seq += mss;
  26. }
  27. }
  28. static struct sk_buff *tcp4_gso_segment(struct sk_buff *skb,
  29. netdev_features_t features)
  30. {
  31. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4))
  32. return ERR_PTR(-EINVAL);
  33. if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
  34. return ERR_PTR(-EINVAL);
  35. if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
  36. const struct iphdr *iph = ip_hdr(skb);
  37. struct tcphdr *th = tcp_hdr(skb);
  38. /* Set up checksum pseudo header, usually expect stack to
  39. * have done this already.
  40. */
  41. th->check = 0;
  42. skb->ip_summed = CHECKSUM_PARTIAL;
  43. __tcp_v4_send_check(skb, iph->saddr, iph->daddr);
  44. }
  45. return tcp_gso_segment(skb, features);
  46. }
  47. struct sk_buff *tcp_gso_segment(struct sk_buff *skb,
  48. netdev_features_t features)
  49. {
  50. struct sk_buff *segs = ERR_PTR(-EINVAL);
  51. unsigned int sum_truesize = 0;
  52. struct tcphdr *th;
  53. unsigned int thlen;
  54. unsigned int seq;
  55. __be32 delta;
  56. unsigned int oldlen;
  57. unsigned int mss;
  58. struct sk_buff *gso_skb = skb;
  59. __sum16 newcheck;
  60. bool ooo_okay, copy_destructor;
  61. th = tcp_hdr(skb);
  62. thlen = th->doff * 4;
  63. if (thlen < sizeof(*th))
  64. goto out;
  65. if (!pskb_may_pull(skb, thlen))
  66. goto out;
  67. oldlen = (u16)~skb->len;
  68. __skb_pull(skb, thlen);
  69. mss = skb_shinfo(skb)->gso_size;
  70. if (unlikely(skb->len <= mss))
  71. goto out;
  72. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  73. /* Packet is from an untrusted source, reset gso_segs. */
  74. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
  75. segs = NULL;
  76. goto out;
  77. }
  78. copy_destructor = gso_skb->destructor == tcp_wfree;
  79. ooo_okay = gso_skb->ooo_okay;
  80. /* All segments but the first should have ooo_okay cleared */
  81. skb->ooo_okay = 0;
  82. segs = skb_segment(skb, features);
  83. if (IS_ERR(segs))
  84. goto out;
  85. /* Only first segment might have ooo_okay set */
  86. segs->ooo_okay = ooo_okay;
  87. /* GSO partial and frag_list segmentation only requires splitting
  88. * the frame into an MSS multiple and possibly a remainder, both
  89. * cases return a GSO skb. So update the mss now.
  90. */
  91. if (skb_is_gso(segs))
  92. mss *= skb_shinfo(segs)->gso_segs;
  93. delta = htonl(oldlen + (thlen + mss));
  94. skb = segs;
  95. th = tcp_hdr(skb);
  96. seq = ntohl(th->seq);
  97. if (unlikely(skb_shinfo(gso_skb)->tx_flags & SKBTX_SW_TSTAMP))
  98. tcp_gso_tstamp(segs, skb_shinfo(gso_skb)->tskey, seq, mss);
  99. newcheck = ~csum_fold((__force __wsum)((__force u32)th->check +
  100. (__force u32)delta));
  101. while (skb->next) {
  102. th->fin = th->psh = 0;
  103. th->check = newcheck;
  104. if (skb->ip_summed == CHECKSUM_PARTIAL)
  105. gso_reset_checksum(skb, ~th->check);
  106. else
  107. th->check = gso_make_checksum(skb, ~th->check);
  108. seq += mss;
  109. if (copy_destructor) {
  110. skb->destructor = gso_skb->destructor;
  111. skb->sk = gso_skb->sk;
  112. sum_truesize += skb->truesize;
  113. }
  114. skb = skb->next;
  115. th = tcp_hdr(skb);
  116. th->seq = htonl(seq);
  117. th->cwr = 0;
  118. }
  119. /* Following permits TCP Small Queues to work well with GSO :
  120. * The callback to TCP stack will be called at the time last frag
  121. * is freed at TX completion, and not right now when gso_skb
  122. * is freed by GSO engine
  123. */
  124. if (copy_destructor) {
  125. int delta;
  126. swap(gso_skb->sk, skb->sk);
  127. swap(gso_skb->destructor, skb->destructor);
  128. sum_truesize += skb->truesize;
  129. delta = sum_truesize - gso_skb->truesize;
  130. /* In some pathological cases, delta can be negative.
  131. * We need to either use refcount_add() or refcount_sub_and_test()
  132. */
  133. if (likely(delta >= 0))
  134. refcount_add(delta, &skb->sk->sk_wmem_alloc);
  135. else
  136. WARN_ON_ONCE(refcount_sub_and_test(-delta, &skb->sk->sk_wmem_alloc));
  137. }
  138. delta = htonl(oldlen + (skb_tail_pointer(skb) -
  139. skb_transport_header(skb)) +
  140. skb->data_len);
  141. th->check = ~csum_fold((__force __wsum)((__force u32)th->check +
  142. (__force u32)delta));
  143. if (skb->ip_summed == CHECKSUM_PARTIAL)
  144. gso_reset_checksum(skb, ~th->check);
  145. else
  146. th->check = gso_make_checksum(skb, ~th->check);
  147. out:
  148. return segs;
  149. }
  150. struct sk_buff *tcp_gro_receive(struct list_head *head, struct sk_buff *skb)
  151. {
  152. struct sk_buff *pp = NULL;
  153. struct sk_buff *p;
  154. struct tcphdr *th;
  155. struct tcphdr *th2;
  156. unsigned int len;
  157. unsigned int thlen;
  158. __be32 flags;
  159. unsigned int mss = 1;
  160. unsigned int hlen;
  161. unsigned int off;
  162. int flush = 1;
  163. int i;
  164. off = skb_gro_offset(skb);
  165. hlen = off + sizeof(*th);
  166. th = skb_gro_header_fast(skb, off);
  167. if (skb_gro_header_hard(skb, hlen)) {
  168. th = skb_gro_header_slow(skb, hlen, off);
  169. if (unlikely(!th))
  170. goto out;
  171. }
  172. thlen = th->doff * 4;
  173. if (thlen < sizeof(*th))
  174. goto out;
  175. hlen = off + thlen;
  176. if (skb_gro_header_hard(skb, hlen)) {
  177. th = skb_gro_header_slow(skb, hlen, off);
  178. if (unlikely(!th))
  179. goto out;
  180. }
  181. skb_gro_pull(skb, thlen);
  182. len = skb_gro_len(skb);
  183. flags = tcp_flag_word(th);
  184. list_for_each_entry(p, head, list) {
  185. if (!NAPI_GRO_CB(p)->same_flow)
  186. continue;
  187. th2 = tcp_hdr(p);
  188. if (*(u32 *)&th->source ^ *(u32 *)&th2->source) {
  189. NAPI_GRO_CB(p)->same_flow = 0;
  190. continue;
  191. }
  192. goto found;
  193. }
  194. p = NULL;
  195. goto out_check_final;
  196. found:
  197. /* Include the IP ID check below from the inner most IP hdr */
  198. flush = NAPI_GRO_CB(p)->flush;
  199. flush |= (__force int)(flags & TCP_FLAG_CWR);
  200. flush |= (__force int)((flags ^ tcp_flag_word(th2)) &
  201. ~(TCP_FLAG_CWR | TCP_FLAG_FIN | TCP_FLAG_PSH));
  202. flush |= (__force int)(th->ack_seq ^ th2->ack_seq);
  203. for (i = sizeof(*th); i < thlen; i += 4)
  204. flush |= *(u32 *)((u8 *)th + i) ^
  205. *(u32 *)((u8 *)th2 + i);
  206. /* When we receive our second frame we can made a decision on if we
  207. * continue this flow as an atomic flow with a fixed ID or if we use
  208. * an incrementing ID.
  209. */
  210. if (NAPI_GRO_CB(p)->flush_id != 1 ||
  211. NAPI_GRO_CB(p)->count != 1 ||
  212. !NAPI_GRO_CB(p)->is_atomic)
  213. flush |= NAPI_GRO_CB(p)->flush_id;
  214. else
  215. NAPI_GRO_CB(p)->is_atomic = false;
  216. mss = skb_shinfo(p)->gso_size;
  217. flush |= (len - 1) >= mss;
  218. flush |= (ntohl(th2->seq) + skb_gro_len(p)) ^ ntohl(th->seq);
  219. #ifdef CONFIG_TLS_DEVICE
  220. flush |= p->decrypted ^ skb->decrypted;
  221. #endif
  222. if (flush || skb_gro_receive(p, skb)) {
  223. mss = 1;
  224. goto out_check_final;
  225. }
  226. tcp_flag_word(th2) |= flags & (TCP_FLAG_FIN | TCP_FLAG_PSH);
  227. out_check_final:
  228. flush = len < mss;
  229. flush |= (__force int)(flags & (TCP_FLAG_URG | TCP_FLAG_PSH |
  230. TCP_FLAG_RST | TCP_FLAG_SYN |
  231. TCP_FLAG_FIN));
  232. if (p && (!NAPI_GRO_CB(skb)->same_flow || flush))
  233. pp = p;
  234. out:
  235. NAPI_GRO_CB(skb)->flush |= (flush != 0);
  236. return pp;
  237. }
  238. int tcp_gro_complete(struct sk_buff *skb)
  239. {
  240. struct tcphdr *th = tcp_hdr(skb);
  241. skb->csum_start = (unsigned char *)th - skb->head;
  242. skb->csum_offset = offsetof(struct tcphdr, check);
  243. skb->ip_summed = CHECKSUM_PARTIAL;
  244. skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
  245. if (th->cwr)
  246. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  247. return 0;
  248. }
  249. EXPORT_SYMBOL(tcp_gro_complete);
  250. static struct sk_buff *tcp4_gro_receive(struct list_head *head, struct sk_buff *skb)
  251. {
  252. /* Don't bother verifying checksum if we're going to flush anyway. */
  253. if (!NAPI_GRO_CB(skb)->flush &&
  254. skb_gro_checksum_validate(skb, IPPROTO_TCP,
  255. inet_gro_compute_pseudo)) {
  256. NAPI_GRO_CB(skb)->flush = 1;
  257. return NULL;
  258. }
  259. return tcp_gro_receive(head, skb);
  260. }
  261. static int tcp4_gro_complete(struct sk_buff *skb, int thoff)
  262. {
  263. const struct iphdr *iph = ip_hdr(skb);
  264. struct tcphdr *th = tcp_hdr(skb);
  265. th->check = ~tcp_v4_check(skb->len - thoff, iph->saddr,
  266. iph->daddr, 0);
  267. skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
  268. if (NAPI_GRO_CB(skb)->is_atomic)
  269. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_FIXEDID;
  270. return tcp_gro_complete(skb);
  271. }
  272. static const struct net_offload tcpv4_offload = {
  273. .callbacks = {
  274. .gso_segment = tcp4_gso_segment,
  275. .gro_receive = tcp4_gro_receive,
  276. .gro_complete = tcp4_gro_complete,
  277. },
  278. };
  279. int __init tcpv4_offload_init(void)
  280. {
  281. return inet_add_offload(&tcpv4_offload, IPPROTO_TCP);
  282. }