veth.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. unsigned ip_summed;
  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. cmd->speed = 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 u32 veth_get_rx_csum(struct net_device *dev)
  88. {
  89. struct veth_priv *priv;
  90. priv = netdev_priv(dev);
  91. return priv->ip_summed == CHECKSUM_UNNECESSARY;
  92. }
  93. static int veth_set_rx_csum(struct net_device *dev, u32 data)
  94. {
  95. struct veth_priv *priv;
  96. priv = netdev_priv(dev);
  97. priv->ip_summed = data ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
  98. return 0;
  99. }
  100. static u32 veth_get_tx_csum(struct net_device *dev)
  101. {
  102. return (dev->features & NETIF_F_NO_CSUM) != 0;
  103. }
  104. static int veth_set_tx_csum(struct net_device *dev, u32 data)
  105. {
  106. if (data)
  107. dev->features |= NETIF_F_NO_CSUM;
  108. else
  109. dev->features &= ~NETIF_F_NO_CSUM;
  110. return 0;
  111. }
  112. static const struct ethtool_ops veth_ethtool_ops = {
  113. .get_settings = veth_get_settings,
  114. .get_drvinfo = veth_get_drvinfo,
  115. .get_link = ethtool_op_get_link,
  116. .get_rx_csum = veth_get_rx_csum,
  117. .set_rx_csum = veth_set_rx_csum,
  118. .get_tx_csum = veth_get_tx_csum,
  119. .set_tx_csum = veth_set_tx_csum,
  120. .get_sg = ethtool_op_get_sg,
  121. .set_sg = ethtool_op_set_sg,
  122. .get_strings = veth_get_strings,
  123. .get_sset_count = veth_get_sset_count,
  124. .get_ethtool_stats = veth_get_ethtool_stats,
  125. };
  126. /*
  127. * xmit
  128. */
  129. static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
  130. {
  131. struct net_device *rcv = NULL;
  132. struct veth_priv *priv, *rcv_priv;
  133. struct veth_net_stats *stats, *rcv_stats;
  134. int length;
  135. priv = netdev_priv(dev);
  136. rcv = priv->peer;
  137. rcv_priv = netdev_priv(rcv);
  138. stats = this_cpu_ptr(priv->stats);
  139. rcv_stats = this_cpu_ptr(rcv_priv->stats);
  140. if (!(rcv->flags & IFF_UP))
  141. goto tx_drop;
  142. /* don't change ip_summed == CHECKSUM_PARTIAL, as that
  143. will cause bad checksum on forwarded packets */
  144. if (skb->ip_summed == CHECKSUM_NONE)
  145. skb->ip_summed = rcv_priv->ip_summed;
  146. length = skb->len;
  147. if (dev_forward_skb(rcv, skb) != NET_RX_SUCCESS)
  148. goto rx_drop;
  149. stats->tx_bytes += length;
  150. stats->tx_packets++;
  151. rcv_stats->rx_bytes += length;
  152. rcv_stats->rx_packets++;
  153. return NETDEV_TX_OK;
  154. tx_drop:
  155. kfree_skb(skb);
  156. stats->tx_dropped++;
  157. return NETDEV_TX_OK;
  158. rx_drop:
  159. rcv_stats->rx_dropped++;
  160. return NETDEV_TX_OK;
  161. }
  162. /*
  163. * general routines
  164. */
  165. static struct net_device_stats *veth_get_stats(struct net_device *dev)
  166. {
  167. struct veth_priv *priv;
  168. int cpu;
  169. struct veth_net_stats *stats, total = {0};
  170. priv = netdev_priv(dev);
  171. for_each_possible_cpu(cpu) {
  172. stats = per_cpu_ptr(priv->stats, cpu);
  173. total.rx_packets += stats->rx_packets;
  174. total.tx_packets += stats->tx_packets;
  175. total.rx_bytes += stats->rx_bytes;
  176. total.tx_bytes += stats->tx_bytes;
  177. total.tx_dropped += stats->tx_dropped;
  178. total.rx_dropped += stats->rx_dropped;
  179. }
  180. dev->stats.rx_packets = total.rx_packets;
  181. dev->stats.tx_packets = total.tx_packets;
  182. dev->stats.rx_bytes = total.rx_bytes;
  183. dev->stats.tx_bytes = total.tx_bytes;
  184. dev->stats.tx_dropped = total.tx_dropped;
  185. dev->stats.rx_dropped = total.rx_dropped;
  186. return &dev->stats;
  187. }
  188. static int veth_open(struct net_device *dev)
  189. {
  190. struct veth_priv *priv;
  191. priv = netdev_priv(dev);
  192. if (priv->peer == NULL)
  193. return -ENOTCONN;
  194. if (priv->peer->flags & IFF_UP) {
  195. netif_carrier_on(dev);
  196. netif_carrier_on(priv->peer);
  197. }
  198. return 0;
  199. }
  200. static int veth_close(struct net_device *dev)
  201. {
  202. struct veth_priv *priv = netdev_priv(dev);
  203. netif_carrier_off(dev);
  204. netif_carrier_off(priv->peer);
  205. return 0;
  206. }
  207. static int is_valid_veth_mtu(int new_mtu)
  208. {
  209. return new_mtu >= MIN_MTU && new_mtu <= MAX_MTU;
  210. }
  211. static int veth_change_mtu(struct net_device *dev, int new_mtu)
  212. {
  213. if (!is_valid_veth_mtu(new_mtu))
  214. return -EINVAL;
  215. dev->mtu = new_mtu;
  216. return 0;
  217. }
  218. static int veth_dev_init(struct net_device *dev)
  219. {
  220. struct veth_net_stats __percpu *stats;
  221. struct veth_priv *priv;
  222. stats = alloc_percpu(struct veth_net_stats);
  223. if (stats == NULL)
  224. return -ENOMEM;
  225. priv = netdev_priv(dev);
  226. priv->stats = stats;
  227. return 0;
  228. }
  229. static void veth_dev_free(struct net_device *dev)
  230. {
  231. struct veth_priv *priv;
  232. priv = netdev_priv(dev);
  233. free_percpu(priv->stats);
  234. free_netdev(dev);
  235. }
  236. static const struct net_device_ops veth_netdev_ops = {
  237. .ndo_init = veth_dev_init,
  238. .ndo_open = veth_open,
  239. .ndo_stop = veth_close,
  240. .ndo_start_xmit = veth_xmit,
  241. .ndo_change_mtu = veth_change_mtu,
  242. .ndo_get_stats = veth_get_stats,
  243. .ndo_set_mac_address = eth_mac_addr,
  244. };
  245. static void veth_setup(struct net_device *dev)
  246. {
  247. ether_setup(dev);
  248. dev->netdev_ops = &veth_netdev_ops;
  249. dev->ethtool_ops = &veth_ethtool_ops;
  250. dev->features |= NETIF_F_LLTX;
  251. dev->destructor = veth_dev_free;
  252. }
  253. /*
  254. * netlink interface
  255. */
  256. static int veth_validate(struct nlattr *tb[], struct nlattr *data[])
  257. {
  258. if (tb[IFLA_ADDRESS]) {
  259. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  260. return -EINVAL;
  261. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  262. return -EADDRNOTAVAIL;
  263. }
  264. if (tb[IFLA_MTU]) {
  265. if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
  266. return -EINVAL;
  267. }
  268. return 0;
  269. }
  270. static struct rtnl_link_ops veth_link_ops;
  271. static int veth_newlink(struct net *src_net, struct net_device *dev,
  272. struct nlattr *tb[], struct nlattr *data[])
  273. {
  274. int err;
  275. struct net_device *peer;
  276. struct veth_priv *priv;
  277. char ifname[IFNAMSIZ];
  278. struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
  279. struct ifinfomsg *ifmp;
  280. struct net *net;
  281. /*
  282. * create and register peer first
  283. */
  284. if (data != NULL && data[VETH_INFO_PEER] != NULL) {
  285. struct nlattr *nla_peer;
  286. nla_peer = data[VETH_INFO_PEER];
  287. ifmp = nla_data(nla_peer);
  288. err = nla_parse(peer_tb, IFLA_MAX,
  289. nla_data(nla_peer) + sizeof(struct ifinfomsg),
  290. nla_len(nla_peer) - sizeof(struct ifinfomsg),
  291. ifla_policy);
  292. if (err < 0)
  293. return err;
  294. err = veth_validate(peer_tb, NULL);
  295. if (err < 0)
  296. return err;
  297. tbp = peer_tb;
  298. } else {
  299. ifmp = NULL;
  300. tbp = tb;
  301. }
  302. if (tbp[IFLA_IFNAME])
  303. nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
  304. else
  305. snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
  306. net = rtnl_link_get_net(src_net, tbp);
  307. if (IS_ERR(net))
  308. return PTR_ERR(net);
  309. peer = rtnl_create_link(src_net, net, ifname, &veth_link_ops, tbp);
  310. if (IS_ERR(peer)) {
  311. put_net(net);
  312. return PTR_ERR(peer);
  313. }
  314. if (tbp[IFLA_ADDRESS] == NULL)
  315. random_ether_addr(peer->dev_addr);
  316. err = register_netdevice(peer);
  317. put_net(net);
  318. net = NULL;
  319. if (err < 0)
  320. goto err_register_peer;
  321. netif_carrier_off(peer);
  322. err = rtnl_configure_link(peer, ifmp);
  323. if (err < 0)
  324. goto err_configure_peer;
  325. /*
  326. * register dev last
  327. *
  328. * note, that since we've registered new device the dev's name
  329. * should be re-allocated
  330. */
  331. if (tb[IFLA_ADDRESS] == NULL)
  332. random_ether_addr(dev->dev_addr);
  333. err = register_netdevice(dev);
  334. if (err < 0)
  335. goto err_register_dev;
  336. netif_carrier_off(dev);
  337. /*
  338. * tie the deviced together
  339. */
  340. priv = netdev_priv(dev);
  341. priv->peer = peer;
  342. priv = netdev_priv(peer);
  343. priv->peer = dev;
  344. return 0;
  345. err_register_dev:
  346. /* nothing to do */
  347. err_configure_peer:
  348. unregister_netdevice(peer);
  349. return err;
  350. err_register_peer:
  351. free_netdev(peer);
  352. return err;
  353. }
  354. static void veth_dellink(struct net_device *dev, struct list_head *head)
  355. {
  356. struct veth_priv *priv;
  357. struct net_device *peer;
  358. priv = netdev_priv(dev);
  359. peer = priv->peer;
  360. unregister_netdevice_queue(dev, head);
  361. unregister_netdevice_queue(peer, head);
  362. }
  363. static const struct nla_policy veth_policy[VETH_INFO_MAX + 1];
  364. static struct rtnl_link_ops veth_link_ops = {
  365. .kind = DRV_NAME,
  366. .priv_size = sizeof(struct veth_priv),
  367. .setup = veth_setup,
  368. .validate = veth_validate,
  369. .newlink = veth_newlink,
  370. .dellink = veth_dellink,
  371. .policy = veth_policy,
  372. .maxtype = VETH_INFO_MAX,
  373. };
  374. /*
  375. * init/fini
  376. */
  377. static __init int veth_init(void)
  378. {
  379. return rtnl_link_register(&veth_link_ops);
  380. }
  381. static __exit void veth_exit(void)
  382. {
  383. rtnl_link_unregister(&veth_link_ops);
  384. }
  385. module_init(veth_init);
  386. module_exit(veth_exit);
  387. MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
  388. MODULE_LICENSE("GPL v2");
  389. MODULE_ALIAS_RTNL_LINK(DRV_NAME);