tcp_offload.c 7.6 KB

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