veth.c 12 KB

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