veth.c 9.4 KB

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