vport-internal_dev.c 7.7 KB

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