veth.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 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_dev_init(struct net_device *dev)
  203. {
  204. struct veth_net_stats *stats;
  205. struct veth_priv *priv;
  206. stats = alloc_percpu(struct veth_net_stats);
  207. if (stats == NULL)
  208. return -ENOMEM;
  209. priv = netdev_priv(dev);
  210. priv->stats = stats;
  211. return 0;
  212. }
  213. static void veth_dev_free(struct net_device *dev)
  214. {
  215. struct veth_priv *priv;
  216. priv = netdev_priv(dev);
  217. free_percpu(priv->stats);
  218. free_netdev(dev);
  219. }
  220. static void veth_setup(struct net_device *dev)
  221. {
  222. ether_setup(dev);
  223. dev->hard_start_xmit = veth_xmit;
  224. dev->get_stats = veth_get_stats;
  225. dev->open = veth_open;
  226. dev->ethtool_ops = &veth_ethtool_ops;
  227. dev->features |= NETIF_F_LLTX;
  228. dev->init = veth_dev_init;
  229. dev->destructor = veth_dev_free;
  230. }
  231. static void veth_change_state(struct net_device *dev)
  232. {
  233. struct net_device *peer;
  234. struct veth_priv *priv;
  235. priv = netdev_priv(dev);
  236. peer = priv->peer;
  237. if (netif_carrier_ok(peer)) {
  238. if (!netif_carrier_ok(dev))
  239. netif_carrier_on(dev);
  240. } else {
  241. if (netif_carrier_ok(dev))
  242. netif_carrier_off(dev);
  243. }
  244. }
  245. static int veth_device_event(struct notifier_block *unused,
  246. unsigned long event, void *ptr)
  247. {
  248. struct net_device *dev = ptr;
  249. if (dev->open != veth_open)
  250. goto out;
  251. switch (event) {
  252. case NETDEV_CHANGE:
  253. veth_change_state(dev);
  254. break;
  255. }
  256. out:
  257. return NOTIFY_DONE;
  258. }
  259. static struct notifier_block veth_notifier_block __read_mostly = {
  260. .notifier_call = veth_device_event,
  261. };
  262. /*
  263. * netlink interface
  264. */
  265. static int veth_validate(struct nlattr *tb[], struct nlattr *data[])
  266. {
  267. if (tb[IFLA_ADDRESS]) {
  268. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  269. return -EINVAL;
  270. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  271. return -EADDRNOTAVAIL;
  272. }
  273. return 0;
  274. }
  275. static struct rtnl_link_ops veth_link_ops;
  276. static int veth_newlink(struct net_device *dev,
  277. struct nlattr *tb[], struct nlattr *data[])
  278. {
  279. int err;
  280. struct net_device *peer;
  281. struct veth_priv *priv;
  282. char ifname[IFNAMSIZ];
  283. struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
  284. /*
  285. * create and register peer first
  286. *
  287. * struct ifinfomsg is at the head of VETH_INFO_PEER, but we
  288. * skip it since no info from it is useful yet
  289. */
  290. if (data != NULL && data[VETH_INFO_PEER] != NULL) {
  291. struct nlattr *nla_peer;
  292. nla_peer = data[VETH_INFO_PEER];
  293. err = nla_parse(peer_tb, IFLA_MAX,
  294. nla_data(nla_peer) + sizeof(struct ifinfomsg),
  295. nla_len(nla_peer) - sizeof(struct ifinfomsg),
  296. ifla_policy);
  297. if (err < 0)
  298. return err;
  299. err = veth_validate(peer_tb, NULL);
  300. if (err < 0)
  301. return err;
  302. tbp = peer_tb;
  303. } else
  304. tbp = tb;
  305. if (tbp[IFLA_IFNAME])
  306. nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
  307. else
  308. snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
  309. peer = rtnl_create_link(dev_net(dev), ifname, &veth_link_ops, tbp);
  310. if (IS_ERR(peer))
  311. return PTR_ERR(peer);
  312. if (tbp[IFLA_ADDRESS] == NULL)
  313. random_ether_addr(peer->dev_addr);
  314. err = register_netdevice(peer);
  315. if (err < 0)
  316. goto err_register_peer;
  317. netif_carrier_off(peer);
  318. /*
  319. * register dev last
  320. *
  321. * note, that since we've registered new device the dev's name
  322. * should be re-allocated
  323. */
  324. if (tb[IFLA_ADDRESS] == NULL)
  325. random_ether_addr(dev->dev_addr);
  326. if (tb[IFLA_IFNAME])
  327. nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
  328. else
  329. snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
  330. if (strchr(dev->name, '%')) {
  331. err = dev_alloc_name(dev, dev->name);
  332. if (err < 0)
  333. goto err_alloc_name;
  334. }
  335. err = register_netdevice(dev);
  336. if (err < 0)
  337. goto err_register_dev;
  338. netif_carrier_off(dev);
  339. /*
  340. * tie the deviced together
  341. */
  342. priv = netdev_priv(dev);
  343. priv->dev = dev;
  344. priv->peer = peer;
  345. list_add(&priv->list, &veth_list);
  346. priv = netdev_priv(peer);
  347. priv->dev = peer;
  348. priv->peer = dev;
  349. INIT_LIST_HEAD(&priv->list);
  350. return 0;
  351. err_register_dev:
  352. /* nothing to do */
  353. err_alloc_name:
  354. unregister_netdevice(peer);
  355. return err;
  356. err_register_peer:
  357. free_netdev(peer);
  358. return err;
  359. }
  360. static void veth_dellink(struct net_device *dev)
  361. {
  362. struct veth_priv *priv;
  363. struct net_device *peer;
  364. priv = netdev_priv(dev);
  365. peer = priv->peer;
  366. if (!list_empty(&priv->list))
  367. list_del(&priv->list);
  368. priv = netdev_priv(peer);
  369. if (!list_empty(&priv->list))
  370. list_del(&priv->list);
  371. unregister_netdevice(dev);
  372. unregister_netdevice(peer);
  373. }
  374. static const struct nla_policy veth_policy[VETH_INFO_MAX + 1];
  375. static struct rtnl_link_ops veth_link_ops = {
  376. .kind = DRV_NAME,
  377. .priv_size = sizeof(struct veth_priv),
  378. .setup = veth_setup,
  379. .validate = veth_validate,
  380. .newlink = veth_newlink,
  381. .dellink = veth_dellink,
  382. .policy = veth_policy,
  383. .maxtype = VETH_INFO_MAX,
  384. };
  385. /*
  386. * init/fini
  387. */
  388. static __init int veth_init(void)
  389. {
  390. register_netdevice_notifier(&veth_notifier_block);
  391. return rtnl_link_register(&veth_link_ops);
  392. }
  393. static __exit void veth_exit(void)
  394. {
  395. rtnl_link_unregister(&veth_link_ops);
  396. unregister_netdevice_notifier(&veth_notifier_block);
  397. }
  398. module_init(veth_init);
  399. module_exit(veth_exit);
  400. MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
  401. MODULE_LICENSE("GPL v2");
  402. MODULE_ALIAS_RTNL_LINK(DRV_NAME);