veth.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * drivers/net/veth.c
  3. *
  4. * Copyright (C) 2007 OpenVZ http://openvz.org, SWsoft Inc
  5. *
  6. * Author: Pavel Emelianov <xemul@openvz.org>
  7. * Ethtool interface from: Eric W. Biederman <ebiederm@xmission.com>
  8. *
  9. */
  10. #include <linux/netdevice.h>
  11. #include <linux/slab.h>
  12. #include <linux/ethtool.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/u64_stats_sync.h>
  15. #include <net/rtnetlink.h>
  16. #include <net/dst.h>
  17. #include <net/xfrm.h>
  18. #include <linux/veth.h>
  19. #include <linux/module.h>
  20. #define DRV_NAME "veth"
  21. #define DRV_VERSION "1.0"
  22. struct pcpu_vstats {
  23. u64 packets;
  24. u64 bytes;
  25. struct u64_stats_sync syncp;
  26. };
  27. struct veth_priv {
  28. struct net_device __rcu *peer;
  29. atomic64_t dropped;
  30. unsigned requested_headroom;
  31. };
  32. /*
  33. * ethtool interface
  34. */
  35. static struct {
  36. const char string[ETH_GSTRING_LEN];
  37. } ethtool_stats_keys[] = {
  38. { "peer_ifindex" },
  39. };
  40. static int veth_get_link_ksettings(struct net_device *dev,
  41. struct ethtool_link_ksettings *cmd)
  42. {
  43. cmd->base.speed = SPEED_10000;
  44. cmd->base.duplex = DUPLEX_FULL;
  45. cmd->base.port = PORT_TP;
  46. cmd->base.autoneg = AUTONEG_DISABLE;
  47. return 0;
  48. }
  49. static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  50. {
  51. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  52. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  53. }
  54. static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  55. {
  56. switch(stringset) {
  57. case ETH_SS_STATS:
  58. memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
  59. break;
  60. }
  61. }
  62. static int veth_get_sset_count(struct net_device *dev, int sset)
  63. {
  64. switch (sset) {
  65. case ETH_SS_STATS:
  66. return ARRAY_SIZE(ethtool_stats_keys);
  67. default:
  68. return -EOPNOTSUPP;
  69. }
  70. }
  71. static void veth_get_ethtool_stats(struct net_device *dev,
  72. struct ethtool_stats *stats, u64 *data)
  73. {
  74. struct veth_priv *priv = netdev_priv(dev);
  75. struct net_device *peer = rtnl_dereference(priv->peer);
  76. data[0] = peer ? peer->ifindex : 0;
  77. }
  78. static const struct ethtool_ops veth_ethtool_ops = {
  79. .get_drvinfo = veth_get_drvinfo,
  80. .get_link = ethtool_op_get_link,
  81. .get_strings = veth_get_strings,
  82. .get_sset_count = veth_get_sset_count,
  83. .get_ethtool_stats = veth_get_ethtool_stats,
  84. .get_link_ksettings = veth_get_link_ksettings,
  85. };
  86. static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
  87. {
  88. struct veth_priv *priv = netdev_priv(dev);
  89. struct net_device *rcv;
  90. int length = skb->len;
  91. rcu_read_lock();
  92. rcv = rcu_dereference(priv->peer);
  93. if (unlikely(!rcv)) {
  94. kfree_skb(skb);
  95. goto drop;
  96. }
  97. if (likely(dev_forward_skb(rcv, skb) == NET_RX_SUCCESS)) {
  98. struct pcpu_vstats *stats = this_cpu_ptr(dev->vstats);
  99. u64_stats_update_begin(&stats->syncp);
  100. stats->bytes += length;
  101. stats->packets++;
  102. u64_stats_update_end(&stats->syncp);
  103. } else {
  104. drop:
  105. atomic64_inc(&priv->dropped);
  106. }
  107. rcu_read_unlock();
  108. return NETDEV_TX_OK;
  109. }
  110. /*
  111. * general routines
  112. */
  113. static u64 veth_stats_one(struct pcpu_vstats *result, struct net_device *dev)
  114. {
  115. struct veth_priv *priv = netdev_priv(dev);
  116. int cpu;
  117. result->packets = 0;
  118. result->bytes = 0;
  119. for_each_possible_cpu(cpu) {
  120. struct pcpu_vstats *stats = per_cpu_ptr(dev->vstats, cpu);
  121. u64 packets, bytes;
  122. unsigned int start;
  123. do {
  124. start = u64_stats_fetch_begin_irq(&stats->syncp);
  125. packets = stats->packets;
  126. bytes = stats->bytes;
  127. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  128. result->packets += packets;
  129. result->bytes += bytes;
  130. }
  131. return atomic64_read(&priv->dropped);
  132. }
  133. static void veth_get_stats64(struct net_device *dev,
  134. struct rtnl_link_stats64 *tot)
  135. {
  136. struct veth_priv *priv = netdev_priv(dev);
  137. struct net_device *peer;
  138. struct pcpu_vstats one;
  139. tot->tx_dropped = veth_stats_one(&one, dev);
  140. tot->tx_bytes = one.bytes;
  141. tot->tx_packets = one.packets;
  142. rcu_read_lock();
  143. peer = rcu_dereference(priv->peer);
  144. if (peer) {
  145. tot->rx_dropped = veth_stats_one(&one, peer);
  146. tot->rx_bytes = one.bytes;
  147. tot->rx_packets = one.packets;
  148. }
  149. rcu_read_unlock();
  150. }
  151. /* fake multicast ability */
  152. static void veth_set_multicast_list(struct net_device *dev)
  153. {
  154. }
  155. static int veth_open(struct net_device *dev)
  156. {
  157. struct veth_priv *priv = netdev_priv(dev);
  158. struct net_device *peer = rtnl_dereference(priv->peer);
  159. if (!peer)
  160. return -ENOTCONN;
  161. if (peer->flags & IFF_UP) {
  162. netif_carrier_on(dev);
  163. netif_carrier_on(peer);
  164. }
  165. return 0;
  166. }
  167. static int veth_close(struct net_device *dev)
  168. {
  169. struct veth_priv *priv = netdev_priv(dev);
  170. struct net_device *peer = rtnl_dereference(priv->peer);
  171. netif_carrier_off(dev);
  172. if (peer)
  173. netif_carrier_off(peer);
  174. return 0;
  175. }
  176. static int is_valid_veth_mtu(int mtu)
  177. {
  178. return mtu >= ETH_MIN_MTU && mtu <= ETH_MAX_MTU;
  179. }
  180. static int veth_dev_init(struct net_device *dev)
  181. {
  182. dev->vstats = netdev_alloc_pcpu_stats(struct pcpu_vstats);
  183. if (!dev->vstats)
  184. return -ENOMEM;
  185. return 0;
  186. }
  187. static void veth_dev_free(struct net_device *dev)
  188. {
  189. free_percpu(dev->vstats);
  190. free_netdev(dev);
  191. }
  192. #ifdef CONFIG_NET_POLL_CONTROLLER
  193. static void veth_poll_controller(struct net_device *dev)
  194. {
  195. /* veth only receives frames when its peer sends one
  196. * Since it's a synchronous operation, we are guaranteed
  197. * never to have pending data when we poll for it so
  198. * there is nothing to do here.
  199. *
  200. * We need this though so netpoll recognizes us as an interface that
  201. * supports polling, which enables bridge devices in virt setups to
  202. * still use netconsole
  203. */
  204. }
  205. #endif /* CONFIG_NET_POLL_CONTROLLER */
  206. static int veth_get_iflink(const struct net_device *dev)
  207. {
  208. struct veth_priv *priv = netdev_priv(dev);
  209. struct net_device *peer;
  210. int iflink;
  211. rcu_read_lock();
  212. peer = rcu_dereference(priv->peer);
  213. iflink = peer ? peer->ifindex : 0;
  214. rcu_read_unlock();
  215. return iflink;
  216. }
  217. static void veth_set_rx_headroom(struct net_device *dev, int new_hr)
  218. {
  219. struct veth_priv *peer_priv, *priv = netdev_priv(dev);
  220. struct net_device *peer;
  221. if (new_hr < 0)
  222. new_hr = 0;
  223. rcu_read_lock();
  224. peer = rcu_dereference(priv->peer);
  225. if (unlikely(!peer))
  226. goto out;
  227. peer_priv = netdev_priv(peer);
  228. priv->requested_headroom = new_hr;
  229. new_hr = max(priv->requested_headroom, peer_priv->requested_headroom);
  230. dev->needed_headroom = new_hr;
  231. peer->needed_headroom = new_hr;
  232. out:
  233. rcu_read_unlock();
  234. }
  235. static const struct net_device_ops veth_netdev_ops = {
  236. .ndo_init = veth_dev_init,
  237. .ndo_open = veth_open,
  238. .ndo_stop = veth_close,
  239. .ndo_start_xmit = veth_xmit,
  240. .ndo_get_stats64 = veth_get_stats64,
  241. .ndo_set_rx_mode = veth_set_multicast_list,
  242. .ndo_set_mac_address = eth_mac_addr,
  243. #ifdef CONFIG_NET_POLL_CONTROLLER
  244. .ndo_poll_controller = veth_poll_controller,
  245. #endif
  246. .ndo_get_iflink = veth_get_iflink,
  247. .ndo_features_check = passthru_features_check,
  248. .ndo_set_rx_headroom = veth_set_rx_headroom,
  249. };
  250. #define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
  251. NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
  252. NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
  253. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
  254. NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
  255. static void veth_setup(struct net_device *dev)
  256. {
  257. ether_setup(dev);
  258. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  259. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  260. dev->priv_flags |= IFF_NO_QUEUE;
  261. dev->priv_flags |= IFF_PHONY_HEADROOM;
  262. dev->netdev_ops = &veth_netdev_ops;
  263. dev->ethtool_ops = &veth_ethtool_ops;
  264. dev->features |= NETIF_F_LLTX;
  265. dev->features |= VETH_FEATURES;
  266. dev->vlan_features = dev->features &
  267. ~(NETIF_F_HW_VLAN_CTAG_TX |
  268. NETIF_F_HW_VLAN_STAG_TX |
  269. NETIF_F_HW_VLAN_CTAG_RX |
  270. NETIF_F_HW_VLAN_STAG_RX);
  271. dev->destructor = veth_dev_free;
  272. dev->max_mtu = ETH_MAX_MTU;
  273. dev->hw_features = VETH_FEATURES;
  274. dev->hw_enc_features = VETH_FEATURES;
  275. dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
  276. }
  277. /*
  278. * netlink interface
  279. */
  280. static int veth_validate(struct nlattr *tb[], struct nlattr *data[])
  281. {
  282. if (tb[IFLA_ADDRESS]) {
  283. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  284. return -EINVAL;
  285. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  286. return -EADDRNOTAVAIL;
  287. }
  288. if (tb[IFLA_MTU]) {
  289. if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
  290. return -EINVAL;
  291. }
  292. return 0;
  293. }
  294. static struct rtnl_link_ops veth_link_ops;
  295. static int veth_newlink(struct net *src_net, struct net_device *dev,
  296. struct nlattr *tb[], struct nlattr *data[])
  297. {
  298. int err;
  299. struct net_device *peer;
  300. struct veth_priv *priv;
  301. char ifname[IFNAMSIZ];
  302. struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
  303. unsigned char name_assign_type;
  304. struct ifinfomsg *ifmp;
  305. struct net *net;
  306. /*
  307. * create and register peer first
  308. */
  309. if (data != NULL && data[VETH_INFO_PEER] != NULL) {
  310. struct nlattr *nla_peer;
  311. nla_peer = data[VETH_INFO_PEER];
  312. ifmp = nla_data(nla_peer);
  313. err = rtnl_nla_parse_ifla(peer_tb,
  314. nla_data(nla_peer) + sizeof(struct ifinfomsg),
  315. nla_len(nla_peer) - sizeof(struct ifinfomsg),
  316. NULL);
  317. if (err < 0)
  318. return err;
  319. err = veth_validate(peer_tb, NULL);
  320. if (err < 0)
  321. return err;
  322. tbp = peer_tb;
  323. } else {
  324. ifmp = NULL;
  325. tbp = tb;
  326. }
  327. if (tbp[IFLA_IFNAME]) {
  328. nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
  329. name_assign_type = NET_NAME_USER;
  330. } else {
  331. snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
  332. name_assign_type = NET_NAME_ENUM;
  333. }
  334. net = rtnl_link_get_net(src_net, tbp);
  335. if (IS_ERR(net))
  336. return PTR_ERR(net);
  337. peer = rtnl_create_link(net, ifname, name_assign_type,
  338. &veth_link_ops, tbp);
  339. if (IS_ERR(peer)) {
  340. put_net(net);
  341. return PTR_ERR(peer);
  342. }
  343. if (tbp[IFLA_ADDRESS] == NULL)
  344. eth_hw_addr_random(peer);
  345. if (ifmp && (dev->ifindex != 0))
  346. peer->ifindex = ifmp->ifi_index;
  347. err = register_netdevice(peer);
  348. put_net(net);
  349. net = NULL;
  350. if (err < 0)
  351. goto err_register_peer;
  352. netif_carrier_off(peer);
  353. err = rtnl_configure_link(peer, ifmp);
  354. if (err < 0)
  355. goto err_configure_peer;
  356. /*
  357. * register dev last
  358. *
  359. * note, that since we've registered new device the dev's name
  360. * should be re-allocated
  361. */
  362. if (tb[IFLA_ADDRESS] == NULL)
  363. eth_hw_addr_random(dev);
  364. if (tb[IFLA_IFNAME])
  365. nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
  366. else
  367. snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
  368. err = register_netdevice(dev);
  369. if (err < 0)
  370. goto err_register_dev;
  371. netif_carrier_off(dev);
  372. /*
  373. * tie the deviced together
  374. */
  375. priv = netdev_priv(dev);
  376. rcu_assign_pointer(priv->peer, peer);
  377. priv = netdev_priv(peer);
  378. rcu_assign_pointer(priv->peer, dev);
  379. return 0;
  380. err_register_dev:
  381. /* nothing to do */
  382. err_configure_peer:
  383. unregister_netdevice(peer);
  384. return err;
  385. err_register_peer:
  386. free_netdev(peer);
  387. return err;
  388. }
  389. static void veth_dellink(struct net_device *dev, struct list_head *head)
  390. {
  391. struct veth_priv *priv;
  392. struct net_device *peer;
  393. priv = netdev_priv(dev);
  394. peer = rtnl_dereference(priv->peer);
  395. /* Note : dellink() is called from default_device_exit_batch(),
  396. * before a rcu_synchronize() point. The devices are guaranteed
  397. * not being freed before one RCU grace period.
  398. */
  399. RCU_INIT_POINTER(priv->peer, NULL);
  400. unregister_netdevice_queue(dev, head);
  401. if (peer) {
  402. priv = netdev_priv(peer);
  403. RCU_INIT_POINTER(priv->peer, NULL);
  404. unregister_netdevice_queue(peer, head);
  405. }
  406. }
  407. static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
  408. [VETH_INFO_PEER] = { .len = sizeof(struct ifinfomsg) },
  409. };
  410. static struct net *veth_get_link_net(const struct net_device *dev)
  411. {
  412. struct veth_priv *priv = netdev_priv(dev);
  413. struct net_device *peer = rtnl_dereference(priv->peer);
  414. return peer ? dev_net(peer) : dev_net(dev);
  415. }
  416. static struct rtnl_link_ops veth_link_ops = {
  417. .kind = DRV_NAME,
  418. .priv_size = sizeof(struct veth_priv),
  419. .setup = veth_setup,
  420. .validate = veth_validate,
  421. .newlink = veth_newlink,
  422. .dellink = veth_dellink,
  423. .policy = veth_policy,
  424. .maxtype = VETH_INFO_MAX,
  425. .get_link_net = veth_get_link_net,
  426. };
  427. /*
  428. * init/fini
  429. */
  430. static __init int veth_init(void)
  431. {
  432. return rtnl_link_register(&veth_link_ops);
  433. }
  434. static __exit void veth_exit(void)
  435. {
  436. rtnl_link_unregister(&veth_link_ops);
  437. }
  438. module_init(veth_init);
  439. module_exit(veth_exit);
  440. MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
  441. MODULE_LICENSE("GPL v2");
  442. MODULE_ALIAS_RTNL_LINK(DRV_NAME);