vport-vxlan.c 4.2 KB

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