veth.c 9.9 KB

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