Repository: cilium/ebpf
Pull request #171 (2020-11-20 10:28:55)

internal/unix: add BPF_F_NO_PREALLOC const
...and use it in MapSpec.Flags where appropriate.


Patch:
diff --git a/internal/unix/types_linux.go b/internal/unix/types_linux.go
index d8135c1..69a24fb 100644
--- a/internal/unix/types_linux.go
+++ b/internal/unix/types_linux.go
@@ -20,6 +20,7 @@ const (
 	EPERM                    = linux.EPERM
 	ESRCH                    = linux.ESRCH
 	ENODEV                   = linux.ENODEV
+	BPF_F_NO_PREALLOC        = linux.BPF_F_NO_PREALLOC
 	BPF_F_NUMA_NODE          = linux.BPF_F_NUMA_NODE
 	BPF_F_RDONLY_PROG        = linux.BPF_F_RDONLY_PROG
 	BPF_F_WRONLY_PROG        = linux.BPF_F_WRONLY_PROG
diff --git a/internal/unix/types_other.go b/internal/unix/types_other.go
index 416b8d2..be60ae3 100644
--- a/internal/unix/types_other.go
+++ b/internal/unix/types_other.go
@@ -20,6 +20,7 @@ const (
 	EPERM                    = syscall.EPERM
 	ESRCH                    = syscall.ESRCH
 	ENODEV                   = syscall.ENODEV
+	BPF_F_NO_PREALLOC        = 0
 	BPF_F_NUMA_NODE          = 0
 	BPF_F_RDONLY_PROG        = 0
 	BPF_F_WRONLY_PROG        = 0
diff --git a/readme.md b/readme.md
index 298db57..bbdb5a7 100644
--- a/readme.md
+++ b/readme.md
@@ -25,5 +25,5 @@ right now**. Expect to update your code if you want to follow along.
 
 * [eBPF.io](https://ebpf.io) (recommended)
 * [Cilium eBPF documentation](https://docs.cilium.io/en/latest/bpf/#bpf-guide) (recommended)
-* [Linux documentation on BPF](http://elixir.free-electrons.com/linux/latest/source/Documentation/networking/filter.txt)
+* [Linux documentation on BPF](https://www.kernel.org/doc/html/latest/networking/filter.html)
 * [eBPF features by Linux version](https://github.com/iovisor/bcc/blob/master/docs/kernel-versions.md)


Test patch:
diff --git a/elf_reader_test.go b/elf_reader_test.go
index e92e0a2..50d6103 100644
--- a/elf_reader_test.go
+++ b/elf_reader_test.go
@@ -11,14 +11,13 @@ import (
 	"github.com/cilium/ebpf/internal"
 	"github.com/cilium/ebpf/internal/btf"
 	"github.com/cilium/ebpf/internal/testutils"
+	"github.com/cilium/ebpf/internal/unix"
 
 	"github.com/google/go-cmp/cmp"
 	"github.com/google/go-cmp/cmp/cmpopts"
 )
 
 func TestLoadCollectionSpec(t *testing.T) {
-	const BPF_F_NO_PREALLOC = 1
-
 	coll := &CollectionSpec{
 		Maps: map[string]*MapSpec{
 			"hash_map": {
@@ -27,7 +26,7 @@ func TestLoadCollectionSpec(t *testing.T) {
 				KeySize:    4,
 				ValueSize:  8,
 				MaxEntries: 1,
-				Flags:      BPF_F_NO_PREALLOC,
+				Flags:      unix.BPF_F_NO_PREALLOC,
 			},
 			"hash_map2": {
 				Name:       "hash_map2",
diff --git a/info_test.go b/info_test.go
index 8aad146..823c523 100644
--- a/info_test.go
+++ b/info_test.go
@@ -6,6 +6,7 @@ import (
 
 	"github.com/cilium/ebpf/internal"
 	"github.com/cilium/ebpf/internal/testutils"
+	"github.com/cilium/ebpf/internal/unix"
 )
 
 func TestMapInfoFromProc(t *testing.T) {
@@ -15,7 +16,7 @@ func TestMapInfoFromProc(t *testing.T) {
 		KeySize:    4,
 		ValueSize:  5,
 		MaxEntries: 2,
-		Flags:      0x1, // BPF_F_NO_PREALLOC
+		Flags:      unix.BPF_F_NO_PREALLOC,
 	})
 	if err != nil {
 		t.Fatal(err)
@@ -43,8 +44,8 @@ func TestMapInfoFromProc(t *testing.T) {
 		t.Error("Expected MaxEntries of 2, got", info.MaxEntries)
 	}
 
-	if info.Flags != 1 {
-		t.Error("Expected Flags to be 1, got", info.Flags)
+	if info.Flags != unix.BPF_F_NO_PREALLOC {
+		t.Errorf("Expected Flags to be %d, got %d", unix.BPF_F_NO_PREALLOC, info.Flags)
 	}
 
 	if info.Name != "" && info.Name != "testing" {