示例#1
0
文件: bridge.go 项目: squaremo/cni
func setupVeth(netns string, br *netlink.Bridge, ifName string, mtu int, hairpinMode bool) error {
	var hostVethName string

	err := ns.WithNetNSPath(netns, false, func(hostNS *os.File) error {
		// create the veth pair in the container and move host end into host netns
		hostVeth, _, err := ip.SetupVeth(ifName, mtu, hostNS)
		if err != nil {
			return err
		}

		hostVethName = hostVeth.Attrs().Name
		return nil
	})
	if err != nil {
		return err
	}

	// need to lookup hostVeth again as its index has changed during ns move
	hostVeth, err := netlink.LinkByName(hostVethName)
	if err != nil {
		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 %v: %v", hostVethName, br.Attrs().Name, err)
	}

	// set hairpin mode
	if err = netlink.LinkSetHairpin(hostVeth, hairpinMode); err != nil {
		return fmt.Errorf("failed to setup hairpin mode for %v: %v", hostVethName, err)
	}

	return nil
}
示例#2
0
func NewBridgeAdapter(link *netlink.Bridge) *BridgeAdapter {
	return &BridgeAdapter{
		uuid:   link.Attrs().Name,
		name:   link.Attrs().Name,
		tags:   []string{},
		perm:   PermR,
		config: make(map[string]interface{}),
		link:   link,
	}
}
示例#3
0
func (nm *NetlinkMonitor) ensureBridge(link *netlink.Bridge) canvas.Node {
	b := nm.g.NodeByPath("b:" + link.Attrs().Name)
	if b == nil {
		a := canvas.NewBridgeAdapter(link)
		node := canvas.NewAdapterNode(a)
		node.SetID(nm.g.NewNodeID())
		nm.g.AddNode(node)
		b = node
	}
	return b
}