github.com/cilium/ebpf@v0.15.1-0.20240517100537-8079b37aa138/link/iter_test.go (about)

     1  package link
     2  
     3  import (
     4  	"io"
     5  	"testing"
     6  
     7  	"github.com/cilium/ebpf"
     8  	"github.com/cilium/ebpf/internal/testutils"
     9  )
    10  
    11  func TestIter(t *testing.T) {
    12  	testutils.SkipOnOldKernel(t, "5.9", "bpf_map iter")
    13  
    14  	prog := mustLoadProgram(t, ebpf.Tracing, ebpf.AttachTraceIter, "bpf_map")
    15  
    16  	it, err := AttachIter(IterOptions{
    17  		Program: prog,
    18  	})
    19  	if err != nil {
    20  		t.Fatal("Can't create iter:", err)
    21  	}
    22  
    23  	file, err := it.Open()
    24  	if err != nil {
    25  		t.Fatal("Can't open iter instance:", err)
    26  	}
    27  	defer file.Close()
    28  
    29  	contents, err := io.ReadAll(file)
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  
    34  	if len(contents) != 0 {
    35  		t.Error("Non-empty output from no-op iterator:", string(contents))
    36  	}
    37  
    38  	testLink(t, it, prog)
    39  }
    40  
    41  func TestIterMapElements(t *testing.T) {
    42  	testutils.SkipOnOldKernel(t, "5.9", "bpf_map_elem iter")
    43  
    44  	prog := mustLoadProgram(t, ebpf.Tracing, ebpf.AttachTraceIter, "bpf_map_elem")
    45  
    46  	arr, err := ebpf.NewMap(&ebpf.MapSpec{
    47  		Type:       ebpf.Array,
    48  		KeySize:    4,
    49  		ValueSize:  4,
    50  		MaxEntries: 3,
    51  	})
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	defer arr.Close()
    56  
    57  	it, err := AttachIter(IterOptions{
    58  		Program: prog,
    59  		Map:     arr,
    60  	})
    61  	if err != nil {
    62  		t.Fatal("Can't create iter:", err)
    63  	}
    64  	defer it.Close()
    65  
    66  	file, err := it.Open()
    67  	if err != nil {
    68  		t.Fatal("Can't open iter instance:", err)
    69  	}
    70  	defer file.Close()
    71  
    72  	contents, err := io.ReadAll(file)
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  
    77  	if len(contents) != 0 {
    78  		t.Error("Non-empty output from no-op iterator:", string(contents))
    79  	}
    80  }
    81  
    82  func TestUDPIter(t *testing.T) {
    83  	// Introduced by 5788b3a07fc5 ("net: bpf: Implement bpf iterator for udp")
    84  	testutils.SkipOnOldKernel(t, "5.9", "udp iter")
    85  
    86  	prog := mustLoadProgram(t, ebpf.Tracing, ebpf.AttachTraceIter, "udp")
    87  
    88  	it, err := AttachIter(IterOptions{
    89  		Program: prog,
    90  	})
    91  	if err != nil {
    92  		t.Fatal("Can't create iter:", err)
    93  	}
    94  
    95  	file, err := it.Open()
    96  	if err != nil {
    97  		t.Fatal("Can't open iter instance:", err)
    98  	}
    99  	defer file.Close()
   100  
   101  	contents, err := io.ReadAll(file)
   102  	if err != nil {
   103  		t.Fatal(err)
   104  	}
   105  
   106  	if len(contents) != 0 {
   107  		t.Error("Non-empty output from no-op iterator:", string(contents))
   108  	}
   109  
   110  	testLink(t, it, prog)
   111  }