Skip to content

Commit

Permalink
libnetwork: fix sandbox restore
Browse files Browse the repository at this point in the history
The method to restore a network namespace takes a collection of
interfaces to restore with the options to apply. The interface names are
structured data, tuples of (SrcName, DstPrefix) but for whatever reason
are being passed into Restore() serialized to strings. A refactor,
f0be4d1, accidentally broke the
serialization by dropping the delimiter. Rather than fix the
serialization and leave the time-bomb for someone else to trip over,
pass the interface names as structured data.

Signed-off-by: Cory Snider <csnider@mirantis.com>
  • Loading branch information
corhere committed May 30, 2023
1 parent 18bf3aa commit 50eb2d2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
16 changes: 5 additions & 11 deletions libnetwork/osl/namespace_linux.go
Expand Up @@ -470,16 +470,10 @@ func (n *networkNamespace) Destroy() error {
}

// Restore restore the network namespace
func (n *networkNamespace) Restore(ifsopt map[string][]IfaceOption, routes []*types.StaticRoute, gw net.IP, gw6 net.IP) error {
func (n *networkNamespace) Restore(ifsopt map[Iface][]IfaceOption, routes []*types.StaticRoute, gw net.IP, gw6 net.IP) error {
// restore interfaces
for name, opts := range ifsopt {
if !strings.Contains(name, "+") {
return fmt.Errorf("wrong iface name in restore osl sandbox interface: %s", name)
}
seps := strings.Split(name, "+")
srcName := seps[0]
dstPrefix := seps[1]
i := &nwIface{srcName: srcName, dstName: dstPrefix, ns: n}
i := &nwIface{srcName: name.SrcName, dstName: name.DstPrefix, ns: n}
i.processInterfaceOptions(opts...)
if i.master != "" {
i.dstMaster = n.findDst(i.master, true)
Expand Down Expand Up @@ -531,7 +525,7 @@ func (n *networkNamespace) Restore(ifsopt map[string][]IfaceOption, routes []*ty
}

var index int
indexStr := strings.TrimPrefix(i.dstName, dstPrefix)
indexStr := strings.TrimPrefix(i.dstName, name.DstPrefix)
if indexStr != "" {
index, err = strconv.Atoi(indexStr)
if err != nil {
Expand All @@ -540,8 +534,8 @@ func (n *networkNamespace) Restore(ifsopt map[string][]IfaceOption, routes []*ty
}
index++
n.Lock()
if index > n.nextIfIndex[dstPrefix] {
n.nextIfIndex[dstPrefix] = index
if index > n.nextIfIndex[name.DstPrefix] {
n.nextIfIndex[name.DstPrefix] = index
}
n.iFaces = append(n.iFaces, i)
n.Unlock()
Expand Down
6 changes: 5 additions & 1 deletion libnetwork/osl/sandbox.go
Expand Up @@ -17,6 +17,10 @@ const (
SandboxTypeLoadBalancer = iota
)

type Iface struct {
SrcName, DstPrefix string
}

// IfaceOption is a function option type to set interface options.
type IfaceOption func(i *nwIface)

Expand Down Expand Up @@ -89,7 +93,7 @@ type Sandbox interface {
Destroy() error

// Restore restores the sandbox.
Restore(ifsopt map[string][]IfaceOption, routes []*types.StaticRoute, gw net.IP, gw6 net.IP) error
Restore(ifsopt map[Iface][]IfaceOption, routes []*types.StaticRoute, gw net.IP, gw6 net.IP) error

// ApplyOSTweaks applies operating system specific knobs on the sandbox.
ApplyOSTweaks([]SandboxType)
Expand Down
4 changes: 2 additions & 2 deletions libnetwork/sandbox.go
Expand Up @@ -765,7 +765,7 @@ func (sb *Sandbox) restoreOslSandbox() error {
var routes []*types.StaticRoute

// restore osl sandbox
Ifaces := make(map[string][]osl.IfaceOption)
Ifaces := make(map[osl.Iface][]osl.IfaceOption)
for _, ep := range sb.endpoints {
ep.mu.Lock()
joinInfo := ep.joinInfo
Expand All @@ -790,7 +790,7 @@ func (sb *Sandbox) restoreOslSandbox() error {
if len(i.llAddrs) != 0 {
ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().LinkLocalAddresses(i.llAddrs))
}
Ifaces[i.srcName+i.dstPrefix] = ifaceOptions
Ifaces[osl.Iface{SrcName: i.srcName, DstPrefix: i.dstPrefix}] = ifaceOptions
if joinInfo != nil {
routes = append(routes, joinInfo.StaticRoutes...)
}
Expand Down

0 comments on commit 50eb2d2

Please sign in to comment.