Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 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> |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 13 | #include <linux/miscdevice.h> |
| 14 | #include <linux/module.h> |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 15 | #include <linux/moduleparam.h> |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 16 | #include <linux/mutex.h> |
| 17 | #include <linux/workqueue.h> |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 18 | #include <linux/file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Ingo Molnar | e601757 | 2017-02-01 16:36:40 +0100 | [diff] [blame] | 20 | #include <linux/sched/clock.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 21 | #include <linux/sched/signal.h> |
Michael S. Tsirkin | 23cc5a9 | 2013-01-23 21:46:47 +0100 | [diff] [blame] | 22 | #include <linux/vmalloc.h> |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 23 | |
| 24 | #include <linux/net.h> |
| 25 | #include <linux/if_packet.h> |
| 26 | #include <linux/if_arp.h> |
| 27 | #include <linux/if_tun.h> |
Arnd Bergmann | 501c774 | 2010-02-18 05:46:50 +0000 | [diff] [blame] | 28 | #include <linux/if_macvlan.h> |
Sainath Grandhi | 635b8c8 | 2017-02-10 16:03:47 -0800 | [diff] [blame] | 29 | #include <linux/if_tap.h> |
Basil Gor | c53cff5e | 2012-05-03 22:55:23 +0000 | [diff] [blame] | 30 | #include <linux/if_vlan.h> |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 31 | #include <linux/skb_array.h> |
| 32 | #include <linux/skbuff.h> |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 33 | |
| 34 | #include <net/sock.h> |
| 35 | |
| 36 | #include "vhost.h" |
| 37 | |
Michael S. Tsirkin | f9611c4 | 2012-12-06 14:56:00 +0200 | [diff] [blame] | 38 | static int experimental_zcopytx = 1; |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 39 | module_param(experimental_zcopytx, int, 0444); |
Michael S. Tsirkin | f9611c4 | 2012-12-06 14:56:00 +0200 | [diff] [blame] | 40 | MODULE_PARM_DESC(experimental_zcopytx, "Enable Zero Copy TX;" |
| 41 | " 1 -Enable; 0 - Disable"); |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 42 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 43 | /* Max number of bytes transferred before requeueing the job. |
| 44 | * Using this limit prevents one virtqueue from starving others. */ |
| 45 | #define VHOST_NET_WEIGHT 0x80000 |
| 46 | |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 47 | /* MAX number of TX used buffers for outstanding zerocopy */ |
| 48 | #define VHOST_MAX_PEND 128 |
| 49 | #define VHOST_GOODCOPY_LEN 256 |
| 50 | |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 51 | /* |
| 52 | * For transmit, used buffer len is unused; we override it to track buffer |
| 53 | * status internally; used for zerocopy tx only. |
| 54 | */ |
| 55 | /* Lower device DMA failed */ |
Michael S. Tsirkin | bf99573 | 2014-10-24 11:49:27 +0300 | [diff] [blame] | 56 | #define VHOST_DMA_FAILED_LEN ((__force __virtio32)3) |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 57 | /* Lower device DMA done */ |
Michael S. Tsirkin | bf99573 | 2014-10-24 11:49:27 +0300 | [diff] [blame] | 58 | #define VHOST_DMA_DONE_LEN ((__force __virtio32)2) |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 59 | /* Lower device DMA in progress */ |
Michael S. Tsirkin | bf99573 | 2014-10-24 11:49:27 +0300 | [diff] [blame] | 60 | #define VHOST_DMA_IN_PROGRESS ((__force __virtio32)1) |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 61 | /* Buffer unused */ |
Michael S. Tsirkin | bf99573 | 2014-10-24 11:49:27 +0300 | [diff] [blame] | 62 | #define VHOST_DMA_CLEAR_LEN ((__force __virtio32)0) |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 63 | |
Michael S. Tsirkin | bf99573 | 2014-10-24 11:49:27 +0300 | [diff] [blame] | 64 | #define VHOST_DMA_IS_DONE(len) ((__force u32)(len) >= (__force u32)VHOST_DMA_DONE_LEN) |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 65 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 66 | enum { |
Asias He | 8570a6e | 2013-05-06 16:38:20 +0800 | [diff] [blame] | 67 | VHOST_NET_FEATURES = VHOST_FEATURES | |
| 68 | (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 69 | (1ULL << VIRTIO_NET_F_MRG_RXBUF) | |
| 70 | (1ULL << VIRTIO_F_IOMMU_PLATFORM) |
Asias He | 8570a6e | 2013-05-06 16:38:20 +0800 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | enum { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 74 | VHOST_NET_VQ_RX = 0, |
| 75 | VHOST_NET_VQ_TX = 1, |
| 76 | VHOST_NET_VQ_MAX = 2, |
| 77 | }; |
| 78 | |
Asias He | fe729a5 | 2013-05-06 16:38:24 +0800 | [diff] [blame] | 79 | struct vhost_net_ubuf_ref { |
Michael S. Tsirkin | 0ad8b48 | 2014-02-13 11:42:05 +0200 | [diff] [blame] | 80 | /* refcount follows semantics similar to kref: |
| 81 | * 0: object is released |
| 82 | * 1: no outstanding ubufs |
| 83 | * >1: outstanding ubufs |
| 84 | */ |
| 85 | atomic_t refcount; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 86 | wait_queue_head_t wait; |
| 87 | struct vhost_virtqueue *vq; |
| 88 | }; |
| 89 | |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 90 | #define VHOST_RX_BATCH 64 |
| 91 | struct vhost_net_buf { |
Jason Wang | 5990a30 | 2018-01-04 11:14:27 +0800 | [diff] [blame^] | 92 | void **queue; |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 93 | int tail; |
| 94 | int head; |
| 95 | }; |
| 96 | |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 97 | struct vhost_net_virtqueue { |
| 98 | struct vhost_virtqueue vq; |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame] | 99 | size_t vhost_hlen; |
| 100 | size_t sock_hlen; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 101 | /* vhost zerocopy support fields below: */ |
| 102 | /* last used idx for outstanding DMA zerocopy buffers */ |
| 103 | int upend_idx; |
| 104 | /* first used idx for DMA done zerocopy buffers */ |
| 105 | int done_idx; |
| 106 | /* an array of userspace buffers info */ |
| 107 | struct ubuf_info *ubuf_info; |
| 108 | /* Reference counting for outstanding ubufs. |
| 109 | * Protected by vq mutex. Writers must also take device mutex. */ |
Asias He | fe729a5 | 2013-05-06 16:38:24 +0800 | [diff] [blame] | 110 | struct vhost_net_ubuf_ref *ubufs; |
Jason Wang | 5990a30 | 2018-01-04 11:14:27 +0800 | [diff] [blame^] | 111 | struct ptr_ring *rx_ring; |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 112 | struct vhost_net_buf rxq; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 113 | }; |
| 114 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 115 | struct vhost_net { |
| 116 | struct vhost_dev dev; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 117 | struct vhost_net_virtqueue vqs[VHOST_NET_VQ_MAX]; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 118 | struct vhost_poll poll[VHOST_NET_VQ_MAX]; |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 119 | /* Number of TX recently submitted. |
| 120 | * Protected by tx vq lock. */ |
| 121 | unsigned tx_packets; |
| 122 | /* Number of times zerocopy TX recently failed. |
| 123 | * Protected by tx vq lock. */ |
| 124 | unsigned tx_zcopy_err; |
Michael S. Tsirkin | 1280c27 | 2012-12-04 00:17:14 +0200 | [diff] [blame] | 125 | /* Flush in progress. Protected by tx vq lock. */ |
| 126 | bool tx_flush; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
Asias He | fe729a5 | 2013-05-06 16:38:24 +0800 | [diff] [blame] | 129 | static unsigned vhost_net_zcopy_mask __read_mostly; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 130 | |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 131 | static void *vhost_net_buf_get_ptr(struct vhost_net_buf *rxq) |
| 132 | { |
| 133 | if (rxq->tail != rxq->head) |
| 134 | return rxq->queue[rxq->head]; |
| 135 | else |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | static int vhost_net_buf_get_size(struct vhost_net_buf *rxq) |
| 140 | { |
| 141 | return rxq->tail - rxq->head; |
| 142 | } |
| 143 | |
| 144 | static int vhost_net_buf_is_empty(struct vhost_net_buf *rxq) |
| 145 | { |
| 146 | return rxq->tail == rxq->head; |
| 147 | } |
| 148 | |
| 149 | static void *vhost_net_buf_consume(struct vhost_net_buf *rxq) |
| 150 | { |
| 151 | void *ret = vhost_net_buf_get_ptr(rxq); |
| 152 | ++rxq->head; |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | static int vhost_net_buf_produce(struct vhost_net_virtqueue *nvq) |
| 157 | { |
| 158 | struct vhost_net_buf *rxq = &nvq->rxq; |
| 159 | |
| 160 | rxq->head = 0; |
Jason Wang | 5990a30 | 2018-01-04 11:14:27 +0800 | [diff] [blame^] | 161 | rxq->tail = ptr_ring_consume_batched(nvq->rx_ring, rxq->queue, |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 162 | VHOST_RX_BATCH); |
| 163 | return rxq->tail; |
| 164 | } |
| 165 | |
| 166 | static void vhost_net_buf_unproduce(struct vhost_net_virtqueue *nvq) |
| 167 | { |
| 168 | struct vhost_net_buf *rxq = &nvq->rxq; |
| 169 | |
Jason Wang | 5990a30 | 2018-01-04 11:14:27 +0800 | [diff] [blame^] | 170 | if (nvq->rx_ring && !vhost_net_buf_is_empty(rxq)) { |
| 171 | ptr_ring_unconsume(nvq->rx_ring, rxq->queue + rxq->head, |
| 172 | vhost_net_buf_get_size(rxq), |
| 173 | __skb_array_destroy_skb); |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 174 | rxq->head = rxq->tail = 0; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | static int vhost_net_buf_peek(struct vhost_net_virtqueue *nvq) |
| 179 | { |
| 180 | struct vhost_net_buf *rxq = &nvq->rxq; |
| 181 | |
| 182 | if (!vhost_net_buf_is_empty(rxq)) |
| 183 | goto out; |
| 184 | |
| 185 | if (!vhost_net_buf_produce(nvq)) |
| 186 | return 0; |
| 187 | |
| 188 | out: |
| 189 | return __skb_array_len_with_tag(vhost_net_buf_get_ptr(rxq)); |
| 190 | } |
| 191 | |
| 192 | static void vhost_net_buf_init(struct vhost_net_buf *rxq) |
| 193 | { |
| 194 | rxq->head = rxq->tail = 0; |
| 195 | } |
| 196 | |
Asias He | fe729a5 | 2013-05-06 16:38:24 +0800 | [diff] [blame] | 197 | static void vhost_net_enable_zcopy(int vq) |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 198 | { |
Asias He | fe729a5 | 2013-05-06 16:38:24 +0800 | [diff] [blame] | 199 | vhost_net_zcopy_mask |= 0x1 << vq; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 200 | } |
| 201 | |
Asias He | fe729a5 | 2013-05-06 16:38:24 +0800 | [diff] [blame] | 202 | static struct vhost_net_ubuf_ref * |
| 203 | vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy) |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 204 | { |
Asias He | fe729a5 | 2013-05-06 16:38:24 +0800 | [diff] [blame] | 205 | struct vhost_net_ubuf_ref *ubufs; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 206 | /* No zero copy backend? Nothing to count. */ |
| 207 | if (!zcopy) |
| 208 | return NULL; |
| 209 | ubufs = kmalloc(sizeof(*ubufs), GFP_KERNEL); |
| 210 | if (!ubufs) |
| 211 | return ERR_PTR(-ENOMEM); |
Michael S. Tsirkin | 0ad8b48 | 2014-02-13 11:42:05 +0200 | [diff] [blame] | 212 | atomic_set(&ubufs->refcount, 1); |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 213 | init_waitqueue_head(&ubufs->wait); |
| 214 | ubufs->vq = vq; |
| 215 | return ubufs; |
| 216 | } |
| 217 | |
Michael S. Tsirkin | 0ad8b48 | 2014-02-13 11:42:05 +0200 | [diff] [blame] | 218 | static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref *ubufs) |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 219 | { |
Michael S. Tsirkin | 0ad8b48 | 2014-02-13 11:42:05 +0200 | [diff] [blame] | 220 | int r = atomic_sub_return(1, &ubufs->refcount); |
| 221 | if (unlikely(!r)) |
| 222 | wake_up(&ubufs->wait); |
| 223 | return r; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 224 | } |
| 225 | |
Asias He | fe729a5 | 2013-05-06 16:38:24 +0800 | [diff] [blame] | 226 | static void vhost_net_ubuf_put_and_wait(struct vhost_net_ubuf_ref *ubufs) |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 227 | { |
Michael S. Tsirkin | 0ad8b48 | 2014-02-13 11:42:05 +0200 | [diff] [blame] | 228 | vhost_net_ubuf_put(ubufs); |
| 229 | wait_event(ubufs->wait, !atomic_read(&ubufs->refcount)); |
Michael S. Tsirkin | c38e39c | 2013-06-25 17:29:46 +0300 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static void vhost_net_ubuf_put_wait_and_free(struct vhost_net_ubuf_ref *ubufs) |
| 233 | { |
| 234 | vhost_net_ubuf_put_and_wait(ubufs); |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 235 | kfree(ubufs); |
| 236 | } |
| 237 | |
Asias He | b1ad849 | 2013-05-06 11:16:00 +0800 | [diff] [blame] | 238 | static void vhost_net_clear_ubuf_info(struct vhost_net *n) |
| 239 | { |
Asias He | b1ad849 | 2013-05-06 11:16:00 +0800 | [diff] [blame] | 240 | int i; |
| 241 | |
Michael S. Tsirkin | 288cfe7 | 2013-06-06 15:20:46 +0300 | [diff] [blame] | 242 | for (i = 0; i < VHOST_NET_VQ_MAX; ++i) { |
| 243 | kfree(n->vqs[i].ubuf_info); |
| 244 | n->vqs[i].ubuf_info = NULL; |
Asias He | b1ad849 | 2013-05-06 11:16:00 +0800 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Asias He | 0a1febf | 2013-06-05 21:17:38 +0800 | [diff] [blame] | 248 | static int vhost_net_set_ubuf_info(struct vhost_net *n) |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 249 | { |
| 250 | bool zcopy; |
| 251 | int i; |
| 252 | |
Michael S. Tsirkin | 288cfe7 | 2013-06-06 15:20:46 +0300 | [diff] [blame] | 253 | for (i = 0; i < VHOST_NET_VQ_MAX; ++i) { |
Asias He | fe729a5 | 2013-05-06 16:38:24 +0800 | [diff] [blame] | 254 | zcopy = vhost_net_zcopy_mask & (0x1 << i); |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 255 | if (!zcopy) |
| 256 | continue; |
| 257 | n->vqs[i].ubuf_info = kmalloc(sizeof(*n->vqs[i].ubuf_info) * |
| 258 | UIO_MAXIOV, GFP_KERNEL); |
| 259 | if (!n->vqs[i].ubuf_info) |
| 260 | goto err; |
| 261 | } |
| 262 | return 0; |
| 263 | |
| 264 | err: |
Michael S. Tsirkin | 288cfe7 | 2013-06-06 15:20:46 +0300 | [diff] [blame] | 265 | vhost_net_clear_ubuf_info(n); |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 266 | return -ENOMEM; |
| 267 | } |
| 268 | |
Asias He | 0a1febf | 2013-06-05 21:17:38 +0800 | [diff] [blame] | 269 | static void vhost_net_vq_reset(struct vhost_net *n) |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 270 | { |
| 271 | int i; |
| 272 | |
Michael S. Tsirkin | 288cfe7 | 2013-06-06 15:20:46 +0300 | [diff] [blame] | 273 | vhost_net_clear_ubuf_info(n); |
| 274 | |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 275 | for (i = 0; i < VHOST_NET_VQ_MAX; i++) { |
| 276 | n->vqs[i].done_idx = 0; |
| 277 | n->vqs[i].upend_idx = 0; |
| 278 | n->vqs[i].ubufs = NULL; |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame] | 279 | n->vqs[i].vhost_hlen = 0; |
| 280 | n->vqs[i].sock_hlen = 0; |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 281 | vhost_net_buf_init(&n->vqs[i].rxq); |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | } |
| 285 | |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 286 | static void vhost_net_tx_packet(struct vhost_net *net) |
| 287 | { |
| 288 | ++net->tx_packets; |
| 289 | if (net->tx_packets < 1024) |
| 290 | return; |
| 291 | net->tx_packets = 0; |
| 292 | net->tx_zcopy_err = 0; |
| 293 | } |
| 294 | |
| 295 | static void vhost_net_tx_err(struct vhost_net *net) |
| 296 | { |
| 297 | ++net->tx_zcopy_err; |
| 298 | } |
| 299 | |
| 300 | static bool vhost_net_tx_select_zcopy(struct vhost_net *net) |
| 301 | { |
Michael S. Tsirkin | 1280c27 | 2012-12-04 00:17:14 +0200 | [diff] [blame] | 302 | /* TX flush waits for outstanding DMAs to be done. |
| 303 | * Don't start new DMAs. |
| 304 | */ |
| 305 | return !net->tx_flush && |
| 306 | net->tx_packets / 64 >= net->tx_zcopy_err; |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 309 | static bool vhost_sock_zcopy(struct socket *sock) |
| 310 | { |
| 311 | return unlikely(experimental_zcopytx) && |
| 312 | sock_flag(sock->sk, SOCK_ZEROCOPY); |
| 313 | } |
| 314 | |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 315 | /* In case of DMA done not in order in lower device driver for some reason. |
| 316 | * upend_idx is used to track end of used idx, done_idx is used to track head |
| 317 | * of used idx. Once lower device DMA done contiguously, we will signal KVM |
| 318 | * guest used idx. |
| 319 | */ |
Jason Wang | 094afe7 | 2013-09-02 16:40:56 +0800 | [diff] [blame] | 320 | static void vhost_zerocopy_signal_used(struct vhost_net *net, |
| 321 | struct vhost_virtqueue *vq) |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 322 | { |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 323 | struct vhost_net_virtqueue *nvq = |
| 324 | container_of(vq, struct vhost_net_virtqueue, vq); |
Jason Wang | c92112a | 2013-09-02 16:40:57 +0800 | [diff] [blame] | 325 | int i, add; |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 326 | int j = 0; |
| 327 | |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 328 | for (i = nvq->done_idx; i != nvq->upend_idx; i = (i + 1) % UIO_MAXIOV) { |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 329 | if (vq->heads[i].len == VHOST_DMA_FAILED_LEN) |
| 330 | vhost_net_tx_err(net); |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 331 | if (VHOST_DMA_IS_DONE(vq->heads[i].len)) { |
| 332 | vq->heads[i].len = VHOST_DMA_CLEAR_LEN; |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 333 | ++j; |
| 334 | } else |
| 335 | break; |
| 336 | } |
Jason Wang | c92112a | 2013-09-02 16:40:57 +0800 | [diff] [blame] | 337 | while (j) { |
| 338 | add = min(UIO_MAXIOV - nvq->done_idx, j); |
| 339 | vhost_add_used_and_signal_n(vq->dev, vq, |
| 340 | &vq->heads[nvq->done_idx], add); |
| 341 | nvq->done_idx = (nvq->done_idx + add) % UIO_MAXIOV; |
| 342 | j -= add; |
| 343 | } |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 346 | static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success) |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 347 | { |
Asias He | fe729a5 | 2013-05-06 16:38:24 +0800 | [diff] [blame] | 348 | struct vhost_net_ubuf_ref *ubufs = ubuf->ctx; |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 349 | struct vhost_virtqueue *vq = ubufs->vq; |
Michael S. Tsirkin | 0ad8b48 | 2014-02-13 11:42:05 +0200 | [diff] [blame] | 350 | int cnt; |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 351 | |
Michael S. Tsirkin | b0c057c | 2014-02-13 11:45:11 +0200 | [diff] [blame] | 352 | rcu_read_lock_bh(); |
| 353 | |
Jason Wang | 19c73b3 | 2013-09-02 16:41:00 +0800 | [diff] [blame] | 354 | /* set len to mark this desc buffers done DMA */ |
| 355 | vq->heads[ubuf->desc].len = success ? |
| 356 | VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN; |
Michael S. Tsirkin | 0ad8b48 | 2014-02-13 11:42:05 +0200 | [diff] [blame] | 357 | cnt = vhost_net_ubuf_put(ubufs); |
Jason Wang | 19c73b3 | 2013-09-02 16:41:00 +0800 | [diff] [blame] | 358 | |
Michael S. Tsirkin | 24eb21a | 2012-11-01 09:16:55 +0000 | [diff] [blame] | 359 | /* |
| 360 | * Trigger polling thread if guest stopped submitting new buffers: |
Michael S. Tsirkin | 0ad8b48 | 2014-02-13 11:42:05 +0200 | [diff] [blame] | 361 | * in this case, the refcount after decrement will eventually reach 1. |
Michael S. Tsirkin | 24eb21a | 2012-11-01 09:16:55 +0000 | [diff] [blame] | 362 | * We also trigger polling periodically after each 16 packets |
| 363 | * (the value 16 here is more or less arbitrary, it's tuned to trigger |
| 364 | * less than 10% of times). |
| 365 | */ |
Michael S. Tsirkin | 0ad8b48 | 2014-02-13 11:42:05 +0200 | [diff] [blame] | 366 | if (cnt <= 1 || !(cnt % 16)) |
Michael S. Tsirkin | 24eb21a | 2012-11-01 09:16:55 +0000 | [diff] [blame] | 367 | vhost_poll_queue(&vq->poll); |
Michael S. Tsirkin | b0c057c | 2014-02-13 11:45:11 +0200 | [diff] [blame] | 368 | |
| 369 | rcu_read_unlock_bh(); |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Jason Wang | 0308813 | 2016-03-04 06:24:53 -0500 | [diff] [blame] | 372 | static inline unsigned long busy_clock(void) |
| 373 | { |
| 374 | return local_clock() >> 10; |
| 375 | } |
| 376 | |
| 377 | static bool vhost_can_busy_poll(struct vhost_dev *dev, |
| 378 | unsigned long endtime) |
| 379 | { |
| 380 | return likely(!need_resched()) && |
| 381 | likely(!time_after(busy_clock(), endtime)) && |
| 382 | likely(!signal_pending(current)) && |
| 383 | !vhost_has_work(dev); |
| 384 | } |
| 385 | |
Jason Wang | 8241a1e | 2016-06-01 01:56:33 -0400 | [diff] [blame] | 386 | static void vhost_net_disable_vq(struct vhost_net *n, |
| 387 | struct vhost_virtqueue *vq) |
| 388 | { |
| 389 | struct vhost_net_virtqueue *nvq = |
| 390 | container_of(vq, struct vhost_net_virtqueue, vq); |
| 391 | struct vhost_poll *poll = n->poll + (nvq - n->vqs); |
| 392 | if (!vq->private_data) |
| 393 | return; |
| 394 | vhost_poll_stop(poll); |
| 395 | } |
| 396 | |
| 397 | static int vhost_net_enable_vq(struct vhost_net *n, |
| 398 | struct vhost_virtqueue *vq) |
| 399 | { |
| 400 | struct vhost_net_virtqueue *nvq = |
| 401 | container_of(vq, struct vhost_net_virtqueue, vq); |
| 402 | struct vhost_poll *poll = n->poll + (nvq - n->vqs); |
| 403 | struct socket *sock; |
| 404 | |
| 405 | sock = vq->private_data; |
| 406 | if (!sock) |
| 407 | return 0; |
| 408 | |
| 409 | return vhost_poll_start(poll, sock->file); |
| 410 | } |
| 411 | |
Jason Wang | 0308813 | 2016-03-04 06:24:53 -0500 | [diff] [blame] | 412 | static int vhost_net_tx_get_vq_desc(struct vhost_net *net, |
| 413 | struct vhost_virtqueue *vq, |
| 414 | struct iovec iov[], unsigned int iov_size, |
| 415 | unsigned int *out_num, unsigned int *in_num) |
| 416 | { |
| 417 | unsigned long uninitialized_var(endtime); |
| 418 | int r = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov), |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 419 | out_num, in_num, NULL, NULL); |
Jason Wang | 0308813 | 2016-03-04 06:24:53 -0500 | [diff] [blame] | 420 | |
| 421 | if (r == vq->num && vq->busyloop_timeout) { |
| 422 | preempt_disable(); |
| 423 | endtime = busy_clock() + vq->busyloop_timeout; |
| 424 | while (vhost_can_busy_poll(vq->dev, endtime) && |
| 425 | vhost_vq_avail_empty(vq->dev, vq)) |
Christian Borntraeger | f2f09a4 | 2016-10-25 11:03:14 +0200 | [diff] [blame] | 426 | cpu_relax(); |
Jason Wang | 0308813 | 2016-03-04 06:24:53 -0500 | [diff] [blame] | 427 | preempt_enable(); |
| 428 | r = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov), |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 429 | out_num, in_num, NULL, NULL); |
Jason Wang | 0308813 | 2016-03-04 06:24:53 -0500 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | return r; |
| 433 | } |
| 434 | |
Jason Wang | 0ed005c | 2017-01-18 15:02:02 +0800 | [diff] [blame] | 435 | static bool vhost_exceeds_maxpend(struct vhost_net *net) |
| 436 | { |
| 437 | struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX]; |
| 438 | struct vhost_virtqueue *vq = &nvq->vq; |
| 439 | |
Willem de Bruijn | 1e6f745 | 2017-10-06 13:22:31 -0400 | [diff] [blame] | 440 | return (nvq->upend_idx + UIO_MAXIOV - nvq->done_idx) % UIO_MAXIOV > |
| 441 | min_t(unsigned int, VHOST_MAX_PEND, vq->num >> 2); |
Jason Wang | 0ed005c | 2017-01-18 15:02:02 +0800 | [diff] [blame] | 442 | } |
| 443 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 444 | /* Expects to be always run from workqueue - which acts as |
| 445 | * read-size critical section for our kind of RCU. */ |
| 446 | static void handle_tx(struct vhost_net *net) |
| 447 | { |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 448 | struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX]; |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame] | 449 | struct vhost_virtqueue *vq = &nvq->vq; |
Al Viro | 98a527a | 2014-12-10 15:00:58 -0500 | [diff] [blame] | 450 | unsigned out, in; |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 451 | int head; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 452 | struct msghdr msg = { |
| 453 | .msg_name = NULL, |
| 454 | .msg_namelen = 0, |
| 455 | .msg_control = NULL, |
| 456 | .msg_controllen = 0, |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 457 | .msg_flags = MSG_DONTWAIT, |
| 458 | }; |
| 459 | size_t len, total_len = 0; |
Jason Wang | 70181d51 | 2013-04-10 20:50:48 +0000 | [diff] [blame] | 460 | int err; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 461 | size_t hdr_size; |
Arnd Bergmann | 28457ee | 2010-03-09 19:24:45 +0100 | [diff] [blame] | 462 | struct socket *sock; |
Asias He | fe729a5 | 2013-05-06 16:38:24 +0800 | [diff] [blame] | 463 | struct vhost_net_ubuf_ref *uninitialized_var(ubufs); |
Michael S. Tsirkin | cedb9bd | 2012-12-06 17:00:18 +0200 | [diff] [blame] | 464 | bool zcopy, zcopy_used; |
Arnd Bergmann | 28457ee | 2010-03-09 19:24:45 +0100 | [diff] [blame] | 465 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 466 | mutex_lock(&vq->mutex); |
Asias He | 2e26af7 | 2013-05-07 14:54:33 +0800 | [diff] [blame] | 467 | sock = vq->private_data; |
| 468 | if (!sock) |
| 469 | goto out; |
| 470 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 471 | if (!vq_iotlb_prefetch(vq)) |
| 472 | goto out; |
| 473 | |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 474 | vhost_disable_notify(&net->dev, vq); |
Jason Wang | feb8892 | 2017-11-13 11:45:34 +0800 | [diff] [blame] | 475 | vhost_net_disable_vq(net, vq); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 476 | |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame] | 477 | hdr_size = nvq->vhost_hlen; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 478 | zcopy = nvq->ubufs; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 479 | |
| 480 | for (;;) { |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 481 | /* Release DMAs done buffers first */ |
| 482 | if (zcopy) |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 483 | vhost_zerocopy_signal_used(net, vq); |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 484 | |
Jason Wang | f7c6be4 | 2013-09-02 16:41:01 +0800 | [diff] [blame] | 485 | |
Jason Wang | 0308813 | 2016-03-04 06:24:53 -0500 | [diff] [blame] | 486 | head = vhost_net_tx_get_vq_desc(net, vq, vq->iov, |
| 487 | ARRAY_SIZE(vq->iov), |
| 488 | &out, &in); |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 489 | /* On error, stop handling until the next kick. */ |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 490 | if (unlikely(head < 0)) |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 491 | break; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 492 | /* Nothing new? Wait for eventfd to tell us they refilled. */ |
| 493 | if (head == vq->num) { |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 494 | if (unlikely(vhost_enable_notify(&net->dev, vq))) { |
| 495 | vhost_disable_notify(&net->dev, vq); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 496 | continue; |
| 497 | } |
| 498 | break; |
| 499 | } |
| 500 | if (in) { |
| 501 | vq_err(vq, "Unexpected descriptor format for TX: " |
| 502 | "out %d, int %d\n", out, in); |
| 503 | break; |
| 504 | } |
| 505 | /* Skip header. TODO: support TSO. */ |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 506 | len = iov_length(vq->iov, out); |
Al Viro | c0371da | 2014-11-24 10:42:55 -0500 | [diff] [blame] | 507 | iov_iter_init(&msg.msg_iter, WRITE, vq->iov, out, len); |
Al Viro | 98a527a | 2014-12-10 15:00:58 -0500 | [diff] [blame] | 508 | iov_iter_advance(&msg.msg_iter, hdr_size); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 509 | /* Sanity check */ |
Al Viro | 01e97e6 | 2014-12-15 21:39:31 -0500 | [diff] [blame] | 510 | if (!msg_data_left(&msg)) { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 511 | vq_err(vq, "Unexpected header len for TX: " |
| 512 | "%zd expected %zd\n", |
Al Viro | 98a527a | 2014-12-10 15:00:58 -0500 | [diff] [blame] | 513 | len, hdr_size); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 514 | break; |
| 515 | } |
Al Viro | 01e97e6 | 2014-12-15 21:39:31 -0500 | [diff] [blame] | 516 | len = msg_data_left(&msg); |
Jason Wang | ce21a02 | 2013-09-02 16:40:59 +0800 | [diff] [blame] | 517 | |
| 518 | zcopy_used = zcopy && len >= VHOST_GOODCOPY_LEN |
Willem de Bruijn | 1e6f745 | 2017-10-06 13:22:31 -0400 | [diff] [blame] | 519 | && !vhost_exceeds_maxpend(net) |
Jason Wang | ce21a02 | 2013-09-02 16:40:59 +0800 | [diff] [blame] | 520 | && vhost_net_tx_select_zcopy(net); |
Michael S. Tsirkin | cedb9bd | 2012-12-06 17:00:18 +0200 | [diff] [blame] | 521 | |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 522 | /* use msg_control to pass vhost zerocopy ubuf info to skb */ |
Michael S. Tsirkin | cedb9bd | 2012-12-06 17:00:18 +0200 | [diff] [blame] | 523 | if (zcopy_used) { |
Jason Wang | ce21a02 | 2013-09-02 16:40:59 +0800 | [diff] [blame] | 524 | struct ubuf_info *ubuf; |
| 525 | ubuf = nvq->ubuf_info + nvq->upend_idx; |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 526 | |
Michael S. Tsirkin | 8b38694 | 2014-10-24 14:19:48 +0300 | [diff] [blame] | 527 | vq->heads[nvq->upend_idx].id = cpu_to_vhost32(vq, head); |
Jason Wang | ce21a02 | 2013-09-02 16:40:59 +0800 | [diff] [blame] | 528 | vq->heads[nvq->upend_idx].len = VHOST_DMA_IN_PROGRESS; |
| 529 | ubuf->callback = vhost_zerocopy_callback; |
| 530 | ubuf->ctx = nvq->ubufs; |
| 531 | ubuf->desc = nvq->upend_idx; |
Eric Dumazet | c1d1b43 | 2017-08-31 16:48:22 -0700 | [diff] [blame] | 532 | refcount_set(&ubuf->refcnt, 1); |
Jason Wang | ce21a02 | 2013-09-02 16:40:59 +0800 | [diff] [blame] | 533 | msg.msg_control = ubuf; |
| 534 | msg.msg_controllen = sizeof(ubuf); |
| 535 | ubufs = nvq->ubufs; |
Michael S. Tsirkin | 0ad8b48 | 2014-02-13 11:42:05 +0200 | [diff] [blame] | 536 | atomic_inc(&ubufs->refcount); |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 537 | nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV; |
Jason Wang | ce21a02 | 2013-09-02 16:40:59 +0800 | [diff] [blame] | 538 | } else { |
Jason Wang | 4364d5f | 2013-06-05 15:40:46 +0800 | [diff] [blame] | 539 | msg.msg_control = NULL; |
Jason Wang | ce21a02 | 2013-09-02 16:40:59 +0800 | [diff] [blame] | 540 | ubufs = NULL; |
| 541 | } |
Jason Wang | 0ed005c | 2017-01-18 15:02:02 +0800 | [diff] [blame] | 542 | |
| 543 | total_len += len; |
| 544 | if (total_len < VHOST_NET_WEIGHT && |
| 545 | !vhost_vq_avail_empty(&net->dev, vq) && |
| 546 | likely(!vhost_exceeds_maxpend(net))) { |
| 547 | msg.msg_flags |= MSG_MORE; |
| 548 | } else { |
| 549 | msg.msg_flags &= ~MSG_MORE; |
| 550 | } |
| 551 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 552 | /* TODO: Check specific error and bomb out unless ENOBUFS? */ |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 553 | err = sock->ops->sendmsg(sock, &msg, len); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 554 | if (unlikely(err < 0)) { |
Michael S. Tsirkin | cedb9bd | 2012-12-06 17:00:18 +0200 | [diff] [blame] | 555 | if (zcopy_used) { |
Jason Wang | ce21a02 | 2013-09-02 16:40:59 +0800 | [diff] [blame] | 556 | vhost_net_ubuf_put(ubufs); |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 557 | nvq->upend_idx = ((unsigned)nvq->upend_idx - 1) |
| 558 | % UIO_MAXIOV; |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 559 | } |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 560 | vhost_discard_vq_desc(vq, 1); |
Jason Wang | feb8892 | 2017-11-13 11:45:34 +0800 | [diff] [blame] | 561 | vhost_net_enable_vq(net, vq); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 562 | break; |
| 563 | } |
| 564 | if (err != len) |
Michael S. Tsirkin | 95c0ec6 | 2010-06-24 17:10:25 +0300 | [diff] [blame] | 565 | pr_debug("Truncated TX packet: " |
| 566 | " len %d != %zd\n", err, len); |
Michael S. Tsirkin | cedb9bd | 2012-12-06 17:00:18 +0200 | [diff] [blame] | 567 | if (!zcopy_used) |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 568 | vhost_add_used_and_signal(&net->dev, vq, head, 0); |
Jason Wang | c8fb217 | 2012-05-02 11:42:41 +0800 | [diff] [blame] | 569 | else |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 570 | vhost_zerocopy_signal_used(net, vq); |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 571 | vhost_net_tx_packet(net); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 572 | if (unlikely(total_len >= VHOST_NET_WEIGHT)) { |
| 573 | vhost_poll_queue(&vq->poll); |
| 574 | break; |
| 575 | } |
| 576 | } |
Asias He | 2e26af7 | 2013-05-07 14:54:33 +0800 | [diff] [blame] | 577 | out: |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 578 | mutex_unlock(&vq->mutex); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 581 | static int peek_head_len(struct vhost_net_virtqueue *rvq, struct sock *sk) |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 582 | { |
| 583 | struct sk_buff *head; |
| 584 | int len = 0; |
Jason Wang | 783e398 | 2011-01-17 16:11:17 +0800 | [diff] [blame] | 585 | unsigned long flags; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 586 | |
Jason Wang | 5990a30 | 2018-01-04 11:14:27 +0800 | [diff] [blame^] | 587 | if (rvq->rx_ring) |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 588 | return vhost_net_buf_peek(rvq); |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 589 | |
Jason Wang | 783e398 | 2011-01-17 16:11:17 +0800 | [diff] [blame] | 590 | spin_lock_irqsave(&sk->sk_receive_queue.lock, flags); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 591 | head = skb_peek(&sk->sk_receive_queue); |
Basil Gor | c53cff5e | 2012-05-03 22:55:23 +0000 | [diff] [blame] | 592 | if (likely(head)) { |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 593 | len = head->len; |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 594 | if (skb_vlan_tag_present(head)) |
Basil Gor | c53cff5e | 2012-05-03 22:55:23 +0000 | [diff] [blame] | 595 | len += VLAN_HLEN; |
| 596 | } |
| 597 | |
Jason Wang | 783e398 | 2011-01-17 16:11:17 +0800 | [diff] [blame] | 598 | spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 599 | return len; |
| 600 | } |
| 601 | |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 602 | static int sk_has_rx_data(struct sock *sk) |
| 603 | { |
| 604 | struct socket *sock = sk->sk_socket; |
| 605 | |
| 606 | if (sock->ops->peek_len) |
| 607 | return sock->ops->peek_len(sock); |
| 608 | |
| 609 | return skb_queue_empty(&sk->sk_receive_queue); |
| 610 | } |
| 611 | |
Jason Wang | 0308813 | 2016-03-04 06:24:53 -0500 | [diff] [blame] | 612 | static int vhost_net_rx_peek_head_len(struct vhost_net *net, struct sock *sk) |
| 613 | { |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 614 | struct vhost_net_virtqueue *rvq = &net->vqs[VHOST_NET_VQ_RX]; |
Jason Wang | 0308813 | 2016-03-04 06:24:53 -0500 | [diff] [blame] | 615 | struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX]; |
| 616 | struct vhost_virtqueue *vq = &nvq->vq; |
| 617 | unsigned long uninitialized_var(endtime); |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 618 | int len = peek_head_len(rvq, sk); |
Jason Wang | 0308813 | 2016-03-04 06:24:53 -0500 | [diff] [blame] | 619 | |
| 620 | if (!len && vq->busyloop_timeout) { |
| 621 | /* Both tx vq and rx socket were polled here */ |
| 622 | mutex_lock(&vq->mutex); |
| 623 | vhost_disable_notify(&net->dev, vq); |
| 624 | |
| 625 | preempt_disable(); |
| 626 | endtime = busy_clock() + vq->busyloop_timeout; |
| 627 | |
| 628 | while (vhost_can_busy_poll(&net->dev, endtime) && |
Jason Wang | 1576d98 | 2016-06-30 14:45:36 +0800 | [diff] [blame] | 629 | !sk_has_rx_data(sk) && |
Jason Wang | 0308813 | 2016-03-04 06:24:53 -0500 | [diff] [blame] | 630 | vhost_vq_avail_empty(&net->dev, vq)) |
Christian Borntraeger | f2f09a4 | 2016-10-25 11:03:14 +0200 | [diff] [blame] | 631 | cpu_relax(); |
Jason Wang | 0308813 | 2016-03-04 06:24:53 -0500 | [diff] [blame] | 632 | |
| 633 | preempt_enable(); |
| 634 | |
Jason Wang | 8b949be | 2017-09-05 09:22:05 +0800 | [diff] [blame] | 635 | if (!vhost_vq_avail_empty(&net->dev, vq)) |
Jason Wang | 0308813 | 2016-03-04 06:24:53 -0500 | [diff] [blame] | 636 | vhost_poll_queue(&vq->poll); |
Jason Wang | 8b949be | 2017-09-05 09:22:05 +0800 | [diff] [blame] | 637 | else if (unlikely(vhost_enable_notify(&net->dev, vq))) { |
| 638 | vhost_disable_notify(&net->dev, vq); |
| 639 | vhost_poll_queue(&vq->poll); |
| 640 | } |
| 641 | |
Jason Wang | 0308813 | 2016-03-04 06:24:53 -0500 | [diff] [blame] | 642 | mutex_unlock(&vq->mutex); |
| 643 | |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 644 | len = peek_head_len(rvq, sk); |
Jason Wang | 0308813 | 2016-03-04 06:24:53 -0500 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | return len; |
| 648 | } |
| 649 | |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 650 | /* This is a multi-buffer version of vhost_get_desc, that works if |
| 651 | * vq has read descriptors only. |
| 652 | * @vq - the relevant virtqueue |
| 653 | * @datalen - data length we'll be reading |
| 654 | * @iovcount - returned count of io vectors we fill |
| 655 | * @log - vhost log |
| 656 | * @log_num - log offset |
Jason Wang | 9424936 | 2011-01-17 16:11:08 +0800 | [diff] [blame] | 657 | * @quota - headcount quota, 1 for big buffer |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 658 | * returns number of buffer heads allocated, negative on error |
| 659 | */ |
| 660 | static int get_rx_bufs(struct vhost_virtqueue *vq, |
| 661 | struct vring_used_elem *heads, |
| 662 | int datalen, |
| 663 | unsigned *iovcount, |
| 664 | struct vhost_log *log, |
Jason Wang | 9424936 | 2011-01-17 16:11:08 +0800 | [diff] [blame] | 665 | unsigned *log_num, |
| 666 | unsigned int quota) |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 667 | { |
| 668 | unsigned int out, in; |
| 669 | int seg = 0; |
| 670 | int headcount = 0; |
| 671 | unsigned d; |
| 672 | int r, nlogs = 0; |
Michael S. Tsirkin | 8b38694 | 2014-10-24 14:19:48 +0300 | [diff] [blame] | 673 | /* len is always initialized before use since we are always called with |
| 674 | * datalen > 0. |
| 675 | */ |
| 676 | u32 uninitialized_var(len); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 677 | |
Jason Wang | 9424936 | 2011-01-17 16:11:08 +0800 | [diff] [blame] | 678 | while (datalen > 0 && headcount < quota) { |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 679 | if (unlikely(seg >= UIO_MAXIOV)) { |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 680 | r = -ENOBUFS; |
| 681 | goto err; |
| 682 | } |
Michael S. Tsirkin | 47283be | 2014-06-05 15:20:27 +0300 | [diff] [blame] | 683 | r = vhost_get_vq_desc(vq, vq->iov + seg, |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 684 | ARRAY_SIZE(vq->iov) - seg, &out, |
| 685 | &in, log, log_num); |
Michael S. Tsirkin | a39ee44 | 2014-03-27 12:53:37 +0200 | [diff] [blame] | 686 | if (unlikely(r < 0)) |
| 687 | goto err; |
| 688 | |
| 689 | d = r; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 690 | if (d == vq->num) { |
| 691 | r = 0; |
| 692 | goto err; |
| 693 | } |
| 694 | if (unlikely(out || in <= 0)) { |
| 695 | vq_err(vq, "unexpected descriptor format for RX: " |
| 696 | "out %d, in %d\n", out, in); |
| 697 | r = -EINVAL; |
| 698 | goto err; |
| 699 | } |
| 700 | if (unlikely(log)) { |
| 701 | nlogs += *log_num; |
| 702 | log += *log_num; |
| 703 | } |
Michael S. Tsirkin | 8b38694 | 2014-10-24 14:19:48 +0300 | [diff] [blame] | 704 | heads[headcount].id = cpu_to_vhost32(vq, d); |
| 705 | len = iov_length(vq->iov + seg, in); |
| 706 | heads[headcount].len = cpu_to_vhost32(vq, len); |
| 707 | datalen -= len; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 708 | ++headcount; |
| 709 | seg += in; |
| 710 | } |
Michael S. Tsirkin | 99975cc | 2015-01-07 10:51:00 +0200 | [diff] [blame] | 711 | heads[headcount - 1].len = cpu_to_vhost32(vq, len + datalen); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 712 | *iovcount = seg; |
| 713 | if (unlikely(log)) |
| 714 | *log_num = nlogs; |
Michael S. Tsirkin | d8316f3 | 2014-03-27 12:00:26 +0200 | [diff] [blame] | 715 | |
| 716 | /* Detect overrun */ |
| 717 | if (unlikely(datalen > 0)) { |
| 718 | r = UIO_MAXIOV + 1; |
| 719 | goto err; |
| 720 | } |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 721 | return headcount; |
| 722 | err: |
| 723 | vhost_discard_vq_desc(vq, headcount); |
| 724 | return r; |
| 725 | } |
| 726 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 727 | /* Expects to be always run from workqueue - which acts as |
| 728 | * read-size critical section for our kind of RCU. */ |
Jason Wang | 9424936 | 2011-01-17 16:11:08 +0800 | [diff] [blame] | 729 | static void handle_rx(struct vhost_net *net) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 730 | { |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame] | 731 | struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX]; |
| 732 | struct vhost_virtqueue *vq = &nvq->vq; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 733 | unsigned uninitialized_var(in), log; |
| 734 | struct vhost_log *vq_log; |
| 735 | struct msghdr msg = { |
| 736 | .msg_name = NULL, |
| 737 | .msg_namelen = 0, |
| 738 | .msg_control = NULL, /* FIXME: get and handle RX aux data. */ |
| 739 | .msg_controllen = 0, |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 740 | .msg_flags = MSG_DONTWAIT, |
| 741 | }; |
Jason Wang | 0960b64 | 2015-02-15 16:35:17 +0800 | [diff] [blame] | 742 | struct virtio_net_hdr hdr = { |
| 743 | .flags = 0, |
| 744 | .gso_type = VIRTIO_NET_HDR_GSO_NONE |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 745 | }; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 746 | size_t total_len = 0; |
Michael S. Tsirkin | 910a578 | 2012-10-24 20:37:51 +0200 | [diff] [blame] | 747 | int err, mergeable; |
| 748 | s16 headcount; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 749 | size_t vhost_hlen, sock_hlen; |
| 750 | size_t vhost_len, sock_len; |
Asias He | 2e26af7 | 2013-05-07 14:54:33 +0800 | [diff] [blame] | 751 | struct socket *sock; |
Al Viro | ba7438ae | 2014-12-10 15:51:28 -0500 | [diff] [blame] | 752 | struct iov_iter fixup; |
Jason Wang | 0960b64 | 2015-02-15 16:35:17 +0800 | [diff] [blame] | 753 | __virtio16 num_buffers; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 754 | |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 755 | mutex_lock(&vq->mutex); |
Asias He | 2e26af7 | 2013-05-07 14:54:33 +0800 | [diff] [blame] | 756 | sock = vq->private_data; |
| 757 | if (!sock) |
| 758 | goto out; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 759 | |
| 760 | if (!vq_iotlb_prefetch(vq)) |
| 761 | goto out; |
| 762 | |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 763 | vhost_disable_notify(&net->dev, vq); |
Jason Wang | 8241a1e | 2016-06-01 01:56:33 -0400 | [diff] [blame] | 764 | vhost_net_disable_vq(net, vq); |
Asias He | 2e26af7 | 2013-05-07 14:54:33 +0800 | [diff] [blame] | 765 | |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame] | 766 | vhost_hlen = nvq->vhost_hlen; |
| 767 | sock_hlen = nvq->sock_hlen; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 768 | |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 769 | vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ? |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 770 | vq->log : NULL; |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 771 | mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 772 | |
Jason Wang | 0308813 | 2016-03-04 06:24:53 -0500 | [diff] [blame] | 773 | while ((sock_len = vhost_net_rx_peek_head_len(net, sock->sk))) { |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 774 | sock_len += sock_hlen; |
| 775 | vhost_len = sock_len + vhost_hlen; |
| 776 | headcount = get_rx_bufs(vq, vq->heads, vhost_len, |
Jason Wang | 9424936 | 2011-01-17 16:11:08 +0800 | [diff] [blame] | 777 | &in, vq_log, &log, |
| 778 | likely(mergeable) ? UIO_MAXIOV : 1); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 779 | /* On error, stop handling until the next kick. */ |
| 780 | if (unlikely(headcount < 0)) |
Jason Wang | 8241a1e | 2016-06-01 01:56:33 -0400 | [diff] [blame] | 781 | goto out; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 782 | /* OK, now we need to know about added descriptors. */ |
| 783 | if (!headcount) { |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 784 | if (unlikely(vhost_enable_notify(&net->dev, vq))) { |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 785 | /* They have slipped one in as we were |
| 786 | * doing that: check again. */ |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 787 | vhost_disable_notify(&net->dev, vq); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 788 | continue; |
| 789 | } |
| 790 | /* Nothing new? Wait for eventfd to tell us |
| 791 | * they refilled. */ |
Jason Wang | 8241a1e | 2016-06-01 01:56:33 -0400 | [diff] [blame] | 792 | goto out; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 793 | } |
Jason Wang | 5990a30 | 2018-01-04 11:14:27 +0800 | [diff] [blame^] | 794 | if (nvq->rx_ring) |
Wei Xu | 6e47408 | 2017-12-01 05:10:36 -0500 | [diff] [blame] | 795 | msg.msg_control = vhost_net_buf_consume(&nvq->rxq); |
| 796 | /* On overrun, truncate and discard */ |
| 797 | if (unlikely(headcount > UIO_MAXIOV)) { |
| 798 | iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1); |
| 799 | err = sock->ops->recvmsg(sock, &msg, |
| 800 | 1, MSG_DONTWAIT | MSG_TRUNC); |
| 801 | pr_debug("Discarded rx packet: len %zd\n", sock_len); |
| 802 | continue; |
| 803 | } |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 804 | /* We don't need to be notified again. */ |
Al Viro | ba7438ae | 2014-12-10 15:51:28 -0500 | [diff] [blame] | 805 | iov_iter_init(&msg.msg_iter, READ, vq->iov, in, vhost_len); |
| 806 | fixup = msg.msg_iter; |
| 807 | if (unlikely((vhost_hlen))) { |
| 808 | /* We will supply the header ourselves |
| 809 | * TODO: support TSO. |
| 810 | */ |
| 811 | iov_iter_advance(&msg.msg_iter, vhost_hlen); |
Al Viro | ba7438ae | 2014-12-10 15:51:28 -0500 | [diff] [blame] | 812 | } |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 813 | err = sock->ops->recvmsg(sock, &msg, |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 814 | sock_len, MSG_DONTWAIT | MSG_TRUNC); |
| 815 | /* Userspace might have consumed the packet meanwhile: |
| 816 | * it's not supposed to do this usually, but might be hard |
| 817 | * to prevent. Discard data we got (if any) and keep going. */ |
| 818 | if (unlikely(err != sock_len)) { |
| 819 | pr_debug("Discarded rx packet: " |
| 820 | " len %d, expected %zd\n", err, sock_len); |
| 821 | vhost_discard_vq_desc(vq, headcount); |
| 822 | continue; |
| 823 | } |
Al Viro | ba7438ae | 2014-12-10 15:51:28 -0500 | [diff] [blame] | 824 | /* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */ |
Michael S. Tsirkin | 4c5a844 | 2015-02-25 15:19:28 +0100 | [diff] [blame] | 825 | if (unlikely(vhost_hlen)) { |
| 826 | if (copy_to_iter(&hdr, sizeof(hdr), |
| 827 | &fixup) != sizeof(hdr)) { |
| 828 | vq_err(vq, "Unable to write vnet_hdr " |
| 829 | "at addr %p\n", vq->iov->iov_base); |
Jason Wang | 8241a1e | 2016-06-01 01:56:33 -0400 | [diff] [blame] | 830 | goto out; |
Michael S. Tsirkin | 4c5a844 | 2015-02-25 15:19:28 +0100 | [diff] [blame] | 831 | } |
| 832 | } else { |
| 833 | /* Header came from socket; we'll need to patch |
| 834 | * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF |
| 835 | */ |
| 836 | iov_iter_advance(&fixup, sizeof(hdr)); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 837 | } |
| 838 | /* TODO: Should check and handle checksum. */ |
Michael S. Tsirkin | 5201aa4 | 2015-02-03 11:07:06 +0200 | [diff] [blame] | 839 | |
Jason Wang | 0960b64 | 2015-02-15 16:35:17 +0800 | [diff] [blame] | 840 | num_buffers = cpu_to_vhost16(vq, headcount); |
Jason Wang | cfbdab9 | 2011-01-17 16:10:59 +0800 | [diff] [blame] | 841 | if (likely(mergeable) && |
Michael S. Tsirkin | 0d79a49 | 2015-02-25 15:20:01 +0100 | [diff] [blame] | 842 | copy_to_iter(&num_buffers, sizeof num_buffers, |
| 843 | &fixup) != sizeof num_buffers) { |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 844 | vq_err(vq, "Failed num_buffers write"); |
| 845 | vhost_discard_vq_desc(vq, headcount); |
Jason Wang | 8241a1e | 2016-06-01 01:56:33 -0400 | [diff] [blame] | 846 | goto out; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 847 | } |
| 848 | vhost_add_used_and_signal_n(&net->dev, vq, vq->heads, |
| 849 | headcount); |
| 850 | if (unlikely(vq_log)) |
| 851 | vhost_log_write(vq, vq_log, log, vhost_len); |
| 852 | total_len += vhost_len; |
| 853 | if (unlikely(total_len >= VHOST_NET_WEIGHT)) { |
| 854 | vhost_poll_queue(&vq->poll); |
Jason Wang | 8241a1e | 2016-06-01 01:56:33 -0400 | [diff] [blame] | 855 | goto out; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 856 | } |
| 857 | } |
Jason Wang | 8241a1e | 2016-06-01 01:56:33 -0400 | [diff] [blame] | 858 | vhost_net_enable_vq(net, vq); |
Asias He | 2e26af7 | 2013-05-07 14:54:33 +0800 | [diff] [blame] | 859 | out: |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 860 | mutex_unlock(&vq->mutex); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 861 | } |
| 862 | |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 863 | static void handle_tx_kick(struct vhost_work *work) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 864 | { |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 865 | struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, |
| 866 | poll.work); |
| 867 | struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev); |
| 868 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 869 | handle_tx(net); |
| 870 | } |
| 871 | |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 872 | static void handle_rx_kick(struct vhost_work *work) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 873 | { |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 874 | struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, |
| 875 | poll.work); |
| 876 | struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev); |
| 877 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 878 | handle_rx(net); |
| 879 | } |
| 880 | |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 881 | static void handle_tx_net(struct vhost_work *work) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 882 | { |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 883 | struct vhost_net *net = container_of(work, struct vhost_net, |
| 884 | poll[VHOST_NET_VQ_TX].work); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 885 | handle_tx(net); |
| 886 | } |
| 887 | |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 888 | static void handle_rx_net(struct vhost_work *work) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 889 | { |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 890 | struct vhost_net *net = container_of(work, struct vhost_net, |
| 891 | poll[VHOST_NET_VQ_RX].work); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 892 | handle_rx(net); |
| 893 | } |
| 894 | |
| 895 | static int vhost_net_open(struct inode *inode, struct file *f) |
| 896 | { |
Michael S. Tsirkin | 23cc5a9 | 2013-01-23 21:46:47 +0100 | [diff] [blame] | 897 | struct vhost_net *n; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 898 | struct vhost_dev *dev; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 899 | struct vhost_virtqueue **vqs; |
Jason Wang | 5990a30 | 2018-01-04 11:14:27 +0800 | [diff] [blame^] | 900 | void **queue; |
Zhi Yong Wu | 59566b6e | 2013-12-07 04:13:03 +0800 | [diff] [blame] | 901 | int i; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 902 | |
Michal Hocko | dcda9b0 | 2017-07-12 14:36:45 -0700 | [diff] [blame] | 903 | n = kvmalloc(sizeof *n, GFP_KERNEL | __GFP_RETRY_MAYFAIL); |
Michal Hocko | 6c5ab65 | 2017-05-08 15:57:15 -0700 | [diff] [blame] | 904 | if (!n) |
| 905 | return -ENOMEM; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 906 | vqs = kmalloc(VHOST_NET_VQ_MAX * sizeof(*vqs), GFP_KERNEL); |
| 907 | if (!vqs) { |
Romain Francoise | d04257b | 2014-06-12 10:42:34 +0200 | [diff] [blame] | 908 | kvfree(n); |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 909 | return -ENOMEM; |
| 910 | } |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 911 | |
Jason Wang | 5990a30 | 2018-01-04 11:14:27 +0800 | [diff] [blame^] | 912 | queue = kmalloc_array(VHOST_RX_BATCH, sizeof(void *), |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 913 | GFP_KERNEL); |
| 914 | if (!queue) { |
| 915 | kfree(vqs); |
| 916 | kvfree(n); |
| 917 | return -ENOMEM; |
| 918 | } |
| 919 | n->vqs[VHOST_NET_VQ_RX].rxq.queue = queue; |
| 920 | |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 921 | dev = &n->dev; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 922 | vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq; |
| 923 | vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq; |
| 924 | n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick; |
| 925 | n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 926 | for (i = 0; i < VHOST_NET_VQ_MAX; i++) { |
| 927 | n->vqs[i].ubufs = NULL; |
| 928 | n->vqs[i].ubuf_info = NULL; |
| 929 | n->vqs[i].upend_idx = 0; |
| 930 | n->vqs[i].done_idx = 0; |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame] | 931 | n->vqs[i].vhost_hlen = 0; |
| 932 | n->vqs[i].sock_hlen = 0; |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 933 | vhost_net_buf_init(&n->vqs[i].rxq); |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 934 | } |
Zhi Yong Wu | 59566b6e | 2013-12-07 04:13:03 +0800 | [diff] [blame] | 935 | vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 936 | |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 937 | vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev); |
| 938 | vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 939 | |
| 940 | f->private_data = n; |
| 941 | |
| 942 | return 0; |
| 943 | } |
| 944 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 945 | static struct socket *vhost_net_stop_vq(struct vhost_net *n, |
| 946 | struct vhost_virtqueue *vq) |
| 947 | { |
| 948 | struct socket *sock; |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 949 | struct vhost_net_virtqueue *nvq = |
| 950 | container_of(vq, struct vhost_net_virtqueue, vq); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 951 | |
| 952 | mutex_lock(&vq->mutex); |
Asias He | 22fa90c | 2013-05-07 14:54:36 +0800 | [diff] [blame] | 953 | sock = vq->private_data; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 954 | vhost_net_disable_vq(n, vq); |
Asias He | 22fa90c | 2013-05-07 14:54:36 +0800 | [diff] [blame] | 955 | vq->private_data = NULL; |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 956 | vhost_net_buf_unproduce(nvq); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 957 | mutex_unlock(&vq->mutex); |
| 958 | return sock; |
| 959 | } |
| 960 | |
| 961 | static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock, |
| 962 | struct socket **rx_sock) |
| 963 | { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 964 | *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq); |
| 965 | *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | static void vhost_net_flush_vq(struct vhost_net *n, int index) |
| 969 | { |
| 970 | vhost_poll_flush(n->poll + index); |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 971 | vhost_poll_flush(&n->vqs[index].vq.poll); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | static void vhost_net_flush(struct vhost_net *n) |
| 975 | { |
| 976 | vhost_net_flush_vq(n, VHOST_NET_VQ_TX); |
| 977 | vhost_net_flush_vq(n, VHOST_NET_VQ_RX); |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 978 | if (n->vqs[VHOST_NET_VQ_TX].ubufs) { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 979 | mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex); |
Michael S. Tsirkin | 1280c27 | 2012-12-04 00:17:14 +0200 | [diff] [blame] | 980 | n->tx_flush = true; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 981 | mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex); |
Michael S. Tsirkin | 1280c27 | 2012-12-04 00:17:14 +0200 | [diff] [blame] | 982 | /* Wait for all lower device DMAs done. */ |
Asias He | fe729a5 | 2013-05-06 16:38:24 +0800 | [diff] [blame] | 983 | vhost_net_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs); |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 984 | mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex); |
Michael S. Tsirkin | 1280c27 | 2012-12-04 00:17:14 +0200 | [diff] [blame] | 985 | n->tx_flush = false; |
Michael S. Tsirkin | 0ad8b48 | 2014-02-13 11:42:05 +0200 | [diff] [blame] | 986 | atomic_set(&n->vqs[VHOST_NET_VQ_TX].ubufs->refcount, 1); |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 987 | mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex); |
Michael S. Tsirkin | 1280c27 | 2012-12-04 00:17:14 +0200 | [diff] [blame] | 988 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | static int vhost_net_release(struct inode *inode, struct file *f) |
| 992 | { |
| 993 | struct vhost_net *n = f->private_data; |
| 994 | struct socket *tx_sock; |
| 995 | struct socket *rx_sock; |
| 996 | |
| 997 | vhost_net_stop(n, &tx_sock, &rx_sock); |
| 998 | vhost_net_flush(n); |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 999 | vhost_dev_stop(&n->dev); |
Michael S. Tsirkin | ea5d404 | 2011-11-27 19:05:58 +0200 | [diff] [blame] | 1000 | vhost_dev_cleanup(&n->dev, false); |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame] | 1001 | vhost_net_vq_reset(n); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1002 | if (tx_sock) |
Al Viro | 09aaacf | 2014-03-05 20:39:00 -0500 | [diff] [blame] | 1003 | sockfd_put(tx_sock); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1004 | if (rx_sock) |
Al Viro | 09aaacf | 2014-03-05 20:39:00 -0500 | [diff] [blame] | 1005 | sockfd_put(rx_sock); |
Michael S. Tsirkin | b0c057c | 2014-02-13 11:45:11 +0200 | [diff] [blame] | 1006 | /* Make sure no callbacks are outstanding */ |
| 1007 | synchronize_rcu_bh(); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1008 | /* We do an extra flush before freeing memory, |
| 1009 | * since jobs can re-queue themselves. */ |
| 1010 | vhost_net_flush(n); |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 1011 | kfree(n->vqs[VHOST_NET_VQ_RX].rxq.queue); |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1012 | kfree(n->dev.vqs); |
Romain Francoise | d04257b | 2014-06-12 10:42:34 +0200 | [diff] [blame] | 1013 | kvfree(n); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1014 | return 0; |
| 1015 | } |
| 1016 | |
| 1017 | static struct socket *get_raw_socket(int fd) |
| 1018 | { |
| 1019 | struct { |
| 1020 | struct sockaddr_ll sa; |
| 1021 | char buf[MAX_ADDR_LEN]; |
| 1022 | } uaddr; |
| 1023 | int uaddr_len = sizeof uaddr, r; |
| 1024 | struct socket *sock = sockfd_lookup(fd, &r); |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 1025 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1026 | if (!sock) |
| 1027 | return ERR_PTR(-ENOTSOCK); |
| 1028 | |
| 1029 | /* Parameter checking */ |
| 1030 | if (sock->sk->sk_type != SOCK_RAW) { |
| 1031 | r = -ESOCKTNOSUPPORT; |
| 1032 | goto err; |
| 1033 | } |
| 1034 | |
| 1035 | r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa, |
| 1036 | &uaddr_len, 0); |
| 1037 | if (r) |
| 1038 | goto err; |
| 1039 | |
| 1040 | if (uaddr.sa.sll_family != AF_PACKET) { |
| 1041 | r = -EPFNOSUPPORT; |
| 1042 | goto err; |
| 1043 | } |
| 1044 | return sock; |
| 1045 | err: |
Al Viro | 09aaacf | 2014-03-05 20:39:00 -0500 | [diff] [blame] | 1046 | sockfd_put(sock); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1047 | return ERR_PTR(r); |
| 1048 | } |
| 1049 | |
Jason Wang | 5990a30 | 2018-01-04 11:14:27 +0800 | [diff] [blame^] | 1050 | static struct ptr_ring *get_tap_ptr_ring(int fd) |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 1051 | { |
Jason Wang | 5990a30 | 2018-01-04 11:14:27 +0800 | [diff] [blame^] | 1052 | struct ptr_ring *ring; |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 1053 | struct file *file = fget(fd); |
| 1054 | |
| 1055 | if (!file) |
| 1056 | return NULL; |
Jason Wang | 5990a30 | 2018-01-04 11:14:27 +0800 | [diff] [blame^] | 1057 | ring = tun_get_tx_ring(file); |
| 1058 | if (!IS_ERR(ring)) |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 1059 | goto out; |
Jason Wang | 5990a30 | 2018-01-04 11:14:27 +0800 | [diff] [blame^] | 1060 | ring = tap_get_ptr_ring(file); |
| 1061 | if (!IS_ERR(ring)) |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 1062 | goto out; |
Jason Wang | 5990a30 | 2018-01-04 11:14:27 +0800 | [diff] [blame^] | 1063 | ring = NULL; |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 1064 | out: |
| 1065 | fput(file); |
Jason Wang | 5990a30 | 2018-01-04 11:14:27 +0800 | [diff] [blame^] | 1066 | return ring; |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 1067 | } |
| 1068 | |
Arnd Bergmann | 501c774 | 2010-02-18 05:46:50 +0000 | [diff] [blame] | 1069 | static struct socket *get_tap_socket(int fd) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1070 | { |
| 1071 | struct file *file = fget(fd); |
| 1072 | struct socket *sock; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 1073 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1074 | if (!file) |
| 1075 | return ERR_PTR(-EBADF); |
| 1076 | sock = tun_get_socket(file); |
Arnd Bergmann | 501c774 | 2010-02-18 05:46:50 +0000 | [diff] [blame] | 1077 | if (!IS_ERR(sock)) |
| 1078 | return sock; |
Sainath Grandhi | 635b8c8 | 2017-02-10 16:03:47 -0800 | [diff] [blame] | 1079 | sock = tap_get_socket(file); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1080 | if (IS_ERR(sock)) |
| 1081 | fput(file); |
| 1082 | return sock; |
| 1083 | } |
| 1084 | |
| 1085 | static struct socket *get_socket(int fd) |
| 1086 | { |
| 1087 | struct socket *sock; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 1088 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1089 | /* special case to disable backend */ |
| 1090 | if (fd == -1) |
| 1091 | return NULL; |
| 1092 | sock = get_raw_socket(fd); |
| 1093 | if (!IS_ERR(sock)) |
| 1094 | return sock; |
Arnd Bergmann | 501c774 | 2010-02-18 05:46:50 +0000 | [diff] [blame] | 1095 | sock = get_tap_socket(fd); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1096 | if (!IS_ERR(sock)) |
| 1097 | return sock; |
| 1098 | return ERR_PTR(-ENOTSOCK); |
| 1099 | } |
| 1100 | |
| 1101 | static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd) |
| 1102 | { |
| 1103 | struct socket *sock, *oldsock; |
| 1104 | struct vhost_virtqueue *vq; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 1105 | struct vhost_net_virtqueue *nvq; |
Asias He | fe729a5 | 2013-05-06 16:38:24 +0800 | [diff] [blame] | 1106 | struct vhost_net_ubuf_ref *ubufs, *oldubufs = NULL; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1107 | int r; |
| 1108 | |
| 1109 | mutex_lock(&n->dev.mutex); |
| 1110 | r = vhost_dev_check_owner(&n->dev); |
| 1111 | if (r) |
| 1112 | goto err; |
| 1113 | |
| 1114 | if (index >= VHOST_NET_VQ_MAX) { |
| 1115 | r = -ENOBUFS; |
| 1116 | goto err; |
| 1117 | } |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1118 | vq = &n->vqs[index].vq; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 1119 | nvq = &n->vqs[index]; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1120 | mutex_lock(&vq->mutex); |
| 1121 | |
| 1122 | /* Verify that ring has been setup correctly. */ |
| 1123 | if (!vhost_vq_access_ok(vq)) { |
| 1124 | r = -EFAULT; |
Jeff Dike | 1dace8c | 2010-03-04 16:10:14 -0500 | [diff] [blame] | 1125 | goto err_vq; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1126 | } |
| 1127 | sock = get_socket(fd); |
| 1128 | if (IS_ERR(sock)) { |
| 1129 | r = PTR_ERR(sock); |
Jeff Dike | 1dace8c | 2010-03-04 16:10:14 -0500 | [diff] [blame] | 1130 | goto err_vq; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | /* start polling new socket */ |
Asias He | 22fa90c | 2013-05-07 14:54:36 +0800 | [diff] [blame] | 1134 | oldsock = vq->private_data; |
David S. Miller | 11fe883 | 2010-07-20 18:25:24 -0700 | [diff] [blame] | 1135 | if (sock != oldsock) { |
Asias He | fe729a5 | 2013-05-06 16:38:24 +0800 | [diff] [blame] | 1136 | ubufs = vhost_net_ubuf_alloc(vq, |
| 1137 | sock && vhost_sock_zcopy(sock)); |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 1138 | if (IS_ERR(ubufs)) { |
| 1139 | r = PTR_ERR(ubufs); |
| 1140 | goto err_ubufs; |
| 1141 | } |
Jason Wang | 692a998 | 2013-01-28 01:05:17 +0000 | [diff] [blame] | 1142 | |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 1143 | vhost_net_disable_vq(n, vq); |
Asias He | 22fa90c | 2013-05-07 14:54:36 +0800 | [diff] [blame] | 1144 | vq->private_data = sock; |
Jason Wang | c67df11 | 2017-05-17 12:14:45 +0800 | [diff] [blame] | 1145 | vhost_net_buf_unproduce(nvq); |
| 1146 | if (index == VHOST_NET_VQ_RX) |
Jason Wang | 5990a30 | 2018-01-04 11:14:27 +0800 | [diff] [blame^] | 1147 | nvq->rx_ring = get_tap_ptr_ring(fd); |
Greg Kurz | 80f7d03 | 2016-02-16 15:59:44 +0100 | [diff] [blame] | 1148 | r = vhost_vq_init_access(vq); |
Jason Wang | f59281d | 2011-06-21 18:04:27 +0800 | [diff] [blame] | 1149 | if (r) |
Jason Wang | 692a998 | 2013-01-28 01:05:17 +0000 | [diff] [blame] | 1150 | goto err_used; |
Jason Wang | 2b8b328 | 2013-01-28 01:05:18 +0000 | [diff] [blame] | 1151 | r = vhost_net_enable_vq(n, vq); |
| 1152 | if (r) |
| 1153 | goto err_used; |
Jason Wang | 692a998 | 2013-01-28 01:05:17 +0000 | [diff] [blame] | 1154 | |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 1155 | oldubufs = nvq->ubufs; |
| 1156 | nvq->ubufs = ubufs; |
Michael S. Tsirkin | 64e9a9b | 2012-12-03 07:31:51 +0000 | [diff] [blame] | 1157 | |
| 1158 | n->tx_packets = 0; |
| 1159 | n->tx_zcopy_err = 0; |
Michael S. Tsirkin | 1280c27 | 2012-12-04 00:17:14 +0200 | [diff] [blame] | 1160 | n->tx_flush = false; |
Jeff Dike | dd1f407 | 2010-03-04 16:10:14 -0500 | [diff] [blame] | 1161 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1162 | |
Michael S. Tsirkin | 1680e90 | 2010-07-15 15:19:12 +0300 | [diff] [blame] | 1163 | mutex_unlock(&vq->mutex); |
| 1164 | |
Michael S. Tsirkin | c047e5f | 2011-07-20 13:41:31 +0300 | [diff] [blame] | 1165 | if (oldubufs) { |
Michael S. Tsirkin | c38e39c | 2013-06-25 17:29:46 +0300 | [diff] [blame] | 1166 | vhost_net_ubuf_put_wait_and_free(oldubufs); |
Michael S. Tsirkin | c047e5f | 2011-07-20 13:41:31 +0300 | [diff] [blame] | 1167 | mutex_lock(&vq->mutex); |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 1168 | vhost_zerocopy_signal_used(n, vq); |
Michael S. Tsirkin | c047e5f | 2011-07-20 13:41:31 +0300 | [diff] [blame] | 1169 | mutex_unlock(&vq->mutex); |
| 1170 | } |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 1171 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1172 | if (oldsock) { |
| 1173 | vhost_net_flush_vq(n, index); |
Al Viro | 09aaacf | 2014-03-05 20:39:00 -0500 | [diff] [blame] | 1174 | sockfd_put(oldsock); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1175 | } |
Jeff Dike | 1dace8c | 2010-03-04 16:10:14 -0500 | [diff] [blame] | 1176 | |
Michael S. Tsirkin | 1680e90 | 2010-07-15 15:19:12 +0300 | [diff] [blame] | 1177 | mutex_unlock(&n->dev.mutex); |
| 1178 | return 0; |
| 1179 | |
Jason Wang | 692a998 | 2013-01-28 01:05:17 +0000 | [diff] [blame] | 1180 | err_used: |
Asias He | 22fa90c | 2013-05-07 14:54:36 +0800 | [diff] [blame] | 1181 | vq->private_data = oldsock; |
Jason Wang | 692a998 | 2013-01-28 01:05:17 +0000 | [diff] [blame] | 1182 | vhost_net_enable_vq(n, vq); |
| 1183 | if (ubufs) |
Michael S. Tsirkin | c38e39c | 2013-06-25 17:29:46 +0300 | [diff] [blame] | 1184 | vhost_net_ubuf_put_wait_and_free(ubufs); |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 1185 | err_ubufs: |
Al Viro | 09aaacf | 2014-03-05 20:39:00 -0500 | [diff] [blame] | 1186 | sockfd_put(sock); |
Jeff Dike | 1dace8c | 2010-03-04 16:10:14 -0500 | [diff] [blame] | 1187 | err_vq: |
| 1188 | mutex_unlock(&vq->mutex); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1189 | err: |
| 1190 | mutex_unlock(&n->dev.mutex); |
| 1191 | return r; |
| 1192 | } |
| 1193 | |
| 1194 | static long vhost_net_reset_owner(struct vhost_net *n) |
| 1195 | { |
| 1196 | struct socket *tx_sock = NULL; |
| 1197 | struct socket *rx_sock = NULL; |
| 1198 | long err; |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 1199 | struct vhost_umem *umem; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 1200 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1201 | mutex_lock(&n->dev.mutex); |
| 1202 | err = vhost_dev_check_owner(&n->dev); |
| 1203 | if (err) |
| 1204 | goto done; |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 1205 | umem = vhost_dev_reset_owner_prepare(); |
| 1206 | if (!umem) { |
Michael S. Tsirkin | 150b9e5 | 2013-04-28 17:12:08 +0300 | [diff] [blame] | 1207 | err = -ENOMEM; |
| 1208 | goto done; |
| 1209 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1210 | vhost_net_stop(n, &tx_sock, &rx_sock); |
| 1211 | vhost_net_flush(n); |
Jason Wang | a9709d6 | 2016-06-23 02:04:31 -0400 | [diff] [blame] | 1212 | vhost_dev_reset_owner(&n->dev, umem); |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame] | 1213 | vhost_net_vq_reset(n); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1214 | done: |
| 1215 | mutex_unlock(&n->dev.mutex); |
| 1216 | if (tx_sock) |
Al Viro | 09aaacf | 2014-03-05 20:39:00 -0500 | [diff] [blame] | 1217 | sockfd_put(tx_sock); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1218 | if (rx_sock) |
Al Viro | 09aaacf | 2014-03-05 20:39:00 -0500 | [diff] [blame] | 1219 | sockfd_put(rx_sock); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1220 | return err; |
| 1221 | } |
| 1222 | |
| 1223 | static int vhost_net_set_features(struct vhost_net *n, u64 features) |
| 1224 | { |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 1225 | size_t vhost_hlen, sock_hlen, hdr_len; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1226 | int i; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 1227 | |
Michael S. Tsirkin | e4fca7d | 2014-10-24 14:23:52 +0300 | [diff] [blame] | 1228 | hdr_len = (features & ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | |
| 1229 | (1ULL << VIRTIO_F_VERSION_1))) ? |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 1230 | sizeof(struct virtio_net_hdr_mrg_rxbuf) : |
| 1231 | sizeof(struct virtio_net_hdr); |
| 1232 | if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) { |
| 1233 | /* vhost provides vnet_hdr */ |
| 1234 | vhost_hlen = hdr_len; |
| 1235 | sock_hlen = 0; |
| 1236 | } else { |
| 1237 | /* socket provides vnet_hdr */ |
| 1238 | vhost_hlen = 0; |
| 1239 | sock_hlen = hdr_len; |
| 1240 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1241 | mutex_lock(&n->dev.mutex); |
| 1242 | if ((features & (1 << VHOST_F_LOG_ALL)) && |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1243 | !vhost_log_access_ok(&n->dev)) |
| 1244 | goto out_unlock; |
| 1245 | |
| 1246 | if ((features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))) { |
| 1247 | if (vhost_init_device_iotlb(&n->dev, true)) |
| 1248 | goto out_unlock; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1249 | } |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1250 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1251 | for (i = 0; i < VHOST_NET_VQ_MAX; ++i) { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1252 | mutex_lock(&n->vqs[i].vq.mutex); |
Michael S. Tsirkin | ea16c51 | 2014-06-05 15:20:23 +0300 | [diff] [blame] | 1253 | n->vqs[i].vq.acked_features = features; |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame] | 1254 | n->vqs[i].vhost_hlen = vhost_hlen; |
| 1255 | n->vqs[i].sock_hlen = sock_hlen; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1256 | mutex_unlock(&n->vqs[i].vq.mutex); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1257 | } |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1258 | mutex_unlock(&n->dev.mutex); |
| 1259 | return 0; |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1260 | |
| 1261 | out_unlock: |
| 1262 | mutex_unlock(&n->dev.mutex); |
| 1263 | return -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Asias He | b1ad849 | 2013-05-06 11:16:00 +0800 | [diff] [blame] | 1266 | static long vhost_net_set_owner(struct vhost_net *n) |
| 1267 | { |
| 1268 | int r; |
| 1269 | |
| 1270 | mutex_lock(&n->dev.mutex); |
Michael S. Tsirkin | 05c0535 | 2013-06-06 15:20:39 +0300 | [diff] [blame] | 1271 | if (vhost_dev_has_owner(&n->dev)) { |
| 1272 | r = -EBUSY; |
| 1273 | goto out; |
| 1274 | } |
Asias He | b1ad849 | 2013-05-06 11:16:00 +0800 | [diff] [blame] | 1275 | r = vhost_net_set_ubuf_info(n); |
| 1276 | if (r) |
| 1277 | goto out; |
| 1278 | r = vhost_dev_set_owner(&n->dev); |
| 1279 | if (r) |
| 1280 | vhost_net_clear_ubuf_info(n); |
| 1281 | vhost_net_flush(n); |
| 1282 | out: |
| 1283 | mutex_unlock(&n->dev.mutex); |
| 1284 | return r; |
| 1285 | } |
| 1286 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1287 | static long vhost_net_ioctl(struct file *f, unsigned int ioctl, |
| 1288 | unsigned long arg) |
| 1289 | { |
| 1290 | struct vhost_net *n = f->private_data; |
| 1291 | void __user *argp = (void __user *)arg; |
| 1292 | u64 __user *featurep = argp; |
| 1293 | struct vhost_vring_file backend; |
| 1294 | u64 features; |
| 1295 | int r; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 1296 | |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1297 | switch (ioctl) { |
| 1298 | case VHOST_NET_SET_BACKEND: |
Takuya Yoshikawa | d3553a5 | 2010-05-27 19:01:58 +0900 | [diff] [blame] | 1299 | if (copy_from_user(&backend, argp, sizeof backend)) |
| 1300 | return -EFAULT; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1301 | return vhost_net_set_backend(n, backend.index, backend.fd); |
| 1302 | case VHOST_GET_FEATURES: |
Stefan Hajnoczi | 0dd05a3 | 2012-07-21 06:55:36 +0000 | [diff] [blame] | 1303 | features = VHOST_NET_FEATURES; |
Takuya Yoshikawa | d3553a5 | 2010-05-27 19:01:58 +0900 | [diff] [blame] | 1304 | if (copy_to_user(featurep, &features, sizeof features)) |
| 1305 | return -EFAULT; |
| 1306 | return 0; |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1307 | case VHOST_SET_FEATURES: |
Takuya Yoshikawa | d3553a5 | 2010-05-27 19:01:58 +0900 | [diff] [blame] | 1308 | if (copy_from_user(&features, featurep, sizeof features)) |
| 1309 | return -EFAULT; |
Stefan Hajnoczi | 0dd05a3 | 2012-07-21 06:55:36 +0000 | [diff] [blame] | 1310 | if (features & ~VHOST_NET_FEATURES) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1311 | return -EOPNOTSUPP; |
| 1312 | return vhost_net_set_features(n, features); |
| 1313 | case VHOST_RESET_OWNER: |
| 1314 | return vhost_net_reset_owner(n); |
Asias He | b1ad849 | 2013-05-06 11:16:00 +0800 | [diff] [blame] | 1315 | case VHOST_SET_OWNER: |
| 1316 | return vhost_net_set_owner(n); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1317 | default: |
| 1318 | mutex_lock(&n->dev.mutex); |
Michael S. Tsirkin | 935cdee | 2012-12-06 14:03:34 +0200 | [diff] [blame] | 1319 | r = vhost_dev_ioctl(&n->dev, ioctl, argp); |
| 1320 | if (r == -ENOIOCTLCMD) |
| 1321 | r = vhost_vring_ioctl(&n->dev, ioctl, argp); |
| 1322 | else |
| 1323 | vhost_net_flush(n); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1324 | mutex_unlock(&n->dev.mutex); |
| 1325 | return r; |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | #ifdef CONFIG_COMPAT |
| 1330 | static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl, |
| 1331 | unsigned long arg) |
| 1332 | { |
| 1333 | return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg)); |
| 1334 | } |
| 1335 | #endif |
| 1336 | |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1337 | static ssize_t vhost_net_chr_read_iter(struct kiocb *iocb, struct iov_iter *to) |
| 1338 | { |
| 1339 | struct file *file = iocb->ki_filp; |
| 1340 | struct vhost_net *n = file->private_data; |
| 1341 | struct vhost_dev *dev = &n->dev; |
| 1342 | int noblock = file->f_flags & O_NONBLOCK; |
| 1343 | |
| 1344 | return vhost_chr_read_iter(dev, to, noblock); |
| 1345 | } |
| 1346 | |
| 1347 | static ssize_t vhost_net_chr_write_iter(struct kiocb *iocb, |
| 1348 | struct iov_iter *from) |
| 1349 | { |
| 1350 | struct file *file = iocb->ki_filp; |
| 1351 | struct vhost_net *n = file->private_data; |
| 1352 | struct vhost_dev *dev = &n->dev; |
| 1353 | |
| 1354 | return vhost_chr_write_iter(dev, from); |
| 1355 | } |
| 1356 | |
| 1357 | static unsigned int vhost_net_chr_poll(struct file *file, poll_table *wait) |
| 1358 | { |
| 1359 | struct vhost_net *n = file->private_data; |
| 1360 | struct vhost_dev *dev = &n->dev; |
| 1361 | |
| 1362 | return vhost_chr_poll(file, dev, wait); |
| 1363 | } |
| 1364 | |
Tobias Klauser | 373a83a | 2010-05-17 15:12:49 +0200 | [diff] [blame] | 1365 | static const struct file_operations vhost_net_fops = { |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1366 | .owner = THIS_MODULE, |
| 1367 | .release = vhost_net_release, |
Jason Wang | 6b1e6cc | 2016-06-23 02:04:32 -0400 | [diff] [blame] | 1368 | .read_iter = vhost_net_chr_read_iter, |
| 1369 | .write_iter = vhost_net_chr_write_iter, |
| 1370 | .poll = vhost_net_chr_poll, |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1371 | .unlocked_ioctl = vhost_net_ioctl, |
| 1372 | #ifdef CONFIG_COMPAT |
| 1373 | .compat_ioctl = vhost_net_compat_ioctl, |
| 1374 | #endif |
| 1375 | .open = vhost_net_open, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1376 | .llseek = noop_llseek, |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1377 | }; |
| 1378 | |
| 1379 | static struct miscdevice vhost_net_misc = { |
stephen hemminger | 7c7c7f0 | 2012-01-11 19:30:38 +0000 | [diff] [blame] | 1380 | .minor = VHOST_NET_MINOR, |
| 1381 | .name = "vhost-net", |
| 1382 | .fops = &vhost_net_fops, |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1383 | }; |
| 1384 | |
Christoph Hellwig | a8d3782 | 2010-04-13 14:11:25 -0400 | [diff] [blame] | 1385 | static int vhost_net_init(void) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1386 | { |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 1387 | if (experimental_zcopytx) |
Asias He | fe729a5 | 2013-05-06 16:38:24 +0800 | [diff] [blame] | 1388 | vhost_net_enable_zcopy(VHOST_NET_VQ_TX); |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 1389 | return misc_register(&vhost_net_misc); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1390 | } |
| 1391 | module_init(vhost_net_init); |
| 1392 | |
Christoph Hellwig | a8d3782 | 2010-04-13 14:11:25 -0400 | [diff] [blame] | 1393 | static void vhost_net_exit(void) |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1394 | { |
| 1395 | misc_deregister(&vhost_net_misc); |
Michael S. Tsirkin | 3a4d5c9 | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1396 | } |
| 1397 | module_exit(vhost_net_exit); |
| 1398 | |
| 1399 | MODULE_VERSION("0.0.1"); |
| 1400 | MODULE_LICENSE("GPL v2"); |
| 1401 | MODULE_AUTHOR("Michael S. Tsirkin"); |
| 1402 | MODULE_DESCRIPTION("Host kernel accelerator for virtio net"); |
stephen hemminger | 7c7c7f0 | 2012-01-11 19:30:38 +0000 | [diff] [blame] | 1403 | MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR); |
| 1404 | MODULE_ALIAS("devname:vhost-net"); |