vport-internal_dev.c 7.8 KB

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