veth.c 9.0 KB

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