github.com/sagernet/netlink@v0.0.0-20240612041022-b9a21c07ac6a/devlink_test.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package netlink
     5  
     6  import (
     7  	"flag"
     8  	"net"
     9  	"testing"
    10  )
    11  
    12  func TestDevLinkGetDeviceList(t *testing.T) {
    13  	minKernelRequired(t, 4, 12)
    14  	setUpNetlinkTestWithKModule(t, "devlink")
    15  	_, err := DevLinkGetDeviceList()
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  }
    20  
    21  func TestDevLinkGetDeviceByName(t *testing.T) {
    22  	minKernelRequired(t, 4, 12)
    23  	setUpNetlinkTestWithKModule(t, "devlink")
    24  	_, err := DevLinkGetDeviceByName("foo", "bar")
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  }
    29  
    30  func TestDevLinkSetEswitchMode(t *testing.T) {
    31  	minKernelRequired(t, 4, 12)
    32  	setUpNetlinkTestWithKModule(t, "devlink")
    33  	dev, err := DevLinkGetDeviceByName("foo", "bar")
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	err = DevLinkSetEswitchMode(dev, "switchdev")
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	err = DevLinkSetEswitchMode(dev, "legacy")
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  }
    46  
    47  func TestDevLinkGetAllPortList(t *testing.T) {
    48  	minKernelRequired(t, 5, 4)
    49  	ports, err := DevLinkGetAllPortList()
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	t.Log("devlink port count = ", len(ports))
    54  	for _, port := range ports {
    55  		t.Log(*port)
    56  	}
    57  }
    58  
    59  func TestDevLinkAddDelSfPort(t *testing.T) {
    60  	var addAttrs DevLinkPortAddAttrs
    61  	minKernelRequired(t, 5, 13)
    62  	if bus == "" || device == "" {
    63  		t.Log("devlink bus and device are empty, skipping test")
    64  		return
    65  	}
    66  
    67  	dev, err := DevLinkGetDeviceByName(bus, device)
    68  	if err != nil {
    69  		t.Fatal(err)
    70  		return
    71  	}
    72  	addAttrs.SfNumberValid = true
    73  	addAttrs.SfNumber = uint32(sfnum)
    74  	addAttrs.PfNumber = 0
    75  	port, err2 := DevLinkPortAdd(dev.BusName, dev.DeviceName, 7, addAttrs)
    76  	if err2 != nil {
    77  		t.Fatal(err2)
    78  		return
    79  	}
    80  	t.Log(*port)
    81  	if port.Fn != nil {
    82  		t.Log("function attributes = ", *port.Fn)
    83  	}
    84  	err2 = DevLinkPortDel(dev.BusName, dev.DeviceName, port.PortIndex)
    85  	if err2 != nil {
    86  		t.Fatal(err2)
    87  	}
    88  }
    89  
    90  func TestDevLinkSfPortFnSet(t *testing.T) {
    91  	var addAttrs DevLinkPortAddAttrs
    92  	var stateAttr DevlinkPortFnSetAttrs
    93  
    94  	minKernelRequired(t, 5, 12)
    95  	if bus == "" || device == "" {
    96  		t.Log("devlink bus and device are empty, skipping test")
    97  		return
    98  	}
    99  
   100  	dev, err := DevLinkGetDeviceByName(bus, device)
   101  	if err != nil {
   102  		t.Fatal(err)
   103  		return
   104  	}
   105  	addAttrs.SfNumberValid = true
   106  	addAttrs.SfNumber = uint32(sfnum)
   107  	addAttrs.PfNumber = 0
   108  	port, err2 := DevLinkPortAdd(dev.BusName, dev.DeviceName, 7, addAttrs)
   109  	if err2 != nil {
   110  		t.Fatal(err2)
   111  		return
   112  	}
   113  	t.Log(*port)
   114  	if port.Fn != nil {
   115  		t.Log("function attributes = ", *port.Fn)
   116  	}
   117  	macAttr := DevlinkPortFnSetAttrs{
   118  		FnAttrs: DevlinkPortFn{
   119  			HwAddr: net.HardwareAddr{0x00, 0x11, 0x22, 0x33, 0x44, 0x55},
   120  		},
   121  		HwAddrValid: true,
   122  	}
   123  	err2 = DevlinkPortFnSet(dev.BusName, dev.DeviceName, port.PortIndex, macAttr)
   124  	if err2 != nil {
   125  		t.Log("function mac set err = ", err2)
   126  	}
   127  	stateAttr.FnAttrs.State = 1
   128  	stateAttr.StateValid = true
   129  	err2 = DevlinkPortFnSet(dev.BusName, dev.DeviceName, port.PortIndex, stateAttr)
   130  	if err2 != nil {
   131  		t.Log("function state set err = ", err2)
   132  	}
   133  
   134  	port, err3 := DevLinkGetPortByIndex(dev.BusName, dev.DeviceName, port.PortIndex)
   135  	if err3 == nil {
   136  		t.Log(*port)
   137  		t.Log(*port.Fn)
   138  	}
   139  	err2 = DevLinkPortDel(dev.BusName, dev.DeviceName, port.PortIndex)
   140  	if err2 != nil {
   141  		t.Fatal(err2)
   142  	}
   143  }
   144  
   145  var bus string
   146  var device string
   147  var sfnum uint
   148  
   149  func init() {
   150  	flag.StringVar(&bus, "bus", "", "devlink device bus name")
   151  	flag.StringVar(&device, "device", "", "devlink device devicename")
   152  	flag.UintVar(&sfnum, "sfnum", 0, "devlink port sfnumber")
   153  }
   154  
   155  func TestDevlinkGetDeviceInfoByNameAsMap(t *testing.T) {
   156  	info, err := pkgHandle.DevlinkGetDeviceInfoByNameAsMap("pci", "0000:00:00.0", mockDevlinkInfoGetter)
   157  	if err != nil {
   158  		t.Fatal(err)
   159  	}
   160  	testInfo := devlinkTestInfoParesd()
   161  	for k, v := range info {
   162  		if testInfo[k] != v {
   163  			t.Fatal("Value", v, "retrieved for key", k, "is not equal to", testInfo[k])
   164  		}
   165  	}
   166  }
   167  
   168  func TestDevlinkGetDeviceInfoByName(t *testing.T) {
   169  	info, err := pkgHandle.DevlinkGetDeviceInfoByName("pci", "0000:00:00.0", mockDevlinkInfoGetter)
   170  	if err != nil {
   171  		t.Fatal(err)
   172  	}
   173  	testInfo := parseInfoData(devlinkTestInfoParesd())
   174  	if !areInfoStructsEqual(info, testInfo) {
   175  		t.Fatal("Info structures are not equal")
   176  	}
   177  }
   178  
   179  func TestDevlinkGetDeviceInfoByNameAsMapFail(t *testing.T) {
   180  	_, err := pkgHandle.DevlinkGetDeviceInfoByNameAsMap("pci", "0000:00:00.0", mockDevlinkInfoGetterEmpty)
   181  	if err == nil {
   182  		t.Fatal()
   183  	}
   184  }
   185  
   186  func TestDevlinkGetDeviceInfoByNameFail(t *testing.T) {
   187  	_, err := pkgHandle.DevlinkGetDeviceInfoByName("pci", "0000:00:00.0", mockDevlinkInfoGetterEmpty)
   188  	if err == nil {
   189  		t.Fatal()
   190  	}
   191  }
   192  
   193  func mockDevlinkInfoGetter(bus, device string) ([]byte, error) {
   194  	return devlinkInfo(), nil
   195  }
   196  
   197  func mockDevlinkInfoGetterEmpty(bus, device string) ([]byte, error) {
   198  	return []byte{}, nil
   199  }
   200  
   201  func devlinkInfo() []byte {
   202  	return []byte{51, 1, 0, 0, 8, 0, 1, 0, 112, 99, 105, 0, 17, 0, 2, 0, 48,
   203  		48, 48, 48, 58, 56, 52, 58, 48, 48, 46, 48, 0, 0, 0, 0, 8, 0, 98, 0,
   204  		105, 99, 101, 0, 28, 0, 99, 0, 51, 48, 45, 56, 57, 45, 97, 51, 45,
   205  		102, 102, 45, 102, 102, 45, 99, 97, 45, 48, 53, 45, 54, 56, 0, 36,
   206  		0, 100, 0, 13, 0, 103, 0, 98, 111, 97, 114, 100, 46, 105, 100, 0, 0,
   207  		0, 0, 15, 0, 104, 0, 75, 56, 53, 53, 56, 53, 45, 48, 48, 48, 0, 0,
   208  		28, 0, 101, 0, 12, 0, 103, 0, 102, 119, 46, 109, 103, 109, 116, 0,
   209  		10, 0, 104, 0, 53, 46, 52, 46, 53, 0, 0, 0, 28, 0, 101, 0, 16, 0,
   210  		103, 0, 102, 119, 46, 109, 103, 109, 116, 46, 97, 112, 105, 0, 8, 0,
   211  		104, 0, 49, 46, 55, 0, 40, 0, 101, 0, 18, 0, 103, 0, 102, 119, 46,
   212  		109, 103, 109, 116, 46, 98, 117, 105, 108, 100, 0, 0, 0, 15, 0, 104,
   213  		0, 48, 120, 51, 57, 49, 102, 55, 54, 52, 48, 0, 0, 32, 0, 101, 0,
   214  		12, 0, 103, 0, 102, 119, 46, 117, 110, 100, 105, 0, 13, 0, 104, 0,
   215  		49, 46, 50, 56, 57, 56, 46, 48, 0, 0, 0, 0, 32, 0, 101, 0, 16, 0,
   216  		103, 0, 102, 119, 46, 112, 115, 105, 100, 46, 97, 112, 105, 0, 9, 0,
   217  		104, 0, 50, 46, 52, 50, 0, 0, 0, 0, 40, 0, 101, 0, 17, 0, 103, 0,
   218  		102, 119, 46, 98, 117, 110, 100, 108, 101, 95, 105, 100, 0, 0, 0, 0,
   219  		15, 0, 104, 0, 48, 120, 56, 48, 48, 48, 55, 48, 54, 98, 0, 0, 48, 0,
   220  		101, 0, 16, 0, 103, 0, 102, 119, 46, 97, 112, 112, 46, 110, 97, 109,
   221  		101, 0, 27, 0, 104, 0, 73, 67, 69, 32, 79, 83, 32, 68, 101, 102, 97,
   222  		117, 108, 116, 32, 80, 97, 99, 107, 97, 103, 101, 0, 0, 32, 0, 101,
   223  		0, 11, 0, 103, 0, 102, 119, 46, 97, 112, 112, 0, 0, 13, 0, 104, 0,
   224  		49, 46, 51, 46, 50, 52, 46, 48, 0, 0, 0, 0, 44, 0, 101, 0, 21, 0,
   225  		103, 0, 102, 119, 46, 97, 112, 112, 46, 98, 117, 110, 100, 108,
   226  		101, 95, 105, 100, 0, 0, 0, 0, 15, 0, 104, 0, 48, 120, 99, 48, 48,
   227  		48, 48, 48, 48, 49, 0, 0, 44, 0, 101, 0, 15, 0, 103, 0, 102, 119,
   228  		46, 110, 101, 116, 108, 105, 115, 116, 0, 0, 21, 0, 104, 0, 50, 46,
   229  		52, 48, 46, 50, 48, 48, 48, 45, 51, 46, 49, 54, 46, 48, 0, 0, 0, 0,
   230  		44, 0, 101, 0, 21, 0, 103, 0, 102, 119, 46, 110, 101, 116, 108, 105,
   231  		115, 116, 46, 98, 117, 105, 108, 100, 0, 0, 0, 0, 15, 0, 104, 0, 48,
   232  		120, 54, 55, 54, 97, 52, 56, 57, 100, 0, 0}
   233  }
   234  
   235  func devlinkTestInfoParesd() map[string]string {
   236  	return map[string]string{
   237  		"board.id":         "K85585-000",
   238  		"fw.app":           "1.3.24.0",
   239  		"fw.app.bundle_id": "0xc0000001",
   240  		"fw.app.name":      "ICE OS Default Package",
   241  		"fw.bundle_id":     "0x8000706b",
   242  		"fw.mgmt":          "5.4.5",
   243  		"fw.mgmt.api":      "1.7",
   244  		"fw.mgmt.build":    "0x391f7640",
   245  		"fw.netlist":       "2.40.2000-3.16.0",
   246  		"fw.netlist.build": "0x676a489d",
   247  		"fw.psid.api":      "2.42",
   248  		"fw.undi":          "1.2898.0",
   249  		"driver":           "ice",
   250  		"serialNumber":     "30-89-a3-ff-ff-ca-05-68",
   251  	}
   252  }
   253  
   254  func areInfoStructsEqual(first *DevlinkDeviceInfo, second *DevlinkDeviceInfo) bool {
   255  	if first.FwApp != second.FwApp || first.FwAppBoundleID != second.FwAppBoundleID ||
   256  		first.FwAppName != second.FwAppName || first.FwBoundleID != second.FwBoundleID ||
   257  		first.FwMgmt != second.FwMgmt || first.FwMgmtAPI != second.FwMgmtAPI ||
   258  		first.FwMgmtBuild != second.FwMgmtBuild || first.FwNetlist != second.FwNetlist ||
   259  		first.FwNetlistBuild != second.FwNetlistBuild || first.FwPsidAPI != second.FwPsidAPI ||
   260  		first.BoardID != second.BoardID || first.FwUndi != second.FwUndi ||
   261  		first.Driver != second.Driver || first.SerialNumber != second.SerialNumber {
   262  		return false
   263  	}
   264  	return true
   265  }