tcp_offload.c 7.8 KB

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