Commit | Line | Data |
---|---|---|
3a4d5c94 MT |
1 | /* 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> | |
13 | #include <linux/mmu_context.h> | |
14 | #include <linux/miscdevice.h> | |
15 | #include <linux/module.h> | |
16 | #include <linux/mutex.h> | |
17 | #include <linux/workqueue.h> | |
18 | #include <linux/rcupdate.h> | |
19 | #include <linux/file.h> | |
5a0e3ad6 | 20 | #include <linux/slab.h> |
3a4d5c94 MT |
21 | |
22 | #include <linux/net.h> | |
23 | #include <linux/if_packet.h> | |
24 | #include <linux/if_arp.h> | |
25 | #include <linux/if_tun.h> | |
501c774c | 26 | #include <linux/if_macvlan.h> |
3a4d5c94 MT |
27 | |
28 | #include <net/sock.h> | |
29 | ||
30 | #include "vhost.h" | |
31 | ||
32 | /* Max number of bytes transferred before requeueing the job. | |
33 | * Using this limit prevents one virtqueue from starving others. */ | |
34 | #define VHOST_NET_WEIGHT 0x80000 | |
35 | ||
36 | enum { | |
37 | VHOST_NET_VQ_RX = 0, | |
38 | VHOST_NET_VQ_TX = 1, | |
39 | VHOST_NET_VQ_MAX = 2, | |
40 | }; | |
41 | ||
42 | enum vhost_net_poll_state { | |
43 | VHOST_NET_POLL_DISABLED = 0, | |
44 | VHOST_NET_POLL_STARTED = 1, | |
45 | VHOST_NET_POLL_STOPPED = 2, | |
46 | }; | |
47 | ||
48 | struct vhost_net { | |
49 | struct vhost_dev dev; | |
50 | struct vhost_virtqueue vqs[VHOST_NET_VQ_MAX]; | |
51 | struct vhost_poll poll[VHOST_NET_VQ_MAX]; | |
52 | /* Tells us whether we are polling a socket for TX. | |
53 | * We only do this when socket buffer fills up. | |
54 | * Protected by tx vq lock. */ | |
55 | enum vhost_net_poll_state tx_poll_state; | |
56 | }; | |
57 | ||
58 | /* Pop first len bytes from iovec. Return number of segments used. */ | |
59 | static int move_iovec_hdr(struct iovec *from, struct iovec *to, | |
60 | size_t len, int iov_count) | |
61 | { | |
62 | int seg = 0; | |
63 | size_t size; | |
64 | 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 | } | |
77 | ||
78 | /* Caller must have TX VQ lock */ | |
79 | static void tx_poll_stop(struct vhost_net *net) | |
80 | { | |
81 | if (likely(net->tx_poll_state != VHOST_NET_POLL_STARTED)) | |
82 | return; | |
83 | vhost_poll_stop(net->poll + VHOST_NET_VQ_TX); | |
84 | net->tx_poll_state = VHOST_NET_POLL_STOPPED; | |
85 | } | |
86 | ||
87 | /* Caller must have TX VQ lock */ | |
88 | static void tx_poll_start(struct vhost_net *net, struct socket *sock) | |
89 | { | |
90 | if (unlikely(net->tx_poll_state != VHOST_NET_POLL_STOPPED)) | |
91 | return; | |
92 | vhost_poll_start(net->poll + VHOST_NET_VQ_TX, sock->file); | |
93 | net->tx_poll_state = VHOST_NET_POLL_STARTED; | |
94 | } | |
95 | ||
96 | /* Expects to be always run from workqueue - which acts as | |
97 | * read-size critical section for our kind of RCU. */ | |
98 | static void handle_tx(struct vhost_net *net) | |
99 | { | |
100 | struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_TX]; | |
d5675bd2 MT |
101 | unsigned out, in, s; |
102 | int head; | |
3a4d5c94 MT |
103 | struct msghdr msg = { |
104 | .msg_name = NULL, | |
105 | .msg_namelen = 0, | |
106 | .msg_control = NULL, | |
107 | .msg_controllen = 0, | |
108 | .msg_iov = vq->iov, | |
109 | .msg_flags = MSG_DONTWAIT, | |
110 | }; | |
111 | size_t len, total_len = 0; | |
112 | int err, wmem; | |
113 | size_t hdr_size; | |
114 | struct socket *sock = rcu_dereference(vq->private_data); | |
115 | if (!sock) | |
116 | return; | |
117 | ||
118 | wmem = atomic_read(&sock->sk->sk_wmem_alloc); | |
39286fa4 SS |
119 | if (wmem >= sock->sk->sk_sndbuf) { |
120 | mutex_lock(&vq->mutex); | |
121 | tx_poll_start(net, sock); | |
122 | mutex_unlock(&vq->mutex); | |
3a4d5c94 | 123 | return; |
39286fa4 | 124 | } |
3a4d5c94 MT |
125 | |
126 | use_mm(net->dev.mm); | |
127 | mutex_lock(&vq->mutex); | |
128 | vhost_disable_notify(vq); | |
129 | ||
0e255572 | 130 | if (wmem < sock->sk->sk_sndbuf / 2) |
3a4d5c94 MT |
131 | tx_poll_stop(net); |
132 | hdr_size = vq->hdr_size; | |
133 | ||
134 | for (;;) { | |
135 | head = vhost_get_vq_desc(&net->dev, vq, vq->iov, | |
136 | ARRAY_SIZE(vq->iov), | |
137 | &out, &in, | |
138 | NULL, NULL); | |
d5675bd2 MT |
139 | /* On error, stop handling until the next kick. */ |
140 | if (head < 0) | |
141 | break; | |
3a4d5c94 MT |
142 | /* Nothing new? Wait for eventfd to tell us they refilled. */ |
143 | if (head == vq->num) { | |
144 | wmem = atomic_read(&sock->sk->sk_wmem_alloc); | |
145 | if (wmem >= sock->sk->sk_sndbuf * 3 / 4) { | |
146 | tx_poll_start(net, sock); | |
147 | set_bit(SOCK_ASYNC_NOSPACE, &sock->flags); | |
148 | break; | |
149 | } | |
150 | if (unlikely(vhost_enable_notify(vq))) { | |
151 | vhost_disable_notify(vq); | |
152 | continue; | |
153 | } | |
154 | break; | |
155 | } | |
156 | if (in) { | |
157 | vq_err(vq, "Unexpected descriptor format for TX: " | |
158 | "out %d, int %d\n", out, in); | |
159 | break; | |
160 | } | |
161 | /* Skip header. TODO: support TSO. */ | |
162 | s = move_iovec_hdr(vq->iov, vq->hdr, hdr_size, out); | |
163 | msg.msg_iovlen = out; | |
164 | len = iov_length(vq->iov, out); | |
165 | /* Sanity check */ | |
166 | if (!len) { | |
167 | vq_err(vq, "Unexpected header len for TX: " | |
168 | "%zd expected %zd\n", | |
169 | iov_length(vq->hdr, s), hdr_size); | |
170 | break; | |
171 | } | |
172 | /* TODO: Check specific error and bomb out unless ENOBUFS? */ | |
173 | err = sock->ops->sendmsg(NULL, sock, &msg, len); | |
174 | if (unlikely(err < 0)) { | |
175 | vhost_discard_vq_desc(vq); | |
176 | tx_poll_start(net, sock); | |
177 | break; | |
178 | } | |
179 | if (err != len) | |
180 | pr_err("Truncated TX packet: " | |
181 | " len %d != %zd\n", err, len); | |
182 | vhost_add_used_and_signal(&net->dev, vq, head, 0); | |
183 | total_len += len; | |
184 | if (unlikely(total_len >= VHOST_NET_WEIGHT)) { | |
185 | vhost_poll_queue(&vq->poll); | |
186 | break; | |
187 | } | |
188 | } | |
189 | ||
190 | mutex_unlock(&vq->mutex); | |
191 | unuse_mm(net->dev.mm); | |
192 | } | |
193 | ||
194 | /* Expects to be always run from workqueue - which acts as | |
195 | * read-size critical section for our kind of RCU. */ | |
196 | static void handle_rx(struct vhost_net *net) | |
197 | { | |
198 | struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_RX]; | |
d5675bd2 MT |
199 | unsigned out, in, log, s; |
200 | int head; | |
3a4d5c94 MT |
201 | struct vhost_log *vq_log; |
202 | struct msghdr msg = { | |
203 | .msg_name = NULL, | |
204 | .msg_namelen = 0, | |
205 | .msg_control = NULL, /* FIXME: get and handle RX aux data. */ | |
206 | .msg_controllen = 0, | |
207 | .msg_iov = vq->iov, | |
208 | .msg_flags = MSG_DONTWAIT, | |
209 | }; | |
210 | ||
211 | struct virtio_net_hdr hdr = { | |
212 | .flags = 0, | |
213 | .gso_type = VIRTIO_NET_HDR_GSO_NONE | |
214 | }; | |
215 | ||
216 | size_t len, total_len = 0; | |
217 | int err; | |
218 | size_t hdr_size; | |
219 | struct socket *sock = rcu_dereference(vq->private_data); | |
220 | if (!sock || skb_queue_empty(&sock->sk->sk_receive_queue)) | |
221 | return; | |
222 | ||
223 | use_mm(net->dev.mm); | |
224 | mutex_lock(&vq->mutex); | |
225 | vhost_disable_notify(vq); | |
226 | hdr_size = vq->hdr_size; | |
227 | ||
228 | vq_log = unlikely(vhost_has_feature(&net->dev, VHOST_F_LOG_ALL)) ? | |
229 | vq->log : NULL; | |
230 | ||
231 | for (;;) { | |
232 | head = vhost_get_vq_desc(&net->dev, vq, vq->iov, | |
233 | ARRAY_SIZE(vq->iov), | |
234 | &out, &in, | |
235 | vq_log, &log); | |
d5675bd2 MT |
236 | /* On error, stop handling until the next kick. */ |
237 | if (head < 0) | |
238 | break; | |
3a4d5c94 MT |
239 | /* OK, now we need to know about added descriptors. */ |
240 | if (head == vq->num) { | |
241 | if (unlikely(vhost_enable_notify(vq))) { | |
242 | /* They have slipped one in as we were | |
243 | * doing that: check again. */ | |
244 | vhost_disable_notify(vq); | |
245 | continue; | |
246 | } | |
247 | /* Nothing new? Wait for eventfd to tell us | |
248 | * they refilled. */ | |
249 | break; | |
250 | } | |
251 | /* We don't need to be notified again. */ | |
252 | if (out) { | |
253 | vq_err(vq, "Unexpected descriptor format for RX: " | |
254 | "out %d, int %d\n", | |
255 | out, in); | |
256 | break; | |
257 | } | |
258 | /* Skip header. TODO: support TSO/mergeable rx buffers. */ | |
259 | s = move_iovec_hdr(vq->iov, vq->hdr, hdr_size, in); | |
260 | msg.msg_iovlen = in; | |
261 | len = iov_length(vq->iov, in); | |
262 | /* Sanity check */ | |
263 | if (!len) { | |
264 | vq_err(vq, "Unexpected header len for RX: " | |
265 | "%zd expected %zd\n", | |
266 | iov_length(vq->hdr, s), hdr_size); | |
267 | break; | |
268 | } | |
269 | err = sock->ops->recvmsg(NULL, sock, &msg, | |
270 | len, MSG_DONTWAIT | MSG_TRUNC); | |
271 | /* TODO: Check specific error and bomb out unless EAGAIN? */ | |
272 | if (err < 0) { | |
273 | vhost_discard_vq_desc(vq); | |
274 | break; | |
275 | } | |
276 | /* TODO: Should check and handle checksum. */ | |
277 | if (err > len) { | |
278 | pr_err("Discarded truncated rx packet: " | |
279 | " len %d > %zd\n", err, len); | |
280 | vhost_discard_vq_desc(vq); | |
281 | continue; | |
282 | } | |
283 | len = err; | |
284 | err = memcpy_toiovec(vq->hdr, (unsigned char *)&hdr, hdr_size); | |
285 | if (err) { | |
286 | vq_err(vq, "Unable to write vnet_hdr at addr %p: %d\n", | |
287 | vq->iov->iov_base, err); | |
288 | break; | |
289 | } | |
290 | len += hdr_size; | |
291 | vhost_add_used_and_signal(&net->dev, vq, head, len); | |
292 | if (unlikely(vq_log)) | |
293 | vhost_log_write(vq, vq_log, log, len); | |
294 | total_len += len; | |
295 | if (unlikely(total_len >= VHOST_NET_WEIGHT)) { | |
296 | vhost_poll_queue(&vq->poll); | |
297 | break; | |
298 | } | |
299 | } | |
300 | ||
301 | mutex_unlock(&vq->mutex); | |
302 | unuse_mm(net->dev.mm); | |
303 | } | |
304 | ||
305 | static void handle_tx_kick(struct work_struct *work) | |
306 | { | |
307 | struct vhost_virtqueue *vq; | |
308 | struct vhost_net *net; | |
309 | vq = container_of(work, struct vhost_virtqueue, poll.work); | |
310 | net = container_of(vq->dev, struct vhost_net, dev); | |
311 | handle_tx(net); | |
312 | } | |
313 | ||
314 | static void handle_rx_kick(struct work_struct *work) | |
315 | { | |
316 | struct vhost_virtqueue *vq; | |
317 | struct vhost_net *net; | |
318 | vq = container_of(work, struct vhost_virtqueue, poll.work); | |
319 | net = container_of(vq->dev, struct vhost_net, dev); | |
320 | handle_rx(net); | |
321 | } | |
322 | ||
323 | static void handle_tx_net(struct work_struct *work) | |
324 | { | |
325 | struct vhost_net *net; | |
326 | net = container_of(work, struct vhost_net, poll[VHOST_NET_VQ_TX].work); | |
327 | handle_tx(net); | |
328 | } | |
329 | ||
330 | static void handle_rx_net(struct work_struct *work) | |
331 | { | |
332 | struct vhost_net *net; | |
333 | net = container_of(work, struct vhost_net, poll[VHOST_NET_VQ_RX].work); | |
334 | handle_rx(net); | |
335 | } | |
336 | ||
337 | static int vhost_net_open(struct inode *inode, struct file *f) | |
338 | { | |
339 | struct vhost_net *n = kmalloc(sizeof *n, GFP_KERNEL); | |
340 | int r; | |
341 | if (!n) | |
342 | return -ENOMEM; | |
343 | n->vqs[VHOST_NET_VQ_TX].handle_kick = handle_tx_kick; | |
344 | n->vqs[VHOST_NET_VQ_RX].handle_kick = handle_rx_kick; | |
345 | r = vhost_dev_init(&n->dev, n->vqs, VHOST_NET_VQ_MAX); | |
346 | if (r < 0) { | |
347 | kfree(n); | |
348 | return r; | |
349 | } | |
350 | ||
351 | vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT); | |
352 | vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN); | |
353 | n->tx_poll_state = VHOST_NET_POLL_DISABLED; | |
354 | ||
355 | f->private_data = n; | |
356 | ||
357 | return 0; | |
358 | } | |
359 | ||
360 | static void vhost_net_disable_vq(struct vhost_net *n, | |
361 | struct vhost_virtqueue *vq) | |
362 | { | |
363 | if (!vq->private_data) | |
364 | return; | |
365 | if (vq == n->vqs + VHOST_NET_VQ_TX) { | |
366 | tx_poll_stop(n); | |
367 | n->tx_poll_state = VHOST_NET_POLL_DISABLED; | |
368 | } else | |
369 | vhost_poll_stop(n->poll + VHOST_NET_VQ_RX); | |
370 | } | |
371 | ||
372 | static void vhost_net_enable_vq(struct vhost_net *n, | |
373 | struct vhost_virtqueue *vq) | |
374 | { | |
375 | struct socket *sock = vq->private_data; | |
376 | if (!sock) | |
377 | return; | |
378 | if (vq == n->vqs + VHOST_NET_VQ_TX) { | |
379 | n->tx_poll_state = VHOST_NET_POLL_STOPPED; | |
380 | tx_poll_start(n, sock); | |
381 | } else | |
382 | vhost_poll_start(n->poll + VHOST_NET_VQ_RX, sock->file); | |
383 | } | |
384 | ||
385 | static struct socket *vhost_net_stop_vq(struct vhost_net *n, | |
386 | struct vhost_virtqueue *vq) | |
387 | { | |
388 | struct socket *sock; | |
389 | ||
390 | mutex_lock(&vq->mutex); | |
391 | sock = vq->private_data; | |
392 | vhost_net_disable_vq(n, vq); | |
393 | rcu_assign_pointer(vq->private_data, NULL); | |
394 | mutex_unlock(&vq->mutex); | |
395 | return sock; | |
396 | } | |
397 | ||
398 | static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock, | |
399 | struct socket **rx_sock) | |
400 | { | |
401 | *tx_sock = vhost_net_stop_vq(n, n->vqs + VHOST_NET_VQ_TX); | |
402 | *rx_sock = vhost_net_stop_vq(n, n->vqs + VHOST_NET_VQ_RX); | |
403 | } | |
404 | ||
405 | static void vhost_net_flush_vq(struct vhost_net *n, int index) | |
406 | { | |
407 | vhost_poll_flush(n->poll + index); | |
408 | vhost_poll_flush(&n->dev.vqs[index].poll); | |
409 | } | |
410 | ||
411 | static void vhost_net_flush(struct vhost_net *n) | |
412 | { | |
413 | vhost_net_flush_vq(n, VHOST_NET_VQ_TX); | |
414 | vhost_net_flush_vq(n, VHOST_NET_VQ_RX); | |
415 | } | |
416 | ||
417 | static int vhost_net_release(struct inode *inode, struct file *f) | |
418 | { | |
419 | struct vhost_net *n = f->private_data; | |
420 | struct socket *tx_sock; | |
421 | struct socket *rx_sock; | |
422 | ||
423 | vhost_net_stop(n, &tx_sock, &rx_sock); | |
424 | vhost_net_flush(n); | |
425 | vhost_dev_cleanup(&n->dev); | |
426 | if (tx_sock) | |
427 | fput(tx_sock->file); | |
428 | if (rx_sock) | |
429 | fput(rx_sock->file); | |
430 | /* We do an extra flush before freeing memory, | |
431 | * since jobs can re-queue themselves. */ | |
432 | vhost_net_flush(n); | |
433 | kfree(n); | |
434 | return 0; | |
435 | } | |
436 | ||
437 | static struct socket *get_raw_socket(int fd) | |
438 | { | |
439 | struct { | |
440 | struct sockaddr_ll sa; | |
441 | char buf[MAX_ADDR_LEN]; | |
442 | } uaddr; | |
443 | int uaddr_len = sizeof uaddr, r; | |
444 | struct socket *sock = sockfd_lookup(fd, &r); | |
445 | if (!sock) | |
446 | return ERR_PTR(-ENOTSOCK); | |
447 | ||
448 | /* Parameter checking */ | |
449 | if (sock->sk->sk_type != SOCK_RAW) { | |
450 | r = -ESOCKTNOSUPPORT; | |
451 | goto err; | |
452 | } | |
453 | ||
454 | r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa, | |
455 | &uaddr_len, 0); | |
456 | if (r) | |
457 | goto err; | |
458 | ||
459 | if (uaddr.sa.sll_family != AF_PACKET) { | |
460 | r = -EPFNOSUPPORT; | |
461 | goto err; | |
462 | } | |
463 | return sock; | |
464 | err: | |
465 | fput(sock->file); | |
466 | return ERR_PTR(r); | |
467 | } | |
468 | ||
501c774c | 469 | static struct socket *get_tap_socket(int fd) |
3a4d5c94 MT |
470 | { |
471 | struct file *file = fget(fd); | |
472 | struct socket *sock; | |
473 | if (!file) | |
474 | return ERR_PTR(-EBADF); | |
475 | sock = tun_get_socket(file); | |
501c774c AB |
476 | if (!IS_ERR(sock)) |
477 | return sock; | |
478 | sock = macvtap_get_socket(file); | |
3a4d5c94 MT |
479 | if (IS_ERR(sock)) |
480 | fput(file); | |
481 | return sock; | |
482 | } | |
483 | ||
484 | static struct socket *get_socket(int fd) | |
485 | { | |
486 | struct socket *sock; | |
487 | /* special case to disable backend */ | |
488 | if (fd == -1) | |
489 | return NULL; | |
490 | sock = get_raw_socket(fd); | |
491 | if (!IS_ERR(sock)) | |
492 | return sock; | |
501c774c | 493 | sock = get_tap_socket(fd); |
3a4d5c94 MT |
494 | if (!IS_ERR(sock)) |
495 | return sock; | |
496 | return ERR_PTR(-ENOTSOCK); | |
497 | } | |
498 | ||
499 | static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd) | |
500 | { | |
501 | struct socket *sock, *oldsock; | |
502 | struct vhost_virtqueue *vq; | |
503 | int r; | |
504 | ||
505 | mutex_lock(&n->dev.mutex); | |
506 | r = vhost_dev_check_owner(&n->dev); | |
507 | if (r) | |
508 | goto err; | |
509 | ||
510 | if (index >= VHOST_NET_VQ_MAX) { | |
511 | r = -ENOBUFS; | |
512 | goto err; | |
513 | } | |
514 | vq = n->vqs + index; | |
515 | mutex_lock(&vq->mutex); | |
516 | ||
517 | /* Verify that ring has been setup correctly. */ | |
518 | if (!vhost_vq_access_ok(vq)) { | |
519 | r = -EFAULT; | |
1dace8c8 | 520 | goto err_vq; |
3a4d5c94 MT |
521 | } |
522 | sock = get_socket(fd); | |
523 | if (IS_ERR(sock)) { | |
524 | r = PTR_ERR(sock); | |
1dace8c8 | 525 | goto err_vq; |
3a4d5c94 MT |
526 | } |
527 | ||
528 | /* start polling new socket */ | |
529 | oldsock = vq->private_data; | |
530 | if (sock == oldsock) | |
531 | goto done; | |
532 | ||
533 | vhost_net_disable_vq(n, vq); | |
534 | rcu_assign_pointer(vq->private_data, sock); | |
535 | vhost_net_enable_vq(n, vq); | |
3a4d5c94 MT |
536 | done: |
537 | if (oldsock) { | |
538 | vhost_net_flush_vq(n, index); | |
539 | fput(oldsock->file); | |
540 | } | |
1dace8c8 JD |
541 | |
542 | err_vq: | |
543 | mutex_unlock(&vq->mutex); | |
3a4d5c94 MT |
544 | err: |
545 | mutex_unlock(&n->dev.mutex); | |
546 | return r; | |
547 | } | |
548 | ||
549 | static long vhost_net_reset_owner(struct vhost_net *n) | |
550 | { | |
551 | struct socket *tx_sock = NULL; | |
552 | struct socket *rx_sock = NULL; | |
553 | long err; | |
554 | mutex_lock(&n->dev.mutex); | |
555 | err = vhost_dev_check_owner(&n->dev); | |
556 | if (err) | |
557 | goto done; | |
558 | vhost_net_stop(n, &tx_sock, &rx_sock); | |
559 | vhost_net_flush(n); | |
560 | err = vhost_dev_reset_owner(&n->dev); | |
561 | done: | |
562 | mutex_unlock(&n->dev.mutex); | |
563 | if (tx_sock) | |
564 | fput(tx_sock->file); | |
565 | if (rx_sock) | |
566 | fput(rx_sock->file); | |
567 | return err; | |
568 | } | |
569 | ||
570 | static int vhost_net_set_features(struct vhost_net *n, u64 features) | |
571 | { | |
572 | size_t hdr_size = features & (1 << VHOST_NET_F_VIRTIO_NET_HDR) ? | |
573 | sizeof(struct virtio_net_hdr) : 0; | |
574 | int i; | |
575 | mutex_lock(&n->dev.mutex); | |
576 | if ((features & (1 << VHOST_F_LOG_ALL)) && | |
577 | !vhost_log_access_ok(&n->dev)) { | |
578 | mutex_unlock(&n->dev.mutex); | |
579 | return -EFAULT; | |
580 | } | |
581 | n->dev.acked_features = features; | |
582 | smp_wmb(); | |
583 | for (i = 0; i < VHOST_NET_VQ_MAX; ++i) { | |
584 | mutex_lock(&n->vqs[i].mutex); | |
585 | n->vqs[i].hdr_size = hdr_size; | |
586 | mutex_unlock(&n->vqs[i].mutex); | |
587 | } | |
588 | vhost_net_flush(n); | |
589 | mutex_unlock(&n->dev.mutex); | |
590 | return 0; | |
591 | } | |
592 | ||
593 | static long vhost_net_ioctl(struct file *f, unsigned int ioctl, | |
594 | unsigned long arg) | |
595 | { | |
596 | struct vhost_net *n = f->private_data; | |
597 | void __user *argp = (void __user *)arg; | |
598 | u64 __user *featurep = argp; | |
599 | struct vhost_vring_file backend; | |
600 | u64 features; | |
601 | int r; | |
602 | switch (ioctl) { | |
603 | case VHOST_NET_SET_BACKEND: | |
d3553a52 TY |
604 | if (copy_from_user(&backend, argp, sizeof backend)) |
605 | return -EFAULT; | |
3a4d5c94 MT |
606 | return vhost_net_set_backend(n, backend.index, backend.fd); |
607 | case VHOST_GET_FEATURES: | |
608 | features = VHOST_FEATURES; | |
d3553a52 TY |
609 | if (copy_to_user(featurep, &features, sizeof features)) |
610 | return -EFAULT; | |
611 | return 0; | |
3a4d5c94 | 612 | case VHOST_SET_FEATURES: |
d3553a52 TY |
613 | if (copy_from_user(&features, featurep, sizeof features)) |
614 | return -EFAULT; | |
3a4d5c94 MT |
615 | if (features & ~VHOST_FEATURES) |
616 | return -EOPNOTSUPP; | |
617 | return vhost_net_set_features(n, features); | |
618 | case VHOST_RESET_OWNER: | |
619 | return vhost_net_reset_owner(n); | |
620 | default: | |
621 | mutex_lock(&n->dev.mutex); | |
622 | r = vhost_dev_ioctl(&n->dev, ioctl, arg); | |
623 | vhost_net_flush(n); | |
624 | mutex_unlock(&n->dev.mutex); | |
625 | return r; | |
626 | } | |
627 | } | |
628 | ||
629 | #ifdef CONFIG_COMPAT | |
630 | static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl, | |
631 | unsigned long arg) | |
632 | { | |
633 | return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg)); | |
634 | } | |
635 | #endif | |
636 | ||
637 | const static struct file_operations vhost_net_fops = { | |
638 | .owner = THIS_MODULE, | |
639 | .release = vhost_net_release, | |
640 | .unlocked_ioctl = vhost_net_ioctl, | |
641 | #ifdef CONFIG_COMPAT | |
642 | .compat_ioctl = vhost_net_compat_ioctl, | |
643 | #endif | |
644 | .open = vhost_net_open, | |
645 | }; | |
646 | ||
647 | static struct miscdevice vhost_net_misc = { | |
648 | VHOST_NET_MINOR, | |
649 | "vhost-net", | |
650 | &vhost_net_fops, | |
651 | }; | |
652 | ||
a8d3782f | 653 | static int vhost_net_init(void) |
3a4d5c94 MT |
654 | { |
655 | int r = vhost_init(); | |
656 | if (r) | |
657 | goto err_init; | |
658 | r = misc_register(&vhost_net_misc); | |
659 | if (r) | |
660 | goto err_reg; | |
661 | return 0; | |
662 | err_reg: | |
663 | vhost_cleanup(); | |
664 | err_init: | |
665 | return r; | |
666 | ||
667 | } | |
668 | module_init(vhost_net_init); | |
669 | ||
a8d3782f | 670 | static void vhost_net_exit(void) |
3a4d5c94 MT |
671 | { |
672 | misc_deregister(&vhost_net_misc); | |
673 | vhost_cleanup(); | |
674 | } | |
675 | module_exit(vhost_net_exit); | |
676 | ||
677 | MODULE_VERSION("0.0.1"); | |
678 | MODULE_LICENSE("GPL v2"); | |
679 | MODULE_AUTHOR("Michael S. Tsirkin"); | |
680 | MODULE_DESCRIPTION("Host kernel accelerator for virtio net"); |