vport-internal_dev.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. /* Called with rcu_read_lock_bh. */
  40. static int internal_dev_xmit(struct sk_buff *skb, struct net_device *netdev)
  41. {
  42. int len, err;
  43. len = skb->len;
  44. rcu_read_lock();
  45. err = ovs_vport_receive(internal_dev_priv(netdev)->vport, skb, NULL);
  46. rcu_read_unlock();
  47. if (likely(!err)) {
  48. struct pcpu_sw_netstats *tstats = this_cpu_ptr(netdev->tstats);
  49. u64_stats_update_begin(&tstats->syncp);
  50. tstats->tx_bytes += len;
  51. tstats->tx_packets++;
  52. u64_stats_update_end(&tstats->syncp);
  53. } else {
  54. netdev->stats.tx_errors++;
  55. }
  56. return 0;
  57. }
  58. static int internal_dev_open(struct net_device *netdev)
  59. {
  60. netif_start_queue(netdev);
  61. return 0;
  62. }
  63. static int internal_dev_stop(struct net_device *netdev)
  64. {
  65. netif_stop_queue(netdev);
  66. return 0;
  67. }
  68. static void internal_dev_getinfo(struct net_device *netdev,
  69. struct ethtool_drvinfo *info)
  70. {
  71. strlcpy(info->driver, "openvswitch", sizeof(info->driver));
  72. }
  73. static const struct ethtool_ops internal_dev_ethtool_ops = {
  74. .get_drvinfo = internal_dev_getinfo,
  75. .get_link = ethtool_op_get_link,
  76. };
  77. static int internal_dev_change_mtu(struct net_device *netdev, int new_mtu)
  78. {
  79. if (new_mtu < 68)
  80. return -EINVAL;
  81. netdev->mtu = new_mtu;
  82. return 0;
  83. }
  84. static void internal_dev_destructor(struct net_device *dev)
  85. {
  86. struct vport *vport = ovs_internal_dev_get_vport(dev);
  87. ovs_vport_free(vport);
  88. free_netdev(dev);
  89. }
  90. static struct rtnl_link_stats64 *
  91. internal_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
  92. {
  93. int i;
  94. memset(stats, 0, sizeof(*stats));
  95. stats->rx_errors = dev->stats.rx_errors;
  96. stats->tx_errors = dev->stats.tx_errors;
  97. stats->tx_dropped = dev->stats.tx_dropped;
  98. stats->rx_dropped = dev->stats.rx_dropped;
  99. for_each_possible_cpu(i) {
  100. const struct pcpu_sw_netstats *percpu_stats;
  101. struct pcpu_sw_netstats local_stats;
  102. unsigned int start;
  103. percpu_stats = per_cpu_ptr(dev->tstats, i);
  104. do {
  105. start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
  106. local_stats = *percpu_stats;
  107. } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
  108. stats->rx_bytes += local_stats.rx_bytes;
  109. stats->rx_packets += local_stats.rx_packets;
  110. stats->tx_bytes += local_stats.tx_bytes;
  111. stats->tx_packets += local_stats.tx_packets;
  112. }
  113. return stats;
  114. }
  115. static const struct net_device_ops internal_dev_netdev_ops = {
  116. .ndo_open = internal_dev_open,
  117. .ndo_stop = internal_dev_stop,
  118. .ndo_start_xmit = internal_dev_xmit,
  119. .ndo_set_mac_address = eth_mac_addr,
  120. .ndo_change_mtu = internal_dev_change_mtu,
  121. .ndo_get_stats64 = internal_get_stats,
  122. };
  123. static struct rtnl_link_ops internal_dev_link_ops __read_mostly = {
  124. .kind = "openvswitch",
  125. };
  126. static void do_setup(struct net_device *netdev)
  127. {
  128. ether_setup(netdev);
  129. netdev->netdev_ops = &internal_dev_netdev_ops;
  130. netdev->priv_flags &= ~IFF_TX_SKB_SHARING;
  131. netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_OPENVSWITCH;
  132. netdev->destructor = internal_dev_destructor;
  133. netdev->ethtool_ops = &internal_dev_ethtool_ops;
  134. netdev->rtnl_link_ops = &internal_dev_link_ops;
  135. netdev->tx_queue_len = 0;
  136. netdev->features = NETIF_F_LLTX | NETIF_F_SG | NETIF_F_FRAGLIST |
  137. NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
  138. NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL;
  139. netdev->vlan_features = netdev->features;
  140. netdev->hw_enc_features = netdev->features;
  141. netdev->features |= NETIF_F_HW_VLAN_CTAG_TX;
  142. netdev->hw_features = netdev->features & ~NETIF_F_LLTX;
  143. eth_hw_addr_random(netdev);
  144. }
  145. static struct vport *internal_dev_create(const struct vport_parms *parms)
  146. {
  147. struct vport *vport;
  148. struct internal_dev *internal_dev;
  149. int err;
  150. vport = ovs_vport_alloc(0, &ovs_internal_vport_ops, parms);
  151. if (IS_ERR(vport)) {
  152. err = PTR_ERR(vport);
  153. goto error;
  154. }
  155. vport->dev = alloc_netdev(sizeof(struct internal_dev),
  156. parms->name, NET_NAME_UNKNOWN, do_setup);
  157. if (!vport->dev) {
  158. err = -ENOMEM;
  159. goto error_free_vport;
  160. }
  161. vport->dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  162. if (!vport->dev->tstats) {
  163. err = -ENOMEM;
  164. goto error_free_netdev;
  165. }
  166. dev_net_set(vport->dev, ovs_dp_get_net(vport->dp));
  167. internal_dev = internal_dev_priv(vport->dev);
  168. internal_dev->vport = vport;
  169. /* Restrict bridge port to current netns. */
  170. if (vport->port_no == OVSP_LOCAL)
  171. vport->dev->features |= NETIF_F_NETNS_LOCAL;
  172. rtnl_lock();
  173. err = register_netdevice(vport->dev);
  174. if (err)
  175. goto error_unlock;
  176. dev_set_promiscuity(vport->dev, 1);
  177. rtnl_unlock();
  178. netif_start_queue(vport->dev);
  179. return vport;
  180. error_unlock:
  181. rtnl_unlock();
  182. free_percpu(vport->dev->tstats);
  183. error_free_netdev:
  184. free_netdev(vport->dev);
  185. error_free_vport:
  186. ovs_vport_free(vport);
  187. error:
  188. return ERR_PTR(err);
  189. }
  190. static void internal_dev_destroy(struct vport *vport)
  191. {
  192. netif_stop_queue(vport->dev);
  193. rtnl_lock();
  194. dev_set_promiscuity(vport->dev, -1);
  195. /* unregister_netdevice() waits for an RCU grace period. */
  196. unregister_netdevice(vport->dev);
  197. free_percpu(vport->dev->tstats);
  198. rtnl_unlock();
  199. }
  200. static netdev_tx_t internal_dev_recv(struct sk_buff *skb)
  201. {
  202. struct net_device *netdev = skb->dev;
  203. struct pcpu_sw_netstats *stats;
  204. if (unlikely(!(netdev->flags & IFF_UP))) {
  205. kfree_skb(skb);
  206. netdev->stats.rx_dropped++;
  207. return NETDEV_TX_OK;
  208. }
  209. skb_dst_drop(skb);
  210. nf_reset(skb);
  211. secpath_reset(skb);
  212. skb->pkt_type = PACKET_HOST;
  213. skb->protocol = eth_type_trans(skb, netdev);
  214. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  215. stats = this_cpu_ptr(netdev->tstats);
  216. u64_stats_update_begin(&stats->syncp);
  217. stats->rx_packets++;
  218. stats->rx_bytes += skb->len;
  219. u64_stats_update_end(&stats->syncp);
  220. netif_rx(skb);
  221. return NETDEV_TX_OK;
  222. }
  223. static struct vport_ops ovs_internal_vport_ops = {
  224. .type = OVS_VPORT_TYPE_INTERNAL,
  225. .create = internal_dev_create,
  226. .destroy = internal_dev_destroy,
  227. .send = internal_dev_recv,
  228. };
  229. int ovs_is_internal_dev(const struct net_device *netdev)
  230. {
  231. return netdev->netdev_ops == &internal_dev_netdev_ops;
  232. }
  233. struct vport *ovs_internal_dev_get_vport(struct net_device *netdev)
  234. {
  235. if (!ovs_is_internal_dev(netdev))
  236. return NULL;
  237. return internal_dev_priv(netdev)->vport;
  238. }
  239. int ovs_internal_dev_rtnl_link_register(void)
  240. {
  241. int err;
  242. err = rtnl_link_register(&internal_dev_link_ops);
  243. if (err < 0)
  244. return err;
  245. err = ovs_vport_ops_register(&ovs_internal_vport_ops);
  246. if (err < 0)
  247. rtnl_link_unregister(&internal_dev_link_ops);
  248. return err;
  249. }
  250. void ovs_internal_dev_rtnl_link_unregister(void)
  251. {
  252. ovs_vport_ops_unregister(&ovs_internal_vport_ops);
  253. rtnl_link_unregister(&internal_dev_link_ops);
  254. }