blob: 9f57cd45fe8ffcd8da040c4b41cb09f77b16a74c [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>
15#include <linux/mutex.h>
16#include <linux/workqueue.h>
17#include <linux/rcupdate.h>
18#include <linux/file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000020
21#include <linux/net.h>
22#include <linux/if_packet.h>
23#include <linux/if_arp.h>
24#include <linux/if_tun.h>
Arnd Bergmann501c7742010-02-18 05:46:50 +000025#include <linux/if_macvlan.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000026
27#include <net/sock.h>
28
29#include "vhost.h"
30
31/* Max number of bytes transferred before requeueing the job.
32 * Using this limit prevents one virtqueue from starving others. */
33#define VHOST_NET_WEIGHT 0x80000
34
35enum {
36 VHOST_NET_VQ_RX = 0,
37 VHOST_NET_VQ_TX = 1,
38 VHOST_NET_VQ_MAX = 2,
39};
40
41enum vhost_net_poll_state {
42 VHOST_NET_POLL_DISABLED = 0,
43 VHOST_NET_POLL_STARTED = 1,
44 VHOST_NET_POLL_STOPPED = 2,
45};
46
47struct vhost_net {
48 struct vhost_dev dev;
49 struct vhost_virtqueue vqs[VHOST_NET_VQ_MAX];
50 struct vhost_poll poll[VHOST_NET_VQ_MAX];
51 /* Tells us whether we are polling a socket for TX.
52 * We only do this when socket buffer fills up.
53 * Protected by tx vq lock. */
54 enum vhost_net_poll_state tx_poll_state;
55};
56
57/* Pop first len bytes from iovec. Return number of segments used. */
58static int move_iovec_hdr(struct iovec *from, struct iovec *to,
59 size_t len, int iov_count)
60{
61 int seg = 0;
62 size_t size;
Krishna Kumard47effe2011-03-01 17:06:37 +053063
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000064 while (len && seg < iov_count) {
65 size = min(from->iov_len, len);
66 to->iov_base = from->iov_base;
67 to->iov_len = size;
68 from->iov_len -= size;
69 from->iov_base += size;
70 len -= size;
71 ++from;
72 ++to;
73 ++seg;
74 }
75 return seg;
76}
David Stevens8dd014a2010-07-27 18:52:21 +030077/* Copy iovec entries for len bytes from iovec. */
78static void copy_iovec_hdr(const struct iovec *from, struct iovec *to,
79 size_t len, int iovcount)
80{
81 int seg = 0;
82 size_t size;
Krishna Kumard47effe2011-03-01 17:06:37 +053083
David Stevens8dd014a2010-07-27 18:52:21 +030084 while (len && seg < iovcount) {
85 size = min(from->iov_len, len);
86 to->iov_base = from->iov_base;
87 to->iov_len = size;
88 len -= size;
89 ++from;
90 ++to;
91 ++seg;
92 }
93}
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000094
95/* Caller must have TX VQ lock */
96static void tx_poll_stop(struct vhost_net *net)
97{
98 if (likely(net->tx_poll_state != VHOST_NET_POLL_STARTED))
99 return;
100 vhost_poll_stop(net->poll + VHOST_NET_VQ_TX);
101 net->tx_poll_state = VHOST_NET_POLL_STOPPED;
102}
103
104/* Caller must have TX VQ lock */
105static void tx_poll_start(struct vhost_net *net, struct socket *sock)
106{
107 if (unlikely(net->tx_poll_state != VHOST_NET_POLL_STOPPED))
108 return;
109 vhost_poll_start(net->poll + VHOST_NET_VQ_TX, sock->file);
110 net->tx_poll_state = VHOST_NET_POLL_STARTED;
111}
112
113/* Expects to be always run from workqueue - which acts as
114 * read-size critical section for our kind of RCU. */
115static void handle_tx(struct vhost_net *net)
116{
117 struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_TX];
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300118 unsigned out, in, s;
119 int head;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000120 struct msghdr msg = {
121 .msg_name = NULL,
122 .msg_namelen = 0,
123 .msg_control = NULL,
124 .msg_controllen = 0,
125 .msg_iov = vq->iov,
126 .msg_flags = MSG_DONTWAIT,
127 };
128 size_t len, total_len = 0;
129 int err, wmem;
130 size_t hdr_size;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100131 struct socket *sock;
132
Michael S. Tsirkin5e182472011-01-18 13:04:43 +0200133 /* TODO: check that we are running from vhost_worker? */
Michael S. Tsirkin11cd1a82010-11-14 17:31:52 +0200134 sock = rcu_dereference_check(vq->private_data, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000135 if (!sock)
136 return;
137
138 wmem = atomic_read(&sock->sk->sk_wmem_alloc);
Sridhar Samudrala39286fa2010-02-28 19:39:16 +0200139 if (wmem >= sock->sk->sk_sndbuf) {
140 mutex_lock(&vq->mutex);
141 tx_poll_start(net, sock);
142 mutex_unlock(&vq->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000143 return;
Sridhar Samudrala39286fa2010-02-28 19:39:16 +0200144 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000145
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000146 mutex_lock(&vq->mutex);
147 vhost_disable_notify(vq);
148
Michael S. Tsirkin0e255572010-03-08 23:24:22 +0200149 if (wmem < sock->sk->sk_sndbuf / 2)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000150 tx_poll_stop(net);
David Stevens8dd014a2010-07-27 18:52:21 +0300151 hdr_size = vq->vhost_hlen;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000152
153 for (;;) {
154 head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
155 ARRAY_SIZE(vq->iov),
156 &out, &in,
157 NULL, NULL);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300158 /* On error, stop handling until the next kick. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300159 if (unlikely(head < 0))
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300160 break;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000161 /* Nothing new? Wait for eventfd to tell us they refilled. */
162 if (head == vq->num) {
163 wmem = atomic_read(&sock->sk->sk_wmem_alloc);
164 if (wmem >= sock->sk->sk_sndbuf * 3 / 4) {
165 tx_poll_start(net, sock);
166 set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
167 break;
168 }
169 if (unlikely(vhost_enable_notify(vq))) {
170 vhost_disable_notify(vq);
171 continue;
172 }
173 break;
174 }
175 if (in) {
176 vq_err(vq, "Unexpected descriptor format for TX: "
177 "out %d, int %d\n", out, in);
178 break;
179 }
180 /* Skip header. TODO: support TSO. */
181 s = move_iovec_hdr(vq->iov, vq->hdr, hdr_size, out);
182 msg.msg_iovlen = out;
183 len = iov_length(vq->iov, out);
184 /* Sanity check */
185 if (!len) {
186 vq_err(vq, "Unexpected header len for TX: "
187 "%zd expected %zd\n",
188 iov_length(vq->hdr, s), hdr_size);
189 break;
190 }
191 /* TODO: Check specific error and bomb out unless ENOBUFS? */
192 err = sock->ops->sendmsg(NULL, sock, &msg, len);
193 if (unlikely(err < 0)) {
David Stevens8dd014a2010-07-27 18:52:21 +0300194 vhost_discard_vq_desc(vq, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000195 tx_poll_start(net, sock);
196 break;
197 }
198 if (err != len)
Michael S. Tsirkin95c0ec62010-06-24 17:10:25 +0300199 pr_debug("Truncated TX packet: "
200 " len %d != %zd\n", err, len);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000201 vhost_add_used_and_signal(&net->dev, vq, head, 0);
202 total_len += len;
203 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
204 vhost_poll_queue(&vq->poll);
205 break;
206 }
207 }
208
209 mutex_unlock(&vq->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000210}
211
David Stevens8dd014a2010-07-27 18:52:21 +0300212static int peek_head_len(struct sock *sk)
213{
214 struct sk_buff *head;
215 int len = 0;
216
217 lock_sock(sk);
218 head = skb_peek(&sk->sk_receive_queue);
219 if (head)
220 len = head->len;
221 release_sock(sk);
222 return len;
223}
224
225/* This is a multi-buffer version of vhost_get_desc, that works if
226 * vq has read descriptors only.
227 * @vq - the relevant virtqueue
228 * @datalen - data length we'll be reading
229 * @iovcount - returned count of io vectors we fill
230 * @log - vhost log
231 * @log_num - log offset
232 * returns number of buffer heads allocated, negative on error
233 */
234static int get_rx_bufs(struct vhost_virtqueue *vq,
235 struct vring_used_elem *heads,
236 int datalen,
237 unsigned *iovcount,
238 struct vhost_log *log,
239 unsigned *log_num)
240{
241 unsigned int out, in;
242 int seg = 0;
243 int headcount = 0;
244 unsigned d;
245 int r, nlogs = 0;
246
247 while (datalen > 0) {
Jason Wange0e9b402010-09-14 23:53:05 +0800248 if (unlikely(seg >= UIO_MAXIOV)) {
David Stevens8dd014a2010-07-27 18:52:21 +0300249 r = -ENOBUFS;
250 goto err;
251 }
252 d = vhost_get_vq_desc(vq->dev, vq, vq->iov + seg,
253 ARRAY_SIZE(vq->iov) - seg, &out,
254 &in, log, log_num);
255 if (d == vq->num) {
256 r = 0;
257 goto err;
258 }
259 if (unlikely(out || in <= 0)) {
260 vq_err(vq, "unexpected descriptor format for RX: "
261 "out %d, in %d\n", out, in);
262 r = -EINVAL;
263 goto err;
264 }
265 if (unlikely(log)) {
266 nlogs += *log_num;
267 log += *log_num;
268 }
269 heads[headcount].id = d;
270 heads[headcount].len = iov_length(vq->iov + seg, in);
271 datalen -= heads[headcount].len;
272 ++headcount;
273 seg += in;
274 }
275 heads[headcount - 1].len += datalen;
276 *iovcount = seg;
277 if (unlikely(log))
278 *log_num = nlogs;
279 return headcount;
280err:
281 vhost_discard_vq_desc(vq, headcount);
282 return r;
283}
284
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000285/* Expects to be always run from workqueue - which acts as
286 * read-size critical section for our kind of RCU. */
David Stevens8dd014a2010-07-27 18:52:21 +0300287static void handle_rx_big(struct vhost_net *net)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000288{
289 struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_RX];
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300290 unsigned out, in, log, s;
291 int head;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000292 struct vhost_log *vq_log;
293 struct msghdr msg = {
294 .msg_name = NULL,
295 .msg_namelen = 0,
296 .msg_control = NULL, /* FIXME: get and handle RX aux data. */
297 .msg_controllen = 0,
298 .msg_iov = vq->iov,
299 .msg_flags = MSG_DONTWAIT,
300 };
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000301 struct virtio_net_hdr hdr = {
302 .flags = 0,
303 .gso_type = VIRTIO_NET_HDR_GSO_NONE
304 };
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000305 size_t len, total_len = 0;
306 int err;
307 size_t hdr_size;
Michael S. Tsirkin5e182472011-01-18 13:04:43 +0200308 /* TODO: check that we are running from vhost_worker? */
309 struct socket *sock = rcu_dereference_check(vq->private_data, 1);
Krishna Kumard47effe2011-03-01 17:06:37 +0530310
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000311 if (!sock || skb_queue_empty(&sock->sk->sk_receive_queue))
312 return;
313
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000314 mutex_lock(&vq->mutex);
315 vhost_disable_notify(vq);
David Stevens8dd014a2010-07-27 18:52:21 +0300316 hdr_size = vq->vhost_hlen;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000317
318 vq_log = unlikely(vhost_has_feature(&net->dev, VHOST_F_LOG_ALL)) ?
319 vq->log : NULL;
320
321 for (;;) {
322 head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
323 ARRAY_SIZE(vq->iov),
324 &out, &in,
325 vq_log, &log);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300326 /* On error, stop handling until the next kick. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300327 if (unlikely(head < 0))
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300328 break;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000329 /* OK, now we need to know about added descriptors. */
330 if (head == vq->num) {
331 if (unlikely(vhost_enable_notify(vq))) {
332 /* They have slipped one in as we were
333 * doing that: check again. */
334 vhost_disable_notify(vq);
335 continue;
336 }
337 /* Nothing new? Wait for eventfd to tell us
338 * they refilled. */
339 break;
340 }
341 /* We don't need to be notified again. */
342 if (out) {
343 vq_err(vq, "Unexpected descriptor format for RX: "
344 "out %d, int %d\n",
345 out, in);
346 break;
347 }
348 /* Skip header. TODO: support TSO/mergeable rx buffers. */
349 s = move_iovec_hdr(vq->iov, vq->hdr, hdr_size, in);
350 msg.msg_iovlen = in;
351 len = iov_length(vq->iov, in);
352 /* Sanity check */
353 if (!len) {
354 vq_err(vq, "Unexpected header len for RX: "
355 "%zd expected %zd\n",
356 iov_length(vq->hdr, s), hdr_size);
357 break;
358 }
359 err = sock->ops->recvmsg(NULL, sock, &msg,
360 len, MSG_DONTWAIT | MSG_TRUNC);
361 /* TODO: Check specific error and bomb out unless EAGAIN? */
362 if (err < 0) {
David Stevens8dd014a2010-07-27 18:52:21 +0300363 vhost_discard_vq_desc(vq, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000364 break;
365 }
366 /* TODO: Should check and handle checksum. */
367 if (err > len) {
Michael S. Tsirkin95c0ec62010-06-24 17:10:25 +0300368 pr_debug("Discarded truncated rx packet: "
369 " len %d > %zd\n", err, len);
David Stevens8dd014a2010-07-27 18:52:21 +0300370 vhost_discard_vq_desc(vq, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000371 continue;
372 }
373 len = err;
374 err = memcpy_toiovec(vq->hdr, (unsigned char *)&hdr, hdr_size);
375 if (err) {
376 vq_err(vq, "Unable to write vnet_hdr at addr %p: %d\n",
377 vq->iov->iov_base, err);
378 break;
379 }
380 len += hdr_size;
381 vhost_add_used_and_signal(&net->dev, vq, head, len);
382 if (unlikely(vq_log))
383 vhost_log_write(vq, vq_log, log, len);
384 total_len += len;
385 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
386 vhost_poll_queue(&vq->poll);
387 break;
388 }
389 }
390
391 mutex_unlock(&vq->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000392}
393
David Stevens8dd014a2010-07-27 18:52:21 +0300394/* Expects to be always run from workqueue - which acts as
395 * read-size critical section for our kind of RCU. */
396static void handle_rx_mergeable(struct vhost_net *net)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000397{
David Stevens8dd014a2010-07-27 18:52:21 +0300398 struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_RX];
399 unsigned uninitialized_var(in), log;
400 struct vhost_log *vq_log;
401 struct msghdr msg = {
402 .msg_name = NULL,
403 .msg_namelen = 0,
404 .msg_control = NULL, /* FIXME: get and handle RX aux data. */
405 .msg_controllen = 0,
406 .msg_iov = vq->iov,
407 .msg_flags = MSG_DONTWAIT,
408 };
David Stevens8dd014a2010-07-27 18:52:21 +0300409 struct virtio_net_hdr_mrg_rxbuf hdr = {
410 .hdr.flags = 0,
411 .hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE
412 };
David Stevens8dd014a2010-07-27 18:52:21 +0300413 size_t total_len = 0;
Jason Wangcfbdab92011-01-17 16:10:59 +0800414 int err, headcount, mergeable;
David Stevens8dd014a2010-07-27 18:52:21 +0300415 size_t vhost_hlen, sock_hlen;
416 size_t vhost_len, sock_len;
Michael S. Tsirkin5e182472011-01-18 13:04:43 +0200417 /* TODO: check that we are running from vhost_worker? */
418 struct socket *sock = rcu_dereference_check(vq->private_data, 1);
Krishna Kumard47effe2011-03-01 17:06:37 +0530419
David Stevens8dd014a2010-07-27 18:52:21 +0300420 if (!sock || skb_queue_empty(&sock->sk->sk_receive_queue))
421 return;
422
David Stevens8dd014a2010-07-27 18:52:21 +0300423 mutex_lock(&vq->mutex);
424 vhost_disable_notify(vq);
425 vhost_hlen = vq->vhost_hlen;
426 sock_hlen = vq->sock_hlen;
427
428 vq_log = unlikely(vhost_has_feature(&net->dev, VHOST_F_LOG_ALL)) ?
429 vq->log : NULL;
Jason Wangcfbdab92011-01-17 16:10:59 +0800430 mergeable = vhost_has_feature(&net->dev, VIRTIO_NET_F_MRG_RXBUF);
David Stevens8dd014a2010-07-27 18:52:21 +0300431
432 while ((sock_len = peek_head_len(sock->sk))) {
433 sock_len += sock_hlen;
434 vhost_len = sock_len + vhost_hlen;
435 headcount = get_rx_bufs(vq, vq->heads, vhost_len,
436 &in, vq_log, &log);
437 /* On error, stop handling until the next kick. */
438 if (unlikely(headcount < 0))
439 break;
440 /* OK, now we need to know about added descriptors. */
441 if (!headcount) {
442 if (unlikely(vhost_enable_notify(vq))) {
443 /* They have slipped one in as we were
444 * doing that: check again. */
445 vhost_disable_notify(vq);
446 continue;
447 }
448 /* Nothing new? Wait for eventfd to tell us
449 * they refilled. */
450 break;
451 }
452 /* We don't need to be notified again. */
453 if (unlikely((vhost_hlen)))
454 /* Skip header. TODO: support TSO. */
455 move_iovec_hdr(vq->iov, vq->hdr, vhost_hlen, in);
456 else
457 /* Copy the header for use in VIRTIO_NET_F_MRG_RXBUF:
Jason Wanga290aec2010-11-29 13:48:40 +0800458 * needed because recvmsg can modify msg_iov. */
David Stevens8dd014a2010-07-27 18:52:21 +0300459 copy_iovec_hdr(vq->iov, vq->hdr, sock_hlen, in);
460 msg.msg_iovlen = in;
461 err = sock->ops->recvmsg(NULL, sock, &msg,
462 sock_len, MSG_DONTWAIT | MSG_TRUNC);
463 /* Userspace might have consumed the packet meanwhile:
464 * it's not supposed to do this usually, but might be hard
465 * to prevent. Discard data we got (if any) and keep going. */
466 if (unlikely(err != sock_len)) {
467 pr_debug("Discarded rx packet: "
468 " len %d, expected %zd\n", err, sock_len);
469 vhost_discard_vq_desc(vq, headcount);
470 continue;
471 }
472 if (unlikely(vhost_hlen) &&
473 memcpy_toiovecend(vq->hdr, (unsigned char *)&hdr, 0,
474 vhost_hlen)) {
475 vq_err(vq, "Unable to write vnet_hdr at addr %p\n",
476 vq->iov->iov_base);
477 break;
478 }
479 /* TODO: Should check and handle checksum. */
Jason Wangcfbdab92011-01-17 16:10:59 +0800480 if (likely(mergeable) &&
David Stevens8dd014a2010-07-27 18:52:21 +0300481 memcpy_toiovecend(vq->hdr, (unsigned char *)&headcount,
482 offsetof(typeof(hdr), num_buffers),
483 sizeof hdr.num_buffers)) {
484 vq_err(vq, "Failed num_buffers write");
485 vhost_discard_vq_desc(vq, headcount);
486 break;
487 }
488 vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
489 headcount);
490 if (unlikely(vq_log))
491 vhost_log_write(vq, vq_log, log, vhost_len);
492 total_len += vhost_len;
493 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
494 vhost_poll_queue(&vq->poll);
495 break;
496 }
497 }
498
499 mutex_unlock(&vq->mutex);
David Stevens8dd014a2010-07-27 18:52:21 +0300500}
501
502static void handle_rx(struct vhost_net *net)
503{
504 if (vhost_has_feature(&net->dev, VIRTIO_NET_F_MRG_RXBUF))
505 handle_rx_mergeable(net);
506 else
507 handle_rx_big(net);
508}
509
Tejun Heoc23f34452010-06-02 20:40:00 +0200510static void handle_tx_kick(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000511{
Tejun Heoc23f34452010-06-02 20:40:00 +0200512 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
513 poll.work);
514 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
515
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000516 handle_tx(net);
517}
518
Tejun Heoc23f34452010-06-02 20:40:00 +0200519static void handle_rx_kick(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000520{
Tejun Heoc23f34452010-06-02 20:40:00 +0200521 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
522 poll.work);
523 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
524
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000525 handle_rx(net);
526}
527
Tejun Heoc23f34452010-06-02 20:40:00 +0200528static void handle_tx_net(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000529{
Tejun Heoc23f34452010-06-02 20:40:00 +0200530 struct vhost_net *net = container_of(work, struct vhost_net,
531 poll[VHOST_NET_VQ_TX].work);
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_net(struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000536{
Tejun Heoc23f34452010-06-02 20:40:00 +0200537 struct vhost_net *net = container_of(work, struct vhost_net,
538 poll[VHOST_NET_VQ_RX].work);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000539 handle_rx(net);
540}
541
542static int vhost_net_open(struct inode *inode, struct file *f)
543{
544 struct vhost_net *n = kmalloc(sizeof *n, GFP_KERNEL);
Tejun Heoc23f34452010-06-02 20:40:00 +0200545 struct vhost_dev *dev;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000546 int r;
Tejun Heoc23f34452010-06-02 20:40:00 +0200547
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000548 if (!n)
549 return -ENOMEM;
Tejun Heoc23f34452010-06-02 20:40:00 +0200550
551 dev = &n->dev;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000552 n->vqs[VHOST_NET_VQ_TX].handle_kick = handle_tx_kick;
553 n->vqs[VHOST_NET_VQ_RX].handle_kick = handle_rx_kick;
Tejun Heoc23f34452010-06-02 20:40:00 +0200554 r = vhost_dev_init(dev, n->vqs, VHOST_NET_VQ_MAX);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000555 if (r < 0) {
556 kfree(n);
557 return r;
558 }
559
Tejun Heoc23f34452010-06-02 20:40:00 +0200560 vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
561 vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000562 n->tx_poll_state = VHOST_NET_POLL_DISABLED;
563
564 f->private_data = n;
565
566 return 0;
567}
568
569static void vhost_net_disable_vq(struct vhost_net *n,
570 struct vhost_virtqueue *vq)
571{
572 if (!vq->private_data)
573 return;
574 if (vq == n->vqs + VHOST_NET_VQ_TX) {
575 tx_poll_stop(n);
576 n->tx_poll_state = VHOST_NET_POLL_DISABLED;
577 } else
578 vhost_poll_stop(n->poll + VHOST_NET_VQ_RX);
579}
580
581static void vhost_net_enable_vq(struct vhost_net *n,
582 struct vhost_virtqueue *vq)
583{
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100584 struct socket *sock;
585
586 sock = rcu_dereference_protected(vq->private_data,
587 lockdep_is_held(&vq->mutex));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000588 if (!sock)
589 return;
590 if (vq == n->vqs + VHOST_NET_VQ_TX) {
591 n->tx_poll_state = VHOST_NET_POLL_STOPPED;
592 tx_poll_start(n, sock);
593 } else
594 vhost_poll_start(n->poll + VHOST_NET_VQ_RX, sock->file);
595}
596
597static struct socket *vhost_net_stop_vq(struct vhost_net *n,
598 struct vhost_virtqueue *vq)
599{
600 struct socket *sock;
601
602 mutex_lock(&vq->mutex);
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100603 sock = rcu_dereference_protected(vq->private_data,
604 lockdep_is_held(&vq->mutex));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000605 vhost_net_disable_vq(n, vq);
606 rcu_assign_pointer(vq->private_data, NULL);
607 mutex_unlock(&vq->mutex);
608 return sock;
609}
610
611static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
612 struct socket **rx_sock)
613{
614 *tx_sock = vhost_net_stop_vq(n, n->vqs + VHOST_NET_VQ_TX);
615 *rx_sock = vhost_net_stop_vq(n, n->vqs + VHOST_NET_VQ_RX);
616}
617
618static void vhost_net_flush_vq(struct vhost_net *n, int index)
619{
620 vhost_poll_flush(n->poll + index);
621 vhost_poll_flush(&n->dev.vqs[index].poll);
622}
623
624static void vhost_net_flush(struct vhost_net *n)
625{
626 vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
627 vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
628}
629
630static int vhost_net_release(struct inode *inode, struct file *f)
631{
632 struct vhost_net *n = f->private_data;
633 struct socket *tx_sock;
634 struct socket *rx_sock;
635
636 vhost_net_stop(n, &tx_sock, &rx_sock);
637 vhost_net_flush(n);
638 vhost_dev_cleanup(&n->dev);
639 if (tx_sock)
640 fput(tx_sock->file);
641 if (rx_sock)
642 fput(rx_sock->file);
643 /* We do an extra flush before freeing memory,
644 * since jobs can re-queue themselves. */
645 vhost_net_flush(n);
646 kfree(n);
647 return 0;
648}
649
650static struct socket *get_raw_socket(int fd)
651{
652 struct {
653 struct sockaddr_ll sa;
654 char buf[MAX_ADDR_LEN];
655 } uaddr;
656 int uaddr_len = sizeof uaddr, r;
657 struct socket *sock = sockfd_lookup(fd, &r);
Krishna Kumard47effe2011-03-01 17:06:37 +0530658
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000659 if (!sock)
660 return ERR_PTR(-ENOTSOCK);
661
662 /* Parameter checking */
663 if (sock->sk->sk_type != SOCK_RAW) {
664 r = -ESOCKTNOSUPPORT;
665 goto err;
666 }
667
668 r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa,
669 &uaddr_len, 0);
670 if (r)
671 goto err;
672
673 if (uaddr.sa.sll_family != AF_PACKET) {
674 r = -EPFNOSUPPORT;
675 goto err;
676 }
677 return sock;
678err:
679 fput(sock->file);
680 return ERR_PTR(r);
681}
682
Arnd Bergmann501c7742010-02-18 05:46:50 +0000683static struct socket *get_tap_socket(int fd)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000684{
685 struct file *file = fget(fd);
686 struct socket *sock;
Krishna Kumard47effe2011-03-01 17:06:37 +0530687
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000688 if (!file)
689 return ERR_PTR(-EBADF);
690 sock = tun_get_socket(file);
Arnd Bergmann501c7742010-02-18 05:46:50 +0000691 if (!IS_ERR(sock))
692 return sock;
693 sock = macvtap_get_socket(file);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000694 if (IS_ERR(sock))
695 fput(file);
696 return sock;
697}
698
699static struct socket *get_socket(int fd)
700{
701 struct socket *sock;
Krishna Kumard47effe2011-03-01 17:06:37 +0530702
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000703 /* special case to disable backend */
704 if (fd == -1)
705 return NULL;
706 sock = get_raw_socket(fd);
707 if (!IS_ERR(sock))
708 return sock;
Arnd Bergmann501c7742010-02-18 05:46:50 +0000709 sock = get_tap_socket(fd);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000710 if (!IS_ERR(sock))
711 return sock;
712 return ERR_PTR(-ENOTSOCK);
713}
714
715static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
716{
717 struct socket *sock, *oldsock;
718 struct vhost_virtqueue *vq;
719 int r;
720
721 mutex_lock(&n->dev.mutex);
722 r = vhost_dev_check_owner(&n->dev);
723 if (r)
724 goto err;
725
726 if (index >= VHOST_NET_VQ_MAX) {
727 r = -ENOBUFS;
728 goto err;
729 }
730 vq = n->vqs + index;
731 mutex_lock(&vq->mutex);
732
733 /* Verify that ring has been setup correctly. */
734 if (!vhost_vq_access_ok(vq)) {
735 r = -EFAULT;
Jeff Dike1dace8c2010-03-04 16:10:14 -0500736 goto err_vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000737 }
738 sock = get_socket(fd);
739 if (IS_ERR(sock)) {
740 r = PTR_ERR(sock);
Jeff Dike1dace8c2010-03-04 16:10:14 -0500741 goto err_vq;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000742 }
743
744 /* start polling new socket */
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100745 oldsock = rcu_dereference_protected(vq->private_data,
746 lockdep_is_held(&vq->mutex));
David S. Miller11fe8832010-07-20 18:25:24 -0700747 if (sock != oldsock) {
Krishna Kumard47effe2011-03-01 17:06:37 +0530748 vhost_net_disable_vq(n, vq);
749 rcu_assign_pointer(vq->private_data, sock);
750 vhost_net_enable_vq(n, vq);
Jeff Dikedd1f4072010-03-04 16:10:14 -0500751 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000752
Michael S. Tsirkin1680e902010-07-15 15:19:12 +0300753 mutex_unlock(&vq->mutex);
754
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000755 if (oldsock) {
756 vhost_net_flush_vq(n, index);
757 fput(oldsock->file);
758 }
Jeff Dike1dace8c2010-03-04 16:10:14 -0500759
Michael S. Tsirkin1680e902010-07-15 15:19:12 +0300760 mutex_unlock(&n->dev.mutex);
761 return 0;
762
Jeff Dike1dace8c2010-03-04 16:10:14 -0500763err_vq:
764 mutex_unlock(&vq->mutex);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000765err:
766 mutex_unlock(&n->dev.mutex);
767 return r;
768}
769
770static long vhost_net_reset_owner(struct vhost_net *n)
771{
772 struct socket *tx_sock = NULL;
773 struct socket *rx_sock = NULL;
774 long err;
Krishna Kumard47effe2011-03-01 17:06:37 +0530775
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000776 mutex_lock(&n->dev.mutex);
777 err = vhost_dev_check_owner(&n->dev);
778 if (err)
779 goto done;
780 vhost_net_stop(n, &tx_sock, &rx_sock);
781 vhost_net_flush(n);
782 err = vhost_dev_reset_owner(&n->dev);
783done:
784 mutex_unlock(&n->dev.mutex);
785 if (tx_sock)
786 fput(tx_sock->file);
787 if (rx_sock)
788 fput(rx_sock->file);
789 return err;
790}
791
792static int vhost_net_set_features(struct vhost_net *n, u64 features)
793{
David Stevens8dd014a2010-07-27 18:52:21 +0300794 size_t vhost_hlen, sock_hlen, hdr_len;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000795 int i;
David Stevens8dd014a2010-07-27 18:52:21 +0300796
797 hdr_len = (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ?
798 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
799 sizeof(struct virtio_net_hdr);
800 if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
801 /* vhost provides vnet_hdr */
802 vhost_hlen = hdr_len;
803 sock_hlen = 0;
804 } else {
805 /* socket provides vnet_hdr */
806 vhost_hlen = 0;
807 sock_hlen = hdr_len;
808 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000809 mutex_lock(&n->dev.mutex);
810 if ((features & (1 << VHOST_F_LOG_ALL)) &&
811 !vhost_log_access_ok(&n->dev)) {
812 mutex_unlock(&n->dev.mutex);
813 return -EFAULT;
814 }
815 n->dev.acked_features = features;
816 smp_wmb();
817 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
818 mutex_lock(&n->vqs[i].mutex);
David Stevens8dd014a2010-07-27 18:52:21 +0300819 n->vqs[i].vhost_hlen = vhost_hlen;
820 n->vqs[i].sock_hlen = sock_hlen;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000821 mutex_unlock(&n->vqs[i].mutex);
822 }
823 vhost_net_flush(n);
824 mutex_unlock(&n->dev.mutex);
825 return 0;
826}
827
828static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
829 unsigned long arg)
830{
831 struct vhost_net *n = f->private_data;
832 void __user *argp = (void __user *)arg;
833 u64 __user *featurep = argp;
834 struct vhost_vring_file backend;
835 u64 features;
836 int r;
Krishna Kumard47effe2011-03-01 17:06:37 +0530837
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000838 switch (ioctl) {
839 case VHOST_NET_SET_BACKEND:
Takuya Yoshikawad3553a52010-05-27 19:01:58 +0900840 if (copy_from_user(&backend, argp, sizeof backend))
841 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000842 return vhost_net_set_backend(n, backend.index, backend.fd);
843 case VHOST_GET_FEATURES:
844 features = VHOST_FEATURES;
Takuya Yoshikawad3553a52010-05-27 19:01:58 +0900845 if (copy_to_user(featurep, &features, sizeof features))
846 return -EFAULT;
847 return 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000848 case VHOST_SET_FEATURES:
Takuya Yoshikawad3553a52010-05-27 19:01:58 +0900849 if (copy_from_user(&features, featurep, sizeof features))
850 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000851 if (features & ~VHOST_FEATURES)
852 return -EOPNOTSUPP;
853 return vhost_net_set_features(n, features);
854 case VHOST_RESET_OWNER:
855 return vhost_net_reset_owner(n);
856 default:
857 mutex_lock(&n->dev.mutex);
858 r = vhost_dev_ioctl(&n->dev, ioctl, arg);
859 vhost_net_flush(n);
860 mutex_unlock(&n->dev.mutex);
861 return r;
862 }
863}
864
865#ifdef CONFIG_COMPAT
866static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
867 unsigned long arg)
868{
869 return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
870}
871#endif
872
Tobias Klauser373a83a2010-05-17 15:12:49 +0200873static const struct file_operations vhost_net_fops = {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000874 .owner = THIS_MODULE,
875 .release = vhost_net_release,
876 .unlocked_ioctl = vhost_net_ioctl,
877#ifdef CONFIG_COMPAT
878 .compat_ioctl = vhost_net_compat_ioctl,
879#endif
880 .open = vhost_net_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200881 .llseek = noop_llseek,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000882};
883
884static struct miscdevice vhost_net_misc = {
Alan Cox79907d82010-06-09 09:39:49 +0100885 MISC_DYNAMIC_MINOR,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000886 "vhost-net",
887 &vhost_net_fops,
888};
889
Christoph Hellwiga8d37822010-04-13 14:11:25 -0400890static int vhost_net_init(void)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000891{
Tejun Heoc23f34452010-06-02 20:40:00 +0200892 return misc_register(&vhost_net_misc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000893}
894module_init(vhost_net_init);
895
Christoph Hellwiga8d37822010-04-13 14:11:25 -0400896static void vhost_net_exit(void)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000897{
898 misc_deregister(&vhost_net_misc);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000899}
900module_exit(vhost_net_exit);
901
902MODULE_VERSION("0.0.1");
903MODULE_LICENSE("GPL v2");
904MODULE_AUTHOR("Michael S. Tsirkin");
905MODULE_DESCRIPTION("Host kernel accelerator for virtio net");