diff --git a/qemu/qmp.go b/qemu/qmp.go index 92000fe03f..264601d7cb 100644 --- a/qemu/qmp.go +++ b/qemu/qmp.go @@ -764,6 +764,22 @@ func (q *QMP) ExecuteNetdevAdd(ctx context.Context, netdevType, netdevID, ifname return q.executeCommand(ctx, "netdev_add", args, nil) } +// ExecuteNetdevChardevAdd adds a Net device to a QEMU instance +// using the netdev_add command. netdevID is the id of the device to add. +// Must be valid QMP identifier. +func (q *QMP) ExecuteNetdevChardevAdd(ctx context.Context, netdevType, netdevID, chardev string, queues int) error { + args := map[string]interface{}{ + "type": netdevType, + "id": netdevID, + "chardev": chardev, + } + if queues > 1 { + args["queues"] = queues + } + + return q.executeCommand(ctx, "netdev_add", args, nil) +} + // ExecuteNetdevAddByFds adds a Net device to a QEMU instance // using the netdev_add command by fds and vhostfds. netdevID is the id of the device to add. // Must be valid QMP identifier. diff --git a/qemu/qmp_test.go b/qemu/qmp_test.go index d5718c11a4..47ab3fd924 100644 --- a/qemu/qmp_test.go +++ b/qemu/qmp_test.go @@ -425,6 +425,28 @@ func TestQMPNetdevAdd(t *testing.T) { <-disconnectedCh } +// Checks that the netdev_add command is correctly sent. +// +// We start a QMPLoop, send the netdev_add command and stop the loop. +// +// The netdev_add command should be correctly sent and the QMP loop should +// exit gracefully. +func TestQMPNetdevChardevAdd(t *testing.T) { + connectedCh := make(chan *QMPVersion) + disconnectedCh := make(chan struct{}) + buf := newQMPTestCommandBuffer(t) + buf.AddCommand("netdev_add", nil, "return", nil) + cfg := QMPConfig{Logger: qmpTestLogger{}} + q := startQMPLoop(buf, cfg, connectedCh, disconnectedCh) + q.version = checkVersion(t, connectedCh) + err := q.ExecuteNetdevChardevAdd(context.Background(), "tap", "br0", "chr0", 8) + if err != nil { + t.Fatalf("Unexpected error %v", err) + } + q.Shutdown() + <-disconnectedCh +} + // Checks that the netdev_add command with fds is correctly sent. // // We start a QMPLoop, send the netdev_add command with fds and stop the loop.