github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/cmd/examples/map_pinning/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "path" 7 8 "github.com/dylandreimerink/gobpfld" 9 "github.com/dylandreimerink/gobpfld/bpfsys" 10 "github.com/dylandreimerink/gobpfld/bpftypes" 11 ) 12 13 // This example command demonstrates and tests pinning and unpinning of maps 14 15 const mapSize = 100 16 17 func main() { 18 testMap := &gobpfld.ArrayMap{ 19 AbstractMap: gobpfld.AbstractMap{ 20 Name: gobpfld.MustNewObjName("xdp_stats_map"), 21 Definition: gobpfld.BPFMapDef{ 22 Type: bpftypes.BPF_MAP_TYPE_ARRAY, 23 KeySize: 4, // SizeOf(uint32) 24 ValueSize: 8, // SizeOf(uint64) 25 MaxEntries: mapSize, 26 }, 27 }, 28 } 29 testMap2 := &gobpfld.ArrayMap{ 30 AbstractMap: gobpfld.AbstractMap{ 31 Name: gobpfld.MustNewObjName("xdp_stats_map2"), 32 Definition: gobpfld.BPFMapDef{ 33 Type: bpftypes.BPF_MAP_TYPE_ARRAY, 34 KeySize: 4, // SizeOf(uint32) 35 ValueSize: 8, // SizeOf(uint64) 36 MaxEntries: mapSize, 37 }, 38 }, 39 } 40 41 pinPath2 := "abc/gei" 42 43 err := testMap2.Load() 44 if err != nil { 45 fmt.Fprintf(os.Stderr, "error while loading map: %s\n", err.Error()) 46 os.Exit(1) 47 } 48 49 // Pin map 2 to the FS 50 err = testMap2.Pin(pinPath2) 51 if err != nil { 52 fmt.Fprintf(os.Stderr, "error while pinning map: %s\n", err.Error()) 53 os.Exit(1) 54 } 55 56 // Unload it so we can unpin it later 57 err = testMap2.Close() 58 if err != nil { 59 fmt.Fprintf(os.Stderr, "error while unloading map: %s\n", err.Error()) 60 os.Exit(1) 61 } 62 63 // Load map 1 64 err = testMap.Load() 65 if err != nil { 66 fmt.Fprintf(os.Stderr, "error while loading map: %s\n", err.Error()) 67 os.Exit(1) 68 } 69 70 pinPath := "abc/def" 71 72 // Pin to the same dir as map 2 73 err = testMap.Pin(pinPath) 74 if err != nil { 75 fmt.Fprintf(os.Stderr, "error while pinning map: %s\n", err.Error()) 76 os.Exit(1) 77 } 78 79 // Load map 1 so we can unpin it 80 err = testMap.Close() 81 if err != nil { 82 fmt.Fprintf(os.Stderr, "error while unloading map: %s\n", err.Error()) 83 os.Exit(1) 84 } 85 86 // Unpin and remove pinned file 87 err = testMap.Unpin(pinPath, true) 88 if err != nil { 89 fmt.Fprintf(os.Stderr, "error while unpinning map: %s\n", err.Error()) 90 os.Exit(1) 91 } 92 93 // Stat the dir to check that removing map 2 didn't remove the "abc" dir which still contains 94 // the pinned map 2 95 stat, err := os.Stat(fmt.Sprint(gobpfld.BPFSysPath, path.Dir(pinPath))) 96 if err != nil { 97 fmt.Fprintf(os.Stderr, "error while stating dir '%s': %s\n", path.Dir(pinPath), err.Error()) 98 os.Exit(1) 99 } 100 if !stat.IsDir() { 101 fmt.Fprintf(os.Stderr, "path is not a dir: %s\n", path.Dir(pinPath)) 102 os.Exit(1) 103 } 104 105 // Increment map 1 to check that the unpinned map 1 is still accessible 106 key := uint32(0) 107 value := uint64(0) 108 109 err = testMap.Get(key, &value) 110 if err != nil { 111 fmt.Fprintf(os.Stderr, "error while getting from map: %s\n", err.Error()) 112 os.Exit(1) 113 } 114 115 value++ 116 117 err = testMap.Set(key, &value, bpfsys.BPFMapElemAny) 118 if err != nil { 119 fmt.Fprintf(os.Stderr, "error while setting to map: %s\n", err.Error()) 120 os.Exit(1) 121 } 122 123 // Unpin + remove map 2 to cleanup 124 err = testMap2.Unpin(pinPath2, true) 125 if err != nil { 126 fmt.Fprintf(os.Stderr, "error while unloading map: %s\n", err.Error()) 127 os.Exit(1) 128 } 129 130 // Check that the dir of the pin path now has been removed after the last pinned map was unpinned 131 _, err = os.Stat(fmt.Sprint(gobpfld.BPFSysPath, path.Dir(pinPath))) 132 if err == nil { 133 fmt.Fprintf(os.Stderr, "no error while stating pin path dir: %s\n", path.Dir(pinPath)) 134 os.Exit(1) 135 } 136 }