| 1 | package guestos |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "strings" |
| 7 | |
| 8 | "github.com/google/uuid" |
| 9 | ) |
| 10 | |
| 11 | const windowsHostManagerVersion = 1 |
| 12 | const windowsHostManagerWireLimit = 64 << 10 |
| 13 | |
| 14 | type windowsHostManagerRequest struct { |
| 15 | Version int `json:"version"` |
| 16 | VMID uuid.UUID `json:"vm_id"` |
| 17 | Operation string `json:"operation"` |
| 18 | MemoryGiB int `json:"memory_gib,omitempty"` |
| 19 | CPUs int `json:"cpus,omitempty"` |
| 20 | Storage *windowsHostStorageRequest `json:"storage,omitempty"` |
| 21 | } |
| 22 | |
| 23 | type windowsStorageDisk struct { |
| 24 | Path string `json:"path"` |
| 25 | Volume uint32 `json:"volume"` |
| 26 | FileID uint64 `json:"file_id"` |
| 27 | } |
| 28 | |
| 29 | type windowsVMStorageLayout struct { |
| 30 | Configuration string `json:"configuration"` |
| 31 | Snapshots string `json:"snapshots"` |
| 32 | Paging string `json:"paging"` |
| 33 | } |
| 34 | |
| 35 | type windowsHostStorageRequest struct { |
| 36 | Action string `json:"action"` |
| 37 | ID string `json:"id,omitempty"` |
| 38 | SourceVolume uint32 `json:"source_volume,omitempty"` |
| 39 | SourceFileID uint64 `json:"source_file_id,omitempty"` |
| 40 | Disk string `json:"disk,omitempty"` |
| 41 | } |
| 42 | |
| 43 | type windowsHostStorageState struct { |
| 44 | Disk windowsStorageDisk `json:"disk"` |
| 45 | Layout *windowsVMStorageLayout `json:"layout,omitempty"` |
| 46 | Pending string `json:"pending,omitempty"` |
| 47 | Outcome string `json:"outcome,omitempty"` |
| 48 | } |
| 49 | |
| 50 | func validWindowsStorageID(id string) bool { |
| 51 | return len(id) == 32 && strings.Trim(id, "0123456789abcdef") == "" |
| 52 | } |
| 53 | |
| 54 | func (r windowsHostStorageRequest) validate() error { |
| 55 | if r.Action == "status" { |
| 56 | if r.ID == "" && r.SourceVolume == 0 && r.SourceFileID == 0 && r.Disk == "" { |
| 57 | return nil |
| 58 | } |
| 59 | return errors.New("invalid_request") |
| 60 | } |
| 61 | if !validWindowsStorageID(r.ID) || r.SourceVolume == 0 || r.SourceFileID == 0 { |
| 62 | return errors.New("invalid_request") |
| 63 | } |
| 64 | switch r.Action { |
| 65 | case "begin", "commit", "rollback": |
| 66 | if r.Disk != "" { |
| 67 | return errors.New("invalid_request") |
| 68 | } |
| 69 | case "attach": |
| 70 | if r.Disk == "" || len(r.Disk) > 32700 || strings.ContainsAny(r.Disk, "\x00\r\n") { |
| 71 | return errors.New("invalid_request") |
| 72 | } |
| 73 | default: |
| 74 | return errors.New("invalid_request") |
| 75 | } |
| 76 | return nil |
| 77 | } |
| 78 | |
| 79 | type windowsHostManagerResponse struct { |
| 80 | Version int `json:"version"` |
| 81 | VMID uuid.UUID `json:"vm_id"` |
| 82 | OK bool `json:"ok"` |
| 83 | Result json.RawMessage `json:"result,omitempty"` |
| 84 | Error string `json:"error,omitempty"` |
| 85 | } |
| 86 | |
| 87 | func (r windowsHostManagerRequest) validate() error { |
| 88 | if r.Version != windowsHostManagerVersion || r.VMID == uuid.Nil { |
| 89 | return errors.New("invalid_request") |
| 90 | } |
| 91 | if r.Operation == "storage" { |
| 92 | if r.Storage == nil || r.MemoryGiB != 0 || r.CPUs != 0 { |
| 93 | return errors.New("invalid_request") |
| 94 | } |
| 95 | return r.Storage.validate() |
| 96 | } |
| 97 | if r.Storage != nil { |
| 98 | return errors.New("invalid_request") |
| 99 | } |
| 100 | switch r.Operation { |
| 101 | case "inspect", "start", "shutdown": |
| 102 | if r.MemoryGiB != 0 || r.CPUs != 0 { |
| 103 | return errors.New("invalid_request") |
| 104 | } |
| 105 | case "resources": |
| 106 | if r.MemoryGiB == 0 && r.CPUs == 0 { |
| 107 | return nil |
| 108 | } |
| 109 | if r.MemoryGiB < 4 || r.MemoryGiB > 1024 || r.CPUs < 2 || r.CPUs > 1024 { |
| 110 | return errors.New("invalid_request") |
| 111 | } |
| 112 | default: |
| 113 | return errors.New("invalid_operation") |
| 114 | } |
| 115 | return nil |
| 116 | } |