dropbox / goebpf

Library to work with eBPF programs from Go
Other
1.14k stars 85 forks source link

error while loading BPF_MAP_TYPE_PROG_ARRAY map #63

Closed SunHu1 closed 2 years ago

SunHu1 commented 2 years ago

A part of my program is as below,encountered this problem while executing. Loadmap() failed: ebpf_map_update_elem() failed: Invalid argument this program works when i changed BPF_MAP_TYPE_PROG_ARRAY to others like BPF_MAP_TYPE_PERCPU_ARRAY

var elf = flag.String("elf", "/usr/src/linux-source-5.11.0/samples/bpf/test_dump.o", "clang/llvm compiled binary file") var elf1 = flag.String("elf1", "xdp_fw.o", "clang/llvm compiled binary file") // Create eBPF system bpf := goebpf.NewDefaultEbpfSystem() // Load .ELF files compiled by clang/llvm err := bpf.LoadElf(elf, 1) if err != nil { fatalError("Loadbpf() failed: %v", err) } next_prog_map := bpf.GetMapByName("next_prog_map").(goebpf.EbpfMap) fmt.Println("46464646", next_prog_map) bpf1 := goebpf.NewDefaultEbpfSystem() // Load .ELF files compiled by clang/llvm err = bpf1.LoadElf(*elf1, 1) if err != nil { fatalError("Loadbpf1() failed: %v", err) } shtest := bpf1.GetProgramByName("firewall") err = next_prog_map.Update(0, shtest.GetFd()) if err != nil { fatalError("Loadmap() failed: %v", err) }

SunHu1 commented 2 years ago

I get it, I need to call the load function like this shtest := bpf1.GetProgramByName("firewall") err = shtest.Load() Otherwise I will get a nil fd