本文整理汇总了Golang中github.com/vishvananda/netlink.Link类的典型用法代码示例。如果您正苦于以下问题:Golang Link类的具体用法?Golang Link怎么用?Golang Link使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Link类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ensureEgressFd
func ensureEgressFd(link netlink.Link, fd int) error {
q, err := ensureQdisc(link, "fq_codel", netlink.MakeHandle(1, 0), netlink.HANDLE_ROOT)
if err != nil {
return err
}
fHandle := netlink.MakeHandle(0, 2)
filter := &netlink.U32{
FilterAttrs: netlink.FilterAttrs{
LinkIndex: link.Attrs().Index,
Parent: q.Attrs().Handle,
Priority: 1,
Protocol: syscall.ETH_P_ALL,
},
Actions: []netlink.Action{
&netlink.BpfAction{Fd: fd, Name: "bpf1"},
},
ClassId: fHandle,
}
filters, err := netlink.FilterList(link, netlink.HANDLE_MIN_EGRESS)
if err != nil {
return fmt.Errorf("failed fetching egress filter list: %s", err)
}
for _, f := range filters {
if f, ok := f.(*netlink.U32); ok {
if f.ClassId == fHandle {
return nil
}
}
}
if err := netlink.FilterAdd(filter); err != nil {
return fmt.Errorf("failed adding egress filter: %v", err)
}
return nil
}
示例2: getDynamicIP
func getDynamicIP(t Netlink, link netlink.Link, dc client.Client) (*dhcp.Packet, error) {
var ack *dhcp.Packet
var err error
// use dhcp to acquire address
id, err := client.NewID(link.Attrs().Index, link.Attrs().HardwareAddr)
if err != nil {
return nil, err
}
ack, err = dc.Request(id)
if err != nil {
log.Errorf("error sending dhcp request: %s", err)
return nil, err
}
if ack.YourIP() == nil || ack.SubnetMask() == nil {
err = fmt.Errorf("dhcp assigned nil ip or subnet mask")
log.Error(err)
return nil, err
}
log.Infof("DHCP response: IP=%s, SubnetMask=%s, Gateway=%s, DNS=%s, Lease Time=%s", ack.YourIP(), ack.SubnetMask(), ack.Gateway(), ack.DNS(), ack.LeaseTime())
defer func() {
if err != nil && ack != nil {
dc.Release(ack)
}
}()
return ack, nil
}
示例3: delRouteIface
// delRouteIface clean up the required L3 mode default ns route
func delRouteIface(ipVlanL3Network *net.IPNet, iface netlink.Link) error {
return netlink.RouteDel(&netlink.Route{
LinkIndex: iface.Attrs().Index,
Scope: netlink.SCOPE_LINK,
Dst: ipVlanL3Network,
})
}
示例4: setFqCodelFd
func setFqCodelFd(iface netlink.Link, path string) error {
fd, err := netlink.BpfOpen(path)
if err != nil {
return fmt.Errorf("failed loading bpf program %v", err)
}
defer syscall.Close(fd)
fq := &netlink.GenericQdisc{
QdiscAttrs: netlink.QdiscAttrs{
LinkIndex: iface.Attrs().Index,
Handle: netlink.MakeHandle(1, 0),
Parent: netlink.HANDLE_ROOT,
},
QdiscType: "fq_codel",
}
if err := netlink.QdiscAdd(fq); err != nil {
return fmt.Errorf("failed setting egress qdisc: %v", err)
}
u32 := &netlink.U32{
FilterAttrs: netlink.FilterAttrs{
LinkIndex: iface.Attrs().Index,
Parent: fq.QdiscAttrs.Handle,
Protocol: syscall.ETH_P_ALL,
//Handle: 10,
//Priority: 10,
},
ClassId: netlink.MakeHandle(1, 2),
BpfFd: fd,
}
if err := netlink.FilterAdd(u32); err != nil {
return fmt.Errorf("failed adding egress filter: %v", err)
}
return nil
}
示例5: onLinkDeleted
func (u *NetLinkProbe) onLinkDeleted(link netlink.Link) {
index := link.Attrs().Index
u.Graph.Lock()
defer u.Graph.Unlock()
intf := u.Graph.LookupFirstChild(u.Root, graph.Metadata{"IfIndex": index})
// case of removing the interface from a bridge
if intf != nil {
parents := u.Graph.LookupParents(intf, graph.Metadata{"Type": "bridge"})
for _, parent := range parents {
u.Graph.Unlink(parent, intf)
}
}
// check whether the interface has been deleted or not
// we get a delete event when an interace is removed from a bridge
_, err := u.netlink.LinkByIndex(index)
if err != nil && intf != nil {
// if openvswitch do not remove let's do the job by ovs piece of code
if intf.Metadata()["Driver"] == "openvswitch" && intf.Metadata()["UUID"] != "" {
u.Graph.Unlink(u.Root, intf)
} else {
u.Graph.DelNode(intf)
}
}
delete(u.indexToChildrenQueue, int64(index))
}
示例6: linkAddrUpdate
func linkAddrUpdate(old, new *net.IPNet, t Netlink, link netlink.Link) error {
log.Infof("setting ip address %s for link %s", new, link.Attrs().Name)
if old != nil && !old.IP.Equal(new.IP) {
log.Debugf("removing old address %s", old)
if err := t.AddrDel(link, &netlink.Addr{IPNet: old}); err != nil {
if errno, ok := err.(syscall.Errno); !ok || errno != syscall.EADDRNOTAVAIL {
log.Errorf("failed to remove existing address %s: %s", old, err)
return err
}
}
log.Debugf("removed old address %s for link %s", old, link.Attrs().Name)
}
// assign IP to NIC
if err := t.AddrAdd(link, &netlink.Addr{IPNet: new}); err != nil {
if errno, ok := err.(syscall.Errno); !ok || errno != syscall.EEXIST {
log.Errorf("failed to assign ip %s for link %s", new, link.Attrs().Name)
return err
}
log.Warnf("address %s already set on interface %s", new, link.Attrs().Name)
}
log.Debugf("added address %s to link %s", new, link.Attrs().Name)
return nil
}
示例7: AddRouteIface
// addRouteIface required for L3 mode adds a link scoped route in the default ns
func AddRouteIface(ipVlanL3Network *net.IPNet, iface netlink.Link) error {
return netlink.RouteAdd(&netlink.Route{
LinkIndex: iface.Attrs().Index,
Scope: netlink.SCOPE_UNIVERSE,
Dst: ipVlanL3Network,
})
}
示例8: updateDefaultRoute
func updateDefaultRoute(t Netlink, link netlink.Link, endpoint *NetworkEndpoint) error {
// Add routes
if !endpoint.Network.Default || ip.IsUnspecifiedIP(endpoint.Network.Gateway.IP) {
log.Debugf("not setting route for network: default=%v gateway=%s", endpoint.Network.Default, endpoint.Network.Gateway.IP)
return nil
}
_, defaultNet, _ := net.ParseCIDR("0.0.0.0/0")
// delete default route first
if err := t.RouteDel(&netlink.Route{LinkIndex: link.Attrs().Index, Dst: defaultNet}); err != nil {
if errno, ok := err.(syscall.Errno); !ok || errno != syscall.ESRCH {
return fmt.Errorf("could not update default route: %s", err)
}
}
log.Infof("Setting default gateway to %s", endpoint.Network.Gateway.IP)
route := &netlink.Route{LinkIndex: link.Attrs().Index, Dst: defaultNet, Gw: endpoint.Network.Gateway.IP}
if err := t.RouteAdd(route); err != nil {
detail := fmt.Sprintf("failed to add gateway route for endpoint %s: %s", endpoint.Network.Name, err)
return errors.New(detail)
}
log.Infof("updated default route to %s interface, gateway: %s", endpoint.Network.Name, endpoint.Network.Gateway.IP)
return nil
}
示例9: addGenericLinkToTopology
func (u *NetLinkProbe) addGenericLinkToTopology(link netlink.Link, m graph.Metadata) *graph.Node {
name := link.Attrs().Name
index := int64(link.Attrs().Index)
var intf *graph.Node
if name != "lo" {
intf = u.Graph.LookupFirstChild(u.Root, graph.Metadata{
"IfIndex": index,
})
}
if intf == nil {
intf = u.Graph.NewNode(graph.GenID(), m)
}
if intf == nil {
return nil
}
if !u.Graph.AreLinked(u.Root, intf) {
u.Graph.Link(u.Root, intf)
}
u.handleIntfIsChild(intf, link)
u.handleIntfIsVeth(intf, link)
u.handleIntfIsBond(intf, link)
return intf
}
示例10: addIPConfigToLink
func addIPConfigToLink(ipConfig *ipam.IPConfig, link netlink.Link, ifName string) error {
log.Debugf("Configuring link %+v/%s with %+v", link, ifName, ipConfig)
addr := &netlink.Addr{IPNet: &ipConfig.IP}
if err := netlink.AddrAdd(link, addr); err != nil {
return fmt.Errorf("failed to add addr to %q: %v", ifName, err)
}
// Sort provided routes to make sure we apply any more specific
// routes first which may be used as nexthops in wider routes
sort.Sort(ipam.ByMask(ipConfig.Routes))
for _, r := range ipConfig.Routes {
log.Debugf("Adding route %+v", r)
rt := &netlink.Route{
LinkIndex: link.Attrs().Index,
Scope: netlink.SCOPE_UNIVERSE,
Dst: &r.Destination,
Gw: r.NextHop,
}
if r.IsL2() {
rt.Scope = netlink.SCOPE_LINK
}
if err := netlink.RouteAdd(rt); err != nil {
if !os.IsExist(err) {
return fmt.Errorf("failed to add route '%s via %v dev %v': %v",
r.Destination.String(), r.NextHop, ifName, err)
}
}
}
return nil
}
示例11: setIngressFd
func setIngressFd(iface netlink.Link, path string) error {
fd, err := netlink.BpfOpen(path)
if err != nil {
return fmt.Errorf("failed loading bpf program %v", err)
}
defer syscall.Close(fd)
ingress := &netlink.Ingress{
QdiscAttrs: netlink.QdiscAttrs{
LinkIndex: iface.Attrs().Index,
Handle: netlink.MakeHandle(0xffff, 0),
Parent: netlink.HANDLE_INGRESS,
},
}
if err := netlink.QdiscAdd(ingress); err != nil {
return fmt.Errorf("failed setting ingress qdisc: %v", err)
}
u32 := &netlink.U32{
FilterAttrs: netlink.FilterAttrs{
LinkIndex: iface.Attrs().Index,
Parent: ingress.QdiscAttrs.Handle,
Priority: 1,
Protocol: syscall.ETH_P_ALL,
},
ClassId: netlink.MakeHandle(1, 1),
BpfFd: fd,
}
if err := netlink.FilterAdd(u32); err != nil {
return fmt.Errorf("failed adding ingress filter: %v", err)
}
return nil
}
示例12: LinkSetUp
func (t *Mocker) LinkSetUp(link netlink.Link) error {
defer trace.End(trace.Begin(fmt.Sprintf("Bringing %s up", link.Attrs().Name)))
iface := link.(*Interface)
iface.Up = true
return nil
}
示例13: LinkSetAlias
func (t *Mocker) LinkSetAlias(link netlink.Link, alias string) error {
defer trace.End(trace.Begin(fmt.Sprintf("Adding alias %s to %s", alias, link.Attrs().Name)))
iface := link.(*Interface)
iface.Alias = alias
return nil
}
示例14: LinkSetDown
func (t *Mocker) LinkSetDown(link netlink.Link) error {
defer trace.End(trace.Begin(fmt.Sprintf("Bringing %s down", link.Attrs().Name)))
iface := link.(*Interface)
iface.Up = false
// TODO: should this drop addresses?
return nil
}
示例15: delRemoteRoute
// delRemoteRoute deletes a host-scoped route to a device.
func delRemoteRoute(neighborNetwork *net.IPNet, nextHop net.IP, iface netlink.Link) error {
return netlink.RouteDel(&netlink.Route{
Scope: netlink.SCOPE_UNIVERSE,
LinkIndex: iface.Attrs().Index,
Dst: neighborNetwork,
Gw: nextHop,
})
}