vport-internal_dev.c 7.6 KB

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