-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.c
836 lines (696 loc) · 23.8 KB
/
helper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
// SPDX-FileCopyrightText: The vmnet-helper authors
// SPDX-License-Identifier: Apache-2.0
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/event.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/un.h>
#include <time.h>
#include <unistd.h>
#include <uuid/uuid.h>
#include <vmnet/vmnet.h>
#include "log.h"
#include "options.h"
#include "socket_x.h"
#include "version.h"
// vmnet_read() can return up to 256 packets. There is no constant in vmnet for
// this value. https://developer.apple.com/documentation/vmnet?language=objc
// sendmsg_x() and recvmsg_x() do not document any value but testing show that
// we can read or write 64 packets in one call. Tesiting with iperf3 shows
// that there is no reason to use more than 64.
#define MAX_PACKET_COUNT 64
#define MICROSECOND 1000
// Testing show that one retry in enough in 72% of cases. The following stats
// are from 300 seconds iperf3 run at 7.85 Gbits/sec rate (679 kps).
//
// retries count distribution
// ------------------------------------------------------------------------------------
// 1 68 ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// 2 17 |||||||||||||||||
// 3 3 |||
// 6 2 ||
// 4 2 ||
// 8 1 |
// 13 1 |
#define VM_RETRY_DELAY (50 * MICROSECOND)
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
// Apple recommend receive buffer size to be 4 times the size of the send
// buffer size, but send buffer size is not used to allocate a buffer in
// datagram sockets, it only limits the maximum packet size. The receive buffer
// size determine how many packets can be queued. Using bigger receive buffer
// size reduces number of retries and improves throughput.
static const int SNDBUF_SIZE = 64 * 1024;
static const int RCVBUF_SIZE = 2 * 1024 * 1024;
static const uintptr_t SHUTDOWN_EVENT = 1;
enum {
STATUS_FAILURE = 1,
STATUS_STOPPED = 2,
};
struct version {
int major;
int minor;
int point;
};
struct endpoint {
dispatch_queue_t queue;
struct vmpktdesc packets[MAX_PACKET_COUNT];
struct msghdr_x msgs[MAX_PACKET_COUNT];
struct iovec iovs[MAX_PACKET_COUNT];
unsigned char *buffers;
};
static void init_endpoint(struct endpoint *e, size_t max_packet_size)
{
e->buffers = calloc(MAX_PACKET_COUNT, max_packet_size);
if (e->buffers == NULL) {
ERRORF("[main] calloc: %s", strerror(errno));
exit(EXIT_FAILURE);
}
// Bind iovs and buffers to packets and msgs - this can be done once.
for (int i = 0; i < MAX_PACKET_COUNT; i++) {
struct iovec *iov = &e->iovs[i];
iov->iov_base = &e->buffers[i * max_packet_size];
// For reading and writing vmnet interface.
struct vmpktdesc *packet = &e->packets[i];
packet->vm_pkt_iovcnt = 1;
packet->vm_pkt_iov = iov;
// For reading and vm socket.
struct msghdr_x *msg = &e->msgs[i];
msg->msg_hdr.msg_iovlen = 1;
msg->msg_hdr.msg_iov = iov;
}
}
static struct options options = {
.fd = -1,
.operation_mode = VMNET_SHARED_MODE,
.start_address = "192.168.105.1",
.end_address = "192.168.105.254",
.subnet_mask = "255.255.255.0",
};
static struct endpoint host;
static struct endpoint vm;
static interface_ref interface;
static size_t max_packet_size;
static int kq = -1;
static int status;
static bool has_bulk_forwarding;
// Ensures exclusive access to options.socket. This protects from the user
// error of creating multiple helpers using the same socket.
static char *socket_lockfile;
static const char *host_strerror(vmnet_return_t v)
{
switch (v) {
case VMNET_SUCCESS:
return "VMNET_SUCCESS";
case VMNET_FAILURE:
return "VMNET_FAILURE";
case VMNET_MEM_FAILURE:
return "VMNET_MEM_FAILURE";
case VMNET_INVALID_ARGUMENT:
return "VMNET_INVALID_ARGUMENT";
case VMNET_SETUP_INCOMPLETE:
return "VMNET_SETUP_INCOMPLETE";
case VMNET_INVALID_ACCESS:
return "VMNET_INVALID_ACCESS";
case VMNET_PACKET_TOO_BIG:
return "VMNET_PACKET_TOO_BIG";
case VMNET_BUFFER_EXHAUSTED:
return "VMNET_BUFFER_EXHAUSTED";
case VMNET_TOO_MANY_PACKETS:
return "VMNET_TOO_MANY_PACKETS";
default:
return "(unknown status)";
}
}
static void setup_kq(void)
{
kq = kqueue();
if (kq == -1) {
ERRORF("[main] kqueue: %s", strerror(errno));
exit(EXIT_FAILURE);
}
struct kevent changes[] = {
{.ident=SIGTERM, .filter=EVFILT_SIGNAL, .flags=EV_ADD},
{.ident=SIGINT, .filter=EVFILT_SIGNAL, .flags=EV_ADD},
{.ident=SHUTDOWN_EVENT, .filter=EVFILT_USER, .flags=EV_ADD},
};
sigset_t mask;
sigemptyset(&mask);
for (size_t i = 0; i < ARRAY_SIZE(changes); i++) {
if (changes[i].filter == EVFILT_SIGNAL) {
sigaddset(&mask, changes[i].ident);
}
}
if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0) {
ERRORF("[main] sigprocmask: %s", strerror(errno));
exit(EXIT_FAILURE);
}
// We will receive EPIPE on the socket.
signal(SIGPIPE, SIG_IGN);
if (kevent(kq, changes, ARRAY_SIZE(changes), NULL, 0, NULL) != 0) {
ERRORF("[main] kevent: %s", strerror(errno));
exit(EXIT_FAILURE);
}
}
static void trigger_shutdown(int flags)
{
struct kevent event = {
.ident=SHUTDOWN_EVENT,
.filter=EVFILT_USER,
.fflags=NOTE_TRIGGER | NOTE_FFOR | (flags & NOTE_FFLAGSMASK),
};
if (kevent(kq, &event, 1, NULL, 0, NULL) != 0) {
ERRORF("kevent: %s", strerror(errno));
exit(EXIT_FAILURE);
}
}
static void write_vmnet_info(xpc_object_t param)
{
__block int count = 0;
#define print_item(fmt, key, value) \
do { \
if (count++ > 0) \
printf(","); \
printf(fmt, key, value); \
} while (0)
printf("{");
xpc_dictionary_apply(param, ^bool(const char *key, xpc_object_t value) {
xpc_type_t t = xpc_get_type(value);
if (t == XPC_TYPE_UINT64) {
print_item("\"%s\":%llu", key, xpc_uint64_get_value(value));
} else if (t == XPC_TYPE_INT64) {
print_item("\"%s\":%lld", key, xpc_int64_get_value(value));
} else if (t == XPC_TYPE_STRING) {
print_item("\"%s\":\"%s\"", key, xpc_string_get_string_ptr(value));
} else if (t == XPC_TYPE_UUID) {
char uuid_str[36 + 1];
uuid_unparse(xpc_uuid_get_bytes(value), uuid_str);
print_item("\"%s\":\"%s\"", key, uuid_str);
}
return true;
});
printf("}\n");
fflush(stdout);
}
static void start_host_interface(void)
{
DEBUG("[main] starting vmnet interface");
host.queue = dispatch_queue_create("com.github.nirs.vmnet-helper.host", DISPATCH_QUEUE_SERIAL);
xpc_object_t desc = xpc_dictionary_create(NULL, NULL, 0);
xpc_dictionary_set_uuid(desc, vmnet_interface_id_key, options.interface_id);
xpc_dictionary_set_uint64(desc, vmnet_operation_mode_key, options.operation_mode);
if (options.operation_mode == VMNET_BRIDGED_MODE) {
xpc_dictionary_set_string(desc, vmnet_shared_interface_name_key, options.shared_interface);
} else {
xpc_dictionary_set_string(desc, vmnet_start_address_key, options.start_address);
xpc_dictionary_set_string(desc, vmnet_end_address_key, options.end_address);
xpc_dictionary_set_string(desc, vmnet_subnet_mask_key, options.subnet_mask);
}
dispatch_semaphore_t completed = dispatch_semaphore_create(0);
interface = vmnet_start_interface(
desc, host.queue, ^(vmnet_return_t status, xpc_object_t param) {
if (status != VMNET_SUCCESS) {
ERRORF("[main] vmnet_start_interface: %s", host_strerror(status));
exit(EXIT_FAILURE);
}
write_vmnet_info(param);
max_packet_size = xpc_dictionary_get_uint64(param, vmnet_max_packet_size_key);
dispatch_semaphore_signal(completed);
});
dispatch_semaphore_wait(completed, DISPATCH_TIME_FOREVER);
xpc_release(desc);
INFO("[main] started vmnet interface");
}
static void drop_privileges(void)
{
if (options.gid != 0) {
if (setgid(options.gid) < 0) {
ERRORF("[main] unable to change gid to %d: %s", options.gid, strerror(errno));
exit(EXIT_FAILURE);
}
}
if (options.uid != 0) {
if (setuid(options.uid) < 0) {
ERRORF("[main] unable to change uid to %d: %s", options.uid, strerror(errno));
exit(EXIT_FAILURE);
}
}
INFOF("[main] running as uid: %d gid: %d", geteuid(), getegid());
}
static void remove_socket_lockfile(void)
{
if (socket_lockfile == NULL) {
return;
}
DEBUGF("[main] remove lockfile \"%s\"", socket_lockfile);
if (remove(socket_lockfile) < 0 && errno != ENOENT) {
WARNF("[main] remove(\"%s\": %s", socket_lockfile, strerror(errno));
}
free(socket_lockfile);
socket_lockfile = NULL;
}
static void create_socket_lockfile(void)
{
if (asprintf(&socket_lockfile, "%s.lock", options.socket) < 0) {
ERRORF("[main] asprintf: %s", strerror(errno));
exit(EXIT_FAILURE);
}
// We msut hold this lockfile for the rest of the process lifetime, so we
// explicicly "leak" the file descriptor to make it harder to close it by
// mistake.
if (open(socket_lockfile, O_RDONLY | O_CREAT | O_EXLOCK | O_NONBLOCK, 0600) < 0) {
ERRORF("[main] open(\"%s\"): %s", socket_lockfile, strerror(errno));
exit(EXIT_FAILURE);
}
if (atexit(remove_socket_lockfile) < 0) {
ERRORF("[main] atexit: %s", strerror(errno));
exit(EXIT_FAILURE);
}
DEBUGF("[main] created lockfile \"%s\"", socket_lockfile);
}
static void remove_socket_silently(void)
{
if (options.socket == NULL) {
return;
}
DEBUGF("[main] remove socket \"%s\"", options.socket);
if (remove(options.socket) < 0 && errno != ENOENT) {
WARNF("[main] remove(\"%s\"): %s", options.socket, strerror(errno));
}
options.socket = NULL;
}
static void create_socket(void)
{
options.fd = socket(PF_LOCAL, SOCK_DGRAM, 0);
if (options.fd < 0) {
ERRORF("[main] socket: %s", strerror(errno));
exit(EXIT_FAILURE);
}
// Setting socket buffer size is a performance optimization - don't fail on
// errors.
if (setsockopt(options.fd, SOL_SOCKET, SO_SNDBUF, &SNDBUF_SIZE, sizeof(SNDBUF_SIZE)) < 0) {
WARNF("[main] setsockopt(SO_SNDBUF, %d): %s", SNDBUF_SIZE, strerror(errno));
}
if (setsockopt(options.fd, SOL_SOCKET, SO_RCVBUF, &RCVBUF_SIZE, sizeof(RCVBUF_SIZE)) < 0) {
WARNF("[main] setsockopt(SO_RCVBUF, %d): %s", RCVBUF_SIZE, strerror(errno));
}
if (remove(options.socket) < 0 && errno != ENOENT) {
ERRORF("[main] remove(\"%s\"): %s", options.socket, strerror(errno));
exit(EXIT_FAILURE);
}
struct sockaddr_un address = {.sun_family=PF_LOCAL};
strncpy(address.sun_path, options.socket, sizeof(address.sun_path) - 1);
if (bind(options.fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
ERRORF("[main] bind(\"%s\"): %s", options.socket, strerror(errno));
exit(EXIT_FAILURE);
}
// Racy, but fchmod() before bind() does not work on darwin.
if (chmod(options.socket, 0600) < 0) {
ERRORF("[main] fchmod: %s", strerror(errno));
exit(EXIT_FAILURE);
}
if (atexit(remove_socket_silently) < 0) {
ERRORF("[main] atexit: %s", strerror(errno));
exit(EXIT_FAILURE);
}
DEBUGF("[main] created socket \"%s\"", options.socket);
}
static void wait_for_client(void)
{
INFOF("[main] waiting for client on \"%s\"", options.socket);
struct kevent add = {.ident=options.fd, .filter=EVFILT_READ, .flags=EV_ADD};
if (kevent(kq, &add, 1, NULL, 0, NULL) != 0) {
ERRORF("[main] kevent: %s", strerror(errno));
exit(EXIT_FAILURE);
}
struct kevent events[1];
while (1) {
int n = kevent(kq, NULL, 0, events, 1, NULL);
if (n < 0) {
ERRORF("[main] kevent: %s", strerror(errno));
exit(EXIT_FAILURE);
}
if (n > 0) {
if (events[0].filter == EVFILT_SIGNAL) {
INFOF("[main] received signal %s", strsignal(events[0].ident));
exit(EXIT_SUCCESS);
}
if (events[0].filter == EVFILT_READ) {
break;
}
}
}
struct kevent delete = {.ident=options.fd, .filter=EVFILT_READ, .flags=EV_DELETE};
if (kevent(kq, &delete, 1, NULL, 0, NULL) != 0) {
ERRORF("[main] kevent: %s", strerror(errno));
exit(EXIT_FAILURE);
}
}
// Connecting the the client address allows sending packets to client using
// sendmsg_x() or write(), and ensures that we never read packets from
// other clients while we serve this one.
static void connect_socket(void)
{
unsigned char buf[64];
struct sockaddr_un client = {0};
socklen_t len = sizeof(client);
ssize_t n = recvfrom(options.fd, buf, sizeof(buf), MSG_PEEK,
(struct sockaddr *)&client, &len);
if (n < 0) {
ERRORF("[main] recvfrom: %s", strerror(errno));
exit(EXIT_FAILURE);
}
DEBUGF("[main] connecting to \"%s\"", client.sun_path);
if (connect(options.fd, (const struct sockaddr *)&client, sizeof(client)) < 0) {
ERRORF("[main] connect(\"%s\"): %s", client.sun_path, strerror(errno));
exit(EXIT_FAILURE);
}
// vfkit and krunkit sendd an invalid "handshake" frame ("VFKT"), which
// fail later in vmnet_write() with INVALID_ARGUMENT.
if (n < 64) {
DEBUGF("[main] dropping invalid packet (%zd bytes)", n);
n = read(options.fd, buf, sizeof(buf));
if (n < 0) {
ERRORF("[main] read: %s", strerror(errno));
exit(EXIT_FAILURE);
}
}
INFOF("[main] serving client \"%s\"", client.sun_path);
}
static void setup_socket(void)
{
if (options.fd != -1) {
DEBUGF("[main] using fd %d", options.fd);
return;
}
create_socket_lockfile();
create_socket();
wait_for_client();
connect_socket();
// Once a client connected, we cannot serve any other client, so it would
// be nice to remove the socket now. This breaks krunkit since libkrun does
// not connect to the socket and use sendto().
// https://github.com/containers/libkrun/blob/57a5a6bfbe5d2333d88fd88fcedb9c1d1fec9cc2/src/devices/src/virtio/net/gvproxy.rs#L113
}
static void setup_host_buffers(void)
{
DEBUGF("[main] allocating %d packets of %zu bytes for host",
MAX_PACKET_COUNT, max_packet_size);
init_endpoint(&host, max_packet_size);
}
static void setup_vm_buffers(void)
{
DEBUGF("[main] allocating %d packets of %zu bytes for vm",
MAX_PACKET_COUNT, max_packet_size);
init_endpoint(&vm, max_packet_size);
}
static int read_from_host(void)
{
int count = MAX_PACKET_COUNT;
// Reset packets and iovs - must be done before reading from vment.
for (int i = 0; i < count; i++) {
host.packets[i].vm_pkt_size = max_packet_size;
host.packets[i].vm_flags = 0;
}
for (int i = 0; i < count; i++) {
host.iovs[i].iov_len = max_packet_size;
}
vmnet_return_t status = vmnet_read(interface, &host.packets[0], &count);
if (status != VMNET_SUCCESS) {
ERRORF("[host->vm] vmnet_read: %s", host_strerror(status));
return -1;
}
return count;
}
// When sendmsg_x()/write() fail with ENOBUFS we need to wait until the kernel
// has buffer spacce, but we don't have a way to wait for event. Polling with
// very short sleep typically work after 1 retry.
static inline void wait_for_buffer_space(void)
{
struct timespec t = {.tv_nsec=VM_RETRY_DELAY};
nanosleep(&t, NULL);
}
static void write_to_vm(int count)
{
for (int i = 0; i < count; i++) {
host.msgs[i].msg_len = host.packets[i].vm_pkt_size;
}
int sent = 0;
// Fast path.
if (has_bulk_forwarding) {
uint64_t retries = 0;
while (1) {
ssize_t n = sendmsg_x(options.fd, &host.msgs[sent], count-sent, 0);
if (n == -1) {
if (errno == ENOBUFS) {
wait_for_buffer_space();
retries++;
continue;
}
ERRORF("[host->vm] sendmsg_x: %s", strerror(errno));
break;
}
sent += n;
if (sent == count) {
DEBUGF("[host->vm] forwarded %d packets, %lld retries", count, retries);
return;
}
}
}
// Slow path.
int forwarded = 0;
int dropped = 0;
for (int i = sent; i < count; i++) {
struct vmpktdesc *packet = &host.packets[i];
ssize_t len;
uint64_t retries = 0;
while (1) {
len = write(options.fd, packet->vm_pkt_iov[0].iov_base,
packet->vm_pkt_size);
if (len == -1 && errno == ENOBUFS) {
wait_for_buffer_space();
retries++;
continue;
}
break;
}
if (len < 0) {
// TODO: like socket_vmnet we drop the packet and continue. Maybe trigger shutdown?
ERRORF("[host->vm] write: %s", strerror(errno));
dropped++;
continue;
}
forwarded++;
if (retries > 0) {
DEBUGF("[host->vm] write completed after %lld retries", retries);
}
// Partial write should not be possible with datagram socket.
assert((size_t)len == packet->vm_pkt_size);
}
DEBUGF("[host->vm] forwarded %d packets, %d dropped", forwarded, dropped);
}
static void packets_available(xpc_object_t event)
{
int available = xpc_dictionary_get_uint64(
event, vmnet_estimated_packets_available_key);
DEBUGF("[host->vm] %d packets available", available);
while (1) {
int count = read_from_host();
if (count < 1) {
break;
}
write_to_vm(count);
}
}
static void start_forwarding_from_host(void)
{
DEBUG("[main] enable host forwarding");
vmnet_return_t status = vmnet_interface_set_event_callback(
interface, VMNET_INTERFACE_PACKETS_AVAILABLE, host.queue,
^(interface_event_t __attribute__((unused)) event_id, xpc_object_t event) {
packets_available(event);
});
if (status != VMNET_SUCCESS) {
ERRORF("[host->vm] vmnet_interface_set_event_callback: %s", host_strerror(status));
exit(EXIT_FAILURE);
}
INFO("[main] started host formwarding");
}
static int read_from_vm(void)
{
// Fast path - read multiple packets with one syscall.
if (has_bulk_forwarding) {
int max_packets = MAX_PACKET_COUNT;
// Reset iovs - must be done before reading from vm. recvmsg_x() reads
// iov_len but does not modify it.
for (int i = 0; i < max_packets; i++) {
vm.iovs[i].iov_len = max_packet_size;
}
int count = recvmsg_x(options.fd, vm.msgs, max_packets, 0);
if (count != -1) {
return count;
}
ERRORF("[vm->host] recvmsg_x: %s", strerror(errno));
}
// Slow path - read one packet.
vm.iovs[0].iov_len = max_packet_size;
int len = read(options.fd, vm.iovs[0].iov_base, vm.iovs[0].iov_len);
if (len == -1) {
ERRORF("[vm->host] read: %s", strerror(errno));
return -1;
}
vm.msgs[0].msg_len = len;
return 1;
}
static int write_to_host(int count)
{
// Update packets and iovs to match msgs. vmnet_write() uses vm_pkt_size
// but require iov_len to match.
for (int i = 0; i < count; i++) {
vm.packets[i].vm_pkt_size = vm.msgs[i].msg_len;
}
for (int i = 0; i < count; i++) {
vm.iovs[i].iov_len = vm.msgs[i].msg_len;
}
vmnet_return_t status = vmnet_write(interface, vm.packets, &count);
if (status != VMNET_SUCCESS) {
ERRORF("[vm->host] vmnet_write: %s", host_strerror(status));
return -1;
}
return 0;
}
static void forward_from_vm(void)
{
DEBUG("[vm->host] started");
while (1) {
int count = read_from_vm();
if (count == -1) {
trigger_shutdown(STATUS_FAILURE);
break;
}
if (count == 0) {
INFO("[vm->host] socket was closed by peer");
trigger_shutdown(STATUS_STOPPED);
break;
}
if (write_to_host(count)) {
trigger_shutdown(STATUS_FAILURE);
break;
}
DEBUGF("[vm->host] forwarded %d packets", count);
}
INFO("[vm->host] stopped");
}
static void start_forwarding_from_vm(void)
{
vm.queue = dispatch_queue_create("com.github.nirs.vmnet-helper.vm", DISPATCH_QUEUE_SERIAL);
dispatch_async(vm.queue, ^{
forward_from_vm();
});
INFO("[main] started vm forwarding");
}
static void wait_for_termination(void)
{
INFO("[main] waiting for termination");
struct kevent events[1];
while (1) {
int n = kevent(kq, NULL, 0, events, 1, NULL);
if (n < 0) {
ERRORF("[main] kevent: %s", strerror(errno));
status |= STATUS_FAILURE;
break;
}
if (n > 0) {
if (events[0].filter == EVFILT_SIGNAL) {
INFOF("[main] received signal %s", strsignal(events[0].ident));
status |= STATUS_STOPPED;
break;
}
if (events[0].filter == EVFILT_USER) {
INFO("[main] received shutdown event");
status |= events[0].fflags;
break;
}
}
}
}
static void stop_host_interface(void)
{
if (interface == NULL) {
return;
}
DEBUG("[main] stopping vmnet interface");
dispatch_semaphore_t completed = dispatch_semaphore_create(0);
vmnet_return_t status = vmnet_stop_interface(
interface, host.queue, ^(vmnet_return_t status) {
if (status != VMNET_SUCCESS) {
ERRORF("[main] vmnet_stop_interface: %s", host_strerror(status));
exit(EXIT_FAILURE);
}
dispatch_semaphore_signal(completed);
});
if (status != VMNET_SUCCESS) {
ERRORF("[main] vmnet_stop_interface: %s", host_strerror(status));
exit(EXIT_FAILURE);
}
dispatch_semaphore_wait(completed, DISPATCH_TIME_FOREVER);
INFO("[main] stopped vmnet interface");
}
static int os_product_version(struct version *v)
{
char buf[20];
size_t len = sizeof(buf);
if (sysctlbyname("kern.osproductversion", buf, &len, NULL, 0) != 0) {
WARNF("sysctlbyname(kern.osproductversion): %s", strerror(errno));
return -1;
}
char *s = buf;
int *numbers[] = {&v->major, &v->minor, &v->point};
for (unsigned i = 0; i < ARRAY_SIZE(numbers); i++) {
char *p = strsep(&s, ".");
if (p == NULL) {
break;
}
*numbers[i] = atoi(p);
}
return 0;
}
static void check_os_version(const char *prog)
{
struct version v = {0};
if (os_product_version(&v)) {
return;
}
INFOF("[main] running %s %s on macOS %d.%d.%d",
prog, GIT_VERSION, v.major, v.minor, v.point);
if (v.major > 13) {
INFO("[main] enabling bulk forwarding");
has_bulk_forwarding = true;
}
}
int main(int argc, char **argv)
{
parse_options(&options, argc, argv);
check_os_version(argv[0]);
setup_kq();
start_host_interface();
drop_privileges();
setup_socket();
setup_host_buffers();
setup_vm_buffers();
start_forwarding_from_host();
start_forwarding_from_vm();
wait_for_termination();
stop_host_interface();
return (status == 0 || status & STATUS_STOPPED) ? EXIT_SUCCESS : EXIT_FAILURE;
}