veth.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. skb_orphan(skb);
  135. priv = netdev_priv(dev);
  136. rcv = priv->peer;
  137. rcv_priv = netdev_priv(rcv);
  138. cpu = smp_processor_id();
  139. stats = per_cpu_ptr(priv->stats, cpu);
  140. rcv_stats = per_cpu_ptr(rcv_priv->stats, cpu);
  141. if (!(rcv->flags & IFF_UP))
  142. goto tx_drop;
  143. if (skb->len > (rcv->mtu + MTU_PAD))
  144. goto rx_drop;
  145. skb->tstamp.tv64 = 0;
  146. skb->pkt_type = PACKET_HOST;
  147. skb->protocol = eth_type_trans(skb, rcv);
  148. if (dev->features & NETIF_F_NO_CSUM)
  149. skb->ip_summed = rcv_priv->ip_summed;
  150. skb->mark = 0;
  151. secpath_reset(skb);
  152. nf_reset(skb);
  153. length = skb->len;
  154. stats->tx_bytes += length;
  155. stats->tx_packets++;
  156. rcv_stats->rx_bytes += length;
  157. rcv_stats->rx_packets++;
  158. netif_rx(skb);
  159. return NETDEV_TX_OK;
  160. tx_drop:
  161. kfree_skb(skb);
  162. stats->tx_dropped++;
  163. return NETDEV_TX_OK;
  164. rx_drop:
  165. kfree_skb(skb);
  166. rcv_stats->rx_dropped++;
  167. return NETDEV_TX_OK;
  168. }
  169. /*
  170. * general routines
  171. */
  172. static struct net_device_stats *veth_get_stats(struct net_device *dev)
  173. {
  174. struct veth_priv *priv;
  175. struct net_device_stats *dev_stats;
  176. int cpu;
  177. struct veth_net_stats *stats;
  178. priv = netdev_priv(dev);
  179. dev_stats = &dev->stats;
  180. dev_stats->rx_packets = 0;
  181. dev_stats->tx_packets = 0;
  182. dev_stats->rx_bytes = 0;
  183. dev_stats->tx_bytes = 0;
  184. dev_stats->tx_dropped = 0;
  185. dev_stats->rx_dropped = 0;
  186. for_each_online_cpu(cpu) {
  187. stats = per_cpu_ptr(priv->stats, cpu);
  188. dev_stats->rx_packets += stats->rx_packets;
  189. dev_stats->tx_packets += stats->tx_packets;
  190. dev_stats->rx_bytes += stats->rx_bytes;
  191. dev_stats->tx_bytes += stats->tx_bytes;
  192. dev_stats->tx_dropped += stats->tx_dropped;
  193. dev_stats->rx_dropped += stats->rx_dropped;
  194. }
  195. return dev_stats;
  196. }
  197. static int veth_open(struct net_device *dev)
  198. {
  199. struct veth_priv *priv;
  200. priv = netdev_priv(dev);
  201. if (priv->peer == NULL)
  202. return -ENOTCONN;
  203. if (priv->peer->flags & IFF_UP) {
  204. netif_carrier_on(dev);
  205. netif_carrier_on(priv->peer);
  206. }
  207. return 0;
  208. }
  209. static int veth_close(struct net_device *dev)
  210. {
  211. struct veth_priv *priv = netdev_priv(dev);
  212. netif_carrier_off(dev);
  213. netif_carrier_off(priv->peer);
  214. return 0;
  215. }
  216. static int is_valid_veth_mtu(int new_mtu)
  217. {
  218. return (new_mtu >= MIN_MTU && new_mtu <= MAX_MTU);
  219. }
  220. static int veth_change_mtu(struct net_device *dev, int new_mtu)
  221. {
  222. if (!is_valid_veth_mtu(new_mtu))
  223. return -EINVAL;
  224. dev->mtu = new_mtu;
  225. return 0;
  226. }
  227. static int veth_dev_init(struct net_device *dev)
  228. {
  229. struct veth_net_stats *stats;
  230. struct veth_priv *priv;
  231. stats = alloc_percpu(struct veth_net_stats);
  232. if (stats == NULL)
  233. return -ENOMEM;
  234. priv = netdev_priv(dev);
  235. priv->stats = stats;
  236. return 0;
  237. }
  238. static void veth_dev_free(struct net_device *dev)
  239. {
  240. struct veth_priv *priv;
  241. priv = netdev_priv(dev);
  242. free_percpu(priv->stats);
  243. free_netdev(dev);
  244. }
  245. static const struct net_device_ops veth_netdev_ops = {
  246. .ndo_init = veth_dev_init,
  247. .ndo_open = veth_open,
  248. .ndo_stop = veth_close,
  249. .ndo_start_xmit = veth_xmit,
  250. .ndo_change_mtu = veth_change_mtu,
  251. .ndo_get_stats = veth_get_stats,
  252. .ndo_set_mac_address = eth_mac_addr,
  253. };
  254. static void veth_setup(struct net_device *dev)
  255. {
  256. ether_setup(dev);
  257. dev->netdev_ops = &veth_netdev_ops;
  258. dev->ethtool_ops = &veth_ethtool_ops;
  259. dev->features |= NETIF_F_LLTX;
  260. dev->destructor = veth_dev_free;
  261. }
  262. /*
  263. * netlink interface
  264. */
  265. static int veth_validate(struct nlattr *tb[], struct nlattr *data[])
  266. {
  267. if (tb[IFLA_ADDRESS]) {
  268. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  269. return -EINVAL;
  270. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  271. return -EADDRNOTAVAIL;
  272. }
  273. if (tb[IFLA_MTU]) {
  274. if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
  275. return -EINVAL;
  276. }
  277. return 0;
  278. }
  279. static struct rtnl_link_ops veth_link_ops;
  280. static int veth_newlink(struct net *src_net, struct net_device *dev,
  281. struct nlattr *tb[], struct nlattr *data[])
  282. {
  283. int err;
  284. struct net_device *peer;
  285. struct veth_priv *priv;
  286. char ifname[IFNAMSIZ];
  287. struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
  288. struct net *net;
  289. /*
  290. * create and register peer first
  291. *
  292. * struct ifinfomsg is at the head of VETH_INFO_PEER, but we
  293. * skip it since no info from it is useful yet
  294. */
  295. if (data != NULL && data[VETH_INFO_PEER] != NULL) {
  296. struct nlattr *nla_peer;
  297. nla_peer = data[VETH_INFO_PEER];
  298. err = nla_parse(peer_tb, IFLA_MAX,
  299. nla_data(nla_peer) + sizeof(struct ifinfomsg),
  300. nla_len(nla_peer) - sizeof(struct ifinfomsg),
  301. ifla_policy);
  302. if (err < 0)
  303. return err;
  304. err = veth_validate(peer_tb, NULL);
  305. if (err < 0)
  306. return err;
  307. tbp = peer_tb;
  308. } else
  309. tbp = tb;
  310. if (tbp[IFLA_IFNAME])
  311. nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
  312. else
  313. snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
  314. net = rtnl_link_get_net(src_net, tbp);
  315. if (IS_ERR(net))
  316. return PTR_ERR(net);
  317. peer = rtnl_create_link(src_net, net, ifname, &veth_link_ops, tbp);
  318. if (IS_ERR(peer)) {
  319. put_net(net);
  320. return PTR_ERR(peer);
  321. }
  322. if (tbp[IFLA_ADDRESS] == NULL)
  323. random_ether_addr(peer->dev_addr);
  324. err = register_netdevice(peer);
  325. put_net(net);
  326. net = NULL;
  327. if (err < 0)
  328. goto err_register_peer;
  329. netif_carrier_off(peer);
  330. /*
  331. * register dev last
  332. *
  333. * note, that since we've registered new device the dev's name
  334. * should be re-allocated
  335. */
  336. if (tb[IFLA_ADDRESS] == NULL)
  337. random_ether_addr(dev->dev_addr);
  338. if (tb[IFLA_IFNAME])
  339. nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
  340. else
  341. snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
  342. if (strchr(dev->name, '%')) {
  343. err = dev_alloc_name(dev, dev->name);
  344. if (err < 0)
  345. goto err_alloc_name;
  346. }
  347. err = register_netdevice(dev);
  348. if (err < 0)
  349. goto err_register_dev;
  350. netif_carrier_off(dev);
  351. /*
  352. * tie the deviced together
  353. */
  354. priv = netdev_priv(dev);
  355. priv->peer = peer;
  356. priv = netdev_priv(peer);
  357. priv->peer = dev;
  358. return 0;
  359. err_register_dev:
  360. /* nothing to do */
  361. err_alloc_name:
  362. unregister_netdevice(peer);
  363. return err;
  364. err_register_peer:
  365. free_netdev(peer);
  366. return err;
  367. }
  368. static void veth_dellink(struct net_device *dev, struct list_head *head)
  369. {
  370. struct veth_priv *priv;
  371. struct net_device *peer;
  372. priv = netdev_priv(dev);
  373. peer = priv->peer;
  374. unregister_netdevice_queue(dev, head);
  375. unregister_netdevice_queue(peer, head);
  376. }
  377. static const struct nla_policy veth_policy[VETH_INFO_MAX + 1];
  378. static struct rtnl_link_ops veth_link_ops = {
  379. .kind = DRV_NAME,
  380. .priv_size = sizeof(struct veth_priv),
  381. .setup = veth_setup,
  382. .validate = veth_validate,
  383. .newlink = veth_newlink,
  384. .dellink = veth_dellink,
  385. .policy = veth_policy,
  386. .maxtype = VETH_INFO_MAX,
  387. };
  388. /*
  389. * init/fini
  390. */
  391. static __init int veth_init(void)
  392. {
  393. return rtnl_link_register(&veth_link_ops);
  394. }
  395. static __exit void veth_exit(void)
  396. {
  397. rtnl_link_unregister(&veth_link_ops);
  398. }
  399. module_init(veth_init);
  400. module_exit(veth_exit);
  401. MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
  402. MODULE_LICENSE("GPL v2");
  403. MODULE_ALIAS_RTNL_LINK(DRV_NAME);