blob: 176aa030dc5fad3796309e6a722108c8c97cc470 [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>
18#include <linux/rcupdate.h>
19#include <linux/file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.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>
Basil Gorc53cff5e2012-05-03 22:55:23 +000027#include <linux/if_vlan.h>
Michael S. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-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. Tsirkin3a4d5c92010-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 He3ab2e422013-04-27 11:16:48 +080067struct vhost_net_virtqueue {
68 struct vhost_virtqueue vq;
69};
70
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000071struct vhost_net {
72 struct vhost_dev dev;
Asias He3ab2e422013-04-27 11:16:48 +080073 struct vhost_net_virtqueue vqs[VHOST_NET_VQ_MAX];
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000074 struct vhost_poll poll[VHOST_NET_VQ_MAX];
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000075 /* Number of TX recently submitted.
76 * Protected by tx vq lock. */
77 unsigned tx_packets;
78 /* Number of times zerocopy TX recently failed.
79 * Protected by tx vq lock. */
80 unsigned tx_zcopy_err;
Michael S. Tsirkin1280c272012-12-04 00:17:14 +020081 /* Flush in progress. Protected by tx vq lock. */
82 bool tx_flush;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000083};
84
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000085static void vhost_net_tx_packet(struct vhost_net *net)
86{
87 ++net->tx_packets;
88 if (net->tx_packets < 1024)
89 return;
90 net->tx_packets = 0;
91 net->tx_zcopy_err = 0;
92}
93
94static void vhost_net_tx_err(struct vhost_net *net)
95{
96 ++net->tx_zcopy_err;
97}
98
99static bool vhost_net_tx_select_zcopy(struct vhost_net *net)
100{
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200101 /* TX flush waits for outstanding DMAs to be done.
102 * Don't start new DMAs.
103 */
104 return !net->tx_flush &&
105 net->tx_packets / 64 >= net->tx_zcopy_err;
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000106}
107
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000108static bool vhost_sock_zcopy(struct socket *sock)
109{
110 return unlikely(experimental_zcopytx) &&
111 sock_flag(sock->sk, SOCK_ZEROCOPY);
112}
113
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000114/* Pop first len bytes from iovec. Return number of segments used. */
115static int move_iovec_hdr(struct iovec *from, struct iovec *to,
116 size_t len, int iov_count)
117{
118 int seg = 0;
119 size_t size;
Krishna Kumard47effe2011-03-01 17:06:37 +0530120
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000121 while (len && seg < iov_count) {
122 size = min(from->iov_len, len);
123 to->iov_base = from->iov_base;
124 to->iov_len = size;
125 from->iov_len -= size;
126 from->iov_base += size;
127 len -= size;
128 ++from;
129 ++to;
130 ++seg;
131 }
132 return seg;
133}
David Stevens8dd014a2010-07-27 18:52:21 +0300134/* Copy iovec entries for len bytes from iovec. */
135static void copy_iovec_hdr(const struct iovec *from, struct iovec *to,
136 size_t len, int iovcount)
137{
138 int seg = 0;
139 size_t size;
Krishna Kumard47effe2011-03-01 17:06:37 +0530140
David Stevens8dd014a2010-07-27 18:52:21 +0300141 while (len && seg < iovcount) {
142 size = min(from->iov_len, len);
143 to->iov_base = from->iov_base;
144 to->iov_len = size;
145 len -= size;
146 ++from;
147 ++to;
148 ++seg;
149 }
150}
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000151
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000152/* In case of DMA done not in order in lower device driver for some reason.
153 * upend_idx is used to track end of used idx, done_idx is used to track head
154 * of used idx. Once lower device DMA done contiguously, we will signal KVM
155 * guest used idx.
156 */
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000157static int vhost_zerocopy_signal_used(struct vhost_net *net,
158 struct vhost_virtqueue *vq)
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000159{
160 int i;
161 int j = 0;
162
163 for (i = vq->done_idx; i != vq->upend_idx; i = (i + 1) % UIO_MAXIOV) {
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000164 if (vq->heads[i].len == VHOST_DMA_FAILED_LEN)
165 vhost_net_tx_err(net);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000166 if (VHOST_DMA_IS_DONE(vq->heads[i].len)) {
167 vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
168 vhost_add_used_and_signal(vq->dev, vq,
169 vq->heads[i].id, 0);
170 ++j;
171 } else
172 break;
173 }
174 if (j)
175 vq->done_idx = i;
176 return j;
177}
178
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000179static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000180{
181 struct vhost_ubuf_ref *ubufs = ubuf->ctx;
182 struct vhost_virtqueue *vq = ubufs->vq;
Michael S. Tsirkin24eb21a2012-11-01 09:16:55 +0000183 int cnt = atomic_read(&ubufs->kref.refcount);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000184
Michael S. Tsirkin24eb21a2012-11-01 09:16:55 +0000185 /*
186 * Trigger polling thread if guest stopped submitting new buffers:
187 * in this case, the refcount after decrement will eventually reach 1
188 * so here it is 2.
189 * We also trigger polling periodically after each 16 packets
190 * (the value 16 here is more or less arbitrary, it's tuned to trigger
191 * less than 10% of times).
192 */
193 if (cnt <= 2 || !(cnt % 16))
194 vhost_poll_queue(&vq->poll);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000195 /* set len to mark this desc buffers done DMA */
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000196 vq->heads[ubuf->desc].len = success ?
197 VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000198 vhost_ubuf_put(ubufs);
199}
200
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000201/* Expects to be always run from workqueue - which acts as
202 * read-size critical section for our kind of RCU. */
203static void handle_tx(struct vhost_net *net)
204{
Asias He3ab2e422013-04-27 11:16:48 +0800205 struct vhost_virtqueue *vq = &net->vqs[VHOST_NET_VQ_TX].vq;
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300206 unsigned out, in, s;
207 int head;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000208 struct msghdr msg = {
209 .msg_name = NULL,
210 .msg_namelen = 0,
211 .msg_control = NULL,
212 .msg_controllen = 0,
213 .msg_iov = vq->iov,
214 .msg_flags = MSG_DONTWAIT,
215 };
216 size_t len, total_len = 0;
Jason Wang70181d512013-04-10 20:50:48 +0000217 int err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000218 size_t hdr_size;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100219 struct socket *sock;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000220 struct vhost_ubuf_ref *uninitialized_var(ubufs);
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200221 bool zcopy, zcopy_used;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100222
Michael S. Tsirkin5e182472011-01-18 13:04:43 +0200223 /* TODO: check that we are running from vhost_worker? */
Michael S. Tsirkin11cd1a82010-11-14 17:31:52 +0200224 sock = rcu_dereference_check(vq->private_data, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000225 if (!sock)
226 return;
227
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000228 mutex_lock(&vq->mutex);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300229 vhost_disable_notify(&net->dev, vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000230
David Stevens8dd014a2010-07-27 18:52:21 +0300231 hdr_size = vq->vhost_hlen;
Jason Wangc460f052012-05-02 11:42:23 +0800232 zcopy = vq->ubufs;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000233
234 for (;;) {
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000235 /* Release DMAs done buffers first */
236 if (zcopy)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000237 vhost_zerocopy_signal_used(net, vq);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000238
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000239 head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
240 ARRAY_SIZE(vq->iov),
241 &out, &in,
242 NULL, NULL);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300243 /* On error, stop handling until the next kick. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300244 if (unlikely(head < 0))
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300245 break;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000246 /* Nothing new? Wait for eventfd to tell us they refilled. */
247 if (head == vq->num) {
Shirley Ma9e380822011-07-20 10:23:12 -0700248 int num_pends;
249
Shirley Ma9e380822011-07-20 10:23:12 -0700250 /* If more outstanding DMAs, queue the work.
251 * Handle upend_idx wrap around
252 */
253 num_pends = likely(vq->upend_idx >= vq->done_idx) ?
254 (vq->upend_idx - vq->done_idx) :
255 (vq->upend_idx + UIO_MAXIOV - vq->done_idx);
Jason Wang70181d512013-04-10 20:50:48 +0000256 if (unlikely(num_pends > VHOST_MAX_PEND))
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000257 break;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300258 if (unlikely(vhost_enable_notify(&net->dev, vq))) {
259 vhost_disable_notify(&net->dev, vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000260 continue;
261 }
262 break;
263 }
264 if (in) {
265 vq_err(vq, "Unexpected descriptor format for TX: "
266 "out %d, int %d\n", out, in);
267 break;
268 }
269 /* Skip header. TODO: support TSO. */
270 s = move_iovec_hdr(vq->iov, vq->hdr, hdr_size, out);
271 msg.msg_iovlen = out;
272 len = iov_length(vq->iov, out);
273 /* Sanity check */
274 if (!len) {
275 vq_err(vq, "Unexpected header len for TX: "
276 "%zd expected %zd\n",
277 iov_length(vq->hdr, s), hdr_size);
278 break;
279 }
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200280 zcopy_used = zcopy && (len >= VHOST_GOODCOPY_LEN ||
281 vq->upend_idx != vq->done_idx);
282
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000283 /* use msg_control to pass vhost zerocopy ubuf info to skb */
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200284 if (zcopy_used) {
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000285 vq->heads[vq->upend_idx].id = head;
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000286 if (!vhost_net_tx_select_zcopy(net) ||
287 len < VHOST_GOODCOPY_LEN) {
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000288 /* copy don't need to wait for DMA done */
289 vq->heads[vq->upend_idx].len =
290 VHOST_DMA_DONE_LEN;
291 msg.msg_control = NULL;
292 msg.msg_controllen = 0;
293 ubufs = NULL;
294 } else {
Michael S. Tsirkin46aa92d2013-03-17 02:46:09 +0000295 struct ubuf_info *ubuf;
296 ubuf = vq->ubuf_info + vq->upend_idx;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000297
Michael S. Tsirkin70e4cb92012-11-01 09:16:37 +0000298 vq->heads[vq->upend_idx].len =
299 VHOST_DMA_IN_PROGRESS;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000300 ubuf->callback = vhost_zerocopy_callback;
Michael S. Tsirkinca8f4fb2012-04-09 00:24:02 +0000301 ubuf->ctx = vq->ubufs;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000302 ubuf->desc = vq->upend_idx;
303 msg.msg_control = ubuf;
304 msg.msg_controllen = sizeof(ubuf);
305 ubufs = vq->ubufs;
306 kref_get(&ubufs->kref);
307 }
308 vq->upend_idx = (vq->upend_idx + 1) % UIO_MAXIOV;
309 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000310 /* TODO: Check specific error and bomb out unless ENOBUFS? */
311 err = sock->ops->sendmsg(NULL, sock, &msg, len);
312 if (unlikely(err < 0)) {
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200313 if (zcopy_used) {
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000314 if (ubufs)
315 vhost_ubuf_put(ubufs);
316 vq->upend_idx = ((unsigned)vq->upend_idx - 1) %
317 UIO_MAXIOV;
318 }
David Stevens8dd014a2010-07-27 18:52:21 +0300319 vhost_discard_vq_desc(vq, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000320 break;
321 }
322 if (err != len)
Michael S. Tsirkin95c0ec62010-06-24 17:10:25 +0300323 pr_debug("Truncated TX packet: "
324 " len %d != %zd\n", err, len);
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200325 if (!zcopy_used)
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000326 vhost_add_used_and_signal(&net->dev, vq, head, 0);
Jason Wangc8fb2172012-05-02 11:42:41 +0800327 else
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000328 vhost_zerocopy_signal_used(net, vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000329 total_len += len;
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000330 vhost_net_tx_packet(net);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000331 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
332 vhost_poll_queue(&vq->poll);
333 break;
334 }
335 }
336
337 mutex_unlock(&vq->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000338}
339
David Stevens8dd014a2010-07-27 18:52:21 +0300340static int peek_head_len(struct sock *sk)
341{
342 struct sk_buff *head;
343 int len = 0;
Jason Wang783e3982011-01-17 16:11:17 +0800344 unsigned long flags;
David Stevens8dd014a2010-07-27 18:52:21 +0300345
Jason Wang783e3982011-01-17 16:11:17 +0800346 spin_lock_irqsave(&sk->sk_receive_queue.lock, flags);
David Stevens8dd014a2010-07-27 18:52:21 +0300347 head = skb_peek(&sk->sk_receive_queue);
Basil Gorc53cff5e2012-05-03 22:55:23 +0000348 if (likely(head)) {
David Stevens8dd014a2010-07-27 18:52:21 +0300349 len = head->len;
Basil Gorc53cff5e2012-05-03 22:55:23 +0000350 if (vlan_tx_tag_present(head))
351 len += VLAN_HLEN;
352 }
353
Jason Wang783e3982011-01-17 16:11:17 +0800354 spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags);
David Stevens8dd014a2010-07-27 18:52:21 +0300355 return len;
356}
357
358/* This is a multi-buffer version of vhost_get_desc, that works if
359 * vq has read descriptors only.
360 * @vq - the relevant virtqueue
361 * @datalen - data length we'll be reading
362 * @iovcount - returned count of io vectors we fill
363 * @log - vhost log
364 * @log_num - log offset
Jason Wang94249362011-01-17 16:11:08 +0800365 * @quota - headcount quota, 1 for big buffer
David Stevens8dd014a2010-07-27 18:52:21 +0300366 * returns number of buffer heads allocated, negative on error
367 */
368static int get_rx_bufs(struct vhost_virtqueue *vq,
369 struct vring_used_elem *heads,
370 int datalen,
371 unsigned *iovcount,
372 struct vhost_log *log,
Jason Wang94249362011-01-17 16:11:08 +0800373 unsigned *log_num,
374 unsigned int quota)
David Stevens8dd014a2010-07-27 18:52:21 +0300375{
376 unsigned int out, in;
377 int seg = 0;
378 int headcount = 0;
379 unsigned d;
380 int r, nlogs = 0;
381
Jason Wang94249362011-01-17 16:11:08 +0800382 while (datalen > 0 && headcount < quota) {
Jason Wange0e9b402010-09-14 23:53:05 +0800383 if (unlikely(seg >= UIO_MAXIOV)) {
David Stevens8dd014a2010-07-27 18:52:21 +0300384 r = -ENOBUFS;
385 goto err;
386 }
387 d = vhost_get_vq_desc(vq->dev, vq, vq->iov + seg,
388 ARRAY_SIZE(vq->iov) - seg, &out,
389 &in, log, log_num);
390 if (d == vq->num) {
391 r = 0;
392 goto err;
393 }
394 if (unlikely(out || in <= 0)) {
395 vq_err(vq, "unexpected descriptor format for RX: "
396 "out %d, in %d\n", out, in);
397 r = -EINVAL;
398 goto err;
399 }
400 if (unlikely(log)) {
401 nlogs += *log_num;
402 log += *log_num;
403 }
404 heads[headcount].id = d;
405 heads[headcount].len = iov_length(vq->iov + seg, in);
406 datalen -= heads[headcount].len;
407 ++headcount;
408 seg += in;
409 }
410 heads[headcount - 1].len += datalen;
411 *iovcount = seg;
412 if (unlikely(log))
413 *log_num = nlogs;
414 return headcount;
415err:
416 vhost_discard_vq_desc(vq, headcount);
417 return r;
418}
419
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000420/* Expects to be always run from workqueue - which acts as
421 * read-size critical section for our kind of RCU. */
Jason Wang94249362011-01-17 16:11:08 +0800422static void handle_rx(struct vhost_net *net)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000423{
Asias He3ab2e422013-04-27 11:16:48 +0800424 struct vhost_virtqueue *vq = &net->vqs[VHOST_NET_VQ_RX].vq;
David Stevens8dd014a2010-07-27 18:52:21 +0300425 unsigned uninitialized_var(in), log;
426 struct vhost_log *vq_log;
427 struct msghdr msg = {
428 .msg_name = NULL,
429 .msg_namelen = 0,
430 .msg_control = NULL, /* FIXME: get and handle RX aux data. */
431 .msg_controllen = 0,
432 .msg_iov = vq->iov,
433 .msg_flags = MSG_DONTWAIT,
434 };
David Stevens8dd014a2010-07-27 18:52:21 +0300435 struct virtio_net_hdr_mrg_rxbuf hdr = {
436 .hdr.flags = 0,
437 .hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE
438 };
David Stevens8dd014a2010-07-27 18:52:21 +0300439 size_t total_len = 0;
Michael S. Tsirkin910a5782012-10-24 20:37:51 +0200440 int err, mergeable;
441 s16 headcount;
David Stevens8dd014a2010-07-27 18:52:21 +0300442 size_t vhost_hlen, sock_hlen;
443 size_t vhost_len, sock_len;
Michael S. Tsirkin5e182472011-01-18 13:04:43 +0200444 /* TODO: check that we are running from vhost_worker? */
445 struct socket *sock = rcu_dereference_check(vq->private_data, 1);
Krishna Kumard47effe2011-03-01 17:06:37 +0530446
Michael S. Tsirkinde4d7682011-03-13 23:00:52 +0200447 if (!sock)
David Stevens8dd014a2010-07-27 18:52:21 +0300448 return;
449
David Stevens8dd014a2010-07-27 18:52:21 +0300450 mutex_lock(&vq->mutex);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300451 vhost_disable_notify(&net->dev, vq);
David Stevens8dd014a2010-07-27 18:52:21 +0300452 vhost_hlen = vq->vhost_hlen;
453 sock_hlen = vq->sock_hlen;
454
455 vq_log = unlikely(vhost_has_feature(&net->dev, VHOST_F_LOG_ALL)) ?
456 vq->log : NULL;
Jason Wangcfbdab92011-01-17 16:10:59 +0800457 mergeable = vhost_has_feature(&net->dev, VIRTIO_NET_F_MRG_RXBUF);
David Stevens8dd014a2010-07-27 18:52:21 +0300458
459 while ((sock_len = peek_head_len(sock->sk))) {
460 sock_len += sock_hlen;
461 vhost_len = sock_len + vhost_hlen;
462 headcount = get_rx_bufs(vq, vq->heads, vhost_len,
Jason Wang94249362011-01-17 16:11:08 +0800463 &in, vq_log, &log,
464 likely(mergeable) ? UIO_MAXIOV : 1);
David Stevens8dd014a2010-07-27 18:52:21 +0300465 /* On error, stop handling until the next kick. */
466 if (unlikely(headcount < 0))
467 break;
468 /* OK, now we need to know about added descriptors. */
469 if (!headcount) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300470 if (unlikely(vhost_enable_notify(&net->dev, vq))) {
David Stevens8dd014a2010-07-27 18:52:21 +0300471 /* They have slipped one in as we were
472 * doing that: check again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300473 vhost_disable_notify(&net->dev, vq);
David Stevens8dd014a2010-07-27 18:52:21 +0300474 continue;
475 }
476 /* Nothing new? Wait for eventfd to tell us
477 * they refilled. */
478 break;
479 }
480 /* We don't need to be notified again. */
481 if (unlikely((vhost_hlen)))
482 /* Skip header. TODO: support TSO. */
483 move_iovec_hdr(vq->iov, vq->hdr, vhost_hlen, in);
484 else
485 /* Copy the header for use in VIRTIO_NET_F_MRG_RXBUF:
Jason Wanga290aec2010-11-29 13:48:40 +0800486 * needed because recvmsg can modify msg_iov. */
David Stevens8dd014a2010-07-27 18:52:21 +0300487 copy_iovec_hdr(vq->iov, vq->hdr, sock_hlen, in);
488 msg.msg_iovlen = in;
489 err = sock->ops->recvmsg(NULL, sock, &msg,
490 sock_len, MSG_DONTWAIT | MSG_TRUNC);
491 /* Userspace might have consumed the packet meanwhile:
492 * it's not supposed to do this usually, but might be hard
493 * to prevent. Discard data we got (if any) and keep going. */
494 if (unlikely(err != sock_len)) {
495 pr_debug("Discarded rx packet: "
496 " len %d, expected %zd\n", err, sock_len);
497 vhost_discard_vq_desc(vq, headcount);
498 continue;
499 }
500 if (unlikely(vhost_hlen) &&
501 memcpy_toiovecend(vq->hdr, (unsigned char *)&hdr, 0,
502 vhost_hlen)) {
503 vq_err(vq, "Unable to write vnet_hdr at addr %p\n",
504 vq->iov->iov_base);
505 break;
506 }
507 /* TODO: Should check and handle checksum. */
Jason Wangcfbdab92011-01-17 16:10:59 +0800508 if (likely(mergeable) &&
David Stevens8dd014a2010-07-27 18:52:21 +0300509 memcpy_toiovecend(vq->hdr, (unsigned char *)&headcount,
510 offsetof(typeof(hdr), num_buffers),
511 sizeof hdr.num_buffers)) {
512 vq_err(vq, "Failed num_buffers write");
513 vhost_discard_vq_desc(vq, headcount);
514 break;
515 }
516 vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
517 headcount);
518 if (unlikely(vq_log))
519 vhost_log_write(vq, vq_log, log, vhost_len);
520 total_len += vhost_len;
521 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
522 vhost_poll_queue(&vq->poll);
523 break;
524 }
525 }
526
527 mutex_unlock(&vq->mutex);
David Stevens8dd014a2010-07-27 18:52:21 +0300528}
529
Tejun Heoc23f34452010-06-02 20:40:00 +0200530static void handle_tx_kick(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000531{
Tejun Heoc23f34452010-06-02 20:40:00 +0200532 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
533 poll.work);
534 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
535
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000536 handle_tx(net);
537}
538
Tejun Heoc23f34452010-06-02 20:40:00 +0200539static void handle_rx_kick(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000540{
Tejun Heoc23f34452010-06-02 20:40:00 +0200541 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
542 poll.work);
543 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
544
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000545 handle_rx(net);
546}
547
Tejun Heoc23f34452010-06-02 20:40:00 +0200548static void handle_tx_net(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000549{
Tejun Heoc23f34452010-06-02 20:40:00 +0200550 struct vhost_net *net = container_of(work, struct vhost_net,
551 poll[VHOST_NET_VQ_TX].work);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000552 handle_tx(net);
553}
554
Tejun Heoc23f34452010-06-02 20:40:00 +0200555static void handle_rx_net(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000556{
Tejun Heoc23f34452010-06-02 20:40:00 +0200557 struct vhost_net *net = container_of(work, struct vhost_net,
558 poll[VHOST_NET_VQ_RX].work);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000559 handle_rx(net);
560}
561
562static int vhost_net_open(struct inode *inode, struct file *f)
563{
564 struct vhost_net *n = kmalloc(sizeof *n, GFP_KERNEL);
Tejun Heoc23f34452010-06-02 20:40:00 +0200565 struct vhost_dev *dev;
Asias He3ab2e422013-04-27 11:16:48 +0800566 struct vhost_virtqueue **vqs;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000567 int r;
Tejun Heoc23f34452010-06-02 20:40:00 +0200568
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000569 if (!n)
570 return -ENOMEM;
Asias He3ab2e422013-04-27 11:16:48 +0800571 vqs = kmalloc(VHOST_NET_VQ_MAX * sizeof(*vqs), GFP_KERNEL);
572 if (!vqs) {
573 kfree(n);
574 return -ENOMEM;
575 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200576
577 dev = &n->dev;
Asias He3ab2e422013-04-27 11:16:48 +0800578 vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq;
579 vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq;
580 n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick;
581 n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick;
582 r = vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000583 if (r < 0) {
584 kfree(n);
Asias He3ab2e422013-04-27 11:16:48 +0800585 kfree(vqs);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000586 return r;
587 }
588
Tejun Heoc23f34452010-06-02 20:40:00 +0200589 vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
590 vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000591
592 f->private_data = n;
593
594 return 0;
595}
596
597static void vhost_net_disable_vq(struct vhost_net *n,
598 struct vhost_virtqueue *vq)
599{
Asias He3ab2e422013-04-27 11:16:48 +0800600 struct vhost_net_virtqueue *nvq =
601 container_of(vq, struct vhost_net_virtqueue, vq);
602 struct vhost_poll *poll = n->poll + (nvq - n->vqs);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000603 if (!vq->private_data)
604 return;
Jason Wang70181d512013-04-10 20:50:48 +0000605 vhost_poll_stop(poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000606}
607
Jason Wang2b8b3282013-01-28 01:05:18 +0000608static int vhost_net_enable_vq(struct vhost_net *n,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000609 struct vhost_virtqueue *vq)
610{
Asias He3ab2e422013-04-27 11:16:48 +0800611 struct vhost_net_virtqueue *nvq =
612 container_of(vq, struct vhost_net_virtqueue, vq);
613 struct vhost_poll *poll = n->poll + (nvq - n->vqs);
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100614 struct socket *sock;
615
616 sock = rcu_dereference_protected(vq->private_data,
617 lockdep_is_held(&vq->mutex));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000618 if (!sock)
Jason Wang2b8b3282013-01-28 01:05:18 +0000619 return 0;
Jason Wang2b8b3282013-01-28 01:05:18 +0000620
Jason Wang70181d512013-04-10 20:50:48 +0000621 return vhost_poll_start(poll, sock->file);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000622}
623
624static struct socket *vhost_net_stop_vq(struct vhost_net *n,
625 struct vhost_virtqueue *vq)
626{
627 struct socket *sock;
628
629 mutex_lock(&vq->mutex);
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100630 sock = rcu_dereference_protected(vq->private_data,
631 lockdep_is_held(&vq->mutex));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000632 vhost_net_disable_vq(n, vq);
633 rcu_assign_pointer(vq->private_data, NULL);
634 mutex_unlock(&vq->mutex);
635 return sock;
636}
637
638static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
639 struct socket **rx_sock)
640{
Asias He3ab2e422013-04-27 11:16:48 +0800641 *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq);
642 *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000643}
644
645static void vhost_net_flush_vq(struct vhost_net *n, int index)
646{
647 vhost_poll_flush(n->poll + index);
Asias He3ab2e422013-04-27 11:16:48 +0800648 vhost_poll_flush(&n->vqs[index].vq.poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000649}
650
651static void vhost_net_flush(struct vhost_net *n)
652{
653 vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
654 vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
Asias He3ab2e422013-04-27 11:16:48 +0800655 if (n->vqs[VHOST_NET_VQ_TX].vq.ubufs) {
656 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200657 n->tx_flush = true;
Asias He3ab2e422013-04-27 11:16:48 +0800658 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200659 /* Wait for all lower device DMAs done. */
Asias He3ab2e422013-04-27 11:16:48 +0800660 vhost_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].vq.ubufs);
661 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200662 n->tx_flush = false;
Asias He3ab2e422013-04-27 11:16:48 +0800663 kref_init(&n->vqs[VHOST_NET_VQ_TX].vq.ubufs->kref);
664 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200665 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000666}
667
668static int vhost_net_release(struct inode *inode, struct file *f)
669{
670 struct vhost_net *n = f->private_data;
671 struct socket *tx_sock;
672 struct socket *rx_sock;
673
674 vhost_net_stop(n, &tx_sock, &rx_sock);
675 vhost_net_flush(n);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000676 vhost_dev_stop(&n->dev);
Michael S. Tsirkinea5d4042011-11-27 19:05:58 +0200677 vhost_dev_cleanup(&n->dev, false);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000678 if (tx_sock)
679 fput(tx_sock->file);
680 if (rx_sock)
681 fput(rx_sock->file);
682 /* We do an extra flush before freeing memory,
683 * since jobs can re-queue themselves. */
684 vhost_net_flush(n);
Asias He3ab2e422013-04-27 11:16:48 +0800685 kfree(n->dev.vqs);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000686 kfree(n);
687 return 0;
688}
689
690static struct socket *get_raw_socket(int fd)
691{
692 struct {
693 struct sockaddr_ll sa;
694 char buf[MAX_ADDR_LEN];
695 } uaddr;
696 int uaddr_len = sizeof uaddr, r;
697 struct socket *sock = sockfd_lookup(fd, &r);
Krishna Kumard47effe2011-03-01 17:06:37 +0530698
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000699 if (!sock)
700 return ERR_PTR(-ENOTSOCK);
701
702 /* Parameter checking */
703 if (sock->sk->sk_type != SOCK_RAW) {
704 r = -ESOCKTNOSUPPORT;
705 goto err;
706 }
707
708 r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa,
709 &uaddr_len, 0);
710 if (r)
711 goto err;
712
713 if (uaddr.sa.sll_family != AF_PACKET) {
714 r = -EPFNOSUPPORT;
715 goto err;
716 }
717 return sock;
718err:
719 fput(sock->file);
720 return ERR_PTR(r);
721}
722
Arnd Bergmann501c7742010-02-18 05:46:50 +0000723static struct socket *get_tap_socket(int fd)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000724{
725 struct file *file = fget(fd);
726 struct socket *sock;
Krishna Kumard47effe2011-03-01 17:06:37 +0530727
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000728 if (!file)
729 return ERR_PTR(-EBADF);
730 sock = tun_get_socket(file);
Arnd Bergmann501c7742010-02-18 05:46:50 +0000731 if (!IS_ERR(sock))
732 return sock;
733 sock = macvtap_get_socket(file);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000734 if (IS_ERR(sock))
735 fput(file);
736 return sock;
737}
738
739static struct socket *get_socket(int fd)
740{
741 struct socket *sock;
Krishna Kumard47effe2011-03-01 17:06:37 +0530742
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000743 /* special case to disable backend */
744 if (fd == -1)
745 return NULL;
746 sock = get_raw_socket(fd);
747 if (!IS_ERR(sock))
748 return sock;
Arnd Bergmann501c7742010-02-18 05:46:50 +0000749 sock = get_tap_socket(fd);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000750 if (!IS_ERR(sock))
751 return sock;
752 return ERR_PTR(-ENOTSOCK);
753}
754
755static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
756{
757 struct socket *sock, *oldsock;
758 struct vhost_virtqueue *vq;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000759 struct vhost_ubuf_ref *ubufs, *oldubufs = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000760 int r;
761
762 mutex_lock(&n->dev.mutex);
763 r = vhost_dev_check_owner(&n->dev);
764 if (r)
765 goto err;
766
767 if (index >= VHOST_NET_VQ_MAX) {
768 r = -ENOBUFS;
769 goto err;
770 }
Asias He3ab2e422013-04-27 11:16:48 +0800771 vq = &n->vqs[index].vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000772 mutex_lock(&vq->mutex);
773
774 /* Verify that ring has been setup correctly. */
775 if (!vhost_vq_access_ok(vq)) {
776 r = -EFAULT;
Jeff Dike1dace8c2010-03-04 16:10:14 -0500777 goto err_vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000778 }
779 sock = get_socket(fd);
780 if (IS_ERR(sock)) {
781 r = PTR_ERR(sock);
Jeff Dike1dace8c2010-03-04 16:10:14 -0500782 goto err_vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000783 }
784
785 /* start polling new socket */
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100786 oldsock = rcu_dereference_protected(vq->private_data,
787 lockdep_is_held(&vq->mutex));
David S. Miller11fe8832010-07-20 18:25:24 -0700788 if (sock != oldsock) {
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000789 ubufs = vhost_ubuf_alloc(vq, sock && vhost_sock_zcopy(sock));
790 if (IS_ERR(ubufs)) {
791 r = PTR_ERR(ubufs);
792 goto err_ubufs;
793 }
Jason Wang692a9982013-01-28 01:05:17 +0000794
Krishna Kumard47effe2011-03-01 17:06:37 +0530795 vhost_net_disable_vq(n, vq);
796 rcu_assign_pointer(vq->private_data, sock);
Jason Wangf59281d2011-06-21 18:04:27 +0800797 r = vhost_init_used(vq);
798 if (r)
Jason Wang692a9982013-01-28 01:05:17 +0000799 goto err_used;
Jason Wang2b8b3282013-01-28 01:05:18 +0000800 r = vhost_net_enable_vq(n, vq);
801 if (r)
802 goto err_used;
Jason Wang692a9982013-01-28 01:05:17 +0000803
804 oldubufs = vq->ubufs;
805 vq->ubufs = ubufs;
Michael S. Tsirkin64e9a9b2012-12-03 07:31:51 +0000806
807 n->tx_packets = 0;
808 n->tx_zcopy_err = 0;
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200809 n->tx_flush = false;
Jeff Dikedd1f4072010-03-04 16:10:14 -0500810 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000811
Michael S. Tsirkin1680e902010-07-15 15:19:12 +0300812 mutex_unlock(&vq->mutex);
813
Michael S. Tsirkinc047e5f2011-07-20 13:41:31 +0300814 if (oldubufs) {
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000815 vhost_ubuf_put_and_wait(oldubufs);
Michael S. Tsirkinc047e5f2011-07-20 13:41:31 +0300816 mutex_lock(&vq->mutex);
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000817 vhost_zerocopy_signal_used(n, vq);
Michael S. Tsirkinc047e5f2011-07-20 13:41:31 +0300818 mutex_unlock(&vq->mutex);
819 }
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000820
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000821 if (oldsock) {
822 vhost_net_flush_vq(n, index);
823 fput(oldsock->file);
824 }
Jeff Dike1dace8c2010-03-04 16:10:14 -0500825
Michael S. Tsirkin1680e902010-07-15 15:19:12 +0300826 mutex_unlock(&n->dev.mutex);
827 return 0;
828
Jason Wang692a9982013-01-28 01:05:17 +0000829err_used:
830 rcu_assign_pointer(vq->private_data, oldsock);
831 vhost_net_enable_vq(n, vq);
832 if (ubufs)
833 vhost_ubuf_put_and_wait(ubufs);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000834err_ubufs:
835 fput(sock->file);
Jeff Dike1dace8c2010-03-04 16:10:14 -0500836err_vq:
837 mutex_unlock(&vq->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000838err:
839 mutex_unlock(&n->dev.mutex);
840 return r;
841}
842
843static long vhost_net_reset_owner(struct vhost_net *n)
844{
845 struct socket *tx_sock = NULL;
846 struct socket *rx_sock = NULL;
847 long err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530848
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000849 mutex_lock(&n->dev.mutex);
850 err = vhost_dev_check_owner(&n->dev);
851 if (err)
852 goto done;
853 vhost_net_stop(n, &tx_sock, &rx_sock);
854 vhost_net_flush(n);
855 err = vhost_dev_reset_owner(&n->dev);
856done:
857 mutex_unlock(&n->dev.mutex);
858 if (tx_sock)
859 fput(tx_sock->file);
860 if (rx_sock)
861 fput(rx_sock->file);
862 return err;
863}
864
865static int vhost_net_set_features(struct vhost_net *n, u64 features)
866{
David Stevens8dd014a2010-07-27 18:52:21 +0300867 size_t vhost_hlen, sock_hlen, hdr_len;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000868 int i;
David Stevens8dd014a2010-07-27 18:52:21 +0300869
870 hdr_len = (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ?
871 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
872 sizeof(struct virtio_net_hdr);
873 if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
874 /* vhost provides vnet_hdr */
875 vhost_hlen = hdr_len;
876 sock_hlen = 0;
877 } else {
878 /* socket provides vnet_hdr */
879 vhost_hlen = 0;
880 sock_hlen = hdr_len;
881 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000882 mutex_lock(&n->dev.mutex);
883 if ((features & (1 << VHOST_F_LOG_ALL)) &&
884 !vhost_log_access_ok(&n->dev)) {
885 mutex_unlock(&n->dev.mutex);
886 return -EFAULT;
887 }
888 n->dev.acked_features = features;
889 smp_wmb();
890 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
Asias He3ab2e422013-04-27 11:16:48 +0800891 mutex_lock(&n->vqs[i].vq.mutex);
892 n->vqs[i].vq.vhost_hlen = vhost_hlen;
893 n->vqs[i].vq.sock_hlen = sock_hlen;
894 mutex_unlock(&n->vqs[i].vq.mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000895 }
896 vhost_net_flush(n);
897 mutex_unlock(&n->dev.mutex);
898 return 0;
899}
900
901static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
902 unsigned long arg)
903{
904 struct vhost_net *n = f->private_data;
905 void __user *argp = (void __user *)arg;
906 u64 __user *featurep = argp;
907 struct vhost_vring_file backend;
908 u64 features;
909 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +0530910
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000911 switch (ioctl) {
912 case VHOST_NET_SET_BACKEND:
Takuya Yoshikawad3553a52010-05-27 19:01:58 +0900913 if (copy_from_user(&backend, argp, sizeof backend))
914 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000915 return vhost_net_set_backend(n, backend.index, backend.fd);
916 case VHOST_GET_FEATURES:
Stefan Hajnoczi0dd05a32012-07-21 06:55:36 +0000917 features = VHOST_NET_FEATURES;
Takuya Yoshikawad3553a52010-05-27 19:01:58 +0900918 if (copy_to_user(featurep, &features, sizeof features))
919 return -EFAULT;
920 return 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000921 case VHOST_SET_FEATURES:
Takuya Yoshikawad3553a52010-05-27 19:01:58 +0900922 if (copy_from_user(&features, featurep, sizeof features))
923 return -EFAULT;
Stefan Hajnoczi0dd05a32012-07-21 06:55:36 +0000924 if (features & ~VHOST_NET_FEATURES)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000925 return -EOPNOTSUPP;
926 return vhost_net_set_features(n, features);
927 case VHOST_RESET_OWNER:
928 return vhost_net_reset_owner(n);
929 default:
930 mutex_lock(&n->dev.mutex);
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200931 r = vhost_dev_ioctl(&n->dev, ioctl, argp);
932 if (r == -ENOIOCTLCMD)
933 r = vhost_vring_ioctl(&n->dev, ioctl, argp);
934 else
935 vhost_net_flush(n);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000936 mutex_unlock(&n->dev.mutex);
937 return r;
938 }
939}
940
941#ifdef CONFIG_COMPAT
942static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
943 unsigned long arg)
944{
945 return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
946}
947#endif
948
Tobias Klauser373a83a2010-05-17 15:12:49 +0200949static const struct file_operations vhost_net_fops = {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000950 .owner = THIS_MODULE,
951 .release = vhost_net_release,
952 .unlocked_ioctl = vhost_net_ioctl,
953#ifdef CONFIG_COMPAT
954 .compat_ioctl = vhost_net_compat_ioctl,
955#endif
956 .open = vhost_net_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200957 .llseek = noop_llseek,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000958};
959
960static struct miscdevice vhost_net_misc = {
stephen hemminger7c7c7f02012-01-11 19:30:38 +0000961 .minor = VHOST_NET_MINOR,
962 .name = "vhost-net",
963 .fops = &vhost_net_fops,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000964};
965
Christoph Hellwiga8d37822010-04-13 14:11:25 -0400966static int vhost_net_init(void)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000967{
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000968 if (experimental_zcopytx)
969 vhost_enable_zcopy(VHOST_NET_VQ_TX);
Tejun Heoc23f34452010-06-02 20:40:00 +0200970 return misc_register(&vhost_net_misc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000971}
972module_init(vhost_net_init);
973
Christoph Hellwiga8d37822010-04-13 14:11:25 -0400974static void vhost_net_exit(void)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000975{
976 misc_deregister(&vhost_net_misc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000977}
978module_exit(vhost_net_exit);
979
980MODULE_VERSION("0.0.1");
981MODULE_LICENSE("GPL v2");
982MODULE_AUTHOR("Michael S. Tsirkin");
983MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
stephen hemminger7c7c7f02012-01-11 19:30:38 +0000984MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR);
985MODULE_ALIAS("devname:vhost-net");