2023-10-24 11:14:21 +00:00
|
|
|
//go:build linux
|
2017-11-07 19:49:48 +00:00
|
|
|
// +build linux
|
|
|
|
|
2014-09-01 03:27:34 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
2017-09-27 14:08:13 +00:00
|
|
|
"bytes"
|
2020-10-19 12:44:21 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
2017-05-05 09:02:53 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2017-11-08 11:42:16 +00:00
|
|
|
"log"
|
2014-09-01 03:27:34 +00:00
|
|
|
"os"
|
2017-09-27 14:08:13 +00:00
|
|
|
"os/exec"
|
2014-09-01 03:27:34 +00:00
|
|
|
"runtime"
|
2017-05-05 09:02:53 +00:00
|
|
|
"strings"
|
2014-09-01 03:27:34 +00:00
|
|
|
"testing"
|
2015-02-10 23:00:36 +00:00
|
|
|
|
2023-10-24 11:14:21 +00:00
|
|
|
"github.com/vishvananda/netlink/nl"
|
2015-02-10 23:00:36 +00:00
|
|
|
"github.com/vishvananda/netns"
|
2017-10-20 20:38:07 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2014-09-01 03:27:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type tearDownNetlinkTest func()
|
|
|
|
|
2024-04-09 09:04:24 +00:00
|
|
|
func skipUnlessRoot(t testing.TB) {
|
2023-10-06 11:24:47 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2014-09-01 03:27:34 +00:00
|
|
|
if os.Getuid() != 0 {
|
2017-11-07 23:41:07 +00:00
|
|
|
t.Skip("Test requires root privileges.")
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
2017-02-06 16:14:07 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 16:56:37 +00:00
|
|
|
func skipUnlessKModuleLoaded(t *testing.T, moduleNames ...string) {
|
2023-12-27 15:45:01 +00:00
|
|
|
t.Helper()
|
|
|
|
file, err := ioutil.ReadFile("/proc/modules")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to open /proc/modules", err)
|
|
|
|
}
|
2023-11-24 16:56:37 +00:00
|
|
|
|
|
|
|
foundRequiredMods := make(map[string]bool)
|
|
|
|
lines := strings.Split(string(file), "\n")
|
|
|
|
|
|
|
|
for _, name := range moduleNames {
|
|
|
|
foundRequiredMods[name] = false
|
|
|
|
for _, line := range lines {
|
2023-12-27 15:45:01 +00:00
|
|
|
n := strings.Split(line, " ")[0]
|
2023-11-24 16:56:37 +00:00
|
|
|
if n == name {
|
|
|
|
foundRequiredMods[name] = true
|
2023-12-27 15:45:01 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-11-24 16:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
failed := false
|
|
|
|
for _, name := range moduleNames {
|
|
|
|
if found, _ := foundRequiredMods[name]; !found {
|
|
|
|
t.Logf("Test requires missing kmodule %q.", name)
|
|
|
|
failed = true
|
2023-12-27 15:45:01 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-24 16:56:37 +00:00
|
|
|
if failed {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
2023-12-27 15:45:01 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 09:04:24 +00:00
|
|
|
func setUpNetlinkTest(t testing.TB) tearDownNetlinkTest {
|
2017-02-06 16:14:07 +00:00
|
|
|
skipUnlessRoot(t)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
|
|
|
// new temporary namespace so we don't pollute the host
|
|
|
|
// lock thread since the namespace is thread local
|
|
|
|
runtime.LockOSThread()
|
|
|
|
var err error
|
|
|
|
ns, err := netns.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create newns", ns)
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
ns.Close()
|
|
|
|
runtime.UnlockOSThread()
|
|
|
|
}
|
|
|
|
}
|
2017-02-19 03:22:09 +00:00
|
|
|
|
2020-10-19 12:44:21 +00:00
|
|
|
// setUpNamedNetlinkTest create a temporary named names space with a random name
|
|
|
|
func setUpNamedNetlinkTest(t *testing.T) (string, tearDownNetlinkTest) {
|
|
|
|
skipUnlessRoot(t)
|
|
|
|
|
|
|
|
origNS, err := netns.Get()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed saving orig namespace")
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a random name
|
|
|
|
rnd := make([]byte, 4)
|
|
|
|
if _, err := rand.Read(rnd); err != nil {
|
|
|
|
t.Fatal("failed creating random ns name")
|
|
|
|
}
|
|
|
|
name := "netlinktest-" + hex.EncodeToString(rnd)
|
|
|
|
|
|
|
|
ns, err := netns.NewNamed(name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create new ns", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.LockOSThread()
|
|
|
|
cleanup := func() {
|
|
|
|
ns.Close()
|
|
|
|
netns.DeleteNamed(name)
|
|
|
|
netns.Set(origNS)
|
|
|
|
runtime.UnlockOSThread()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := netns.Set(ns); err != nil {
|
|
|
|
cleanup()
|
|
|
|
t.Fatal("Failed entering new namespace", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return name, cleanup
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:10:03 +00:00
|
|
|
func setUpNetlinkTestWithLoopback(t *testing.T) tearDownNetlinkTest {
|
|
|
|
skipUnlessRoot(t)
|
|
|
|
|
|
|
|
runtime.LockOSThread()
|
|
|
|
ns, err := netns.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create new netns", ns)
|
|
|
|
}
|
|
|
|
|
|
|
|
link, err := LinkByName("lo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to find \"lo\" in new netns: %v", err)
|
|
|
|
}
|
|
|
|
if err := LinkSetUp(link); err != nil {
|
|
|
|
t.Fatalf("Failed to bring up \"lo\" in new netns: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
ns.Close()
|
|
|
|
runtime.UnlockOSThread()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 17:57:21 +00:00
|
|
|
func setUpF(t *testing.T, path, value string) {
|
|
|
|
file, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to open %s: %s", path, err)
|
|
|
|
}
|
2021-09-10 02:30:28 +00:00
|
|
|
defer file.Close()
|
2018-03-16 17:57:21 +00:00
|
|
|
file.WriteString(value)
|
|
|
|
}
|
|
|
|
|
2017-02-19 03:22:09 +00:00
|
|
|
func setUpMPLSNetlinkTest(t *testing.T) tearDownNetlinkTest {
|
|
|
|
if _, err := os.Stat("/proc/sys/net/mpls/platform_labels"); err != nil {
|
2017-11-07 23:41:07 +00:00
|
|
|
t.Skip("Test requires MPLS support.")
|
2017-02-19 03:22:09 +00:00
|
|
|
}
|
|
|
|
f := setUpNetlinkTest(t)
|
2018-03-16 17:57:21 +00:00
|
|
|
setUpF(t, "/proc/sys/net/mpls/platform_labels", "1024")
|
|
|
|
setUpF(t, "/proc/sys/net/mpls/conf/lo/input", "1")
|
2017-02-19 03:22:09 +00:00
|
|
|
return f
|
|
|
|
}
|
2017-05-05 09:02:53 +00:00
|
|
|
|
2017-09-27 14:08:13 +00:00
|
|
|
func setUpSEG6NetlinkTest(t *testing.T) tearDownNetlinkTest {
|
|
|
|
// check if SEG6 options are enabled in Kernel Config
|
|
|
|
cmd := exec.Command("uname", "-r")
|
|
|
|
var out bytes.Buffer
|
|
|
|
cmd.Stdout = &out
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
t.Fatal("Failed to run: uname -r")
|
|
|
|
}
|
|
|
|
s := []string{"/boot/config-", strings.TrimRight(out.String(), "\n")}
|
|
|
|
filename := strings.Join(s, "")
|
|
|
|
|
|
|
|
grepKey := func(key, fname string) (string, error) {
|
|
|
|
cmd := exec.Command("grep", key, filename)
|
|
|
|
var out bytes.Buffer
|
|
|
|
cmd.Stdout = &out
|
|
|
|
err := cmd.Run() // "err != nil" if no line matched with grep
|
|
|
|
return strings.TrimRight(out.String(), "\n"), err
|
|
|
|
}
|
|
|
|
key := string("CONFIG_IPV6_SEG6_LWTUNNEL=y")
|
|
|
|
if _, err := grepKey(key, filename); err != nil {
|
|
|
|
msg := "Skipped test because it requires SEG6_LWTUNNEL support."
|
2018-10-29 23:13:14 +00:00
|
|
|
log.Println(msg)
|
2017-09-27 14:08:13 +00:00
|
|
|
t.Skip(msg)
|
|
|
|
}
|
|
|
|
// Add CONFIG_IPV6_SEG6_HMAC to support seg6_hamc
|
|
|
|
// key := string("CONFIG_IPV6_SEG6_HMAC=y")
|
|
|
|
|
|
|
|
return setUpNetlinkTest(t)
|
|
|
|
}
|
|
|
|
|
2023-11-24 16:56:37 +00:00
|
|
|
func setUpNetlinkTestWithKModule(t *testing.T, moduleNames ...string) tearDownNetlinkTest {
|
|
|
|
skipUnlessKModuleLoaded(t, moduleNames...)
|
2017-05-05 09:02:53 +00:00
|
|
|
return setUpNetlinkTest(t)
|
|
|
|
}
|
2023-11-24 16:56:37 +00:00
|
|
|
func setUpNamedNetlinkTestWithKModule(t *testing.T, moduleNames ...string) (string, tearDownNetlinkTest) {
|
|
|
|
file, err := ioutil.ReadFile("/proc/modules")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to open /proc/modules", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
foundRequiredMods := make(map[string]bool)
|
|
|
|
lines := strings.Split(string(file), "\n")
|
|
|
|
|
|
|
|
for _, name := range moduleNames {
|
|
|
|
foundRequiredMods[name] = false
|
|
|
|
for _, line := range lines {
|
|
|
|
n := strings.Split(line, " ")[0]
|
|
|
|
if n == name {
|
|
|
|
foundRequiredMods[name] = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
failed := false
|
|
|
|
for _, name := range moduleNames {
|
|
|
|
if found, _ := foundRequiredMods[name]; !found {
|
|
|
|
t.Logf("Test requires missing kmodule %q.", name)
|
|
|
|
failed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if failed {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
return setUpNamedNetlinkTest(t)
|
|
|
|
}
|
2017-06-06 09:09:16 +00:00
|
|
|
|
|
|
|
func remountSysfs() error {
|
2017-10-20 20:38:07 +00:00
|
|
|
if err := unix.Mount("", "/", "none", unix.MS_SLAVE|unix.MS_REC, ""); err != nil {
|
2017-06-06 09:09:16 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-10-20 20:38:07 +00:00
|
|
|
if err := unix.Unmount("/sys", unix.MNT_DETACH); err != nil {
|
2017-06-06 09:09:16 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-10-20 20:38:07 +00:00
|
|
|
return unix.Mount("", "/sys", "sysfs", 0, "")
|
2017-06-06 09:09:16 +00:00
|
|
|
}
|
2017-11-07 23:14:34 +00:00
|
|
|
|
|
|
|
func minKernelRequired(t *testing.T, kernel, major int) {
|
2023-10-06 11:24:47 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2017-11-07 23:14:34 +00:00
|
|
|
k, m, err := KernelVersion()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if k < kernel || k == kernel && m < major {
|
|
|
|
t.Skipf("Host Kernel (%d.%d) does not meet test's minimum required version: (%d.%d)",
|
|
|
|
k, m, kernel, major)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func KernelVersion() (kernel, major int, err error) {
|
|
|
|
uts := unix.Utsname{}
|
|
|
|
if err = unix.Uname(&uts); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ba := make([]byte, 0, len(uts.Release))
|
|
|
|
for _, b := range uts.Release {
|
|
|
|
if b == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ba = append(ba, byte(b))
|
|
|
|
}
|
|
|
|
var rest string
|
|
|
|
if n, _ := fmt.Sscanf(string(ba), "%d.%d%s", &kernel, &major, &rest); n < 2 {
|
|
|
|
err = fmt.Errorf("can't parse kernel version in %q", string(ba))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2023-10-24 11:14:21 +00:00
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
nl.EnableErrorMessageReporting = true
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|