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.
40 #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 vhost_backend_cleanup(struct virtio_net *dev)
102 user_set_mem_table(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg)
104 struct VhostUserMemory memory = pmsg->payload.memory;
105 struct virtio_memory_regions *pregion;
106 uint64_t mapped_address, mapped_size;
107 struct virtio_net *dev;
108 unsigned int idx = 0;
109 struct orig_region_map *pregion_orig;
112 /* unmap old memory regions one by one*/
113 dev = get_device(ctx);
117 /* Remove from the data plane. */
118 if (dev->flags & VIRTIO_DEV_RUNNING)
119 notify_ops->destroy_device(dev);
122 free_mem_region(dev);
128 sizeof(struct virtio_memory) +
129 sizeof(struct virtio_memory_regions) * memory.nregions +
130 sizeof(struct orig_region_map) * memory.nregions);
131 if (dev->mem == NULL) {
132 RTE_LOG(ERR, VHOST_CONFIG,
133 "(%"PRIu64") Failed to allocate memory for dev->mem\n",
137 dev->mem->nregions = memory.nregions;
139 pregion_orig = orig_region(dev->mem, memory.nregions);
140 for (idx = 0; idx < memory.nregions; idx++) {
141 pregion = &dev->mem->regions[idx];
142 pregion->guest_phys_address =
143 memory.regions[idx].guest_phys_addr;
144 pregion->guest_phys_address_end =
145 memory.regions[idx].guest_phys_addr +
146 memory.regions[idx].memory_size;
147 pregion->memory_size =
148 memory.regions[idx].memory_size;
149 pregion->userspace_address =
150 memory.regions[idx].userspace_addr;
153 mapped_size = memory.regions[idx].memory_size +
154 memory.regions[idx].mmap_offset;
156 /* mmap() without flag of MAP_ANONYMOUS, should be called
157 * with length argument aligned with hugepagesz at older
158 * longterm version Linux, like 2.6.32 and 3.2.72, or
159 * mmap() will fail with EINVAL.
161 * to avoid failure, make sure in caller to keep length
164 alignment = get_blk_size(pmsg->fds[idx]);
165 mapped_size = RTE_ALIGN_CEIL(mapped_size, alignment);
167 mapped_address = (uint64_t)(uintptr_t)mmap(NULL,
169 PROT_READ | PROT_WRITE, MAP_SHARED,
173 RTE_LOG(INFO, VHOST_CONFIG,
174 "mapped region %d fd:%d to:%p sz:0x%"PRIx64" "
175 "off:0x%"PRIx64" align:0x%"PRIx64"\n",
176 idx, pmsg->fds[idx], (void *)(uintptr_t)mapped_address,
177 mapped_size, memory.regions[idx].mmap_offset,
180 if (mapped_address == (uint64_t)(uintptr_t)MAP_FAILED) {
181 RTE_LOG(ERR, VHOST_CONFIG,
182 "mmap qemu guest failed.\n");
186 pregion_orig[idx].mapped_address = mapped_address;
187 pregion_orig[idx].mapped_size = mapped_size;
188 pregion_orig[idx].blksz = alignment;
189 pregion_orig[idx].fd = pmsg->fds[idx];
191 mapped_address += memory.regions[idx].mmap_offset;
193 pregion->address_offset = mapped_address -
194 pregion->guest_phys_address;
196 if (memory.regions[idx].guest_phys_addr == 0) {
197 dev->mem->base_address =
198 memory.regions[idx].userspace_addr;
199 dev->mem->mapped_address =
200 pregion->address_offset;
203 LOG_DEBUG(VHOST_CONFIG,
204 "REGION: %u GPA: %p QEMU VA: %p SIZE (%"PRIu64")\n",
206 (void *)(uintptr_t)pregion->guest_phys_address,
207 (void *)(uintptr_t)pregion->userspace_address,
208 pregion->memory_size);
215 munmap((void *)(uintptr_t)pregion_orig[idx].mapped_address,
216 pregion_orig[idx].mapped_size);
217 close(pregion_orig[idx].fd);
225 vq_is_ready(struct vhost_virtqueue *vq)
227 return vq && vq->desc &&
233 virtio_is_ready(struct virtio_net *dev)
235 struct vhost_virtqueue *rvq, *tvq;
238 for (i = 0; i < dev->virt_qp_nb; i++) {
239 rvq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ];
240 tvq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ];
242 if (!vq_is_ready(rvq) || !vq_is_ready(tvq)) {
243 RTE_LOG(INFO, VHOST_CONFIG,
244 "virtio is not ready for processing.\n");
249 RTE_LOG(INFO, VHOST_CONFIG,
250 "virtio is now ready for processing.\n");
255 user_set_vring_call(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg)
257 struct vhost_vring_file file;
259 file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
260 if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
263 file.fd = pmsg->fds[0];
264 RTE_LOG(INFO, VHOST_CONFIG,
265 "vring call idx:%d file:%d\n", file.index, file.fd);
266 vhost_set_vring_call(ctx, &file);
271 * In vhost-user, when we receive kick message, will test whether virtio
272 * device is ready for packet processing.
275 user_set_vring_kick(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg)
277 struct vhost_vring_file file;
278 struct virtio_net *dev = get_device(ctx);
280 file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
281 if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
284 file.fd = pmsg->fds[0];
285 RTE_LOG(INFO, VHOST_CONFIG,
286 "vring kick idx:%d file:%d\n", file.index, file.fd);
287 vhost_set_vring_kick(ctx, &file);
289 if (virtio_is_ready(dev) &&
290 !(dev->flags & VIRTIO_DEV_RUNNING))
291 notify_ops->new_device(dev);
295 * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
298 user_get_vring_base(struct vhost_device_ctx ctx,
299 struct vhost_vring_state *state)
301 struct virtio_net *dev = get_device(ctx);
305 /* We have to stop the queue (virtio) if it is running. */
306 if (dev->flags & VIRTIO_DEV_RUNNING)
307 notify_ops->destroy_device(dev);
309 /* Here we are safe to get the last used index */
310 vhost_get_vring_base(ctx, state->index, state);
312 RTE_LOG(INFO, VHOST_CONFIG,
313 "vring base idx:%d file:%d\n", state->index, state->num);
315 * Based on current qemu vhost-user implementation, this message is
316 * sent and only sent in vhost_vring_stop.
317 * TODO: cleanup the vring, it isn't usable since here.
319 if (dev->virtqueue[state->index]->kickfd >= 0) {
320 close(dev->virtqueue[state->index]->kickfd);
321 dev->virtqueue[state->index]->kickfd = -1;
328 * when virtio queues are ready to work, qemu will send us to
329 * enable the virtio queue pair.
332 user_set_vring_enable(struct vhost_device_ctx ctx,
333 struct vhost_vring_state *state)
335 struct virtio_net *dev = get_device(ctx);
336 int enable = (int)state->num;
338 RTE_LOG(INFO, VHOST_CONFIG,
339 "set queue enable: %d to qp idx: %d\n",
340 enable, state->index);
342 if (notify_ops->vring_state_changed) {
343 notify_ops->vring_state_changed(dev, state->index, enable);
346 dev->virtqueue[state->index]->enabled = enable;
352 user_set_protocol_features(struct vhost_device_ctx ctx,
353 uint64_t protocol_features)
355 struct virtio_net *dev;
357 dev = get_device(ctx);
358 if (dev == NULL || protocol_features & ~VHOST_USER_PROTOCOL_FEATURES)
361 dev->protocol_features = protocol_features;
365 user_set_log_base(struct vhost_device_ctx ctx,
366 struct VhostUserMsg *msg)
368 struct virtio_net *dev;
369 int fd = msg->fds[0];
373 dev = get_device(ctx);
378 RTE_LOG(ERR, VHOST_CONFIG, "invalid log fd: %d\n", fd);
382 if (msg->size != sizeof(VhostUserLog)) {
383 RTE_LOG(ERR, VHOST_CONFIG,
384 "invalid log base msg size: %"PRId32" != %d\n",
385 msg->size, (int)sizeof(VhostUserLog));
389 size = msg->payload.log.mmap_size;
390 off = msg->payload.log.mmap_offset;
391 RTE_LOG(INFO, VHOST_CONFIG,
392 "log mmap size: %"PRId64", offset: %"PRId64"\n",
396 * mmap from 0 to workaround a hugepage mmap bug: mmap will
397 * fail when offset is not page size aligned.
399 addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
400 if (addr == MAP_FAILED) {
401 RTE_LOG(ERR, VHOST_CONFIG, "mmap log base failed!\n");
405 /* TODO: unmap on stop */
406 dev->log_base = (uint64_t)(uintptr_t)addr + off;
407 dev->log_size = size;
413 * An rarp packet is constructed and broadcasted to notify switches about
414 * the new location of the migrated VM, so that packets from outside will
415 * not be lost after migration.
417 * However, we don't actually "send" a rarp packet here, instead, we set
418 * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
421 user_send_rarp(struct vhost_device_ctx ctx, struct VhostUserMsg *msg)
423 struct virtio_net *dev;
424 uint8_t *mac = (uint8_t *)&msg->payload.u64;
426 dev = get_device(ctx);
430 RTE_LOG(DEBUG, VHOST_CONFIG,
431 ":: mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
432 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
433 memcpy(dev->mac.addr_bytes, mac, 6);
436 * Set the flag to inject a RARP broadcast packet at
437 * rte_vhost_dequeue_burst().
439 * rte_smp_wmb() is for making sure the mac is copied
440 * before the flag is set.
443 rte_atomic16_set(&dev->broadcast_rarp, 1);