blob: c31655548da25387b4b3fc169886267870cc78fa [file] [log] [blame]
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001/* Copyright (C) 2009 Red Hat, Inc.
2 * Author: Michael S. Tsirkin <mst@redhat.com>
3 *
4 * This work is licensed under the terms of the GNU GPL, version 2.
5 *
6 * virtio-net server in host kernel.
7 */
8
9#include <linux/compat.h>
10#include <linux/eventfd.h>
11#include <linux/vhost.h>
12#include <linux/virtio_net.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000013#include <linux/miscdevice.h>
14#include <linux/module.h>
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +000015#include <linux/moduleparam.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000016#include <linux/mutex.h>
17#include <linux/workqueue.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000018#include <linux/file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Ingo Molnare6017572017-02-01 16:36:40 +010020#include <linux/sched/clock.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010021#include <linux/sched/signal.h>
Michael S. Tsirkin23cc5a92013-01-23 21:46:47 +010022#include <linux/vmalloc.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000023
24#include <linux/net.h>
25#include <linux/if_packet.h>
26#include <linux/if_arp.h>
27#include <linux/if_tun.h>
Arnd Bergmann501c7742010-02-18 05:46:50 +000028#include <linux/if_macvlan.h>
Sainath Grandhi635b8c82017-02-10 16:03:47 -080029#include <linux/if_tap.h>
Basil Gorc53cff5e2012-05-03 22:55:23 +000030#include <linux/if_vlan.h>
Jason Wangc67df112017-05-17 12:14:45 +080031#include <linux/skb_array.h>
32#include <linux/skbuff.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000033
34#include <net/sock.h>
35
36#include "vhost.h"
37
Michael S. Tsirkinf9611c42012-12-06 14:56:00 +020038static int experimental_zcopytx = 1;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +000039module_param(experimental_zcopytx, int, 0444);
Michael S. Tsirkinf9611c42012-12-06 14:56:00 +020040MODULE_PARM_DESC(experimental_zcopytx, "Enable Zero Copy TX;"
41 " 1 -Enable; 0 - Disable");
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +000042
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000043/* Max number of bytes transferred before requeueing the job.
44 * Using this limit prevents one virtqueue from starving others. */
45#define VHOST_NET_WEIGHT 0x80000
46
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +000047/* MAX number of TX used buffers for outstanding zerocopy */
48#define VHOST_MAX_PEND 128
49#define VHOST_GOODCOPY_LEN 256
50
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000051/*
52 * For transmit, used buffer len is unused; we override it to track buffer
53 * status internally; used for zerocopy tx only.
54 */
55/* Lower device DMA failed */
Michael S. Tsirkinbf995732014-10-24 11:49:27 +030056#define VHOST_DMA_FAILED_LEN ((__force __virtio32)3)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000057/* Lower device DMA done */
Michael S. Tsirkinbf995732014-10-24 11:49:27 +030058#define VHOST_DMA_DONE_LEN ((__force __virtio32)2)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000059/* Lower device DMA in progress */
Michael S. Tsirkinbf995732014-10-24 11:49:27 +030060#define VHOST_DMA_IN_PROGRESS ((__force __virtio32)1)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000061/* Buffer unused */
Michael S. Tsirkinbf995732014-10-24 11:49:27 +030062#define VHOST_DMA_CLEAR_LEN ((__force __virtio32)0)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000063
Michael S. Tsirkinbf995732014-10-24 11:49:27 +030064#define VHOST_DMA_IS_DONE(len) ((__force u32)(len) >= (__force u32)VHOST_DMA_DONE_LEN)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000065
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000066enum {
Asias He8570a6e2013-05-06 16:38:20 +080067 VHOST_NET_FEATURES = VHOST_FEATURES |
68 (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) |
Jason Wang6b1e6cc2016-06-23 02:04:32 -040069 (1ULL << VIRTIO_NET_F_MRG_RXBUF) |
70 (1ULL << VIRTIO_F_IOMMU_PLATFORM)
Asias He8570a6e2013-05-06 16:38:20 +080071};
72
73enum {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000074 VHOST_NET_VQ_RX = 0,
75 VHOST_NET_VQ_TX = 1,
76 VHOST_NET_VQ_MAX = 2,
77};
78
Asias Hefe729a52013-05-06 16:38:24 +080079struct vhost_net_ubuf_ref {
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +020080 /* refcount follows semantics similar to kref:
81 * 0: object is released
82 * 1: no outstanding ubufs
83 * >1: outstanding ubufs
84 */
85 atomic_t refcount;
Asias He28394002013-04-27 15:07:46 +080086 wait_queue_head_t wait;
87 struct vhost_virtqueue *vq;
88};
89
Jason Wangc67df112017-05-17 12:14:45 +080090#define VHOST_RX_BATCH 64
91struct vhost_net_buf {
Jason Wang5990a302018-01-04 11:14:27 +080092 void **queue;
Jason Wangc67df112017-05-17 12:14:45 +080093 int tail;
94 int head;
95};
96
Asias He3ab2e422013-04-27 11:16:48 +080097struct vhost_net_virtqueue {
98 struct vhost_virtqueue vq;
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +030099 size_t vhost_hlen;
100 size_t sock_hlen;
Asias He28394002013-04-27 15:07:46 +0800101 /* vhost zerocopy support fields below: */
102 /* last used idx for outstanding DMA zerocopy buffers */
103 int upend_idx;
104 /* first used idx for DMA done zerocopy buffers */
105 int done_idx;
106 /* an array of userspace buffers info */
107 struct ubuf_info *ubuf_info;
108 /* Reference counting for outstanding ubufs.
109 * Protected by vq mutex. Writers must also take device mutex. */
Asias Hefe729a52013-05-06 16:38:24 +0800110 struct vhost_net_ubuf_ref *ubufs;
Jason Wang5990a302018-01-04 11:14:27 +0800111 struct ptr_ring *rx_ring;
Jason Wangc67df112017-05-17 12:14:45 +0800112 struct vhost_net_buf rxq;
Asias He3ab2e422013-04-27 11:16:48 +0800113};
114
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000115struct vhost_net {
116 struct vhost_dev dev;
Asias He3ab2e422013-04-27 11:16:48 +0800117 struct vhost_net_virtqueue vqs[VHOST_NET_VQ_MAX];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000118 struct vhost_poll poll[VHOST_NET_VQ_MAX];
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000119 /* Number of TX recently submitted.
120 * Protected by tx vq lock. */
121 unsigned tx_packets;
122 /* Number of times zerocopy TX recently failed.
123 * Protected by tx vq lock. */
124 unsigned tx_zcopy_err;
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200125 /* Flush in progress. Protected by tx vq lock. */
126 bool tx_flush;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000127};
128
Asias Hefe729a52013-05-06 16:38:24 +0800129static unsigned vhost_net_zcopy_mask __read_mostly;
Asias He28394002013-04-27 15:07:46 +0800130
Jason Wangc67df112017-05-17 12:14:45 +0800131static void *vhost_net_buf_get_ptr(struct vhost_net_buf *rxq)
132{
133 if (rxq->tail != rxq->head)
134 return rxq->queue[rxq->head];
135 else
136 return NULL;
137}
138
139static int vhost_net_buf_get_size(struct vhost_net_buf *rxq)
140{
141 return rxq->tail - rxq->head;
142}
143
144static int vhost_net_buf_is_empty(struct vhost_net_buf *rxq)
145{
146 return rxq->tail == rxq->head;
147}
148
149static void *vhost_net_buf_consume(struct vhost_net_buf *rxq)
150{
151 void *ret = vhost_net_buf_get_ptr(rxq);
152 ++rxq->head;
153 return ret;
154}
155
156static int vhost_net_buf_produce(struct vhost_net_virtqueue *nvq)
157{
158 struct vhost_net_buf *rxq = &nvq->rxq;
159
160 rxq->head = 0;
Jason Wang5990a302018-01-04 11:14:27 +0800161 rxq->tail = ptr_ring_consume_batched(nvq->rx_ring, rxq->queue,
Jason Wangc67df112017-05-17 12:14:45 +0800162 VHOST_RX_BATCH);
163 return rxq->tail;
164}
165
166static void vhost_net_buf_unproduce(struct vhost_net_virtqueue *nvq)
167{
168 struct vhost_net_buf *rxq = &nvq->rxq;
169
Jason Wang5990a302018-01-04 11:14:27 +0800170 if (nvq->rx_ring && !vhost_net_buf_is_empty(rxq)) {
171 ptr_ring_unconsume(nvq->rx_ring, rxq->queue + rxq->head,
172 vhost_net_buf_get_size(rxq),
173 __skb_array_destroy_skb);
Jason Wangc67df112017-05-17 12:14:45 +0800174 rxq->head = rxq->tail = 0;
175 }
176}
177
178static int vhost_net_buf_peek(struct vhost_net_virtqueue *nvq)
179{
180 struct vhost_net_buf *rxq = &nvq->rxq;
181
182 if (!vhost_net_buf_is_empty(rxq))
183 goto out;
184
185 if (!vhost_net_buf_produce(nvq))
186 return 0;
187
188out:
189 return __skb_array_len_with_tag(vhost_net_buf_get_ptr(rxq));
190}
191
192static void vhost_net_buf_init(struct vhost_net_buf *rxq)
193{
194 rxq->head = rxq->tail = 0;
195}
196
Asias Hefe729a52013-05-06 16:38:24 +0800197static void vhost_net_enable_zcopy(int vq)
Asias He28394002013-04-27 15:07:46 +0800198{
Asias Hefe729a52013-05-06 16:38:24 +0800199 vhost_net_zcopy_mask |= 0x1 << vq;
Asias He28394002013-04-27 15:07:46 +0800200}
201
Asias Hefe729a52013-05-06 16:38:24 +0800202static struct vhost_net_ubuf_ref *
203vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy)
Asias He28394002013-04-27 15:07:46 +0800204{
Asias Hefe729a52013-05-06 16:38:24 +0800205 struct vhost_net_ubuf_ref *ubufs;
Asias He28394002013-04-27 15:07:46 +0800206 /* No zero copy backend? Nothing to count. */
207 if (!zcopy)
208 return NULL;
209 ubufs = kmalloc(sizeof(*ubufs), GFP_KERNEL);
210 if (!ubufs)
211 return ERR_PTR(-ENOMEM);
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200212 atomic_set(&ubufs->refcount, 1);
Asias He28394002013-04-27 15:07:46 +0800213 init_waitqueue_head(&ubufs->wait);
214 ubufs->vq = vq;
215 return ubufs;
216}
217
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200218static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref *ubufs)
Asias He28394002013-04-27 15:07:46 +0800219{
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200220 int r = atomic_sub_return(1, &ubufs->refcount);
221 if (unlikely(!r))
222 wake_up(&ubufs->wait);
223 return r;
Asias He28394002013-04-27 15:07:46 +0800224}
225
Asias Hefe729a52013-05-06 16:38:24 +0800226static void vhost_net_ubuf_put_and_wait(struct vhost_net_ubuf_ref *ubufs)
Asias He28394002013-04-27 15:07:46 +0800227{
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200228 vhost_net_ubuf_put(ubufs);
229 wait_event(ubufs->wait, !atomic_read(&ubufs->refcount));
Michael S. Tsirkinc38e39c2013-06-25 17:29:46 +0300230}
231
232static void vhost_net_ubuf_put_wait_and_free(struct vhost_net_ubuf_ref *ubufs)
233{
234 vhost_net_ubuf_put_and_wait(ubufs);
Asias He28394002013-04-27 15:07:46 +0800235 kfree(ubufs);
236}
237
Asias Heb1ad8492013-05-06 11:16:00 +0800238static void vhost_net_clear_ubuf_info(struct vhost_net *n)
239{
Asias Heb1ad8492013-05-06 11:16:00 +0800240 int i;
241
Michael S. Tsirkin288cfe72013-06-06 15:20:46 +0300242 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
243 kfree(n->vqs[i].ubuf_info);
244 n->vqs[i].ubuf_info = NULL;
Asias Heb1ad8492013-05-06 11:16:00 +0800245 }
246}
247
Asias He0a1febf2013-06-05 21:17:38 +0800248static int vhost_net_set_ubuf_info(struct vhost_net *n)
Asias He28394002013-04-27 15:07:46 +0800249{
250 bool zcopy;
251 int i;
252
Michael S. Tsirkin288cfe72013-06-06 15:20:46 +0300253 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
Asias Hefe729a52013-05-06 16:38:24 +0800254 zcopy = vhost_net_zcopy_mask & (0x1 << i);
Asias He28394002013-04-27 15:07:46 +0800255 if (!zcopy)
256 continue;
257 n->vqs[i].ubuf_info = kmalloc(sizeof(*n->vqs[i].ubuf_info) *
258 UIO_MAXIOV, GFP_KERNEL);
259 if (!n->vqs[i].ubuf_info)
260 goto err;
261 }
262 return 0;
263
264err:
Michael S. Tsirkin288cfe72013-06-06 15:20:46 +0300265 vhost_net_clear_ubuf_info(n);
Asias He28394002013-04-27 15:07:46 +0800266 return -ENOMEM;
267}
268
Asias He0a1febf2013-06-05 21:17:38 +0800269static void vhost_net_vq_reset(struct vhost_net *n)
Asias He28394002013-04-27 15:07:46 +0800270{
271 int i;
272
Michael S. Tsirkin288cfe72013-06-06 15:20:46 +0300273 vhost_net_clear_ubuf_info(n);
274
Asias He28394002013-04-27 15:07:46 +0800275 for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
276 n->vqs[i].done_idx = 0;
277 n->vqs[i].upend_idx = 0;
278 n->vqs[i].ubufs = NULL;
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300279 n->vqs[i].vhost_hlen = 0;
280 n->vqs[i].sock_hlen = 0;
Jason Wangc67df112017-05-17 12:14:45 +0800281 vhost_net_buf_init(&n->vqs[i].rxq);
Asias He28394002013-04-27 15:07:46 +0800282 }
283
284}
285
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000286static void vhost_net_tx_packet(struct vhost_net *net)
287{
288 ++net->tx_packets;
289 if (net->tx_packets < 1024)
290 return;
291 net->tx_packets = 0;
292 net->tx_zcopy_err = 0;
293}
294
295static void vhost_net_tx_err(struct vhost_net *net)
296{
297 ++net->tx_zcopy_err;
298}
299
300static bool vhost_net_tx_select_zcopy(struct vhost_net *net)
301{
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200302 /* TX flush waits for outstanding DMAs to be done.
303 * Don't start new DMAs.
304 */
305 return !net->tx_flush &&
306 net->tx_packets / 64 >= net->tx_zcopy_err;
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000307}
308
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000309static bool vhost_sock_zcopy(struct socket *sock)
310{
311 return unlikely(experimental_zcopytx) &&
312 sock_flag(sock->sk, SOCK_ZEROCOPY);
313}
314
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000315/* In case of DMA done not in order in lower device driver for some reason.
316 * upend_idx is used to track end of used idx, done_idx is used to track head
317 * of used idx. Once lower device DMA done contiguously, we will signal KVM
318 * guest used idx.
319 */
Jason Wang094afe72013-09-02 16:40:56 +0800320static void vhost_zerocopy_signal_used(struct vhost_net *net,
321 struct vhost_virtqueue *vq)
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000322{
Asias He28394002013-04-27 15:07:46 +0800323 struct vhost_net_virtqueue *nvq =
324 container_of(vq, struct vhost_net_virtqueue, vq);
Jason Wangc92112a2013-09-02 16:40:57 +0800325 int i, add;
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000326 int j = 0;
327
Asias He28394002013-04-27 15:07:46 +0800328 for (i = nvq->done_idx; i != nvq->upend_idx; i = (i + 1) % UIO_MAXIOV) {
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000329 if (vq->heads[i].len == VHOST_DMA_FAILED_LEN)
330 vhost_net_tx_err(net);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000331 if (VHOST_DMA_IS_DONE(vq->heads[i].len)) {
332 vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000333 ++j;
334 } else
335 break;
336 }
Jason Wangc92112a2013-09-02 16:40:57 +0800337 while (j) {
338 add = min(UIO_MAXIOV - nvq->done_idx, j);
339 vhost_add_used_and_signal_n(vq->dev, vq,
340 &vq->heads[nvq->done_idx], add);
341 nvq->done_idx = (nvq->done_idx + add) % UIO_MAXIOV;
342 j -= add;
343 }
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000344}
345
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000346static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000347{
Asias Hefe729a52013-05-06 16:38:24 +0800348 struct vhost_net_ubuf_ref *ubufs = ubuf->ctx;
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000349 struct vhost_virtqueue *vq = ubufs->vq;
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200350 int cnt;
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000351
Michael S. Tsirkinb0c057c2014-02-13 11:45:11 +0200352 rcu_read_lock_bh();
353
Jason Wang19c73b32013-09-02 16:41:00 +0800354 /* set len to mark this desc buffers done DMA */
355 vq->heads[ubuf->desc].len = success ?
356 VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200357 cnt = vhost_net_ubuf_put(ubufs);
Jason Wang19c73b32013-09-02 16:41:00 +0800358
Michael S. Tsirkin24eb21a2012-11-01 09:16:55 +0000359 /*
360 * Trigger polling thread if guest stopped submitting new buffers:
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200361 * in this case, the refcount after decrement will eventually reach 1.
Michael S. Tsirkin24eb21a2012-11-01 09:16:55 +0000362 * We also trigger polling periodically after each 16 packets
363 * (the value 16 here is more or less arbitrary, it's tuned to trigger
364 * less than 10% of times).
365 */
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200366 if (cnt <= 1 || !(cnt % 16))
Michael S. Tsirkin24eb21a2012-11-01 09:16:55 +0000367 vhost_poll_queue(&vq->poll);
Michael S. Tsirkinb0c057c2014-02-13 11:45:11 +0200368
369 rcu_read_unlock_bh();
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000370}
371
Jason Wang03088132016-03-04 06:24:53 -0500372static inline unsigned long busy_clock(void)
373{
374 return local_clock() >> 10;
375}
376
377static bool vhost_can_busy_poll(struct vhost_dev *dev,
378 unsigned long endtime)
379{
380 return likely(!need_resched()) &&
381 likely(!time_after(busy_clock(), endtime)) &&
382 likely(!signal_pending(current)) &&
383 !vhost_has_work(dev);
384}
385
Jason Wang8241a1e2016-06-01 01:56:33 -0400386static void vhost_net_disable_vq(struct vhost_net *n,
387 struct vhost_virtqueue *vq)
388{
389 struct vhost_net_virtqueue *nvq =
390 container_of(vq, struct vhost_net_virtqueue, vq);
391 struct vhost_poll *poll = n->poll + (nvq - n->vqs);
392 if (!vq->private_data)
393 return;
394 vhost_poll_stop(poll);
395}
396
397static int vhost_net_enable_vq(struct vhost_net *n,
398 struct vhost_virtqueue *vq)
399{
400 struct vhost_net_virtqueue *nvq =
401 container_of(vq, struct vhost_net_virtqueue, vq);
402 struct vhost_poll *poll = n->poll + (nvq - n->vqs);
403 struct socket *sock;
404
405 sock = vq->private_data;
406 if (!sock)
407 return 0;
408
409 return vhost_poll_start(poll, sock->file);
410}
411
Jason Wang03088132016-03-04 06:24:53 -0500412static int vhost_net_tx_get_vq_desc(struct vhost_net *net,
413 struct vhost_virtqueue *vq,
414 struct iovec iov[], unsigned int iov_size,
415 unsigned int *out_num, unsigned int *in_num)
416{
417 unsigned long uninitialized_var(endtime);
418 int r = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400419 out_num, in_num, NULL, NULL);
Jason Wang03088132016-03-04 06:24:53 -0500420
421 if (r == vq->num && vq->busyloop_timeout) {
422 preempt_disable();
423 endtime = busy_clock() + vq->busyloop_timeout;
424 while (vhost_can_busy_poll(vq->dev, endtime) &&
425 vhost_vq_avail_empty(vq->dev, vq))
Christian Borntraegerf2f09a42016-10-25 11:03:14 +0200426 cpu_relax();
Jason Wang03088132016-03-04 06:24:53 -0500427 preempt_enable();
428 r = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400429 out_num, in_num, NULL, NULL);
Jason Wang03088132016-03-04 06:24:53 -0500430 }
431
432 return r;
433}
434
Jason Wang0ed005c2017-01-18 15:02:02 +0800435static bool vhost_exceeds_maxpend(struct vhost_net *net)
436{
437 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
438 struct vhost_virtqueue *vq = &nvq->vq;
439
Willem de Bruijn1e6f7452017-10-06 13:22:31 -0400440 return (nvq->upend_idx + UIO_MAXIOV - nvq->done_idx) % UIO_MAXIOV >
441 min_t(unsigned int, VHOST_MAX_PEND, vq->num >> 2);
Jason Wang0ed005c2017-01-18 15:02:02 +0800442}
443
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000444/* Expects to be always run from workqueue - which acts as
445 * read-size critical section for our kind of RCU. */
446static void handle_tx(struct vhost_net *net)
447{
Asias He28394002013-04-27 15:07:46 +0800448 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300449 struct vhost_virtqueue *vq = &nvq->vq;
Al Viro98a527a2014-12-10 15:00:58 -0500450 unsigned out, in;
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300451 int head;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000452 struct msghdr msg = {
453 .msg_name = NULL,
454 .msg_namelen = 0,
455 .msg_control = NULL,
456 .msg_controllen = 0,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000457 .msg_flags = MSG_DONTWAIT,
458 };
459 size_t len, total_len = 0;
Jason Wang70181d512013-04-10 20:50:48 +0000460 int err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000461 size_t hdr_size;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100462 struct socket *sock;
Asias Hefe729a52013-05-06 16:38:24 +0800463 struct vhost_net_ubuf_ref *uninitialized_var(ubufs);
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200464 bool zcopy, zcopy_used;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100465
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000466 mutex_lock(&vq->mutex);
Asias He2e26af72013-05-07 14:54:33 +0800467 sock = vq->private_data;
468 if (!sock)
469 goto out;
470
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400471 if (!vq_iotlb_prefetch(vq))
472 goto out;
473
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300474 vhost_disable_notify(&net->dev, vq);
Jason Wangfeb88922017-11-13 11:45:34 +0800475 vhost_net_disable_vq(net, vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000476
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300477 hdr_size = nvq->vhost_hlen;
Asias He28394002013-04-27 15:07:46 +0800478 zcopy = nvq->ubufs;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000479
480 for (;;) {
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000481 /* Release DMAs done buffers first */
482 if (zcopy)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000483 vhost_zerocopy_signal_used(net, vq);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000484
Jason Wangf7c6be42013-09-02 16:41:01 +0800485
Jason Wang03088132016-03-04 06:24:53 -0500486 head = vhost_net_tx_get_vq_desc(net, vq, vq->iov,
487 ARRAY_SIZE(vq->iov),
488 &out, &in);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300489 /* On error, stop handling until the next kick. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300490 if (unlikely(head < 0))
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300491 break;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000492 /* Nothing new? Wait for eventfd to tell us they refilled. */
493 if (head == vq->num) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300494 if (unlikely(vhost_enable_notify(&net->dev, vq))) {
495 vhost_disable_notify(&net->dev, vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000496 continue;
497 }
498 break;
499 }
500 if (in) {
501 vq_err(vq, "Unexpected descriptor format for TX: "
502 "out %d, int %d\n", out, in);
503 break;
504 }
505 /* Skip header. TODO: support TSO. */
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000506 len = iov_length(vq->iov, out);
Al Viroc0371da2014-11-24 10:42:55 -0500507 iov_iter_init(&msg.msg_iter, WRITE, vq->iov, out, len);
Al Viro98a527a2014-12-10 15:00:58 -0500508 iov_iter_advance(&msg.msg_iter, hdr_size);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000509 /* Sanity check */
Al Viro01e97e62014-12-15 21:39:31 -0500510 if (!msg_data_left(&msg)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000511 vq_err(vq, "Unexpected header len for TX: "
512 "%zd expected %zd\n",
Al Viro98a527a2014-12-10 15:00:58 -0500513 len, hdr_size);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000514 break;
515 }
Al Viro01e97e62014-12-15 21:39:31 -0500516 len = msg_data_left(&msg);
Jason Wangce21a022013-09-02 16:40:59 +0800517
518 zcopy_used = zcopy && len >= VHOST_GOODCOPY_LEN
Willem de Bruijn1e6f7452017-10-06 13:22:31 -0400519 && !vhost_exceeds_maxpend(net)
Jason Wangce21a022013-09-02 16:40:59 +0800520 && vhost_net_tx_select_zcopy(net);
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200521
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000522 /* use msg_control to pass vhost zerocopy ubuf info to skb */
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200523 if (zcopy_used) {
Jason Wangce21a022013-09-02 16:40:59 +0800524 struct ubuf_info *ubuf;
525 ubuf = nvq->ubuf_info + nvq->upend_idx;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000526
Michael S. Tsirkin8b386942014-10-24 14:19:48 +0300527 vq->heads[nvq->upend_idx].id = cpu_to_vhost32(vq, head);
Jason Wangce21a022013-09-02 16:40:59 +0800528 vq->heads[nvq->upend_idx].len = VHOST_DMA_IN_PROGRESS;
529 ubuf->callback = vhost_zerocopy_callback;
530 ubuf->ctx = nvq->ubufs;
531 ubuf->desc = nvq->upend_idx;
Eric Dumazetc1d1b432017-08-31 16:48:22 -0700532 refcount_set(&ubuf->refcnt, 1);
Jason Wangce21a022013-09-02 16:40:59 +0800533 msg.msg_control = ubuf;
534 msg.msg_controllen = sizeof(ubuf);
535 ubufs = nvq->ubufs;
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200536 atomic_inc(&ubufs->refcount);
Asias He28394002013-04-27 15:07:46 +0800537 nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV;
Jason Wangce21a022013-09-02 16:40:59 +0800538 } else {
Jason Wang4364d5f2013-06-05 15:40:46 +0800539 msg.msg_control = NULL;
Jason Wangce21a022013-09-02 16:40:59 +0800540 ubufs = NULL;
541 }
Jason Wang0ed005c2017-01-18 15:02:02 +0800542
543 total_len += len;
544 if (total_len < VHOST_NET_WEIGHT &&
545 !vhost_vq_avail_empty(&net->dev, vq) &&
546 likely(!vhost_exceeds_maxpend(net))) {
547 msg.msg_flags |= MSG_MORE;
548 } else {
549 msg.msg_flags &= ~MSG_MORE;
550 }
551
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000552 /* TODO: Check specific error and bomb out unless ENOBUFS? */
Ying Xue1b784142015-03-02 15:37:48 +0800553 err = sock->ops->sendmsg(sock, &msg, len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000554 if (unlikely(err < 0)) {
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200555 if (zcopy_used) {
Jason Wangce21a022013-09-02 16:40:59 +0800556 vhost_net_ubuf_put(ubufs);
Asias He28394002013-04-27 15:07:46 +0800557 nvq->upend_idx = ((unsigned)nvq->upend_idx - 1)
558 % UIO_MAXIOV;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000559 }
David Stevens8dd014a2010-07-27 18:52:21 +0300560 vhost_discard_vq_desc(vq, 1);
Jason Wangfeb88922017-11-13 11:45:34 +0800561 vhost_net_enable_vq(net, vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000562 break;
563 }
564 if (err != len)
Michael S. Tsirkin95c0ec62010-06-24 17:10:25 +0300565 pr_debug("Truncated TX packet: "
566 " len %d != %zd\n", err, len);
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200567 if (!zcopy_used)
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000568 vhost_add_used_and_signal(&net->dev, vq, head, 0);
Jason Wangc8fb2172012-05-02 11:42:41 +0800569 else
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000570 vhost_zerocopy_signal_used(net, vq);
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000571 vhost_net_tx_packet(net);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000572 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
573 vhost_poll_queue(&vq->poll);
574 break;
575 }
576 }
Asias He2e26af72013-05-07 14:54:33 +0800577out:
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000578 mutex_unlock(&vq->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000579}
580
Jason Wangc67df112017-05-17 12:14:45 +0800581static int peek_head_len(struct vhost_net_virtqueue *rvq, struct sock *sk)
David Stevens8dd014a2010-07-27 18:52:21 +0300582{
583 struct sk_buff *head;
584 int len = 0;
Jason Wang783e3982011-01-17 16:11:17 +0800585 unsigned long flags;
David Stevens8dd014a2010-07-27 18:52:21 +0300586
Jason Wang5990a302018-01-04 11:14:27 +0800587 if (rvq->rx_ring)
Jason Wangc67df112017-05-17 12:14:45 +0800588 return vhost_net_buf_peek(rvq);
Jason Wang1576d982016-06-30 14:45:36 +0800589
Jason Wang783e3982011-01-17 16:11:17 +0800590 spin_lock_irqsave(&sk->sk_receive_queue.lock, flags);
David Stevens8dd014a2010-07-27 18:52:21 +0300591 head = skb_peek(&sk->sk_receive_queue);
Basil Gorc53cff5e2012-05-03 22:55:23 +0000592 if (likely(head)) {
David Stevens8dd014a2010-07-27 18:52:21 +0300593 len = head->len;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100594 if (skb_vlan_tag_present(head))
Basil Gorc53cff5e2012-05-03 22:55:23 +0000595 len += VLAN_HLEN;
596 }
597
Jason Wang783e3982011-01-17 16:11:17 +0800598 spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags);
David Stevens8dd014a2010-07-27 18:52:21 +0300599 return len;
600}
601
Jason Wang1576d982016-06-30 14:45:36 +0800602static int sk_has_rx_data(struct sock *sk)
603{
604 struct socket *sock = sk->sk_socket;
605
606 if (sock->ops->peek_len)
607 return sock->ops->peek_len(sock);
608
609 return skb_queue_empty(&sk->sk_receive_queue);
610}
611
Jason Wang03088132016-03-04 06:24:53 -0500612static int vhost_net_rx_peek_head_len(struct vhost_net *net, struct sock *sk)
613{
Jason Wangc67df112017-05-17 12:14:45 +0800614 struct vhost_net_virtqueue *rvq = &net->vqs[VHOST_NET_VQ_RX];
Jason Wang03088132016-03-04 06:24:53 -0500615 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
616 struct vhost_virtqueue *vq = &nvq->vq;
617 unsigned long uninitialized_var(endtime);
Jason Wangc67df112017-05-17 12:14:45 +0800618 int len = peek_head_len(rvq, sk);
Jason Wang03088132016-03-04 06:24:53 -0500619
620 if (!len && vq->busyloop_timeout) {
621 /* Both tx vq and rx socket were polled here */
622 mutex_lock(&vq->mutex);
623 vhost_disable_notify(&net->dev, vq);
624
625 preempt_disable();
626 endtime = busy_clock() + vq->busyloop_timeout;
627
628 while (vhost_can_busy_poll(&net->dev, endtime) &&
Jason Wang1576d982016-06-30 14:45:36 +0800629 !sk_has_rx_data(sk) &&
Jason Wang03088132016-03-04 06:24:53 -0500630 vhost_vq_avail_empty(&net->dev, vq))
Christian Borntraegerf2f09a42016-10-25 11:03:14 +0200631 cpu_relax();
Jason Wang03088132016-03-04 06:24:53 -0500632
633 preempt_enable();
634
Jason Wang8b949be2017-09-05 09:22:05 +0800635 if (!vhost_vq_avail_empty(&net->dev, vq))
Jason Wang03088132016-03-04 06:24:53 -0500636 vhost_poll_queue(&vq->poll);
Jason Wang8b949be2017-09-05 09:22:05 +0800637 else if (unlikely(vhost_enable_notify(&net->dev, vq))) {
638 vhost_disable_notify(&net->dev, vq);
639 vhost_poll_queue(&vq->poll);
640 }
641
Jason Wang03088132016-03-04 06:24:53 -0500642 mutex_unlock(&vq->mutex);
643
Jason Wangc67df112017-05-17 12:14:45 +0800644 len = peek_head_len(rvq, sk);
Jason Wang03088132016-03-04 06:24:53 -0500645 }
646
647 return len;
648}
649
David Stevens8dd014a2010-07-27 18:52:21 +0300650/* This is a multi-buffer version of vhost_get_desc, that works if
651 * vq has read descriptors only.
652 * @vq - the relevant virtqueue
653 * @datalen - data length we'll be reading
654 * @iovcount - returned count of io vectors we fill
655 * @log - vhost log
656 * @log_num - log offset
Jason Wang94249362011-01-17 16:11:08 +0800657 * @quota - headcount quota, 1 for big buffer
David Stevens8dd014a2010-07-27 18:52:21 +0300658 * returns number of buffer heads allocated, negative on error
659 */
660static int get_rx_bufs(struct vhost_virtqueue *vq,
661 struct vring_used_elem *heads,
662 int datalen,
663 unsigned *iovcount,
664 struct vhost_log *log,
Jason Wang94249362011-01-17 16:11:08 +0800665 unsigned *log_num,
666 unsigned int quota)
David Stevens8dd014a2010-07-27 18:52:21 +0300667{
668 unsigned int out, in;
669 int seg = 0;
670 int headcount = 0;
671 unsigned d;
672 int r, nlogs = 0;
Michael S. Tsirkin8b386942014-10-24 14:19:48 +0300673 /* len is always initialized before use since we are always called with
674 * datalen > 0.
675 */
676 u32 uninitialized_var(len);
David Stevens8dd014a2010-07-27 18:52:21 +0300677
Jason Wang94249362011-01-17 16:11:08 +0800678 while (datalen > 0 && headcount < quota) {
Jason Wange0e9b402010-09-14 23:53:05 +0800679 if (unlikely(seg >= UIO_MAXIOV)) {
David Stevens8dd014a2010-07-27 18:52:21 +0300680 r = -ENOBUFS;
681 goto err;
682 }
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300683 r = vhost_get_vq_desc(vq, vq->iov + seg,
David Stevens8dd014a2010-07-27 18:52:21 +0300684 ARRAY_SIZE(vq->iov) - seg, &out,
685 &in, log, log_num);
Michael S. Tsirkina39ee442014-03-27 12:53:37 +0200686 if (unlikely(r < 0))
687 goto err;
688
689 d = r;
David Stevens8dd014a2010-07-27 18:52:21 +0300690 if (d == vq->num) {
691 r = 0;
692 goto err;
693 }
694 if (unlikely(out || in <= 0)) {
695 vq_err(vq, "unexpected descriptor format for RX: "
696 "out %d, in %d\n", out, in);
697 r = -EINVAL;
698 goto err;
699 }
700 if (unlikely(log)) {
701 nlogs += *log_num;
702 log += *log_num;
703 }
Michael S. Tsirkin8b386942014-10-24 14:19:48 +0300704 heads[headcount].id = cpu_to_vhost32(vq, d);
705 len = iov_length(vq->iov + seg, in);
706 heads[headcount].len = cpu_to_vhost32(vq, len);
707 datalen -= len;
David Stevens8dd014a2010-07-27 18:52:21 +0300708 ++headcount;
709 seg += in;
710 }
Michael S. Tsirkin99975cc2015-01-07 10:51:00 +0200711 heads[headcount - 1].len = cpu_to_vhost32(vq, len + datalen);
David Stevens8dd014a2010-07-27 18:52:21 +0300712 *iovcount = seg;
713 if (unlikely(log))
714 *log_num = nlogs;
Michael S. Tsirkind8316f32014-03-27 12:00:26 +0200715
716 /* Detect overrun */
717 if (unlikely(datalen > 0)) {
718 r = UIO_MAXIOV + 1;
719 goto err;
720 }
David Stevens8dd014a2010-07-27 18:52:21 +0300721 return headcount;
722err:
723 vhost_discard_vq_desc(vq, headcount);
724 return r;
725}
726
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000727/* Expects to be always run from workqueue - which acts as
728 * read-size critical section for our kind of RCU. */
Jason Wang94249362011-01-17 16:11:08 +0800729static void handle_rx(struct vhost_net *net)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000730{
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300731 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX];
732 struct vhost_virtqueue *vq = &nvq->vq;
David Stevens8dd014a2010-07-27 18:52:21 +0300733 unsigned uninitialized_var(in), log;
734 struct vhost_log *vq_log;
735 struct msghdr msg = {
736 .msg_name = NULL,
737 .msg_namelen = 0,
738 .msg_control = NULL, /* FIXME: get and handle RX aux data. */
739 .msg_controllen = 0,
David Stevens8dd014a2010-07-27 18:52:21 +0300740 .msg_flags = MSG_DONTWAIT,
741 };
Jason Wang0960b642015-02-15 16:35:17 +0800742 struct virtio_net_hdr hdr = {
743 .flags = 0,
744 .gso_type = VIRTIO_NET_HDR_GSO_NONE
David Stevens8dd014a2010-07-27 18:52:21 +0300745 };
David Stevens8dd014a2010-07-27 18:52:21 +0300746 size_t total_len = 0;
Michael S. Tsirkin910a5782012-10-24 20:37:51 +0200747 int err, mergeable;
748 s16 headcount;
David Stevens8dd014a2010-07-27 18:52:21 +0300749 size_t vhost_hlen, sock_hlen;
750 size_t vhost_len, sock_len;
Asias He2e26af72013-05-07 14:54:33 +0800751 struct socket *sock;
Al Viroba7438ae2014-12-10 15:51:28 -0500752 struct iov_iter fixup;
Jason Wang0960b642015-02-15 16:35:17 +0800753 __virtio16 num_buffers;
David Stevens8dd014a2010-07-27 18:52:21 +0300754
David Stevens8dd014a2010-07-27 18:52:21 +0300755 mutex_lock(&vq->mutex);
Asias He2e26af72013-05-07 14:54:33 +0800756 sock = vq->private_data;
757 if (!sock)
758 goto out;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400759
760 if (!vq_iotlb_prefetch(vq))
761 goto out;
762
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300763 vhost_disable_notify(&net->dev, vq);
Jason Wang8241a1e2016-06-01 01:56:33 -0400764 vhost_net_disable_vq(net, vq);
Asias He2e26af72013-05-07 14:54:33 +0800765
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300766 vhost_hlen = nvq->vhost_hlen;
767 sock_hlen = nvq->sock_hlen;
David Stevens8dd014a2010-07-27 18:52:21 +0300768
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300769 vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
David Stevens8dd014a2010-07-27 18:52:21 +0300770 vq->log : NULL;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300771 mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF);
David Stevens8dd014a2010-07-27 18:52:21 +0300772
Jason Wang03088132016-03-04 06:24:53 -0500773 while ((sock_len = vhost_net_rx_peek_head_len(net, sock->sk))) {
David Stevens8dd014a2010-07-27 18:52:21 +0300774 sock_len += sock_hlen;
775 vhost_len = sock_len + vhost_hlen;
776 headcount = get_rx_bufs(vq, vq->heads, vhost_len,
Jason Wang94249362011-01-17 16:11:08 +0800777 &in, vq_log, &log,
778 likely(mergeable) ? UIO_MAXIOV : 1);
David Stevens8dd014a2010-07-27 18:52:21 +0300779 /* On error, stop handling until the next kick. */
780 if (unlikely(headcount < 0))
Jason Wang8241a1e2016-06-01 01:56:33 -0400781 goto out;
David Stevens8dd014a2010-07-27 18:52:21 +0300782 /* OK, now we need to know about added descriptors. */
783 if (!headcount) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300784 if (unlikely(vhost_enable_notify(&net->dev, vq))) {
David Stevens8dd014a2010-07-27 18:52:21 +0300785 /* They have slipped one in as we were
786 * doing that: check again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300787 vhost_disable_notify(&net->dev, vq);
David Stevens8dd014a2010-07-27 18:52:21 +0300788 continue;
789 }
790 /* Nothing new? Wait for eventfd to tell us
791 * they refilled. */
Jason Wang8241a1e2016-06-01 01:56:33 -0400792 goto out;
David Stevens8dd014a2010-07-27 18:52:21 +0300793 }
Jason Wang5990a302018-01-04 11:14:27 +0800794 if (nvq->rx_ring)
Wei Xu6e474082017-12-01 05:10:36 -0500795 msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
796 /* On overrun, truncate and discard */
797 if (unlikely(headcount > UIO_MAXIOV)) {
798 iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
799 err = sock->ops->recvmsg(sock, &msg,
800 1, MSG_DONTWAIT | MSG_TRUNC);
801 pr_debug("Discarded rx packet: len %zd\n", sock_len);
802 continue;
803 }
David Stevens8dd014a2010-07-27 18:52:21 +0300804 /* We don't need to be notified again. */
Al Viroba7438ae2014-12-10 15:51:28 -0500805 iov_iter_init(&msg.msg_iter, READ, vq->iov, in, vhost_len);
806 fixup = msg.msg_iter;
807 if (unlikely((vhost_hlen))) {
808 /* We will supply the header ourselves
809 * TODO: support TSO.
810 */
811 iov_iter_advance(&msg.msg_iter, vhost_hlen);
Al Viroba7438ae2014-12-10 15:51:28 -0500812 }
Ying Xue1b784142015-03-02 15:37:48 +0800813 err = sock->ops->recvmsg(sock, &msg,
David Stevens8dd014a2010-07-27 18:52:21 +0300814 sock_len, MSG_DONTWAIT | MSG_TRUNC);
815 /* Userspace might have consumed the packet meanwhile:
816 * it's not supposed to do this usually, but might be hard
817 * to prevent. Discard data we got (if any) and keep going. */
818 if (unlikely(err != sock_len)) {
819 pr_debug("Discarded rx packet: "
820 " len %d, expected %zd\n", err, sock_len);
821 vhost_discard_vq_desc(vq, headcount);
822 continue;
823 }
Al Viroba7438ae2014-12-10 15:51:28 -0500824 /* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */
Michael S. Tsirkin4c5a8442015-02-25 15:19:28 +0100825 if (unlikely(vhost_hlen)) {
826 if (copy_to_iter(&hdr, sizeof(hdr),
827 &fixup) != sizeof(hdr)) {
828 vq_err(vq, "Unable to write vnet_hdr "
829 "at addr %p\n", vq->iov->iov_base);
Jason Wang8241a1e2016-06-01 01:56:33 -0400830 goto out;
Michael S. Tsirkin4c5a8442015-02-25 15:19:28 +0100831 }
832 } else {
833 /* Header came from socket; we'll need to patch
834 * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF
835 */
836 iov_iter_advance(&fixup, sizeof(hdr));
David Stevens8dd014a2010-07-27 18:52:21 +0300837 }
838 /* TODO: Should check and handle checksum. */
Michael S. Tsirkin5201aa42015-02-03 11:07:06 +0200839
Jason Wang0960b642015-02-15 16:35:17 +0800840 num_buffers = cpu_to_vhost16(vq, headcount);
Jason Wangcfbdab92011-01-17 16:10:59 +0800841 if (likely(mergeable) &&
Michael S. Tsirkin0d79a492015-02-25 15:20:01 +0100842 copy_to_iter(&num_buffers, sizeof num_buffers,
843 &fixup) != sizeof num_buffers) {
David Stevens8dd014a2010-07-27 18:52:21 +0300844 vq_err(vq, "Failed num_buffers write");
845 vhost_discard_vq_desc(vq, headcount);
Jason Wang8241a1e2016-06-01 01:56:33 -0400846 goto out;
David Stevens8dd014a2010-07-27 18:52:21 +0300847 }
848 vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
849 headcount);
850 if (unlikely(vq_log))
851 vhost_log_write(vq, vq_log, log, vhost_len);
852 total_len += vhost_len;
853 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
854 vhost_poll_queue(&vq->poll);
Jason Wang8241a1e2016-06-01 01:56:33 -0400855 goto out;
David Stevens8dd014a2010-07-27 18:52:21 +0300856 }
857 }
Jason Wang8241a1e2016-06-01 01:56:33 -0400858 vhost_net_enable_vq(net, vq);
Asias He2e26af72013-05-07 14:54:33 +0800859out:
David Stevens8dd014a2010-07-27 18:52:21 +0300860 mutex_unlock(&vq->mutex);
David Stevens8dd014a2010-07-27 18:52:21 +0300861}
862
Tejun Heoc23f34452010-06-02 20:40:00 +0200863static void handle_tx_kick(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000864{
Tejun Heoc23f34452010-06-02 20:40:00 +0200865 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
866 poll.work);
867 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
868
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000869 handle_tx(net);
870}
871
Tejun Heoc23f34452010-06-02 20:40:00 +0200872static void handle_rx_kick(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000873{
Tejun Heoc23f34452010-06-02 20:40:00 +0200874 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
875 poll.work);
876 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
877
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000878 handle_rx(net);
879}
880
Tejun Heoc23f34452010-06-02 20:40:00 +0200881static void handle_tx_net(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000882{
Tejun Heoc23f34452010-06-02 20:40:00 +0200883 struct vhost_net *net = container_of(work, struct vhost_net,
884 poll[VHOST_NET_VQ_TX].work);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000885 handle_tx(net);
886}
887
Tejun Heoc23f34452010-06-02 20:40:00 +0200888static void handle_rx_net(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000889{
Tejun Heoc23f34452010-06-02 20:40:00 +0200890 struct vhost_net *net = container_of(work, struct vhost_net,
891 poll[VHOST_NET_VQ_RX].work);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000892 handle_rx(net);
893}
894
895static int vhost_net_open(struct inode *inode, struct file *f)
896{
Michael S. Tsirkin23cc5a92013-01-23 21:46:47 +0100897 struct vhost_net *n;
Tejun Heoc23f34452010-06-02 20:40:00 +0200898 struct vhost_dev *dev;
Asias He3ab2e422013-04-27 11:16:48 +0800899 struct vhost_virtqueue **vqs;
Jason Wang5990a302018-01-04 11:14:27 +0800900 void **queue;
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +0800901 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200902
Michal Hockodcda9b02017-07-12 14:36:45 -0700903 n = kvmalloc(sizeof *n, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hocko6c5ab652017-05-08 15:57:15 -0700904 if (!n)
905 return -ENOMEM;
Asias He3ab2e422013-04-27 11:16:48 +0800906 vqs = kmalloc(VHOST_NET_VQ_MAX * sizeof(*vqs), GFP_KERNEL);
907 if (!vqs) {
Romain Francoised04257b2014-06-12 10:42:34 +0200908 kvfree(n);
Asias He3ab2e422013-04-27 11:16:48 +0800909 return -ENOMEM;
910 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200911
Jason Wang5990a302018-01-04 11:14:27 +0800912 queue = kmalloc_array(VHOST_RX_BATCH, sizeof(void *),
Jason Wangc67df112017-05-17 12:14:45 +0800913 GFP_KERNEL);
914 if (!queue) {
915 kfree(vqs);
916 kvfree(n);
917 return -ENOMEM;
918 }
919 n->vqs[VHOST_NET_VQ_RX].rxq.queue = queue;
920
Tejun Heoc23f34452010-06-02 20:40:00 +0200921 dev = &n->dev;
Asias He3ab2e422013-04-27 11:16:48 +0800922 vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq;
923 vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq;
924 n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick;
925 n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick;
Asias He28394002013-04-27 15:07:46 +0800926 for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
927 n->vqs[i].ubufs = NULL;
928 n->vqs[i].ubuf_info = NULL;
929 n->vqs[i].upend_idx = 0;
930 n->vqs[i].done_idx = 0;
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300931 n->vqs[i].vhost_hlen = 0;
932 n->vqs[i].sock_hlen = 0;
Jason Wangc67df112017-05-17 12:14:45 +0800933 vhost_net_buf_init(&n->vqs[i].rxq);
Asias He28394002013-04-27 15:07:46 +0800934 }
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +0800935 vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000936
Tejun Heoc23f34452010-06-02 20:40:00 +0200937 vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
938 vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000939
940 f->private_data = n;
941
942 return 0;
943}
944
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000945static struct socket *vhost_net_stop_vq(struct vhost_net *n,
946 struct vhost_virtqueue *vq)
947{
948 struct socket *sock;
Jason Wangc67df112017-05-17 12:14:45 +0800949 struct vhost_net_virtqueue *nvq =
950 container_of(vq, struct vhost_net_virtqueue, vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000951
952 mutex_lock(&vq->mutex);
Asias He22fa90c2013-05-07 14:54:36 +0800953 sock = vq->private_data;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000954 vhost_net_disable_vq(n, vq);
Asias He22fa90c2013-05-07 14:54:36 +0800955 vq->private_data = NULL;
Jason Wangc67df112017-05-17 12:14:45 +0800956 vhost_net_buf_unproduce(nvq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000957 mutex_unlock(&vq->mutex);
958 return sock;
959}
960
961static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
962 struct socket **rx_sock)
963{
Asias He3ab2e422013-04-27 11:16:48 +0800964 *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq);
965 *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000966}
967
968static void vhost_net_flush_vq(struct vhost_net *n, int index)
969{
970 vhost_poll_flush(n->poll + index);
Asias He3ab2e422013-04-27 11:16:48 +0800971 vhost_poll_flush(&n->vqs[index].vq.poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000972}
973
974static void vhost_net_flush(struct vhost_net *n)
975{
976 vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
977 vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
Asias He28394002013-04-27 15:07:46 +0800978 if (n->vqs[VHOST_NET_VQ_TX].ubufs) {
Asias He3ab2e422013-04-27 11:16:48 +0800979 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200980 n->tx_flush = true;
Asias He3ab2e422013-04-27 11:16:48 +0800981 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200982 /* Wait for all lower device DMAs done. */
Asias Hefe729a52013-05-06 16:38:24 +0800983 vhost_net_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs);
Asias He3ab2e422013-04-27 11:16:48 +0800984 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200985 n->tx_flush = false;
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200986 atomic_set(&n->vqs[VHOST_NET_VQ_TX].ubufs->refcount, 1);
Asias He3ab2e422013-04-27 11:16:48 +0800987 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200988 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000989}
990
991static int vhost_net_release(struct inode *inode, struct file *f)
992{
993 struct vhost_net *n = f->private_data;
994 struct socket *tx_sock;
995 struct socket *rx_sock;
996
997 vhost_net_stop(n, &tx_sock, &rx_sock);
998 vhost_net_flush(n);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000999 vhost_dev_stop(&n->dev);
Michael S. Tsirkinea5d4042011-11-27 19:05:58 +02001000 vhost_dev_cleanup(&n->dev, false);
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +03001001 vhost_net_vq_reset(n);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001002 if (tx_sock)
Al Viro09aaacf2014-03-05 20:39:00 -05001003 sockfd_put(tx_sock);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001004 if (rx_sock)
Al Viro09aaacf2014-03-05 20:39:00 -05001005 sockfd_put(rx_sock);
Michael S. Tsirkinb0c057c2014-02-13 11:45:11 +02001006 /* Make sure no callbacks are outstanding */
1007 synchronize_rcu_bh();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001008 /* We do an extra flush before freeing memory,
1009 * since jobs can re-queue themselves. */
1010 vhost_net_flush(n);
Jason Wangc67df112017-05-17 12:14:45 +08001011 kfree(n->vqs[VHOST_NET_VQ_RX].rxq.queue);
Asias He3ab2e422013-04-27 11:16:48 +08001012 kfree(n->dev.vqs);
Romain Francoised04257b2014-06-12 10:42:34 +02001013 kvfree(n);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001014 return 0;
1015}
1016
1017static struct socket *get_raw_socket(int fd)
1018{
1019 struct {
1020 struct sockaddr_ll sa;
1021 char buf[MAX_ADDR_LEN];
1022 } uaddr;
1023 int uaddr_len = sizeof uaddr, r;
1024 struct socket *sock = sockfd_lookup(fd, &r);
Krishna Kumard47effe2011-03-01 17:06:37 +05301025
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001026 if (!sock)
1027 return ERR_PTR(-ENOTSOCK);
1028
1029 /* Parameter checking */
1030 if (sock->sk->sk_type != SOCK_RAW) {
1031 r = -ESOCKTNOSUPPORT;
1032 goto err;
1033 }
1034
1035 r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa,
1036 &uaddr_len, 0);
1037 if (r)
1038 goto err;
1039
1040 if (uaddr.sa.sll_family != AF_PACKET) {
1041 r = -EPFNOSUPPORT;
1042 goto err;
1043 }
1044 return sock;
1045err:
Al Viro09aaacf2014-03-05 20:39:00 -05001046 sockfd_put(sock);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001047 return ERR_PTR(r);
1048}
1049
Jason Wang5990a302018-01-04 11:14:27 +08001050static struct ptr_ring *get_tap_ptr_ring(int fd)
Jason Wangc67df112017-05-17 12:14:45 +08001051{
Jason Wang5990a302018-01-04 11:14:27 +08001052 struct ptr_ring *ring;
Jason Wangc67df112017-05-17 12:14:45 +08001053 struct file *file = fget(fd);
1054
1055 if (!file)
1056 return NULL;
Jason Wang5990a302018-01-04 11:14:27 +08001057 ring = tun_get_tx_ring(file);
1058 if (!IS_ERR(ring))
Jason Wangc67df112017-05-17 12:14:45 +08001059 goto out;
Jason Wang5990a302018-01-04 11:14:27 +08001060 ring = tap_get_ptr_ring(file);
1061 if (!IS_ERR(ring))
Jason Wangc67df112017-05-17 12:14:45 +08001062 goto out;
Jason Wang5990a302018-01-04 11:14:27 +08001063 ring = NULL;
Jason Wangc67df112017-05-17 12:14:45 +08001064out:
1065 fput(file);
Jason Wang5990a302018-01-04 11:14:27 +08001066 return ring;
Jason Wangc67df112017-05-17 12:14:45 +08001067}
1068
Arnd Bergmann501c7742010-02-18 05:46:50 +00001069static struct socket *get_tap_socket(int fd)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001070{
1071 struct file *file = fget(fd);
1072 struct socket *sock;
Krishna Kumard47effe2011-03-01 17:06:37 +05301073
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001074 if (!file)
1075 return ERR_PTR(-EBADF);
1076 sock = tun_get_socket(file);
Arnd Bergmann501c7742010-02-18 05:46:50 +00001077 if (!IS_ERR(sock))
1078 return sock;
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001079 sock = tap_get_socket(file);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001080 if (IS_ERR(sock))
1081 fput(file);
1082 return sock;
1083}
1084
1085static struct socket *get_socket(int fd)
1086{
1087 struct socket *sock;
Krishna Kumard47effe2011-03-01 17:06:37 +05301088
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001089 /* special case to disable backend */
1090 if (fd == -1)
1091 return NULL;
1092 sock = get_raw_socket(fd);
1093 if (!IS_ERR(sock))
1094 return sock;
Arnd Bergmann501c7742010-02-18 05:46:50 +00001095 sock = get_tap_socket(fd);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001096 if (!IS_ERR(sock))
1097 return sock;
1098 return ERR_PTR(-ENOTSOCK);
1099}
1100
1101static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
1102{
1103 struct socket *sock, *oldsock;
1104 struct vhost_virtqueue *vq;
Asias He28394002013-04-27 15:07:46 +08001105 struct vhost_net_virtqueue *nvq;
Asias Hefe729a52013-05-06 16:38:24 +08001106 struct vhost_net_ubuf_ref *ubufs, *oldubufs = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001107 int r;
1108
1109 mutex_lock(&n->dev.mutex);
1110 r = vhost_dev_check_owner(&n->dev);
1111 if (r)
1112 goto err;
1113
1114 if (index >= VHOST_NET_VQ_MAX) {
1115 r = -ENOBUFS;
1116 goto err;
1117 }
Asias He3ab2e422013-04-27 11:16:48 +08001118 vq = &n->vqs[index].vq;
Asias He28394002013-04-27 15:07:46 +08001119 nvq = &n->vqs[index];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001120 mutex_lock(&vq->mutex);
1121
1122 /* Verify that ring has been setup correctly. */
1123 if (!vhost_vq_access_ok(vq)) {
1124 r = -EFAULT;
Jeff Dike1dace8c2010-03-04 16:10:14 -05001125 goto err_vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001126 }
1127 sock = get_socket(fd);
1128 if (IS_ERR(sock)) {
1129 r = PTR_ERR(sock);
Jeff Dike1dace8c2010-03-04 16:10:14 -05001130 goto err_vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001131 }
1132
1133 /* start polling new socket */
Asias He22fa90c2013-05-07 14:54:36 +08001134 oldsock = vq->private_data;
David S. Miller11fe8832010-07-20 18:25:24 -07001135 if (sock != oldsock) {
Asias Hefe729a52013-05-06 16:38:24 +08001136 ubufs = vhost_net_ubuf_alloc(vq,
1137 sock && vhost_sock_zcopy(sock));
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +00001138 if (IS_ERR(ubufs)) {
1139 r = PTR_ERR(ubufs);
1140 goto err_ubufs;
1141 }
Jason Wang692a9982013-01-28 01:05:17 +00001142
Krishna Kumard47effe2011-03-01 17:06:37 +05301143 vhost_net_disable_vq(n, vq);
Asias He22fa90c2013-05-07 14:54:36 +08001144 vq->private_data = sock;
Jason Wangc67df112017-05-17 12:14:45 +08001145 vhost_net_buf_unproduce(nvq);
1146 if (index == VHOST_NET_VQ_RX)
Jason Wang5990a302018-01-04 11:14:27 +08001147 nvq->rx_ring = get_tap_ptr_ring(fd);
Greg Kurz80f7d032016-02-16 15:59:44 +01001148 r = vhost_vq_init_access(vq);
Jason Wangf59281d2011-06-21 18:04:27 +08001149 if (r)
Jason Wang692a9982013-01-28 01:05:17 +00001150 goto err_used;
Jason Wang2b8b3282013-01-28 01:05:18 +00001151 r = vhost_net_enable_vq(n, vq);
1152 if (r)
1153 goto err_used;
Jason Wang692a9982013-01-28 01:05:17 +00001154
Asias He28394002013-04-27 15:07:46 +08001155 oldubufs = nvq->ubufs;
1156 nvq->ubufs = ubufs;
Michael S. Tsirkin64e9a9b2012-12-03 07:31:51 +00001157
1158 n->tx_packets = 0;
1159 n->tx_zcopy_err = 0;
Michael S. Tsirkin1280c272012-12-04 00:17:14 +02001160 n->tx_flush = false;
Jeff Dikedd1f4072010-03-04 16:10:14 -05001161 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001162
Michael S. Tsirkin1680e902010-07-15 15:19:12 +03001163 mutex_unlock(&vq->mutex);
1164
Michael S. Tsirkinc047e5f2011-07-20 13:41:31 +03001165 if (oldubufs) {
Michael S. Tsirkinc38e39c2013-06-25 17:29:46 +03001166 vhost_net_ubuf_put_wait_and_free(oldubufs);
Michael S. Tsirkinc047e5f2011-07-20 13:41:31 +03001167 mutex_lock(&vq->mutex);
Michael S. Tsirkineaae8132012-11-01 09:16:51 +00001168 vhost_zerocopy_signal_used(n, vq);
Michael S. Tsirkinc047e5f2011-07-20 13:41:31 +03001169 mutex_unlock(&vq->mutex);
1170 }
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +00001171
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001172 if (oldsock) {
1173 vhost_net_flush_vq(n, index);
Al Viro09aaacf2014-03-05 20:39:00 -05001174 sockfd_put(oldsock);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001175 }
Jeff Dike1dace8c2010-03-04 16:10:14 -05001176
Michael S. Tsirkin1680e902010-07-15 15:19:12 +03001177 mutex_unlock(&n->dev.mutex);
1178 return 0;
1179
Jason Wang692a9982013-01-28 01:05:17 +00001180err_used:
Asias He22fa90c2013-05-07 14:54:36 +08001181 vq->private_data = oldsock;
Jason Wang692a9982013-01-28 01:05:17 +00001182 vhost_net_enable_vq(n, vq);
1183 if (ubufs)
Michael S. Tsirkinc38e39c2013-06-25 17:29:46 +03001184 vhost_net_ubuf_put_wait_and_free(ubufs);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +00001185err_ubufs:
Al Viro09aaacf2014-03-05 20:39:00 -05001186 sockfd_put(sock);
Jeff Dike1dace8c2010-03-04 16:10:14 -05001187err_vq:
1188 mutex_unlock(&vq->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001189err:
1190 mutex_unlock(&n->dev.mutex);
1191 return r;
1192}
1193
1194static long vhost_net_reset_owner(struct vhost_net *n)
1195{
1196 struct socket *tx_sock = NULL;
1197 struct socket *rx_sock = NULL;
1198 long err;
Jason Wanga9709d62016-06-23 02:04:31 -04001199 struct vhost_umem *umem;
Krishna Kumard47effe2011-03-01 17:06:37 +05301200
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001201 mutex_lock(&n->dev.mutex);
1202 err = vhost_dev_check_owner(&n->dev);
1203 if (err)
1204 goto done;
Jason Wanga9709d62016-06-23 02:04:31 -04001205 umem = vhost_dev_reset_owner_prepare();
1206 if (!umem) {
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +03001207 err = -ENOMEM;
1208 goto done;
1209 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001210 vhost_net_stop(n, &tx_sock, &rx_sock);
1211 vhost_net_flush(n);
Jason Wanga9709d62016-06-23 02:04:31 -04001212 vhost_dev_reset_owner(&n->dev, umem);
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +03001213 vhost_net_vq_reset(n);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001214done:
1215 mutex_unlock(&n->dev.mutex);
1216 if (tx_sock)
Al Viro09aaacf2014-03-05 20:39:00 -05001217 sockfd_put(tx_sock);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001218 if (rx_sock)
Al Viro09aaacf2014-03-05 20:39:00 -05001219 sockfd_put(rx_sock);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001220 return err;
1221}
1222
1223static int vhost_net_set_features(struct vhost_net *n, u64 features)
1224{
David Stevens8dd014a2010-07-27 18:52:21 +03001225 size_t vhost_hlen, sock_hlen, hdr_len;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001226 int i;
David Stevens8dd014a2010-07-27 18:52:21 +03001227
Michael S. Tsirkine4fca7d2014-10-24 14:23:52 +03001228 hdr_len = (features & ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
1229 (1ULL << VIRTIO_F_VERSION_1))) ?
David Stevens8dd014a2010-07-27 18:52:21 +03001230 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
1231 sizeof(struct virtio_net_hdr);
1232 if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
1233 /* vhost provides vnet_hdr */
1234 vhost_hlen = hdr_len;
1235 sock_hlen = 0;
1236 } else {
1237 /* socket provides vnet_hdr */
1238 vhost_hlen = 0;
1239 sock_hlen = hdr_len;
1240 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001241 mutex_lock(&n->dev.mutex);
1242 if ((features & (1 << VHOST_F_LOG_ALL)) &&
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001243 !vhost_log_access_ok(&n->dev))
1244 goto out_unlock;
1245
1246 if ((features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))) {
1247 if (vhost_init_device_iotlb(&n->dev, true))
1248 goto out_unlock;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001249 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001250
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001251 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +08001252 mutex_lock(&n->vqs[i].vq.mutex);
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001253 n->vqs[i].vq.acked_features = features;
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +03001254 n->vqs[i].vhost_hlen = vhost_hlen;
1255 n->vqs[i].sock_hlen = sock_hlen;
Asias He3ab2e422013-04-27 11:16:48 +08001256 mutex_unlock(&n->vqs[i].vq.mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001257 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001258 mutex_unlock(&n->dev.mutex);
1259 return 0;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001260
1261out_unlock:
1262 mutex_unlock(&n->dev.mutex);
1263 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001264}
1265
Asias Heb1ad8492013-05-06 11:16:00 +08001266static long vhost_net_set_owner(struct vhost_net *n)
1267{
1268 int r;
1269
1270 mutex_lock(&n->dev.mutex);
Michael S. Tsirkin05c05352013-06-06 15:20:39 +03001271 if (vhost_dev_has_owner(&n->dev)) {
1272 r = -EBUSY;
1273 goto out;
1274 }
Asias Heb1ad8492013-05-06 11:16:00 +08001275 r = vhost_net_set_ubuf_info(n);
1276 if (r)
1277 goto out;
1278 r = vhost_dev_set_owner(&n->dev);
1279 if (r)
1280 vhost_net_clear_ubuf_info(n);
1281 vhost_net_flush(n);
1282out:
1283 mutex_unlock(&n->dev.mutex);
1284 return r;
1285}
1286
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001287static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
1288 unsigned long arg)
1289{
1290 struct vhost_net *n = f->private_data;
1291 void __user *argp = (void __user *)arg;
1292 u64 __user *featurep = argp;
1293 struct vhost_vring_file backend;
1294 u64 features;
1295 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301296
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001297 switch (ioctl) {
1298 case VHOST_NET_SET_BACKEND:
Takuya Yoshikawad3553a52010-05-27 19:01:58 +09001299 if (copy_from_user(&backend, argp, sizeof backend))
1300 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001301 return vhost_net_set_backend(n, backend.index, backend.fd);
1302 case VHOST_GET_FEATURES:
Stefan Hajnoczi0dd05a32012-07-21 06:55:36 +00001303 features = VHOST_NET_FEATURES;
Takuya Yoshikawad3553a52010-05-27 19:01:58 +09001304 if (copy_to_user(featurep, &features, sizeof features))
1305 return -EFAULT;
1306 return 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001307 case VHOST_SET_FEATURES:
Takuya Yoshikawad3553a52010-05-27 19:01:58 +09001308 if (copy_from_user(&features, featurep, sizeof features))
1309 return -EFAULT;
Stefan Hajnoczi0dd05a32012-07-21 06:55:36 +00001310 if (features & ~VHOST_NET_FEATURES)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001311 return -EOPNOTSUPP;
1312 return vhost_net_set_features(n, features);
1313 case VHOST_RESET_OWNER:
1314 return vhost_net_reset_owner(n);
Asias Heb1ad8492013-05-06 11:16:00 +08001315 case VHOST_SET_OWNER:
1316 return vhost_net_set_owner(n);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001317 default:
1318 mutex_lock(&n->dev.mutex);
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001319 r = vhost_dev_ioctl(&n->dev, ioctl, argp);
1320 if (r == -ENOIOCTLCMD)
1321 r = vhost_vring_ioctl(&n->dev, ioctl, argp);
1322 else
1323 vhost_net_flush(n);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001324 mutex_unlock(&n->dev.mutex);
1325 return r;
1326 }
1327}
1328
1329#ifdef CONFIG_COMPAT
1330static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
1331 unsigned long arg)
1332{
1333 return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1334}
1335#endif
1336
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001337static ssize_t vhost_net_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
1338{
1339 struct file *file = iocb->ki_filp;
1340 struct vhost_net *n = file->private_data;
1341 struct vhost_dev *dev = &n->dev;
1342 int noblock = file->f_flags & O_NONBLOCK;
1343
1344 return vhost_chr_read_iter(dev, to, noblock);
1345}
1346
1347static ssize_t vhost_net_chr_write_iter(struct kiocb *iocb,
1348 struct iov_iter *from)
1349{
1350 struct file *file = iocb->ki_filp;
1351 struct vhost_net *n = file->private_data;
1352 struct vhost_dev *dev = &n->dev;
1353
1354 return vhost_chr_write_iter(dev, from);
1355}
1356
1357static unsigned int vhost_net_chr_poll(struct file *file, poll_table *wait)
1358{
1359 struct vhost_net *n = file->private_data;
1360 struct vhost_dev *dev = &n->dev;
1361
1362 return vhost_chr_poll(file, dev, wait);
1363}
1364
Tobias Klauser373a83a2010-05-17 15:12:49 +02001365static const struct file_operations vhost_net_fops = {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001366 .owner = THIS_MODULE,
1367 .release = vhost_net_release,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001368 .read_iter = vhost_net_chr_read_iter,
1369 .write_iter = vhost_net_chr_write_iter,
1370 .poll = vhost_net_chr_poll,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001371 .unlocked_ioctl = vhost_net_ioctl,
1372#ifdef CONFIG_COMPAT
1373 .compat_ioctl = vhost_net_compat_ioctl,
1374#endif
1375 .open = vhost_net_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001376 .llseek = noop_llseek,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001377};
1378
1379static struct miscdevice vhost_net_misc = {
stephen hemminger7c7c7f02012-01-11 19:30:38 +00001380 .minor = VHOST_NET_MINOR,
1381 .name = "vhost-net",
1382 .fops = &vhost_net_fops,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001383};
1384
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001385static int vhost_net_init(void)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001386{
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +00001387 if (experimental_zcopytx)
Asias Hefe729a52013-05-06 16:38:24 +08001388 vhost_net_enable_zcopy(VHOST_NET_VQ_TX);
Tejun Heoc23f34452010-06-02 20:40:00 +02001389 return misc_register(&vhost_net_misc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001390}
1391module_init(vhost_net_init);
1392
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001393static void vhost_net_exit(void)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001394{
1395 misc_deregister(&vhost_net_misc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001396}
1397module_exit(vhost_net_exit);
1398
1399MODULE_VERSION("0.0.1");
1400MODULE_LICENSE("GPL v2");
1401MODULE_AUTHOR("Michael S. Tsirkin");
1402MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
stephen hemminger7c7c7f02012-01-11 19:30:38 +00001403MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR);
1404MODULE_ALIAS("devname:vhost-net");