vport-internal_dev.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #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 "datapath.h"
  28. #include "vport-internal_dev.h"
  29. #include "vport-netdev.h"
  30. struct internal_dev {
  31. struct vport *vport;
  32. };
  33. static struct internal_dev *internal_dev_priv(struct net_device *netdev)
  34. {
  35. return netdev_priv(netdev);
  36. }
  37. /* This function is only called by the kernel network layer.*/
  38. static struct rtnl_link_stats64 *internal_dev_get_stats(struct net_device *netdev,
  39. struct rtnl_link_stats64 *stats)
  40. {
  41. struct vport *vport = ovs_internal_dev_get_vport(netdev);
  42. struct ovs_vport_stats vport_stats;
  43. ovs_vport_get_stats(vport, &vport_stats);
  44. /* The tx and rx stats need to be swapped because the
  45. * switch and host OS have opposite perspectives. */
  46. stats->rx_packets = vport_stats.tx_packets;
  47. stats->tx_packets = vport_stats.rx_packets;
  48. stats->rx_bytes = vport_stats.tx_bytes;
  49. stats->tx_bytes = vport_stats.rx_bytes;
  50. stats->rx_errors = vport_stats.tx_errors;
  51. stats->tx_errors = vport_stats.rx_errors;
  52. stats->rx_dropped = vport_stats.tx_dropped;
  53. stats->tx_dropped = vport_stats.rx_dropped;
  54. return stats;
  55. }
  56. static int internal_dev_mac_addr(struct net_device *dev, void *p)
  57. {
  58. struct sockaddr *addr = p;
  59. if (!is_valid_ether_addr(addr->sa_data))
  60. return -EADDRNOTAVAIL;
  61. dev->addr_assign_type &= ~NET_ADDR_RANDOM;
  62. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  63. return 0;
  64. }
  65. /* Called with rcu_read_lock_bh. */
  66. static int internal_dev_xmit(struct sk_buff *skb, struct net_device *netdev)
  67. {
  68. rcu_read_lock();
  69. ovs_vport_receive(internal_dev_priv(netdev)->vport, skb);
  70. rcu_read_unlock();
  71. return 0;
  72. }
  73. static int internal_dev_open(struct net_device *netdev)
  74. {
  75. netif_start_queue(netdev);
  76. return 0;
  77. }
  78. static int internal_dev_stop(struct net_device *netdev)
  79. {
  80. netif_stop_queue(netdev);
  81. return 0;
  82. }
  83. static void internal_dev_getinfo(struct net_device *netdev,
  84. struct ethtool_drvinfo *info)
  85. {
  86. strcpy(info->driver, "openvswitch");
  87. }
  88. static const struct ethtool_ops internal_dev_ethtool_ops = {
  89. .get_drvinfo = internal_dev_getinfo,
  90. .get_link = ethtool_op_get_link,
  91. };
  92. static int internal_dev_change_mtu(struct net_device *netdev, int new_mtu)
  93. {
  94. if (new_mtu < 68)
  95. return -EINVAL;
  96. netdev->mtu = new_mtu;
  97. return 0;
  98. }
  99. static void internal_dev_destructor(struct net_device *dev)
  100. {
  101. struct vport *vport = ovs_internal_dev_get_vport(dev);
  102. ovs_vport_free(vport);
  103. free_netdev(dev);
  104. }
  105. static const struct net_device_ops internal_dev_netdev_ops = {
  106. .ndo_open = internal_dev_open,
  107. .ndo_stop = internal_dev_stop,
  108. .ndo_start_xmit = internal_dev_xmit,
  109. .ndo_set_mac_address = internal_dev_mac_addr,
  110. .ndo_change_mtu = internal_dev_change_mtu,
  111. .ndo_get_stats64 = internal_dev_get_stats,
  112. };
  113. static void do_setup(struct net_device *netdev)
  114. {
  115. ether_setup(netdev);
  116. netdev->netdev_ops = &internal_dev_netdev_ops;
  117. netdev->priv_flags &= ~IFF_TX_SKB_SHARING;
  118. netdev->destructor = internal_dev_destructor;
  119. SET_ETHTOOL_OPS(netdev, &internal_dev_ethtool_ops);
  120. netdev->tx_queue_len = 0;
  121. netdev->features = NETIF_F_LLTX | NETIF_F_SG | NETIF_F_FRAGLIST |
  122. NETIF_F_HIGHDMA | NETIF_F_HW_CSUM | NETIF_F_TSO;
  123. netdev->vlan_features = netdev->features;
  124. netdev->features |= NETIF_F_HW_VLAN_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, do_setup);
  143. if (!netdev_vport->dev) {
  144. err = -ENOMEM;
  145. goto error_free_vport;
  146. }
  147. internal_dev = internal_dev_priv(netdev_vport->dev);
  148. internal_dev->vport = vport;
  149. err = register_netdevice(netdev_vport->dev);
  150. if (err)
  151. goto error_free_netdev;
  152. dev_set_promiscuity(netdev_vport->dev, 1);
  153. netif_start_queue(netdev_vport->dev);
  154. return vport;
  155. error_free_netdev:
  156. free_netdev(netdev_vport->dev);
  157. error_free_vport:
  158. ovs_vport_free(vport);
  159. error:
  160. return ERR_PTR(err);
  161. }
  162. static void internal_dev_destroy(struct vport *vport)
  163. {
  164. struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
  165. netif_stop_queue(netdev_vport->dev);
  166. dev_set_promiscuity(netdev_vport->dev, -1);
  167. /* unregister_netdevice() waits for an RCU grace period. */
  168. unregister_netdevice(netdev_vport->dev);
  169. }
  170. static int internal_dev_recv(struct vport *vport, struct sk_buff *skb)
  171. {
  172. struct net_device *netdev = netdev_vport_priv(vport)->dev;
  173. int len;
  174. len = skb->len;
  175. skb_dst_drop(skb);
  176. nf_reset(skb);
  177. secpath_reset(skb);
  178. skb->dev = netdev;
  179. skb->pkt_type = PACKET_HOST;
  180. skb->protocol = eth_type_trans(skb, netdev);
  181. netif_rx(skb);
  182. return len;
  183. }
  184. const struct vport_ops ovs_internal_vport_ops = {
  185. .type = OVS_VPORT_TYPE_INTERNAL,
  186. .create = internal_dev_create,
  187. .destroy = internal_dev_destroy,
  188. .get_name = ovs_netdev_get_name,
  189. .get_ifindex = ovs_netdev_get_ifindex,
  190. .send = internal_dev_recv,
  191. };
  192. int ovs_is_internal_dev(const struct net_device *netdev)
  193. {
  194. return netdev->netdev_ops == &internal_dev_netdev_ops;
  195. }
  196. struct vport *ovs_internal_dev_get_vport(struct net_device *netdev)
  197. {
  198. if (!ovs_is_internal_dev(netdev))
  199. return NULL;
  200. return internal_dev_priv(netdev)->vport;
  201. }