vport-netdev.c 4.8 KB

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