當前位置: 首頁>>代碼示例>>Golang>>正文


Golang netlink.LinkByName函數代碼示例

本文整理匯總了Golang中github.com/vishvananda/netlink.LinkByName函數的典型用法代碼示例。如果您正苦於以下問題:Golang LinkByName函數的具體用法?Golang LinkByName怎麽用?Golang LinkByName使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了LinkByName函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: setIfNs

func setIfNs(ifname string, pid int) error {
	link, err := netlink.LinkByName(ifname)
	if err != nil {
		if !strings.Contains(err.Error(), "Link not found") {
			log.Errorf("unable to find link %q. Error: %q", ifname, err)
			return err
		}
		// try once more as sometimes (somehow) link creation is taking
		// sometime, causing link not found error
		time.Sleep(1 * time.Second)
		link, err = netlink.LinkByName(ifname)
		if err != nil {
			log.Errorf("unable to find link %q. Error %q", ifname, err)
			return err
		}
	}

	err = netlink.LinkSetNsPid(link, pid)
	if err != nil {
		log.Errorf("unable to move interface '%s' to pid %d. Error: %s",
			ifname, pid, err)
	}

	return err
}
開發者ID:balajisiva,項目名稱:netplugin,代碼行數:25,代碼來源:docker.go

示例2: SetupNetwork

/*   SetupNetwork()
 */
func SetupNetwork(addr string) error {
	// Bring up loop back interface.
	lo, e := netlink.LinkByName("lo")
	if e != nil {
		return fmt.Errorf("Failed to find loopback interface: %v", e)
	}

	if e := netlink.LinkSetUp(lo); e != nil {
		return fmt.Errorf("Failed to setup loopback interface: %v", e)
	}

	if len(addr) > 0 {
		veth, e := netlink.LinkByName("veth0")
		if e != nil {
			return fmt.Errorf("Failed to find veth interface: %v", e)
		}

		addr, e := netlink.ParseAddr(addr)
		if e != nil {
			return fmt.Errorf("Failed to parse NetworkAddr: %v", e)
		}

		netlink.AddrAdd(veth, addr)
		if e := netlink.LinkSetUp(veth); e != nil {
			return fmt.Errorf("Network link failed to come up: %v", e)
		}
	}

	return nil
}
開發者ID:tswindell,項目名稱:nest-boot,代碼行數:32,代碼來源:main.go

示例3: checkSandbox

func checkSandbox(t *testing.T, info libnetwork.EndpointInfo) {
	origns, err := netns.Get()
	if err != nil {
		t.Fatalf("Could not get the current netns: %v", err)
	}
	defer origns.Close()

	key := info.Sandbox().Key()
	f, err := os.OpenFile(key, os.O_RDONLY, 0)
	if err != nil {
		t.Fatalf("Failed to open network namespace path %q: %v", key, err)
	}
	defer f.Close()

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	nsFD := f.Fd()
	if err = netns.Set(netns.NsHandle(nsFD)); err != nil {
		t.Fatalf("Setting to the namespace pointed to by the sandbox %s failed: %v", key, err)
	}
	defer netns.Set(origns)

	_, err = netlink.LinkByName("eth0")
	if err != nil {
		t.Fatalf("Could not find the interface eth0 inside the sandbox: %v", err)
	}

	_, err = netlink.LinkByName("eth1")
	if err != nil {
		t.Fatalf("Could not find the interface eth1 inside the sandbox: %v", err)
	}
}
開發者ID:hurrygeek,項目名稱:libnetwork,代碼行數:33,代碼來源:libnetwork_test.go

示例4: setInterfaceIP

// Set the IP addr of a netlink interface
func (driver *driver) setInterfaceIP(name string, rawIP string) error {
	var netlinkRetryTimer time.Duration
	netlinkRetryTimer = 2
	iface, err := netlink.LinkByName(name)
	if err != nil {
		log.Debugf("error retrieving new OVS bridge netlink link [ %s ] allowing another [ %d ] seconds for the host to finish creating it..", bridgeName, netlinkRetryTimer)
		time.Sleep(netlinkRetryTimer * time.Second)
		iface, err = netlink.LinkByName(name)
		if err != nil {
			log.Debugf("error retrieving new OVS bridge netlink link [ %s ] allowing another [ %d ] seconds for the host to finish creating it..", bridgeName, netlinkRetryTimer)
			time.Sleep(netlinkRetryTimer * time.Second)
			iface, err = netlink.LinkByName(name)
			if err != nil {
				log.Fatalf("Abandoning retrieving the new OVS bridge link from netlink, Run [ ip link ] to troubleshoot the error: %s", err)
				return err
			}
		}
	}
	ipNet, err := netlink.ParseIPNet(rawIP)
	if err != nil {
		return err
	}
	addr := &netlink.Addr{ipNet, ""}
	return netlink.AddrAdd(iface, addr)
}
開發者ID:m-barthelemy,項目名稱:docker-ovs-plugin,代碼行數:26,代碼來源:utils.go

示例5: getIntfName

func (d *OvsDriver) getIntfName() (string, error) {
	// take a lock for modifying shared state
	d.lock.Lock()
	defer d.lock.Unlock()

	// get the next available port number
	for i := 0; i < maxIntfRetry; i++ {
		// Pick next port number
		d.oper.CurrPortNum++
		if d.oper.CurrPortNum >= maxPortNum {
			d.oper.CurrPortNum = 0 // roll over
		}
		intfName := fmt.Sprintf("vport%d", d.oper.CurrPortNum)
		ovsIntfName := getOvsPortName(intfName, false)

		// check if the port name is already in use
		_, err := netlink.LinkByName(intfName)
		_, err2 := netlink.LinkByName(ovsIntfName)
		if err != nil && strings.Contains(err.Error(), "not found") &&
			err2 != nil && strings.Contains(err2.Error(), "not found") {
			// save the new state
			err = d.oper.Write()
			if err != nil {
				return "", err
			}
			return intfName, nil
		}
	}

	return "", core.Errorf("Could not get intf name. Max retry exceeded")
}
開發者ID:jojimt,項目名稱:netplugin,代碼行數:31,代碼來源:ovsdriver.go

示例6: makeIface

func (e *Endpoint) makeIface(network *Network, netns string) error {
	if vethLink, _ := netlink.LinkByName("veth" + e.EndpointShortID); vethLink != nil {
		log.Println("veth"+e.EndpointShortID, "already exist")
	} else {
		ethLink, err := netlink.LinkByName(network.Eth)
		if err != nil {
			fmt.Println("[Err] LinkByName:", err)
			return err
		}

		attrs := netlink.NewLinkAttrs()
		attrs.Name = "veth" + e.EndpointShortID
		attrs.ParentIndex = ethLink.Attrs().Index

		vlan := &netlink.Vlan{
			attrs, network.VLanID,
		}

		if err := netlink.LinkAdd(vlan); err != nil {
			log.Println("Err: ", err, "LinkAdd")
			return err
		}
	}

	if err := exec.Command("ip", "link", "set", "veth"+e.EndpointShortID, "netns", netns).Run(); err != nil {
		log.Println("Err: ", err, "LinkSet NS")
		return err
	}
	return nil
}
開發者ID:anarcher,項目名稱:docker-vlan-driver,代碼行數:30,代碼來源:endpoint.go

示例7: attach

// attach a container network interface to an external network
func (v *veth) attach(n *configs.Network) (err error) {
	brl, err := netlink.LinkByName(n.Bridge)
	if err != nil {
		return err
	}
	br, ok := brl.(*netlink.Bridge)
	if !ok {
		return fmt.Errorf("Wrong device type %T", brl)
	}
	host, err := netlink.LinkByName(n.HostInterfaceName)
	if err != nil {
		return err
	}

	if err := netlink.LinkSetMaster(host, br); err != nil {
		return err
	}
	if err := netlink.LinkSetMTU(host, n.Mtu); err != nil {
		return err
	}
	if n.HairpinMode {
		if err := netlink.LinkSetHairpin(host, true); err != nil {
			return err
		}
	}
	if err := netlink.LinkSetUp(host); err != nil {
		return err
	}

	return nil
}
開發者ID:johndmulhausen,項目名稱:kubernetes,代碼行數:32,代碼來源:network_linux.go

示例8: cleanExistingRoutes

// Remove routes with netlink syscalls with a scope of:
// RT_SCOPE_LINK = 0xfd (253)
// RT_SCOPE_UNIVERSE = 0x0 (0)
func cleanExistingRoutes(ifaceStr string) error {
	iface, err := netlink.LinkByName(ifaceStr)
	ipvlanParentIface, err := netlink.LinkByName(ifaceStr)
	if err != nil {
		log.Errorf("Error occoured finding the linux link [ %s ] from netlink: %s", ipvlanParentIface.Attrs().Name, err)
		return err
	}
	routes, err := netlink.RouteList(iface, netlink.FAMILY_V4)
	if err != nil {
		log.Errorf("Unable to retreive netlink routes: %s", err)
		return err
	}
	ifaceIP, err := getIfaceIP(ifaceStr)
	if err != nil {
		log.Errorf("Unable to retreive a usable IP via ethernet interface: %s", ifaceStr)
		return err
	}
	for _, route := range routes {
		if netOverlaps(ifaceIP, route.Dst) == true {
			log.Warnf("Ignoring route [ %v ] as it is associated to the [ %s ] interface", ifaceIP, ifaceStr)
		} else if route.Scope == 0x0 || route.Scope == 0xfd {
			// Remove link and universal routes from the docker host ipvlan interface
			log.Infof("Cleaning static route cache for the destination: [ %s ]", route.Dst)
			err := delRoute(route, ipvlanParentIface)
			if err != nil {
				log.Errorf("Error deleting static route cache for Destination: [ %s ] and Nexthop [ %s ] Error: %s", route.Dst, route.Gw, err)
			}
		}
	}
	return nil
}
開發者ID:nerdalert,項目名稱:bgp-ipvlan-docker,代碼行數:34,代碼來源:del_routes.go

示例9: CreateAndAttachVeth

// 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
}
開發者ID:n054,項目名稱:weave,代碼行數:59,代碼來源:veth.go

示例10: createNetwork

func (driver *driver) createNetwork(w http.ResponseWriter, r *http.Request) {
	var create networkCreate
	err := json.NewDecoder(r.Body).Decode(&create)
	if err != nil {
		sendError(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
		return
	}

	if driver.network != "" {
		errorResponsef(w, "You get just one network, and you already made %s", driver.network)
		return
	}
	driver.network = create.NetworkID
	containerCidr := driver.pluginConfig.containerSubnet
	driver.cidr = containerCidr
	// Todo: check for ipam errors
	driver.ipAllocator.RequestIP(containerCidr, nil)

	emptyResponse(w)

	// TODO: sort out what rules are in place by default vs. plugin
	err = driver.natOut()
	if err != nil {
		log.Warnf("error setting up outboud nat: %s", err)
	}

	if ipVlanMode == ipVlanL3 {
		log.Debugf("Adding route for the local ipvlan subnet [ %s ] in the default namespace using the specified host interface [ %s]", containerCidr.String(), ipVlanEthIface)
		ipvlanParent, err := netlink.LinkByName(ipVlanEthIface)
		// Add a route in the default NS to point to the IPVlan namespace subnet
		addRouteIface(containerCidr, ipvlanParent)
		if err != nil {
			log.Debugf("a problem occurred adding the container subnet default namespace route", err)
		}
	} else if ipVlanMode == ipVlanL3Routing {
		log.Debugf("Adding route for the local ipvlan subnet [ %s ] in the default namespace using the specified host interface [ %s]", containerCidr.String(), ipVlanEthIface)
		ipvlanParent, err := netlink.LinkByName(ipVlanEthIface)
		// Add a route in the default NS to point to the IPVlan namespace subnet
		addRouteIface(containerCidr, ipvlanParent)
		if err != nil {
			log.Debugf("a problem occurred adding the container subnet default namespace route", err)
		}

		// Announce the local IPVLAN network to the other peers in the BGP cluster
		log.Infof("New Docker network: [ %s ]", containerCidr.String())
		err = routing.AdvertizeNewRoute(containerCidr)
		if err != nil {
			log.Fatalf("Error installing container route : %s", err)
		}
	}
}
開發者ID:zenvdeluca,項目名稱:ipvlan-docker-plugin,代碼行數:51,代碼來源:driver.go

示例11: Add

func (Bridge) Add(bridge, slaveIf *net.Interface) error {
	netlinkMu.Lock()
	defer netlinkMu.Unlock()

	master, err := netlink.LinkByName(bridge.Name)
	if err != nil {
		return err
	}

	slave, err := netlink.LinkByName(slaveIf.Name)
	if err != nil {
		return err
	}
	return netlink.LinkSetMaster(slave, master.(*netlink.Bridge))
}
開發者ID:cloudfoundry,項目名稱:guardian,代碼行數:15,代碼來源:bridge_linux.go

示例12: removeDefaultBridgeInterface

// Remove default bridge interface if present (--bridge=none use case)
func removeDefaultBridgeInterface() {
	if lnk, err := netlink.LinkByName(bridge.DefaultBridgeName); err == nil {
		if err := netlink.LinkDel(lnk); err != nil {
			logrus.Warnf("Failed to remove bridge interface (%s): %v", bridge.DefaultBridgeName, err)
		}
	}
}
開發者ID:docker,項目名稱:docker,代碼行數:8,代碼來源:daemon_unix.go

示例13: setupRoute

func setupRoute(config *configs.Config) error {
	for _, config := range config.Routes {
		_, dst, err := net.ParseCIDR(config.Destination)
		if err != nil {
			return err
		}
		src := net.ParseIP(config.Source)
		if src == nil {
			return fmt.Errorf("Invalid source for route: %s", config.Source)
		}
		gw := net.ParseIP(config.Gateway)
		if gw == nil {
			return fmt.Errorf("Invalid gateway for route: %s", config.Gateway)
		}
		l, err := netlink.LinkByName(config.InterfaceName)
		if err != nil {
			return err
		}
		route := &netlink.Route{
			Scope:     netlink.SCOPE_UNIVERSE,
			Dst:       dst,
			Src:       src,
			Gw:        gw,
			LinkIndex: l.Attrs().Index,
		}
		if err := netlink.RouteAdd(route); err != nil {
			return err
		}
	}
	return nil
}
開發者ID:johndmulhausen,項目名稱:kubernetes,代碼行數:31,代碼來源:init_linux.go

示例14: setupBridge

func (b *Bridge) setupBridge(externalPort string) error {

	la := netlink.NewLinkAttrs()
	la.Name = b.bridgeName
	bridge, _ := netlink.LinkByName(b.bridgeName)

	if bridge == nil {
		log.Debugf("Bridge %s does not exist ", b.bridgeName)
		out, err := exec.Command("ovs-vsctl", "add-br", b.bridgeName).CombinedOutput()
		if err != nil {
			log.Fatalf("Bridge %s creation failed been created.  Resp: %s, err: %s", b.bridgeName, out, err)
		}
		log.Infof("Bridge %s has been created.  Resp: %s", b.bridgeName, out)

		out, err = exec.Command("ovs-vsctl", "add-port", b.bridgeName, externalPort).CombinedOutput()
		if err != nil {
			log.Fatalf("Failed to add external port %s.  Resp: %s, err: %s", externalPort, out, err)
		}
		log.Infof("External port %s has been added to %s. Resp: %s", externalPort, b.bridgeName, out)

		out, err = exec.Command("ifconfig", externalPort, "0.0.0.0").CombinedOutput()
		if err != nil {
			log.Fatalf("Failed to ip address of port %s. Resp: %s, err: %s", externalPort, out, err)
		}
		log.Infof("Ip address of port  %s has been cleaned. Resp: %s", externalPort, out)

		return err
	} else {
		log.Debugf("Bridge %s already exsist", b.bridgeName)
	}

	return nil
}
開發者ID:akanto,項目名稱:dhcp-plugin,代碼行數:33,代碼來源:ovslib.go

示例15: setLinkUp

// setLinkUp sets the link up
func setLinkUp(name string) error {
	iface, err := netlink.LinkByName(name)
	if err != nil {
		return err
	}
	return netlink.LinkSetUp(iface)
}
開發者ID:balajisiva,項目名稱:netplugin,代碼行數:8,代碼來源:ovsSwitch.go


注:本文中的github.com/vishvananda/netlink.LinkByName函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。