-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnetdevice.go
529 lines (441 loc) · 14.8 KB
/
netdevice.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
/*
// 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 qcli
import (
"fmt"
"log"
"os"
"strconv"
"strings"
)
// NetDeviceType is a qemu networking device type.
type NetDeviceType string
const (
// USER is SLIRP user-space networking device type
USER NetDeviceType = "user"
// MCASTSOCKET is a socket networking device type
MCASTSOCKET NetDeviceType = "mcastsocket"
// 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"
DisabledNetDeviceROMFile = "off"
)
// 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 USER:
return "user"
case MCASTSOCKET:
return "socket"
case TAP, MACVTAP, IPVTAP, VETHTAP:
return "tap" // -netdev tap,<props> -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 vhost-user,<props> (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)
}
if netdev.Driver != VirtioNet {
return netdev.Driver
}
// Handle virtio-net transport
var device string
switch n {
case MCASTSOCKET:
device = "virtio-net"
case USER:
device = "virtio-net"
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 ""
}
}
// -netdev tap,ifname=,downscript=,script=
type NetDeviceTap struct {
// IfName is the interface name,
IFName string `yaml:"ifname"`
// DownScript is the tap interface deconfiguration script.
DownScript string `yaml:"downscript-file"`
// Script is the tap interface configuration script.
Script string `yaml:"upscript-file"`
}
type Port struct {
Address string `yaml:"address"`
Port int `yaml:"port"`
}
type PortRule struct {
Protocol string `yaml:"protocol"`
Host Port `yaml:"host-port"`
Guest Port `yaml:"guest-port"`
}
/*
func (p *PortRule) UnmarshalYAML(unmarshal func(interface{}) error) error {
DefaultPortProtocol := "tcp"
DefaultPortHostAddress := ""
DefaultPortGuestAddress := ""
var ruleVal map[string]string
var err error
if err = unmarshal(&ruleVal); err != nil {
return err
}
for hostVal, guestVal := range ruleVal {
hostToks := strings.Split(hostVal, ":")
if len(hostToks) == 3 {
p.Protocol = hostToks[0]
p.Host.Address = hostToks[1]
p.Host.Port, err = strconv.Atoi(hostToks[2])
if err != nil {
return err
}
} else if len(hostToks) == 2 {
p.Protocol = DefaultPortProtocol
p.Host.Address = hostToks[0]
p.Host.Port, err = strconv.Atoi(hostToks[1])
if err != nil {
return err
}
} else {
p.Protocol = DefaultPortProtocol
p.Host.Address = DefaultPortHostAddress
p.Host.Port, err = strconv.Atoi(hostToks[0])
if err != nil {
return err
}
}
guestToks := strings.Split(guestVal, ":")
if len(guestToks) == 2 {
p.Guest.Address = guestToks[0]
p.Guest.Port, err = strconv.Atoi(guestToks[1])
if err != nil {
return err
}
} else {
p.Guest.Address = DefaultPortGuestAddress
p.Guest.Port, err = strconv.Atoi(guestToks[0])
if err != nil {
return err
}
}
break
}
if p.Protocol != "tcp" && p.Protocol != "udp" {
return fmt.Errorf("Invalid PortRule.Protocol value: %s . Must be 'tcp' or 'udp'", p.Protocol)
}
return nil
}
*/
func (p PortRule) String() string {
return fmt.Sprintf("%s:%s:%d-%s:%d", p.Protocol,
p.Host.Address, p.Host.Port, p.Guest.Address, p.Guest.Port)
}
const EmptyPortRule = "::0-:0"
// -netdev user,
type NetDeviceUser struct {
IPV4 bool `yaml:"ipv4-enable"`
IPV4NetAddr string `yaml:"ipv4-network-address"`
HostForward []PortRule `yaml:"host-port-rules"`
}
// -netdev socket,listen=
type NetDeviceMcastSocket struct {
Address string `yaml:"address"`
Port string `yaml:"port"`
}
// -netdev socket,mcast=
// -netdev socket,udp=
// NetDevice represents a guest networking device
type NetDevice struct {
// Type is the netdev type (e.g. tap).
Type NetDeviceType `yaml:"type"`
// Driver is the qemu device driver
Driver DeviceDriver `yaml:"driver"`
// ID is the netdevice identifier.
ID string `yaml:"id"`
// Bus is the bus path name of a PCI device.
Bus string `yaml:"bus"`
// Addr is the address offset of a PCI device.
Addr string `yaml:"address"`
// 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 `yaml:"vhost-enable"`
// MACAddress is the networking device interface MAC address.
MACAddress string `yaml:"macaddress"`
// DisableModern prevents qemu from relying on fast MMIO.
DisableModern bool `yaml:"disable-modern"`
// ROMFile specifies the ROM file being used for this device.
ROMFile string `yaml:"rom-file"`
// DevNo identifies the ccw devices for s390x architecture
DevNo string `yaml:"ccw-dev-no"`
// Transport is the virtio transport for this device.
Transport VirtioTransport `yaml:"transport"`
// -netdev tap,.*
Tap NetDeviceTap `yaml:"tap-device"`
// -netdev user,.*
User NetDeviceUser `yaml:"user-device"`
// -netdev socket,mcast=
McastSocket NetDeviceMcastSocket `yaml:"mcast-socket"`
// bootindex
BootIndex string `yaml:"bootindex"`
}
// 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() error {
if netdev.ID == "" {
return fmt.Errorf("NetDevice has empty ID field")
}
if netdev.Type == "" {
return fmt.Errorf("NetDevice has empty Type field")
}
switch netdev.Type {
case USER, MCASTSOCKET, TAP, MACVTAP:
break
default:
return fmt.Errorf("NetDevice has Unknown Type value: %s", netdev.Type)
}
if netdev.Type == TAP && netdev.Tap.IFName == "" {
return fmt.Errorf("Netdevice Type=TAP has empty IFName field")
}
if netdev.Type == MCASTSOCKET {
if netdev.McastSocket.Address == "" {
return fmt.Errorf("Netdevice Type=MCASTSOCKET has empty Address field")
}
if netdev.McastSocket.Port == "" {
return fmt.Errorf("Netdevice Type=MCASTSOCKET has empty Port field")
}
}
return nil
}
// 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("%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=0x%02x", addr))
}
}
if netdev.BootIndex != "" {
deviceParams = append(deviceParams, fmt.Sprintf("bootindex=%s", netdev.BootIndex))
}
if strings.HasPrefix(string(driver), "virtio") {
if s := netdev.Transport.disableModern(config, netdev.DisableModern); s != "" {
deviceParams = append(deviceParams, 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) && netdev.ROMFile != "" {
// allow setting romfile= to disable the built-in romfile for the nic
romfile := netdev.ROMFile
if romfile == DisabledNetDeviceROMFile {
romfile = ""
}
deviceParams = append(deviceParams, fmt.Sprintf("romfile=%s", 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, ":")))
}
}
switch netdev.Type {
case TAP:
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.Tap.IFName))
if netdev.Tap.DownScript != "" {
netdevParams = append(netdevParams, fmt.Sprintf("downscript=%s", netdev.Tap.DownScript))
}
if netdev.Tap.Script != "" {
netdevParams = append(netdevParams, fmt.Sprintf("script=%s", netdev.Tap.Script))
}
}
case USER:
if netdev.User.IPV4 {
netdevParams = append(netdevParams, "ipv4=on")
} else {
netdevParams = append(netdevParams, "ipv4=off")
}
for _, rule := range netdev.User.HostForward {
hostfwd := rule.String()
if hostfwd != EmptyPortRule {
netdevParams = append(netdevParams, fmt.Sprintf("hostfwd=%s", hostfwd))
}
}
if netdev.User.IPV4NetAddr != "" {
netdevParams = append(netdevParams, fmt.Sprintf("net=%s", netdev.User.IPV4NetAddr))
}
case MCASTSOCKET:
var mcastParam string
mcastParam = fmt.Sprintf("mcast=%s:%s", netdev.McastSocket.Address, netdev.McastSocket.Port)
netdevParams = append(netdevParams, mcastParam)
}
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
}