veth.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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/list.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/ethtool.h>
  13. #include <linux/etherdevice.h>
  14. #include <net/dst.h>
  15. #include <net/xfrm.h>
  16. #include <net/veth.h>
  17. #define DRV_NAME "veth"
  18. #define DRV_VERSION "1.0"
  19. struct veth_net_stats {
  20. unsigned long rx_packets;
  21. unsigned long tx_packets;
  22. unsigned long rx_bytes;
  23. unsigned long tx_bytes;
  24. unsigned long tx_dropped;
  25. };
  26. struct veth_priv {
  27. struct net_device *peer;
  28. struct net_device *dev;
  29. struct list_head list;
  30. struct veth_net_stats *stats;
  31. unsigned ip_summed;
  32. };
  33. static LIST_HEAD(veth_list);
  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. cmd->speed = 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. strcpy(info->driver, DRV_NAME);
  59. strcpy(info->version, DRV_VERSION);
  60. strcpy(info->fw_version, "N/A");
  61. }
  62. static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  63. {
  64. switch(stringset) {
  65. case ETH_SS_STATS:
  66. memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
  67. break;
  68. }
  69. }
  70. static int veth_get_sset_count(struct net_device *dev, int sset)
  71. {
  72. switch (sset) {
  73. case ETH_SS_STATS:
  74. return ARRAY_SIZE(ethtool_stats_keys);
  75. default:
  76. return -EOPNOTSUPP;
  77. }
  78. }
  79. static void veth_get_ethtool_stats(struct net_device *dev,
  80. struct ethtool_stats *stats, u64 *data)
  81. {
  82. struct veth_priv *priv;
  83. priv = netdev_priv(dev);
  84. data[0] = priv->peer->ifindex;
  85. }
  86. static u32 veth_get_rx_csum(struct net_device *dev)
  87. {
  88. struct veth_priv *priv;
  89. priv = netdev_priv(dev);
  90. return priv->ip_summed == CHECKSUM_UNNECESSARY;
  91. }
  92. static int veth_set_rx_csum(struct net_device *dev, u32 data)
  93. {
  94. struct veth_priv *priv;
  95. priv = netdev_priv(dev);
  96. priv->ip_summed = data ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
  97. return 0;
  98. }
  99. static u32 veth_get_tx_csum(struct net_device *dev)
  100. {
  101. return (dev->features & NETIF_F_NO_CSUM) != 0;
  102. }
  103. static int veth_set_tx_csum(struct net_device *dev, u32 data)
  104. {
  105. if (data)
  106. dev->features |= NETIF_F_NO_CSUM;
  107. else
  108. dev->features &= ~NETIF_F_NO_CSUM;
  109. return 0;
  110. }
  111. static struct ethtool_ops veth_ethtool_ops = {
  112. .get_settings = veth_get_settings,
  113. .get_drvinfo = veth_get_drvinfo,
  114. .get_link = ethtool_op_get_link,
  115. .get_rx_csum = veth_get_rx_csum,
  116. .set_rx_csum = veth_set_rx_csum,
  117. .get_tx_csum = veth_get_tx_csum,
  118. .set_tx_csum = veth_set_tx_csum,
  119. .get_sg = ethtool_op_get_sg,
  120. .set_sg = ethtool_op_set_sg,
  121. .get_strings = veth_get_strings,
  122. .get_sset_count = veth_get_sset_count,
  123. .get_ethtool_stats = veth_get_ethtool_stats,
  124. };
  125. /*
  126. * xmit
  127. */
  128. static int veth_xmit(struct sk_buff *skb, struct net_device *dev)
  129. {
  130. struct net_device *rcv = NULL;
  131. struct veth_priv *priv, *rcv_priv;
  132. struct veth_net_stats *stats;
  133. int length, cpu;
  134. skb_orphan(skb);
  135. priv = netdev_priv(dev);
  136. rcv = priv->peer;
  137. rcv_priv = netdev_priv(rcv);
  138. cpu = smp_processor_id();
  139. stats = per_cpu_ptr(priv->stats, cpu);
  140. if (!(rcv->flags & IFF_UP))
  141. goto outf;
  142. skb->pkt_type = PACKET_HOST;
  143. skb->protocol = eth_type_trans(skb, rcv);
  144. if (dev->features & NETIF_F_NO_CSUM)
  145. skb->ip_summed = rcv_priv->ip_summed;
  146. dst_release(skb->dst);
  147. skb->dst = NULL;
  148. skb->mark = 0;
  149. secpath_reset(skb);
  150. nf_reset(skb);
  151. length = skb->len;
  152. stats->tx_bytes += length;
  153. stats->tx_packets++;
  154. stats = per_cpu_ptr(rcv_priv->stats, cpu);
  155. stats->rx_bytes += length;
  156. stats->rx_packets++;
  157. netif_rx(skb);
  158. return 0;
  159. outf:
  160. kfree_skb(skb);
  161. stats->tx_dropped++;
  162. return 0;
  163. }
  164. /*
  165. * general routines
  166. */
  167. static struct net_device_stats *veth_get_stats(struct net_device *dev)
  168. {
  169. struct veth_priv *priv;
  170. struct net_device_stats *dev_stats;
  171. int cpu;
  172. struct veth_net_stats *stats;
  173. priv = netdev_priv(dev);
  174. dev_stats = &dev->stats;
  175. dev_stats->rx_packets = 0;
  176. dev_stats->tx_packets = 0;
  177. dev_stats->rx_bytes = 0;
  178. dev_stats->tx_bytes = 0;
  179. dev_stats->tx_dropped = 0;
  180. for_each_online_cpu(cpu) {
  181. stats = per_cpu_ptr(priv->stats, cpu);
  182. dev_stats->rx_packets += stats->rx_packets;
  183. dev_stats->tx_packets += stats->tx_packets;
  184. dev_stats->rx_bytes += stats->rx_bytes;
  185. dev_stats->tx_bytes += stats->tx_bytes;
  186. dev_stats->tx_dropped += stats->tx_dropped;
  187. }
  188. return dev_stats;
  189. }
  190. static int veth_open(struct net_device *dev)
  191. {
  192. struct veth_priv *priv;
  193. priv = netdev_priv(dev);
  194. if (priv->peer == NULL)
  195. return -ENOTCONN;
  196. if (priv->peer->flags & IFF_UP) {
  197. netif_carrier_on(dev);
  198. netif_carrier_on(priv->peer);
  199. }
  200. return 0;
  201. }
  202. static int veth_close(struct net_device *dev)
  203. {
  204. struct veth_priv *priv;
  205. if (netif_carrier_ok(dev)) {
  206. priv = netdev_priv(dev);
  207. netif_carrier_off(dev);
  208. netif_carrier_off(priv->peer);
  209. }
  210. return 0;
  211. }
  212. static int veth_dev_init(struct net_device *dev)
  213. {
  214. struct veth_net_stats *stats;
  215. struct veth_priv *priv;
  216. stats = alloc_percpu(struct veth_net_stats);
  217. if (stats == NULL)
  218. return -ENOMEM;
  219. priv = netdev_priv(dev);
  220. priv->stats = stats;
  221. return 0;
  222. }
  223. static void veth_dev_free(struct net_device *dev)
  224. {
  225. struct veth_priv *priv;
  226. priv = netdev_priv(dev);
  227. free_percpu(priv->stats);
  228. free_netdev(dev);
  229. }
  230. static void veth_setup(struct net_device *dev)
  231. {
  232. ether_setup(dev);
  233. dev->hard_start_xmit = veth_xmit;
  234. dev->get_stats = veth_get_stats;
  235. dev->open = veth_open;
  236. dev->stop = veth_close;
  237. dev->ethtool_ops = &veth_ethtool_ops;
  238. dev->features |= NETIF_F_LLTX;
  239. dev->init = veth_dev_init;
  240. dev->destructor = veth_dev_free;
  241. }
  242. /*
  243. * netlink interface
  244. */
  245. static int veth_validate(struct nlattr *tb[], struct nlattr *data[])
  246. {
  247. if (tb[IFLA_ADDRESS]) {
  248. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  249. return -EINVAL;
  250. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  251. return -EADDRNOTAVAIL;
  252. }
  253. return 0;
  254. }
  255. static struct rtnl_link_ops veth_link_ops;
  256. static int veth_newlink(struct net_device *dev,
  257. struct nlattr *tb[], struct nlattr *data[])
  258. {
  259. int err;
  260. struct net_device *peer;
  261. struct veth_priv *priv;
  262. char ifname[IFNAMSIZ];
  263. struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
  264. /*
  265. * create and register peer first
  266. *
  267. * struct ifinfomsg is at the head of VETH_INFO_PEER, but we
  268. * skip it since no info from it is useful yet
  269. */
  270. if (data != NULL && data[VETH_INFO_PEER] != NULL) {
  271. struct nlattr *nla_peer;
  272. nla_peer = data[VETH_INFO_PEER];
  273. err = nla_parse(peer_tb, IFLA_MAX,
  274. nla_data(nla_peer) + sizeof(struct ifinfomsg),
  275. nla_len(nla_peer) - sizeof(struct ifinfomsg),
  276. ifla_policy);
  277. if (err < 0)
  278. return err;
  279. err = veth_validate(peer_tb, NULL);
  280. if (err < 0)
  281. return err;
  282. tbp = peer_tb;
  283. } else
  284. tbp = tb;
  285. if (tbp[IFLA_IFNAME])
  286. nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
  287. else
  288. snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
  289. peer = rtnl_create_link(dev->nd_net, ifname, &veth_link_ops, tbp);
  290. if (IS_ERR(peer))
  291. return PTR_ERR(peer);
  292. if (tbp[IFLA_ADDRESS] == NULL)
  293. random_ether_addr(peer->dev_addr);
  294. err = register_netdevice(peer);
  295. if (err < 0)
  296. goto err_register_peer;
  297. netif_carrier_off(peer);
  298. /*
  299. * register dev last
  300. *
  301. * note, that since we've registered new device the dev's name
  302. * should be re-allocated
  303. */
  304. if (tb[IFLA_ADDRESS] == NULL)
  305. random_ether_addr(dev->dev_addr);
  306. if (tb[IFLA_IFNAME])
  307. nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
  308. else
  309. snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
  310. if (strchr(dev->name, '%')) {
  311. err = dev_alloc_name(dev, dev->name);
  312. if (err < 0)
  313. goto err_alloc_name;
  314. }
  315. err = register_netdevice(dev);
  316. if (err < 0)
  317. goto err_register_dev;
  318. netif_carrier_off(dev);
  319. /*
  320. * tie the deviced together
  321. */
  322. priv = netdev_priv(dev);
  323. priv->dev = dev;
  324. priv->peer = peer;
  325. list_add(&priv->list, &veth_list);
  326. priv = netdev_priv(peer);
  327. priv->dev = peer;
  328. priv->peer = dev;
  329. INIT_LIST_HEAD(&priv->list);
  330. return 0;
  331. err_register_dev:
  332. /* nothing to do */
  333. err_alloc_name:
  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)
  341. {
  342. struct veth_priv *priv;
  343. struct net_device *peer;
  344. priv = netdev_priv(dev);
  345. peer = priv->peer;
  346. if (!list_empty(&priv->list))
  347. list_del(&priv->list);
  348. priv = netdev_priv(peer);
  349. if (!list_empty(&priv->list))
  350. list_del(&priv->list);
  351. unregister_netdevice(dev);
  352. unregister_netdevice(peer);
  353. }
  354. static const struct nla_policy veth_policy[VETH_INFO_MAX + 1];
  355. static struct rtnl_link_ops veth_link_ops = {
  356. .kind = DRV_NAME,
  357. .priv_size = sizeof(struct veth_priv),
  358. .setup = veth_setup,
  359. .validate = veth_validate,
  360. .newlink = veth_newlink,
  361. .dellink = veth_dellink,
  362. .policy = veth_policy,
  363. .maxtype = VETH_INFO_MAX,
  364. };
  365. /*
  366. * init/fini
  367. */
  368. static __init int veth_init(void)
  369. {
  370. return rtnl_link_register(&veth_link_ops);
  371. }
  372. static __exit void veth_exit(void)
  373. {
  374. struct veth_priv *priv, *next;
  375. rtnl_lock();
  376. /*
  377. * cannot trust __rtnl_link_unregister() to unregister all
  378. * devices, as each ->dellink call will remove two devices
  379. * from the list at once.
  380. */
  381. list_for_each_entry_safe(priv, next, &veth_list, list)
  382. veth_dellink(priv->dev);
  383. __rtnl_link_unregister(&veth_link_ops);
  384. rtnl_unlock();
  385. }
  386. module_init(veth_init);
  387. module_exit(veth_exit);
  388. MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
  389. MODULE_LICENSE("GPL v2");
  390. MODULE_ALIAS_RTNL_LINK(DRV_NAME);