blob: e34e195b9cf63d3291aaf2b9b5c9bbdad393b2df [file] [log] [blame]
Michael S. Tsirkin3a4d5c94e2010-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. Tsirkin3a4d5c94e2010-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. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000016#include <linux/mutex.h>
17#include <linux/workqueue.h>
18#include <linux/rcupdate.h>
19#include <linux/file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Michael S. Tsirkin3a4d5c94e2010-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>
Basil Gorc53cff5e2012-05-03 22:55:23 +000027#include <linux/if_vlan.h>
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000028
29#include <net/sock.h>
30
31#include "vhost.h"
32
Michael S. Tsirkinf9611c42012-12-06 14:56:00 +020033static int experimental_zcopytx = 1;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +000034module_param(experimental_zcopytx, int, 0444);
Michael S. Tsirkinf9611c42012-12-06 14:56:00 +020035MODULE_PARM_DESC(experimental_zcopytx, "Enable Zero Copy TX;"
36 " 1 -Enable; 0 - Disable");
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +000037
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000038/* Max number of bytes transferred before requeueing the job.
39 * Using this limit prevents one virtqueue from starving others. */
40#define VHOST_NET_WEIGHT 0x80000
41
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +000042/* MAX number of TX used buffers for outstanding zerocopy */
43#define VHOST_MAX_PEND 128
44#define VHOST_GOODCOPY_LEN 256
45
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000046/*
47 * For transmit, used buffer len is unused; we override it to track buffer
48 * status internally; used for zerocopy tx only.
49 */
50/* Lower device DMA failed */
51#define VHOST_DMA_FAILED_LEN 3
52/* Lower device DMA done */
53#define VHOST_DMA_DONE_LEN 2
54/* Lower device DMA in progress */
55#define VHOST_DMA_IN_PROGRESS 1
56/* Buffer unused */
57#define VHOST_DMA_CLEAR_LEN 0
58
59#define VHOST_DMA_IS_DONE(len) ((len) >= VHOST_DMA_DONE_LEN)
60
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000061enum {
62 VHOST_NET_VQ_RX = 0,
63 VHOST_NET_VQ_TX = 1,
64 VHOST_NET_VQ_MAX = 2,
65};
66
Asias He28394002013-04-27 15:07:46 +080067struct vhost_ubuf_ref {
68 struct kref kref;
69 wait_queue_head_t wait;
70 struct vhost_virtqueue *vq;
71};
72
Asias He3ab2e422013-04-27 11:16:48 +080073struct vhost_net_virtqueue {
74 struct vhost_virtqueue vq;
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +030075 /* hdr is used to store the virtio header.
76 * Since each iovec has >= 1 byte length, we never need more than
77 * header length entries to store the header. */
78 struct iovec hdr[sizeof(struct virtio_net_hdr_mrg_rxbuf)];
79 size_t vhost_hlen;
80 size_t sock_hlen;
Asias He28394002013-04-27 15:07:46 +080081 /* vhost zerocopy support fields below: */
82 /* last used idx for outstanding DMA zerocopy buffers */
83 int upend_idx;
84 /* first used idx for DMA done zerocopy buffers */
85 int done_idx;
86 /* an array of userspace buffers info */
87 struct ubuf_info *ubuf_info;
88 /* Reference counting for outstanding ubufs.
89 * Protected by vq mutex. Writers must also take device mutex. */
90 struct vhost_ubuf_ref *ubufs;
Asias He3ab2e422013-04-27 11:16:48 +080091};
92
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000093struct vhost_net {
94 struct vhost_dev dev;
Asias He3ab2e422013-04-27 11:16:48 +080095 struct vhost_net_virtqueue vqs[VHOST_NET_VQ_MAX];
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +000096 struct vhost_poll poll[VHOST_NET_VQ_MAX];
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000097 /* Number of TX recently submitted.
98 * Protected by tx vq lock. */
99 unsigned tx_packets;
100 /* Number of times zerocopy TX recently failed.
101 * Protected by tx vq lock. */
102 unsigned tx_zcopy_err;
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200103 /* Flush in progress. Protected by tx vq lock. */
104 bool tx_flush;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000105};
106
Asias He28394002013-04-27 15:07:46 +0800107static unsigned vhost_zcopy_mask __read_mostly;
108
109void vhost_enable_zcopy(int vq)
110{
111 vhost_zcopy_mask |= 0x1 << vq;
112}
113
114static void vhost_zerocopy_done_signal(struct kref *kref)
115{
116 struct vhost_ubuf_ref *ubufs = container_of(kref, struct vhost_ubuf_ref,
117 kref);
118 wake_up(&ubufs->wait);
119}
120
121struct vhost_ubuf_ref *vhost_ubuf_alloc(struct vhost_virtqueue *vq,
122 bool zcopy)
123{
124 struct vhost_ubuf_ref *ubufs;
125 /* No zero copy backend? Nothing to count. */
126 if (!zcopy)
127 return NULL;
128 ubufs = kmalloc(sizeof(*ubufs), GFP_KERNEL);
129 if (!ubufs)
130 return ERR_PTR(-ENOMEM);
131 kref_init(&ubufs->kref);
132 init_waitqueue_head(&ubufs->wait);
133 ubufs->vq = vq;
134 return ubufs;
135}
136
137void vhost_ubuf_put(struct vhost_ubuf_ref *ubufs)
138{
139 kref_put(&ubufs->kref, vhost_zerocopy_done_signal);
140}
141
142void vhost_ubuf_put_and_wait(struct vhost_ubuf_ref *ubufs)
143{
144 kref_put(&ubufs->kref, vhost_zerocopy_done_signal);
145 wait_event(ubufs->wait, !atomic_read(&ubufs->kref.refcount));
146 kfree(ubufs);
147}
148
149int vhost_net_set_ubuf_info(struct vhost_net *n)
150{
151 bool zcopy;
152 int i;
153
154 for (i = 0; i < n->dev.nvqs; ++i) {
155 zcopy = vhost_zcopy_mask & (0x1 << i);
156 if (!zcopy)
157 continue;
158 n->vqs[i].ubuf_info = kmalloc(sizeof(*n->vqs[i].ubuf_info) *
159 UIO_MAXIOV, GFP_KERNEL);
160 if (!n->vqs[i].ubuf_info)
161 goto err;
162 }
163 return 0;
164
165err:
166 while (i--) {
167 zcopy = vhost_zcopy_mask & (0x1 << i);
168 if (!zcopy)
169 continue;
170 kfree(n->vqs[i].ubuf_info);
171 }
172 return -ENOMEM;
173}
174
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300175void vhost_net_vq_reset(struct vhost_net *n)
Asias He28394002013-04-27 15:07:46 +0800176{
177 int i;
178
179 for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
180 n->vqs[i].done_idx = 0;
181 n->vqs[i].upend_idx = 0;
182 n->vqs[i].ubufs = NULL;
183 kfree(n->vqs[i].ubuf_info);
184 n->vqs[i].ubuf_info = NULL;
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300185 n->vqs[i].vhost_hlen = 0;
186 n->vqs[i].sock_hlen = 0;
Asias He28394002013-04-27 15:07:46 +0800187 }
188
189}
190
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000191static void vhost_net_tx_packet(struct vhost_net *net)
192{
193 ++net->tx_packets;
194 if (net->tx_packets < 1024)
195 return;
196 net->tx_packets = 0;
197 net->tx_zcopy_err = 0;
198}
199
200static void vhost_net_tx_err(struct vhost_net *net)
201{
202 ++net->tx_zcopy_err;
203}
204
205static bool vhost_net_tx_select_zcopy(struct vhost_net *net)
206{
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200207 /* TX flush waits for outstanding DMAs to be done.
208 * Don't start new DMAs.
209 */
210 return !net->tx_flush &&
211 net->tx_packets / 64 >= net->tx_zcopy_err;
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000212}
213
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000214static bool vhost_sock_zcopy(struct socket *sock)
215{
216 return unlikely(experimental_zcopytx) &&
217 sock_flag(sock->sk, SOCK_ZEROCOPY);
218}
219
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000220/* Pop first len bytes from iovec. Return number of segments used. */
221static int move_iovec_hdr(struct iovec *from, struct iovec *to,
222 size_t len, int iov_count)
223{
224 int seg = 0;
225 size_t size;
Krishna Kumard47effe2011-03-01 17:06:37 +0530226
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000227 while (len && seg < iov_count) {
228 size = min(from->iov_len, len);
229 to->iov_base = from->iov_base;
230 to->iov_len = size;
231 from->iov_len -= size;
232 from->iov_base += size;
233 len -= size;
234 ++from;
235 ++to;
236 ++seg;
237 }
238 return seg;
239}
David Stevens8dd014a2010-07-27 18:52:21 +0300240/* Copy iovec entries for len bytes from iovec. */
241static void copy_iovec_hdr(const struct iovec *from, struct iovec *to,
242 size_t len, int iovcount)
243{
244 int seg = 0;
245 size_t size;
Krishna Kumard47effe2011-03-01 17:06:37 +0530246
David Stevens8dd014a2010-07-27 18:52:21 +0300247 while (len && seg < iovcount) {
248 size = min(from->iov_len, len);
249 to->iov_base = from->iov_base;
250 to->iov_len = size;
251 len -= size;
252 ++from;
253 ++to;
254 ++seg;
255 }
256}
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000257
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000258/* In case of DMA done not in order in lower device driver for some reason.
259 * upend_idx is used to track end of used idx, done_idx is used to track head
260 * of used idx. Once lower device DMA done contiguously, we will signal KVM
261 * guest used idx.
262 */
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000263static int vhost_zerocopy_signal_used(struct vhost_net *net,
264 struct vhost_virtqueue *vq)
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000265{
Asias He28394002013-04-27 15:07:46 +0800266 struct vhost_net_virtqueue *nvq =
267 container_of(vq, struct vhost_net_virtqueue, vq);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000268 int i;
269 int j = 0;
270
Asias He28394002013-04-27 15:07:46 +0800271 for (i = nvq->done_idx; i != nvq->upend_idx; i = (i + 1) % UIO_MAXIOV) {
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000272 if (vq->heads[i].len == VHOST_DMA_FAILED_LEN)
273 vhost_net_tx_err(net);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000274 if (VHOST_DMA_IS_DONE(vq->heads[i].len)) {
275 vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
276 vhost_add_used_and_signal(vq->dev, vq,
277 vq->heads[i].id, 0);
278 ++j;
279 } else
280 break;
281 }
282 if (j)
Asias He28394002013-04-27 15:07:46 +0800283 nvq->done_idx = i;
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000284 return j;
285}
286
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000287static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000288{
289 struct vhost_ubuf_ref *ubufs = ubuf->ctx;
290 struct vhost_virtqueue *vq = ubufs->vq;
Michael S. Tsirkin24eb21a2012-11-01 09:16:55 +0000291 int cnt = atomic_read(&ubufs->kref.refcount);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000292
Michael S. Tsirkin24eb21a2012-11-01 09:16:55 +0000293 /*
294 * Trigger polling thread if guest stopped submitting new buffers:
295 * in this case, the refcount after decrement will eventually reach 1
296 * so here it is 2.
297 * We also trigger polling periodically after each 16 packets
298 * (the value 16 here is more or less arbitrary, it's tuned to trigger
299 * less than 10% of times).
300 */
301 if (cnt <= 2 || !(cnt % 16))
302 vhost_poll_queue(&vq->poll);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000303 /* set len to mark this desc buffers done DMA */
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000304 vq->heads[ubuf->desc].len = success ?
305 VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000306 vhost_ubuf_put(ubufs);
307}
308
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000309/* Expects to be always run from workqueue - which acts as
310 * read-size critical section for our kind of RCU. */
311static void handle_tx(struct vhost_net *net)
312{
Asias He28394002013-04-27 15:07:46 +0800313 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300314 struct vhost_virtqueue *vq = &nvq->vq;
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300315 unsigned out, in, s;
316 int head;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000317 struct msghdr msg = {
318 .msg_name = NULL,
319 .msg_namelen = 0,
320 .msg_control = NULL,
321 .msg_controllen = 0,
322 .msg_iov = vq->iov,
323 .msg_flags = MSG_DONTWAIT,
324 };
325 size_t len, total_len = 0;
Jason Wang70181d512013-04-10 20:50:48 +0000326 int err;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000327 size_t hdr_size;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100328 struct socket *sock;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000329 struct vhost_ubuf_ref *uninitialized_var(ubufs);
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200330 bool zcopy, zcopy_used;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100331
Michael S. Tsirkin5e182472011-01-18 13:04:43 +0200332 /* TODO: check that we are running from vhost_worker? */
Michael S. Tsirkin11cd1a82010-11-14 17:31:52 +0200333 sock = rcu_dereference_check(vq->private_data, 1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000334 if (!sock)
335 return;
336
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000337 mutex_lock(&vq->mutex);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300338 vhost_disable_notify(&net->dev, vq);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000339
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300340 hdr_size = nvq->vhost_hlen;
Asias He28394002013-04-27 15:07:46 +0800341 zcopy = nvq->ubufs;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000342
343 for (;;) {
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000344 /* Release DMAs done buffers first */
345 if (zcopy)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000346 vhost_zerocopy_signal_used(net, vq);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000347
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000348 head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
349 ARRAY_SIZE(vq->iov),
350 &out, &in,
351 NULL, NULL);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300352 /* On error, stop handling until the next kick. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300353 if (unlikely(head < 0))
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300354 break;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000355 /* Nothing new? Wait for eventfd to tell us they refilled. */
356 if (head == vq->num) {
Shirley Ma9e380822011-07-20 10:23:12 -0700357 int num_pends;
358
Shirley Ma9e380822011-07-20 10:23:12 -0700359 /* If more outstanding DMAs, queue the work.
360 * Handle upend_idx wrap around
361 */
Asias He28394002013-04-27 15:07:46 +0800362 num_pends = likely(nvq->upend_idx >= nvq->done_idx) ?
363 (nvq->upend_idx - nvq->done_idx) :
364 (nvq->upend_idx + UIO_MAXIOV -
365 nvq->done_idx);
Jason Wang70181d512013-04-10 20:50:48 +0000366 if (unlikely(num_pends > VHOST_MAX_PEND))
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000367 break;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300368 if (unlikely(vhost_enable_notify(&net->dev, vq))) {
369 vhost_disable_notify(&net->dev, vq);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000370 continue;
371 }
372 break;
373 }
374 if (in) {
375 vq_err(vq, "Unexpected descriptor format for TX: "
376 "out %d, int %d\n", out, in);
377 break;
378 }
379 /* Skip header. TODO: support TSO. */
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300380 s = move_iovec_hdr(vq->iov, nvq->hdr, hdr_size, out);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000381 msg.msg_iovlen = out;
382 len = iov_length(vq->iov, out);
383 /* Sanity check */
384 if (!len) {
385 vq_err(vq, "Unexpected header len for TX: "
386 "%zd expected %zd\n",
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300387 iov_length(nvq->hdr, s), hdr_size);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000388 break;
389 }
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200390 zcopy_used = zcopy && (len >= VHOST_GOODCOPY_LEN ||
Asias He28394002013-04-27 15:07:46 +0800391 nvq->upend_idx != nvq->done_idx);
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200392
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000393 /* use msg_control to pass vhost zerocopy ubuf info to skb */
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200394 if (zcopy_used) {
Asias He28394002013-04-27 15:07:46 +0800395 vq->heads[nvq->upend_idx].id = head;
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000396 if (!vhost_net_tx_select_zcopy(net) ||
397 len < VHOST_GOODCOPY_LEN) {
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000398 /* copy don't need to wait for DMA done */
Asias He28394002013-04-27 15:07:46 +0800399 vq->heads[nvq->upend_idx].len =
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000400 VHOST_DMA_DONE_LEN;
401 msg.msg_control = NULL;
402 msg.msg_controllen = 0;
403 ubufs = NULL;
404 } else {
Michael S. Tsirkin46aa92d2013-03-17 02:46:09 +0000405 struct ubuf_info *ubuf;
Asias He28394002013-04-27 15:07:46 +0800406 ubuf = nvq->ubuf_info + nvq->upend_idx;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000407
Asias He28394002013-04-27 15:07:46 +0800408 vq->heads[nvq->upend_idx].len =
Michael S. Tsirkin70e4cb92012-11-01 09:16:37 +0000409 VHOST_DMA_IN_PROGRESS;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000410 ubuf->callback = vhost_zerocopy_callback;
Asias He28394002013-04-27 15:07:46 +0800411 ubuf->ctx = nvq->ubufs;
412 ubuf->desc = nvq->upend_idx;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000413 msg.msg_control = ubuf;
414 msg.msg_controllen = sizeof(ubuf);
Asias He28394002013-04-27 15:07:46 +0800415 ubufs = nvq->ubufs;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000416 kref_get(&ubufs->kref);
417 }
Asias He28394002013-04-27 15:07:46 +0800418 nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000419 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000420 /* TODO: Check specific error and bomb out unless ENOBUFS? */
421 err = sock->ops->sendmsg(NULL, sock, &msg, len);
422 if (unlikely(err < 0)) {
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200423 if (zcopy_used) {
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000424 if (ubufs)
425 vhost_ubuf_put(ubufs);
Asias He28394002013-04-27 15:07:46 +0800426 nvq->upend_idx = ((unsigned)nvq->upend_idx - 1)
427 % UIO_MAXIOV;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000428 }
David Stevens8dd014a2010-07-27 18:52:21 +0300429 vhost_discard_vq_desc(vq, 1);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000430 break;
431 }
432 if (err != len)
Michael S. Tsirkin95c0ec62010-06-24 17:10:25 +0300433 pr_debug("Truncated TX packet: "
434 " len %d != %zd\n", err, len);
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200435 if (!zcopy_used)
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000436 vhost_add_used_and_signal(&net->dev, vq, head, 0);
Jason Wangc8fb2172012-05-02 11:42:41 +0800437 else
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000438 vhost_zerocopy_signal_used(net, vq);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000439 total_len += len;
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000440 vhost_net_tx_packet(net);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000441 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
442 vhost_poll_queue(&vq->poll);
443 break;
444 }
445 }
446
447 mutex_unlock(&vq->mutex);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000448}
449
David Stevens8dd014a2010-07-27 18:52:21 +0300450static int peek_head_len(struct sock *sk)
451{
452 struct sk_buff *head;
453 int len = 0;
Jason Wang783e3982011-01-17 16:11:17 +0800454 unsigned long flags;
David Stevens8dd014a2010-07-27 18:52:21 +0300455
Jason Wang783e3982011-01-17 16:11:17 +0800456 spin_lock_irqsave(&sk->sk_receive_queue.lock, flags);
David Stevens8dd014a2010-07-27 18:52:21 +0300457 head = skb_peek(&sk->sk_receive_queue);
Basil Gorc53cff5e2012-05-03 22:55:23 +0000458 if (likely(head)) {
David Stevens8dd014a2010-07-27 18:52:21 +0300459 len = head->len;
Basil Gorc53cff5e2012-05-03 22:55:23 +0000460 if (vlan_tx_tag_present(head))
461 len += VLAN_HLEN;
462 }
463
Jason Wang783e3982011-01-17 16:11:17 +0800464 spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags);
David Stevens8dd014a2010-07-27 18:52:21 +0300465 return len;
466}
467
468/* This is a multi-buffer version of vhost_get_desc, that works if
469 * vq has read descriptors only.
470 * @vq - the relevant virtqueue
471 * @datalen - data length we'll be reading
472 * @iovcount - returned count of io vectors we fill
473 * @log - vhost log
474 * @log_num - log offset
Jason Wang94249362011-01-17 16:11:08 +0800475 * @quota - headcount quota, 1 for big buffer
David Stevens8dd014a2010-07-27 18:52:21 +0300476 * returns number of buffer heads allocated, negative on error
477 */
478static int get_rx_bufs(struct vhost_virtqueue *vq,
479 struct vring_used_elem *heads,
480 int datalen,
481 unsigned *iovcount,
482 struct vhost_log *log,
Jason Wang94249362011-01-17 16:11:08 +0800483 unsigned *log_num,
484 unsigned int quota)
David Stevens8dd014a2010-07-27 18:52:21 +0300485{
486 unsigned int out, in;
487 int seg = 0;
488 int headcount = 0;
489 unsigned d;
490 int r, nlogs = 0;
491
Jason Wang94249362011-01-17 16:11:08 +0800492 while (datalen > 0 && headcount < quota) {
Jason Wange0e9b402010-09-14 23:53:05 +0800493 if (unlikely(seg >= UIO_MAXIOV)) {
David Stevens8dd014a2010-07-27 18:52:21 +0300494 r = -ENOBUFS;
495 goto err;
496 }
497 d = vhost_get_vq_desc(vq->dev, vq, vq->iov + seg,
498 ARRAY_SIZE(vq->iov) - seg, &out,
499 &in, log, log_num);
500 if (d == vq->num) {
501 r = 0;
502 goto err;
503 }
504 if (unlikely(out || in <= 0)) {
505 vq_err(vq, "unexpected descriptor format for RX: "
506 "out %d, in %d\n", out, in);
507 r = -EINVAL;
508 goto err;
509 }
510 if (unlikely(log)) {
511 nlogs += *log_num;
512 log += *log_num;
513 }
514 heads[headcount].id = d;
515 heads[headcount].len = iov_length(vq->iov + seg, in);
516 datalen -= heads[headcount].len;
517 ++headcount;
518 seg += in;
519 }
520 heads[headcount - 1].len += datalen;
521 *iovcount = seg;
522 if (unlikely(log))
523 *log_num = nlogs;
524 return headcount;
525err:
526 vhost_discard_vq_desc(vq, headcount);
527 return r;
528}
529
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000530/* Expects to be always run from workqueue - which acts as
531 * read-size critical section for our kind of RCU. */
Jason Wang94249362011-01-17 16:11:08 +0800532static void handle_rx(struct vhost_net *net)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000533{
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300534 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX];
535 struct vhost_virtqueue *vq = &nvq->vq;
David Stevens8dd014a2010-07-27 18:52:21 +0300536 unsigned uninitialized_var(in), log;
537 struct vhost_log *vq_log;
538 struct msghdr msg = {
539 .msg_name = NULL,
540 .msg_namelen = 0,
541 .msg_control = NULL, /* FIXME: get and handle RX aux data. */
542 .msg_controllen = 0,
543 .msg_iov = vq->iov,
544 .msg_flags = MSG_DONTWAIT,
545 };
David Stevens8dd014a2010-07-27 18:52:21 +0300546 struct virtio_net_hdr_mrg_rxbuf hdr = {
547 .hdr.flags = 0,
548 .hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE
549 };
David Stevens8dd014a2010-07-27 18:52:21 +0300550 size_t total_len = 0;
Michael S. Tsirkin910a5782012-10-24 20:37:51 +0200551 int err, mergeable;
552 s16 headcount;
David Stevens8dd014a2010-07-27 18:52:21 +0300553 size_t vhost_hlen, sock_hlen;
554 size_t vhost_len, sock_len;
Michael S. Tsirkin5e182472011-01-18 13:04:43 +0200555 /* TODO: check that we are running from vhost_worker? */
556 struct socket *sock = rcu_dereference_check(vq->private_data, 1);
Krishna Kumard47effe2011-03-01 17:06:37 +0530557
Michael S. Tsirkinde4d7682011-03-13 23:00:52 +0200558 if (!sock)
David Stevens8dd014a2010-07-27 18:52:21 +0300559 return;
560
David Stevens8dd014a2010-07-27 18:52:21 +0300561 mutex_lock(&vq->mutex);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300562 vhost_disable_notify(&net->dev, vq);
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300563 vhost_hlen = nvq->vhost_hlen;
564 sock_hlen = nvq->sock_hlen;
David Stevens8dd014a2010-07-27 18:52:21 +0300565
566 vq_log = unlikely(vhost_has_feature(&net->dev, VHOST_F_LOG_ALL)) ?
567 vq->log : NULL;
Jason Wangcfbdab92011-01-17 16:10:59 +0800568 mergeable = vhost_has_feature(&net->dev, VIRTIO_NET_F_MRG_RXBUF);
David Stevens8dd014a2010-07-27 18:52:21 +0300569
570 while ((sock_len = peek_head_len(sock->sk))) {
571 sock_len += sock_hlen;
572 vhost_len = sock_len + vhost_hlen;
573 headcount = get_rx_bufs(vq, vq->heads, vhost_len,
Jason Wang94249362011-01-17 16:11:08 +0800574 &in, vq_log, &log,
575 likely(mergeable) ? UIO_MAXIOV : 1);
David Stevens8dd014a2010-07-27 18:52:21 +0300576 /* On error, stop handling until the next kick. */
577 if (unlikely(headcount < 0))
578 break;
579 /* OK, now we need to know about added descriptors. */
580 if (!headcount) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300581 if (unlikely(vhost_enable_notify(&net->dev, vq))) {
David Stevens8dd014a2010-07-27 18:52:21 +0300582 /* They have slipped one in as we were
583 * doing that: check again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300584 vhost_disable_notify(&net->dev, vq);
David Stevens8dd014a2010-07-27 18:52:21 +0300585 continue;
586 }
587 /* Nothing new? Wait for eventfd to tell us
588 * they refilled. */
589 break;
590 }
591 /* We don't need to be notified again. */
592 if (unlikely((vhost_hlen)))
593 /* Skip header. TODO: support TSO. */
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300594 move_iovec_hdr(vq->iov, nvq->hdr, vhost_hlen, in);
David Stevens8dd014a2010-07-27 18:52:21 +0300595 else
596 /* Copy the header for use in VIRTIO_NET_F_MRG_RXBUF:
Jason Wanga290aec2010-11-29 13:48:40 +0800597 * needed because recvmsg can modify msg_iov. */
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300598 copy_iovec_hdr(vq->iov, nvq->hdr, sock_hlen, in);
David Stevens8dd014a2010-07-27 18:52:21 +0300599 msg.msg_iovlen = in;
600 err = sock->ops->recvmsg(NULL, sock, &msg,
601 sock_len, MSG_DONTWAIT | MSG_TRUNC);
602 /* Userspace might have consumed the packet meanwhile:
603 * it's not supposed to do this usually, but might be hard
604 * to prevent. Discard data we got (if any) and keep going. */
605 if (unlikely(err != sock_len)) {
606 pr_debug("Discarded rx packet: "
607 " len %d, expected %zd\n", err, sock_len);
608 vhost_discard_vq_desc(vq, headcount);
609 continue;
610 }
611 if (unlikely(vhost_hlen) &&
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300612 memcpy_toiovecend(nvq->hdr, (unsigned char *)&hdr, 0,
David Stevens8dd014a2010-07-27 18:52:21 +0300613 vhost_hlen)) {
614 vq_err(vq, "Unable to write vnet_hdr at addr %p\n",
615 vq->iov->iov_base);
616 break;
617 }
618 /* TODO: Should check and handle checksum. */
Jason Wangcfbdab92011-01-17 16:10:59 +0800619 if (likely(mergeable) &&
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300620 memcpy_toiovecend(nvq->hdr, (unsigned char *)&headcount,
David Stevens8dd014a2010-07-27 18:52:21 +0300621 offsetof(typeof(hdr), num_buffers),
622 sizeof hdr.num_buffers)) {
623 vq_err(vq, "Failed num_buffers write");
624 vhost_discard_vq_desc(vq, headcount);
625 break;
626 }
627 vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
628 headcount);
629 if (unlikely(vq_log))
630 vhost_log_write(vq, vq_log, log, vhost_len);
631 total_len += vhost_len;
632 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
633 vhost_poll_queue(&vq->poll);
634 break;
635 }
636 }
637
638 mutex_unlock(&vq->mutex);
David Stevens8dd014a2010-07-27 18:52:21 +0300639}
640
Tejun Heoc23f34452010-06-02 20:40:00 +0200641static void handle_tx_kick(struct vhost_work *work)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000642{
Tejun Heoc23f34452010-06-02 20:40:00 +0200643 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
644 poll.work);
645 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
646
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000647 handle_tx(net);
648}
649
Tejun Heoc23f34452010-06-02 20:40:00 +0200650static void handle_rx_kick(struct vhost_work *work)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000651{
Tejun Heoc23f34452010-06-02 20:40:00 +0200652 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
653 poll.work);
654 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
655
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000656 handle_rx(net);
657}
658
Tejun Heoc23f34452010-06-02 20:40:00 +0200659static void handle_tx_net(struct vhost_work *work)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000660{
Tejun Heoc23f34452010-06-02 20:40:00 +0200661 struct vhost_net *net = container_of(work, struct vhost_net,
662 poll[VHOST_NET_VQ_TX].work);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000663 handle_tx(net);
664}
665
Tejun Heoc23f34452010-06-02 20:40:00 +0200666static void handle_rx_net(struct vhost_work *work)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000667{
Tejun Heoc23f34452010-06-02 20:40:00 +0200668 struct vhost_net *net = container_of(work, struct vhost_net,
669 poll[VHOST_NET_VQ_RX].work);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000670 handle_rx(net);
671}
672
673static int vhost_net_open(struct inode *inode, struct file *f)
674{
675 struct vhost_net *n = kmalloc(sizeof *n, GFP_KERNEL);
Tejun Heoc23f34452010-06-02 20:40:00 +0200676 struct vhost_dev *dev;
Asias He3ab2e422013-04-27 11:16:48 +0800677 struct vhost_virtqueue **vqs;
Asias He28394002013-04-27 15:07:46 +0800678 int r, i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200679
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000680 if (!n)
681 return -ENOMEM;
Asias He3ab2e422013-04-27 11:16:48 +0800682 vqs = kmalloc(VHOST_NET_VQ_MAX * sizeof(*vqs), GFP_KERNEL);
683 if (!vqs) {
684 kfree(n);
685 return -ENOMEM;
686 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200687
688 dev = &n->dev;
Asias He3ab2e422013-04-27 11:16:48 +0800689 vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq;
690 vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq;
691 n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick;
692 n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick;
Asias He28394002013-04-27 15:07:46 +0800693 for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
694 n->vqs[i].ubufs = NULL;
695 n->vqs[i].ubuf_info = NULL;
696 n->vqs[i].upend_idx = 0;
697 n->vqs[i].done_idx = 0;
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300698 n->vqs[i].vhost_hlen = 0;
699 n->vqs[i].sock_hlen = 0;
Asias He28394002013-04-27 15:07:46 +0800700 }
Asias He3ab2e422013-04-27 11:16:48 +0800701 r = vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000702 if (r < 0) {
703 kfree(n);
Asias He3ab2e422013-04-27 11:16:48 +0800704 kfree(vqs);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000705 return r;
706 }
707
Tejun Heoc23f34452010-06-02 20:40:00 +0200708 vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
709 vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000710
711 f->private_data = n;
712
713 return 0;
714}
715
716static void vhost_net_disable_vq(struct vhost_net *n,
717 struct vhost_virtqueue *vq)
718{
Asias He3ab2e422013-04-27 11:16:48 +0800719 struct vhost_net_virtqueue *nvq =
720 container_of(vq, struct vhost_net_virtqueue, vq);
721 struct vhost_poll *poll = n->poll + (nvq - n->vqs);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000722 if (!vq->private_data)
723 return;
Jason Wang70181d512013-04-10 20:50:48 +0000724 vhost_poll_stop(poll);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000725}
726
Jason Wang2b8b3282013-01-28 01:05:18 +0000727static int vhost_net_enable_vq(struct vhost_net *n,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000728 struct vhost_virtqueue *vq)
729{
Asias He3ab2e422013-04-27 11:16:48 +0800730 struct vhost_net_virtqueue *nvq =
731 container_of(vq, struct vhost_net_virtqueue, vq);
732 struct vhost_poll *poll = n->poll + (nvq - n->vqs);
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100733 struct socket *sock;
734
735 sock = rcu_dereference_protected(vq->private_data,
736 lockdep_is_held(&vq->mutex));
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000737 if (!sock)
Jason Wang2b8b3282013-01-28 01:05:18 +0000738 return 0;
Jason Wang2b8b3282013-01-28 01:05:18 +0000739
Jason Wang70181d512013-04-10 20:50:48 +0000740 return vhost_poll_start(poll, sock->file);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000741}
742
743static struct socket *vhost_net_stop_vq(struct vhost_net *n,
744 struct vhost_virtqueue *vq)
745{
746 struct socket *sock;
747
748 mutex_lock(&vq->mutex);
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100749 sock = rcu_dereference_protected(vq->private_data,
750 lockdep_is_held(&vq->mutex));
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000751 vhost_net_disable_vq(n, vq);
752 rcu_assign_pointer(vq->private_data, NULL);
753 mutex_unlock(&vq->mutex);
754 return sock;
755}
756
757static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
758 struct socket **rx_sock)
759{
Asias He3ab2e422013-04-27 11:16:48 +0800760 *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq);
761 *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000762}
763
764static void vhost_net_flush_vq(struct vhost_net *n, int index)
765{
766 vhost_poll_flush(n->poll + index);
Asias He3ab2e422013-04-27 11:16:48 +0800767 vhost_poll_flush(&n->vqs[index].vq.poll);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000768}
769
770static void vhost_net_flush(struct vhost_net *n)
771{
772 vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
773 vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
Asias He28394002013-04-27 15:07:46 +0800774 if (n->vqs[VHOST_NET_VQ_TX].ubufs) {
Asias He3ab2e422013-04-27 11:16:48 +0800775 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200776 n->tx_flush = true;
Asias He3ab2e422013-04-27 11:16:48 +0800777 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200778 /* Wait for all lower device DMAs done. */
Asias He28394002013-04-27 15:07:46 +0800779 vhost_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs);
Asias He3ab2e422013-04-27 11:16:48 +0800780 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200781 n->tx_flush = false;
Asias He28394002013-04-27 15:07:46 +0800782 kref_init(&n->vqs[VHOST_NET_VQ_TX].ubufs->kref);
Asias He3ab2e422013-04-27 11:16:48 +0800783 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200784 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000785}
786
787static int vhost_net_release(struct inode *inode, struct file *f)
788{
789 struct vhost_net *n = f->private_data;
790 struct socket *tx_sock;
791 struct socket *rx_sock;
792
793 vhost_net_stop(n, &tx_sock, &rx_sock);
794 vhost_net_flush(n);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000795 vhost_dev_stop(&n->dev);
Michael S. Tsirkinea5d4042011-11-27 19:05:58 +0200796 vhost_dev_cleanup(&n->dev, false);
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300797 vhost_net_vq_reset(n);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000798 if (tx_sock)
799 fput(tx_sock->file);
800 if (rx_sock)
801 fput(rx_sock->file);
802 /* We do an extra flush before freeing memory,
803 * since jobs can re-queue themselves. */
804 vhost_net_flush(n);
Asias He3ab2e422013-04-27 11:16:48 +0800805 kfree(n->dev.vqs);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000806 kfree(n);
807 return 0;
808}
809
810static struct socket *get_raw_socket(int fd)
811{
812 struct {
813 struct sockaddr_ll sa;
814 char buf[MAX_ADDR_LEN];
815 } uaddr;
816 int uaddr_len = sizeof uaddr, r;
817 struct socket *sock = sockfd_lookup(fd, &r);
Krishna Kumard47effe2011-03-01 17:06:37 +0530818
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000819 if (!sock)
820 return ERR_PTR(-ENOTSOCK);
821
822 /* Parameter checking */
823 if (sock->sk->sk_type != SOCK_RAW) {
824 r = -ESOCKTNOSUPPORT;
825 goto err;
826 }
827
828 r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa,
829 &uaddr_len, 0);
830 if (r)
831 goto err;
832
833 if (uaddr.sa.sll_family != AF_PACKET) {
834 r = -EPFNOSUPPORT;
835 goto err;
836 }
837 return sock;
838err:
839 fput(sock->file);
840 return ERR_PTR(r);
841}
842
Arnd Bergmann501c7742010-02-18 05:46:50 +0000843static struct socket *get_tap_socket(int fd)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000844{
845 struct file *file = fget(fd);
846 struct socket *sock;
Krishna Kumard47effe2011-03-01 17:06:37 +0530847
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000848 if (!file)
849 return ERR_PTR(-EBADF);
850 sock = tun_get_socket(file);
Arnd Bergmann501c7742010-02-18 05:46:50 +0000851 if (!IS_ERR(sock))
852 return sock;
853 sock = macvtap_get_socket(file);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000854 if (IS_ERR(sock))
855 fput(file);
856 return sock;
857}
858
859static struct socket *get_socket(int fd)
860{
861 struct socket *sock;
Krishna Kumard47effe2011-03-01 17:06:37 +0530862
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000863 /* special case to disable backend */
864 if (fd == -1)
865 return NULL;
866 sock = get_raw_socket(fd);
867 if (!IS_ERR(sock))
868 return sock;
Arnd Bergmann501c7742010-02-18 05:46:50 +0000869 sock = get_tap_socket(fd);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000870 if (!IS_ERR(sock))
871 return sock;
872 return ERR_PTR(-ENOTSOCK);
873}
874
875static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
876{
877 struct socket *sock, *oldsock;
878 struct vhost_virtqueue *vq;
Asias He28394002013-04-27 15:07:46 +0800879 struct vhost_net_virtqueue *nvq;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000880 struct vhost_ubuf_ref *ubufs, *oldubufs = NULL;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000881 int r;
882
883 mutex_lock(&n->dev.mutex);
884 r = vhost_dev_check_owner(&n->dev);
885 if (r)
886 goto err;
887
888 if (index >= VHOST_NET_VQ_MAX) {
889 r = -ENOBUFS;
890 goto err;
891 }
Asias He3ab2e422013-04-27 11:16:48 +0800892 vq = &n->vqs[index].vq;
Asias He28394002013-04-27 15:07:46 +0800893 nvq = &n->vqs[index];
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000894 mutex_lock(&vq->mutex);
895
896 /* Verify that ring has been setup correctly. */
897 if (!vhost_vq_access_ok(vq)) {
898 r = -EFAULT;
Jeff Dike1dace8c2010-03-04 16:10:14 -0500899 goto err_vq;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000900 }
901 sock = get_socket(fd);
902 if (IS_ERR(sock)) {
903 r = PTR_ERR(sock);
Jeff Dike1dace8c2010-03-04 16:10:14 -0500904 goto err_vq;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000905 }
906
907 /* start polling new socket */
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100908 oldsock = rcu_dereference_protected(vq->private_data,
909 lockdep_is_held(&vq->mutex));
David S. Miller11fe8832010-07-20 18:25:24 -0700910 if (sock != oldsock) {
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000911 ubufs = vhost_ubuf_alloc(vq, sock && vhost_sock_zcopy(sock));
912 if (IS_ERR(ubufs)) {
913 r = PTR_ERR(ubufs);
914 goto err_ubufs;
915 }
Jason Wang692a9982013-01-28 01:05:17 +0000916
Krishna Kumard47effe2011-03-01 17:06:37 +0530917 vhost_net_disable_vq(n, vq);
918 rcu_assign_pointer(vq->private_data, sock);
Jason Wangf59281d2011-06-21 18:04:27 +0800919 r = vhost_init_used(vq);
920 if (r)
Jason Wang692a9982013-01-28 01:05:17 +0000921 goto err_used;
Jason Wang2b8b3282013-01-28 01:05:18 +0000922 r = vhost_net_enable_vq(n, vq);
923 if (r)
924 goto err_used;
Jason Wang692a9982013-01-28 01:05:17 +0000925
Asias He28394002013-04-27 15:07:46 +0800926 oldubufs = nvq->ubufs;
927 nvq->ubufs = ubufs;
Michael S. Tsirkin64e9a9b2012-12-03 07:31:51 +0000928
929 n->tx_packets = 0;
930 n->tx_zcopy_err = 0;
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200931 n->tx_flush = false;
Jeff Dikedd1f4072010-03-04 16:10:14 -0500932 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000933
Michael S. Tsirkin1680e902010-07-15 15:19:12 +0300934 mutex_unlock(&vq->mutex);
935
Michael S. Tsirkinc047e5f2011-07-20 13:41:31 +0300936 if (oldubufs) {
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000937 vhost_ubuf_put_and_wait(oldubufs);
Michael S. Tsirkinc047e5f2011-07-20 13:41:31 +0300938 mutex_lock(&vq->mutex);
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000939 vhost_zerocopy_signal_used(n, vq);
Michael S. Tsirkinc047e5f2011-07-20 13:41:31 +0300940 mutex_unlock(&vq->mutex);
941 }
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000942
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000943 if (oldsock) {
944 vhost_net_flush_vq(n, index);
945 fput(oldsock->file);
946 }
Jeff Dike1dace8c2010-03-04 16:10:14 -0500947
Michael S. Tsirkin1680e902010-07-15 15:19:12 +0300948 mutex_unlock(&n->dev.mutex);
949 return 0;
950
Jason Wang692a9982013-01-28 01:05:17 +0000951err_used:
952 rcu_assign_pointer(vq->private_data, oldsock);
953 vhost_net_enable_vq(n, vq);
954 if (ubufs)
955 vhost_ubuf_put_and_wait(ubufs);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000956err_ubufs:
957 fput(sock->file);
Jeff Dike1dace8c2010-03-04 16:10:14 -0500958err_vq:
959 mutex_unlock(&vq->mutex);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000960err:
961 mutex_unlock(&n->dev.mutex);
962 return r;
963}
964
965static long vhost_net_reset_owner(struct vhost_net *n)
966{
967 struct socket *tx_sock = NULL;
968 struct socket *rx_sock = NULL;
969 long err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530970
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000971 mutex_lock(&n->dev.mutex);
972 err = vhost_dev_check_owner(&n->dev);
973 if (err)
974 goto done;
975 vhost_net_stop(n, &tx_sock, &rx_sock);
976 vhost_net_flush(n);
977 err = vhost_dev_reset_owner(&n->dev);
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +0300978 vhost_net_vq_reset(n);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000979done:
980 mutex_unlock(&n->dev.mutex);
981 if (tx_sock)
982 fput(tx_sock->file);
983 if (rx_sock)
984 fput(rx_sock->file);
985 return err;
986}
987
988static int vhost_net_set_features(struct vhost_net *n, u64 features)
989{
David Stevens8dd014a2010-07-27 18:52:21 +0300990 size_t vhost_hlen, sock_hlen, hdr_len;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +0000991 int i;
David Stevens8dd014a2010-07-27 18:52:21 +0300992
993 hdr_len = (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ?
994 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
995 sizeof(struct virtio_net_hdr);
996 if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
997 /* vhost provides vnet_hdr */
998 vhost_hlen = hdr_len;
999 sock_hlen = 0;
1000 } else {
1001 /* socket provides vnet_hdr */
1002 vhost_hlen = 0;
1003 sock_hlen = hdr_len;
1004 }
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001005 mutex_lock(&n->dev.mutex);
1006 if ((features & (1 << VHOST_F_LOG_ALL)) &&
1007 !vhost_log_access_ok(&n->dev)) {
1008 mutex_unlock(&n->dev.mutex);
1009 return -EFAULT;
1010 }
1011 n->dev.acked_features = features;
1012 smp_wmb();
1013 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +08001014 mutex_lock(&n->vqs[i].vq.mutex);
Michael S. Tsirkin81f95a52013-04-28 15:51:40 +03001015 n->vqs[i].vhost_hlen = vhost_hlen;
1016 n->vqs[i].sock_hlen = sock_hlen;
Asias He3ab2e422013-04-27 11:16:48 +08001017 mutex_unlock(&n->vqs[i].vq.mutex);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001018 }
1019 vhost_net_flush(n);
1020 mutex_unlock(&n->dev.mutex);
1021 return 0;
1022}
1023
1024static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
1025 unsigned long arg)
1026{
1027 struct vhost_net *n = f->private_data;
1028 void __user *argp = (void __user *)arg;
1029 u64 __user *featurep = argp;
1030 struct vhost_vring_file backend;
1031 u64 features;
1032 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +05301033
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001034 switch (ioctl) {
1035 case VHOST_NET_SET_BACKEND:
Takuya Yoshikawad3553a52010-05-27 19:01:58 +09001036 if (copy_from_user(&backend, argp, sizeof backend))
1037 return -EFAULT;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001038 return vhost_net_set_backend(n, backend.index, backend.fd);
1039 case VHOST_GET_FEATURES:
Stefan Hajnoczi0dd05a32012-07-21 06:55:36 +00001040 features = VHOST_NET_FEATURES;
Takuya Yoshikawad3553a52010-05-27 19:01:58 +09001041 if (copy_to_user(featurep, &features, sizeof features))
1042 return -EFAULT;
1043 return 0;
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001044 case VHOST_SET_FEATURES:
Takuya Yoshikawad3553a52010-05-27 19:01:58 +09001045 if (copy_from_user(&features, featurep, sizeof features))
1046 return -EFAULT;
Stefan Hajnoczi0dd05a32012-07-21 06:55:36 +00001047 if (features & ~VHOST_NET_FEATURES)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001048 return -EOPNOTSUPP;
1049 return vhost_net_set_features(n, features);
1050 case VHOST_RESET_OWNER:
1051 return vhost_net_reset_owner(n);
1052 default:
1053 mutex_lock(&n->dev.mutex);
Asias He28394002013-04-27 15:07:46 +08001054 if (ioctl == VHOST_SET_OWNER) {
1055 r = vhost_net_set_ubuf_info(n);
1056 if (r)
1057 goto out;
1058 }
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001059 r = vhost_dev_ioctl(&n->dev, ioctl, argp);
1060 if (r == -ENOIOCTLCMD)
1061 r = vhost_vring_ioctl(&n->dev, ioctl, argp);
1062 else
1063 vhost_net_flush(n);
Asias He28394002013-04-27 15:07:46 +08001064out:
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001065 mutex_unlock(&n->dev.mutex);
1066 return r;
1067 }
1068}
1069
1070#ifdef CONFIG_COMPAT
1071static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
1072 unsigned long arg)
1073{
1074 return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1075}
1076#endif
1077
Tobias Klauser373a83a2010-05-17 15:12:49 +02001078static const struct file_operations vhost_net_fops = {
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001079 .owner = THIS_MODULE,
1080 .release = vhost_net_release,
1081 .unlocked_ioctl = vhost_net_ioctl,
1082#ifdef CONFIG_COMPAT
1083 .compat_ioctl = vhost_net_compat_ioctl,
1084#endif
1085 .open = vhost_net_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001086 .llseek = noop_llseek,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001087};
1088
1089static struct miscdevice vhost_net_misc = {
stephen hemminger7c7c7f02012-01-11 19:30:38 +00001090 .minor = VHOST_NET_MINOR,
1091 .name = "vhost-net",
1092 .fops = &vhost_net_fops,
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001093};
1094
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001095static int vhost_net_init(void)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001096{
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +00001097 if (experimental_zcopytx)
1098 vhost_enable_zcopy(VHOST_NET_VQ_TX);
Tejun Heoc23f34452010-06-02 20:40:00 +02001099 return misc_register(&vhost_net_misc);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001100}
1101module_init(vhost_net_init);
1102
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001103static void vhost_net_exit(void)
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001104{
1105 misc_deregister(&vhost_net_misc);
Michael S. Tsirkin3a4d5c94e2010-01-14 06:17:27 +00001106}
1107module_exit(vhost_net_exit);
1108
1109MODULE_VERSION("0.0.1");
1110MODULE_LICENSE("GPL v2");
1111MODULE_AUTHOR("Michael S. Tsirkin");
1112MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
stephen hemminger7c7c7f02012-01-11 19:30:38 +00001113MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR);
1114MODULE_ALIAS("devname:vhost-net");