veth.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. static const struct net_device_ops veth_netdev_ops = {
  213. .ndo_init = veth_dev_init,
  214. .ndo_open = veth_open,
  215. .ndo_stop = veth_close,
  216. .ndo_start_xmit = veth_xmit,
  217. .ndo_change_mtu = veth_change_mtu,
  218. .ndo_get_stats64 = veth_get_stats64,
  219. .ndo_set_rx_mode = veth_set_multicast_list,
  220. .ndo_set_mac_address = eth_mac_addr,
  221. };
  222. #define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
  223. NETIF_F_HW_CSUM | NETIF_F_RXCSUM | NETIF_F_HIGHDMA | \
  224. NETIF_F_GSO_GRE | NETIF_F_GSO_UDP_TUNNEL | \
  225. NETIF_F_GSO_IPIP | NETIF_F_GSO_SIT | NETIF_F_UFO | \
  226. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
  227. NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
  228. static void veth_setup(struct net_device *dev)
  229. {
  230. ether_setup(dev);
  231. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  232. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  233. dev->netdev_ops = &veth_netdev_ops;
  234. dev->ethtool_ops = &veth_ethtool_ops;
  235. dev->features |= NETIF_F_LLTX;
  236. dev->features |= VETH_FEATURES;
  237. dev->vlan_features = dev->features &
  238. ~(NETIF_F_HW_VLAN_CTAG_TX |
  239. NETIF_F_HW_VLAN_STAG_TX |
  240. NETIF_F_HW_VLAN_CTAG_RX |
  241. NETIF_F_HW_VLAN_STAG_RX);
  242. dev->destructor = veth_dev_free;
  243. dev->hw_features = VETH_FEATURES;
  244. dev->hw_enc_features = VETH_FEATURES;
  245. }
  246. /*
  247. * netlink interface
  248. */
  249. static int veth_validate(struct nlattr *tb[], struct nlattr *data[])
  250. {
  251. if (tb[IFLA_ADDRESS]) {
  252. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  253. return -EINVAL;
  254. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  255. return -EADDRNOTAVAIL;
  256. }
  257. if (tb[IFLA_MTU]) {
  258. if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
  259. return -EINVAL;
  260. }
  261. return 0;
  262. }
  263. static struct rtnl_link_ops veth_link_ops;
  264. static int veth_newlink(struct net *src_net, struct net_device *dev,
  265. struct nlattr *tb[], struct nlattr *data[])
  266. {
  267. int err;
  268. struct net_device *peer;
  269. struct veth_priv *priv;
  270. char ifname[IFNAMSIZ];
  271. struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
  272. struct ifinfomsg *ifmp;
  273. struct net *net;
  274. /*
  275. * create and register peer first
  276. */
  277. if (data != NULL && data[VETH_INFO_PEER] != NULL) {
  278. struct nlattr *nla_peer;
  279. nla_peer = data[VETH_INFO_PEER];
  280. ifmp = nla_data(nla_peer);
  281. err = rtnl_nla_parse_ifla(peer_tb,
  282. nla_data(nla_peer) + sizeof(struct ifinfomsg),
  283. nla_len(nla_peer) - sizeof(struct ifinfomsg));
  284. if (err < 0)
  285. return err;
  286. err = veth_validate(peer_tb, NULL);
  287. if (err < 0)
  288. return err;
  289. tbp = peer_tb;
  290. } else {
  291. ifmp = NULL;
  292. tbp = tb;
  293. }
  294. if (tbp[IFLA_IFNAME])
  295. nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
  296. else
  297. snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
  298. net = rtnl_link_get_net(src_net, tbp);
  299. if (IS_ERR(net))
  300. return PTR_ERR(net);
  301. peer = rtnl_create_link(net, ifname, &veth_link_ops, tbp);
  302. if (IS_ERR(peer)) {
  303. put_net(net);
  304. return PTR_ERR(peer);
  305. }
  306. if (tbp[IFLA_ADDRESS] == NULL)
  307. eth_hw_addr_random(peer);
  308. if (ifmp && (dev->ifindex != 0))
  309. peer->ifindex = ifmp->ifi_index;
  310. err = register_netdevice(peer);
  311. put_net(net);
  312. net = NULL;
  313. if (err < 0)
  314. goto err_register_peer;
  315. netif_carrier_off(peer);
  316. err = rtnl_configure_link(peer, ifmp);
  317. if (err < 0)
  318. goto err_configure_peer;
  319. /*
  320. * register dev last
  321. *
  322. * note, that since we've registered new device the dev's name
  323. * should be re-allocated
  324. */
  325. if (tb[IFLA_ADDRESS] == NULL)
  326. eth_hw_addr_random(dev);
  327. if (tb[IFLA_IFNAME])
  328. nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
  329. else
  330. snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
  331. err = register_netdevice(dev);
  332. if (err < 0)
  333. goto err_register_dev;
  334. netif_carrier_off(dev);
  335. /*
  336. * tie the deviced together
  337. */
  338. priv = netdev_priv(dev);
  339. rcu_assign_pointer(priv->peer, peer);
  340. priv = netdev_priv(peer);
  341. rcu_assign_pointer(priv->peer, dev);
  342. return 0;
  343. err_register_dev:
  344. /* nothing to do */
  345. err_configure_peer:
  346. unregister_netdevice(peer);
  347. return err;
  348. err_register_peer:
  349. free_netdev(peer);
  350. return err;
  351. }
  352. static void veth_dellink(struct net_device *dev, struct list_head *head)
  353. {
  354. struct veth_priv *priv;
  355. struct net_device *peer;
  356. priv = netdev_priv(dev);
  357. peer = rtnl_dereference(priv->peer);
  358. /* Note : dellink() is called from default_device_exit_batch(),
  359. * before a rcu_synchronize() point. The devices are guaranteed
  360. * not being freed before one RCU grace period.
  361. */
  362. RCU_INIT_POINTER(priv->peer, NULL);
  363. unregister_netdevice_queue(dev, head);
  364. if (peer) {
  365. priv = netdev_priv(peer);
  366. RCU_INIT_POINTER(priv->peer, NULL);
  367. unregister_netdevice_queue(peer, head);
  368. }
  369. }
  370. static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
  371. [VETH_INFO_PEER] = { .len = sizeof(struct ifinfomsg) },
  372. };
  373. static struct rtnl_link_ops veth_link_ops = {
  374. .kind = DRV_NAME,
  375. .priv_size = sizeof(struct veth_priv),
  376. .setup = veth_setup,
  377. .validate = veth_validate,
  378. .newlink = veth_newlink,
  379. .dellink = veth_dellink,
  380. .policy = veth_policy,
  381. .maxtype = VETH_INFO_MAX,
  382. };
  383. /*
  384. * init/fini
  385. */
  386. static __init int veth_init(void)
  387. {
  388. return rtnl_link_register(&veth_link_ops);
  389. }
  390. static __exit void veth_exit(void)
  391. {
  392. rtnl_link_unregister(&veth_link_ops);
  393. }
  394. module_init(veth_init);
  395. module_exit(veth_exit);
  396. MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
  397. MODULE_LICENSE("GPL v2");
  398. MODULE_ALIAS_RTNL_LINK(DRV_NAME);