1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * Example program for Host Bandwidth Managment
10 * This program loads a cgroup skb BPF program to enforce cgroup output
11 * (egress) or input (ingress) bandwidth limits.
13 * USAGE: hbm [-d] [-l] [-n <id>] [-r <rate>] [-s] [-t <secs>] [-w] [-h] [prog]
15 * -d Print BPF trace debug buffer
16 * -l Also limit flows doing loopback
17 * -n <#> To create cgroup \"/hbm#\" and attach prog
19 * --no_cn Do not return cn notifications
20 * -r <rate> Rate limit in Mbps
21 * -s Get HBM stats (marked, dropped, etc.)
22 * -t <time> Exit after specified seconds (default is 0)
23 * -w Work conserving flag. cgroup can increase its bandwidth
24 * beyond the rate limit specified while there is available
25 * bandwidth. Current implementation assumes there is only
26 * NIC (eth0), but can be extended to support multiple NICs.
27 * Currrently only supported for egress.
29 * prog BPF program file name. Name defaults to hbm_out_kern.o
41 #include <linux/unistd.h>
42 #include <linux/compiler.h>
44 #include <linux/bpf.h>
48 #include "cgroup_helpers.h"
51 #include <bpf/libbpf.h>
54 int minRate = 1000; /* cgroup rate limit in Mbps */
55 int rate = 1000; /* can grow if rate conserving is enabled */
60 bool work_conserving_flag;
64 static void Usage(void);
65 static void read_trace_pipe2(void);
66 static void do_error(char *msg, bool errno_flag);
68 #define TRACEFS "/sys/kernel/tracing/"
70 static struct bpf_program *bpf_prog;
71 static struct bpf_object *obj;
72 static int queue_stats_fd;
74 static void read_trace_pipe2(void)
78 char *outFname = "hbm_out.log";
80 trace_fd = open(TRACEFS "trace_pipe", O_RDONLY, 0);
82 printf("Error opening trace_pipe\n");
86 // Future support of ingress
88 // outFname = "hbm_in.log";
89 outf = fopen(outFname, "w");
92 printf("Error creating %s\n", outFname);
95 static char buf[4097];
98 sz = read(trace_fd, buf, sizeof(buf) - 1);
103 fprintf(outf, "%s\n", buf);
110 static void do_error(char *msg, bool errno_flag)
113 printf("ERROR: %s, errno: %d\n", msg, errno);
115 printf("ERROR: %s\n", msg);
119 static int prog_load(char *prog)
121 struct bpf_program *pos;
122 const char *sec_name;
124 obj = bpf_object__open_file(prog, NULL);
125 if (libbpf_get_error(obj)) {
126 printf("ERROR: opening BPF object file failed\n");
130 /* load BPF program */
131 if (bpf_object__load(obj)) {
132 printf("ERROR: loading BPF object file failed\n");
136 bpf_object__for_each_program(pos, obj) {
137 sec_name = bpf_program__section_name(pos);
138 if (sec_name && !strcmp(sec_name, "cgroup_skb/egress")) {
144 printf("ERROR: finding a prog in obj file failed\n");
148 queue_stats_fd = bpf_object__find_map_fd_by_name(obj, "queue_stats");
149 if (queue_stats_fd < 0) {
150 printf("ERROR: finding a map in obj file failed\n");
157 bpf_object__close(obj);
161 static int run_bpf_prog(char *prog, int cg_id)
163 struct hbm_queue_stats qstats = {0};
164 char cg_dir[100], cg_pin_path[100];
165 struct bpf_link *link = NULL;
170 sprintf(cg_dir, "/hbm%d", cg_id);
171 rc = prog_load(prog);
175 if (setup_cgroup_environment()) {
176 printf("ERROR: setting cgroup environment\n");
179 cg1 = create_and_get_cgroup(cg_dir);
181 printf("ERROR: create_and_get_cgroup\n");
184 if (join_cgroup(cg_dir)) {
185 printf("ERROR: join_cgroup\n");
190 qstats.stats = stats_flag ? 1 : 0;
191 qstats.loopback = loopback_flag ? 1 : 0;
192 qstats.no_cn = no_cn_flag ? 1 : 0;
193 if (bpf_map_update_elem(queue_stats_fd, &key, &qstats, BPF_ANY)) {
194 printf("ERROR: Could not update map element\n");
199 bpf_program__set_expected_attach_type(bpf_prog, BPF_CGROUP_INET_INGRESS);
201 link = bpf_program__attach_cgroup(bpf_prog, cg1);
202 if (libbpf_get_error(link)) {
203 fprintf(stderr, "ERROR: bpf_program__attach_cgroup failed\n");
207 sprintf(cg_pin_path, "/sys/fs/bpf/hbm%d", cg_id);
208 rc = bpf_link__pin(link, cg_pin_path);
210 printf("ERROR: bpf_link__pin failed: %d\n", rc);
214 if (work_conserving_flag) {
215 struct timeval t0, t_last, t_new;
217 unsigned long long last_eth_tx_bytes, new_eth_tx_bytes;
218 signed long long last_cg_tx_bytes, new_cg_tx_bytes;
219 signed long long delta_time, delta_bytes, delta_rate;
221 #define DELTA_RATE_CHECK 10000 /* in us */
222 #define RATE_THRESHOLD 9500000000 /* 9.5 Gbps */
224 bpf_map_lookup_elem(queue_stats_fd, &key, &qstats);
225 if (gettimeofday(&t0, NULL) < 0)
226 do_error("gettimeofday failed", true);
228 fin = fopen("/sys/class/net/eth0/statistics/tx_bytes", "r");
229 if (fscanf(fin, "%llu", &last_eth_tx_bytes) != 1)
230 do_error("fscanf fails", false);
232 last_cg_tx_bytes = qstats.bytes_total;
234 usleep(DELTA_RATE_CHECK);
235 if (gettimeofday(&t_new, NULL) < 0)
236 do_error("gettimeofday failed", true);
237 delta_ms = (t_new.tv_sec - t0.tv_sec) * 1000 +
238 (t_new.tv_usec - t0.tv_usec)/1000;
239 if (delta_ms > dur * 1000)
241 delta_time = (t_new.tv_sec - t_last.tv_sec) * 1000000 +
242 (t_new.tv_usec - t_last.tv_usec);
246 fin = fopen("/sys/class/net/eth0/statistics/tx_bytes",
248 if (fscanf(fin, "%llu", &new_eth_tx_bytes) != 1)
249 do_error("fscanf fails", false);
251 printf(" new_eth_tx_bytes:%llu\n",
253 bpf_map_lookup_elem(queue_stats_fd, &key, &qstats);
254 new_cg_tx_bytes = qstats.bytes_total;
255 delta_bytes = new_eth_tx_bytes - last_eth_tx_bytes;
256 last_eth_tx_bytes = new_eth_tx_bytes;
257 delta_rate = (delta_bytes * 8000000) / delta_time;
258 printf("%5d - eth_rate:%.1fGbps cg_rate:%.3fGbps",
259 delta_ms, delta_rate/1000000000.0,
261 if (delta_rate < RATE_THRESHOLD) {
262 /* can increase cgroup rate limit, but first
263 * check if we are using the current limit.
264 * Currently increasing by 6.25%, unknown
265 * if that is the optimal rate.
269 delta_bytes = new_cg_tx_bytes -
271 last_cg_tx_bytes = new_cg_tx_bytes;
272 delta_rate = (delta_bytes * 8000000) /
274 printf(" rate:%.3fGbps",
275 delta_rate/1000000000.0);
276 rate_diff100 = (((long long)rate)*1000000 -
278 (((long long) rate) * 1000000);
279 printf(" rdiff:%d", rate_diff100);
280 if (rate_diff100 <= 3) {
282 if (rate > RATE_THRESHOLD / 1000000)
283 rate = RATE_THRESHOLD / 1000000;
290 /* Need to decrease cgroup rate limit.
291 * Currently decreasing by 12.5%, unknown
300 if (bpf_map_update_elem(queue_stats_fd, &key, &qstats, BPF_ANY))
301 do_error("update map element fails", false);
307 if (stats_flag && bpf_map_lookup_elem(queue_stats_fd, &key, &qstats)) {
312 sprintf(fname, "hbm.%d.in", cg_id);
314 sprintf(fname, "hbm.%d.out", cg_id);
315 fout = fopen(fname, "w");
316 fprintf(fout, "id:%d\n", cg_id);
317 fprintf(fout, "ERROR: Could not lookup queue_stats\n");
319 } else if (stats_flag && qstats.lastPacketTime >
320 qstats.firstPacketTime) {
321 long long delta_us = (qstats.lastPacketTime -
322 qstats.firstPacketTime)/1000;
323 unsigned int rate_mbps = ((qstats.bytes_total -
324 qstats.bytes_dropped) * 8 /
326 double percent_pkts, percent_bytes;
330 static const char *returnValNames[] = {
336 #define RET_VAL_COUNT 4
338 // Future support of ingress
340 // sprintf(fname, "hbm.%d.in", cg_id);
342 sprintf(fname, "hbm.%d.out", cg_id);
343 fout = fopen(fname, "w");
344 fprintf(fout, "id:%d\n", cg_id);
345 fprintf(fout, "rate_mbps:%d\n", rate_mbps);
346 fprintf(fout, "duration:%.1f secs\n",
347 (qstats.lastPacketTime - qstats.firstPacketTime) /
349 fprintf(fout, "packets:%d\n", (int)qstats.pkts_total);
350 fprintf(fout, "bytes_MB:%d\n", (int)(qstats.bytes_total /
352 fprintf(fout, "pkts_dropped:%d\n", (int)qstats.pkts_dropped);
353 fprintf(fout, "bytes_dropped_MB:%d\n",
354 (int)(qstats.bytes_dropped /
356 // Marked Pkts and Bytes
357 percent_pkts = (qstats.pkts_marked * 100.0) /
358 (qstats.pkts_total + 1);
359 percent_bytes = (qstats.bytes_marked * 100.0) /
360 (qstats.bytes_total + 1);
361 fprintf(fout, "pkts_marked_percent:%6.2f\n", percent_pkts);
362 fprintf(fout, "bytes_marked_percent:%6.2f\n", percent_bytes);
364 // Dropped Pkts and Bytes
365 percent_pkts = (qstats.pkts_dropped * 100.0) /
366 (qstats.pkts_total + 1);
367 percent_bytes = (qstats.bytes_dropped * 100.0) /
368 (qstats.bytes_total + 1);
369 fprintf(fout, "pkts_dropped_percent:%6.2f\n", percent_pkts);
370 fprintf(fout, "bytes_dropped_percent:%6.2f\n", percent_bytes);
373 percent_pkts = (qstats.pkts_ecn_ce * 100.0) /
374 (qstats.pkts_total + 1);
375 fprintf(fout, "pkts_ecn_ce:%6.2f (%d)\n", percent_pkts,
376 (int)qstats.pkts_ecn_ce);
379 fprintf(fout, "avg cwnd:%d\n",
380 (int)(qstats.sum_cwnd / (qstats.sum_cwnd_cnt + 1)));
382 fprintf(fout, "avg rtt:%d\n",
383 (int)(qstats.sum_rtt / (qstats.pkts_total + 1)));
386 fprintf(fout, "avg credit_ms:%.03f\n",
388 (qstats.pkts_total + 1.0)) / 1000000.0);
390 fprintf(fout, "avg credit:%d\n",
391 (int)(qstats.sum_credit /
392 (1500 * ((int)qstats.pkts_total ) + 1)));
394 // Return values stats
395 for (k = 0; k < RET_VAL_COUNT; k++) {
396 percent_pkts = (qstats.returnValCount[k] * 100.0) /
397 (qstats.pkts_total + 1);
398 fprintf(fout, "%s:%6.2f (%d)\n", returnValNames[k],
399 percent_pkts, (int)qstats.returnValCount[k]);
412 bpf_link__destroy(link);
413 bpf_object__close(obj);
419 cleanup_cgroup_environment();
423 static void Usage(void)
425 printf("This program loads a cgroup skb BPF program to enforce\n"
426 "cgroup output (egress) bandwidth limits.\n\n"
427 "USAGE: hbm [-o] [-d] [-l] [-n <id>] [--no_cn] [-r <rate>]\n"
428 " [-s] [-t <secs>] [-w] [-h] [prog]\n"
430 " -o indicates egress direction (default)\n"
431 " -d print BPF trace debug buffer\n"
432 " --edt use fq's Earliest Departure Time\n"
433 " -l also limit flows using loopback\n"
434 " -n <#> to create cgroup \"/hbm#\" and attach prog\n"
435 " Default is /hbm1\n"
436 " --no_cn disable CN notifications\n"
437 " -r <rate> Rate in Mbps\n"
438 " -s Update HBM stats\n"
439 " -t <time> Exit after specified seconds (default is 0)\n"
440 " -w Work conserving flag. cgroup can increase\n"
441 " bandwidth beyond the rate limit specified\n"
442 " while there is available bandwidth. Current\n"
443 " implementation assumes there is only eth0\n"
444 " but can be extended to support multiple NICs\n"
445 " -h print this info\n"
446 " prog BPF program file name. Name defaults to\n"
447 " hbm_out_kern.o\n");
450 int main(int argc, char **argv)
452 char *prog = "hbm_out_kern.o";
455 char *optstring = "iodln:r:st:wh";
456 struct option loptions[] = {
457 {"no_cn", 0, NULL, 1},
462 while ((k = getopt_long(argc, argv, optstring, loptions, NULL)) != -1) {
468 prog = "hbm_edt_kern.o";
477 loopback_flag = true;
480 cg_id = atoi(optarg);
483 minRate = atoi(optarg) * 1.024;
493 work_conserving_flag = true;
496 if (optopt == 'n' || optopt == 'r' || optopt == 't')
498 "Option -%c requires an argument.\n\n",
509 printf("HBM prog: %s\n", prog != NULL ? prog : "NULL");
511 /* Use libbpf 1.0 API mode */
512 libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
514 return run_bpf_prog(prog, cg_id);