本文整理汇总了Golang中github.com/vishvananda/netlink.LinkSetDown函数的典型用法代码示例。如果您正苦于以下问题:Golang LinkSetDown函数的具体用法?Golang LinkSetDown怎么用?Golang LinkSetDown使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了LinkSetDown函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: releaseVF
func releaseVF(conf *NetConf, ifName string, initns ns.NetNS) error {
// get VF device
vfDev, err := netlink.LinkByName(ifName)
if err != nil {
return fmt.Errorf("failed to lookup vf %d device %q: %v", conf.VF, ifName, err)
}
// device name in init netns
index := vfDev.Attrs().Index
devName := fmt.Sprintf("dev%d", index)
// shutdown VF device
if err = netlink.LinkSetDown(vfDev); err != nil {
return fmt.Errorf("failed to down vf % device: %v", conf.VF, err)
}
// rename VF device
err = renameLink(ifName, devName)
if err != nil {
return fmt.Errorf("failed to rename vf %d evice %q to %q: %v", conf.VF, ifName, devName, err)
}
// move VF device to init netns
if err = netlink.LinkSetNsFd(vfDev, int(initns.Fd())); err != nil {
return fmt.Errorf("failed to move vf %d to init netns: %v", conf.VF, err)
}
return nil
}
示例2: AddSlave
func (b *Bonding) AddSlave(slave string) {
if b.err != nil {
return
}
if ok, err := contains(base+b.name+"/bonding/slaves", slave); err != nil {
b.err = err
return
} else if ok {
return
}
link, err := netlink.LinkByName(slave)
if err != nil {
b.err = err
return
}
b.err = netlink.LinkSetDown(link)
if b.err != nil {
return
}
p := base + b.name + "/bonding/slaves"
logrus.Infof("Adding slave %s to master %s", slave, b.name)
b.err = ioutil.WriteFile(p, []byte("+"+slave), 0644)
}
示例3: AddInterface
func (n *networkNamespace) AddInterface(i *Interface) error {
n.Lock()
i.DstName = fmt.Sprintf("%s%d", i.DstName, n.nextIfIndex)
n.nextIfIndex++
n.Unlock()
runtime.LockOSThread()
defer runtime.UnlockOSThread()
origns, err := netns.Get()
if err != nil {
return err
}
defer origns.Close()
f, err := os.OpenFile(n.path, os.O_RDONLY, 0)
if err != nil {
return fmt.Errorf("failed get network namespace %q: %v", n.path, err)
}
defer f.Close()
// Find the network interface identified by the SrcName attribute.
iface, err := netlink.LinkByName(i.SrcName)
if err != nil {
return err
}
// Move the network interface to the destination namespace.
nsFD := f.Fd()
if err := netlink.LinkSetNsFd(iface, int(nsFD)); err != nil {
return err
}
if err = netns.Set(netns.NsHandle(nsFD)); err != nil {
return err
}
defer netns.Set(origns)
// Down the interface before configuring
if err := netlink.LinkSetDown(iface); err != nil {
return err
}
// Configure the interface now this is moved in the proper namespace.
if err := configureInterface(iface, i); err != nil {
return err
}
// Up the interface.
if err := netlink.LinkSetUp(iface); err != nil {
return err
}
n.Lock()
n.sinfo.Interfaces = append(n.sinfo.Interfaces, i)
n.Unlock()
return nil
}
示例4: linkDown
func (b *Bonding) linkDown() error {
link, err := netlink.LinkByName(b.name)
if err != nil {
return err
}
return netlink.LinkSetDown(link)
}
示例5: RemoveInterface
func (n *networkNamespace) RemoveInterface(i *Interface) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
origns, err := netns.Get()
if err != nil {
return err
}
defer origns.Close()
f, err := os.OpenFile(n.path, os.O_RDONLY, 0)
if err != nil {
return fmt.Errorf("failed get network namespace %q: %v", n.path, err)
}
defer f.Close()
nsFD := f.Fd()
if err = netns.Set(netns.NsHandle(nsFD)); err != nil {
return err
}
defer netns.Set(origns)
// Find the network inteerface identified by the DstName attribute.
iface, err := netlink.LinkByName(i.DstName)
if err != nil {
return err
}
// Down the interface before configuring
if err := netlink.LinkSetDown(iface); err != nil {
return err
}
err = netlink.LinkSetName(iface, i.SrcName)
if err != nil {
fmt.Println("LinkSetName failed: ", err)
return err
}
// Move the network interface to caller namespace.
if err := netlink.LinkSetNsFd(iface, int(origns)); err != nil {
fmt.Println("LinkSetNsPid failed: ", err)
return err
}
n.Lock()
for index, intf := range n.sinfo.Interfaces {
if intf == i {
n.sinfo.Interfaces = append(n.sinfo.Interfaces[:index], n.sinfo.Interfaces[index+1:]...)
break
}
}
n.Unlock()
return nil
}
示例6: deleteLink
func (n *networks) deleteLink(config networkConfig) error {
if li, err := netlink.LinkByName(config.BridgeName); err == nil {
if err := netlink.LinkSetDown(li); err != nil {
return ErrNetlinkError{"bring bridge down", err}
}
if err := netlink.LinkDel(li); err != nil {
return ErrNetlinkError{"delete bridge", err}
}
}
if li, err := netlink.LinkByName(config.LinkName); err == nil {
if err := netlink.LinkSetDown(li); err != nil {
return ErrNetlinkError{"bring vlan down", err}
}
if err := netlink.LinkDel(li); err != nil {
return ErrNetlinkError{"delete vlan", err}
}
}
return nil
}
示例7: Remove
func (i *nwIface) Remove() error {
i.Lock()
n := i.ns
i.Unlock()
n.Lock()
path := n.path
isDefault := n.isDefault
n.Unlock()
return nsInvoke(path, func(nsFD int) error { return nil }, func(callerFD int) error {
// Find the network inteerface identified by the DstName attribute.
iface, err := netlink.LinkByName(i.DstName())
if err != nil {
return err
}
// Down the interface before configuring
if err := netlink.LinkSetDown(iface); err != nil {
return err
}
err = netlink.LinkSetName(iface, i.SrcName())
if err != nil {
fmt.Println("LinkSetName failed: ", err)
return err
}
// if it is a bridge just delete it.
if i.Bridge() {
if err := netlink.LinkDel(iface); err != nil {
return fmt.Errorf("failed deleting bridge %q: %v", i.SrcName(), err)
}
} else if !isDefault {
// Move the network interface to caller namespace.
if err := netlink.LinkSetNsFd(iface, callerFD); err != nil {
fmt.Println("LinkSetNsPid failed: ", err)
return err
}
}
n.Lock()
for index, intf := range n.iFaces {
if intf == i {
n.iFaces = append(n.iFaces[:index], n.iFaces[index+1:]...)
break
}
}
n.Unlock()
return nil
})
}
示例8: 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
}
示例9: NetIfaceDown
func NetIfaceDown(cResp chan<- *Response, rawArgs *json.RawMessage, tag string) {
args := &struct {
Ifname string `json:"ifname"`
}{}
json.Unmarshal(*rawArgs, &args)
iface := &netlink.Device{netlink.LinkAttrs{Name: args.Ifname}}
if err := netlink.LinkSetDown(iface); err != nil {
cResp <- &Response{nil, tag, NewRTNetlinkError(err)}
return
}
cResp <- &Response{true, tag, nil}
}
示例10: bridgeDestroy
func bridgeDestroy() {
bridgeInt, err := netlink.LinkByName("mycbridge")
if err != nil {
panic(err)
}
err = netlink.LinkSetDown(bridgeInt)
if err != nil {
panic(err)
}
err = netlink.LinkDel(bridgeInt)
if err != nil {
panic(err)
}
}
示例11: deleteBridgeAddr
func deleteBridgeAddr(br *netlink.Bridge, ipn *net.IPNet) error {
addr := &netlink.Addr{IPNet: ipn, Label: ""}
if err := netlink.LinkSetDown(br); err != nil {
return fmt.Errorf("could not set down bridge %q: %v", br.Name, err)
}
if err := netlink.AddrDel(br, addr); err != nil {
return fmt.Errorf("could not remove IP address from %q: %v", br.Name, err)
}
if err := netlink.LinkSetUp(br); err != nil {
return fmt.Errorf("could not set up bridge %q: %v", br.Name, err)
}
return nil
}
示例12: cmdDel
func cmdDel(args *skel.CmdArgs) error {
args.IfName = "lo" // ignore config, this only works for loopback
err := ns.WithNetNSPath(args.Netns, func(ns.NetNS) error {
link, err := netlink.LinkByName(args.IfName)
if err != nil {
return err // not tested
}
err = netlink.LinkSetDown(link)
if err != nil {
return err // not tested
}
return nil
})
if err != nil {
return err // not tested
}
return nil
}
示例13: routingDown
func routingDown() {
veth, err := netlink.LinkByName("myveth1")
if err != nil {
panic(err)
}
routes := createRoutes(veth)
for _, route := range routes {
fmt.Println("Removing route", route)
err := netlink.RouteDel(route)
if err != nil {
// panic(err)
fmt.Println(err)
}
}
err = netlink.LinkSetDown(veth)
if err != nil {
panic(err)
}
}
示例14: collectionInterfaceInfo
func collectionInterfaceInfo() []supervisor.InterfaceInfo {
infos := []supervisor.InterfaceInfo{}
links, err := netlink.LinkList()
if err != nil {
glog.Error(err)
return infos
}
for _, link := range links {
if link.Type() != "veth" {
// lo is here too
continue
}
addrs, err := netlink.AddrList(link, netlink.FAMILY_V4)
if err != nil {
glog.Error(err)
return infos
}
for _, addr := range addrs {
info := supervisor.InterfaceInfo{
Ip: addr.IPNet.String(),
Index: link.Attrs().Index,
PeerIndex: link.Attrs().ParentIndex,
}
glog.Infof("get interface %v", info)
infos = append(infos, info)
}
// set link down, tap device take over it
netlink.LinkSetDown(link)
}
return infos
}
示例15: configureInterface
// configureInterface configures the network interface in the network namespace.
func configureInterface(name string, pid int, addr *net.IPNet, gatewayIP string) error {
// Lock the OS Thread so we don't accidentally switch namespaces
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// Save the current network namespace
origns, err := netns.Get()
if err != nil {
return fmt.Errorf("Getting current network namespace failed: %v", err)
}
defer origns.Close()
// Get the namespace
newns, err := netns.GetFromPid(pid)
if err != nil {
return fmt.Errorf("Getting network namespace for pid %d failed: %v", pid, err)
}
defer newns.Close()
// Enter the namespace
if err := netns.Set(newns); err != nil {
return fmt.Errorf("Entering network namespace failed: %v", err)
}
// Find the network interface identified by the name
iface, err := netlink.LinkByName(name)
if err != nil {
return fmt.Errorf("Getting link by name %s failed: %v", name, err)
}
// Bring the interface down
if err := netlink.LinkSetDown(iface); err != nil {
return fmt.Errorf("Bringing interface [ %#v ] down failed: %v", iface, err)
}
// Change the interface name to eth0 in the namespace
if err := netlink.LinkSetName(iface, containerInterface); err != nil {
return fmt.Errorf("Renaming interface %s to %s failed: %v", name, defaultContainerInterface, err)
}
// Add the IP address
ipAddr := &netlink.Addr{IPNet: addr, Label: ""}
if err := netlink.AddrAdd(iface, ipAddr); err != nil {
return fmt.Errorf("Setting interface %s ip to %s failed: %v", name, addr.String(), err)
}
// Bring the interface up
if err := netlink.LinkSetUp(iface); err != nil {
return fmt.Errorf("Bringing interface [ %#v ] up failed: %v", iface, err)
}
// Add the gateway route
gw := net.ParseIP(gatewayIP)
err = netlink.RouteAdd(&netlink.Route{
Scope: netlink.SCOPE_UNIVERSE,
LinkIndex: iface.Attrs().Index,
Gw: gw,
})
if err != nil {
return fmt.Errorf("Adding route %s to interface %s failed: %v", gw.String(), name, err)
}
// Switch back to the original namespace
if err := netns.Set(origns); err != nil {
return fmt.Errorf("Switching back to original namespace failed: %v", err)
}
return nil
}