vport-netdev.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) 2007-2012 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/if_arp.h>
  20. #include <linux/if_bridge.h>
  21. #include <linux/if_vlan.h>
  22. #include <linux/kernel.h>
  23. #include <linux/llc.h>
  24. #include <linux/rtnetlink.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/openvswitch.h>
  27. #include <linux/export.h>
  28. #include <net/ip_tunnels.h>
  29. #include <net/rtnetlink.h>
  30. #include "datapath.h"
  31. #include "vport.h"
  32. #include "vport-internal_dev.h"
  33. #include "vport-netdev.h"
  34. static struct vport_ops ovs_netdev_vport_ops;
  35. /* Must be called with rcu_read_lock. */
  36. static void netdev_port_receive(struct sk_buff *skb)
  37. {
  38. struct vport *vport;
  39. vport = ovs_netdev_get_vport(skb->dev);
  40. if (unlikely(!vport))
  41. goto error;
  42. if (unlikely(skb_warn_if_lro(skb)))
  43. goto error;
  44. /* Make our own copy of the packet. Otherwise we will mangle the
  45. * packet for anyone who came before us (e.g. tcpdump via AF_PACKET).
  46. */
  47. skb = skb_share_check(skb, GFP_ATOMIC);
  48. if (unlikely(!skb))
  49. return;
  50. skb_push(skb, ETH_HLEN);
  51. skb_postpush_rcsum(skb, skb->data, ETH_HLEN);
  52. ovs_vport_receive(vport, skb, skb_tunnel_info(skb));
  53. return;
  54. error:
  55. kfree_skb(skb);
  56. }
  57. /* Called with rcu_read_lock and bottom-halves disabled. */
  58. static rx_handler_result_t netdev_frame_hook(struct sk_buff **pskb)
  59. {
  60. struct sk_buff *skb = *pskb;
  61. if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
  62. return RX_HANDLER_PASS;
  63. netdev_port_receive(skb);
  64. return RX_HANDLER_CONSUMED;
  65. }
  66. static struct net_device *get_dpdev(const struct datapath *dp)
  67. {
  68. struct vport *local;
  69. local = ovs_vport_ovsl(dp, OVSP_LOCAL);
  70. BUG_ON(!local);
  71. return local->dev;
  72. }
  73. struct vport *ovs_netdev_link(struct vport *vport, const char *name)
  74. {
  75. int err;
  76. vport->dev = dev_get_by_name(ovs_dp_get_net(vport->dp), name);
  77. if (!vport->dev) {
  78. err = -ENODEV;
  79. goto error_free_vport;
  80. }
  81. if (vport->dev->flags & IFF_LOOPBACK ||
  82. vport->dev->type != ARPHRD_ETHER ||
  83. ovs_is_internal_dev(vport->dev)) {
  84. err = -EINVAL;
  85. goto error_put;
  86. }
  87. rtnl_lock();
  88. err = netdev_master_upper_dev_link(vport->dev,
  89. get_dpdev(vport->dp));
  90. if (err)
  91. goto error_unlock;
  92. err = netdev_rx_handler_register(vport->dev, netdev_frame_hook,
  93. vport);
  94. if (err)
  95. goto error_master_upper_dev_unlink;
  96. dev_disable_lro(vport->dev);
  97. dev_set_promiscuity(vport->dev, 1);
  98. vport->dev->priv_flags |= IFF_OVS_DATAPATH;
  99. rtnl_unlock();
  100. return vport;
  101. error_master_upper_dev_unlink:
  102. netdev_upper_dev_unlink(vport->dev, get_dpdev(vport->dp));
  103. error_unlock:
  104. rtnl_unlock();
  105. error_put:
  106. dev_put(vport->dev);
  107. error_free_vport:
  108. ovs_vport_free(vport);
  109. return ERR_PTR(err);
  110. }
  111. EXPORT_SYMBOL_GPL(ovs_netdev_link);
  112. static struct vport *netdev_create(const struct vport_parms *parms)
  113. {
  114. struct vport *vport;
  115. vport = ovs_vport_alloc(0, &ovs_netdev_vport_ops, parms);
  116. if (IS_ERR(vport))
  117. return vport;
  118. return ovs_netdev_link(vport, parms->name);
  119. }
  120. static void vport_netdev_free(struct rcu_head *rcu)
  121. {
  122. struct vport *vport = container_of(rcu, struct vport, rcu);
  123. if (vport->dev)
  124. dev_put(vport->dev);
  125. ovs_vport_free(vport);
  126. }
  127. void ovs_netdev_detach_dev(struct vport *vport)
  128. {
  129. ASSERT_RTNL();
  130. vport->dev->priv_flags &= ~IFF_OVS_DATAPATH;
  131. netdev_rx_handler_unregister(vport->dev);
  132. netdev_upper_dev_unlink(vport->dev,
  133. netdev_master_upper_dev_get(vport->dev));
  134. dev_set_promiscuity(vport->dev, -1);
  135. }
  136. EXPORT_SYMBOL_GPL(ovs_netdev_detach_dev);
  137. static void netdev_destroy(struct vport *vport)
  138. {
  139. rtnl_lock();
  140. if (vport->dev->priv_flags & IFF_OVS_DATAPATH)
  141. ovs_netdev_detach_dev(vport);
  142. rtnl_unlock();
  143. call_rcu(&vport->rcu, vport_netdev_free);
  144. }
  145. void ovs_netdev_tunnel_destroy(struct vport *vport)
  146. {
  147. rtnl_lock();
  148. if (vport->dev->priv_flags & IFF_OVS_DATAPATH)
  149. ovs_netdev_detach_dev(vport);
  150. /* We can be invoked by both explicit vport deletion and
  151. * underlying netdev deregistration; delete the link only
  152. * if it's not already shutting down.
  153. */
  154. if (vport->dev->reg_state == NETREG_REGISTERED)
  155. rtnl_delete_link(vport->dev);
  156. dev_put(vport->dev);
  157. vport->dev = NULL;
  158. rtnl_unlock();
  159. call_rcu(&vport->rcu, vport_netdev_free);
  160. }
  161. EXPORT_SYMBOL_GPL(ovs_netdev_tunnel_destroy);
  162. /* Returns null if this device is not attached to a datapath. */
  163. struct vport *ovs_netdev_get_vport(struct net_device *dev)
  164. {
  165. if (likely(dev->priv_flags & IFF_OVS_DATAPATH))
  166. return (struct vport *)
  167. rcu_dereference_rtnl(dev->rx_handler_data);
  168. else
  169. return NULL;
  170. }
  171. static struct vport_ops ovs_netdev_vport_ops = {
  172. .type = OVS_VPORT_TYPE_NETDEV,
  173. .create = netdev_create,
  174. .destroy = netdev_destroy,
  175. .send = dev_queue_xmit,
  176. };
  177. int __init ovs_netdev_init(void)
  178. {
  179. return ovs_vport_ops_register(&ovs_netdev_vport_ops);
  180. }
  181. void ovs_netdev_exit(void)
  182. {
  183. ovs_vport_ops_unregister(&ovs_netdev_vport_ops);
  184. }