Skip to content

Commit

Permalink
fix(qemu): Correctly forward multiple ports
Browse files Browse the repository at this point in the history
GitHub-Fixes: #1959
Signed-off-by: Cezar Craciunoiu <[email protected]>
  • Loading branch information
craciunoiuc committed Nov 19, 2024
1 parent 8497e22 commit d1a6867
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 36 deletions.
40 changes: 21 additions & 19 deletions machine/qemu/qemu_netdev.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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=")
Expand Down
39 changes: 22 additions & 17 deletions machine/qemu/v1alpha1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d1a6867

Please sign in to comment.