Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* Copyright (c) 2019 Facebook
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * This program is free software; you can redistribute it and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * modify it under the terms of version 2 of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * License as published by the Free Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Example program for Host Bandwidth Managment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * This program loads a cgroup skb BPF program to enforce cgroup output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * (egress) or input (ingress) bandwidth limits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * USAGE: hbm [-d] [-l] [-n <id>] [-r <rate>] [-s] [-t <secs>] [-w] [-h] [prog]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *   Where:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *    -d	Print BPF trace debug buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *    -l	Also limit flows doing loopback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *    -n <#>	To create cgroup \"/hbm#\" and attach prog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *		Default is /hbm1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *    --no_cn   Do not return cn notifications
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *    -r <rate>	Rate limit in Mbps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *    -s	Get HBM stats (marked, dropped, etc.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *    -t <time>	Exit after specified seconds (default is 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *    -w	Work conserving flag. cgroup can increase its bandwidth
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *		beyond the rate limit specified while there is available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *		bandwidth. Current implementation assumes there is only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  *		NIC (eth0), but can be extended to support multiple NICs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  *		Currrently only supported for egress.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  *    -h	Print this info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  *    prog	BPF program file name. Name defaults to hbm_out_kern.o
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define _GNU_SOURCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <sys/resource.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <sys/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #include <linux/unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #include <linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #include <bpf/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #include <getopt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #include "bpf_load.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #include "bpf_rlimit.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #include "cgroup_helpers.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #include "hbm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #include "bpf_util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #include <bpf/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) bool outFlag = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) int minRate = 1000;		/* cgroup rate limit in Mbps */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) int rate = 1000;		/* can grow if rate conserving is enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) int dur = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) bool stats_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) bool loopback_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) bool debugFlag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) bool work_conserving_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) bool no_cn_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) bool edt_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static void Usage(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static void read_trace_pipe2(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static void do_error(char *msg, bool errno_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) #define DEBUGFS "/sys/kernel/debug/tracing/"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) struct bpf_object *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) int bpfprog_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) int cgroup_storage_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static void read_trace_pipe2(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	int trace_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	FILE *outf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	char *outFname = "hbm_out.log";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (trace_fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		printf("Error opening trace_pipe\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) //	Future support of ingress
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) //	if (!outFlag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) //		outFname = "hbm_in.log";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	outf = fopen(outFname, "w");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (outf == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		printf("Error creating %s\n", outFname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		static char buf[4097];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		ssize_t sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		sz = read(trace_fd, buf, sizeof(buf) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		if (sz > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			buf[sz] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			puts(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			if (outf != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 				fprintf(outf, "%s\n", buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 				fflush(outf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static void do_error(char *msg, bool errno_flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (errno_flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		printf("ERROR: %s, errno: %d\n", msg, errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		printf("ERROR: %s\n", msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static int prog_load(char *prog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	struct bpf_prog_load_attr prog_load_attr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		.prog_type = BPF_PROG_TYPE_CGROUP_SKB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		.file = prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		.expected_attach_type = BPF_CGROUP_INET_EGRESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	int map_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	struct bpf_map *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (access(prog, O_RDONLY) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		printf("Error accessing file %s: %s\n", prog, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (bpf_prog_load_xattr(&prog_load_attr, &obj, &bpfprog_fd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		map = bpf_object__find_map_by_name(obj, "queue_stats");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		map_fd = bpf_map__fd(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		if (map_fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			printf("Map not found: %s\n", strerror(map_fd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		printf("ERROR: bpf_prog_load_xattr failed for: %s\n", prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		printf("  Output from verifier:\n%s\n------\n", bpf_log_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		ret = map_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static int run_bpf_prog(char *prog, int cg_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	int map_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	int key = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	int cg1 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	int type = BPF_CGROUP_INET_EGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	char cg_dir[100];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	struct hbm_queue_stats qstats = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	sprintf(cg_dir, "/hbm%d", cg_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	map_fd = prog_load(prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (map_fd  == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (setup_cgroup_environment()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		printf("ERROR: setting cgroup environment\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	cg1 = create_and_get_cgroup(cg_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (!cg1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		printf("ERROR: create_and_get_cgroup\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	if (join_cgroup(cg_dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		printf("ERROR: join_cgroup\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	qstats.rate = rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	qstats.stats = stats_flag ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	qstats.loopback = loopback_flag ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	qstats.no_cn = no_cn_flag ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (bpf_map_update_elem(map_fd, &key, &qstats, BPF_ANY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		printf("ERROR: Could not update map element\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	if (!outFlag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		type = BPF_CGROUP_INET_INGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	if (bpf_prog_attach(bpfprog_fd, cg1, type, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		printf("ERROR: bpf_prog_attach fails!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		log_err("Attaching prog");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (work_conserving_flag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		struct timeval t0, t_last, t_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		FILE *fin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		unsigned long long last_eth_tx_bytes, new_eth_tx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		signed long long last_cg_tx_bytes, new_cg_tx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		signed long long delta_time, delta_bytes, delta_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		int delta_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) #define DELTA_RATE_CHECK 10000		/* in us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) #define RATE_THRESHOLD 9500000000	/* 9.5 Gbps */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		bpf_map_lookup_elem(map_fd, &key, &qstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		if (gettimeofday(&t0, NULL) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			do_error("gettimeofday failed", true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		t_last = t0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		fin = fopen("/sys/class/net/eth0/statistics/tx_bytes", "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		if (fscanf(fin, "%llu", &last_eth_tx_bytes) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			do_error("fscanf fails", false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		fclose(fin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		last_cg_tx_bytes = qstats.bytes_total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 			usleep(DELTA_RATE_CHECK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			if (gettimeofday(&t_new, NULL) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 				do_error("gettimeofday failed", true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			delta_ms = (t_new.tv_sec - t0.tv_sec) * 1000 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 				(t_new.tv_usec - t0.tv_usec)/1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			if (delta_ms > dur * 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			delta_time = (t_new.tv_sec - t_last.tv_sec) * 1000000 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 				(t_new.tv_usec - t_last.tv_usec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			if (delta_time == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			t_last = t_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			fin = fopen("/sys/class/net/eth0/statistics/tx_bytes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 				    "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			if (fscanf(fin, "%llu", &new_eth_tx_bytes) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 				do_error("fscanf fails", false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			fclose(fin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			printf("  new_eth_tx_bytes:%llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			       new_eth_tx_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			bpf_map_lookup_elem(map_fd, &key, &qstats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			new_cg_tx_bytes = qstats.bytes_total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			delta_bytes = new_eth_tx_bytes - last_eth_tx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			last_eth_tx_bytes = new_eth_tx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			delta_rate = (delta_bytes * 8000000) / delta_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			printf("%5d - eth_rate:%.1fGbps cg_rate:%.3fGbps",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			       delta_ms, delta_rate/1000000000.0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			       rate/1000.0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			if (delta_rate < RATE_THRESHOLD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 				/* can increase cgroup rate limit, but first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 				 * check if we are using the current limit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 				 * Currently increasing by 6.25%, unknown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 				 * if that is the optimal rate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 				int rate_diff100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 				delta_bytes = new_cg_tx_bytes -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 					last_cg_tx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 				last_cg_tx_bytes = new_cg_tx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 				delta_rate = (delta_bytes * 8000000) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 					delta_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 				printf(" rate:%.3fGbps",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 				       delta_rate/1000000000.0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 				rate_diff100 = (((long long)rate)*1000000 -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 						     delta_rate) * 100 /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 					(((long long) rate) * 1000000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 				printf("  rdiff:%d", rate_diff100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 				if (rate_diff100  <= 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 					rate += (rate >> 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 					if (rate > RATE_THRESHOLD / 1000000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 						rate = RATE_THRESHOLD / 1000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 					qstats.rate = rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 					printf(" INC\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 					printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 				/* Need to decrease cgroup rate limit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 				 * Currently decreasing by 12.5%, unknown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 				 * if that is optimal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 				printf(" DEC\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 				rate -= (rate >> 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 				if (rate < minRate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 					rate = minRate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 				qstats.rate = rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			if (bpf_map_update_elem(map_fd, &key, &qstats, BPF_ANY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 				do_error("update map element fails", false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		sleep(dur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	// Get stats!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	if (stats_flag && bpf_map_lookup_elem(map_fd, &key, &qstats)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		char fname[100];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		FILE *fout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		if (!outFlag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			sprintf(fname, "hbm.%d.in", cg_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			sprintf(fname, "hbm.%d.out", cg_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		fout = fopen(fname, "w");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		fprintf(fout, "id:%d\n", cg_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		fprintf(fout, "ERROR: Could not lookup queue_stats\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	} else if (stats_flag && qstats.lastPacketTime >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		   qstats.firstPacketTime) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		long long delta_us = (qstats.lastPacketTime -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 				      qstats.firstPacketTime)/1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		unsigned int rate_mbps = ((qstats.bytes_total -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 					   qstats.bytes_dropped) * 8 /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 					  delta_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		double percent_pkts, percent_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		char fname[100];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		FILE *fout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		int k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		static const char *returnValNames[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			"DROP_PKT",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			"ALLOW_PKT",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			"DROP_PKT_CWR",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 			"ALLOW_PKT_CWR"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) #define RET_VAL_COUNT 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) // Future support of ingress
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) //		if (!outFlag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) //			sprintf(fname, "hbm.%d.in", cg_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) //		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		sprintf(fname, "hbm.%d.out", cg_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		fout = fopen(fname, "w");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		fprintf(fout, "id:%d\n", cg_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		fprintf(fout, "rate_mbps:%d\n", rate_mbps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		fprintf(fout, "duration:%.1f secs\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			(qstats.lastPacketTime - qstats.firstPacketTime) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 			1000000000.0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		fprintf(fout, "packets:%d\n", (int)qstats.pkts_total);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		fprintf(fout, "bytes_MB:%d\n", (int)(qstats.bytes_total /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 						     1000000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		fprintf(fout, "pkts_dropped:%d\n", (int)qstats.pkts_dropped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		fprintf(fout, "bytes_dropped_MB:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			(int)(qstats.bytes_dropped /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 						       1000000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		// Marked Pkts and Bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		percent_pkts = (qstats.pkts_marked * 100.0) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			(qstats.pkts_total + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		percent_bytes = (qstats.bytes_marked * 100.0) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			(qstats.bytes_total + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		fprintf(fout, "pkts_marked_percent:%6.2f\n", percent_pkts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		fprintf(fout, "bytes_marked_percent:%6.2f\n", percent_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		// Dropped Pkts and Bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		percent_pkts = (qstats.pkts_dropped * 100.0) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 			(qstats.pkts_total + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		percent_bytes = (qstats.bytes_dropped * 100.0) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 			(qstats.bytes_total + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		fprintf(fout, "pkts_dropped_percent:%6.2f\n", percent_pkts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		fprintf(fout, "bytes_dropped_percent:%6.2f\n", percent_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		// ECN CE markings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		percent_pkts = (qstats.pkts_ecn_ce * 100.0) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 			(qstats.pkts_total + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		fprintf(fout, "pkts_ecn_ce:%6.2f (%d)\n", percent_pkts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			(int)qstats.pkts_ecn_ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		// Average cwnd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		fprintf(fout, "avg cwnd:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			(int)(qstats.sum_cwnd / (qstats.sum_cwnd_cnt + 1)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		// Average rtt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		fprintf(fout, "avg rtt:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 			(int)(qstats.sum_rtt / (qstats.pkts_total + 1)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		// Average credit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		if (edt_flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 			fprintf(fout, "avg credit_ms:%.03f\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 				(qstats.sum_credit /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 				 (qstats.pkts_total + 1.0)) / 1000000.0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			fprintf(fout, "avg credit:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 				(int)(qstats.sum_credit /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 				      (1500 * ((int)qstats.pkts_total ) + 1)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		// Return values stats
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		for (k = 0; k < RET_VAL_COUNT; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 			percent_pkts = (qstats.returnValCount[k] * 100.0) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 				(qstats.pkts_total + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 			fprintf(fout, "%s:%6.2f (%d)\n", returnValNames[k],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 				percent_pkts, (int)qstats.returnValCount[k]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		fclose(fout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (debugFlag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		read_trace_pipe2();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	rc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	if (cg1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		close(cg1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	cleanup_cgroup_environment();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) static void Usage(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	printf("This program loads a cgroup skb BPF program to enforce\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	       "cgroup output (egress) bandwidth limits.\n\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	       "USAGE: hbm [-o] [-d]  [-l] [-n <id>] [--no_cn] [-r <rate>]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	       "           [-s] [-t <secs>] [-w] [-h] [prog]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	       "  Where:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	       "    -o         indicates egress direction (default)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	       "    -d         print BPF trace debug buffer\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	       "    --edt      use fq's Earliest Departure Time\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	       "    -l         also limit flows using loopback\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	       "    -n <#>     to create cgroup \"/hbm#\" and attach prog\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	       "               Default is /hbm1\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	       "    --no_cn    disable CN notifications\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	       "    -r <rate>  Rate in Mbps\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	       "    -s         Update HBM stats\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	       "    -t <time>  Exit after specified seconds (default is 0)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	       "    -w	       Work conserving flag. cgroup can increase\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	       "               bandwidth beyond the rate limit specified\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	       "               while there is available bandwidth. Current\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	       "               implementation assumes there is only eth0\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	       "               but can be extended to support multiple NICs\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	       "    -h         print this info\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	       "    prog       BPF program file name. Name defaults to\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	       "                 hbm_out_kern.o\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	char *prog = "hbm_out_kern.o";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	int  k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	int cg_id = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	char *optstring = "iodln:r:st:wh";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	struct option loptions[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		{"no_cn", 0, NULL, 1},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		{"edt", 0, NULL, 2},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		{NULL, 0, NULL, 0}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	while ((k = getopt_long(argc, argv, optstring, loptions, NULL)) != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		switch (k) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 			no_cn_flag = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 			prog = "hbm_edt_kern.o";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 			edt_flag = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		case'o':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		case 'd':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 			debugFlag = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		case 'l':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 			loopback_flag = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		case 'n':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 			cg_id = atoi(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		case 'r':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 			minRate = atoi(optarg) * 1.024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 			rate = minRate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		case 's':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 			stats_flag = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		case 't':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 			dur = atoi(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		case 'w':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 			work_conserving_flag = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		case '?':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 			if (optopt == 'n' || optopt == 'r' || optopt == 't')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 				fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 					"Option -%c requires an argument.\n\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 					optopt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		case 'h':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 			__fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 			Usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	if (optind < argc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		prog = argv[optind];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	printf("HBM prog: %s\n", prog != NULL ? prog : "NULL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	return run_bpf_prog(prog, cg_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) }