blob: d7856a8f589a7135625051e52737f3461a3eb92c [file] [log] [blame]
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001#include <linux/etherdevice.h>
2#include <linux/if_macvlan.h>
Basil Gorf09e2242012-05-03 22:55:24 +00003#include <linux/if_vlan.h>
Arnd Bergmann20d29d72010-01-30 12:24:26 +00004#include <linux/interrupt.h>
5#include <linux/nsproxy.h>
6#include <linux/compat.h>
7#include <linux/if_tun.h>
8#include <linux/module.h>
9#include <linux/skbuff.h>
10#include <linux/cache.h>
11#include <linux/sched.h>
12#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Arnd Bergmann20d29d72010-01-30 12:24:26 +000014#include <linux/init.h>
15#include <linux/wait.h>
16#include <linux/cdev.h>
Al Viro40401532012-02-13 03:58:52 +000017#include <linux/idr.h>
Arnd Bergmann20d29d72010-01-30 12:24:26 +000018#include <linux/fs.h>
19
20#include <net/net_namespace.h>
21#include <net/rtnetlink.h>
22#include <net/sock.h>
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +000023#include <linux/virtio_net.h>
Arnd Bergmann20d29d72010-01-30 12:24:26 +000024
25/*
26 * A macvtap queue is the central object of this driver, it connects
27 * an open character device to a macvlan interface. There can be
28 * multiple queues on one interface, which map back to queues
29 * implemented in hardware on the underlying device.
30 *
31 * macvtap_proto is used to allocate queues through the sock allocation
32 * mechanism.
33 *
Arnd Bergmann20d29d72010-01-30 12:24:26 +000034 */
35struct macvtap_queue {
36 struct sock sk;
37 struct socket sock;
Eric Dumazet43815482010-04-29 11:01:49 +000038 struct socket_wq wq;
Michael S. Tsirkin55afbd02010-04-29 13:50:48 +030039 int vnet_hdr_sz;
Eric Dumazet13707f92011-01-26 19:28:23 +000040 struct macvlan_dev __rcu *vlan;
Arnd Bergmann20d29d72010-01-30 12:24:26 +000041 struct file *file;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +000042 unsigned int flags;
Jason Wang376b1aa2013-06-05 23:54:38 +000043 u16 queue_index;
Jason Wang815f2362013-06-05 23:54:39 +000044 bool enabled;
45 struct list_head next;
Arnd Bergmann20d29d72010-01-30 12:24:26 +000046};
47
48static struct proto macvtap_proto = {
49 .name = "macvtap",
50 .owner = THIS_MODULE,
51 .obj_size = sizeof (struct macvtap_queue),
52};
53
54/*
Eric W. Biedermane09eff72011-10-20 04:29:24 +000055 * Variables for dealing with macvtaps device numbers.
Arnd Bergmann20d29d72010-01-30 12:24:26 +000056 */
David S. Miller1ebed712010-07-10 19:25:50 -070057static dev_t macvtap_major;
Eric W. Biedermane09eff72011-10-20 04:29:24 +000058#define MACVTAP_NUM_DEVS (1U << MINORBITS)
59static DEFINE_MUTEX(minor_lock);
60static DEFINE_IDR(minor_idr);
61
Shirley Ma97bc3632011-07-06 12:26:11 +000062#define GOODCOPY_LEN 128
Arnd Bergmann20d29d72010-01-30 12:24:26 +000063static struct class *macvtap_class;
64static struct cdev macvtap_cdev;
65
Arnd Bergmann501c7742010-02-18 05:46:50 +000066static const struct proto_ops macvtap_socket_ops;
67
Arnd Bergmann20d29d72010-01-30 12:24:26 +000068/*
69 * RCU usage:
Arnd Bergmann02df55d2010-02-18 05:45:36 +000070 * The macvtap_queue and the macvlan_dev are loosely coupled, the
71 * pointers from one to the other can only be read while rcu_read_lock
Vlad Yasevich441ac0f2013-06-25 16:04:19 -040072 * or rtnl is held.
Arnd Bergmann20d29d72010-01-30 12:24:26 +000073 *
Arnd Bergmann02df55d2010-02-18 05:45:36 +000074 * Both the file and the macvlan_dev hold a reference on the macvtap_queue
75 * through sock_hold(&q->sk). When the macvlan_dev goes away first,
76 * q->vlan becomes inaccessible. When the files gets closed,
77 * macvtap_get_queue() fails.
Arnd Bergmann20d29d72010-01-30 12:24:26 +000078 *
Arnd Bergmann02df55d2010-02-18 05:45:36 +000079 * There may still be references to the struct sock inside of the
80 * queue from outbound SKBs, but these never reference back to the
81 * file or the dev. The data structure is freed through __sk_free
82 * when both our references and any pending SKBs are gone.
Arnd Bergmann20d29d72010-01-30 12:24:26 +000083 */
Arnd Bergmann20d29d72010-01-30 12:24:26 +000084
Jason Wang815f2362013-06-05 23:54:39 +000085static int macvtap_enable_queue(struct net_device *dev, struct file *file,
Arnd Bergmann20d29d72010-01-30 12:24:26 +000086 struct macvtap_queue *q)
87{
88 struct macvlan_dev *vlan = netdev_priv(dev);
Jason Wang815f2362013-06-05 23:54:39 +000089 int err = -EINVAL;
90
Vlad Yasevich441ac0f2013-06-25 16:04:19 -040091 ASSERT_RTNL();
Jason Wang815f2362013-06-05 23:54:39 +000092
93 if (q->enabled)
94 goto out;
95
96 err = 0;
97 rcu_assign_pointer(vlan->taps[vlan->numvtaps], q);
98 q->queue_index = vlan->numvtaps;
99 q->enabled = true;
100
101 vlan->numvtaps++;
102out:
Jason Wang815f2362013-06-05 23:54:39 +0000103 return err;
104}
105
106static int macvtap_set_queue(struct net_device *dev, struct file *file,
107 struct macvtap_queue *q)
108{
109 struct macvlan_dev *vlan = netdev_priv(dev);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000110 int err = -EBUSY;
111
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400112 rtnl_lock();
Jason Wang815f2362013-06-05 23:54:39 +0000113 if (vlan->numqueues == MAX_MACVTAP_QUEUES)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000114 goto out;
115
116 err = 0;
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000117 rcu_assign_pointer(q->vlan, vlan);
Jason Wang376b1aa2013-06-05 23:54:38 +0000118 rcu_assign_pointer(vlan->taps[vlan->numvtaps], q);
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000119 sock_hold(&q->sk);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000120
121 q->file = file;
Jason Wang376b1aa2013-06-05 23:54:38 +0000122 q->queue_index = vlan->numvtaps;
Jason Wang815f2362013-06-05 23:54:39 +0000123 q->enabled = true;
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000124 file->private_data = q;
Jason Wang815f2362013-06-05 23:54:39 +0000125 list_add_tail(&q->next, &vlan->queue_list);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000126
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000127 vlan->numvtaps++;
Jason Wang815f2362013-06-05 23:54:39 +0000128 vlan->numqueues++;
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000129
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000130out:
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400131 rtnl_unlock();
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000132 return err;
133}
134
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400135static int macvtap_disable_queue(struct macvtap_queue *q)
Jason Wang815f2362013-06-05 23:54:39 +0000136{
137 struct macvlan_dev *vlan;
138 struct macvtap_queue *nq;
139
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400140 ASSERT_RTNL();
Jason Wang815f2362013-06-05 23:54:39 +0000141 if (!q->enabled)
142 return -EINVAL;
143
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400144 vlan = rtnl_dereference(q->vlan);
145
Jason Wang815f2362013-06-05 23:54:39 +0000146 if (vlan) {
147 int index = q->queue_index;
148 BUG_ON(index >= vlan->numvtaps);
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400149 nq = rtnl_dereference(vlan->taps[vlan->numvtaps - 1]);
Jason Wang815f2362013-06-05 23:54:39 +0000150 nq->queue_index = index;
151
152 rcu_assign_pointer(vlan->taps[index], nq);
153 RCU_INIT_POINTER(vlan->taps[vlan->numvtaps - 1], NULL);
154 q->enabled = false;
155
156 vlan->numvtaps--;
157 }
158
159 return 0;
160}
161
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000162/*
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000163 * The file owning the queue got closed, give up both
164 * the reference that the files holds as well as the
165 * one from the macvlan_dev if that still exists.
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000166 *
167 * Using the spinlock makes sure that we don't get
168 * to the queue again after destroying it.
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000169 */
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000170static void macvtap_put_queue(struct macvtap_queue *q)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000171{
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000172 struct macvlan_dev *vlan;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000173
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400174 rtnl_lock();
175 vlan = rtnl_dereference(q->vlan);
176
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000177 if (vlan) {
Jason Wang815f2362013-06-05 23:54:39 +0000178 if (q->enabled)
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400179 BUG_ON(macvtap_disable_queue(q));
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000180
Jason Wang815f2362013-06-05 23:54:39 +0000181 vlan->numqueues--;
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000182 RCU_INIT_POINTER(q->vlan, NULL);
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000183 sock_put(&q->sk);
Jason Wang815f2362013-06-05 23:54:39 +0000184 list_del_init(&q->next);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000185 }
186
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400187 rtnl_unlock();
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000188
189 synchronize_rcu();
190 sock_put(&q->sk);
191}
192
193/*
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000194 * Select a queue based on the rxq of the device on which this packet
195 * arrived. If the incoming device is not mq, calculate a flow hash
196 * to select a queue. If all fails, find the first available queue.
197 * Cache vlan->numvtaps since it can become zero during the execution
198 * of this function.
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000199 */
200static struct macvtap_queue *macvtap_get_queue(struct net_device *dev,
201 struct sk_buff *skb)
202{
203 struct macvlan_dev *vlan = netdev_priv(dev);
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000204 struct macvtap_queue *tap = NULL;
Jason Wang815f2362013-06-05 23:54:39 +0000205 /* Access to taps array is protected by rcu, but access to numvtaps
206 * isn't. Below we use it to lookup a queue, but treat it as a hint
207 * and validate that the result isn't NULL - in case we are
208 * racing against queue removal.
209 */
Jason Wanged0483f2013-06-05 23:54:33 +0000210 int numvtaps = ACCESS_ONCE(vlan->numvtaps);
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000211 __u32 rxq;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000212
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000213 if (!numvtaps)
214 goto out;
215
Krishna Kumaref0002b2011-11-23 22:17:14 +0000216 /* Check if we can use flow to select a queue */
217 rxq = skb_get_rxhash(skb);
218 if (rxq) {
219 tap = rcu_dereference(vlan->taps[rxq % numvtaps]);
Jason Wang376b1aa2013-06-05 23:54:38 +0000220 goto out;
Krishna Kumaref0002b2011-11-23 22:17:14 +0000221 }
222
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000223 if (likely(skb_rx_queue_recorded(skb))) {
224 rxq = skb_get_rx_queue(skb);
225
226 while (unlikely(rxq >= numvtaps))
227 rxq -= numvtaps;
228
229 tap = rcu_dereference(vlan->taps[rxq]);
Jason Wang376b1aa2013-06-05 23:54:38 +0000230 goto out;
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000231 }
232
Jason Wang376b1aa2013-06-05 23:54:38 +0000233 tap = rcu_dereference(vlan->taps[0]);
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000234out:
235 return tap;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000236}
237
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000238/*
239 * The net_device is going away, give up the reference
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000240 * that it holds on all queues and safely set the pointer
241 * from the queues to NULL.
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000242 */
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000243static void macvtap_del_queues(struct net_device *dev)
244{
245 struct macvlan_dev *vlan = netdev_priv(dev);
Jason Wang815f2362013-06-05 23:54:39 +0000246 struct macvtap_queue *q, *tmp, *qlist[MAX_MACVTAP_QUEUES];
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000247 int i, j = 0;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000248
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400249 ASSERT_RTNL();
Jason Wang815f2362013-06-05 23:54:39 +0000250 list_for_each_entry_safe(q, tmp, &vlan->queue_list, next) {
251 list_del_init(&q->next);
Jason Wang376b1aa2013-06-05 23:54:38 +0000252 qlist[j++] = q;
Jason Wang376b1aa2013-06-05 23:54:38 +0000253 RCU_INIT_POINTER(q->vlan, NULL);
Jason Wang815f2362013-06-05 23:54:39 +0000254 if (q->enabled)
255 vlan->numvtaps--;
256 vlan->numqueues--;
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000257 }
Jason Wang815f2362013-06-05 23:54:39 +0000258 for (i = 0; i < vlan->numvtaps; i++)
259 RCU_INIT_POINTER(vlan->taps[i], NULL);
260 BUG_ON(vlan->numvtaps);
261 BUG_ON(vlan->numqueues);
Eric W. Biederman99f34b32011-10-20 04:26:01 +0000262 /* guarantee that any future macvtap_set_queue will fail */
263 vlan->numvtaps = MAX_MACVTAP_QUEUES;
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000264
265 for (--j; j >= 0; j--)
266 sock_put(&qlist[j]->sk);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000267}
268
269/*
270 * Forward happens for data that gets sent from one macvlan
271 * endpoint to another one in bridge mode. We just take
272 * the skb and put it into the receive queue.
273 */
274static int macvtap_forward(struct net_device *dev, struct sk_buff *skb)
275{
276 struct macvtap_queue *q = macvtap_get_queue(dev, skb);
277 if (!q)
Herbert Xu8a357472010-07-21 21:44:31 +0000278 goto drop;
279
280 if (skb_queue_len(&q->sk.sk_receive_queue) >= dev->tx_queue_len)
281 goto drop;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000282
283 skb_queue_tail(&q->sk.sk_receive_queue, skb);
Eric Dumazet4a4771a2010-04-25 22:20:06 +0000284 wake_up_interruptible_poll(sk_sleep(&q->sk), POLLIN | POLLRDNORM | POLLRDBAND);
Herbert Xu8a357472010-07-21 21:44:31 +0000285 return NET_RX_SUCCESS;
286
287drop:
288 kfree_skb(skb);
289 return NET_RX_DROP;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000290}
291
292/*
293 * Receive is for data from the external interface (lowerdev),
294 * in case of macvtap, we can treat that the same way as
295 * forward, which macvlan cannot.
296 */
297static int macvtap_receive(struct sk_buff *skb)
298{
299 skb_push(skb, ETH_HLEN);
300 return macvtap_forward(skb->dev, skb);
301}
302
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000303static int macvtap_get_minor(struct macvlan_dev *vlan)
304{
305 int retval = -ENOMEM;
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000306
307 mutex_lock(&minor_lock);
Tejun Heoec09ebc2013-02-27 17:04:34 -0800308 retval = idr_alloc(&minor_idr, vlan, 1, MACVTAP_NUM_DEVS, GFP_KERNEL);
309 if (retval >= 0) {
310 vlan->minor = retval;
311 } else if (retval == -ENOSPC) {
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000312 printk(KERN_ERR "too many macvtap devices\n");
313 retval = -EINVAL;
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000314 }
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000315 mutex_unlock(&minor_lock);
Tejun Heoec09ebc2013-02-27 17:04:34 -0800316 return retval < 0 ? retval : 0;
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000317}
318
319static void macvtap_free_minor(struct macvlan_dev *vlan)
320{
321 mutex_lock(&minor_lock);
322 if (vlan->minor) {
323 idr_remove(&minor_idr, vlan->minor);
324 vlan->minor = 0;
325 }
326 mutex_unlock(&minor_lock);
327}
328
329static struct net_device *dev_get_by_macvtap_minor(int minor)
330{
331 struct net_device *dev = NULL;
332 struct macvlan_dev *vlan;
333
334 mutex_lock(&minor_lock);
335 vlan = idr_find(&minor_idr, minor);
336 if (vlan) {
337 dev = vlan->dev;
338 dev_hold(dev);
339 }
340 mutex_unlock(&minor_lock);
341 return dev;
342}
343
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000344static int macvtap_newlink(struct net *src_net,
345 struct net_device *dev,
346 struct nlattr *tb[],
347 struct nlattr *data[])
348{
Jason Wang815f2362013-06-05 23:54:39 +0000349 struct macvlan_dev *vlan = netdev_priv(dev);
350 INIT_LIST_HEAD(&vlan->queue_list);
351
Eric W. Biederman9bf19072011-10-20 04:28:46 +0000352 /* Don't put anything that may fail after macvlan_common_newlink
353 * because we can't undo what it does.
354 */
355 return macvlan_common_newlink(src_net, dev, tb, data,
356 macvtap_receive, macvtap_forward);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000357}
358
359static void macvtap_dellink(struct net_device *dev,
360 struct list_head *head)
361{
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000362 macvtap_del_queues(dev);
363 macvlan_dellink(dev, head);
364}
365
Herbert Xu8a357472010-07-21 21:44:31 +0000366static void macvtap_setup(struct net_device *dev)
367{
368 macvlan_common_setup(dev);
369 dev->tx_queue_len = TUN_READQ_SIZE;
370}
371
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000372static struct rtnl_link_ops macvtap_link_ops __read_mostly = {
373 .kind = "macvtap",
Herbert Xu8a357472010-07-21 21:44:31 +0000374 .setup = macvtap_setup,
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000375 .newlink = macvtap_newlink,
376 .dellink = macvtap_dellink,
377};
378
379
380static void macvtap_sock_write_space(struct sock *sk)
381{
Eric Dumazet43815482010-04-29 11:01:49 +0000382 wait_queue_head_t *wqueue;
383
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000384 if (!sock_writeable(sk) ||
385 !test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
386 return;
387
Eric Dumazet43815482010-04-29 11:01:49 +0000388 wqueue = sk_sleep(sk);
389 if (wqueue && waitqueue_active(wqueue))
390 wake_up_interruptible_poll(wqueue, POLLOUT | POLLWRNORM | POLLWRBAND);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000391}
392
Eric W. Biederman2259fef2011-10-20 04:27:24 +0000393static void macvtap_sock_destruct(struct sock *sk)
394{
395 skb_queue_purge(&sk->sk_receive_queue);
396}
397
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000398static int macvtap_open(struct inode *inode, struct file *file)
399{
400 struct net *net = current->nsproxy->net_ns;
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000401 struct net_device *dev = dev_get_by_macvtap_minor(iminor(inode));
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000402 struct macvtap_queue *q;
403 int err;
404
405 err = -ENODEV;
406 if (!dev)
407 goto out;
408
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000409 err = -ENOMEM;
410 q = (struct macvtap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
411 &macvtap_proto);
412 if (!q)
413 goto out;
414
Jason Wangd9a90a32013-06-13 14:23:35 +0800415 RCU_INIT_POINTER(q->sock.wq, &q->wq);
Eric Dumazet43815482010-04-29 11:01:49 +0000416 init_waitqueue_head(&q->wq.wait);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000417 q->sock.type = SOCK_RAW;
418 q->sock.state = SS_CONNECTED;
Arnd Bergmann501c7742010-02-18 05:46:50 +0000419 q->sock.file = file;
420 q->sock.ops = &macvtap_socket_ops;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000421 sock_init_data(&q->sock, &q->sk);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000422 q->sk.sk_write_space = macvtap_sock_write_space;
Eric W. Biederman2259fef2011-10-20 04:27:24 +0000423 q->sk.sk_destruct = macvtap_sock_destruct;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000424 q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
Michael S. Tsirkin55afbd02010-04-29 13:50:48 +0300425 q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000426
Shirley Ma97bc3632011-07-06 12:26:11 +0000427 /*
428 * so far only KVM virtio_net uses macvtap, enable zero copy between
429 * guest kernel and host kernel when lower device supports zerocopy
Eric W. Biederman047af9cf2011-10-20 04:26:39 +0000430 *
431 * The macvlan supports zerocopy iff the lower device supports zero
432 * copy so we don't have to look at the lower device directly.
Shirley Ma97bc3632011-07-06 12:26:11 +0000433 */
Eric W. Biederman047af9cf2011-10-20 04:26:39 +0000434 if ((dev->features & NETIF_F_HIGHDMA) && (dev->features & NETIF_F_SG))
435 sock_set_flag(&q->sk, SOCK_ZEROCOPY);
Shirley Ma97bc3632011-07-06 12:26:11 +0000436
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000437 err = macvtap_set_queue(dev, file, q);
438 if (err)
439 sock_put(&q->sk);
440
441out:
442 if (dev)
443 dev_put(dev);
444
445 return err;
446}
447
448static int macvtap_release(struct inode *inode, struct file *file)
449{
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000450 struct macvtap_queue *q = file->private_data;
451 macvtap_put_queue(q);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000452 return 0;
453}
454
455static unsigned int macvtap_poll(struct file *file, poll_table * wait)
456{
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000457 struct macvtap_queue *q = file->private_data;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000458 unsigned int mask = POLLERR;
459
460 if (!q)
461 goto out;
462
463 mask = 0;
Eric Dumazet43815482010-04-29 11:01:49 +0000464 poll_wait(file, &q->wq.wait, wait);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000465
466 if (!skb_queue_empty(&q->sk.sk_receive_queue))
467 mask |= POLLIN | POLLRDNORM;
468
469 if (sock_writeable(&q->sk) ||
470 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &q->sock.flags) &&
471 sock_writeable(&q->sk)))
472 mask |= POLLOUT | POLLWRNORM;
473
474out:
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000475 return mask;
476}
477
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000478static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,
479 size_t len, size_t linear,
480 int noblock, int *err)
481{
482 struct sk_buff *skb;
483
484 /* Under a page? Don't bother with paged skb. */
485 if (prepad + len < PAGE_SIZE || !linear)
486 linear = len;
487
488 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
489 err);
490 if (!skb)
491 return NULL;
492
493 skb_reserve(skb, prepad);
494 skb_put(skb, linear);
495 skb->data_len = len - linear;
496 skb->len += len - linear;
497
498 return skb;
499}
500
Shirley Ma97bc3632011-07-06 12:26:11 +0000501/* set skb frags from iovec, this can move to core network code for reuse */
502static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
503 int offset, size_t count)
504{
505 int len = iov_length(from, count) - offset;
506 int copy = skb_headlen(skb);
507 int size, offset1 = 0;
508 int i = 0;
Shirley Ma97bc3632011-07-06 12:26:11 +0000509
510 /* Skip over from offset */
511 while (count && (offset >= from->iov_len)) {
512 offset -= from->iov_len;
513 ++from;
514 --count;
515 }
516
517 /* copy up to skb headlen */
518 while (count && (copy > 0)) {
519 size = min_t(unsigned int, copy, from->iov_len - offset);
520 if (copy_from_user(skb->data + offset1, from->iov_base + offset,
521 size))
522 return -EFAULT;
523 if (copy > size) {
524 ++from;
525 --count;
Jason Wang3afc9622012-05-02 11:41:30 +0800526 offset = 0;
527 } else
528 offset += size;
Shirley Ma97bc3632011-07-06 12:26:11 +0000529 copy -= size;
530 offset1 += size;
Shirley Ma97bc3632011-07-06 12:26:11 +0000531 }
532
533 if (len == offset1)
534 return 0;
535
536 while (count--) {
537 struct page *page[MAX_SKB_FRAGS];
538 int num_pages;
539 unsigned long base;
Jason Wang4ef67eb2012-05-02 11:41:44 +0800540 unsigned long truesize;
Shirley Ma97bc3632011-07-06 12:26:11 +0000541
Jason Wang3afc9622012-05-02 11:41:30 +0800542 len = from->iov_len - offset;
Shirley Ma97bc3632011-07-06 12:26:11 +0000543 if (!len) {
Jason Wang3afc9622012-05-02 11:41:30 +0800544 offset = 0;
Shirley Ma97bc3632011-07-06 12:26:11 +0000545 ++from;
546 continue;
547 }
Jason Wang3afc9622012-05-02 11:41:30 +0800548 base = (unsigned long)from->iov_base + offset;
Shirley Ma97bc3632011-07-06 12:26:11 +0000549 size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
Jason Wangb92946e2012-05-02 11:42:15 +0800550 if (i + size > MAX_SKB_FRAGS)
551 return -EMSGSIZE;
Shirley Ma97bc3632011-07-06 12:26:11 +0000552 num_pages = get_user_pages_fast(base, size, 0, &page[i]);
Jason Wangb92946e2012-05-02 11:42:15 +0800553 if (num_pages != size) {
Jason Wang02ce04b2012-05-02 11:41:58 +0800554 for (i = 0; i < num_pages; i++)
555 put_page(page[i]);
Shirley Ma97bc3632011-07-06 12:26:11 +0000556 return -EFAULT;
Jason Wang02ce04b2012-05-02 11:41:58 +0800557 }
Jason Wang4ef67eb2012-05-02 11:41:44 +0800558 truesize = size * PAGE_SIZE;
Shirley Ma97bc3632011-07-06 12:26:11 +0000559 skb->data_len += len;
560 skb->len += len;
Jason Wang4ef67eb2012-05-02 11:41:44 +0800561 skb->truesize += truesize;
562 atomic_add(truesize, &skb->sk->sk_wmem_alloc);
Shirley Ma97bc3632011-07-06 12:26:11 +0000563 while (len) {
Jason Wang653fc912011-09-18 23:48:31 +0000564 int off = base & ~PAGE_MASK;
565 int size = min_t(int, len, PAGE_SIZE - off);
566 __skb_fill_page_desc(skb, i, page[i], off, size);
Shirley Ma97bc3632011-07-06 12:26:11 +0000567 skb_shinfo(skb)->nr_frags++;
568 /* increase sk_wmem_alloc */
Jason Wang653fc912011-09-18 23:48:31 +0000569 base += size;
570 len -= size;
Shirley Ma97bc3632011-07-06 12:26:11 +0000571 i++;
572 }
Jason Wang3afc9622012-05-02 11:41:30 +0800573 offset = 0;
Shirley Ma97bc3632011-07-06 12:26:11 +0000574 ++from;
575 }
576 return 0;
577}
578
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000579/*
580 * macvtap_skb_from_vnet_hdr and macvtap_skb_to_vnet_hdr should
581 * be shared with the tun/tap driver.
582 */
583static int macvtap_skb_from_vnet_hdr(struct sk_buff *skb,
584 struct virtio_net_hdr *vnet_hdr)
585{
586 unsigned short gso_type = 0;
587 if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
588 switch (vnet_hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
589 case VIRTIO_NET_HDR_GSO_TCPV4:
590 gso_type = SKB_GSO_TCPV4;
591 break;
592 case VIRTIO_NET_HDR_GSO_TCPV6:
593 gso_type = SKB_GSO_TCPV6;
594 break;
595 case VIRTIO_NET_HDR_GSO_UDP:
596 gso_type = SKB_GSO_UDP;
597 break;
598 default:
599 return -EINVAL;
600 }
601
602 if (vnet_hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
603 gso_type |= SKB_GSO_TCP_ECN;
604
605 if (vnet_hdr->gso_size == 0)
606 return -EINVAL;
607 }
608
609 if (vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
610 if (!skb_partial_csum_set(skb, vnet_hdr->csum_start,
611 vnet_hdr->csum_offset))
612 return -EINVAL;
613 }
614
615 if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
616 skb_shinfo(skb)->gso_size = vnet_hdr->gso_size;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +0000617 skb_shinfo(skb)->gso_type = gso_type;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000618
619 /* Header must be checked, and gso_segs computed. */
620 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
621 skb_shinfo(skb)->gso_segs = 0;
622 }
623 return 0;
624}
625
626static int macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,
627 struct virtio_net_hdr *vnet_hdr)
628{
629 memset(vnet_hdr, 0, sizeof(*vnet_hdr));
630
631 if (skb_is_gso(skb)) {
632 struct skb_shared_info *sinfo = skb_shinfo(skb);
633
634 /* This is a hint as to how much should be linear. */
635 vnet_hdr->hdr_len = skb_headlen(skb);
636 vnet_hdr->gso_size = sinfo->gso_size;
637 if (sinfo->gso_type & SKB_GSO_TCPV4)
638 vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
639 else if (sinfo->gso_type & SKB_GSO_TCPV6)
640 vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
641 else if (sinfo->gso_type & SKB_GSO_UDP)
642 vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
643 else
644 BUG();
645 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
646 vnet_hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
647 } else
648 vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
649
650 if (skb->ip_summed == CHECKSUM_PARTIAL) {
651 vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +0000652 vnet_hdr->csum_start = skb_checksum_start_offset(skb);
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000653 vnet_hdr->csum_offset = skb->csum_offset;
Jason Wang10a8d942011-06-10 00:56:17 +0000654 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
655 vnet_hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000656 } /* else everything is zero */
657
658 return 0;
659}
660
661
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000662/* Get packet from user space buffer */
Shirley Ma97bc3632011-07-06 12:26:11 +0000663static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
664 const struct iovec *iv, unsigned long total_len,
665 size_t count, int noblock)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000666{
667 struct sk_buff *skb;
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000668 struct macvlan_dev *vlan;
Shirley Ma97bc3632011-07-06 12:26:11 +0000669 unsigned long len = total_len;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000670 int err;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000671 struct virtio_net_hdr vnet_hdr = { 0 };
672 int vnet_hdr_len = 0;
Jason Wangb92946e2012-05-02 11:42:15 +0800673 int copylen = 0;
Shirley Ma97bc3632011-07-06 12:26:11 +0000674 bool zerocopy = false;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000675
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000676 if (q->flags & IFF_VNET_HDR) {
Michael S. Tsirkin55afbd02010-04-29 13:50:48 +0300677 vnet_hdr_len = q->vnet_hdr_sz;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000678
679 err = -EINVAL;
Nicolas Kaiserce3c8692011-03-04 13:49:41 +0000680 if (len < vnet_hdr_len)
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000681 goto err;
Nicolas Kaiserce3c8692011-03-04 13:49:41 +0000682 len -= vnet_hdr_len;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000683
684 err = memcpy_fromiovecend((void *)&vnet_hdr, iv, 0,
Michael S. Tsirkin55afbd02010-04-29 13:50:48 +0300685 sizeof(vnet_hdr));
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000686 if (err < 0)
687 goto err;
688 if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
689 vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
690 vnet_hdr.hdr_len)
691 vnet_hdr.hdr_len = vnet_hdr.csum_start +
692 vnet_hdr.csum_offset + 2;
693 err = -EINVAL;
694 if (vnet_hdr.hdr_len > len)
695 goto err;
696 }
697
698 err = -EINVAL;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000699 if (unlikely(len < ETH_HLEN))
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000700 goto err;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000701
Jason Wangb92946e2012-05-02 11:42:15 +0800702 err = -EMSGSIZE;
703 if (unlikely(count > UIO_MAXIOV))
704 goto err;
705
Shirley Ma97bc3632011-07-06 12:26:11 +0000706 if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY))
707 zerocopy = true;
708
709 if (zerocopy) {
Jason Wangb92946e2012-05-02 11:42:15 +0800710 /* Userspace may produce vectors with count greater than
711 * MAX_SKB_FRAGS, so we need to linearize parts of the skb
712 * to let the rest of data to be fit in the frags.
713 */
714 if (count > MAX_SKB_FRAGS) {
715 copylen = iov_length(iv, count - MAX_SKB_FRAGS);
716 if (copylen < vnet_hdr_len)
717 copylen = 0;
718 else
719 copylen -= vnet_hdr_len;
720 }
Shirley Ma97bc3632011-07-06 12:26:11 +0000721 /* There are 256 bytes to be copied in skb, so there is enough
722 * room for skb expand head in case it is used.
723 * The rest buffer is mapped from userspace.
724 */
Jason Wangb92946e2012-05-02 11:42:15 +0800725 if (copylen < vnet_hdr.hdr_len)
726 copylen = vnet_hdr.hdr_len;
Shirley Ma97bc3632011-07-06 12:26:11 +0000727 if (!copylen)
728 copylen = GOODCOPY_LEN;
729 } else
730 copylen = len;
731
732 skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, copylen,
733 vnet_hdr.hdr_len, noblock, &err);
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000734 if (!skb)
735 goto err;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000736
Jason Wang01d66572012-05-02 11:42:06 +0800737 if (zerocopy)
Shirley Ma97bc3632011-07-06 12:26:11 +0000738 err = zerocopy_sg_from_iovec(skb, iv, vnet_hdr_len, count);
Jason Wang01d66572012-05-02 11:42:06 +0800739 else
Shirley Ma97bc3632011-07-06 12:26:11 +0000740 err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len,
741 len);
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000742 if (err)
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000743 goto err_kfree;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000744
745 skb_set_network_header(skb, ETH_HLEN);
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000746 skb_reset_mac_header(skb);
747 skb->protocol = eth_hdr(skb)->h_proto;
748
749 if (vnet_hdr_len) {
750 err = macvtap_skb_from_vnet_hdr(skb, &vnet_hdr);
751 if (err)
752 goto err_kfree;
753 }
754
Jason Wang40893fd2013-03-26 23:11:22 +0000755 skb_probe_transport_header(skb, ETH_HLEN);
Jason Wang9b4d6692013-03-25 20:19:55 +0000756
Vlad Yasevichac4e4af2013-06-25 16:04:20 -0400757 rcu_read_lock();
758 vlan = rcu_dereference(q->vlan);
Shirley Ma97bc3632011-07-06 12:26:11 +0000759 /* copy skb_ubuf_info for callback when skb has no error */
Jason Wang01d66572012-05-02 11:42:06 +0800760 if (zerocopy) {
Shirley Ma97bc3632011-07-06 12:26:11 +0000761 skb_shinfo(skb)->destructor_arg = m->msg_control;
Jason Wang01d66572012-05-02 11:42:06 +0800762 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +0000763 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
Jason Wang01d66572012-05-02 11:42:06 +0800764 }
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000765 if (vlan)
766 macvlan_start_xmit(skb, vlan->dev);
767 else
768 kfree_skb(skb);
Vlad Yasevichac4e4af2013-06-25 16:04:20 -0400769 rcu_read_unlock();
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000770
Shirley Ma97bc3632011-07-06 12:26:11 +0000771 return total_len;
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000772
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000773err_kfree:
774 kfree_skb(skb);
775
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000776err:
Vlad Yasevichac4e4af2013-06-25 16:04:20 -0400777 rcu_read_lock();
778 vlan = rcu_dereference(q->vlan);
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000779 if (vlan)
Eric Dumazet1ac9ad12011-01-12 12:13:14 +0000780 vlan->dev->stats.tx_dropped++;
Vlad Yasevichac4e4af2013-06-25 16:04:20 -0400781 rcu_read_unlock();
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000782
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000783 return err;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000784}
785
786static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,
787 unsigned long count, loff_t pos)
788{
789 struct file *file = iocb->ki_filp;
790 ssize_t result = -ENOLINK;
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000791 struct macvtap_queue *q = file->private_data;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000792
Shirley Ma97bc3632011-07-06 12:26:11 +0000793 result = macvtap_get_user(q, NULL, iv, iov_length(iv, count), count,
794 file->f_flags & O_NONBLOCK);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000795 return result;
796}
797
798/* Put packet to the user space buffer */
799static ssize_t macvtap_put_user(struct macvtap_queue *q,
800 const struct sk_buff *skb,
801 const struct iovec *iv, int len)
802{
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000803 struct macvlan_dev *vlan;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000804 int ret;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000805 int vnet_hdr_len = 0;
Basil Gorf09e2242012-05-03 22:55:24 +0000806 int vlan_offset = 0;
807 int copied;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000808
809 if (q->flags & IFF_VNET_HDR) {
810 struct virtio_net_hdr vnet_hdr;
Michael S. Tsirkin55afbd02010-04-29 13:50:48 +0300811 vnet_hdr_len = q->vnet_hdr_sz;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000812 if ((len -= vnet_hdr_len) < 0)
813 return -EINVAL;
814
815 ret = macvtap_skb_to_vnet_hdr(skb, &vnet_hdr);
816 if (ret)
817 return ret;
818
Michael S. Tsirkin55afbd02010-04-29 13:50:48 +0300819 if (memcpy_toiovecend(iv, (void *)&vnet_hdr, 0, sizeof(vnet_hdr)))
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000820 return -EFAULT;
821 }
Basil Gorf09e2242012-05-03 22:55:24 +0000822 copied = vnet_hdr_len;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000823
Basil Gorf09e2242012-05-03 22:55:24 +0000824 if (!vlan_tx_tag_present(skb))
825 len = min_t(int, skb->len, len);
826 else {
827 int copy;
828 struct {
829 __be16 h_vlan_proto;
830 __be16 h_vlan_TCI;
831 } veth;
832 veth.h_vlan_proto = htons(ETH_P_8021Q);
833 veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb));
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000834
Basil Gorf09e2242012-05-03 22:55:24 +0000835 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
836 len = min_t(int, skb->len + VLAN_HLEN, len);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000837
Basil Gorf09e2242012-05-03 22:55:24 +0000838 copy = min_t(int, vlan_offset, len);
839 ret = skb_copy_datagram_const_iovec(skb, 0, iv, copied, copy);
840 len -= copy;
841 copied += copy;
842 if (ret || !len)
843 goto done;
844
845 copy = min_t(int, sizeof(veth), len);
846 ret = memcpy_toiovecend(iv, (void *)&veth, copied, copy);
847 len -= copy;
848 copied += copy;
849 if (ret || !len)
850 goto done;
851 }
852
853 ret = skb_copy_datagram_const_iovec(skb, vlan_offset, iv, copied, len);
854 copied += len;
855
856done:
Vlad Yasevichac4e4af2013-06-25 16:04:20 -0400857 rcu_read_lock();
858 vlan = rcu_dereference(q->vlan);
Arnd Bergmann501c7742010-02-18 05:46:50 +0000859 if (vlan)
Basil Gorf09e2242012-05-03 22:55:24 +0000860 macvlan_count_rx(vlan, copied - vnet_hdr_len, ret == 0, 0);
Vlad Yasevichac4e4af2013-06-25 16:04:20 -0400861 rcu_read_unlock();
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000862
Basil Gorf09e2242012-05-03 22:55:24 +0000863 return ret ? ret : copied;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000864}
865
Arnd Bergmann501c7742010-02-18 05:46:50 +0000866static ssize_t macvtap_do_read(struct macvtap_queue *q, struct kiocb *iocb,
867 const struct iovec *iv, unsigned long len,
868 int noblock)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000869{
Hong zhi guoccf7e722012-06-06 22:36:27 +0000870 DEFINE_WAIT(wait);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000871 struct sk_buff *skb;
Arnd Bergmann501c7742010-02-18 05:46:50 +0000872 ssize_t ret = 0;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000873
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000874 while (len) {
Jason Wang89cee912013-06-05 23:54:34 +0000875 if (!noblock)
876 prepare_to_wait(sk_sleep(&q->sk), &wait,
877 TASK_INTERRUPTIBLE);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000878
879 /* Read frames from the queue */
880 skb = skb_dequeue(&q->sk.sk_receive_queue);
881 if (!skb) {
Arnd Bergmann501c7742010-02-18 05:46:50 +0000882 if (noblock) {
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000883 ret = -EAGAIN;
884 break;
885 }
886 if (signal_pending(current)) {
887 ret = -ERESTARTSYS;
888 break;
889 }
890 /* Nothing to read, let's sleep */
891 schedule();
892 continue;
893 }
894 ret = macvtap_put_user(q, skb, iv, len);
895 kfree_skb(skb);
896 break;
897 }
898
Jason Wang89cee912013-06-05 23:54:34 +0000899 if (!noblock)
900 finish_wait(sk_sleep(&q->sk), &wait);
Arnd Bergmann501c7742010-02-18 05:46:50 +0000901 return ret;
902}
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000903
Arnd Bergmann501c7742010-02-18 05:46:50 +0000904static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
905 unsigned long count, loff_t pos)
906{
907 struct file *file = iocb->ki_filp;
908 struct macvtap_queue *q = file->private_data;
909 ssize_t len, ret = 0;
910
911 len = iov_length(iv, count);
912 if (len < 0) {
913 ret = -EINVAL;
914 goto out;
915 }
916
917 ret = macvtap_do_read(q, iocb, iv, len, file->f_flags & O_NONBLOCK);
918 ret = min_t(ssize_t, ret, len); /* XXX copied from tun.c. Why? */
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000919out:
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000920 return ret;
921}
922
Jason Wang8f475a32013-06-05 23:54:36 +0000923static struct macvlan_dev *macvtap_get_vlan(struct macvtap_queue *q)
924{
925 struct macvlan_dev *vlan;
926
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400927 ASSERT_RTNL();
928 vlan = rtnl_dereference(q->vlan);
Jason Wang8f475a32013-06-05 23:54:36 +0000929 if (vlan)
930 dev_hold(vlan->dev);
Jason Wang8f475a32013-06-05 23:54:36 +0000931
932 return vlan;
933}
934
935static void macvtap_put_vlan(struct macvlan_dev *vlan)
936{
937 dev_put(vlan->dev);
938}
939
Jason Wang815f2362013-06-05 23:54:39 +0000940static int macvtap_ioctl_set_queue(struct file *file, unsigned int flags)
941{
942 struct macvtap_queue *q = file->private_data;
943 struct macvlan_dev *vlan;
944 int ret;
945
946 vlan = macvtap_get_vlan(q);
947 if (!vlan)
948 return -EINVAL;
949
950 if (flags & IFF_ATTACH_QUEUE)
951 ret = macvtap_enable_queue(vlan->dev, file, q);
952 else if (flags & IFF_DETACH_QUEUE)
953 ret = macvtap_disable_queue(q);
Jason Wangf57855a2013-06-13 14:23:36 +0800954 else
955 ret = -EINVAL;
Jason Wang815f2362013-06-05 23:54:39 +0000956
957 macvtap_put_vlan(vlan);
958 return ret;
959}
960
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000961/*
962 * provide compatibility with generic tun/tap interface
963 */
964static long macvtap_ioctl(struct file *file, unsigned int cmd,
965 unsigned long arg)
966{
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000967 struct macvtap_queue *q = file->private_data;
968 struct macvlan_dev *vlan;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000969 void __user *argp = (void __user *)arg;
970 struct ifreq __user *ifr = argp;
971 unsigned int __user *up = argp;
972 unsigned int u;
Michael S. Tsirkin55afbd02010-04-29 13:50:48 +0300973 int __user *sp = argp;
974 int s;
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000975 int ret;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000976
977 switch (cmd) {
978 case TUNSETIFF:
979 /* ignore the name, just look at flags */
980 if (get_user(u, &ifr->ifr_flags))
981 return -EFAULT;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000982
983 ret = 0;
Jason Wang815f2362013-06-05 23:54:39 +0000984 if ((u & ~(IFF_VNET_HDR | IFF_MULTI_QUEUE)) !=
985 (IFF_NO_PI | IFF_TAP))
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000986 ret = -EINVAL;
987 else
988 q->flags = u;
989
990 return ret;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000991
992 case TUNGETIFF:
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400993 rtnl_lock();
Jason Wang8f475a32013-06-05 23:54:36 +0000994 vlan = macvtap_get_vlan(q);
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400995 if (!vlan) {
996 rtnl_unlock();
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000997 return -ENOLINK;
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400998 }
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000999
1000 ret = 0;
Eric Dumazet13707f92011-01-26 19:28:23 +00001001 if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +00001002 put_user(q->flags, &ifr->ifr_flags))
Arnd Bergmann02df55d2010-02-18 05:45:36 +00001003 ret = -EFAULT;
Jason Wang8f475a32013-06-05 23:54:36 +00001004 macvtap_put_vlan(vlan);
Vlad Yasevich441ac0f2013-06-25 16:04:19 -04001005 rtnl_unlock();
Arnd Bergmann02df55d2010-02-18 05:45:36 +00001006 return ret;
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001007
Jason Wang815f2362013-06-05 23:54:39 +00001008 case TUNSETQUEUE:
1009 if (get_user(u, &ifr->ifr_flags))
1010 return -EFAULT;
Vlad Yasevich441ac0f2013-06-25 16:04:19 -04001011 rtnl_lock();
1012 ret = macvtap_ioctl_set_queue(file, u);
1013 rtnl_unlock();
Jason Wang815f2362013-06-05 23:54:39 +00001014
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001015 case TUNGETFEATURES:
Jason Wangdf09b362013-06-05 23:54:40 +00001016 if (put_user(IFF_TAP | IFF_NO_PI | IFF_VNET_HDR |
1017 IFF_MULTI_QUEUE, up))
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001018 return -EFAULT;
1019 return 0;
1020
1021 case TUNSETSNDBUF:
1022 if (get_user(u, up))
1023 return -EFAULT;
1024
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001025 q->sk.sk_sndbuf = u;
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001026 return 0;
1027
Michael S. Tsirkin55afbd02010-04-29 13:50:48 +03001028 case TUNGETVNETHDRSZ:
1029 s = q->vnet_hdr_sz;
1030 if (put_user(s, sp))
1031 return -EFAULT;
1032 return 0;
1033
1034 case TUNSETVNETHDRSZ:
1035 if (get_user(s, sp))
1036 return -EFAULT;
1037 if (s < (int)sizeof(struct virtio_net_hdr))
1038 return -EINVAL;
1039
1040 q->vnet_hdr_sz = s;
1041 return 0;
1042
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001043 case TUNSETOFFLOAD:
1044 /* let the user check for future flags */
1045 if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +00001046 TUN_F_TSO_ECN | TUN_F_UFO))
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001047 return -EINVAL;
1048
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +00001049 /* TODO: only accept frames with the features that
1050 got enabled for forwarded frames */
1051 if (!(q->flags & IFF_VNET_HDR))
1052 return -EINVAL;
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001053 return 0;
1054
1055 default:
1056 return -EINVAL;
1057 }
1058}
1059
1060#ifdef CONFIG_COMPAT
1061static long macvtap_compat_ioctl(struct file *file, unsigned int cmd,
1062 unsigned long arg)
1063{
1064 return macvtap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1065}
1066#endif
1067
1068static const struct file_operations macvtap_fops = {
1069 .owner = THIS_MODULE,
1070 .open = macvtap_open,
1071 .release = macvtap_release,
1072 .aio_read = macvtap_aio_read,
1073 .aio_write = macvtap_aio_write,
1074 .poll = macvtap_poll,
1075 .llseek = no_llseek,
1076 .unlocked_ioctl = macvtap_ioctl,
1077#ifdef CONFIG_COMPAT
1078 .compat_ioctl = macvtap_compat_ioctl,
1079#endif
1080};
1081
Arnd Bergmann501c7742010-02-18 05:46:50 +00001082static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,
1083 struct msghdr *m, size_t total_len)
1084{
1085 struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
Shirley Ma97bc3632011-07-06 12:26:11 +00001086 return macvtap_get_user(q, m, m->msg_iov, total_len, m->msg_iovlen,
Arnd Bergmann501c7742010-02-18 05:46:50 +00001087 m->msg_flags & MSG_DONTWAIT);
1088}
1089
1090static int macvtap_recvmsg(struct kiocb *iocb, struct socket *sock,
1091 struct msghdr *m, size_t total_len,
1092 int flags)
1093{
1094 struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
1095 int ret;
1096 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
1097 return -EINVAL;
1098 ret = macvtap_do_read(q, iocb, m->msg_iov, total_len,
1099 flags & MSG_DONTWAIT);
1100 if (ret > total_len) {
1101 m->msg_flags |= MSG_TRUNC;
1102 ret = flags & MSG_TRUNC ? ret : total_len;
1103 }
1104 return ret;
1105}
1106
1107/* Ops structure to mimic raw sockets with tun */
1108static const struct proto_ops macvtap_socket_ops = {
1109 .sendmsg = macvtap_sendmsg,
1110 .recvmsg = macvtap_recvmsg,
1111};
1112
1113/* Get an underlying socket object from tun file. Returns error unless file is
1114 * attached to a device. The returned object works like a packet socket, it
1115 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
1116 * holding a reference to the file for as long as the socket is in use. */
1117struct socket *macvtap_get_socket(struct file *file)
1118{
1119 struct macvtap_queue *q;
1120 if (file->f_op != &macvtap_fops)
1121 return ERR_PTR(-EINVAL);
1122 q = file->private_data;
1123 if (!q)
1124 return ERR_PTR(-EBADFD);
1125 return &q->sock;
1126}
1127EXPORT_SYMBOL_GPL(macvtap_get_socket);
1128
Eric W. Biederman9bf19072011-10-20 04:28:46 +00001129static int macvtap_device_event(struct notifier_block *unused,
1130 unsigned long event, void *ptr)
1131{
Jiri Pirko351638e2013-05-28 01:30:21 +00001132 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane09eff72011-10-20 04:29:24 +00001133 struct macvlan_dev *vlan;
Eric W. Biederman9bf19072011-10-20 04:28:46 +00001134 struct device *classdev;
1135 dev_t devt;
Eric W. Biedermane09eff72011-10-20 04:29:24 +00001136 int err;
Eric W. Biederman9bf19072011-10-20 04:28:46 +00001137
1138 if (dev->rtnl_link_ops != &macvtap_link_ops)
1139 return NOTIFY_DONE;
1140
Eric W. Biedermane09eff72011-10-20 04:29:24 +00001141 vlan = netdev_priv(dev);
Eric W. Biederman9bf19072011-10-20 04:28:46 +00001142
1143 switch (event) {
1144 case NETDEV_REGISTER:
1145 /* Create the device node here after the network device has
1146 * been registered but before register_netdevice has
1147 * finished running.
1148 */
Eric W. Biedermane09eff72011-10-20 04:29:24 +00001149 err = macvtap_get_minor(vlan);
1150 if (err)
1151 return notifier_from_errno(err);
1152
1153 devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
Eric W. Biederman9bf19072011-10-20 04:28:46 +00001154 classdev = device_create(macvtap_class, &dev->dev, devt,
1155 dev, "tap%d", dev->ifindex);
Eric W. Biedermane09eff72011-10-20 04:29:24 +00001156 if (IS_ERR(classdev)) {
1157 macvtap_free_minor(vlan);
Eric W. Biederman9bf19072011-10-20 04:28:46 +00001158 return notifier_from_errno(PTR_ERR(classdev));
Eric W. Biedermane09eff72011-10-20 04:29:24 +00001159 }
Eric W. Biederman9bf19072011-10-20 04:28:46 +00001160 break;
1161 case NETDEV_UNREGISTER:
Eric W. Biedermane09eff72011-10-20 04:29:24 +00001162 devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
Eric W. Biederman9bf19072011-10-20 04:28:46 +00001163 device_destroy(macvtap_class, devt);
Eric W. Biedermane09eff72011-10-20 04:29:24 +00001164 macvtap_free_minor(vlan);
Eric W. Biederman9bf19072011-10-20 04:28:46 +00001165 break;
1166 }
1167
1168 return NOTIFY_DONE;
1169}
1170
1171static struct notifier_block macvtap_notifier_block __read_mostly = {
1172 .notifier_call = macvtap_device_event,
1173};
1174
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001175static int macvtap_init(void)
1176{
1177 int err;
1178
1179 err = alloc_chrdev_region(&macvtap_major, 0,
1180 MACVTAP_NUM_DEVS, "macvtap");
1181 if (err)
1182 goto out1;
1183
1184 cdev_init(&macvtap_cdev, &macvtap_fops);
1185 err = cdev_add(&macvtap_cdev, macvtap_major, MACVTAP_NUM_DEVS);
1186 if (err)
1187 goto out2;
1188
1189 macvtap_class = class_create(THIS_MODULE, "macvtap");
1190 if (IS_ERR(macvtap_class)) {
1191 err = PTR_ERR(macvtap_class);
1192 goto out3;
1193 }
1194
Eric W. Biederman9bf19072011-10-20 04:28:46 +00001195 err = register_netdevice_notifier(&macvtap_notifier_block);
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001196 if (err)
1197 goto out4;
1198
Eric W. Biederman9bf19072011-10-20 04:28:46 +00001199 err = macvlan_link_register(&macvtap_link_ops);
1200 if (err)
1201 goto out5;
1202
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001203 return 0;
1204
Eric W. Biederman9bf19072011-10-20 04:28:46 +00001205out5:
1206 unregister_netdevice_notifier(&macvtap_notifier_block);
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001207out4:
1208 class_unregister(macvtap_class);
1209out3:
1210 cdev_del(&macvtap_cdev);
1211out2:
1212 unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
1213out1:
1214 return err;
1215}
1216module_init(macvtap_init);
1217
1218static void macvtap_exit(void)
1219{
1220 rtnl_link_unregister(&macvtap_link_ops);
Eric W. Biederman9bf19072011-10-20 04:28:46 +00001221 unregister_netdevice_notifier(&macvtap_notifier_block);
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001222 class_unregister(macvtap_class);
1223 cdev_del(&macvtap_cdev);
1224 unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
1225}
1226module_exit(macvtap_exit);
1227
1228MODULE_ALIAS_RTNL_LINK("macvtap");
1229MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
1230MODULE_LICENSE("GPL");