blob: 6f6228e4fd3f50064b0167f5ca89a0efd97dbf29 [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/wait.h>
15#include <linux/cdev.h>
Al Viro40401532012-02-13 03:58:52 +000016#include <linux/idr.h>
Arnd Bergmann20d29d72010-01-30 12:24:26 +000017#include <linux/fs.h>
Herbert Xu6c36d2e2014-11-07 21:22:25 +080018#include <linux/uio.h>
Arnd Bergmann20d29d72010-01-30 12:24:26 +000019
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>
Jason Wang362899b2016-07-15 03:46:31 -040024#include <linux/skb_array.h>
Arnd Bergmann20d29d72010-01-30 12:24:26 +000025
26/*
27 * A macvtap queue is the central object of this driver, it connects
28 * an open character device to a macvlan interface. There can be
29 * multiple queues on one interface, which map back to queues
30 * implemented in hardware on the underlying device.
31 *
32 * macvtap_proto is used to allocate queues through the sock allocation
33 * mechanism.
34 *
Arnd Bergmann20d29d72010-01-30 12:24:26 +000035 */
36struct macvtap_queue {
37 struct sock sk;
38 struct socket sock;
Eric Dumazet43815482010-04-29 11:01:49 +000039 struct socket_wq wq;
Michael S. Tsirkin55afbd02010-04-29 13:50:48 +030040 int vnet_hdr_sz;
Eric Dumazet13707f92011-01-26 19:28:23 +000041 struct macvlan_dev __rcu *vlan;
Arnd Bergmann20d29d72010-01-30 12:24:26 +000042 struct file *file;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +000043 unsigned int flags;
Jason Wang376b1aa2013-06-05 23:54:38 +000044 u16 queue_index;
Jason Wang815f2362013-06-05 23:54:39 +000045 bool enabled;
46 struct list_head next;
Jason Wang362899b2016-07-15 03:46:31 -040047 struct skb_array skb_array;
Arnd Bergmann20d29d72010-01-30 12:24:26 +000048};
49
Michael S. Tsirkin01b07fb2014-12-16 15:05:10 +020050#define MACVTAP_FEATURES (IFF_VNET_HDR | IFF_MULTI_QUEUE)
51
52#define MACVTAP_VNET_LE 0x80000000
Greg Kurz8b8e6582015-04-24 14:50:36 +020053#define MACVTAP_VNET_BE 0x40000000
54
55#ifdef CONFIG_TUN_VNET_CROSS_LE
56static inline bool macvtap_legacy_is_little_endian(struct macvtap_queue *q)
57{
58 return q->flags & MACVTAP_VNET_BE ? false :
59 virtio_legacy_is_little_endian();
60}
61
62static long macvtap_get_vnet_be(struct macvtap_queue *q, int __user *sp)
63{
64 int s = !!(q->flags & MACVTAP_VNET_BE);
65
66 if (put_user(s, sp))
67 return -EFAULT;
68
69 return 0;
70}
71
72static long macvtap_set_vnet_be(struct macvtap_queue *q, int __user *sp)
73{
74 int s;
75
76 if (get_user(s, sp))
77 return -EFAULT;
78
79 if (s)
80 q->flags |= MACVTAP_VNET_BE;
81 else
82 q->flags &= ~MACVTAP_VNET_BE;
83
84 return 0;
85}
86#else
87static inline bool macvtap_legacy_is_little_endian(struct macvtap_queue *q)
88{
89 return virtio_legacy_is_little_endian();
90}
91
92static long macvtap_get_vnet_be(struct macvtap_queue *q, int __user *argp)
93{
94 return -EINVAL;
95}
96
97static long macvtap_set_vnet_be(struct macvtap_queue *q, int __user *argp)
98{
99 return -EINVAL;
100}
101#endif /* CONFIG_TUN_VNET_CROSS_LE */
Michael S. Tsirkin6ae7feb2014-11-23 17:24:15 +0200102
Greg Kurz5b11e152015-04-24 14:24:48 +0200103static inline bool macvtap_is_little_endian(struct macvtap_queue *q)
104{
Greg Kurz7d824102015-04-24 14:26:24 +0200105 return q->flags & MACVTAP_VNET_LE ||
Greg Kurz8b8e6582015-04-24 14:50:36 +0200106 macvtap_legacy_is_little_endian(q);
Greg Kurz5b11e152015-04-24 14:24:48 +0200107}
Michael S. Tsirkin6ae7feb2014-11-23 17:24:15 +0200108
109static inline u16 macvtap16_to_cpu(struct macvtap_queue *q, __virtio16 val)
110{
Greg Kurz5b11e152015-04-24 14:24:48 +0200111 return __virtio16_to_cpu(macvtap_is_little_endian(q), val);
Michael S. Tsirkin6ae7feb2014-11-23 17:24:15 +0200112}
113
114static inline __virtio16 cpu_to_macvtap16(struct macvtap_queue *q, u16 val)
115{
Greg Kurz5b11e152015-04-24 14:24:48 +0200116 return __cpu_to_virtio16(macvtap_is_little_endian(q), val);
Michael S. Tsirkin6ae7feb2014-11-23 17:24:15 +0200117}
118
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000119static struct proto macvtap_proto = {
120 .name = "macvtap",
121 .owner = THIS_MODULE,
122 .obj_size = sizeof (struct macvtap_queue),
123};
124
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000125#define MACVTAP_NUM_DEVS (1U << MINORBITS)
126static DEFINE_MUTEX(minor_lock);
Sainath Grandhia8e04692017-02-10 16:03:46 -0800127DEFINE_IDR(minor_idr);
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000128
Shirley Ma97bc3632011-07-06 12:26:11 +0000129#define GOODCOPY_LEN 128
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000130
Arnd Bergmann501c7742010-02-18 05:46:50 +0000131static const struct proto_ops macvtap_socket_ops;
132
Vlad Yasevich2be5c762013-06-25 16:04:21 -0400133#define RX_OFFLOADS (NETIF_F_GRO | NETIF_F_LRO)
Jason Wangf23d5382015-10-23 00:57:05 -0400134#define TAP_FEATURES (NETIF_F_GSO | NETIF_F_SG | NETIF_F_FRAGLIST)
Vlad Yasevicha567dd62013-08-16 15:25:00 -0400135
Vlad Yasevich6acf54f2013-12-11 13:27:10 -0500136static struct macvlan_dev *macvtap_get_vlan_rcu(const struct net_device *dev)
137{
138 return rcu_dereference(dev->rx_handler_data);
139}
140
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000141/*
142 * RCU usage:
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000143 * The macvtap_queue and the macvlan_dev are loosely coupled, the
144 * pointers from one to the other can only be read while rcu_read_lock
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400145 * or rtnl is held.
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000146 *
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000147 * Both the file and the macvlan_dev hold a reference on the macvtap_queue
148 * through sock_hold(&q->sk). When the macvlan_dev goes away first,
149 * q->vlan becomes inaccessible. When the files gets closed,
150 * macvtap_get_queue() fails.
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000151 *
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000152 * There may still be references to the struct sock inside of the
153 * queue from outbound SKBs, but these never reference back to the
154 * file or the dev. The data structure is freed through __sk_free
155 * when both our references and any pending SKBs are gone.
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000156 */
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000157
Jason Wang815f2362013-06-05 23:54:39 +0000158static int macvtap_enable_queue(struct net_device *dev, struct file *file,
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000159 struct macvtap_queue *q)
160{
161 struct macvlan_dev *vlan = netdev_priv(dev);
Jason Wang815f2362013-06-05 23:54:39 +0000162 int err = -EINVAL;
163
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400164 ASSERT_RTNL();
Jason Wang815f2362013-06-05 23:54:39 +0000165
166 if (q->enabled)
167 goto out;
168
169 err = 0;
170 rcu_assign_pointer(vlan->taps[vlan->numvtaps], q);
171 q->queue_index = vlan->numvtaps;
172 q->enabled = true;
173
174 vlan->numvtaps++;
175out:
Jason Wang815f2362013-06-05 23:54:39 +0000176 return err;
177}
178
Vlad Yasevich40b8fe42014-09-22 16:34:17 -0400179/* Requires RTNL */
Jason Wang815f2362013-06-05 23:54:39 +0000180static int macvtap_set_queue(struct net_device *dev, struct file *file,
181 struct macvtap_queue *q)
182{
183 struct macvlan_dev *vlan = netdev_priv(dev);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000184
Jason Wang815f2362013-06-05 23:54:39 +0000185 if (vlan->numqueues == MAX_MACVTAP_QUEUES)
Vlad Yasevich40b8fe42014-09-22 16:34:17 -0400186 return -EBUSY;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000187
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000188 rcu_assign_pointer(q->vlan, vlan);
Jason Wang376b1aa2013-06-05 23:54:38 +0000189 rcu_assign_pointer(vlan->taps[vlan->numvtaps], q);
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000190 sock_hold(&q->sk);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000191
192 q->file = file;
Jason Wang376b1aa2013-06-05 23:54:38 +0000193 q->queue_index = vlan->numvtaps;
Jason Wang815f2362013-06-05 23:54:39 +0000194 q->enabled = true;
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000195 file->private_data = q;
Jason Wang815f2362013-06-05 23:54:39 +0000196 list_add_tail(&q->next, &vlan->queue_list);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000197
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000198 vlan->numvtaps++;
Jason Wang815f2362013-06-05 23:54:39 +0000199 vlan->numqueues++;
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000200
Vlad Yasevich40b8fe42014-09-22 16:34:17 -0400201 return 0;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000202}
203
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400204static int macvtap_disable_queue(struct macvtap_queue *q)
Jason Wang815f2362013-06-05 23:54:39 +0000205{
206 struct macvlan_dev *vlan;
207 struct macvtap_queue *nq;
208
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400209 ASSERT_RTNL();
Jason Wang815f2362013-06-05 23:54:39 +0000210 if (!q->enabled)
211 return -EINVAL;
212
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400213 vlan = rtnl_dereference(q->vlan);
214
Jason Wang815f2362013-06-05 23:54:39 +0000215 if (vlan) {
216 int index = q->queue_index;
217 BUG_ON(index >= vlan->numvtaps);
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400218 nq = rtnl_dereference(vlan->taps[vlan->numvtaps - 1]);
Jason Wang815f2362013-06-05 23:54:39 +0000219 nq->queue_index = index;
220
221 rcu_assign_pointer(vlan->taps[index], nq);
222 RCU_INIT_POINTER(vlan->taps[vlan->numvtaps - 1], NULL);
223 q->enabled = false;
224
225 vlan->numvtaps--;
226 }
227
228 return 0;
229}
230
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000231/*
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000232 * The file owning the queue got closed, give up both
233 * the reference that the files holds as well as the
234 * one from the macvlan_dev if that still exists.
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000235 *
236 * Using the spinlock makes sure that we don't get
237 * to the queue again after destroying it.
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000238 */
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000239static void macvtap_put_queue(struct macvtap_queue *q)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000240{
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000241 struct macvlan_dev *vlan;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000242
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400243 rtnl_lock();
244 vlan = rtnl_dereference(q->vlan);
245
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000246 if (vlan) {
Jason Wang815f2362013-06-05 23:54:39 +0000247 if (q->enabled)
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400248 BUG_ON(macvtap_disable_queue(q));
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000249
Jason Wang815f2362013-06-05 23:54:39 +0000250 vlan->numqueues--;
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000251 RCU_INIT_POINTER(q->vlan, NULL);
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000252 sock_put(&q->sk);
Jason Wang815f2362013-06-05 23:54:39 +0000253 list_del_init(&q->next);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000254 }
255
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400256 rtnl_unlock();
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000257
258 synchronize_rcu();
259 sock_put(&q->sk);
260}
261
262/*
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000263 * Select a queue based on the rxq of the device on which this packet
264 * arrived. If the incoming device is not mq, calculate a flow hash
265 * to select a queue. If all fails, find the first available queue.
266 * Cache vlan->numvtaps since it can become zero during the execution
267 * of this function.
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000268 */
269static struct macvtap_queue *macvtap_get_queue(struct net_device *dev,
270 struct sk_buff *skb)
271{
272 struct macvlan_dev *vlan = netdev_priv(dev);
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000273 struct macvtap_queue *tap = NULL;
Jason Wang815f2362013-06-05 23:54:39 +0000274 /* Access to taps array is protected by rcu, but access to numvtaps
275 * isn't. Below we use it to lookup a queue, but treat it as a hint
276 * and validate that the result isn't NULL - in case we are
277 * racing against queue removal.
278 */
Jason Wanged0483f2013-06-05 23:54:33 +0000279 int numvtaps = ACCESS_ONCE(vlan->numvtaps);
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000280 __u32 rxq;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000281
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000282 if (!numvtaps)
283 goto out;
284
Jason Wang1b16bf42016-07-15 03:46:30 -0400285 if (numvtaps == 1)
286 goto single;
287
Krishna Kumaref0002b2011-11-23 22:17:14 +0000288 /* Check if we can use flow to select a queue */
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800289 rxq = skb_get_hash(skb);
Krishna Kumaref0002b2011-11-23 22:17:14 +0000290 if (rxq) {
291 tap = rcu_dereference(vlan->taps[rxq % numvtaps]);
Jason Wang376b1aa2013-06-05 23:54:38 +0000292 goto out;
Krishna Kumaref0002b2011-11-23 22:17:14 +0000293 }
294
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000295 if (likely(skb_rx_queue_recorded(skb))) {
296 rxq = skb_get_rx_queue(skb);
297
298 while (unlikely(rxq >= numvtaps))
299 rxq -= numvtaps;
300
301 tap = rcu_dereference(vlan->taps[rxq]);
Jason Wang376b1aa2013-06-05 23:54:38 +0000302 goto out;
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000303 }
304
Jason Wang1b16bf42016-07-15 03:46:30 -0400305single:
Jason Wang376b1aa2013-06-05 23:54:38 +0000306 tap = rcu_dereference(vlan->taps[0]);
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000307out:
308 return tap;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000309}
310
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000311/*
312 * The net_device is going away, give up the reference
Krishna Kumar1565c7c2010-08-04 06:15:59 +0000313 * that it holds on all queues and safely set the pointer
314 * from the queues to NULL.
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000315 */
Sainath Grandhia8e04692017-02-10 16:03:46 -0800316void macvtap_del_queues(struct net_device *dev)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000317{
318 struct macvlan_dev *vlan = netdev_priv(dev);
Pankaj Guptadfe816c2015-06-19 19:47:53 +0530319 struct macvtap_queue *q, *tmp;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000320
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400321 ASSERT_RTNL();
Jason Wang815f2362013-06-05 23:54:39 +0000322 list_for_each_entry_safe(q, tmp, &vlan->queue_list, next) {
323 list_del_init(&q->next);
Jason Wang376b1aa2013-06-05 23:54:38 +0000324 RCU_INIT_POINTER(q->vlan, NULL);
Jason Wang815f2362013-06-05 23:54:39 +0000325 if (q->enabled)
326 vlan->numvtaps--;
327 vlan->numqueues--;
Pankaj Guptadfe816c2015-06-19 19:47:53 +0530328 sock_put(&q->sk);
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000329 }
Jason Wang815f2362013-06-05 23:54:39 +0000330 BUG_ON(vlan->numvtaps);
331 BUG_ON(vlan->numqueues);
Eric W. Biederman99f34b32011-10-20 04:26:01 +0000332 /* guarantee that any future macvtap_set_queue will fail */
333 vlan->numvtaps = MAX_MACVTAP_QUEUES;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000334}
335
Sainath Grandhia8e04692017-02-10 16:03:46 -0800336rx_handler_result_t macvtap_handle_frame(struct sk_buff **pskb)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000337{
Vlad Yasevich6acf54f2013-12-11 13:27:10 -0500338 struct sk_buff *skb = *pskb;
339 struct net_device *dev = skb->dev;
340 struct macvlan_dev *vlan;
341 struct macvtap_queue *q;
Vlad Yasevicha567dd62013-08-16 15:25:00 -0400342 netdev_features_t features = TAP_FEATURES;
343
Vlad Yasevich6acf54f2013-12-11 13:27:10 -0500344 vlan = macvtap_get_vlan_rcu(dev);
345 if (!vlan)
346 return RX_HANDLER_PASS;
347
348 q = macvtap_get_queue(dev, skb);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000349 if (!q)
Vlad Yasevich6acf54f2013-12-11 13:27:10 -0500350 return RX_HANDLER_PASS;
Herbert Xu8a357472010-07-21 21:44:31 +0000351
Jason Wang362899b2016-07-15 03:46:31 -0400352 if (__skb_array_full(&q->skb_array))
Herbert Xu8a357472010-07-21 21:44:31 +0000353 goto drop;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000354
Vlad Yasevich6acf54f2013-12-11 13:27:10 -0500355 skb_push(skb, ETH_HLEN);
356
Vlad Yasevich3e4f8b72013-06-25 16:04:22 -0400357 /* Apply the forward feature mask so that we perform segmentation
Vlad Yaseviche5733322013-08-16 15:25:02 -0400358 * according to users wishes. This only works if VNET_HDR is
359 * enabled.
Vlad Yasevich3e4f8b72013-06-25 16:04:22 -0400360 */
Vlad Yaseviche5733322013-08-16 15:25:02 -0400361 if (q->flags & IFF_VNET_HDR)
362 features |= vlan->tap_features;
Johannes Berg8b86a612015-04-17 15:45:04 +0200363 if (netif_needs_gso(skb, features)) {
Vlad Yasevich3e4f8b72013-06-25 16:04:22 -0400364 struct sk_buff *segs = __skb_gso_segment(skb, features, false);
365
366 if (IS_ERR(segs))
367 goto drop;
368
369 if (!segs) {
Jason Wang362899b2016-07-15 03:46:31 -0400370 if (skb_array_produce(&q->skb_array, skb))
371 goto drop;
Vlad Yasevich3e4f8b72013-06-25 16:04:22 -0400372 goto wake_up;
373 }
374
Eric Dumazetbe0bd312016-05-06 05:58:21 -0700375 consume_skb(skb);
Vlad Yasevich3e4f8b72013-06-25 16:04:22 -0400376 while (segs) {
377 struct sk_buff *nskb = segs->next;
378
379 segs->next = NULL;
Jason Wang362899b2016-07-15 03:46:31 -0400380 if (skb_array_produce(&q->skb_array, segs)) {
381 kfree_skb(segs);
382 kfree_skb_list(nskb);
383 break;
384 }
Vlad Yasevich3e4f8b72013-06-25 16:04:22 -0400385 segs = nskb;
386 }
387 } else {
Vlad Yasevichcbdb0422014-04-29 10:09:50 -0400388 /* If we receive a partial checksum and the tap side
389 * doesn't support checksum offload, compute the checksum.
390 * Note: it doesn't matter which checksum feature to
Sainath Grandhia8e04692017-02-10 16:03:46 -0800391 * check, we either support them all or none.
Vlad Yasevichcbdb0422014-04-29 10:09:50 -0400392 */
393 if (skb->ip_summed == CHECKSUM_PARTIAL &&
Tom Herberta1882222015-12-14 11:19:43 -0800394 !(features & NETIF_F_CSUM_MASK) &&
Vlad Yasevichcbdb0422014-04-29 10:09:50 -0400395 skb_checksum_help(skb))
396 goto drop;
Jason Wang362899b2016-07-15 03:46:31 -0400397 if (skb_array_produce(&q->skb_array, skb))
398 goto drop;
Vlad Yasevich3e4f8b72013-06-25 16:04:22 -0400399 }
400
401wake_up:
Eric Dumazet4a4771a2010-04-25 22:20:06 +0000402 wake_up_interruptible_poll(sk_sleep(&q->sk), POLLIN | POLLRDNORM | POLLRDBAND);
Vlad Yasevich6acf54f2013-12-11 13:27:10 -0500403 return RX_HANDLER_CONSUMED;
Herbert Xu8a357472010-07-21 21:44:31 +0000404
405drop:
Vlad Yasevich6acf54f2013-12-11 13:27:10 -0500406 /* Count errors/drops only here, thus don't care about args. */
407 macvlan_count_rx(vlan, 0, 0, 0);
Herbert Xu8a357472010-07-21 21:44:31 +0000408 kfree_skb(skb);
Vlad Yasevich6acf54f2013-12-11 13:27:10 -0500409 return RX_HANDLER_CONSUMED;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000410}
411
Sainath Grandhia8e04692017-02-10 16:03:46 -0800412int macvtap_get_minor(struct macvlan_dev *vlan)
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000413{
414 int retval = -ENOMEM;
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000415
416 mutex_lock(&minor_lock);
Tejun Heoec09ebc2013-02-27 17:04:34 -0800417 retval = idr_alloc(&minor_idr, vlan, 1, MACVTAP_NUM_DEVS, GFP_KERNEL);
418 if (retval >= 0) {
419 vlan->minor = retval;
420 } else if (retval == -ENOSPC) {
Zhang Shengju763dfa272016-11-29 11:26:32 +0800421 netdev_err(vlan->dev, "Too many macvtap devices\n");
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000422 retval = -EINVAL;
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000423 }
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000424 mutex_unlock(&minor_lock);
Tejun Heoec09ebc2013-02-27 17:04:34 -0800425 return retval < 0 ? retval : 0;
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000426}
427
Sainath Grandhia8e04692017-02-10 16:03:46 -0800428void macvtap_free_minor(struct macvlan_dev *vlan)
Eric W. Biedermane09eff72011-10-20 04:29:24 +0000429{
430 mutex_lock(&minor_lock);
431 if (vlan->minor) {
432 idr_remove(&minor_idr, vlan->minor);
433 vlan->minor = 0;
434 }
435 mutex_unlock(&minor_lock);
436}
437
438static struct net_device *dev_get_by_macvtap_minor(int minor)
439{
440 struct net_device *dev = NULL;
441 struct macvlan_dev *vlan;
442
443 mutex_lock(&minor_lock);
444 vlan = idr_find(&minor_idr, minor);
445 if (vlan) {
446 dev = vlan->dev;
447 dev_hold(dev);
448 }
449 mutex_unlock(&minor_lock);
450 return dev;
451}
452
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000453static void macvtap_sock_write_space(struct sock *sk)
454{
Eric Dumazet43815482010-04-29 11:01:49 +0000455 wait_queue_head_t *wqueue;
456
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000457 if (!sock_writeable(sk) ||
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800458 !test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000459 return;
460
Eric Dumazet43815482010-04-29 11:01:49 +0000461 wqueue = sk_sleep(sk);
462 if (wqueue && waitqueue_active(wqueue))
463 wake_up_interruptible_poll(wqueue, POLLOUT | POLLWRNORM | POLLWRBAND);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000464}
465
Eric W. Biederman2259fef2011-10-20 04:27:24 +0000466static void macvtap_sock_destruct(struct sock *sk)
467{
Jason Wang362899b2016-07-15 03:46:31 -0400468 struct macvtap_queue *q = container_of(sk, struct macvtap_queue, sk);
Jason Wang362899b2016-07-15 03:46:31 -0400469
Jason Wang104a4932016-08-11 18:15:56 +0800470 skb_array_cleanup(&q->skb_array);
Eric W. Biederman2259fef2011-10-20 04:27:24 +0000471}
472
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000473static int macvtap_open(struct inode *inode, struct file *file)
474{
475 struct net *net = current->nsproxy->net_ns;
Vlad Yasevich40b8fe42014-09-22 16:34:17 -0400476 struct net_device *dev;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000477 struct macvtap_queue *q;
Vlad Yasevich40b8fe42014-09-22 16:34:17 -0400478 int err = -ENODEV;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000479
Vlad Yasevich40b8fe42014-09-22 16:34:17 -0400480 rtnl_lock();
481 dev = dev_get_by_macvtap_minor(iminor(inode));
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000482 if (!dev)
Jason Wang362899b2016-07-15 03:46:31 -0400483 goto err;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000484
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000485 err = -ENOMEM;
486 q = (struct macvtap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500487 &macvtap_proto, 0);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000488 if (!q)
Jason Wang362899b2016-07-15 03:46:31 -0400489 goto err;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000490
Jason Wangd9a90a32013-06-13 14:23:35 +0800491 RCU_INIT_POINTER(q->sock.wq, &q->wq);
Eric Dumazet43815482010-04-29 11:01:49 +0000492 init_waitqueue_head(&q->wq.wait);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000493 q->sock.type = SOCK_RAW;
494 q->sock.state = SS_CONNECTED;
Arnd Bergmann501c7742010-02-18 05:46:50 +0000495 q->sock.file = file;
496 q->sock.ops = &macvtap_socket_ops;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000497 sock_init_data(&q->sock, &q->sk);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000498 q->sk.sk_write_space = macvtap_sock_write_space;
Eric W. Biederman2259fef2011-10-20 04:27:24 +0000499 q->sk.sk_destruct = macvtap_sock_destruct;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000500 q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
Michael S. Tsirkin55afbd02010-04-29 13:50:48 +0300501 q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000502
Shirley Ma97bc3632011-07-06 12:26:11 +0000503 /*
504 * so far only KVM virtio_net uses macvtap, enable zero copy between
505 * guest kernel and host kernel when lower device supports zerocopy
Eric W. Biederman047af9cf2011-10-20 04:26:39 +0000506 *
507 * The macvlan supports zerocopy iff the lower device supports zero
508 * copy so we don't have to look at the lower device directly.
Shirley Ma97bc3632011-07-06 12:26:11 +0000509 */
Eric W. Biederman047af9cf2011-10-20 04:26:39 +0000510 if ((dev->features & NETIF_F_HIGHDMA) && (dev->features & NETIF_F_SG))
511 sock_set_flag(&q->sk, SOCK_ZEROCOPY);
Shirley Ma97bc3632011-07-06 12:26:11 +0000512
Jason Wang362899b2016-07-15 03:46:31 -0400513 err = -ENOMEM;
514 if (skb_array_init(&q->skb_array, dev->tx_queue_len, GFP_KERNEL))
515 goto err_array;
516
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000517 err = macvtap_set_queue(dev, file, q);
518 if (err)
Jason Wang362899b2016-07-15 03:46:31 -0400519 goto err_queue;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000520
Jason Wang362899b2016-07-15 03:46:31 -0400521 dev_put(dev);
522
523 rtnl_unlock();
524 return err;
525
526err_queue:
527 skb_array_cleanup(&q->skb_array);
528err_array:
529 sock_put(&q->sk);
530err:
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000531 if (dev)
532 dev_put(dev);
533
Vlad Yasevich40b8fe42014-09-22 16:34:17 -0400534 rtnl_unlock();
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000535 return err;
536}
537
538static int macvtap_release(struct inode *inode, struct file *file)
539{
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000540 struct macvtap_queue *q = file->private_data;
541 macvtap_put_queue(q);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000542 return 0;
543}
544
545static unsigned int macvtap_poll(struct file *file, poll_table * wait)
546{
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000547 struct macvtap_queue *q = file->private_data;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000548 unsigned int mask = POLLERR;
549
550 if (!q)
551 goto out;
552
553 mask = 0;
Eric Dumazet43815482010-04-29 11:01:49 +0000554 poll_wait(file, &q->wq.wait, wait);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000555
Jason Wang362899b2016-07-15 03:46:31 -0400556 if (!skb_array_empty(&q->skb_array))
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000557 mask |= POLLIN | POLLRDNORM;
558
559 if (sock_writeable(&q->sk) ||
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800560 (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &q->sock.flags) &&
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000561 sock_writeable(&q->sk)))
562 mask |= POLLOUT | POLLWRNORM;
563
564out:
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000565 return mask;
566}
567
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000568static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,
569 size_t len, size_t linear,
570 int noblock, int *err)
571{
572 struct sk_buff *skb;
573
574 /* Under a page? Don't bother with paged skb. */
575 if (prepad + len < PAGE_SIZE || !linear)
576 linear = len;
577
578 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -0700579 err, 0);
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000580 if (!skb)
581 return NULL;
582
583 skb_reserve(skb, prepad);
584 skb_put(skb, linear);
585 skb->data_len = len - linear;
586 skb->len += len - linear;
587
588 return skb;
589}
590
Eric Dumazet2f1d8b92015-02-27 18:35:35 -0800591/* Neighbour code has some assumptions on HH_DATA_MOD alignment */
592#define MACVTAP_RESERVE HH_DATA_OFF(ETH_HLEN)
593
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000594/* Get packet from user space buffer */
Shirley Ma97bc3632011-07-06 12:26:11 +0000595static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
Al Virof5ff53b2014-06-19 15:36:49 -0400596 struct iov_iter *from, int noblock)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000597{
Eric Dumazet2f1d8b92015-02-27 18:35:35 -0800598 int good_linear = SKB_MAX_HEAD(MACVTAP_RESERVE);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000599 struct sk_buff *skb;
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000600 struct macvlan_dev *vlan;
Al Virof5ff53b2014-06-19 15:36:49 -0400601 unsigned long total_len = iov_iter_count(from);
Shirley Ma97bc3632011-07-06 12:26:11 +0000602 unsigned long len = total_len;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000603 int err;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000604 struct virtio_net_hdr vnet_hdr = { 0 };
605 int vnet_hdr_len = 0;
Jason Wangb92946e2012-05-02 11:42:15 +0800606 int copylen = 0;
Ivan Vecerac5c62f12015-07-23 16:37:43 +0200607 int depth;
Shirley Ma97bc3632011-07-06 12:26:11 +0000608 bool zerocopy = false;
Jason Wang61d46bf2013-07-10 13:43:28 +0800609 size_t linear;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000610
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000611 if (q->flags & IFF_VNET_HDR) {
Willem de Bruijn837585a2017-02-03 18:20:49 -0500612 vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000613
614 err = -EINVAL;
Nicolas Kaiserce3c8692011-03-04 13:49:41 +0000615 if (len < vnet_hdr_len)
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000616 goto err;
Nicolas Kaiserce3c8692011-03-04 13:49:41 +0000617 len -= vnet_hdr_len;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000618
Al Virof5ff53b2014-06-19 15:36:49 -0400619 err = -EFAULT;
Al Virocbbd26b2016-11-01 22:09:04 -0400620 if (!copy_from_iter_full(&vnet_hdr, sizeof(vnet_hdr), from))
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000621 goto err;
Al Virof5ff53b2014-06-19 15:36:49 -0400622 iov_iter_advance(from, vnet_hdr_len - sizeof(vnet_hdr));
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000623 if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
Michael S. Tsirkin6ae7feb2014-11-23 17:24:15 +0200624 macvtap16_to_cpu(q, vnet_hdr.csum_start) +
625 macvtap16_to_cpu(q, vnet_hdr.csum_offset) + 2 >
626 macvtap16_to_cpu(q, vnet_hdr.hdr_len))
627 vnet_hdr.hdr_len = cpu_to_macvtap16(q,
628 macvtap16_to_cpu(q, vnet_hdr.csum_start) +
629 macvtap16_to_cpu(q, vnet_hdr.csum_offset) + 2);
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000630 err = -EINVAL;
Michael S. Tsirkin6ae7feb2014-11-23 17:24:15 +0200631 if (macvtap16_to_cpu(q, vnet_hdr.hdr_len) > len)
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000632 goto err;
633 }
634
635 err = -EINVAL;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000636 if (unlikely(len < ETH_HLEN))
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000637 goto err;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000638
Jason Wangece793f2013-07-18 10:55:16 +0800639 if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
Al Virof5ff53b2014-06-19 15:36:49 -0400640 struct iov_iter i;
641
Michael S. Tsirkin6ae7feb2014-11-23 17:24:15 +0200642 copylen = vnet_hdr.hdr_len ?
643 macvtap16_to_cpu(q, vnet_hdr.hdr_len) : GOODCOPY_LEN;
Jason Wang16a3fa22013-11-13 14:00:40 +0800644 if (copylen > good_linear)
645 copylen = good_linear;
Willem de Bruijn8e2ad412016-03-08 15:18:54 -0500646 else if (copylen < ETH_HLEN)
647 copylen = ETH_HLEN;
Jason Wang61d46bf2013-07-10 13:43:28 +0800648 linear = copylen;
Al Virof5ff53b2014-06-19 15:36:49 -0400649 i = *from;
650 iov_iter_advance(&i, copylen);
651 if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
Jason Wangece793f2013-07-18 10:55:16 +0800652 zerocopy = true;
653 }
654
655 if (!zerocopy) {
Shirley Ma97bc3632011-07-06 12:26:11 +0000656 copylen = len;
Willem de Bruijn8e2ad412016-03-08 15:18:54 -0500657 linear = macvtap16_to_cpu(q, vnet_hdr.hdr_len);
658 if (linear > good_linear)
Jason Wang16a3fa22013-11-13 14:00:40 +0800659 linear = good_linear;
Willem de Bruijn8e2ad412016-03-08 15:18:54 -0500660 else if (linear < ETH_HLEN)
661 linear = ETH_HLEN;
Jason Wang61d46bf2013-07-10 13:43:28 +0800662 }
Shirley Ma97bc3632011-07-06 12:26:11 +0000663
Eric Dumazet2f1d8b92015-02-27 18:35:35 -0800664 skb = macvtap_alloc_skb(&q->sk, MACVTAP_RESERVE, copylen,
Jason Wang61d46bf2013-07-10 13:43:28 +0800665 linear, noblock, &err);
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000666 if (!skb)
667 goto err;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000668
Jason Wang01d66572012-05-02 11:42:06 +0800669 if (zerocopy)
Al Virof5ff53b2014-06-19 15:36:49 -0400670 err = zerocopy_sg_from_iter(skb, from);
Jason Wangaa196ee2016-11-30 13:17:52 +0800671 else
Al Virof5ff53b2014-06-19 15:36:49 -0400672 err = skb_copy_datagram_from_iter(skb, 0, from, len);
Jason Wangece793f2013-07-18 10:55:16 +0800673
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000674 if (err)
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000675 goto err_kfree;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000676
677 skb_set_network_header(skb, ETH_HLEN);
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000678 skb_reset_mac_header(skb);
679 skb->protocol = eth_hdr(skb)->h_proto;
680
681 if (vnet_hdr_len) {
Mike Rapoportfd88d682016-06-08 16:09:19 +0300682 err = virtio_net_hdr_to_skb(skb, &vnet_hdr,
683 macvtap_is_little_endian(q));
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000684 if (err)
685 goto err_kfree;
686 }
687
Jason Wang40893fd2013-03-26 23:11:22 +0000688 skb_probe_transport_header(skb, ETH_HLEN);
Jason Wang9b4d6692013-03-25 20:19:55 +0000689
Ivan Vecerac5c62f12015-07-23 16:37:43 +0200690 /* Move network header to the right position for VLAN tagged packets */
691 if ((skb->protocol == htons(ETH_P_8021Q) ||
692 skb->protocol == htons(ETH_P_8021AD)) &&
693 __vlan_get_protocol(skb, skb->protocol, &depth) != 0)
694 skb_set_network_header(skb, depth);
695
Vlad Yasevichac4e4af2013-06-25 16:04:20 -0400696 rcu_read_lock();
697 vlan = rcu_dereference(q->vlan);
Shirley Ma97bc3632011-07-06 12:26:11 +0000698 /* copy skb_ubuf_info for callback when skb has no error */
Jason Wang01d66572012-05-02 11:42:06 +0800699 if (zerocopy) {
Shirley Ma97bc3632011-07-06 12:26:11 +0000700 skb_shinfo(skb)->destructor_arg = m->msg_control;
Jason Wang01d66572012-05-02 11:42:06 +0800701 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +0000702 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
Jason Wangaa196ee2016-11-30 13:17:52 +0800703 } else if (m && m->msg_control) {
704 struct ubuf_info *uarg = m->msg_control;
705 uarg->callback(uarg, false);
Jason Wang01d66572012-05-02 11:42:06 +0800706 }
Jason Wangaa196ee2016-11-30 13:17:52 +0800707
Eric Dumazet29d79192013-08-08 08:06:14 -0700708 if (vlan) {
Vlad Yasevich6acf54f2013-12-11 13:27:10 -0500709 skb->dev = vlan->dev;
710 dev_queue_xmit(skb);
Eric Dumazet29d79192013-08-08 08:06:14 -0700711 } else {
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000712 kfree_skb(skb);
Eric Dumazet29d79192013-08-08 08:06:14 -0700713 }
Vlad Yasevichac4e4af2013-06-25 16:04:20 -0400714 rcu_read_unlock();
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000715
Shirley Ma97bc3632011-07-06 12:26:11 +0000716 return total_len;
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000717
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000718err_kfree:
719 kfree_skb(skb);
720
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000721err:
Vlad Yasevichac4e4af2013-06-25 16:04:20 -0400722 rcu_read_lock();
723 vlan = rcu_dereference(q->vlan);
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000724 if (vlan)
Jason Wangcd3e22b2013-11-25 17:19:04 +0800725 this_cpu_inc(vlan->pcpu_stats->tx_dropped);
Vlad Yasevichac4e4af2013-06-25 16:04:20 -0400726 rcu_read_unlock();
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000727
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000728 return err;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000729}
730
Al Virof5ff53b2014-06-19 15:36:49 -0400731static ssize_t macvtap_write_iter(struct kiocb *iocb, struct iov_iter *from)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000732{
733 struct file *file = iocb->ki_filp;
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000734 struct macvtap_queue *q = file->private_data;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000735
Al Virof5ff53b2014-06-19 15:36:49 -0400736 return macvtap_get_user(q, NULL, from, file->f_flags & O_NONBLOCK);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000737}
738
739/* Put packet to the user space buffer */
740static ssize_t macvtap_put_user(struct macvtap_queue *q,
741 const struct sk_buff *skb,
Herbert Xu6c36d2e2014-11-07 21:22:25 +0800742 struct iov_iter *iter)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000743{
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000744 int ret;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000745 int vnet_hdr_len = 0;
Basil Gorf09e2242012-05-03 22:55:24 +0000746 int vlan_offset = 0;
Herbert Xu6c36d2e2014-11-07 21:22:25 +0800747 int total;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000748
749 if (q->flags & IFF_VNET_HDR) {
750 struct virtio_net_hdr vnet_hdr;
Willem de Bruijn837585a2017-02-03 18:20:49 -0500751 vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
Herbert Xu6c36d2e2014-11-07 21:22:25 +0800752 if (iov_iter_count(iter) < vnet_hdr_len)
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000753 return -EINVAL;
754
Jarno Rajahalme3e9e40e2016-11-18 15:40:38 -0800755 if (virtio_net_hdr_from_skb(skb, &vnet_hdr,
Jason Wang6391a442017-01-20 14:32:42 +0800756 macvtap_is_little_endian(q), true))
Mike Rapoportfd88d682016-06-08 16:09:19 +0300757 BUG();
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000758
Herbert Xu6c36d2e2014-11-07 21:22:25 +0800759 if (copy_to_iter(&vnet_hdr, sizeof(vnet_hdr), iter) !=
760 sizeof(vnet_hdr))
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000761 return -EFAULT;
Jason Wang7cc76f52014-11-20 16:31:05 +0800762
763 iov_iter_advance(iter, vnet_hdr_len - sizeof(vnet_hdr));
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000764 }
Herbert Xu6c36d2e2014-11-07 21:22:25 +0800765 total = vnet_hdr_len;
Jason Wangce232ce2013-12-11 13:08:34 +0800766 total += skb->len;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000767
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100768 if (skb_vlan_tag_present(skb)) {
Basil Gorf09e2242012-05-03 22:55:24 +0000769 struct {
770 __be16 h_vlan_proto;
771 __be16 h_vlan_TCI;
772 } veth;
Jason Wang0fbe0d42013-07-16 13:36:34 +0800773 veth.h_vlan_proto = skb->vlan_proto;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100774 veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000775
Basil Gorf09e2242012-05-03 22:55:24 +0000776 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
Jason Wangce232ce2013-12-11 13:08:34 +0800777 total += VLAN_HLEN;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000778
Herbert Xu6c36d2e2014-11-07 21:22:25 +0800779 ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
780 if (ret || !iov_iter_count(iter))
Basil Gorf09e2242012-05-03 22:55:24 +0000781 goto done;
782
Herbert Xu6c36d2e2014-11-07 21:22:25 +0800783 ret = copy_to_iter(&veth, sizeof(veth), iter);
784 if (ret != sizeof(veth) || !iov_iter_count(iter))
Basil Gorf09e2242012-05-03 22:55:24 +0000785 goto done;
786 }
787
Herbert Xu6c36d2e2014-11-07 21:22:25 +0800788 ret = skb_copy_datagram_iter(skb, vlan_offset, iter,
789 skb->len - vlan_offset);
Basil Gorf09e2242012-05-03 22:55:24 +0000790
791done:
Jason Wangce232ce2013-12-11 13:08:34 +0800792 return ret ? ret : total;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000793}
794
Zhi Yong Wu55ec8e22013-12-07 04:13:05 +0800795static ssize_t macvtap_do_read(struct macvtap_queue *q,
Al Viro3af0bfe2014-11-07 14:13:53 -0500796 struct iov_iter *to,
Arnd Bergmann501c7742010-02-18 05:46:50 +0000797 int noblock)
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000798{
Hong zhi guoccf7e722012-06-06 22:36:27 +0000799 DEFINE_WAIT(wait);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000800 struct sk_buff *skb;
Arnd Bergmann501c7742010-02-18 05:46:50 +0000801 ssize_t ret = 0;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000802
Al Viro3af0bfe2014-11-07 14:13:53 -0500803 if (!iov_iter_count(to))
804 return 0;
805
806 while (1) {
Jason Wang89cee912013-06-05 23:54:34 +0000807 if (!noblock)
808 prepare_to_wait(sk_sleep(&q->sk), &wait,
809 TASK_INTERRUPTIBLE);
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000810
811 /* Read frames from the queue */
Jason Wang362899b2016-07-15 03:46:31 -0400812 skb = skb_array_consume(&q->skb_array);
Al Viro3af0bfe2014-11-07 14:13:53 -0500813 if (skb)
814 break;
815 if (noblock) {
816 ret = -EAGAIN;
817 break;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000818 }
Al Viro3af0bfe2014-11-07 14:13:53 -0500819 if (signal_pending(current)) {
820 ret = -ERESTARTSYS;
821 break;
822 }
823 /* Nothing to read, let's sleep */
824 schedule();
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000825 }
Vlad Yasevicha499a2e2015-11-09 09:14:17 -0500826 if (!noblock)
827 finish_wait(sk_sleep(&q->sk), &wait);
828
Al Viro3af0bfe2014-11-07 14:13:53 -0500829 if (skb) {
830 ret = macvtap_put_user(q, skb, to);
Jason Wangf51a5e82014-12-01 16:53:15 +0800831 if (unlikely(ret < 0))
832 kfree_skb(skb);
833 else
834 consume_skb(skb);
Al Viro3af0bfe2014-11-07 14:13:53 -0500835 }
Arnd Bergmann501c7742010-02-18 05:46:50 +0000836 return ret;
837}
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000838
Al Viro3af0bfe2014-11-07 14:13:53 -0500839static ssize_t macvtap_read_iter(struct kiocb *iocb, struct iov_iter *to)
Arnd Bergmann501c7742010-02-18 05:46:50 +0000840{
841 struct file *file = iocb->ki_filp;
842 struct macvtap_queue *q = file->private_data;
Al Viro3af0bfe2014-11-07 14:13:53 -0500843 ssize_t len = iov_iter_count(to), ret;
Arnd Bergmann501c7742010-02-18 05:46:50 +0000844
Al Viro3af0bfe2014-11-07 14:13:53 -0500845 ret = macvtap_do_read(q, to, file->f_flags & O_NONBLOCK);
Jason Wangce232ce2013-12-11 13:08:34 +0800846 ret = min_t(ssize_t, ret, len);
Zhi Yong Wue6ebc7f2013-12-06 14:16:50 +0800847 if (ret > 0)
848 iocb->ki_pos = ret;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000849 return ret;
850}
851
Jason Wang8f475a32013-06-05 23:54:36 +0000852static struct macvlan_dev *macvtap_get_vlan(struct macvtap_queue *q)
853{
854 struct macvlan_dev *vlan;
855
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400856 ASSERT_RTNL();
857 vlan = rtnl_dereference(q->vlan);
Jason Wang8f475a32013-06-05 23:54:36 +0000858 if (vlan)
859 dev_hold(vlan->dev);
Jason Wang8f475a32013-06-05 23:54:36 +0000860
861 return vlan;
862}
863
864static void macvtap_put_vlan(struct macvlan_dev *vlan)
865{
866 dev_put(vlan->dev);
867}
868
Jason Wang815f2362013-06-05 23:54:39 +0000869static int macvtap_ioctl_set_queue(struct file *file, unsigned int flags)
870{
871 struct macvtap_queue *q = file->private_data;
872 struct macvlan_dev *vlan;
873 int ret;
874
875 vlan = macvtap_get_vlan(q);
876 if (!vlan)
877 return -EINVAL;
878
879 if (flags & IFF_ATTACH_QUEUE)
880 ret = macvtap_enable_queue(vlan->dev, file, q);
881 else if (flags & IFF_DETACH_QUEUE)
882 ret = macvtap_disable_queue(q);
Jason Wangf57855a2013-06-13 14:23:36 +0800883 else
884 ret = -EINVAL;
Jason Wang815f2362013-06-05 23:54:39 +0000885
886 macvtap_put_vlan(vlan);
887 return ret;
888}
889
Vlad Yasevich2be5c762013-06-25 16:04:21 -0400890static int set_offload(struct macvtap_queue *q, unsigned long arg)
891{
892 struct macvlan_dev *vlan;
893 netdev_features_t features;
894 netdev_features_t feature_mask = 0;
895
896 vlan = rtnl_dereference(q->vlan);
897 if (!vlan)
898 return -ENOLINK;
899
900 features = vlan->dev->features;
901
902 if (arg & TUN_F_CSUM) {
903 feature_mask = NETIF_F_HW_CSUM;
904
905 if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
906 if (arg & TUN_F_TSO_ECN)
907 feature_mask |= NETIF_F_TSO_ECN;
908 if (arg & TUN_F_TSO4)
909 feature_mask |= NETIF_F_TSO;
910 if (arg & TUN_F_TSO6)
911 feature_mask |= NETIF_F_TSO6;
912 }
Vlad Yaseviche3e3c422015-02-03 16:36:17 -0500913
914 if (arg & TUN_F_UFO)
915 feature_mask |= NETIF_F_UFO;
Vlad Yasevich2be5c762013-06-25 16:04:21 -0400916 }
917
918 /* tun/tap driver inverts the usage for TSO offloads, where
919 * setting the TSO bit means that the userspace wants to
920 * accept TSO frames and turning it off means that user space
921 * does not support TSO.
922 * For macvtap, we have to invert it to mean the same thing.
923 * When user space turns off TSO, we turn off GSO/LRO so that
924 * user-space will not receive TSO frames.
925 */
Vlad Yaseviche3e3c422015-02-03 16:36:17 -0500926 if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_UFO))
Vlad Yasevich2be5c762013-06-25 16:04:21 -0400927 features |= RX_OFFLOADS;
928 else
929 features &= ~RX_OFFLOADS;
930
931 /* tap_features are the same as features on tun/tap and
932 * reflect user expectations.
933 */
Vlad Yasevicha567dd62013-08-16 15:25:00 -0400934 vlan->tap_features = feature_mask;
Vlad Yasevich2be5c762013-06-25 16:04:21 -0400935 vlan->set_features = features;
936 netdev_update_features(vlan->dev);
937
938 return 0;
939}
940
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000941/*
942 * provide compatibility with generic tun/tap interface
943 */
944static long macvtap_ioctl(struct file *file, unsigned int cmd,
945 unsigned long arg)
946{
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000947 struct macvtap_queue *q = file->private_data;
948 struct macvlan_dev *vlan;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000949 void __user *argp = (void __user *)arg;
950 struct ifreq __user *ifr = argp;
951 unsigned int __user *up = argp;
Michael S. Tsirkin39ec7de2014-12-16 15:04:56 +0200952 unsigned short u;
Michael S. Tsirkin55afbd02010-04-29 13:50:48 +0300953 int __user *sp = argp;
Justin Cormack7f460d32015-05-13 19:19:02 +0100954 struct sockaddr sa;
Michael S. Tsirkin55afbd02010-04-29 13:50:48 +0300955 int s;
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000956 int ret;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000957
958 switch (cmd) {
959 case TUNSETIFF:
960 /* ignore the name, just look at flags */
961 if (get_user(u, &ifr->ifr_flags))
962 return -EFAULT;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000963
964 ret = 0;
Michael S. Tsirkin6ae7feb2014-11-23 17:24:15 +0200965 if ((u & ~MACVTAP_FEATURES) != (IFF_NO_PI | IFF_TAP))
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000966 ret = -EINVAL;
967 else
Michael S. Tsirkin39ec7de2014-12-16 15:04:56 +0200968 q->flags = (q->flags & ~MACVTAP_FEATURES) | u;
Arnd Bergmannb9fb9ee2010-02-18 05:48:17 +0000969
970 return ret;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000971
972 case TUNGETIFF:
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400973 rtnl_lock();
Jason Wang8f475a32013-06-05 23:54:36 +0000974 vlan = macvtap_get_vlan(q);
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400975 if (!vlan) {
976 rtnl_unlock();
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000977 return -ENOLINK;
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400978 }
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000979
980 ret = 0;
Michael S. Tsirkin39ec7de2014-12-16 15:04:56 +0200981 u = q->flags;
Eric Dumazet13707f92011-01-26 19:28:23 +0000982 if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
Michael S. Tsirkin39ec7de2014-12-16 15:04:56 +0200983 put_user(u, &ifr->ifr_flags))
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000984 ret = -EFAULT;
Jason Wang8f475a32013-06-05 23:54:36 +0000985 macvtap_put_vlan(vlan);
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400986 rtnl_unlock();
Arnd Bergmann02df55d2010-02-18 05:45:36 +0000987 return ret;
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000988
Jason Wang815f2362013-06-05 23:54:39 +0000989 case TUNSETQUEUE:
990 if (get_user(u, &ifr->ifr_flags))
991 return -EFAULT;
Vlad Yasevich441ac0f2013-06-25 16:04:19 -0400992 rtnl_lock();
993 ret = macvtap_ioctl_set_queue(file, u);
994 rtnl_unlock();
Jason Wang82a19eb2013-07-16 13:36:33 +0800995 return ret;
Jason Wang815f2362013-06-05 23:54:39 +0000996
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000997 case TUNGETFEATURES:
Michael S. Tsirkin6ae7feb2014-11-23 17:24:15 +0200998 if (put_user(IFF_TAP | IFF_NO_PI | MACVTAP_FEATURES, up))
Arnd Bergmann20d29d72010-01-30 12:24:26 +0000999 return -EFAULT;
1000 return 0;
1001
1002 case TUNSETSNDBUF:
Michael S. Tsirkin3ea79242015-09-18 13:41:09 +03001003 if (get_user(s, sp))
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001004 return -EFAULT;
1005
Michael S. Tsirkin3ea79242015-09-18 13:41:09 +03001006 q->sk.sk_sndbuf = s;
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001007 return 0;
1008
Michael S. Tsirkin55afbd02010-04-29 13:50:48 +03001009 case TUNGETVNETHDRSZ:
1010 s = q->vnet_hdr_sz;
1011 if (put_user(s, sp))
1012 return -EFAULT;
1013 return 0;
1014
1015 case TUNSETVNETHDRSZ:
1016 if (get_user(s, sp))
1017 return -EFAULT;
1018 if (s < (int)sizeof(struct virtio_net_hdr))
1019 return -EINVAL;
1020
1021 q->vnet_hdr_sz = s;
1022 return 0;
1023
Michael S. Tsirkin01b07fb2014-12-16 15:05:10 +02001024 case TUNGETVNETLE:
1025 s = !!(q->flags & MACVTAP_VNET_LE);
1026 if (put_user(s, sp))
1027 return -EFAULT;
1028 return 0;
1029
1030 case TUNSETVNETLE:
1031 if (get_user(s, sp))
1032 return -EFAULT;
1033 if (s)
1034 q->flags |= MACVTAP_VNET_LE;
1035 else
1036 q->flags &= ~MACVTAP_VNET_LE;
1037 return 0;
1038
Greg Kurz8b8e6582015-04-24 14:50:36 +02001039 case TUNGETVNETBE:
1040 return macvtap_get_vnet_be(q, sp);
1041
1042 case TUNSETVNETBE:
1043 return macvtap_set_vnet_be(q, sp);
1044
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001045 case TUNSETOFFLOAD:
1046 /* let the user check for future flags */
1047 if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
Vlad Yaseviche3e3c422015-02-03 16:36:17 -05001048 TUN_F_TSO_ECN | TUN_F_UFO))
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001049 return -EINVAL;
1050
Vlad Yasevich2be5c762013-06-25 16:04:21 -04001051 rtnl_lock();
1052 ret = set_offload(q, arg);
1053 rtnl_unlock();
1054 return ret;
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001055
Justin Cormackb5082082015-05-11 20:00:10 +01001056 case SIOCGIFHWADDR:
1057 rtnl_lock();
1058 vlan = macvtap_get_vlan(q);
1059 if (!vlan) {
1060 rtnl_unlock();
1061 return -ENOLINK;
1062 }
1063 ret = 0;
1064 u = vlan->dev->type;
1065 if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
1066 copy_to_user(&ifr->ifr_hwaddr.sa_data, vlan->dev->dev_addr, ETH_ALEN) ||
1067 put_user(u, &ifr->ifr_hwaddr.sa_family))
1068 ret = -EFAULT;
1069 macvtap_put_vlan(vlan);
1070 rtnl_unlock();
1071 return ret;
1072
1073 case SIOCSIFHWADDR:
Justin Cormack7f460d32015-05-13 19:19:02 +01001074 if (copy_from_user(&sa, &ifr->ifr_hwaddr, sizeof(sa)))
1075 return -EFAULT;
Justin Cormackb5082082015-05-11 20:00:10 +01001076 rtnl_lock();
1077 vlan = macvtap_get_vlan(q);
1078 if (!vlan) {
1079 rtnl_unlock();
1080 return -ENOLINK;
1081 }
Justin Cormack7f460d32015-05-13 19:19:02 +01001082 ret = dev_set_mac_address(vlan->dev, &sa);
Justin Cormackb5082082015-05-11 20:00:10 +01001083 macvtap_put_vlan(vlan);
1084 rtnl_unlock();
1085 return ret;
1086
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001087 default:
1088 return -EINVAL;
1089 }
1090}
1091
1092#ifdef CONFIG_COMPAT
1093static long macvtap_compat_ioctl(struct file *file, unsigned int cmd,
1094 unsigned long arg)
1095{
1096 return macvtap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1097}
1098#endif
1099
Sainath Grandhia8e04692017-02-10 16:03:46 -08001100const struct file_operations macvtap_fops = {
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001101 .owner = THIS_MODULE,
1102 .open = macvtap_open,
1103 .release = macvtap_release,
Al Viro3af0bfe2014-11-07 14:13:53 -05001104 .read_iter = macvtap_read_iter,
Al Virof5ff53b2014-06-19 15:36:49 -04001105 .write_iter = macvtap_write_iter,
Arnd Bergmann20d29d72010-01-30 12:24:26 +00001106 .poll = macvtap_poll,
1107 .llseek = no_llseek,
1108 .unlocked_ioctl = macvtap_ioctl,
1109#ifdef CONFIG_COMPAT
1110 .compat_ioctl = macvtap_compat_ioctl,
1111#endif
1112};
1113
Ying Xue1b784142015-03-02 15:37:48 +08001114static int macvtap_sendmsg(struct socket *sock, struct msghdr *m,
1115 size_t total_len)
Arnd Bergmann501c7742010-02-18 05:46:50 +00001116{
1117 struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
Al Viroc0371da2014-11-24 10:42:55 -05001118 return macvtap_get_user(q, m, &m->msg_iter, m->msg_flags & MSG_DONTWAIT);
Arnd Bergmann501c7742010-02-18 05:46:50 +00001119}
1120
Ying Xue1b784142015-03-02 15:37:48 +08001121static int macvtap_recvmsg(struct socket *sock, struct msghdr *m,
1122 size_t total_len, int flags)
Arnd Bergmann501c7742010-02-18 05:46:50 +00001123{
1124 struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
1125 int ret;
1126 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
1127 return -EINVAL;
Al Viroc0371da2014-11-24 10:42:55 -05001128 ret = macvtap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT);
David S. Millerde2aa472013-12-10 22:06:18 -05001129 if (ret > total_len) {
1130 m->msg_flags |= MSG_TRUNC;
1131 ret = flags & MSG_TRUNC ? ret : total_len;
1132 }
Arnd Bergmann501c7742010-02-18 05:46:50 +00001133 return ret;
1134}
1135
Jason Wang362899b2016-07-15 03:46:31 -04001136static int macvtap_peek_len(struct socket *sock)
1137{
1138 struct macvtap_queue *q = container_of(sock, struct macvtap_queue,
1139 sock);
1140 return skb_array_peek_len(&q->skb_array);
1141}
1142
Arnd Bergmann501c7742010-02-18 05:46:50 +00001143/* Ops structure to mimic raw sockets with tun */
1144static const struct proto_ops macvtap_socket_ops = {
1145 .sendmsg = macvtap_sendmsg,
1146 .recvmsg = macvtap_recvmsg,
Jason Wang362899b2016-07-15 03:46:31 -04001147 .peek_len = macvtap_peek_len,
Arnd Bergmann501c7742010-02-18 05:46:50 +00001148};
1149
1150/* Get an underlying socket object from tun file. Returns error unless file is
1151 * attached to a device. The returned object works like a packet socket, it
1152 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
1153 * holding a reference to the file for as long as the socket is in use. */
1154struct socket *macvtap_get_socket(struct file *file)
1155{
1156 struct macvtap_queue *q;
1157 if (file->f_op != &macvtap_fops)
1158 return ERR_PTR(-EINVAL);
1159 q = file->private_data;
1160 if (!q)
1161 return ERR_PTR(-EBADFD);
1162 return &q->sock;
1163}
1164EXPORT_SYMBOL_GPL(macvtap_get_socket);
1165
Sainath Grandhia8e04692017-02-10 16:03:46 -08001166int macvtap_queue_resize(struct macvlan_dev *vlan)
Jason Wang362899b2016-07-15 03:46:31 -04001167{
1168 struct net_device *dev = vlan->dev;
1169 struct macvtap_queue *q;
1170 struct skb_array **arrays;
1171 int n = vlan->numqueues;
1172 int ret, i = 0;
1173
1174 arrays = kmalloc(sizeof *arrays * n, GFP_KERNEL);
1175 if (!arrays)
1176 return -ENOMEM;
1177
1178 list_for_each_entry(q, &vlan->queue_list, next)
1179 arrays[i++] = &q->skb_array;
1180
1181 ret = skb_array_resize_multiple(arrays, n,
1182 dev->tx_queue_len, GFP_KERNEL);
1183
1184 kfree(arrays);
1185 return ret;
1186}