2 * vmnet-common.m - network client wrapper for Apple vmnet.framework
4 * Copyright(c) 2022 Vladislav Yaroshchuk <vladislav.yaroshchuk@jetbrains.com>
5 * Copyright(c) 2021 Phillip Tennen <phillip@axleos.com>
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
13 #include "qemu/main-loop.h"
15 #include "qapi/qapi-types-net.h"
16 #include "vmnet_int.h"
18 #include "qemu/error-report.h"
19 #include "qapi/error.h"
20 #include "sysemu/runstate.h"
22 #include <vmnet/vmnet.h>
23 #include <dispatch/dispatch.h>
26 static void vmnet_send_completed(NetClientState *nc, ssize_t len);
29 const char *vmnet_status_map_str(vmnet_return_t status)
35 return "general failure (possibly not enough privileges)";
36 case VMNET_MEM_FAILURE:
37 return "memory allocation failure";
38 case VMNET_INVALID_ARGUMENT:
39 return "invalid argument specified";
40 case VMNET_SETUP_INCOMPLETE:
41 return "interface setup is not complete";
42 case VMNET_INVALID_ACCESS:
43 return "invalid access, permission denied";
44 case VMNET_PACKET_TOO_BIG:
45 return "packet size is larger than MTU";
46 case VMNET_BUFFER_EXHAUSTED:
47 return "buffers exhausted in kernel";
48 case VMNET_TOO_MANY_PACKETS:
49 return "packet count exceeds limit";
50 case VMNET_SHARING_SERVICE_BUSY:
51 return "conflict, sharing service is in use";
53 return "unknown vmnet error";
59 * Write packets from QEMU to vmnet interface.
61 * vmnet.framework supports iov, but writing more than
62 * one iov into vmnet interface fails with
63 * 'VMNET_INVALID_ARGUMENT'. Collecting provided iovs into
64 * one and passing it to vmnet works fine. That's the
65 * reason why receive_iov() left unimplemented. But it still
66 * works with good performance having .receive() only.
68 ssize_t vmnet_receive_common(NetClientState *nc,
72 VmnetState *s = DO_UPCAST(VmnetState, nc, nc);
73 struct vmpktdesc packet;
76 vmnet_return_t if_status;
78 if (size > s->max_packet_size) {
79 warn_report("vmnet: packet is too big, %zu > %" PRIu64,
85 iov.iov_base = (char *) buf;
88 packet.vm_pkt_iovcnt = 1;
90 packet.vm_pkt_size = size;
91 packet.vm_pkt_iov = &iov;
94 if_status = vmnet_write(s->vmnet_if, &packet, &pkt_cnt);
95 if (if_status != VMNET_SUCCESS) {
96 error_report("vmnet: write error: %s\n",
97 vmnet_status_map_str(if_status));
109 * Read packets from vmnet interface and write them
110 * to temporary buffers in VmnetState.
112 * Returns read packets number (may be 0) on success,
115 static int vmnet_read_packets(VmnetState *s)
117 assert(s->packets_send_current_pos == s->packets_send_end_pos);
119 struct vmpktdesc *packets = s->packets_buf;
120 vmnet_return_t status;
123 /* Read as many packets as present */
124 s->packets_send_current_pos = 0;
125 s->packets_send_end_pos = VMNET_PACKETS_LIMIT;
126 for (i = 0; i < s->packets_send_end_pos; ++i) {
127 packets[i].vm_pkt_size = s->max_packet_size;
128 packets[i].vm_pkt_iovcnt = 1;
129 packets[i].vm_flags = 0;
132 status = vmnet_read(s->vmnet_if, packets, &s->packets_send_end_pos);
133 if (status != VMNET_SUCCESS) {
134 error_printf("vmnet: read failed: %s\n",
135 vmnet_status_map_str(status));
136 s->packets_send_current_pos = 0;
137 s->packets_send_end_pos = 0;
140 return s->packets_send_end_pos;
145 * Write packets from temporary buffers in VmnetState
148 static void vmnet_write_packets_to_qemu(VmnetState *s)
150 while (s->packets_send_current_pos < s->packets_send_end_pos) {
151 ssize_t size = qemu_send_packet_async(&s->nc,
152 s->iov_buf[s->packets_send_current_pos].iov_base,
153 s->packets_buf[s->packets_send_current_pos].vm_pkt_size,
154 vmnet_send_completed);
157 /* QEMU is not ready to consume more packets -
158 * stop and wait for completion callback call */
161 ++s->packets_send_current_pos;
167 * Bottom half callback that transfers packets from vmnet interface
170 * The process of transferring packets is three-staged:
171 * 1. Handle vmnet event;
172 * 2. Read packets from vmnet interface into temporary buffer;
173 * 3. Write packets from temporary buffer to QEMU.
175 * QEMU may suspend this process on the last stage, returning 0 from
176 * qemu_send_packet_async function. If this happens, we should
177 * respectfully wait until it is ready to consume more packets,
178 * write left ones in temporary buffer and only after this
179 * continue reading more packets from vmnet interface.
181 * Packets to be transferred are stored into packets_buf,
182 * in the window [packets_send_current_pos..packets_send_end_pos)
183 * including current_pos, excluding end_pos.
185 * Thus, if QEMU is not ready, buffer is not read and
186 * packets_send_current_pos < packets_send_end_pos.
188 static void vmnet_send_bh(void *opaque)
190 NetClientState *nc = (NetClientState *) opaque;
191 VmnetState *s = DO_UPCAST(VmnetState, nc, nc);
194 * Do nothing if QEMU is not ready - wait
195 * for completion callback invocation
197 if (s->packets_send_current_pos < s->packets_send_end_pos) {
201 /* Read packets from vmnet interface */
202 if (vmnet_read_packets(s) > 0) {
203 /* Send them to QEMU */
204 vmnet_write_packets_to_qemu(s);
210 * Completion callback to be invoked by QEMU when it becomes
211 * ready to consume more packets.
213 static void vmnet_send_completed(NetClientState *nc, ssize_t len)
215 VmnetState *s = DO_UPCAST(VmnetState, nc, nc);
217 /* Callback is invoked eq queued packet is sent */
218 ++s->packets_send_current_pos;
220 /* Complete sending packets left in VmnetState buffers */
221 vmnet_write_packets_to_qemu(s);
223 /* And read new ones from vmnet if VmnetState buffer is ready */
224 if (s->packets_send_current_pos < s->packets_send_end_pos) {
225 qemu_bh_schedule(s->send_bh);
230 static void vmnet_bufs_init(VmnetState *s)
232 struct vmpktdesc *packets = s->packets_buf;
233 struct iovec *iov = s->iov_buf;
236 for (i = 0; i < VMNET_PACKETS_LIMIT; ++i) {
237 iov[i].iov_len = s->max_packet_size;
238 iov[i].iov_base = g_malloc0(iov[i].iov_len);
239 packets[i].vm_pkt_iov = iov + i;
244 * Called on state change to un-register/re-register handlers
246 static void vmnet_vm_state_change_cb(void *opaque, bool running, RunState state)
248 VmnetState *s = opaque;
251 vmnet_interface_set_event_callback(
253 VMNET_INTERFACE_PACKETS_AVAILABLE,
255 ^(interface_event_t event_id, xpc_object_t event) {
256 assert(event_id == VMNET_INTERFACE_PACKETS_AVAILABLE);
258 * This function is being called from a non qemu thread, so
259 * we only schedule a BH, and do the rest of the io completion
260 * handling from vmnet_send_bh() which runs in a qemu context.
262 qemu_bh_schedule(s->send_bh);
265 vmnet_interface_set_event_callback(
267 VMNET_INTERFACE_PACKETS_AVAILABLE,
273 int vmnet_if_create(NetClientState *nc,
274 xpc_object_t if_desc,
277 VmnetState *s = DO_UPCAST(VmnetState, nc, nc);
278 dispatch_semaphore_t if_created_sem = dispatch_semaphore_create(0);
279 __block vmnet_return_t if_status;
281 s->if_queue = dispatch_queue_create(
282 "org.qemu.vmnet.if_queue",
283 DISPATCH_QUEUE_SERIAL
286 xpc_dictionary_set_bool(
288 vmnet_allocate_mac_address_key,
293 qemu_log("vmnet.start.interface_desc:\n");
294 xpc_dictionary_apply(if_desc,
295 ^bool(const char *k, xpc_object_t v) {
296 char *desc = xpc_copy_description(v);
297 qemu_log(" %s=%s\n", k, desc);
303 s->vmnet_if = vmnet_start_interface(
306 ^(vmnet_return_t status, xpc_object_t interface_param) {
308 if (status != VMNET_SUCCESS || !interface_param) {
309 dispatch_semaphore_signal(if_created_sem);
314 qemu_log("vmnet.start.interface_param:\n");
315 xpc_dictionary_apply(interface_param,
316 ^bool(const char *k, xpc_object_t v) {
317 char *desc = xpc_copy_description(v);
318 qemu_log(" %s=%s\n", k, desc);
324 s->mtu = xpc_dictionary_get_uint64(
327 s->max_packet_size = xpc_dictionary_get_uint64(
329 vmnet_max_packet_size_key);
331 dispatch_semaphore_signal(if_created_sem);
334 if (s->vmnet_if == NULL) {
335 dispatch_release(s->if_queue);
336 dispatch_release(if_created_sem);
338 "unable to create interface with requested params");
342 dispatch_semaphore_wait(if_created_sem, DISPATCH_TIME_FOREVER);
343 dispatch_release(if_created_sem);
345 if (if_status != VMNET_SUCCESS) {
346 dispatch_release(s->if_queue);
348 "cannot create vmnet interface: %s",
349 vmnet_status_map_str(if_status));
353 s->send_bh = aio_bh_new(qemu_get_aio_context(), vmnet_send_bh, nc);
356 s->packets_send_current_pos = 0;
357 s->packets_send_end_pos = 0;
359 vmnet_vm_state_change_cb(s, 1, RUN_STATE_RUNNING);
361 s->change = qemu_add_vm_change_state_handler(vmnet_vm_state_change_cb, s);
367 void vmnet_cleanup_common(NetClientState *nc)
369 VmnetState *s = DO_UPCAST(VmnetState, nc, nc);
370 dispatch_semaphore_t if_stopped_sem;
372 if (s->vmnet_if == NULL) {
376 vmnet_vm_state_change_cb(s, 0, RUN_STATE_SHUTDOWN);
377 qemu_del_vm_change_state_handler(s->change);
378 if_stopped_sem = dispatch_semaphore_create(0);
379 vmnet_stop_interface(
382 ^(vmnet_return_t status) {
383 assert(status == VMNET_SUCCESS);
384 dispatch_semaphore_signal(if_stopped_sem);
386 dispatch_semaphore_wait(if_stopped_sem, DISPATCH_TIME_FOREVER);
388 qemu_purge_queued_packets(nc);
390 qemu_bh_delete(s->send_bh);
391 dispatch_release(if_stopped_sem);
392 dispatch_release(s->if_queue);
394 for (int i = 0; i < VMNET_PACKETS_LIMIT; ++i) {
395 g_free(s->iov_buf[i].iov_base);