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_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. }
  191. #ifdef CONFIG_NET_POLL_CONTROLLER
  192. static void veth_poll_controller(struct net_device *dev)
  193. {
  194. /* veth only receives frames when its peer sends one
  195. * Since it's a synchronous operation, we are guaranteed
  196. * never to have pending data when we poll for it so
  197. * there is nothing to do here.
  198. *
  199. * We need this though so netpoll recognizes us as an interface that
  200. * supports polling, which enables bridge devices in virt setups to
  201. * still use netconsole
  202. */
  203. }
  204. #endif /* CONFIG_NET_POLL_CONTROLLER */
  205. static int veth_get_iflink(const struct net_device *dev)
  206. {
  207. struct veth_priv *priv = netdev_priv(dev);
  208. struct net_device *peer;
  209. int iflink;
  210. rcu_read_lock();
  211. peer = rcu_dereference(priv->peer);
  212. iflink = peer ? peer->ifindex : 0;
  213. rcu_read_unlock();
  214. return iflink;
  215. }
  216. static void veth_set_rx_headroom(struct net_device *dev, int new_hr)
  217. {
  218. struct veth_priv *peer_priv, *priv = netdev_priv(dev);
  219. struct net_device *peer;
  220. if (new_hr < 0)
  221. new_hr = 0;
  222. rcu_read_lock();
  223. peer = rcu_dereference(priv->peer);
  224. if (unlikely(!peer))
  225. goto out;
  226. peer_priv = netdev_priv(peer);
  227. priv->requested_headroom = new_hr;
  228. new_hr = max(priv->requested_headroom, peer_priv->requested_headroom);
  229. dev->needed_headroom = new_hr;
  230. peer->needed_headroom = new_hr;
  231. out:
  232. rcu_read_unlock();
  233. }
  234. static const struct net_device_ops veth_netdev_ops = {
  235. .ndo_init = veth_dev_init,
  236. .ndo_open = veth_open,
  237. .ndo_stop = veth_close,
  238. .ndo_start_xmit = veth_xmit,
  239. .ndo_get_stats64 = veth_get_stats64,
  240. .ndo_set_rx_mode = veth_set_multicast_list,
  241. .ndo_set_mac_address = eth_mac_addr,
  242. #ifdef CONFIG_NET_POLL_CONTROLLER
  243. .ndo_poll_controller = veth_poll_controller,
  244. #endif
  245. .ndo_get_iflink = veth_get_iflink,
  246. .ndo_features_check = passthru_features_check,
  247. .ndo_set_rx_headroom = veth_set_rx_headroom,
  248. };
  249. #define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
  250. NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
  251. NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
  252. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
  253. NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
  254. static void veth_setup(struct net_device *dev)
  255. {
  256. ether_setup(dev);
  257. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  258. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  259. dev->priv_flags |= IFF_NO_QUEUE;
  260. dev->priv_flags |= IFF_PHONY_HEADROOM;
  261. dev->netdev_ops = &veth_netdev_ops;
  262. dev->ethtool_ops = &veth_ethtool_ops;
  263. dev->features |= NETIF_F_LLTX;
  264. dev->features |= VETH_FEATURES;
  265. dev->vlan_features = dev->features &
  266. ~(NETIF_F_HW_VLAN_CTAG_TX |
  267. NETIF_F_HW_VLAN_STAG_TX |
  268. NETIF_F_HW_VLAN_CTAG_RX |
  269. NETIF_F_HW_VLAN_STAG_RX);
  270. dev->needs_free_netdev = true;
  271. dev->priv_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. struct netlink_ext_ack *extack)
  282. {
  283. if (tb[IFLA_ADDRESS]) {
  284. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  285. return -EINVAL;
  286. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  287. return -EADDRNOTAVAIL;
  288. }
  289. if (tb[IFLA_MTU]) {
  290. if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
  291. return -EINVAL;
  292. }
  293. return 0;
  294. }
  295. static struct rtnl_link_ops veth_link_ops;
  296. static int veth_newlink(struct net *src_net, struct net_device *dev,
  297. struct nlattr *tb[], struct nlattr *data[],
  298. struct netlink_ext_ack *extack)
  299. {
  300. int err;
  301. struct net_device *peer;
  302. struct veth_priv *priv;
  303. char ifname[IFNAMSIZ];
  304. struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
  305. unsigned char name_assign_type;
  306. struct ifinfomsg *ifmp;
  307. struct net *net;
  308. /*
  309. * create and register peer first
  310. */
  311. if (data != NULL && data[VETH_INFO_PEER] != NULL) {
  312. struct nlattr *nla_peer;
  313. nla_peer = data[VETH_INFO_PEER];
  314. ifmp = nla_data(nla_peer);
  315. err = rtnl_nla_parse_ifla(peer_tb,
  316. nla_data(nla_peer) + sizeof(struct ifinfomsg),
  317. nla_len(nla_peer) - sizeof(struct ifinfomsg),
  318. NULL);
  319. if (err < 0)
  320. return err;
  321. err = veth_validate(peer_tb, NULL, extack);
  322. if (err < 0)
  323. return err;
  324. tbp = peer_tb;
  325. } else {
  326. ifmp = NULL;
  327. tbp = tb;
  328. }
  329. if (ifmp && tbp[IFLA_IFNAME]) {
  330. nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
  331. name_assign_type = NET_NAME_USER;
  332. } else {
  333. snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
  334. name_assign_type = NET_NAME_ENUM;
  335. }
  336. net = rtnl_link_get_net(src_net, tbp);
  337. if (IS_ERR(net))
  338. return PTR_ERR(net);
  339. peer = rtnl_create_link(net, ifname, name_assign_type,
  340. &veth_link_ops, tbp);
  341. if (IS_ERR(peer)) {
  342. put_net(net);
  343. return PTR_ERR(peer);
  344. }
  345. if (!ifmp || !tbp[IFLA_ADDRESS])
  346. eth_hw_addr_random(peer);
  347. if (ifmp && (dev->ifindex != 0))
  348. peer->ifindex = ifmp->ifi_index;
  349. peer->gso_max_size = dev->gso_max_size;
  350. peer->gso_max_segs = dev->gso_max_segs;
  351. err = register_netdevice(peer);
  352. put_net(net);
  353. net = NULL;
  354. if (err < 0)
  355. goto err_register_peer;
  356. netif_carrier_off(peer);
  357. err = rtnl_configure_link(peer, ifmp);
  358. if (err < 0)
  359. goto err_configure_peer;
  360. /*
  361. * register dev last
  362. *
  363. * note, that since we've registered new device the dev's name
  364. * should be re-allocated
  365. */
  366. if (tb[IFLA_ADDRESS] == NULL)
  367. eth_hw_addr_random(dev);
  368. if (tb[IFLA_IFNAME])
  369. nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
  370. else
  371. snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
  372. err = register_netdevice(dev);
  373. if (err < 0)
  374. goto err_register_dev;
  375. netif_carrier_off(dev);
  376. /*
  377. * tie the deviced together
  378. */
  379. priv = netdev_priv(dev);
  380. rcu_assign_pointer(priv->peer, peer);
  381. priv = netdev_priv(peer);
  382. rcu_assign_pointer(priv->peer, dev);
  383. return 0;
  384. err_register_dev:
  385. /* nothing to do */
  386. err_configure_peer:
  387. unregister_netdevice(peer);
  388. return err;
  389. err_register_peer:
  390. free_netdev(peer);
  391. return err;
  392. }
  393. static void veth_dellink(struct net_device *dev, struct list_head *head)
  394. {
  395. struct veth_priv *priv;
  396. struct net_device *peer;
  397. priv = netdev_priv(dev);
  398. peer = rtnl_dereference(priv->peer);
  399. /* Note : dellink() is called from default_device_exit_batch(),
  400. * before a rcu_synchronize() point. The devices are guaranteed
  401. * not being freed before one RCU grace period.
  402. */
  403. RCU_INIT_POINTER(priv->peer, NULL);
  404. unregister_netdevice_queue(dev, head);
  405. if (peer) {
  406. priv = netdev_priv(peer);
  407. RCU_INIT_POINTER(priv->peer, NULL);
  408. unregister_netdevice_queue(peer, head);
  409. }
  410. }
  411. static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
  412. [VETH_INFO_PEER] = { .len = sizeof(struct ifinfomsg) },
  413. };
  414. static struct net *veth_get_link_net(const struct net_device *dev)
  415. {
  416. struct veth_priv *priv = netdev_priv(dev);
  417. struct net_device *peer = rtnl_dereference(priv->peer);
  418. return peer ? dev_net(peer) : dev_net(dev);
  419. }
  420. static struct rtnl_link_ops veth_link_ops = {
  421. .kind = DRV_NAME,
  422. .priv_size = sizeof(struct veth_priv),
  423. .setup = veth_setup,
  424. .validate = veth_validate,
  425. .newlink = veth_newlink,
  426. .dellink = veth_dellink,
  427. .policy = veth_policy,
  428. .maxtype = VETH_INFO_MAX,
  429. .get_link_net = veth_get_link_net,
  430. };
  431. /*
  432. * init/fini
  433. */
  434. static __init int veth_init(void)
  435. {
  436. return rtnl_link_register(&veth_link_ops);
  437. }
  438. static __exit void veth_exit(void)
  439. {
  440. rtnl_link_unregister(&veth_link_ops);
  441. }
  442. module_init(veth_init);
  443. module_exit(veth_exit);
  444. MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
  445. MODULE_LICENSE("GPL v2");
  446. MODULE_ALIAS_RTNL_LINK(DRV_NAME);