vport-netdev.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. if (skb->dev->type == ARPHRD_ETHER) {
  51. skb_push(skb, ETH_HLEN);
  52. skb_postpush_rcsum(skb, skb->data, ETH_HLEN);
  53. }
  54. ovs_vport_receive(vport, skb, skb_tunnel_info(skb));
  55. return;
  56. error:
  57. kfree_skb(skb);
  58. }
  59. /* Called with rcu_read_lock and bottom-halves disabled. */
  60. static rx_handler_result_t netdev_frame_hook(struct sk_buff **pskb)
  61. {
  62. struct sk_buff *skb = *pskb;
  63. if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
  64. return RX_HANDLER_PASS;
  65. netdev_port_receive(skb);
  66. return RX_HANDLER_CONSUMED;
  67. }
  68. static struct net_device *get_dpdev(const struct datapath *dp)
  69. {
  70. struct vport *local;
  71. local = ovs_vport_ovsl(dp, OVSP_LOCAL);
  72. BUG_ON(!local);
  73. return local->dev;
  74. }
  75. struct vport *ovs_netdev_link(struct vport *vport, const char *name)
  76. {
  77. int err;
  78. vport->dev = dev_get_by_name(ovs_dp_get_net(vport->dp), name);
  79. if (!vport->dev) {
  80. err = -ENODEV;
  81. goto error_free_vport;
  82. }
  83. if (vport->dev->flags & IFF_LOOPBACK ||
  84. (vport->dev->type != ARPHRD_ETHER &&
  85. vport->dev->type != ARPHRD_NONE) ||
  86. ovs_is_internal_dev(vport->dev)) {
  87. err = -EINVAL;
  88. goto error_put;
  89. }
  90. rtnl_lock();
  91. err = netdev_master_upper_dev_link(vport->dev,
  92. get_dpdev(vport->dp),
  93. NULL, NULL, NULL);
  94. if (err)
  95. goto error_unlock;
  96. err = netdev_rx_handler_register(vport->dev, netdev_frame_hook,
  97. vport);
  98. if (err)
  99. goto error_master_upper_dev_unlink;
  100. dev_disable_lro(vport->dev);
  101. dev_set_promiscuity(vport->dev, 1);
  102. vport->dev->priv_flags |= IFF_OVS_DATAPATH;
  103. rtnl_unlock();
  104. return vport;
  105. error_master_upper_dev_unlink:
  106. netdev_upper_dev_unlink(vport->dev, get_dpdev(vport->dp));
  107. error_unlock:
  108. rtnl_unlock();
  109. error_put:
  110. dev_put(vport->dev);
  111. error_free_vport:
  112. ovs_vport_free(vport);
  113. return ERR_PTR(err);
  114. }
  115. EXPORT_SYMBOL_GPL(ovs_netdev_link);
  116. static struct vport *netdev_create(const struct vport_parms *parms)
  117. {
  118. struct vport *vport;
  119. vport = ovs_vport_alloc(0, &ovs_netdev_vport_ops, parms);
  120. if (IS_ERR(vport))
  121. return vport;
  122. return ovs_netdev_link(vport, parms->name);
  123. }
  124. static void vport_netdev_free(struct rcu_head *rcu)
  125. {
  126. struct vport *vport = container_of(rcu, struct vport, rcu);
  127. if (vport->dev)
  128. dev_put(vport->dev);
  129. ovs_vport_free(vport);
  130. }
  131. void ovs_netdev_detach_dev(struct vport *vport)
  132. {
  133. ASSERT_RTNL();
  134. vport->dev->priv_flags &= ~IFF_OVS_DATAPATH;
  135. netdev_rx_handler_unregister(vport->dev);
  136. netdev_upper_dev_unlink(vport->dev,
  137. netdev_master_upper_dev_get(vport->dev));
  138. dev_set_promiscuity(vport->dev, -1);
  139. }
  140. static void netdev_destroy(struct vport *vport)
  141. {
  142. rtnl_lock();
  143. if (vport->dev->priv_flags & IFF_OVS_DATAPATH)
  144. ovs_netdev_detach_dev(vport);
  145. rtnl_unlock();
  146. call_rcu(&vport->rcu, vport_netdev_free);
  147. }
  148. void ovs_netdev_tunnel_destroy(struct vport *vport)
  149. {
  150. rtnl_lock();
  151. if (vport->dev->priv_flags & IFF_OVS_DATAPATH)
  152. ovs_netdev_detach_dev(vport);
  153. /* We can be invoked by both explicit vport deletion and
  154. * underlying netdev deregistration; delete the link only
  155. * if it's not already shutting down.
  156. */
  157. if (vport->dev->reg_state == NETREG_REGISTERED)
  158. rtnl_delete_link(vport->dev);
  159. dev_put(vport->dev);
  160. vport->dev = NULL;
  161. rtnl_unlock();
  162. call_rcu(&vport->rcu, vport_netdev_free);
  163. }
  164. EXPORT_SYMBOL_GPL(ovs_netdev_tunnel_destroy);
  165. /* Returns null if this device is not attached to a datapath. */
  166. struct vport *ovs_netdev_get_vport(struct net_device *dev)
  167. {
  168. if (likely(dev->priv_flags & IFF_OVS_DATAPATH))
  169. return (struct vport *)
  170. rcu_dereference_rtnl(dev->rx_handler_data);
  171. else
  172. return NULL;
  173. }
  174. static struct vport_ops ovs_netdev_vport_ops = {
  175. .type = OVS_VPORT_TYPE_NETDEV,
  176. .create = netdev_create,
  177. .destroy = netdev_destroy,
  178. .send = dev_queue_xmit,
  179. };
  180. int __init ovs_netdev_init(void)
  181. {
  182. return ovs_vport_ops_register(&ovs_netdev_vport_ops);
  183. }
  184. void ovs_netdev_exit(void)
  185. {
  186. ovs_vport_ops_unregister(&ovs_netdev_vport_ops);
  187. }