vport-netdev.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2007-2011 Nicira Networks.
  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. kfree_skb(skb);
  35. return;
  36. }
  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. * (No one comes after us, since we tell handle_bridge() that we took
  40. * the packet.) */
  41. skb = skb_share_check(skb, GFP_ATOMIC);
  42. if (unlikely(!skb))
  43. return;
  44. skb_push(skb, ETH_HLEN);
  45. ovs_vport_receive(vport, skb);
  46. }
  47. /* Called with rcu_read_lock and bottom-halves disabled. */
  48. static rx_handler_result_t netdev_frame_hook(struct sk_buff **pskb)
  49. {
  50. struct sk_buff *skb = *pskb;
  51. struct vport *vport;
  52. if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
  53. return RX_HANDLER_PASS;
  54. vport = ovs_netdev_get_vport(skb->dev);
  55. netdev_port_receive(vport, skb);
  56. return RX_HANDLER_CONSUMED;
  57. }
  58. static struct vport *netdev_create(const struct vport_parms *parms)
  59. {
  60. struct vport *vport;
  61. struct netdev_vport *netdev_vport;
  62. int err;
  63. vport = ovs_vport_alloc(sizeof(struct netdev_vport),
  64. &ovs_netdev_vport_ops, parms);
  65. if (IS_ERR(vport)) {
  66. err = PTR_ERR(vport);
  67. goto error;
  68. }
  69. netdev_vport = netdev_vport_priv(vport);
  70. netdev_vport->dev = dev_get_by_name(&init_net, parms->name);
  71. if (!netdev_vport->dev) {
  72. err = -ENODEV;
  73. goto error_free_vport;
  74. }
  75. if (netdev_vport->dev->flags & IFF_LOOPBACK ||
  76. netdev_vport->dev->type != ARPHRD_ETHER ||
  77. ovs_is_internal_dev(netdev_vport->dev)) {
  78. err = -EINVAL;
  79. goto error_put;
  80. }
  81. err = netdev_rx_handler_register(netdev_vport->dev, netdev_frame_hook,
  82. vport);
  83. if (err)
  84. goto error_put;
  85. dev_set_promiscuity(netdev_vport->dev, 1);
  86. netdev_vport->dev->priv_flags |= IFF_OVS_DATAPATH;
  87. return vport;
  88. error_put:
  89. dev_put(netdev_vport->dev);
  90. error_free_vport:
  91. ovs_vport_free(vport);
  92. error:
  93. return ERR_PTR(err);
  94. }
  95. static void netdev_destroy(struct vport *vport)
  96. {
  97. struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
  98. netdev_vport->dev->priv_flags &= ~IFF_OVS_DATAPATH;
  99. netdev_rx_handler_unregister(netdev_vport->dev);
  100. dev_set_promiscuity(netdev_vport->dev, -1);
  101. synchronize_rcu();
  102. dev_put(netdev_vport->dev);
  103. ovs_vport_free(vport);
  104. }
  105. const char *ovs_netdev_get_name(const struct vport *vport)
  106. {
  107. const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
  108. return netdev_vport->dev->name;
  109. }
  110. int ovs_netdev_get_ifindex(const struct vport *vport)
  111. {
  112. const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
  113. return netdev_vport->dev->ifindex;
  114. }
  115. static unsigned int packet_length(const struct sk_buff *skb)
  116. {
  117. unsigned int length = skb->len - ETH_HLEN;
  118. if (skb->protocol == htons(ETH_P_8021Q))
  119. length -= VLAN_HLEN;
  120. return length;
  121. }
  122. static int netdev_send(struct vport *vport, struct sk_buff *skb)
  123. {
  124. struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
  125. int mtu = netdev_vport->dev->mtu;
  126. int len;
  127. if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) {
  128. net_warn_ratelimited("%s: dropped over-mtu packet: %d > %d\n",
  129. ovs_dp_name(vport->dp),
  130. packet_length(skb), mtu);
  131. goto error;
  132. }
  133. if (unlikely(skb_warn_if_lro(skb)))
  134. goto error;
  135. skb->dev = netdev_vport->dev;
  136. len = skb->len;
  137. dev_queue_xmit(skb);
  138. return len;
  139. error:
  140. kfree_skb(skb);
  141. ovs_vport_record_error(vport, VPORT_E_TX_DROPPED);
  142. return 0;
  143. }
  144. /* Returns null if this device is not attached to a datapath. */
  145. struct vport *ovs_netdev_get_vport(struct net_device *dev)
  146. {
  147. if (likely(dev->priv_flags & IFF_OVS_DATAPATH))
  148. return (struct vport *)
  149. rcu_dereference_rtnl(dev->rx_handler_data);
  150. else
  151. return NULL;
  152. }
  153. const struct vport_ops ovs_netdev_vport_ops = {
  154. .type = OVS_VPORT_TYPE_NETDEV,
  155. .create = netdev_create,
  156. .destroy = netdev_destroy,
  157. .get_name = ovs_netdev_get_name,
  158. .get_ifindex = ovs_netdev_get_ifindex,
  159. .send = netdev_send,
  160. };