blob: bbf38befefb28d9153c94b01eaba1f2a6a785279 [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>
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +020035#include <net/xdp.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000036
37#include "vhost.h"
38
Michael S. Tsirkinf9611c42012-12-06 14:56:00 +020039static int experimental_zcopytx = 1;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +000040module_param(experimental_zcopytx, int, 0444);
Michael S. Tsirkinf9611c42012-12-06 14:56:00 +020041MODULE_PARM_DESC(experimental_zcopytx, "Enable Zero Copy TX;"
42 " 1 -Enable; 0 - Disable");
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +000043
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000044/* Max number of bytes transferred before requeueing the job.
45 * Using this limit prevents one virtqueue from starving others. */
46#define VHOST_NET_WEIGHT 0x80000
47
haibinzhang(张海斌)a2ac9992018-04-09 07:22:17 +000048/* Max number of packets transferred before requeueing the job.
49 * Using this limit prevents one virtqueue from starving rx. */
50#define VHOST_NET_PKT_WEIGHT(vq) ((vq)->num * 2)
51
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +000052/* MAX number of TX used buffers for outstanding zerocopy */
53#define VHOST_MAX_PEND 128
54#define VHOST_GOODCOPY_LEN 256
55
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000056/*
57 * For transmit, used buffer len is unused; we override it to track buffer
58 * status internally; used for zerocopy tx only.
59 */
60/* Lower device DMA failed */
Michael S. Tsirkinbf995732014-10-24 11:49:27 +030061#define VHOST_DMA_FAILED_LEN ((__force __virtio32)3)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000062/* Lower device DMA done */
Michael S. Tsirkinbf995732014-10-24 11:49:27 +030063#define VHOST_DMA_DONE_LEN ((__force __virtio32)2)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000064/* Lower device DMA in progress */
Michael S. Tsirkinbf995732014-10-24 11:49:27 +030065#define VHOST_DMA_IN_PROGRESS ((__force __virtio32)1)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000066/* Buffer unused */
Michael S. Tsirkinbf995732014-10-24 11:49:27 +030067#define VHOST_DMA_CLEAR_LEN ((__force __virtio32)0)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000068
Michael S. Tsirkinbf995732014-10-24 11:49:27 +030069#define VHOST_DMA_IS_DONE(len) ((__force u32)(len) >= (__force u32)VHOST_DMA_DONE_LEN)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000070
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000071enum {
Asias He8570a6e2013-05-06 16:38:20 +080072 VHOST_NET_FEATURES = VHOST_FEATURES |
73 (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) |
Jason Wang6b1e6cc2016-06-23 02:04:32 -040074 (1ULL << VIRTIO_NET_F_MRG_RXBUF) |
75 (1ULL << VIRTIO_F_IOMMU_PLATFORM)
Asias He8570a6e2013-05-06 16:38:20 +080076};
77
78enum {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000079 VHOST_NET_VQ_RX = 0,
80 VHOST_NET_VQ_TX = 1,
81 VHOST_NET_VQ_MAX = 2,
82};
83
Asias Hefe729a52013-05-06 16:38:24 +080084struct vhost_net_ubuf_ref {
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +020085 /* refcount follows semantics similar to kref:
86 * 0: object is released
87 * 1: no outstanding ubufs
88 * >1: outstanding ubufs
89 */
90 atomic_t refcount;
Asias He28394002013-04-27 15:07:46 +080091 wait_queue_head_t wait;
92 struct vhost_virtqueue *vq;
93};
94
Jason Wangc67df112017-05-17 12:14:45 +080095#define VHOST_RX_BATCH 64
96struct vhost_net_buf {
Jason Wang5990a302018-01-04 11:14:27 +080097 void **queue;
Jason Wangc67df112017-05-17 12:14:45 +080098 int tail;
99 int head;
100};
101
Asias He3ab2e422013-04-27 11:16:48 +0800102struct vhost_net_virtqueue {
103 struct vhost_virtqueue vq;
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300104 size_t vhost_hlen;
105 size_t sock_hlen;
Asias He28394002013-04-27 15:07:46 +0800106 /* vhost zerocopy support fields below: */
107 /* last used idx for outstanding DMA zerocopy buffers */
108 int upend_idx;
109 /* first used idx for DMA done zerocopy buffers */
110 int done_idx;
111 /* an array of userspace buffers info */
112 struct ubuf_info *ubuf_info;
113 /* Reference counting for outstanding ubufs.
114 * Protected by vq mutex. Writers must also take device mutex. */
Asias Hefe729a52013-05-06 16:38:24 +0800115 struct vhost_net_ubuf_ref *ubufs;
Jason Wang5990a302018-01-04 11:14:27 +0800116 struct ptr_ring *rx_ring;
Jason Wangc67df112017-05-17 12:14:45 +0800117 struct vhost_net_buf rxq;
Asias He3ab2e422013-04-27 11:16:48 +0800118};
119
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000120struct vhost_net {
121 struct vhost_dev dev;
Asias He3ab2e422013-04-27 11:16:48 +0800122 struct vhost_net_virtqueue vqs[VHOST_NET_VQ_MAX];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000123 struct vhost_poll poll[VHOST_NET_VQ_MAX];
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000124 /* Number of TX recently submitted.
125 * Protected by tx vq lock. */
126 unsigned tx_packets;
127 /* Number of times zerocopy TX recently failed.
128 * Protected by tx vq lock. */
129 unsigned tx_zcopy_err;
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200130 /* Flush in progress. Protected by tx vq lock. */
131 bool tx_flush;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000132};
133
Asias Hefe729a52013-05-06 16:38:24 +0800134static unsigned vhost_net_zcopy_mask __read_mostly;
Asias He28394002013-04-27 15:07:46 +0800135
Jason Wangc67df112017-05-17 12:14:45 +0800136static void *vhost_net_buf_get_ptr(struct vhost_net_buf *rxq)
137{
138 if (rxq->tail != rxq->head)
139 return rxq->queue[rxq->head];
140 else
141 return NULL;
142}
143
144static int vhost_net_buf_get_size(struct vhost_net_buf *rxq)
145{
146 return rxq->tail - rxq->head;
147}
148
149static int vhost_net_buf_is_empty(struct vhost_net_buf *rxq)
150{
151 return rxq->tail == rxq->head;
152}
153
154static void *vhost_net_buf_consume(struct vhost_net_buf *rxq)
155{
156 void *ret = vhost_net_buf_get_ptr(rxq);
157 ++rxq->head;
158 return ret;
159}
160
161static int vhost_net_buf_produce(struct vhost_net_virtqueue *nvq)
162{
163 struct vhost_net_buf *rxq = &nvq->rxq;
164
165 rxq->head = 0;
Jason Wang5990a302018-01-04 11:14:27 +0800166 rxq->tail = ptr_ring_consume_batched(nvq->rx_ring, rxq->queue,
Jason Wangc67df112017-05-17 12:14:45 +0800167 VHOST_RX_BATCH);
168 return rxq->tail;
169}
170
171static void vhost_net_buf_unproduce(struct vhost_net_virtqueue *nvq)
172{
173 struct vhost_net_buf *rxq = &nvq->rxq;
174
Jason Wang5990a302018-01-04 11:14:27 +0800175 if (nvq->rx_ring && !vhost_net_buf_is_empty(rxq)) {
176 ptr_ring_unconsume(nvq->rx_ring, rxq->queue + rxq->head,
177 vhost_net_buf_get_size(rxq),
Jason Wang3a403072018-03-09 14:50:34 +0800178 tun_ptr_free);
Jason Wangc67df112017-05-17 12:14:45 +0800179 rxq->head = rxq->tail = 0;
180 }
181}
182
Jason Wangfc72d1d2018-01-04 11:14:28 +0800183static int vhost_net_buf_peek_len(void *ptr)
184{
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +0200185 if (tun_is_xdp_frame(ptr)) {
186 struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
Jason Wangfc72d1d2018-01-04 11:14:28 +0800187
Jesper Dangaard Brouer1ffcbc82018-04-17 16:45:47 +0200188 return xdpf->len;
Jason Wangfc72d1d2018-01-04 11:14:28 +0800189 }
190
191 return __skb_array_len_with_tag(ptr);
192}
193
Jason Wangc67df112017-05-17 12:14:45 +0800194static int vhost_net_buf_peek(struct vhost_net_virtqueue *nvq)
195{
196 struct vhost_net_buf *rxq = &nvq->rxq;
197
198 if (!vhost_net_buf_is_empty(rxq))
199 goto out;
200
201 if (!vhost_net_buf_produce(nvq))
202 return 0;
203
204out:
Jason Wangfc72d1d2018-01-04 11:14:28 +0800205 return vhost_net_buf_peek_len(vhost_net_buf_get_ptr(rxq));
Jason Wangc67df112017-05-17 12:14:45 +0800206}
207
208static void vhost_net_buf_init(struct vhost_net_buf *rxq)
209{
210 rxq->head = rxq->tail = 0;
211}
212
Asias Hefe729a52013-05-06 16:38:24 +0800213static void vhost_net_enable_zcopy(int vq)
Asias He28394002013-04-27 15:07:46 +0800214{
Asias Hefe729a52013-05-06 16:38:24 +0800215 vhost_net_zcopy_mask |= 0x1 << vq;
Asias He28394002013-04-27 15:07:46 +0800216}
217
Asias Hefe729a52013-05-06 16:38:24 +0800218static struct vhost_net_ubuf_ref *
219vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy)
Asias He28394002013-04-27 15:07:46 +0800220{
Asias Hefe729a52013-05-06 16:38:24 +0800221 struct vhost_net_ubuf_ref *ubufs;
Asias He28394002013-04-27 15:07:46 +0800222 /* No zero copy backend? Nothing to count. */
223 if (!zcopy)
224 return NULL;
225 ubufs = kmalloc(sizeof(*ubufs), GFP_KERNEL);
226 if (!ubufs)
227 return ERR_PTR(-ENOMEM);
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200228 atomic_set(&ubufs->refcount, 1);
Asias He28394002013-04-27 15:07:46 +0800229 init_waitqueue_head(&ubufs->wait);
230 ubufs->vq = vq;
231 return ubufs;
232}
233
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200234static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref *ubufs)
Asias He28394002013-04-27 15:07:46 +0800235{
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200236 int r = atomic_sub_return(1, &ubufs->refcount);
237 if (unlikely(!r))
238 wake_up(&ubufs->wait);
239 return r;
Asias He28394002013-04-27 15:07:46 +0800240}
241
Asias Hefe729a52013-05-06 16:38:24 +0800242static void vhost_net_ubuf_put_and_wait(struct vhost_net_ubuf_ref *ubufs)
Asias He28394002013-04-27 15:07:46 +0800243{
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200244 vhost_net_ubuf_put(ubufs);
245 wait_event(ubufs->wait, !atomic_read(&ubufs->refcount));
Michael S. Tsirkinc38e39c2013-06-25 17:29:46 +0300246}
247
248static void vhost_net_ubuf_put_wait_and_free(struct vhost_net_ubuf_ref *ubufs)
249{
250 vhost_net_ubuf_put_and_wait(ubufs);
Asias He28394002013-04-27 15:07:46 +0800251 kfree(ubufs);
252}
253
Asias Heb1ad8492013-05-06 11:16:00 +0800254static void vhost_net_clear_ubuf_info(struct vhost_net *n)
255{
Asias Heb1ad8492013-05-06 11:16:00 +0800256 int i;
257
Michael S. Tsirkin288cfe72013-06-06 15:20:46 +0300258 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
259 kfree(n->vqs[i].ubuf_info);
260 n->vqs[i].ubuf_info = NULL;
Asias Heb1ad8492013-05-06 11:16:00 +0800261 }
262}
263
Asias He0a1febf2013-06-05 21:17:38 +0800264static int vhost_net_set_ubuf_info(struct vhost_net *n)
Asias He28394002013-04-27 15:07:46 +0800265{
266 bool zcopy;
267 int i;
268
Michael S. Tsirkin288cfe72013-06-06 15:20:46 +0300269 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
Asias Hefe729a52013-05-06 16:38:24 +0800270 zcopy = vhost_net_zcopy_mask & (0x1 << i);
Asias He28394002013-04-27 15:07:46 +0800271 if (!zcopy)
272 continue;
273 n->vqs[i].ubuf_info = kmalloc(sizeof(*n->vqs[i].ubuf_info) *
274 UIO_MAXIOV, GFP_KERNEL);
275 if (!n->vqs[i].ubuf_info)
276 goto err;
277 }
278 return 0;
279
280err:
Michael S. Tsirkin288cfe72013-06-06 15:20:46 +0300281 vhost_net_clear_ubuf_info(n);
Asias He28394002013-04-27 15:07:46 +0800282 return -ENOMEM;
283}
284
Asias He0a1febf2013-06-05 21:17:38 +0800285static void vhost_net_vq_reset(struct vhost_net *n)
Asias He28394002013-04-27 15:07:46 +0800286{
287 int i;
288
Michael S. Tsirkin288cfe72013-06-06 15:20:46 +0300289 vhost_net_clear_ubuf_info(n);
290
Asias He28394002013-04-27 15:07:46 +0800291 for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
292 n->vqs[i].done_idx = 0;
293 n->vqs[i].upend_idx = 0;
294 n->vqs[i].ubufs = NULL;
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300295 n->vqs[i].vhost_hlen = 0;
296 n->vqs[i].sock_hlen = 0;
Jason Wangc67df112017-05-17 12:14:45 +0800297 vhost_net_buf_init(&n->vqs[i].rxq);
Asias He28394002013-04-27 15:07:46 +0800298 }
299
300}
301
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000302static void vhost_net_tx_packet(struct vhost_net *net)
303{
304 ++net->tx_packets;
305 if (net->tx_packets < 1024)
306 return;
307 net->tx_packets = 0;
308 net->tx_zcopy_err = 0;
309}
310
311static void vhost_net_tx_err(struct vhost_net *net)
312{
313 ++net->tx_zcopy_err;
314}
315
316static bool vhost_net_tx_select_zcopy(struct vhost_net *net)
317{
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200318 /* TX flush waits for outstanding DMAs to be done.
319 * Don't start new DMAs.
320 */
321 return !net->tx_flush &&
322 net->tx_packets / 64 >= net->tx_zcopy_err;
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000323}
324
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000325static bool vhost_sock_zcopy(struct socket *sock)
326{
327 return unlikely(experimental_zcopytx) &&
328 sock_flag(sock->sk, SOCK_ZEROCOPY);
329}
330
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000331/* In case of DMA done not in order in lower device driver for some reason.
332 * upend_idx is used to track end of used idx, done_idx is used to track head
333 * of used idx. Once lower device DMA done contiguously, we will signal KVM
334 * guest used idx.
335 */
Jason Wang094afe72013-09-02 16:40:56 +0800336static void vhost_zerocopy_signal_used(struct vhost_net *net,
337 struct vhost_virtqueue *vq)
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000338{
Asias He28394002013-04-27 15:07:46 +0800339 struct vhost_net_virtqueue *nvq =
340 container_of(vq, struct vhost_net_virtqueue, vq);
Jason Wangc92112a2013-09-02 16:40:57 +0800341 int i, add;
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000342 int j = 0;
343
Asias He28394002013-04-27 15:07:46 +0800344 for (i = nvq->done_idx; i != nvq->upend_idx; i = (i + 1) % UIO_MAXIOV) {
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000345 if (vq->heads[i].len == VHOST_DMA_FAILED_LEN)
346 vhost_net_tx_err(net);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000347 if (VHOST_DMA_IS_DONE(vq->heads[i].len)) {
348 vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000349 ++j;
350 } else
351 break;
352 }
Jason Wangc92112a2013-09-02 16:40:57 +0800353 while (j) {
354 add = min(UIO_MAXIOV - nvq->done_idx, j);
355 vhost_add_used_and_signal_n(vq->dev, vq,
356 &vq->heads[nvq->done_idx], add);
357 nvq->done_idx = (nvq->done_idx + add) % UIO_MAXIOV;
358 j -= add;
359 }
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000360}
361
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000362static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000363{
Asias Hefe729a52013-05-06 16:38:24 +0800364 struct vhost_net_ubuf_ref *ubufs = ubuf->ctx;
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000365 struct vhost_virtqueue *vq = ubufs->vq;
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200366 int cnt;
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000367
Michael S. Tsirkinb0c057c2014-02-13 11:45:11 +0200368 rcu_read_lock_bh();
369
Jason Wang19c73b32013-09-02 16:41:00 +0800370 /* set len to mark this desc buffers done DMA */
371 vq->heads[ubuf->desc].len = success ?
372 VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200373 cnt = vhost_net_ubuf_put(ubufs);
Jason Wang19c73b32013-09-02 16:41:00 +0800374
Michael S. Tsirkin24eb21a2012-11-01 09:16:55 +0000375 /*
376 * Trigger polling thread if guest stopped submitting new buffers:
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200377 * in this case, the refcount after decrement will eventually reach 1.
Michael S. Tsirkin24eb21a2012-11-01 09:16:55 +0000378 * We also trigger polling periodically after each 16 packets
379 * (the value 16 here is more or less arbitrary, it's tuned to trigger
380 * less than 10% of times).
381 */
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200382 if (cnt <= 1 || !(cnt % 16))
Michael S. Tsirkin24eb21a2012-11-01 09:16:55 +0000383 vhost_poll_queue(&vq->poll);
Michael S. Tsirkinb0c057c2014-02-13 11:45:11 +0200384
385 rcu_read_unlock_bh();
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000386}
387
Jason Wang03088132016-03-04 06:24:53 -0500388static inline unsigned long busy_clock(void)
389{
390 return local_clock() >> 10;
391}
392
393static bool vhost_can_busy_poll(struct vhost_dev *dev,
394 unsigned long endtime)
395{
396 return likely(!need_resched()) &&
397 likely(!time_after(busy_clock(), endtime)) &&
398 likely(!signal_pending(current)) &&
399 !vhost_has_work(dev);
400}
401
Jason Wang8241a1e2016-06-01 01:56:33 -0400402static void vhost_net_disable_vq(struct vhost_net *n,
403 struct vhost_virtqueue *vq)
404{
405 struct vhost_net_virtqueue *nvq =
406 container_of(vq, struct vhost_net_virtqueue, vq);
407 struct vhost_poll *poll = n->poll + (nvq - n->vqs);
408 if (!vq->private_data)
409 return;
410 vhost_poll_stop(poll);
411}
412
413static int vhost_net_enable_vq(struct vhost_net *n,
414 struct vhost_virtqueue *vq)
415{
416 struct vhost_net_virtqueue *nvq =
417 container_of(vq, struct vhost_net_virtqueue, vq);
418 struct vhost_poll *poll = n->poll + (nvq - n->vqs);
419 struct socket *sock;
420
421 sock = vq->private_data;
422 if (!sock)
423 return 0;
424
425 return vhost_poll_start(poll, sock->file);
426}
427
Jason Wang03088132016-03-04 06:24:53 -0500428static int vhost_net_tx_get_vq_desc(struct vhost_net *net,
429 struct vhost_virtqueue *vq,
430 struct iovec iov[], unsigned int iov_size,
431 unsigned int *out_num, unsigned int *in_num)
432{
433 unsigned long uninitialized_var(endtime);
434 int r = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400435 out_num, in_num, NULL, NULL);
Jason Wang03088132016-03-04 06:24:53 -0500436
437 if (r == vq->num && vq->busyloop_timeout) {
438 preempt_disable();
439 endtime = busy_clock() + vq->busyloop_timeout;
440 while (vhost_can_busy_poll(vq->dev, endtime) &&
441 vhost_vq_avail_empty(vq->dev, vq))
Christian Borntraegerf2f09a42016-10-25 11:03:14 +0200442 cpu_relax();
Jason Wang03088132016-03-04 06:24:53 -0500443 preempt_enable();
444 r = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400445 out_num, in_num, NULL, NULL);
Jason Wang03088132016-03-04 06:24:53 -0500446 }
447
448 return r;
449}
450
Jason Wang0ed005c2017-01-18 15:02:02 +0800451static bool vhost_exceeds_maxpend(struct vhost_net *net)
452{
453 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
454 struct vhost_virtqueue *vq = &nvq->vq;
455
Willem de Bruijn1e6f7452017-10-06 13:22:31 -0400456 return (nvq->upend_idx + UIO_MAXIOV - nvq->done_idx) % UIO_MAXIOV >
457 min_t(unsigned int, VHOST_MAX_PEND, vq->num >> 2);
Jason Wang0ed005c2017-01-18 15:02:02 +0800458}
459
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000460/* Expects to be always run from workqueue - which acts as
461 * read-size critical section for our kind of RCU. */
462static void handle_tx(struct vhost_net *net)
463{
Asias He28394002013-04-27 15:07:46 +0800464 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300465 struct vhost_virtqueue *vq = &nvq->vq;
Al Viro98a527a2014-12-10 15:00:58 -0500466 unsigned out, in;
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300467 int head;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000468 struct msghdr msg = {
469 .msg_name = NULL,
470 .msg_namelen = 0,
471 .msg_control = NULL,
472 .msg_controllen = 0,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000473 .msg_flags = MSG_DONTWAIT,
474 };
475 size_t len, total_len = 0;
Jason Wang70181d512013-04-10 20:50:48 +0000476 int err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000477 size_t hdr_size;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100478 struct socket *sock;
Asias Hefe729a52013-05-06 16:38:24 +0800479 struct vhost_net_ubuf_ref *uninitialized_var(ubufs);
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200480 bool zcopy, zcopy_used;
haibinzhang(张海斌)a2ac9992018-04-09 07:22:17 +0000481 int sent_pkts = 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100482
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000483 mutex_lock(&vq->mutex);
Asias He2e26af72013-05-07 14:54:33 +0800484 sock = vq->private_data;
485 if (!sock)
486 goto out;
487
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400488 if (!vq_iotlb_prefetch(vq))
489 goto out;
490
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300491 vhost_disable_notify(&net->dev, vq);
Jason Wangfeb88922017-11-13 11:45:34 +0800492 vhost_net_disable_vq(net, vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000493
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300494 hdr_size = nvq->vhost_hlen;
Asias He28394002013-04-27 15:07:46 +0800495 zcopy = nvq->ubufs;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000496
497 for (;;) {
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000498 /* Release DMAs done buffers first */
499 if (zcopy)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000500 vhost_zerocopy_signal_used(net, vq);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000501
Jason Wangf7c6be42013-09-02 16:41:01 +0800502
Jason Wang03088132016-03-04 06:24:53 -0500503 head = vhost_net_tx_get_vq_desc(net, vq, vq->iov,
504 ARRAY_SIZE(vq->iov),
505 &out, &in);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300506 /* On error, stop handling until the next kick. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300507 if (unlikely(head < 0))
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300508 break;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000509 /* Nothing new? Wait for eventfd to tell us they refilled. */
510 if (head == vq->num) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300511 if (unlikely(vhost_enable_notify(&net->dev, vq))) {
512 vhost_disable_notify(&net->dev, vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000513 continue;
514 }
515 break;
516 }
517 if (in) {
518 vq_err(vq, "Unexpected descriptor format for TX: "
519 "out %d, int %d\n", out, in);
520 break;
521 }
522 /* Skip header. TODO: support TSO. */
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000523 len = iov_length(vq->iov, out);
Al Viroc0371da2014-11-24 10:42:55 -0500524 iov_iter_init(&msg.msg_iter, WRITE, vq->iov, out, len);
Al Viro98a527a2014-12-10 15:00:58 -0500525 iov_iter_advance(&msg.msg_iter, hdr_size);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000526 /* Sanity check */
Al Viro01e97e62014-12-15 21:39:31 -0500527 if (!msg_data_left(&msg)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000528 vq_err(vq, "Unexpected header len for TX: "
529 "%zd expected %zd\n",
Al Viro98a527a2014-12-10 15:00:58 -0500530 len, hdr_size);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000531 break;
532 }
Al Viro01e97e62014-12-15 21:39:31 -0500533 len = msg_data_left(&msg);
Jason Wangce21a022013-09-02 16:40:59 +0800534
535 zcopy_used = zcopy && len >= VHOST_GOODCOPY_LEN
Willem de Bruijn1e6f7452017-10-06 13:22:31 -0400536 && !vhost_exceeds_maxpend(net)
Jason Wangce21a022013-09-02 16:40:59 +0800537 && vhost_net_tx_select_zcopy(net);
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200538
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000539 /* use msg_control to pass vhost zerocopy ubuf info to skb */
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200540 if (zcopy_used) {
Jason Wangce21a022013-09-02 16:40:59 +0800541 struct ubuf_info *ubuf;
542 ubuf = nvq->ubuf_info + nvq->upend_idx;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000543
Michael S. Tsirkin8b386942014-10-24 14:19:48 +0300544 vq->heads[nvq->upend_idx].id = cpu_to_vhost32(vq, head);
Jason Wangce21a022013-09-02 16:40:59 +0800545 vq->heads[nvq->upend_idx].len = VHOST_DMA_IN_PROGRESS;
546 ubuf->callback = vhost_zerocopy_callback;
547 ubuf->ctx = nvq->ubufs;
548 ubuf->desc = nvq->upend_idx;
Eric Dumazetc1d1b432017-08-31 16:48:22 -0700549 refcount_set(&ubuf->refcnt, 1);
Jason Wangce21a022013-09-02 16:40:59 +0800550 msg.msg_control = ubuf;
551 msg.msg_controllen = sizeof(ubuf);
552 ubufs = nvq->ubufs;
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200553 atomic_inc(&ubufs->refcount);
Asias He28394002013-04-27 15:07:46 +0800554 nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV;
Jason Wangce21a022013-09-02 16:40:59 +0800555 } else {
Jason Wang4364d5f2013-06-05 15:40:46 +0800556 msg.msg_control = NULL;
Jason Wangce21a022013-09-02 16:40:59 +0800557 ubufs = NULL;
558 }
Jason Wang0ed005c2017-01-18 15:02:02 +0800559
560 total_len += len;
561 if (total_len < VHOST_NET_WEIGHT &&
562 !vhost_vq_avail_empty(&net->dev, vq) &&
563 likely(!vhost_exceeds_maxpend(net))) {
564 msg.msg_flags |= MSG_MORE;
565 } else {
566 msg.msg_flags &= ~MSG_MORE;
567 }
568
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000569 /* TODO: Check specific error and bomb out unless ENOBUFS? */
Ying Xue1b784142015-03-02 15:37:48 +0800570 err = sock->ops->sendmsg(sock, &msg, len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000571 if (unlikely(err < 0)) {
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200572 if (zcopy_used) {
Jason Wangce21a022013-09-02 16:40:59 +0800573 vhost_net_ubuf_put(ubufs);
Asias He28394002013-04-27 15:07:46 +0800574 nvq->upend_idx = ((unsigned)nvq->upend_idx - 1)
575 % UIO_MAXIOV;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000576 }
David Stevens8dd014a2010-07-27 18:52:21 +0300577 vhost_discard_vq_desc(vq, 1);
Jason Wangfeb88922017-11-13 11:45:34 +0800578 vhost_net_enable_vq(net, vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000579 break;
580 }
581 if (err != len)
Michael S. Tsirkin95c0ec62010-06-24 17:10:25 +0300582 pr_debug("Truncated TX packet: "
583 " len %d != %zd\n", err, len);
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200584 if (!zcopy_used)
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000585 vhost_add_used_and_signal(&net->dev, vq, head, 0);
Jason Wangc8fb2172012-05-02 11:42:41 +0800586 else
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000587 vhost_zerocopy_signal_used(net, vq);
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000588 vhost_net_tx_packet(net);
haibinzhang(张海斌)a2ac9992018-04-09 07:22:17 +0000589 if (unlikely(total_len >= VHOST_NET_WEIGHT) ||
590 unlikely(++sent_pkts >= VHOST_NET_PKT_WEIGHT(vq))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000591 vhost_poll_queue(&vq->poll);
592 break;
593 }
594 }
Asias He2e26af72013-05-07 14:54:33 +0800595out:
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000596 mutex_unlock(&vq->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000597}
598
Jason Wangc67df112017-05-17 12:14:45 +0800599static int peek_head_len(struct vhost_net_virtqueue *rvq, struct sock *sk)
David Stevens8dd014a2010-07-27 18:52:21 +0300600{
601 struct sk_buff *head;
602 int len = 0;
Jason Wang783e3982011-01-17 16:11:17 +0800603 unsigned long flags;
David Stevens8dd014a2010-07-27 18:52:21 +0300604
Jason Wang5990a302018-01-04 11:14:27 +0800605 if (rvq->rx_ring)
Jason Wangc67df112017-05-17 12:14:45 +0800606 return vhost_net_buf_peek(rvq);
Jason Wang1576d982016-06-30 14:45:36 +0800607
Jason Wang783e3982011-01-17 16:11:17 +0800608 spin_lock_irqsave(&sk->sk_receive_queue.lock, flags);
David Stevens8dd014a2010-07-27 18:52:21 +0300609 head = skb_peek(&sk->sk_receive_queue);
Basil Gorc53cff5e2012-05-03 22:55:23 +0000610 if (likely(head)) {
David Stevens8dd014a2010-07-27 18:52:21 +0300611 len = head->len;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100612 if (skb_vlan_tag_present(head))
Basil Gorc53cff5e2012-05-03 22:55:23 +0000613 len += VLAN_HLEN;
614 }
615
Jason Wang783e3982011-01-17 16:11:17 +0800616 spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags);
David Stevens8dd014a2010-07-27 18:52:21 +0300617 return len;
618}
619
Jason Wang1576d982016-06-30 14:45:36 +0800620static int sk_has_rx_data(struct sock *sk)
621{
622 struct socket *sock = sk->sk_socket;
623
624 if (sock->ops->peek_len)
625 return sock->ops->peek_len(sock);
626
627 return skb_queue_empty(&sk->sk_receive_queue);
628}
629
Jason Wang03088132016-03-04 06:24:53 -0500630static int vhost_net_rx_peek_head_len(struct vhost_net *net, struct sock *sk)
631{
Jason Wangc67df112017-05-17 12:14:45 +0800632 struct vhost_net_virtqueue *rvq = &net->vqs[VHOST_NET_VQ_RX];
Jason Wang03088132016-03-04 06:24:53 -0500633 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
634 struct vhost_virtqueue *vq = &nvq->vq;
635 unsigned long uninitialized_var(endtime);
Jason Wangc67df112017-05-17 12:14:45 +0800636 int len = peek_head_len(rvq, sk);
Jason Wang03088132016-03-04 06:24:53 -0500637
638 if (!len && vq->busyloop_timeout) {
639 /* Both tx vq and rx socket were polled here */
Jason Wangaaa31492018-03-26 16:10:23 +0800640 mutex_lock_nested(&vq->mutex, 1);
Jason Wang03088132016-03-04 06:24:53 -0500641 vhost_disable_notify(&net->dev, vq);
642
643 preempt_disable();
644 endtime = busy_clock() + vq->busyloop_timeout;
645
646 while (vhost_can_busy_poll(&net->dev, endtime) &&
Jason Wang1576d982016-06-30 14:45:36 +0800647 !sk_has_rx_data(sk) &&
Jason Wang03088132016-03-04 06:24:53 -0500648 vhost_vq_avail_empty(&net->dev, vq))
Christian Borntraegerf2f09a42016-10-25 11:03:14 +0200649 cpu_relax();
Jason Wang03088132016-03-04 06:24:53 -0500650
651 preempt_enable();
652
Jason Wang8b949be2017-09-05 09:22:05 +0800653 if (!vhost_vq_avail_empty(&net->dev, vq))
Jason Wang03088132016-03-04 06:24:53 -0500654 vhost_poll_queue(&vq->poll);
Jason Wang8b949be2017-09-05 09:22:05 +0800655 else if (unlikely(vhost_enable_notify(&net->dev, vq))) {
656 vhost_disable_notify(&net->dev, vq);
657 vhost_poll_queue(&vq->poll);
658 }
659
Jason Wang03088132016-03-04 06:24:53 -0500660 mutex_unlock(&vq->mutex);
661
Jason Wangc67df112017-05-17 12:14:45 +0800662 len = peek_head_len(rvq, sk);
Jason Wang03088132016-03-04 06:24:53 -0500663 }
664
665 return len;
666}
667
David Stevens8dd014a2010-07-27 18:52:21 +0300668/* This is a multi-buffer version of vhost_get_desc, that works if
669 * vq has read descriptors only.
670 * @vq - the relevant virtqueue
671 * @datalen - data length we'll be reading
672 * @iovcount - returned count of io vectors we fill
673 * @log - vhost log
674 * @log_num - log offset
Jason Wang94249362011-01-17 16:11:08 +0800675 * @quota - headcount quota, 1 for big buffer
David Stevens8dd014a2010-07-27 18:52:21 +0300676 * returns number of buffer heads allocated, negative on error
677 */
678static int get_rx_bufs(struct vhost_virtqueue *vq,
679 struct vring_used_elem *heads,
680 int datalen,
681 unsigned *iovcount,
682 struct vhost_log *log,
Jason Wang94249362011-01-17 16:11:08 +0800683 unsigned *log_num,
684 unsigned int quota)
David Stevens8dd014a2010-07-27 18:52:21 +0300685{
686 unsigned int out, in;
687 int seg = 0;
688 int headcount = 0;
689 unsigned d;
690 int r, nlogs = 0;
Michael S. Tsirkin8b386942014-10-24 14:19:48 +0300691 /* len is always initialized before use since we are always called with
692 * datalen > 0.
693 */
694 u32 uninitialized_var(len);
David Stevens8dd014a2010-07-27 18:52:21 +0300695
Jason Wang94249362011-01-17 16:11:08 +0800696 while (datalen > 0 && headcount < quota) {
Jason Wange0e9b402010-09-14 23:53:05 +0800697 if (unlikely(seg >= UIO_MAXIOV)) {
David Stevens8dd014a2010-07-27 18:52:21 +0300698 r = -ENOBUFS;
699 goto err;
700 }
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300701 r = vhost_get_vq_desc(vq, vq->iov + seg,
David Stevens8dd014a2010-07-27 18:52:21 +0300702 ARRAY_SIZE(vq->iov) - seg, &out,
703 &in, log, log_num);
Michael S. Tsirkina39ee442014-03-27 12:53:37 +0200704 if (unlikely(r < 0))
705 goto err;
706
707 d = r;
David Stevens8dd014a2010-07-27 18:52:21 +0300708 if (d == vq->num) {
709 r = 0;
710 goto err;
711 }
712 if (unlikely(out || in <= 0)) {
713 vq_err(vq, "unexpected descriptor format for RX: "
714 "out %d, in %d\n", out, in);
715 r = -EINVAL;
716 goto err;
717 }
718 if (unlikely(log)) {
719 nlogs += *log_num;
720 log += *log_num;
721 }
Michael S. Tsirkin8b386942014-10-24 14:19:48 +0300722 heads[headcount].id = cpu_to_vhost32(vq, d);
723 len = iov_length(vq->iov + seg, in);
724 heads[headcount].len = cpu_to_vhost32(vq, len);
725 datalen -= len;
David Stevens8dd014a2010-07-27 18:52:21 +0300726 ++headcount;
727 seg += in;
728 }
Michael S. Tsirkin99975cc2015-01-07 10:51:00 +0200729 heads[headcount - 1].len = cpu_to_vhost32(vq, len + datalen);
David Stevens8dd014a2010-07-27 18:52:21 +0300730 *iovcount = seg;
731 if (unlikely(log))
732 *log_num = nlogs;
Michael S. Tsirkind8316f32014-03-27 12:00:26 +0200733
734 /* Detect overrun */
735 if (unlikely(datalen > 0)) {
736 r = UIO_MAXIOV + 1;
737 goto err;
738 }
David Stevens8dd014a2010-07-27 18:52:21 +0300739 return headcount;
740err:
741 vhost_discard_vq_desc(vq, headcount);
742 return r;
743}
744
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000745/* Expects to be always run from workqueue - which acts as
746 * read-size critical section for our kind of RCU. */
Jason Wang94249362011-01-17 16:11:08 +0800747static void handle_rx(struct vhost_net *net)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000748{
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300749 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX];
750 struct vhost_virtqueue *vq = &nvq->vq;
David Stevens8dd014a2010-07-27 18:52:21 +0300751 unsigned uninitialized_var(in), log;
752 struct vhost_log *vq_log;
753 struct msghdr msg = {
754 .msg_name = NULL,
755 .msg_namelen = 0,
756 .msg_control = NULL, /* FIXME: get and handle RX aux data. */
757 .msg_controllen = 0,
David Stevens8dd014a2010-07-27 18:52:21 +0300758 .msg_flags = MSG_DONTWAIT,
759 };
Jason Wang0960b642015-02-15 16:35:17 +0800760 struct virtio_net_hdr hdr = {
761 .flags = 0,
762 .gso_type = VIRTIO_NET_HDR_GSO_NONE
David Stevens8dd014a2010-07-27 18:52:21 +0300763 };
David Stevens8dd014a2010-07-27 18:52:21 +0300764 size_t total_len = 0;
Michael S. Tsirkin910a5782012-10-24 20:37:51 +0200765 int err, mergeable;
Jason Wange2b3b352018-01-09 18:27:45 +0800766 s16 headcount, nheads = 0;
David Stevens8dd014a2010-07-27 18:52:21 +0300767 size_t vhost_hlen, sock_hlen;
768 size_t vhost_len, sock_len;
Asias He2e26af72013-05-07 14:54:33 +0800769 struct socket *sock;
Al Viroba7438a2014-12-10 15:51:28 -0500770 struct iov_iter fixup;
Jason Wang0960b642015-02-15 16:35:17 +0800771 __virtio16 num_buffers;
David Stevens8dd014a2010-07-27 18:52:21 +0300772
Jason Wangaaa31492018-03-26 16:10:23 +0800773 mutex_lock_nested(&vq->mutex, 0);
Asias He2e26af72013-05-07 14:54:33 +0800774 sock = vq->private_data;
775 if (!sock)
776 goto out;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400777
778 if (!vq_iotlb_prefetch(vq))
779 goto out;
780
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300781 vhost_disable_notify(&net->dev, vq);
Jason Wang8241a1e2016-06-01 01:56:33 -0400782 vhost_net_disable_vq(net, vq);
Asias He2e26af72013-05-07 14:54:33 +0800783
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300784 vhost_hlen = nvq->vhost_hlen;
785 sock_hlen = nvq->sock_hlen;
David Stevens8dd014a2010-07-27 18:52:21 +0300786
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300787 vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
David Stevens8dd014a2010-07-27 18:52:21 +0300788 vq->log : NULL;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300789 mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF);
David Stevens8dd014a2010-07-27 18:52:21 +0300790
Jason Wang03088132016-03-04 06:24:53 -0500791 while ((sock_len = vhost_net_rx_peek_head_len(net, sock->sk))) {
David Stevens8dd014a2010-07-27 18:52:21 +0300792 sock_len += sock_hlen;
793 vhost_len = sock_len + vhost_hlen;
Jason Wange2b3b352018-01-09 18:27:45 +0800794 headcount = get_rx_bufs(vq, vq->heads + nheads, vhost_len,
Jason Wang94249362011-01-17 16:11:08 +0800795 &in, vq_log, &log,
796 likely(mergeable) ? UIO_MAXIOV : 1);
David Stevens8dd014a2010-07-27 18:52:21 +0300797 /* On error, stop handling until the next kick. */
798 if (unlikely(headcount < 0))
Jason Wang8241a1e2016-06-01 01:56:33 -0400799 goto out;
David Stevens8dd014a2010-07-27 18:52:21 +0300800 /* OK, now we need to know about added descriptors. */
801 if (!headcount) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300802 if (unlikely(vhost_enable_notify(&net->dev, vq))) {
David Stevens8dd014a2010-07-27 18:52:21 +0300803 /* They have slipped one in as we were
804 * doing that: check again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300805 vhost_disable_notify(&net->dev, vq);
David Stevens8dd014a2010-07-27 18:52:21 +0300806 continue;
807 }
808 /* Nothing new? Wait for eventfd to tell us
809 * they refilled. */
Jason Wang8241a1e2016-06-01 01:56:33 -0400810 goto out;
David Stevens8dd014a2010-07-27 18:52:21 +0300811 }
Jason Wang5990a302018-01-04 11:14:27 +0800812 if (nvq->rx_ring)
Wei Xu6e474082017-12-01 05:10:36 -0500813 msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
814 /* On overrun, truncate and discard */
815 if (unlikely(headcount > UIO_MAXIOV)) {
816 iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
817 err = sock->ops->recvmsg(sock, &msg,
818 1, MSG_DONTWAIT | MSG_TRUNC);
819 pr_debug("Discarded rx packet: len %zd\n", sock_len);
820 continue;
821 }
David Stevens8dd014a2010-07-27 18:52:21 +0300822 /* We don't need to be notified again. */
Al Viroba7438a2014-12-10 15:51:28 -0500823 iov_iter_init(&msg.msg_iter, READ, vq->iov, in, vhost_len);
824 fixup = msg.msg_iter;
825 if (unlikely((vhost_hlen))) {
826 /* We will supply the header ourselves
827 * TODO: support TSO.
828 */
829 iov_iter_advance(&msg.msg_iter, vhost_hlen);
Al Viroba7438a2014-12-10 15:51:28 -0500830 }
Ying Xue1b784142015-03-02 15:37:48 +0800831 err = sock->ops->recvmsg(sock, &msg,
David Stevens8dd014a2010-07-27 18:52:21 +0300832 sock_len, MSG_DONTWAIT | MSG_TRUNC);
833 /* Userspace might have consumed the packet meanwhile:
834 * it's not supposed to do this usually, but might be hard
835 * to prevent. Discard data we got (if any) and keep going. */
836 if (unlikely(err != sock_len)) {
837 pr_debug("Discarded rx packet: "
838 " len %d, expected %zd\n", err, sock_len);
839 vhost_discard_vq_desc(vq, headcount);
840 continue;
841 }
Al Viroba7438a2014-12-10 15:51:28 -0500842 /* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */
Michael S. Tsirkin4c5a8442015-02-25 15:19:28 +0100843 if (unlikely(vhost_hlen)) {
844 if (copy_to_iter(&hdr, sizeof(hdr),
845 &fixup) != sizeof(hdr)) {
846 vq_err(vq, "Unable to write vnet_hdr "
847 "at addr %p\n", vq->iov->iov_base);
Jason Wang8241a1e2016-06-01 01:56:33 -0400848 goto out;
Michael S. Tsirkin4c5a8442015-02-25 15:19:28 +0100849 }
850 } else {
851 /* Header came from socket; we'll need to patch
852 * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF
853 */
854 iov_iter_advance(&fixup, sizeof(hdr));
David Stevens8dd014a2010-07-27 18:52:21 +0300855 }
856 /* TODO: Should check and handle checksum. */
Michael S. Tsirkin5201aa42015-02-03 11:07:06 +0200857
Jason Wang0960b642015-02-15 16:35:17 +0800858 num_buffers = cpu_to_vhost16(vq, headcount);
Jason Wangcfbdab92011-01-17 16:10:59 +0800859 if (likely(mergeable) &&
Michael S. Tsirkin0d79a492015-02-25 15:20:01 +0100860 copy_to_iter(&num_buffers, sizeof num_buffers,
861 &fixup) != sizeof num_buffers) {
David Stevens8dd014a2010-07-27 18:52:21 +0300862 vq_err(vq, "Failed num_buffers write");
863 vhost_discard_vq_desc(vq, headcount);
Jason Wang8241a1e2016-06-01 01:56:33 -0400864 goto out;
David Stevens8dd014a2010-07-27 18:52:21 +0300865 }
Jason Wange2b3b352018-01-09 18:27:45 +0800866 nheads += headcount;
867 if (nheads > VHOST_RX_BATCH) {
868 vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
869 nheads);
870 nheads = 0;
871 }
David Stevens8dd014a2010-07-27 18:52:21 +0300872 if (unlikely(vq_log))
873 vhost_log_write(vq, vq_log, log, vhost_len);
874 total_len += vhost_len;
875 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
876 vhost_poll_queue(&vq->poll);
Jason Wang8241a1e2016-06-01 01:56:33 -0400877 goto out;
David Stevens8dd014a2010-07-27 18:52:21 +0300878 }
879 }
Jason Wang8241a1e2016-06-01 01:56:33 -0400880 vhost_net_enable_vq(net, vq);
Asias He2e26af72013-05-07 14:54:33 +0800881out:
Jason Wange2b3b352018-01-09 18:27:45 +0800882 if (nheads)
883 vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
884 nheads);
David Stevens8dd014a2010-07-27 18:52:21 +0300885 mutex_unlock(&vq->mutex);
David Stevens8dd014a2010-07-27 18:52:21 +0300886}
887
Tejun Heoc23f34452010-06-02 20:40:00 +0200888static void handle_tx_kick(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000889{
Tejun Heoc23f34452010-06-02 20:40:00 +0200890 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
891 poll.work);
892 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
893
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000894 handle_tx(net);
895}
896
Tejun Heoc23f34452010-06-02 20:40:00 +0200897static void handle_rx_kick(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000898{
Tejun Heoc23f34452010-06-02 20:40:00 +0200899 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
900 poll.work);
901 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
902
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000903 handle_rx(net);
904}
905
Tejun Heoc23f34452010-06-02 20:40:00 +0200906static void handle_tx_net(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000907{
Tejun Heoc23f34452010-06-02 20:40:00 +0200908 struct vhost_net *net = container_of(work, struct vhost_net,
909 poll[VHOST_NET_VQ_TX].work);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000910 handle_tx(net);
911}
912
Tejun Heoc23f34452010-06-02 20:40:00 +0200913static void handle_rx_net(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000914{
Tejun Heoc23f34452010-06-02 20:40:00 +0200915 struct vhost_net *net = container_of(work, struct vhost_net,
916 poll[VHOST_NET_VQ_RX].work);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000917 handle_rx(net);
918}
919
920static int vhost_net_open(struct inode *inode, struct file *f)
921{
Michael S. Tsirkin23cc5a92013-01-23 21:46:47 +0100922 struct vhost_net *n;
Tejun Heoc23f34452010-06-02 20:40:00 +0200923 struct vhost_dev *dev;
Asias He3ab2e422013-04-27 11:16:48 +0800924 struct vhost_virtqueue **vqs;
Jason Wang5990a302018-01-04 11:14:27 +0800925 void **queue;
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +0800926 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200927
Michal Hockodcda9b02017-07-12 14:36:45 -0700928 n = kvmalloc(sizeof *n, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hocko6c5ab652017-05-08 15:57:15 -0700929 if (!n)
930 return -ENOMEM;
Asias He3ab2e422013-04-27 11:16:48 +0800931 vqs = kmalloc(VHOST_NET_VQ_MAX * sizeof(*vqs), GFP_KERNEL);
932 if (!vqs) {
Romain Francoised04257b2014-06-12 10:42:34 +0200933 kvfree(n);
Asias He3ab2e422013-04-27 11:16:48 +0800934 return -ENOMEM;
935 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200936
Jason Wang5990a302018-01-04 11:14:27 +0800937 queue = kmalloc_array(VHOST_RX_BATCH, sizeof(void *),
Jason Wangc67df112017-05-17 12:14:45 +0800938 GFP_KERNEL);
939 if (!queue) {
940 kfree(vqs);
941 kvfree(n);
942 return -ENOMEM;
943 }
944 n->vqs[VHOST_NET_VQ_RX].rxq.queue = queue;
945
Tejun Heoc23f34452010-06-02 20:40:00 +0200946 dev = &n->dev;
Asias He3ab2e422013-04-27 11:16:48 +0800947 vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq;
948 vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq;
949 n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick;
950 n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick;
Asias He28394002013-04-27 15:07:46 +0800951 for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
952 n->vqs[i].ubufs = NULL;
953 n->vqs[i].ubuf_info = NULL;
954 n->vqs[i].upend_idx = 0;
955 n->vqs[i].done_idx = 0;
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300956 n->vqs[i].vhost_hlen = 0;
957 n->vqs[i].sock_hlen = 0;
Alexander Potapenkoab7e34b2018-03-09 14:50:32 +0800958 n->vqs[i].rx_ring = NULL;
Jason Wangc67df112017-05-17 12:14:45 +0800959 vhost_net_buf_init(&n->vqs[i].rxq);
Asias He28394002013-04-27 15:07:46 +0800960 }
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +0800961 vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000962
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800963 vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, EPOLLOUT, dev);
964 vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, EPOLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000965
966 f->private_data = n;
967
968 return 0;
969}
970
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000971static struct socket *vhost_net_stop_vq(struct vhost_net *n,
972 struct vhost_virtqueue *vq)
973{
974 struct socket *sock;
Jason Wangc67df112017-05-17 12:14:45 +0800975 struct vhost_net_virtqueue *nvq =
976 container_of(vq, struct vhost_net_virtqueue, vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000977
978 mutex_lock(&vq->mutex);
Asias He22fa90c2013-05-07 14:54:36 +0800979 sock = vq->private_data;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000980 vhost_net_disable_vq(n, vq);
Asias He22fa90c2013-05-07 14:54:36 +0800981 vq->private_data = NULL;
Jason Wangc67df112017-05-17 12:14:45 +0800982 vhost_net_buf_unproduce(nvq);
Jason Wang303fd71b2018-03-09 14:50:33 +0800983 nvq->rx_ring = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000984 mutex_unlock(&vq->mutex);
985 return sock;
986}
987
988static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
989 struct socket **rx_sock)
990{
Asias He3ab2e422013-04-27 11:16:48 +0800991 *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq);
992 *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000993}
994
995static void vhost_net_flush_vq(struct vhost_net *n, int index)
996{
997 vhost_poll_flush(n->poll + index);
Asias He3ab2e422013-04-27 11:16:48 +0800998 vhost_poll_flush(&n->vqs[index].vq.poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000999}
1000
1001static void vhost_net_flush(struct vhost_net *n)
1002{
1003 vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
1004 vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
Asias He28394002013-04-27 15:07:46 +08001005 if (n->vqs[VHOST_NET_VQ_TX].ubufs) {
Asias He3ab2e422013-04-27 11:16:48 +08001006 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +02001007 n->tx_flush = true;
Asias He3ab2e422013-04-27 11:16:48 +08001008 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +02001009 /* Wait for all lower device DMAs done. */
Asias Hefe729a52013-05-06 16:38:24 +08001010 vhost_net_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs);
Asias He3ab2e422013-04-27 11:16:48 +08001011 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +02001012 n->tx_flush = false;
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +02001013 atomic_set(&n->vqs[VHOST_NET_VQ_TX].ubufs->refcount, 1);
Asias He3ab2e422013-04-27 11:16:48 +08001014 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +02001015 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001016}
1017
1018static int vhost_net_release(struct inode *inode, struct file *f)
1019{
1020 struct vhost_net *n = f->private_data;
1021 struct socket *tx_sock;
1022 struct socket *rx_sock;
1023
1024 vhost_net_stop(n, &tx_sock, &rx_sock);
1025 vhost_net_flush(n);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +00001026 vhost_dev_stop(&n->dev);
夷则(Caspar)f6f93f72017-12-25 00:08:58 +08001027 vhost_dev_cleanup(&n->dev);
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +03001028 vhost_net_vq_reset(n);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001029 if (tx_sock)
Al Viro09aaacf2014-03-05 20:39:00 -05001030 sockfd_put(tx_sock);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001031 if (rx_sock)
Al Viro09aaacf2014-03-05 20:39:00 -05001032 sockfd_put(rx_sock);
Michael S. Tsirkinb0c057c2014-02-13 11:45:11 +02001033 /* Make sure no callbacks are outstanding */
1034 synchronize_rcu_bh();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001035 /* We do an extra flush before freeing memory,
1036 * since jobs can re-queue themselves. */
1037 vhost_net_flush(n);
Jason Wangc67df112017-05-17 12:14:45 +08001038 kfree(n->vqs[VHOST_NET_VQ_RX].rxq.queue);
Asias He3ab2e422013-04-27 11:16:48 +08001039 kfree(n->dev.vqs);
Romain Francoised04257b2014-06-12 10:42:34 +02001040 kvfree(n);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001041 return 0;
1042}
1043
1044static struct socket *get_raw_socket(int fd)
1045{
1046 struct {
1047 struct sockaddr_ll sa;
1048 char buf[MAX_ADDR_LEN];
1049 } uaddr;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001050 int r;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001051 struct socket *sock = sockfd_lookup(fd, &r);
Krishna Kumard47effe2011-03-01 17:06:37 +05301052
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001053 if (!sock)
1054 return ERR_PTR(-ENOTSOCK);
1055
1056 /* Parameter checking */
1057 if (sock->sk->sk_type != SOCK_RAW) {
1058 r = -ESOCKTNOSUPPORT;
1059 goto err;
1060 }
1061
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001062 r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa, 0);
1063 if (r < 0)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001064 goto err;
1065
1066 if (uaddr.sa.sll_family != AF_PACKET) {
1067 r = -EPFNOSUPPORT;
1068 goto err;
1069 }
1070 return sock;
1071err:
Al Viro09aaacf2014-03-05 20:39:00 -05001072 sockfd_put(sock);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001073 return ERR_PTR(r);
1074}
1075
Jason Wang5990a302018-01-04 11:14:27 +08001076static struct ptr_ring *get_tap_ptr_ring(int fd)
Jason Wangc67df112017-05-17 12:14:45 +08001077{
Jason Wang5990a302018-01-04 11:14:27 +08001078 struct ptr_ring *ring;
Jason Wangc67df112017-05-17 12:14:45 +08001079 struct file *file = fget(fd);
1080
1081 if (!file)
1082 return NULL;
Jason Wang5990a302018-01-04 11:14:27 +08001083 ring = tun_get_tx_ring(file);
1084 if (!IS_ERR(ring))
Jason Wangc67df112017-05-17 12:14:45 +08001085 goto out;
Jason Wang5990a302018-01-04 11:14:27 +08001086 ring = tap_get_ptr_ring(file);
1087 if (!IS_ERR(ring))
Jason Wangc67df112017-05-17 12:14:45 +08001088 goto out;
Jason Wang5990a302018-01-04 11:14:27 +08001089 ring = NULL;
Jason Wangc67df112017-05-17 12:14:45 +08001090out:
1091 fput(file);
Jason Wang5990a302018-01-04 11:14:27 +08001092 return ring;
Jason Wangc67df112017-05-17 12:14:45 +08001093}
1094
Arnd Bergmann501c7742010-02-18 05:46:50 +00001095static struct socket *get_tap_socket(int fd)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001096{
1097 struct file *file = fget(fd);
1098 struct socket *sock;
Krishna Kumard47effe2011-03-01 17:06:37 +05301099
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001100 if (!file)
1101 return ERR_PTR(-EBADF);
1102 sock = tun_get_socket(file);
Arnd Bergmann501c7742010-02-18 05:46:50 +00001103 if (!IS_ERR(sock))
1104 return sock;
Sainath Grandhi635b8c82017-02-10 16:03:47 -08001105 sock = tap_get_socket(file);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001106 if (IS_ERR(sock))
1107 fput(file);
1108 return sock;
1109}
1110
1111static struct socket *get_socket(int fd)
1112{
1113 struct socket *sock;
Krishna Kumard47effe2011-03-01 17:06:37 +05301114
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001115 /* special case to disable backend */
1116 if (fd == -1)
1117 return NULL;
1118 sock = get_raw_socket(fd);
1119 if (!IS_ERR(sock))
1120 return sock;
Arnd Bergmann501c7742010-02-18 05:46:50 +00001121 sock = get_tap_socket(fd);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001122 if (!IS_ERR(sock))
1123 return sock;
1124 return ERR_PTR(-ENOTSOCK);
1125}
1126
1127static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
1128{
1129 struct socket *sock, *oldsock;
1130 struct vhost_virtqueue *vq;
Asias He28394002013-04-27 15:07:46 +08001131 struct vhost_net_virtqueue *nvq;
Asias Hefe729a52013-05-06 16:38:24 +08001132 struct vhost_net_ubuf_ref *ubufs, *oldubufs = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001133 int r;
1134
1135 mutex_lock(&n->dev.mutex);
1136 r = vhost_dev_check_owner(&n->dev);
1137 if (r)
1138 goto err;
1139
1140 if (index >= VHOST_NET_VQ_MAX) {
1141 r = -ENOBUFS;
1142 goto err;
1143 }
Asias He3ab2e422013-04-27 11:16:48 +08001144 vq = &n->vqs[index].vq;
Asias He28394002013-04-27 15:07:46 +08001145 nvq = &n->vqs[index];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001146 mutex_lock(&vq->mutex);
1147
1148 /* Verify that ring has been setup correctly. */
1149 if (!vhost_vq_access_ok(vq)) {
1150 r = -EFAULT;
Jeff Dike1dace8c2010-03-04 16:10:14 -05001151 goto err_vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001152 }
1153 sock = get_socket(fd);
1154 if (IS_ERR(sock)) {
1155 r = PTR_ERR(sock);
Jeff Dike1dace8c2010-03-04 16:10:14 -05001156 goto err_vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001157 }
1158
1159 /* start polling new socket */
Asias He22fa90c2013-05-07 14:54:36 +08001160 oldsock = vq->private_data;
David S. Miller11fe8832010-07-20 18:25:24 -07001161 if (sock != oldsock) {
Asias Hefe729a52013-05-06 16:38:24 +08001162 ubufs = vhost_net_ubuf_alloc(vq,
1163 sock && vhost_sock_zcopy(sock));
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +00001164 if (IS_ERR(ubufs)) {
1165 r = PTR_ERR(ubufs);
1166 goto err_ubufs;
1167 }
Jason Wang692a9982013-01-28 01:05:17 +00001168
Krishna Kumard47effe2011-03-01 17:06:37 +05301169 vhost_net_disable_vq(n, vq);
Asias He22fa90c2013-05-07 14:54:36 +08001170 vq->private_data = sock;
Jason Wangc67df112017-05-17 12:14:45 +08001171 vhost_net_buf_unproduce(nvq);
Greg Kurz80f7d032016-02-16 15:59:44 +01001172 r = vhost_vq_init_access(vq);
Jason Wangf59281d2011-06-21 18:04:27 +08001173 if (r)
Jason Wang692a9982013-01-28 01:05:17 +00001174 goto err_used;
Jason Wang2b8b3282013-01-28 01:05:18 +00001175 r = vhost_net_enable_vq(n, vq);
1176 if (r)
1177 goto err_used;
Jason Wang303fd71b2018-03-09 14:50:33 +08001178 if (index == VHOST_NET_VQ_RX)
1179 nvq->rx_ring = get_tap_ptr_ring(fd);
Jason Wang692a9982013-01-28 01:05:17 +00001180
Asias He28394002013-04-27 15:07:46 +08001181 oldubufs = nvq->ubufs;
1182 nvq->ubufs = ubufs;
Michael S. Tsirkin64e9a9b2012-12-03 07:31:51 +00001183
1184 n->tx_packets = 0;
1185 n->tx_zcopy_err = 0;
Michael S. Tsirkin1280c272012-12-04 00:17:14 +02001186 n->tx_flush = false;
Jeff Dikedd1f4072010-03-04 16:10:14 -05001187 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001188
Michael S. Tsirkin1680e902010-07-15 15:19:12 +03001189 mutex_unlock(&vq->mutex);
1190
Michael S. Tsirkinc047e5f2011-07-20 13:41:31 +03001191 if (oldubufs) {
Michael S. Tsirkinc38e39c2013-06-25 17:29:46 +03001192 vhost_net_ubuf_put_wait_and_free(oldubufs);
Michael S. Tsirkinc047e5f2011-07-20 13:41:31 +03001193 mutex_lock(&vq->mutex);
Michael S. Tsirkineaae8132012-11-01 09:16:51 +00001194 vhost_zerocopy_signal_used(n, vq);
Michael S. Tsirkinc047e5f2011-07-20 13:41:31 +03001195 mutex_unlock(&vq->mutex);
1196 }
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +00001197
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001198 if (oldsock) {
1199 vhost_net_flush_vq(n, index);
Al Viro09aaacf2014-03-05 20:39:00 -05001200 sockfd_put(oldsock);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001201 }
Jeff Dike1dace8c2010-03-04 16:10:14 -05001202
Michael S. Tsirkin1680e902010-07-15 15:19:12 +03001203 mutex_unlock(&n->dev.mutex);
1204 return 0;
1205
Jason Wang692a9982013-01-28 01:05:17 +00001206err_used:
Asias He22fa90c2013-05-07 14:54:36 +08001207 vq->private_data = oldsock;
Jason Wang692a9982013-01-28 01:05:17 +00001208 vhost_net_enable_vq(n, vq);
1209 if (ubufs)
Michael S. Tsirkinc38e39c2013-06-25 17:29:46 +03001210 vhost_net_ubuf_put_wait_and_free(ubufs);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +00001211err_ubufs:
Al Viro09aaacf2014-03-05 20:39:00 -05001212 sockfd_put(sock);
Jeff Dike1dace8c2010-03-04 16:10:14 -05001213err_vq:
1214 mutex_unlock(&vq->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001215err:
1216 mutex_unlock(&n->dev.mutex);
1217 return r;
1218}
1219
1220static long vhost_net_reset_owner(struct vhost_net *n)
1221{
1222 struct socket *tx_sock = NULL;
1223 struct socket *rx_sock = NULL;
1224 long err;
Jason Wanga9709d62016-06-23 02:04:31 -04001225 struct vhost_umem *umem;
Krishna Kumard47effe2011-03-01 17:06:37 +05301226
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001227 mutex_lock(&n->dev.mutex);
1228 err = vhost_dev_check_owner(&n->dev);
1229 if (err)
1230 goto done;
Jason Wanga9709d62016-06-23 02:04:31 -04001231 umem = vhost_dev_reset_owner_prepare();
1232 if (!umem) {
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +03001233 err = -ENOMEM;
1234 goto done;
1235 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001236 vhost_net_stop(n, &tx_sock, &rx_sock);
1237 vhost_net_flush(n);
Jason Wang4cd87952018-01-25 22:03:52 +08001238 vhost_dev_stop(&n->dev);
Jason Wanga9709d62016-06-23 02:04:31 -04001239 vhost_dev_reset_owner(&n->dev, umem);
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +03001240 vhost_net_vq_reset(n);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001241done:
1242 mutex_unlock(&n->dev.mutex);
1243 if (tx_sock)
Al Viro09aaacf2014-03-05 20:39:00 -05001244 sockfd_put(tx_sock);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001245 if (rx_sock)
Al Viro09aaacf2014-03-05 20:39:00 -05001246 sockfd_put(rx_sock);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001247 return err;
1248}
1249
1250static int vhost_net_set_features(struct vhost_net *n, u64 features)
1251{
David Stevens8dd014a2010-07-27 18:52:21 +03001252 size_t vhost_hlen, sock_hlen, hdr_len;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001253 int i;
David Stevens8dd014a2010-07-27 18:52:21 +03001254
Michael S. Tsirkine4fca7d2014-10-24 14:23:52 +03001255 hdr_len = (features & ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
1256 (1ULL << VIRTIO_F_VERSION_1))) ?
David Stevens8dd014a2010-07-27 18:52:21 +03001257 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
1258 sizeof(struct virtio_net_hdr);
1259 if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
1260 /* vhost provides vnet_hdr */
1261 vhost_hlen = hdr_len;
1262 sock_hlen = 0;
1263 } else {
1264 /* socket provides vnet_hdr */
1265 vhost_hlen = 0;
1266 sock_hlen = hdr_len;
1267 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001268 mutex_lock(&n->dev.mutex);
1269 if ((features & (1 << VHOST_F_LOG_ALL)) &&
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001270 !vhost_log_access_ok(&n->dev))
1271 goto out_unlock;
1272
1273 if ((features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))) {
1274 if (vhost_init_device_iotlb(&n->dev, true))
1275 goto out_unlock;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001276 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001277
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001278 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +08001279 mutex_lock(&n->vqs[i].vq.mutex);
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001280 n->vqs[i].vq.acked_features = features;
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +03001281 n->vqs[i].vhost_hlen = vhost_hlen;
1282 n->vqs[i].sock_hlen = sock_hlen;
Asias He3ab2e422013-04-27 11:16:48 +08001283 mutex_unlock(&n->vqs[i].vq.mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001284 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001285 mutex_unlock(&n->dev.mutex);
1286 return 0;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001287
1288out_unlock:
1289 mutex_unlock(&n->dev.mutex);
1290 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001291}
1292
Asias Heb1ad8492013-05-06 11:16:00 +08001293static long vhost_net_set_owner(struct vhost_net *n)
1294{
1295 int r;
1296
1297 mutex_lock(&n->dev.mutex);
Michael S. Tsirkin05c05352013-06-06 15:20:39 +03001298 if (vhost_dev_has_owner(&n->dev)) {
1299 r = -EBUSY;
1300 goto out;
1301 }
Asias Heb1ad8492013-05-06 11:16:00 +08001302 r = vhost_net_set_ubuf_info(n);
1303 if (r)
1304 goto out;
1305 r = vhost_dev_set_owner(&n->dev);
1306 if (r)
1307 vhost_net_clear_ubuf_info(n);
1308 vhost_net_flush(n);
1309out:
1310 mutex_unlock(&n->dev.mutex);
1311 return r;
1312}
1313
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001314static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
1315 unsigned long arg)
1316{
1317 struct vhost_net *n = f->private_data;
1318 void __user *argp = (void __user *)arg;
1319 u64 __user *featurep = argp;
1320 struct vhost_vring_file backend;
1321 u64 features;
1322 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301323
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001324 switch (ioctl) {
1325 case VHOST_NET_SET_BACKEND:
Takuya Yoshikawad3553a52010-05-27 19:01:58 +09001326 if (copy_from_user(&backend, argp, sizeof backend))
1327 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001328 return vhost_net_set_backend(n, backend.index, backend.fd);
1329 case VHOST_GET_FEATURES:
Stefan Hajnoczi0dd05a32012-07-21 06:55:36 +00001330 features = VHOST_NET_FEATURES;
Takuya Yoshikawad3553a52010-05-27 19:01:58 +09001331 if (copy_to_user(featurep, &features, sizeof features))
1332 return -EFAULT;
1333 return 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001334 case VHOST_SET_FEATURES:
Takuya Yoshikawad3553a52010-05-27 19:01:58 +09001335 if (copy_from_user(&features, featurep, sizeof features))
1336 return -EFAULT;
Stefan Hajnoczi0dd05a32012-07-21 06:55:36 +00001337 if (features & ~VHOST_NET_FEATURES)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001338 return -EOPNOTSUPP;
1339 return vhost_net_set_features(n, features);
1340 case VHOST_RESET_OWNER:
1341 return vhost_net_reset_owner(n);
Asias Heb1ad8492013-05-06 11:16:00 +08001342 case VHOST_SET_OWNER:
1343 return vhost_net_set_owner(n);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001344 default:
1345 mutex_lock(&n->dev.mutex);
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001346 r = vhost_dev_ioctl(&n->dev, ioctl, argp);
1347 if (r == -ENOIOCTLCMD)
1348 r = vhost_vring_ioctl(&n->dev, ioctl, argp);
1349 else
1350 vhost_net_flush(n);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001351 mutex_unlock(&n->dev.mutex);
1352 return r;
1353 }
1354}
1355
1356#ifdef CONFIG_COMPAT
1357static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
1358 unsigned long arg)
1359{
1360 return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1361}
1362#endif
1363
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001364static ssize_t vhost_net_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
1365{
1366 struct file *file = iocb->ki_filp;
1367 struct vhost_net *n = file->private_data;
1368 struct vhost_dev *dev = &n->dev;
1369 int noblock = file->f_flags & O_NONBLOCK;
1370
1371 return vhost_chr_read_iter(dev, to, noblock);
1372}
1373
1374static ssize_t vhost_net_chr_write_iter(struct kiocb *iocb,
1375 struct iov_iter *from)
1376{
1377 struct file *file = iocb->ki_filp;
1378 struct vhost_net *n = file->private_data;
1379 struct vhost_dev *dev = &n->dev;
1380
1381 return vhost_chr_write_iter(dev, from);
1382}
1383
Al Viroafc9a422017-07-03 06:39:46 -04001384static __poll_t vhost_net_chr_poll(struct file *file, poll_table *wait)
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001385{
1386 struct vhost_net *n = file->private_data;
1387 struct vhost_dev *dev = &n->dev;
1388
1389 return vhost_chr_poll(file, dev, wait);
1390}
1391
Tobias Klauser373a83a2010-05-17 15:12:49 +02001392static const struct file_operations vhost_net_fops = {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001393 .owner = THIS_MODULE,
1394 .release = vhost_net_release,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001395 .read_iter = vhost_net_chr_read_iter,
1396 .write_iter = vhost_net_chr_write_iter,
1397 .poll = vhost_net_chr_poll,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001398 .unlocked_ioctl = vhost_net_ioctl,
1399#ifdef CONFIG_COMPAT
1400 .compat_ioctl = vhost_net_compat_ioctl,
1401#endif
1402 .open = vhost_net_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001403 .llseek = noop_llseek,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001404};
1405
1406static struct miscdevice vhost_net_misc = {
stephen hemminger7c7c7f02012-01-11 19:30:38 +00001407 .minor = VHOST_NET_MINOR,
1408 .name = "vhost-net",
1409 .fops = &vhost_net_fops,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001410};
1411
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001412static int vhost_net_init(void)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001413{
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +00001414 if (experimental_zcopytx)
Asias Hefe729a52013-05-06 16:38:24 +08001415 vhost_net_enable_zcopy(VHOST_NET_VQ_TX);
Tejun Heoc23f34452010-06-02 20:40:00 +02001416 return misc_register(&vhost_net_misc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001417}
1418module_init(vhost_net_init);
1419
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001420static void vhost_net_exit(void)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001421{
1422 misc_deregister(&vhost_net_misc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001423}
1424module_exit(vhost_net_exit);
1425
1426MODULE_VERSION("0.0.1");
1427MODULE_LICENSE("GPL v2");
1428MODULE_AUTHOR("Michael S. Tsirkin");
1429MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
stephen hemminger7c7c7f02012-01-11 19:30:38 +00001430MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR);
1431MODULE_ALIAS("devname:vhost-net");