2017-02-20 17:34:32 +00:00
|
|
|
// +build linux
|
|
|
|
|
2016-05-08 18:35:49 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
2016-05-09 23:52:35 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
2016-05-18 17:21:46 +00:00
|
|
|
"fmt"
|
2016-05-09 23:52:35 +00:00
|
|
|
"io"
|
2016-05-18 17:21:46 +00:00
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2016-05-08 18:35:49 +00:00
|
|
|
"testing"
|
2016-11-10 22:01:03 +00:00
|
|
|
"time"
|
|
|
|
"unsafe"
|
2016-05-08 18:35:49 +00:00
|
|
|
|
2016-06-20 16:25:15 +00:00
|
|
|
"github.com/vishvananda/netlink/nl"
|
2016-05-08 18:35:49 +00:00
|
|
|
"github.com/vishvananda/netns"
|
2017-10-20 20:38:07 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2016-05-08 18:35:49 +00:00
|
|
|
)
|
|
|
|
|
2021-09-04 14:27:58 +00:00
|
|
|
func TestHandleCreateClose(t *testing.T) {
|
2016-05-08 18:35:49 +00:00
|
|
|
h, err := NewHandle()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-06-20 16:25:15 +00:00
|
|
|
for _, f := range nl.SupportedNlFamilies {
|
|
|
|
sh, ok := h.sockets[f]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Handle socket(s) for family %d was not created", f)
|
|
|
|
}
|
|
|
|
if sh.Socket == nil {
|
|
|
|
t.Fatalf("Socket for family %d was not created", f)
|
|
|
|
}
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 14:27:58 +00:00
|
|
|
h.Close()
|
2016-06-20 16:25:15 +00:00
|
|
|
if h.sockets != nil {
|
2021-09-04 14:27:58 +00:00
|
|
|
t.Fatalf("Handle socket(s) were not closed")
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleCreateNetns(t *testing.T) {
|
2017-02-06 16:14:07 +00:00
|
|
|
skipUnlessRoot(t)
|
|
|
|
|
2016-05-09 23:52:35 +00:00
|
|
|
id := make([]byte, 4)
|
|
|
|
if _, err := io.ReadFull(rand.Reader, id); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ifName := "dummy-" + hex.EncodeToString(id)
|
2016-05-08 18:35:49 +00:00
|
|
|
|
|
|
|
// Create an handle on the current netns
|
|
|
|
curNs, err := netns.Get()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer curNs.Close()
|
|
|
|
|
|
|
|
ch, err := NewHandleAt(curNs)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-09-04 14:27:58 +00:00
|
|
|
defer ch.Close()
|
2016-05-08 18:35:49 +00:00
|
|
|
|
|
|
|
// Create an handle on a custom netns
|
|
|
|
newNs, err := netns.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer newNs.Close()
|
|
|
|
|
|
|
|
nh, err := NewHandleAt(newNs)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-09-04 14:27:58 +00:00
|
|
|
defer nh.Close()
|
2016-05-08 18:35:49 +00:00
|
|
|
|
|
|
|
// Create an interface using the current handle
|
|
|
|
err = ch.LinkAdd(&Dummy{LinkAttrs{Name: ifName}})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
l, err := ch.LinkByName(ifName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if l.Type() != "dummy" {
|
|
|
|
t.Fatalf("Unexpected link type: %s", l.Type())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the new handle cannot find the interface
|
|
|
|
ll, err := nh.LinkByName(ifName)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Unexpected link found on netns %s: %v", newNs, ll)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the interface to the new netns
|
|
|
|
err = ch.LinkSetNsFd(l, int(newNs))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify new netns handle can find the interface while current cannot
|
|
|
|
ll, err = nh.LinkByName(ifName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if ll.Type() != "dummy" {
|
|
|
|
t.Fatalf("Unexpected link type: %s", ll.Type())
|
|
|
|
}
|
|
|
|
ll, err = ch.LinkByName(ifName)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Unexpected link found on netns %s: %v", curNs, ll)
|
|
|
|
}
|
|
|
|
}
|
2016-05-18 17:21:46 +00:00
|
|
|
|
2016-11-10 22:01:03 +00:00
|
|
|
func TestHandleTimeout(t *testing.T) {
|
|
|
|
h, err := NewHandle()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-09-04 14:27:58 +00:00
|
|
|
defer h.Close()
|
2016-11-10 22:01:03 +00:00
|
|
|
|
|
|
|
for _, sh := range h.sockets {
|
2017-10-20 20:38:07 +00:00
|
|
|
verifySockTimeVal(t, sh.Socket.GetFd(), unix.Timeval{Sec: 0, Usec: 0})
|
2016-11-10 22:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h.SetSocketTimeout(2*time.Second + 8*time.Millisecond)
|
|
|
|
|
|
|
|
for _, sh := range h.sockets {
|
2017-10-20 20:38:07 +00:00
|
|
|
verifySockTimeVal(t, sh.Socket.GetFd(), unix.Timeval{Sec: 2, Usec: 8000})
|
2016-11-10 22:01:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 06:32:37 +00:00
|
|
|
func TestHandleReceiveBuffer(t *testing.T) {
|
|
|
|
h, err := NewHandle()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-09-04 14:27:58 +00:00
|
|
|
defer h.Close()
|
2017-09-12 06:38:42 +00:00
|
|
|
if err := h.SetSocketReceiveBufferSize(65536, false); err != nil {
|
2017-09-12 06:32:37 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
sizes, err := h.GetSocketReceiveBufferSize()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(sizes) != len(h.sockets) {
|
|
|
|
t.Fatalf("Unexpected number of socket buffer sizes: %d (expected %d)",
|
|
|
|
len(sizes), len(h.sockets))
|
|
|
|
}
|
|
|
|
for _, s := range sizes {
|
|
|
|
if s < 65536 || s > 2*65536 {
|
|
|
|
t.Fatalf("Unexpected socket receive buffer size: %d (expected around %d)",
|
|
|
|
s, 65536)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 20:38:07 +00:00
|
|
|
func verifySockTimeVal(t *testing.T, fd int, tv unix.Timeval) {
|
2016-11-10 22:01:03 +00:00
|
|
|
var (
|
2017-10-20 20:38:07 +00:00
|
|
|
tr unix.Timeval
|
2016-11-10 22:01:03 +00:00
|
|
|
v = uint32(0x10)
|
|
|
|
)
|
2017-10-20 20:38:07 +00:00
|
|
|
_, _, errno := unix.Syscall6(unix.SYS_GETSOCKOPT, uintptr(fd), unix.SOL_SOCKET, unix.SO_SNDTIMEO, uintptr(unsafe.Pointer(&tr)), uintptr(unsafe.Pointer(&v)), 0)
|
2016-11-10 22:01:03 +00:00
|
|
|
if errno != 0 {
|
|
|
|
t.Fatal(errno)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tr.Sec != tv.Sec || tr.Usec != tv.Usec {
|
|
|
|
t.Fatalf("Unexpected timeout value read: %v. Expected: %v", tr, tv)
|
|
|
|
}
|
|
|
|
|
2017-10-20 20:38:07 +00:00
|
|
|
_, _, errno = unix.Syscall6(unix.SYS_GETSOCKOPT, uintptr(fd), unix.SOL_SOCKET, unix.SO_RCVTIMEO, uintptr(unsafe.Pointer(&tr)), uintptr(unsafe.Pointer(&v)), 0)
|
2016-11-10 22:01:03 +00:00
|
|
|
if errno != 0 {
|
|
|
|
t.Fatal(errno)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tr.Sec != tv.Sec || tr.Usec != tv.Usec {
|
|
|
|
t.Fatalf("Unexpected timeout value read: %v. Expected: %v", tr, tv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 17:21:46 +00:00
|
|
|
var (
|
|
|
|
iter = 10
|
|
|
|
numThread = uint32(4)
|
|
|
|
prefix = "iface"
|
|
|
|
handle1 *Handle
|
|
|
|
handle2 *Handle
|
|
|
|
ns1 netns.NsHandle
|
|
|
|
ns2 netns.NsHandle
|
|
|
|
done uint32
|
|
|
|
initError error
|
|
|
|
once sync.Once
|
|
|
|
)
|
|
|
|
|
|
|
|
func getXfrmState(thread int) *XfrmState {
|
|
|
|
return &XfrmState{
|
|
|
|
Src: net.IPv4(byte(192), byte(168), 1, byte(1+thread)),
|
|
|
|
Dst: net.IPv4(byte(192), byte(168), 2, byte(1+thread)),
|
|
|
|
Proto: XFRM_PROTO_AH,
|
|
|
|
Mode: XFRM_MODE_TUNNEL,
|
|
|
|
Spi: thread,
|
|
|
|
Auth: &XfrmStateAlgo{
|
|
|
|
Name: "hmac(sha256)",
|
|
|
|
Key: []byte("abcdefghijklmnopqrstuvwzyzABCDEF"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getXfrmPolicy(thread int) *XfrmPolicy {
|
|
|
|
return &XfrmPolicy{
|
|
|
|
Src: &net.IPNet{IP: net.IPv4(byte(10), byte(10), byte(thread), 0), Mask: []byte{255, 255, 255, 0}},
|
|
|
|
Dst: &net.IPNet{IP: net.IPv4(byte(10), byte(10), byte(thread), 0), Mask: []byte{255, 255, 255, 0}},
|
|
|
|
Proto: 17,
|
|
|
|
DstPort: 1234,
|
|
|
|
SrcPort: 5678,
|
|
|
|
Dir: XFRM_DIR_OUT,
|
|
|
|
Tmpls: []XfrmPolicyTmpl{
|
|
|
|
{
|
|
|
|
Src: net.IPv4(byte(192), byte(168), 1, byte(thread)),
|
|
|
|
Dst: net.IPv4(byte(192), byte(168), 2, byte(thread)),
|
|
|
|
Proto: XFRM_PROTO_ESP,
|
|
|
|
Mode: XFRM_MODE_TUNNEL,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func initParallel() {
|
|
|
|
ns1, initError = netns.New()
|
|
|
|
if initError != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
handle1, initError = NewHandleAt(ns1)
|
|
|
|
if initError != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ns2, initError = netns.New()
|
|
|
|
if initError != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
handle2, initError = NewHandleAt(ns2)
|
|
|
|
if initError != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parallelDone() {
|
|
|
|
atomic.AddUint32(&done, 1)
|
|
|
|
if done == numThread {
|
|
|
|
if ns1.IsOpen() {
|
|
|
|
ns1.Close()
|
|
|
|
}
|
|
|
|
if ns2.IsOpen() {
|
|
|
|
ns2.Close()
|
|
|
|
}
|
|
|
|
if handle1 != nil {
|
2021-09-04 14:27:58 +00:00
|
|
|
handle1.Close()
|
2016-05-18 17:21:46 +00:00
|
|
|
}
|
|
|
|
if handle2 != nil {
|
2021-09-04 14:27:58 +00:00
|
|
|
handle2.Close()
|
2016-05-18 17:21:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do few route and xfrm operation on the two handles in parallel
|
|
|
|
func runParallelTests(t *testing.T, thread int) {
|
2017-02-06 16:14:07 +00:00
|
|
|
skipUnlessRoot(t)
|
2016-05-18 17:21:46 +00:00
|
|
|
defer parallelDone()
|
|
|
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
once.Do(initParallel)
|
|
|
|
if initError != nil {
|
|
|
|
t.Fatal(initError)
|
|
|
|
}
|
|
|
|
|
|
|
|
state := getXfrmState(thread)
|
|
|
|
policy := getXfrmPolicy(thread)
|
|
|
|
for i := 0; i < iter; i++ {
|
|
|
|
ifName := fmt.Sprintf("%s_%d_%d", prefix, thread, i)
|
|
|
|
link := &Dummy{LinkAttrs{Name: ifName}}
|
|
|
|
err := handle1.LinkAdd(link)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
l, err := handle1.LinkByName(ifName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = handle1.LinkSetUp(l)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
handle1.LinkSetNsFd(l, int(ns2))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = handle1.XfrmStateAdd(state)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = handle1.XfrmPolicyAdd(policy)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = handle2.LinkSetDown(l)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = handle2.XfrmStateAdd(state)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = handle2.XfrmPolicyAdd(policy)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = handle2.LinkByName(ifName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
handle2.LinkSetNsFd(l, int(ns1))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = handle1.LinkSetUp(l)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
l, err = handle1.LinkByName(ifName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = handle1.XfrmPolicyDel(policy)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = handle2.XfrmPolicyDel(policy)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = handle1.XfrmStateDel(state)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = handle2.XfrmStateDel(state)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleParallel1(t *testing.T) {
|
|
|
|
runParallelTests(t, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleParallel2(t *testing.T) {
|
|
|
|
runParallelTests(t, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleParallel3(t *testing.T) {
|
|
|
|
runParallelTests(t, 3)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleParallel4(t *testing.T) {
|
|
|
|
runParallelTests(t, 4)
|
|
|
|
}
|