tcp_offload.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. swap(gso_skb->sk, skb->sk);
  126. swap(gso_skb->destructor, skb->destructor);
  127. sum_truesize += skb->truesize;
  128. atomic_add(sum_truesize - gso_skb->truesize,
  129. &skb->sk->sk_wmem_alloc);
  130. }
  131. delta = htonl(oldlen + (skb_tail_pointer(skb) -
  132. skb_transport_header(skb)) +
  133. skb->data_len);
  134. th->check = ~csum_fold((__force __wsum)((__force u32)th->check +
  135. (__force u32)delta));
  136. if (skb->ip_summed == CHECKSUM_PARTIAL)
  137. gso_reset_checksum(skb, ~th->check);
  138. else
  139. th->check = gso_make_checksum(skb, ~th->check);
  140. out:
  141. return segs;
  142. }
  143. struct sk_buff **tcp_gro_receive(struct sk_buff **head, struct sk_buff *skb)
  144. {
  145. struct sk_buff **pp = NULL;
  146. struct sk_buff *p;
  147. struct tcphdr *th;
  148. struct tcphdr *th2;
  149. unsigned int len;
  150. unsigned int thlen;
  151. __be32 flags;
  152. unsigned int mss = 1;
  153. unsigned int hlen;
  154. unsigned int off;
  155. int flush = 1;
  156. int i;
  157. off = skb_gro_offset(skb);
  158. hlen = off + sizeof(*th);
  159. th = skb_gro_header_fast(skb, off);
  160. if (skb_gro_header_hard(skb, hlen)) {
  161. th = skb_gro_header_slow(skb, hlen, off);
  162. if (unlikely(!th))
  163. goto out;
  164. }
  165. thlen = th->doff * 4;
  166. if (thlen < sizeof(*th))
  167. goto out;
  168. hlen = off + thlen;
  169. if (skb_gro_header_hard(skb, hlen)) {
  170. th = skb_gro_header_slow(skb, hlen, off);
  171. if (unlikely(!th))
  172. goto out;
  173. }
  174. skb_gro_pull(skb, thlen);
  175. len = skb_gro_len(skb);
  176. flags = tcp_flag_word(th);
  177. for (; (p = *head); head = &p->next) {
  178. if (!NAPI_GRO_CB(p)->same_flow)
  179. continue;
  180. th2 = tcp_hdr(p);
  181. if (*(u32 *)&th->source ^ *(u32 *)&th2->source) {
  182. NAPI_GRO_CB(p)->same_flow = 0;
  183. continue;
  184. }
  185. goto found;
  186. }
  187. goto out_check_final;
  188. found:
  189. /* Include the IP ID check below from the inner most IP hdr */
  190. flush = NAPI_GRO_CB(p)->flush;
  191. flush |= (__force int)(flags & TCP_FLAG_CWR);
  192. flush |= (__force int)((flags ^ tcp_flag_word(th2)) &
  193. ~(TCP_FLAG_CWR | TCP_FLAG_FIN | TCP_FLAG_PSH));
  194. flush |= (__force int)(th->ack_seq ^ th2->ack_seq);
  195. for (i = sizeof(*th); i < thlen; i += 4)
  196. flush |= *(u32 *)((u8 *)th + i) ^
  197. *(u32 *)((u8 *)th2 + i);
  198. /* When we receive our second frame we can made a decision on if we
  199. * continue this flow as an atomic flow with a fixed ID or if we use
  200. * an incrementing ID.
  201. */
  202. if (NAPI_GRO_CB(p)->flush_id != 1 ||
  203. NAPI_GRO_CB(p)->count != 1 ||
  204. !NAPI_GRO_CB(p)->is_atomic)
  205. flush |= NAPI_GRO_CB(p)->flush_id;
  206. else
  207. NAPI_GRO_CB(p)->is_atomic = false;
  208. mss = skb_shinfo(p)->gso_size;
  209. flush |= (len - 1) >= mss;
  210. flush |= (ntohl(th2->seq) + skb_gro_len(p)) ^ ntohl(th->seq);
  211. if (flush || skb_gro_receive(head, skb)) {
  212. mss = 1;
  213. goto out_check_final;
  214. }
  215. p = *head;
  216. th2 = tcp_hdr(p);
  217. tcp_flag_word(th2) |= flags & (TCP_FLAG_FIN | TCP_FLAG_PSH);
  218. out_check_final:
  219. flush = len < mss;
  220. flush |= (__force int)(flags & (TCP_FLAG_URG | TCP_FLAG_PSH |
  221. TCP_FLAG_RST | TCP_FLAG_SYN |
  222. TCP_FLAG_FIN));
  223. if (p && (!NAPI_GRO_CB(skb)->same_flow || flush))
  224. pp = head;
  225. out:
  226. NAPI_GRO_CB(skb)->flush |= (flush != 0);
  227. return pp;
  228. }
  229. int tcp_gro_complete(struct sk_buff *skb)
  230. {
  231. struct tcphdr *th = tcp_hdr(skb);
  232. skb->csum_start = (unsigned char *)th - skb->head;
  233. skb->csum_offset = offsetof(struct tcphdr, check);
  234. skb->ip_summed = CHECKSUM_PARTIAL;
  235. skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
  236. if (th->cwr)
  237. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  238. return 0;
  239. }
  240. EXPORT_SYMBOL(tcp_gro_complete);
  241. static struct sk_buff **tcp4_gro_receive(struct sk_buff **head, struct sk_buff *skb)
  242. {
  243. /* Don't bother verifying checksum if we're going to flush anyway. */
  244. if (!NAPI_GRO_CB(skb)->flush &&
  245. skb_gro_checksum_validate(skb, IPPROTO_TCP,
  246. inet_gro_compute_pseudo)) {
  247. NAPI_GRO_CB(skb)->flush = 1;
  248. return NULL;
  249. }
  250. return tcp_gro_receive(head, skb);
  251. }
  252. static int tcp4_gro_complete(struct sk_buff *skb, int thoff)
  253. {
  254. const struct iphdr *iph = ip_hdr(skb);
  255. struct tcphdr *th = tcp_hdr(skb);
  256. th->check = ~tcp_v4_check(skb->len - thoff, iph->saddr,
  257. iph->daddr, 0);
  258. skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
  259. if (NAPI_GRO_CB(skb)->is_atomic)
  260. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_FIXEDID;
  261. return tcp_gro_complete(skb);
  262. }
  263. static const struct net_offload tcpv4_offload = {
  264. .callbacks = {
  265. .gso_segment = tcp4_gso_segment,
  266. .gro_receive = tcp4_gro_receive,
  267. .gro_complete = tcp4_gro_complete,
  268. },
  269. };
  270. int __init tcpv4_offload_init(void)
  271. {
  272. return inet_add_offload(&tcpv4_offload, IPPROTO_TCP);
  273. }