vport-netdev.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_skb_postpush_rcsum(skb, skb->data, ETH_HLEN);
  45. ovs_vport_receive(vport, skb);
  46. return;
  47. error:
  48. kfree_skb(skb);
  49. }
  50. /* Called with rcu_read_lock and bottom-halves disabled. */
  51. static rx_handler_result_t netdev_frame_hook(struct sk_buff **pskb)
  52. {
  53. struct sk_buff *skb = *pskb;
  54. struct vport *vport;
  55. if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
  56. return RX_HANDLER_PASS;
  57. vport = ovs_netdev_get_vport(skb->dev);
  58. netdev_port_receive(vport, skb);
  59. return RX_HANDLER_CONSUMED;
  60. }
  61. static struct vport *netdev_create(const struct vport_parms *parms)
  62. {
  63. struct vport *vport;
  64. struct netdev_vport *netdev_vport;
  65. int err;
  66. vport = ovs_vport_alloc(sizeof(struct netdev_vport),
  67. &ovs_netdev_vport_ops, parms);
  68. if (IS_ERR(vport)) {
  69. err = PTR_ERR(vport);
  70. goto error;
  71. }
  72. netdev_vport = netdev_vport_priv(vport);
  73. netdev_vport->dev = dev_get_by_name(ovs_dp_get_net(vport->dp), parms->name);
  74. if (!netdev_vport->dev) {
  75. err = -ENODEV;
  76. goto error_free_vport;
  77. }
  78. if (netdev_vport->dev->flags & IFF_LOOPBACK ||
  79. netdev_vport->dev->type != ARPHRD_ETHER ||
  80. ovs_is_internal_dev(netdev_vport->dev)) {
  81. err = -EINVAL;
  82. goto error_put;
  83. }
  84. rtnl_lock();
  85. err = netdev_rx_handler_register(netdev_vport->dev, netdev_frame_hook,
  86. vport);
  87. if (err)
  88. goto error_unlock;
  89. dev_set_promiscuity(netdev_vport->dev, 1);
  90. netdev_vport->dev->priv_flags |= IFF_OVS_DATAPATH;
  91. rtnl_unlock();
  92. return vport;
  93. error_unlock:
  94. rtnl_unlock();
  95. error_put:
  96. dev_put(netdev_vport->dev);
  97. error_free_vport:
  98. ovs_vport_free(vport);
  99. error:
  100. return ERR_PTR(err);
  101. }
  102. static void free_port_rcu(struct rcu_head *rcu)
  103. {
  104. struct netdev_vport *netdev_vport = container_of(rcu,
  105. struct netdev_vport, rcu);
  106. dev_put(netdev_vport->dev);
  107. ovs_vport_free(vport_from_priv(netdev_vport));
  108. }
  109. static void netdev_destroy(struct vport *vport)
  110. {
  111. struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
  112. rtnl_lock();
  113. netdev_vport->dev->priv_flags &= ~IFF_OVS_DATAPATH;
  114. netdev_rx_handler_unregister(netdev_vport->dev);
  115. dev_set_promiscuity(netdev_vport->dev, -1);
  116. rtnl_unlock();
  117. call_rcu(&netdev_vport->rcu, free_port_rcu);
  118. }
  119. const char *ovs_netdev_get_name(const struct vport *vport)
  120. {
  121. const struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
  122. return netdev_vport->dev->name;
  123. }
  124. static unsigned int packet_length(const struct sk_buff *skb)
  125. {
  126. unsigned int length = skb->len - ETH_HLEN;
  127. if (skb->protocol == htons(ETH_P_8021Q))
  128. length -= VLAN_HLEN;
  129. return length;
  130. }
  131. static int netdev_send(struct vport *vport, struct sk_buff *skb)
  132. {
  133. struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
  134. int mtu = netdev_vport->dev->mtu;
  135. int len;
  136. if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) {
  137. net_warn_ratelimited("%s: dropped over-mtu packet: %d > %d\n",
  138. netdev_vport->dev->name,
  139. packet_length(skb), mtu);
  140. goto drop;
  141. }
  142. skb->dev = netdev_vport->dev;
  143. len = skb->len;
  144. dev_queue_xmit(skb);
  145. return len;
  146. drop:
  147. kfree_skb(skb);
  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. };