vport-netdev.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2007-2012 Nicira, Inc.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/if_arp.h>
  7. #include <linux/if_bridge.h>
  8. #include <linux/if_vlan.h>
  9. #include <linux/kernel.h>
  10. #include <linux/llc.h>
  11. #include <linux/rtnetlink.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/openvswitch.h>
  14. #include <linux/export.h>
  15. #include <net/ip_tunnels.h>
  16. #include <net/rtnetlink.h>
  17. #include "datapath.h"
  18. #include "vport.h"
  19. #include "vport-internal_dev.h"
  20. #include "vport-netdev.h"
  21. static struct vport_ops ovs_netdev_vport_ops;
  22. /* Must be called with rcu_read_lock. */
  23. static void netdev_port_receive(struct sk_buff *skb)
  24. {
  25. struct vport *vport;
  26. vport = ovs_netdev_get_vport(skb->dev);
  27. if (unlikely(!vport))
  28. goto error;
  29. if (unlikely(skb_warn_if_lro(skb)))
  30. goto error;
  31. /* Make our own copy of the packet. Otherwise we will mangle the
  32. * packet for anyone who came before us (e.g. tcpdump via AF_PACKET).
  33. */
  34. skb = skb_share_check(skb, GFP_ATOMIC);
  35. if (unlikely(!skb))
  36. return;
  37. if (skb->dev->type == ARPHRD_ETHER)
  38. skb_push_rcsum(skb, ETH_HLEN);
  39. ovs_vport_receive(vport, skb, skb_tunnel_info(skb));
  40. return;
  41. error:
  42. kfree_skb(skb);
  43. }
  44. /* Called with rcu_read_lock and bottom-halves disabled. */
  45. static rx_handler_result_t netdev_frame_hook(struct sk_buff **pskb)
  46. {
  47. struct sk_buff *skb = *pskb;
  48. if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
  49. return RX_HANDLER_PASS;
  50. netdev_port_receive(skb);
  51. return RX_HANDLER_CONSUMED;
  52. }
  53. static struct net_device *get_dpdev(const struct datapath *dp)
  54. {
  55. struct vport *local;
  56. local = ovs_vport_ovsl(dp, OVSP_LOCAL);
  57. return local->dev;
  58. }
  59. struct vport *ovs_netdev_link(struct vport *vport, const char *name)
  60. {
  61. int err;
  62. vport->dev = dev_get_by_name(ovs_dp_get_net(vport->dp), name);
  63. if (!vport->dev) {
  64. err = -ENODEV;
  65. goto error_free_vport;
  66. }
  67. /* Ensure that the device exists and that the provided
  68. * name is not one of its aliases.
  69. */
  70. if (strcmp(name, ovs_vport_name(vport))) {
  71. err = -ENODEV;
  72. goto error_put;
  73. }
  74. netdev_tracker_alloc(vport->dev, &vport->dev_tracker, GFP_KERNEL);
  75. if (vport->dev->flags & IFF_LOOPBACK ||
  76. (vport->dev->type != ARPHRD_ETHER &&
  77. vport->dev->type != ARPHRD_NONE) ||
  78. ovs_is_internal_dev(vport->dev)) {
  79. err = -EINVAL;
  80. goto error_put;
  81. }
  82. rtnl_lock();
  83. err = netdev_master_upper_dev_link(vport->dev,
  84. get_dpdev(vport->dp),
  85. NULL, NULL, NULL);
  86. if (err)
  87. goto error_unlock;
  88. err = netdev_rx_handler_register(vport->dev, netdev_frame_hook,
  89. vport);
  90. if (err)
  91. goto error_master_upper_dev_unlink;
  92. dev_disable_lro(vport->dev);
  93. dev_set_promiscuity(vport->dev, 1);
  94. vport->dev->priv_flags |= IFF_OVS_DATAPATH;
  95. rtnl_unlock();
  96. return vport;
  97. error_master_upper_dev_unlink:
  98. netdev_upper_dev_unlink(vport->dev, get_dpdev(vport->dp));
  99. error_unlock:
  100. rtnl_unlock();
  101. error_put:
  102. netdev_put(vport->dev, &vport->dev_tracker);
  103. error_free_vport:
  104. ovs_vport_free(vport);
  105. return ERR_PTR(err);
  106. }
  107. EXPORT_SYMBOL_GPL(ovs_netdev_link);
  108. static struct vport *netdev_create(const struct vport_parms *parms)
  109. {
  110. struct vport *vport;
  111. vport = ovs_vport_alloc(0, &ovs_netdev_vport_ops, parms);
  112. if (IS_ERR(vport))
  113. return vport;
  114. return ovs_netdev_link(vport, parms->name);
  115. }
  116. static void vport_netdev_free(struct rcu_head *rcu)
  117. {
  118. struct vport *vport = container_of(rcu, struct vport, rcu);
  119. netdev_put(vport->dev, &vport->dev_tracker);
  120. ovs_vport_free(vport);
  121. }
  122. void ovs_netdev_detach_dev(struct vport *vport)
  123. {
  124. ASSERT_RTNL();
  125. netdev_rx_handler_unregister(vport->dev);
  126. netdev_upper_dev_unlink(vport->dev,
  127. netdev_master_upper_dev_get(vport->dev));
  128. dev_set_promiscuity(vport->dev, -1);
  129. /* paired with smp_mb() in netdev_destroy() */
  130. smp_wmb();
  131. vport->dev->priv_flags &= ~IFF_OVS_DATAPATH;
  132. }
  133. static void netdev_destroy(struct vport *vport)
  134. {
  135. /* When called from ovs_db_notify_wq() after a dp_device_event(), the
  136. * port has already been detached, so we can avoid taking the RTNL by
  137. * checking this first.
  138. */
  139. if (netif_is_ovs_port(vport->dev)) {
  140. rtnl_lock();
  141. /* Check again while holding the lock to ensure we don't race
  142. * with the netdev notifier and detach twice.
  143. */
  144. if (netif_is_ovs_port(vport->dev))
  145. ovs_netdev_detach_dev(vport);
  146. rtnl_unlock();
  147. }
  148. /* paired with smp_wmb() in ovs_netdev_detach_dev() */
  149. smp_mb();
  150. call_rcu(&vport->rcu, vport_netdev_free);
  151. }
  152. void ovs_netdev_tunnel_destroy(struct vport *vport)
  153. {
  154. rtnl_lock();
  155. if (netif_is_ovs_port(vport->dev))
  156. ovs_netdev_detach_dev(vport);
  157. /* We can be invoked by both explicit vport deletion and
  158. * underlying netdev deregistration; delete the link only
  159. * if it's not already shutting down.
  160. */
  161. if (vport->dev->reg_state == NETREG_REGISTERED)
  162. rtnl_delete_link(vport->dev, 0, NULL);
  163. rtnl_unlock();
  164. call_rcu(&vport->rcu, vport_netdev_free);
  165. }
  166. EXPORT_SYMBOL_GPL(ovs_netdev_tunnel_destroy);
  167. /* Returns null if this device is not attached to a datapath. */
  168. struct vport *ovs_netdev_get_vport(struct net_device *dev)
  169. {
  170. if (likely(netif_is_ovs_port(dev)))
  171. return (struct vport *)
  172. rcu_dereference_rtnl(dev->rx_handler_data);
  173. else
  174. return NULL;
  175. }
  176. static struct vport_ops ovs_netdev_vport_ops = {
  177. .type = OVS_VPORT_TYPE_NETDEV,
  178. .create = netdev_create,
  179. .destroy = netdev_destroy,
  180. .send = dev_queue_xmit,
  181. };
  182. int __init ovs_netdev_init(void)
  183. {
  184. return ovs_vport_ops_register(&ovs_netdev_vport_ops);
  185. }
  186. void ovs_netdev_exit(void)
  187. {
  188. ovs_vport_ops_unregister(&ovs_netdev_vport_ops);
  189. }