veth.c 12 KB

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