tcp_offload.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. struct sk_buff *tcp_gso_segment(struct sk_buff *skb,
  29. netdev_features_t features)
  30. {
  31. struct sk_buff *segs = ERR_PTR(-EINVAL);
  32. unsigned int sum_truesize = 0;
  33. struct tcphdr *th;
  34. unsigned int thlen;
  35. unsigned int seq;
  36. __be32 delta;
  37. unsigned int oldlen;
  38. unsigned int mss;
  39. struct sk_buff *gso_skb = skb;
  40. __sum16 newcheck;
  41. bool ooo_okay, copy_destructor;
  42. if (!pskb_may_pull(skb, sizeof(*th)))
  43. goto out;
  44. th = tcp_hdr(skb);
  45. thlen = th->doff * 4;
  46. if (thlen < sizeof(*th))
  47. goto out;
  48. if (!pskb_may_pull(skb, thlen))
  49. goto out;
  50. oldlen = (u16)~skb->len;
  51. __skb_pull(skb, thlen);
  52. mss = tcp_skb_mss(skb);
  53. if (unlikely(skb->len <= mss))
  54. goto out;
  55. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  56. /* Packet is from an untrusted source, reset gso_segs. */
  57. int type = skb_shinfo(skb)->gso_type;
  58. if (unlikely(type &
  59. ~(SKB_GSO_TCPV4 |
  60. SKB_GSO_DODGY |
  61. SKB_GSO_TCP_ECN |
  62. SKB_GSO_TCPV6 |
  63. SKB_GSO_GRE |
  64. SKB_GSO_GRE_CSUM |
  65. SKB_GSO_IPIP |
  66. SKB_GSO_SIT |
  67. SKB_GSO_MPLS |
  68. SKB_GSO_UDP_TUNNEL |
  69. SKB_GSO_UDP_TUNNEL_CSUM |
  70. 0) ||
  71. !(type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))))
  72. goto out;
  73. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
  74. segs = NULL;
  75. goto out;
  76. }
  77. copy_destructor = gso_skb->destructor == tcp_wfree;
  78. ooo_okay = gso_skb->ooo_okay;
  79. /* All segments but the first should have ooo_okay cleared */
  80. skb->ooo_okay = 0;
  81. segs = skb_segment(skb, features);
  82. if (IS_ERR(segs))
  83. goto out;
  84. /* Only first segment might have ooo_okay set */
  85. segs->ooo_okay = ooo_okay;
  86. delta = htonl(oldlen + (thlen + mss));
  87. skb = segs;
  88. th = tcp_hdr(skb);
  89. seq = ntohl(th->seq);
  90. if (unlikely(skb_shinfo(gso_skb)->tx_flags & SKBTX_SW_TSTAMP))
  91. tcp_gso_tstamp(segs, skb_shinfo(gso_skb)->tskey, seq, mss);
  92. newcheck = ~csum_fold((__force __wsum)((__force u32)th->check +
  93. (__force u32)delta));
  94. do {
  95. th->fin = th->psh = 0;
  96. th->check = newcheck;
  97. if (skb->ip_summed != CHECKSUM_PARTIAL)
  98. th->check = gso_make_checksum(skb, ~th->check);
  99. seq += mss;
  100. if (copy_destructor) {
  101. skb->destructor = gso_skb->destructor;
  102. skb->sk = gso_skb->sk;
  103. sum_truesize += skb->truesize;
  104. }
  105. skb = skb->next;
  106. th = tcp_hdr(skb);
  107. th->seq = htonl(seq);
  108. th->cwr = 0;
  109. } while (skb->next);
  110. /* Following permits TCP Small Queues to work well with GSO :
  111. * The callback to TCP stack will be called at the time last frag
  112. * is freed at TX completion, and not right now when gso_skb
  113. * is freed by GSO engine
  114. */
  115. if (copy_destructor) {
  116. swap(gso_skb->sk, skb->sk);
  117. swap(gso_skb->destructor, skb->destructor);
  118. sum_truesize += skb->truesize;
  119. atomic_add(sum_truesize - gso_skb->truesize,
  120. &skb->sk->sk_wmem_alloc);
  121. }
  122. delta = htonl(oldlen + (skb_tail_pointer(skb) -
  123. skb_transport_header(skb)) +
  124. skb->data_len);
  125. th->check = ~csum_fold((__force __wsum)((__force u32)th->check +
  126. (__force u32)delta));
  127. if (skb->ip_summed != CHECKSUM_PARTIAL)
  128. th->check = gso_make_checksum(skb, ~th->check);
  129. out:
  130. return segs;
  131. }
  132. struct sk_buff **tcp_gro_receive(struct sk_buff **head, struct sk_buff *skb)
  133. {
  134. struct sk_buff **pp = NULL;
  135. struct sk_buff *p;
  136. struct tcphdr *th;
  137. struct tcphdr *th2;
  138. unsigned int len;
  139. unsigned int thlen;
  140. __be32 flags;
  141. unsigned int mss = 1;
  142. unsigned int hlen;
  143. unsigned int off;
  144. int flush = 1;
  145. int i;
  146. off = skb_gro_offset(skb);
  147. hlen = off + sizeof(*th);
  148. th = skb_gro_header_fast(skb, off);
  149. if (skb_gro_header_hard(skb, hlen)) {
  150. th = skb_gro_header_slow(skb, hlen, off);
  151. if (unlikely(!th))
  152. goto out;
  153. }
  154. thlen = th->doff * 4;
  155. if (thlen < sizeof(*th))
  156. goto out;
  157. hlen = off + thlen;
  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. skb_gro_pull(skb, thlen);
  164. len = skb_gro_len(skb);
  165. flags = tcp_flag_word(th);
  166. for (; (p = *head); head = &p->next) {
  167. if (!NAPI_GRO_CB(p)->same_flow)
  168. continue;
  169. th2 = tcp_hdr(p);
  170. if (*(u32 *)&th->source ^ *(u32 *)&th2->source) {
  171. NAPI_GRO_CB(p)->same_flow = 0;
  172. continue;
  173. }
  174. goto found;
  175. }
  176. goto out_check_final;
  177. found:
  178. /* Include the IP ID check below from the inner most IP hdr */
  179. flush = NAPI_GRO_CB(p)->flush | NAPI_GRO_CB(p)->flush_id;
  180. flush |= (__force int)(flags & TCP_FLAG_CWR);
  181. flush |= (__force int)((flags ^ tcp_flag_word(th2)) &
  182. ~(TCP_FLAG_CWR | TCP_FLAG_FIN | TCP_FLAG_PSH));
  183. flush |= (__force int)(th->ack_seq ^ th2->ack_seq);
  184. for (i = sizeof(*th); i < thlen; i += 4)
  185. flush |= *(u32 *)((u8 *)th + i) ^
  186. *(u32 *)((u8 *)th2 + i);
  187. mss = tcp_skb_mss(p);
  188. flush |= (len - 1) >= mss;
  189. flush |= (ntohl(th2->seq) + skb_gro_len(p)) ^ ntohl(th->seq);
  190. if (flush || skb_gro_receive(head, skb)) {
  191. mss = 1;
  192. goto out_check_final;
  193. }
  194. p = *head;
  195. th2 = tcp_hdr(p);
  196. tcp_flag_word(th2) |= flags & (TCP_FLAG_FIN | TCP_FLAG_PSH);
  197. out_check_final:
  198. flush = len < mss;
  199. flush |= (__force int)(flags & (TCP_FLAG_URG | TCP_FLAG_PSH |
  200. TCP_FLAG_RST | TCP_FLAG_SYN |
  201. TCP_FLAG_FIN));
  202. if (p && (!NAPI_GRO_CB(skb)->same_flow || flush))
  203. pp = head;
  204. out:
  205. NAPI_GRO_CB(skb)->flush |= (flush != 0);
  206. return pp;
  207. }
  208. int tcp_gro_complete(struct sk_buff *skb)
  209. {
  210. struct tcphdr *th = tcp_hdr(skb);
  211. skb->csum_start = (unsigned char *)th - skb->head;
  212. skb->csum_offset = offsetof(struct tcphdr, check);
  213. skb->ip_summed = CHECKSUM_PARTIAL;
  214. skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
  215. if (th->cwr)
  216. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  217. return 0;
  218. }
  219. EXPORT_SYMBOL(tcp_gro_complete);
  220. static int tcp_v4_gso_send_check(struct sk_buff *skb)
  221. {
  222. const struct iphdr *iph;
  223. struct tcphdr *th;
  224. if (!pskb_may_pull(skb, sizeof(*th)))
  225. return -EINVAL;
  226. iph = ip_hdr(skb);
  227. th = tcp_hdr(skb);
  228. th->check = 0;
  229. skb->ip_summed = CHECKSUM_PARTIAL;
  230. __tcp_v4_send_check(skb, iph->saddr, iph->daddr);
  231. return 0;
  232. }
  233. static struct sk_buff **tcp4_gro_receive(struct sk_buff **head, struct sk_buff *skb)
  234. {
  235. /* Use the IP hdr immediately proceeding for this transport */
  236. const struct iphdr *iph = skb_gro_network_header(skb);
  237. __wsum wsum;
  238. /* Don't bother verifying checksum if we're going to flush anyway. */
  239. if (NAPI_GRO_CB(skb)->flush)
  240. goto skip_csum;
  241. wsum = NAPI_GRO_CB(skb)->csum;
  242. switch (skb->ip_summed) {
  243. case CHECKSUM_NONE:
  244. wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb),
  245. 0);
  246. /* fall through */
  247. case CHECKSUM_COMPLETE:
  248. if (!tcp_v4_check(skb_gro_len(skb), iph->saddr, iph->daddr,
  249. wsum)) {
  250. skb->ip_summed = CHECKSUM_UNNECESSARY;
  251. break;
  252. }
  253. NAPI_GRO_CB(skb)->flush = 1;
  254. return NULL;
  255. }
  256. skip_csum:
  257. return tcp_gro_receive(head, skb);
  258. }
  259. static int tcp4_gro_complete(struct sk_buff *skb, int thoff)
  260. {
  261. const struct iphdr *iph = ip_hdr(skb);
  262. struct tcphdr *th = tcp_hdr(skb);
  263. th->check = ~tcp_v4_check(skb->len - thoff, iph->saddr,
  264. iph->daddr, 0);
  265. skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
  266. return tcp_gro_complete(skb);
  267. }
  268. static const struct net_offload tcpv4_offload = {
  269. .callbacks = {
  270. .gso_send_check = tcp_v4_gso_send_check,
  271. .gso_segment = tcp_gso_segment,
  272. .gro_receive = tcp4_gro_receive,
  273. .gro_complete = tcp4_gro_complete,
  274. },
  275. };
  276. int __init tcpv4_offload_init(void)
  277. {
  278. return inet_add_offload(&tcpv4_offload, IPPROTO_TCP);
  279. }