-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
virtualization_11.m
1048 lines (900 loc) · 34.5 KB
/
virtualization_11.m
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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// virtualization_11.m
//
// Created by codehex.
//
#import "virtualization_11.h"
@implementation Observer
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
{
if ([keyPath isEqualToString:@"state"]) {
int newState = (int)[change[NSKeyValueChangeNewKey] integerValue];
changeStateOnObserver(newState, (uintptr_t)context);
}
}
@end
@implementation VZVirtualMachineDelegateWrapper
- (instancetype)init
{
self = [super init];
if (self) {
_delegates = [NSHashTable weakObjectsHashTable];
}
return self;
}
- (void)addDelegate:(id<VZVirtualMachineDelegate>)delegate
{
[self.delegates addObject:delegate];
}
- (void)guestDidStopVirtualMachine:(VZVirtualMachine *)virtualMachine
{
for (id<VZVirtualMachineDelegate> delegate in self.delegates) {
if ([delegate respondsToSelector:@selector(guestDidStopVirtualMachine:)]) {
[delegate guestDidStopVirtualMachine:virtualMachine];
}
}
}
- (void)virtualMachine:(VZVirtualMachine *)virtualMachine didStopWithError:(NSError *)error
{
for (id<VZVirtualMachineDelegate> delegate in self.delegates) {
if ([delegate respondsToSelector:@selector(virtualMachine:didStopWithError:)]) {
[delegate virtualMachine:virtualMachine didStopWithError:error];
}
}
}
- (void)virtualMachine:(VZVirtualMachine *)virtualMachine networkDevice:(VZNetworkDevice *)networkDevice
attachmentWasDisconnectedWithError:(NSError *)error
{
for (id<VZVirtualMachineDelegate> delegate in self.delegates) {
if ([delegate respondsToSelector:@selector(virtualMachine:networkDevice:attachmentWasDisconnectedWithError:)]) {
[delegate virtualMachine:virtualMachine networkDevice:networkDevice attachmentWasDisconnectedWithError:error];
}
}
}
@end
@implementation NetworkDeviceDisconnectedHandler {
uintptr_t _cgoHandle;
}
- (instancetype)initWithHandle:(uintptr_t)cgoHandle
{
self = [super init];
if (self) {
_cgoHandle = cgoHandle;
}
return self;
}
- (void)virtualMachine:(VZVirtualMachine *)virtualMachine
networkDevice:(VZNetworkDevice *)networkDevice
attachmentWasDisconnectedWithError:(NSError *)error
{
int index = [self networkDevices:virtualMachine.networkDevices indexOf:networkDevice];
emitAttachmentWasDisconnected(index, error, _cgoHandle);
}
- (int)networkDevices:(NSArray<VZNetworkDevice *> *)networkDevices
indexOf:(VZNetworkDevice *)networkDevice
{
NSInteger index = [networkDevices indexOfObject:networkDevice];
if (index != NSNotFound) {
return (int)index;
}
return -1;
}
- (void)dealloc
{
closeAttachmentWasDisconnectedChannel(_cgoHandle);
[super dealloc];
}
@end
@implementation ObservableVZVirtualMachine {
Observer *_observer;
VZVirtualMachineDelegateWrapper *_delegateWrapper;
};
- (instancetype)initWithConfiguration:(VZVirtualMachineConfiguration *)configuration
queue:(dispatch_queue_t)queue
statusUpdateHandle:(uintptr_t)statusUpdateHandle
{
self = [super initWithConfiguration:configuration queue:queue];
if (self) {
_observer = [[Observer alloc] init];
[self addObserver:_observer
forKeyPath:@"state"
options:NSKeyValueObservingOptionNew
context:(void *)statusUpdateHandle];
_delegateWrapper = [[VZVirtualMachineDelegateWrapper alloc] init];
[super setDelegate:_delegateWrapper];
}
return self;
}
- (void)setDelegate:(id<VZVirtualMachineDelegate>)delegate
{
if (delegate != _delegateWrapper) {
[_delegateWrapper addDelegate:delegate];
}
}
- (id<VZVirtualMachineDelegate>)delegate
{
return _delegateWrapper;
}
- (void)dealloc
{
[self removeObserver:_observer forKeyPath:@"state"];
[_observer release];
[_delegateWrapper release];
[super dealloc];
}
@end
@implementation VZVirtioSocketListenerDelegateImpl {
uintptr_t _cgoHandle;
}
- (instancetype)initWithHandle:(uintptr_t)cgoHandle
{
self = [super init];
if (self) {
_cgoHandle = cgoHandle;
}
return self;
}
- (BOOL)listener:(VZVirtioSocketListener *)listener shouldAcceptNewConnection:(VZVirtioSocketConnection *)connection fromSocketDevice:(VZVirtioSocketDevice *)socketDevice;
{
return (BOOL)shouldAcceptNewConnectionHandler(_cgoHandle, connection, socketDevice);
}
@end
/*!
@abstract Create a VZLinuxBootLoader with the Linux kernel passed as URL.
@param kernelPath Path of Linux kernel on the local file system.
*/
void *newVZLinuxBootLoader(const char *kernelPath)
{
if (@available(macOS 11, *)) {
NSString *kernelPathNSString = [NSString stringWithUTF8String:kernelPath];
NSURL *kernelURL = [NSURL fileURLWithPath:kernelPathNSString];
return [[VZLinuxBootLoader alloc] initWithKernelURL:kernelURL];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Set the command-line parameters.
@param bootLoader VZLinuxBootLoader
@param commandLine The command-line parameters passed to the kernel on boot.
@link https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html
*/
void setCommandLineVZLinuxBootLoader(void *bootLoaderPtr, const char *commandLine)
{
if (@available(macOS 11, *)) {
NSString *commandLineNSString = [NSString stringWithUTF8String:commandLine];
[(VZLinuxBootLoader *)bootLoaderPtr setCommandLine:commandLineNSString];
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Set the optional initial RAM disk.
@param bootLoader VZLinuxBootLoader
@param ramdiskPath The RAM disk is mapped into memory before booting the kernel.
@link https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html
*/
void setInitialRamdiskURLVZLinuxBootLoader(void *bootLoaderPtr, const char *ramdiskPath)
{
if (@available(macOS 11, *)) {
NSString *ramdiskPathNSString = [NSString stringWithUTF8String:ramdiskPath];
NSURL *ramdiskURL = [NSURL fileURLWithPath:ramdiskPathNSString];
[(VZLinuxBootLoader *)bootLoaderPtr setInitialRamdiskURL:ramdiskURL];
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Validate the configuration.
@param config Virtual machine configuration.
@param error If not nil, assigned with the validation error if the validation failed.
@return true if the configuration is valid.
*/
bool validateVZVirtualMachineConfiguration(void *config, void **error)
{
if (@available(macOS 11, *)) {
return (bool)[(VZVirtualMachineConfiguration *)config
validateWithError:(NSError *_Nullable *_Nullable)error];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract: Minimum amount of memory required by virtual machines.
@see VZVirtualMachineConfiguration.memorySize
*/
unsigned long long minimumAllowedMemorySizeVZVirtualMachineConfiguration()
{
if (@available(macOS 11, *)) {
return (unsigned long long)[VZVirtualMachineConfiguration minimumAllowedMemorySize];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract: Maximum amount of memory allowed for a virtual machine.
@see VZVirtualMachineConfiguration.memorySize
*/
unsigned long long maximumAllowedMemorySizeVZVirtualMachineConfiguration()
{
if (@available(macOS 11, *)) {
return (unsigned long long)[VZVirtualMachineConfiguration maximumAllowedMemorySize];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract: Minimum number of CPUs for a virtual machine.
@see VZVirtualMachineConfiguration.CPUCount
*/
unsigned int minimumAllowedCPUCountVZVirtualMachineConfiguration()
{
if (@available(macOS 11, *)) {
return (unsigned int)[VZVirtualMachineConfiguration minimumAllowedCPUCount];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract: Maximum number of CPUs for a virtual machine.
@see VZVirtualMachineConfiguration.CPUCount
*/
unsigned int maximumAllowedCPUCountVZVirtualMachineConfiguration()
{
if (@available(macOS 11, *)) {
return (unsigned int)[VZVirtualMachineConfiguration maximumAllowedCPUCount];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Create a new Virtual machine configuration.
@param bootLoader Boot loader used when the virtual machine starts.
@param CPUCount Number of CPUs.
@discussion
The number of CPUs must be a value between VZVirtualMachineConfiguration.minimumAllowedCPUCount
and VZVirtualMachineConfiguration.maximumAllowedCPUCount.
@see VZVirtualMachineConfiguration.minimumAllowedCPUCount
@see VZVirtualMachineConfiguration.maximumAllowedCPUCount
@param memorySize Virtual machine memory size in bytes.
@discussion
The memory size must be a multiple of a 1 megabyte (1024 * 1024 bytes) between VZVirtualMachineConfiguration.minimumAllowedMemorySize
and VZVirtualMachineConfiguration.maximumAllowedMemorySize.
The memorySize represents the total physical memory seen by a guest OS running in the virtual machine.
Not all memory is allocated on start, the virtual machine allocates memory on demand.
@see VZVirtualMachineConfiguration.minimumAllowedMemorySize
@see VZVirtualMachineConfiguration.maximumAllowedMemorySize
*/
void *newVZVirtualMachineConfiguration(void *bootLoaderPtr,
unsigned int CPUCount,
unsigned long long memorySize)
{
if (@available(macOS 11, *)) {
VZVirtualMachineConfiguration *config = [[VZVirtualMachineConfiguration alloc] init];
[config setBootLoader:(VZLinuxBootLoader *)bootLoaderPtr];
[config setCPUCount:(NSUInteger)CPUCount];
[config setMemorySize:memorySize];
return config;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract List of entropy devices. Empty by default.
@see VZVirtioEntropyDeviceConfiguration
*/
void setEntropyDevicesVZVirtualMachineConfiguration(void *config,
void *entropyDevices)
{
if (@available(macOS 11, *)) {
[(VZVirtualMachineConfiguration *)config setEntropyDevices:[(NSMutableArray *)entropyDevices copy]];
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract List of memory balloon devices. Empty by default.
@see VZVirtioTraditionalMemoryBalloonDeviceConfiguration
*/
void setMemoryBalloonDevicesVZVirtualMachineConfiguration(void *config,
void *memoryBalloonDevices)
{
if (@available(macOS 11, *)) {
[(VZVirtualMachineConfiguration *)config setMemoryBalloonDevices:[(NSMutableArray *)memoryBalloonDevices copy]];
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract List of network adapters. Empty by default.
@see VZVirtioNetworkDeviceConfiguration
*/
void setNetworkDevicesVZVirtualMachineConfiguration(void *config,
void *networkDevices)
{
if (@available(macOS 11, *)) {
[(VZVirtualMachineConfiguration *)config setNetworkDevices:[(NSMutableArray *)networkDevices copy]];
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Return the list of network devices configurations for this VZVirtualMachineConfiguration. Return an empty array if no network device configuration is set.
*/
void *networkDevicesVZVirtualMachineConfiguration(void *config)
{
if (@available(macOS 11, *)) {
return [(VZVirtualMachineConfiguration *)config networkDevices]; // NSArray<VZSocketDeviceConfiguration *>
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract List of serial ports. Empty by default.
@see VZVirtioConsoleDeviceSerialPortConfiguration
*/
void setSerialPortsVZVirtualMachineConfiguration(void *config,
void *serialPorts)
{
if (@available(macOS 11, *)) {
[(VZVirtualMachineConfiguration *)config setSerialPorts:[(NSMutableArray *)serialPorts copy]];
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract List of socket devices. Empty by default.
@see VZVirtioSocketDeviceConfiguration
*/
void setSocketDevicesVZVirtualMachineConfiguration(void *config,
void *socketDevices)
{
if (@available(macOS 11, *)) {
[(VZVirtualMachineConfiguration *)config setSocketDevices:[(NSMutableArray *)socketDevices copy]];
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Return the list of socket devices configurations for this VZVirtualMachineConfiguration. Return an empty array if no socket device configuration is set.
*/
void *socketDevicesVZVirtualMachineConfiguration(void *config)
{
if (@available(macOS 11, *)) {
return [(VZVirtualMachineConfiguration *)config socketDevices]; // NSArray<VZSocketDeviceConfiguration *>
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract List of disk devices. Empty by default.
@see VZVirtioBlockDeviceConfiguration
*/
void setStorageDevicesVZVirtualMachineConfiguration(void *config,
void *storageDevices)
{
if (@available(macOS 11, *)) {
[(VZVirtualMachineConfiguration *)config setStorageDevices:[(NSMutableArray *)storageDevices copy]];
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Return the list of storage devices configurations for this VZVirtualMachineConfiguration. Return an empty array if no storage device configuration is set.
*/
void *storageDevicesVZVirtualMachineConfiguration(void *config)
{
if (@available(macOS 11, *)) {
return [(VZVirtualMachineConfiguration *)config storageDevices]; // NSArray<VZStorageDeviceConfiguration *>
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Intialize the VZFileHandleSerialPortAttachment from file descriptors.
@param readFileDescriptor File descriptor for reading from the file.
@param writeFileDescriptor File descriptor for writing to the file.
@discussion
Each file descriptor must a valid.
*/
void *newVZFileHandleSerialPortAttachment(int readFileDescriptor, int writeFileDescriptor)
{
if (@available(macOS 11, *)) {
VZFileHandleSerialPortAttachment *ret;
@autoreleasepool {
NSFileHandle *fileHandleForReading = [[NSFileHandle alloc] initWithFileDescriptor:readFileDescriptor];
NSFileHandle *fileHandleForWriting = [[NSFileHandle alloc] initWithFileDescriptor:writeFileDescriptor];
ret = [[VZFileHandleSerialPortAttachment alloc]
initWithFileHandleForReading:fileHandleForReading
fileHandleForWriting:fileHandleForWriting];
}
return ret;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Initialize the VZFileSerialPortAttachment from a URL of a file.
@param filePath The path of the file for the attachment on the local file system.
@param shouldAppend True if the file should be opened in append mode, false otherwise.
When a file is opened in append mode, writing to that file will append to the end of it.
@param error If not nil, used to report errors if initialization fails.
@return A VZFileSerialPortAttachment on success. Nil otherwise and the error parameter is populated if set.
*/
void *newVZFileSerialPortAttachment(const char *filePath, bool shouldAppend, void **error)
{
if (@available(macOS 11, *)) {
NSString *filePathNSString = [NSString stringWithUTF8String:filePath];
NSURL *fileURL = [NSURL fileURLWithPath:filePathNSString];
return [[VZFileSerialPortAttachment alloc]
initWithURL:fileURL
append:(BOOL)shouldAppend
error:(NSError *_Nullable *_Nullable)error];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Create a new Virtio Console Serial Port Device configuration
@param attachment Base class for a serial port attachment.
@discussion
The device creates a console which enables communication between the host and the guest through the Virtio interface.
The device sets up a single port on the Virtio console device.
*/
void *newVZVirtioConsoleDeviceSerialPortConfiguration(void *attachment)
{
if (@available(macOS 11, *)) {
VZVirtioConsoleDeviceSerialPortConfiguration *config = [[VZVirtioConsoleDeviceSerialPortConfiguration alloc] init];
[config setAttachment:(VZSerialPortAttachment *)attachment];
return config;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Return the list of network interfaces available for bridging.
@discussion
A bridged interface is shared between the virtual machine and the host system. Both host and virtual machine send and receive packets on the same physical interface but have distinct network layers.
VZBridgedNetworkInterface cannot be instantiated directly. It can be used with a VZBridgedNetworkDeviceAttachment to set up a network device VZNetworkDeviceConfiguration.
@seealso VZBridgedNetworkDeviceAttachment
@seealso VZNATNetworkDeviceAttachment
@seealso VZNetworkDeviceConfiguration
*/
void *VZBridgedNetworkInterface_networkInterfaces()
{
if (@available(macOS 11, *)) {
return [VZBridgedNetworkInterface networkInterfaces]; // NSArray<VZBridgedNetworkInterface *>
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Return the unique identifier for this interface. The identifier is the BSD name associated with the interface (e.g. "en0").
*/
const char *VZBridgedNetworkInterface_identifier(void *networkInterface)
{
if (@available(macOS 11, *)) {
return [[(VZBridgedNetworkInterface *)networkInterface identifier] UTF8String];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Return a display name if available (e.g. "Ethernet").
*/
const char *VZBridgedNetworkInterface_localizedDisplayName(void *networkInterface)
{
if (@available(macOS 11, *)) {
return [[(VZBridgedNetworkInterface *)networkInterface localizedDisplayName] UTF8String];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Create a new Network device attachment bridging a host physical interface with a virtual network device.
@param networkInterface a network interface that bridges a physical interface.
@discussion
A bridged network allows the virtual machine to use the same physical interface as the host. Both host and virtual machine
send and receive packets on the same physical interface but have distinct network layers.
The bridge network device attachment is used with a VZNetworkDeviceConfiguration to define a virtual network device.
Using a VZBridgedNetworkDeviceAttachment requires the app to have the "com.apple.vm.networking" entitlement.
@see VZBridgedNetworkInterface
@see VZNetworkDeviceConfiguration
@see VZVirtioNetworkDeviceConfiguration
*/
void *newVZBridgedNetworkDeviceAttachment(void *networkInterface)
{
if (@available(macOS 11, *)) {
return [[VZBridgedNetworkDeviceAttachment alloc] initWithInterface:(VZBridgedNetworkInterface *)networkInterface];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Create a new Network device attachment using network address translation (NAT) with outside networks.
@discussion
Using the NAT attachment type, the host serves as router and performs network address translation for accesses to outside networks.
@see VZNetworkDeviceConfiguration
@see VZVirtioNetworkDeviceConfiguration
*/
void *newVZNATNetworkDeviceAttachment()
{
if (@available(macOS 11, *)) {
return [[VZNATNetworkDeviceAttachment alloc] init];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Create a new Network device attachment sending raw network packets over a file handle.
@discussion
The file handle attachment transmits the raw packets/frames between the virtual network interface and a file handle.
The data transmitted through this attachment is at the level of the data link layer.
The file handle must hold a connected datagram socket.
@see VZNetworkDeviceConfiguration
@see VZVirtioNetworkDeviceConfiguration
*/
void *newVZFileHandleNetworkDeviceAttachment(int fileDescriptor)
{
if (@available(macOS 11, *)) {
VZFileHandleNetworkDeviceAttachment *ret;
@autoreleasepool {
NSFileHandle *fileHandle = [[NSFileHandle alloc] initWithFileDescriptor:fileDescriptor];
ret = [[VZFileHandleNetworkDeviceAttachment alloc] initWithFileHandle:fileHandle];
}
return ret;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Create a new Configuration of a paravirtualized network device of type Virtio Network Device.
@discussion
The communication channel used on the host is defined through the attachment. It is set with the VZNetworkDeviceConfiguration.attachment property.
The configuration is only valid with valid MACAddress and attachment.
@see VZVirtualMachineConfiguration.networkDevices
@param attachment Base class for a network device attachment.
@discussion
A network device attachment defines how a virtual network device interfaces with the host system.
VZNetworkDeviceAttachment should not be instantiated directly. One of its subclasses should be used instead.
Common attachment types include:
- VZNATNetworkDeviceAttachment
- VZFileHandleNetworkDeviceAttachment
@see VZBridgedNetworkDeviceAttachment
@see VZFileHandleNetworkDeviceAttachment
@see VZNATNetworkDeviceAttachment
*/
void *newVZVirtioNetworkDeviceConfiguration(void *attachment)
{
if (@available(macOS 11, *)) {
VZVirtioNetworkDeviceConfiguration *config = [[VZVirtioNetworkDeviceConfiguration alloc] init];
[config setAttachment:(VZNetworkDeviceAttachment *)attachment];
return config;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Create a new Virtio Entropy Device confiuration
@discussion The device exposes a source of entropy for the guest's random number generator.
*/
void *newVZVirtioEntropyDeviceConfiguration()
{
if (@available(macOS 11, *)) {
return [[VZVirtioEntropyDeviceConfiguration alloc] init];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Initialize a VZVirtioBlockDeviceConfiguration with a device attachment.
@param attachment The storage device attachment. This defines how the virtualized device operates on the host side.
@see VZDiskImageStorageDeviceAttachment
*/
void *newVZVirtioBlockDeviceConfiguration(void *attachment)
{
if (@available(macOS 11, *)) {
return [[VZVirtioBlockDeviceConfiguration alloc] initWithAttachment:(VZStorageDeviceAttachment *)attachment];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Initialize the attachment from a local file url.
@param diskPath Local file path to the disk image in RAW format.
@param readOnly If YES, the device attachment is read-only, otherwise the device can write data to the disk image.
@param error If not nil, assigned with the error if the initialization failed.
@return A VZDiskImageStorageDeviceAttachment on success. Nil otherwise and the error parameter is populated if set.
*/
void *newVZDiskImageStorageDeviceAttachment(const char *diskPath, bool readOnly, void **error)
{
if (@available(macOS 11, *)) {
NSString *diskPathNSString = [NSString stringWithUTF8String:diskPath];
NSURL *diskURL = [NSURL fileURLWithPath:diskPathNSString];
return [[VZDiskImageStorageDeviceAttachment alloc]
initWithURL:diskURL
readOnly:(BOOL)readOnly
error:(NSError *_Nullable *_Nullable)error];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Create a configuration of the Virtio traditional memory balloon device.
@discussion
This configuration creates a Virtio traditional memory balloon device which allows for managing guest memory.
Only one Virtio traditional memory balloon device can be used per virtual machine.
@see VZVirtioTraditionalMemoryBalloonDevice
*/
void *newVZVirtioTraditionalMemoryBalloonDeviceConfiguration()
{
if (@available(macOS 11, *)) {
return [[VZVirtioTraditionalMemoryBalloonDeviceConfiguration alloc] init];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Create a configuration of the Virtio socket device.
@discussion
This configuration creates a Virtio socket device for the guest which communicates with the host through the Virtio interface.
Only one Virtio socket device can be used per virtual machine.
@see VZVirtioSocketDevice
*/
void *newVZVirtioSocketDeviceConfiguration()
{
if (@available(macOS 11, *)) {
return [[VZVirtioSocketDeviceConfiguration alloc] init];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract The VZVirtioSocketListener object represents a listener for the Virtio socket device.
@discussion
The listener encompasses a VZVirtioSocketListenerDelegate object.
VZVirtioSocketListener is used with VZVirtioSocketDevice to listen to a particular port.
The delegate is used when a guest connects to a port associated with the listener.
@see VZVirtioSocketDevice
@see VZVirtioSocketListenerDelegate
*/
void *newVZVirtioSocketListener(uintptr_t cgoHandle)
{
if (@available(macOS 11, *)) {
VZVirtioSocketListener *ret = [[VZVirtioSocketListener alloc] init];
[ret setDelegate:[[[VZVirtioSocketListenerDelegateImpl alloc] initWithHandle:cgoHandle] autorelease]];
return ret;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Sets a listener at a specified port.
@discussion
There is only one listener per port, any existing listener will be removed, and the specified listener here will be set instead.
The same listener can be registered on multiple ports.
The listener's delegate will be called whenever the guest connects to that port.
@param listener The VZVirtioSocketListener object to be set.
@param port The port number to set the listener at.
*/
void VZVirtioSocketDevice_setSocketListenerForPort(void *socketDevice, void *vmQueue, void *listener, uint32_t port)
{
if (@available(macOS 11, *)) {
dispatch_sync((dispatch_queue_t)vmQueue, ^{
[(VZVirtioSocketDevice *)socketDevice setSocketListener:(VZVirtioSocketListener *)listener forPort:port];
});
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Removes the listener at a specfied port.
@discussion Does nothing if the port had no listener.
@param port The port number at which the listener is to be removed.
*/
void VZVirtioSocketDevice_removeSocketListenerForPort(void *socketDevice, void *vmQueue, uint32_t port)
{
if (@available(macOS 11, *)) {
dispatch_sync((dispatch_queue_t)vmQueue, ^{
[(VZVirtioSocketDevice *)socketDevice removeSocketListenerForPort:port];
});
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Connects to a specified port.
@discussion Does nothing if the guest does not listen on that port.
@param port The port number to connect to.
@param completionHandler Block called after the connection has been successfully established or on error.
The error parameter passed to the block is nil if the connection was successful.
*/
void VZVirtioSocketDevice_connectToPort(void *socketDevice, void *vmQueue, uint32_t port, uintptr_t cgoHandle)
{
if (@available(macOS 11, *)) {
dispatch_async((dispatch_queue_t)vmQueue, ^{
[(VZVirtioSocketDevice *)socketDevice connectToPort:port
completionHandler:^(VZVirtioSocketConnection *connection, NSError *err) {
connectionHandler(connection, err, cgoHandle);
}];
});
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
VZVirtioSocketConnectionFlat convertVZVirtioSocketConnection2Flat(void *connection)
{
if (@available(macOS 11, *)) {
VZVirtioSocketConnectionFlat ret;
ret.sourcePort = [(VZVirtioSocketConnection *)connection sourcePort];
ret.destinationPort = [(VZVirtioSocketConnection *)connection destinationPort];
ret.fileDescriptor = [(VZVirtioSocketConnection *)connection fileDescriptor];
return ret;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Initialize the virtual machine.
@param config The configuration of the virtual machine.
The configuration must be valid. Validation can be performed at runtime with [VZVirtualMachineConfiguration validateWithError:].
The configuration is copied by the initializer.
@param queue The serial queue on which the virtual machine operates.
Every operation on the virtual machine must be done on that queue. The callbacks and delegate methods are invoked on that queue.
If the queue is not serial, the behavior is undefined.
*/
void *newVZVirtualMachineWithDispatchQueue(void *config, void *queue, uintptr_t statusUpdateCgoHandle, uintptr_t disconnectedCgoHandle)
{
if (@available(macOS 11, *)) {
ObservableVZVirtualMachine *vm = [[ObservableVZVirtualMachine alloc]
initWithConfiguration:(VZVirtualMachineConfiguration *)config
queue:(dispatch_queue_t)queue
statusUpdateHandle:statusUpdateCgoHandle];
NetworkDeviceDisconnectedHandler *delegate = [[NetworkDeviceDisconnectedHandler alloc] initWithHandle:disconnectedCgoHandle];
[vm setDelegate:[delegate autorelease]];
return vm;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Return the list of socket devices configured on this virtual machine. Return an empty array if no socket device is configured.
@see VZVirtioSocketDeviceConfiguration
@see VZVirtualMachineConfiguration
*/
void *VZVirtualMachine_socketDevices(void *machine)
{
if (@available(macOS 11, *)) {
return [(VZVirtualMachine *)machine socketDevices]; // NSArray<VZSocketDevice *>
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Initialize the VZMACAddress from a string representation of a MAC address.
@param string
The string should be formatted representing the 6 bytes in hexadecimal separated by a colon character.
e.g. "01:23:45:ab:cd:ef"
The alphabetical characters can appear lowercase or uppercase.
@return A VZMACAddress or nil if the string is not formatted correctly.
*/
void *newVZMACAddress(const char *macAddress)
{
if (@available(macOS 11, *)) {
NSString *str = [NSString stringWithUTF8String:macAddress];
return [[VZMACAddress alloc] initWithString:str];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Create a valid, random, unicast, locally administered address.
@discussion The generated address is not guaranteed to be unique.
*/
void *newRandomLocallyAdministeredVZMACAddress()
{
if (@available(macOS 11, *)) {
return [VZMACAddress randomLocallyAdministeredAddress];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Sets the media access control address of the device.
*/
void setNetworkDevicesVZMACAddress(void *config, void *macAddress)
{
if (@available(macOS 11, *)) {
[(VZNetworkDeviceConfiguration *)config setMACAddress:[(VZMACAddress *)macAddress copy]];
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract The address represented as a string.
@discussion
The 6 bytes are represented in hexadecimal form, separated by a colon character.
Alphabetical characters are lowercase.
The address is compatible with the parameter of -[VZMACAddress initWithString:].
*/
const char *getVZMACAddressString(void *macAddress)
{
if (@available(macOS 11, *)) {
return [[(VZMACAddress *)macAddress string] UTF8String];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Request that the guest turns itself off.
@param error If not nil, assigned with the error if the request failed.
@return YES if the request was made successfully.
*/
bool requestStopVirtualMachine(void *machine, void *queue, void **error)
{
if (@available(macOS 11, *)) {
__block BOOL ret;
dispatch_sync((dispatch_queue_t)queue, ^{
ret = [(VZVirtualMachine *)machine requestStopWithError:(NSError *_Nullable *_Nullable)error];
});
return (bool)ret;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
void *makeDispatchQueue(const char *label)
{
// dispatch_queue_attr_t attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_DEFAULT, 0);
dispatch_queue_t queue = dispatch_queue_create(label, DISPATCH_QUEUE_SERIAL);
// dispatch_retain(queue);
return queue;
}
void startWithCompletionHandler(void *machine, void *queue, uintptr_t cgoHandle)
{
if (@available(macOS 11, *)) {
vm_completion_handler_t handler = makeVMCompletionHandler(cgoHandle);
dispatch_sync((dispatch_queue_t)queue, ^{
[(VZVirtualMachine *)machine startWithCompletionHandler:handler];
});
Block_release(handler);
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
void pauseWithCompletionHandler(void *machine, void *queue, uintptr_t cgoHandle)
{
if (@available(macOS 11, *)) {
vm_completion_handler_t handler = makeVMCompletionHandler(cgoHandle);
dispatch_sync((dispatch_queue_t)queue, ^{
[(VZVirtualMachine *)machine pauseWithCompletionHandler:handler];
});
Block_release(handler);
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
void resumeWithCompletionHandler(void *machine, void *queue, uintptr_t cgoHandle)
{
if (@available(macOS 11, *)) {
vm_completion_handler_t handler = makeVMCompletionHandler(cgoHandle);
dispatch_sync((dispatch_queue_t)queue, ^{
[(VZVirtualMachine *)machine resumeWithCompletionHandler:handler];
});
Block_release(handler);
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
// TODO(codehex): use KVO
bool vmCanStart(void *machine, void *queue)
{
if (@available(macOS 11, *)) {
__block BOOL result;
dispatch_sync((dispatch_queue_t)queue, ^{