vport-vxlan.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014 Nicira, Inc.
  4. * Copyright (c) 2013 Cisco Systems, Inc.
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/skbuff.h>
  8. #include <linux/openvswitch.h>
  9. #include <linux/module.h>
  10. #include <net/udp.h>
  11. #include <net/ip_tunnels.h>
  12. #include <net/rtnetlink.h>
  13. #include <net/vxlan.h>
  14. #include "datapath.h"
  15. #include "vport.h"
  16. #include "vport-netdev.h"
  17. static struct vport_ops ovs_vxlan_netdev_vport_ops;
  18. static int vxlan_get_options(const struct vport *vport, struct sk_buff *skb)
  19. {
  20. struct vxlan_dev *vxlan = netdev_priv(vport->dev);
  21. __be16 dst_port = vxlan->cfg.dst_port;
  22. if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(dst_port)))
  23. return -EMSGSIZE;
  24. if (vxlan->cfg.flags & VXLAN_F_GBP) {
  25. struct nlattr *exts;
  26. exts = nla_nest_start_noflag(skb, OVS_TUNNEL_ATTR_EXTENSION);
  27. if (!exts)
  28. return -EMSGSIZE;
  29. if (vxlan->cfg.flags & VXLAN_F_GBP &&
  30. nla_put_flag(skb, OVS_VXLAN_EXT_GBP))
  31. return -EMSGSIZE;
  32. nla_nest_end(skb, exts);
  33. }
  34. return 0;
  35. }
  36. static const struct nla_policy exts_policy[OVS_VXLAN_EXT_MAX + 1] = {
  37. [OVS_VXLAN_EXT_GBP] = { .type = NLA_FLAG, },
  38. };
  39. static int vxlan_configure_exts(struct vport *vport, struct nlattr *attr,
  40. struct vxlan_config *conf)
  41. {
  42. struct nlattr *exts[OVS_VXLAN_EXT_MAX + 1];
  43. int err;
  44. if (nla_len(attr) < sizeof(struct nlattr))
  45. return -EINVAL;
  46. err = nla_parse_nested_deprecated(exts, OVS_VXLAN_EXT_MAX, attr,
  47. exts_policy, NULL);
  48. if (err < 0)
  49. return err;
  50. if (exts[OVS_VXLAN_EXT_GBP])
  51. conf->flags |= VXLAN_F_GBP;
  52. return 0;
  53. }
  54. static struct vport *vxlan_tnl_create(const struct vport_parms *parms)
  55. {
  56. struct net *net = ovs_dp_get_net(parms->dp);
  57. struct nlattr *options = parms->options;
  58. struct net_device *dev;
  59. struct vport *vport;
  60. struct nlattr *a;
  61. int err;
  62. struct vxlan_config conf = {
  63. .no_share = true,
  64. .flags = VXLAN_F_COLLECT_METADATA | VXLAN_F_UDP_ZERO_CSUM6_RX,
  65. /* Don't restrict the packets that can be sent by MTU */
  66. .mtu = IP_MAX_MTU,
  67. };
  68. if (!options) {
  69. err = -EINVAL;
  70. goto error;
  71. }
  72. a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
  73. if (a && nla_len(a) == sizeof(u16)) {
  74. conf.dst_port = htons(nla_get_u16(a));
  75. } else {
  76. /* Require destination port from userspace. */
  77. err = -EINVAL;
  78. goto error;
  79. }
  80. vport = ovs_vport_alloc(0, &ovs_vxlan_netdev_vport_ops, parms);
  81. if (IS_ERR(vport))
  82. return vport;
  83. a = nla_find_nested(options, OVS_TUNNEL_ATTR_EXTENSION);
  84. if (a) {
  85. err = vxlan_configure_exts(vport, a, &conf);
  86. if (err) {
  87. ovs_vport_free(vport);
  88. goto error;
  89. }
  90. }
  91. rtnl_lock();
  92. dev = vxlan_dev_create(net, parms->name, NET_NAME_USER, &conf);
  93. if (IS_ERR(dev)) {
  94. rtnl_unlock();
  95. ovs_vport_free(vport);
  96. return ERR_CAST(dev);
  97. }
  98. err = dev_change_flags(dev, dev->flags | IFF_UP, NULL);
  99. if (err < 0) {
  100. rtnl_delete_link(dev);
  101. rtnl_unlock();
  102. ovs_vport_free(vport);
  103. goto error;
  104. }
  105. rtnl_unlock();
  106. return vport;
  107. error:
  108. return ERR_PTR(err);
  109. }
  110. static struct vport *vxlan_create(const struct vport_parms *parms)
  111. {
  112. struct vport *vport;
  113. vport = vxlan_tnl_create(parms);
  114. if (IS_ERR(vport))
  115. return vport;
  116. return ovs_netdev_link(vport, parms->name);
  117. }
  118. static struct vport_ops ovs_vxlan_netdev_vport_ops = {
  119. .type = OVS_VPORT_TYPE_VXLAN,
  120. .create = vxlan_create,
  121. .destroy = ovs_netdev_tunnel_destroy,
  122. .get_options = vxlan_get_options,
  123. .send = dev_queue_xmit,
  124. };
  125. static int __init ovs_vxlan_tnl_init(void)
  126. {
  127. return ovs_vport_ops_register(&ovs_vxlan_netdev_vport_ops);
  128. }
  129. static void __exit ovs_vxlan_tnl_exit(void)
  130. {
  131. ovs_vport_ops_unregister(&ovs_vxlan_netdev_vport_ops);
  132. }
  133. module_init(ovs_vxlan_tnl_init);
  134. module_exit(ovs_vxlan_tnl_exit);
  135. MODULE_DESCRIPTION("OVS: VXLAN switching port");
  136. MODULE_LICENSE("GPL");
  137. MODULE_ALIAS("vport-type-4");