github.com/vishvananda/netlink@v1.3.0/socket_xdp_linux_test.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package netlink
     5  
     6  import (
     7  	"os"
     8  	"testing"
     9  
    10  	"golang.org/x/sys/unix"
    11  )
    12  
    13  func TestSocketXDPGetInfo(t *testing.T) {
    14  	xdpsockfd, err := unix.Socket(unix.AF_XDP, unix.SOCK_RAW, 0)
    15  	if err != nil {
    16  		t.Fatal(err)
    17  	}
    18  	defer unix.Close(xdpsockfd)
    19  
    20  	wantFamily := unix.AF_XDP
    21  
    22  	var xdpsockstat unix.Stat_t
    23  	err = unix.Fstat(xdpsockfd, &xdpsockstat)
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	wantIno := xdpsockstat.Ino
    28  
    29  	result, err := SocketXDPGetInfo(uint32(wantIno), SOCK_ANY_COOKIE)
    30  	if err != nil {
    31  		if os.IsNotExist(err) {
    32  			t.Skip("kernel lacks support for AF_XDP socket diagnosis")
    33  		}
    34  		t.Fatal(err)
    35  	}
    36  
    37  	if got := result.XDPDiagMsg.Family; got != uint8(wantFamily) {
    38  		t.Fatalf("protocol family = %v, want %v", got, wantFamily)
    39  	}
    40  	if got := result.XDPDiagMsg.Ino; got != uint32(wantIno) {
    41  		t.Fatalf("protocol ino = %v, want %v", got, wantIno)
    42  	}
    43  	if result.XDPInfo == nil {
    44  		t.Fatalf("want non-nil XDPInfo, got nil")
    45  	}
    46  	if got := result.XDPInfo.Ifindex; got != 0 {
    47  		t.Fatalf("ifindex = %v, want 0", got)
    48  	}
    49  }