vport-netdev.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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), NULL, NULL);
  93. if (err)
  94. goto error_unlock;
  95. err = netdev_rx_handler_register(vport->dev, netdev_frame_hook,
  96. vport);
  97. if (err)
  98. goto error_master_upper_dev_unlink;
  99. dev_disable_lro(vport->dev);
  100. dev_set_promiscuity(vport->dev, 1);
  101. vport->dev->priv_flags |= IFF_OVS_DATAPATH;
  102. rtnl_unlock();
  103. return vport;
  104. error_master_upper_dev_unlink:
  105. netdev_upper_dev_unlink(vport->dev, get_dpdev(vport->dp));
  106. error_unlock:
  107. rtnl_unlock();
  108. error_put:
  109. dev_put(vport->dev);
  110. error_free_vport:
  111. ovs_vport_free(vport);
  112. return ERR_PTR(err);
  113. }
  114. EXPORT_SYMBOL_GPL(ovs_netdev_link);
  115. static struct vport *netdev_create(const struct vport_parms *parms)
  116. {
  117. struct vport *vport;
  118. vport = ovs_vport_alloc(0, &ovs_netdev_vport_ops, parms);
  119. if (IS_ERR(vport))
  120. return vport;
  121. return ovs_netdev_link(vport, parms->name);
  122. }
  123. static void vport_netdev_free(struct rcu_head *rcu)
  124. {
  125. struct vport *vport = container_of(rcu, struct vport, rcu);
  126. if (vport->dev)
  127. dev_put(vport->dev);
  128. ovs_vport_free(vport);
  129. }
  130. void ovs_netdev_detach_dev(struct vport *vport)
  131. {
  132. ASSERT_RTNL();
  133. vport->dev->priv_flags &= ~IFF_OVS_DATAPATH;
  134. netdev_rx_handler_unregister(vport->dev);
  135. netdev_upper_dev_unlink(vport->dev,
  136. netdev_master_upper_dev_get(vport->dev));
  137. dev_set_promiscuity(vport->dev, -1);
  138. }
  139. static void netdev_destroy(struct vport *vport)
  140. {
  141. rtnl_lock();
  142. if (vport->dev->priv_flags & IFF_OVS_DATAPATH)
  143. ovs_netdev_detach_dev(vport);
  144. rtnl_unlock();
  145. call_rcu(&vport->rcu, vport_netdev_free);
  146. }
  147. void ovs_netdev_tunnel_destroy(struct vport *vport)
  148. {
  149. rtnl_lock();
  150. if (vport->dev->priv_flags & IFF_OVS_DATAPATH)
  151. ovs_netdev_detach_dev(vport);
  152. /* We can be invoked by both explicit vport deletion and
  153. * underlying netdev deregistration; delete the link only
  154. * if it's not already shutting down.
  155. */
  156. if (vport->dev->reg_state == NETREG_REGISTERED)
  157. rtnl_delete_link(vport->dev);
  158. dev_put(vport->dev);
  159. vport->dev = NULL;
  160. rtnl_unlock();
  161. call_rcu(&vport->rcu, vport_netdev_free);
  162. }
  163. EXPORT_SYMBOL_GPL(ovs_netdev_tunnel_destroy);
  164. /* Returns null if this device is not attached to a datapath. */
  165. struct vport *ovs_netdev_get_vport(struct net_device *dev)
  166. {
  167. if (likely(dev->priv_flags & IFF_OVS_DATAPATH))
  168. return (struct vport *)
  169. rcu_dereference_rtnl(dev->rx_handler_data);
  170. else
  171. return NULL;
  172. }
  173. static struct vport_ops ovs_netdev_vport_ops = {
  174. .type = OVS_VPORT_TYPE_NETDEV,
  175. .create = netdev_create,
  176. .destroy = netdev_destroy,
  177. .send = dev_queue_xmit,
  178. };
  179. int __init ovs_netdev_init(void)
  180. {
  181. return ovs_vport_ops_register(&ovs_netdev_vport_ops);
  182. }
  183. void ovs_netdev_exit(void)
  184. {
  185. ovs_vport_ops_unregister(&ovs_netdev_vport_ops);
  186. }