vport-vxlan.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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, key, TUNNEL_KEY, NULL, 0);
  64. ovs_vport_receive(vport, skb, &tun_info);
  65. }
  66. static int vxlan_get_options(const struct vport *vport, struct sk_buff *skb)
  67. {
  68. struct vxlan_port *vxlan_port = vxlan_vport(vport);
  69. __be16 dst_port = inet_sk(vxlan_port->vs->sock->sk)->inet_sport;
  70. if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(dst_port)))
  71. return -EMSGSIZE;
  72. return 0;
  73. }
  74. static void vxlan_tnl_destroy(struct vport *vport)
  75. {
  76. struct vxlan_port *vxlan_port = vxlan_vport(vport);
  77. vxlan_sock_release(vxlan_port->vs);
  78. ovs_vport_deferred_free(vport);
  79. }
  80. static struct vport *vxlan_tnl_create(const struct vport_parms *parms)
  81. {
  82. struct net *net = ovs_dp_get_net(parms->dp);
  83. struct nlattr *options = parms->options;
  84. struct vxlan_port *vxlan_port;
  85. struct vxlan_sock *vs;
  86. struct vport *vport;
  87. struct nlattr *a;
  88. u16 dst_port;
  89. int err;
  90. if (!options) {
  91. err = -EINVAL;
  92. goto error;
  93. }
  94. a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
  95. if (a && nla_len(a) == sizeof(u16)) {
  96. dst_port = nla_get_u16(a);
  97. } else {
  98. /* Require destination port from userspace. */
  99. err = -EINVAL;
  100. goto error;
  101. }
  102. vport = ovs_vport_alloc(sizeof(struct vxlan_port),
  103. &ovs_vxlan_vport_ops, parms);
  104. if (IS_ERR(vport))
  105. return vport;
  106. vxlan_port = vxlan_vport(vport);
  107. strncpy(vxlan_port->name, parms->name, IFNAMSIZ);
  108. vs = vxlan_sock_add(net, htons(dst_port), vxlan_rcv, vport, true, 0);
  109. if (IS_ERR(vs)) {
  110. ovs_vport_free(vport);
  111. return (void *)vs;
  112. }
  113. vxlan_port->vs = vs;
  114. return vport;
  115. error:
  116. return ERR_PTR(err);
  117. }
  118. static int vxlan_tnl_send(struct vport *vport, struct sk_buff *skb)
  119. {
  120. struct net *net = ovs_dp_get_net(vport->dp);
  121. struct vxlan_port *vxlan_port = vxlan_vport(vport);
  122. __be16 dst_port = inet_sk(vxlan_port->vs->sock->sk)->inet_sport;
  123. struct ovs_key_ipv4_tunnel *tun_key;
  124. struct rtable *rt;
  125. struct flowi4 fl;
  126. __be16 src_port;
  127. __be16 df;
  128. int err;
  129. if (unlikely(!OVS_CB(skb)->egress_tun_info)) {
  130. err = -EINVAL;
  131. goto error;
  132. }
  133. tun_key = &OVS_CB(skb)->egress_tun_info->tunnel;
  134. /* Route lookup */
  135. memset(&fl, 0, sizeof(fl));
  136. fl.daddr = tun_key->ipv4_dst;
  137. fl.saddr = tun_key->ipv4_src;
  138. fl.flowi4_tos = RT_TOS(tun_key->ipv4_tos);
  139. fl.flowi4_mark = skb->mark;
  140. fl.flowi4_proto = IPPROTO_UDP;
  141. rt = ip_route_output_key(net, &fl);
  142. if (IS_ERR(rt)) {
  143. err = PTR_ERR(rt);
  144. goto error;
  145. }
  146. df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ?
  147. htons(IP_DF) : 0;
  148. skb->ignore_df = 1;
  149. src_port = udp_flow_src_port(net, skb, 0, 0, true);
  150. err = vxlan_xmit_skb(vxlan_port->vs, rt, skb,
  151. fl.saddr, tun_key->ipv4_dst,
  152. tun_key->ipv4_tos, tun_key->ipv4_ttl, df,
  153. src_port, dst_port,
  154. htonl(be64_to_cpu(tun_key->tun_id) << 8),
  155. false);
  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. static 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. .owner = THIS_MODULE,
  174. };
  175. static int __init ovs_vxlan_tnl_init(void)
  176. {
  177. return ovs_vport_ops_register(&ovs_vxlan_vport_ops);
  178. }
  179. static void __exit ovs_vxlan_tnl_exit(void)
  180. {
  181. ovs_vport_ops_unregister(&ovs_vxlan_vport_ops);
  182. }
  183. module_init(ovs_vxlan_tnl_init);
  184. module_exit(ovs_vxlan_tnl_exit);
  185. MODULE_DESCRIPTION("OVS: VXLAN switching port");
  186. MODULE_LICENSE("GPL");
  187. MODULE_ALIAS("vport-type-4");