2014-09-01 03:27:34 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
2015-01-31 20:05:11 +00:00
|
|
|
"bytes"
|
|
|
|
"net"
|
2015-08-19 15:31:39 +00:00
|
|
|
"syscall"
|
2014-09-01 03:27:34 +00:00
|
|
|
"testing"
|
2015-08-19 15:31:39 +00:00
|
|
|
"time"
|
2014-12-05 02:25:38 +00:00
|
|
|
|
|
|
|
"github.com/vishvananda/netns"
|
2014-09-01 03:27:34 +00:00
|
|
|
)
|
|
|
|
|
2015-06-12 18:09:57 +00:00
|
|
|
const (
|
|
|
|
testTxQLen int = 100
|
|
|
|
defaultTxQLen int = 1000
|
|
|
|
)
|
2014-12-05 02:25:38 +00:00
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
func testLinkAddDel(t *testing.T, link Link) {
|
2014-09-01 03:27:34 +00:00
|
|
|
links, err := LinkList()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
num := len(links)
|
|
|
|
|
|
|
|
if err := LinkAdd(link); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
result, err := LinkByName(base.Name)
|
2014-09-01 03:27:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
rBase := result.Attrs()
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
if vlan, ok := link.(*Vlan); ok {
|
|
|
|
other, ok := result.(*Vlan)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("Result of create is not a vlan")
|
|
|
|
}
|
|
|
|
if vlan.VlanId != other.VlanId {
|
|
|
|
t.Fatal("Link.VlanId id doesn't match")
|
|
|
|
}
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 18:09:57 +00:00
|
|
|
if veth, ok := result.(*Veth); ok {
|
|
|
|
if rBase.TxQLen != base.TxQLen {
|
|
|
|
t.Fatalf("qlen is %d, should be %d", rBase.TxQLen, base.TxQLen)
|
2014-12-05 02:25:38 +00:00
|
|
|
}
|
2015-02-07 01:49:37 +00:00
|
|
|
if rBase.MTU != base.MTU {
|
|
|
|
t.Fatalf("MTU is %d, should be %d", rBase.MTU, base.MTU)
|
|
|
|
}
|
2014-12-05 02:25:38 +00:00
|
|
|
|
2015-12-18 19:32:28 +00:00
|
|
|
if original, ok := link.(*Veth); ok {
|
|
|
|
if original.PeerName != "" {
|
|
|
|
var peer *Veth
|
|
|
|
other, err := LinkByName(original.PeerName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Peer %s not created", veth.PeerName)
|
|
|
|
}
|
|
|
|
if peer, ok = other.(*Veth); !ok {
|
|
|
|
t.Fatalf("Peer %s is incorrect type", veth.PeerName)
|
|
|
|
}
|
|
|
|
if peer.TxQLen != testTxQLen {
|
|
|
|
t.Fatalf("TxQLen of peer is %d, should be %d", peer.TxQLen, testTxQLen)
|
|
|
|
}
|
2014-09-24 05:21:11 +00:00
|
|
|
}
|
2015-12-18 19:32:28 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// recent kernels set the parent index for veths in the response
|
|
|
|
if rBase.ParentIndex == 0 && base.ParentIndex != 0 {
|
|
|
|
t.Fatal("Created link doesn't have parent %d but it should", base.ParentIndex)
|
|
|
|
} else if rBase.ParentIndex != 0 && base.ParentIndex == 0 {
|
|
|
|
t.Fatalf("Created link has parent %d but it shouldn't", rBase.ParentIndex)
|
|
|
|
} else if rBase.ParentIndex != 0 && base.ParentIndex != 0 {
|
|
|
|
if rBase.ParentIndex != base.ParentIndex {
|
|
|
|
t.Fatalf("Link.ParentIndex doesn't match %d != %d", rBase.ParentIndex, base.ParentIndex)
|
2014-12-05 02:25:38 +00:00
|
|
|
}
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-16 00:44:20 +00:00
|
|
|
if vxlan, ok := link.(*Vxlan); ok {
|
|
|
|
other, ok := result.(*Vxlan)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("Result of create is not a vxlan")
|
|
|
|
}
|
|
|
|
compareVxlan(t, vxlan, other)
|
|
|
|
}
|
|
|
|
|
2015-02-10 23:00:36 +00:00
|
|
|
if ipv, ok := link.(*IPVlan); ok {
|
|
|
|
other, ok := result.(*IPVlan)
|
|
|
|
if !ok {
|
2015-02-12 19:12:05 +00:00
|
|
|
t.Fatal("Result of create is not a ipvlan")
|
2015-02-10 23:00:36 +00:00
|
|
|
}
|
|
|
|
if ipv.Mode != other.Mode {
|
|
|
|
t.Fatalf("Got unexpected mode: %d, expected: %d", other.Mode, ipv.Mode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 17:19:47 +00:00
|
|
|
if macv, ok := link.(*Macvlan); ok {
|
|
|
|
other, ok := result.(*Macvlan)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("Result of create is not a macvlan")
|
|
|
|
}
|
|
|
|
if macv.Mode != other.Mode {
|
|
|
|
t.Fatalf("Got unexpected mode: %d, expected: %d", other.Mode, macv.Mode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-01 03:27:34 +00:00
|
|
|
if err = LinkDel(link); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
links, err = LinkList()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(links) != num {
|
|
|
|
t.Fatal("Link not removed properly")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-16 00:44:20 +00:00
|
|
|
func compareVxlan(t *testing.T, expected, actual *Vxlan) {
|
|
|
|
|
|
|
|
if actual.VxlanId != expected.VxlanId {
|
2015-02-12 19:00:59 +00:00
|
|
|
t.Fatal("Vxlan.VxlanId doesn't match")
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
if expected.SrcAddr != nil && !actual.SrcAddr.Equal(expected.SrcAddr) {
|
2015-02-12 19:00:59 +00:00
|
|
|
t.Fatal("Vxlan.SrcAddr doesn't match")
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
if expected.Group != nil && !actual.Group.Equal(expected.Group) {
|
2015-02-12 19:00:59 +00:00
|
|
|
t.Fatal("Vxlan.Group doesn't match")
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
if expected.TTL != -1 && actual.TTL != expected.TTL {
|
2015-02-12 19:00:59 +00:00
|
|
|
t.Fatal("Vxlan.TTL doesn't match")
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
if expected.TOS != -1 && actual.TOS != expected.TOS {
|
2015-02-12 19:00:59 +00:00
|
|
|
t.Fatal("Vxlan.TOS doesn't match")
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
if actual.Learning != expected.Learning {
|
2015-02-12 19:00:59 +00:00
|
|
|
t.Fatal("Vxlan.Learning doesn't match")
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
if actual.Proxy != expected.Proxy {
|
2015-02-12 19:00:59 +00:00
|
|
|
t.Fatal("Vxlan.Proxy doesn't match")
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
if actual.RSC != expected.RSC {
|
2015-02-12 19:00:59 +00:00
|
|
|
t.Fatal("Vxlan.RSC doesn't match")
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
if actual.L2miss != expected.L2miss {
|
2015-02-12 19:00:59 +00:00
|
|
|
t.Fatal("Vxlan.L2miss doesn't match")
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
if actual.L3miss != expected.L3miss {
|
2015-02-12 19:00:59 +00:00
|
|
|
t.Fatal("Vxlan.L3miss doesn't match")
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
2015-07-31 11:48:03 +00:00
|
|
|
if actual.GBP != expected.GBP {
|
|
|
|
t.Fatal("Vxlan.GBP doesn't match")
|
|
|
|
}
|
2014-09-16 00:44:20 +00:00
|
|
|
if expected.NoAge {
|
|
|
|
if !actual.NoAge {
|
2015-02-12 19:00:59 +00:00
|
|
|
t.Fatal("Vxlan.NoAge doesn't match")
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
} else if expected.Age > 0 && actual.Age != expected.Age {
|
2015-02-12 19:00:59 +00:00
|
|
|
t.Fatal("Vxlan.Age doesn't match")
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
if expected.Limit > 0 && actual.Limit != expected.Limit {
|
2015-02-12 19:00:59 +00:00
|
|
|
t.Fatal("Vxlan.Limit doesn't match")
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
if expected.Port > 0 && actual.Port != expected.Port {
|
2015-02-12 19:00:59 +00:00
|
|
|
t.Fatal("Vxlan.Port doesn't match")
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
if expected.PortLow > 0 || expected.PortHigh > 0 {
|
|
|
|
if actual.PortLow != expected.PortLow {
|
2015-02-12 19:00:59 +00:00
|
|
|
t.Fatal("Vxlan.PortLow doesn't match")
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
if actual.PortHigh != expected.PortHigh {
|
2015-02-12 19:00:59 +00:00
|
|
|
t.Fatal("Vxlan.PortHigh doesn't match")
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-01 03:27:34 +00:00
|
|
|
func TestLinkAddDelDummy(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
testLinkAddDel(t, &Dummy{LinkAttrs{Name: "foo"}})
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
2015-08-18 21:06:35 +00:00
|
|
|
func TestLinkAddDelIfb(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
testLinkAddDel(t, &Ifb{LinkAttrs{Name: "foo"}})
|
|
|
|
}
|
|
|
|
|
2014-09-01 03:27:34 +00:00
|
|
|
func TestLinkAddDelBridge(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
2015-02-07 01:49:37 +00:00
|
|
|
testLinkAddDel(t, &Bridge{LinkAttrs{Name: "foo", MTU: 1400}})
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
2015-12-09 00:36:30 +00:00
|
|
|
func TestLinkAddDelGretap(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
testLinkAddDel(t, &Gretap{
|
|
|
|
LinkAttrs: LinkAttrs{Name: "foo"},
|
2015-12-10 18:57:59 +00:00
|
|
|
IKey: 0x101,
|
|
|
|
OKey: 0x101,
|
|
|
|
PMtuDisc: 1,
|
|
|
|
Local: net.IPv4(127, 0, 0, 1),
|
|
|
|
Remote: net.IPv4(127, 0, 0, 1)})
|
2015-12-09 00:36:30 +00:00
|
|
|
}
|
|
|
|
|
2014-09-01 03:27:34 +00:00
|
|
|
func TestLinkAddDelVlan(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
parent := &Dummy{LinkAttrs{Name: "foo"}}
|
2014-09-01 03:27:34 +00:00
|
|
|
if err := LinkAdd(parent); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-09-24 05:21:11 +00:00
|
|
|
|
|
|
|
testLinkAddDel(t, &Vlan{LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index}, 900})
|
2014-09-01 03:27:34 +00:00
|
|
|
|
|
|
|
if err := LinkDel(parent); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkAddDelMacvlan(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
parent := &Dummy{LinkAttrs{Name: "foo"}}
|
2014-09-01 03:27:34 +00:00
|
|
|
if err := LinkAdd(parent); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-09-24 05:21:11 +00:00
|
|
|
|
2015-02-20 17:19:47 +00:00
|
|
|
testLinkAddDel(t, &Macvlan{
|
|
|
|
LinkAttrs: LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index},
|
|
|
|
Mode: MACVLAN_MODE_PRIVATE,
|
|
|
|
})
|
2014-09-01 03:27:34 +00:00
|
|
|
|
|
|
|
if err := LinkDel(parent); err != nil {
|
2015-07-27 13:48:26 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkAddDelMacvtap(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
parent := &Dummy{LinkAttrs{Name: "foo"}}
|
|
|
|
if err := LinkAdd(parent); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testLinkAddDel(t, &Macvtap{
|
|
|
|
Macvlan: Macvlan{
|
|
|
|
LinkAttrs: LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index},
|
|
|
|
Mode: MACVLAN_MODE_PRIVATE,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := LinkDel(parent); err != nil {
|
2014-09-01 03:27:34 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkAddDelVeth(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
2015-12-18 19:32:28 +00:00
|
|
|
veth := &Veth{LinkAttrs: LinkAttrs{Name: "foo", TxQLen: testTxQLen, MTU: 1400}, PeerName: "bar"}
|
|
|
|
testLinkAddDel(t, veth)
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
2015-12-09 12:33:45 +00:00
|
|
|
func TestLinkAddDelBond(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
2015-12-10 10:09:24 +00:00
|
|
|
testLinkAddDel(t, NewLinkBond(LinkAttrs{Name: "foo"}))
|
2015-12-09 12:33:45 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 18:09:57 +00:00
|
|
|
func TestLinkAddVethWithDefaultTxQLen(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
la := NewLinkAttrs()
|
|
|
|
la.Name = "foo"
|
|
|
|
|
|
|
|
veth := &Veth{LinkAttrs: la, PeerName: "bar"}
|
|
|
|
if err := LinkAdd(veth); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
link, err := LinkByName("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if veth, ok := link.(*Veth); !ok {
|
|
|
|
t.Fatalf("unexpected link type: %T", link)
|
|
|
|
} else {
|
|
|
|
if veth.TxQLen != defaultTxQLen {
|
|
|
|
t.Fatalf("TxQLen is %d, should be %d", veth.TxQLen, defaultTxQLen)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
peer, err := LinkByName("bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if veth, ok := peer.(*Veth); !ok {
|
|
|
|
t.Fatalf("unexpected link type: %T", link)
|
|
|
|
} else {
|
|
|
|
if veth.TxQLen != defaultTxQLen {
|
|
|
|
t.Fatalf("TxQLen is %d, should be %d", veth.TxQLen, defaultTxQLen)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkAddVethWithZeroTxQLen(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
la := NewLinkAttrs()
|
|
|
|
la.Name = "foo"
|
|
|
|
la.TxQLen = 0
|
|
|
|
|
|
|
|
veth := &Veth{LinkAttrs: la, PeerName: "bar"}
|
|
|
|
if err := LinkAdd(veth); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
link, err := LinkByName("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if veth, ok := link.(*Veth); !ok {
|
|
|
|
t.Fatalf("unexpected link type: %T", link)
|
|
|
|
} else {
|
|
|
|
if veth.TxQLen != 0 {
|
|
|
|
t.Fatalf("TxQLen is %d, should be %d", veth.TxQLen, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
peer, err := LinkByName("bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if veth, ok := peer.(*Veth); !ok {
|
|
|
|
t.Fatalf("unexpected link type: %T", link)
|
|
|
|
} else {
|
|
|
|
if veth.TxQLen != 0 {
|
|
|
|
t.Fatalf("TxQLen is %d, should be %d", veth.TxQLen, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkAddDummyWithTxQLen(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
la := NewLinkAttrs()
|
|
|
|
la.Name = "foo"
|
|
|
|
la.TxQLen = 1500
|
|
|
|
|
|
|
|
dummy := &Dummy{LinkAttrs: la}
|
|
|
|
if err := LinkAdd(dummy); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
link, err := LinkByName("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if dummy, ok := link.(*Dummy); !ok {
|
|
|
|
t.Fatalf("unexpected link type: %T", link)
|
|
|
|
} else {
|
|
|
|
if dummy.TxQLen != 1500 {
|
|
|
|
t.Fatalf("TxQLen is %d, should be %d", dummy.TxQLen, 1500)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-01 03:27:34 +00:00
|
|
|
func TestLinkAddDelBridgeMaster(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
master := &Bridge{LinkAttrs{Name: "foo"}}
|
2014-09-01 03:27:34 +00:00
|
|
|
if err := LinkAdd(master); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-09-24 05:21:11 +00:00
|
|
|
testLinkAddDel(t, &Dummy{LinkAttrs{Name: "bar", MasterIndex: master.Attrs().Index}})
|
2014-09-01 03:27:34 +00:00
|
|
|
|
|
|
|
if err := LinkDel(master); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkSetUnsetResetMaster(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
master := &Bridge{LinkAttrs{Name: "foo"}}
|
2014-09-01 03:27:34 +00:00
|
|
|
if err := LinkAdd(master); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
newmaster := &Bridge{LinkAttrs{Name: "bar"}}
|
2014-09-01 03:27:34 +00:00
|
|
|
if err := LinkAdd(newmaster); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
slave := &Dummy{LinkAttrs{Name: "baz"}}
|
2014-09-01 03:27:34 +00:00
|
|
|
if err := LinkAdd(slave); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-12-09 11:26:37 +00:00
|
|
|
nonexistsmaster := &Bridge{LinkAttrs{Name: "foobar"}}
|
|
|
|
|
|
|
|
if err := LinkSetMaster(slave, nonexistsmaster); err == nil {
|
|
|
|
t.Fatal("error expected")
|
|
|
|
}
|
|
|
|
|
2014-09-01 03:27:34 +00:00
|
|
|
if err := LinkSetMaster(slave, master); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
link, err := LinkByName("baz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
if link.Attrs().MasterIndex != master.Attrs().Index {
|
2014-09-01 03:27:34 +00:00
|
|
|
t.Fatal("Master not set properly")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := LinkSetMaster(slave, newmaster); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
link, err = LinkByName("baz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
if link.Attrs().MasterIndex != newmaster.Attrs().Index {
|
2014-09-01 03:27:34 +00:00
|
|
|
t.Fatal("Master not reset properly")
|
|
|
|
}
|
|
|
|
|
2015-12-09 11:26:37 +00:00
|
|
|
if err := LinkSetNoMaster(slave); err != nil {
|
2014-09-01 03:27:34 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
link, err = LinkByName("baz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
if link.Attrs().MasterIndex != 0 {
|
2014-09-01 03:27:34 +00:00
|
|
|
t.Fatal("Master not unset properly")
|
|
|
|
}
|
|
|
|
if err := LinkDel(slave); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := LinkDel(newmaster); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := LinkDel(master); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkSetNs(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
basens, err := netns.Get()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to get basens")
|
|
|
|
}
|
|
|
|
defer basens.Close()
|
|
|
|
|
|
|
|
newns, err := netns.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create newns")
|
|
|
|
}
|
|
|
|
defer newns.Close()
|
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
link := &Veth{LinkAttrs{Name: "foo"}, "bar"}
|
2014-09-01 03:27:34 +00:00
|
|
|
if err := LinkAdd(link); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
peer, err := LinkByName("bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
LinkSetNsFd(peer, int(basens))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to set newns for link")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = LinkByName("bar")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Link bar is still in newns")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = netns.Set(basens)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to set basens")
|
|
|
|
}
|
|
|
|
|
|
|
|
peer, err = LinkByName("bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Link is not in basens")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := LinkDel(peer); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = netns.Set(newns)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to set newns")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = LinkByName("foo")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Other half of veth pair not deleted")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-09-16 00:44:20 +00:00
|
|
|
|
|
|
|
func TestLinkAddDelVxlan(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
parent := &Dummy{
|
|
|
|
LinkAttrs{Name: "foo"},
|
|
|
|
}
|
|
|
|
if err := LinkAdd(parent); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
vxlan := Vxlan{
|
|
|
|
LinkAttrs: LinkAttrs{
|
|
|
|
Name: "bar",
|
|
|
|
},
|
2014-10-29 00:22:52 +00:00
|
|
|
VxlanId: 10,
|
2014-09-16 00:44:20 +00:00
|
|
|
VtepDevIndex: parent.Index,
|
|
|
|
Learning: true,
|
|
|
|
L2miss: true,
|
|
|
|
L3miss: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
testLinkAddDel(t, &vxlan)
|
|
|
|
if err := LinkDel(parent); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2014-10-14 01:07:39 +00:00
|
|
|
|
2015-02-10 23:00:36 +00:00
|
|
|
func TestLinkAddDelIPVlanL2(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
parent := &Dummy{LinkAttrs{Name: "foo"}}
|
|
|
|
if err := LinkAdd(parent); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ipv := IPVlan{
|
|
|
|
LinkAttrs: LinkAttrs{
|
|
|
|
Name: "bar",
|
|
|
|
ParentIndex: parent.Index,
|
|
|
|
},
|
|
|
|
Mode: IPVLAN_MODE_L2,
|
|
|
|
}
|
|
|
|
|
|
|
|
testLinkAddDel(t, &ipv)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkAddDelIPVlanL3(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
parent := &Dummy{LinkAttrs{Name: "foo"}}
|
|
|
|
if err := LinkAdd(parent); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ipv := IPVlan{
|
|
|
|
LinkAttrs: LinkAttrs{
|
|
|
|
Name: "bar",
|
|
|
|
ParentIndex: parent.Index,
|
|
|
|
},
|
|
|
|
Mode: IPVLAN_MODE_L3,
|
|
|
|
}
|
|
|
|
|
|
|
|
testLinkAddDel(t, &ipv)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkAddDelIPVlanNoParent(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
ipv := IPVlan{
|
|
|
|
LinkAttrs: LinkAttrs{
|
|
|
|
Name: "bar",
|
|
|
|
},
|
|
|
|
Mode: IPVLAN_MODE_L3,
|
|
|
|
}
|
|
|
|
err := LinkAdd(&ipv)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Add should fail if ipvlan creating without ParentIndex")
|
|
|
|
}
|
|
|
|
if err.Error() != "Can't create ipvlan link without ParentIndex" {
|
|
|
|
t.Fatalf("Error should be about missing ParentIndex, got %q", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-14 01:07:39 +00:00
|
|
|
func TestLinkByIndex(t *testing.T) {
|
2014-10-31 19:21:39 +00:00
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
2014-10-14 01:07:39 +00:00
|
|
|
dummy := &Dummy{LinkAttrs{Name: "dummy"}}
|
|
|
|
if err := LinkAdd(dummy); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
found, err := LinkByIndex(dummy.Index)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if found.Attrs().Index != dummy.Attrs().Index {
|
|
|
|
t.Fatalf("Indices don't match: %v != %v", found.Attrs().Index, dummy.Attrs().Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
LinkDel(dummy)
|
|
|
|
|
|
|
|
// test not found
|
|
|
|
_, err = LinkByIndex(dummy.Attrs().Index)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("LinkByIndex(%v) found deleted link", err)
|
|
|
|
}
|
|
|
|
}
|
2015-01-31 19:55:10 +00:00
|
|
|
|
|
|
|
func TestLinkSet(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
iface := &Dummy{LinkAttrs{Name: "foo"}}
|
|
|
|
if err := LinkAdd(iface); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
link, err := LinkByName("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = LinkSetName(link, "bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not change interface name: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
link, err = LinkByName("bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Interface name not changed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = LinkSetMTU(link, 1400)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not set MTU: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
link, err = LinkByName("bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if link.Attrs().MTU != 1400 {
|
|
|
|
t.Fatal("MTU not changed!")
|
|
|
|
}
|
2015-01-31 20:05:11 +00:00
|
|
|
|
|
|
|
addr, err := net.ParseMAC("00:12:34:56:78:AB")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = LinkSetHardwareAddr(link, addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
link, err = LinkByName("bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(link.Attrs().HardwareAddr, addr) {
|
|
|
|
t.Fatalf("hardware address not changed!")
|
|
|
|
}
|
2015-11-30 19:55:24 +00:00
|
|
|
|
|
|
|
err = LinkSetAlias(link, "barAlias")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not set alias: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
link, err = LinkByName("bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if link.Attrs().Alias != "barAlias" {
|
|
|
|
t.Fatalf("alias not changed!")
|
|
|
|
}
|
|
|
|
|
|
|
|
link, err = LinkByAlias("barAlias")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-01-31 19:55:10 +00:00
|
|
|
}
|
2015-08-19 15:31:39 +00:00
|
|
|
|
|
|
|
func expectLinkUpdate(ch <-chan LinkUpdate, ifaceName string, up bool) bool {
|
|
|
|
for {
|
|
|
|
timeout := time.After(time.Minute)
|
|
|
|
select {
|
|
|
|
case update := <-ch:
|
|
|
|
if ifaceName == update.Link.Attrs().Name && (update.IfInfomsg.Flags&syscall.IFF_UP != 0) == up {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
case <-timeout:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkSubscribe(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
ch := make(chan LinkUpdate)
|
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
if err := LinkSubscribe(ch, done); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
link := &Veth{LinkAttrs{Name: "foo", TxQLen: testTxQLen, MTU: 1400}, "bar"}
|
|
|
|
if err := LinkAdd(link); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !expectLinkUpdate(ch, "foo", false) {
|
|
|
|
t.Fatal("Add update not received as expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := LinkSetUp(link); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !expectLinkUpdate(ch, "foo", true) {
|
|
|
|
t.Fatal("Link Up update not received as expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := LinkDel(link); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !expectLinkUpdate(ch, "foo", false) {
|
|
|
|
t.Fatal("Del update not received as expected")
|
|
|
|
}
|
|
|
|
}
|