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