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