veth.c 9.7 KB

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