vport-vxlan.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #include <linux/kernel.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/openvswitch.h>
  22. #include <linux/module.h>
  23. #include <net/udp.h>
  24. #include <net/ip_tunnels.h>
  25. #include <net/rtnetlink.h>
  26. #include <net/vxlan.h>
  27. #include "datapath.h"
  28. #include "vport.h"
  29. #include "vport-netdev.h"
  30. static struct vport_ops ovs_vxlan_netdev_vport_ops;
  31. static int vxlan_get_options(const struct vport *vport, struct sk_buff *skb)
  32. {
  33. struct vxlan_dev *vxlan = netdev_priv(vport->dev);
  34. __be16 dst_port = vxlan->cfg.dst_port;
  35. if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(dst_port)))
  36. return -EMSGSIZE;
  37. if (vxlan->flags & VXLAN_F_GBP) {
  38. struct nlattr *exts;
  39. exts = nla_nest_start(skb, OVS_TUNNEL_ATTR_EXTENSION);
  40. if (!exts)
  41. return -EMSGSIZE;
  42. if (vxlan->flags & VXLAN_F_GBP &&
  43. nla_put_flag(skb, OVS_VXLAN_EXT_GBP))
  44. return -EMSGSIZE;
  45. nla_nest_end(skb, exts);
  46. }
  47. return 0;
  48. }
  49. static const struct nla_policy exts_policy[OVS_VXLAN_EXT_MAX + 1] = {
  50. [OVS_VXLAN_EXT_GBP] = { .type = NLA_FLAG, },
  51. };
  52. static int vxlan_configure_exts(struct vport *vport, struct nlattr *attr,
  53. struct vxlan_config *conf)
  54. {
  55. struct nlattr *exts[OVS_VXLAN_EXT_MAX + 1];
  56. int err;
  57. if (nla_len(attr) < sizeof(struct nlattr))
  58. return -EINVAL;
  59. err = nla_parse_nested(exts, OVS_VXLAN_EXT_MAX, attr, exts_policy);
  60. if (err < 0)
  61. return err;
  62. if (exts[OVS_VXLAN_EXT_GBP])
  63. conf->flags |= VXLAN_F_GBP;
  64. return 0;
  65. }
  66. static struct vport *vxlan_tnl_create(const struct vport_parms *parms)
  67. {
  68. struct net *net = ovs_dp_get_net(parms->dp);
  69. struct nlattr *options = parms->options;
  70. struct net_device *dev;
  71. struct vport *vport;
  72. struct nlattr *a;
  73. int err;
  74. struct vxlan_config conf = {
  75. .no_share = true,
  76. .flags = VXLAN_F_COLLECT_METADATA,
  77. };
  78. if (!options) {
  79. err = -EINVAL;
  80. goto error;
  81. }
  82. a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
  83. if (a && nla_len(a) == sizeof(u16)) {
  84. conf.dst_port = htons(nla_get_u16(a));
  85. } else {
  86. /* Require destination port from userspace. */
  87. err = -EINVAL;
  88. goto error;
  89. }
  90. vport = ovs_vport_alloc(0, &ovs_vxlan_netdev_vport_ops, parms);
  91. if (IS_ERR(vport))
  92. return vport;
  93. a = nla_find_nested(options, OVS_TUNNEL_ATTR_EXTENSION);
  94. if (a) {
  95. err = vxlan_configure_exts(vport, a, &conf);
  96. if (err) {
  97. ovs_vport_free(vport);
  98. goto error;
  99. }
  100. }
  101. rtnl_lock();
  102. dev = vxlan_dev_create(net, parms->name, NET_NAME_USER, &conf);
  103. if (IS_ERR(dev)) {
  104. rtnl_unlock();
  105. ovs_vport_free(vport);
  106. return ERR_CAST(dev);
  107. }
  108. dev_change_flags(dev, dev->flags | IFF_UP);
  109. rtnl_unlock();
  110. return vport;
  111. error:
  112. return ERR_PTR(err);
  113. }
  114. static struct vport *vxlan_create(const struct vport_parms *parms)
  115. {
  116. struct vport *vport;
  117. vport = vxlan_tnl_create(parms);
  118. if (IS_ERR(vport))
  119. return vport;
  120. return ovs_netdev_link(vport, parms->name);
  121. }
  122. static int vxlan_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
  123. struct dp_upcall_info *upcall)
  124. {
  125. struct vxlan_dev *vxlan = netdev_priv(vport->dev);
  126. struct net *net = ovs_dp_get_net(vport->dp);
  127. __be16 dst_port = vxlan_dev_dst_port(vxlan);
  128. __be16 src_port;
  129. int port_min;
  130. int port_max;
  131. inet_get_local_port_range(net, &port_min, &port_max);
  132. src_port = udp_flow_src_port(net, skb, 0, 0, true);
  133. return ovs_tunnel_get_egress_info(upcall, net,
  134. skb, IPPROTO_UDP,
  135. src_port, dst_port);
  136. }
  137. static struct vport_ops ovs_vxlan_netdev_vport_ops = {
  138. .type = OVS_VPORT_TYPE_VXLAN,
  139. .create = vxlan_create,
  140. .destroy = ovs_netdev_tunnel_destroy,
  141. .get_options = vxlan_get_options,
  142. .send = ovs_netdev_send,
  143. .get_egress_tun_info = vxlan_get_egress_tun_info,
  144. };
  145. static int __init ovs_vxlan_tnl_init(void)
  146. {
  147. return ovs_vport_ops_register(&ovs_vxlan_netdev_vport_ops);
  148. }
  149. static void __exit ovs_vxlan_tnl_exit(void)
  150. {
  151. ovs_vport_ops_unregister(&ovs_vxlan_netdev_vport_ops);
  152. }
  153. module_init(ovs_vxlan_tnl_init);
  154. module_exit(ovs_vxlan_tnl_exit);
  155. MODULE_DESCRIPTION("OVS: VXLAN switching port");
  156. MODULE_LICENSE("GPL");
  157. MODULE_ALIAS("vport-type-4");