/* drivers/net/ifb.c: The purpose of this driver is to provide a device that allows for sharing of resources: 1) qdiscs/policies that are per device as opposed to system wide. ifb allows for a device which can be redirected to thus providing an impression of sharing. 2) Allows for queueing incoming traffic for shaping instead of dropping. The original concept is based on what is known as the IMQ driver initially written by Martin Devera, later rewritten by Patrick McHardy and then maintained by Andre Correa. You need the tc action mirror or redirect to feed this device packets. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Authors: Jamal Hadi Salim (2005) */ #include #include #include #include #include #include #include #include #define TX_Q_LIMIT 32 struct ifb_q_private { struct tasklet_struct ifb_tasklet; struct sk_buff_head rq; struct u64_stats_sync syncp; u64 rx_packets; u64 rx_bytes; u64 rx_dropped; }; struct ifb_private { struct ifb_q_private __percpu *q; }; static int numifbs = 2; static void ri_tasklet(unsigned long _dev) { struct net_device *dev = (struct net_device *)_dev; struct ifb_private *p = netdev_priv(dev); struct ifb_q_private *qp; struct netdev_queue *txq; struct sk_buff *skb; struct sk_buff_head tq; __skb_queue_head_init(&tq); txq = netdev_get_tx_queue(dev, raw_smp_processor_id()); qp = per_cpu_ptr(p->q, raw_smp_processor_id()); if (!__netif_tx_trylock(txq)) { tasklet_schedule(&qp->ifb_tasklet); return; } skb_queue_splice_tail_init(&qp->rq, &tq); if (netif_tx_queue_stopped(txq)) netif_tx_wake_queue(txq); __netif_tx_unlock(txq); while ((skb = __skb_dequeue(&tq)) != NULL) { u32 from = G_TC_FROM(skb->tc_verd); skb->tc_verd = 0; skb->tc_verd = SET_TC_NCLS(skb->tc_verd); u64_stats_update_begin(&qp->syncp); txq->tx_packets++; txq->tx_bytes += skb->len; rcu_read_lock(); skb->dev = dev_get_by_index_rcu(&init_net, skb->skb_iif); if (!skb->dev) { rcu_read_unlock(); txq->tx_dropped++; u64_stats_update_end(&qp->syncp); dev_kfree_skb(skb); continue; } rcu_read_unlock(); u64_stats_update_end(&qp->syncp); skb->skb_iif = dev->ifindex; if (from & AT_EGRESS) { dev_queue_xmit(skb); } else if (from & AT_INGRESS) { skb_pull(skb, skb->dev->hard_header_len); netif_rx(skb); } else BUG(); } } static netdev_tx_t ifb_xmit(struct sk_buff *skb, struct net_device *dev) { struct ifb_private *p = netdev_priv(dev); struct ifb_q_private *qp = per_cpu_ptr(p->q, skb_get_queue_mapping(skb)); u32 from = G_TC_FROM(skb->tc_verd); u64_stats_update_begin(&qp->syncp); qp->rx_packets++; qp->rx_bytes += skb->len; if (!(from & (AT_INGRESS|AT_EGRESS)) || !skb->skb_iif) { qp->rx_dropped++; u64_stats_update_end(&qp->syncp); dev_kfree_skb(skb); return NETDEV_TX_OK; } u64_stats_update_end(&qp->syncp); __skb_queue_tail(&qp->rq, skb); if (skb_queue_len(&qp->rq) == 1) tasklet_schedule(&qp->ifb_tasklet); if (skb_queue_len(&qp->rq) >= dev->tx_queue_len) netif_stop_queue(dev); return NETDEV_TX_OK; } static int ifb_close(struct net_device *dev) { struct ifb_private *p = netdev_priv(dev); struct ifb_q_private *qp; int cpu; for_each_possible_cpu(cpu) { qp = per_cpu_ptr(p->q, cpu); tasklet_kill(&qp->ifb_tasklet); netif_tx_stop_queue(netdev_get_tx_queue(dev, cpu)); __skb_queue_purge(&qp->rq); } return 0; } static int ifb_open(struct net_device *dev) { int cpu; for_each_possible_cpu(cpu) netif_tx_start_queue(netdev_get_tx_queue(dev, cpu)); return 0; } static int ifb_init(struct net_device *dev) { struct ifb_private *p = netdev_priv(dev); struct ifb_q_private *q; int cpu; p->q = alloc_percpu(struct ifb_q_private); if (!p->q) return -ENOMEM; for_each_possible_cpu(cpu) { q = per_cpu_ptr(p->q, cpu); tasklet_init(&q->ifb_tasklet, ri_tasklet, (unsigned long)dev); __skb_queue_head_init(&q->rq); } return 0; } static void ifb_uninit(struct net_device *dev) { struct ifb_private *p = netdev_priv(dev); free_percpu(p->q); } static u16 ifb_select_queue(struct net_device *dev, struct sk_buff *skb) { return smp_processor_id(); } static struct rtnl_link_stats64 *ifb_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) { struct ifb_private *p = netdev_priv(dev); struct ifb_q_private *q; struct netdev_queue *txq; int cpu; u64 rx_packets, rx_bytes, rx_dropped; u64 tx_packets, tx_bytes, tx_dropped; unsigned int start; for_each_possible_cpu(cpu) { q = per_cpu_ptr(p->q, cpu); txq = netdev_get_tx_queue(dev, cpu); do { start = u64_stats_fetch_begin_bh(&q->syncp); rx_packets = q->rx_packets; rx_bytes = q->rx_bytes; rx_dropped = q->rx_dropped; tx_packets = txq->tx_packets; tx_bytes = txq->tx_bytes; tx_dropped = txq->tx_dropped; } while (u64_stats_fetch_retry_bh(&q->syncp, start)); stats->rx_packets += rx_packets; stats->rx_bytes += rx_bytes; stats->rx_dropped += rx_dropped; stats->tx_packets += tx_packets; stats->tx_bytes += tx_bytes; stats->tx_dropped += tx_dropped; } return stats; } static const struct net_device_ops ifb_netdev_ops = { .ndo_init = ifb_init, .ndo_uninit = ifb_uninit, .ndo_open = ifb_open, .ndo_stop = ifb_close, .ndo_start_xmit = ifb_xmit, .ndo_validate_addr = eth_validate_addr, .ndo_select_queue = ifb_select_queue, .ndo_get_stats64 = ifb_get_stats64, }; static void ifb_setup(struct net_device *dev) { /* Initialize the device structure. */ dev->destructor = free_netdev; dev->netdev_ops = &ifb_netdev_ops; /* Fill in device structure with ethernet-generic values. */ ether_setup(dev); dev->tx_queue_len = TX_Q_LIMIT; dev->flags |= IFF_NOARP; dev->flags &= ~IFF_MULTICAST; dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; random_ether_addr(dev->dev_addr); } static int ifb_validate(struct nlattr *tb[], struct nlattr *data[]) { if (tb[IFLA_ADDRESS]) { if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) return -EINVAL; if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) return -EADDRNOTAVAIL; } return 0; } static struct rtnl_link_ops ifb_link_ops __read_mostly = { .kind = "ifb", .priv_size = sizeof(struct ifb_private), .setup = ifb_setup, .validate = ifb_validate, }; /* Number of ifb devices to be set up by this module. */ module_param(numifbs, int, 0); MODULE_PARM_DESC(numifbs, "Number of ifb devices"); static int __init ifb_init_one(int index) { struct net_device *dev_ifb; int err; dev_ifb = alloc_netdev_mq(sizeof(struct ifb_private), "ifb%d", ifb_setup, num_possible_cpus()); if (!dev_ifb) return -ENOMEM; err = dev_alloc_name(dev_ifb, dev_ifb->name); if (err < 0) goto err; dev_ifb->rtnl_link_ops = &ifb_link_ops; err = register_netdevice(dev_ifb); if (err < 0) goto err; return 0; err: free_netdev(dev_ifb); return err; } static int __init ifb_init_module(void) { int i, err; rtnl_lock(); err = __rtnl_link_register(&ifb_link_ops); for (i = 0; i < numifbs && !err; i++) err = ifb_init_one(i); if (err) __rtnl_link_unregister(&ifb_link_ops); rtnl_unlock(); return err; } static void __exit ifb_cleanup_module(void) { rtnl_link_unregister(&ifb_link_ops); } module_init(ifb_init_module); module_exit(ifb_cleanup_module); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Jamal Hadi Salim"); MODULE_ALIAS_RTNL_LINK("ifb");