udp_offload.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. * UDPv4 GSO support
  11. */
  12. #include <linux/skbuff.h>
  13. #include <net/udp.h>
  14. #include <net/protocol.h>
  15. static DEFINE_SPINLOCK(udp_offload_lock);
  16. static struct udp_offload_priv __rcu *udp_offload_base __read_mostly;
  17. struct udp_offload_priv {
  18. struct udp_offload *offload;
  19. struct rcu_head rcu;
  20. struct udp_offload_priv __rcu *next;
  21. };
  22. static int udp4_ufo_send_check(struct sk_buff *skb)
  23. {
  24. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  25. return -EINVAL;
  26. if (likely(!skb->encapsulation)) {
  27. const struct iphdr *iph;
  28. struct udphdr *uh;
  29. iph = ip_hdr(skb);
  30. uh = udp_hdr(skb);
  31. uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
  32. IPPROTO_UDP, 0);
  33. skb->csum_start = skb_transport_header(skb) - skb->head;
  34. skb->csum_offset = offsetof(struct udphdr, check);
  35. skb->ip_summed = CHECKSUM_PARTIAL;
  36. }
  37. return 0;
  38. }
  39. static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
  40. netdev_features_t features)
  41. {
  42. struct sk_buff *segs = ERR_PTR(-EINVAL);
  43. unsigned int mss;
  44. int offset;
  45. __wsum csum;
  46. if (skb->encapsulation &&
  47. skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL) {
  48. segs = skb_udp_tunnel_segment(skb, features);
  49. goto out;
  50. }
  51. mss = skb_shinfo(skb)->gso_size;
  52. if (unlikely(skb->len <= mss))
  53. goto out;
  54. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  55. /* Packet is from an untrusted source, reset gso_segs. */
  56. int type = skb_shinfo(skb)->gso_type;
  57. if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY |
  58. SKB_GSO_UDP_TUNNEL |
  59. SKB_GSO_IPIP |
  60. SKB_GSO_GRE | SKB_GSO_MPLS) ||
  61. !(type & (SKB_GSO_UDP))))
  62. goto out;
  63. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
  64. segs = NULL;
  65. goto out;
  66. }
  67. /* Do software UFO. Complete and fill in the UDP checksum as
  68. * HW cannot do checksum of UDP packets sent as multiple
  69. * IP fragments.
  70. */
  71. offset = skb_checksum_start_offset(skb);
  72. csum = skb_checksum(skb, offset, skb->len - offset, 0);
  73. offset += skb->csum_offset;
  74. *(__sum16 *)(skb->data + offset) = csum_fold(csum);
  75. skb->ip_summed = CHECKSUM_NONE;
  76. /* Fragment the skb. IP headers of the fragments are updated in
  77. * inet_gso_segment()
  78. */
  79. segs = skb_segment(skb, features);
  80. out:
  81. return segs;
  82. }
  83. int udp_add_offload(struct udp_offload *uo)
  84. {
  85. struct udp_offload_priv __rcu **head = &udp_offload_base;
  86. struct udp_offload_priv *new_offload = kzalloc(sizeof(*new_offload), GFP_KERNEL);
  87. if (!new_offload)
  88. return -ENOMEM;
  89. new_offload->offload = uo;
  90. spin_lock(&udp_offload_lock);
  91. rcu_assign_pointer(new_offload->next, rcu_dereference(*head));
  92. rcu_assign_pointer(*head, new_offload);
  93. spin_unlock(&udp_offload_lock);
  94. return 0;
  95. }
  96. EXPORT_SYMBOL(udp_add_offload);
  97. static void udp_offload_free_routine(struct rcu_head *head)
  98. {
  99. struct udp_offload_priv *ou_priv = container_of(head, struct udp_offload_priv, rcu);
  100. kfree(ou_priv);
  101. }
  102. void udp_del_offload(struct udp_offload *uo)
  103. {
  104. struct udp_offload_priv __rcu **head = &udp_offload_base;
  105. struct udp_offload_priv *uo_priv;
  106. spin_lock(&udp_offload_lock);
  107. uo_priv = rcu_dereference(*head);
  108. for (; uo_priv != NULL;
  109. uo_priv = rcu_dereference(*head)) {
  110. if (uo_priv->offload == uo) {
  111. rcu_assign_pointer(*head, rcu_dereference(uo_priv->next));
  112. goto unlock;
  113. }
  114. head = &uo_priv->next;
  115. }
  116. pr_warn("udp_del_offload: didn't find offload for port %d\n", ntohs(uo->port));
  117. unlock:
  118. spin_unlock(&udp_offload_lock);
  119. if (uo_priv != NULL)
  120. call_rcu(&uo_priv->rcu, udp_offload_free_routine);
  121. }
  122. EXPORT_SYMBOL(udp_del_offload);
  123. static struct sk_buff **udp_gro_receive(struct sk_buff **head, struct sk_buff *skb)
  124. {
  125. struct udp_offload_priv *uo_priv;
  126. struct sk_buff *p, **pp = NULL;
  127. struct udphdr *uh, *uh2;
  128. unsigned int hlen, off;
  129. int flush = 1;
  130. if (NAPI_GRO_CB(skb)->udp_mark ||
  131. (!skb->encapsulation && skb->ip_summed != CHECKSUM_COMPLETE))
  132. goto out;
  133. /* mark that this skb passed once through the udp gro layer */
  134. NAPI_GRO_CB(skb)->udp_mark = 1;
  135. off = skb_gro_offset(skb);
  136. hlen = off + sizeof(*uh);
  137. uh = skb_gro_header_fast(skb, off);
  138. if (skb_gro_header_hard(skb, hlen)) {
  139. uh = skb_gro_header_slow(skb, hlen, off);
  140. if (unlikely(!uh))
  141. goto out;
  142. }
  143. rcu_read_lock();
  144. uo_priv = rcu_dereference(udp_offload_base);
  145. for (; uo_priv != NULL; uo_priv = rcu_dereference(uo_priv->next)) {
  146. if (uo_priv->offload->port == uh->dest &&
  147. uo_priv->offload->callbacks.gro_receive)
  148. goto unflush;
  149. }
  150. goto out_unlock;
  151. unflush:
  152. flush = 0;
  153. for (p = *head; p; p = p->next) {
  154. if (!NAPI_GRO_CB(p)->same_flow)
  155. continue;
  156. uh2 = (struct udphdr *)(p->data + off);
  157. if ((*(u32 *)&uh->source != *(u32 *)&uh2->source)) {
  158. NAPI_GRO_CB(p)->same_flow = 0;
  159. continue;
  160. }
  161. }
  162. skb_gro_pull(skb, sizeof(struct udphdr)); /* pull encapsulating udp header */
  163. pp = uo_priv->offload->callbacks.gro_receive(head, skb);
  164. out_unlock:
  165. rcu_read_unlock();
  166. out:
  167. NAPI_GRO_CB(skb)->flush |= flush;
  168. return pp;
  169. }
  170. static int udp_gro_complete(struct sk_buff *skb, int nhoff)
  171. {
  172. struct udp_offload_priv *uo_priv;
  173. __be16 newlen = htons(skb->len - nhoff);
  174. struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
  175. int err = -ENOSYS;
  176. uh->len = newlen;
  177. rcu_read_lock();
  178. uo_priv = rcu_dereference(udp_offload_base);
  179. for (; uo_priv != NULL; uo_priv = rcu_dereference(uo_priv->next)) {
  180. if (uo_priv->offload->port == uh->dest &&
  181. uo_priv->offload->callbacks.gro_complete)
  182. break;
  183. }
  184. if (uo_priv != NULL)
  185. err = uo_priv->offload->callbacks.gro_complete(skb, nhoff + sizeof(struct udphdr));
  186. rcu_read_unlock();
  187. return err;
  188. }
  189. static const struct net_offload udpv4_offload = {
  190. .callbacks = {
  191. .gso_send_check = udp4_ufo_send_check,
  192. .gso_segment = udp4_ufo_fragment,
  193. .gro_receive = udp_gro_receive,
  194. .gro_complete = udp_gro_complete,
  195. },
  196. };
  197. int __init udpv4_offload_init(void)
  198. {
  199. return inet_add_offload(&udpv4_offload, IPPROTO_UDP);
  200. }