veth.c 9.7 KB

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