本文整理汇总了Golang中github.com/vishvananda/netlink.LinkSetUp函数的典型用法代码示例。如果您正苦于以下问题:Golang LinkSetUp函数的具体用法?Golang LinkSetUp怎么用?Golang LinkSetUp使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了LinkSetUp函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: SetupVeth
// SetupVeth sets up a virtual ethernet link.
// Should be in container netns.
func SetupVeth(contVethName string, mtu int, hostNS *os.File) (hostVeth, contVeth netlink.Link, err error) {
var hostVethName string
hostVethName, contVeth, err = makeVeth(contVethName, mtu)
if err != nil {
return
}
if err = netlink.LinkSetUp(contVeth); err != nil {
err = fmt.Errorf("failed to set %q up: %v", contVethName, err)
return
}
hostVeth, err = netlink.LinkByName(hostVethName)
if err != nil {
err = fmt.Errorf("failed to lookup %q: %v", hostVethName, err)
return
}
if err = netlink.LinkSetUp(hostVeth); err != nil {
err = fmt.Errorf("failed to set %q up: %v", contVethName, err)
return
}
if err = netlink.LinkSetNsFd(hostVeth, int(hostNS.Fd())); err != nil {
err = fmt.Errorf("failed to move veth to host netns: %v", err)
return
}
return
}
示例3: wireTapInterface
// Wire up a tap interface for communicating with the guest. Returns the name
// of the created tap interface.
func wireTapInterface(config *netConfig) string {
// Drop link on eth0 before configuring anything
eth0, err := netlink.LinkByName("eth0")
if err != nil {
log.Fatalf("LinkByName(eth0): %v", err)
}
if err := netlink.LinkSetDown(eth0); err != nil {
log.Fatalf("LinkSetDown(eth0): %v", err)
}
// Flush any L3 addresses on eth0
if err := flushAddresses(eth0); err != nil {
log.Fatalf("flushAddresses(eth0): %v", err)
}
// Generate and set random MAC address for eth0
eth0Addr := generateHardwareAddr()
if err := netlink.LinkSetHardwareAddr(eth0, eth0Addr); err != nil {
log.Fatalf("LinkSetHardwareAddr(eth0): %v", err)
}
// Create "tap0" (interface to guest)
tap0Attrs := netlink.NewLinkAttrs()
tap0Attrs.Name = "tap0"
tap0 := &netlink.Tuntap{tap0Attrs, netlink.TUNTAP_MODE_TAP}
if err := netlink.LinkAdd(tap0); err != nil {
log.Fatalf("LinkAdd(tap0): %v", err)
}
// Create a new bridge, br0 and add eth0 and tap0 to it
br0Attrs := netlink.NewLinkAttrs()
br0Attrs.Name = "br0"
br0 := &netlink.Bridge{br0Attrs}
if err := netlink.LinkAdd(br0); err != nil {
log.Fatalf("LinkAdd(br0): %v", err)
}
if err := netlink.LinkSetMaster(eth0, br0); err != nil {
log.Fatalf("LinkSetMaster(eth0, br0): %v", err)
}
if err := netlink.LinkSetMaster(tap0, br0); err != nil {
log.Fatalf("LinkSetMaster(tap0, br0): %v", err)
}
// Set all links up
if err := netlink.LinkSetUp(tap0); err != nil {
log.Fatalf("LinkSetUp(tap0): %v", err)
}
if err := netlink.LinkSetUp(eth0); err != nil {
log.Fatalf("LinkSetUp(eth0): %v", err)
}
if err := netlink.LinkSetUp(br0); err != nil {
log.Fatalf("LinkSetUp(br0): %v", err)
}
return tap0Attrs.Name
}
示例4: routingUp
func routingUp() {
veth, err := netlink.LinkByName("myveth1")
if err != nil {
panic(err)
}
err = netlink.LinkSetUp(veth)
if err != nil {
panic(err)
}
addr, _ := netlink.ParseAddr("172.19.80.2/24")
err = netlink.AddrAdd(veth, addr)
if err != nil {
panic(err)
}
routes := createRoutes(veth)
for _, route := range routes {
fmt.Println("Adding route", route)
err := netlink.RouteAdd(route)
if err != nil {
fmt.Println(err)
// panic(err)
}
}
}
示例5: endpointCreate
func endpointCreate() {
hostIfName := "myveth0"
containerIfName := "myveth1"
veth, err := netlink.LinkByName("myveth0")
if err == nil && veth != nil {
return
}
veth = &netlink.Veth{
LinkAttrs: netlink.LinkAttrs{Name: hostIfName, TxQLen: 0},
PeerName: containerIfName}
if err := netlink.LinkAdd(veth); err != nil {
panic(err)
}
la := netlink.NewLinkAttrs()
la.Name = "mycbridge"
mybridge := &netlink.Bridge{la}
err = netlink.LinkSetMaster(veth, mybridge)
if err != nil {
panic(err)
}
err = netlink.LinkSetUp(veth)
if err != nil {
panic(err)
}
}
示例6: configureIface
func configureIface(ifname string, ipn ip.IP4Net, mtu int) error {
iface, err := netlink.LinkByName(ifname)
if err != nil {
return fmt.Errorf("failed to lookup interface %v", ifname)
}
err = netlink.AddrAdd(iface, &netlink.Addr{IPNet: ipn.ToIPNet(), Label: ""})
if err != nil {
return fmt.Errorf("failed to add IP address %v to %v: %v", ipn.String(), ifname, err)
}
err = netlink.LinkSetMTU(iface, mtu)
if err != nil {
return fmt.Errorf("failed to set MTU for %v: %v", ifname, err)
}
err = netlink.LinkSetUp(iface)
if err != nil {
return fmt.Errorf("failed to set interface %v to UP state: %v", ifname, err)
}
// explicitly add a route since there might be a route for a subnet already
// installed by Docker and then it won't get auto added
err = netlink.RouteAdd(&netlink.Route{
LinkIndex: iface.Attrs().Index,
Scope: netlink.SCOPE_UNIVERSE,
Dst: ipn.Network().ToIPNet(),
})
if err != nil && err != syscall.EEXIST {
return fmt.Errorf("failed to add route (%v -> %v): %v", ipn.Network().String(), ifname, err)
}
return nil
}
示例7: Join
func (d *Driver) Join(r *dknet.JoinRequest) (*dknet.JoinResponse, error) {
// create and attach local name to the bridge
localVethPair := vethPair(truncateID(r.EndpointID))
if err := netlink.LinkAdd(localVethPair); err != nil {
log.Errorf("failed to create the veth pair named: [ %v ] error: [ %s ] ", localVethPair, err)
return nil, err
}
// Bring the veth pair up
err := netlink.LinkSetUp(localVethPair)
if err != nil {
log.Warnf("Error enabling Veth local iface: [ %v ]", localVethPair)
return nil, err
}
bridgeName := d.networks[r.NetworkID].BridgeName
err = d.addOvsVethPort(bridgeName, localVethPair.Name, 0)
if err != nil {
log.Errorf("error attaching veth [ %s ] to bridge [ %s ]", localVethPair.Name, bridgeName)
return nil, err
}
log.Infof("Attached veth [ %s ] to bridge [ %s ]", localVethPair.Name, bridgeName)
// SrcName gets renamed to DstPrefix + ID on the container iface
res := &dknet.JoinResponse{
InterfaceName: dknet.InterfaceName{
SrcName: localVethPair.PeerName,
DstPrefix: containerEthName,
},
Gateway: d.networks[r.NetworkID].Gateway,
}
log.Debugf("Join endpoint %s:%s to %s", r.NetworkID, r.EndpointID, r.SandboxKey)
return res, nil
}
示例8: addUplink
// addUplink adds a dummy uplink to ofnet agent
func addUplink(ofa *OfnetAgent, linkName string, ofpPortNo uint32) (*netlink.Veth, error) {
link := &netlink.Veth{
LinkAttrs: netlink.LinkAttrs{
Name: linkName,
TxQLen: 100,
MTU: 1400,
},
PeerName: linkName + "peer",
}
// delete old link if it exists.. and ignore error
netlink.LinkDel(link)
time.Sleep(100 * time.Millisecond)
if err := netlink.LinkAdd(link); err != nil {
return nil, err
}
// add it to ofnet
err := ofa.AddUplink(ofpPortNo, linkName)
if err != nil {
return nil, err
}
time.Sleep(time.Second)
// mark the link as up
if err := netlink.LinkSetUp(link); err != nil {
return nil, err
}
return link, nil
}
示例9: configureIface
// ConfigureIface takes the result of IPAM plugin and
// applies to the ifName interface
func configureIface(conf *NetConf) error {
ifName := conf.IfName
link, err := netlink.LinkByName(ifName)
if err != nil {
return fmt.Errorf("failed to lookup %q: %v", ifName, err)
}
if err := netlink.LinkSetUp(link); err != nil {
return fmt.Errorf("failed to set %q UP: %v", ifName, err)
}
i, n, err := net.ParseCIDR(conf.IPAddr)
if err != nil {
return fmt.Errorf("failed to parse ip address :%s", conf.IPAddr)
}
addr := &net.IPNet{IP: i, Mask: n.Mask}
nlAddr := &netlink.Addr{IPNet: addr, Label: ""}
if err = netlink.AddrAdd(link, nlAddr); err != nil {
return fmt.Errorf("failed to add IP addr to %q: %v", ifName, err)
}
gw := net.ParseIP(conf.Gateway)
if gw == nil {
return fmt.Errorf("parse gateway: %s return nil", conf.Gateway)
}
if err = ip.AddDefaultRoute(gw, link); err != nil {
// we skip over duplicate routes as we assume the first one wins
if !os.IsExist(err) {
return fmt.Errorf("failed to add default route via %v dev %v: %v", gw, ifName, err)
}
}
return nil
}
示例10: createInterface
func createInterface(t *testing.T, name string, nws ...string) {
// Add interface
link := &netlink.Bridge{
LinkAttrs: netlink.LinkAttrs{
Name: "test",
},
}
bips := []*net.IPNet{}
for _, nw := range nws {
bip, err := types.ParseCIDR(nw)
if err != nil {
t.Fatal(err)
}
bips = append(bips, bip)
}
if err := netlink.LinkAdd(link); err != nil {
t.Fatalf("Failed to create interface via netlink: %v", err)
}
for _, bip := range bips {
if err := netlink.AddrAdd(link, &netlink.Addr{IPNet: bip}); err != nil {
t.Fatal(err)
}
}
if err := netlink.LinkSetUp(link); err != nil {
t.Fatal(err)
}
}
示例11: 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
}
示例12: loopbackUp
func loopbackUp() error {
iface, err := netlink.LinkByName("lo")
if err != nil {
return err
}
return netlink.LinkSetUp(iface)
}
示例13: 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)
}
示例14: acquire
func (l *DHCPLease) acquire() error {
c, err := newDHCPClient(l.link)
if err != nil {
return err
}
defer c.Close()
if (l.link.Attrs().Flags & net.FlagUp) != net.FlagUp {
log.Printf("Link %q down. Attempting to set up", l.link.Attrs().Name)
if err = netlink.LinkSetUp(l.link); err != nil {
return err
}
}
pkt, err := backoffRetry(func() (*dhcp4.Packet, error) {
ok, ack, err := c.Request()
switch {
case err != nil:
return nil, err
case !ok:
return nil, fmt.Errorf("DHCP server NACK'd own offer")
default:
return &ack, nil
}
})
if err != nil {
return err
}
return l.commit(pkt)
}
示例15: ensureBridge
func ensureBridge(brName string, mtu int) (*netlink.Bridge, error) {
br := &netlink.Bridge{
LinkAttrs: netlink.LinkAttrs{
Name: brName,
MTU: mtu,
},
}
if err := netlink.LinkAdd(br); err != nil {
if err != syscall.EEXIST {
return nil, fmt.Errorf("could not add %q: %v", brName, err)
}
// it's ok if the device already exists as long as config is similar
br, err = bridgeByName(brName)
if err != nil {
return nil, err
}
}
if err := netlink.LinkSetUp(br); err != nil {
return nil, err
}
return br, nil
}