veth.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. unsigned long rx_packets;
  23. unsigned long tx_packets;
  24. unsigned long rx_bytes;
  25. unsigned long tx_bytes;
  26. unsigned long tx_dropped;
  27. unsigned long 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 net_device_stats *veth_get_stats(struct net_device *dev)
  134. {
  135. struct veth_priv *priv;
  136. int cpu;
  137. struct veth_net_stats *stats, total = {0};
  138. priv = netdev_priv(dev);
  139. for_each_possible_cpu(cpu) {
  140. stats = per_cpu_ptr(priv->stats, cpu);
  141. total.rx_packets += stats->rx_packets;
  142. total.tx_packets += stats->tx_packets;
  143. total.rx_bytes += stats->rx_bytes;
  144. total.tx_bytes += stats->tx_bytes;
  145. total.tx_dropped += stats->tx_dropped;
  146. total.rx_dropped += stats->rx_dropped;
  147. }
  148. dev->stats.rx_packets = total.rx_packets;
  149. dev->stats.tx_packets = total.tx_packets;
  150. dev->stats.rx_bytes = total.rx_bytes;
  151. dev->stats.tx_bytes = total.tx_bytes;
  152. dev->stats.tx_dropped = total.tx_dropped;
  153. dev->stats.rx_dropped = total.rx_dropped;
  154. return &dev->stats;
  155. }
  156. static int veth_open(struct net_device *dev)
  157. {
  158. struct veth_priv *priv;
  159. priv = netdev_priv(dev);
  160. if (priv->peer == NULL)
  161. return -ENOTCONN;
  162. if (priv->peer->flags & IFF_UP) {
  163. netif_carrier_on(dev);
  164. netif_carrier_on(priv->peer);
  165. }
  166. return 0;
  167. }
  168. static int veth_close(struct net_device *dev)
  169. {
  170. struct veth_priv *priv = netdev_priv(dev);
  171. netif_carrier_off(dev);
  172. netif_carrier_off(priv->peer);
  173. return 0;
  174. }
  175. static int is_valid_veth_mtu(int new_mtu)
  176. {
  177. return new_mtu >= MIN_MTU && new_mtu <= MAX_MTU;
  178. }
  179. static int veth_change_mtu(struct net_device *dev, int new_mtu)
  180. {
  181. if (!is_valid_veth_mtu(new_mtu))
  182. return -EINVAL;
  183. dev->mtu = new_mtu;
  184. return 0;
  185. }
  186. static int veth_dev_init(struct net_device *dev)
  187. {
  188. struct veth_net_stats __percpu *stats;
  189. struct veth_priv *priv;
  190. stats = alloc_percpu(struct veth_net_stats);
  191. if (stats == NULL)
  192. return -ENOMEM;
  193. priv = netdev_priv(dev);
  194. priv->stats = stats;
  195. return 0;
  196. }
  197. static void veth_dev_free(struct net_device *dev)
  198. {
  199. struct veth_priv *priv;
  200. priv = netdev_priv(dev);
  201. free_percpu(priv->stats);
  202. free_netdev(dev);
  203. }
  204. static const struct net_device_ops veth_netdev_ops = {
  205. .ndo_init = veth_dev_init,
  206. .ndo_open = veth_open,
  207. .ndo_stop = veth_close,
  208. .ndo_start_xmit = veth_xmit,
  209. .ndo_change_mtu = veth_change_mtu,
  210. .ndo_get_stats = veth_get_stats,
  211. .ndo_set_mac_address = eth_mac_addr,
  212. };
  213. static void veth_setup(struct net_device *dev)
  214. {
  215. ether_setup(dev);
  216. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  217. dev->netdev_ops = &veth_netdev_ops;
  218. dev->ethtool_ops = &veth_ethtool_ops;
  219. dev->features |= NETIF_F_LLTX;
  220. dev->destructor = veth_dev_free;
  221. dev->hw_features = NETIF_F_NO_CSUM | NETIF_F_SG | NETIF_F_RXCSUM;
  222. }
  223. /*
  224. * netlink interface
  225. */
  226. static int veth_validate(struct nlattr *tb[], struct nlattr *data[])
  227. {
  228. if (tb[IFLA_ADDRESS]) {
  229. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  230. return -EINVAL;
  231. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  232. return -EADDRNOTAVAIL;
  233. }
  234. if (tb[IFLA_MTU]) {
  235. if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
  236. return -EINVAL;
  237. }
  238. return 0;
  239. }
  240. static struct rtnl_link_ops veth_link_ops;
  241. static int veth_newlink(struct net *src_net, struct net_device *dev,
  242. struct nlattr *tb[], struct nlattr *data[])
  243. {
  244. int err;
  245. struct net_device *peer;
  246. struct veth_priv *priv;
  247. char ifname[IFNAMSIZ];
  248. struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
  249. struct ifinfomsg *ifmp;
  250. struct net *net;
  251. /*
  252. * create and register peer first
  253. */
  254. if (data != NULL && data[VETH_INFO_PEER] != NULL) {
  255. struct nlattr *nla_peer;
  256. nla_peer = data[VETH_INFO_PEER];
  257. ifmp = nla_data(nla_peer);
  258. err = nla_parse(peer_tb, IFLA_MAX,
  259. nla_data(nla_peer) + sizeof(struct ifinfomsg),
  260. nla_len(nla_peer) - sizeof(struct ifinfomsg),
  261. ifla_policy);
  262. if (err < 0)
  263. return err;
  264. err = veth_validate(peer_tb, NULL);
  265. if (err < 0)
  266. return err;
  267. tbp = peer_tb;
  268. } else {
  269. ifmp = NULL;
  270. tbp = tb;
  271. }
  272. if (tbp[IFLA_IFNAME])
  273. nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
  274. else
  275. snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
  276. net = rtnl_link_get_net(src_net, tbp);
  277. if (IS_ERR(net))
  278. return PTR_ERR(net);
  279. peer = rtnl_create_link(src_net, net, ifname, &veth_link_ops, tbp);
  280. if (IS_ERR(peer)) {
  281. put_net(net);
  282. return PTR_ERR(peer);
  283. }
  284. if (tbp[IFLA_ADDRESS] == NULL)
  285. random_ether_addr(peer->dev_addr);
  286. err = register_netdevice(peer);
  287. put_net(net);
  288. net = NULL;
  289. if (err < 0)
  290. goto err_register_peer;
  291. netif_carrier_off(peer);
  292. err = rtnl_configure_link(peer, ifmp);
  293. if (err < 0)
  294. goto err_configure_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. err_configure_peer:
  328. unregister_netdevice(peer);
  329. return err;
  330. err_register_peer:
  331. free_netdev(peer);
  332. return err;
  333. }
  334. static void veth_dellink(struct net_device *dev, struct list_head *head)
  335. {
  336. struct veth_priv *priv;
  337. struct net_device *peer;
  338. priv = netdev_priv(dev);
  339. peer = priv->peer;
  340. unregister_netdevice_queue(dev, head);
  341. unregister_netdevice_queue(peer, head);
  342. }
  343. static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
  344. [VETH_INFO_PEER] = { .len = sizeof(struct ifinfomsg) },
  345. };
  346. static struct rtnl_link_ops veth_link_ops = {
  347. .kind = DRV_NAME,
  348. .priv_size = sizeof(struct veth_priv),
  349. .setup = veth_setup,
  350. .validate = veth_validate,
  351. .newlink = veth_newlink,
  352. .dellink = veth_dellink,
  353. .policy = veth_policy,
  354. .maxtype = VETH_INFO_MAX,
  355. };
  356. /*
  357. * init/fini
  358. */
  359. static __init int veth_init(void)
  360. {
  361. return rtnl_link_register(&veth_link_ops);
  362. }
  363. static __exit void veth_exit(void)
  364. {
  365. rtnl_link_unregister(&veth_link_ops);
  366. }
  367. module_init(veth_init);
  368. module_exit(veth_exit);
  369. MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
  370. MODULE_LICENSE("GPL v2");
  371. MODULE_ALIAS_RTNL_LINK(DRV_NAME);