veth.c 12 KB

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