vport-vxlan.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. #include "vport-vxlan.h"
  40. /**
  41. * struct vxlan_port - Keeps track of open UDP ports
  42. * @vs: vxlan_sock created for the port.
  43. * @name: vport name.
  44. */
  45. struct vxlan_port {
  46. struct vxlan_sock *vs;
  47. char name[IFNAMSIZ];
  48. u32 exts; /* VXLAN_F_* in <net/vxlan.h> */
  49. };
  50. static struct vport_ops ovs_vxlan_vport_ops;
  51. static inline struct vxlan_port *vxlan_vport(const struct vport *vport)
  52. {
  53. return vport_priv(vport);
  54. }
  55. /* Called with rcu_read_lock and BH disabled. */
  56. static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb,
  57. struct vxlan_metadata *md)
  58. {
  59. struct ovs_tunnel_info tun_info;
  60. struct vxlan_port *vxlan_port;
  61. struct vport *vport = vs->data;
  62. struct iphdr *iph;
  63. struct ovs_vxlan_opts opts = {
  64. .gbp = md->gbp,
  65. };
  66. __be64 key;
  67. __be16 flags;
  68. flags = TUNNEL_KEY | (udp_hdr(skb)->check != 0 ? TUNNEL_CSUM : 0);
  69. vxlan_port = vxlan_vport(vport);
  70. if (vxlan_port->exts & VXLAN_F_GBP && md->gbp)
  71. flags |= TUNNEL_VXLAN_OPT;
  72. /* Save outer tunnel values */
  73. iph = ip_hdr(skb);
  74. key = cpu_to_be64(ntohl(md->vni) >> 8);
  75. ovs_flow_tun_info_init(&tun_info, iph,
  76. udp_hdr(skb)->source, udp_hdr(skb)->dest,
  77. key, flags, &opts, sizeof(opts));
  78. ovs_vport_receive(vport, skb, &tun_info);
  79. }
  80. static int vxlan_get_options(const struct vport *vport, struct sk_buff *skb)
  81. {
  82. struct vxlan_port *vxlan_port = vxlan_vport(vport);
  83. __be16 dst_port = inet_sk(vxlan_port->vs->sock->sk)->inet_sport;
  84. if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(dst_port)))
  85. return -EMSGSIZE;
  86. if (vxlan_port->exts) {
  87. struct nlattr *exts;
  88. exts = nla_nest_start(skb, OVS_TUNNEL_ATTR_EXTENSION);
  89. if (!exts)
  90. return -EMSGSIZE;
  91. if (vxlan_port->exts & VXLAN_F_GBP &&
  92. nla_put_flag(skb, OVS_VXLAN_EXT_GBP))
  93. return -EMSGSIZE;
  94. nla_nest_end(skb, exts);
  95. }
  96. return 0;
  97. }
  98. static void vxlan_tnl_destroy(struct vport *vport)
  99. {
  100. struct vxlan_port *vxlan_port = vxlan_vport(vport);
  101. vxlan_sock_release(vxlan_port->vs);
  102. ovs_vport_deferred_free(vport);
  103. }
  104. static const struct nla_policy exts_policy[OVS_VXLAN_EXT_MAX+1] = {
  105. [OVS_VXLAN_EXT_GBP] = { .type = NLA_FLAG, },
  106. };
  107. static int vxlan_configure_exts(struct vport *vport, struct nlattr *attr)
  108. {
  109. struct nlattr *exts[OVS_VXLAN_EXT_MAX+1];
  110. struct vxlan_port *vxlan_port;
  111. int err;
  112. if (nla_len(attr) < sizeof(struct nlattr))
  113. return -EINVAL;
  114. err = nla_parse_nested(exts, OVS_VXLAN_EXT_MAX, attr, exts_policy);
  115. if (err < 0)
  116. return err;
  117. vxlan_port = vxlan_vport(vport);
  118. if (exts[OVS_VXLAN_EXT_GBP])
  119. vxlan_port->exts |= VXLAN_F_GBP;
  120. return 0;
  121. }
  122. static struct vport *vxlan_tnl_create(const struct vport_parms *parms)
  123. {
  124. struct net *net = ovs_dp_get_net(parms->dp);
  125. struct nlattr *options = parms->options;
  126. struct vxlan_port *vxlan_port;
  127. struct vxlan_sock *vs;
  128. struct vport *vport;
  129. struct nlattr *a;
  130. u16 dst_port;
  131. int err;
  132. if (!options) {
  133. err = -EINVAL;
  134. goto error;
  135. }
  136. a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
  137. if (a && nla_len(a) == sizeof(u16)) {
  138. dst_port = nla_get_u16(a);
  139. } else {
  140. /* Require destination port from userspace. */
  141. err = -EINVAL;
  142. goto error;
  143. }
  144. vport = ovs_vport_alloc(sizeof(struct vxlan_port),
  145. &ovs_vxlan_vport_ops, parms);
  146. if (IS_ERR(vport))
  147. return vport;
  148. vxlan_port = vxlan_vport(vport);
  149. strncpy(vxlan_port->name, parms->name, IFNAMSIZ);
  150. a = nla_find_nested(options, OVS_TUNNEL_ATTR_EXTENSION);
  151. if (a) {
  152. err = vxlan_configure_exts(vport, a);
  153. if (err) {
  154. ovs_vport_free(vport);
  155. goto error;
  156. }
  157. }
  158. vs = vxlan_sock_add(net, htons(dst_port), vxlan_rcv, vport, true,
  159. vxlan_port->exts);
  160. if (IS_ERR(vs)) {
  161. ovs_vport_free(vport);
  162. return (void *)vs;
  163. }
  164. vxlan_port->vs = vs;
  165. return vport;
  166. error:
  167. return ERR_PTR(err);
  168. }
  169. static int vxlan_ext_gbp(struct sk_buff *skb)
  170. {
  171. const struct ovs_tunnel_info *tun_info;
  172. const struct ovs_vxlan_opts *opts;
  173. tun_info = OVS_CB(skb)->egress_tun_info;
  174. opts = tun_info->options;
  175. if (tun_info->tunnel.tun_flags & TUNNEL_VXLAN_OPT &&
  176. tun_info->options_len >= sizeof(*opts))
  177. return opts->gbp;
  178. else
  179. return 0;
  180. }
  181. static int vxlan_tnl_send(struct vport *vport, struct sk_buff *skb)
  182. {
  183. struct net *net = ovs_dp_get_net(vport->dp);
  184. struct vxlan_port *vxlan_port = vxlan_vport(vport);
  185. struct sock *sk = vxlan_port->vs->sock->sk;
  186. __be16 dst_port = inet_sk(sk)->inet_sport;
  187. const struct ovs_key_ipv4_tunnel *tun_key;
  188. struct vxlan_metadata md = {0};
  189. struct rtable *rt;
  190. struct flowi4 fl;
  191. __be16 src_port;
  192. __be16 df;
  193. int err;
  194. u32 vxflags;
  195. if (unlikely(!OVS_CB(skb)->egress_tun_info)) {
  196. err = -EINVAL;
  197. goto error;
  198. }
  199. tun_key = &OVS_CB(skb)->egress_tun_info->tunnel;
  200. rt = ovs_tunnel_route_lookup(net, tun_key, skb->mark, &fl, IPPROTO_UDP);
  201. if (IS_ERR(rt)) {
  202. err = PTR_ERR(rt);
  203. goto error;
  204. }
  205. df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ?
  206. htons(IP_DF) : 0;
  207. skb->ignore_df = 1;
  208. src_port = udp_flow_src_port(net, skb, 0, 0, true);
  209. md.vni = htonl(be64_to_cpu(tun_key->tun_id) << 8);
  210. md.gbp = vxlan_ext_gbp(skb);
  211. vxflags = vxlan_port->exts |
  212. (tun_key->tun_flags & TUNNEL_CSUM ? VXLAN_F_UDP_CSUM : 0);
  213. err = vxlan_xmit_skb(rt, sk, skb, fl.saddr, tun_key->ipv4_dst,
  214. tun_key->ipv4_tos, tun_key->ipv4_ttl, df,
  215. src_port, dst_port,
  216. &md, false, vxflags);
  217. if (err < 0)
  218. ip_rt_put(rt);
  219. return err;
  220. error:
  221. kfree_skb(skb);
  222. return err;
  223. }
  224. static int vxlan_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
  225. struct ovs_tunnel_info *egress_tun_info)
  226. {
  227. struct net *net = ovs_dp_get_net(vport->dp);
  228. struct vxlan_port *vxlan_port = vxlan_vport(vport);
  229. __be16 dst_port = inet_sk(vxlan_port->vs->sock->sk)->inet_sport;
  230. __be16 src_port;
  231. int port_min;
  232. int port_max;
  233. inet_get_local_port_range(net, &port_min, &port_max);
  234. src_port = udp_flow_src_port(net, skb, 0, 0, true);
  235. return ovs_tunnel_get_egress_info(egress_tun_info, net,
  236. OVS_CB(skb)->egress_tun_info,
  237. IPPROTO_UDP, skb->mark,
  238. src_port, dst_port);
  239. }
  240. static const char *vxlan_get_name(const struct vport *vport)
  241. {
  242. struct vxlan_port *vxlan_port = vxlan_vport(vport);
  243. return vxlan_port->name;
  244. }
  245. static struct vport_ops ovs_vxlan_vport_ops = {
  246. .type = OVS_VPORT_TYPE_VXLAN,
  247. .create = vxlan_tnl_create,
  248. .destroy = vxlan_tnl_destroy,
  249. .get_name = vxlan_get_name,
  250. .get_options = vxlan_get_options,
  251. .send = vxlan_tnl_send,
  252. .get_egress_tun_info = vxlan_get_egress_tun_info,
  253. .owner = THIS_MODULE,
  254. };
  255. static int __init ovs_vxlan_tnl_init(void)
  256. {
  257. return ovs_vport_ops_register(&ovs_vxlan_vport_ops);
  258. }
  259. static void __exit ovs_vxlan_tnl_exit(void)
  260. {
  261. ovs_vport_ops_unregister(&ovs_vxlan_vport_ops);
  262. }
  263. module_init(ovs_vxlan_tnl_init);
  264. module_exit(ovs_vxlan_tnl_exit);
  265. MODULE_DESCRIPTION("OVS: VXLAN switching port");
  266. MODULE_LICENSE("GPL");
  267. MODULE_ALIAS("vport-type-4");