vport-vxlan.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (c) 2014 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 <linux/module.h>
  26. #include <net/icmp.h>
  27. #include <net/ip.h>
  28. #include <net/udp.h>
  29. #include <net/ip_tunnels.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 struct vport_ops ovs_vxlan_vport_ops;
  49. static inline struct vxlan_port *vxlan_vport(const struct vport *vport)
  50. {
  51. return vport_priv(vport);
  52. }
  53. /* Called with rcu_read_lock and BH disabled. */
  54. static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb, __be32 vx_vni)
  55. {
  56. struct ovs_tunnel_info tun_info;
  57. struct vport *vport = vs->data;
  58. struct iphdr *iph;
  59. __be64 key;
  60. /* Save outer tunnel values */
  61. iph = ip_hdr(skb);
  62. key = cpu_to_be64(ntohl(vx_vni) >> 8);
  63. ovs_flow_tun_info_init(&tun_info, iph,
  64. udp_hdr(skb)->source, udp_hdr(skb)->dest,
  65. key, TUNNEL_KEY, NULL, 0);
  66. ovs_vport_receive(vport, skb, &tun_info);
  67. }
  68. static int vxlan_get_options(const struct vport *vport, struct sk_buff *skb)
  69. {
  70. struct vxlan_port *vxlan_port = vxlan_vport(vport);
  71. __be16 dst_port = inet_sk(vxlan_port->vs->sock->sk)->inet_sport;
  72. if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(dst_port)))
  73. return -EMSGSIZE;
  74. return 0;
  75. }
  76. static void vxlan_tnl_destroy(struct vport *vport)
  77. {
  78. struct vxlan_port *vxlan_port = vxlan_vport(vport);
  79. vxlan_sock_release(vxlan_port->vs);
  80. ovs_vport_deferred_free(vport);
  81. }
  82. static struct vport *vxlan_tnl_create(const struct vport_parms *parms)
  83. {
  84. struct net *net = ovs_dp_get_net(parms->dp);
  85. struct nlattr *options = parms->options;
  86. struct vxlan_port *vxlan_port;
  87. struct vxlan_sock *vs;
  88. struct vport *vport;
  89. struct nlattr *a;
  90. u16 dst_port;
  91. int err;
  92. if (!options) {
  93. err = -EINVAL;
  94. goto error;
  95. }
  96. a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
  97. if (a && nla_len(a) == sizeof(u16)) {
  98. dst_port = nla_get_u16(a);
  99. } else {
  100. /* Require destination port from userspace. */
  101. err = -EINVAL;
  102. goto error;
  103. }
  104. vport = ovs_vport_alloc(sizeof(struct vxlan_port),
  105. &ovs_vxlan_vport_ops, parms);
  106. if (IS_ERR(vport))
  107. return vport;
  108. vxlan_port = vxlan_vport(vport);
  109. strncpy(vxlan_port->name, parms->name, IFNAMSIZ);
  110. vs = vxlan_sock_add(net, htons(dst_port), vxlan_rcv, vport, true, 0);
  111. if (IS_ERR(vs)) {
  112. ovs_vport_free(vport);
  113. return (void *)vs;
  114. }
  115. vxlan_port->vs = vs;
  116. return vport;
  117. error:
  118. return ERR_PTR(err);
  119. }
  120. static int vxlan_tnl_send(struct vport *vport, struct sk_buff *skb)
  121. {
  122. struct net *net = ovs_dp_get_net(vport->dp);
  123. struct vxlan_port *vxlan_port = vxlan_vport(vport);
  124. __be16 dst_port = inet_sk(vxlan_port->vs->sock->sk)->inet_sport;
  125. struct ovs_key_ipv4_tunnel *tun_key;
  126. struct rtable *rt;
  127. struct flowi4 fl;
  128. __be16 src_port;
  129. __be16 df;
  130. int err;
  131. if (unlikely(!OVS_CB(skb)->egress_tun_info)) {
  132. err = -EINVAL;
  133. goto error;
  134. }
  135. tun_key = &OVS_CB(skb)->egress_tun_info->tunnel;
  136. /* Route lookup */
  137. memset(&fl, 0, sizeof(fl));
  138. fl.daddr = tun_key->ipv4_dst;
  139. fl.saddr = tun_key->ipv4_src;
  140. fl.flowi4_tos = RT_TOS(tun_key->ipv4_tos);
  141. fl.flowi4_mark = skb->mark;
  142. fl.flowi4_proto = IPPROTO_UDP;
  143. rt = ip_route_output_key(net, &fl);
  144. if (IS_ERR(rt)) {
  145. err = PTR_ERR(rt);
  146. goto error;
  147. }
  148. df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ?
  149. htons(IP_DF) : 0;
  150. skb->ignore_df = 1;
  151. src_port = udp_flow_src_port(net, skb, 0, 0, true);
  152. err = vxlan_xmit_skb(vxlan_port->vs, rt, skb,
  153. fl.saddr, tun_key->ipv4_dst,
  154. tun_key->ipv4_tos, tun_key->ipv4_ttl, df,
  155. src_port, dst_port,
  156. htonl(be64_to_cpu(tun_key->tun_id) << 8),
  157. false);
  158. if (err < 0)
  159. ip_rt_put(rt);
  160. return err;
  161. error:
  162. kfree_skb(skb);
  163. return err;
  164. }
  165. static int vxlan_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
  166. struct ovs_tunnel_info *egress_tun_info)
  167. {
  168. struct net *net = ovs_dp_get_net(vport->dp);
  169. struct vxlan_port *vxlan_port = vxlan_vport(vport);
  170. __be16 dst_port = inet_sk(vxlan_port->vs->sock->sk)->inet_sport;
  171. __be16 src_port;
  172. int port_min;
  173. int port_max;
  174. inet_get_local_port_range(net, &port_min, &port_max);
  175. src_port = udp_flow_src_port(net, skb, 0, 0, true);
  176. return ovs_tunnel_get_egress_info(egress_tun_info, net,
  177. OVS_CB(skb)->egress_tun_info,
  178. IPPROTO_UDP, skb->mark,
  179. src_port, dst_port);
  180. }
  181. static const char *vxlan_get_name(const struct vport *vport)
  182. {
  183. struct vxlan_port *vxlan_port = vxlan_vport(vport);
  184. return vxlan_port->name;
  185. }
  186. static struct vport_ops ovs_vxlan_vport_ops = {
  187. .type = OVS_VPORT_TYPE_VXLAN,
  188. .create = vxlan_tnl_create,
  189. .destroy = vxlan_tnl_destroy,
  190. .get_name = vxlan_get_name,
  191. .get_options = vxlan_get_options,
  192. .send = vxlan_tnl_send,
  193. .get_egress_tun_info = vxlan_get_egress_tun_info,
  194. .owner = THIS_MODULE,
  195. };
  196. static int __init ovs_vxlan_tnl_init(void)
  197. {
  198. return ovs_vport_ops_register(&ovs_vxlan_vport_ops);
  199. }
  200. static void __exit ovs_vxlan_tnl_exit(void)
  201. {
  202. ovs_vport_ops_unregister(&ovs_vxlan_vport_ops);
  203. }
  204. module_init(ovs_vxlan_tnl_init);
  205. module_exit(ovs_vxlan_tnl_exit);
  206. MODULE_DESCRIPTION("OVS: VXLAN switching port");
  207. MODULE_LICENSE("GPL");
  208. MODULE_ALIAS("vport-type-4");