tcp_offload.c 7.9 KB

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