veth.c 9.7 KB

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