]> Pileus Git - ~andy/linux/blobdiff - drivers/net/tun.c
tun: only queue packets on device
[~andy/linux] / drivers / net / tun.c
index bdbb526eca7b201fbd9927f14883b179ee03143a..a1b2389e6d7fdacce6abb7b3b1ed7d24a2029d05 100644 (file)
@@ -109,15 +109,23 @@ struct tap_filter {
        unsigned char   addr[FLT_EXACT_COUNT][ETH_ALEN];
 };
 
+/* 1024 is probably a high enough limit: modern hypervisors seem to support on
+ * the order of 100-200 CPUs so this leaves us some breathing space if we want
+ * to match a queue per guest CPU.
+ */
+#define MAX_TAP_QUEUES 1024
+
+#define TUN_FLOW_EXPIRE (3 * HZ)
+
 /* A tun_file connects an open character device to a tuntap netdevice. It
  * also contains all socket related strctures (except sock_fprog and tap_filter)
  * to serve as one transmit queue for tuntap device. The sock_fprog and
  * tap_filter were kept in tun_struct since they were used for filtering for the
- * netdevice not for a specific queue (at least I didn't see the reqirement for
+ * netdevice not for a specific queue (at least I didn't see the requirement for
  * this).
  *
  * RCU usage:
- * The tun_file and tun_struct are loosely coupled, the pointer from on to the
+ * The tun_file and tun_struct are loosely coupled, the pointer from one to the
  * other can only be read while rcu_read_lock or rtnl_lock is held.
  */
 struct tun_file {
@@ -129,14 +137,28 @@ struct tun_file {
        struct fasync_struct *fasync;
        /* only used for fasnyc */
        unsigned int flags;
+       u16 queue_index;
 };
 
+struct tun_flow_entry {
+       struct hlist_node hash_link;
+       struct rcu_head rcu;
+       struct tun_struct *tun;
+
+       u32 rxhash;
+       int queue_index;
+       unsigned long updated;
+};
+
+#define TUN_NUM_FLOW_ENTRIES 1024
+
 /* Since the socket were moved to tun_file, to preserve the behavior of persist
- * device, socket fileter, sndbuf and vnet header size were restore when the
+ * device, socket filter, sndbuf and vnet header size were restore when the
  * file were attached to a persist device.
  */
 struct tun_struct {
-       struct tun_file __rcu   *tfile;
+       struct tun_file __rcu   *tfiles[MAX_TAP_QUEUES];
+       unsigned int            numqueues;
        unsigned int            flags;
        kuid_t                  owner;
        kgid_t                  group;
@@ -155,58 +177,328 @@ struct tun_struct {
 #ifdef TUN_DEBUG
        int debug;
 #endif
+       spinlock_t lock;
+       struct kmem_cache *flow_cache;
+       struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
+       struct timer_list flow_gc_timer;
+       unsigned long ageing_time;
 };
 
+static inline u32 tun_hashfn(u32 rxhash)
+{
+       return rxhash & 0x3ff;
+}
+
+static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
+{
+       struct tun_flow_entry *e;
+       struct hlist_node *n;
+
+       hlist_for_each_entry_rcu(e, n, head, hash_link) {
+               if (e->rxhash == rxhash)
+                       return e;
+       }
+       return NULL;
+}
+
+static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
+                                             struct hlist_head *head,
+                                             u32 rxhash, u16 queue_index)
+{
+       struct tun_flow_entry *e = kmem_cache_alloc(tun->flow_cache,
+                                                   GFP_ATOMIC);
+       if (e) {
+               tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
+                         rxhash, queue_index);
+               e->updated = jiffies;
+               e->rxhash = rxhash;
+               e->queue_index = queue_index;
+               e->tun = tun;
+               hlist_add_head_rcu(&e->hash_link, head);
+       }
+       return e;
+}
+
+static void tun_flow_free(struct rcu_head *head)
+{
+       struct tun_flow_entry *e
+               = container_of(head, struct tun_flow_entry, rcu);
+       kmem_cache_free(e->tun->flow_cache, e);
+}
+
+static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
+{
+       tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
+                 e->rxhash, e->queue_index);
+       hlist_del_rcu(&e->hash_link);
+       call_rcu(&e->rcu, tun_flow_free);
+}
+
+static void tun_flow_flush(struct tun_struct *tun)
+{
+       int i;
+
+       spin_lock_bh(&tun->lock);
+       for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
+               struct tun_flow_entry *e;
+               struct hlist_node *h, *n;
+
+               hlist_for_each_entry_safe(e, h, n, &tun->flows[i], hash_link)
+                       tun_flow_delete(tun, e);
+       }
+       spin_unlock_bh(&tun->lock);
+}
+
+static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
+{
+       int i;
+
+       spin_lock_bh(&tun->lock);
+       for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
+               struct tun_flow_entry *e;
+               struct hlist_node *h, *n;
+
+               hlist_for_each_entry_safe(e, h, n, &tun->flows[i], hash_link) {
+                       if (e->queue_index == queue_index)
+                               tun_flow_delete(tun, e);
+               }
+       }
+       spin_unlock_bh(&tun->lock);
+}
+
+static void tun_flow_cleanup(unsigned long data)
+{
+       struct tun_struct *tun = (struct tun_struct *)data;
+       unsigned long delay = tun->ageing_time;
+       unsigned long next_timer = jiffies + delay;
+       unsigned long count = 0;
+       int i;
+
+       tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
+
+       spin_lock_bh(&tun->lock);
+       for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
+               struct tun_flow_entry *e;
+               struct hlist_node *h, *n;
+
+               hlist_for_each_entry_safe(e, h, n, &tun->flows[i], hash_link) {
+                       unsigned long this_timer;
+                       count++;
+                       this_timer = e->updated + delay;
+                       if (time_before_eq(this_timer, jiffies))
+                               tun_flow_delete(tun, e);
+                       else if (time_before(this_timer, next_timer))
+                               next_timer = this_timer;
+               }
+       }
+
+       if (count)
+               mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
+       spin_unlock_bh(&tun->lock);
+}
+
+static void tun_flow_update(struct tun_struct *tun, struct sk_buff *skb,
+                           u16 queue_index)
+{
+       struct hlist_head *head;
+       struct tun_flow_entry *e;
+       unsigned long delay = tun->ageing_time;
+       u32 rxhash = skb_get_rxhash(skb);
+
+       if (!rxhash)
+               return;
+       else
+               head = &tun->flows[tun_hashfn(rxhash)];
+
+       rcu_read_lock();
+
+       if (tun->numqueues == 1)
+               goto unlock;
+
+       e = tun_flow_find(head, rxhash);
+       if (likely(e)) {
+               /* TODO: keep queueing to old queue until it's empty? */
+               e->queue_index = queue_index;
+               e->updated = jiffies;
+       } else {
+               spin_lock_bh(&tun->lock);
+               if (!tun_flow_find(head, rxhash))
+                       tun_flow_create(tun, head, rxhash, queue_index);
+
+               if (!timer_pending(&tun->flow_gc_timer))
+                       mod_timer(&tun->flow_gc_timer,
+                                 round_jiffies_up(jiffies + delay));
+               spin_unlock_bh(&tun->lock);
+       }
+
+unlock:
+       rcu_read_unlock();
+}
+
+/* We try to identify a flow through its rxhash first. The reason that
+ * we do not check rxq no. is becuase some cards(e.g 82599), chooses
+ * the rxq based on the txq where the last packet of the flow comes. As
+ * the userspace application move between processors, we may get a
+ * different rxq no. here. If we could not get rxhash, then we would
+ * hope the rxq no. may help here.
+ */
+static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb)
+{
+       struct tun_struct *tun = netdev_priv(dev);
+       struct tun_flow_entry *e;
+       u32 txq = 0;
+       u32 numqueues = 0;
+
+       rcu_read_lock();
+       numqueues = tun->numqueues;
+
+       txq = skb_get_rxhash(skb);
+       if (txq) {
+               e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
+               if (e)
+                       txq = e->queue_index;
+               else
+                       /* use multiply and shift instead of expensive divide */
+                       txq = ((u64)txq * numqueues) >> 32;
+       } else if (likely(skb_rx_queue_recorded(skb))) {
+               txq = skb_get_rx_queue(skb);
+               while (unlikely(txq >= numqueues))
+                       txq -= numqueues;
+       }
+
+       rcu_read_unlock();
+       return txq;
+}
+
+static inline bool tun_not_capable(struct tun_struct *tun)
+{
+       const struct cred *cred = current_cred();
+       struct net *net = dev_net(tun->dev);
+
+       return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
+                 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
+               !ns_capable(net->user_ns, CAP_NET_ADMIN);
+}
+
+static void tun_set_real_num_queues(struct tun_struct *tun)
+{
+       netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
+       netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
+}
+
+static void __tun_detach(struct tun_file *tfile, bool clean)
+{
+       struct tun_file *ntfile;
+       struct tun_struct *tun;
+       struct net_device *dev;
+
+       tun = rcu_dereference_protected(tfile->tun,
+                                       lockdep_rtnl_is_held());
+       if (tun) {
+               u16 index = tfile->queue_index;
+               BUG_ON(index >= tun->numqueues);
+               dev = tun->dev;
+
+               rcu_assign_pointer(tun->tfiles[index],
+                                  tun->tfiles[tun->numqueues - 1]);
+               rcu_assign_pointer(tfile->tun, NULL);
+               ntfile = rcu_dereference_protected(tun->tfiles[index],
+                                                  lockdep_rtnl_is_held());
+               ntfile->queue_index = index;
+
+               --tun->numqueues;
+               sock_put(&tfile->sk);
+
+               synchronize_net();
+               tun_flow_delete_by_queue(tun, tun->numqueues + 1);
+               /* Drop read queue */
+               skb_queue_purge(&tfile->sk.sk_receive_queue);
+               tun_set_real_num_queues(tun);
+
+               if (tun->numqueues == 0 && !(tun->flags & TUN_PERSIST))
+                       if (dev->reg_state == NETREG_REGISTERED)
+                               unregister_netdevice(dev);
+       }
+
+       if (clean) {
+               BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
+                                &tfile->socket.flags));
+               sk_release_kernel(&tfile->sk);
+       }
+}
+
+static void tun_detach(struct tun_file *tfile, bool clean)
+{
+       rtnl_lock();
+       __tun_detach(tfile, clean);
+       rtnl_unlock();
+}
+
+static void tun_detach_all(struct net_device *dev)
+{
+       struct tun_struct *tun = netdev_priv(dev);
+       struct tun_file *tfile;
+       int i, n = tun->numqueues;
+
+       for (i = 0; i < n; i++) {
+               tfile = rcu_dereference_protected(tun->tfiles[i],
+                                                 lockdep_rtnl_is_held());
+               BUG_ON(!tfile);
+               wake_up_all(&tfile->wq.wait);
+               rcu_assign_pointer(tfile->tun, NULL);
+               --tun->numqueues;
+       }
+       BUG_ON(tun->numqueues != 0);
+
+       synchronize_net();
+       for (i = 0; i < n; i++) {
+               tfile = rcu_dereference_protected(tun->tfiles[i],
+                                                 lockdep_rtnl_is_held());
+               /* Drop read queue */
+               skb_queue_purge(&tfile->sk.sk_receive_queue);
+               sock_put(&tfile->sk);
+       }
+}
+
 static int tun_attach(struct tun_struct *tun, struct file *file)
 {
        struct tun_file *tfile = file->private_data;
        int err;
 
-       ASSERT_RTNL();
-
-       netif_tx_lock_bh(tun->dev);
-
        err = -EINVAL;
-       if (tfile->tun)
+       if (rcu_dereference_protected(tfile->tun, lockdep_rtnl_is_held()))
                goto out;
 
        err = -EBUSY;
-       if (tun->tfile)
+       if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1)
+               goto out;
+
+       err = -E2BIG;
+       if (tun->numqueues == MAX_TAP_QUEUES)
                goto out;
 
        err = 0;
 
-       /* Re-attach filter when attaching to a persist device */
+       /* Re-attach the filter to presist device */
        if (tun->filter_attached == true) {
                err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
                if (!err)
                        goto out;
        }
+       tfile->queue_index = tun->numqueues;
        rcu_assign_pointer(tfile->tun, tun);
-       tfile->socket.sk->sk_sndbuf = tun->sndbuf;
-       rcu_assign_pointer(tun->tfile, tfile);
-       netif_carrier_on(tun->dev);
+       rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
        sock_hold(&tfile->sk);
+       tun->numqueues++;
 
-out:
-       netif_tx_unlock_bh(tun->dev);
-       return err;
-}
+       tun_set_real_num_queues(tun);
 
-static void __tun_detach(struct tun_struct *tun)
-{
-       struct tun_file *tfile = rcu_dereference_protected(tun->tfile,
-                                                       lockdep_rtnl_is_held());
-       /* Detach from net device */
-       netif_carrier_off(tun->dev);
-       rcu_assign_pointer(tun->tfile, NULL);
-       if (tfile) {
-               rcu_assign_pointer(tfile->tun, NULL);
+       /* device is allowed to go away first, so no need to hold extra
+        * refcnt.
+        */
 
-               synchronize_net();
-               /* Drop read queue */
-               skb_queue_purge(&tfile->socket.sk->sk_receive_queue);
-       }
+out:
+       return err;
 }
 
 static struct tun_struct *__tun_get(struct tun_file *tfile)
@@ -349,30 +641,20 @@ static const struct ethtool_ops tun_ethtool_ops;
 /* Net device detach from fd. */
 static void tun_net_uninit(struct net_device *dev)
 {
-       struct tun_struct *tun = netdev_priv(dev);
-       struct tun_file *tfile = rcu_dereference_protected(tun->tfile,
-                                                       lockdep_rtnl_is_held());
-
-       /* Inform the methods they need to stop using the dev.
-        */
-       if (tfile) {
-               wake_up_all(&tfile->wq.wait);
-               __tun_detach(tun);
-               synchronize_net();
-       }
+       tun_detach_all(dev);
 }
 
 /* Net device open. */
 static int tun_net_open(struct net_device *dev)
 {
-       netif_start_queue(dev);
+       netif_tx_start_all_queues(dev);
        return 0;
 }
 
 /* Net device close. */
 static int tun_net_close(struct net_device *dev)
 {
-       netif_stop_queue(dev);
+       netif_tx_stop_all_queues(dev);
        return 0;
 }
 
@@ -380,16 +662,20 @@ static int tun_net_close(struct net_device *dev)
 static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
 {
        struct tun_struct *tun = netdev_priv(dev);
+       int txq = skb->queue_mapping;
        struct tun_file *tfile;
 
        rcu_read_lock();
-       tfile = rcu_dereference(tun->tfile);
+       tfile = rcu_dereference(tun->tfiles[txq]);
+
        /* Drop packet if interface is not attached */
-       if (!tfile)
+       if (txq >= tun->numqueues)
                goto drop;
 
        tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
 
+       BUG_ON(!tfile);
+
        /* Drop if the filter does not like it.
         * This is a noop if the filter is disabled.
         * Filter can be enabled only for the TAP devices. */
@@ -400,22 +686,12 @@ static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
            sk_filter(tfile->socket.sk, skb))
                goto drop;
 
+       /* Limit the number of packets queued by dividing txq length with the
+        * number of queues.
+        */
        if (skb_queue_len(&tfile->socket.sk->sk_receive_queue)
-           >= dev->tx_queue_len) {
-               if (!(tun->flags & TUN_ONE_QUEUE)) {
-                       /* Normal queueing mode. */
-                       /* Packet scheduler handles dropping of further packets. */
-                       netif_stop_queue(dev);
-
-                       /* We won't see all dropped packets individually, so overrun
-                        * error is more appropriate. */
-                       dev->stats.tx_fifo_errors++;
-               } else {
-                       /* Single queue mode.
-                        * Driver handles dropping of all packets itself. */
-                       goto drop;
-               }
-       }
+                         >= dev->tx_queue_len / tun->numqueues)
+               goto drop;
 
        /* Orphan the skb - required as we might hang on to it
         * for indefinite time. */
@@ -437,6 +713,7 @@ static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
 
 drop:
        dev->stats.tx_dropped++;
+       skb_tx_error(skb);
        kfree_skb(skb);
        rcu_read_unlock();
        return NETDEV_TX_OK;
@@ -494,6 +771,7 @@ static const struct net_device_ops tun_netdev_ops = {
        .ndo_start_xmit         = tun_net_xmit,
        .ndo_change_mtu         = tun_net_change_mtu,
        .ndo_fix_features       = tun_net_fix_features,
+       .ndo_select_queue       = tun_select_queue,
 #ifdef CONFIG_NET_POLL_CONTROLLER
        .ndo_poll_controller    = tun_poll_controller,
 #endif
@@ -509,11 +787,43 @@ static const struct net_device_ops tap_netdev_ops = {
        .ndo_set_rx_mode        = tun_net_mclist,
        .ndo_set_mac_address    = eth_mac_addr,
        .ndo_validate_addr      = eth_validate_addr,
+       .ndo_select_queue       = tun_select_queue,
 #ifdef CONFIG_NET_POLL_CONTROLLER
        .ndo_poll_controller    = tun_poll_controller,
 #endif
 };
 
+static int tun_flow_init(struct tun_struct *tun)
+{
+       int i;
+
+       tun->flow_cache = kmem_cache_create("tun_flow_cache",
+                                           sizeof(struct tun_flow_entry), 0, 0,
+                                           NULL);
+       if (!tun->flow_cache)
+               return -ENOMEM;
+
+       for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
+               INIT_HLIST_HEAD(&tun->flows[i]);
+
+       tun->ageing_time = TUN_FLOW_EXPIRE;
+       setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
+       mod_timer(&tun->flow_gc_timer,
+                 round_jiffies_up(jiffies + tun->ageing_time));
+
+       return 0;
+}
+
+static void tun_flow_uninit(struct tun_struct *tun)
+{
+       del_timer_sync(&tun->flow_gc_timer);
+       tun_flow_flush(tun);
+
+       /* Wait for completion of call_rcu()'s */
+       rcu_barrier();
+       kmem_cache_destroy(tun->flow_cache);
+}
+
 /* Initialize net device. */
 static void tun_net_init(struct net_device *dev)
 {
@@ -550,7 +860,7 @@ static void tun_net_init(struct net_device *dev)
 /* Character device part */
 
 /* Poll */
-static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
+static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
 {
        struct tun_file *tfile = file->private_data;
        struct tun_struct *tun = __tun_get(tfile);
@@ -856,6 +1166,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
        tun->dev->stats.rx_packets++;
        tun->dev->stats.rx_bytes += len;
 
+       tun_flow_update(tun, skb, tfile->queue_index);
        return total_len;
 }
 
@@ -969,7 +1280,7 @@ static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
        struct sk_buff *skb;
        ssize_t ret = 0;
 
-       tun_debug(KERN_INFO, tun, "tun_chr_read\n");
+       tun_debug(KERN_INFO, tun, "tun_do_read\n");
 
        if (unlikely(!noblock))
                add_wait_queue(&tfile->wq.wait, &wait);
@@ -995,7 +1306,6 @@ static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
                        schedule();
                        continue;
                }
-               netif_wake_queue(tun->dev);
 
                ret = tun_put_user(tun, tfile, skb, iv, len);
                kfree_skb(skb);
@@ -1033,6 +1343,14 @@ out:
        return ret;
 }
 
+static void tun_free_netdev(struct net_device *dev)
+{
+       struct tun_struct *tun = netdev_priv(dev);
+
+       tun_flow_uninit(tun);
+       free_netdev(dev);
+}
+
 static void tun_setup(struct net_device *dev)
 {
        struct tun_struct *tun = netdev_priv(dev);
@@ -1041,7 +1359,7 @@ static void tun_setup(struct net_device *dev)
        tun->group = INVALID_GID;
 
        dev->ethtool_ops = &tun_ethtool_ops;
-       dev->destructor = free_netdev;
+       dev->destructor = tun_free_netdev;
 }
 
 /* Trivial set of netlink ops to allow deleting tun or tap
@@ -1150,12 +1468,18 @@ static int tun_flags(struct tun_struct *tun)
        if (tun->flags & TUN_NO_PI)
                flags |= IFF_NO_PI;
 
+       /* This flag has no real effect.  We track the value for backwards
+        * compatibility.
+        */
        if (tun->flags & TUN_ONE_QUEUE)
                flags |= IFF_ONE_QUEUE;
 
        if (tun->flags & TUN_VNET_HDR)
                flags |= IFF_VNET_HDR;
 
+       if (tun->flags & TUN_TAP_MQ)
+               flags |= IFF_MULTI_QUEUE;
+
        return flags;
 }
 
@@ -1199,8 +1523,6 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
 
        dev = __dev_get_by_name(net, ifr->ifr_name);
        if (dev) {
-               const struct cred *cred = current_cred();
-
                if (ifr->ifr_flags & IFF_TUN_EXCL)
                        return -EBUSY;
                if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
@@ -1210,9 +1532,7 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
                else
                        return -EINVAL;
 
-               if (((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
-                    (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
-                   !capable(CAP_NET_ADMIN))
+               if (tun_not_capable(tun))
                        return -EPERM;
                err = security_tun_dev_attach(tfile->socket.sk);
                if (err < 0)
@@ -1226,7 +1546,7 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
                char *name;
                unsigned long flags = 0;
 
-               if (!capable(CAP_NET_ADMIN))
+               if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
                        return -EPERM;
                err = security_tun_dev_create();
                if (err < 0)
@@ -1247,8 +1567,9 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
                if (*ifr->ifr_name)
                        name = ifr->ifr_name;
 
-               dev = alloc_netdev(sizeof(struct tun_struct), name,
-                                  tun_setup);
+               dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
+                                      tun_setup,
+                                      MAX_TAP_QUEUES, MAX_TAP_QUEUES);
                if (!dev)
                        return -ENOMEM;
 
@@ -1264,14 +1585,23 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
                tun->filter_attached = false;
                tun->sndbuf = tfile->socket.sk->sk_sndbuf;
 
+               spin_lock_init(&tun->lock);
+
                security_tun_dev_post_create(&tfile->sk);
 
                tun_net_init(dev);
 
+               if (tun_flow_init(tun))
+                       goto err_free_dev;
+
                dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
                        TUN_USER_FEATURES;
                dev->features = dev->hw_features;
 
+               err = tun_attach(tun, file);
+               if (err < 0)
+                       goto err_free_dev;
+
                err = register_netdevice(tun->dev);
                if (err < 0)
                        goto err_free_dev;
@@ -1281,9 +1611,7 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
                    device_create_file(&tun->dev->dev, &dev_attr_group))
                        pr_err("Failed to create tun sysfs files\n");
 
-               err = tun_attach(tun, file);
-               if (err < 0)
-                       goto failed;
+               netif_carrier_on(tun->dev);
        }
 
        tun_debug(KERN_INFO, tun, "tun_set_iff\n");
@@ -1293,6 +1621,9 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
        else
                tun->flags &= ~TUN_NO_PI;
 
+       /* This flag has no real effect.  We track the value for backwards
+        * compatibility.
+        */
        if (ifr->ifr_flags & IFF_ONE_QUEUE)
                tun->flags |= TUN_ONE_QUEUE;
        else
@@ -1303,22 +1634,26 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
        else
                tun->flags &= ~TUN_VNET_HDR;
 
+       if (ifr->ifr_flags & IFF_MULTI_QUEUE)
+               tun->flags |= TUN_TAP_MQ;
+       else
+               tun->flags &= ~TUN_TAP_MQ;
+
        /* Make sure persistent devices do not get stuck in
         * xoff state.
         */
        if (netif_running(tun->dev))
-               netif_wake_queue(tun->dev);
+               netif_tx_wake_all_queues(tun->dev);
 
        strcpy(ifr->ifr_name, tun->dev->name);
        return 0;
 
  err_free_dev:
        free_netdev(dev);
- failed:
        return err;
 }
 
-static int tun_get_iff(struct net *net, struct tun_struct *tun,
+static void tun_get_iff(struct net *net, struct tun_struct *tun,
                       struct ifreq *ifr)
 {
        tun_debug(KERN_INFO, tun, "tun_get_iff\n");
@@ -1327,7 +1662,6 @@ static int tun_get_iff(struct net *net, struct tun_struct *tun,
 
        ifr->ifr_flags = tun_flags(tun);
 
-       return 0;
 }
 
 /* This is like a cut-down ethtool ops, except done via tun fd so no
@@ -1369,6 +1703,85 @@ static int set_offload(struct tun_struct *tun, unsigned long arg)
        return 0;
 }
 
+static void tun_detach_filter(struct tun_struct *tun, int n)
+{
+       int i;
+       struct tun_file *tfile;
+
+       for (i = 0; i < n; i++) {
+               tfile = rcu_dereference_protected(tun->tfiles[i],
+                                                 lockdep_rtnl_is_held());
+               sk_detach_filter(tfile->socket.sk);
+       }
+
+       tun->filter_attached = false;
+}
+
+static int tun_attach_filter(struct tun_struct *tun)
+{
+       int i, ret = 0;
+       struct tun_file *tfile;
+
+       for (i = 0; i < tun->numqueues; i++) {
+               tfile = rcu_dereference_protected(tun->tfiles[i],
+                                                 lockdep_rtnl_is_held());
+               ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
+               if (ret) {
+                       tun_detach_filter(tun, i);
+                       return ret;
+               }
+       }
+
+       tun->filter_attached = true;
+       return ret;
+}
+
+static void tun_set_sndbuf(struct tun_struct *tun)
+{
+       struct tun_file *tfile;
+       int i;
+
+       for (i = 0; i < tun->numqueues; i++) {
+               tfile = rcu_dereference_protected(tun->tfiles[i],
+                                               lockdep_rtnl_is_held());
+               tfile->socket.sk->sk_sndbuf = tun->sndbuf;
+       }
+}
+
+static int tun_set_queue(struct file *file, struct ifreq *ifr)
+{
+       struct tun_file *tfile = file->private_data;
+       struct tun_struct *tun;
+       struct net_device *dev;
+       int ret = 0;
+
+       rtnl_lock();
+
+       if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
+               dev = __dev_get_by_name(tfile->net, ifr->ifr_name);
+               if (!dev) {
+                       ret = -EINVAL;
+                       goto unlock;
+               }
+
+               tun = netdev_priv(dev);
+               if (dev->netdev_ops != &tap_netdev_ops &&
+                       dev->netdev_ops != &tun_netdev_ops)
+                       ret = -EINVAL;
+               else if (tun_not_capable(tun))
+                       ret = -EPERM;
+               else
+                       ret = tun_attach(tun, file);
+       } else if (ifr->ifr_flags & IFF_DETACH_QUEUE)
+               __tun_detach(tfile, false);
+       else
+               ret = -EINVAL;
+
+unlock:
+       rtnl_unlock();
+       return ret;
+}
+
 static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                            unsigned long arg, int ifreq_len)
 {
@@ -1382,7 +1795,7 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
        int vnet_hdr_sz;
        int ret;
 
-       if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) {
+       if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
                if (copy_from_user(&ifr, argp, ifreq_len))
                        return -EFAULT;
        } else {
@@ -1393,10 +1806,12 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                 * This is needed because we never checked for invalid flags on
                 * TUNSETIFF. */
                return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
-                               IFF_VNET_HDR,
+                               IFF_VNET_HDR | IFF_MULTI_QUEUE,
                                (unsigned int __user*)argp);
-       }
+       } else if (cmd == TUNSETQUEUE)
+               return tun_set_queue(file, &ifr);
 
+       ret = 0;
        rtnl_lock();
 
        tun = __tun_get(tfile);
@@ -1422,9 +1837,7 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
        ret = 0;
        switch (cmd) {
        case TUNGETIFF:
-               ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
-               if (ret)
-                       break;
+               tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
 
                if (copy_to_user(argp, &ifr, ifreq_len))
                        ret = -EFAULT;
@@ -1537,7 +1950,8 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                        break;
                }
 
-               tun->sndbuf = tfile->socket.sk->sk_sndbuf = sndbuf;
+               tun->sndbuf = sndbuf;
+               tun_set_sndbuf(tun);
                break;
 
        case TUNGETVNETHDRSZ:
@@ -1568,9 +1982,7 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
                        break;
 
-               ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
-               if (!ret)
-                       tun->filter_attached = true;
+               ret = tun_attach_filter(tun);
                break;
 
        case TUNDETACHFILTER:
@@ -1578,9 +1990,8 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
                ret = -EINVAL;
                if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
                        break;
-               ret = sk_detach_filter(tfile->socket.sk);
-               if (!ret)
-                       tun->filter_attached = false;
+               ret = 0;
+               tun_detach_filter(tun, tun->numqueues);
                break;
 
        default:
@@ -1685,37 +2096,9 @@ static int tun_chr_open(struct inode *inode, struct file * file)
 static int tun_chr_close(struct inode *inode, struct file *file)
 {
        struct tun_file *tfile = file->private_data;
-       struct tun_struct *tun;
        struct net *net = tfile->net;
 
-       rtnl_lock();
-
-       tun = rcu_dereference_protected(tfile->tun, lockdep_rtnl_is_held());
-       if (tun) {
-               struct net_device *dev = tun->dev;
-
-               tun_debug(KERN_INFO, tun, "tun_chr_close\n");
-
-               __tun_detach(tun);
-
-               synchronize_net();
-
-               /* If desirable, unregister the netdevice. */
-               if (!(tun->flags & TUN_PERSIST)) {
-                       if (dev->reg_state == NETREG_REGISTERED)
-                               unregister_netdevice(dev);
-               }
-
-               /* drop the reference that netdevice holds */
-               sock_put(&tfile->sk);
-       }
-
-       rtnl_unlock();
-
-       /* drop the reference that file holds */
-       BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
-                        &tfile->socket.flags));
-       sk_release_kernel(&tfile->sk);
+       tun_detach(tfile, true);
        put_net(net);
 
        return 0;