From d1a686770b931b0e8726a4b2fd368294f0d1938b Mon Sep 17 00:00:00 2001 From: Cezar Craciunoiu Date: Tue, 19 Nov 2024 13:25:14 +0200 Subject: [PATCH] fix(qemu): Correctly forward multiple ports GitHub-Fixes: #1959 Signed-off-by: Cezar Craciunoiu --- machine/qemu/qemu_netdev.go | 40 +++++++++++++++++++------------------ machine/qemu/v1alpha1.go | 39 ++++++++++++++++++++---------------- 2 files changed, 43 insertions(+), 36 deletions(-) diff --git a/machine/qemu/qemu_netdev.go b/machine/qemu/qemu_netdev.go index 371018a03..b4dae9dc0 100644 --- a/machine/qemu/qemu_netdev.go +++ b/machine/qemu/qemu_netdev.go @@ -362,23 +362,23 @@ func (nd QemuNetDevTap) String() string { // Configure a user mode network backend. type QemuNetDevUser struct { // ID of the network device. - Id string `json:"id,omitempty"` - Ipv4 bool `json:"ipv4,omitempty"` - Net string `json:"net,omitempty"` - Host string `json:"host,omitempty"` - Ipv6 bool `json:"ipv6,omitempty"` - Ipv6Net string `json:"ipv6-net,omitempty"` - Ipv6Host string `json:"ipv6-host,omitempty"` - Restrict bool `json:"restrict,omitempty"` - Hostname string `json:"hostname,omitempty"` - Domainname string `json:"domainname,omitempty"` - Tftp string `json:"tftp,omitempty"` - TftpServerName string `json:"tftp_server_name,omitempty"` - Bootfile string `json:"bootfile,omitempty"` - Hostfwd string `json:"hostfwd,omitempty"` - Guestfwd string `json:"guestfwd,omitempty"` - Smb string `json:"smb,omitempty"` - Smbserver string `json:"smbserver,omitempty"` + Id string `json:"id,omitempty"` + Ipv4 bool `json:"ipv4,omitempty"` + Net string `json:"net,omitempty"` + Host string `json:"host,omitempty"` + Ipv6 bool `json:"ipv6,omitempty"` + Ipv6Net string `json:"ipv6-net,omitempty"` + Ipv6Host string `json:"ipv6-host,omitempty"` + Restrict bool `json:"restrict,omitempty"` + Hostname string `json:"hostname,omitempty"` + Domainname string `json:"domainname,omitempty"` + Tftp string `json:"tftp,omitempty"` + TftpServerName string `json:"tftp_server_name,omitempty"` + Bootfile string `json:"bootfile,omitempty"` + Hostfwd []string `json:"hostfwd,omitempty"` + Guestfwd string `json:"guestfwd,omitempty"` + Smb string `json:"smb,omitempty"` + Smbserver string `json:"smbserver,omitempty"` } // String returns a QEMU command-line compatible netdev string with the format: @@ -440,8 +440,10 @@ func (nd QemuNetDevUser) String() string { ret.WriteString(nd.Bootfile) } if len(nd.Hostfwd) > 0 { - ret.WriteString(",hostfwd=") - ret.WriteString(nd.Hostfwd) + for _, rule := range nd.Hostfwd { + ret.WriteString(",hostfwd=") + ret.WriteString(rule) + } } if len(nd.Guestfwd) > 0 { ret.WriteString(",guestfwd=") diff --git a/machine/qemu/v1alpha1.go b/machine/qemu/v1alpha1.go index c0cfe206c..bdb570b09 100644 --- a/machine/qemu/v1alpha1.go +++ b/machine/qemu/v1alpha1.go @@ -302,26 +302,31 @@ func (service *machineV1alpha1Service) Create(ctx context.Context, machine *mach } if len(machine.Spec.Ports) > 0 { + mac := "" + hostfwds := make([]string, 0, len(machine.Spec.Ports)) for _, port := range machine.Spec.Ports { - mac := port.MacAddress - if mac == "" { - startMac = macaddr.IncrementMacAddress(startMac) - mac = startMac.String() + if port.MacAddress != "" { + mac = port.MacAddress } - - hostnetid := fmt.Sprintf("hostnet%d", hostnetCounter) - hostnetCounter++ - qopts = append(qopts, - WithDevice(QemuDeviceVirtioNetPci{ - Mac: mac, - Netdev: hostnetid, - }), - WithNetDevice(QemuNetDevUser{ - Id: hostnetid, - Hostfwd: fmt.Sprintf("%s::%d-:%d", port.Protocol, port.HostPort, port.MachinePort), - }), - ) + hostfwds = append(hostfwds, fmt.Sprintf("%s::%d-:%d", port.Protocol, port.HostPort, port.MachinePort)) + } + if mac == "" { + startMac = macaddr.IncrementMacAddress(startMac) + mac = startMac.String() } + + hostnetid := fmt.Sprintf("hostnet%d", hostnetCounter) + + qopts = append(qopts, + WithDevice(QemuDeviceVirtioNetPci{ + Mac: mac, + Netdev: hostnetid, + }), + WithNetDevice(QemuNetDevUser{ + Id: hostnetid, + Hostfwd: hostfwds, + }), + ) } var fstab []string