hbm.c 13 KB

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