This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
qemu.go
2715 lines (2161 loc) · 75.2 KB
/
qemu.go
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
/*
// Copyright contributors to the Virtual Machine Manager for Go project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
// Package qemu provides methods and types for launching and managing QEMU
// instances. Instances can be launched with the LaunchQemu function and
// managed thereafter via QMPStart and the QMP object that this function
// returns. To manage a qemu instance after it has been launched you need
// to pass the -qmp option during launch requesting the qemu instance to create
// a QMP unix domain manageent socket, e.g.,
// -qmp unix:/tmp/qmp-socket,server,nowait. For more information see the
// example below.
package qemu
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"syscall"
"context"
)
// Machine describes the machine type qemu will emulate.
type Machine struct {
// Type is the machine type to be used by qemu.
Type string
// Acceleration are the machine acceleration options to be used by qemu.
Acceleration string
// Options are options for the machine type
// For example gic-version=host and usb=off
Options string
}
const (
// MachineTypeMicrovm is the QEMU microvm machine type for amd64
MachineTypeMicrovm string = "microvm"
)
// Device is the qemu device interface.
type Device interface {
Valid() bool
QemuParams(config *Config) []string
}
// DeviceDriver is the device driver string.
type DeviceDriver string
const (
// NVDIMM is the Non Volatile DIMM device driver.
NVDIMM DeviceDriver = "nvdimm"
// VirtioNet is the virtio networking device driver.
VirtioNet DeviceDriver = "virtio-net"
// VirtioNetPCI is the virt-io pci networking device driver.
VirtioNetPCI DeviceDriver = "virtio-net-pci"
// VirtioNetCCW is the virt-io ccw networking device driver.
VirtioNetCCW DeviceDriver = "virtio-net-ccw"
// VirtioBlock is the block device driver.
VirtioBlock DeviceDriver = "virtio-blk"
// Console is the console device driver.
Console DeviceDriver = "virtconsole"
// Virtio9P is the 9pfs device driver.
Virtio9P DeviceDriver = "virtio-9p"
// VirtioSerial is the serial device driver.
VirtioSerial DeviceDriver = "virtio-serial"
// VirtioSerialPort is the serial port device driver.
VirtioSerialPort DeviceDriver = "virtserialport"
// VirtioRng is the paravirtualized RNG device driver.
VirtioRng DeviceDriver = "virtio-rng"
// VirtioBalloon is the memory balloon device driver.
VirtioBalloon DeviceDriver = "virtio-balloon"
//VhostUserSCSI represents a SCSI vhostuser device type.
VhostUserSCSI DeviceDriver = "vhost-user-scsi"
//VhostUserNet represents a net vhostuser device type.
VhostUserNet DeviceDriver = "virtio-net"
//VhostUserBlk represents a block vhostuser device type.
VhostUserBlk DeviceDriver = "vhost-user-blk"
//VhostUserFS represents a virtio-fs vhostuser device type
VhostUserFS DeviceDriver = "vhost-user-fs"
// PCIBridgeDriver represents a PCI bridge device type.
PCIBridgeDriver DeviceDriver = "pci-bridge"
// PCIePCIBridgeDriver represents a PCIe to PCI bridge device type.
PCIePCIBridgeDriver DeviceDriver = "pcie-pci-bridge"
// VfioPCI is the vfio driver with PCI transport.
VfioPCI DeviceDriver = "vfio-pci"
// VfioCCW is the vfio driver with CCW transport.
VfioCCW DeviceDriver = "vfio-ccw"
// VfioAP is the vfio driver with AP transport.
VfioAP DeviceDriver = "vfio-ap"
// VHostVSockPCI is a generic Vsock vhost device with PCI transport.
VHostVSockPCI DeviceDriver = "vhost-vsock-pci"
// PCIeRootPort is a PCIe Root Port, the PCIe device should be hotplugged to this port.
PCIeRootPort DeviceDriver = "pcie-root-port"
)
func isDimmSupported(config *Config) bool {
switch runtime.GOARCH {
case "amd64", "386", "ppc64le":
if config != nil && config.Machine.Type == MachineTypeMicrovm {
// microvm does not support NUMA
return false
}
return true
default:
return false
}
}
// VirtioTransport is the transport in use for a virtio device.
type VirtioTransport string
const (
// TransportPCI is the PCI transport for virtio device.
TransportPCI VirtioTransport = "pci"
// TransportCCW is the CCW transport for virtio devices.
TransportCCW VirtioTransport = "ccw"
// TransportMMIO is the MMIO transport for virtio devices.
TransportMMIO VirtioTransport = "mmio"
)
// defaultTransport returns the default transport for the current combination
// of host's architecture and QEMU machine type.
func (transport VirtioTransport) defaultTransport(config *Config) VirtioTransport {
switch runtime.GOARCH {
case "amd64", "386":
if config != nil && config.Machine.Type == MachineTypeMicrovm {
return TransportMMIO
}
return TransportPCI
case "s390x":
return TransportCCW
default:
return TransportPCI
}
}
// isVirtioPCI returns true if the transport is PCI.
func (transport VirtioTransport) isVirtioPCI(config *Config) bool {
if transport == "" {
transport = transport.defaultTransport(config)
}
return transport == TransportPCI
}
// isVirtioCCW returns true if the transport is CCW.
func (transport VirtioTransport) isVirtioCCW(config *Config) bool {
if transport == "" {
transport = transport.defaultTransport(config)
}
return transport == TransportCCW
}
// getName returns the name of the current transport.
func (transport VirtioTransport) getName(config *Config) string {
if transport == "" {
transport = transport.defaultTransport(config)
}
return string(transport)
}
// disableModern returns the parameters with the disable-modern option.
// In case the device driver is not a PCI device and it doesn't have the option
// an empty string is returned.
func (transport VirtioTransport) disableModern(config *Config, disable bool) string {
if !transport.isVirtioPCI(config) {
return ""
}
if disable {
return "disable-modern=true"
}
return "disable-modern=false"
}
// ObjectType is a string representing a qemu object type.
type ObjectType string
const (
// MemoryBackendFile represents a guest memory mapped file.
MemoryBackendFile ObjectType = "memory-backend-file"
)
// Object is a qemu object representation.
type Object struct {
// Driver is the qemu device driver
Driver DeviceDriver
// Type is the qemu object type.
Type ObjectType
// ID is the user defined object ID.
ID string
// DeviceID is the user defined device ID.
DeviceID string
// MemPath is the object's memory path.
// This is only relevant for memory objects
MemPath string
// Size is the object size in bytes
Size uint64
}
// Valid returns true if the Object structure is valid and complete.
func (object Object) Valid() bool {
switch object.Type {
case MemoryBackendFile:
if object.ID == "" || object.MemPath == "" || object.Size == 0 {
return false
}
default:
return false
}
return true
}
// QemuParams returns the qemu parameters built out of this Object device.
func (object Object) QemuParams(config *Config) []string {
var objectParams []string
var deviceParams []string
var qemuParams []string
deviceParams = append(deviceParams, string(object.Driver))
deviceParams = append(deviceParams, fmt.Sprintf(",id=%s", object.DeviceID))
switch object.Type {
case MemoryBackendFile:
objectParams = append(objectParams, string(object.Type))
objectParams = append(objectParams, fmt.Sprintf(",id=%s", object.ID))
objectParams = append(objectParams, fmt.Sprintf(",mem-path=%s", object.MemPath))
objectParams = append(objectParams, fmt.Sprintf(",size=%d", object.Size))
deviceParams = append(deviceParams, fmt.Sprintf(",memdev=%s", object.ID))
}
qemuParams = append(qemuParams, "-device")
qemuParams = append(qemuParams, strings.Join(deviceParams, ""))
qemuParams = append(qemuParams, "-object")
qemuParams = append(qemuParams, strings.Join(objectParams, ""))
return qemuParams
}
// Virtio9PMultidev filesystem behaviour to deal
// with multiple devices being shared with a 9p export.
type Virtio9PMultidev string
const (
// Remap shares multiple devices with only one export.
Remap Virtio9PMultidev = "remap"
// Warn assumes that only one device is shared by the same export.
// Only a warning message is logged (once) by qemu on host side.
// This is the default behaviour.
Warn Virtio9PMultidev = "warn"
// Forbid like "warn" but also deny access to additional devices on guest.
Forbid Virtio9PMultidev = "forbid"
)
// FSDriver represents a qemu filesystem driver.
type FSDriver string
// SecurityModelType is a qemu filesystem security model type.
type SecurityModelType string
const (
// Local is the local qemu filesystem driver.
Local FSDriver = "local"
// Handle is the handle qemu filesystem driver.
Handle FSDriver = "handle"
// Proxy is the proxy qemu filesystem driver.
Proxy FSDriver = "proxy"
)
const (
// None is like passthrough without failure reports.
None SecurityModelType = "none"
// PassThrough uses the same credentials on both the host and guest.
PassThrough SecurityModelType = "passthrough"
// MappedXattr stores some files attributes as extended attributes.
MappedXattr SecurityModelType = "mapped-xattr"
// MappedFile stores some files attributes in the .virtfs directory.
MappedFile SecurityModelType = "mapped-file"
)
// FSDevice represents a qemu filesystem configuration.
type FSDevice struct {
// Driver is the qemu device driver
Driver DeviceDriver
// FSDriver is the filesystem driver backend.
FSDriver FSDriver
// ID is the filesystem identifier.
ID string
// Path is the host root path for this filesystem.
Path string
// MountTag is the device filesystem mount point tag.
MountTag string
// SecurityModel is the security model for this filesystem device.
SecurityModel SecurityModelType
// DisableModern prevents qemu from relying on fast MMIO.
DisableModern bool
// ROMFile specifies the ROM file being used for this device.
ROMFile string
// DevNo identifies the ccw devices for s390x architecture
DevNo string
// Transport is the virtio transport for this device.
Transport VirtioTransport
// Multidev is the filesystem behaviour to deal
// with multiple devices being shared with a 9p export
Multidev Virtio9PMultidev
}
// Virtio9PTransport is a map of the virtio-9p device name that corresponds
// to each transport.
var Virtio9PTransport = map[VirtioTransport]string{
TransportPCI: "virtio-9p-pci",
TransportCCW: "virtio-9p-ccw",
TransportMMIO: "virtio-9p-device",
}
// Valid returns true if the FSDevice structure is valid and complete.
func (fsdev FSDevice) Valid() bool {
if fsdev.ID == "" || fsdev.Path == "" || fsdev.MountTag == "" {
return false
}
return true
}
// QemuParams returns the qemu parameters built out of this filesystem device.
func (fsdev FSDevice) QemuParams(config *Config) []string {
var fsParams []string
var deviceParams []string
var qemuParams []string
deviceParams = append(deviceParams, fsdev.deviceName(config))
if s := fsdev.Transport.disableModern(config, fsdev.DisableModern); s != "" {
deviceParams = append(deviceParams, fmt.Sprintf(",%s", s))
}
deviceParams = append(deviceParams, fmt.Sprintf(",fsdev=%s", fsdev.ID))
deviceParams = append(deviceParams, fmt.Sprintf(",mount_tag=%s", fsdev.MountTag))
if fsdev.Transport.isVirtioPCI(config) {
deviceParams = append(deviceParams, fmt.Sprintf(",romfile=%s", fsdev.ROMFile))
}
if fsdev.Transport.isVirtioCCW(config) {
if config.Knobs.IOMMUPlatform {
deviceParams = append(deviceParams, ",iommu_platform=on")
}
deviceParams = append(deviceParams, fmt.Sprintf(",devno=%s", fsdev.DevNo))
}
fsParams = append(fsParams, string(fsdev.FSDriver))
fsParams = append(fsParams, fmt.Sprintf(",id=%s", fsdev.ID))
fsParams = append(fsParams, fmt.Sprintf(",path=%s", fsdev.Path))
fsParams = append(fsParams, fmt.Sprintf(",security_model=%s", fsdev.SecurityModel))
if fsdev.Multidev != "" {
fsParams = append(fsParams, fmt.Sprintf(",multidevs=%s", fsdev.Multidev))
}
qemuParams = append(qemuParams, "-device")
qemuParams = append(qemuParams, strings.Join(deviceParams, ""))
qemuParams = append(qemuParams, "-fsdev")
qemuParams = append(qemuParams, strings.Join(fsParams, ""))
return qemuParams
}
// deviceName returns the QEMU shared filesystem device name for the current
// combination of driver and transport.
func (fsdev FSDevice) deviceName(config *Config) string {
if fsdev.Transport == "" {
fsdev.Transport = fsdev.Transport.defaultTransport(config)
}
switch fsdev.Driver {
case Virtio9P:
return Virtio9PTransport[fsdev.Transport]
}
return string(fsdev.Driver)
}
// CharDeviceBackend is the character device backend for qemu
type CharDeviceBackend string
const (
// Pipe creates a 2 way connection to the guest.
Pipe CharDeviceBackend = "pipe"
// Socket creates a 2 way stream socket (TCP or Unix).
Socket CharDeviceBackend = "socket"
// CharConsole sends traffic from the guest to QEMU's standard output.
CharConsole CharDeviceBackend = "console"
// Serial sends traffic from the guest to a serial device on the host.
Serial CharDeviceBackend = "serial"
// TTY is an alias for Serial.
TTY CharDeviceBackend = "tty"
// PTY creates a new pseudo-terminal on the host and connect to it.
PTY CharDeviceBackend = "pty"
)
// CharDevice represents a qemu character device.
type CharDevice struct {
Backend CharDeviceBackend
// Driver is the qemu device driver
Driver DeviceDriver
// Bus is the serial bus associated to this device.
Bus string
// DeviceID is the user defined device ID.
DeviceID string
ID string
Path string
Name string
// DisableModern prevents qemu from relying on fast MMIO.
DisableModern bool
// ROMFile specifies the ROM file being used for this device.
ROMFile string
// DevNo identifies the ccw devices for s390x architecture
DevNo string
// Transport is the virtio transport for this device.
Transport VirtioTransport
}
// VirtioSerialTransport is a map of the virtio-serial device name that
// corresponds to each transport.
var VirtioSerialTransport = map[VirtioTransport]string{
TransportPCI: "virtio-serial-pci",
TransportCCW: "virtio-serial-ccw",
TransportMMIO: "virtio-serial-device",
}
// Valid returns true if the CharDevice structure is valid and complete.
func (cdev CharDevice) Valid() bool {
if cdev.ID == "" || cdev.Path == "" {
return false
}
return true
}
// QemuParams returns the qemu parameters built out of this character device.
func (cdev CharDevice) QemuParams(config *Config) []string {
var cdevParams []string
var deviceParams []string
var qemuParams []string
deviceParams = append(deviceParams, cdev.deviceName(config))
if cdev.Driver == VirtioSerial {
if s := cdev.Transport.disableModern(config, cdev.DisableModern); s != "" {
deviceParams = append(deviceParams, fmt.Sprintf(",%s", s))
}
}
if cdev.Bus != "" {
deviceParams = append(deviceParams, fmt.Sprintf(",bus=%s", cdev.Bus))
}
deviceParams = append(deviceParams, fmt.Sprintf(",chardev=%s", cdev.ID))
deviceParams = append(deviceParams, fmt.Sprintf(",id=%s", cdev.DeviceID))
if cdev.Name != "" {
deviceParams = append(deviceParams, fmt.Sprintf(",name=%s", cdev.Name))
}
if cdev.Driver == VirtioSerial && cdev.Transport.isVirtioPCI(config) {
deviceParams = append(deviceParams, fmt.Sprintf(",romfile=%s", cdev.ROMFile))
}
if cdev.Driver == VirtioSerial && cdev.Transport.isVirtioCCW(config) {
if config.Knobs.IOMMUPlatform {
deviceParams = append(deviceParams, ",iommu_platform=on")
}
deviceParams = append(deviceParams, fmt.Sprintf(",devno=%s", cdev.DevNo))
}
cdevParams = append(cdevParams, string(cdev.Backend))
cdevParams = append(cdevParams, fmt.Sprintf(",id=%s", cdev.ID))
if cdev.Backend == Socket {
cdevParams = append(cdevParams, fmt.Sprintf(",path=%s,server,nowait", cdev.Path))
} else {
cdevParams = append(cdevParams, fmt.Sprintf(",path=%s", cdev.Path))
}
qemuParams = append(qemuParams, "-device")
qemuParams = append(qemuParams, strings.Join(deviceParams, ""))
qemuParams = append(qemuParams, "-chardev")
qemuParams = append(qemuParams, strings.Join(cdevParams, ""))
return qemuParams
}
// deviceName returns the QEMU device name for the current combination of
// driver and transport.
func (cdev CharDevice) deviceName(config *Config) string {
if cdev.Transport == "" {
cdev.Transport = cdev.Transport.defaultTransport(config)
}
switch cdev.Driver {
case VirtioSerial:
return VirtioSerialTransport[cdev.Transport]
}
return string(cdev.Driver)
}
// NetDeviceType is a qemu networking device type.
type NetDeviceType string
const (
// TAP is a TAP networking device type.
TAP NetDeviceType = "tap"
// MACVTAP is a macvtap networking device type.
MACVTAP NetDeviceType = "macvtap"
// IPVTAP is a ipvtap virtual networking device type.
IPVTAP NetDeviceType = "ipvtap"
// VETHTAP is a veth-tap virtual networking device type.
VETHTAP NetDeviceType = "vethtap"
// VFIO is a direct assigned PCI device or PCI VF
VFIO NetDeviceType = "VFIO"
// VHOSTUSER is a vhost-user port (socket)
VHOSTUSER NetDeviceType = "vhostuser"
)
// QemuNetdevParam converts to the QEMU -netdev parameter notation
func (n NetDeviceType) QemuNetdevParam(netdev *NetDevice, config *Config) string {
if netdev.Transport == "" {
netdev.Transport = netdev.Transport.defaultTransport(config)
}
switch n {
case TAP:
return "tap"
case MACVTAP:
return "tap"
case IPVTAP:
return "tap"
case VETHTAP:
return "tap" // -netdev type=tap -device virtio-net-pci
case VFIO:
if netdev.Transport == TransportMMIO {
log.Fatal("vfio devices are not support with the MMIO transport")
}
return "" // -device vfio-pci (no netdev)
case VHOSTUSER:
if netdev.Transport == TransportCCW {
log.Fatal("vhost-user devices are not supported on IBM Z")
}
return "vhost-user" // -netdev type=vhost-user (no device)
default:
return ""
}
}
// QemuDeviceParam converts to the QEMU -device parameter notation
func (n NetDeviceType) QemuDeviceParam(netdev *NetDevice, config *Config) DeviceDriver {
if netdev.Transport == "" {
netdev.Transport = netdev.Transport.defaultTransport(config)
}
var device string
switch n {
case TAP:
device = "virtio-net"
case MACVTAP:
device = "virtio-net"
case IPVTAP:
device = "virtio-net"
case VETHTAP:
device = "virtio-net" // -netdev type=tap -device virtio-net-pci
case VFIO:
if netdev.Transport == TransportMMIO {
log.Fatal("vfio devices are not support with the MMIO transport")
}
device = "vfio" // -device vfio-pci (no netdev)
case VHOSTUSER:
if netdev.Transport == TransportCCW {
log.Fatal("vhost-user devices are not supported on IBM Z")
}
return "" // -netdev type=vhost-user (no device)
default:
return ""
}
switch netdev.Transport {
case TransportPCI:
return DeviceDriver(device + "-pci")
case TransportCCW:
return DeviceDriver(device + "-ccw")
case TransportMMIO:
return DeviceDriver(device + "-device")
default:
return ""
}
}
// NetDevice represents a guest networking device
type NetDevice struct {
// Type is the netdev type (e.g. tap).
Type NetDeviceType
// Driver is the qemu device driver
Driver DeviceDriver
// ID is the netdevice identifier.
ID string
// IfName is the interface name,
IFName string
// Bus is the bus path name of a PCI device.
Bus string
// Addr is the address offset of a PCI device.
Addr string
// DownScript is the tap interface deconfiguration script.
DownScript string
// Script is the tap interface configuration script.
Script string
// FDs represents the list of already existing file descriptors to be used.
// This is mostly useful for mq support.
FDs []*os.File
VhostFDs []*os.File
// VHost enables virtio device emulation from the host kernel instead of from qemu.
VHost bool
// MACAddress is the networking device interface MAC address.
MACAddress string
// DisableModern prevents qemu from relying on fast MMIO.
DisableModern bool
// ROMFile specifies the ROM file being used for this device.
ROMFile string
// DevNo identifies the ccw devices for s390x architecture
DevNo string
// Transport is the virtio transport for this device.
Transport VirtioTransport
}
// VirtioNetTransport is a map of the virtio-net device name that corresponds
// to each transport.
var VirtioNetTransport = map[VirtioTransport]string{
TransportPCI: "virtio-net-pci",
TransportCCW: "virtio-net-ccw",
TransportMMIO: "virtio-net-device",
}
// Valid returns true if the NetDevice structure is valid and complete.
func (netdev NetDevice) Valid() bool {
if netdev.ID == "" || netdev.IFName == "" {
return false
}
switch netdev.Type {
case TAP:
return true
case MACVTAP:
return true
default:
return false
}
}
// mqParameter returns the parameters for multi-queue driver. If the driver is a PCI device then the
// vector flag is required. If the driver is a CCW type than the vector flag is not implemented and only
// multi-queue option mq needs to be activated. See comment in libvirt code at
// https://github.com/libvirt/libvirt/blob/6e7e965dcd3d885739129b1454ce19e819b54c25/src/qemu/qemu_command.c#L3633
func (netdev NetDevice) mqParameter(config *Config) string {
p := []string{",mq=on"}
if netdev.Transport.isVirtioPCI(config) {
// https://www.linux-kvm.org/page/Multiqueue
// -netdev tap,vhost=on,queues=N
// enable mq and specify msix vectors in qemu cmdline
// (2N+2 vectors, N for tx queues, N for rx queues, 1 for config, and one for possible control vq)
// -device virtio-net-pci,mq=on,vectors=2N+2...
// enable mq in guest by 'ethtool -L eth0 combined $queue_num'
// Clearlinux automatically sets up the queues properly
// The agent implementation should do this to ensure that it is
// always set
vectors := len(netdev.FDs)*2 + 2
p = append(p, fmt.Sprintf(",vectors=%d", vectors))
}
return strings.Join(p, "")
}
// QemuDeviceParams returns the -device parameters for this network device
func (netdev NetDevice) QemuDeviceParams(config *Config) []string {
var deviceParams []string
driver := netdev.Type.QemuDeviceParam(&netdev, config)
if driver == "" {
return nil
}
deviceParams = append(deviceParams, fmt.Sprintf("driver=%s", driver))
deviceParams = append(deviceParams, fmt.Sprintf(",netdev=%s", netdev.ID))
deviceParams = append(deviceParams, fmt.Sprintf(",mac=%s", netdev.MACAddress))
if netdev.Bus != "" {
deviceParams = append(deviceParams, fmt.Sprintf(",bus=%s", netdev.Bus))
}
if netdev.Addr != "" {
addr, err := strconv.Atoi(netdev.Addr)
if err == nil && addr >= 0 {
deviceParams = append(deviceParams, fmt.Sprintf(",addr=%x", addr))
}
}
if s := netdev.Transport.disableModern(config, netdev.DisableModern); s != "" {
deviceParams = append(deviceParams, fmt.Sprintf(",%s", s))
}
if len(netdev.FDs) > 0 {
// Note: We are appending to the device params here
deviceParams = append(deviceParams, netdev.mqParameter(config))
}
if netdev.Transport.isVirtioPCI(config) {
deviceParams = append(deviceParams, fmt.Sprintf(",romfile=%s", netdev.ROMFile))
}
if netdev.Transport.isVirtioCCW(config) {
if config.Knobs.IOMMUPlatform {
deviceParams = append(deviceParams, ",iommu_platform=on")
}
deviceParams = append(deviceParams, fmt.Sprintf(",devno=%s", netdev.DevNo))
}
return deviceParams
}
// QemuNetdevParams returns the -netdev parameters for this network device
func (netdev NetDevice) QemuNetdevParams(config *Config) []string {
var netdevParams []string
netdevType := netdev.Type.QemuNetdevParam(&netdev, config)
if netdevType == "" {
return nil
}
netdevParams = append(netdevParams, netdevType)
netdevParams = append(netdevParams, fmt.Sprintf(",id=%s", netdev.ID))
if netdev.VHost {
netdevParams = append(netdevParams, ",vhost=on")
if len(netdev.VhostFDs) > 0 {
var fdParams []string
qemuFDs := config.appendFDs(netdev.VhostFDs)
for _, fd := range qemuFDs {
fdParams = append(fdParams, fmt.Sprintf("%d", fd))
}
netdevParams = append(netdevParams, fmt.Sprintf(",vhostfds=%s", strings.Join(fdParams, ":")))
}
}
if len(netdev.FDs) > 0 {
var fdParams []string
qemuFDs := config.appendFDs(netdev.FDs)
for _, fd := range qemuFDs {
fdParams = append(fdParams, fmt.Sprintf("%d", fd))
}
netdevParams = append(netdevParams, fmt.Sprintf(",fds=%s", strings.Join(fdParams, ":")))
} else {
netdevParams = append(netdevParams, fmt.Sprintf(",ifname=%s", netdev.IFName))
if netdev.DownScript != "" {
netdevParams = append(netdevParams, fmt.Sprintf(",downscript=%s", netdev.DownScript))
}
if netdev.Script != "" {
netdevParams = append(netdevParams, fmt.Sprintf(",script=%s", netdev.Script))
}
}
return netdevParams
}
// QemuParams returns the qemu parameters built out of this network device.
func (netdev NetDevice) QemuParams(config *Config) []string {
var netdevParams []string
var deviceParams []string
var qemuParams []string
// Macvtap can only be connected via fds
if (netdev.Type == MACVTAP) && (len(netdev.FDs) == 0) {
return nil // implicit error
}
if netdev.Type.QemuNetdevParam(&netdev, config) != "" {
netdevParams = netdev.QemuNetdevParams(config)
if netdevParams != nil {
qemuParams = append(qemuParams, "-netdev")
qemuParams = append(qemuParams, strings.Join(netdevParams, ""))
}
}
if netdev.Type.QemuDeviceParam(&netdev, config) != "" {
deviceParams = netdev.QemuDeviceParams(config)
if deviceParams != nil {
qemuParams = append(qemuParams, "-device")
qemuParams = append(qemuParams, strings.Join(deviceParams, ""))
}
}
return qemuParams
}
// SerialDevice represents a qemu serial device.
type SerialDevice struct {
// Driver is the qemu device driver
Driver DeviceDriver
// ID is the serial device identifier.
ID string
// DisableModern prevents qemu from relying on fast MMIO.
DisableModern bool
// ROMFile specifies the ROM file being used for this device.
ROMFile string
// DevNo identifies the ccw devices for s390x architecture
DevNo string
// Transport is the virtio transport for this device.
Transport VirtioTransport
// MaxPorts is the maximum number of ports for this device.
MaxPorts uint
}
// Valid returns true if the SerialDevice structure is valid and complete.
func (dev SerialDevice) Valid() bool {
if dev.Driver == "" || dev.ID == "" {
return false
}
return true
}
// QemuParams returns the qemu parameters built out of this serial device.
func (dev SerialDevice) QemuParams(config *Config) []string {
var deviceParams []string
var qemuParams []string
deviceParams = append(deviceParams, dev.deviceName(config))
if s := dev.Transport.disableModern(config, dev.DisableModern); s != "" {
deviceParams = append(deviceParams, fmt.Sprintf(",%s", s))
}
deviceParams = append(deviceParams, fmt.Sprintf(",id=%s", dev.ID))
if dev.Transport.isVirtioPCI(config) {
deviceParams = append(deviceParams, fmt.Sprintf(",romfile=%s", dev.ROMFile))
if dev.Driver == VirtioSerial && dev.MaxPorts != 0 {
deviceParams = append(deviceParams, fmt.Sprintf(",max_ports=%d", dev.MaxPorts))
}
}
if dev.Transport.isVirtioCCW(config) {
if config.Knobs.IOMMUPlatform {
deviceParams = append(deviceParams, ",iommu_platform=on")
}
deviceParams = append(deviceParams, fmt.Sprintf(",devno=%s", dev.DevNo))
}
qemuParams = append(qemuParams, "-device")
qemuParams = append(qemuParams, strings.Join(deviceParams, ""))
return qemuParams
}
// deviceName returns the QEMU device name for the current combination of
// driver and transport.
func (dev SerialDevice) deviceName(config *Config) string {
if dev.Transport == "" {
dev.Transport = dev.Transport.defaultTransport(config)
}
switch dev.Driver {
case VirtioSerial:
return VirtioSerialTransport[dev.Transport]
}
return string(dev.Driver)
}
// BlockDeviceInterface defines the type of interface the device is connected to.
type BlockDeviceInterface string
// BlockDeviceAIO defines the type of asynchronous I/O the block device should use.
type BlockDeviceAIO string
// BlockDeviceFormat defines the image format used on a block device.
type BlockDeviceFormat string
const (
// NoInterface for block devices with no interfaces.
NoInterface BlockDeviceInterface = "none"
// SCSI represents a SCSI block device interface.
SCSI BlockDeviceInterface = "scsi"
)
const (
// Threads is the pthread asynchronous I/O implementation.
Threads BlockDeviceAIO = "threads"
// Native is the pthread asynchronous I/O implementation.