veth.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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/ethtool.h>
  12. #include <linux/etherdevice.h>
  13. #include <net/dst.h>
  14. #include <net/xfrm.h>
  15. #include <linux/veth.h>
  16. #define DRV_NAME "veth"
  17. #define DRV_VERSION "1.0"
  18. struct veth_net_stats {
  19. unsigned long rx_packets;
  20. unsigned long tx_packets;
  21. unsigned long rx_bytes;
  22. unsigned long tx_bytes;
  23. unsigned long tx_dropped;
  24. };
  25. struct veth_priv {
  26. struct net_device *peer;
  27. struct veth_net_stats *stats;
  28. unsigned ip_summed;
  29. };
  30. /*
  31. * ethtool interface
  32. */
  33. static struct {
  34. const char string[ETH_GSTRING_LEN];
  35. } ethtool_stats_keys[] = {
  36. { "peer_ifindex" },
  37. };
  38. static int veth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  39. {
  40. cmd->supported = 0;
  41. cmd->advertising = 0;
  42. cmd->speed = SPEED_10000;
  43. cmd->duplex = DUPLEX_FULL;
  44. cmd->port = PORT_TP;
  45. cmd->phy_address = 0;
  46. cmd->transceiver = XCVR_INTERNAL;
  47. cmd->autoneg = AUTONEG_DISABLE;
  48. cmd->maxtxpkt = 0;
  49. cmd->maxrxpkt = 0;
  50. return 0;
  51. }
  52. static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  53. {
  54. strcpy(info->driver, DRV_NAME);
  55. strcpy(info->version, DRV_VERSION);
  56. strcpy(info->fw_version, "N/A");
  57. }
  58. static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  59. {
  60. switch(stringset) {
  61. case ETH_SS_STATS:
  62. memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
  63. break;
  64. }
  65. }
  66. static int veth_get_sset_count(struct net_device *dev, int sset)
  67. {
  68. switch (sset) {
  69. case ETH_SS_STATS:
  70. return ARRAY_SIZE(ethtool_stats_keys);
  71. default:
  72. return -EOPNOTSUPP;
  73. }
  74. }
  75. static void veth_get_ethtool_stats(struct net_device *dev,
  76. struct ethtool_stats *stats, u64 *data)
  77. {
  78. struct veth_priv *priv;
  79. priv = netdev_priv(dev);
  80. data[0] = priv->peer->ifindex;
  81. }
  82. static u32 veth_get_rx_csum(struct net_device *dev)
  83. {
  84. struct veth_priv *priv;
  85. priv = netdev_priv(dev);
  86. return priv->ip_summed == CHECKSUM_UNNECESSARY;
  87. }
  88. static int veth_set_rx_csum(struct net_device *dev, u32 data)
  89. {
  90. struct veth_priv *priv;
  91. priv = netdev_priv(dev);
  92. priv->ip_summed = data ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
  93. return 0;
  94. }
  95. static u32 veth_get_tx_csum(struct net_device *dev)
  96. {
  97. return (dev->features & NETIF_F_NO_CSUM) != 0;
  98. }
  99. static int veth_set_tx_csum(struct net_device *dev, u32 data)
  100. {
  101. if (data)
  102. dev->features |= NETIF_F_NO_CSUM;
  103. else
  104. dev->features &= ~NETIF_F_NO_CSUM;
  105. return 0;
  106. }
  107. static struct ethtool_ops veth_ethtool_ops = {
  108. .get_settings = veth_get_settings,
  109. .get_drvinfo = veth_get_drvinfo,
  110. .get_link = ethtool_op_get_link,
  111. .get_rx_csum = veth_get_rx_csum,
  112. .set_rx_csum = veth_set_rx_csum,
  113. .get_tx_csum = veth_get_tx_csum,
  114. .set_tx_csum = veth_set_tx_csum,
  115. .get_sg = ethtool_op_get_sg,
  116. .set_sg = ethtool_op_set_sg,
  117. .get_strings = veth_get_strings,
  118. .get_sset_count = veth_get_sset_count,
  119. .get_ethtool_stats = veth_get_ethtool_stats,
  120. };
  121. /*
  122. * xmit
  123. */
  124. static int veth_xmit(struct sk_buff *skb, struct net_device *dev)
  125. {
  126. struct net_device *rcv = NULL;
  127. struct veth_priv *priv, *rcv_priv;
  128. struct veth_net_stats *stats;
  129. int length, cpu;
  130. skb_orphan(skb);
  131. priv = netdev_priv(dev);
  132. rcv = priv->peer;
  133. rcv_priv = netdev_priv(rcv);
  134. cpu = smp_processor_id();
  135. stats = per_cpu_ptr(priv->stats, cpu);
  136. if (!(rcv->flags & IFF_UP))
  137. goto outf;
  138. skb->pkt_type = PACKET_HOST;
  139. skb->protocol = eth_type_trans(skb, rcv);
  140. if (dev->features & NETIF_F_NO_CSUM)
  141. skb->ip_summed = rcv_priv->ip_summed;
  142. dst_release(skb->dst);
  143. skb->dst = NULL;
  144. skb->mark = 0;
  145. secpath_reset(skb);
  146. nf_reset(skb);
  147. length = skb->len;
  148. stats->tx_bytes += length;
  149. stats->tx_packets++;
  150. stats = per_cpu_ptr(rcv_priv->stats, cpu);
  151. stats->rx_bytes += length;
  152. stats->rx_packets++;
  153. netif_rx(skb);
  154. return 0;
  155. outf:
  156. kfree_skb(skb);
  157. stats->tx_dropped++;
  158. return 0;
  159. }
  160. /*
  161. * general routines
  162. */
  163. static struct net_device_stats *veth_get_stats(struct net_device *dev)
  164. {
  165. struct veth_priv *priv;
  166. struct net_device_stats *dev_stats;
  167. int cpu;
  168. struct veth_net_stats *stats;
  169. priv = netdev_priv(dev);
  170. dev_stats = &dev->stats;
  171. dev_stats->rx_packets = 0;
  172. dev_stats->tx_packets = 0;
  173. dev_stats->rx_bytes = 0;
  174. dev_stats->tx_bytes = 0;
  175. dev_stats->tx_dropped = 0;
  176. for_each_online_cpu(cpu) {
  177. stats = per_cpu_ptr(priv->stats, cpu);
  178. dev_stats->rx_packets += stats->rx_packets;
  179. dev_stats->tx_packets += stats->tx_packets;
  180. dev_stats->rx_bytes += stats->rx_bytes;
  181. dev_stats->tx_bytes += stats->tx_bytes;
  182. dev_stats->tx_dropped += stats->tx_dropped;
  183. }
  184. return dev_stats;
  185. }
  186. static int veth_open(struct net_device *dev)
  187. {
  188. struct veth_priv *priv;
  189. priv = netdev_priv(dev);
  190. if (priv->peer == NULL)
  191. return -ENOTCONN;
  192. if (priv->peer->flags & IFF_UP) {
  193. netif_carrier_on(dev);
  194. netif_carrier_on(priv->peer);
  195. }
  196. return 0;
  197. }
  198. static int veth_dev_init(struct net_device *dev)
  199. {
  200. struct veth_net_stats *stats;
  201. struct veth_priv *priv;
  202. stats = alloc_percpu(struct veth_net_stats);
  203. if (stats == NULL)
  204. return -ENOMEM;
  205. priv = netdev_priv(dev);
  206. priv->stats = stats;
  207. return 0;
  208. }
  209. static void veth_dev_free(struct net_device *dev)
  210. {
  211. struct veth_priv *priv;
  212. priv = netdev_priv(dev);
  213. free_percpu(priv->stats);
  214. free_netdev(dev);
  215. }
  216. static const struct net_device_ops veth_netdev_ops = {
  217. .ndo_init = veth_dev_init,
  218. .ndo_open = veth_open,
  219. .ndo_start_xmit = veth_xmit,
  220. .ndo_get_stats = veth_get_stats,
  221. };
  222. static void veth_setup(struct net_device *dev)
  223. {
  224. ether_setup(dev);
  225. dev->netdev_ops = &veth_netdev_ops;
  226. dev->ethtool_ops = &veth_ethtool_ops;
  227. dev->features |= NETIF_F_LLTX;
  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->netdev_ops->ndo_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. priv = netdev_priv(peer);
  344. priv->peer = dev;
  345. return 0;
  346. err_register_dev:
  347. /* nothing to do */
  348. err_alloc_name:
  349. unregister_netdevice(peer);
  350. return err;
  351. err_register_peer:
  352. free_netdev(peer);
  353. return err;
  354. }
  355. static void veth_dellink(struct net_device *dev)
  356. {
  357. struct veth_priv *priv;
  358. struct net_device *peer;
  359. priv = netdev_priv(dev);
  360. peer = priv->peer;
  361. unregister_netdevice(dev);
  362. unregister_netdevice(peer);
  363. }
  364. static const struct nla_policy veth_policy[VETH_INFO_MAX + 1];
  365. static struct rtnl_link_ops veth_link_ops = {
  366. .kind = DRV_NAME,
  367. .priv_size = sizeof(struct veth_priv),
  368. .setup = veth_setup,
  369. .validate = veth_validate,
  370. .newlink = veth_newlink,
  371. .dellink = veth_dellink,
  372. .policy = veth_policy,
  373. .maxtype = VETH_INFO_MAX,
  374. };
  375. /*
  376. * init/fini
  377. */
  378. static __init int veth_init(void)
  379. {
  380. register_netdevice_notifier(&veth_notifier_block);
  381. return rtnl_link_register(&veth_link_ops);
  382. }
  383. static __exit void veth_exit(void)
  384. {
  385. rtnl_link_unregister(&veth_link_ops);
  386. unregister_netdevice_notifier(&veth_notifier_block);
  387. }
  388. module_init(veth_init);
  389. module_exit(veth_exit);
  390. MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
  391. MODULE_LICENSE("GPL v2");
  392. MODULE_ALIAS_RTNL_LINK(DRV_NAME);