// create and attach a veth to the Weave bridge func CreateAndAttachVeth(name, peerName, bridgeName string, mtu int, keepTXOn bool, init func(peer netlink.Link) error) (*netlink.Veth, error) { bridge, err := netlink.LinkByName(bridgeName) if err != nil { return nil, fmt.Errorf(`bridge "%s" not present; did you launch weave?`, bridgeName) } if mtu == 0 { mtu = bridge.Attrs().MTU } veth := &netlink.Veth{ LinkAttrs: netlink.LinkAttrs{ Name: name, MTU: mtu}, PeerName: peerName, } if err := netlink.LinkAdd(veth); err != nil { return nil, fmt.Errorf(`could not create veth pair %s-%s: %s`, name, peerName, err) } cleanup := func(format string, a ...interface{}) (*netlink.Veth, error) { netlink.LinkDel(veth) return nil, fmt.Errorf(format, a...) } switch bridgeType := DetectBridgeType(bridgeName, DatapathName); bridgeType { case Bridge, BridgedFastdp: if err := netlink.LinkSetMasterByIndex(veth, bridge.Attrs().Index); err != nil { return cleanup(`unable to set master of %s: %s`, name, err) } if bridgeType == Bridge && !keepTXOn { if err := EthtoolTXOff(peerName); err != nil { return cleanup(`unable to set tx off on %q: %s`, peerName, err) } } case Fastdp: if err := odp.AddDatapathInterface(bridgeName, name); err != nil { return cleanup(`failed to attach %s to device "%s": %s`, name, bridgeName, err) } default: return cleanup(`invalid bridge configuration`) } if init != nil { peer, err := netlink.LinkByName(peerName) if err != nil { return cleanup("unable to find peer veth %s: %s", peerName, err) } if err := init(peer); err != nil { return cleanup("initializing veth: %s", err) } } if err := netlink.LinkSetUp(veth); err != nil { return cleanup("unable to bring veth up: %s", err) } return veth, nil }
func (driver *driver) JoinEndpoint(j *api.JoinRequest) (*api.JoinResponse, error) { endID := j.EndpointID maybeBridge, err := netlink.LinkByName(WeaveBridge) if err != nil { return nil, errorf(`bridge "%s" not present; did you launch weave?`, WeaveBridge) } // create and attach local name to the bridge local := vethPair(endID[:5]) local.Attrs().MTU = maybeBridge.Attrs().MTU if err := netlink.LinkAdd(local); err != nil { return nil, errorf("could not create veth pair: %s", err) } switch maybeBridge.(type) { case *netlink.Bridge: if err := netlink.LinkSetMasterByIndex(local, maybeBridge.Attrs().Index); err != nil { return nil, errorf(`unable to set master: %s`, err) } case *netlink.GenericLink: if maybeBridge.Type() != "openvswitch" { Log.Errorf("device %s is %+v", WeaveBridge, maybeBridge) return nil, errorf(`device "%s" is of type "%s"`, WeaveBridge, maybeBridge.Type()) } odp.AddDatapathInterface(WeaveBridge, local.Name) case *netlink.Device: Log.Warnf("kernel does not report what kind of device %s is, just %+v", WeaveBridge, maybeBridge) // Assume it's our openvswitch device, and the kernel has not been updated to report the kind. odp.AddDatapathInterface(WeaveBridge, local.Name) default: Log.Errorf("device %s is %+v", WeaveBridge, maybeBridge) return nil, errorf(`device "%s" not a bridge`, WeaveBridge) } if err := netlink.LinkSetUp(local); err != nil { return nil, errorf(`unable to bring veth up: %s`, err) } ifname := &api.InterfaceName{ SrcName: local.PeerName, DstPrefix: "ethwe", } response := &api.JoinResponse{ InterfaceName: ifname, } if !driver.noMulticastRoute { multicastRoute := api.StaticRoute{ Destination: "224.0.0.0/4", RouteType: types.CONNECTED, } response.StaticRoutes = append(response.StaticRoutes, multicastRoute) } Log.Infof("Join endpoint %s:%s to %s", j.NetworkID, j.EndpointID, j.SandboxKey) return response, nil }
func (driver *driver) JoinEndpoint(j *api.JoinRequest) (*api.JoinResponse, error) { endID := j.EndpointID // create and attach local name to the bridge local := vethPair(endID[:5]) if err := netlink.LinkAdd(local); err != nil { return nil, errorf("could not create veth pair: %s", err) } if maybeBridge, err := netlink.LinkByName(WeaveBridge); err != nil { return nil, errorf(`bridge "%s" not present; did you launch weave?`, WeaveBridge) } else { switch maybeBridge.(type) { case *netlink.Bridge: if err := netlink.LinkSetMasterByIndex(local, maybeBridge.Attrs().Index); err != nil { return nil, errorf(`unable to set master: %s`, err) } case *netlink.GenericLink: if maybeBridge.Type() != "openvswitch" { Log.Errorf("device %s is %+v", WeaveBridge, maybeBridge) return nil, errorf(`device "%s" is of type "%s"`, WeaveBridge, maybeBridge.Type()) } odp.AddDatapathInterface(WeaveBridge, local.Name) default: Log.Errorf("device %s is %+v", WeaveBridge, maybeBridge) return nil, errorf(`device "%s" not a bridge`, WeaveBridge) } } if err := netlink.LinkSetUp(local); err != nil { return nil, errorf(`unable to bring veth up: %s`, err) } ifname := &api.InterfaceName{ SrcName: local.PeerName, DstPrefix: "ethwe", } response := &api.JoinResponse{ InterfaceName: ifname, } if driver.nameserver != "" { routeToDNS := api.StaticRoute{ Destination: driver.nameserver + "/32", RouteType: types.CONNECTED, NextHop: "", } response.StaticRoutes = []api.StaticRoute{routeToDNS} } Log.Infof("Join endpoint %s:%s to %s", j.NetworkID, j.EndpointID, j.SandboxKey) return response, nil }
func resourceLXCBridgeCreate(d *schema.ResourceData, meta interface{}) error { br := d.Get("name").(string) var bridge netlink.Link bridge, err := netlink.LinkByName(br) if err != nil { bridge = &netlink.Bridge{netlink.LinkAttrs{ Name: d.Get("name").(string), }} if err := netlink.LinkAdd(bridge); err != nil { return fmt.Errorf("Error creating bridge %s: %v", br, err) } if ifaceName, ok := d.GetOk("hostInterface"); ok { iface, err := netlink.LinkByName(ifaceName.(string)) if err != nil { return fmt.Errorf("Error adding host interface %s to bridge %s : unknow host interface %v", ifaceName, br, err) } if err := netlink.LinkSetMasterByIndex(iface, bridge.Attrs().Index); err != nil { return fmt.Errorf("Error adding host interface %s to bridge %s : %v", ifaceName, br, err) } } log.Printf("[INFO] Created new bridge %s: %v", br, bridge) } else { log.Printf("[INFO] Found existing bridge %s: %v", br, bridge) } log.Printf("[INFO] Bringing bridge %s up", br) if err := netlink.LinkSetUp(bridge); err != nil { return fmt.Errorf("Error bringing bridge %s up: %v", br, err) } d.SetId(strconv.Itoa(bridge.Attrs().Index)) return resourceLXCBridgeRead(d, meta) }