本文整理汇总了Golang中github.com/vishvananda/netlink.LinkByIndex函数的典型用法代码示例。如果您正苦于以下问题:Golang LinkByIndex函数的具体用法?Golang LinkByIndex怎么用?Golang LinkByIndex使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了LinkByIndex函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: resourceLXCBridgeDelete
func resourceLXCBridgeDelete(d *schema.ResourceData, meta interface{}) error {
bridgeIndex, err := strconv.Atoi(d.Id())
if err != nil {
return fmt.Errorf("Internal error reading resource ID: %v", err)
}
bridge, err := netlink.LinkByIndex(bridgeIndex)
if err != nil {
return fmt.Errorf("Unable to find bridge %v: %v", bridgeIndex, err)
}
links, err := netlink.LinkList()
if err != nil {
return fmt.Errorf("Error listing interfaces: %v", err)
}
bridgeEmpty := true
for _, link := range links {
if link.Attrs().MasterIndex == bridge.Attrs().Index {
bridgeEmpty = false
log.Printf("[INFO] Link %s is still attached to bridge %s", link.Attrs().Name, bridge.Attrs().Name)
}
}
if bridgeEmpty == false {
return fmt.Errorf("Unable to delete bridge %s. Interfaces are still attached to it.", bridge.Attrs().Name)
} else {
if err := netlink.LinkDel(bridge); err != nil {
return fmt.Errorf("Error deleting bridge: %s", err)
}
}
return nil
}
示例2: testAccCheckLXCBridgeExists
func testAccCheckLXCBridgeExists(t *testing.T, n string, bridge *netlink.Link) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %v", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}
bridgeIndex, err := strconv.Atoi(rs.Primary.ID)
if err != nil {
return fmt.Errorf("Internal error reading resource ID.")
}
br, err := netlink.LinkByIndex(bridgeIndex)
if err != nil {
return fmt.Errorf("Error searching for bridge.")
} else {
*bridge = br
return nil
}
return fmt.Errorf("Unable to find bridge.")
}
}
示例3: gatherLinks
// gatherLinks listens for netlink notifications and collects the list of links
// created during the subscription window.
func gatherLinks(ch <-chan netlink.LinkUpdate) ([]netlink.Link, error) {
linkSet := make(map[int32]bool)
timeout := time.After(1000 * time.Millisecond)
Debug.Printf("Waiting for link updates\n")
outer:
for {
select {
case update := <-ch:
if update.Header.Type == syscall.RTM_NEWLINK {
if _, ok := update.Link.(*netlink.Veth); ok {
linkSet[update.Index] = true
}
} else if update.Header.Type == syscall.RTM_DELLINK {
delete(linkSet, update.Index)
}
case <-timeout:
break outer
}
}
links := []netlink.Link{}
for index, _ := range linkSet {
l, err := netlink.LinkByIndex(int(index))
if err == nil {
links = append(links, l)
}
}
return links, nil
}
示例4: ensureLink
func ensureLink(vxlan *netlink.Vxlan) (*netlink.Vxlan, error) {
err := netlink.LinkAdd(vxlan)
if err == syscall.EEXIST {
// it's ok if the device already exists as long as config is similar
existing, err := netlink.LinkByName(vxlan.Name)
if err != nil {
return nil, err
}
incompat := vxlanLinksIncompat(vxlan, existing)
if incompat == "" {
return existing.(*netlink.Vxlan), nil
}
// delete existing
log.Warningf("%q already exists with incompatable configuration: %v; recreating device", vxlan.Name, incompat)
if err = netlink.LinkDel(existing); err != nil {
return nil, fmt.Errorf("failed to delete interface: %v", err)
}
// create new
if err = netlink.LinkAdd(vxlan); err != nil {
return nil, fmt.Errorf("failed to create vxlan interface: %v", err)
}
} else if err != nil {
return nil, err
}
ifindex := vxlan.Index
link, err := netlink.LinkByIndex(vxlan.Index)
if err != nil {
return nil, fmt.Errorf("can't locate created vxlan device with index %v", ifindex)
}
var ok bool
if vxlan, ok = link.(*netlink.Vxlan); !ok {
return nil, fmt.Errorf("created vxlan device with index %v is not vxlan", ifindex)
}
return vxlan, nil
}
示例5: onLinkAdded
func (u *NetLinkProbe) onLinkAdded(index int) {
link, err := netlink.LinkByIndex(index)
if err != nil {
logging.GetLogger().Warningf("Failed to find interface %d: %s", index, err.Error())
return
}
u.addLinkToTopology(link)
}
示例6: GetBridgeFromIndex
func GetBridgeFromIndex(idx int) (string, error) {
var attr, bridge *netlink.LinkAttrs
links, err := netlink.LinkList()
if err != nil {
glog.Error(err)
return "", err
}
for _, link := range links {
if link.Type() != "veth" {
continue
}
if link.Attrs().Index == idx {
attr = link.Attrs()
break
}
}
if attr == nil {
return "", fmt.Errorf("cann't find nic whose ifindex is %d", idx)
}
for _, link := range links {
if link.Type() != "bridge" && link.Type() != "openvswitch" {
continue
}
if link.Attrs().Index == attr.MasterIndex {
bridge = link.Attrs()
break
}
}
if bridge == nil {
return "", fmt.Errorf("cann't find bridge contains nic whose ifindex is %d", idx)
}
if bridge.Name == "ovs-system" {
veth, err := netlink.LinkByIndex(idx)
if err != nil {
return "", err
}
out, err := exec.Command("ovs-vsctl", "port-to-br", veth.Attrs().Name).CombinedOutput()
if err != nil {
return "", err
}
bridge.Name = strings.TrimSpace(string(out))
}
glog.Infof("find bridge %s", bridge.Name)
return bridge.Name, nil
}
示例7: addMacvlan
// Adds a macvlan interface to a container for use with the egress router feature
func addMacvlan(netns string) error {
var defIface netlink.Link
var err error
// Find interface with the default route
routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
if err != nil {
return fmt.Errorf("failed to read routes: %v", err)
}
for _, r := range routes {
if r.Dst == nil {
defIface, err = netlink.LinkByIndex(r.LinkIndex)
if err != nil {
return fmt.Errorf("failed to get default route interface: %v", err)
}
}
}
if defIface == nil {
return fmt.Errorf("failed to find default route interface")
}
podNs, err := ns.GetNS(netns)
if err != nil {
return fmt.Errorf("could not open netns %q", netns)
}
defer podNs.Close()
err = netlink.LinkAdd(&netlink.Macvlan{
LinkAttrs: netlink.LinkAttrs{
MTU: defIface.Attrs().MTU,
Name: "macvlan0",
ParentIndex: defIface.Attrs().Index,
Namespace: netlink.NsFd(podNs.Fd()),
},
Mode: netlink.MACVLAN_MODE_PRIVATE,
})
if err != nil {
return fmt.Errorf("failed to create macvlan interface: %v", err)
}
return podNs.Do(func(netns ns.NetNS) error {
l, err := netlink.LinkByName("macvlan0")
if err != nil {
return fmt.Errorf("failed to find macvlan interface: %v", err)
}
err = netlink.LinkSetUp(l)
if err != nil {
return fmt.Errorf("failed to set macvlan interface up: %v", err)
}
return nil
})
}
示例8: onLinkDeleted
func (u *NetLinkProbe) onLinkDeleted(index int) {
logging.GetLogger().Debugf("Link %d deleted", index)
u.Graph.Lock()
defer u.Graph.Unlock()
var intf *graph.Node
intfs := u.Graph.LookupNodes(graph.Metadata{"IfIndex": int64(index)})
switch l := len(intfs); {
case l == 1:
intf = intfs[0]
case l > 1:
Loop:
for _, i := range intfs {
parents := u.Graph.LookupParentNodes(i, nil)
for _, parent := range parents {
if parent.ID == u.Root.ID {
intf = i
break Loop
}
}
}
}
// case of removing the interface from a bridge
if intf != nil {
parents := u.Graph.LookupParentNodes(intf, graph.Metadata{"Type": "bridge"})
for _, parent := range parents {
u.Graph.Unlink(parent, intf)
}
}
// check wheter the interface has been deleted or not
// we get a delete event when an interace is removed from a bridge
_, err := netlink.LinkByIndex(index)
if err != nil && intf != nil {
if driver, ok := intf.Metadata()["Driver"]; ok {
// if openvswitch do not remove let's do the job by ovs piece of code
if driver == "openvswitch" {
u.Graph.Unlink(u.Root, intf)
} else {
u.Graph.DelNode(intf)
}
}
}
delete(u.indexToChildrenQueue, int64(index))
}
示例9: matchRoute
func matchRoute(ifaceName string, dest net.IP) func(m syscall.NetlinkMessage) (bool, error) {
return func(m syscall.NetlinkMessage) (bool, error) {
switch m.Header.Type {
case syscall.RTM_NEWROUTE:
route, err := deserializeRoute(m.Data)
if err != nil {
return true, err
}
link, _ := netlink.LinkByIndex(route.LinkIndex)
if link.Attrs().Name == ifaceName && route.Dst.IP.Equal(dest) {
return true, nil
}
}
return false, nil
}
}
示例10: resourceLXCBridgeRead
func resourceLXCBridgeRead(d *schema.ResourceData, meta interface{}) error {
bridgeIndex, err := strconv.Atoi(d.Id())
if err != nil {
return fmt.Errorf("Internal error reading resource ID: %v", err)
}
bridge, err := netlink.LinkByIndex(bridgeIndex)
if err != nil {
return fmt.Errorf("Unable to find bridge %v: %v", bridgeIndex, err)
}
d.Set("mac", bridge.Attrs().HardwareAddr.String())
log.Printf("[INFO] Bridge info: %v", bridge)
return nil
}
示例11: forEachRoute
func forEachRoute(ignoreIfaceNames map[string]struct{}, check func(netlink.Route) error) error {
routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
if err != nil {
return err
}
for _, route := range routes {
if link, err := netlink.LinkByIndex(route.LinkIndex); err == nil {
if _, found := ignoreIfaceNames[link.Attrs().Name]; found {
continue
}
}
if err := check(route); err != nil {
return err
}
}
return nil
}
示例12: addMacvlan
// Adds a macvlan interface to a container for use with the egress router feature
func addMacvlan(netns string) error {
var defIface netlink.Link
var err error
// Find interface with the default route
routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
if err != nil {
return fmt.Errorf("failed to read routes: %v", err)
}
for _, r := range routes {
if r.Dst == nil {
defIface, err = netlink.LinkByIndex(r.LinkIndex)
if err != nil {
return fmt.Errorf("failed to get default route interface: %v", err)
}
}
}
if defIface == nil {
return fmt.Errorf("failed to find default route interface")
}
return ns.WithNetNSPath(netns, func(ns.NetNS) error {
err := netlink.LinkAdd(&netlink.Macvlan{
LinkAttrs: netlink.LinkAttrs{
MTU: defIface.Attrs().MTU,
Name: "macvlan0",
ParentIndex: defIface.Attrs().Index,
},
Mode: netlink.MACVLAN_MODE_PRIVATE,
})
if err != nil {
return fmt.Errorf("failed to create macvlan interface: %v", err)
}
l, err := netlink.LinkByName("macvlan0")
if err != nil {
return fmt.Errorf("failed to find macvlan interface: %v", err)
}
err = netlink.LinkSetUp(l)
if err != nil {
return fmt.Errorf("failed to set macvlan interface up: %v", err)
}
return nil
})
}
示例13: testAccCheckLXCBridgeDestroy
func testAccCheckLXCBridgeDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "lxc_bridge" {
continue
}
bridgeIndex, err := strconv.Atoi(rs.Primary.ID)
if err != nil {
return fmt.Errorf("Internal error reading resource ID.")
}
_, err = netlink.LinkByIndex(bridgeIndex)
if err == nil {
return fmt.Errorf("Bridge still exists.")
}
}
return nil
}
示例14: getVethInfo
// For a given container, returns host veth name, container veth MAC, and pod IP
func getVethInfo(netns, containerIfname string) (string, string, string, error) {
var (
peerIfindex int
contVeth netlink.Link
err error
podIP string
)
containerNs, err := ns.GetNS(netns)
if err != nil {
return "", "", "", fmt.Errorf("failed to get container netns: %v", err)
}
defer containerNs.Close()
err = containerNs.Do(func(ns.NetNS) error {
contVeth, err = netlink.LinkByName(containerIfname)
if err != nil {
return err
}
peerIfindex = contVeth.Attrs().ParentIndex
addrs, err := netlink.AddrList(contVeth, syscall.AF_INET)
if err != nil {
return fmt.Errorf("failed to get container IP addresses: %v", err)
}
if len(addrs) == 0 {
return fmt.Errorf("container had no addresses")
}
podIP = addrs[0].IP.String()
return nil
})
if err != nil {
return "", "", "", fmt.Errorf("failed to inspect container interface: %v", err)
}
hostVeth, err := netlink.LinkByIndex(peerIfindex)
if err != nil {
return "", "", "", fmt.Errorf("failed to get host veth: %v", err)
}
return hostVeth.Attrs().Name, contVeth.Attrs().HardwareAddr.String(), podIP, nil
}
示例15: cacheUpdater
func (mapper *NetLinkMapper) cacheUpdater() {
logging.GetLogger().Debug("Start NetLink cache updater")
var ifIndex uint32
for {
ifIndex = <-mapper.cacheUpdaterChan
logging.GetLogger().Debug("ifIndex request received: %s", ifIndex)
var attrs *netlink.LinkAttrs
link, err := netlink.LinkByIndex(int(ifIndex))
if err != nil {
logging.GetLogger().Error("Error while getting interface by index via netling: ", err.Error())
attrs = &netlink.LinkAttrs{}
} else {
attrs = link.Attrs()
}
mapper.cache.Set(strconv.Itoa(int(ifIndex)), attrs, cache.DefaultExpiration)
}
}