blob: 87c216c1e54ede98ac69c8bc36d9e9fa9c940170 [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
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000067struct vhost_net {
68 struct vhost_dev dev;
69 struct vhost_virtqueue vqs[VHOST_NET_VQ_MAX];
70 struct vhost_poll poll[VHOST_NET_VQ_MAX];
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000071 /* Number of TX recently submitted.
72 * Protected by tx vq lock. */
73 unsigned tx_packets;
74 /* Number of times zerocopy TX recently failed.
75 * Protected by tx vq lock. */
76 unsigned tx_zcopy_err;
Michael S. Tsirkin1280c272012-12-04 00:17:14 +020077 /* Flush in progress. Protected by tx vq lock. */
78 bool tx_flush;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000079};
80
Michael S. Tsirkineaae8132012-11-01 09:16:51 +000081static void vhost_net_tx_packet(struct vhost_net *net)
82{
83 ++net->tx_packets;
84 if (net->tx_packets < 1024)
85 return;
86 net->tx_packets = 0;
87 net->tx_zcopy_err = 0;
88}
89
90static void vhost_net_tx_err(struct vhost_net *net)
91{
92 ++net->tx_zcopy_err;
93}
94
95static bool vhost_net_tx_select_zcopy(struct vhost_net *net)
96{
Michael S. Tsirkin1280c272012-12-04 00:17:14 +020097 /* TX flush waits for outstanding DMAs to be done.
98 * Don't start new DMAs.
99 */
100 return !net->tx_flush &&
101 net->tx_packets / 64 >= net->tx_zcopy_err;
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000102}
103
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000104static bool vhost_sock_zcopy(struct socket *sock)
105{
106 return unlikely(experimental_zcopytx) &&
107 sock_flag(sock->sk, SOCK_ZEROCOPY);
108}
109
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000110/* Pop first len bytes from iovec. Return number of segments used. */
111static int move_iovec_hdr(struct iovec *from, struct iovec *to,
112 size_t len, int iov_count)
113{
114 int seg = 0;
115 size_t size;
Krishna Kumard47effe2011-03-01 17:06:37 +0530116
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000117 while (len && seg < iov_count) {
118 size = min(from->iov_len, len);
119 to->iov_base = from->iov_base;
120 to->iov_len = size;
121 from->iov_len -= size;
122 from->iov_base += size;
123 len -= size;
124 ++from;
125 ++to;
126 ++seg;
127 }
128 return seg;
129}
David Stevens8dd014a2010-07-27 18:52:21 +0300130/* Copy iovec entries for len bytes from iovec. */
131static void copy_iovec_hdr(const struct iovec *from, struct iovec *to,
132 size_t len, int iovcount)
133{
134 int seg = 0;
135 size_t size;
Krishna Kumard47effe2011-03-01 17:06:37 +0530136
David Stevens8dd014a2010-07-27 18:52:21 +0300137 while (len && seg < iovcount) {
138 size = min(from->iov_len, len);
139 to->iov_base = from->iov_base;
140 to->iov_len = size;
141 len -= size;
142 ++from;
143 ++to;
144 ++seg;
145 }
146}
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000147
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000148/* In case of DMA done not in order in lower device driver for some reason.
149 * upend_idx is used to track end of used idx, done_idx is used to track head
150 * of used idx. Once lower device DMA done contiguously, we will signal KVM
151 * guest used idx.
152 */
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000153static int vhost_zerocopy_signal_used(struct vhost_net *net,
154 struct vhost_virtqueue *vq)
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000155{
156 int i;
157 int j = 0;
158
159 for (i = vq->done_idx; i != vq->upend_idx; i = (i + 1) % UIO_MAXIOV) {
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000160 if (vq->heads[i].len == VHOST_DMA_FAILED_LEN)
161 vhost_net_tx_err(net);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000162 if (VHOST_DMA_IS_DONE(vq->heads[i].len)) {
163 vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
164 vhost_add_used_and_signal(vq->dev, vq,
165 vq->heads[i].id, 0);
166 ++j;
167 } else
168 break;
169 }
170 if (j)
171 vq->done_idx = i;
172 return j;
173}
174
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000175static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000176{
177 struct vhost_ubuf_ref *ubufs = ubuf->ctx;
178 struct vhost_virtqueue *vq = ubufs->vq;
Michael S. Tsirkin24eb21a2012-11-01 09:16:55 +0000179 int cnt = atomic_read(&ubufs->kref.refcount);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000180
Michael S. Tsirkin24eb21a2012-11-01 09:16:55 +0000181 /*
182 * Trigger polling thread if guest stopped submitting new buffers:
183 * in this case, the refcount after decrement will eventually reach 1
184 * so here it is 2.
185 * We also trigger polling periodically after each 16 packets
186 * (the value 16 here is more or less arbitrary, it's tuned to trigger
187 * less than 10% of times).
188 */
189 if (cnt <= 2 || !(cnt % 16))
190 vhost_poll_queue(&vq->poll);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000191 /* set len to mark this desc buffers done DMA */
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000192 vq->heads[ubuf->desc].len = success ?
193 VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000194 vhost_ubuf_put(ubufs);
195}
196
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000197/* Expects to be always run from workqueue - which acts as
198 * read-size critical section for our kind of RCU. */
199static void handle_tx(struct vhost_net *net)
200{
201 struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_TX];
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300202 unsigned out, in, s;
203 int head;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000204 struct msghdr msg = {
205 .msg_name = NULL,
206 .msg_namelen = 0,
207 .msg_control = NULL,
208 .msg_controllen = 0,
209 .msg_iov = vq->iov,
210 .msg_flags = MSG_DONTWAIT,
211 };
212 size_t len, total_len = 0;
Jason Wang70181d512013-04-10 20:50:48 +0000213 int err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000214 size_t hdr_size;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100215 struct socket *sock;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000216 struct vhost_ubuf_ref *uninitialized_var(ubufs);
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200217 bool zcopy, zcopy_used;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100218
Michael S. Tsirkin5e182472011-01-18 13:04:43 +0200219 /* TODO: check that we are running from vhost_worker? */
Michael S. Tsirkin11cd1a82010-11-14 17:31:52 +0200220 sock = rcu_dereference_check(vq->private_data, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000221 if (!sock)
222 return;
223
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000224 mutex_lock(&vq->mutex);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300225 vhost_disable_notify(&net->dev, vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000226
David Stevens8dd014a2010-07-27 18:52:21 +0300227 hdr_size = vq->vhost_hlen;
Jason Wangc460f052012-05-02 11:42:23 +0800228 zcopy = vq->ubufs;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000229
230 for (;;) {
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000231 /* Release DMAs done buffers first */
232 if (zcopy)
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000233 vhost_zerocopy_signal_used(net, vq);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000234
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000235 head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
236 ARRAY_SIZE(vq->iov),
237 &out, &in,
238 NULL, NULL);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300239 /* On error, stop handling until the next kick. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300240 if (unlikely(head < 0))
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300241 break;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000242 /* Nothing new? Wait for eventfd to tell us they refilled. */
243 if (head == vq->num) {
Shirley Ma9e380822011-07-20 10:23:12 -0700244 int num_pends;
245
Shirley Ma9e380822011-07-20 10:23:12 -0700246 /* If more outstanding DMAs, queue the work.
247 * Handle upend_idx wrap around
248 */
249 num_pends = likely(vq->upend_idx >= vq->done_idx) ?
250 (vq->upend_idx - vq->done_idx) :
251 (vq->upend_idx + UIO_MAXIOV - vq->done_idx);
Jason Wang70181d512013-04-10 20:50:48 +0000252 if (unlikely(num_pends > VHOST_MAX_PEND))
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000253 break;
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300254 if (unlikely(vhost_enable_notify(&net->dev, vq))) {
255 vhost_disable_notify(&net->dev, vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000256 continue;
257 }
258 break;
259 }
260 if (in) {
261 vq_err(vq, "Unexpected descriptor format for TX: "
262 "out %d, int %d\n", out, in);
263 break;
264 }
265 /* Skip header. TODO: support TSO. */
266 s = move_iovec_hdr(vq->iov, vq->hdr, hdr_size, out);
267 msg.msg_iovlen = out;
268 len = iov_length(vq->iov, out);
269 /* Sanity check */
270 if (!len) {
271 vq_err(vq, "Unexpected header len for TX: "
272 "%zd expected %zd\n",
273 iov_length(vq->hdr, s), hdr_size);
274 break;
275 }
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200276 zcopy_used = zcopy && (len >= VHOST_GOODCOPY_LEN ||
277 vq->upend_idx != vq->done_idx);
278
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000279 /* use msg_control to pass vhost zerocopy ubuf info to skb */
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200280 if (zcopy_used) {
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000281 vq->heads[vq->upend_idx].id = head;
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000282 if (!vhost_net_tx_select_zcopy(net) ||
283 len < VHOST_GOODCOPY_LEN) {
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000284 /* copy don't need to wait for DMA done */
285 vq->heads[vq->upend_idx].len =
286 VHOST_DMA_DONE_LEN;
287 msg.msg_control = NULL;
288 msg.msg_controllen = 0;
289 ubufs = NULL;
290 } else {
Michael S. Tsirkin46aa92d2013-03-17 02:46:09 +0000291 struct ubuf_info *ubuf;
292 ubuf = vq->ubuf_info + vq->upend_idx;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000293
Michael S. Tsirkin70e4cb92012-11-01 09:16:37 +0000294 vq->heads[vq->upend_idx].len =
295 VHOST_DMA_IN_PROGRESS;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000296 ubuf->callback = vhost_zerocopy_callback;
Michael S. Tsirkinca8f4fb2012-04-09 00:24:02 +0000297 ubuf->ctx = vq->ubufs;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000298 ubuf->desc = vq->upend_idx;
299 msg.msg_control = ubuf;
300 msg.msg_controllen = sizeof(ubuf);
301 ubufs = vq->ubufs;
302 kref_get(&ubufs->kref);
303 }
304 vq->upend_idx = (vq->upend_idx + 1) % UIO_MAXIOV;
305 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000306 /* TODO: Check specific error and bomb out unless ENOBUFS? */
307 err = sock->ops->sendmsg(NULL, sock, &msg, len);
308 if (unlikely(err < 0)) {
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200309 if (zcopy_used) {
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000310 if (ubufs)
311 vhost_ubuf_put(ubufs);
312 vq->upend_idx = ((unsigned)vq->upend_idx - 1) %
313 UIO_MAXIOV;
314 }
David Stevens8dd014a2010-07-27 18:52:21 +0300315 vhost_discard_vq_desc(vq, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000316 break;
317 }
318 if (err != len)
Michael S. Tsirkin95c0ec62010-06-24 17:10:25 +0300319 pr_debug("Truncated TX packet: "
320 " len %d != %zd\n", err, len);
Michael S. Tsirkincedb9bd2012-12-06 17:00:18 +0200321 if (!zcopy_used)
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000322 vhost_add_used_and_signal(&net->dev, vq, head, 0);
Jason Wangc8fb2172012-05-02 11:42:41 +0800323 else
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000324 vhost_zerocopy_signal_used(net, vq);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000325 total_len += len;
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000326 vhost_net_tx_packet(net);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000327 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
328 vhost_poll_queue(&vq->poll);
329 break;
330 }
331 }
332
333 mutex_unlock(&vq->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000334}
335
David Stevens8dd014a2010-07-27 18:52:21 +0300336static int peek_head_len(struct sock *sk)
337{
338 struct sk_buff *head;
339 int len = 0;
Jason Wang783e3982011-01-17 16:11:17 +0800340 unsigned long flags;
David Stevens8dd014a2010-07-27 18:52:21 +0300341
Jason Wang783e3982011-01-17 16:11:17 +0800342 spin_lock_irqsave(&sk->sk_receive_queue.lock, flags);
David Stevens8dd014a2010-07-27 18:52:21 +0300343 head = skb_peek(&sk->sk_receive_queue);
Basil Gorc53cff5e2012-05-03 22:55:23 +0000344 if (likely(head)) {
David Stevens8dd014a2010-07-27 18:52:21 +0300345 len = head->len;
Basil Gorc53cff5e2012-05-03 22:55:23 +0000346 if (vlan_tx_tag_present(head))
347 len += VLAN_HLEN;
348 }
349
Jason Wang783e3982011-01-17 16:11:17 +0800350 spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags);
David Stevens8dd014a2010-07-27 18:52:21 +0300351 return len;
352}
353
354/* This is a multi-buffer version of vhost_get_desc, that works if
355 * vq has read descriptors only.
356 * @vq - the relevant virtqueue
357 * @datalen - data length we'll be reading
358 * @iovcount - returned count of io vectors we fill
359 * @log - vhost log
360 * @log_num - log offset
Jason Wang94249362011-01-17 16:11:08 +0800361 * @quota - headcount quota, 1 for big buffer
David Stevens8dd014a2010-07-27 18:52:21 +0300362 * returns number of buffer heads allocated, negative on error
363 */
364static int get_rx_bufs(struct vhost_virtqueue *vq,
365 struct vring_used_elem *heads,
366 int datalen,
367 unsigned *iovcount,
368 struct vhost_log *log,
Jason Wang94249362011-01-17 16:11:08 +0800369 unsigned *log_num,
370 unsigned int quota)
David Stevens8dd014a2010-07-27 18:52:21 +0300371{
372 unsigned int out, in;
373 int seg = 0;
374 int headcount = 0;
375 unsigned d;
376 int r, nlogs = 0;
377
Jason Wang94249362011-01-17 16:11:08 +0800378 while (datalen > 0 && headcount < quota) {
Jason Wange0e9b402010-09-14 23:53:05 +0800379 if (unlikely(seg >= UIO_MAXIOV)) {
David Stevens8dd014a2010-07-27 18:52:21 +0300380 r = -ENOBUFS;
381 goto err;
382 }
383 d = vhost_get_vq_desc(vq->dev, vq, vq->iov + seg,
384 ARRAY_SIZE(vq->iov) - seg, &out,
385 &in, log, log_num);
386 if (d == vq->num) {
387 r = 0;
388 goto err;
389 }
390 if (unlikely(out || in <= 0)) {
391 vq_err(vq, "unexpected descriptor format for RX: "
392 "out %d, in %d\n", out, in);
393 r = -EINVAL;
394 goto err;
395 }
396 if (unlikely(log)) {
397 nlogs += *log_num;
398 log += *log_num;
399 }
400 heads[headcount].id = d;
401 heads[headcount].len = iov_length(vq->iov + seg, in);
402 datalen -= heads[headcount].len;
403 ++headcount;
404 seg += in;
405 }
406 heads[headcount - 1].len += datalen;
407 *iovcount = seg;
408 if (unlikely(log))
409 *log_num = nlogs;
410 return headcount;
411err:
412 vhost_discard_vq_desc(vq, headcount);
413 return r;
414}
415
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000416/* Expects to be always run from workqueue - which acts as
417 * read-size critical section for our kind of RCU. */
Jason Wang94249362011-01-17 16:11:08 +0800418static void handle_rx(struct vhost_net *net)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000419{
David Stevens8dd014a2010-07-27 18:52:21 +0300420 struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_RX];
421 unsigned uninitialized_var(in), log;
422 struct vhost_log *vq_log;
423 struct msghdr msg = {
424 .msg_name = NULL,
425 .msg_namelen = 0,
426 .msg_control = NULL, /* FIXME: get and handle RX aux data. */
427 .msg_controllen = 0,
428 .msg_iov = vq->iov,
429 .msg_flags = MSG_DONTWAIT,
430 };
David Stevens8dd014a2010-07-27 18:52:21 +0300431 struct virtio_net_hdr_mrg_rxbuf hdr = {
432 .hdr.flags = 0,
433 .hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE
434 };
David Stevens8dd014a2010-07-27 18:52:21 +0300435 size_t total_len = 0;
Michael S. Tsirkin910a5782012-10-24 20:37:51 +0200436 int err, mergeable;
437 s16 headcount;
David Stevens8dd014a2010-07-27 18:52:21 +0300438 size_t vhost_hlen, sock_hlen;
439 size_t vhost_len, sock_len;
Michael S. Tsirkin5e182472011-01-18 13:04:43 +0200440 /* TODO: check that we are running from vhost_worker? */
441 struct socket *sock = rcu_dereference_check(vq->private_data, 1);
Krishna Kumard47effe2011-03-01 17:06:37 +0530442
Michael S. Tsirkinde4d7682011-03-13 23:00:52 +0200443 if (!sock)
David Stevens8dd014a2010-07-27 18:52:21 +0300444 return;
445
David Stevens8dd014a2010-07-27 18:52:21 +0300446 mutex_lock(&vq->mutex);
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300447 vhost_disable_notify(&net->dev, vq);
David Stevens8dd014a2010-07-27 18:52:21 +0300448 vhost_hlen = vq->vhost_hlen;
449 sock_hlen = vq->sock_hlen;
450
451 vq_log = unlikely(vhost_has_feature(&net->dev, VHOST_F_LOG_ALL)) ?
452 vq->log : NULL;
Jason Wangcfbdab92011-01-17 16:10:59 +0800453 mergeable = vhost_has_feature(&net->dev, VIRTIO_NET_F_MRG_RXBUF);
David Stevens8dd014a2010-07-27 18:52:21 +0300454
455 while ((sock_len = peek_head_len(sock->sk))) {
456 sock_len += sock_hlen;
457 vhost_len = sock_len + vhost_hlen;
458 headcount = get_rx_bufs(vq, vq->heads, vhost_len,
Jason Wang94249362011-01-17 16:11:08 +0800459 &in, vq_log, &log,
460 likely(mergeable) ? UIO_MAXIOV : 1);
David Stevens8dd014a2010-07-27 18:52:21 +0300461 /* On error, stop handling until the next kick. */
462 if (unlikely(headcount < 0))
463 break;
464 /* OK, now we need to know about added descriptors. */
465 if (!headcount) {
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300466 if (unlikely(vhost_enable_notify(&net->dev, vq))) {
David Stevens8dd014a2010-07-27 18:52:21 +0300467 /* They have slipped one in as we were
468 * doing that: check again. */
Michael S. Tsirkin8ea8cf82011-05-20 02:10:54 +0300469 vhost_disable_notify(&net->dev, vq);
David Stevens8dd014a2010-07-27 18:52:21 +0300470 continue;
471 }
472 /* Nothing new? Wait for eventfd to tell us
473 * they refilled. */
474 break;
475 }
476 /* We don't need to be notified again. */
477 if (unlikely((vhost_hlen)))
478 /* Skip header. TODO: support TSO. */
479 move_iovec_hdr(vq->iov, vq->hdr, vhost_hlen, in);
480 else
481 /* Copy the header for use in VIRTIO_NET_F_MRG_RXBUF:
Jason Wanga290aec2010-11-29 13:48:40 +0800482 * needed because recvmsg can modify msg_iov. */
David Stevens8dd014a2010-07-27 18:52:21 +0300483 copy_iovec_hdr(vq->iov, vq->hdr, sock_hlen, in);
484 msg.msg_iovlen = in;
485 err = sock->ops->recvmsg(NULL, sock, &msg,
486 sock_len, MSG_DONTWAIT | MSG_TRUNC);
487 /* Userspace might have consumed the packet meanwhile:
488 * it's not supposed to do this usually, but might be hard
489 * to prevent. Discard data we got (if any) and keep going. */
490 if (unlikely(err != sock_len)) {
491 pr_debug("Discarded rx packet: "
492 " len %d, expected %zd\n", err, sock_len);
493 vhost_discard_vq_desc(vq, headcount);
494 continue;
495 }
496 if (unlikely(vhost_hlen) &&
497 memcpy_toiovecend(vq->hdr, (unsigned char *)&hdr, 0,
498 vhost_hlen)) {
499 vq_err(vq, "Unable to write vnet_hdr at addr %p\n",
500 vq->iov->iov_base);
501 break;
502 }
503 /* TODO: Should check and handle checksum. */
Jason Wangcfbdab92011-01-17 16:10:59 +0800504 if (likely(mergeable) &&
David Stevens8dd014a2010-07-27 18:52:21 +0300505 memcpy_toiovecend(vq->hdr, (unsigned char *)&headcount,
506 offsetof(typeof(hdr), num_buffers),
507 sizeof hdr.num_buffers)) {
508 vq_err(vq, "Failed num_buffers write");
509 vhost_discard_vq_desc(vq, headcount);
510 break;
511 }
512 vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
513 headcount);
514 if (unlikely(vq_log))
515 vhost_log_write(vq, vq_log, log, vhost_len);
516 total_len += vhost_len;
517 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
518 vhost_poll_queue(&vq->poll);
519 break;
520 }
521 }
522
523 mutex_unlock(&vq->mutex);
David Stevens8dd014a2010-07-27 18:52:21 +0300524}
525
Tejun Heoc23f34452010-06-02 20:40:00 +0200526static void handle_tx_kick(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000527{
Tejun Heoc23f34452010-06-02 20:40:00 +0200528 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
529 poll.work);
530 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
531
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000532 handle_tx(net);
533}
534
Tejun Heoc23f34452010-06-02 20:40:00 +0200535static void handle_rx_kick(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000536{
Tejun Heoc23f34452010-06-02 20:40:00 +0200537 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
538 poll.work);
539 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
540
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000541 handle_rx(net);
542}
543
Tejun Heoc23f34452010-06-02 20:40:00 +0200544static void handle_tx_net(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000545{
Tejun Heoc23f34452010-06-02 20:40:00 +0200546 struct vhost_net *net = container_of(work, struct vhost_net,
547 poll[VHOST_NET_VQ_TX].work);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000548 handle_tx(net);
549}
550
Tejun Heoc23f34452010-06-02 20:40:00 +0200551static void handle_rx_net(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000552{
Tejun Heoc23f34452010-06-02 20:40:00 +0200553 struct vhost_net *net = container_of(work, struct vhost_net,
554 poll[VHOST_NET_VQ_RX].work);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000555 handle_rx(net);
556}
557
558static int vhost_net_open(struct inode *inode, struct file *f)
559{
560 struct vhost_net *n = kmalloc(sizeof *n, GFP_KERNEL);
Tejun Heoc23f34452010-06-02 20:40:00 +0200561 struct vhost_dev *dev;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000562 int r;
Tejun Heoc23f34452010-06-02 20:40:00 +0200563
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000564 if (!n)
565 return -ENOMEM;
Tejun Heoc23f34452010-06-02 20:40:00 +0200566
567 dev = &n->dev;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000568 n->vqs[VHOST_NET_VQ_TX].handle_kick = handle_tx_kick;
569 n->vqs[VHOST_NET_VQ_RX].handle_kick = handle_rx_kick;
Tejun Heoc23f34452010-06-02 20:40:00 +0200570 r = vhost_dev_init(dev, n->vqs, VHOST_NET_VQ_MAX);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000571 if (r < 0) {
572 kfree(n);
573 return r;
574 }
575
Tejun Heoc23f34452010-06-02 20:40:00 +0200576 vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
577 vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000578
579 f->private_data = n;
580
581 return 0;
582}
583
584static void vhost_net_disable_vq(struct vhost_net *n,
585 struct vhost_virtqueue *vq)
586{
Jason Wang70181d512013-04-10 20:50:48 +0000587 struct vhost_poll *poll = n->poll + (vq - n->vqs);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000588 if (!vq->private_data)
589 return;
Jason Wang70181d512013-04-10 20:50:48 +0000590 vhost_poll_stop(poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000591}
592
Jason Wang2b8b3282013-01-28 01:05:18 +0000593static int vhost_net_enable_vq(struct vhost_net *n,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000594 struct vhost_virtqueue *vq)
595{
Jason Wang70181d512013-04-10 20:50:48 +0000596 struct vhost_poll *poll = n->poll + (vq - n->vqs);
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100597 struct socket *sock;
598
599 sock = rcu_dereference_protected(vq->private_data,
600 lockdep_is_held(&vq->mutex));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000601 if (!sock)
Jason Wang2b8b3282013-01-28 01:05:18 +0000602 return 0;
Jason Wang2b8b3282013-01-28 01:05:18 +0000603
Jason Wang70181d512013-04-10 20:50:48 +0000604 return vhost_poll_start(poll, sock->file);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000605}
606
607static struct socket *vhost_net_stop_vq(struct vhost_net *n,
608 struct vhost_virtqueue *vq)
609{
610 struct socket *sock;
611
612 mutex_lock(&vq->mutex);
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100613 sock = rcu_dereference_protected(vq->private_data,
614 lockdep_is_held(&vq->mutex));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000615 vhost_net_disable_vq(n, vq);
616 rcu_assign_pointer(vq->private_data, NULL);
617 mutex_unlock(&vq->mutex);
618 return sock;
619}
620
621static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
622 struct socket **rx_sock)
623{
624 *tx_sock = vhost_net_stop_vq(n, n->vqs + VHOST_NET_VQ_TX);
625 *rx_sock = vhost_net_stop_vq(n, n->vqs + VHOST_NET_VQ_RX);
626}
627
628static void vhost_net_flush_vq(struct vhost_net *n, int index)
629{
630 vhost_poll_flush(n->poll + index);
631 vhost_poll_flush(&n->dev.vqs[index].poll);
632}
633
634static void vhost_net_flush(struct vhost_net *n)
635{
636 vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
637 vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200638 if (n->dev.vqs[VHOST_NET_VQ_TX].ubufs) {
639 mutex_lock(&n->dev.vqs[VHOST_NET_VQ_TX].mutex);
640 n->tx_flush = true;
641 mutex_unlock(&n->dev.vqs[VHOST_NET_VQ_TX].mutex);
642 /* Wait for all lower device DMAs done. */
643 vhost_ubuf_put_and_wait(n->dev.vqs[VHOST_NET_VQ_TX].ubufs);
644 mutex_lock(&n->dev.vqs[VHOST_NET_VQ_TX].mutex);
645 n->tx_flush = false;
646 kref_init(&n->dev.vqs[VHOST_NET_VQ_TX].ubufs->kref);
647 mutex_unlock(&n->dev.vqs[VHOST_NET_VQ_TX].mutex);
648 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000649}
650
651static int vhost_net_release(struct inode *inode, struct file *f)
652{
653 struct vhost_net *n = f->private_data;
654 struct socket *tx_sock;
655 struct socket *rx_sock;
656
657 vhost_net_stop(n, &tx_sock, &rx_sock);
658 vhost_net_flush(n);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000659 vhost_dev_stop(&n->dev);
Michael S. Tsirkinea5d4042011-11-27 19:05:58 +0200660 vhost_dev_cleanup(&n->dev, false);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000661 if (tx_sock)
662 fput(tx_sock->file);
663 if (rx_sock)
664 fput(rx_sock->file);
665 /* We do an extra flush before freeing memory,
666 * since jobs can re-queue themselves. */
667 vhost_net_flush(n);
668 kfree(n);
669 return 0;
670}
671
672static struct socket *get_raw_socket(int fd)
673{
674 struct {
675 struct sockaddr_ll sa;
676 char buf[MAX_ADDR_LEN];
677 } uaddr;
678 int uaddr_len = sizeof uaddr, r;
679 struct socket *sock = sockfd_lookup(fd, &r);
Krishna Kumard47effe2011-03-01 17:06:37 +0530680
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000681 if (!sock)
682 return ERR_PTR(-ENOTSOCK);
683
684 /* Parameter checking */
685 if (sock->sk->sk_type != SOCK_RAW) {
686 r = -ESOCKTNOSUPPORT;
687 goto err;
688 }
689
690 r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa,
691 &uaddr_len, 0);
692 if (r)
693 goto err;
694
695 if (uaddr.sa.sll_family != AF_PACKET) {
696 r = -EPFNOSUPPORT;
697 goto err;
698 }
699 return sock;
700err:
701 fput(sock->file);
702 return ERR_PTR(r);
703}
704
Arnd Bergmann501c7742010-02-18 05:46:50 +0000705static struct socket *get_tap_socket(int fd)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000706{
707 struct file *file = fget(fd);
708 struct socket *sock;
Krishna Kumard47effe2011-03-01 17:06:37 +0530709
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000710 if (!file)
711 return ERR_PTR(-EBADF);
712 sock = tun_get_socket(file);
Arnd Bergmann501c7742010-02-18 05:46:50 +0000713 if (!IS_ERR(sock))
714 return sock;
715 sock = macvtap_get_socket(file);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000716 if (IS_ERR(sock))
717 fput(file);
718 return sock;
719}
720
721static struct socket *get_socket(int fd)
722{
723 struct socket *sock;
Krishna Kumard47effe2011-03-01 17:06:37 +0530724
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000725 /* special case to disable backend */
726 if (fd == -1)
727 return NULL;
728 sock = get_raw_socket(fd);
729 if (!IS_ERR(sock))
730 return sock;
Arnd Bergmann501c7742010-02-18 05:46:50 +0000731 sock = get_tap_socket(fd);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000732 if (!IS_ERR(sock))
733 return sock;
734 return ERR_PTR(-ENOTSOCK);
735}
736
737static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
738{
739 struct socket *sock, *oldsock;
740 struct vhost_virtqueue *vq;
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000741 struct vhost_ubuf_ref *ubufs, *oldubufs = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000742 int r;
743
744 mutex_lock(&n->dev.mutex);
745 r = vhost_dev_check_owner(&n->dev);
746 if (r)
747 goto err;
748
749 if (index >= VHOST_NET_VQ_MAX) {
750 r = -ENOBUFS;
751 goto err;
752 }
753 vq = n->vqs + index;
754 mutex_lock(&vq->mutex);
755
756 /* Verify that ring has been setup correctly. */
757 if (!vhost_vq_access_ok(vq)) {
758 r = -EFAULT;
Jeff Dike1dace8c2010-03-04 16:10:14 -0500759 goto err_vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000760 }
761 sock = get_socket(fd);
762 if (IS_ERR(sock)) {
763 r = PTR_ERR(sock);
Jeff Dike1dace8c2010-03-04 16:10:14 -0500764 goto err_vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000765 }
766
767 /* start polling new socket */
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100768 oldsock = rcu_dereference_protected(vq->private_data,
769 lockdep_is_held(&vq->mutex));
David S. Miller11fe8832010-07-20 18:25:24 -0700770 if (sock != oldsock) {
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000771 ubufs = vhost_ubuf_alloc(vq, sock && vhost_sock_zcopy(sock));
772 if (IS_ERR(ubufs)) {
773 r = PTR_ERR(ubufs);
774 goto err_ubufs;
775 }
Jason Wang692a9982013-01-28 01:05:17 +0000776
Krishna Kumard47effe2011-03-01 17:06:37 +0530777 vhost_net_disable_vq(n, vq);
778 rcu_assign_pointer(vq->private_data, sock);
Jason Wangf59281d2011-06-21 18:04:27 +0800779 r = vhost_init_used(vq);
780 if (r)
Jason Wang692a9982013-01-28 01:05:17 +0000781 goto err_used;
Jason Wang2b8b3282013-01-28 01:05:18 +0000782 r = vhost_net_enable_vq(n, vq);
783 if (r)
784 goto err_used;
Jason Wang692a9982013-01-28 01:05:17 +0000785
786 oldubufs = vq->ubufs;
787 vq->ubufs = ubufs;
Michael S. Tsirkin64e9a9b2012-12-03 07:31:51 +0000788
789 n->tx_packets = 0;
790 n->tx_zcopy_err = 0;
Michael S. Tsirkin1280c272012-12-04 00:17:14 +0200791 n->tx_flush = false;
Jeff Dikedd1f4072010-03-04 16:10:14 -0500792 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000793
Michael S. Tsirkin1680e902010-07-15 15:19:12 +0300794 mutex_unlock(&vq->mutex);
795
Michael S. Tsirkinc047e5f2011-07-20 13:41:31 +0300796 if (oldubufs) {
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000797 vhost_ubuf_put_and_wait(oldubufs);
Michael S. Tsirkinc047e5f2011-07-20 13:41:31 +0300798 mutex_lock(&vq->mutex);
Michael S. Tsirkineaae8132012-11-01 09:16:51 +0000799 vhost_zerocopy_signal_used(n, vq);
Michael S. Tsirkinc047e5f2011-07-20 13:41:31 +0300800 mutex_unlock(&vq->mutex);
801 }
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000802
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000803 if (oldsock) {
804 vhost_net_flush_vq(n, index);
805 fput(oldsock->file);
806 }
Jeff Dike1dace8c2010-03-04 16:10:14 -0500807
Michael S. Tsirkin1680e902010-07-15 15:19:12 +0300808 mutex_unlock(&n->dev.mutex);
809 return 0;
810
Jason Wang692a9982013-01-28 01:05:17 +0000811err_used:
812 rcu_assign_pointer(vq->private_data, oldsock);
813 vhost_net_enable_vq(n, vq);
814 if (ubufs)
815 vhost_ubuf_put_and_wait(ubufs);
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000816err_ubufs:
817 fput(sock->file);
Jeff Dike1dace8c2010-03-04 16:10:14 -0500818err_vq:
819 mutex_unlock(&vq->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000820err:
821 mutex_unlock(&n->dev.mutex);
822 return r;
823}
824
825static long vhost_net_reset_owner(struct vhost_net *n)
826{
827 struct socket *tx_sock = NULL;
828 struct socket *rx_sock = NULL;
829 long err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530830
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000831 mutex_lock(&n->dev.mutex);
832 err = vhost_dev_check_owner(&n->dev);
833 if (err)
834 goto done;
835 vhost_net_stop(n, &tx_sock, &rx_sock);
836 vhost_net_flush(n);
837 err = vhost_dev_reset_owner(&n->dev);
838done:
839 mutex_unlock(&n->dev.mutex);
840 if (tx_sock)
841 fput(tx_sock->file);
842 if (rx_sock)
843 fput(rx_sock->file);
844 return err;
845}
846
847static int vhost_net_set_features(struct vhost_net *n, u64 features)
848{
David Stevens8dd014a2010-07-27 18:52:21 +0300849 size_t vhost_hlen, sock_hlen, hdr_len;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000850 int i;
David Stevens8dd014a2010-07-27 18:52:21 +0300851
852 hdr_len = (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ?
853 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
854 sizeof(struct virtio_net_hdr);
855 if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
856 /* vhost provides vnet_hdr */
857 vhost_hlen = hdr_len;
858 sock_hlen = 0;
859 } else {
860 /* socket provides vnet_hdr */
861 vhost_hlen = 0;
862 sock_hlen = hdr_len;
863 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000864 mutex_lock(&n->dev.mutex);
865 if ((features & (1 << VHOST_F_LOG_ALL)) &&
866 !vhost_log_access_ok(&n->dev)) {
867 mutex_unlock(&n->dev.mutex);
868 return -EFAULT;
869 }
870 n->dev.acked_features = features;
871 smp_wmb();
872 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
873 mutex_lock(&n->vqs[i].mutex);
David Stevens8dd014a2010-07-27 18:52:21 +0300874 n->vqs[i].vhost_hlen = vhost_hlen;
875 n->vqs[i].sock_hlen = sock_hlen;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000876 mutex_unlock(&n->vqs[i].mutex);
877 }
878 vhost_net_flush(n);
879 mutex_unlock(&n->dev.mutex);
880 return 0;
881}
882
883static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
884 unsigned long arg)
885{
886 struct vhost_net *n = f->private_data;
887 void __user *argp = (void __user *)arg;
888 u64 __user *featurep = argp;
889 struct vhost_vring_file backend;
890 u64 features;
891 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +0530892
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000893 switch (ioctl) {
894 case VHOST_NET_SET_BACKEND:
Takuya Yoshikawad3553a52010-05-27 19:01:58 +0900895 if (copy_from_user(&backend, argp, sizeof backend))
896 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000897 return vhost_net_set_backend(n, backend.index, backend.fd);
898 case VHOST_GET_FEATURES:
Stefan Hajnoczi0dd05a32012-07-21 06:55:36 +0000899 features = VHOST_NET_FEATURES;
Takuya Yoshikawad3553a52010-05-27 19:01:58 +0900900 if (copy_to_user(featurep, &features, sizeof features))
901 return -EFAULT;
902 return 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000903 case VHOST_SET_FEATURES:
Takuya Yoshikawad3553a52010-05-27 19:01:58 +0900904 if (copy_from_user(&features, featurep, sizeof features))
905 return -EFAULT;
Stefan Hajnoczi0dd05a32012-07-21 06:55:36 +0000906 if (features & ~VHOST_NET_FEATURES)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000907 return -EOPNOTSUPP;
908 return vhost_net_set_features(n, features);
909 case VHOST_RESET_OWNER:
910 return vhost_net_reset_owner(n);
911 default:
912 mutex_lock(&n->dev.mutex);
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200913 r = vhost_dev_ioctl(&n->dev, ioctl, argp);
914 if (r == -ENOIOCTLCMD)
915 r = vhost_vring_ioctl(&n->dev, ioctl, argp);
916 else
917 vhost_net_flush(n);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000918 mutex_unlock(&n->dev.mutex);
919 return r;
920 }
921}
922
923#ifdef CONFIG_COMPAT
924static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
925 unsigned long arg)
926{
927 return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
928}
929#endif
930
Tobias Klauser373a83a2010-05-17 15:12:49 +0200931static const struct file_operations vhost_net_fops = {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000932 .owner = THIS_MODULE,
933 .release = vhost_net_release,
934 .unlocked_ioctl = vhost_net_ioctl,
935#ifdef CONFIG_COMPAT
936 .compat_ioctl = vhost_net_compat_ioctl,
937#endif
938 .open = vhost_net_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200939 .llseek = noop_llseek,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000940};
941
942static struct miscdevice vhost_net_misc = {
stephen hemminger7c7c7f02012-01-11 19:30:38 +0000943 .minor = VHOST_NET_MINOR,
944 .name = "vhost-net",
945 .fops = &vhost_net_fops,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000946};
947
Christoph Hellwiga8d37822010-04-13 14:11:25 -0400948static int vhost_net_init(void)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000949{
Michael S. Tsirkinbab632d2011-07-18 03:48:46 +0000950 if (experimental_zcopytx)
951 vhost_enable_zcopy(VHOST_NET_VQ_TX);
Tejun Heoc23f34452010-06-02 20:40:00 +0200952 return misc_register(&vhost_net_misc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000953}
954module_init(vhost_net_init);
955
Christoph Hellwiga8d37822010-04-13 14:11:25 -0400956static void vhost_net_exit(void)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000957{
958 misc_deregister(&vhost_net_misc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000959}
960module_exit(vhost_net_exit);
961
962MODULE_VERSION("0.0.1");
963MODULE_LICENSE("GPL v2");
964MODULE_AUTHOR("Michael S. Tsirkin");
965MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
stephen hemminger7c7c7f02012-01-11 19:30:38 +0000966MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR);
967MODULE_ALIAS("devname:vhost-net");