veth.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 tx_packets;
  26. u64 rx_bytes;
  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. strcpy(info->driver, DRV_NAME);
  60. strcpy(info->version, DRV_VERSION);
  61. strcpy(info->fw_version, "N/A");
  62. }
  63. static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  64. {
  65. switch(stringset) {
  66. case ETH_SS_STATS:
  67. memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
  68. break;
  69. }
  70. }
  71. static int veth_get_sset_count(struct net_device *dev, int sset)
  72. {
  73. switch (sset) {
  74. case ETH_SS_STATS:
  75. return ARRAY_SIZE(ethtool_stats_keys);
  76. default:
  77. return -EOPNOTSUPP;
  78. }
  79. }
  80. static void veth_get_ethtool_stats(struct net_device *dev,
  81. struct ethtool_stats *stats, u64 *data)
  82. {
  83. struct veth_priv *priv;
  84. priv = netdev_priv(dev);
  85. data[0] = priv->peer->ifindex;
  86. }
  87. static const struct ethtool_ops veth_ethtool_ops = {
  88. .get_settings = veth_get_settings,
  89. .get_drvinfo = veth_get_drvinfo,
  90. .get_link = ethtool_op_get_link,
  91. .get_strings = veth_get_strings,
  92. .get_sset_count = veth_get_sset_count,
  93. .get_ethtool_stats = veth_get_ethtool_stats,
  94. };
  95. /*
  96. * xmit
  97. */
  98. static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
  99. {
  100. struct net_device *rcv = NULL;
  101. struct veth_priv *priv, *rcv_priv;
  102. struct veth_net_stats *stats, *rcv_stats;
  103. int length;
  104. priv = netdev_priv(dev);
  105. rcv = priv->peer;
  106. rcv_priv = netdev_priv(rcv);
  107. stats = this_cpu_ptr(priv->stats);
  108. rcv_stats = this_cpu_ptr(rcv_priv->stats);
  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. u64_stats_update_begin(&stats->syncp);
  118. stats->tx_bytes += length;
  119. stats->tx_packets++;
  120. u64_stats_update_end(&stats->syncp);
  121. u64_stats_update_begin(&rcv_stats->syncp);
  122. rcv_stats->rx_bytes += length;
  123. rcv_stats->rx_packets++;
  124. u64_stats_update_end(&rcv_stats->syncp);
  125. return NETDEV_TX_OK;
  126. rx_drop:
  127. u64_stats_update_begin(&rcv_stats->syncp);
  128. rcv_stats->rx_dropped++;
  129. u64_stats_update_end(&rcv_stats->syncp);
  130. return NETDEV_TX_OK;
  131. }
  132. /*
  133. * general routines
  134. */
  135. static struct rtnl_link_stats64 *veth_get_stats64(struct net_device *dev,
  136. struct rtnl_link_stats64 *tot)
  137. {
  138. struct veth_priv *priv = netdev_priv(dev);
  139. int cpu;
  140. for_each_possible_cpu(cpu) {
  141. struct veth_net_stats *stats = per_cpu_ptr(priv->stats, cpu);
  142. u64 rx_packets, rx_bytes, rx_dropped;
  143. u64 tx_packets, tx_bytes;
  144. unsigned int start;
  145. do {
  146. start = u64_stats_fetch_begin_bh(&stats->syncp);
  147. rx_packets = stats->rx_packets;
  148. tx_packets = stats->tx_packets;
  149. rx_bytes = stats->rx_bytes;
  150. tx_bytes = stats->tx_bytes;
  151. rx_dropped = stats->rx_dropped;
  152. } while (u64_stats_fetch_retry_bh(&stats->syncp, start));
  153. tot->rx_packets += rx_packets;
  154. tot->tx_packets += tx_packets;
  155. tot->rx_bytes += rx_bytes;
  156. tot->tx_bytes += tx_bytes;
  157. tot->rx_dropped += rx_dropped;
  158. }
  159. return tot;
  160. }
  161. static int veth_open(struct net_device *dev)
  162. {
  163. struct veth_priv *priv;
  164. priv = netdev_priv(dev);
  165. if (priv->peer == NULL)
  166. return -ENOTCONN;
  167. if (priv->peer->flags & IFF_UP) {
  168. netif_carrier_on(dev);
  169. netif_carrier_on(priv->peer);
  170. }
  171. return 0;
  172. }
  173. static int veth_close(struct net_device *dev)
  174. {
  175. struct veth_priv *priv = netdev_priv(dev);
  176. netif_carrier_off(dev);
  177. netif_carrier_off(priv->peer);
  178. return 0;
  179. }
  180. static int is_valid_veth_mtu(int new_mtu)
  181. {
  182. return new_mtu >= MIN_MTU && new_mtu <= MAX_MTU;
  183. }
  184. static int veth_change_mtu(struct net_device *dev, int new_mtu)
  185. {
  186. if (!is_valid_veth_mtu(new_mtu))
  187. return -EINVAL;
  188. dev->mtu = new_mtu;
  189. return 0;
  190. }
  191. static int veth_dev_init(struct net_device *dev)
  192. {
  193. struct veth_net_stats __percpu *stats;
  194. struct veth_priv *priv;
  195. stats = alloc_percpu(struct veth_net_stats);
  196. if (stats == NULL)
  197. return -ENOMEM;
  198. priv = netdev_priv(dev);
  199. priv->stats = stats;
  200. return 0;
  201. }
  202. static void veth_dev_free(struct net_device *dev)
  203. {
  204. struct veth_priv *priv;
  205. priv = netdev_priv(dev);
  206. free_percpu(priv->stats);
  207. free_netdev(dev);
  208. }
  209. static const struct net_device_ops veth_netdev_ops = {
  210. .ndo_init = veth_dev_init,
  211. .ndo_open = veth_open,
  212. .ndo_stop = veth_close,
  213. .ndo_start_xmit = veth_xmit,
  214. .ndo_change_mtu = veth_change_mtu,
  215. .ndo_get_stats64 = veth_get_stats64,
  216. .ndo_set_mac_address = eth_mac_addr,
  217. };
  218. static void veth_setup(struct net_device *dev)
  219. {
  220. ether_setup(dev);
  221. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  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_NO_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(src_net, 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. random_ether_addr(peer->dev_addr);
  291. err = register_netdevice(peer);
  292. put_net(net);
  293. net = NULL;
  294. if (err < 0)
  295. goto err_register_peer;
  296. netif_carrier_off(peer);
  297. err = rtnl_configure_link(peer, ifmp);
  298. if (err < 0)
  299. goto err_configure_peer;
  300. /*
  301. * register dev last
  302. *
  303. * note, that since we've registered new device the dev's name
  304. * should be re-allocated
  305. */
  306. if (tb[IFLA_ADDRESS] == NULL)
  307. random_ether_addr(dev->dev_addr);
  308. if (tb[IFLA_IFNAME])
  309. nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
  310. else
  311. snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
  312. if (strchr(dev->name, '%')) {
  313. err = dev_alloc_name(dev, dev->name);
  314. if (err < 0)
  315. goto err_alloc_name;
  316. }
  317. err = register_netdevice(dev);
  318. if (err < 0)
  319. goto err_register_dev;
  320. netif_carrier_off(dev);
  321. /*
  322. * tie the deviced together
  323. */
  324. priv = netdev_priv(dev);
  325. priv->peer = peer;
  326. priv = netdev_priv(peer);
  327. priv->peer = dev;
  328. return 0;
  329. err_register_dev:
  330. /* nothing to do */
  331. err_alloc_name:
  332. err_configure_peer:
  333. unregister_netdevice(peer);
  334. return err;
  335. err_register_peer:
  336. free_netdev(peer);
  337. return err;
  338. }
  339. static void veth_dellink(struct net_device *dev, struct list_head *head)
  340. {
  341. struct veth_priv *priv;
  342. struct net_device *peer;
  343. priv = netdev_priv(dev);
  344. peer = priv->peer;
  345. unregister_netdevice_queue(dev, head);
  346. unregister_netdevice_queue(peer, head);
  347. }
  348. static const struct nla_policy veth_policy[VETH_INFO_MAX + 1];
  349. static struct rtnl_link_ops veth_link_ops = {
  350. .kind = DRV_NAME,
  351. .priv_size = sizeof(struct veth_priv),
  352. .setup = veth_setup,
  353. .validate = veth_validate,
  354. .newlink = veth_newlink,
  355. .dellink = veth_dellink,
  356. .policy = veth_policy,
  357. .maxtype = VETH_INFO_MAX,
  358. };
  359. /*
  360. * init/fini
  361. */
  362. static __init int veth_init(void)
  363. {
  364. return rtnl_link_register(&veth_link_ops);
  365. }
  366. static __exit void veth_exit(void)
  367. {
  368. rtnl_link_unregister(&veth_link_ops);
  369. }
  370. module_init(veth_init);
  371. module_exit(veth_exit);
  372. MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
  373. MODULE_LICENSE("GPL v2");
  374. MODULE_ALIAS_RTNL_LINK(DRV_NAME);