vport-internal_dev.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #include <linux/hardirq.h>
  19. #include <linux/if_vlan.h>
  20. #include <linux/kernel.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/ethtool.h>
  24. #include <linux/skbuff.h>
  25. #include <net/dst.h>
  26. #include <net/xfrm.h>
  27. #include <net/rtnetlink.h>
  28. #include "datapath.h"
  29. #include "vport-internal_dev.h"
  30. #include "vport-netdev.h"
  31. struct internal_dev {
  32. struct vport *vport;
  33. };
  34. static struct vport_ops ovs_internal_vport_ops;
  35. static struct internal_dev *internal_dev_priv(struct net_device *netdev)
  36. {
  37. return netdev_priv(netdev);
  38. }
  39. /* This function is only called by the kernel network layer.*/
  40. static struct rtnl_link_stats64 *internal_dev_get_stats(struct net_device *netdev,
  41. struct rtnl_link_stats64 *stats)
  42. {
  43. struct vport *vport = ovs_internal_dev_get_vport(netdev);
  44. struct ovs_vport_stats vport_stats;
  45. ovs_vport_get_stats(vport, &vport_stats);
  46. /* The tx and rx stats need to be swapped because the
  47. * switch and host OS have opposite perspectives. */
  48. stats->rx_packets = vport_stats.tx_packets;
  49. stats->tx_packets = vport_stats.rx_packets;
  50. stats->rx_bytes = vport_stats.tx_bytes;
  51. stats->tx_bytes = vport_stats.rx_bytes;
  52. stats->rx_errors = vport_stats.tx_errors;
  53. stats->tx_errors = vport_stats.rx_errors;
  54. stats->rx_dropped = vport_stats.tx_dropped;
  55. stats->tx_dropped = vport_stats.rx_dropped;
  56. return stats;
  57. }
  58. /* Called with rcu_read_lock_bh. */
  59. static int internal_dev_xmit(struct sk_buff *skb, struct net_device *netdev)
  60. {
  61. rcu_read_lock();
  62. ovs_vport_receive(internal_dev_priv(netdev)->vport, skb, NULL);
  63. rcu_read_unlock();
  64. return 0;
  65. }
  66. static int internal_dev_open(struct net_device *netdev)
  67. {
  68. netif_start_queue(netdev);
  69. return 0;
  70. }
  71. static int internal_dev_stop(struct net_device *netdev)
  72. {
  73. netif_stop_queue(netdev);
  74. return 0;
  75. }
  76. static void internal_dev_getinfo(struct net_device *netdev,
  77. struct ethtool_drvinfo *info)
  78. {
  79. strlcpy(info->driver, "openvswitch", sizeof(info->driver));
  80. }
  81. static const struct ethtool_ops internal_dev_ethtool_ops = {
  82. .get_drvinfo = internal_dev_getinfo,
  83. .get_link = ethtool_op_get_link,
  84. };
  85. static int internal_dev_change_mtu(struct net_device *netdev, int new_mtu)
  86. {
  87. if (new_mtu < 68)
  88. return -EINVAL;
  89. netdev->mtu = new_mtu;
  90. return 0;
  91. }
  92. static void internal_dev_destructor(struct net_device *dev)
  93. {
  94. struct vport *vport = ovs_internal_dev_get_vport(dev);
  95. ovs_vport_free(vport);
  96. free_netdev(dev);
  97. }
  98. static const struct net_device_ops internal_dev_netdev_ops = {
  99. .ndo_open = internal_dev_open,
  100. .ndo_stop = internal_dev_stop,
  101. .ndo_start_xmit = internal_dev_xmit,
  102. .ndo_set_mac_address = eth_mac_addr,
  103. .ndo_change_mtu = internal_dev_change_mtu,
  104. .ndo_get_stats64 = internal_dev_get_stats,
  105. };
  106. static struct rtnl_link_ops internal_dev_link_ops __read_mostly = {
  107. .kind = "openvswitch",
  108. };
  109. static void do_setup(struct net_device *netdev)
  110. {
  111. ether_setup(netdev);
  112. netdev->netdev_ops = &internal_dev_netdev_ops;
  113. netdev->priv_flags &= ~IFF_TX_SKB_SHARING;
  114. netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  115. netdev->destructor = internal_dev_destructor;
  116. netdev->ethtool_ops = &internal_dev_ethtool_ops;
  117. netdev->rtnl_link_ops = &internal_dev_link_ops;
  118. netdev->tx_queue_len = 0;
  119. netdev->features = NETIF_F_LLTX | NETIF_F_SG | NETIF_F_FRAGLIST |
  120. NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
  121. NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL;
  122. netdev->vlan_features = netdev->features;
  123. netdev->hw_enc_features = netdev->features;
  124. netdev->features |= NETIF_F_HW_VLAN_CTAG_TX;
  125. netdev->hw_features = netdev->features & ~NETIF_F_LLTX;
  126. eth_hw_addr_random(netdev);
  127. }
  128. static struct vport *internal_dev_create(const struct vport_parms *parms)
  129. {
  130. struct vport *vport;
  131. struct netdev_vport *netdev_vport;
  132. struct internal_dev *internal_dev;
  133. int err;
  134. vport = ovs_vport_alloc(sizeof(struct netdev_vport),
  135. &ovs_internal_vport_ops, parms);
  136. if (IS_ERR(vport)) {
  137. err = PTR_ERR(vport);
  138. goto error;
  139. }
  140. netdev_vport = netdev_vport_priv(vport);
  141. netdev_vport->dev = alloc_netdev(sizeof(struct internal_dev),
  142. parms->name, NET_NAME_UNKNOWN,
  143. do_setup);
  144. if (!netdev_vport->dev) {
  145. err = -ENOMEM;
  146. goto error_free_vport;
  147. }
  148. dev_net_set(netdev_vport->dev, ovs_dp_get_net(vport->dp));
  149. internal_dev = internal_dev_priv(netdev_vport->dev);
  150. internal_dev->vport = vport;
  151. /* Restrict bridge port to current netns. */
  152. if (vport->port_no == OVSP_LOCAL)
  153. netdev_vport->dev->features |= NETIF_F_NETNS_LOCAL;
  154. rtnl_lock();
  155. err = register_netdevice(netdev_vport->dev);
  156. if (err)
  157. goto error_free_netdev;
  158. dev_set_promiscuity(netdev_vport->dev, 1);
  159. rtnl_unlock();
  160. netif_start_queue(netdev_vport->dev);
  161. return vport;
  162. error_free_netdev:
  163. rtnl_unlock();
  164. free_netdev(netdev_vport->dev);
  165. error_free_vport:
  166. ovs_vport_free(vport);
  167. error:
  168. return ERR_PTR(err);
  169. }
  170. static void internal_dev_destroy(struct vport *vport)
  171. {
  172. struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
  173. netif_stop_queue(netdev_vport->dev);
  174. rtnl_lock();
  175. dev_set_promiscuity(netdev_vport->dev, -1);
  176. /* unregister_netdevice() waits for an RCU grace period. */
  177. unregister_netdevice(netdev_vport->dev);
  178. rtnl_unlock();
  179. }
  180. static int internal_dev_recv(struct vport *vport, struct sk_buff *skb)
  181. {
  182. struct net_device *netdev = netdev_vport_priv(vport)->dev;
  183. int len;
  184. if (unlikely(!(netdev->flags & IFF_UP))) {
  185. kfree_skb(skb);
  186. return 0;
  187. }
  188. len = skb->len;
  189. skb_dst_drop(skb);
  190. nf_reset(skb);
  191. secpath_reset(skb);
  192. skb->dev = netdev;
  193. skb->pkt_type = PACKET_HOST;
  194. skb->protocol = eth_type_trans(skb, netdev);
  195. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  196. netif_rx(skb);
  197. return len;
  198. }
  199. static struct vport_ops ovs_internal_vport_ops = {
  200. .type = OVS_VPORT_TYPE_INTERNAL,
  201. .create = internal_dev_create,
  202. .destroy = internal_dev_destroy,
  203. .get_name = ovs_netdev_get_name,
  204. .send = internal_dev_recv,
  205. };
  206. int ovs_is_internal_dev(const struct net_device *netdev)
  207. {
  208. return netdev->netdev_ops == &internal_dev_netdev_ops;
  209. }
  210. struct vport *ovs_internal_dev_get_vport(struct net_device *netdev)
  211. {
  212. if (!ovs_is_internal_dev(netdev))
  213. return NULL;
  214. return internal_dev_priv(netdev)->vport;
  215. }
  216. int ovs_internal_dev_rtnl_link_register(void)
  217. {
  218. int err;
  219. err = rtnl_link_register(&internal_dev_link_ops);
  220. if (err < 0)
  221. return err;
  222. err = ovs_vport_ops_register(&ovs_internal_vport_ops);
  223. if (err < 0)
  224. rtnl_link_unregister(&internal_dev_link_ops);
  225. return err;
  226. }
  227. void ovs_internal_dev_rtnl_link_unregister(void)
  228. {
  229. ovs_vport_ops_unregister(&ovs_internal_vport_ops);
  230. rtnl_link_unregister(&internal_dev_link_ops);
  231. }