vport-internal_dev.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/if_vlan.h>
  19. #include <linux/kernel.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/ethtool.h>
  23. #include <linux/skbuff.h>
  24. #include <net/dst.h>
  25. #include <net/xfrm.h>
  26. #include <net/rtnetlink.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 vport_ops ovs_internal_vport_ops;
  34. static struct internal_dev *internal_dev_priv(struct net_device *netdev)
  35. {
  36. return netdev_priv(netdev);
  37. }
  38. /* Called with rcu_read_lock_bh. */
  39. static netdev_tx_t
  40. 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 NETDEV_TX_OK;
  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 void internal_dev_destructor(struct net_device *dev)
  78. {
  79. struct vport *vport = ovs_internal_dev_get_vport(dev);
  80. ovs_vport_free(vport);
  81. }
  82. static void
  83. internal_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
  84. {
  85. int i;
  86. memset(stats, 0, sizeof(*stats));
  87. stats->rx_errors = dev->stats.rx_errors;
  88. stats->tx_errors = dev->stats.tx_errors;
  89. stats->tx_dropped = dev->stats.tx_dropped;
  90. stats->rx_dropped = dev->stats.rx_dropped;
  91. for_each_possible_cpu(i) {
  92. const struct pcpu_sw_netstats *percpu_stats;
  93. struct pcpu_sw_netstats local_stats;
  94. unsigned int start;
  95. percpu_stats = per_cpu_ptr(dev->tstats, i);
  96. do {
  97. start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
  98. local_stats = *percpu_stats;
  99. } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
  100. stats->rx_bytes += local_stats.rx_bytes;
  101. stats->rx_packets += local_stats.rx_packets;
  102. stats->tx_bytes += local_stats.tx_bytes;
  103. stats->tx_packets += local_stats.tx_packets;
  104. }
  105. }
  106. static const struct net_device_ops internal_dev_netdev_ops = {
  107. .ndo_open = internal_dev_open,
  108. .ndo_stop = internal_dev_stop,
  109. .ndo_start_xmit = internal_dev_xmit,
  110. .ndo_set_mac_address = eth_mac_addr,
  111. .ndo_get_stats64 = internal_get_stats,
  112. };
  113. static struct rtnl_link_ops internal_dev_link_ops __read_mostly = {
  114. .kind = "openvswitch",
  115. };
  116. static void do_setup(struct net_device *netdev)
  117. {
  118. ether_setup(netdev);
  119. netdev->max_mtu = ETH_MAX_MTU;
  120. netdev->netdev_ops = &internal_dev_netdev_ops;
  121. netdev->priv_flags &= ~IFF_TX_SKB_SHARING;
  122. netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_OPENVSWITCH |
  123. IFF_NO_QUEUE;
  124. netdev->needs_free_netdev = true;
  125. netdev->priv_destructor = NULL;
  126. netdev->ethtool_ops = &internal_dev_ethtool_ops;
  127. netdev->rtnl_link_ops = &internal_dev_link_ops;
  128. netdev->features = NETIF_F_LLTX | NETIF_F_SG | NETIF_F_FRAGLIST |
  129. NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
  130. NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL;
  131. netdev->vlan_features = netdev->features;
  132. netdev->hw_enc_features = netdev->features;
  133. netdev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
  134. netdev->hw_features = netdev->features & ~NETIF_F_LLTX;
  135. eth_hw_addr_random(netdev);
  136. }
  137. static struct vport *internal_dev_create(const struct vport_parms *parms)
  138. {
  139. struct vport *vport;
  140. struct internal_dev *internal_dev;
  141. struct net_device *dev;
  142. int err;
  143. vport = ovs_vport_alloc(0, &ovs_internal_vport_ops, parms);
  144. if (IS_ERR(vport)) {
  145. err = PTR_ERR(vport);
  146. goto error;
  147. }
  148. dev = alloc_netdev(sizeof(struct internal_dev),
  149. parms->name, NET_NAME_USER, do_setup);
  150. vport->dev = dev;
  151. if (!vport->dev) {
  152. err = -ENOMEM;
  153. goto error_free_vport;
  154. }
  155. vport->dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  156. if (!vport->dev->tstats) {
  157. err = -ENOMEM;
  158. goto error_free_netdev;
  159. }
  160. dev_net_set(vport->dev, ovs_dp_get_net(vport->dp));
  161. internal_dev = internal_dev_priv(vport->dev);
  162. internal_dev->vport = vport;
  163. /* Restrict bridge port to current netns. */
  164. if (vport->port_no == OVSP_LOCAL)
  165. vport->dev->features |= NETIF_F_NETNS_LOCAL;
  166. rtnl_lock();
  167. err = register_netdevice(vport->dev);
  168. if (err)
  169. goto error_unlock;
  170. vport->dev->priv_destructor = internal_dev_destructor;
  171. dev_set_promiscuity(vport->dev, 1);
  172. rtnl_unlock();
  173. netif_start_queue(vport->dev);
  174. return vport;
  175. error_unlock:
  176. rtnl_unlock();
  177. free_percpu(dev->tstats);
  178. error_free_netdev:
  179. free_netdev(dev);
  180. error_free_vport:
  181. ovs_vport_free(vport);
  182. error:
  183. return ERR_PTR(err);
  184. }
  185. static void internal_dev_destroy(struct vport *vport)
  186. {
  187. netif_stop_queue(vport->dev);
  188. rtnl_lock();
  189. dev_set_promiscuity(vport->dev, -1);
  190. /* unregister_netdevice() waits for an RCU grace period. */
  191. unregister_netdevice(vport->dev);
  192. free_percpu(vport->dev->tstats);
  193. rtnl_unlock();
  194. }
  195. static netdev_tx_t internal_dev_recv(struct sk_buff *skb)
  196. {
  197. struct net_device *netdev = skb->dev;
  198. struct pcpu_sw_netstats *stats;
  199. if (unlikely(!(netdev->flags & IFF_UP))) {
  200. kfree_skb(skb);
  201. netdev->stats.rx_dropped++;
  202. return NETDEV_TX_OK;
  203. }
  204. skb_dst_drop(skb);
  205. nf_reset(skb);
  206. secpath_reset(skb);
  207. skb->pkt_type = PACKET_HOST;
  208. skb->protocol = eth_type_trans(skb, netdev);
  209. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  210. stats = this_cpu_ptr(netdev->tstats);
  211. u64_stats_update_begin(&stats->syncp);
  212. stats->rx_packets++;
  213. stats->rx_bytes += skb->len;
  214. u64_stats_update_end(&stats->syncp);
  215. netif_rx(skb);
  216. return NETDEV_TX_OK;
  217. }
  218. static struct vport_ops ovs_internal_vport_ops = {
  219. .type = OVS_VPORT_TYPE_INTERNAL,
  220. .create = internal_dev_create,
  221. .destroy = internal_dev_destroy,
  222. .send = internal_dev_recv,
  223. };
  224. int ovs_is_internal_dev(const struct net_device *netdev)
  225. {
  226. return netdev->netdev_ops == &internal_dev_netdev_ops;
  227. }
  228. struct vport *ovs_internal_dev_get_vport(struct net_device *netdev)
  229. {
  230. if (!ovs_is_internal_dev(netdev))
  231. return NULL;
  232. return internal_dev_priv(netdev)->vport;
  233. }
  234. int ovs_internal_dev_rtnl_link_register(void)
  235. {
  236. int err;
  237. err = rtnl_link_register(&internal_dev_link_ops);
  238. if (err < 0)
  239. return err;
  240. err = ovs_vport_ops_register(&ovs_internal_vport_ops);
  241. if (err < 0)
  242. rtnl_link_unregister(&internal_dev_link_ops);
  243. return err;
  244. }
  245. void ovs_internal_dev_rtnl_link_unregister(void)
  246. {
  247. ovs_vport_ops_unregister(&ovs_internal_vport_ops);
  248. rtnl_link_unregister(&internal_dev_link_ops);
  249. }