Exemplo n.º 1
0
int main (int ac, char **argv)
{
    if (ac != 3) {
        printf("Usage: analysis <lat_start> <lat_end>\n");
        return -1;
    }

    char filename[256];

    int lat_start = bpf_obj_get(argv[1]);
    int lat_end = bpf_obj_get(argv[2]);
    
    struct Time {long val;};
    struct Time start = { .val = 0,};
	struct Time end = {.val = 0,};
 
    int key = 0;
    long sum = 0;
	for (key = 1; key <= 100; key++) {
       bpf_lookup_elem(lat_start, &key, &start);
       bpf_lookup_elem(lat_end, &key, &end);
       printf("%d %ld\n", key, end.val - start.val);
       sum+= (end.val - start.val);
    }

   printf("Average: %f\n", sum/(float)100);
	return 0;
}
Exemplo n.º 2
0
static int bpf_do_map(const char *file, uint32_t flags, uint32_t key,
		      uint32_t value)
{
	int fd, ret;

	if (flags & BPF_F_PIN) {
		fd = bpf_map_create();
		printf("bpf: map fd:%d (%s)\n", fd, strerror(errno));
		assert(fd > 0);

		ret = bpf_obj_pin(fd, file);
		printf("bpf: pin ret:(%d,%s)\n", ret, strerror(errno));
		assert(ret == 0);
	} else {
		fd = bpf_obj_get(file);
		printf("bpf: get fd:%d (%s)\n", fd, strerror(errno));
		assert(fd > 0);
	}

	if ((flags & BPF_F_KEY_VAL) == BPF_F_KEY_VAL) {
		ret = bpf_map_update_elem(fd, &key, &value, 0);
		printf("bpf: fd:%d u->(%u:%u) ret:(%d,%s)\n", fd, key, value,
		       ret, strerror(errno));
		assert(ret == 0);
	} else if (flags & BPF_F_KEY) {
		ret = bpf_map_lookup_elem(fd, &key, &value);
		printf("bpf: fd:%d l->(%u):%u ret:(%d,%s)\n", fd, key, value,
		       ret, strerror(errno));
		assert(ret == 0);
	}

	return 0;
}
Exemplo n.º 3
0
static int bpf_do_prog(const char *file, uint32_t flags, const char *object)
{
	int fd, sock, ret;

	if (flags & BPF_F_PIN) {
		fd = bpf_prog_create(object);
		printf("bpf: prog fd:%d (%s)\n", fd, strerror(errno));
		assert(fd > 0);

		ret = bpf_obj_pin(fd, file);
		printf("bpf: pin ret:(%d,%s)\n", ret, strerror(errno));
		assert(ret == 0);
	} else {
		fd = bpf_obj_get(file);
		printf("bpf: get fd:%d (%s)\n", fd, strerror(errno));
		assert(fd > 0);
	}

	sock = open_raw_sock("lo");
	assert(sock > 0);

	ret = setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &fd, sizeof(fd));
	printf("bpf: sock:%d <- fd:%d attached ret:(%d,%s)\n", sock, fd,
	       ret, strerror(errno));
	assert(ret == 0);

	return 0;
}
Exemplo n.º 4
0
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;
}
Exemplo n.º 5
0
int main(int argc, char **argv)
{
	const char *pinned_file = NULL;
	int ifindex = -1;
	int array_key = 0;
	int array_fd = -1;
	int ret = -1;
	int opt;

	while ((opt = getopt(argc, argv, "F:U:i:")) != -1) {
		switch (opt) {
		/* General args */
		case 'U':
			pinned_file = optarg;
			break;
		case 'i':
			ifindex = atoi(optarg);
			break;
		default:
			usage();
			goto out;
		}
	}

	if (ifindex < 0 || !pinned_file) {
		usage();
		goto out;
	}

	array_fd = bpf_obj_get(pinned_file);
	if (array_fd < 0) {
		fprintf(stderr, "bpf_obj_get(%s): %s(%d)\n",
			pinned_file, strerror(errno), errno);
		goto out;
	}

	/* bpf_tunnel_key.remote_ipv4 expects host byte orders */
	ret = bpf_map_update_elem(array_fd, &array_key, &ifindex, 0);
	if (ret) {
		perror("bpf_map_update_elem");
		goto out;
	}

out:
	if (array_fd != -1)
		close(array_fd);
	return ret;
}
Exemplo n.º 6
0
int main (int ac, char **argv) 
{
	if (ac != 3) {
		printf("Usage: master_host <module.bpf> <prog_path>\n");
		return -1;
	}
	
	char filename[256];
	snprintf(filename, sizeof(filename), "%s", argv[1]);
	if (load_bpf_file(filename)){
		printf("%s", bpf_log_buf);
		return 1;
	}
	
	int key = 0;
	int fd = bpf_obj_get(argv[2]);
	if (fd < 0) {
		printf("Failed to load container module %s\n", argv[2]);
		return -1;
	}
	
	int ret = bpf_update_elem(map_fd[0], &key, &fd, BPF_ANY);
	if (ret != 0) {
		printf("Failed to add key-value pair %s\n", strerror(errno));
		return -1;
	}
	
	int sock = open_raw_sock("lo");

	assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, prog_fd,
            sizeof(prog_fd[0])) == 0);

	FILE *f = popen("ping -c5 localhost", "r");
	(void) f;
	
	char q;
	while (q != 'q') {
	    printf("Enter 'q' to exit\n");
		q = getchar();
	}
		
	return 0;
}
/* Load existing map via filesystem, if possible */
int load_map_file(const char *file, struct bpf_map_data *map_data)
{
	int fd;

	if (bpf_fs_check_path(file) < 0) {
		exit(EXIT_FAIL_MAP_FS);
	}

	fd = bpf_obj_get(file);
	if (fd > 0) { /* Great: map file already existed use it */
		// FIXME: Verify map size etc is the same before returning it!
		// data available via map->def.XXX and fdinfo
		if (verbose)
			printf(" - Loaded bpf-map:%-30s from file:%s\n",
			       map_data->name, file);
		return fd;
	}
	return -1;
}
Exemplo n.º 8
0
int main(int argc, char **argv)
{
    const char *pinned_file = NULL, *cg2 = NULL;
    int create_array = 1;
    int array_key = 0;
    int array_fd = -1;
    int cg2_fd = -1;
    int ret = -1;
    int opt;

    while ((opt = getopt(argc, argv, "F:U:v:")) != -1) {
        switch (opt) {
        /* General args */
        case 'F':
            pinned_file = optarg;
            break;
        case 'U':
            pinned_file = optarg;
            create_array = 0;
            break;
        case 'v':
            cg2 = optarg;
            break;
        default:
            usage();
            goto out;
        }
    }

    if (!cg2 || !pinned_file) {
        usage();
        goto out;
    }

    cg2_fd = open(cg2, O_RDONLY);
    if (cg2_fd < 0) {
        fprintf(stderr, "open(%s,...): %s(%d)\n",
                cg2, strerror(errno), errno);
        goto out;
    }

    if (create_array) {
        array_fd = bpf_create_map(BPF_MAP_TYPE_CGROUP_ARRAY,
                                  sizeof(uint32_t), sizeof(uint32_t),
                                  1, 0);
        if (array_fd < 0) {
            fprintf(stderr,
                    "bpf_create_map(BPF_MAP_TYPE_CGROUP_ARRAY,...): %s(%d)\n",
                    strerror(errno), errno);
            goto out;
        }
    } else {
        array_fd = bpf_obj_get(pinned_file);
        if (array_fd < 0) {
            fprintf(stderr, "bpf_obj_get(%s): %s(%d)\n",
                    pinned_file, strerror(errno), errno);
            goto out;
        }
    }

    ret = bpf_map_update_elem(array_fd, &array_key, &cg2_fd, 0);
    if (ret) {
        perror("bpf_map_update_elem");
        goto out;
    }

    if (create_array) {
        ret = bpf_obj_pin(array_fd, pinned_file);
        if (ret) {
            fprintf(stderr, "bpf_obj_pin(..., %s): %s(%d)\n",
                    pinned_file, strerror(errno), errno);
            goto out;
        }
    }

out:
    if (array_fd != -1)
        close(array_fd);
    if (cg2_fd != -1)
        close(cg2_fd);
    return ret;
}