vport-vxlan.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2013 Nicira, Inc.
  3. * Copyright (c) 2013 Cisco Systems, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/in.h>
  21. #include <linux/ip.h>
  22. #include <linux/net.h>
  23. #include <linux/rculist.h>
  24. #include <linux/udp.h>
  25. #include <net/icmp.h>
  26. #include <net/ip.h>
  27. #include <net/udp.h>
  28. #include <net/ip_tunnels.h>
  29. #include <net/udp.h>
  30. #include <net/rtnetlink.h>
  31. #include <net/route.h>
  32. #include <net/dsfield.h>
  33. #include <net/inet_ecn.h>
  34. #include <net/net_namespace.h>
  35. #include <net/netns/generic.h>
  36. #include <net/vxlan.h>
  37. #include "datapath.h"
  38. #include "vport.h"
  39. /**
  40. * struct vxlan_port - Keeps track of open UDP ports
  41. * @vs: vxlan_sock created for the port.
  42. * @name: vport name.
  43. */
  44. struct vxlan_port {
  45. struct vxlan_sock *vs;
  46. char name[IFNAMSIZ];
  47. };
  48. static inline struct vxlan_port *vxlan_vport(const struct vport *vport)
  49. {
  50. return vport_priv(vport);
  51. }
  52. /* Called with rcu_read_lock and BH disabled. */
  53. static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb, __be32 vx_vni)
  54. {
  55. struct ovs_key_ipv4_tunnel tun_key;
  56. struct vport *vport = vs->data;
  57. struct iphdr *iph;
  58. __be64 key;
  59. /* Save outer tunnel values */
  60. iph = ip_hdr(skb);
  61. key = cpu_to_be64(ntohl(vx_vni) >> 8);
  62. ovs_flow_tun_key_init(&tun_key, iph, key, TUNNEL_KEY);
  63. ovs_vport_receive(vport, skb, &tun_key);
  64. }
  65. static int vxlan_get_options(const struct vport *vport, struct sk_buff *skb)
  66. {
  67. struct vxlan_port *vxlan_port = vxlan_vport(vport);
  68. __be16 dst_port = inet_sk(vxlan_port->vs->sock->sk)->inet_sport;
  69. if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(dst_port)))
  70. return -EMSGSIZE;
  71. return 0;
  72. }
  73. static void vxlan_tnl_destroy(struct vport *vport)
  74. {
  75. struct vxlan_port *vxlan_port = vxlan_vport(vport);
  76. vxlan_sock_release(vxlan_port->vs);
  77. ovs_vport_deferred_free(vport);
  78. }
  79. static struct vport *vxlan_tnl_create(const struct vport_parms *parms)
  80. {
  81. struct net *net = ovs_dp_get_net(parms->dp);
  82. struct nlattr *options = parms->options;
  83. struct vxlan_port *vxlan_port;
  84. struct vxlan_sock *vs;
  85. struct vport *vport;
  86. struct nlattr *a;
  87. u16 dst_port;
  88. int err;
  89. if (!options) {
  90. err = -EINVAL;
  91. goto error;
  92. }
  93. a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
  94. if (a && nla_len(a) == sizeof(u16)) {
  95. dst_port = nla_get_u16(a);
  96. } else {
  97. /* Require destination port from userspace. */
  98. err = -EINVAL;
  99. goto error;
  100. }
  101. vport = ovs_vport_alloc(sizeof(struct vxlan_port),
  102. &ovs_vxlan_vport_ops, parms);
  103. if (IS_ERR(vport))
  104. return vport;
  105. vxlan_port = vxlan_vport(vport);
  106. strncpy(vxlan_port->name, parms->name, IFNAMSIZ);
  107. vs = vxlan_sock_add(net, htons(dst_port), vxlan_rcv, vport, true, false);
  108. if (IS_ERR(vs)) {
  109. ovs_vport_free(vport);
  110. return (void *)vs;
  111. }
  112. vxlan_port->vs = vs;
  113. return vport;
  114. error:
  115. return ERR_PTR(err);
  116. }
  117. static int vxlan_tnl_send(struct vport *vport, struct sk_buff *skb)
  118. {
  119. struct net *net = ovs_dp_get_net(vport->dp);
  120. struct vxlan_port *vxlan_port = vxlan_vport(vport);
  121. __be16 dst_port = inet_sk(vxlan_port->vs->sock->sk)->inet_sport;
  122. struct rtable *rt;
  123. struct flowi4 fl;
  124. __be16 src_port;
  125. int port_min;
  126. int port_max;
  127. __be16 df;
  128. int err;
  129. if (unlikely(!OVS_CB(skb)->tun_key)) {
  130. err = -EINVAL;
  131. goto error;
  132. }
  133. /* Route lookup */
  134. memset(&fl, 0, sizeof(fl));
  135. fl.daddr = OVS_CB(skb)->tun_key->ipv4_dst;
  136. fl.saddr = OVS_CB(skb)->tun_key->ipv4_src;
  137. fl.flowi4_tos = RT_TOS(OVS_CB(skb)->tun_key->ipv4_tos);
  138. fl.flowi4_mark = skb->mark;
  139. fl.flowi4_proto = IPPROTO_UDP;
  140. rt = ip_route_output_key(net, &fl);
  141. if (IS_ERR(rt)) {
  142. err = PTR_ERR(rt);
  143. goto error;
  144. }
  145. df = OVS_CB(skb)->tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ?
  146. htons(IP_DF) : 0;
  147. skb->local_df = 1;
  148. inet_get_local_port_range(&port_min, &port_max);
  149. src_port = vxlan_src_port(port_min, port_max, skb);
  150. err = vxlan_xmit_skb(vxlan_port->vs, rt, skb,
  151. fl.saddr, OVS_CB(skb)->tun_key->ipv4_dst,
  152. OVS_CB(skb)->tun_key->ipv4_tos,
  153. OVS_CB(skb)->tun_key->ipv4_ttl, df,
  154. src_port, dst_port,
  155. htonl(be64_to_cpu(OVS_CB(skb)->tun_key->tun_id) << 8));
  156. if (err < 0)
  157. ip_rt_put(rt);
  158. error:
  159. return err;
  160. }
  161. static const char *vxlan_get_name(const struct vport *vport)
  162. {
  163. struct vxlan_port *vxlan_port = vxlan_vport(vport);
  164. return vxlan_port->name;
  165. }
  166. const struct vport_ops ovs_vxlan_vport_ops = {
  167. .type = OVS_VPORT_TYPE_VXLAN,
  168. .create = vxlan_tnl_create,
  169. .destroy = vxlan_tnl_destroy,
  170. .get_name = vxlan_get_name,
  171. .get_options = vxlan_get_options,
  172. .send = vxlan_tnl_send,
  173. };