Skip to content
This repository was archived by the owner on Jan 20, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions qemu/qmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -963,3 +963,40 @@ func (q *QMP) ExecuteGetFD(ctx context.Context, fdname string, fd *os.File) erro
_, err := q.executeCommandWithResponse(ctx, "getfd", args, oob, nil)
return err
}

// ExecuteCharDevUnixSocketAdd adds a character device using as backend a unix socket,
// id is an identifier for the device, path specifies the local path of the unix socket,
// wait is to block waiting for a client to connect, server specifies that the socket is a listening socket.
func (q *QMP) ExecuteCharDevUnixSocketAdd(ctx context.Context, id, path string, wait, server bool) error {
args := map[string]interface{}{
"id": id,
"backend": map[string]interface{}{
"type": "socket",
"data": map[string]interface{}{
"wait": wait,
"server": server,
"addr": map[string]interface{}{
"type": "unix",
"data": map[string]interface{}{
"path": path,
},
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a lot of indentation here. I bet this is what's expected by the JSON syntax from QMP, right ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right

},
},
},
}
return q.executeCommand(ctx, "chardev-add", args, nil)
}

// ExecuteVirtSerialPortAdd adds a virtserialport.
// id is an identifier for the virtserialport, name is a name for the virtserialport and
// it will be visible in the VM, chardev is the character device id previously added.
func (q *QMP) ExecuteVirtSerialPortAdd(ctx context.Context, id, name, chardev string) error {
args := map[string]interface{}{
"driver": VirtioSerialPort,
"id": id,
"name": name,
"chardev": chardev,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other parts of the code, where a parameter is optional, we usually only store it in the map if it is defined, e.g., https://github.com/intel/govmm/pull/31/files#diff-3f8ad3b51588f02283eda2d19c5ffd06R912. Should we do this here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry nop, I'll remove the comment chardev is optional.

}

return q.executeCommand(ctx, "device_add", args, nil)
}
34 changes: 34 additions & 0 deletions qemu/qmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1040,3 +1040,37 @@ func TestExecuteGetFdD(t *testing.T) {
q.Shutdown()
<-disconnectedCh
}

// Checks chardev-add unix socket
func TestExecuteCharDevUnixSocketAdd(t *testing.T) {
connectedCh := make(chan *QMPVersion)
disconnectedCh := make(chan struct{})
buf := newQMPTestCommandBuffer(t)
buf.AddCommand("chardev-add", nil, "return", nil)
cfg := QMPConfig{Logger: qmpTestLogger{}}
q := startQMPLoop(buf, cfg, connectedCh, disconnectedCh)
checkVersion(t, connectedCh)
err := q.ExecuteCharDevUnixSocketAdd(context.Background(), "foo", "foo.sock", false, true)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
q.Shutdown()
<-disconnectedCh
}

// Checks virtio serial port hotplug
func TestExecuteVirtSerialPortAdd(t *testing.T) {
connectedCh := make(chan *QMPVersion)
disconnectedCh := make(chan struct{})
buf := newQMPTestCommandBuffer(t)
buf.AddCommand("device_add", nil, "return", nil)
cfg := QMPConfig{Logger: qmpTestLogger{}}
q := startQMPLoop(buf, cfg, connectedCh, disconnectedCh)
checkVersion(t, connectedCh)
err := q.ExecuteVirtSerialPortAdd(context.Background(), "foo", "foo.channel", "foo")
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
q.Shutdown()
<-disconnectedCh
}