static void print_stacks(void) { struct key_t key = {}, next_key; __u64 value; __u32 stackid = 0, next_id; int fd = map_fd[0], stack_map = map_fd[1]; sys_read_seen = sys_write_seen = false; while (bpf_map_get_next_key(fd, &key, &next_key) == 0) { bpf_map_lookup_elem(fd, &next_key, &value); print_stack(&next_key, value); bpf_map_delete_elem(fd, &next_key); key = next_key; } if (!sys_read_seen || !sys_write_seen) { printf("BUG kernel stack doesn't contain sys_read() and sys_write()\n"); int_exit(0); } /* clear stack map */ while (bpf_map_get_next_key(stack_map, &stackid, &next_id) == 0) { bpf_map_delete_elem(stack_map, &next_id); stackid = next_id; } }
static void print_stacks(int fd) { struct key_t key = {}, next_key; __u64 value; while (bpf_map_get_next_key(fd, &key, &next_key) == 0) { bpf_map_lookup_elem(fd, &next_key, &value); print_stack(&next_key, value); key = next_key; } }
int main(int argc, char **argv) { unsigned int nr_cpus = bpf_num_possible_cpus(); const char *map_filename = "/sys/fs/bpf/tc/globals/lwt_len_hist_map"; uint64_t values[nr_cpus], sum, max_value = 0, data[MAX_INDEX] = {}; uint64_t key = 0, next_key, max_key = 0; char starstr[MAX_STARS]; int i, map_fd; map_fd = bpf_obj_get(map_filename); if (map_fd < 0) { fprintf(stderr, "bpf_obj_get(%s): %s(%d)\n", map_filename, strerror(errno), errno); return -1; } while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) { if (next_key >= MAX_INDEX) { fprintf(stderr, "Key %lu out of bounds\n", next_key); continue; } bpf_map_lookup_elem(map_fd, &next_key, values); sum = 0; for (i = 0; i < nr_cpus; i++) sum += values[i]; data[next_key] = sum; if (sum && next_key > max_key) max_key = next_key; if (sum > max_value) max_value = sum; key = next_key; } for (i = 1; i <= max_key + 1; i++) { stars(starstr, data[i - 1], max_value, MAX_STARS); printf("%8ld -> %-8ld : %-8ld |%-*s|\n", (1l << i) >> 1, (1l << i) - 1, data[i - 1], MAX_STARS, starstr); } close(map_fd); return 0; }
static void print_old_objects(int fd) { long long val = time_get_ns(); __u64 key, next_key; struct pair v; key = write(1, "\e[1;1H\e[2J", 12); /* clear screen */ key = -1; while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) { bpf_map_lookup_elem(map_fd[0], &next_key, &v); key = next_key; if (val - v.val < 1000000000ll) /* object was allocated more then 1 sec ago */ continue; printf("obj 0x%llx is %2lldsec old was allocated at ip %llx\n", next_key, (val - v.val) / 1000000000ll, v.ip); } }
static int map_subset(int map0, int map1) { unsigned long long next_key = 0; unsigned long long value0[nr_cpus], value1[nr_cpus]; int ret; while (!bpf_map_get_next_key(map1, &next_key, &next_key)) { assert(!bpf_map_lookup_elem(map1, &next_key, value1)); ret = bpf_map_lookup_elem(map0, &next_key, value0); if (ret) { printf("key:%llu not found from map. %s(%d)\n", next_key, strerror(errno), errno); return 0; } if (value0[0] != value1[0]) { printf("key:%llu value0:%llu != value1:%llu\n", next_key, value0[0], value1[0]); return 0; } } return 1; }
static void print_ip_map(int fd) { struct ksym *sym; __u64 key, next_key; __u32 value; int i, max; printf("%-19s %-32s %s\n", "ADDR", "KSYM", "COUNT"); /* fetch IPs and counts */ key = 0, i = 0; while (bpf_map_get_next_key(fd, &key, &next_key) == 0) { bpf_map_lookup_elem(fd, &next_key, &value); counts[i].ip = next_key; counts[i++].count = value; key = next_key; } max = i; /* sort and print */ qsort(counts, max, sizeof(struct ipcount), count_cmp); for (i = 0; i < max; i++) { if (counts[i].ip > PAGE_OFFSET) { sym = ksym_search(counts[i].ip); printf("0x%-17llx %-32s %u\n", counts[i].ip, sym->name, counts[i].count); } else { printf("0x%-17llx %-32s %u\n", counts[i].ip, "(user)", counts[i].count); } } if (max == MAX_IPS) { printf("WARNING: IP hash was full (max %d entries); ", max); printf("may have dropped samples\n"); } }
int main(int argc, char **argv) { struct percpu_net_cnt *percpu_netcnt; struct bpf_cgroup_storage_key key; int map_fd, percpu_map_fd; int error = EXIT_FAILURE; struct net_cnt netcnt; struct bpf_object *obj; int prog_fd, cgroup_fd; unsigned long packets; unsigned long bytes; int cpu, nproc; __u32 prog_cnt; nproc = get_nprocs_conf(); percpu_netcnt = malloc(sizeof(*percpu_netcnt) * nproc); if (!percpu_netcnt) { printf("Not enough memory for per-cpu area (%d cpus)\n", nproc); goto err; } if (bpf_prog_load(BPF_PROG, BPF_PROG_TYPE_CGROUP_SKB, &obj, &prog_fd)) { printf("Failed to load bpf program\n"); goto out; } if (setup_cgroup_environment()) { printf("Failed to load bpf program\n"); goto err; } /* Create a cgroup, get fd, and join it */ cgroup_fd = create_and_get_cgroup(TEST_CGROUP); if (!cgroup_fd) { printf("Failed to create test cgroup\n"); goto err; } if (join_cgroup(TEST_CGROUP)) { printf("Failed to join cgroup\n"); goto err; } /* Attach bpf program */ if (bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_INET_EGRESS, 0)) { printf("Failed to attach bpf program"); goto err; } if (system("which ping6 &>/dev/null") == 0) assert(!system("ping6 localhost -c 10000 -f -q > /dev/null")); else assert(!system("ping -6 localhost -c 10000 -f -q > /dev/null")); if (bpf_prog_query(cgroup_fd, BPF_CGROUP_INET_EGRESS, 0, NULL, NULL, &prog_cnt)) { printf("Failed to query attached programs"); goto err; } map_fd = bpf_find_map(__func__, obj, "netcnt"); if (map_fd < 0) { printf("Failed to find bpf map with net counters"); goto err; } percpu_map_fd = bpf_find_map(__func__, obj, "percpu_netcnt"); if (percpu_map_fd < 0) { printf("Failed to find bpf map with percpu net counters"); goto err; } if (bpf_map_get_next_key(map_fd, NULL, &key)) { printf("Failed to get key in cgroup storage\n"); goto err; } if (bpf_map_lookup_elem(map_fd, &key, &netcnt)) { printf("Failed to lookup cgroup storage\n"); goto err; } if (bpf_map_lookup_elem(percpu_map_fd, &key, &percpu_netcnt[0])) { printf("Failed to lookup percpu cgroup storage\n"); goto err; } /* Some packets can be still in per-cpu cache, but not more than * MAX_PERCPU_PACKETS. */ packets = netcnt.packets; bytes = netcnt.bytes; for (cpu = 0; cpu < nproc; cpu++) { if (percpu_netcnt[cpu].packets > MAX_PERCPU_PACKETS) { printf("Unexpected percpu value: %llu\n", percpu_netcnt[cpu].packets); goto err; } packets += percpu_netcnt[cpu].packets; bytes += percpu_netcnt[cpu].bytes; } /* No packets should be lost */ if (packets != 10000) { printf("Unexpected packet count: %lu\n", packets); goto err; } /* Let's check that bytes counter matches the number of packets * multiplied by the size of ipv6 ICMP packet. */ if (bytes != packets * 104) { printf("Unexpected bytes count: %lu\n", bytes); goto err; } error = 0; printf("test_netcnt:PASS\n"); err: cleanup_cgroup_environment(); free(percpu_netcnt); out: return error; }