github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/cmd/examples/map_batch/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 7 "github.com/dylandreimerink/gobpfld" 8 "github.com/dylandreimerink/gobpfld/bpfsys" 9 "github.com/dylandreimerink/gobpfld/bpftypes" 10 ) 11 12 const mapSize = 5 13 14 func main() { 15 testMap := &gobpfld.HashMap{ 16 AbstractMap: gobpfld.AbstractMap{ 17 Name: gobpfld.MustNewObjName("xdp_stats_map"), 18 Definition: gobpfld.BPFMapDef{ 19 Type: bpftypes.BPF_MAP_TYPE_HASH, 20 KeySize: 4, // SizeOf(uint32) 21 ValueSize: 8, // SizeOf(uint64) 22 MaxEntries: mapSize, 23 }, 24 }, 25 } 26 27 err := testMap.Load() 28 if err != nil { 29 fmt.Fprintf(os.Stderr, "error while loading map: %s\n", err.Error()) 30 os.Exit(1) 31 } 32 33 for i := uint32(1); i <= mapSize; i++ { 34 val := uint64(i * 10) 35 err = testMap.Set(&i, &val, bpfsys.BPFMapElemAny) 36 if err != nil { 37 fmt.Fprintf(os.Stderr, "error while setting to map: %s\n", err.Error()) 38 os.Exit(1) 39 } 40 } 41 42 fmt.Println("[loop and get]") 43 fmt.Println("------------------------") 44 45 for i := uint32(1); i <= mapSize; i++ { 46 var val uint64 47 err = testMap.Get(&i, &val) 48 if err != nil { 49 fmt.Fprintf(os.Stderr, "error while setting to map: %s\n", err.Error()) 50 os.Exit(1) 51 } 52 fmt.Printf("%d = %d\n", i, val) 53 } 54 55 fmt.Println("\n[get batch]") 56 fmt.Println("------------------------") 57 58 keys := make([]uint32, mapSize) 59 values := make([]uint64, mapSize) 60 count, _, err := testMap.GetBatch(&keys, &values, mapSize) 61 if err != nil { 62 fmt.Fprintf(os.Stderr, "error while getting batch: %s\n", err.Error()) 63 os.Exit(1) 64 } 65 66 fmt.Printf("Got %d elements\n", count) 67 for i := 0; i < count; i++ { 68 fmt.Printf("%d = %d\n", keys[i], values[i]) 69 } 70 71 fmt.Println("\n[delete batch]") 72 fmt.Println("------------------------") 73 74 count, err = testMap.DeleteBatch(&keys, mapSize) 75 if err != nil { 76 fmt.Fprintf(os.Stderr, "error while getting batch: %s\n", err.Error()) 77 os.Exit(1) 78 } 79 80 fmt.Printf("Deleted %d elements\n", count) 81 82 count, _, err = testMap.GetBatch(&keys, &values, mapSize) 83 if err != nil { 84 fmt.Fprintf(os.Stderr, "error while getting batch: %s\n", err.Error()) 85 os.Exit(1) 86 } 87 fmt.Printf("Got %d elements\n", count) 88 for i := 0; i < count; i++ { 89 fmt.Printf("%d = %d\n", keys[i], values[i]) 90 } 91 92 fmt.Println("\n[add batch]") 93 fmt.Println("------------------------") 94 95 count, err = testMap.SetBatch(&keys, &values, bpfsys.BPFMapElemAny, mapSize) 96 if err != nil { 97 fmt.Fprintf(os.Stderr, "error while getting batch: %s\n", err.Error()) 98 os.Exit(1) 99 } 100 fmt.Printf("Set %d elements\n", count) 101 102 count, _, err = testMap.GetBatch(&keys, &values, mapSize) 103 if err != nil { 104 fmt.Fprintf(os.Stderr, "error while getting batch: %s\n", err.Error()) 105 os.Exit(1) 106 } 107 fmt.Printf("Got %d elements\n", count) 108 for i := 0; i < count; i++ { 109 fmt.Printf("%d = %d\n", keys[i], values[i]) 110 } 111 112 fmt.Println("\n[update batch]") 113 fmt.Println("------------------------") 114 115 for i := 0; i < mapSize; i++ { 116 values[i] = uint64(values[i] + 100) 117 } 118 119 count, err = testMap.SetBatch(&keys, &values, bpfsys.BPFMapElemExists, mapSize) 120 if err != nil { 121 fmt.Fprintf(os.Stderr, "error while getting batch: %s\n", err.Error()) 122 os.Exit(1) 123 } 124 fmt.Printf("Updated %d elements\n", count) 125 126 count, _, err = testMap.GetBatch(&keys, &values, mapSize) 127 if err != nil { 128 fmt.Fprintf(os.Stderr, "error while getting batch: %s\n", err.Error()) 129 os.Exit(1) 130 } 131 fmt.Printf("Got %d elements\n", count) 132 for i := 0; i < count; i++ { 133 fmt.Printf("%d = %d\n", keys[i], values[i]) 134 } 135 }