/* Define one or more maps in the maps section, for example * define a map of type array int -> uint32_t, with 10 entries */ DEFINE_BPF_MAP(name_of_my_map, ARRAY, int, uint32_t, 10);
/* this will also define type-safe accessors: * value * bpf_name_of_my_map_lookup_elem(&key); * int bpf_name_of_my_map_update_elem(&key, &value, flags); * int bpf_name_of_my_map_delete_elem(&key); * as such it is heavily suggested to use lowercase *_map names. * Also note that due to compiler deficiencies you cannot use a type * of 'struct foo' but must instead use just 'foo'. As such structs * must not be defined as 'struct foo {}' and must instead be * 'typedef struct {} foo'. */
SEC("PROGTYPE/PROGNAME") intPROGFUNC(..args..) { <body-of-code ... read or write to MY_MAPNAME ... do other things > }
char _license[] SEC("license") = "GPL"; // or other license
ret = readSectionByIdx(elfFile, i, cs_temp.data); if (ret) return ret; ALOGD("Loaded code section %d (%s)\n", i, name.c_str()); }
/* Check for rel section */ if (cs_temp.data.size() > 0 && i < entries) { ret = getSymName(elfFile, shTable[i + 1].sh_name, name); if (ret) return ret;
if (isRelSection(cs_temp, name)) { ret = readSectionByIdx(elfFile, i + 1, cs_temp.rel_data); if (ret) return ret; ALOGD("Loaded relo section %d (%s)\n", i, name.c_str()); } }
if (cs_temp.data.size() > 0) { cs.push_back(cs_temp); ALOGD("Adding section %d to cs list\n", i); } } return0; }
ret = readSectionByName("maps", elfFile, mdData); if (ret) return ret; md.resize(mdData.size() / sizeof(struct bpf_map_def)); memcpy(md.data(), mdData.data(), mdData.size());
ret = getMapNames(elfFile, mapNames); if (ret) return ret;
mapFds.resize(mapNames.size());
for (int i = 0; i < (int)mapNames.size(); i++) { // Format of pin location is /sys/fs/bpf/map_<filename>_<mapname> string mapPinLoc; bool reuse = false;
static Status initPrograms(){ std::string cg2_path;
if (!CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cg2_path)) { int ret = errno; ALOGE("Failed to find cgroup v2 root"); returnstatusFromErrno(ret, "Failed to find cgroup v2 root"); }
unique_fd cg_fd(open(cg2_path.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC)); if (cg_fd == -1) { int ret = errno; ALOGE("Failed to open the cgroup directory: %s", strerror(ret)); returnstatusFromErrno(ret, "Open the cgroup directory failed"); } RETURN_IF_NOT_OK(attachProgramToCgroup(BPF_EGRESS_PROG_PATH, cg_fd, BPF_CGROUP_INET_EGRESS)); RETURN_IF_NOT_OK(attachProgramToCgroup(BPF_INGRESS_PROG_PATH, cg_fd, BPF_CGROUP_INET_INGRESS));
// For the devices that support cgroup socket filter, the socket filter // should be loaded successfully by bpfloader. So we attach the filter to // cgroup if the program is pinned properly. // TODO: delete the if statement once all devices should support cgroup // socket filter (ie. the minimum kernel version required is 4.14). if (!access(CGROUP_SOCKET_PROG_PATH, F_OK)) { RETURN_IF_NOT_OK( attachProgramToCgroup(CGROUP_SOCKET_PROG_PATH, cg_fd, BPF_CGROUP_INET_SOCK_CREATE)); } return netdutils::status::ok; }