diff --git a/api/machine.go b/api/machine.go index 28ed6ec..e4a7fae 100644 --- a/api/machine.go +++ b/api/machine.go @@ -23,7 +23,8 @@ type MachineSpec struct { Cpu int64 `json:"cpuMillis"` MemoryBytes int64 `json:"memoryBytes"` - Ignition []byte `json:"ignition"` + Ignition []byte `json:"ignition"` + GuestConfig *MachineGuestConfig `json:"guestConfig,omitempty"` Volumes []*VolumeSpec `json:"volumes"` NetworkInterfaces []*NetworkInterfaceSpec `json:"networkInterfaces"` @@ -31,6 +32,10 @@ type MachineSpec struct { ShutdownAt time.Time `json:"shutdownAt,omitempty"` } +type MachineGuestConfig struct { + HostName string `json:"hostname,omitempty"` +} + type MachineStatus struct { VolumeStatus []VolumeStatus `json:"volumeStatus"` NetworkInterfaceStatus []NetworkInterfaceStatus `json:"networkInterfaceStatus"` @@ -109,11 +114,14 @@ type NetworkInterfaceSpec struct { } type NetworkInterfaceStatus struct { - Name string `json:"name"` - Handle string `json:"handle"` - State NetworkInterfaceState `json:"state"` - Type NetworkInterfaceType `json:"type,omitempty"` - Path string `json:"path,omitempty"` + Name string `json:"name"` + Handle string `json:"handle"` + State NetworkInterfaceState `json:"state"` + Type NetworkInterfaceType `json:"type,omitempty"` + Path string `json:"path,omitempty"` + QueuePairs int `json:"queuePairs,omitempty"` + Prefixes []string `json:"prefixes,omitempty"` + Ips []string `json:"ips,omitempty"` } type NetworkInterfaceState string diff --git a/cmd/cloud-hypervisor-provider/app/app.go b/cmd/cloud-hypervisor-provider/app/app.go index f404a5c..544003b 100644 --- a/cmd/cloud-hypervisor-provider/app/app.go +++ b/cmd/cloud-hypervisor-provider/app/app.go @@ -91,7 +91,7 @@ func (o *Options) AddFlags(fs *pflag.FlagSet) { fs.StringVar( &o.CloudHypervisorFirmwarePath, "cloud-hypervisor-firmware-path", - "/usr/local/bin/hypervisor-fw", + "/usr/local/bin/CLOUDHV_EFI.fd", "Path to the cloud-hypervisor firmware.", ) diff --git a/dev-vm/cloud-hypervisor-prepare.sh b/dev-vm/cloud-hypervisor-prepare.sh index 7b3c9c3..f6ba476 100644 --- a/dev-vm/cloud-hypervisor-prepare.sh +++ b/dev-vm/cloud-hypervisor-prepare.sh @@ -5,21 +5,23 @@ export DEBIAN_FRONTEND=noninteractive CH_RELEASE_REPO="${CH_RELEASE_REPO:-ironcore-dev/cloud-hypervisor-provider}" CH_RELEASE_TAG="${CH_RELEASE_TAG:-cloud-hypervisor-fw_cfg}" +EDK2_FW_TAG="${EDK2_FW_TAG:-ch-97eeb7b09}" + case "$(uname -m)" in - x86_64) SUFFIX="" ;; - aarch64) SUFFIX="-aarch64" ;; + x86_64) SUFFIX=""; FW_ASSET="CLOUDHV.fd" ;; + aarch64) SUFFIX="-aarch64"; FW_ASSET="CLOUDHV_EFI.fd" ;; *) echo "unsupported arch $(uname -m)" >&2; exit 1 ;; esac -BASE_URL="https://github.com/${CH_RELEASE_REPO}/releases/download/${CH_RELEASE_TAG}" - -curl -fL "${BASE_URL}/cloud-hypervisor-static${SUFFIX}" -o cloud-hypervisor +CH_BASE_URL="https://github.com/${CH_RELEASE_REPO}/releases/download/${CH_RELEASE_TAG}" +FW_BASE_URL="https://github.com/cloud-hypervisor/edk2/releases/download/${EDK2_FW_TAG}" +curl -fL "${CH_BASE_URL}/cloud-hypervisor-static${SUFFIX}" -o cloud-hypervisor sudo install -m 0755 cloud-hypervisor /usr/local/bin/cloud-hypervisor rm -f cloud-hypervisor -sudo curl -fL "https://github.com/cloud-hypervisor/rust-hypervisor-firmware/releases/download/0.5.0/hypervisor-fw${SUFFIX}" -o "/usr/local/bin/hypervisor-fw" -sudo chmod 0755 /usr/local/bin/hypervisor-fw +sudo curl -fL "${FW_BASE_URL}/${FW_ASSET}" -o /usr/local/bin/CLOUDHV_EFI.fd +sudo chmod 0644 /usr/local/bin/CLOUDHV_EFI.fd /usr/local/bin/cloud-hypervisor --version /usr/local/bin/cloud-hypervisor --help | grep -q fw-cfg-config \ diff --git a/internal/controllers/machine_controller.go b/internal/controllers/machine_controller.go index 65f4110..5d57ac1 100644 --- a/internal/controllers/machine_controller.go +++ b/internal/controllers/machine_controller.go @@ -337,7 +337,7 @@ func (r *MachineReconciler) reconcileNics(ctx context.Context, log logr.Logger, log.V(2).Info("NIC attached but deletion timestamp set", "name", nic.Name) } - appliedNIC, err := plugin.Apply(ctx, nic, machine.ID) + appliedNIC, err := plugin.Apply(ctx, nic, machine) if err != nil { return fmt.Errorf("failed to apply NIC: %w", err) } diff --git a/internal/plugins/networkinterface/apinet/apinet.go b/internal/plugins/networkinterface/apinet/apinet.go index 5745cc4..6d53985 100644 --- a/internal/plugins/networkinterface/apinet/apinet.go +++ b/internal/plugins/networkinterface/apinet/apinet.go @@ -107,14 +107,15 @@ func (p *Plugin) APInetNicName(machineID string, networkInterfaceName string) st func (p *Plugin) Apply( ctx context.Context, spec *api.NetworkInterfaceSpec, - machineID string, + machine *api.Machine, ) (*api.NetworkInterfaceStatus, error) { log := ctrl.LoggerFrom(ctx).WithValues("nicName", spec.Name) + machineID := machine.ID log.V(1).Info("Reconciling nic") log.V(2).Info("Writing network interface dir") - if err := os.MkdirAll(p.host.MachineNetworkInterfaceDir(machineID, spec.Name), os.ModePerm); err != nil { + if err := os.MkdirAll(p.host.MachineNetworkInterfaceDir(machine.ID, spec.Name), os.ModePerm); err != nil { return nil, err } @@ -162,7 +163,7 @@ func (p *Plugin) Apply( return nil, fmt.Errorf("error getting device info: %w", err) } - return &api.NetworkInterfaceStatus{ + status := &api.NetworkInterfaceStatus{ Name: spec.Name, Handle: provider.GetNetworkInterfaceID( apinetNic.Namespace, @@ -173,7 +174,11 @@ func (p *Plugin) Apply( State: api.NetworkInterfaceStatePrepared, Type: deviceType, Path: path, - }, nil + } + if deviceType == api.NetworkInterfaceTAPType { + status.QueuePairs = networkinterface.QueuePairsForVCPUs(int(machine.Spec.Cpu)) + } + return status, nil } log.V(2).Info("Waiting for apinet network interface to become ready") @@ -203,7 +208,7 @@ func (p *Plugin) Apply( return nil, fmt.Errorf("error getting device info: %w", err) } - return &api.NetworkInterfaceStatus{ + status := &api.NetworkInterfaceStatus{ Name: spec.Name, Handle: provider.GetNetworkInterfaceID( apinetNic.Namespace, @@ -214,7 +219,11 @@ func (p *Plugin) Apply( State: api.NetworkInterfaceStatePrepared, Type: deviceType, Path: path, - }, nil + } + if deviceType == api.NetworkInterfaceTAPType { + status.QueuePairs = networkinterface.QueuePairsForVCPUs(int(machine.Spec.Cpu)) + } + return status, nil } return &api.NetworkInterfaceStatus{ diff --git a/internal/plugins/networkinterface/ini/ini.go b/internal/plugins/networkinterface/ini/ini.go new file mode 100644 index 0000000..be2c9e1 --- /dev/null +++ b/internal/plugins/networkinterface/ini/ini.go @@ -0,0 +1,245 @@ +// SPDX-FileCopyrightText: 2026 SAP SE or an SAP affiliate company and IronCore contributors +// SPDX-License-Identifier: Apache-2.0 + +package ini + +import ( + "bytes" + "context" + "crypto/sha256" + "encoding/base32" + "encoding/json" + "fmt" + "net/netip" + "os" + "os/exec" + "path/filepath" + "regexp" + "strings" + "time" + + "github.com/ironcore-dev/cloud-hypervisor-provider/api" + "github.com/ironcore-dev/cloud-hypervisor-provider/internal/host" + "github.com/ironcore-dev/cloud-hypervisor-provider/internal/plugins/networkinterface" + machinepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/machinepoollet/api/v1alpha1" + ctrl "sigs.k8s.io/controller-runtime" +) + +const ( + PluginName = "ini" + execTimeout = 30 * time.Second + waitDelay = time.Second + maxOutput = 1 << 20 + envCommand = "INI_COMMAND" + commandAdd = "ADD" + commandDel = "DEL" +) + +type request struct { + Name string `json:"name"` + Hostname string `json:"hostname,omitempty"` +} + +type addResult struct { + Handle string `json:"handle"` + Tap *tapDevice `json:"tap"` + VF *vfDevice `json:"vf"` + Prefixes []string `json:"prefixes"` + IPs []string `json:"ips"` +} + +type tapDevice struct { + Name string `json:"name"` +} +type vfDevice struct { + PCIAddress string `json:"pci-address"` +} +type errorResult struct { + Error string `json:"error"` +} + +type Plugin struct { + pluginPath string + host host.Paths +} + +func NewPlugin(pluginPath string) (networkinterface.Plugin, error) { + if pluginPath == "" { + return nil, fmt.Errorf("no ini plugin path provided") + } + return &Plugin{pluginPath: pluginPath}, nil +} + +func (p *Plugin) Name() string { return PluginName } + +func (p *Plugin) Init(host host.Paths) error { + p.host = host + return nil +} + +func AttachmentName(machineID, networkInterfaceName string) string { + // inisix uses the attachment name as the basis for a Linux interface name. + // Keep the name within IFNAMSIZ and avoid passing UUIDs or arbitrary user + // input through a plugin command line or into a device name. + hash := sha256.Sum256([]byte(machineID + "\x00" + networkInterfaceName)) + encoded := base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567").WithPadding(base32.NoPadding) + return "ini-" + encoded.EncodeToString(hash[:])[:11] +} + +func (p *Plugin) Apply(ctx context.Context, spec *api.NetworkInterfaceSpec, machine *api.Machine) (*api.NetworkInterfaceStatus, error) { + log := ctrl.LoggerFrom(ctx) + if machine == nil { + return nil, fmt.Errorf("machine must not be nil") + } + + if err := os.MkdirAll(p.host.MachineNetworkInterfaceDir(machine.ID, spec.Name), os.ModePerm); err != nil { + return nil, err + } + + name := AttachmentName(machine.ID, spec.Name) + result := &addResult{} + if err := p.run(ctx, commandAdd, request{Name: name, Hostname: hostname(machine)}, result); err != nil { + return nil, err + } + if result.Handle == "" { + return nil, fmt.Errorf("ini plugin ADD for attachment %q returned an empty handle", name) + } + + prefixes := make([]string, 0, len(result.Prefixes)) + for _, value := range result.Prefixes { + prefix, err := netip.ParsePrefix(value) + if err != nil { + return nil, fmt.Errorf("ini plugin ADD for attachment %q returned invalid prefix %q: %w", name, value, err) + } + prefixes = append(prefixes, prefix.String()) + } + if len(prefixes) == 0 { + return nil, fmt.Errorf("ini plugin ADD for attachment %q returned no prefixes", name) + } + + ips := make([]string, 0, len(result.IPs)) + for _, value := range result.IPs { + ip, err := netip.ParseAddr(value) + if err != nil { + return nil, fmt.Errorf("ini plugin ADD for attachment %q returned invalid ip %q: %w", name, value, err) + } + ips = append(ips, ip.String()) + } + + status := &api.NetworkInterfaceStatus{ + Name: spec.Name, + Handle: result.Handle, + State: api.NetworkInterfaceStatePrepared, + Prefixes: prefixes, + Ips: ips, + } + switch { + case result.Tap != nil && result.VF != nil: + return nil, fmt.Errorf("ini plugin ADD for attachment %q returned more than one device member", name) + case result.Tap != nil: + if result.Tap.Name == "" { + return nil, fmt.Errorf("ini plugin ADD for attachment %q returned a tap device without a name", name) + } + status.Type = api.NetworkInterfaceTAPType + status.Path = result.Tap.Name + status.QueuePairs = networkinterface.QueuePairsForVCPUs(int(machine.Spec.Cpu)) + log.V(1).Info("Applied INI attachment", "attachment", name, "tapName", result.Tap.Name) + case result.VF != nil: + if !pciAddressRE.MatchString(result.VF.PCIAddress) { + return nil, fmt.Errorf("ini plugin ADD for attachment %q returned invalid PCI address %q", name, result.VF.PCIAddress) + } + status.Type = api.NetworkInterfacePCIType + status.Path = filepath.Join("/sys/bus/pci/devices", strings.ToLower(result.VF.PCIAddress)) + default: + return nil, fmt.Errorf("ini plugin ADD for attachment %q returned no supported device member", name) + } + return status, nil +} + +func (p *Plugin) Delete(ctx context.Context, computeNicName, machineID string) error { + if err := p.run(ctx, commandDel, request{Name: AttachmentName(machineID, computeNicName)}, nil); err != nil { + return err + } + return os.RemoveAll(p.host.MachineNetworkInterfaceDir(machineID, computeNicName)) +} + +func hostname(machine *api.Machine) string { + if machine.Spec.GuestConfig != nil && machine.Spec.GuestConfig.HostName != "" { + return machine.Spec.GuestConfig.HostName + } + return sanitizeHostname(machine.Labels[machinepoolletv1alpha1.MachineNameLabel]) +} + +var invalidHostnameChars = regexp.MustCompile(`[^a-z0-9]+`) + +func sanitizeHostname(value string) string { + value = invalidHostnameChars.ReplaceAllString(strings.ToLower(value), "-") + value = strings.Trim(value, "-") + if len(value) > 63 { + value = strings.TrimRight(value[:63], "-") + } + return value +} + +var pciAddressRE = regexp.MustCompile(`^[0-9a-fA-F]{4}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}\.[0-9a-fA-F]$`) + +func (p *Plugin) run(ctx context.Context, command string, req request, out any) error { + input, err := json.Marshal(req) + if err != nil { + return fmt.Errorf("error encoding ini plugin request: %w", err) + } + + ctx, cancel := context.WithTimeout(ctx, execTimeout) + defer cancel() + + var stdout, stderr limitedBuffer + cmd := exec.CommandContext(ctx, p.pluginPath) + cmd.WaitDelay = waitDelay + cmd.Env = append(os.Environ(), envCommand+"="+command) + cmd.Stdin = bytes.NewReader(input) + cmd.Stdout = &stdout + cmd.Stderr = &stderr + + if err := cmd.Run(); err != nil { + if stdout.exceeded || stderr.exceeded { + return fmt.Errorf("ini plugin %s for attachment %q produced too much output", command, req.Name) + } + decoded := &errorResult{} + if jsonErr := json.Unmarshal(bytes.TrimSpace(stdout.Bytes()), decoded); jsonErr == nil && decoded.Error != "" { + return fmt.Errorf("ini plugin %s for attachment %q failed: %s", command, req.Name, decoded.Error) + } + if message := strings.TrimSpace(stderr.String()); message != "" { + return fmt.Errorf("ini plugin %s for attachment %q failed: %w (stderr: %s)", command, req.Name, err, message) + } + return fmt.Errorf("ini plugin %s for attachment %q failed: %w", command, req.Name, err) + } + if stdout.exceeded || stderr.exceeded { + return fmt.Errorf("ini plugin %s for attachment %q produced too much output", command, req.Name) + } + if out == nil { + return nil + } + if err := json.Unmarshal(bytes.TrimSpace(stdout.Bytes()), out); err != nil { + return fmt.Errorf("error decoding ini plugin %s result for attachment %q (stdout: %q): %w", command, req.Name, strings.TrimSpace(stdout.String()), err) + } + return nil +} + +type limitedBuffer struct { + bytes.Buffer + exceeded bool +} + +func (b *limitedBuffer) Write(data []byte) (int, error) { + remaining := maxOutput - b.Len() + if remaining <= 0 { + b.exceeded = true + return len(data), nil + } + if len(data) > remaining { + _, _ = b.Buffer.Write(data[:remaining]) + b.exceeded = true + return len(data), nil + } + return b.Buffer.Write(data) +} diff --git a/internal/plugins/networkinterface/ini/ini_test.go b/internal/plugins/networkinterface/ini/ini_test.go new file mode 100644 index 0000000..1997d63 --- /dev/null +++ b/internal/plugins/networkinterface/ini/ini_test.go @@ -0,0 +1,115 @@ +// SPDX-FileCopyrightText: 2026 SAP SE or an SAP affiliate company and IronCore contributors +// SPDX-License-Identifier: Apache-2.0 + +package ini_test + +import ( + "fmt" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/ironcore-dev/cloud-hypervisor-provider/api" + "github.com/ironcore-dev/cloud-hypervisor-provider/internal/host" + "github.com/ironcore-dev/cloud-hypervisor-provider/internal/plugins/networkinterface/ini" + apiutils "github.com/ironcore-dev/provider-utils/apiutils/api" +) + +func TestApply(t *testing.T) { + root := t.TempDir() + hostPaths, err := host.PathsAt(root) + if err != nil { + t.Fatal(err) + } + pluginPath, capture := fakePlugin(t, `printf '{"handle":"inisix://host/tap/isx-test","tap":{"name":"isx-test"},"prefixes":["2001:db8:1::/64"],"ips":["2001:db8:1::1"]}'`) + plugin, err := ini.NewPlugin(pluginPath) + if err != nil { + t.Fatal(err) + } + if err := plugin.Init(hostPaths); err != nil { + t.Fatal(err) + } + + machine := &api.Machine{ + Metadata: apiutils.Metadata{ + ID: "machine-id", + Labels: map[string]string{"machinepoollet.ironcore.dev/machine-name": "My.Machine"}, + }, + Spec: api.MachineSpec{GuestConfig: &api.MachineGuestConfig{HostName: "guest.example"}}, + } + status, err := plugin.Apply(t.Context(), &api.NetworkInterfaceSpec{Name: "nic0"}, machine) + if err != nil { + t.Fatal(err) + } + want := &api.NetworkInterfaceStatus{ + Name: "nic0", Handle: "inisix://host/tap/isx-test", State: api.NetworkInterfaceStatePrepared, + Type: api.NetworkInterfaceTAPType, Path: "isx-test", QueuePairs: 1, + Prefixes: []string{"2001:db8:1::/64"}, Ips: []string{"2001:db8:1::1"}, + } + if fmt.Sprintf("%#v", status) != fmt.Sprintf("%#v", want) { + t.Errorf("unexpected status: got %#v, want %#v", status, want) + } + if got := strings.TrimSpace(readFile(t, filepath.Join(capture, "stdin"))); got != fmt.Sprintf(`{"name":%q,"hostname":"guest.example"}`, ini.AttachmentName(machine.ID, "nic0")) { + t.Errorf("unexpected request: %s", got) + } +} + +func TestDeleteRemovesDirectory(t *testing.T) { + root := t.TempDir() + hostPaths, err := host.PathsAt(root) + if err != nil { + t.Fatal(err) + } + pluginPath, capture := fakePlugin(t, `cat >/dev/null`) + plugin, err := ini.NewPlugin(pluginPath) + if err != nil { + t.Fatal(err) + } + if err := plugin.Init(hostPaths); err != nil { + t.Fatal(err) + } + dir := hostPaths.MachineNetworkInterfaceDir("machine-id", "nic0") + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatal(err) + } + if err := plugin.Delete(t.Context(), "nic0", "machine-id"); err != nil { + t.Fatal(err) + } + if _, err := os.Stat(dir); !os.IsNotExist(err) { + t.Errorf("network interface directory still exists: %v", err) + } + if got := strings.TrimSpace(readFile(t, filepath.Join(capture, "command"))); got != "DEL" { + t.Errorf("unexpected command: %q", got) + } +} + +func TestAttachmentNameIsSafeForInisix(t *testing.T) { + name := ini.AttachmentName("a3f9b2c1-4d5e-6f70-8192-a3b4c5d6e7f8", "network interface/0") + if len(name) != 15 { + t.Fatalf("attachment name length = %d, want 15: %q", len(name), name) + } + if !strings.HasPrefix(name, "ini-") { + t.Fatalf("attachment name has unexpected prefix: %q", name) + } +} + +func fakePlugin(t *testing.T, body string) (string, string) { + t.Helper() + capture := t.TempDir() + plugin := filepath.Join(capture, "plugin.sh") + script := fmt.Sprintf("#!/bin/sh\nprintf '%%s' \"$INI_COMMAND\" > %q\ncat > %q\n%s\n", filepath.Join(capture, "command"), filepath.Join(capture, "stdin"), body) + if err := os.WriteFile(plugin, []byte(script), 0o755); err != nil { + t.Fatal(err) + } + return plugin, capture +} + +func readFile(t *testing.T, path string) string { + t.Helper() + data, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + return string(data) +} diff --git a/internal/plugins/networkinterface/isolated/isolated.go b/internal/plugins/networkinterface/isolated/isolated.go index b5ec2de..89e5d68 100644 --- a/internal/plugins/networkinterface/isolated/isolated.go +++ b/internal/plugins/networkinterface/isolated/isolated.go @@ -32,12 +32,12 @@ func (p *plugin) Init(host host.Paths) error { func (p *plugin) Apply(ctx context.Context, spec *api.NetworkInterfaceSpec, - machineID string, + machine *api.Machine, ) (*api.NetworkInterfaceStatus, error) { log := ctrl.LoggerFrom(ctx) log.V(1).Info("Writing network interface dir") - if err := os.MkdirAll(p.host.MachineNetworkInterfaceDir(machineID, spec.Name), os.ModePerm); err != nil { + if err := os.MkdirAll(p.host.MachineNetworkInterfaceDir(machine.ID, spec.Name), os.ModePerm); err != nil { return nil, err } @@ -46,7 +46,7 @@ func (p *plugin) Apply(ctx context.Context, }, nil } -func (p *plugin) Delete(ctx context.Context, computeNicName string, machineID string) error { +func (p *plugin) Delete(_ context.Context, computeNicName string, machineID string) error { return os.RemoveAll(p.host.MachineNetworkInterfaceDir(machineID, computeNicName)) } diff --git a/internal/plugins/networkinterface/options/ini.go b/internal/plugins/networkinterface/options/ini.go new file mode 100644 index 0000000..38a3bfa --- /dev/null +++ b/internal/plugins/networkinterface/options/ini.go @@ -0,0 +1,31 @@ +// SPDX-FileCopyrightText: 2026 SAP SE or an SAP affiliate company and IronCore contributors +// SPDX-License-Identifier: Apache-2.0 + +package options + +import ( + "github.com/ironcore-dev/cloud-hypervisor-provider/internal/plugins/networkinterface" + "github.com/ironcore-dev/cloud-hypervisor-provider/internal/plugins/networkinterface/ini" + "github.com/spf13/pflag" + utilruntime "k8s.io/apimachinery/pkg/util/runtime" +) + +type iniOptions struct{ PluginPath string } + +func (o *iniOptions) PluginName() string { return ini.PluginName } + +func (o *iniOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.PluginPath, "ini-plugin-path", "", "Path to the INI network interface plugin executable") +} + +func (o *iniOptions) NetworkInterfacePlugin() (networkinterface.Plugin, func(), error) { + plugin, err := ini.NewPlugin(o.PluginPath) + if err != nil { + return nil, nil, err + } + return plugin, nil, nil +} + +func init() { + utilruntime.Must(DefaultPluginTypeRegistry.Register(&iniOptions{}, 3)) +} diff --git a/internal/plugins/networkinterface/plugins.go b/internal/plugins/networkinterface/plugins.go index cb5eac5..44afc6a 100644 --- a/internal/plugins/networkinterface/plugins.go +++ b/internal/plugins/networkinterface/plugins.go @@ -14,6 +14,15 @@ type Plugin interface { Name() string Init(host host.Paths) error - Apply(ctx context.Context, spec *api.NetworkInterfaceSpec, machineID string) (*api.NetworkInterfaceStatus, error) + Apply(ctx context.Context, spec *api.NetworkInterfaceSpec, machine *api.Machine) (*api.NetworkInterfaceStatus, error) Delete(ctx context.Context, computeNicName string, machineID string) error } + +// QueuePairsForVCPUs returns the deterministic network queue policy used by +// the provider. Cloud Hypervisor exposes two virtio queues per RX/TX pair. +func QueuePairsForVCPUs(vcpus int) int { + if vcpus <= 1 { + return 1 + } + return 2 +} diff --git a/internal/server/machine.go b/internal/server/machine.go index 3c11b87..f1b91d8 100644 --- a/internal/server/machine.go +++ b/internal/server/machine.go @@ -52,6 +52,9 @@ func (s *Server) getIRIMachineSpec(machine *api.Machine) (*iri.MachineSpec, erro Volumes: s.getIRIVolumeSpec(machine), NetworkInterfaces: s.getIRINICSpec(machine), } + if machine.Spec.GuestConfig != nil { + spec.GuestConfig = &iri.GuestConfig{Hostname: machine.Spec.GuestConfig.HostName} + } return spec, nil } diff --git a/internal/server/machine_create.go b/internal/server/machine_create.go index c0516e2..949ee15 100644 --- a/internal/server/machine_create.go +++ b/internal/server/machine_create.go @@ -74,6 +74,11 @@ func (s *Server) createMachineFromIRIMachine( NetworkInterfaces: networkInterfaces, }, } + if iriMachine.Spec.GuestConfig != nil { + machine.Spec.GuestConfig = &api.MachineGuestConfig{ + HostName: iriMachine.Spec.GuestConfig.Hostname, + } + } if err := api.SetObjectMetadata(machine, iriMachine.Metadata); err != nil { return nil, fmt.Errorf("failed to set metadata: %w", err) diff --git a/internal/vmm/manager.go b/internal/vmm/manager.go index b2df7cd..d6a7429 100644 --- a/internal/vmm/manager.go +++ b/internal/vmm/manager.go @@ -270,6 +270,11 @@ func (m *Manager) Status(ctx context.Context, machineID string) (*VMStatus, erro status.NetworkInterfaces = append(status.NetworkInterfaces, name) } } + for _, net := range ptr.Deref(info.Config.Net, nil) { + if name := getNicName(ptr.Deref(net.Id, "")); name != "" { + status.NetworkInterfaces = append(status.NetworkInterfaces, name) + } + } return status, nil } @@ -395,15 +400,28 @@ func (m *Manager) Create(ctx context.Context, machine *api.Machine) error { } var dev []client.DeviceConfig + var nets []client.NetConfig for _, nic := range machine.Status.NetworkInterfaceStatus { if nic.State != api.NetworkInterfaceStatePrepared { return fmt.Errorf("nic %s is not attached", nic.Name) } - dev = append(dev, client.DeviceConfig{ - Id: new(getNicID(nic.Name)), - Path: new(nic.Path), - }) + switch nic.Type { + case api.NetworkInterfaceTAPType: + net := client.NetConfig{ + Id: new(getNicID(nic.Name)), + Tap: new(nic.Path), + NumQueues: ptr.To(nic.QueuePairs * 2), + } + nets = append(nets, net) + case api.NetworkInterfacePCIType: + dev = append(dev, client.DeviceConfig{ + Id: new(getNicID(nic.Name)), + Path: new(nic.Path), + }) + default: + return fmt.Errorf("nic %s has unknown type %q", nic.Name, nic.Type) + } } log.V(2).Info("Creating vm") @@ -418,6 +436,7 @@ func (m *Manager) Create(ctx context.Context, machine *api.Machine) error { Size: machine.Spec.MemoryBytes, Shared: new(true), }, + Net: &nets, Console: &client.ConsoleConfig{ Mode: "Off", }, @@ -485,16 +504,37 @@ func (m *Manager) AttachNetworkInterface(ctx context.Context, instanceID string, return ErrNotFound } - resp, err := apiClient.PutVmAddDeviceWithResponse(ctx, client.DeviceConfig{ - Id: new(getNicID(nic.Name)), - Path: new(nic.Path), - }) - if err != nil { - return wrapIfSocketClosed(fmt.Errorf("failed to add device: %w", err)) + var statusCode int + var responseBody []byte + switch nic.Type { + case api.NetworkInterfaceTAPType: + net := client.NetConfig{ + Id: new(getNicID(nic.Name)), + Tap: new(nic.Path), + NumQueues: ptr.To(nic.QueuePairs * 2), + } + resp, err := apiClient.PutVmAddNetWithResponse(ctx, net) + if err != nil { + return wrapIfSocketClosed(fmt.Errorf("failed to add network interface: %w", err)) + } + statusCode = resp.StatusCode() + responseBody = resp.Body + case api.NetworkInterfacePCIType: + resp, err := apiClient.PutVmAddDeviceWithResponse(ctx, client.DeviceConfig{ + Id: new(getNicID(nic.Name)), + Path: new(nic.Path), + }) + if err != nil { + return wrapIfSocketClosed(fmt.Errorf("failed to add network interface: %w", err)) + } + statusCode = resp.StatusCode() + responseBody = resp.Body + default: + return fmt.Errorf("nic %s has unknown type %q", nic.Name, nic.Type) } - if err := validateStatus(resp.StatusCode()); err != nil { - log.V(1).Info("Failed to add nic", "error", string(resp.Body)) + if err := validateStatus(statusCode); err != nil { + log.V(1).Info("Failed to add nic", "error", string(responseBody)) return err } log.V(1).Info("Added device", "name", nic.Name)