diff --git a/networking/plugins/bridge/bridge.go b/networking/plugins/bridge/bridge.go index 297eca3..71b1c70 100644 --- a/networking/plugins/bridge/bridge.go +++ b/networking/plugins/bridge/bridge.go @@ -1,17 +1,17 @@ package main import ( - "encoding/json" "fmt" - "io/ioutil" "log" "net" "os" "runtime" "syscall" + "github.com/appc/spec/schema/types" "github.com/coreos/rocket/Godeps/_workspace/src/github.com/vishvananda/netlink" + "github.com/coreos/rocket/networking/ipam" "github.com/coreos/rocket/networking/util" ) @@ -97,96 +97,86 @@ func ensureBridge(brName string, ipn *net.IPNet) (*netlink.Bridge, error) { return br, nil } -func loadConf(path string) (*Net, error) { - conf, err := ioutil.ReadFile(path) - if err != nil { - return nil, err - } +func setupVeth(contID types.UUID, netns string, br *netlink.Bridge, ipn *net.IPNet, ifName string) error { + var hostVethName string - n := &Net{ - BrName: defaultBrName, - } - - err = json.Unmarshal(conf, n) - if err != nil { - return nil, err - } - - return n, nil -} - -func parseCIDR(s string) (*net.IPNet, error) { - ip, ipn, err := net.ParseCIDR(s) - if err != nil { - return nil, err - } - - ipn.IP = ip - return ipn, nil -} - -func cmdAdd(contID, netns, netConf, ifName string) error { - conf, err := loadConf(netConf) - if err != nil { - return fmt.Errorf("Failed to load %q: %v", netConf, err) - } - - // create bridge if necessary - var ipn *net.IPNet - if conf.IsGW && conf.IPAlloc.Type == "static" { - ipn, err = parseCIDR(conf.IPAlloc.Subnet) + err := util.WithNetNSPath(netns, func(hostNS *os.File) error { + // create the veth pair in the container and move host end into host netns + hostVeth, _, err := util.SetupVeth(contID.String(), ifName, ipn, hostNS) if err != nil { - return fmt.Errorf("Error parsing ipAlloc.Subnet: %v", err) + return err } - } - br, err := ensureBridge(conf.BrName, ipn) + + hostVethName = hostVeth.Attrs().Name + return nil + }) if err != nil { - return fmt.Errorf("Failed to create bridge %q: %v", conf.BrName, err) - } - - // save a handle to current (host) network namespace - thisNS, err := os.Open("/proc/self/ns/net") - if err != nil { - return fmt.Errorf("Failed to open /proc/self/ns/net: %v", err) - } - - // switch to the container namespace - contNS, err := os.Open(netns) - if err != nil { - return fmt.Errorf("Failed to open %v: %v", netns, err) - } - - if err = util.SetNS(contNS, syscall.CLONE_NEWNET); err != nil { - return fmt.Errorf("Error switching to ns %v: %v", netns, err) - } - - // create the veth pair in the container and move host end into host netns - hostVeth, _, err := util.SetupVeth(contID, ifName, nil, thisNS) - if err != nil { - return fmt.Errorf("Error: %v", err) - } - - // switch back to host netns and plug the host veth end into the bridge - if err = util.SetNS(thisNS, syscall.CLONE_NEWNET); err != nil { - return fmt.Errorf("Error switching to host netns: %v", err) + return err } // need to lookup hostVeth again as its index has changed during ns move - hostVeth, err = netlink.LinkByName(hostVeth.Attrs().Name) + hostVeth, err := netlink.LinkByName(hostVethName) if err != nil { - return fmt.Errorf("Failed to lookup %q: %v", hostVeth.Attrs().Name, err) + return fmt.Errorf("failed to lookup %q: %v", hostVethName, err) } + // connect host veth end to the bridge if err = netlink.LinkSetMaster(hostVeth, br); err != nil { - return fmt.Errorf("Failed to connect %q to bridge %q: %v", hostVeth.Attrs().Name, conf.BrName, err) + return fmt.Errorf("failed to connect %q to bridge: %v", hostVethName, br.Attrs().Name, err) + } + + return nil +} + +func cmdAdd(contID, netns, netConf, ifName string) error { + cid, err := types.NewUUID(contID) + if err != nil { + return fmt.Errorf("error parsing ContainerID: %v", err) + } + + conf := Net{ + BrName: defaultBrName, + } + if err := util.LoadNet(netConf, &conf); err != nil { + return fmt.Errorf("failed to load %q: %v", netConf, err) + } + + ipn, gw, err := ipam.AllocIP(*cid, netConf, ifName, "") + if err != nil { + return err + } + + var gwn *net.IPNet + if conf.IsGW && gw != nil { + gwn = &net.IPNet{ + IP: gw, + Mask: ipn.Mask, + } + } + + // create bridge if necessary + br, err := ensureBridge(conf.BrName, gwn) + if err != nil { + return fmt.Errorf("failed to create bridge %q: %v", conf.BrName, err) + } + + if err = setupVeth(*cid, netns, br, ipn, ifName); err != nil { + return err + } + + // print to stdout the assigned IP for rkt + // TODO(eyakubovich): this will need to be JSON per latest proposal + if _, err = fmt.Print(ipn.String()); err != nil { + return err } return nil } func cmdDel(contID, netns, netConf, ifName string) error { - // TODO - return nil + return util.WithNetNSPath(netns, func(hostNS *os.File) error { + return util.DelLinkByName(ifName) + }) } func usage() int { @@ -213,7 +203,7 @@ func main() { } if err != nil { - log.Print(err) + log.Printf("%v: %v", os.Args[1], err) os.Exit(1) } } diff --git a/networking/plugins/veth/veth.go b/networking/plugins/veth/veth.go index 09d524d..5d6ec13 100644 --- a/networking/plugins/veth/veth.go +++ b/networking/plugins/veth/veth.go @@ -21,7 +21,6 @@ import ( "os" "runtime" "strings" - "syscall" "github.com/appc/spec/schema/types" "github.com/coreos/rocket/Godeps/_workspace/src/github.com/vishvananda/netlink" @@ -52,7 +51,7 @@ func cmdAdd(contID, netns, netConf, ifName, args string) error { cid, err := types.NewUUID(contID) if err != nil { - return fmt.Errorf("Error parsing ContainerID: %v", err) + return fmt.Errorf("error parsing ContainerID: %v", err) } ipn, gw, err := ipam.AllocIP(*cid, netConf, ifName, args) @@ -98,26 +97,9 @@ func cmdAdd(contID, netns, netConf, ifName, args string) error { } func cmdDel(contID, netns, netConf, ifName, args string) error { - // switch to the container namespace - contNS, err := os.Open(netns) - if err != nil { - return fmt.Errorf("Failed to open %v: %v", netns, err) - } - - if err = util.SetNS(contNS, syscall.CLONE_NEWNET); err != nil { - return fmt.Errorf("Error switching to ns %v: %v", netns, err) - } - - iface, err := netlink.LinkByName(ifName) - if err != nil { - return fmt.Errorf("Failed to lookup %q: %v", ifName, err) - } - - if err = netlink.LinkDel(iface); err != nil { - return fmt.Errorf("Failed to delete %q: %v", ifName, err) - } - - return nil + return util.WithNetNSPath(netns, func(hostNS *os.File) error { + return util.DelLinkByName(ifName) + }) } func usage() int { @@ -144,7 +126,7 @@ func main() { } if err != nil { - log.Print(err) + log.Printf("%v: %v", os.Args[1], err) os.Exit(1) } } diff --git a/networking/util/veth.go b/networking/util/link.go similarity index 87% rename from networking/util/veth.go rename to networking/util/link.go index 19fb9cc..1c8f0b9 100644 --- a/networking/util/veth.go +++ b/networking/util/link.go @@ -79,3 +79,16 @@ func SetupVeth(entropy, contVethName string, ipn *net.IPNet, hostNS *os.File) (h return } + +func DelLinkByName(ifName string) error { + iface, err := netlink.LinkByName(ifName) + if err != nil { + return fmt.Errorf("Failed to lookup %q: %v", ifName, err) + } + + if err = netlink.LinkDel(iface); err != nil { + return fmt.Errorf("Failed to delete %q: %v", ifName, err) + } + + return nil +} diff --git a/networking/util/net.go b/networking/util/net.go index 665f010..b58f922 100644 --- a/networking/util/net.go +++ b/networking/util/net.go @@ -27,7 +27,7 @@ type Net struct { IPAlloc struct { Type string `json:"type,omitempty"` Subnet string `json:"subnet,omitempty"` - } + } `json:"ipAlloc"` } func LoadNet(path string, n interface{}) error { diff --git a/stage1/init/init.go b/stage1/init/init.go index 9faffaa..d24261a 100644 --- a/stage1/init/init.go +++ b/stage1/init/init.go @@ -128,7 +128,10 @@ func stage1() int { env = append(env, "LD_LIBRARY_PATH="+filepath.Join(path.Stage1RootfsPath(c.Root), "usr/lib")) if privNet { - n, err := networking.Setup(c.Manifest.UUID) + // careful not to make another local err variable. + // cmd.Run sets the one from parent scope + var n *networking.Networking + n, err = networking.Setup(c.Manifest.UUID) if err != nil { fmt.Fprintf(os.Stderr, "Failed to setup network: %v\n", err) return 6 @@ -141,6 +144,7 @@ func stage1() int { } cmd := exec.Cmd{ + Path: args[0], Args: args, Stdin: os.Stdin, Stdout: os.Stdout,