veth.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. #define MTU_PAD (ETH_HLEN + 4) /* Max difference between L2 and L3 size MTU */
  22. struct veth_net_stats {
  23. unsigned long rx_packets;
  24. unsigned long tx_packets;
  25. unsigned long rx_bytes;
  26. unsigned long tx_bytes;
  27. unsigned long tx_dropped;
  28. unsigned long rx_dropped;
  29. };
  30. struct veth_priv {
  31. struct net_device *peer;
  32. struct veth_net_stats __percpu *stats;
  33. };
  34. /*
  35. * ethtool interface
  36. */
  37. static struct {
  38. const char string[ETH_GSTRING_LEN];
  39. } ethtool_stats_keys[] = {
  40. { "peer_ifindex" },
  41. };
  42. static int veth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  43. {
  44. cmd->supported = 0;
  45. cmd->advertising = 0;
  46. ethtool_cmd_speed_set(cmd, SPEED_10000);
  47. cmd->duplex = DUPLEX_FULL;
  48. cmd->port = PORT_TP;
  49. cmd->phy_address = 0;
  50. cmd->transceiver = XCVR_INTERNAL;
  51. cmd->autoneg = AUTONEG_DISABLE;
  52. cmd->maxtxpkt = 0;
  53. cmd->maxrxpkt = 0;
  54. return 0;
  55. }
  56. static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  57. {
  58. strcpy(info->driver, DRV_NAME);
  59. strcpy(info->version, DRV_VERSION);
  60. strcpy(info->fw_version, "N/A");
  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. if (!(rcv->flags & IFF_UP))
  109. goto tx_drop;
  110. /* don't change ip_summed == CHECKSUM_PARTIAL, as that
  111. will cause bad checksum on forwarded packets */
  112. if (skb->ip_summed == CHECKSUM_NONE &&
  113. rcv->features & NETIF_F_RXCSUM)
  114. skb->ip_summed = CHECKSUM_UNNECESSARY;
  115. length = skb->len;
  116. if (dev_forward_skb(rcv, skb) != NET_RX_SUCCESS)
  117. goto rx_drop;
  118. stats->tx_bytes += length;
  119. stats->tx_packets++;
  120. rcv_stats->rx_bytes += length;
  121. rcv_stats->rx_packets++;
  122. return NETDEV_TX_OK;
  123. tx_drop:
  124. kfree_skb(skb);
  125. stats->tx_dropped++;
  126. return NETDEV_TX_OK;
  127. rx_drop:
  128. rcv_stats->rx_dropped++;
  129. return NETDEV_TX_OK;
  130. }
  131. /*
  132. * general routines
  133. */
  134. static struct net_device_stats *veth_get_stats(struct net_device *dev)
  135. {
  136. struct veth_priv *priv;
  137. int cpu;
  138. struct veth_net_stats *stats, total = {0};
  139. priv = netdev_priv(dev);
  140. for_each_possible_cpu(cpu) {
  141. stats = per_cpu_ptr(priv->stats, cpu);
  142. total.rx_packets += stats->rx_packets;
  143. total.tx_packets += stats->tx_packets;
  144. total.rx_bytes += stats->rx_bytes;
  145. total.tx_bytes += stats->tx_bytes;
  146. total.tx_dropped += stats->tx_dropped;
  147. total.rx_dropped += stats->rx_dropped;
  148. }
  149. dev->stats.rx_packets = total.rx_packets;
  150. dev->stats.tx_packets = total.tx_packets;
  151. dev->stats.rx_bytes = total.rx_bytes;
  152. dev->stats.tx_bytes = total.tx_bytes;
  153. dev->stats.tx_dropped = total.tx_dropped;
  154. dev->stats.rx_dropped = total.rx_dropped;
  155. return &dev->stats;
  156. }
  157. static int veth_open(struct net_device *dev)
  158. {
  159. struct veth_priv *priv;
  160. priv = netdev_priv(dev);
  161. if (priv->peer == NULL)
  162. return -ENOTCONN;
  163. if (priv->peer->flags & IFF_UP) {
  164. netif_carrier_on(dev);
  165. netif_carrier_on(priv->peer);
  166. }
  167. return 0;
  168. }
  169. static int veth_close(struct net_device *dev)
  170. {
  171. struct veth_priv *priv = netdev_priv(dev);
  172. netif_carrier_off(dev);
  173. netif_carrier_off(priv->peer);
  174. return 0;
  175. }
  176. static int is_valid_veth_mtu(int new_mtu)
  177. {
  178. return new_mtu >= MIN_MTU && new_mtu <= MAX_MTU;
  179. }
  180. static int veth_change_mtu(struct net_device *dev, int new_mtu)
  181. {
  182. if (!is_valid_veth_mtu(new_mtu))
  183. return -EINVAL;
  184. dev->mtu = new_mtu;
  185. return 0;
  186. }
  187. static int veth_dev_init(struct net_device *dev)
  188. {
  189. struct veth_net_stats __percpu *stats;
  190. struct veth_priv *priv;
  191. stats = alloc_percpu(struct veth_net_stats);
  192. if (stats == NULL)
  193. return -ENOMEM;
  194. priv = netdev_priv(dev);
  195. priv->stats = stats;
  196. return 0;
  197. }
  198. static void veth_dev_free(struct net_device *dev)
  199. {
  200. struct veth_priv *priv;
  201. priv = netdev_priv(dev);
  202. free_percpu(priv->stats);
  203. free_netdev(dev);
  204. }
  205. static const struct net_device_ops veth_netdev_ops = {
  206. .ndo_init = veth_dev_init,
  207. .ndo_open = veth_open,
  208. .ndo_stop = veth_close,
  209. .ndo_start_xmit = veth_xmit,
  210. .ndo_change_mtu = veth_change_mtu,
  211. .ndo_get_stats = veth_get_stats,
  212. .ndo_set_mac_address = eth_mac_addr,
  213. };
  214. static void veth_setup(struct net_device *dev)
  215. {
  216. ether_setup(dev);
  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. err = register_netdevice(dev);
  304. if (err < 0)
  305. goto err_register_dev;
  306. netif_carrier_off(dev);
  307. /*
  308. * tie the deviced together
  309. */
  310. priv = netdev_priv(dev);
  311. priv->peer = peer;
  312. priv = netdev_priv(peer);
  313. priv->peer = dev;
  314. return 0;
  315. err_register_dev:
  316. /* nothing to do */
  317. err_configure_peer:
  318. unregister_netdevice(peer);
  319. return err;
  320. err_register_peer:
  321. free_netdev(peer);
  322. return err;
  323. }
  324. static void veth_dellink(struct net_device *dev, struct list_head *head)
  325. {
  326. struct veth_priv *priv;
  327. struct net_device *peer;
  328. priv = netdev_priv(dev);
  329. peer = priv->peer;
  330. unregister_netdevice_queue(dev, head);
  331. unregister_netdevice_queue(peer, head);
  332. }
  333. static const struct nla_policy veth_policy[VETH_INFO_MAX + 1];
  334. static struct rtnl_link_ops veth_link_ops = {
  335. .kind = DRV_NAME,
  336. .priv_size = sizeof(struct veth_priv),
  337. .setup = veth_setup,
  338. .validate = veth_validate,
  339. .newlink = veth_newlink,
  340. .dellink = veth_dellink,
  341. .policy = veth_policy,
  342. .maxtype = VETH_INFO_MAX,
  343. };
  344. /*
  345. * init/fini
  346. */
  347. static __init int veth_init(void)
  348. {
  349. return rtnl_link_register(&veth_link_ops);
  350. }
  351. static __exit void veth_exit(void)
  352. {
  353. rtnl_link_unregister(&veth_link_ops);
  354. }
  355. module_init(veth_init);
  356. module_exit(veth_exit);
  357. MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
  358. MODULE_LICENSE("GPL v2");
  359. MODULE_ALIAS_RTNL_LINK(DRV_NAME);