Esempio 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;
}
Esempio n. 2
0
static void print_hist(int fd)
{
	int key;
	long value;
	long data[MAX_INDEX] = {};
	char starstr[MAX_STARS];
	int i;
	int max_ind = -1;
	long max_value = 0;

	for (key = 0; key < MAX_INDEX; key++) {
		bpf_lookup_elem(fd, &key, &value);
		data[key] = value;
		if (value && key > max_ind)
			max_ind = key;
		if (value > max_value)
			max_value = value;
	}

	printf("           syscall write() stats\n");
	printf("     byte_size       : count     distribution\n");
	for (i = 1; i <= max_ind + 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);
	}
}
Esempio n. 3
0
static void print_hist(int fd)
{
	unsigned int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
	__u64 total_events = 0;
	long values[nr_cpus];
	__u64 max_cnt = 0;
	__u64 cnt[SLOTS];
	__u64 value;
	__u32 key;
	int i;

	for (key = 0; key < SLOTS; key++) {
		bpf_lookup_elem(fd, &key, values);
		value = 0;
		for (i = 0; i < nr_cpus; i++)
			value += values[i];
		cnt[key] = value;
		total_events += value;
		if (value > max_cnt)
			max_cnt = value;
	}
	clear_stats(fd);
	for (key = full_range ? 0 : 29; key < SLOTS; key++) {
		int c = num_colors * cnt[key] / (max_cnt + 1);

		if (text_only)
			printf("%s", sym[c]);
		else
			printf("%s %s", color[c], nocolor);
	}
	printf(" # %lld\n", total_events);
}
Esempio n. 4
0
static unsigned long 
get_value(int key) {
	unsigned long value;
	if (bpf_lookup_elem(mapfd, &key, &value)) {
		fail("bpf_lookup_elem failed '%s'\n", strerror(errno));
	}
	return value;
}
Esempio n. 5
0
static int attach_filter(int cg_fd, int type, int verdict)
{
	int prog_fd, map_fd, ret, key;
	long long pkt_cnt, byte_cnt;

	map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY,
				sizeof(key), sizeof(byte_cnt),
				256, 0);
	if (map_fd < 0) {
		printf("Failed to create map: '%s'\n", strerror(errno));
		return EXIT_FAILURE;
	}

	prog_fd = prog_load(map_fd, verdict);
	printf("Output from kernel verifier:\n%s\n-------\n", bpf_log_buf);

	if (prog_fd < 0) {
		printf("Failed to load prog: '%s'\n", strerror(errno));
		return EXIT_FAILURE;
	}

	ret = bpf_prog_attach(prog_fd, cg_fd, type);
	if (ret < 0) {
		printf("Failed to attach prog to cgroup: '%s'\n",
		       strerror(errno));
		return EXIT_FAILURE;
	}
	while (1) {
		key = MAP_KEY_PACKETS;
		assert(bpf_lookup_elem(map_fd, &key, &pkt_cnt) == 0);

		key = MAP_KEY_BYTES;
		assert(bpf_lookup_elem(map_fd, &key, &byte_cnt) == 0);

		printf("cgroup received %lld packets, %lld bytes\n",
		       pkt_cnt, byte_cnt);
		sleep(1);
	}

	return EXIT_SUCCESS;
}
Esempio n. 6
0
int main (int ac, char **argv) 
{
	if (ac != 3) {
		printf("Usage: master <module.bpf> <map_path>\n");
		return -1;
	}
	
	char filename[256];
	
	int sock;
	snprintf(filename, sizeof(filename), "%s", argv[1]);
	if (load_bpf_file(filename)){
		printf("%s", bpf_log_buf);
		return 1;
	}
	
	/* Initializing our map with key val pair ( IPPROTO_ICMP,0) */	
	int key = IPPROTO_ICMP;
	long value = 0;
	int ret = bpf_update_elem(map_fd[0], &key, &value, BPF_ANY);
	if (ret != 0) {
		printf("Failed to add key-value pair: %s\n",strerror(errno));	
		return -1;
	}
	
	sock = open_raw_sock("lo");
	
	
	assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, prog_fd,
			sizeof(prog_fd[0])) == 0);

	/* Persisting map */
	assert(bpf_obj_pin(map_fd[0],argv[2]) == 0);
	printf("MAP persisted to %s\n", argv[2]);
	
	// Pinging to localhost
	FILE * f = popen("ping -c5 localhost", "r");
	(void) f; //Suppress unused variable warning from compiler

	
	long icmp_cnt = 0;
	char q;
	while (q != 'q') {
		assert(bpf_lookup_elem(map_fd[0], &key, &icmp_cnt) == 0);
		printf("ICMP Count %ld | Enter 'q' to quit: ", icmp_cnt);	
		q = getchar();
	}
	
	return 0;
}
Esempio n. 7
0
static void bpf_dump_drops(int fd)
{
	int cpu, max;

	max = sysconf(_SC_NPROCESSORS_ONLN);

	printf(" `- number of drops:");
	for (cpu = 0; cpu < max; cpu++) {
		long drops;

		assert(bpf_lookup_elem(fd, &cpu, &drops) == 0);
		printf("\tcpu%d: %5ld", cpu, drops);
	}
	printf("\n");
}
Esempio n. 8
0
static void bpf_dump_queue(int fd)
{
	/* Just for the same of the example. */
	int max_queue = 4, i;

	printf("  | nic queues:");
	for (i = 0; i < max_queue; i++) {
		struct count_queue cq;
		int ret;

		memset(&cq, 0, sizeof(cq));
		ret = bpf_lookup_elem(fd, &i, &cq);
		assert(ret == 0 || (ret < 0 && errno == ENOENT));

		printf("\tq%d:[pkts: %ld, mis: %ld]",
		       i, cq.total, cq.mismatch);
	}
	printf("\n");
}
Esempio n. 9
0
static void bpf_dump_proto(int fd)
{
	uint8_t protos[] = { IPPROTO_TCP, IPPROTO_UDP, IPPROTO_ICMP };
	char *names[] = { "tcp", "udp", "icmp" };
	int i;

	printf("  ` protos:");
	for (i = 0; i < ARRAY_SIZE(protos); i++) {
		struct count_tuple ct;
		int ret;

		memset(&ct, 0, sizeof(ct));
		ret = bpf_lookup_elem(fd, &protos[i], &ct);
		assert(ret == 0 || (ret < 0 && errno == ENOENT));

		printf("\t%s:[pkts: %ld, bytes: %ld]",
		       names[i], ct.packets, ct.bytes);
	}
	printf("\n");
}
Esempio n. 10
0
int main(int ac, char **argv)
{
	char filename[256];
	long key, next_key, value;
	FILE *f;
	int i;

	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);

	signal(SIGINT, int_exit);

	/* start 'ping' in the background to have some kfree_skb events */
	f = popen("ping -c5 localhost", "r");
	(void) f;

	/* start 'dd' in the background to have plenty of 'write' syscalls */
	f = popen("dd if=/dev/zero of=/dev/null count=5000000", "r");
	(void) f;

	if (load_bpf_file(filename)) {
		printf("%s", bpf_log_buf);
		return 1;
	}

	for (i = 0; i < 5; i++) {
		key = 0;
		while (bpf_get_next_key(map_fd[0], &key, &next_key) == 0) {
			bpf_lookup_elem(map_fd[0], &next_key, &value);
			printf("location 0x%lx count %ld\n", next_key, value);
			key = next_key;
		}
		if (key)
			printf("\n");
		sleep(1);
	}
	print_hist(map_fd[1]);

	return 0;
}