diff --git a/qmp.go b/qmp.go index f6774c4e10..0e08e4d261 100644 --- a/qmp.go +++ b/qmp.go @@ -736,3 +736,18 @@ func (q *QMP) ExecutePCIVFIODeviceAdd(ctx context.Context, devID, bdf, addr, bus } return q.executeCommand(ctx, "device_add", args, nil) } + +// ExecuteCPUDeviceAdd adds a CPU to a QEMU instance using the device_add command. +// driver is the CPU model, cpuID must be a unique ID to identify the CPU, socketID is the socket number within +// node/board the CPU belongs to, coreID is the core number within socket the CPU belongs to, threadID is the +// thread number within core the CPU belongs to. +func (q *QMP) ExecuteCPUDeviceAdd(ctx context.Context, driver, cpuID, socketID, coreID, threadID string) error { + args := map[string]interface{}{ + "driver": driver, + "id": cpuID, + "socket-id": socketID, + "core-id": coreID, + "thread-id": threadID, + } + return q.executeCommand(ctx, "device_add", args, nil) +} diff --git a/qmp_test.go b/qmp_test.go index e8072431c8..7e83f4f939 100644 --- a/qmp_test.go +++ b/qmp_test.go @@ -810,3 +810,25 @@ func TestQMPPCIDeviceAdd(t *testing.T) { q.Shutdown() <-disconnectedCh } + +// Checks that CPU are correctly added using device_add +func TestQMPCPUDeviceAdd(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) + driver := "qemu64-x86_64-cpu" + cpuID := "cpu-0" + socketID := "0" + coreID := "1" + threadID := "0" + err := q.ExecuteCPUDeviceAdd(context.Background(), driver, cpuID, socketID, coreID, threadID) + if err != nil { + t.Fatalf("Unexpected error %v", err) + } + q.Shutdown() + <-disconnectedCh +}