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-user.h"
47 #include "vhost-net-user.h"
48 #include "vhost-net.h"
50 struct orig_region_map {
52 uint64_t mapped_address;
57 #define orig_region(ptr, nregions) \
58 ((struct orig_region_map *)RTE_PTR_ADD((ptr), \
59 sizeof(struct virtio_memory) + \
60 sizeof(struct virtio_memory_regions) * (nregions)))
68 return (uint64_t)stat.st_blksize;
72 free_mem_region(struct virtio_net *dev)
74 struct orig_region_map *region;
77 if (!dev || !dev->mem)
80 region = orig_region(dev->mem, dev->mem->nregions);
81 for (idx = 0; idx < dev->mem->nregions; idx++) {
82 if (region[idx].mapped_address) {
83 munmap((void *)(uintptr_t)region[idx].mapped_address,
84 region[idx].mapped_size);
85 close(region[idx].fd);
91 vhost_backend_cleanup(struct virtio_net *dev)
101 user_set_mem_table(int vid, struct VhostUserMsg *pmsg)
103 struct VhostUserMemory memory = pmsg->payload.memory;
104 struct virtio_memory_regions *pregion;
105 uint64_t mapped_address, mapped_size;
106 struct virtio_net *dev;
107 unsigned int idx = 0;
108 struct orig_region_map *pregion_orig;
111 /* unmap old memory regions one by one*/
112 dev = get_device(vid);
116 /* Remove from the data plane. */
117 if (dev->flags & VIRTIO_DEV_RUNNING) {
118 dev->flags &= ~VIRTIO_DEV_RUNNING;
119 notify_ops->destroy_device(vid);
123 free_mem_region(dev);
129 sizeof(struct virtio_memory) +
130 sizeof(struct virtio_memory_regions) * memory.nregions +
131 sizeof(struct orig_region_map) * memory.nregions);
132 if (dev->mem == NULL) {
133 RTE_LOG(ERR, VHOST_CONFIG,
134 "(%d) failed to allocate memory for dev->mem\n",
138 dev->mem->nregions = memory.nregions;
140 pregion_orig = orig_region(dev->mem, memory.nregions);
141 for (idx = 0; idx < memory.nregions; idx++) {
142 pregion = &dev->mem->regions[idx];
143 pregion->guest_phys_address =
144 memory.regions[idx].guest_phys_addr;
145 pregion->guest_phys_address_end =
146 memory.regions[idx].guest_phys_addr +
147 memory.regions[idx].memory_size;
148 pregion->memory_size =
149 memory.regions[idx].memory_size;
150 pregion->userspace_address =
151 memory.regions[idx].userspace_addr;
154 mapped_size = memory.regions[idx].memory_size +
155 memory.regions[idx].mmap_offset;
157 /* mmap() without flag of MAP_ANONYMOUS, should be called
158 * with length argument aligned with hugepagesz at older
159 * longterm version Linux, like 2.6.32 and 3.2.72, or
160 * mmap() will fail with EINVAL.
162 * to avoid failure, make sure in caller to keep length
165 alignment = get_blk_size(pmsg->fds[idx]);
166 mapped_size = RTE_ALIGN_CEIL(mapped_size, alignment);
168 mapped_address = (uint64_t)(uintptr_t)mmap(NULL,
170 PROT_READ | PROT_WRITE, MAP_SHARED,
174 RTE_LOG(INFO, VHOST_CONFIG,
175 "mapped region %d fd:%d to:%p sz:0x%"PRIx64" "
176 "off:0x%"PRIx64" align:0x%"PRIx64"\n",
177 idx, pmsg->fds[idx], (void *)(uintptr_t)mapped_address,
178 mapped_size, memory.regions[idx].mmap_offset,
181 if (mapped_address == (uint64_t)(uintptr_t)MAP_FAILED) {
182 RTE_LOG(ERR, VHOST_CONFIG,
183 "mmap qemu guest failed.\n");
187 pregion_orig[idx].mapped_address = mapped_address;
188 pregion_orig[idx].mapped_size = mapped_size;
189 pregion_orig[idx].blksz = alignment;
190 pregion_orig[idx].fd = pmsg->fds[idx];
192 mapped_address += memory.regions[idx].mmap_offset;
194 pregion->address_offset = mapped_address -
195 pregion->guest_phys_address;
197 if (memory.regions[idx].guest_phys_addr == 0) {
198 dev->mem->base_address =
199 memory.regions[idx].userspace_addr;
200 dev->mem->mapped_address =
201 pregion->address_offset;
204 LOG_DEBUG(VHOST_CONFIG,
205 "REGION: %u GPA: %p QEMU VA: %p SIZE (%"PRIu64")\n",
207 (void *)(uintptr_t)pregion->guest_phys_address,
208 (void *)(uintptr_t)pregion->userspace_address,
209 pregion->memory_size);
216 munmap((void *)(uintptr_t)pregion_orig[idx].mapped_address,
217 pregion_orig[idx].mapped_size);
218 close(pregion_orig[idx].fd);
226 vq_is_ready(struct vhost_virtqueue *vq)
228 return vq && vq->desc &&
229 vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
230 vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD;
234 virtio_is_ready(struct virtio_net *dev)
236 struct vhost_virtqueue *rvq, *tvq;
239 for (i = 0; i < dev->virt_qp_nb; i++) {
240 rvq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ];
241 tvq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ];
243 if (!vq_is_ready(rvq) || !vq_is_ready(tvq)) {
244 RTE_LOG(INFO, VHOST_CONFIG,
245 "virtio is not ready for processing.\n");
250 RTE_LOG(INFO, VHOST_CONFIG,
251 "virtio is now ready for processing.\n");
256 user_set_vring_call(int vid, struct VhostUserMsg *pmsg)
258 struct vhost_vring_file file;
260 file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
261 if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
262 file.fd = VIRTIO_INVALID_EVENTFD;
264 file.fd = pmsg->fds[0];
265 RTE_LOG(INFO, VHOST_CONFIG,
266 "vring call idx:%d file:%d\n", file.index, file.fd);
267 vhost_set_vring_call(vid, &file);
272 * In vhost-user, when we receive kick message, will test whether virtio
273 * device is ready for packet processing.
276 user_set_vring_kick(int vid, struct VhostUserMsg *pmsg)
278 struct vhost_vring_file file;
279 struct virtio_net *dev = get_device(vid);
284 file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
285 if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
286 file.fd = VIRTIO_INVALID_EVENTFD;
288 file.fd = pmsg->fds[0];
289 RTE_LOG(INFO, VHOST_CONFIG,
290 "vring kick idx:%d file:%d\n", file.index, file.fd);
291 vhost_set_vring_kick(vid, &file);
293 if (virtio_is_ready(dev) && !(dev->flags & VIRTIO_DEV_RUNNING)) {
294 if (notify_ops->new_device(vid) == 0)
295 dev->flags |= VIRTIO_DEV_RUNNING;
300 * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
303 user_get_vring_base(int vid, struct vhost_vring_state *state)
305 struct virtio_net *dev = get_device(vid);
309 /* We have to stop the queue (virtio) if it is running. */
310 if (dev->flags & VIRTIO_DEV_RUNNING)
311 notify_ops->destroy_device(vid);
313 /* Here we are safe to get the last used index */
314 vhost_get_vring_base(vid, state->index, state);
316 RTE_LOG(INFO, VHOST_CONFIG,
317 "vring base idx:%d file:%d\n", state->index, state->num);
319 * Based on current qemu vhost-user implementation, this message is
320 * sent and only sent in vhost_vring_stop.
321 * TODO: cleanup the vring, it isn't usable since here.
323 if (dev->virtqueue[state->index]->kickfd >= 0)
324 close(dev->virtqueue[state->index]->kickfd);
326 dev->virtqueue[state->index]->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
332 * when virtio queues are ready to work, qemu will send us to
333 * enable the virtio queue pair.
336 user_set_vring_enable(int vid, struct vhost_vring_state *state)
338 struct virtio_net *dev = get_device(vid);
339 int enable = (int)state->num;
341 RTE_LOG(INFO, VHOST_CONFIG,
342 "set queue enable: %d to qp idx: %d\n",
343 enable, state->index);
345 if (notify_ops->vring_state_changed)
346 notify_ops->vring_state_changed(vid, state->index, enable);
348 dev->virtqueue[state->index]->enabled = enable;
354 user_set_protocol_features(int vid, uint64_t protocol_features)
356 struct virtio_net *dev;
358 dev = get_device(vid);
359 if (dev == NULL || protocol_features & ~VHOST_USER_PROTOCOL_FEATURES)
362 dev->protocol_features = protocol_features;
366 user_set_log_base(int vid, struct VhostUserMsg *msg)
368 struct virtio_net *dev;
369 int fd = msg->fds[0];
373 dev = get_device(vid);
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(int vid, struct VhostUserMsg *msg)
423 struct virtio_net *dev;
424 uint8_t *mac = (uint8_t *)&msg->payload.u64;
426 dev = get_device(vid);
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);