veth.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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/slab.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. #define MIN_MTU 68 /* Min L3 MTU */
  20. #define MAX_MTU 65535 /* Max L3 MTU (arbitrary) */
  21. struct veth_net_stats {
  22. u64 rx_packets;
  23. u64 tx_packets;
  24. u64 rx_bytes;
  25. u64 tx_bytes;
  26. u64 tx_dropped;
  27. u64 rx_dropped;
  28. };
  29. struct veth_priv {
  30. struct net_device *peer;
  31. struct veth_net_stats __percpu *stats;
  32. };
  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. ethtool_cmd_speed_set(cmd, 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 const struct ethtool_ops veth_ethtool_ops = {
  86. .get_settings = veth_get_settings,
  87. .get_drvinfo = veth_get_drvinfo,
  88. .get_link = ethtool_op_get_link,
  89. .get_strings = veth_get_strings,
  90. .get_sset_count = veth_get_sset_count,
  91. .get_ethtool_stats = veth_get_ethtool_stats,
  92. };
  93. /*
  94. * xmit
  95. */
  96. static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
  97. {
  98. struct net_device *rcv = NULL;
  99. struct veth_priv *priv, *rcv_priv;
  100. struct veth_net_stats *stats, *rcv_stats;
  101. int length;
  102. priv = netdev_priv(dev);
  103. rcv = priv->peer;
  104. rcv_priv = netdev_priv(rcv);
  105. stats = this_cpu_ptr(priv->stats);
  106. rcv_stats = this_cpu_ptr(rcv_priv->stats);
  107. if (!(rcv->flags & IFF_UP))
  108. goto tx_drop;
  109. /* don't change ip_summed == CHECKSUM_PARTIAL, as that
  110. will cause bad checksum on forwarded packets */
  111. if (skb->ip_summed == CHECKSUM_NONE &&
  112. rcv->features & NETIF_F_RXCSUM)
  113. skb->ip_summed = CHECKSUM_UNNECESSARY;
  114. length = skb->len;
  115. if (dev_forward_skb(rcv, skb) != NET_RX_SUCCESS)
  116. goto rx_drop;
  117. stats->tx_bytes += length;
  118. stats->tx_packets++;
  119. rcv_stats->rx_bytes += length;
  120. rcv_stats->rx_packets++;
  121. return NETDEV_TX_OK;
  122. tx_drop:
  123. kfree_skb(skb);
  124. stats->tx_dropped++;
  125. return NETDEV_TX_OK;
  126. rx_drop:
  127. rcv_stats->rx_dropped++;
  128. return NETDEV_TX_OK;
  129. }
  130. /*
  131. * general routines
  132. */
  133. static struct rtnl_link_stats64 *veth_get_stats64(struct net_device *dev,
  134. struct rtnl_link_stats64 *tot)
  135. {
  136. struct veth_priv *priv;
  137. int cpu;
  138. struct veth_net_stats *stats;
  139. priv = netdev_priv(dev);
  140. for_each_possible_cpu(cpu) {
  141. stats = per_cpu_ptr(priv->stats, cpu);
  142. tot->rx_packets += stats->rx_packets;
  143. tot->tx_packets += stats->tx_packets;
  144. tot->rx_bytes += stats->rx_bytes;
  145. tot->tx_bytes += stats->tx_bytes;
  146. tot->tx_dropped += stats->tx_dropped;
  147. tot->rx_dropped += stats->rx_dropped;
  148. }
  149. return tot;
  150. }
  151. static int veth_open(struct net_device *dev)
  152. {
  153. struct veth_priv *priv;
  154. priv = netdev_priv(dev);
  155. if (priv->peer == NULL)
  156. return -ENOTCONN;
  157. if (priv->peer->flags & IFF_UP) {
  158. netif_carrier_on(dev);
  159. netif_carrier_on(priv->peer);
  160. }
  161. return 0;
  162. }
  163. static int veth_close(struct net_device *dev)
  164. {
  165. struct veth_priv *priv = netdev_priv(dev);
  166. netif_carrier_off(dev);
  167. netif_carrier_off(priv->peer);
  168. return 0;
  169. }
  170. static int is_valid_veth_mtu(int new_mtu)
  171. {
  172. return new_mtu >= MIN_MTU && new_mtu <= MAX_MTU;
  173. }
  174. static int veth_change_mtu(struct net_device *dev, int new_mtu)
  175. {
  176. if (!is_valid_veth_mtu(new_mtu))
  177. return -EINVAL;
  178. dev->mtu = new_mtu;
  179. return 0;
  180. }
  181. static int veth_dev_init(struct net_device *dev)
  182. {
  183. struct veth_net_stats __percpu *stats;
  184. struct veth_priv *priv;
  185. stats = alloc_percpu(struct veth_net_stats);
  186. if (stats == NULL)
  187. return -ENOMEM;
  188. priv = netdev_priv(dev);
  189. priv->stats = stats;
  190. return 0;
  191. }
  192. static void veth_dev_free(struct net_device *dev)
  193. {
  194. struct veth_priv *priv;
  195. priv = netdev_priv(dev);
  196. free_percpu(priv->stats);
  197. free_netdev(dev);
  198. }
  199. static const struct net_device_ops veth_netdev_ops = {
  200. .ndo_init = veth_dev_init,
  201. .ndo_open = veth_open,
  202. .ndo_stop = veth_close,
  203. .ndo_start_xmit = veth_xmit,
  204. .ndo_change_mtu = veth_change_mtu,
  205. .ndo_get_stats64 = veth_get_stats64,
  206. .ndo_set_mac_address = eth_mac_addr,
  207. };
  208. static void veth_setup(struct net_device *dev)
  209. {
  210. ether_setup(dev);
  211. dev->netdev_ops = &veth_netdev_ops;
  212. dev->ethtool_ops = &veth_ethtool_ops;
  213. dev->features |= NETIF_F_LLTX;
  214. dev->destructor = veth_dev_free;
  215. dev->hw_features = NETIF_F_NO_CSUM | NETIF_F_SG | NETIF_F_RXCSUM;
  216. }
  217. /*
  218. * netlink interface
  219. */
  220. static int veth_validate(struct nlattr *tb[], struct nlattr *data[])
  221. {
  222. if (tb[IFLA_ADDRESS]) {
  223. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  224. return -EINVAL;
  225. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  226. return -EADDRNOTAVAIL;
  227. }
  228. if (tb[IFLA_MTU]) {
  229. if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
  230. return -EINVAL;
  231. }
  232. return 0;
  233. }
  234. static struct rtnl_link_ops veth_link_ops;
  235. static int veth_newlink(struct net *src_net, struct net_device *dev,
  236. struct nlattr *tb[], struct nlattr *data[])
  237. {
  238. int err;
  239. struct net_device *peer;
  240. struct veth_priv *priv;
  241. char ifname[IFNAMSIZ];
  242. struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
  243. struct ifinfomsg *ifmp;
  244. struct net *net;
  245. /*
  246. * create and register peer first
  247. */
  248. if (data != NULL && data[VETH_INFO_PEER] != NULL) {
  249. struct nlattr *nla_peer;
  250. nla_peer = data[VETH_INFO_PEER];
  251. ifmp = nla_data(nla_peer);
  252. err = nla_parse(peer_tb, IFLA_MAX,
  253. nla_data(nla_peer) + sizeof(struct ifinfomsg),
  254. nla_len(nla_peer) - sizeof(struct ifinfomsg),
  255. ifla_policy);
  256. if (err < 0)
  257. return err;
  258. err = veth_validate(peer_tb, NULL);
  259. if (err < 0)
  260. return err;
  261. tbp = peer_tb;
  262. } else {
  263. ifmp = NULL;
  264. tbp = tb;
  265. }
  266. if (tbp[IFLA_IFNAME])
  267. nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
  268. else
  269. snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
  270. net = rtnl_link_get_net(src_net, tbp);
  271. if (IS_ERR(net))
  272. return PTR_ERR(net);
  273. peer = rtnl_create_link(src_net, net, ifname, &veth_link_ops, tbp);
  274. if (IS_ERR(peer)) {
  275. put_net(net);
  276. return PTR_ERR(peer);
  277. }
  278. if (tbp[IFLA_ADDRESS] == NULL)
  279. random_ether_addr(peer->dev_addr);
  280. err = register_netdevice(peer);
  281. put_net(net);
  282. net = NULL;
  283. if (err < 0)
  284. goto err_register_peer;
  285. netif_carrier_off(peer);
  286. err = rtnl_configure_link(peer, ifmp);
  287. if (err < 0)
  288. goto err_configure_peer;
  289. /*
  290. * register dev last
  291. *
  292. * note, that since we've registered new device the dev's name
  293. * should be re-allocated
  294. */
  295. if (tb[IFLA_ADDRESS] == NULL)
  296. random_ether_addr(dev->dev_addr);
  297. if (tb[IFLA_IFNAME])
  298. nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
  299. else
  300. snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
  301. if (strchr(dev->name, '%')) {
  302. err = dev_alloc_name(dev, dev->name);
  303. if (err < 0)
  304. goto err_alloc_name;
  305. }
  306. err = register_netdevice(dev);
  307. if (err < 0)
  308. goto err_register_dev;
  309. netif_carrier_off(dev);
  310. /*
  311. * tie the deviced together
  312. */
  313. priv = netdev_priv(dev);
  314. priv->peer = peer;
  315. priv = netdev_priv(peer);
  316. priv->peer = dev;
  317. return 0;
  318. err_register_dev:
  319. /* nothing to do */
  320. err_alloc_name:
  321. err_configure_peer:
  322. unregister_netdevice(peer);
  323. return err;
  324. err_register_peer:
  325. free_netdev(peer);
  326. return err;
  327. }
  328. static void veth_dellink(struct net_device *dev, struct list_head *head)
  329. {
  330. struct veth_priv *priv;
  331. struct net_device *peer;
  332. priv = netdev_priv(dev);
  333. peer = priv->peer;
  334. unregister_netdevice_queue(dev, head);
  335. unregister_netdevice_queue(peer, head);
  336. }
  337. static const struct nla_policy veth_policy[VETH_INFO_MAX + 1];
  338. static struct rtnl_link_ops veth_link_ops = {
  339. .kind = DRV_NAME,
  340. .priv_size = sizeof(struct veth_priv),
  341. .setup = veth_setup,
  342. .validate = veth_validate,
  343. .newlink = veth_newlink,
  344. .dellink = veth_dellink,
  345. .policy = veth_policy,
  346. .maxtype = VETH_INFO_MAX,
  347. };
  348. /*
  349. * init/fini
  350. */
  351. static __init int veth_init(void)
  352. {
  353. return rtnl_link_register(&veth_link_ops);
  354. }
  355. static __exit void veth_exit(void)
  356. {
  357. rtnl_link_unregister(&veth_link_ops);
  358. }
  359. module_init(veth_init);
  360. module_exit(veth_exit);
  361. MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
  362. MODULE_LICENSE("GPL v2");
  363. MODULE_ALIAS_RTNL_LINK(DRV_NAME);