当前位置: 首页>>代码示例>>Golang>>正文


Golang net.InterfaceByName函数代码示例

本文整理汇总了Golang中net.InterfaceByName函数的典型用法代码示例。如果您正苦于以下问题:Golang InterfaceByName函数的具体用法?Golang InterfaceByName怎么用?Golang InterfaceByName使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了InterfaceByName函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: setupNetworking

func setupNetworking(c *Config) error {
	if c.IP == "" {
		return nil
	}

	// loopback
	iface, err := net.InterfaceByName("lo")
	if err != nil {
		return fmt.Errorf("Unable to set up networking: %v", err)
	}
	if err := netlink.NetworkLinkUp(iface); err != nil {
		return fmt.Errorf("Unable to set up networking: %v", err)
	}
	if iface, err = net.InterfaceByName("eth0"); err != nil {
		return fmt.Errorf("Unable to set up networking: %v", err)
	}
	ip, ipNet, err := net.ParseCIDR(c.IP)
	if err != nil {
		return fmt.Errorf("Unable to set up networking: %v", err)
	}
	if err := netlink.NetworkLinkAddIp(iface, ip, ipNet); err != nil {
		return fmt.Errorf("Unable to set up networking: %v", err)
	}
	if c.Gateway != "" {
		if err := netlink.AddDefaultGw(c.Gateway, "eth0"); err != nil {
			return fmt.Errorf("Unable to set up networking: %v", err)
		}
	}
	if err := netlink.NetworkLinkUp(iface); err != nil {
		return fmt.Errorf("Unable to set up networking: %v", err)
	}

	return nil
}
开发者ID:devick,项目名称:flynn,代码行数:34,代码来源:init.go

示例2: setupNetworking

func setupNetworking(args *ContainerInitArgs) error {
	// loopback
	iface, err := net.InterfaceByName("lo")
	if err != nil {
		return fmt.Errorf("Unable to set up networking: %v", err)
	}
	if err := netlink.NetworkLinkUp(iface); err != nil {
		return fmt.Errorf("Unable to set up networking: %v", err)
	}
	if args.ip != "" {
		if iface, err = net.InterfaceByName("eth0"); err != nil {
			return fmt.Errorf("Unable to set up networking: %v", err)
		}
		ip, ipNet, err := net.ParseCIDR(args.ip)
		if err != nil {
			return fmt.Errorf("Unable to set up networking: %v", err)
		}
		if err := netlink.NetworkLinkAddIp(iface, ip, ipNet); err != nil {
			return fmt.Errorf("Unable to set up networking: %v", err)
		}
		if args.gateway != "" {
			if err := netlink.AddDefaultGw(args.gateway, "eth0"); err != nil {
				return fmt.Errorf("Unable to set up networking: %v", err)
			}
		}
		if err := netlink.NetworkLinkUp(iface); err != nil {
			return fmt.Errorf("Unable to set up networking: %v", err)
		}
	}

	return nil
}
开发者ID:snormore,项目名称:flynn,代码行数:32,代码来源:init.go

示例3: NewLinkWithOptions

// NewLinkWithOptions creates new network link on Linux host and sets some of its network
// parameters passed in as LinkOptions
//
// Calling NewLinkWithOptions is equivalent of running following commands one after another if
// particular option is passed in as a parameter:
// 		ip link add name ${ifcName} type dummy
// 		ip link set dev ${ifcName} address ${MAC address}
//		ip link set dev ${ifcName} mtu ${MTU value}
//		ip link set dev ${ifcName} up
// NewLinkWithOptions returns Linker which is initialized to a pointer of type Link if the network
// link with given LinkOptions was created successfully on the Linux host.
// It attempts to delete the link if any of the LinkOptions are incorrect or if setting the options
// failed and returns error.
func NewLinkWithOptions(ifcName string, opts LinkOptions) (Linker, error) {
	if ok, err := NetInterfaceNameValid(ifcName); !ok {
		return nil, err
	}

	if _, err := net.InterfaceByName(ifcName); err == nil {
		return nil, fmt.Errorf("Interface name %s already assigned on the host", ifcName)
	}

	if err := netlink.NetworkLinkAdd(ifcName, "dummy"); err != nil {
		return nil, fmt.Errorf("Could not create new link %s: %s", ifcName, err)
	}

	newIfc, err := net.InterfaceByName(ifcName)
	if err != nil {
		return nil, fmt.Errorf("Could not find the new interface: %s", err)
	}

	if (opts != LinkOptions{}) {
		errOpts := setLinkOptions(newIfc, opts)
		if errOpts != nil {
			if errDel := DeleteLink(newIfc.Name); err != nil {
				return nil, fmt.Errorf("Incorrect options specified: %s. Attempt to delete the link failed: %s",
					errOpts, errDel)
			}
			return nil, fmt.Errorf("Could not set link options: %s", errOpts)
		}
	}

	return &Link{
		ifc: newIfc,
	}, nil
}
开发者ID:RoPe93,项目名称:oz,代码行数:46,代码来源:link_linux.go

示例4: NewMacVtapLink

// NewMacVtapLink creates macvtap network link
//
// It is equivalent of running:
//		ip link add name mvt${RANDOM STRING} link ${master interface} type macvtap
// NewMacVtapLink returns MacVtaper which is initialized to a pointer of type MacVtapLink if the
// macvtap link was created successfully on the Linux host. Newly created link is assigned
// a random name starting with "mvt". It sets the macvlan mode to "bridge" which is a default.
// It returns error if the link could not be created.
func NewMacVtapLink(masterDev string) (MacVtaper, error) {
	macVtapDev := makeNetInterfaceName("mvt")

	if ok, err := NetInterfaceNameValid(masterDev); !ok {
		return nil, err
	}

	if _, err := net.InterfaceByName(masterDev); err != nil {
		return nil, fmt.Errorf("Master MAC VTAP device %s does not exist on the host", masterDev)
	}

	if err := netlink.NetworkLinkAddMacVtap(masterDev, macVtapDev, default_mode); err != nil {
		return nil, err
	}

	macVtapIfc, err := net.InterfaceByName(macVtapDev)
	if err != nil {
		return nil, fmt.Errorf("Could not find the new interface: %s", err)
	}

	masterIfc, err := net.InterfaceByName(masterDev)
	if err != nil {
		return nil, fmt.Errorf("Could not find the new interface: %s", err)
	}

	return &MacVtapLink{
		MacVlanLink: &MacVlanLink{
			Link: Link{
				ifc: macVtapIfc,
			},
			masterIfc: masterIfc,
			mode:      default_mode,
		},
	}, nil
}
开发者ID:Safe3,项目名称:oz,代码行数:43,代码来源:macvtap_linux.go

示例5: attach

// attach a container network interface to an external network
func (v *veth) attach(n *configs.Network) (err error) {
	bridge, err := net.InterfaceByName(n.Bridge)
	if err != nil {
		return err
	}
	host, err := net.InterfaceByName(n.HostInterfaceName)
	if err != nil {
		return err
	}
	if err := netlink.AddToBridge(host, bridge); err != nil {
		return err
	}
	if err := netlink.NetworkSetMTU(host, n.Mtu); err != nil {
		return err
	}
	if n.HairpinMode {
		if err := netlink.SetHairpinMode(host, true); err != nil {
			return err
		}
	}
	if err := netlink.NetworkLinkUp(host); err != nil {
		return err
	}

	return nil
}
开发者ID:pirater,项目名称:os,代码行数:27,代码来源:network_linux.go

示例6: NewVethPair

// NewVethPair creates a pair of veth network links.
//
// It is equivalent of running:
// 		ip link add name veth${RANDOM STRING} type veth peer name veth${RANDOM STRING}.
// NewVethPair returns Vether which is initialized to a pointer of type VethPair if the
// veth link was successfully created on Linux host. Newly created pair of veth links
// are assigned random names starting with "veth".
// NewVethPair returns error if the veth pair could not be created.
func NewVethPair() (Vether, error) {
	ifcName := makeNetInterfaceName("veth")
	peerName := makeNetInterfaceName("veth")

	if err := netlink.NetworkCreateVethPair(ifcName, peerName, 0); err != nil {
		return nil, err
	}

	newIfc, err := net.InterfaceByName(ifcName)
	if err != nil {
		return nil, fmt.Errorf("Could not find the new interface: %s", err)
	}

	peerIfc, err := net.InterfaceByName(peerName)
	if err != nil {
		return nil, fmt.Errorf("Could not find the new interface: %s", err)
	}

	return &VethPair{
		Link: Link{
			ifc: newIfc,
		},
		peerIfc: peerIfc,
	}, nil
}
开发者ID:xSmurf,项目名称:tenus,代码行数:33,代码来源:veth_linux.go

示例7: Create

// Create creates a bridge device and returns the interface.
// If the device already exists, returns the existing interface.
func (Bridge) Create(name string, ip net.IP, subnet *net.IPNet) (intf *net.Interface, err error) {
	netlinkMu.Lock()
	defer netlinkMu.Unlock()

	if intf, _ := net.InterfaceByName(name); intf != nil {
		return intf, nil
	}

	link := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: name}}

	if err := netlink.LinkAdd(link); err != nil && err.Error() != "file exists" {
		return nil, fmt.Errorf("devices: create bridge: %v", err)
	}

	hAddr, _ := net.ParseMAC(randMacAddr())
	err = netlink.LinkSetHardwareAddr(link, hAddr)
	if err != nil {
		return nil, fmt.Errorf("devices: set hardware address: %v", err)
	}

	if intf, err = net.InterfaceByName(name); err != nil {
		return nil, fmt.Errorf("devices: look up created bridge interface: %v", err)
	}

	addr := &netlink.Addr{IPNet: &net.IPNet{IP: ip, Mask: subnet.Mask}}
	if err = netlink.AddrAdd(link, addr); err != nil && err.Error() != "file exists" {
		return nil, fmt.Errorf("devices: add IP to bridge: %v", err)
	}
	return intf, nil
}
开发者ID:cloudfoundry,项目名称:guardian,代码行数:32,代码来源:bridge_linux.go

示例8: Run

func (mon *Monitor) Run(m *monitor.Monitor) {
	memory := GetMemoryStats()

	lanEth, _ := net.InterfaceByName("eth1")
	wanEth, _ := net.InterfaceByName("eth0")

	iData := InterfaceData{lanEth, wanEth}

	mon1 := HWMon{"hwmon0", []string{"2", "3"}}
	mon2 := HWMon{"hwmon1", []string{"1"}}

	temps1, err := mon1.Temperatures()
	if err != nil {
		panic(err)
	}
	temps2, err := mon2.Temperatures()
	if err != nil {
		panic(err)
	}

	allTemps := make(map[string]float64)
	for key, value := range temps1 {
		allTemps[key] = value
	}

	for key, value := range temps2 {
		allTemps[key] = value
	}

	data := Data{memory, iData, allTemps}

	m.SendData(&data)
}
开发者ID:zakkymuha,项目名称:simple-router,代码行数:33,代码来源:monitor.go

示例9: TestCreateBridgeWithMac

//
// netlink package tests which do not use RTNETLINK
//
func TestCreateBridgeWithMac(t *testing.T) {
	if testing.Short() {
		return
	}

	name := "testbridge"

	if err := CreateBridge(name, true); err != nil {
		t.Fatal(err)
	}

	if _, err := net.InterfaceByName(name); err != nil {
		t.Fatal(err)
	}

	// cleanup and tests

	if err := DeleteBridge(name); err != nil {
		t.Fatal(err)
	}

	if _, err := net.InterfaceByName(name); err == nil {
		t.Fatalf("expected error getting interface because %s bridge was deleted", name)
	}
}
开发者ID:NERSC,项目名称:docker,代码行数:28,代码来源:netlink_linux_test.go

示例10: NewBridge

// NewBridge creates new network bridge on Linux host.
//
// It is equivalent of running: ip link add name br${RANDOM STRING} type bridge
// NewBridge returns Bridger which is initialized to a pointer of type Bridge if the
// bridge was created successfully on the Linux host. Newly created bridge is assigned
// a random name starting with "br".
// It returns error if the bridge could not be created.
func NewBridge() (Bridger, error) {
	brDev := makeNetInterfaceName("br")

	if ok, err := NetInterfaceNameValid(brDev); !ok {
		return nil, err
	}

	if _, err := net.InterfaceByName(brDev); err == nil {
		return nil, fmt.Errorf("Interface name %s already assigned on the host", brDev)
	}

	if err := netlink.NetworkLinkAdd(brDev, "bridge"); err != nil {
		return nil, err
	}

	newIfc, err := net.InterfaceByName(brDev)
	if err != nil {
		return nil, fmt.Errorf("Could not find the new interface: %s", err)
	}

	return &Bridge{
		Link: Link{
			ifc: newIfc,
		},
	}, nil
}
开发者ID:sysbot,项目名称:tenus,代码行数:33,代码来源:bridge_linux.go

示例11: GetTapFd

func GetTapFd(tapname, bridge string) (device string, tapFile *os.File, err error) {
	var (
		req   ifReq
		errno syscall.Errno
	)

	tapFile, err = os.OpenFile("/dev/net/tun", os.O_RDWR, 0)
	if err != nil {
		return "", nil, err
	}

	req.Flags = CIFF_TAP | CIFF_NO_PI | CIFF_ONE_QUEUE
	if tapname != "" {
		copy(req.Name[:len(req.Name)-1], []byte(tapname))
	}
	_, _, errno = syscall.Syscall(syscall.SYS_IOCTL, tapFile.Fd(),
		uintptr(syscall.TUNSETIFF),
		uintptr(unsafe.Pointer(&req)))
	if errno != 0 {
		err = fmt.Errorf("create tap device failed\n")
		tapFile.Close()
		return "", nil, err
	}

	device = strings.Trim(string(req.Name[:]), "\x00")

	tapIface, err := net.InterfaceByName(device)
	if err != nil {
		glog.Errorf("get interface by name %s failed %s", device, err)
		tapFile.Close()
		return "", nil, err
	}

	bIface, err := net.InterfaceByName(bridge)
	if err != nil {
		glog.Errorf("get interface by name %s failed", bridge)
		tapFile.Close()
		return "", nil, err
	}

	err = AddToBridge(tapIface, bIface)
	if err != nil {
		glog.Errorf("Add to bridge failed %s %s", bridge, device)
		tapFile.Close()
		return "", nil, err
	}

	err = NetworkLinkUp(tapIface)
	if err != nil {
		glog.Errorf("Link up device %s failed", device)
		tapFile.Close()
		return "", nil, err
	}

	return device, tapFile, nil

}
开发者ID:hyperhq,项目名称:runv,代码行数:57,代码来源:network_linux.go

示例12: initialize

func (v *veth) initialize(config *network) error {
	peer := config.TempVethPeerName
	if peer == "" {
		return fmt.Errorf("peer is not specified")
	}
	child, err := net.InterfaceByName(peer)
	if err != nil {
		return err
	}
	if err := netlink.NetworkLinkDown(child); err != nil {
		return err
	}
	if err := netlink.NetworkChangeName(child, config.Name); err != nil {
		return err
	}
	// get the interface again after we changed the name as the index also changes.
	if child, err = net.InterfaceByName(config.Name); err != nil {
		return err
	}
	if config.MacAddress != "" {
		if err := netlink.NetworkSetMacAddress(child, config.MacAddress); err != nil {
			return err
		}
	}
	ip, ipNet, err := net.ParseCIDR(config.Address)
	if err != nil {
		return err
	}
	if err := netlink.NetworkLinkAddIp(child, ip, ipNet); err != nil {
		return err
	}
	if config.IPv6Address != "" {
		if ip, ipNet, err = net.ParseCIDR(config.IPv6Address); err != nil {
			return err
		}
		if err := netlink.NetworkLinkAddIp(child, ip, ipNet); err != nil {
			return err
		}
	}
	if err := netlink.NetworkSetMTU(child, config.Mtu); err != nil {
		return err
	}
	if err := netlink.NetworkLinkUp(child); err != nil {
		return err
	}
	if config.Gateway != "" {
		if err := netlink.AddDefaultGw(config.Gateway, config.Name); err != nil {
			return err
		}
	}
	if config.IPv6Gateway != "" {
		if err := netlink.AddDefaultGw(config.IPv6Gateway, config.Name); err != nil {
			return err
		}
	}
	return nil
}
开发者ID:alena1108,项目名称:kubernetes,代码行数:57,代码来源:network_linux.go

示例13: ExternalIp

func ExternalIp(config *config.ClusterConfig) (string, string, error) {
	mgmtIp := ""
	dataIp := ""

	if config.MgtIface != "" {
		iface, err := net.InterfaceByName(config.MgtIface)
		if err != nil {
			return "", "", errors.New("Invalid management network " +
				"interface specified.")
		}
		mgmtIp, err = ifaceToIp(iface)
		if err != nil {
			return "", "", err
		}
	}

	if config.DataIface != "" {
		iface, err := net.InterfaceByName(config.DataIface)
		if err != nil {
			return "", "", errors.New("Invalid data network interface " +
				"specified.")
		}
		dataIp, err = ifaceToIp(iface)
		if err != nil {
			return "", "", err
		}
	}

	if mgmtIp != "" && dataIp != "" {
		return mgmtIp, dataIp, nil
	} else if mgmtIp != "" { // dataIp is empty
		return mgmtIp, mgmtIp, nil
	} else if dataIp != "" { // mgmtIp is empty
		return dataIp, dataIp, nil
	} // both are empty, try to pick first available interface for both

	// No network interface specified, pick first default.
	ifaces, err := net.Interfaces()
	if err != nil {
		return "", "", err
	}
	for _, iface := range ifaces {
		if iface.Flags&net.FlagUp == 0 {
			continue // interface down
		}
		if iface.Flags&net.FlagLoopback != 0 {
			continue // loopback interface
		}

		mgmtIp, err = ifaceToIp(&iface)
		return mgmtIp, mgmtIp, err
	}

	return "", "", errors.New("Node not connected to the network.")
}
开发者ID:ModelRocket,项目名称:openstorage,代码行数:55,代码来源:manager.go

示例14: Test_NewVethPairWithOptions

func Test_NewVethPairWithOptions(t *testing.T) {
	for _, tt := range vethOptionTests {
		tl := &testLink{}

		if err := tl.prepTestLink(tt.hostIfc, ""); err != nil {
			t.Skipf("NewVlanLink test requries external command: %v", err)
		}

		veth, err := NewVethPairWithOptions(tt.hostIfc, tt.vethOptions)
		if err != nil {
			t.Fatalf("NewVethPairWithOptions(%s, %v) failed to run: %s", tt.hostIfc, tt.vethOptions, err)
		}

		if _, err := net.InterfaceByName(tt.hostIfc); err != nil {
			t.Fatalf("Could not find %s on the host: %s", tt.hostIfc, err)
		}

		if _, err := net.InterfaceByName(tt.vethOptions.PeerName); err != nil {
			t.Fatalf("Could not find %s on the host: %s", tt.vethOptions.PeerName, err)
		}

		vethIfcName := veth.NetInterface().Name
		if vethIfcName != tt.hostIfc {
			tl.teardown()
			t.Fatalf("NewVethPairWithOptions(%s, %v) failed: expected host ifc %s, returned %s",
				tt.hostIfc, tt.vethOptions, tt.hostIfc, vethIfcName)
		}

		vethPeerName := veth.PeerNetInterface().Name
		if vethPeerName != tt.vethOptions.PeerName {
			tl.teardown()
			t.Fatalf("NewVethPairWithOptions(%s, %v) failed: expected peer ifc %s, returned %s",
				tt.hostIfc, tt.vethOptions, tt.vethOptions.PeerName, vethPeerName)
		}

		testRes, err := linkInfo(tt.hostIfc, "veth")
		if err != nil {
			tl.teardown()
			t.Fatalf("Failed to list %s operation mode: %s", tt.hostIfc, err)
		}

		if testRes.linkType != "veth" {
			tl.teardown()
			t.Fatalf("NewVethPairWithOptions(%s, %v) failed: expected linktype veth, returned %s",
				tt.hostIfc, tt.vethOptions, testRes.linkType)
		}

		if err := tl.teardown(); err != nil {
			t.Fatalf("testLink.teardown failed: %v", err)
		} else {
			time.Sleep(10 * time.Millisecond)
		}
	}
}
开发者ID:drptbl,项目名称:oz,代码行数:54,代码来源:veth_linux_test.go

示例15: SetInterfaceMaster

func SetInterfaceMaster(name, master string) error {
	iface, err := net.InterfaceByName(name)
	if err != nil {
		return err
	}
	masterIface, err := net.InterfaceByName(master)
	if err != nil {
		return err
	}
	return netlink.NetworkSetMaster(iface, masterIface)
}
开发者ID:jpgbus,项目名称:docker,代码行数:11,代码来源:network.go


注:本文中的net.InterfaceByName函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。