vport-geneve.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2014 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/in.h>
  11. #include <linux/ip.h>
  12. #include <linux/net.h>
  13. #include <linux/rculist.h>
  14. #include <linux/udp.h>
  15. #include <linux/if_vlan.h>
  16. #include <linux/module.h>
  17. #include <net/geneve.h>
  18. #include <net/icmp.h>
  19. #include <net/ip.h>
  20. #include <net/route.h>
  21. #include <net/udp.h>
  22. #include <net/xfrm.h>
  23. #include "datapath.h"
  24. #include "vport.h"
  25. #include "vport-netdev.h"
  26. static struct vport_ops ovs_geneve_vport_ops;
  27. /**
  28. * struct geneve_port - Keeps track of open UDP ports
  29. * @dst_port: destination port.
  30. */
  31. struct geneve_port {
  32. u16 dst_port;
  33. };
  34. static inline struct geneve_port *geneve_vport(const struct vport *vport)
  35. {
  36. return vport_priv(vport);
  37. }
  38. static int geneve_get_options(const struct vport *vport,
  39. struct sk_buff *skb)
  40. {
  41. struct geneve_port *geneve_port = geneve_vport(vport);
  42. if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, geneve_port->dst_port))
  43. return -EMSGSIZE;
  44. return 0;
  45. }
  46. static struct vport *geneve_tnl_create(const struct vport_parms *parms)
  47. {
  48. struct net *net = ovs_dp_get_net(parms->dp);
  49. struct nlattr *options = parms->options;
  50. struct geneve_port *geneve_port;
  51. struct net_device *dev;
  52. struct vport *vport;
  53. struct nlattr *a;
  54. u16 dst_port;
  55. int err;
  56. if (!options) {
  57. err = -EINVAL;
  58. goto error;
  59. }
  60. a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
  61. if (a && nla_len(a) == sizeof(u16)) {
  62. dst_port = nla_get_u16(a);
  63. } else {
  64. /* Require destination port from userspace. */
  65. err = -EINVAL;
  66. goto error;
  67. }
  68. vport = ovs_vport_alloc(sizeof(struct geneve_port),
  69. &ovs_geneve_vport_ops, parms);
  70. if (IS_ERR(vport))
  71. return vport;
  72. geneve_port = geneve_vport(vport);
  73. geneve_port->dst_port = dst_port;
  74. rtnl_lock();
  75. dev = geneve_dev_create_fb(net, parms->name, NET_NAME_USER, dst_port);
  76. if (IS_ERR(dev)) {
  77. rtnl_unlock();
  78. ovs_vport_free(vport);
  79. return ERR_CAST(dev);
  80. }
  81. err = dev_change_flags(dev, dev->flags | IFF_UP);
  82. if (err < 0) {
  83. rtnl_delete_link(dev);
  84. rtnl_unlock();
  85. ovs_vport_free(vport);
  86. goto error;
  87. }
  88. rtnl_unlock();
  89. return vport;
  90. error:
  91. return ERR_PTR(err);
  92. }
  93. static struct vport *geneve_create(const struct vport_parms *parms)
  94. {
  95. struct vport *vport;
  96. vport = geneve_tnl_create(parms);
  97. if (IS_ERR(vport))
  98. return vport;
  99. return ovs_netdev_link(vport, parms->name);
  100. }
  101. static struct vport_ops ovs_geneve_vport_ops = {
  102. .type = OVS_VPORT_TYPE_GENEVE,
  103. .create = geneve_create,
  104. .destroy = ovs_netdev_tunnel_destroy,
  105. .get_options = geneve_get_options,
  106. .send = dev_queue_xmit,
  107. };
  108. static int __init ovs_geneve_tnl_init(void)
  109. {
  110. return ovs_vport_ops_register(&ovs_geneve_vport_ops);
  111. }
  112. static void __exit ovs_geneve_tnl_exit(void)
  113. {
  114. ovs_vport_ops_unregister(&ovs_geneve_vport_ops);
  115. }
  116. module_init(ovs_geneve_tnl_init);
  117. module_exit(ovs_geneve_tnl_exit);
  118. MODULE_DESCRIPTION("OVS: Geneve switching port");
  119. MODULE_LICENSE("GPL");
  120. MODULE_ALIAS("vport-type-5");