veth.c 12 KB

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