gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/packetimpact/dut/linux/linux.go (about)

     1  // Copyright 2021 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  //go:build linux
    16  // +build linux
    17  
    18  // Package linux provides utilities specific to bringing up linux DUTs.
    19  package linux
    20  
    21  import (
    22  	"os/exec"
    23  	"strings"
    24  
    25  	"gvisor.dev/gvisor/test/packetimpact/dut"
    26  	netdevs "gvisor.dev/gvisor/test/packetimpact/netdevs/netlink"
    27  	"gvisor.dev/gvisor/test/packetimpact/testbench"
    28  )
    29  
    30  const (
    31  	// PosixServerPath is the path to the posix_server.
    32  	PosixServerPath = "test/packetimpact/dut/posix_server"
    33  )
    34  
    35  func uname() (*testbench.DUTUname, error) {
    36  	machine, err := exec.Command("uname", "-m").Output()
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	kernelRelease, err := exec.Command("uname", "-r").Output()
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	kernelVersion, err := exec.Command("uname", "-v").Output()
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	kernelName, err := exec.Command("uname", "-s").Output()
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	operatingSystem, err := exec.Command("uname", "-o").Output()
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	return &testbench.DUTUname{
    57  		Machine:         strings.TrimRight(string(machine), "\n"),
    58  		KernelName:      strings.TrimRight(string(kernelName), "\n"),
    59  		KernelRelease:   strings.TrimRight(string(kernelRelease), "\n"),
    60  		KernelVersion:   strings.TrimRight(string(kernelVersion), "\n"),
    61  		OperatingSystem: strings.TrimRight(string(operatingSystem), "\n"),
    62  	}, nil
    63  }
    64  
    65  // DUTInfo gatthers information about the linux DUT.
    66  func DUTInfo(ifaces dut.Ifaces) (testbench.DUTInfo, error) {
    67  	_, ctrlIPv4, _, err := netdevs.IfaceInfo(ifaces.Ctrl)
    68  	if err != nil {
    69  		return testbench.DUTInfo{}, err
    70  	}
    71  	testLink, testIPv4, testIPv6, err := netdevs.IfaceInfo(ifaces.Test)
    72  	if err != nil {
    73  		return testbench.DUTInfo{}, err
    74  	}
    75  	dutUname, err := uname()
    76  	if err != nil {
    77  		return testbench.DUTInfo{}, err
    78  	}
    79  
    80  	prefix, _ := testIPv4.Mask.Size()
    81  
    82  	return testbench.DUTInfo{
    83  		Net: &testbench.DUTTestNet{
    84  			RemoteIPv6:       testIPv6.IP,
    85  			RemoteIPv4:       testIPv4.IP.To4(),
    86  			IPv4PrefixLength: prefix,
    87  			RemoteDevID:      uint32(testLink.Attrs().Index),
    88  			RemoteDevName:    ifaces.Test,
    89  			POSIXServerIP:    ctrlIPv4.IP.To4(),
    90  			POSIXServerPort:  dut.PosixServerPort,
    91  			RemoteMAC:        testLink.Attrs().HardwareAddr,
    92  		},
    93  		Uname: dutUname,
    94  	}, nil
    95  }