mirror of
https://github.com/rancher/os.git
synced 2025-09-01 14:48:55 +00:00
Update vendor
This commit is contained in:
7
vendor/github.com/vishvananda/netlink/.travis.yml
generated
vendored
7
vendor/github.com/vishvananda/netlink/.travis.yml
generated
vendored
@@ -1,3 +1,8 @@
|
||||
language: go
|
||||
before_script:
|
||||
# make sure we keep path in tact when we sudo
|
||||
- sudo sed -i -e 's/^Defaults\tsecure_path.*$//' /etc/sudoers
|
||||
# modprobe ip_gre or else the first gre device can't be deleted
|
||||
- sudo modprobe ip_gre
|
||||
install:
|
||||
- go get github.com/vishvananda/netns
|
||||
- go get github.com/vishvananda/netns
|
||||
|
2
vendor/github.com/vishvananda/netlink/Makefile
generated
vendored
2
vendor/github.com/vishvananda/netlink/Makefile
generated
vendored
@@ -11,7 +11,7 @@ goroot = $(addprefix ../../../,$(1))
|
||||
unroot = $(subst ../../../,,$(1))
|
||||
fmt = $(addprefix fmt-,$(1))
|
||||
|
||||
all: fmt
|
||||
all: fmt test
|
||||
|
||||
$(call goroot,$(DEPS)):
|
||||
go get $(call unroot,$@)
|
||||
|
2
vendor/github.com/vishvananda/netlink/addr.go
generated
vendored
2
vendor/github.com/vishvananda/netlink/addr.go
generated
vendored
@@ -11,6 +11,8 @@ import (
|
||||
type Addr struct {
|
||||
*net.IPNet
|
||||
Label string
|
||||
Flags int
|
||||
Scope int
|
||||
}
|
||||
|
||||
// String returns $ip/$netmask $label
|
||||
|
18
vendor/github.com/vishvananda/netlink/addr_linux.go
generated
vendored
18
vendor/github.com/vishvananda/netlink/addr_linux.go
generated
vendored
@@ -9,6 +9,9 @@ import (
|
||||
"github.com/vishvananda/netlink/nl"
|
||||
)
|
||||
|
||||
// IFA_FLAGS is a u32 attribute.
|
||||
const IFA_FLAGS = 0x8
|
||||
|
||||
// AddrAdd will add an IP address to a link device.
|
||||
// Equivalent to: `ip addr add $addr dev $link`
|
||||
func AddrAdd(link Link, addr *Addr) error {
|
||||
@@ -35,6 +38,7 @@ func addrHandle(link Link, addr *Addr, req *nl.NetlinkRequest) error {
|
||||
|
||||
msg := nl.NewIfAddrmsg(family)
|
||||
msg.Index = uint32(base.Index)
|
||||
msg.Scope = uint8(addr.Scope)
|
||||
prefixlen, _ := addr.Mask.Size()
|
||||
msg.Prefixlen = uint8(prefixlen)
|
||||
req.AddData(msg)
|
||||
@@ -52,6 +56,13 @@ func addrHandle(link Link, addr *Addr, req *nl.NetlinkRequest) error {
|
||||
addressData := nl.NewRtAttr(syscall.IFA_ADDRESS, addrData)
|
||||
req.AddData(addressData)
|
||||
|
||||
if addr.Flags != 0 {
|
||||
b := make([]byte, 4)
|
||||
native.PutUint32(b, uint32(addr.Flags))
|
||||
flagsData := nl.NewRtAttr(IFA_FLAGS, b)
|
||||
req.AddData(flagsData)
|
||||
}
|
||||
|
||||
if addr.Label != "" {
|
||||
labelData := nl.NewRtAttr(syscall.IFA_LABEL, nl.ZeroTerminated(addr.Label))
|
||||
req.AddData(labelData)
|
||||
@@ -90,6 +101,10 @@ func AddrList(link Link, family int) ([]Addr, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
if family != FAMILY_ALL && msg.Family != uint8(family) {
|
||||
continue
|
||||
}
|
||||
|
||||
attrs, err := nl.ParseRouteAttr(m[msg.Len():])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -111,6 +126,8 @@ func AddrList(link Link, family int) ([]Addr, error) {
|
||||
}
|
||||
case syscall.IFA_LABEL:
|
||||
addr.Label = string(attr.Value[:len(attr.Value)-1])
|
||||
case IFA_FLAGS:
|
||||
addr.Flags = int(native.Uint32(attr.Value[0:4]))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,6 +137,7 @@ func AddrList(link Link, family int) ([]Addr, error) {
|
||||
} else {
|
||||
addr.IPNet = dst
|
||||
}
|
||||
addr.Scope = int(msg.Scope)
|
||||
|
||||
res = append(res, addr)
|
||||
}
|
||||
|
1
vendor/github.com/vishvananda/netlink/class.go
generated
vendored
1
vendor/github.com/vishvananda/netlink/class.go
generated
vendored
@@ -56,6 +56,7 @@ func NewHtbClass(attrs ClassAttrs, cattrs HtbClassAttrs) *HtbClass {
|
||||
ceil := cattrs.Ceil / 8
|
||||
buffer := cattrs.Buffer
|
||||
cbuffer := cattrs.Cbuffer
|
||||
|
||||
if ceil == 0 {
|
||||
ceil = rate
|
||||
}
|
||||
|
22
vendor/github.com/vishvananda/netlink/class_linux.go
generated
vendored
22
vendor/github.com/vishvananda/netlink/class_linux.go
generated
vendored
@@ -1,6 +1,7 @@
|
||||
package netlink
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"syscall"
|
||||
|
||||
"github.com/vishvananda/netlink/nl"
|
||||
@@ -65,15 +66,32 @@ func classPayload(req *nl.NetlinkRequest, class Class) error {
|
||||
options := nl.NewRtAttr(nl.TCA_OPTIONS, nil)
|
||||
if htb, ok := class.(*HtbClass); ok {
|
||||
opt := nl.TcHtbCopt{}
|
||||
opt.Rate.Rate = uint32(htb.Rate)
|
||||
opt.Ceil.Rate = uint32(htb.Ceil)
|
||||
opt.Buffer = htb.Buffer
|
||||
opt.Cbuffer = htb.Cbuffer
|
||||
opt.Quantum = htb.Quantum
|
||||
opt.Level = htb.Level
|
||||
opt.Prio = htb.Prio
|
||||
// TODO: Handle Debug properly. For now default to 0
|
||||
/* Calculate {R,C}Tab and set Rate and Ceil */
|
||||
cell_log := -1
|
||||
ccell_log := -1
|
||||
linklayer := nl.LINKLAYER_ETHERNET
|
||||
mtu := 1600
|
||||
var rtab [256]uint32
|
||||
var ctab [256]uint32
|
||||
tcrate := nl.TcRateSpec{Rate: uint32(htb.Rate)}
|
||||
if CalcRtable(&tcrate, rtab, cell_log, uint32(mtu), linklayer) < 0 {
|
||||
return errors.New("HTB: failed to calculate rate table.")
|
||||
}
|
||||
opt.Rate = tcrate
|
||||
tcceil := nl.TcRateSpec{Rate: uint32(htb.Ceil)}
|
||||
if CalcRtable(&tcceil, ctab, ccell_log, uint32(mtu), linklayer) < 0 {
|
||||
return errors.New("HTB: failed to calculate ceil rate table.")
|
||||
}
|
||||
opt.Ceil = tcceil
|
||||
nl.NewRtAttrChild(options, nl.TCA_HTB_PARMS, opt.Serialize())
|
||||
nl.NewRtAttrChild(options, nl.TCA_HTB_RTAB, SerializeRtab(rtab))
|
||||
nl.NewRtAttrChild(options, nl.TCA_HTB_CTAB, SerializeRtab(ctab))
|
||||
}
|
||||
req.AddData(options)
|
||||
return nil
|
||||
|
313
vendor/github.com/vishvananda/netlink/link.go
generated
vendored
313
vendor/github.com/vishvananda/netlink/link.go
generated
vendored
@@ -1,6 +1,7 @@
|
||||
package netlink
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"syscall"
|
||||
)
|
||||
@@ -29,6 +30,7 @@ type LinkAttrs struct {
|
||||
ParentIndex int // index of the parent link device
|
||||
MasterIndex int // must be the index of a bridge
|
||||
Namespace interface{} // nil | NsPid | NsFd
|
||||
Alias string
|
||||
}
|
||||
|
||||
// NewLinkAttrs returns LinkAttrs structure filled with default values
|
||||
@@ -202,6 +204,7 @@ type Vxlan struct {
|
||||
RSC bool
|
||||
L2miss bool
|
||||
L3miss bool
|
||||
UDPCSum bool
|
||||
NoAge bool
|
||||
GBP bool
|
||||
Age int
|
||||
@@ -240,6 +243,316 @@ func (ipvlan *IPVlan) Type() string {
|
||||
return "ipvlan"
|
||||
}
|
||||
|
||||
// BondMode type
|
||||
type BondMode int
|
||||
|
||||
func (b BondMode) String() string {
|
||||
s, ok := bondModeToString[b]
|
||||
if !ok {
|
||||
return fmt.Sprintf("BondMode(%d)", b)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// StringToBondMode returns bond mode, or uknonw is the s is invalid.
|
||||
func StringToBondMode(s string) BondMode {
|
||||
mode, ok := StringToBondModeMap[s]
|
||||
if !ok {
|
||||
return BOND_MODE_UNKNOWN
|
||||
}
|
||||
return mode
|
||||
}
|
||||
|
||||
// Possible BondMode
|
||||
const (
|
||||
BOND_MODE_802_3AD BondMode = iota
|
||||
BOND_MODE_BALANCE_RR
|
||||
BOND_MODE_ACTIVE_BACKUP
|
||||
BOND_MODE_BALANCE_XOR
|
||||
BOND_MODE_BROADCAST
|
||||
BOND_MODE_BALANCE_TLB
|
||||
BOND_MODE_BALANCE_ALB
|
||||
BOND_MODE_UNKNOWN
|
||||
)
|
||||
|
||||
var bondModeToString = map[BondMode]string{
|
||||
BOND_MODE_802_3AD: "802.3ad",
|
||||
BOND_MODE_BALANCE_RR: "balance-rr",
|
||||
BOND_MODE_ACTIVE_BACKUP: "active-backup",
|
||||
BOND_MODE_BALANCE_XOR: "balance-xor",
|
||||
BOND_MODE_BROADCAST: "broadcast",
|
||||
BOND_MODE_BALANCE_TLB: "balance-tlb",
|
||||
BOND_MODE_BALANCE_ALB: "balance-alb",
|
||||
}
|
||||
var StringToBondModeMap = map[string]BondMode{
|
||||
"802.3ad": BOND_MODE_802_3AD,
|
||||
"balance-rr": BOND_MODE_BALANCE_RR,
|
||||
"active-backup": BOND_MODE_ACTIVE_BACKUP,
|
||||
"balance-xor": BOND_MODE_BALANCE_XOR,
|
||||
"broadcast": BOND_MODE_BROADCAST,
|
||||
"balance-tlb": BOND_MODE_BALANCE_TLB,
|
||||
"balance-alb": BOND_MODE_BALANCE_ALB,
|
||||
}
|
||||
|
||||
// BondArpValidate type
|
||||
type BondArpValidate int
|
||||
|
||||
// Possible BondArpValidate value
|
||||
const (
|
||||
BOND_ARP_VALIDATE_NONE BondArpValidate = iota
|
||||
BOND_ARP_VALIDATE_ACTIVE
|
||||
BOND_ARP_VALIDATE_BACKUP
|
||||
BOND_ARP_VALIDATE_ALL
|
||||
)
|
||||
|
||||
// BondPrimaryReselect type
|
||||
type BondPrimaryReselect int
|
||||
|
||||
// Possible BondPrimaryReselect value
|
||||
const (
|
||||
BOND_PRIMARY_RESELECT_ALWAYS BondPrimaryReselect = iota
|
||||
BOND_PRIMARY_RESELECT_BETTER
|
||||
BOND_PRIMARY_RESELECT_FAILURE
|
||||
)
|
||||
|
||||
// BondArpAllTargets type
|
||||
type BondArpAllTargets int
|
||||
|
||||
// Possible BondArpAllTargets value
|
||||
const (
|
||||
BOND_ARP_ALL_TARGETS_ANY BondArpAllTargets = iota
|
||||
BOND_ARP_ALL_TARGETS_ALL
|
||||
)
|
||||
|
||||
// BondFailOverMac type
|
||||
type BondFailOverMac int
|
||||
|
||||
// Possible BondFailOverMac value
|
||||
const (
|
||||
BOND_FAIL_OVER_MAC_NONE BondFailOverMac = iota
|
||||
BOND_FAIL_OVER_MAC_ACTIVE
|
||||
BOND_FAIL_OVER_MAC_FOLLOW
|
||||
)
|
||||
|
||||
// BondXmitHashPolicy type
|
||||
type BondXmitHashPolicy int
|
||||
|
||||
func (b BondXmitHashPolicy) String() string {
|
||||
s, ok := bondXmitHashPolicyToString[b]
|
||||
if !ok {
|
||||
return fmt.Sprintf("XmitHashPolicy(%d)", b)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// StringToBondXmitHashPolicy returns bond lacp arte, or uknonw is the s is invalid.
|
||||
func StringToBondXmitHashPolicy(s string) BondXmitHashPolicy {
|
||||
lacp, ok := StringToBondXmitHashPolicyMap[s]
|
||||
if !ok {
|
||||
return BOND_XMIT_HASH_POLICY_UNKNOWN
|
||||
}
|
||||
return lacp
|
||||
}
|
||||
|
||||
// Possible BondXmitHashPolicy value
|
||||
const (
|
||||
BOND_XMIT_HASH_POLICY_LAYER2 BondXmitHashPolicy = iota
|
||||
BOND_XMIT_HASH_POLICY_LAYER3_4
|
||||
BOND_XMIT_HASH_POLICY_LAYER2_3
|
||||
BOND_XMIT_HASH_POLICY_ENCAP2_3
|
||||
BOND_XMIT_HASH_POLICY_ENCAP3_4
|
||||
BOND_XMIT_HASH_POLICY_UNKNOWN
|
||||
)
|
||||
|
||||
var bondXmitHashPolicyToString = map[BondXmitHashPolicy]string{
|
||||
BOND_XMIT_HASH_POLICY_LAYER2: "layer2",
|
||||
BOND_XMIT_HASH_POLICY_LAYER3_4: "layer3+4",
|
||||
BOND_XMIT_HASH_POLICY_LAYER2_3: "layer2+3",
|
||||
BOND_XMIT_HASH_POLICY_ENCAP2_3: "encap2+3",
|
||||
BOND_XMIT_HASH_POLICY_ENCAP3_4: "encap3+4",
|
||||
}
|
||||
var StringToBondXmitHashPolicyMap = map[string]BondXmitHashPolicy{
|
||||
"layer2": BOND_XMIT_HASH_POLICY_LAYER2,
|
||||
"layer3+4": BOND_XMIT_HASH_POLICY_LAYER3_4,
|
||||
"layer2+3": BOND_XMIT_HASH_POLICY_LAYER2_3,
|
||||
"encap2+3": BOND_XMIT_HASH_POLICY_ENCAP2_3,
|
||||
"encap3+4": BOND_XMIT_HASH_POLICY_ENCAP3_4,
|
||||
}
|
||||
|
||||
// BondLacpRate type
|
||||
type BondLacpRate int
|
||||
|
||||
func (b BondLacpRate) String() string {
|
||||
s, ok := bondLacpRateToString[b]
|
||||
if !ok {
|
||||
return fmt.Sprintf("LacpRate(%d)", b)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// StringToBondLacpRate returns bond lacp arte, or uknonw is the s is invalid.
|
||||
func StringToBondLacpRate(s string) BondLacpRate {
|
||||
lacp, ok := StringToBondLacpRateMap[s]
|
||||
if !ok {
|
||||
return BOND_LACP_RATE_UNKNOWN
|
||||
}
|
||||
return lacp
|
||||
}
|
||||
|
||||
// Possible BondLacpRate value
|
||||
const (
|
||||
BOND_LACP_RATE_SLOW BondLacpRate = iota
|
||||
BOND_LACP_RATE_FAST
|
||||
BOND_LACP_RATE_UNKNOWN
|
||||
)
|
||||
|
||||
var bondLacpRateToString = map[BondLacpRate]string{
|
||||
BOND_LACP_RATE_SLOW: "slow",
|
||||
BOND_LACP_RATE_FAST: "fast",
|
||||
}
|
||||
var StringToBondLacpRateMap = map[string]BondLacpRate{
|
||||
"slow": BOND_LACP_RATE_SLOW,
|
||||
"fast": BOND_LACP_RATE_FAST,
|
||||
}
|
||||
|
||||
// BondAdSelect type
|
||||
type BondAdSelect int
|
||||
|
||||
// Possible BondAdSelect value
|
||||
const (
|
||||
BOND_AD_SELECT_STABLE BondAdSelect = iota
|
||||
BOND_AD_SELECT_BANDWIDTH
|
||||
BOND_AD_SELECT_COUNT
|
||||
)
|
||||
|
||||
// BondAdInfo
|
||||
type BondAdInfo struct {
|
||||
AggregatorId int
|
||||
NumPorts int
|
||||
ActorKey int
|
||||
PartnerKey int
|
||||
PartnerMac net.HardwareAddr
|
||||
}
|
||||
|
||||
// Bond representation
|
||||
type Bond struct {
|
||||
LinkAttrs
|
||||
Mode BondMode
|
||||
ActiveSlave int
|
||||
Miimon int
|
||||
UpDelay int
|
||||
DownDelay int
|
||||
UseCarrier int
|
||||
ArpInterval int
|
||||
ArpIpTargets []net.IP
|
||||
ArpValidate BondArpValidate
|
||||
ArpAllTargets BondArpAllTargets
|
||||
Primary int
|
||||
PrimaryReselect BondPrimaryReselect
|
||||
FailOverMac BondFailOverMac
|
||||
XmitHashPolicy BondXmitHashPolicy
|
||||
ResendIgmp int
|
||||
NumPeerNotif int
|
||||
AllSlavesActive int
|
||||
MinLinks int
|
||||
LpInterval int
|
||||
PackersPerSlave int
|
||||
LacpRate BondLacpRate
|
||||
AdSelect BondAdSelect
|
||||
// looking at iproute tool AdInfo can only be retrived. It can't be set.
|
||||
AdInfo *BondAdInfo
|
||||
}
|
||||
|
||||
func NewLinkBond(atr LinkAttrs) *Bond {
|
||||
return &Bond{
|
||||
LinkAttrs: atr,
|
||||
Mode: -1,
|
||||
ActiveSlave: -1,
|
||||
Miimon: -1,
|
||||
UpDelay: -1,
|
||||
DownDelay: -1,
|
||||
UseCarrier: -1,
|
||||
ArpInterval: -1,
|
||||
ArpIpTargets: nil,
|
||||
ArpValidate: -1,
|
||||
ArpAllTargets: -1,
|
||||
Primary: -1,
|
||||
PrimaryReselect: -1,
|
||||
FailOverMac: -1,
|
||||
XmitHashPolicy: -1,
|
||||
ResendIgmp: -1,
|
||||
NumPeerNotif: -1,
|
||||
AllSlavesActive: -1,
|
||||
MinLinks: -1,
|
||||
LpInterval: -1,
|
||||
PackersPerSlave: -1,
|
||||
LacpRate: -1,
|
||||
AdSelect: -1,
|
||||
}
|
||||
}
|
||||
|
||||
// Flag mask for bond options. Bond.Flagmask must be set to on for option to work.
|
||||
const (
|
||||
BOND_MODE_MASK uint64 = 1 << (1 + iota)
|
||||
BOND_ACTIVE_SLAVE_MASK
|
||||
BOND_MIIMON_MASK
|
||||
BOND_UPDELAY_MASK
|
||||
BOND_DOWNDELAY_MASK
|
||||
BOND_USE_CARRIER_MASK
|
||||
BOND_ARP_INTERVAL_MASK
|
||||
BOND_ARP_VALIDATE_MASK
|
||||
BOND_ARP_ALL_TARGETS_MASK
|
||||
BOND_PRIMARY_MASK
|
||||
BOND_PRIMARY_RESELECT_MASK
|
||||
BOND_FAIL_OVER_MAC_MASK
|
||||
BOND_XMIT_HASH_POLICY_MASK
|
||||
BOND_RESEND_IGMP_MASK
|
||||
BOND_NUM_PEER_NOTIF_MASK
|
||||
BOND_ALL_SLAVES_ACTIVE_MASK
|
||||
BOND_MIN_LINKS_MASK
|
||||
BOND_LP_INTERVAL_MASK
|
||||
BOND_PACKETS_PER_SLAVE_MASK
|
||||
BOND_LACP_RATE_MASK
|
||||
BOND_AD_SELECT_MASK
|
||||
)
|
||||
|
||||
// Attrs implementation.
|
||||
func (bond *Bond) Attrs() *LinkAttrs {
|
||||
return &bond.LinkAttrs
|
||||
}
|
||||
|
||||
// Type implementation fro Vxlan.
|
||||
func (bond *Bond) Type() string {
|
||||
return "bond"
|
||||
}
|
||||
|
||||
// GreTap devices must specify LocalIP and RemoteIP on create
|
||||
type Gretap struct {
|
||||
LinkAttrs
|
||||
IKey uint32
|
||||
OKey uint32
|
||||
EncapSport uint16
|
||||
EncapDport uint16
|
||||
Local net.IP
|
||||
Remote net.IP
|
||||
IFlags uint16
|
||||
OFlags uint16
|
||||
PMtuDisc uint8
|
||||
Ttl uint8
|
||||
Tos uint8
|
||||
EncapType uint16
|
||||
EncapFlags uint16
|
||||
Link uint32
|
||||
}
|
||||
|
||||
func (gretap *Gretap) Attrs() *LinkAttrs {
|
||||
return &gretap.LinkAttrs
|
||||
}
|
||||
|
||||
func (gretap *Gretap) Type() string {
|
||||
return "gretap"
|
||||
}
|
||||
|
||||
// iproute2 supported devices;
|
||||
// vlan | veth | vcan | dummy | ifb | macvlan | macvtap |
|
||||
// bridge | bond | ipoib | ip6tnl | ipip | sit | vxlan |
|
||||
|
387
vendor/github.com/vishvananda/netlink/link_linux.go
generated
vendored
387
vendor/github.com/vishvananda/netlink/link_linux.go
generated
vendored
@@ -106,6 +106,24 @@ func LinkSetName(link Link, name string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// LinkSetAlias sets the alias of the link device.
|
||||
// Equivalent to: `ip link set dev $link alias $name`
|
||||
func LinkSetAlias(link Link, name string) error {
|
||||
base := link.Attrs()
|
||||
ensureIndex(base)
|
||||
req := nl.NewNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
|
||||
|
||||
msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
|
||||
msg.Index = int32(base.Index)
|
||||
req.AddData(msg)
|
||||
|
||||
data := nl.NewRtAttr(syscall.IFLA_IFALIAS, []byte(name))
|
||||
req.AddData(data)
|
||||
|
||||
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// LinkSetHardwareAddr sets the hardware address of the link device.
|
||||
// Equivalent to: `ip link set $link address $hwaddr`
|
||||
func LinkSetHardwareAddr(link Link, hwaddr net.HardwareAddr) error {
|
||||
@@ -124,6 +142,54 @@ func LinkSetHardwareAddr(link Link, hwaddr net.HardwareAddr) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// LinkSetVfHardwareAddr sets the hardware address of a vf for the link.
|
||||
// Equivalent to: `ip link set $link vf $vf mac $hwaddr`
|
||||
func LinkSetVfHardwareAddr(link Link, vf int, hwaddr net.HardwareAddr) error {
|
||||
base := link.Attrs()
|
||||
ensureIndex(base)
|
||||
req := nl.NewNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
|
||||
|
||||
msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
|
||||
msg.Index = int32(base.Index)
|
||||
req.AddData(msg)
|
||||
|
||||
data := nl.NewRtAttr(nl.IFLA_VFINFO_LIST, nil)
|
||||
info := nl.NewRtAttrChild(data, nl.IFLA_VF_INFO, nil)
|
||||
vfmsg := nl.VfMac{
|
||||
Vf: uint32(vf),
|
||||
}
|
||||
copy(vfmsg.Mac[:], []byte(hwaddr))
|
||||
nl.NewRtAttrChild(info, nl.IFLA_VF_MAC, vfmsg.Serialize())
|
||||
req.AddData(data)
|
||||
|
||||
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// LinkSetVfVlan sets the vlan of a vf for the link.
|
||||
// Equivalent to: `ip link set $link vf $vf vlan $vlan`
|
||||
func LinkSetVfVlan(link Link, vf, vlan int) error {
|
||||
base := link.Attrs()
|
||||
ensureIndex(base)
|
||||
req := nl.NewNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
|
||||
|
||||
msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
|
||||
msg.Index = int32(base.Index)
|
||||
req.AddData(msg)
|
||||
|
||||
data := nl.NewRtAttr(nl.IFLA_VFINFO_LIST, nil)
|
||||
info := nl.NewRtAttrChild(data, nl.IFLA_VF_INFO, nil)
|
||||
vfmsg := nl.VfVlan{
|
||||
Vf: uint32(vf),
|
||||
Vlan: uint32(vlan),
|
||||
}
|
||||
nl.NewRtAttrChild(info, nl.IFLA_VF_VLAN, vfmsg.Serialize())
|
||||
req.AddData(data)
|
||||
|
||||
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// LinkSetMaster sets the master of the link device.
|
||||
// Equivalent to: `ip link set $link master $master`
|
||||
func LinkSetMaster(link Link, master *Bridge) error {
|
||||
@@ -133,9 +199,18 @@ func LinkSetMaster(link Link, master *Bridge) error {
|
||||
ensureIndex(masterBase)
|
||||
index = masterBase.Index
|
||||
}
|
||||
if index <= 0 {
|
||||
return fmt.Errorf("Device does not exist")
|
||||
}
|
||||
return LinkSetMasterByIndex(link, index)
|
||||
}
|
||||
|
||||
// LinkSetNoMaster removes the master of the link device.
|
||||
// Equivalent to: `ip link set $link nomaster`
|
||||
func LinkSetNoMaster(link Link) error {
|
||||
return LinkSetMasterByIndex(link, 0)
|
||||
}
|
||||
|
||||
// LinkSetMasterByIndex sets the master of the link device.
|
||||
// Equivalent to: `ip link set $link master $master`
|
||||
func LinkSetMasterByIndex(link Link, masterIndex int) error {
|
||||
@@ -250,10 +325,12 @@ func addVxlanAttrs(vxlan *Vxlan, linkInfo *nl.RtAttr) {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_L2MISS, boolAttr(vxlan.L2miss))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_L3MISS, boolAttr(vxlan.L3miss))
|
||||
|
||||
if vxlan.UDPCSum {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_UDP_CSUM, boolAttr(vxlan.UDPCSum))
|
||||
}
|
||||
if vxlan.GBP {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_GBP, boolAttr(vxlan.GBP))
|
||||
}
|
||||
|
||||
if vxlan.NoAge {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_AGEING, nl.Uint32Attr(0))
|
||||
} else if vxlan.Age > 0 {
|
||||
@@ -275,6 +352,87 @@ func addVxlanAttrs(vxlan *Vxlan, linkInfo *nl.RtAttr) {
|
||||
}
|
||||
}
|
||||
|
||||
func addBondAttrs(bond *Bond, linkInfo *nl.RtAttr) {
|
||||
data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
|
||||
if bond.Mode >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_MODE, nl.Uint8Attr(uint8(bond.Mode)))
|
||||
}
|
||||
if bond.ActiveSlave >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_ACTIVE_SLAVE, nl.Uint32Attr(uint32(bond.ActiveSlave)))
|
||||
}
|
||||
if bond.Miimon >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_MIIMON, nl.Uint32Attr(uint32(bond.Miimon)))
|
||||
}
|
||||
if bond.UpDelay >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_UPDELAY, nl.Uint32Attr(uint32(bond.UpDelay)))
|
||||
}
|
||||
if bond.DownDelay >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_DOWNDELAY, nl.Uint32Attr(uint32(bond.DownDelay)))
|
||||
}
|
||||
if bond.UseCarrier >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_USE_CARRIER, nl.Uint8Attr(uint8(bond.UseCarrier)))
|
||||
}
|
||||
if bond.ArpInterval >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_ARP_INTERVAL, nl.Uint32Attr(uint32(bond.ArpInterval)))
|
||||
}
|
||||
if bond.ArpIpTargets != nil {
|
||||
msg := nl.NewRtAttrChild(data, nl.IFLA_BOND_ARP_IP_TARGET, nil)
|
||||
for i := range bond.ArpIpTargets {
|
||||
ip := bond.ArpIpTargets[i].To4()
|
||||
if ip != nil {
|
||||
nl.NewRtAttrChild(msg, i, []byte(ip))
|
||||
continue
|
||||
}
|
||||
ip = bond.ArpIpTargets[i].To16()
|
||||
if ip != nil {
|
||||
nl.NewRtAttrChild(msg, i, []byte(ip))
|
||||
}
|
||||
}
|
||||
}
|
||||
if bond.ArpValidate >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_ARP_VALIDATE, nl.Uint32Attr(uint32(bond.ArpValidate)))
|
||||
}
|
||||
if bond.ArpAllTargets >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_ARP_ALL_TARGETS, nl.Uint32Attr(uint32(bond.ArpAllTargets)))
|
||||
}
|
||||
if bond.Primary >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_PRIMARY, nl.Uint32Attr(uint32(bond.Primary)))
|
||||
}
|
||||
if bond.PrimaryReselect >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_PRIMARY_RESELECT, nl.Uint8Attr(uint8(bond.PrimaryReselect)))
|
||||
}
|
||||
if bond.FailOverMac >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_FAIL_OVER_MAC, nl.Uint8Attr(uint8(bond.FailOverMac)))
|
||||
}
|
||||
if bond.XmitHashPolicy >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_XMIT_HASH_POLICY, nl.Uint8Attr(uint8(bond.XmitHashPolicy)))
|
||||
}
|
||||
if bond.ResendIgmp >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_RESEND_IGMP, nl.Uint32Attr(uint32(bond.ResendIgmp)))
|
||||
}
|
||||
if bond.NumPeerNotif >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_NUM_PEER_NOTIF, nl.Uint8Attr(uint8(bond.NumPeerNotif)))
|
||||
}
|
||||
if bond.AllSlavesActive >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_ALL_SLAVES_ACTIVE, nl.Uint8Attr(uint8(bond.AllSlavesActive)))
|
||||
}
|
||||
if bond.MinLinks >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_MIN_LINKS, nl.Uint32Attr(uint32(bond.MinLinks)))
|
||||
}
|
||||
if bond.LpInterval >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_LP_INTERVAL, nl.Uint32Attr(uint32(bond.LpInterval)))
|
||||
}
|
||||
if bond.PackersPerSlave >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_PACKETS_PER_SLAVE, nl.Uint32Attr(uint32(bond.PackersPerSlave)))
|
||||
}
|
||||
if bond.LacpRate >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_AD_LACP_RATE, nl.Uint8Attr(uint8(bond.LacpRate)))
|
||||
}
|
||||
if bond.AdSelect >= 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_BOND_AD_SELECT, nl.Uint8Attr(uint8(bond.AdSelect)))
|
||||
}
|
||||
}
|
||||
|
||||
// LinkAdd adds a new link device. The type and features of the device
|
||||
// are taken fromt the parameters in the link object.
|
||||
// Equivalent to: `ip link add $link`
|
||||
@@ -328,6 +486,27 @@ func LinkAdd(link Link) error {
|
||||
req := nl.NewNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
|
||||
|
||||
msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
|
||||
// TODO: make it shorter
|
||||
if base.Flags&net.FlagUp != 0 {
|
||||
msg.Change = syscall.IFF_UP
|
||||
msg.Flags = syscall.IFF_UP
|
||||
}
|
||||
if base.Flags&net.FlagBroadcast != 0 {
|
||||
msg.Change |= syscall.IFF_BROADCAST
|
||||
msg.Flags |= syscall.IFF_BROADCAST
|
||||
}
|
||||
if base.Flags&net.FlagLoopback != 0 {
|
||||
msg.Change |= syscall.IFF_LOOPBACK
|
||||
msg.Flags |= syscall.IFF_LOOPBACK
|
||||
}
|
||||
if base.Flags&net.FlagPointToPoint != 0 {
|
||||
msg.Change |= syscall.IFF_POINTOPOINT
|
||||
msg.Flags |= syscall.IFF_POINTOPOINT
|
||||
}
|
||||
if base.Flags&net.FlagMulticast != 0 {
|
||||
msg.Change |= syscall.IFF_MULTICAST
|
||||
msg.Flags |= syscall.IFF_MULTICAST
|
||||
}
|
||||
req.AddData(msg)
|
||||
|
||||
if base.ParentIndex != 0 {
|
||||
@@ -388,6 +567,8 @@ func LinkAdd(link Link) error {
|
||||
|
||||
} else if vxlan, ok := link.(*Vxlan); ok {
|
||||
addVxlanAttrs(vxlan, linkInfo)
|
||||
} else if bond, ok := link.(*Bond); ok {
|
||||
addBondAttrs(bond, linkInfo)
|
||||
} else if ipv, ok := link.(*IPVlan); ok {
|
||||
data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
|
||||
nl.NewRtAttrChild(data, nl.IFLA_IPVLAN_MODE, nl.Uint16Attr(uint16(ipv.Mode)))
|
||||
@@ -396,6 +577,8 @@ func LinkAdd(link Link) error {
|
||||
data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
|
||||
nl.NewRtAttrChild(data, nl.IFLA_MACVLAN_MODE, nl.Uint32Attr(macvlanModes[macv.Mode]))
|
||||
}
|
||||
} else if gretap, ok := link.(*Gretap); ok {
|
||||
addGretapAttrs(gretap, linkInfo)
|
||||
}
|
||||
|
||||
req.AddData(linkInfo)
|
||||
@@ -447,6 +630,20 @@ func linkByNameDump(name string) (Link, error) {
|
||||
return nil, fmt.Errorf("Link %s not found", name)
|
||||
}
|
||||
|
||||
func linkByAliasDump(alias string) (Link, error) {
|
||||
links, err := LinkList()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, link := range links {
|
||||
if link.Attrs().Alias == alias {
|
||||
return link, nil
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("Link alias %s not found", alias)
|
||||
}
|
||||
|
||||
// LinkByName finds a link by name and returns a pointer to the object.
|
||||
func LinkByName(name string) (Link, error) {
|
||||
if lookupByDump {
|
||||
@@ -472,6 +669,32 @@ func LinkByName(name string) (Link, error) {
|
||||
return link, err
|
||||
}
|
||||
|
||||
// LinkByAlias finds a link by its alias and returns a pointer to the object.
|
||||
// If there are multiple links with the alias it returns the first one
|
||||
func LinkByAlias(alias string) (Link, error) {
|
||||
if lookupByDump {
|
||||
return linkByAliasDump(alias)
|
||||
}
|
||||
|
||||
req := nl.NewNetlinkRequest(syscall.RTM_GETLINK, syscall.NLM_F_ACK)
|
||||
|
||||
msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
|
||||
req.AddData(msg)
|
||||
|
||||
nameData := nl.NewRtAttr(syscall.IFLA_IFALIAS, nl.ZeroTerminated(alias))
|
||||
req.AddData(nameData)
|
||||
|
||||
link, err := execGetLink(req)
|
||||
if err == syscall.EINVAL {
|
||||
// older kernels don't support looking up via IFLA_IFALIAS
|
||||
// so fall back to dumping all links
|
||||
lookupByDump = true
|
||||
return linkByAliasDump(alias)
|
||||
}
|
||||
|
||||
return link, err
|
||||
}
|
||||
|
||||
// LinkByIndex finds a link by index and returns a pointer to the object.
|
||||
func LinkByIndex(index int) (Link, error) {
|
||||
req := nl.NewNetlinkRequest(syscall.RTM_GETLINK, syscall.NLM_F_ACK)
|
||||
@@ -543,12 +766,16 @@ func linkDeserialize(m []byte) (Link, error) {
|
||||
link = &Veth{}
|
||||
case "vxlan":
|
||||
link = &Vxlan{}
|
||||
case "bond":
|
||||
link = &Bond{}
|
||||
case "ipvlan":
|
||||
link = &IPVlan{}
|
||||
case "macvlan":
|
||||
link = &Macvlan{}
|
||||
case "macvtap":
|
||||
link = &Macvtap{}
|
||||
case "gretap":
|
||||
link = &Gretap{}
|
||||
default:
|
||||
link = &GenericLink{LinkType: linkType}
|
||||
}
|
||||
@@ -562,12 +789,16 @@ func linkDeserialize(m []byte) (Link, error) {
|
||||
parseVlanData(link, data)
|
||||
case "vxlan":
|
||||
parseVxlanData(link, data)
|
||||
case "bond":
|
||||
parseBondData(link, data)
|
||||
case "ipvlan":
|
||||
parseIPVlanData(link, data)
|
||||
case "macvlan":
|
||||
parseMacvlanData(link, data)
|
||||
case "macvtap":
|
||||
parseMacvtapData(link, data)
|
||||
case "gretap":
|
||||
parseGretapData(link, data)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -591,6 +822,8 @@ func linkDeserialize(m []byte) (Link, error) {
|
||||
base.MasterIndex = int(native.Uint32(attr.Value[0:4]))
|
||||
case syscall.IFLA_TXQLEN:
|
||||
base.TxQLen = int(native.Uint32(attr.Value[0:4]))
|
||||
case syscall.IFLA_IFALIAS:
|
||||
base.Alias = string(attr.Value[:len(attr.Value)-1])
|
||||
}
|
||||
}
|
||||
// Links that don't have IFLA_INFO_KIND are hardware devices
|
||||
@@ -632,6 +865,7 @@ func LinkList() ([]Link, error) {
|
||||
// LinkUpdate is used to pass information back from LinkSubscribe()
|
||||
type LinkUpdate struct {
|
||||
nl.IfInfomsg
|
||||
Header syscall.NlMsghdr
|
||||
Link
|
||||
}
|
||||
|
||||
@@ -661,7 +895,7 @@ func LinkSubscribe(ch chan<- LinkUpdate, done <-chan struct{}) error {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ch <- LinkUpdate{IfInfomsg: *ifmsg, Link: link}
|
||||
ch <- LinkUpdate{IfInfomsg: *ifmsg, Header: m.Header, Link: link}
|
||||
}
|
||||
}
|
||||
}()
|
||||
@@ -752,6 +986,8 @@ func parseVxlanData(link Link, data []syscall.NetlinkRouteAttr) {
|
||||
vxlan.L2miss = int8(datum.Value[0]) != 0
|
||||
case nl.IFLA_VXLAN_L3MISS:
|
||||
vxlan.L3miss = int8(datum.Value[0]) != 0
|
||||
case nl.IFLA_VXLAN_UDP_CSUM:
|
||||
vxlan.UDPCSum = int8(datum.Value[0]) != 0
|
||||
case nl.IFLA_VXLAN_GBP:
|
||||
vxlan.GBP = int8(datum.Value[0]) != 0
|
||||
case nl.IFLA_VXLAN_AGEING:
|
||||
@@ -772,6 +1008,60 @@ func parseVxlanData(link Link, data []syscall.NetlinkRouteAttr) {
|
||||
}
|
||||
}
|
||||
|
||||
func parseBondData(link Link, data []syscall.NetlinkRouteAttr) {
|
||||
bond := NewLinkBond(NewLinkAttrs())
|
||||
for i := range data {
|
||||
switch data[i].Attr.Type {
|
||||
case nl.IFLA_BOND_MODE:
|
||||
bond.Mode = BondMode(data[i].Value[0])
|
||||
case nl.IFLA_BOND_ACTIVE_SLAVE:
|
||||
bond.ActiveSlave = int(native.Uint32(data[i].Value[0:4]))
|
||||
case nl.IFLA_BOND_MIIMON:
|
||||
bond.Miimon = int(native.Uint32(data[i].Value[0:4]))
|
||||
case nl.IFLA_BOND_UPDELAY:
|
||||
bond.UpDelay = int(native.Uint32(data[i].Value[0:4]))
|
||||
case nl.IFLA_BOND_DOWNDELAY:
|
||||
bond.DownDelay = int(native.Uint32(data[i].Value[0:4]))
|
||||
case nl.IFLA_BOND_USE_CARRIER:
|
||||
bond.UseCarrier = int(data[i].Value[0])
|
||||
case nl.IFLA_BOND_ARP_INTERVAL:
|
||||
bond.ArpInterval = int(native.Uint32(data[i].Value[0:4]))
|
||||
case nl.IFLA_BOND_ARP_IP_TARGET:
|
||||
// TODO: implement
|
||||
case nl.IFLA_BOND_ARP_VALIDATE:
|
||||
bond.ArpValidate = BondArpValidate(native.Uint32(data[i].Value[0:4]))
|
||||
case nl.IFLA_BOND_ARP_ALL_TARGETS:
|
||||
bond.ArpAllTargets = BondArpAllTargets(native.Uint32(data[i].Value[0:4]))
|
||||
case nl.IFLA_BOND_PRIMARY:
|
||||
bond.Primary = int(native.Uint32(data[i].Value[0:4]))
|
||||
case nl.IFLA_BOND_PRIMARY_RESELECT:
|
||||
bond.PrimaryReselect = BondPrimaryReselect(data[i].Value[0])
|
||||
case nl.IFLA_BOND_FAIL_OVER_MAC:
|
||||
bond.FailOverMac = BondFailOverMac(data[i].Value[0])
|
||||
case nl.IFLA_BOND_XMIT_HASH_POLICY:
|
||||
bond.XmitHashPolicy = BondXmitHashPolicy(data[i].Value[0])
|
||||
case nl.IFLA_BOND_RESEND_IGMP:
|
||||
bond.ResendIgmp = int(native.Uint32(data[i].Value[0:4]))
|
||||
case nl.IFLA_BOND_NUM_PEER_NOTIF:
|
||||
bond.NumPeerNotif = int(data[i].Value[0])
|
||||
case nl.IFLA_BOND_ALL_SLAVES_ACTIVE:
|
||||
bond.AllSlavesActive = int(data[i].Value[0])
|
||||
case nl.IFLA_BOND_MIN_LINKS:
|
||||
bond.MinLinks = int(native.Uint32(data[i].Value[0:4]))
|
||||
case nl.IFLA_BOND_LP_INTERVAL:
|
||||
bond.LpInterval = int(native.Uint32(data[i].Value[0:4]))
|
||||
case nl.IFLA_BOND_PACKETS_PER_SLAVE:
|
||||
bond.PackersPerSlave = int(native.Uint32(data[i].Value[0:4]))
|
||||
case nl.IFLA_BOND_AD_LACP_RATE:
|
||||
bond.LacpRate = BondLacpRate(data[i].Value[0])
|
||||
case nl.IFLA_BOND_AD_SELECT:
|
||||
bond.AdSelect = BondAdSelect(data[i].Value[0])
|
||||
case nl.IFLA_BOND_AD_INFO:
|
||||
// TODO: implement
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func parseIPVlanData(link Link, data []syscall.NetlinkRouteAttr) {
|
||||
ipv := link.(*IPVlan)
|
||||
for _, datum := range data {
|
||||
@@ -828,3 +1118,96 @@ func linkFlags(rawFlags uint32) net.Flags {
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func htonl(val uint32) []byte {
|
||||
bytes := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(bytes, val)
|
||||
return bytes
|
||||
}
|
||||
|
||||
func htons(val uint16) []byte {
|
||||
bytes := make([]byte, 2)
|
||||
binary.BigEndian.PutUint16(bytes, val)
|
||||
return bytes
|
||||
}
|
||||
|
||||
func ntohl(buf []byte) uint32 {
|
||||
return binary.BigEndian.Uint32(buf)
|
||||
}
|
||||
|
||||
func ntohs(buf []byte) uint16 {
|
||||
return binary.BigEndian.Uint16(buf)
|
||||
}
|
||||
|
||||
func addGretapAttrs(gretap *Gretap, linkInfo *nl.RtAttr) {
|
||||
data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
|
||||
|
||||
ip := gretap.Local.To4()
|
||||
if ip != nil {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_LOCAL, []byte(ip))
|
||||
}
|
||||
ip = gretap.Remote.To4()
|
||||
if ip != nil {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_REMOTE, []byte(ip))
|
||||
}
|
||||
|
||||
if gretap.IKey != 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_IKEY, htonl(gretap.IKey))
|
||||
gretap.IFlags |= uint16(nl.GRE_KEY)
|
||||
}
|
||||
|
||||
if gretap.OKey != 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_OKEY, htonl(gretap.OKey))
|
||||
gretap.OFlags |= uint16(nl.GRE_KEY)
|
||||
}
|
||||
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_IFLAGS, htons(gretap.IFlags))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_OFLAGS, htons(gretap.OFlags))
|
||||
|
||||
if gretap.Link != 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_LINK, nl.Uint32Attr(gretap.Link))
|
||||
}
|
||||
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_PMTUDISC, nl.Uint8Attr(gretap.PMtuDisc))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_TTL, nl.Uint8Attr(gretap.Ttl))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_TOS, nl.Uint8Attr(gretap.Tos))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_ENCAP_TYPE, nl.Uint16Attr(gretap.EncapType))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_ENCAP_FLAGS, nl.Uint16Attr(gretap.EncapFlags))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_ENCAP_SPORT, htons(gretap.EncapSport))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_ENCAP_DPORT, htons(gretap.EncapDport))
|
||||
}
|
||||
|
||||
func parseGretapData(link Link, data []syscall.NetlinkRouteAttr) {
|
||||
gre := link.(*Gretap)
|
||||
for _, datum := range data {
|
||||
switch datum.Attr.Type {
|
||||
case nl.IFLA_GRE_OKEY:
|
||||
gre.IKey = ntohl(datum.Value[0:4])
|
||||
case nl.IFLA_GRE_IKEY:
|
||||
gre.OKey = ntohl(datum.Value[0:4])
|
||||
case nl.IFLA_GRE_LOCAL:
|
||||
gre.Local = net.IP(datum.Value[0:4])
|
||||
case nl.IFLA_GRE_REMOTE:
|
||||
gre.Remote = net.IP(datum.Value[0:4])
|
||||
case nl.IFLA_GRE_ENCAP_SPORT:
|
||||
gre.EncapSport = ntohs(datum.Value[0:2])
|
||||
case nl.IFLA_GRE_ENCAP_DPORT:
|
||||
gre.EncapDport = ntohs(datum.Value[0:2])
|
||||
case nl.IFLA_GRE_IFLAGS:
|
||||
gre.IFlags = ntohs(datum.Value[0:2])
|
||||
case nl.IFLA_GRE_OFLAGS:
|
||||
gre.OFlags = ntohs(datum.Value[0:2])
|
||||
|
||||
case nl.IFLA_GRE_TTL:
|
||||
gre.Ttl = uint8(datum.Value[0])
|
||||
case nl.IFLA_GRE_TOS:
|
||||
gre.Tos = uint8(datum.Value[0])
|
||||
case nl.IFLA_GRE_PMTUDISC:
|
||||
gre.PMtuDisc = uint8(datum.Value[0])
|
||||
case nl.IFLA_GRE_ENCAP_TYPE:
|
||||
gre.EncapType = native.Uint16(datum.Value[0:2])
|
||||
case nl.IFLA_GRE_ENCAP_FLAGS:
|
||||
gre.EncapFlags = native.Uint16(datum.Value[0:2])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
292
vendor/github.com/vishvananda/netlink/nl/link_linux.go
generated
vendored
292
vendor/github.com/vishvananda/netlink/nl/link_linux.go
generated
vendored
@@ -1,7 +1,13 @@
|
||||
package nl
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
DEFAULT_CHANGE = 0xFFFFFFFF
|
||||
// doesn't exist in syscall
|
||||
IFLA_VFINFO_LIST = 0x16
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -102,3 +108,289 @@ const (
|
||||
MACVLAN_MODE_PASSTHRU = 8
|
||||
MACVLAN_MODE_SOURCE = 16
|
||||
)
|
||||
|
||||
const (
|
||||
IFLA_BOND_UNSPEC = iota
|
||||
IFLA_BOND_MODE
|
||||
IFLA_BOND_ACTIVE_SLAVE
|
||||
IFLA_BOND_MIIMON
|
||||
IFLA_BOND_UPDELAY
|
||||
IFLA_BOND_DOWNDELAY
|
||||
IFLA_BOND_USE_CARRIER
|
||||
IFLA_BOND_ARP_INTERVAL
|
||||
IFLA_BOND_ARP_IP_TARGET
|
||||
IFLA_BOND_ARP_VALIDATE
|
||||
IFLA_BOND_ARP_ALL_TARGETS
|
||||
IFLA_BOND_PRIMARY
|
||||
IFLA_BOND_PRIMARY_RESELECT
|
||||
IFLA_BOND_FAIL_OVER_MAC
|
||||
IFLA_BOND_XMIT_HASH_POLICY
|
||||
IFLA_BOND_RESEND_IGMP
|
||||
IFLA_BOND_NUM_PEER_NOTIF
|
||||
IFLA_BOND_ALL_SLAVES_ACTIVE
|
||||
IFLA_BOND_MIN_LINKS
|
||||
IFLA_BOND_LP_INTERVAL
|
||||
IFLA_BOND_PACKETS_PER_SLAVE
|
||||
IFLA_BOND_AD_LACP_RATE
|
||||
IFLA_BOND_AD_SELECT
|
||||
IFLA_BOND_AD_INFO
|
||||
)
|
||||
|
||||
const (
|
||||
IFLA_BOND_AD_INFO_UNSPEC = iota
|
||||
IFLA_BOND_AD_INFO_AGGREGATOR
|
||||
IFLA_BOND_AD_INFO_NUM_PORTS
|
||||
IFLA_BOND_AD_INFO_ACTOR_KEY
|
||||
IFLA_BOND_AD_INFO_PARTNER_KEY
|
||||
IFLA_BOND_AD_INFO_PARTNER_MAC
|
||||
)
|
||||
|
||||
const (
|
||||
IFLA_BOND_SLAVE_UNSPEC = iota
|
||||
IFLA_BOND_SLAVE_STATE
|
||||
IFLA_BOND_SLAVE_MII_STATUS
|
||||
IFLA_BOND_SLAVE_LINK_FAILURE_COUNT
|
||||
IFLA_BOND_SLAVE_PERM_HWADDR
|
||||
IFLA_BOND_SLAVE_QUEUE_ID
|
||||
IFLA_BOND_SLAVE_AD_AGGREGATOR_ID
|
||||
)
|
||||
|
||||
const (
|
||||
IFLA_GRE_UNSPEC = iota
|
||||
IFLA_GRE_LINK
|
||||
IFLA_GRE_IFLAGS
|
||||
IFLA_GRE_OFLAGS
|
||||
IFLA_GRE_IKEY
|
||||
IFLA_GRE_OKEY
|
||||
IFLA_GRE_LOCAL
|
||||
IFLA_GRE_REMOTE
|
||||
IFLA_GRE_TTL
|
||||
IFLA_GRE_TOS
|
||||
IFLA_GRE_PMTUDISC
|
||||
IFLA_GRE_ENCAP_LIMIT
|
||||
IFLA_GRE_FLOWINFO
|
||||
IFLA_GRE_FLAGS
|
||||
IFLA_GRE_ENCAP_TYPE
|
||||
IFLA_GRE_ENCAP_FLAGS
|
||||
IFLA_GRE_ENCAP_SPORT
|
||||
IFLA_GRE_ENCAP_DPORT
|
||||
IFLA_GRE_COLLECT_METADATA
|
||||
IFLA_GRE_MAX = IFLA_GRE_COLLECT_METADATA
|
||||
)
|
||||
|
||||
const (
|
||||
GRE_CSUM = 0x8000
|
||||
GRE_ROUTING = 0x4000
|
||||
GRE_KEY = 0x2000
|
||||
GRE_SEQ = 0x1000
|
||||
GRE_STRICT = 0x0800
|
||||
GRE_REC = 0x0700
|
||||
GRE_FLAGS = 0x00F8
|
||||
GRE_VERSION = 0x0007
|
||||
)
|
||||
|
||||
const (
|
||||
IFLA_VF_INFO_UNSPEC = iota
|
||||
IFLA_VF_INFO
|
||||
IFLA_VF_INFO_MAX = IFLA_VF_INFO
|
||||
)
|
||||
|
||||
const (
|
||||
IFLA_VF_UNSPEC = iota
|
||||
IFLA_VF_MAC /* Hardware queue specific attributes */
|
||||
IFLA_VF_VLAN
|
||||
IFLA_VF_TX_RATE /* Max TX Bandwidth Allocation */
|
||||
IFLA_VF_SPOOFCHK /* Spoof Checking on/off switch */
|
||||
IFLA_VF_LINK_STATE /* link state enable/disable/auto switch */
|
||||
IFLA_VF_RATE /* Min and Max TX Bandwidth Allocation */
|
||||
IFLA_VF_RSS_QUERY_EN /* RSS Redirection Table and Hash Key query
|
||||
* on/off switch
|
||||
*/
|
||||
IFLA_VF_STATS /* network device statistics */
|
||||
IFLA_VF_MAX = IFLA_VF_STATS
|
||||
)
|
||||
|
||||
const (
|
||||
IFLA_VF_LINK_STATE_AUTO = iota /* link state of the uplink */
|
||||
IFLA_VF_LINK_STATE_ENABLE /* link always up */
|
||||
IFLA_VF_LINK_STATE_DISABLE /* link always down */
|
||||
IFLA_VF_LINK_STATE_MAX = IFLA_VF_LINK_STATE_DISABLE
|
||||
)
|
||||
|
||||
const (
|
||||
IFLA_VF_STATS_RX_PACKETS = iota
|
||||
IFLA_VF_STATS_TX_PACKETS
|
||||
IFLA_VF_STATS_RX_BYTES
|
||||
IFLA_VF_STATS_TX_BYTES
|
||||
IFLA_VF_STATS_BROADCAST
|
||||
IFLA_VF_STATS_MULTICAST
|
||||
IFLA_VF_STATS_MAX = IFLA_VF_STATS_MULTICAST
|
||||
)
|
||||
|
||||
const (
|
||||
SizeofVfMac = 0x24
|
||||
SizeofVfVlan = 0x0c
|
||||
SizeofVfTxRate = 0x08
|
||||
SizeofVfRate = 0x0c
|
||||
SizeofVfSpoofchk = 0x08
|
||||
SizeofVfLinkState = 0x08
|
||||
SizeofVfRssQueryEn = 0x08
|
||||
)
|
||||
|
||||
// struct ifla_vf_mac {
|
||||
// __u32 vf;
|
||||
// __u8 mac[32]; /* MAX_ADDR_LEN */
|
||||
// };
|
||||
|
||||
type VfMac struct {
|
||||
Vf uint32
|
||||
Mac [32]byte
|
||||
}
|
||||
|
||||
func (msg *VfMac) Len() int {
|
||||
return SizeofVfMac
|
||||
}
|
||||
|
||||
func DeserializeVfMac(b []byte) *VfMac {
|
||||
return (*VfMac)(unsafe.Pointer(&b[0:SizeofVfMac][0]))
|
||||
}
|
||||
|
||||
func (msg *VfMac) Serialize() []byte {
|
||||
return (*(*[SizeofVfMac]byte)(unsafe.Pointer(msg)))[:]
|
||||
}
|
||||
|
||||
// struct ifla_vf_vlan {
|
||||
// __u32 vf;
|
||||
// __u32 vlan; /* 0 - 4095, 0 disables VLAN filter */
|
||||
// __u32 qos;
|
||||
// };
|
||||
|
||||
type VfVlan struct {
|
||||
Vf uint32
|
||||
Vlan uint32
|
||||
Qos uint32
|
||||
}
|
||||
|
||||
func (msg *VfVlan) Len() int {
|
||||
return SizeofVfVlan
|
||||
}
|
||||
|
||||
func DeserializeVfVlan(b []byte) *VfVlan {
|
||||
return (*VfVlan)(unsafe.Pointer(&b[0:SizeofVfVlan][0]))
|
||||
}
|
||||
|
||||
func (msg *VfVlan) Serialize() []byte {
|
||||
return (*(*[SizeofVfVlan]byte)(unsafe.Pointer(msg)))[:]
|
||||
}
|
||||
|
||||
// struct ifla_vf_tx_rate {
|
||||
// __u32 vf;
|
||||
// __u32 rate; /* Max TX bandwidth in Mbps, 0 disables throttling */
|
||||
// };
|
||||
|
||||
type VfTxRate struct {
|
||||
Vf uint32
|
||||
Rate uint32
|
||||
}
|
||||
|
||||
func (msg *VfTxRate) Len() int {
|
||||
return SizeofVfTxRate
|
||||
}
|
||||
|
||||
func DeserializeVfTxRate(b []byte) *VfTxRate {
|
||||
return (*VfTxRate)(unsafe.Pointer(&b[0:SizeofVfTxRate][0]))
|
||||
}
|
||||
|
||||
func (msg *VfTxRate) Serialize() []byte {
|
||||
return (*(*[SizeofVfTxRate]byte)(unsafe.Pointer(msg)))[:]
|
||||
}
|
||||
|
||||
// struct ifla_vf_rate {
|
||||
// __u32 vf;
|
||||
// __u32 min_tx_rate; /* Min Bandwidth in Mbps */
|
||||
// __u32 max_tx_rate; /* Max Bandwidth in Mbps */
|
||||
// };
|
||||
|
||||
type VfRate struct {
|
||||
Vf uint32
|
||||
MinTxRate uint32
|
||||
MaxTxRate uint32
|
||||
}
|
||||
|
||||
func (msg *VfRate) Len() int {
|
||||
return SizeofVfRate
|
||||
}
|
||||
|
||||
func DeserializeVfRate(b []byte) *VfRate {
|
||||
return (*VfRate)(unsafe.Pointer(&b[0:SizeofVfRate][0]))
|
||||
}
|
||||
|
||||
func (msg *VfRate) Serialize() []byte {
|
||||
return (*(*[SizeofVfRate]byte)(unsafe.Pointer(msg)))[:]
|
||||
}
|
||||
|
||||
// struct ifla_vf_spoofchk {
|
||||
// __u32 vf;
|
||||
// __u32 setting;
|
||||
// };
|
||||
|
||||
type VfSpoofchk struct {
|
||||
Vf uint32
|
||||
Setting uint32
|
||||
}
|
||||
|
||||
func (msg *VfSpoofchk) Len() int {
|
||||
return SizeofVfSpoofchk
|
||||
}
|
||||
|
||||
func DeserializeVfSpoofchk(b []byte) *VfSpoofchk {
|
||||
return (*VfSpoofchk)(unsafe.Pointer(&b[0:SizeofVfSpoofchk][0]))
|
||||
}
|
||||
|
||||
func (msg *VfSpoofchk) Serialize() []byte {
|
||||
return (*(*[SizeofVfSpoofchk]byte)(unsafe.Pointer(msg)))[:]
|
||||
}
|
||||
|
||||
// struct ifla_vf_link_state {
|
||||
// __u32 vf;
|
||||
// __u32 link_state;
|
||||
// };
|
||||
|
||||
type VfLinkState struct {
|
||||
Vf uint32
|
||||
LinkState uint32
|
||||
}
|
||||
|
||||
func (msg *VfLinkState) Len() int {
|
||||
return SizeofVfLinkState
|
||||
}
|
||||
|
||||
func DeserializeVfLinkState(b []byte) *VfLinkState {
|
||||
return (*VfLinkState)(unsafe.Pointer(&b[0:SizeofVfLinkState][0]))
|
||||
}
|
||||
|
||||
func (msg *VfLinkState) Serialize() []byte {
|
||||
return (*(*[SizeofVfLinkState]byte)(unsafe.Pointer(msg)))[:]
|
||||
}
|
||||
|
||||
// struct ifla_vf_rss_query_en {
|
||||
// __u32 vf;
|
||||
// __u32 setting;
|
||||
// };
|
||||
|
||||
type VfRssQueryEn struct {
|
||||
Vf uint32
|
||||
Setting uint32
|
||||
}
|
||||
|
||||
func (msg *VfRssQueryEn) Len() int {
|
||||
return SizeofVfRssQueryEn
|
||||
}
|
||||
|
||||
func DeserializeVfRssQueryEn(b []byte) *VfRssQueryEn {
|
||||
return (*VfRssQueryEn)(unsafe.Pointer(&b[0:SizeofVfRssQueryEn][0]))
|
||||
}
|
||||
|
||||
func (msg *VfRssQueryEn) Serialize() []byte {
|
||||
return (*(*[SizeofVfRssQueryEn]byte)(unsafe.Pointer(msg)))[:]
|
||||
}
|
||||
|
37
vendor/github.com/vishvananda/netlink/nl/syscall.go
generated
vendored
Normal file
37
vendor/github.com/vishvananda/netlink/nl/syscall.go
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
package nl
|
||||
|
||||
// syscall package lack of rule atributes type.
|
||||
// Thus there are defined below
|
||||
const (
|
||||
FRA_UNSPEC = iota
|
||||
FRA_DST /* destination address */
|
||||
FRA_SRC /* source address */
|
||||
FRA_IIFNAME /* interface name */
|
||||
FRA_GOTO /* target to jump to (FR_ACT_GOTO) */
|
||||
FRA_UNUSED2
|
||||
FRA_PRIORITY /* priority/preference */
|
||||
FRA_UNUSED3
|
||||
FRA_UNUSED4
|
||||
FRA_UNUSED5
|
||||
FRA_FWMARK /* mark */
|
||||
FRA_FLOW /* flow/class id */
|
||||
FRA_TUN_ID
|
||||
FRA_SUPPRESS_IFGROUP
|
||||
FRA_SUPPRESS_PREFIXLEN
|
||||
FRA_TABLE /* Extended table id */
|
||||
FRA_FWMASK /* mask for netfilter mark */
|
||||
FRA_OIFNAME
|
||||
)
|
||||
|
||||
// ip rule netlink request types
|
||||
const (
|
||||
FR_ACT_UNSPEC = iota
|
||||
FR_ACT_TO_TBL /* Pass to fixed table */
|
||||
FR_ACT_GOTO /* Jump to another rule */
|
||||
FR_ACT_NOP /* No operation */
|
||||
FR_ACT_RES3
|
||||
FR_ACT_RES4
|
||||
FR_ACT_BLACKHOLE /* Drop without notification */
|
||||
FR_ACT_UNREACHABLE /* Drop with ENETUNREACH */
|
||||
FR_ACT_PROHIBIT /* Drop with EACCES */
|
||||
)
|
23
vendor/github.com/vishvananda/netlink/route.go
generated
vendored
23
vendor/github.com/vishvananda/netlink/route.go
generated
vendored
@@ -24,17 +24,20 @@ const (
|
||||
FLAG_PERVASIVE NextHopFlag = syscall.RTNH_F_PERVASIVE
|
||||
)
|
||||
|
||||
// Route represents a netlink route. A route is associated with a link,
|
||||
// has a destination network, an optional source ip, and optional
|
||||
// gateway. Advanced route parameters and non-main routing tables are
|
||||
// currently not supported.
|
||||
// Route represents a netlink route.
|
||||
type Route struct {
|
||||
LinkIndex int
|
||||
Scope Scope
|
||||
Dst *net.IPNet
|
||||
Src net.IP
|
||||
Gw net.IP
|
||||
Flags int
|
||||
LinkIndex int
|
||||
ILinkIndex int
|
||||
Scope Scope
|
||||
Dst *net.IPNet
|
||||
Src net.IP
|
||||
Gw net.IP
|
||||
Protocol int
|
||||
Priority int
|
||||
Table int
|
||||
Type int
|
||||
Tos int
|
||||
Flags int
|
||||
}
|
||||
|
||||
func (r Route) String() string {
|
||||
|
132
vendor/github.com/vishvananda/netlink/route_linux.go
generated
vendored
132
vendor/github.com/vishvananda/netlink/route_linux.go
generated
vendored
@@ -10,6 +10,19 @@ import (
|
||||
|
||||
// RtAttr is shared so it is in netlink_linux.go
|
||||
|
||||
const (
|
||||
RT_FILTER_PROTOCOL uint64 = 1 << (1 + iota)
|
||||
RT_FILTER_SCOPE
|
||||
RT_FILTER_TYPE
|
||||
RT_FILTER_TOS
|
||||
RT_FILTER_IIF
|
||||
RT_FILTER_OIF
|
||||
RT_FILTER_DST
|
||||
RT_FILTER_SRC
|
||||
RT_FILTER_GW
|
||||
RT_FILTER_TABLE
|
||||
)
|
||||
|
||||
// RouteAdd will add a route to the system.
|
||||
// Equivalent to: `ip route add $route`
|
||||
func RouteAdd(route *Route) error {
|
||||
@@ -29,8 +42,6 @@ func routeHandle(route *Route, req *nl.NetlinkRequest, msg *nl.RtMsg) error {
|
||||
return fmt.Errorf("one of Dst.IP, Src, or Gw must not be nil")
|
||||
}
|
||||
|
||||
msg.Scope = uint8(route.Scope)
|
||||
msg.Flags = uint32(route.Flags)
|
||||
family := -1
|
||||
var rtAttrs []*nl.RtAttr
|
||||
|
||||
@@ -79,8 +90,34 @@ func routeHandle(route *Route, req *nl.NetlinkRequest, msg *nl.RtMsg) error {
|
||||
rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_GATEWAY, gwData))
|
||||
}
|
||||
|
||||
msg.Family = uint8(family)
|
||||
if route.Table > 0 {
|
||||
if route.Table >= 256 {
|
||||
msg.Table = syscall.RT_TABLE_UNSPEC
|
||||
b := make([]byte, 4)
|
||||
native.PutUint32(b, uint32(route.Table))
|
||||
rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_TABLE, b))
|
||||
} else {
|
||||
msg.Table = uint8(route.Table)
|
||||
}
|
||||
}
|
||||
|
||||
if route.Priority > 0 {
|
||||
b := make([]byte, 4)
|
||||
native.PutUint32(b, uint32(route.Priority))
|
||||
rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_PRIORITY, b))
|
||||
}
|
||||
if route.Tos > 0 {
|
||||
msg.Tos = uint8(route.Tos)
|
||||
}
|
||||
if route.Protocol > 0 {
|
||||
msg.Protocol = uint8(route.Protocol)
|
||||
}
|
||||
if route.Type > 0 {
|
||||
msg.Type = uint8(route.Type)
|
||||
}
|
||||
|
||||
msg.Scope = uint8(route.Scope)
|
||||
msg.Family = uint8(family)
|
||||
req.AddData(msg)
|
||||
for _, attr := range rtAttrs {
|
||||
req.AddData(attr)
|
||||
@@ -102,61 +139,95 @@ func routeHandle(route *Route, req *nl.NetlinkRequest, msg *nl.RtMsg) error {
|
||||
// Equivalent to: `ip route show`.
|
||||
// The list can be filtered by link and ip family.
|
||||
func RouteList(link Link, family int) ([]Route, error) {
|
||||
var routeFilter *Route
|
||||
if link != nil {
|
||||
routeFilter = &Route{
|
||||
LinkIndex: link.Attrs().Index,
|
||||
}
|
||||
}
|
||||
return RouteListFiltered(family, routeFilter, RT_FILTER_OIF)
|
||||
}
|
||||
|
||||
// RouteListFiltered gets a list of routes in the system filtered with specified rules.
|
||||
// All rules must be defined in RouteFilter struct
|
||||
func RouteListFiltered(family int, filter *Route, filterMask uint64) ([]Route, error) {
|
||||
req := nl.NewNetlinkRequest(syscall.RTM_GETROUTE, syscall.NLM_F_DUMP)
|
||||
msg := nl.NewIfInfomsg(family)
|
||||
req.AddData(msg)
|
||||
infmsg := nl.NewIfInfomsg(family)
|
||||
req.AddData(infmsg)
|
||||
|
||||
msgs, err := req.Execute(syscall.NETLINK_ROUTE, syscall.RTM_NEWROUTE)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
index := 0
|
||||
if link != nil {
|
||||
base := link.Attrs()
|
||||
ensureIndex(base)
|
||||
index = base.Index
|
||||
}
|
||||
|
||||
var res []Route
|
||||
for _, m := range msgs {
|
||||
msg := nl.DeserializeRtMsg(m)
|
||||
|
||||
if msg.Flags&syscall.RTM_F_CLONED != 0 {
|
||||
// Ignore cloned routes
|
||||
continue
|
||||
}
|
||||
|
||||
if msg.Table != syscall.RT_TABLE_MAIN {
|
||||
// Ignore non-main tables
|
||||
continue
|
||||
if filter == nil || filter != nil && filterMask&RT_FILTER_TABLE == 0 {
|
||||
// Ignore non-main tables
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
route, err := deserializeRoute(m)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if link != nil && route.LinkIndex != index {
|
||||
// Ignore routes from other interfaces
|
||||
continue
|
||||
if filter != nil {
|
||||
switch {
|
||||
case filterMask&RT_FILTER_TABLE != 0 && route.Table != filter.Table:
|
||||
continue
|
||||
case filterMask&RT_FILTER_PROTOCOL != 0 && route.Protocol != filter.Protocol:
|
||||
continue
|
||||
case filterMask&RT_FILTER_SCOPE != 0 && route.Scope != filter.Scope:
|
||||
continue
|
||||
case filterMask&RT_FILTER_TYPE != 0 && route.Type != filter.Type:
|
||||
continue
|
||||
case filterMask&RT_FILTER_TOS != 0 && route.Tos != filter.Tos:
|
||||
continue
|
||||
case filterMask&RT_FILTER_OIF != 0 && route.LinkIndex != filter.LinkIndex:
|
||||
continue
|
||||
case filterMask&RT_FILTER_IIF != 0 && route.ILinkIndex != filter.ILinkIndex:
|
||||
continue
|
||||
case filterMask&RT_FILTER_GW != 0 && !route.Gw.Equal(filter.Gw):
|
||||
continue
|
||||
case filterMask&RT_FILTER_SRC != 0 && !route.Src.Equal(filter.Src):
|
||||
continue
|
||||
case filterMask&RT_FILTER_DST != 0 && filter.Dst != nil:
|
||||
if route.Dst == nil {
|
||||
continue
|
||||
}
|
||||
aMaskLen, aMaskBits := route.Dst.Mask.Size()
|
||||
bMaskLen, bMaskBits := filter.Dst.Mask.Size()
|
||||
if !(route.Dst.IP.Equal(filter.Dst.IP) && aMaskLen == bMaskLen && aMaskBits == bMaskBits) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
res = append(res, route)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// deserializeRoute decodes a binary netlink message into a Route struct
|
||||
func deserializeRoute(m []byte) (Route, error) {
|
||||
route := Route{}
|
||||
msg := nl.DeserializeRtMsg(m)
|
||||
attrs, err := nl.ParseRouteAttr(m[msg.Len():])
|
||||
if err != nil {
|
||||
return route, err
|
||||
return Route{}, err
|
||||
}
|
||||
route := Route{
|
||||
Scope: Scope(msg.Scope),
|
||||
Protocol: int(msg.Protocol),
|
||||
Table: int(msg.Table),
|
||||
Type: int(msg.Type),
|
||||
Tos: int(msg.Tos),
|
||||
Flags: int(msg.Flags),
|
||||
}
|
||||
route.Scope = Scope(msg.Scope)
|
||||
route.Flags = int(msg.Flags)
|
||||
|
||||
native := nl.NativeEndian()
|
||||
for _, attr := range attrs {
|
||||
@@ -171,8 +242,13 @@ func deserializeRoute(m []byte) (Route, error) {
|
||||
Mask: net.CIDRMask(int(msg.Dst_len), 8*len(attr.Value)),
|
||||
}
|
||||
case syscall.RTA_OIF:
|
||||
routeIndex := int(native.Uint32(attr.Value[0:4]))
|
||||
route.LinkIndex = routeIndex
|
||||
route.LinkIndex = int(native.Uint32(attr.Value[0:4]))
|
||||
case syscall.RTA_IIF:
|
||||
route.ILinkIndex = int(native.Uint32(attr.Value[0:4]))
|
||||
case syscall.RTA_PRIORITY:
|
||||
route.Priority = int(native.Uint32(attr.Value[0:4]))
|
||||
case syscall.RTA_TABLE:
|
||||
route.Table = int(native.Uint32(attr.Value[0:4]))
|
||||
}
|
||||
}
|
||||
return route, nil
|
||||
|
43
vendor/github.com/vishvananda/netlink/rule.go
generated
vendored
Normal file
43
vendor/github.com/vishvananda/netlink/rule.go
generated
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
package netlink
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/vishvananda/netlink/nl"
|
||||
)
|
||||
|
||||
// Rule represents a netlink rule.
|
||||
type Rule struct {
|
||||
*nl.RtMsg
|
||||
Priority int
|
||||
Table int
|
||||
Mark int
|
||||
Mask int
|
||||
TunID uint
|
||||
Goto int
|
||||
Src *net.IPNet
|
||||
Dst *net.IPNet
|
||||
Flow int
|
||||
IifName string
|
||||
OifName string
|
||||
SuppressIfgroup int
|
||||
SuppressPrefixlen int
|
||||
}
|
||||
|
||||
func (r Rule) String() string {
|
||||
return fmt.Sprintf("ip rule %d: from %s table %d", r.Priority, r.Src, r.Table)
|
||||
}
|
||||
|
||||
// NewRule return empty rules.
|
||||
func NewRule() *Rule {
|
||||
return &Rule{
|
||||
SuppressIfgroup: -1,
|
||||
SuppressPrefixlen: -1,
|
||||
Priority: -1,
|
||||
Mark: -1,
|
||||
Mask: -1,
|
||||
Goto: -1,
|
||||
Flow: -1,
|
||||
}
|
||||
}
|
198
vendor/github.com/vishvananda/netlink/rule_linux.go
generated
vendored
Normal file
198
vendor/github.com/vishvananda/netlink/rule_linux.go
generated
vendored
Normal file
@@ -0,0 +1,198 @@
|
||||
package netlink
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"syscall"
|
||||
|
||||
"github.com/vishvananda/netlink/nl"
|
||||
)
|
||||
|
||||
// RuleAdd adds a rule to the system.
|
||||
// Equivalent to: ip rule add
|
||||
func RuleAdd(rule *Rule) error {
|
||||
req := nl.NewNetlinkRequest(syscall.RTM_NEWRULE, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
|
||||
return ruleHandle(rule, req)
|
||||
}
|
||||
|
||||
// RuleDel deletes a rule from the system.
|
||||
// Equivalent to: ip rule del
|
||||
func RuleDel(rule *Rule) error {
|
||||
req := nl.NewNetlinkRequest(syscall.RTM_DELRULE, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
|
||||
return ruleHandle(rule, req)
|
||||
}
|
||||
|
||||
func ruleHandle(rule *Rule, req *nl.NetlinkRequest) error {
|
||||
msg := nl.NewRtMsg()
|
||||
msg.Family = syscall.AF_INET
|
||||
var dstFamily uint8
|
||||
|
||||
var rtAttrs []*nl.RtAttr
|
||||
if rule.Dst != nil && rule.Dst.IP != nil {
|
||||
dstLen, _ := rule.Dst.Mask.Size()
|
||||
msg.Dst_len = uint8(dstLen)
|
||||
msg.Family = uint8(nl.GetIPFamily(rule.Dst.IP))
|
||||
dstFamily = msg.Family
|
||||
var dstData []byte
|
||||
if msg.Family == syscall.AF_INET {
|
||||
dstData = rule.Dst.IP.To4()
|
||||
} else {
|
||||
dstData = rule.Dst.IP.To16()
|
||||
}
|
||||
rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_DST, dstData))
|
||||
}
|
||||
|
||||
if rule.Src != nil && rule.Src.IP != nil {
|
||||
msg.Family = uint8(nl.GetIPFamily(rule.Src.IP))
|
||||
if dstFamily != 0 && dstFamily != msg.Family {
|
||||
return fmt.Errorf("source and destination ip are not the same IP family")
|
||||
}
|
||||
srcLen, _ := rule.Src.Mask.Size()
|
||||
msg.Src_len = uint8(srcLen)
|
||||
var srcData []byte
|
||||
if msg.Family == syscall.AF_INET {
|
||||
srcData = rule.Src.IP.To4()
|
||||
} else {
|
||||
srcData = rule.Src.IP.To16()
|
||||
}
|
||||
rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_SRC, srcData))
|
||||
}
|
||||
|
||||
if rule.Table >= 0 {
|
||||
msg.Table = uint8(rule.Table)
|
||||
if rule.Table >= 256 {
|
||||
msg.Table = syscall.RT_TABLE_UNSPEC
|
||||
}
|
||||
}
|
||||
|
||||
req.AddData(msg)
|
||||
for i := range rtAttrs {
|
||||
req.AddData(rtAttrs[i])
|
||||
}
|
||||
|
||||
var (
|
||||
b = make([]byte, 4)
|
||||
native = nl.NativeEndian()
|
||||
)
|
||||
|
||||
if rule.Priority >= 0 {
|
||||
native.PutUint32(b, uint32(rule.Priority))
|
||||
req.AddData(nl.NewRtAttr(nl.FRA_PRIORITY, b))
|
||||
}
|
||||
if rule.Mark >= 0 {
|
||||
native.PutUint32(b, uint32(rule.Mark))
|
||||
req.AddData(nl.NewRtAttr(nl.FRA_FWMARK, b))
|
||||
}
|
||||
if rule.Mask >= 0 {
|
||||
native.PutUint32(b, uint32(rule.Mask))
|
||||
req.AddData(nl.NewRtAttr(nl.FRA_FWMASK, b))
|
||||
}
|
||||
if rule.Flow >= 0 {
|
||||
native.PutUint32(b, uint32(rule.Flow))
|
||||
req.AddData(nl.NewRtAttr(nl.FRA_FLOW, b))
|
||||
}
|
||||
if rule.TunID > 0 {
|
||||
native.PutUint32(b, uint32(rule.TunID))
|
||||
req.AddData(nl.NewRtAttr(nl.FRA_TUN_ID, b))
|
||||
}
|
||||
if rule.Table >= 256 {
|
||||
native.PutUint32(b, uint32(rule.Table))
|
||||
req.AddData(nl.NewRtAttr(nl.FRA_TABLE, b))
|
||||
}
|
||||
if msg.Table > 0 {
|
||||
if rule.SuppressPrefixlen >= 0 {
|
||||
native.PutUint32(b, uint32(rule.SuppressPrefixlen))
|
||||
req.AddData(nl.NewRtAttr(nl.FRA_SUPPRESS_PREFIXLEN, b))
|
||||
}
|
||||
if rule.SuppressIfgroup >= 0 {
|
||||
native.PutUint32(b, uint32(rule.SuppressIfgroup))
|
||||
req.AddData(nl.NewRtAttr(nl.FRA_SUPPRESS_IFGROUP, b))
|
||||
}
|
||||
}
|
||||
if rule.IifName != "" {
|
||||
req.AddData(nl.NewRtAttr(nl.FRA_IIFNAME, []byte(rule.IifName)))
|
||||
}
|
||||
if rule.OifName != "" {
|
||||
req.AddData(nl.NewRtAttr(nl.FRA_OIFNAME, []byte(rule.OifName)))
|
||||
}
|
||||
if rule.Goto >= 0 {
|
||||
msg.Type = nl.FR_ACT_NOP
|
||||
native.PutUint32(b, uint32(rule.Goto))
|
||||
req.AddData(nl.NewRtAttr(nl.FRA_GOTO, b))
|
||||
}
|
||||
|
||||
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// RuleList lists rules in the system.
|
||||
// Equivalent to: ip rule list
|
||||
func RuleList(family int) ([]Rule, error) {
|
||||
req := nl.NewNetlinkRequest(syscall.RTM_GETRULE, syscall.NLM_F_DUMP|syscall.NLM_F_REQUEST)
|
||||
msg := nl.NewIfInfomsg(family)
|
||||
req.AddData(msg)
|
||||
|
||||
msgs, err := req.Execute(syscall.NETLINK_ROUTE, syscall.RTM_NEWRULE)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
native := nl.NativeEndian()
|
||||
var res = make([]Rule, 0)
|
||||
for i := range msgs {
|
||||
msg := nl.DeserializeRtMsg(msgs[i])
|
||||
attrs, err := nl.ParseRouteAttr(msgs[i][msg.Len():])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rule := NewRule()
|
||||
rule.RtMsg = msg
|
||||
|
||||
for j := range attrs {
|
||||
switch attrs[j].Attr.Type {
|
||||
case syscall.RTA_TABLE:
|
||||
rule.Table = int(native.Uint32(attrs[j].Value[0:4]))
|
||||
case nl.FRA_SRC:
|
||||
rule.Src = &net.IPNet{
|
||||
IP: attrs[j].Value,
|
||||
Mask: net.CIDRMask(int(msg.Src_len), 8*len(attrs[j].Value)),
|
||||
}
|
||||
case nl.FRA_DST:
|
||||
rule.Dst = &net.IPNet{
|
||||
IP: attrs[j].Value,
|
||||
Mask: net.CIDRMask(int(msg.Dst_len), 8*len(attrs[j].Value)),
|
||||
}
|
||||
case nl.FRA_FWMARK:
|
||||
rule.Mark = int(native.Uint32(attrs[j].Value[0:4]))
|
||||
case nl.FRA_FWMASK:
|
||||
rule.Mask = int(native.Uint32(attrs[j].Value[0:4]))
|
||||
case nl.FRA_TUN_ID:
|
||||
rule.TunID = uint(native.Uint64(attrs[j].Value[0:4]))
|
||||
case nl.FRA_IIFNAME:
|
||||
rule.IifName = string(attrs[j].Value[:len(attrs[j].Value)-1])
|
||||
case nl.FRA_OIFNAME:
|
||||
rule.OifName = string(attrs[j].Value[:len(attrs[j].Value)-1])
|
||||
case nl.FRA_SUPPRESS_PREFIXLEN:
|
||||
i := native.Uint32(attrs[j].Value[0:4])
|
||||
if i != 0xffffffff {
|
||||
rule.SuppressPrefixlen = int(i)
|
||||
}
|
||||
case nl.FRA_SUPPRESS_IFGROUP:
|
||||
i := native.Uint32(attrs[j].Value[0:4])
|
||||
if i != 0xffffffff {
|
||||
rule.SuppressIfgroup = int(i)
|
||||
}
|
||||
case nl.FRA_FLOW:
|
||||
rule.Flow = int(native.Uint32(attrs[j].Value[0:4]))
|
||||
case nl.FRA_GOTO:
|
||||
rule.Goto = int(native.Uint32(attrs[j].Value[0:4]))
|
||||
case nl.FRA_PRIORITY:
|
||||
rule.Priority = int(native.Uint32(attrs[j].Value[0:4]))
|
||||
}
|
||||
}
|
||||
res = append(res, *rule)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
3
vendor/github.com/vishvananda/netlink/xfrm_state_linux.go
generated
vendored
3
vendor/github.com/vishvananda/netlink/xfrm_state_linux.go
generated
vendored
@@ -110,9 +110,6 @@ func XfrmStateDel(state *XfrmState) error {
|
||||
func XfrmStateList(family int) ([]XfrmState, error) {
|
||||
req := nl.NewNetlinkRequest(nl.XFRM_MSG_GETSA, syscall.NLM_F_DUMP)
|
||||
|
||||
msg := nl.NewIfInfomsg(family)
|
||||
req.AddData(msg)
|
||||
|
||||
msgs, err := req.Execute(syscall.NETLINK_XFRM, nl.XFRM_MSG_NEWSA)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
Reference in New Issue
Block a user