vport-vxlan.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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->cfg.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->cfg.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. NULL);
  61. if (err < 0)
  62. return err;
  63. if (exts[OVS_VXLAN_EXT_GBP])
  64. conf->flags |= VXLAN_F_GBP;
  65. return 0;
  66. }
  67. static struct vport *vxlan_tnl_create(const struct vport_parms *parms)
  68. {
  69. struct net *net = ovs_dp_get_net(parms->dp);
  70. struct nlattr *options = parms->options;
  71. struct net_device *dev;
  72. struct vport *vport;
  73. struct nlattr *a;
  74. int err;
  75. struct vxlan_config conf = {
  76. .no_share = true,
  77. .flags = VXLAN_F_COLLECT_METADATA | VXLAN_F_UDP_ZERO_CSUM6_RX,
  78. /* Don't restrict the packets that can be sent by MTU */
  79. .mtu = IP_MAX_MTU,
  80. };
  81. if (!options) {
  82. err = -EINVAL;
  83. goto error;
  84. }
  85. a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
  86. if (a && nla_len(a) == sizeof(u16)) {
  87. conf.dst_port = htons(nla_get_u16(a));
  88. } else {
  89. /* Require destination port from userspace. */
  90. err = -EINVAL;
  91. goto error;
  92. }
  93. vport = ovs_vport_alloc(0, &ovs_vxlan_netdev_vport_ops, parms);
  94. if (IS_ERR(vport))
  95. return vport;
  96. a = nla_find_nested(options, OVS_TUNNEL_ATTR_EXTENSION);
  97. if (a) {
  98. err = vxlan_configure_exts(vport, a, &conf);
  99. if (err) {
  100. ovs_vport_free(vport);
  101. goto error;
  102. }
  103. }
  104. rtnl_lock();
  105. dev = vxlan_dev_create(net, parms->name, NET_NAME_USER, &conf);
  106. if (IS_ERR(dev)) {
  107. rtnl_unlock();
  108. ovs_vport_free(vport);
  109. return ERR_CAST(dev);
  110. }
  111. err = dev_change_flags(dev, dev->flags | IFF_UP);
  112. if (err < 0) {
  113. rtnl_delete_link(dev);
  114. rtnl_unlock();
  115. ovs_vport_free(vport);
  116. goto error;
  117. }
  118. rtnl_unlock();
  119. return vport;
  120. error:
  121. return ERR_PTR(err);
  122. }
  123. static struct vport *vxlan_create(const struct vport_parms *parms)
  124. {
  125. struct vport *vport;
  126. vport = vxlan_tnl_create(parms);
  127. if (IS_ERR(vport))
  128. return vport;
  129. return ovs_netdev_link(vport, parms->name);
  130. }
  131. static struct vport_ops ovs_vxlan_netdev_vport_ops = {
  132. .type = OVS_VPORT_TYPE_VXLAN,
  133. .create = vxlan_create,
  134. .destroy = ovs_netdev_tunnel_destroy,
  135. .get_options = vxlan_get_options,
  136. .send = dev_queue_xmit,
  137. };
  138. static int __init ovs_vxlan_tnl_init(void)
  139. {
  140. return ovs_vport_ops_register(&ovs_vxlan_netdev_vport_ops);
  141. }
  142. static void __exit ovs_vxlan_tnl_exit(void)
  143. {
  144. ovs_vport_ops_unregister(&ovs_vxlan_netdev_vport_ops);
  145. }
  146. module_init(ovs_vxlan_tnl_init);
  147. module_exit(ovs_vxlan_tnl_exit);
  148. MODULE_DESCRIPTION("OVS: VXLAN switching port");
  149. MODULE_LICENSE("GPL");
  150. MODULE_ALIAS("vport-type-4");