vport-netdev.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 <net/llc.h>
  28. #include "datapath.h"
  29. #include "vport-internal_dev.h"
  30. #include "vport-netdev.h"
  31. static struct vport_ops ovs_netdev_vport_ops;
  32. /* Must be called with rcu_read_lock. */
  33. static void netdev_port_receive(struct vport *vport, struct sk_buff *skb)
  34. {
  35. if (unlikely(!vport))
  36. goto error;
  37. if (unlikely(skb_warn_if_lro(skb)))
  38. goto error;
  39. /* Make our own copy of the packet. Otherwise we will mangle the
  40. * packet for anyone who came before us (e.g. tcpdump via AF_PACKET).
  41. */
  42. skb = skb_share_check(skb, GFP_ATOMIC);
  43. if (unlikely(!skb))
  44. return;
  45. skb_push(skb, ETH_HLEN);
  46. ovs_skb_postpush_rcsum(skb, skb->data, ETH_HLEN);
  47. ovs_vport_receive(vport, skb, NULL);
  48. return;
  49. error:
  50. kfree_skb(skb);
  51. }
  52. /* Called with rcu_read_lock and bottom-halves disabled. */
  53. static rx_handler_result_t netdev_frame_hook(struct sk_buff **pskb)
  54. {
  55. struct sk_buff *skb = *pskb;
  56. struct vport *vport;
  57. if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
  58. return RX_HANDLER_PASS;
  59. vport = ovs_netdev_get_vport(skb->dev);
  60. netdev_port_receive(vport, skb);
  61. return RX_HANDLER_CONSUMED;
  62. }
  63. static struct net_device *get_dpdev(const struct datapath *dp)
  64. {
  65. struct vport *local;
  66. local = ovs_vport_ovsl(dp, OVSP_LOCAL);
  67. BUG_ON(!local);
  68. return local->dev;
  69. }
  70. static struct vport *netdev_link(struct vport *vport, const char *name)
  71. {
  72. int err;
  73. vport->dev = dev_get_by_name(ovs_dp_get_net(vport->dp), name);
  74. if (!vport->dev) {
  75. err = -ENODEV;
  76. goto error_free_vport;
  77. }
  78. if (vport->dev->flags & IFF_LOOPBACK ||
  79. vport->dev->type != ARPHRD_ETHER ||
  80. ovs_is_internal_dev(vport->dev)) {
  81. err = -EINVAL;
  82. goto error_put;
  83. }
  84. rtnl_lock();
  85. err = netdev_master_upper_dev_link(vport->dev,
  86. get_dpdev(vport->dp));
  87. if (err)
  88. goto error_unlock;
  89. err = netdev_rx_handler_register(vport->dev, netdev_frame_hook,
  90. vport);
  91. if (err)
  92. goto error_master_upper_dev_unlink;
  93. dev_disable_lro(vport->dev);
  94. dev_set_promiscuity(vport->dev, 1);
  95. vport->dev->priv_flags |= IFF_OVS_DATAPATH;
  96. rtnl_unlock();
  97. return vport;
  98. error_master_upper_dev_unlink:
  99. netdev_upper_dev_unlink(vport->dev, get_dpdev(vport->dp));
  100. error_unlock:
  101. rtnl_unlock();
  102. error_put:
  103. dev_put(vport->dev);
  104. error_free_vport:
  105. ovs_vport_free(vport);
  106. return ERR_PTR(err);
  107. }
  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 netdev_link(vport, parms->name);
  115. }
  116. static void free_port_rcu(struct rcu_head *rcu)
  117. {
  118. struct vport *vport = container_of(rcu, struct vport, rcu);
  119. dev_put(vport->dev);
  120. ovs_vport_free(vport);
  121. }
  122. void ovs_netdev_detach_dev(struct vport *vport)
  123. {
  124. ASSERT_RTNL();
  125. vport->dev->priv_flags &= ~IFF_OVS_DATAPATH;
  126. netdev_rx_handler_unregister(vport->dev);
  127. netdev_upper_dev_unlink(vport->dev,
  128. netdev_master_upper_dev_get(vport->dev));
  129. dev_set_promiscuity(vport->dev, -1);
  130. }
  131. static void netdev_destroy(struct vport *vport)
  132. {
  133. rtnl_lock();
  134. if (vport->dev->priv_flags & IFF_OVS_DATAPATH)
  135. ovs_netdev_detach_dev(vport);
  136. rtnl_unlock();
  137. call_rcu(&vport->rcu, free_port_rcu);
  138. }
  139. static unsigned int packet_length(const struct sk_buff *skb)
  140. {
  141. unsigned int length = skb->len - ETH_HLEN;
  142. if (skb->protocol == htons(ETH_P_8021Q))
  143. length -= VLAN_HLEN;
  144. return length;
  145. }
  146. static int netdev_send(struct vport *vport, struct sk_buff *skb)
  147. {
  148. int mtu = vport->dev->mtu;
  149. int len;
  150. if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) {
  151. net_warn_ratelimited("%s: dropped over-mtu packet: %d > %d\n",
  152. vport->dev->name,
  153. packet_length(skb), mtu);
  154. goto drop;
  155. }
  156. skb->dev = vport->dev;
  157. len = skb->len;
  158. dev_queue_xmit(skb);
  159. return len;
  160. drop:
  161. kfree_skb(skb);
  162. return 0;
  163. }
  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 = netdev_send,
  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. }