blob: 2fe35354f20e5ea82509fc9859435029a5a2dfcc [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>
Michael S. Tsirkin23cc5a92013-01-23 21:46:47 +010020#include <linux/vmalloc.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000021
22#include <linux/net.h>
23#include <linux/if_packet.h>
24#include <linux/if_arp.h>
25#include <linux/if_tun.h>
Arnd Bergmann501c7742010-02-18 05:46:50 +000026#include <linux/if_macvlan.h>
Sainath Grandhi635b8c82017-02-10 16:03:47 -080027#include <linux/if_tap.h>
Basil Gorc53cff5e2012-05-03 22:55:23 +000028#include <linux/if_vlan.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000029
30#include <net/sock.h>
31
32#include "vhost.h"
33
Michael S. Tsirkinf9611c42012-12-06 14:56:00 +020034static int experimental_zcopytx = 1;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +000035module_param(experimental_zcopytx, int, 0444);
Michael S. Tsirkinf9611c42012-12-06 14:56:00 +020036MODULE_PARM_DESC(experimental_zcopytx, "Enable Zero Copy TX;"
37 " 1 -Enable; 0 - Disable");
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +000038
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000039/* Max number of bytes transferred before requeueing the job.
40 * Using this limit prevents one virtqueue from starving others. */
41#define VHOST_NET_WEIGHT 0x80000
42
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +000043/* MAX number of TX used buffers for outstanding zerocopy */
44#define VHOST_MAX_PEND 128
45#define VHOST_GOODCOPY_LEN 256
46
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000047/*
48 * For transmit, used buffer len is unused; we override it to track buffer
49 * status internally; used for zerocopy tx only.
50 */
51/* Lower device DMA failed */
Michael S. Tsirkinbf995732014-10-24 11:49:27 +030052#define VHOST_DMA_FAILED_LEN ((__force __virtio32)3)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000053/* Lower device DMA done */
Michael S. Tsirkinbf995732014-10-24 11:49:27 +030054#define VHOST_DMA_DONE_LEN ((__force __virtio32)2)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000055/* Lower device DMA in progress */
Michael S. Tsirkinbf995732014-10-24 11:49:27 +030056#define VHOST_DMA_IN_PROGRESS ((__force __virtio32)1)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000057/* Buffer unused */
Michael S. Tsirkinbf995732014-10-24 11:49:27 +030058#define VHOST_DMA_CLEAR_LEN ((__force __virtio32)0)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000059
Michael S. Tsirkinbf995732014-10-24 11:49:27 +030060#define VHOST_DMA_IS_DONE(len) ((__force u32)(len) >= (__force u32)VHOST_DMA_DONE_LEN)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000061
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000062enum {
Asias He8570a6e2013-05-06 16:38:20 +080063 VHOST_NET_FEATURES = VHOST_FEATURES |
64 (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) |
Jason Wang6b1e6cc2016-06-23 02:04:32 -040065 (1ULL << VIRTIO_NET_F_MRG_RXBUF) |
66 (1ULL << VIRTIO_F_IOMMU_PLATFORM)
Asias He8570a6e2013-05-06 16:38:20 +080067};
68
69enum {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000070 VHOST_NET_VQ_RX = 0,
71 VHOST_NET_VQ_TX = 1,
72 VHOST_NET_VQ_MAX = 2,
73};
74
Asias Hefe729a52013-05-06 16:38:24 +080075struct vhost_net_ubuf_ref {
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +020076 /* refcount follows semantics similar to kref:
77 * 0: object is released
78 * 1: no outstanding ubufs
79 * >1: outstanding ubufs
80 */
81 atomic_t refcount;
Asias He28394002013-04-27 15:07:46 +080082 wait_queue_head_t wait;
83 struct vhost_virtqueue *vq;
84};
85
Asias He3ab2e422013-04-27 11:16:48 +080086struct vhost_net_virtqueue {
87 struct vhost_virtqueue vq;
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +030088 size_t vhost_hlen;
89 size_t sock_hlen;
Asias He28394002013-04-27 15:07:46 +080090 /* vhost zerocopy support fields below: */
91 /* last used idx for outstanding DMA zerocopy buffers */
92 int upend_idx;
93 /* first used idx for DMA done zerocopy buffers */
94 int done_idx;
95 /* an array of userspace buffers info */
96 struct ubuf_info *ubuf_info;
97 /* Reference counting for outstanding ubufs.
98 * Protected by vq mutex. Writers must also take device mutex. */
Asias Hefe729a52013-05-06 16:38:24 +080099 struct vhost_net_ubuf_ref *ubufs;
Asias He3ab2e422013-04-27 11:16:48 +0800100};
101
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000102struct vhost_net {
103 struct vhost_dev dev;
Asias He3ab2e422013-04-27 11:16:48 +0800104 struct vhost_net_virtqueue vqs[VHOST_NET_VQ_MAX];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000105 struct vhost_poll poll[VHOST_NET_VQ_MAX];
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000106 /* Number of TX recently submitted.
107 * Protected by tx vq lock. */
108 unsigned tx_packets;
109 /* Number of times zerocopy TX recently failed.
110 * Protected by tx vq lock. */
111 unsigned tx_zcopy_err;
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200112 /* Flush in progress. Protected by tx vq lock. */
113 bool tx_flush;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000114};
115
Asias Hefe729a52013-05-06 16:38:24 +0800116static unsigned vhost_net_zcopy_mask __read_mostly;
Asias He28394002013-04-27 15:07:46 +0800117
Asias Hefe729a52013-05-06 16:38:24 +0800118static void vhost_net_enable_zcopy(int vq)
Asias He28394002013-04-27 15:07:46 +0800119{
Asias Hefe729a52013-05-06 16:38:24 +0800120 vhost_net_zcopy_mask |= 0x1 << vq;
Asias He28394002013-04-27 15:07:46 +0800121}
122
Asias Hefe729a52013-05-06 16:38:24 +0800123static struct vhost_net_ubuf_ref *
124vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy)
Asias He28394002013-04-27 15:07:46 +0800125{
Asias Hefe729a52013-05-06 16:38:24 +0800126 struct vhost_net_ubuf_ref *ubufs;
Asias He28394002013-04-27 15:07:46 +0800127 /* No zero copy backend? Nothing to count. */
128 if (!zcopy)
129 return NULL;
130 ubufs = kmalloc(sizeof(*ubufs), GFP_KERNEL);
131 if (!ubufs)
132 return ERR_PTR(-ENOMEM);
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200133 atomic_set(&ubufs->refcount, 1);
Asias He28394002013-04-27 15:07:46 +0800134 init_waitqueue_head(&ubufs->wait);
135 ubufs->vq = vq;
136 return ubufs;
137}
138
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200139static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref *ubufs)
Asias He28394002013-04-27 15:07:46 +0800140{
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200141 int r = atomic_sub_return(1, &ubufs->refcount);
142 if (unlikely(!r))
143 wake_up(&ubufs->wait);
144 return r;
Asias He28394002013-04-27 15:07:46 +0800145}
146
Asias Hefe729a52013-05-06 16:38:24 +0800147static void vhost_net_ubuf_put_and_wait(struct vhost_net_ubuf_ref *ubufs)
Asias He28394002013-04-27 15:07:46 +0800148{
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200149 vhost_net_ubuf_put(ubufs);
150 wait_event(ubufs->wait, !atomic_read(&ubufs->refcount));
Michael S. Tsirkinc38e39c2013-06-25 17:29:46 +0300151}
152
153static void vhost_net_ubuf_put_wait_and_free(struct vhost_net_ubuf_ref *ubufs)
154{
155 vhost_net_ubuf_put_and_wait(ubufs);
Asias He28394002013-04-27 15:07:46 +0800156 kfree(ubufs);
157}
158
Asias Heb1ad8492013-05-06 11:16:00 +0800159static void vhost_net_clear_ubuf_info(struct vhost_net *n)
160{
Asias Heb1ad8492013-05-06 11:16:00 +0800161 int i;
162
Michael S. Tsirkin288cfe72013-06-06 15:20:46 +0300163 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
164 kfree(n->vqs[i].ubuf_info);
165 n->vqs[i].ubuf_info = NULL;
Asias Heb1ad8492013-05-06 11:16:00 +0800166 }
167}
168
Asias He0a1febf2013-06-05 21:17:38 +0800169static int vhost_net_set_ubuf_info(struct vhost_net *n)
Asias He28394002013-04-27 15:07:46 +0800170{
171 bool zcopy;
172 int i;
173
Michael S. Tsirkin288cfe72013-06-06 15:20:46 +0300174 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
Asias Hefe729a52013-05-06 16:38:24 +0800175 zcopy = vhost_net_zcopy_mask & (0x1 << i);
Asias He28394002013-04-27 15:07:46 +0800176 if (!zcopy)
177 continue;
178 n->vqs[i].ubuf_info = kmalloc(sizeof(*n->vqs[i].ubuf_info) *
179 UIO_MAXIOV, GFP_KERNEL);
180 if (!n->vqs[i].ubuf_info)
181 goto err;
182 }
183 return 0;
184
185err:
Michael S. Tsirkin288cfe72013-06-06 15:20:46 +0300186 vhost_net_clear_ubuf_info(n);
Asias He28394002013-04-27 15:07:46 +0800187 return -ENOMEM;
188}
189
Asias He0a1febf2013-06-05 21:17:38 +0800190static void vhost_net_vq_reset(struct vhost_net *n)
Asias He28394002013-04-27 15:07:46 +0800191{
192 int i;
193
Michael S. Tsirkin288cfe72013-06-06 15:20:46 +0300194 vhost_net_clear_ubuf_info(n);
195
Asias He28394002013-04-27 15:07:46 +0800196 for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
197 n->vqs[i].done_idx = 0;
198 n->vqs[i].upend_idx = 0;
199 n->vqs[i].ubufs = NULL;
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300200 n->vqs[i].vhost_hlen = 0;
201 n->vqs[i].sock_hlen = 0;
Asias He28394002013-04-27 15:07:46 +0800202 }
203
204}
205
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000206static void vhost_net_tx_packet(struct vhost_net *net)
207{
208 ++net->tx_packets;
209 if (net->tx_packets < 1024)
210 return;
211 net->tx_packets = 0;
212 net->tx_zcopy_err = 0;
213}
214
215static void vhost_net_tx_err(struct vhost_net *net)
216{
217 ++net->tx_zcopy_err;
218}
219
220static bool vhost_net_tx_select_zcopy(struct vhost_net *net)
221{
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200222 /* TX flush waits for outstanding DMAs to be done.
223 * Don't start new DMAs.
224 */
225 return !net->tx_flush &&
226 net->tx_packets / 64 >= net->tx_zcopy_err;
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000227}
228
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000229static bool vhost_sock_zcopy(struct socket *sock)
230{
231 return unlikely(experimental_zcopytx) &&
232 sock_flag(sock->sk, SOCK_ZEROCOPY);
233}
234
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000235/* In case of DMA done not in order in lower device driver for some reason.
236 * upend_idx is used to track end of used idx, done_idx is used to track head
237 * of used idx. Once lower device DMA done contiguously, we will signal KVM
238 * guest used idx.
239 */
Jason Wang094afe72013-09-02 16:40:56 +0800240static void vhost_zerocopy_signal_used(struct vhost_net *net,
241 struct vhost_virtqueue *vq)
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000242{
Asias He28394002013-04-27 15:07:46 +0800243 struct vhost_net_virtqueue *nvq =
244 container_of(vq, struct vhost_net_virtqueue, vq);
Jason Wangc92112a2013-09-02 16:40:57 +0800245 int i, add;
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000246 int j = 0;
247
Asias He28394002013-04-27 15:07:46 +0800248 for (i = nvq->done_idx; i != nvq->upend_idx; i = (i + 1) % UIO_MAXIOV) {
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000249 if (vq->heads[i].len == VHOST_DMA_FAILED_LEN)
250 vhost_net_tx_err(net);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000251 if (VHOST_DMA_IS_DONE(vq->heads[i].len)) {
252 vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000253 ++j;
254 } else
255 break;
256 }
Jason Wangc92112a2013-09-02 16:40:57 +0800257 while (j) {
258 add = min(UIO_MAXIOV - nvq->done_idx, j);
259 vhost_add_used_and_signal_n(vq->dev, vq,
260 &vq->heads[nvq->done_idx], add);
261 nvq->done_idx = (nvq->done_idx + add) % UIO_MAXIOV;
262 j -= add;
263 }
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000264}
265
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000266static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000267{
Asias Hefe729a52013-05-06 16:38:24 +0800268 struct vhost_net_ubuf_ref *ubufs = ubuf->ctx;
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000269 struct vhost_virtqueue *vq = ubufs->vq;
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200270 int cnt;
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000271
Michael S. Tsirkinb0c057c2014-02-13 11:45:11 +0200272 rcu_read_lock_bh();
273
Jason Wang19c73b32013-09-02 16:41:00 +0800274 /* set len to mark this desc buffers done DMA */
275 vq->heads[ubuf->desc].len = success ?
276 VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200277 cnt = vhost_net_ubuf_put(ubufs);
Jason Wang19c73b32013-09-02 16:41:00 +0800278
Michael S. Tsirkin24eb21a2012-11-01 09:16:55 +0000279 /*
280 * Trigger polling thread if guest stopped submitting new buffers:
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200281 * in this case, the refcount after decrement will eventually reach 1.
Michael S. Tsirkin24eb21a2012-11-01 09:16:55 +0000282 * We also trigger polling periodically after each 16 packets
283 * (the value 16 here is more or less arbitrary, it's tuned to trigger
284 * less than 10% of times).
285 */
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200286 if (cnt <= 1 || !(cnt % 16))
Michael S. Tsirkin24eb21a2012-11-01 09:16:55 +0000287 vhost_poll_queue(&vq->poll);
Michael S. Tsirkinb0c057c2014-02-13 11:45:11 +0200288
289 rcu_read_unlock_bh();
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000290}
291
Jason Wang03088132016-03-04 06:24:53 -0500292static inline unsigned long busy_clock(void)
293{
294 return local_clock() >> 10;
295}
296
297static bool vhost_can_busy_poll(struct vhost_dev *dev,
298 unsigned long endtime)
299{
300 return likely(!need_resched()) &&
301 likely(!time_after(busy_clock(), endtime)) &&
302 likely(!signal_pending(current)) &&
303 !vhost_has_work(dev);
304}
305
Jason Wang8241a1e2016-06-01 01:56:33 -0400306static void vhost_net_disable_vq(struct vhost_net *n,
307 struct vhost_virtqueue *vq)
308{
309 struct vhost_net_virtqueue *nvq =
310 container_of(vq, struct vhost_net_virtqueue, vq);
311 struct vhost_poll *poll = n->poll + (nvq - n->vqs);
312 if (!vq->private_data)
313 return;
314 vhost_poll_stop(poll);
315}
316
317static int vhost_net_enable_vq(struct vhost_net *n,
318 struct vhost_virtqueue *vq)
319{
320 struct vhost_net_virtqueue *nvq =
321 container_of(vq, struct vhost_net_virtqueue, vq);
322 struct vhost_poll *poll = n->poll + (nvq - n->vqs);
323 struct socket *sock;
324
325 sock = vq->private_data;
326 if (!sock)
327 return 0;
328
329 return vhost_poll_start(poll, sock->file);
330}
331
Jason Wang03088132016-03-04 06:24:53 -0500332static int vhost_net_tx_get_vq_desc(struct vhost_net *net,
333 struct vhost_virtqueue *vq,
334 struct iovec iov[], unsigned int iov_size,
335 unsigned int *out_num, unsigned int *in_num)
336{
337 unsigned long uninitialized_var(endtime);
338 int r = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400339 out_num, in_num, NULL, NULL);
Jason Wang03088132016-03-04 06:24:53 -0500340
341 if (r == vq->num && vq->busyloop_timeout) {
342 preempt_disable();
343 endtime = busy_clock() + vq->busyloop_timeout;
344 while (vhost_can_busy_poll(vq->dev, endtime) &&
345 vhost_vq_avail_empty(vq->dev, vq))
Christian Borntraegerf2f09a42016-10-25 11:03:14 +0200346 cpu_relax();
Jason Wang03088132016-03-04 06:24:53 -0500347 preempt_enable();
348 r = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400349 out_num, in_num, NULL, NULL);
Jason Wang03088132016-03-04 06:24:53 -0500350 }
351
352 return r;
353}
354
Jason Wang0ed005c2017-01-18 15:02:02 +0800355static bool vhost_exceeds_maxpend(struct vhost_net *net)
356{
357 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
358 struct vhost_virtqueue *vq = &nvq->vq;
359
360 return (nvq->upend_idx + vq->num - VHOST_MAX_PEND) % UIO_MAXIOV
361 == nvq->done_idx;
362}
363
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000364/* Expects to be always run from workqueue - which acts as
365 * read-size critical section for our kind of RCU. */
366static void handle_tx(struct vhost_net *net)
367{
Asias He28394002013-04-27 15:07:46 +0800368 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300369 struct vhost_virtqueue *vq = &nvq->vq;
Al Viro98a527a2014-12-10 15:00:58 -0500370 unsigned out, in;
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300371 int head;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000372 struct msghdr msg = {
373 .msg_name = NULL,
374 .msg_namelen = 0,
375 .msg_control = NULL,
376 .msg_controllen = 0,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000377 .msg_flags = MSG_DONTWAIT,
378 };
379 size_t len, total_len = 0;
Jason Wang70181d512013-04-10 20:50:48 +0000380 int err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000381 size_t hdr_size;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100382 struct socket *sock;
Asias Hefe729a52013-05-06 16:38:24 +0800383 struct vhost_net_ubuf_ref *uninitialized_var(ubufs);
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200384 bool zcopy, zcopy_used;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100385
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000386 mutex_lock(&vq->mutex);
Asias He2e26af72013-05-07 14:54:33 +0800387 sock = vq->private_data;
388 if (!sock)
389 goto out;
390
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400391 if (!vq_iotlb_prefetch(vq))
392 goto out;
393
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300394 vhost_disable_notify(&net->dev, vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000395
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300396 hdr_size = nvq->vhost_hlen;
Asias He28394002013-04-27 15:07:46 +0800397 zcopy = nvq->ubufs;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000398
399 for (;;) {
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000400 /* Release DMAs done buffers first */
401 if (zcopy)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000402 vhost_zerocopy_signal_used(net, vq);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000403
Jason Wangf7c6be42013-09-02 16:41:01 +0800404 /* If more outstanding DMAs, queue the work.
405 * Handle upend_idx wrap around
406 */
Jason Wang0ed005c2017-01-18 15:02:02 +0800407 if (unlikely(vhost_exceeds_maxpend(net)))
Jason Wangf7c6be42013-09-02 16:41:01 +0800408 break;
409
Jason Wang03088132016-03-04 06:24:53 -0500410 head = vhost_net_tx_get_vq_desc(net, vq, vq->iov,
411 ARRAY_SIZE(vq->iov),
412 &out, &in);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300413 /* On error, stop handling until the next kick. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300414 if (unlikely(head < 0))
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300415 break;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000416 /* Nothing new? Wait for eventfd to tell us they refilled. */
417 if (head == vq->num) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300418 if (unlikely(vhost_enable_notify(&net->dev, vq))) {
419 vhost_disable_notify(&net->dev, vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000420 continue;
421 }
422 break;
423 }
424 if (in) {
425 vq_err(vq, "Unexpected descriptor format for TX: "
426 "out %d, int %d\n", out, in);
427 break;
428 }
429 /* Skip header. TODO: support TSO. */
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000430 len = iov_length(vq->iov, out);
Al Viroc0371da2014-11-24 10:42:55 -0500431 iov_iter_init(&msg.msg_iter, WRITE, vq->iov, out, len);
Al Viro98a527a2014-12-10 15:00:58 -0500432 iov_iter_advance(&msg.msg_iter, hdr_size);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000433 /* Sanity check */
Al Viro01e97e62014-12-15 21:39:31 -0500434 if (!msg_data_left(&msg)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000435 vq_err(vq, "Unexpected header len for TX: "
436 "%zd expected %zd\n",
Al Viro98a527a2014-12-10 15:00:58 -0500437 len, hdr_size);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000438 break;
439 }
Al Viro01e97e62014-12-15 21:39:31 -0500440 len = msg_data_left(&msg);
Jason Wangce21a022013-09-02 16:40:59 +0800441
442 zcopy_used = zcopy && len >= VHOST_GOODCOPY_LEN
443 && (nvq->upend_idx + 1) % UIO_MAXIOV !=
444 nvq->done_idx
445 && vhost_net_tx_select_zcopy(net);
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200446
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000447 /* use msg_control to pass vhost zerocopy ubuf info to skb */
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200448 if (zcopy_used) {
Jason Wangce21a022013-09-02 16:40:59 +0800449 struct ubuf_info *ubuf;
450 ubuf = nvq->ubuf_info + nvq->upend_idx;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000451
Michael S. Tsirkin8b386942014-10-24 14:19:48 +0300452 vq->heads[nvq->upend_idx].id = cpu_to_vhost32(vq, head);
Jason Wangce21a022013-09-02 16:40:59 +0800453 vq->heads[nvq->upend_idx].len = VHOST_DMA_IN_PROGRESS;
454 ubuf->callback = vhost_zerocopy_callback;
455 ubuf->ctx = nvq->ubufs;
456 ubuf->desc = nvq->upend_idx;
457 msg.msg_control = ubuf;
458 msg.msg_controllen = sizeof(ubuf);
459 ubufs = nvq->ubufs;
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200460 atomic_inc(&ubufs->refcount);
Asias He28394002013-04-27 15:07:46 +0800461 nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV;
Jason Wangce21a022013-09-02 16:40:59 +0800462 } else {
Jason Wang4364d5f2013-06-05 15:40:46 +0800463 msg.msg_control = NULL;
Jason Wangce21a022013-09-02 16:40:59 +0800464 ubufs = NULL;
465 }
Jason Wang0ed005c2017-01-18 15:02:02 +0800466
467 total_len += len;
468 if (total_len < VHOST_NET_WEIGHT &&
469 !vhost_vq_avail_empty(&net->dev, vq) &&
470 likely(!vhost_exceeds_maxpend(net))) {
471 msg.msg_flags |= MSG_MORE;
472 } else {
473 msg.msg_flags &= ~MSG_MORE;
474 }
475
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000476 /* TODO: Check specific error and bomb out unless ENOBUFS? */
Ying Xue1b784142015-03-02 15:37:48 +0800477 err = sock->ops->sendmsg(sock, &msg, len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000478 if (unlikely(err < 0)) {
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200479 if (zcopy_used) {
Jason Wangce21a022013-09-02 16:40:59 +0800480 vhost_net_ubuf_put(ubufs);
Asias He28394002013-04-27 15:07:46 +0800481 nvq->upend_idx = ((unsigned)nvq->upend_idx - 1)
482 % UIO_MAXIOV;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000483 }
David Stevens8dd014a2010-07-27 18:52:21 +0300484 vhost_discard_vq_desc(vq, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000485 break;
486 }
487 if (err != len)
Michael S. Tsirkin95c0ec62010-06-24 17:10:25 +0300488 pr_debug("Truncated TX packet: "
489 " len %d != %zd\n", err, len);
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200490 if (!zcopy_used)
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000491 vhost_add_used_and_signal(&net->dev, vq, head, 0);
Jason Wangc8fb2172012-05-02 11:42:41 +0800492 else
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000493 vhost_zerocopy_signal_used(net, vq);
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000494 vhost_net_tx_packet(net);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000495 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
496 vhost_poll_queue(&vq->poll);
497 break;
498 }
499 }
Asias He2e26af72013-05-07 14:54:33 +0800500out:
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000501 mutex_unlock(&vq->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000502}
503
David Stevens8dd014a2010-07-27 18:52:21 +0300504static int peek_head_len(struct sock *sk)
505{
Jason Wang1576d982016-06-30 14:45:36 +0800506 struct socket *sock = sk->sk_socket;
David Stevens8dd014a2010-07-27 18:52:21 +0300507 struct sk_buff *head;
508 int len = 0;
Jason Wang783e3982011-01-17 16:11:17 +0800509 unsigned long flags;
David Stevens8dd014a2010-07-27 18:52:21 +0300510
Jason Wang1576d982016-06-30 14:45:36 +0800511 if (sock->ops->peek_len)
512 return sock->ops->peek_len(sock);
513
Jason Wang783e3982011-01-17 16:11:17 +0800514 spin_lock_irqsave(&sk->sk_receive_queue.lock, flags);
David Stevens8dd014a2010-07-27 18:52:21 +0300515 head = skb_peek(&sk->sk_receive_queue);
Basil Gorc53cff5e2012-05-03 22:55:23 +0000516 if (likely(head)) {
David Stevens8dd014a2010-07-27 18:52:21 +0300517 len = head->len;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100518 if (skb_vlan_tag_present(head))
Basil Gorc53cff5e2012-05-03 22:55:23 +0000519 len += VLAN_HLEN;
520 }
521
Jason Wang783e3982011-01-17 16:11:17 +0800522 spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags);
David Stevens8dd014a2010-07-27 18:52:21 +0300523 return len;
524}
525
Jason Wang1576d982016-06-30 14:45:36 +0800526static int sk_has_rx_data(struct sock *sk)
527{
528 struct socket *sock = sk->sk_socket;
529
530 if (sock->ops->peek_len)
531 return sock->ops->peek_len(sock);
532
533 return skb_queue_empty(&sk->sk_receive_queue);
534}
535
Jason Wang03088132016-03-04 06:24:53 -0500536static int vhost_net_rx_peek_head_len(struct vhost_net *net, struct sock *sk)
537{
538 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
539 struct vhost_virtqueue *vq = &nvq->vq;
540 unsigned long uninitialized_var(endtime);
541 int len = peek_head_len(sk);
542
543 if (!len && vq->busyloop_timeout) {
544 /* Both tx vq and rx socket were polled here */
545 mutex_lock(&vq->mutex);
546 vhost_disable_notify(&net->dev, vq);
547
548 preempt_disable();
549 endtime = busy_clock() + vq->busyloop_timeout;
550
551 while (vhost_can_busy_poll(&net->dev, endtime) &&
Jason Wang1576d982016-06-30 14:45:36 +0800552 !sk_has_rx_data(sk) &&
Jason Wang03088132016-03-04 06:24:53 -0500553 vhost_vq_avail_empty(&net->dev, vq))
Christian Borntraegerf2f09a42016-10-25 11:03:14 +0200554 cpu_relax();
Jason Wang03088132016-03-04 06:24:53 -0500555
556 preempt_enable();
557
558 if (vhost_enable_notify(&net->dev, vq))
559 vhost_poll_queue(&vq->poll);
560 mutex_unlock(&vq->mutex);
561
562 len = peek_head_len(sk);
563 }
564
565 return len;
566}
567
David Stevens8dd014a2010-07-27 18:52:21 +0300568/* This is a multi-buffer version of vhost_get_desc, that works if
569 * vq has read descriptors only.
570 * @vq - the relevant virtqueue
571 * @datalen - data length we'll be reading
572 * @iovcount - returned count of io vectors we fill
573 * @log - vhost log
574 * @log_num - log offset
Jason Wang94249362011-01-17 16:11:08 +0800575 * @quota - headcount quota, 1 for big buffer
David Stevens8dd014a2010-07-27 18:52:21 +0300576 * returns number of buffer heads allocated, negative on error
577 */
578static int get_rx_bufs(struct vhost_virtqueue *vq,
579 struct vring_used_elem *heads,
580 int datalen,
581 unsigned *iovcount,
582 struct vhost_log *log,
Jason Wang94249362011-01-17 16:11:08 +0800583 unsigned *log_num,
584 unsigned int quota)
David Stevens8dd014a2010-07-27 18:52:21 +0300585{
586 unsigned int out, in;
587 int seg = 0;
588 int headcount = 0;
589 unsigned d;
590 int r, nlogs = 0;
Michael S. Tsirkin8b386942014-10-24 14:19:48 +0300591 /* len is always initialized before use since we are always called with
592 * datalen > 0.
593 */
594 u32 uninitialized_var(len);
David Stevens8dd014a2010-07-27 18:52:21 +0300595
Jason Wang94249362011-01-17 16:11:08 +0800596 while (datalen > 0 && headcount < quota) {
Jason Wange0e9b402010-09-14 23:53:05 +0800597 if (unlikely(seg >= UIO_MAXIOV)) {
David Stevens8dd014a2010-07-27 18:52:21 +0300598 r = -ENOBUFS;
599 goto err;
600 }
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300601 r = vhost_get_vq_desc(vq, vq->iov + seg,
David Stevens8dd014a2010-07-27 18:52:21 +0300602 ARRAY_SIZE(vq->iov) - seg, &out,
603 &in, log, log_num);
Michael S. Tsirkina39ee442014-03-27 12:53:37 +0200604 if (unlikely(r < 0))
605 goto err;
606
607 d = r;
David Stevens8dd014a2010-07-27 18:52:21 +0300608 if (d == vq->num) {
609 r = 0;
610 goto err;
611 }
612 if (unlikely(out || in <= 0)) {
613 vq_err(vq, "unexpected descriptor format for RX: "
614 "out %d, in %d\n", out, in);
615 r = -EINVAL;
616 goto err;
617 }
618 if (unlikely(log)) {
619 nlogs += *log_num;
620 log += *log_num;
621 }
Michael S. Tsirkin8b386942014-10-24 14:19:48 +0300622 heads[headcount].id = cpu_to_vhost32(vq, d);
623 len = iov_length(vq->iov + seg, in);
624 heads[headcount].len = cpu_to_vhost32(vq, len);
625 datalen -= len;
David Stevens8dd014a2010-07-27 18:52:21 +0300626 ++headcount;
627 seg += in;
628 }
Michael S. Tsirkin99975cc2015-01-07 10:51:00 +0200629 heads[headcount - 1].len = cpu_to_vhost32(vq, len + datalen);
David Stevens8dd014a2010-07-27 18:52:21 +0300630 *iovcount = seg;
631 if (unlikely(log))
632 *log_num = nlogs;
Michael S. Tsirkind8316f32014-03-27 12:00:26 +0200633
634 /* Detect overrun */
635 if (unlikely(datalen > 0)) {
636 r = UIO_MAXIOV + 1;
637 goto err;
638 }
David Stevens8dd014a2010-07-27 18:52:21 +0300639 return headcount;
640err:
641 vhost_discard_vq_desc(vq, headcount);
642 return r;
643}
644
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000645/* Expects to be always run from workqueue - which acts as
646 * read-size critical section for our kind of RCU. */
Jason Wang94249362011-01-17 16:11:08 +0800647static void handle_rx(struct vhost_net *net)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000648{
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300649 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX];
650 struct vhost_virtqueue *vq = &nvq->vq;
David Stevens8dd014a2010-07-27 18:52:21 +0300651 unsigned uninitialized_var(in), log;
652 struct vhost_log *vq_log;
653 struct msghdr msg = {
654 .msg_name = NULL,
655 .msg_namelen = 0,
656 .msg_control = NULL, /* FIXME: get and handle RX aux data. */
657 .msg_controllen = 0,
David Stevens8dd014a2010-07-27 18:52:21 +0300658 .msg_flags = MSG_DONTWAIT,
659 };
Jason Wang0960b642015-02-15 16:35:17 +0800660 struct virtio_net_hdr hdr = {
661 .flags = 0,
662 .gso_type = VIRTIO_NET_HDR_GSO_NONE
David Stevens8dd014a2010-07-27 18:52:21 +0300663 };
David Stevens8dd014a2010-07-27 18:52:21 +0300664 size_t total_len = 0;
Michael S. Tsirkin910a5782012-10-24 20:37:51 +0200665 int err, mergeable;
666 s16 headcount;
David Stevens8dd014a2010-07-27 18:52:21 +0300667 size_t vhost_hlen, sock_hlen;
668 size_t vhost_len, sock_len;
Asias He2e26af72013-05-07 14:54:33 +0800669 struct socket *sock;
Al Viroba7438a2014-12-10 15:51:28 -0500670 struct iov_iter fixup;
Jason Wang0960b642015-02-15 16:35:17 +0800671 __virtio16 num_buffers;
David Stevens8dd014a2010-07-27 18:52:21 +0300672
David Stevens8dd014a2010-07-27 18:52:21 +0300673 mutex_lock(&vq->mutex);
Asias He2e26af72013-05-07 14:54:33 +0800674 sock = vq->private_data;
675 if (!sock)
676 goto out;
Jason Wang6b1e6cc2016-06-23 02:04:32 -0400677
678 if (!vq_iotlb_prefetch(vq))
679 goto out;
680
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300681 vhost_disable_notify(&net->dev, vq);
Jason Wang8241a1e2016-06-01 01:56:33 -0400682 vhost_net_disable_vq(net, vq);
Asias He2e26af72013-05-07 14:54:33 +0800683
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300684 vhost_hlen = nvq->vhost_hlen;
685 sock_hlen = nvq->sock_hlen;
David Stevens8dd014a2010-07-27 18:52:21 +0300686
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300687 vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
David Stevens8dd014a2010-07-27 18:52:21 +0300688 vq->log : NULL;
Michael S. Tsirkinea16c512014-06-05 15:20:23 +0300689 mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF);
David Stevens8dd014a2010-07-27 18:52:21 +0300690
Jason Wang03088132016-03-04 06:24:53 -0500691 while ((sock_len = vhost_net_rx_peek_head_len(net, sock->sk))) {
David Stevens8dd014a2010-07-27 18:52:21 +0300692 sock_len += sock_hlen;
693 vhost_len = sock_len + vhost_hlen;
694 headcount = get_rx_bufs(vq, vq->heads, vhost_len,
Jason Wang94249362011-01-17 16:11:08 +0800695 &in, vq_log, &log,
696 likely(mergeable) ? UIO_MAXIOV : 1);
David Stevens8dd014a2010-07-27 18:52:21 +0300697 /* On error, stop handling until the next kick. */
698 if (unlikely(headcount < 0))
Jason Wang8241a1e2016-06-01 01:56:33 -0400699 goto out;
Michael S. Tsirkind8316f32014-03-27 12:00:26 +0200700 /* On overrun, truncate and discard */
701 if (unlikely(headcount > UIO_MAXIOV)) {
Al Viroc0371da2014-11-24 10:42:55 -0500702 iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
Ying Xue1b784142015-03-02 15:37:48 +0800703 err = sock->ops->recvmsg(sock, &msg,
Michael S. Tsirkind8316f32014-03-27 12:00:26 +0200704 1, MSG_DONTWAIT | MSG_TRUNC);
705 pr_debug("Discarded rx packet: len %zd\n", sock_len);
706 continue;
707 }
David Stevens8dd014a2010-07-27 18:52:21 +0300708 /* OK, now we need to know about added descriptors. */
709 if (!headcount) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300710 if (unlikely(vhost_enable_notify(&net->dev, vq))) {
David Stevens8dd014a2010-07-27 18:52:21 +0300711 /* They have slipped one in as we were
712 * doing that: check again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300713 vhost_disable_notify(&net->dev, vq);
David Stevens8dd014a2010-07-27 18:52:21 +0300714 continue;
715 }
716 /* Nothing new? Wait for eventfd to tell us
717 * they refilled. */
Jason Wang8241a1e2016-06-01 01:56:33 -0400718 goto out;
David Stevens8dd014a2010-07-27 18:52:21 +0300719 }
720 /* We don't need to be notified again. */
Al Viroba7438a2014-12-10 15:51:28 -0500721 iov_iter_init(&msg.msg_iter, READ, vq->iov, in, vhost_len);
722 fixup = msg.msg_iter;
723 if (unlikely((vhost_hlen))) {
724 /* We will supply the header ourselves
725 * TODO: support TSO.
726 */
727 iov_iter_advance(&msg.msg_iter, vhost_hlen);
Al Viroba7438a2014-12-10 15:51:28 -0500728 }
Ying Xue1b784142015-03-02 15:37:48 +0800729 err = sock->ops->recvmsg(sock, &msg,
David Stevens8dd014a2010-07-27 18:52:21 +0300730 sock_len, MSG_DONTWAIT | MSG_TRUNC);
731 /* Userspace might have consumed the packet meanwhile:
732 * it's not supposed to do this usually, but might be hard
733 * to prevent. Discard data we got (if any) and keep going. */
734 if (unlikely(err != sock_len)) {
735 pr_debug("Discarded rx packet: "
736 " len %d, expected %zd\n", err, sock_len);
737 vhost_discard_vq_desc(vq, headcount);
738 continue;
739 }
Al Viroba7438a2014-12-10 15:51:28 -0500740 /* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */
Michael S. Tsirkin4c5a8442015-02-25 15:19:28 +0100741 if (unlikely(vhost_hlen)) {
742 if (copy_to_iter(&hdr, sizeof(hdr),
743 &fixup) != sizeof(hdr)) {
744 vq_err(vq, "Unable to write vnet_hdr "
745 "at addr %p\n", vq->iov->iov_base);
Jason Wang8241a1e2016-06-01 01:56:33 -0400746 goto out;
Michael S. Tsirkin4c5a8442015-02-25 15:19:28 +0100747 }
748 } else {
749 /* Header came from socket; we'll need to patch
750 * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF
751 */
752 iov_iter_advance(&fixup, sizeof(hdr));
David Stevens8dd014a2010-07-27 18:52:21 +0300753 }
754 /* TODO: Should check and handle checksum. */
Michael S. Tsirkin5201aa42015-02-03 11:07:06 +0200755
Jason Wang0960b642015-02-15 16:35:17 +0800756 num_buffers = cpu_to_vhost16(vq, headcount);
Jason Wangcfbdab92011-01-17 16:10:59 +0800757 if (likely(mergeable) &&
Michael S. Tsirkin0d79a492015-02-25 15:20:01 +0100758 copy_to_iter(&num_buffers, sizeof num_buffers,
759 &fixup) != sizeof num_buffers) {
David Stevens8dd014a2010-07-27 18:52:21 +0300760 vq_err(vq, "Failed num_buffers write");
761 vhost_discard_vq_desc(vq, headcount);
Jason Wang8241a1e2016-06-01 01:56:33 -0400762 goto out;
David Stevens8dd014a2010-07-27 18:52:21 +0300763 }
764 vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
765 headcount);
766 if (unlikely(vq_log))
767 vhost_log_write(vq, vq_log, log, vhost_len);
768 total_len += vhost_len;
769 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
770 vhost_poll_queue(&vq->poll);
Jason Wang8241a1e2016-06-01 01:56:33 -0400771 goto out;
David Stevens8dd014a2010-07-27 18:52:21 +0300772 }
773 }
Jason Wang8241a1e2016-06-01 01:56:33 -0400774 vhost_net_enable_vq(net, vq);
Asias He2e26af72013-05-07 14:54:33 +0800775out:
David Stevens8dd014a2010-07-27 18:52:21 +0300776 mutex_unlock(&vq->mutex);
David Stevens8dd014a2010-07-27 18:52:21 +0300777}
778
Tejun Heoc23f34452010-06-02 20:40:00 +0200779static void handle_tx_kick(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000780{
Tejun Heoc23f34452010-06-02 20:40:00 +0200781 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
782 poll.work);
783 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
784
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000785 handle_tx(net);
786}
787
Tejun Heoc23f34452010-06-02 20:40:00 +0200788static void handle_rx_kick(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000789{
Tejun Heoc23f34452010-06-02 20:40:00 +0200790 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
791 poll.work);
792 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
793
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000794 handle_rx(net);
795}
796
Tejun Heoc23f34452010-06-02 20:40:00 +0200797static void handle_tx_net(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000798{
Tejun Heoc23f34452010-06-02 20:40:00 +0200799 struct vhost_net *net = container_of(work, struct vhost_net,
800 poll[VHOST_NET_VQ_TX].work);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000801 handle_tx(net);
802}
803
Tejun Heoc23f34452010-06-02 20:40:00 +0200804static void handle_rx_net(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000805{
Tejun Heoc23f34452010-06-02 20:40:00 +0200806 struct vhost_net *net = container_of(work, struct vhost_net,
807 poll[VHOST_NET_VQ_RX].work);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000808 handle_rx(net);
809}
810
811static int vhost_net_open(struct inode *inode, struct file *f)
812{
Michael S. Tsirkin23cc5a92013-01-23 21:46:47 +0100813 struct vhost_net *n;
Tejun Heoc23f34452010-06-02 20:40:00 +0200814 struct vhost_dev *dev;
Asias He3ab2e422013-04-27 11:16:48 +0800815 struct vhost_virtqueue **vqs;
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +0800816 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200817
Michael S. Tsirkin23cc5a92013-01-23 21:46:47 +0100818 n = kmalloc(sizeof *n, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
819 if (!n) {
820 n = vmalloc(sizeof *n);
821 if (!n)
822 return -ENOMEM;
823 }
Asias He3ab2e422013-04-27 11:16:48 +0800824 vqs = kmalloc(VHOST_NET_VQ_MAX * sizeof(*vqs), GFP_KERNEL);
825 if (!vqs) {
Romain Francoised04257b2014-06-12 10:42:34 +0200826 kvfree(n);
Asias He3ab2e422013-04-27 11:16:48 +0800827 return -ENOMEM;
828 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200829
830 dev = &n->dev;
Asias He3ab2e422013-04-27 11:16:48 +0800831 vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq;
832 vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq;
833 n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick;
834 n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick;
Asias He28394002013-04-27 15:07:46 +0800835 for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
836 n->vqs[i].ubufs = NULL;
837 n->vqs[i].ubuf_info = NULL;
838 n->vqs[i].upend_idx = 0;
839 n->vqs[i].done_idx = 0;
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300840 n->vqs[i].vhost_hlen = 0;
841 n->vqs[i].sock_hlen = 0;
Asias He28394002013-04-27 15:07:46 +0800842 }
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +0800843 vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000844
Tejun Heoc23f34452010-06-02 20:40:00 +0200845 vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
846 vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000847
848 f->private_data = n;
849
850 return 0;
851}
852
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000853static struct socket *vhost_net_stop_vq(struct vhost_net *n,
854 struct vhost_virtqueue *vq)
855{
856 struct socket *sock;
857
858 mutex_lock(&vq->mutex);
Asias He22fa90c2013-05-07 14:54:36 +0800859 sock = vq->private_data;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000860 vhost_net_disable_vq(n, vq);
Asias He22fa90c2013-05-07 14:54:36 +0800861 vq->private_data = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000862 mutex_unlock(&vq->mutex);
863 return sock;
864}
865
866static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
867 struct socket **rx_sock)
868{
Asias He3ab2e422013-04-27 11:16:48 +0800869 *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq);
870 *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000871}
872
873static void vhost_net_flush_vq(struct vhost_net *n, int index)
874{
875 vhost_poll_flush(n->poll + index);
Asias He3ab2e422013-04-27 11:16:48 +0800876 vhost_poll_flush(&n->vqs[index].vq.poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000877}
878
879static void vhost_net_flush(struct vhost_net *n)
880{
881 vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
882 vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
Asias He28394002013-04-27 15:07:46 +0800883 if (n->vqs[VHOST_NET_VQ_TX].ubufs) {
Asias He3ab2e422013-04-27 11:16:48 +0800884 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200885 n->tx_flush = true;
Asias He3ab2e422013-04-27 11:16:48 +0800886 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200887 /* Wait for all lower device DMAs done. */
Asias Hefe729a52013-05-06 16:38:24 +0800888 vhost_net_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs);
Asias He3ab2e422013-04-27 11:16:48 +0800889 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200890 n->tx_flush = false;
Michael S. Tsirkin0ad8b482014-02-13 11:42:05 +0200891 atomic_set(&n->vqs[VHOST_NET_VQ_TX].ubufs->refcount, 1);
Asias He3ab2e422013-04-27 11:16:48 +0800892 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200893 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000894}
895
896static int vhost_net_release(struct inode *inode, struct file *f)
897{
898 struct vhost_net *n = f->private_data;
899 struct socket *tx_sock;
900 struct socket *rx_sock;
901
902 vhost_net_stop(n, &tx_sock, &rx_sock);
903 vhost_net_flush(n);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000904 vhost_dev_stop(&n->dev);
Michael S. Tsirkinea5d4042011-11-27 19:05:58 +0200905 vhost_dev_cleanup(&n->dev, false);
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300906 vhost_net_vq_reset(n);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000907 if (tx_sock)
Al Viro09aaacf2014-03-05 20:39:00 -0500908 sockfd_put(tx_sock);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000909 if (rx_sock)
Al Viro09aaacf2014-03-05 20:39:00 -0500910 sockfd_put(rx_sock);
Michael S. Tsirkinb0c057c2014-02-13 11:45:11 +0200911 /* Make sure no callbacks are outstanding */
912 synchronize_rcu_bh();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000913 /* We do an extra flush before freeing memory,
914 * since jobs can re-queue themselves. */
915 vhost_net_flush(n);
Asias He3ab2e422013-04-27 11:16:48 +0800916 kfree(n->dev.vqs);
Romain Francoised04257b2014-06-12 10:42:34 +0200917 kvfree(n);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000918 return 0;
919}
920
921static struct socket *get_raw_socket(int fd)
922{
923 struct {
924 struct sockaddr_ll sa;
925 char buf[MAX_ADDR_LEN];
926 } uaddr;
927 int uaddr_len = sizeof uaddr, r;
928 struct socket *sock = sockfd_lookup(fd, &r);
Krishna Kumard47effe2011-03-01 17:06:37 +0530929
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000930 if (!sock)
931 return ERR_PTR(-ENOTSOCK);
932
933 /* Parameter checking */
934 if (sock->sk->sk_type != SOCK_RAW) {
935 r = -ESOCKTNOSUPPORT;
936 goto err;
937 }
938
939 r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa,
940 &uaddr_len, 0);
941 if (r)
942 goto err;
943
944 if (uaddr.sa.sll_family != AF_PACKET) {
945 r = -EPFNOSUPPORT;
946 goto err;
947 }
948 return sock;
949err:
Al Viro09aaacf2014-03-05 20:39:00 -0500950 sockfd_put(sock);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000951 return ERR_PTR(r);
952}
953
Arnd Bergmann501c7742010-02-18 05:46:50 +0000954static struct socket *get_tap_socket(int fd)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000955{
956 struct file *file = fget(fd);
957 struct socket *sock;
Krishna Kumard47effe2011-03-01 17:06:37 +0530958
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000959 if (!file)
960 return ERR_PTR(-EBADF);
961 sock = tun_get_socket(file);
Arnd Bergmann501c7742010-02-18 05:46:50 +0000962 if (!IS_ERR(sock))
963 return sock;
Sainath Grandhi635b8c82017-02-10 16:03:47 -0800964 sock = tap_get_socket(file);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000965 if (IS_ERR(sock))
966 fput(file);
967 return sock;
968}
969
970static struct socket *get_socket(int fd)
971{
972 struct socket *sock;
Krishna Kumard47effe2011-03-01 17:06:37 +0530973
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000974 /* special case to disable backend */
975 if (fd == -1)
976 return NULL;
977 sock = get_raw_socket(fd);
978 if (!IS_ERR(sock))
979 return sock;
Arnd Bergmann501c7742010-02-18 05:46:50 +0000980 sock = get_tap_socket(fd);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000981 if (!IS_ERR(sock))
982 return sock;
983 return ERR_PTR(-ENOTSOCK);
984}
985
986static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
987{
988 struct socket *sock, *oldsock;
989 struct vhost_virtqueue *vq;
Asias He28394002013-04-27 15:07:46 +0800990 struct vhost_net_virtqueue *nvq;
Asias Hefe729a52013-05-06 16:38:24 +0800991 struct vhost_net_ubuf_ref *ubufs, *oldubufs = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000992 int r;
993
994 mutex_lock(&n->dev.mutex);
995 r = vhost_dev_check_owner(&n->dev);
996 if (r)
997 goto err;
998
999 if (index >= VHOST_NET_VQ_MAX) {
1000 r = -ENOBUFS;
1001 goto err;
1002 }
Asias He3ab2e422013-04-27 11:16:48 +08001003 vq = &n->vqs[index].vq;
Asias He28394002013-04-27 15:07:46 +08001004 nvq = &n->vqs[index];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001005 mutex_lock(&vq->mutex);
1006
1007 /* Verify that ring has been setup correctly. */
1008 if (!vhost_vq_access_ok(vq)) {
1009 r = -EFAULT;
Jeff Dike1dace8c2010-03-04 16:10:14 -05001010 goto err_vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001011 }
1012 sock = get_socket(fd);
1013 if (IS_ERR(sock)) {
1014 r = PTR_ERR(sock);
Jeff Dike1dace8c2010-03-04 16:10:14 -05001015 goto err_vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001016 }
1017
1018 /* start polling new socket */
Asias He22fa90c2013-05-07 14:54:36 +08001019 oldsock = vq->private_data;
David S. Miller11fe8832010-07-20 18:25:24 -07001020 if (sock != oldsock) {
Asias Hefe729a52013-05-06 16:38:24 +08001021 ubufs = vhost_net_ubuf_alloc(vq,
1022 sock && vhost_sock_zcopy(sock));
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +00001023 if (IS_ERR(ubufs)) {
1024 r = PTR_ERR(ubufs);
1025 goto err_ubufs;
1026 }
Jason Wang692a9982013-01-28 01:05:17 +00001027
Krishna Kumard47effe2011-03-01 17:06:37 +05301028 vhost_net_disable_vq(n, vq);
Asias He22fa90c2013-05-07 14:54:36 +08001029 vq->private_data = sock;
Greg Kurz80f7d032016-02-16 15:59:44 +01001030 r = vhost_vq_init_access(vq);
Jason Wangf59281d2011-06-21 18:04:27 +08001031 if (r)
Jason Wang692a9982013-01-28 01:05:17 +00001032 goto err_used;
Jason Wang2b8b3282013-01-28 01:05:18 +00001033 r = vhost_net_enable_vq(n, vq);
1034 if (r)
1035 goto err_used;
Jason Wang692a9982013-01-28 01:05:17 +00001036
Asias He28394002013-04-27 15:07:46 +08001037 oldubufs = nvq->ubufs;
1038 nvq->ubufs = ubufs;
Michael S. Tsirkin64e9a9b2012-12-03 07:31:51 +00001039
1040 n->tx_packets = 0;
1041 n->tx_zcopy_err = 0;
Michael S. Tsirkin1280c272012-12-04 00:17:14 +02001042 n->tx_flush = false;
Jeff Dikedd1f4072010-03-04 16:10:14 -05001043 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001044
Michael S. Tsirkin1680e902010-07-15 15:19:12 +03001045 mutex_unlock(&vq->mutex);
1046
Michael S. Tsirkinc047e5f2011-07-20 13:41:31 +03001047 if (oldubufs) {
Michael S. Tsirkinc38e39c2013-06-25 17:29:46 +03001048 vhost_net_ubuf_put_wait_and_free(oldubufs);
Michael S. Tsirkinc047e5f2011-07-20 13:41:31 +03001049 mutex_lock(&vq->mutex);
Michael S. Tsirkineaae8132012-11-01 09:16:51 +00001050 vhost_zerocopy_signal_used(n, vq);
Michael S. Tsirkinc047e5f2011-07-20 13:41:31 +03001051 mutex_unlock(&vq->mutex);
1052 }
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +00001053
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001054 if (oldsock) {
1055 vhost_net_flush_vq(n, index);
Al Viro09aaacf2014-03-05 20:39:00 -05001056 sockfd_put(oldsock);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001057 }
Jeff Dike1dace8c2010-03-04 16:10:14 -05001058
Michael S. Tsirkin1680e902010-07-15 15:19:12 +03001059 mutex_unlock(&n->dev.mutex);
1060 return 0;
1061
Jason Wang692a9982013-01-28 01:05:17 +00001062err_used:
Asias He22fa90c2013-05-07 14:54:36 +08001063 vq->private_data = oldsock;
Jason Wang692a9982013-01-28 01:05:17 +00001064 vhost_net_enable_vq(n, vq);
1065 if (ubufs)
Michael S. Tsirkinc38e39c2013-06-25 17:29:46 +03001066 vhost_net_ubuf_put_wait_and_free(ubufs);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +00001067err_ubufs:
Al Viro09aaacf2014-03-05 20:39:00 -05001068 sockfd_put(sock);
Jeff Dike1dace8c2010-03-04 16:10:14 -05001069err_vq:
1070 mutex_unlock(&vq->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001071err:
1072 mutex_unlock(&n->dev.mutex);
1073 return r;
1074}
1075
1076static long vhost_net_reset_owner(struct vhost_net *n)
1077{
1078 struct socket *tx_sock = NULL;
1079 struct socket *rx_sock = NULL;
1080 long err;
Jason Wanga9709d62016-06-23 02:04:31 -04001081 struct vhost_umem *umem;
Krishna Kumard47effe2011-03-01 17:06:37 +05301082
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001083 mutex_lock(&n->dev.mutex);
1084 err = vhost_dev_check_owner(&n->dev);
1085 if (err)
1086 goto done;
Jason Wanga9709d62016-06-23 02:04:31 -04001087 umem = vhost_dev_reset_owner_prepare();
1088 if (!umem) {
Michael S. Tsirkin150b9e52013-04-28 17:12:08 +03001089 err = -ENOMEM;
1090 goto done;
1091 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001092 vhost_net_stop(n, &tx_sock, &rx_sock);
1093 vhost_net_flush(n);
Jason Wanga9709d62016-06-23 02:04:31 -04001094 vhost_dev_reset_owner(&n->dev, umem);
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +03001095 vhost_net_vq_reset(n);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001096done:
1097 mutex_unlock(&n->dev.mutex);
1098 if (tx_sock)
Al Viro09aaacf2014-03-05 20:39:00 -05001099 sockfd_put(tx_sock);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001100 if (rx_sock)
Al Viro09aaacf2014-03-05 20:39:00 -05001101 sockfd_put(rx_sock);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001102 return err;
1103}
1104
1105static int vhost_net_set_features(struct vhost_net *n, u64 features)
1106{
David Stevens8dd014a2010-07-27 18:52:21 +03001107 size_t vhost_hlen, sock_hlen, hdr_len;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001108 int i;
David Stevens8dd014a2010-07-27 18:52:21 +03001109
Michael S. Tsirkine4fca7d2014-10-24 14:23:52 +03001110 hdr_len = (features & ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
1111 (1ULL << VIRTIO_F_VERSION_1))) ?
David Stevens8dd014a2010-07-27 18:52:21 +03001112 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
1113 sizeof(struct virtio_net_hdr);
1114 if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
1115 /* vhost provides vnet_hdr */
1116 vhost_hlen = hdr_len;
1117 sock_hlen = 0;
1118 } else {
1119 /* socket provides vnet_hdr */
1120 vhost_hlen = 0;
1121 sock_hlen = hdr_len;
1122 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001123 mutex_lock(&n->dev.mutex);
1124 if ((features & (1 << VHOST_F_LOG_ALL)) &&
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001125 !vhost_log_access_ok(&n->dev))
1126 goto out_unlock;
1127
1128 if ((features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))) {
1129 if (vhost_init_device_iotlb(&n->dev, true))
1130 goto out_unlock;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001131 }
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001132
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001133 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +08001134 mutex_lock(&n->vqs[i].vq.mutex);
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001135 n->vqs[i].vq.acked_features = features;
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +03001136 n->vqs[i].vhost_hlen = vhost_hlen;
1137 n->vqs[i].sock_hlen = sock_hlen;
Asias He3ab2e422013-04-27 11:16:48 +08001138 mutex_unlock(&n->vqs[i].vq.mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001139 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001140 mutex_unlock(&n->dev.mutex);
1141 return 0;
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001142
1143out_unlock:
1144 mutex_unlock(&n->dev.mutex);
1145 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001146}
1147
Asias Heb1ad8492013-05-06 11:16:00 +08001148static long vhost_net_set_owner(struct vhost_net *n)
1149{
1150 int r;
1151
1152 mutex_lock(&n->dev.mutex);
Michael S. Tsirkin05c05352013-06-06 15:20:39 +03001153 if (vhost_dev_has_owner(&n->dev)) {
1154 r = -EBUSY;
1155 goto out;
1156 }
Asias Heb1ad8492013-05-06 11:16:00 +08001157 r = vhost_net_set_ubuf_info(n);
1158 if (r)
1159 goto out;
1160 r = vhost_dev_set_owner(&n->dev);
1161 if (r)
1162 vhost_net_clear_ubuf_info(n);
1163 vhost_net_flush(n);
1164out:
1165 mutex_unlock(&n->dev.mutex);
1166 return r;
1167}
1168
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001169static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
1170 unsigned long arg)
1171{
1172 struct vhost_net *n = f->private_data;
1173 void __user *argp = (void __user *)arg;
1174 u64 __user *featurep = argp;
1175 struct vhost_vring_file backend;
1176 u64 features;
1177 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301178
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001179 switch (ioctl) {
1180 case VHOST_NET_SET_BACKEND:
Takuya Yoshikawad3553a52010-05-27 19:01:58 +09001181 if (copy_from_user(&backend, argp, sizeof backend))
1182 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001183 return vhost_net_set_backend(n, backend.index, backend.fd);
1184 case VHOST_GET_FEATURES:
Stefan Hajnoczi0dd05a32012-07-21 06:55:36 +00001185 features = VHOST_NET_FEATURES;
Takuya Yoshikawad3553a52010-05-27 19:01:58 +09001186 if (copy_to_user(featurep, &features, sizeof features))
1187 return -EFAULT;
1188 return 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001189 case VHOST_SET_FEATURES:
Takuya Yoshikawad3553a52010-05-27 19:01:58 +09001190 if (copy_from_user(&features, featurep, sizeof features))
1191 return -EFAULT;
Stefan Hajnoczi0dd05a32012-07-21 06:55:36 +00001192 if (features & ~VHOST_NET_FEATURES)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001193 return -EOPNOTSUPP;
1194 return vhost_net_set_features(n, features);
1195 case VHOST_RESET_OWNER:
1196 return vhost_net_reset_owner(n);
Asias Heb1ad8492013-05-06 11:16:00 +08001197 case VHOST_SET_OWNER:
1198 return vhost_net_set_owner(n);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001199 default:
1200 mutex_lock(&n->dev.mutex);
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001201 r = vhost_dev_ioctl(&n->dev, ioctl, argp);
1202 if (r == -ENOIOCTLCMD)
1203 r = vhost_vring_ioctl(&n->dev, ioctl, argp);
1204 else
1205 vhost_net_flush(n);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001206 mutex_unlock(&n->dev.mutex);
1207 return r;
1208 }
1209}
1210
1211#ifdef CONFIG_COMPAT
1212static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
1213 unsigned long arg)
1214{
1215 return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1216}
1217#endif
1218
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001219static ssize_t vhost_net_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
1220{
1221 struct file *file = iocb->ki_filp;
1222 struct vhost_net *n = file->private_data;
1223 struct vhost_dev *dev = &n->dev;
1224 int noblock = file->f_flags & O_NONBLOCK;
1225
1226 return vhost_chr_read_iter(dev, to, noblock);
1227}
1228
1229static ssize_t vhost_net_chr_write_iter(struct kiocb *iocb,
1230 struct iov_iter *from)
1231{
1232 struct file *file = iocb->ki_filp;
1233 struct vhost_net *n = file->private_data;
1234 struct vhost_dev *dev = &n->dev;
1235
1236 return vhost_chr_write_iter(dev, from);
1237}
1238
1239static unsigned int vhost_net_chr_poll(struct file *file, poll_table *wait)
1240{
1241 struct vhost_net *n = file->private_data;
1242 struct vhost_dev *dev = &n->dev;
1243
1244 return vhost_chr_poll(file, dev, wait);
1245}
1246
Tobias Klauser373a83a2010-05-17 15:12:49 +02001247static const struct file_operations vhost_net_fops = {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001248 .owner = THIS_MODULE,
1249 .release = vhost_net_release,
Jason Wang6b1e6cc2016-06-23 02:04:32 -04001250 .read_iter = vhost_net_chr_read_iter,
1251 .write_iter = vhost_net_chr_write_iter,
1252 .poll = vhost_net_chr_poll,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001253 .unlocked_ioctl = vhost_net_ioctl,
1254#ifdef CONFIG_COMPAT
1255 .compat_ioctl = vhost_net_compat_ioctl,
1256#endif
1257 .open = vhost_net_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001258 .llseek = noop_llseek,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001259};
1260
1261static struct miscdevice vhost_net_misc = {
stephen hemminger7c7c7f02012-01-11 19:30:38 +00001262 .minor = VHOST_NET_MINOR,
1263 .name = "vhost-net",
1264 .fops = &vhost_net_fops,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001265};
1266
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001267static int vhost_net_init(void)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001268{
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +00001269 if (experimental_zcopytx)
Asias Hefe729a52013-05-06 16:38:24 +08001270 vhost_net_enable_zcopy(VHOST_NET_VQ_TX);
Tejun Heoc23f34452010-06-02 20:40:00 +02001271 return misc_register(&vhost_net_misc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001272}
1273module_init(vhost_net_init);
1274
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001275static void vhost_net_exit(void)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001276{
1277 misc_deregister(&vhost_net_misc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001278}
1279module_exit(vhost_net_exit);
1280
1281MODULE_VERSION("0.0.1");
1282MODULE_LICENSE("GPL v2");
1283MODULE_AUTHOR("Michael S. Tsirkin");
1284MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
stephen hemminger7c7c7f02012-01-11 19:30:38 +00001285MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR);
1286MODULE_ALIAS("devname:vhost-net");