Skip to content

Commit

Permalink
Merge pull request #13485 from Luap99/ipv6-slirp
Browse files Browse the repository at this point in the history
slirp: fix setup on ipv6 disabled systems
  • Loading branch information
openshift-merge-robot authored Mar 14, 2022
2 parents 33d8081 + 3bb046a commit ae7997a
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions libpod/networking_slirp4netns.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"time"

Expand Down Expand Up @@ -302,11 +303,15 @@ func (r *Runtime) setupSlirp4netns(ctr *Container, netns ns.NetNS) error {
cmd.Stdout = logFile
cmd.Stderr = logFile

var slirpReadyChan (chan struct{})

var slirpReadyWg, netnsReadyWg *sync.WaitGroup
if netOptions.enableIPv6 {
slirpReadyChan = make(chan struct{})
defer close(slirpReadyChan)
// use two wait groups to make sure we set the sysctl before
// starting slirp and reset it only after slirp is ready
slirpReadyWg = &sync.WaitGroup{}
netnsReadyWg = &sync.WaitGroup{}
slirpReadyWg.Add(1)
netnsReadyWg.Add(1)

go func() {
err := ns.WithNetNSPath(netnsPath, func(_ ns.NetNS) error {
// Duplicate Address Detection slows the ipv6 setup down for 1-2 seconds.
Expand All @@ -318,23 +323,37 @@ func (r *Runtime) setupSlirp4netns(ctr *Container, netns ns.NetNS) error {
// is ready in case users rely on this sysctl.
orgValue, err := ioutil.ReadFile(ipv6ConfDefaultAcceptDadSysctl)
if err != nil {
netnsReadyWg.Done()
// on ipv6 disabled systems the sysctl does not exists
// so we should not error
if errors.Is(err, os.ErrNotExist) {
return nil
}
return err
}
err = ioutil.WriteFile(ipv6ConfDefaultAcceptDadSysctl, []byte("0"), 0644)
netnsReadyWg.Done()
if err != nil {
return err
}
// wait for slirp to finish setup
<-slirpReadyChan

// wait until slirp4nets is ready before reseting this value
slirpReadyWg.Wait()
return ioutil.WriteFile(ipv6ConfDefaultAcceptDadSysctl, orgValue, 0644)
})
if err != nil {
logrus.Warnf("failed to set net.ipv6.conf.default.accept_dad sysctl: %v", err)
}
}()

// wait until we set the sysctl
netnsReadyWg.Wait()
}

if err := cmd.Start(); err != nil {
if netOptions.enableIPv6 {
slirpReadyWg.Done()
}
return errors.Wrapf(err, "failed to start slirp4netns process")
}
defer func() {
Expand All @@ -344,11 +363,12 @@ func (r *Runtime) setupSlirp4netns(ctr *Container, netns ns.NetNS) error {
}
}()

if err := waitForSync(syncR, cmd, logFile, 1*time.Second); err != nil {
return err
err = waitForSync(syncR, cmd, logFile, 1*time.Second)
if netOptions.enableIPv6 {
slirpReadyWg.Done()
}
if slirpReadyChan != nil {
slirpReadyChan <- struct{}{}
if err != nil {
return err
}

// Set a default slirp subnet. Parsing a string with the net helper is easier than building the struct myself
Expand Down

0 comments on commit ae7997a

Please sign in to comment.