4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/types.h>
43 #include <rte_common.h>
46 #include "virtio-net.h"
47 #include "virtio-net-user.h"
48 #include "vhost-net-user.h"
49 #include "vhost-net.h"
51 struct orig_region_map {
53 uint64_t mapped_address;
58 #define orig_region(ptr, nregions) \
59 ((struct orig_region_map *)RTE_PTR_ADD((ptr), \
60 sizeof(struct virtio_memory) + \
61 sizeof(struct virtio_memory_regions) * (nregions)))
69 return (uint64_t)stat.st_blksize;
73 free_mem_region(struct virtio_net *dev)
75 struct orig_region_map *region;
78 if (!dev || !dev->mem)
81 region = orig_region(dev->mem, dev->mem->nregions);
82 for (idx = 0; idx < dev->mem->nregions; idx++) {
83 if (region[idx].mapped_address) {
84 munmap((void *)(uintptr_t)region[idx].mapped_address,
85 region[idx].mapped_size);
86 close(region[idx].fd);
92 user_set_mem_table(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg)
94 struct VhostUserMemory memory = pmsg->payload.memory;
95 struct virtio_memory_regions *pregion;
96 uint64_t mapped_address, mapped_size;
97 struct virtio_net *dev;
99 struct orig_region_map *pregion_orig;
102 /* unmap old memory regions one by one*/
103 dev = get_device(ctx);
107 /* Remove from the data plane. */
108 if (dev->flags & VIRTIO_DEV_RUNNING)
109 notify_ops->destroy_device(dev);
112 free_mem_region(dev);
118 sizeof(struct virtio_memory) +
119 sizeof(struct virtio_memory_regions) * memory.nregions +
120 sizeof(struct orig_region_map) * memory.nregions);
121 if (dev->mem == NULL) {
122 RTE_LOG(ERR, VHOST_CONFIG,
123 "(%"PRIu64") Failed to allocate memory for dev->mem\n",
127 dev->mem->nregions = memory.nregions;
129 pregion_orig = orig_region(dev->mem, memory.nregions);
130 for (idx = 0; idx < memory.nregions; idx++) {
131 pregion = &dev->mem->regions[idx];
132 pregion->guest_phys_address =
133 memory.regions[idx].guest_phys_addr;
134 pregion->guest_phys_address_end =
135 memory.regions[idx].guest_phys_addr +
136 memory.regions[idx].memory_size;
137 pregion->memory_size =
138 memory.regions[idx].memory_size;
139 pregion->userspace_address =
140 memory.regions[idx].userspace_addr;
143 mapped_size = memory.regions[idx].memory_size +
144 memory.regions[idx].mmap_offset;
146 /* mmap() without flag of MAP_ANONYMOUS, should be called
147 * with length argument aligned with hugepagesz at older
148 * longterm version Linux, like 2.6.32 and 3.2.72, or
149 * mmap() will fail with EINVAL.
151 * to avoid failure, make sure in caller to keep length
154 alignment = get_blk_size(pmsg->fds[idx]);
155 mapped_size = RTE_ALIGN_CEIL(mapped_size, alignment);
157 mapped_address = (uint64_t)(uintptr_t)mmap(NULL,
159 PROT_READ | PROT_WRITE, MAP_SHARED,
163 RTE_LOG(INFO, VHOST_CONFIG,
164 "mapped region %d fd:%d to:%p sz:0x%"PRIx64" "
165 "off:0x%"PRIx64" align:0x%"PRIx64"\n",
166 idx, pmsg->fds[idx], (void *)(uintptr_t)mapped_address,
167 mapped_size, memory.regions[idx].mmap_offset,
170 if (mapped_address == (uint64_t)(uintptr_t)MAP_FAILED) {
171 RTE_LOG(ERR, VHOST_CONFIG,
172 "mmap qemu guest failed.\n");
176 pregion_orig[idx].mapped_address = mapped_address;
177 pregion_orig[idx].mapped_size = mapped_size;
178 pregion_orig[idx].blksz = alignment;
179 pregion_orig[idx].fd = pmsg->fds[idx];
181 mapped_address += memory.regions[idx].mmap_offset;
183 pregion->address_offset = mapped_address -
184 pregion->guest_phys_address;
186 if (memory.regions[idx].guest_phys_addr == 0) {
187 dev->mem->base_address =
188 memory.regions[idx].userspace_addr;
189 dev->mem->mapped_address =
190 pregion->address_offset;
193 LOG_DEBUG(VHOST_CONFIG,
194 "REGION: %u GPA: %p QEMU VA: %p SIZE (%"PRIu64")\n",
196 (void *)(uintptr_t)pregion->guest_phys_address,
197 (void *)(uintptr_t)pregion->userspace_address,
198 pregion->memory_size);
205 munmap((void *)(uintptr_t)pregion_orig[idx].mapped_address,
206 pregion_orig[idx].mapped_size);
207 close(pregion_orig[idx].fd);
215 vq_is_ready(struct vhost_virtqueue *vq)
217 return vq && vq->desc &&
223 virtio_is_ready(struct virtio_net *dev)
225 struct vhost_virtqueue *rvq, *tvq;
228 for (i = 0; i < dev->virt_qp_nb; i++) {
229 rvq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ];
230 tvq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ];
232 if (!vq_is_ready(rvq) || !vq_is_ready(tvq)) {
233 RTE_LOG(INFO, VHOST_CONFIG,
234 "virtio is not ready for processing.\n");
239 RTE_LOG(INFO, VHOST_CONFIG,
240 "virtio is now ready for processing.\n");
245 user_set_vring_call(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg)
247 struct vhost_vring_file file;
249 file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
250 if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
253 file.fd = pmsg->fds[0];
254 RTE_LOG(INFO, VHOST_CONFIG,
255 "vring call idx:%d file:%d\n", file.index, file.fd);
256 ops->set_vring_call(ctx, &file);
261 * In vhost-user, when we receive kick message, will test whether virtio
262 * device is ready for packet processing.
265 user_set_vring_kick(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg)
267 struct vhost_vring_file file;
268 struct virtio_net *dev = get_device(ctx);
270 file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
271 if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
274 file.fd = pmsg->fds[0];
275 RTE_LOG(INFO, VHOST_CONFIG,
276 "vring kick idx:%d file:%d\n", file.index, file.fd);
277 ops->set_vring_kick(ctx, &file);
279 if (virtio_is_ready(dev) &&
280 !(dev->flags & VIRTIO_DEV_RUNNING))
281 notify_ops->new_device(dev);
285 * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
288 user_get_vring_base(struct vhost_device_ctx ctx,
289 struct vhost_vring_state *state)
291 struct virtio_net *dev = get_device(ctx);
295 /* We have to stop the queue (virtio) if it is running. */
296 if (dev->flags & VIRTIO_DEV_RUNNING)
297 notify_ops->destroy_device(dev);
299 /* Here we are safe to get the last used index */
300 ops->get_vring_base(ctx, state->index, state);
302 RTE_LOG(INFO, VHOST_CONFIG,
303 "vring base idx:%d file:%d\n", state->index, state->num);
305 * Based on current qemu vhost-user implementation, this message is
306 * sent and only sent in vhost_vring_stop.
307 * TODO: cleanup the vring, it isn't usable since here.
309 if (dev->virtqueue[state->index]->kickfd >= 0) {
310 close(dev->virtqueue[state->index]->kickfd);
311 dev->virtqueue[state->index]->kickfd = -1;
318 * when virtio queues are ready to work, qemu will send us to
319 * enable the virtio queue pair.
322 user_set_vring_enable(struct vhost_device_ctx ctx,
323 struct vhost_vring_state *state)
325 struct virtio_net *dev = get_device(ctx);
326 int enable = (int)state->num;
328 RTE_LOG(INFO, VHOST_CONFIG,
329 "set queue enable: %d to qp idx: %d\n",
330 enable, state->index);
332 if (notify_ops->vring_state_changed) {
333 notify_ops->vring_state_changed(dev, state->index, enable);
336 dev->virtqueue[state->index]->enabled = enable;
342 user_destroy_device(struct vhost_device_ctx ctx)
344 struct virtio_net *dev = get_device(ctx);
346 if (dev && (dev->flags & VIRTIO_DEV_RUNNING))
347 notify_ops->destroy_device(dev);
349 if (dev && dev->mem) {
350 free_mem_region(dev);
357 user_set_protocol_features(struct vhost_device_ctx ctx,
358 uint64_t protocol_features)
360 struct virtio_net *dev;
362 dev = get_device(ctx);
363 if (dev == NULL || protocol_features & ~VHOST_USER_PROTOCOL_FEATURES)
366 dev->protocol_features = protocol_features;