2014-09-01 03:27:34 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
2014-09-16 00:44:20 +00:00
|
|
|
"bytes"
|
2014-09-01 03:27:34 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"syscall"
|
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
"github.com/vishvananda/netlink/nl"
|
2014-09-01 03:27:34 +00:00
|
|
|
)
|
|
|
|
|
2014-10-14 01:07:39 +00:00
|
|
|
var native = nl.NativeEndian()
|
2014-10-28 21:15:07 +00:00
|
|
|
var lookupByDump = false
|
2014-10-14 01:07:39 +00:00
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
func ensureIndex(link *LinkAttrs) {
|
2014-09-01 03:27:34 +00:00
|
|
|
if link != nil && link.Index == 0 {
|
|
|
|
newlink, _ := LinkByName(link.Name)
|
|
|
|
if newlink != nil {
|
2014-09-24 05:21:11 +00:00
|
|
|
link.Index = newlink.Attrs().Index
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LinkSetUp enables the link device.
|
|
|
|
// Equivalent to: `ip link set $link up`
|
2014-09-24 05:21:11 +00:00
|
|
|
func LinkSetUp(link Link) error {
|
|
|
|
base := link.Attrs()
|
|
|
|
ensureIndex(base)
|
2014-09-19 02:04:48 +00:00
|
|
|
req := nl.NewNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_ACK)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
|
2014-09-01 03:27:34 +00:00
|
|
|
msg.Change = syscall.IFF_UP
|
|
|
|
msg.Flags = syscall.IFF_UP
|
2014-09-24 05:21:11 +00:00
|
|
|
msg.Index = int32(base.Index)
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(msg)
|
|
|
|
|
|
|
|
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// LinkSetUp disables link device.
|
|
|
|
// Equivalent to: `ip link set $link down`
|
2014-09-24 05:21:11 +00:00
|
|
|
func LinkSetDown(link Link) error {
|
|
|
|
base := link.Attrs()
|
|
|
|
ensureIndex(base)
|
2014-09-19 02:04:48 +00:00
|
|
|
req := nl.NewNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_ACK)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
|
2014-09-01 03:27:34 +00:00
|
|
|
msg.Change = syscall.IFF_UP
|
|
|
|
msg.Flags = 0 & ^syscall.IFF_UP
|
2014-09-24 05:21:11 +00:00
|
|
|
msg.Index = int32(base.Index)
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(msg)
|
|
|
|
|
|
|
|
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// LinkSetMTU sets the mtu of the link device.
|
|
|
|
// Equivalent to: `ip link set $link mtu $mtu`
|
2014-09-24 05:21:11 +00:00
|
|
|
func LinkSetMTU(link Link, mtu int) error {
|
|
|
|
base := link.Attrs()
|
|
|
|
ensureIndex(base)
|
2014-09-19 02:04:48 +00:00
|
|
|
req := nl.NewNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
|
2014-09-01 03:27:34 +00:00
|
|
|
msg.Type = syscall.RTM_SETLINK
|
|
|
|
msg.Flags = syscall.NLM_F_REQUEST
|
2014-09-24 05:21:11 +00:00
|
|
|
msg.Index = int32(base.Index)
|
2014-09-19 02:04:48 +00:00
|
|
|
msg.Change = nl.DEFAULT_CHANGE
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(msg)
|
|
|
|
|
2014-10-14 01:07:39 +00:00
|
|
|
b := make([]byte, 4)
|
2014-09-01 03:27:34 +00:00
|
|
|
native.PutUint32(b, uint32(mtu))
|
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
data := nl.NewRtAttr(syscall.IFLA_MTU, b)
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(data)
|
|
|
|
|
|
|
|
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-31 19:55:10 +00:00
|
|
|
// LinkSetName sets the name of the link device.
|
|
|
|
// Equivalent to: `ip link set $link name $name`
|
|
|
|
func LinkSetName(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.Type = syscall.RTM_SETLINK
|
|
|
|
msg.Flags = syscall.NLM_F_REQUEST
|
|
|
|
msg.Index = int32(base.Index)
|
|
|
|
msg.Change = nl.DEFAULT_CHANGE
|
|
|
|
req.AddData(msg)
|
|
|
|
|
|
|
|
data := nl.NewRtAttr(syscall.IFLA_IFNAME, []byte(name))
|
|
|
|
req.AddData(data)
|
|
|
|
|
|
|
|
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-31 20:05:11 +00:00
|
|
|
// 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 {
|
|
|
|
base := link.Attrs()
|
|
|
|
ensureIndex(base)
|
|
|
|
req := nl.NewNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
|
|
|
|
|
|
|
|
msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
|
|
|
|
msg.Type = syscall.RTM_SETLINK
|
|
|
|
msg.Flags = syscall.NLM_F_REQUEST
|
|
|
|
msg.Index = int32(base.Index)
|
|
|
|
msg.Change = nl.DEFAULT_CHANGE
|
|
|
|
req.AddData(msg)
|
|
|
|
|
|
|
|
data := nl.NewRtAttr(syscall.IFLA_ADDRESS, []byte(hwaddr))
|
|
|
|
req.AddData(data)
|
|
|
|
|
|
|
|
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
// LinkSetMaster sets the master of the link device.
|
2014-09-01 03:27:34 +00:00
|
|
|
// Equivalent to: `ip link set $link master $master`
|
2014-09-24 05:21:11 +00:00
|
|
|
func LinkSetMaster(link Link, master *Bridge) error {
|
|
|
|
index := 0
|
|
|
|
if master != nil {
|
|
|
|
masterBase := master.Attrs()
|
|
|
|
ensureIndex(masterBase)
|
|
|
|
index = masterBase.Index
|
|
|
|
}
|
|
|
|
return LinkSetMasterByIndex(link, index)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LinkSetMasterByIndex sets the master of the link device.
|
|
|
|
// Equivalent to: `ip link set $link master $master`
|
|
|
|
func LinkSetMasterByIndex(link Link, masterIndex int) error {
|
|
|
|
base := link.Attrs()
|
|
|
|
ensureIndex(base)
|
2014-09-19 02:04:48 +00:00
|
|
|
req := nl.NewNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
|
2014-09-01 03:27:34 +00:00
|
|
|
msg.Type = syscall.RTM_SETLINK
|
|
|
|
msg.Flags = syscall.NLM_F_REQUEST
|
2014-09-24 05:21:11 +00:00
|
|
|
msg.Index = int32(base.Index)
|
2014-09-19 02:04:48 +00:00
|
|
|
msg.Change = nl.DEFAULT_CHANGE
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(msg)
|
|
|
|
|
2014-10-14 01:07:39 +00:00
|
|
|
b := make([]byte, 4)
|
2014-09-24 05:21:11 +00:00
|
|
|
native.PutUint32(b, uint32(masterIndex))
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
data := nl.NewRtAttr(syscall.IFLA_MASTER, b)
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(data)
|
|
|
|
|
|
|
|
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// LinkSetNsPid puts the device into a new network namespace. The
|
|
|
|
// pid must be a pid of a running process.
|
|
|
|
// Equivalent to: `ip link set $link netns $pid`
|
2014-09-24 05:21:11 +00:00
|
|
|
func LinkSetNsPid(link Link, nspid int) error {
|
|
|
|
base := link.Attrs()
|
|
|
|
ensureIndex(base)
|
2014-09-19 02:04:48 +00:00
|
|
|
req := nl.NewNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
|
2014-09-01 03:27:34 +00:00
|
|
|
msg.Type = syscall.RTM_SETLINK
|
|
|
|
msg.Flags = syscall.NLM_F_REQUEST
|
2014-09-24 05:21:11 +00:00
|
|
|
msg.Index = int32(base.Index)
|
2014-09-19 02:04:48 +00:00
|
|
|
msg.Change = nl.DEFAULT_CHANGE
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(msg)
|
|
|
|
|
2014-10-14 01:07:39 +00:00
|
|
|
b := make([]byte, 4)
|
2014-09-01 03:27:34 +00:00
|
|
|
native.PutUint32(b, uint32(nspid))
|
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
data := nl.NewRtAttr(syscall.IFLA_NET_NS_PID, b)
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(data)
|
|
|
|
|
|
|
|
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// LinkSetNsPid puts the device into a new network namespace. The
|
|
|
|
// fd must be an open file descriptor to a network namespace.
|
|
|
|
// Similar to: `ip link set $link netns $ns`
|
2014-09-24 05:21:11 +00:00
|
|
|
func LinkSetNsFd(link Link, fd int) error {
|
|
|
|
base := link.Attrs()
|
|
|
|
ensureIndex(base)
|
2014-09-19 02:04:48 +00:00
|
|
|
req := nl.NewNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
|
2014-09-01 03:27:34 +00:00
|
|
|
msg.Type = syscall.RTM_SETLINK
|
|
|
|
msg.Flags = syscall.NLM_F_REQUEST
|
2014-09-24 05:21:11 +00:00
|
|
|
msg.Index = int32(base.Index)
|
2014-09-19 02:04:48 +00:00
|
|
|
msg.Change = nl.DEFAULT_CHANGE
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(msg)
|
|
|
|
|
2014-10-14 01:07:39 +00:00
|
|
|
b := make([]byte, 4)
|
2014-09-01 03:27:34 +00:00
|
|
|
native.PutUint32(b, uint32(fd))
|
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
data := nl.NewRtAttr(nl.IFLA_NET_NS_FD, b)
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(data)
|
|
|
|
|
|
|
|
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-09-16 00:44:20 +00:00
|
|
|
func boolAttr(val bool) []byte {
|
|
|
|
var v uint8
|
|
|
|
if val {
|
|
|
|
v = 1
|
|
|
|
}
|
|
|
|
return nl.Uint8Attr(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
type vxlanPortRange struct {
|
2014-10-29 00:22:52 +00:00
|
|
|
Lo, Hi uint16
|
|
|
|
}
|
2014-09-16 00:44:20 +00:00
|
|
|
|
2014-10-29 00:22:52 +00:00
|
|
|
func addVxlanAttrs(vxlan *Vxlan, linkInfo *nl.RtAttr) {
|
2014-09-16 00:44:20 +00:00
|
|
|
data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
|
|
|
|
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_ID, nl.Uint32Attr(uint32(vxlan.VxlanId)))
|
2014-10-19 18:36:05 +00:00
|
|
|
if vxlan.VtepDevIndex != 0 {
|
2014-09-16 00:44:20 +00:00
|
|
|
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_LINK, nl.Uint32Attr(uint32(vxlan.VtepDevIndex)))
|
|
|
|
}
|
|
|
|
if vxlan.SrcAddr != nil {
|
|
|
|
ip := vxlan.SrcAddr.To4()
|
|
|
|
if ip != nil {
|
|
|
|
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_LOCAL, []byte(ip))
|
|
|
|
} else {
|
|
|
|
ip = vxlan.SrcAddr.To16()
|
|
|
|
if ip != nil {
|
|
|
|
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_LOCAL6, []byte(ip))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if vxlan.Group != nil {
|
|
|
|
group := vxlan.Group.To4()
|
|
|
|
if group != nil {
|
|
|
|
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_GROUP, []byte(group))
|
|
|
|
} else {
|
|
|
|
group = vxlan.Group.To16()
|
|
|
|
if group != nil {
|
|
|
|
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_GROUP6, []byte(group))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_TTL, nl.Uint8Attr(uint8(vxlan.TTL)))
|
|
|
|
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_TOS, nl.Uint8Attr(uint8(vxlan.TOS)))
|
|
|
|
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_LEARNING, boolAttr(vxlan.Learning))
|
|
|
|
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_PROXY, boolAttr(vxlan.Proxy))
|
|
|
|
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_RSC, boolAttr(vxlan.RSC))
|
|
|
|
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_L2MISS, boolAttr(vxlan.L2miss))
|
|
|
|
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_L3MISS, boolAttr(vxlan.L3miss))
|
|
|
|
|
|
|
|
if vxlan.NoAge {
|
|
|
|
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_AGEING, nl.Uint32Attr(0))
|
|
|
|
} else if vxlan.Age > 0 {
|
|
|
|
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_AGEING, nl.Uint32Attr(uint32(vxlan.Age)))
|
|
|
|
}
|
|
|
|
if vxlan.Limit > 0 {
|
|
|
|
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_LIMIT, nl.Uint32Attr(uint32(vxlan.Limit)))
|
|
|
|
}
|
|
|
|
if vxlan.Port > 0 {
|
|
|
|
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_PORT, nl.Uint16Attr(uint16(vxlan.Port)))
|
|
|
|
}
|
|
|
|
if vxlan.PortLow > 0 || vxlan.PortHigh > 0 {
|
2014-10-29 00:22:52 +00:00
|
|
|
pr := vxlanPortRange{uint16(vxlan.PortLow), uint16(vxlan.PortHigh)}
|
2014-09-16 00:44:20 +00:00
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
binary.Write(buf, binary.BigEndian, &pr)
|
|
|
|
|
|
|
|
nl.NewRtAttrChild(data, nl.IFLA_VXLAN_PORT_RANGE, buf.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-01 03:27:34 +00:00
|
|
|
// 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`
|
2014-09-24 05:21:11 +00:00
|
|
|
func LinkAdd(link Link) error {
|
2014-09-01 03:27:34 +00:00
|
|
|
// TODO: set mtu and hardware address
|
|
|
|
// TODO: support extra data for macvlan
|
2014-09-24 05:21:11 +00:00
|
|
|
base := link.Attrs()
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
if base.Name == "" {
|
|
|
|
return fmt.Errorf("LinkAttrs.Name cannot be empty!")
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
req := nl.NewNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(msg)
|
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
if base.ParentIndex != 0 {
|
2014-09-01 03:27:34 +00:00
|
|
|
b := make([]byte, 4)
|
2014-09-24 05:21:11 +00:00
|
|
|
native.PutUint32(b, uint32(base.ParentIndex))
|
2014-09-19 02:04:48 +00:00
|
|
|
data := nl.NewRtAttr(syscall.IFLA_LINK, b)
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(data)
|
2015-02-10 23:00:36 +00:00
|
|
|
} else if link.Type() == "ipvlan" {
|
|
|
|
return fmt.Errorf("Can't create ipvlan link without ParentIndex")
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
nameData := nl.NewRtAttr(syscall.IFLA_IFNAME, nl.ZeroTerminated(base.Name))
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(nameData)
|
|
|
|
|
2015-02-07 01:49:37 +00:00
|
|
|
if base.MTU > 0 {
|
|
|
|
mtu := nl.NewRtAttr(syscall.IFLA_MTU, nl.Uint32Attr(uint32(base.MTU)))
|
|
|
|
req.AddData(mtu)
|
|
|
|
}
|
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
linkInfo := nl.NewRtAttr(syscall.IFLA_LINKINFO, nil)
|
2014-09-24 05:21:11 +00:00
|
|
|
nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_KIND, nl.NonZeroTerminated(link.Type()))
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2014-12-05 01:01:33 +00:00
|
|
|
nl.NewRtAttrChild(linkInfo, syscall.IFLA_TXQLEN, nl.Uint32Attr(base.TxQLen))
|
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
if vlan, ok := link.(*Vlan); ok {
|
2014-09-01 03:27:34 +00:00
|
|
|
b := make([]byte, 2)
|
2014-09-24 05:21:11 +00:00
|
|
|
native.PutUint16(b, uint16(vlan.VlanId))
|
2014-09-19 02:04:48 +00:00
|
|
|
data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
|
|
|
|
nl.NewRtAttrChild(data, nl.IFLA_VLAN_ID, b)
|
2014-09-24 05:21:11 +00:00
|
|
|
} else if veth, ok := link.(*Veth); ok {
|
2014-09-19 02:04:48 +00:00
|
|
|
data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
|
|
|
|
peer := nl.NewRtAttrChild(data, nl.VETH_INFO_PEER, nil)
|
|
|
|
nl.NewIfInfomsgChild(peer, syscall.AF_UNSPEC)
|
2014-09-24 05:21:11 +00:00
|
|
|
nl.NewRtAttrChild(peer, syscall.IFLA_IFNAME, nl.ZeroTerminated(veth.PeerName))
|
2014-12-05 01:01:33 +00:00
|
|
|
nl.NewRtAttrChild(peer, syscall.IFLA_TXQLEN, nl.Uint32Attr(base.TxQLen))
|
2015-02-07 01:49:37 +00:00
|
|
|
if base.MTU > 0 {
|
|
|
|
nl.NewRtAttrChild(peer, syscall.IFLA_MTU, nl.Uint32Attr(uint32(base.MTU)))
|
|
|
|
}
|
2014-09-16 00:44:20 +00:00
|
|
|
} else if vxlan, ok := link.(*Vxlan); ok {
|
|
|
|
addVxlanAttrs(vxlan, linkInfo)
|
2015-02-10 23:00:36 +00:00
|
|
|
} 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)))
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req.AddData(linkInfo)
|
|
|
|
|
|
|
|
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
ensureIndex(base)
|
|
|
|
|
2014-09-01 03:27:34 +00:00
|
|
|
// can't set master during create, so set it afterwards
|
2014-09-24 05:21:11 +00:00
|
|
|
if base.MasterIndex != 0 {
|
|
|
|
// TODO: verify MasterIndex is actually a bridge?
|
|
|
|
return LinkSetMasterByIndex(link, base.MasterIndex)
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LinkAdd adds a new link device. Either Index or Name must be set in
|
|
|
|
// the link object for it to be deleted. The other values are ignored.
|
|
|
|
// Equivalent to: `ip link del $link`
|
2014-09-24 05:21:11 +00:00
|
|
|
func LinkDel(link Link) error {
|
|
|
|
base := link.Attrs()
|
|
|
|
|
|
|
|
ensureIndex(base)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
req := nl.NewNetlinkRequest(syscall.RTM_DELLINK, syscall.NLM_F_ACK)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
|
2014-09-24 05:21:11 +00:00
|
|
|
msg.Index = int32(base.Index)
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(msg)
|
|
|
|
|
|
|
|
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-10-28 21:15:07 +00:00
|
|
|
func linkByNameDump(name string) (Link, error) {
|
|
|
|
links, err := LinkList()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, link := range links {
|
|
|
|
if link.Attrs().Name == name {
|
|
|
|
return link, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Link %s not found", name)
|
|
|
|
}
|
|
|
|
|
2014-10-14 02:01:36 +00:00
|
|
|
// LinkByName finds a link by name and returns a pointer to the object.
|
2014-09-24 05:21:11 +00:00
|
|
|
func LinkByName(name string) (Link, error) {
|
2014-10-28 21:15:07 +00:00
|
|
|
if lookupByDump {
|
|
|
|
return linkByNameDump(name)
|
|
|
|
}
|
|
|
|
|
2014-10-14 01:07:39 +00:00
|
|
|
req := nl.NewNetlinkRequest(syscall.RTM_GETLINK, syscall.NLM_F_ACK)
|
|
|
|
|
|
|
|
msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
|
|
|
|
req.AddData(msg)
|
|
|
|
|
|
|
|
nameData := nl.NewRtAttr(syscall.IFLA_IFNAME, nl.ZeroTerminated(name))
|
|
|
|
req.AddData(nameData)
|
|
|
|
|
2014-10-28 21:15:07 +00:00
|
|
|
link, err := execGetLink(req)
|
|
|
|
if err == syscall.EINVAL {
|
|
|
|
// older kernels don't support looking up via IFLA_IFNAME
|
|
|
|
// so fall back to dumping all links
|
|
|
|
lookupByDump = true
|
|
|
|
return linkByNameDump(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return link, err
|
2014-10-14 01:07:39 +00:00
|
|
|
}
|
|
|
|
|
2014-10-14 02:01:36 +00:00
|
|
|
// LinkByIndex finds a link by index and returns a pointer to the object.
|
2014-10-14 01:07:39 +00:00
|
|
|
func LinkByIndex(index int) (Link, error) {
|
|
|
|
req := nl.NewNetlinkRequest(syscall.RTM_GETLINK, syscall.NLM_F_ACK)
|
|
|
|
|
|
|
|
msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
|
|
|
|
msg.Index = int32(index)
|
|
|
|
req.AddData(msg)
|
|
|
|
|
|
|
|
return execGetLink(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func execGetLink(req *nl.NetlinkRequest) (Link, error) {
|
|
|
|
msgs, err := req.Execute(syscall.NETLINK_ROUTE, 0)
|
2014-09-01 03:27:34 +00:00
|
|
|
if err != nil {
|
2014-10-14 01:07:39 +00:00
|
|
|
if errno, ok := err.(syscall.Errno); ok {
|
|
|
|
if errno == syscall.ENODEV {
|
|
|
|
return nil, fmt.Errorf("Link not found")
|
|
|
|
}
|
|
|
|
}
|
2014-09-01 03:27:34 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-14 01:07:39 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case len(msgs) == 0:
|
|
|
|
return nil, fmt.Errorf("Link not found")
|
|
|
|
|
|
|
|
case len(msgs) == 1:
|
2014-10-14 02:01:36 +00:00
|
|
|
return linkDeserialize(msgs[0])
|
2014-10-14 01:07:39 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("More than one link found")
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-14 02:01:36 +00:00
|
|
|
// linkDeserialize deserializes a raw message received from netlink into
|
|
|
|
// a link object.
|
|
|
|
func linkDeserialize(m []byte) (Link, error) {
|
2014-10-14 01:07:39 +00:00
|
|
|
msg := nl.DeserializeIfInfomsg(m)
|
|
|
|
|
|
|
|
attrs, err := nl.ParseRouteAttr(m[msg.Len():])
|
2014-09-01 03:27:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-14 01:07:39 +00:00
|
|
|
|
|
|
|
base := LinkAttrs{Index: int(msg.Index), Flags: linkFlags(msg.Flags)}
|
|
|
|
var link Link
|
|
|
|
linkType := ""
|
|
|
|
for _, attr := range attrs {
|
|
|
|
switch attr.Attr.Type {
|
|
|
|
case syscall.IFLA_LINKINFO:
|
|
|
|
infos, err := nl.ParseRouteAttr(attr.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, info := range infos {
|
|
|
|
switch info.Attr.Type {
|
|
|
|
case nl.IFLA_INFO_KIND:
|
|
|
|
linkType = string(info.Value[:len(info.Value)-1])
|
|
|
|
switch linkType {
|
|
|
|
case "dummy":
|
|
|
|
link = &Dummy{}
|
|
|
|
case "bridge":
|
|
|
|
link = &Bridge{}
|
|
|
|
case "vlan":
|
|
|
|
link = &Vlan{}
|
|
|
|
case "veth":
|
|
|
|
link = &Veth{}
|
|
|
|
case "vxlan":
|
|
|
|
link = &Vxlan{}
|
2015-02-10 23:00:36 +00:00
|
|
|
case "ipvlan":
|
|
|
|
link = &IPVlan{}
|
2014-10-14 01:07:39 +00:00
|
|
|
default:
|
|
|
|
link = &Generic{LinkType: linkType}
|
|
|
|
}
|
|
|
|
case nl.IFLA_INFO_DATA:
|
|
|
|
data, err := nl.ParseRouteAttr(info.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch linkType {
|
|
|
|
case "vlan":
|
|
|
|
parseVlanData(link, data)
|
|
|
|
case "vxlan":
|
|
|
|
parseVxlanData(link, data)
|
2015-02-10 23:00:36 +00:00
|
|
|
case "ipvlan":
|
|
|
|
parseIPVlanData(link, data)
|
2014-10-14 01:07:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case syscall.IFLA_ADDRESS:
|
|
|
|
var nonzero bool
|
|
|
|
for _, b := range attr.Value {
|
|
|
|
if b != 0 {
|
|
|
|
nonzero = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nonzero {
|
|
|
|
base.HardwareAddr = attr.Value[:]
|
|
|
|
}
|
|
|
|
case syscall.IFLA_IFNAME:
|
|
|
|
base.Name = string(attr.Value[:len(attr.Value)-1])
|
|
|
|
case syscall.IFLA_MTU:
|
|
|
|
base.MTU = int(native.Uint32(attr.Value[0:4]))
|
|
|
|
case syscall.IFLA_LINK:
|
|
|
|
base.ParentIndex = int(native.Uint32(attr.Value[0:4]))
|
|
|
|
case syscall.IFLA_MASTER:
|
|
|
|
base.MasterIndex = int(native.Uint32(attr.Value[0:4]))
|
2014-12-05 01:01:33 +00:00
|
|
|
case syscall.IFLA_TXQLEN:
|
|
|
|
base.TxQLen = native.Uint32(attr.Value[0:4])
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-14 01:07:39 +00:00
|
|
|
// Links that don't have IFLA_INFO_KIND are hardware devices
|
|
|
|
if link == nil {
|
|
|
|
link = &Device{}
|
|
|
|
}
|
|
|
|
*link.Attrs() = base
|
|
|
|
|
|
|
|
return link, nil
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LinkList gets a list of link devices.
|
|
|
|
// Equivalent to: `ip link show`
|
|
|
|
func LinkList() ([]Link, error) {
|
|
|
|
// NOTE(vish): This duplicates functionality in net/iface_linux.go, but we need
|
|
|
|
// to get the message ourselves to parse link type.
|
2014-09-19 02:04:48 +00:00
|
|
|
req := nl.NewNetlinkRequest(syscall.RTM_GETLINK, syscall.NLM_F_DUMP)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
msg := nl.NewIfInfomsg(syscall.AF_UNSPEC)
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(msg)
|
|
|
|
|
|
|
|
msgs, err := req.Execute(syscall.NETLINK_ROUTE, syscall.RTM_NEWLINK)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res := make([]Link, 0)
|
|
|
|
|
|
|
|
for _, m := range msgs {
|
2014-10-14 02:01:36 +00:00
|
|
|
link, err := linkDeserialize(m)
|
2014-09-01 03:27:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = append(res, link)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2014-10-14 01:07:39 +00:00
|
|
|
func parseVlanData(link Link, data []syscall.NetlinkRouteAttr) {
|
2014-09-16 00:44:20 +00:00
|
|
|
vlan := link.(*Vlan)
|
2014-09-01 03:27:34 +00:00
|
|
|
for _, datum := range data {
|
|
|
|
switch datum.Attr.Type {
|
2014-09-19 02:04:48 +00:00
|
|
|
case nl.IFLA_VLAN_ID:
|
2014-09-24 05:21:11 +00:00
|
|
|
vlan.VlanId = int(native.Uint16(datum.Value[0:2]))
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-14 01:07:39 +00:00
|
|
|
func parseVxlanData(link Link, data []syscall.NetlinkRouteAttr) {
|
2014-09-16 00:44:20 +00:00
|
|
|
vxlan := link.(*Vxlan)
|
|
|
|
for _, datum := range data {
|
|
|
|
switch datum.Attr.Type {
|
|
|
|
case nl.IFLA_VXLAN_ID:
|
|
|
|
vxlan.VxlanId = int(native.Uint32(datum.Value[0:4]))
|
|
|
|
case nl.IFLA_VXLAN_LINK:
|
|
|
|
vxlan.VtepDevIndex = int(native.Uint32(datum.Value[0:4]))
|
|
|
|
case nl.IFLA_VXLAN_LOCAL:
|
|
|
|
vxlan.SrcAddr = net.IP(datum.Value[0:4])
|
|
|
|
case nl.IFLA_VXLAN_LOCAL6:
|
|
|
|
vxlan.SrcAddr = net.IP(datum.Value[0:16])
|
|
|
|
case nl.IFLA_VXLAN_GROUP:
|
|
|
|
vxlan.Group = net.IP(datum.Value[0:4])
|
|
|
|
case nl.IFLA_VXLAN_GROUP6:
|
|
|
|
vxlan.Group = net.IP(datum.Value[0:16])
|
|
|
|
case nl.IFLA_VXLAN_TTL:
|
|
|
|
vxlan.TTL = int(datum.Value[0])
|
|
|
|
case nl.IFLA_VXLAN_TOS:
|
|
|
|
vxlan.TOS = int(datum.Value[0])
|
|
|
|
case nl.IFLA_VXLAN_LEARNING:
|
|
|
|
vxlan.Learning = int8(datum.Value[0]) != 0
|
|
|
|
case nl.IFLA_VXLAN_PROXY:
|
|
|
|
vxlan.Proxy = int8(datum.Value[0]) != 0
|
|
|
|
case nl.IFLA_VXLAN_RSC:
|
|
|
|
vxlan.RSC = int8(datum.Value[0]) != 0
|
|
|
|
case nl.IFLA_VXLAN_L2MISS:
|
|
|
|
vxlan.L2miss = int8(datum.Value[0]) != 0
|
|
|
|
case nl.IFLA_VXLAN_L3MISS:
|
|
|
|
vxlan.L3miss = int8(datum.Value[0]) != 0
|
|
|
|
case nl.IFLA_VXLAN_AGEING:
|
|
|
|
vxlan.Age = int(native.Uint32(datum.Value[0:4]))
|
|
|
|
vxlan.NoAge = vxlan.Age == 0
|
|
|
|
case nl.IFLA_VXLAN_LIMIT:
|
|
|
|
vxlan.Limit = int(native.Uint32(datum.Value[0:4]))
|
|
|
|
case nl.IFLA_VXLAN_PORT:
|
|
|
|
vxlan.Port = int(native.Uint16(datum.Value[0:2]))
|
|
|
|
case nl.IFLA_VXLAN_PORT_RANGE:
|
|
|
|
buf := bytes.NewBuffer(datum.Value[0:4])
|
|
|
|
var pr vxlanPortRange
|
|
|
|
if binary.Read(buf, binary.BigEndian, &pr) != nil {
|
|
|
|
vxlan.PortLow = int(pr.Lo)
|
|
|
|
vxlan.PortHigh = int(pr.Hi)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-10 23:00:36 +00:00
|
|
|
func parseIPVlanData(link Link, data []syscall.NetlinkRouteAttr) {
|
|
|
|
ipv := link.(*IPVlan)
|
|
|
|
for _, datum := range data {
|
|
|
|
if datum.Attr.Type == nl.IFLA_IPVLAN_MODE {
|
|
|
|
ipv.Mode = IPVlanMode(native.Uint32(datum.Value[0:4]))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-01 03:27:34 +00:00
|
|
|
// copied from pkg/net_linux.go
|
|
|
|
func linkFlags(rawFlags uint32) net.Flags {
|
|
|
|
var f net.Flags
|
|
|
|
if rawFlags&syscall.IFF_UP != 0 {
|
|
|
|
f |= net.FlagUp
|
|
|
|
}
|
|
|
|
if rawFlags&syscall.IFF_BROADCAST != 0 {
|
|
|
|
f |= net.FlagBroadcast
|
|
|
|
}
|
|
|
|
if rawFlags&syscall.IFF_LOOPBACK != 0 {
|
|
|
|
f |= net.FlagLoopback
|
|
|
|
}
|
|
|
|
if rawFlags&syscall.IFF_POINTOPOINT != 0 {
|
|
|
|
f |= net.FlagPointToPoint
|
|
|
|
}
|
|
|
|
if rawFlags&syscall.IFF_MULTICAST != 0 {
|
|
|
|
f |= net.FlagMulticast
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|