veth.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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/dst.h>
  16. #include <net/xfrm.h>
  17. #include <linux/veth.h>
  18. #include <linux/module.h>
  19. #define DRV_NAME "veth"
  20. #define DRV_VERSION "1.0"
  21. #define MIN_MTU 68 /* Min L3 MTU */
  22. #define MAX_MTU 65535 /* Max L3 MTU (arbitrary) */
  23. struct pcpu_vstats {
  24. u64 packets;
  25. u64 bytes;
  26. struct u64_stats_sync syncp;
  27. };
  28. struct veth_priv {
  29. struct net_device __rcu *peer;
  30. atomic64_t dropped;
  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_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  41. {
  42. cmd->supported = 0;
  43. cmd->advertising = 0;
  44. ethtool_cmd_speed_set(cmd, SPEED_10000);
  45. cmd->duplex = DUPLEX_FULL;
  46. cmd->port = PORT_TP;
  47. cmd->phy_address = 0;
  48. cmd->transceiver = XCVR_INTERNAL;
  49. cmd->autoneg = AUTONEG_DISABLE;
  50. cmd->maxtxpkt = 0;
  51. cmd->maxrxpkt = 0;
  52. return 0;
  53. }
  54. static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  55. {
  56. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  57. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  58. }
  59. static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  60. {
  61. switch(stringset) {
  62. case ETH_SS_STATS:
  63. memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
  64. break;
  65. }
  66. }
  67. static int veth_get_sset_count(struct net_device *dev, int sset)
  68. {
  69. switch (sset) {
  70. case ETH_SS_STATS:
  71. return ARRAY_SIZE(ethtool_stats_keys);
  72. default:
  73. return -EOPNOTSUPP;
  74. }
  75. }
  76. static void veth_get_ethtool_stats(struct net_device *dev,
  77. struct ethtool_stats *stats, u64 *data)
  78. {
  79. struct veth_priv *priv = netdev_priv(dev);
  80. struct net_device *peer = rtnl_dereference(priv->peer);
  81. data[0] = peer ? peer->ifindex : 0;
  82. }
  83. static const struct ethtool_ops veth_ethtool_ops = {
  84. .get_settings = veth_get_settings,
  85. .get_drvinfo = veth_get_drvinfo,
  86. .get_link = ethtool_op_get_link,
  87. .get_strings = veth_get_strings,
  88. .get_sset_count = veth_get_sset_count,
  89. .get_ethtool_stats = veth_get_ethtool_stats,
  90. };
  91. static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
  92. {
  93. struct veth_priv *priv = netdev_priv(dev);
  94. struct net_device *rcv;
  95. int length = skb->len;
  96. rcu_read_lock();
  97. rcv = rcu_dereference(priv->peer);
  98. if (unlikely(!rcv)) {
  99. kfree_skb(skb);
  100. goto drop;
  101. }
  102. /* don't change ip_summed == CHECKSUM_PARTIAL, as that
  103. * will cause bad checksum on forwarded packets
  104. */
  105. if (skb->ip_summed == CHECKSUM_NONE &&
  106. rcv->features & NETIF_F_RXCSUM)
  107. skb->ip_summed = CHECKSUM_UNNECESSARY;
  108. if (likely(dev_forward_skb(rcv, skb) == NET_RX_SUCCESS)) {
  109. struct pcpu_vstats *stats = this_cpu_ptr(dev->vstats);
  110. u64_stats_update_begin(&stats->syncp);
  111. stats->bytes += length;
  112. stats->packets++;
  113. u64_stats_update_end(&stats->syncp);
  114. } else {
  115. drop:
  116. atomic64_inc(&priv->dropped);
  117. }
  118. rcu_read_unlock();
  119. return NETDEV_TX_OK;
  120. }
  121. /*
  122. * general routines
  123. */
  124. static u64 veth_stats_one(struct pcpu_vstats *result, struct net_device *dev)
  125. {
  126. struct veth_priv *priv = netdev_priv(dev);
  127. int cpu;
  128. result->packets = 0;
  129. result->bytes = 0;
  130. for_each_possible_cpu(cpu) {
  131. struct pcpu_vstats *stats = per_cpu_ptr(dev->vstats, cpu);
  132. u64 packets, bytes;
  133. unsigned int start;
  134. do {
  135. start = u64_stats_fetch_begin_bh(&stats->syncp);
  136. packets = stats->packets;
  137. bytes = stats->bytes;
  138. } while (u64_stats_fetch_retry_bh(&stats->syncp, start));
  139. result->packets += packets;
  140. result->bytes += bytes;
  141. }
  142. return atomic64_read(&priv->dropped);
  143. }
  144. static struct rtnl_link_stats64 *veth_get_stats64(struct net_device *dev,
  145. struct rtnl_link_stats64 *tot)
  146. {
  147. struct veth_priv *priv = netdev_priv(dev);
  148. struct net_device *peer;
  149. struct pcpu_vstats one;
  150. tot->tx_dropped = veth_stats_one(&one, dev);
  151. tot->tx_bytes = one.bytes;
  152. tot->tx_packets = one.packets;
  153. rcu_read_lock();
  154. peer = rcu_dereference(priv->peer);
  155. if (peer) {
  156. tot->rx_dropped = veth_stats_one(&one, peer);
  157. tot->rx_bytes = one.bytes;
  158. tot->rx_packets = one.packets;
  159. }
  160. rcu_read_unlock();
  161. return tot;
  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 = alloc_percpu(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. static const struct net_device_ops veth_netdev_ops = {
  208. .ndo_init = veth_dev_init,
  209. .ndo_open = veth_open,
  210. .ndo_stop = veth_close,
  211. .ndo_start_xmit = veth_xmit,
  212. .ndo_change_mtu = veth_change_mtu,
  213. .ndo_get_stats64 = veth_get_stats64,
  214. .ndo_set_mac_address = eth_mac_addr,
  215. };
  216. #define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
  217. NETIF_F_HW_CSUM | NETIF_F_RXCSUM | NETIF_F_HIGHDMA | \
  218. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
  219. NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
  220. static void veth_setup(struct net_device *dev)
  221. {
  222. ether_setup(dev);
  223. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  224. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  225. dev->netdev_ops = &veth_netdev_ops;
  226. dev->ethtool_ops = &veth_ethtool_ops;
  227. dev->features |= NETIF_F_LLTX;
  228. dev->features |= VETH_FEATURES;
  229. dev->vlan_features = dev->features;
  230. dev->destructor = veth_dev_free;
  231. dev->hw_features = VETH_FEATURES;
  232. }
  233. /*
  234. * netlink interface
  235. */
  236. static int veth_validate(struct nlattr *tb[], struct nlattr *data[])
  237. {
  238. if (tb[IFLA_ADDRESS]) {
  239. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  240. return -EINVAL;
  241. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  242. return -EADDRNOTAVAIL;
  243. }
  244. if (tb[IFLA_MTU]) {
  245. if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
  246. return -EINVAL;
  247. }
  248. return 0;
  249. }
  250. static struct rtnl_link_ops veth_link_ops;
  251. static int veth_newlink(struct net *src_net, struct net_device *dev,
  252. struct nlattr *tb[], struct nlattr *data[])
  253. {
  254. int err;
  255. struct net_device *peer;
  256. struct veth_priv *priv;
  257. char ifname[IFNAMSIZ];
  258. struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
  259. struct ifinfomsg *ifmp;
  260. struct net *net;
  261. /*
  262. * create and register peer first
  263. */
  264. if (data != NULL && data[VETH_INFO_PEER] != NULL) {
  265. struct nlattr *nla_peer;
  266. nla_peer = data[VETH_INFO_PEER];
  267. ifmp = nla_data(nla_peer);
  268. err = nla_parse(peer_tb, IFLA_MAX,
  269. nla_data(nla_peer) + sizeof(struct ifinfomsg),
  270. nla_len(nla_peer) - sizeof(struct ifinfomsg),
  271. ifla_policy);
  272. if (err < 0)
  273. return err;
  274. err = veth_validate(peer_tb, NULL);
  275. if (err < 0)
  276. return err;
  277. tbp = peer_tb;
  278. } else {
  279. ifmp = NULL;
  280. tbp = tb;
  281. }
  282. if (tbp[IFLA_IFNAME])
  283. nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
  284. else
  285. snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
  286. net = rtnl_link_get_net(src_net, tbp);
  287. if (IS_ERR(net))
  288. return PTR_ERR(net);
  289. peer = rtnl_create_link(net, ifname, &veth_link_ops, tbp);
  290. if (IS_ERR(peer)) {
  291. put_net(net);
  292. return PTR_ERR(peer);
  293. }
  294. if (tbp[IFLA_ADDRESS] == NULL)
  295. eth_hw_addr_random(peer);
  296. if (ifmp && (dev->ifindex != 0))
  297. peer->ifindex = ifmp->ifi_index;
  298. err = register_netdevice(peer);
  299. put_net(net);
  300. net = NULL;
  301. if (err < 0)
  302. goto err_register_peer;
  303. netif_carrier_off(peer);
  304. err = rtnl_configure_link(peer, ifmp);
  305. if (err < 0)
  306. goto err_configure_peer;
  307. /*
  308. * register dev last
  309. *
  310. * note, that since we've registered new device the dev's name
  311. * should be re-allocated
  312. */
  313. if (tb[IFLA_ADDRESS] == NULL)
  314. eth_hw_addr_random(dev);
  315. if (tb[IFLA_IFNAME])
  316. nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
  317. else
  318. snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
  319. err = register_netdevice(dev);
  320. if (err < 0)
  321. goto err_register_dev;
  322. netif_carrier_off(dev);
  323. /*
  324. * tie the deviced together
  325. */
  326. priv = netdev_priv(dev);
  327. rcu_assign_pointer(priv->peer, peer);
  328. priv = netdev_priv(peer);
  329. rcu_assign_pointer(priv->peer, dev);
  330. return 0;
  331. err_register_dev:
  332. /* nothing to do */
  333. err_configure_peer:
  334. unregister_netdevice(peer);
  335. return err;
  336. err_register_peer:
  337. free_netdev(peer);
  338. return err;
  339. }
  340. static void veth_dellink(struct net_device *dev, struct list_head *head)
  341. {
  342. struct veth_priv *priv;
  343. struct net_device *peer;
  344. priv = netdev_priv(dev);
  345. peer = rtnl_dereference(priv->peer);
  346. /* Note : dellink() is called from default_device_exit_batch(),
  347. * before a rcu_synchronize() point. The devices are guaranteed
  348. * not being freed before one RCU grace period.
  349. */
  350. RCU_INIT_POINTER(priv->peer, NULL);
  351. unregister_netdevice_queue(dev, head);
  352. if (peer) {
  353. priv = netdev_priv(peer);
  354. RCU_INIT_POINTER(priv->peer, NULL);
  355. unregister_netdevice_queue(peer, head);
  356. }
  357. }
  358. static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
  359. [VETH_INFO_PEER] = { .len = sizeof(struct ifinfomsg) },
  360. };
  361. static struct rtnl_link_ops veth_link_ops = {
  362. .kind = DRV_NAME,
  363. .priv_size = sizeof(struct veth_priv),
  364. .setup = veth_setup,
  365. .validate = veth_validate,
  366. .newlink = veth_newlink,
  367. .dellink = veth_dellink,
  368. .policy = veth_policy,
  369. .maxtype = VETH_INFO_MAX,
  370. };
  371. /*
  372. * init/fini
  373. */
  374. static __init int veth_init(void)
  375. {
  376. return rtnl_link_register(&veth_link_ops);
  377. }
  378. static __exit void veth_exit(void)
  379. {
  380. rtnl_link_unregister(&veth_link_ops);
  381. }
  382. module_init(veth_init);
  383. module_exit(veth_exit);
  384. MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
  385. MODULE_LICENSE("GPL v2");
  386. MODULE_ALIAS_RTNL_LINK(DRV_NAME);