Skip to content

Commit

Permalink
Merge pull request containers#18946 from Luap99/slirp4netns
Browse files Browse the repository at this point in the history
use slirp4netns code from c/common
  • Loading branch information
openshift-merge-robot authored Jun 22, 2023
2 parents 436df1b + 614c962 commit 49e0bde
Show file tree
Hide file tree
Showing 23 changed files with 913 additions and 885 deletions.
2 changes: 1 addition & 1 deletion cmd/podman/system/service_abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
"path/filepath"

"github.com/containers/common/pkg/cgroups"
"github.com/containers/common/pkg/servicereaper"
"github.com/containers/podman/v4/cmd/podman/registry"
api "github.com/containers/podman/v4/pkg/api/server"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/domain/infra"
"github.com/containers/podman/v4/pkg/rootless"
"github.com/containers/podman/v4/pkg/servicereaper"
"github.com/containers/podman/v4/utils"
"github.com/coreos/go-systemd/v22/activation"
"github.com/sirupsen/logrus"
Expand Down
2 changes: 1 addition & 1 deletion cmd/rootlessport/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

"github.com/containernetworking/plugins/pkg/ns"
"github.com/containers/common/libnetwork/types"
"github.com/containers/podman/v4/pkg/rootlessport"
"github.com/containers/common/pkg/rootlessport"
rkport "github.com/rootless-containers/rootlesskit/pkg/port"
rkbuiltin "github.com/rootless-containers/rootlesskit/pkg/port/builtin"
rkportutil "github.com/rootless-containers/rootlesskit/pkg/port/portutil"
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/containernetworking/cni v1.1.2
github.com/containernetworking/plugins v1.3.0
github.com/containers/buildah v1.30.1-0.20230504052500-e925b5852e07
github.com/containers/common v0.53.1-0.20230621115248-a2cd3ea30337
github.com/containers/common v0.53.1-0.20230621174116-586a3be4e1fc
github.com/containers/conmon v2.0.20+incompatible
github.com/containers/image/v5 v5.25.1-0.20230613183705-07ced6137083
github.com/containers/libhvee v0.0.5
Expand All @@ -30,7 +30,6 @@ require (
github.com/docker/go-connections v0.4.1-0.20210727194412-58542c764a11
github.com/docker/go-plugins-helpers v0.0.0-20211224144127-6eecb7beb651
github.com/docker/go-units v0.5.0
github.com/fsnotify/fsnotify v1.6.0
github.com/godbus/dbus/v5 v5.1.1-0.20230522191255-76236955d466
github.com/google/gofuzz v1.2.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
Expand Down Expand Up @@ -93,6 +92,7 @@ require (
github.com/disiqueira/gotree/v3 v3.0.2 // indirect
github.com/docker/docker-credential-helpers v0.7.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fsouza/go-dockerclient v1.9.7 // indirect
github.com/go-jose/go-jose/v3 v3.0.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ github.com/containernetworking/plugins v1.3.0 h1:QVNXMT6XloyMUoO2wUOqWTC1hWFV62Q
github.com/containernetworking/plugins v1.3.0/go.mod h1:Pc2wcedTQQCVuROOOaLBPPxrEXqqXBFt3cZ+/yVg6l0=
github.com/containers/buildah v1.30.1-0.20230504052500-e925b5852e07 h1:Bs2sNFh/fSYr4J6JJLFqzyn3dp6HhlA6ewFwRYUpeIE=
github.com/containers/buildah v1.30.1-0.20230504052500-e925b5852e07/go.mod h1:6A/BK0YJLXL8+AqlbceKJrhUT+NtEgsvAc51F7TAllc=
github.com/containers/common v0.53.1-0.20230621115248-a2cd3ea30337 h1:Z9wxp08tzCKgI3ziVwpoMyQcDKH8z9VmgyeHJcnunj4=
github.com/containers/common v0.53.1-0.20230621115248-a2cd3ea30337/go.mod h1:qE1MzGl69IoK7ZNCCH51+aLVjyQtnH0LiZe0wG32Jy0=
github.com/containers/common v0.53.1-0.20230621174116-586a3be4e1fc h1:6yxDNgJGrddAWKeeAH7m0GUzCFRuvc2BqXund52Ui7k=
github.com/containers/common v0.53.1-0.20230621174116-586a3be4e1fc/go.mod h1:qE1MzGl69IoK7ZNCCH51+aLVjyQtnH0LiZe0wG32Jy0=
github.com/containers/conmon v2.0.20+incompatible h1:YbCVSFSCqFjjVwHTPINGdMX1F6JXHGTUje2ZYobNrkg=
github.com/containers/conmon v2.0.20+incompatible/go.mod h1:hgwZ2mtuDrppv78a/cOBNiCm6O0UMWGx1mu7P00nu5I=
github.com/containers/image/v5 v5.25.1-0.20230613183705-07ced6137083 h1:6Pbnll97ls6G0U3DSxaTqp7Sd8Fykc4gd7BUJm7Bpn8=
Expand Down
3 changes: 2 additions & 1 deletion libpod/container_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/containers/common/pkg/resize"
"github.com/containers/common/pkg/util"
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/libpod/events"
"github.com/containers/storage/pkg/stringid"
Expand Down Expand Up @@ -923,7 +924,7 @@ func (c *Container) readExecExitCode(sessionID string) (int, error) {
chWait := make(chan error)
defer close(chWait)

_, err := WaitForFile(exitFile, chWait, time.Second*5)
_, err := util.WaitForFile(exitFile, chWait, time.Second*5)
if err != nil {
return -1, err
}
Expand Down
2 changes: 1 addition & 1 deletion libpod/container_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (c *Container) waitForExitFileAndSync() error {
chWait := make(chan error)
defer close(chWait)

_, err = WaitForFile(exitFile, chWait, time.Second*5)
_, err = cutil.WaitForFile(exitFile, chWait, time.Second*5)
if err != nil {
// Exit file did not appear
// Reset our state
Expand Down
11 changes: 4 additions & 7 deletions libpod/container_internal_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2066,10 +2066,7 @@ func (c *Container) addResolvConf() error {
}
}

ipv6, err := c.checkForIPv6(netStatus)
if err != nil {
return err
}
ipv6 := c.checkForIPv6(netStatus)

networkBackend := c.runtime.config.Network.NetworkBackend
nameservers := make([]string, 0, len(c.runtime.config.Containers.DNSServers)+len(c.config.DNSServer))
Expand Down Expand Up @@ -2137,13 +2134,13 @@ func (c *Container) addResolvConf() error {
}

// Check if a container uses IPv6.
func (c *Container) checkForIPv6(netStatus map[string]types.StatusBlock) (bool, error) {
func (c *Container) checkForIPv6(netStatus map[string]types.StatusBlock) bool {
for _, status := range netStatus {
for _, netInt := range status.Interfaces {
for _, netAddress := range netInt.Subnets {
// Note: only using To16() does not work since it also returns a valid ip for ipv4
if netAddress.IPNet.IP.To4() == nil && netAddress.IPNet.IP.To16() != nil {
return true, nil
return true
}
}
}
Expand Down Expand Up @@ -2207,7 +2204,7 @@ func (c *Container) getHostsEntries() (etchosts.HostEntries, error) {
case c.config.NetMode.IsBridge():
entries = etchosts.GetNetworkHostEntries(c.state.NetworkStatus, names...)
case c.config.NetMode.IsSlirp4netns():
ip, err := GetSlirp4netnsIP(c.slirp4netnsSubnet)
ip, err := getSlirp4netnsIP(c.slirp4netnsSubnet)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions libpod/container_internal_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ func (c *Container) addSlirp4netnsDNS(nameservers []string) []string {
return nameservers
}

func (c *Container) isSlirp4netnsIPv6() (bool, error) {
return false, nil
func (c *Container) isSlirp4netnsIPv6() bool {
return false
}

// check for net=none
Expand Down
31 changes: 20 additions & 11 deletions libpod/container_internal_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"syscall"
"time"

"github.com/containers/common/libnetwork/slirp4netns"
"github.com/containers/common/libnetwork/types"
"github.com/containers/common/pkg/cgroups"
"github.com/containers/common/pkg/config"
Expand Down Expand Up @@ -621,7 +622,7 @@ func (c *Container) setCgroupsPath(g *generate.Generator) error {
func (c *Container) addSlirp4netnsDNS(nameservers []string) []string {
// slirp4netns has a built in DNS forwarder.
if c.config.NetMode.IsSlirp4netns() {
slirp4netnsDNS, err := GetSlirp4netnsDNS(c.slirp4netnsSubnet)
slirp4netnsDNS, err := slirp4netns.GetDNS(c.slirp4netnsSubnet)
if err != nil {
logrus.Warn("Failed to determine Slirp4netns DNS: ", err.Error())
} else {
Expand All @@ -631,20 +632,28 @@ func (c *Container) addSlirp4netnsDNS(nameservers []string) []string {
return nameservers
}

func (c *Container) isSlirp4netnsIPv6() (bool, error) {
func (c *Container) isSlirp4netnsIPv6() bool {
if c.config.NetMode.IsSlirp4netns() {
ctrNetworkSlipOpts := []string{}
if c.config.NetworkOptions != nil {
ctrNetworkSlipOpts = append(ctrNetworkSlipOpts, c.config.NetworkOptions["slirp4netns"]...)
}
slirpOpts, err := parseSlirp4netnsNetworkOptions(c.runtime, ctrNetworkSlipOpts)
if err != nil {
return false, err
extraOptions := c.config.NetworkOptions[slirp4netns.BinaryName]
options := make([]string, 0, len(c.runtime.config.Engine.NetworkCmdOptions)+len(extraOptions))
options = append(options, c.runtime.config.Engine.NetworkCmdOptions...)
options = append(options, extraOptions...)

// loop backwards as the last argument wins and we can exit early
// This should be kept in sync with c/common/libnetwork/slirp4netns.
for i := len(options) - 1; i >= 0; i-- {
switch options[i] {
case "enable_ipv6=true":
return true
case "enable_ipv6=false":
return false
}
}
return slirpOpts.enableIPv6, nil
// default is true
return true
}

return false, nil
return false
}

// check for net=none
Expand Down
3 changes: 2 additions & 1 deletion libpod/info_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/containers/common/libnetwork/pasta"
libpod "github.com/containers/common/libnetwork/slirp4netns"
"github.com/containers/common/pkg/apparmor"
"github.com/containers/common/pkg/cgroups"
"github.com/containers/common/pkg/seccomp"
Expand Down Expand Up @@ -58,7 +59,7 @@ func (r *Runtime) setPlatformHostInfo(info *define.HostInfo) error {

slirp4netnsPath := r.config.Engine.NetworkCmdPath
if slirp4netnsPath == "" {
slirp4netnsPath, _ = r.config.FindHelperBinary(slirp4netnsBinaryName, true)
slirp4netnsPath, _ = r.config.FindHelperBinary(libpod.BinaryName, true)
}
if slirp4netnsPath != "" {
version, err := util.ProgramVersion(slirp4netnsPath)
Expand Down
5 changes: 1 addition & 4 deletions libpod/networking_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,10 +583,7 @@ func (c *Container) NetworkConnect(nameOrID, netName string, netOpts types.PerNe
}
}

ipv6, err := c.checkForIPv6(networkStatus)
if err != nil {
return err
}
ipv6 := c.checkForIPv6(networkStatus)

// Update resolv.conf if required
stringIPs := make([]string, 0, len(results[netName].DNSServerIPs))
Expand Down
2 changes: 1 addition & 1 deletion libpod/networking_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (r *Runtime) GetRootlessNetNs(new bool) (*RootlessNetNS, error) {
return nil, nil
}

func GetSlirp4netnsIP(subnet *net.IPNet) (*net.IP, error) {
func getSlirp4netnsIP(subnet *net.IPNet) (*net.IP, error) {
return nil, errors.New("not implemented GetSlirp4netnsIP")
}

Expand Down
Loading

0 comments on commit 49e0bde

Please sign in to comment.