xdp_sample_user.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #define _GNU_SOURCE
  3. #include <arpa/inet.h>
  4. #include <bpf/bpf.h>
  5. #include <bpf/libbpf.h>
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <getopt.h>
  9. #include <linux/ethtool.h>
  10. #include <linux/hashtable.h>
  11. #include <linux/if_link.h>
  12. #include <linux/jhash.h>
  13. #include <linux/limits.h>
  14. #include <linux/list.h>
  15. #include <linux/sockios.h>
  16. #include <locale.h>
  17. #include <math.h>
  18. #include <net/if.h>
  19. #include <poll.h>
  20. #include <signal.h>
  21. #include <stdbool.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/mman.h>
  27. #include <sys/signalfd.h>
  28. #include <sys/sysinfo.h>
  29. #include <sys/timerfd.h>
  30. #include <sys/utsname.h>
  31. #include <time.h>
  32. #include <unistd.h>
  33. #include "bpf_util.h"
  34. #include "xdp_sample_user.h"
  35. #define __sample_print(fmt, cond, ...) \
  36. ({ \
  37. if (cond) \
  38. printf(fmt, ##__VA_ARGS__); \
  39. })
  40. #define print_always(fmt, ...) __sample_print(fmt, 1, ##__VA_ARGS__)
  41. #define print_default(fmt, ...) \
  42. __sample_print(fmt, sample_log_level & LL_DEFAULT, ##__VA_ARGS__)
  43. #define __print_err(err, fmt, ...) \
  44. ({ \
  45. __sample_print(fmt, err > 0 || sample_log_level & LL_DEFAULT, \
  46. ##__VA_ARGS__); \
  47. sample_err_exp = sample_err_exp ? true : err > 0; \
  48. })
  49. #define print_err(err, fmt, ...) __print_err(err, fmt, ##__VA_ARGS__)
  50. #define __COLUMN(x) "%'10" x " %-13s"
  51. #define FMT_COLUMNf __COLUMN(".0f")
  52. #define FMT_COLUMNd __COLUMN("d")
  53. #define FMT_COLUMNl __COLUMN("llu")
  54. #define RX(rx) rx, "rx/s"
  55. #define PPS(pps) pps, "pkt/s"
  56. #define DROP(drop) drop, "drop/s"
  57. #define ERR(err) err, "error/s"
  58. #define HITS(hits) hits, "hit/s"
  59. #define XMIT(xmit) xmit, "xmit/s"
  60. #define PASS(pass) pass, "pass/s"
  61. #define REDIR(redir) redir, "redir/s"
  62. #define NANOSEC_PER_SEC 1000000000 /* 10^9 */
  63. #define XDP_UNKNOWN (XDP_REDIRECT + 1)
  64. #define XDP_ACTION_MAX (XDP_UNKNOWN + 1)
  65. #define XDP_REDIRECT_ERR_MAX 7
  66. enum map_type {
  67. MAP_RX,
  68. MAP_REDIRECT_ERR,
  69. MAP_CPUMAP_ENQUEUE,
  70. MAP_CPUMAP_KTHREAD,
  71. MAP_EXCEPTION,
  72. MAP_DEVMAP_XMIT,
  73. MAP_DEVMAP_XMIT_MULTI,
  74. NUM_MAP,
  75. };
  76. enum log_level {
  77. LL_DEFAULT = 1U << 0,
  78. LL_SIMPLE = 1U << 1,
  79. LL_DEBUG = 1U << 2,
  80. };
  81. struct record {
  82. __u64 timestamp;
  83. struct datarec total;
  84. struct datarec *cpu;
  85. };
  86. struct map_entry {
  87. struct hlist_node node;
  88. __u64 pair;
  89. struct record val;
  90. };
  91. struct stats_record {
  92. struct record rx_cnt;
  93. struct record redir_err[XDP_REDIRECT_ERR_MAX];
  94. struct record kthread;
  95. struct record exception[XDP_ACTION_MAX];
  96. struct record devmap_xmit;
  97. DECLARE_HASHTABLE(xmit_map, 5);
  98. struct record enq[];
  99. };
  100. struct sample_output {
  101. struct {
  102. __u64 rx;
  103. __u64 redir;
  104. __u64 drop;
  105. __u64 drop_xmit;
  106. __u64 err;
  107. __u64 xmit;
  108. } totals;
  109. struct {
  110. union {
  111. __u64 pps;
  112. __u64 num;
  113. };
  114. __u64 drop;
  115. __u64 err;
  116. } rx_cnt;
  117. struct {
  118. __u64 suc;
  119. __u64 err;
  120. } redir_cnt;
  121. struct {
  122. __u64 hits;
  123. } except_cnt;
  124. struct {
  125. __u64 pps;
  126. __u64 drop;
  127. __u64 err;
  128. double bavg;
  129. } xmit_cnt;
  130. };
  131. struct xdp_desc {
  132. int ifindex;
  133. __u32 prog_id;
  134. int flags;
  135. } sample_xdp_progs[32];
  136. struct datarec *sample_mmap[NUM_MAP];
  137. struct bpf_map *sample_map[NUM_MAP];
  138. size_t sample_map_count[NUM_MAP];
  139. enum log_level sample_log_level;
  140. struct sample_output sample_out;
  141. unsigned long sample_interval;
  142. bool sample_err_exp;
  143. int sample_xdp_cnt;
  144. int sample_n_cpus;
  145. int sample_sig_fd;
  146. int sample_mask;
  147. static const char *xdp_redirect_err_names[XDP_REDIRECT_ERR_MAX] = {
  148. /* Key=1 keeps unknown errors */
  149. "Success",
  150. "Unknown",
  151. "EINVAL",
  152. "ENETDOWN",
  153. "EMSGSIZE",
  154. "EOPNOTSUPP",
  155. "ENOSPC",
  156. };
  157. /* Keyed from Unknown */
  158. static const char *xdp_redirect_err_help[XDP_REDIRECT_ERR_MAX - 1] = {
  159. "Unknown error",
  160. "Invalid redirection",
  161. "Device being redirected to is down",
  162. "Packet length too large for device",
  163. "Operation not supported",
  164. "No space in ptr_ring of cpumap kthread",
  165. };
  166. static const char *xdp_action_names[XDP_ACTION_MAX] = {
  167. [XDP_ABORTED] = "XDP_ABORTED",
  168. [XDP_DROP] = "XDP_DROP",
  169. [XDP_PASS] = "XDP_PASS",
  170. [XDP_TX] = "XDP_TX",
  171. [XDP_REDIRECT] = "XDP_REDIRECT",
  172. [XDP_UNKNOWN] = "XDP_UNKNOWN",
  173. };
  174. static __u64 gettime(void)
  175. {
  176. struct timespec t;
  177. int res;
  178. res = clock_gettime(CLOCK_MONOTONIC, &t);
  179. if (res < 0) {
  180. fprintf(stderr, "Error with gettimeofday! (%i)\n", res);
  181. return UINT64_MAX;
  182. }
  183. return (__u64)t.tv_sec * NANOSEC_PER_SEC + t.tv_nsec;
  184. }
  185. static const char *action2str(int action)
  186. {
  187. if (action < XDP_ACTION_MAX)
  188. return xdp_action_names[action];
  189. return NULL;
  190. }
  191. static void sample_print_help(int mask)
  192. {
  193. printf("Output format description\n\n"
  194. "By default, redirect success statistics are disabled, use -s to enable.\n"
  195. "The terse output mode is default, verbose mode can be activated using -v\n"
  196. "Use SIGQUIT (Ctrl + \\) to switch the mode dynamically at runtime\n\n"
  197. "Terse mode displays at most the following fields:\n"
  198. " rx/s Number of packets received per second\n"
  199. " redir/s Number of packets successfully redirected per second\n"
  200. " err,drop/s Aggregated count of errors per second (including dropped packets)\n"
  201. " xmit/s Number of packets transmitted on the output device per second\n\n"
  202. "Output description for verbose mode:\n"
  203. " FIELD DESCRIPTION\n");
  204. if (mask & SAMPLE_RX_CNT) {
  205. printf(" receive\t\tDisplays the number of packets received & errors encountered\n"
  206. " \t\t\tWhenever an error or packet drop occurs, details of per CPU error\n"
  207. " \t\t\tand drop statistics will be expanded inline in terse mode.\n"
  208. " \t\t\t\tpkt/s - Packets received per second\n"
  209. " \t\t\t\tdrop/s - Packets dropped per second\n"
  210. " \t\t\t\terror/s - Errors encountered per second\n\n");
  211. }
  212. if (mask & (SAMPLE_REDIRECT_CNT | SAMPLE_REDIRECT_ERR_CNT)) {
  213. printf(" redirect\t\tDisplays the number of packets successfully redirected\n"
  214. " \t\t\tErrors encountered are expanded under redirect_err field\n"
  215. " \t\t\tNote that passing -s to enable it has a per packet overhead\n"
  216. " \t\t\t\tredir/s - Packets redirected successfully per second\n\n"
  217. " redirect_err\t\tDisplays the number of packets that failed redirection\n"
  218. " \t\t\tThe errno is expanded under this field with per CPU count\n"
  219. " \t\t\tThe recognized errors are:\n");
  220. for (int i = 2; i < XDP_REDIRECT_ERR_MAX; i++)
  221. printf("\t\t\t %s: %s\n", xdp_redirect_err_names[i],
  222. xdp_redirect_err_help[i - 1]);
  223. printf(" \n\t\t\t\terror/s - Packets that failed redirection per second\n\n");
  224. }
  225. if (mask & SAMPLE_CPUMAP_ENQUEUE_CNT) {
  226. printf(" enqueue to cpu N\tDisplays the number of packets enqueued to bulk queue of CPU N\n"
  227. " \t\t\tExpands to cpu:FROM->N to display enqueue stats for each CPU enqueuing to CPU N\n"
  228. " \t\t\tReceived packets can be associated with the CPU redirect program is enqueuing \n"
  229. " \t\t\tpackets to.\n"
  230. " \t\t\t\tpkt/s - Packets enqueued per second from other CPU to CPU N\n"
  231. " \t\t\t\tdrop/s - Packets dropped when trying to enqueue to CPU N\n"
  232. " \t\t\t\tbulk-avg - Average number of packets processed for each event\n\n");
  233. }
  234. if (mask & SAMPLE_CPUMAP_KTHREAD_CNT) {
  235. printf(" kthread\t\tDisplays the number of packets processed in CPUMAP kthread for each CPU\n"
  236. " \t\t\tPackets consumed from ptr_ring in kthread, and its xdp_stats (after calling \n"
  237. " \t\t\tCPUMAP bpf prog) are expanded below this. xdp_stats are expanded as a total and\n"
  238. " \t\t\tthen per-CPU to associate it to each CPU's pinned CPUMAP kthread.\n"
  239. " \t\t\t\tpkt/s - Packets consumed per second from ptr_ring\n"
  240. " \t\t\t\tdrop/s - Packets dropped per second in kthread\n"
  241. " \t\t\t\tsched - Number of times kthread called schedule()\n\n"
  242. " \t\t\txdp_stats (also expands to per-CPU counts)\n"
  243. " \t\t\t\tpass/s - XDP_PASS count for CPUMAP program execution\n"
  244. " \t\t\t\tdrop/s - XDP_DROP count for CPUMAP program execution\n"
  245. " \t\t\t\tredir/s - XDP_REDIRECT count for CPUMAP program execution\n\n");
  246. }
  247. if (mask & SAMPLE_EXCEPTION_CNT) {
  248. printf(" xdp_exception\t\tDisplays xdp_exception tracepoint events\n"
  249. " \t\t\tThis can occur due to internal driver errors, unrecognized\n"
  250. " \t\t\tXDP actions and due to explicit user trigger by use of XDP_ABORTED\n"
  251. " \t\t\tEach action is expanded below this field with its count\n"
  252. " \t\t\t\thit/s - Number of times the tracepoint was hit per second\n\n");
  253. }
  254. if (mask & SAMPLE_DEVMAP_XMIT_CNT) {
  255. printf(" devmap_xmit\t\tDisplays devmap_xmit tracepoint events\n"
  256. " \t\t\tThis tracepoint is invoked for successful transmissions on output\n"
  257. " \t\t\tdevice but these statistics are not available for generic XDP mode,\n"
  258. " \t\t\thence they will be omitted from the output when using SKB mode\n"
  259. " \t\t\t\txmit/s - Number of packets that were transmitted per second\n"
  260. " \t\t\t\tdrop/s - Number of packets that failed transmissions per second\n"
  261. " \t\t\t\tdrv_err/s - Number of internal driver errors per second\n"
  262. " \t\t\t\tbulk-avg - Average number of packets processed for each event\n\n");
  263. }
  264. }
  265. void sample_usage(char *argv[], const struct option *long_options,
  266. const char *doc, int mask, bool error)
  267. {
  268. int i;
  269. if (!error)
  270. sample_print_help(mask);
  271. printf("\n%s\nOption for %s:\n", doc, argv[0]);
  272. for (i = 0; long_options[i].name != 0; i++) {
  273. printf(" --%-15s", long_options[i].name);
  274. if (long_options[i].flag != NULL)
  275. printf(" flag (internal value: %d)",
  276. *long_options[i].flag);
  277. else
  278. printf("\t short-option: -%c", long_options[i].val);
  279. printf("\n");
  280. }
  281. printf("\n");
  282. }
  283. static struct datarec *alloc_record_per_cpu(void)
  284. {
  285. unsigned int nr_cpus = libbpf_num_possible_cpus();
  286. struct datarec *array;
  287. array = calloc(nr_cpus, sizeof(*array));
  288. if (!array) {
  289. fprintf(stderr, "Failed to allocate memory (nr_cpus: %u)\n",
  290. nr_cpus);
  291. return NULL;
  292. }
  293. return array;
  294. }
  295. static int map_entry_init(struct map_entry *e, __u64 pair)
  296. {
  297. e->pair = pair;
  298. INIT_HLIST_NODE(&e->node);
  299. e->val.timestamp = gettime();
  300. e->val.cpu = alloc_record_per_cpu();
  301. if (!e->val.cpu)
  302. return -ENOMEM;
  303. return 0;
  304. }
  305. static void map_collect_percpu(struct datarec *values, struct record *rec)
  306. {
  307. /* For percpu maps, userspace gets a value per possible CPU */
  308. unsigned int nr_cpus = libbpf_num_possible_cpus();
  309. __u64 sum_xdp_redirect = 0;
  310. __u64 sum_processed = 0;
  311. __u64 sum_xdp_pass = 0;
  312. __u64 sum_xdp_drop = 0;
  313. __u64 sum_dropped = 0;
  314. __u64 sum_issue = 0;
  315. int i;
  316. /* Get time as close as possible to reading map contents */
  317. rec->timestamp = gettime();
  318. /* Record and sum values from each CPU */
  319. for (i = 0; i < nr_cpus; i++) {
  320. rec->cpu[i].processed = READ_ONCE(values[i].processed);
  321. rec->cpu[i].dropped = READ_ONCE(values[i].dropped);
  322. rec->cpu[i].issue = READ_ONCE(values[i].issue);
  323. rec->cpu[i].xdp_pass = READ_ONCE(values[i].xdp_pass);
  324. rec->cpu[i].xdp_drop = READ_ONCE(values[i].xdp_drop);
  325. rec->cpu[i].xdp_redirect = READ_ONCE(values[i].xdp_redirect);
  326. sum_processed += rec->cpu[i].processed;
  327. sum_dropped += rec->cpu[i].dropped;
  328. sum_issue += rec->cpu[i].issue;
  329. sum_xdp_pass += rec->cpu[i].xdp_pass;
  330. sum_xdp_drop += rec->cpu[i].xdp_drop;
  331. sum_xdp_redirect += rec->cpu[i].xdp_redirect;
  332. }
  333. rec->total.processed = sum_processed;
  334. rec->total.dropped = sum_dropped;
  335. rec->total.issue = sum_issue;
  336. rec->total.xdp_pass = sum_xdp_pass;
  337. rec->total.xdp_drop = sum_xdp_drop;
  338. rec->total.xdp_redirect = sum_xdp_redirect;
  339. }
  340. static int map_collect_percpu_devmap(int map_fd, struct stats_record *rec)
  341. {
  342. unsigned int nr_cpus = bpf_num_possible_cpus();
  343. __u32 batch, count = 32;
  344. struct datarec *values;
  345. bool init = false;
  346. __u64 *keys;
  347. int i, ret;
  348. keys = calloc(count, sizeof(__u64));
  349. if (!keys)
  350. return -ENOMEM;
  351. values = calloc(count * nr_cpus, sizeof(struct datarec));
  352. if (!values) {
  353. free(keys);
  354. return -ENOMEM;
  355. }
  356. for (;;) {
  357. bool exit = false;
  358. ret = bpf_map_lookup_batch(map_fd, init ? &batch : NULL, &batch,
  359. keys, values, &count, NULL);
  360. if (ret < 0 && errno != ENOENT)
  361. break;
  362. if (errno == ENOENT)
  363. exit = true;
  364. init = true;
  365. for (i = 0; i < count; i++) {
  366. struct map_entry *e, *x = NULL;
  367. __u64 pair = keys[i];
  368. struct datarec *arr;
  369. arr = &values[i * nr_cpus];
  370. hash_for_each_possible(rec->xmit_map, e, node, pair) {
  371. if (e->pair == pair) {
  372. x = e;
  373. break;
  374. }
  375. }
  376. if (!x) {
  377. x = calloc(1, sizeof(*x));
  378. if (!x)
  379. goto cleanup;
  380. if (map_entry_init(x, pair) < 0) {
  381. free(x);
  382. goto cleanup;
  383. }
  384. hash_add(rec->xmit_map, &x->node, pair);
  385. }
  386. map_collect_percpu(arr, &x->val);
  387. }
  388. if (exit)
  389. break;
  390. count = 32;
  391. }
  392. free(values);
  393. free(keys);
  394. return 0;
  395. cleanup:
  396. free(values);
  397. free(keys);
  398. return -ENOMEM;
  399. }
  400. static struct stats_record *alloc_stats_record(void)
  401. {
  402. struct stats_record *rec;
  403. int i;
  404. rec = calloc(1, sizeof(*rec) + sample_n_cpus * sizeof(struct record));
  405. if (!rec) {
  406. fprintf(stderr, "Failed to allocate memory\n");
  407. return NULL;
  408. }
  409. if (sample_mask & SAMPLE_RX_CNT) {
  410. rec->rx_cnt.cpu = alloc_record_per_cpu();
  411. if (!rec->rx_cnt.cpu) {
  412. fprintf(stderr,
  413. "Failed to allocate rx_cnt per-CPU array\n");
  414. goto end_rec;
  415. }
  416. }
  417. if (sample_mask & (SAMPLE_REDIRECT_CNT | SAMPLE_REDIRECT_ERR_CNT)) {
  418. for (i = 0; i < XDP_REDIRECT_ERR_MAX; i++) {
  419. rec->redir_err[i].cpu = alloc_record_per_cpu();
  420. if (!rec->redir_err[i].cpu) {
  421. fprintf(stderr,
  422. "Failed to allocate redir_err per-CPU array for "
  423. "\"%s\" case\n",
  424. xdp_redirect_err_names[i]);
  425. while (i--)
  426. free(rec->redir_err[i].cpu);
  427. goto end_rx_cnt;
  428. }
  429. }
  430. }
  431. if (sample_mask & SAMPLE_CPUMAP_KTHREAD_CNT) {
  432. rec->kthread.cpu = alloc_record_per_cpu();
  433. if (!rec->kthread.cpu) {
  434. fprintf(stderr,
  435. "Failed to allocate kthread per-CPU array\n");
  436. goto end_redir;
  437. }
  438. }
  439. if (sample_mask & SAMPLE_EXCEPTION_CNT) {
  440. for (i = 0; i < XDP_ACTION_MAX; i++) {
  441. rec->exception[i].cpu = alloc_record_per_cpu();
  442. if (!rec->exception[i].cpu) {
  443. fprintf(stderr,
  444. "Failed to allocate exception per-CPU array for "
  445. "\"%s\" case\n",
  446. action2str(i));
  447. while (i--)
  448. free(rec->exception[i].cpu);
  449. goto end_kthread;
  450. }
  451. }
  452. }
  453. if (sample_mask & SAMPLE_DEVMAP_XMIT_CNT) {
  454. rec->devmap_xmit.cpu = alloc_record_per_cpu();
  455. if (!rec->devmap_xmit.cpu) {
  456. fprintf(stderr,
  457. "Failed to allocate devmap_xmit per-CPU array\n");
  458. goto end_exception;
  459. }
  460. }
  461. if (sample_mask & SAMPLE_DEVMAP_XMIT_CNT_MULTI)
  462. hash_init(rec->xmit_map);
  463. if (sample_mask & SAMPLE_CPUMAP_ENQUEUE_CNT) {
  464. for (i = 0; i < sample_n_cpus; i++) {
  465. rec->enq[i].cpu = alloc_record_per_cpu();
  466. if (!rec->enq[i].cpu) {
  467. fprintf(stderr,
  468. "Failed to allocate enqueue per-CPU array for "
  469. "CPU %d\n",
  470. i);
  471. while (i--)
  472. free(rec->enq[i].cpu);
  473. goto end_devmap_xmit;
  474. }
  475. }
  476. }
  477. return rec;
  478. end_devmap_xmit:
  479. free(rec->devmap_xmit.cpu);
  480. end_exception:
  481. for (i = 0; i < XDP_ACTION_MAX; i++)
  482. free(rec->exception[i].cpu);
  483. end_kthread:
  484. free(rec->kthread.cpu);
  485. end_redir:
  486. for (i = 0; i < XDP_REDIRECT_ERR_MAX; i++)
  487. free(rec->redir_err[i].cpu);
  488. end_rx_cnt:
  489. free(rec->rx_cnt.cpu);
  490. end_rec:
  491. free(rec);
  492. return NULL;
  493. }
  494. static void free_stats_record(struct stats_record *r)
  495. {
  496. struct hlist_node *tmp;
  497. struct map_entry *e;
  498. int i;
  499. for (i = 0; i < sample_n_cpus; i++)
  500. free(r->enq[i].cpu);
  501. hash_for_each_safe(r->xmit_map, i, tmp, e, node) {
  502. hash_del(&e->node);
  503. free(e->val.cpu);
  504. free(e);
  505. }
  506. free(r->devmap_xmit.cpu);
  507. for (i = 0; i < XDP_ACTION_MAX; i++)
  508. free(r->exception[i].cpu);
  509. free(r->kthread.cpu);
  510. for (i = 0; i < XDP_REDIRECT_ERR_MAX; i++)
  511. free(r->redir_err[i].cpu);
  512. free(r->rx_cnt.cpu);
  513. free(r);
  514. }
  515. static double calc_period(struct record *r, struct record *p)
  516. {
  517. double period_ = 0;
  518. __u64 period = 0;
  519. period = r->timestamp - p->timestamp;
  520. if (period > 0)
  521. period_ = ((double)period / NANOSEC_PER_SEC);
  522. return period_;
  523. }
  524. static double sample_round(double val)
  525. {
  526. if (val - floor(val) < 0.5)
  527. return floor(val);
  528. return ceil(val);
  529. }
  530. static __u64 calc_pps(struct datarec *r, struct datarec *p, double period_)
  531. {
  532. __u64 packets = 0;
  533. __u64 pps = 0;
  534. if (period_ > 0) {
  535. packets = r->processed - p->processed;
  536. pps = sample_round(packets / period_);
  537. }
  538. return pps;
  539. }
  540. static __u64 calc_drop_pps(struct datarec *r, struct datarec *p, double period_)
  541. {
  542. __u64 packets = 0;
  543. __u64 pps = 0;
  544. if (period_ > 0) {
  545. packets = r->dropped - p->dropped;
  546. pps = sample_round(packets / period_);
  547. }
  548. return pps;
  549. }
  550. static __u64 calc_errs_pps(struct datarec *r, struct datarec *p, double period_)
  551. {
  552. __u64 packets = 0;
  553. __u64 pps = 0;
  554. if (period_ > 0) {
  555. packets = r->issue - p->issue;
  556. pps = sample_round(packets / period_);
  557. }
  558. return pps;
  559. }
  560. static __u64 calc_info_pps(struct datarec *r, struct datarec *p, double period_)
  561. {
  562. __u64 packets = 0;
  563. __u64 pps = 0;
  564. if (period_ > 0) {
  565. packets = r->info - p->info;
  566. pps = sample_round(packets / period_);
  567. }
  568. return pps;
  569. }
  570. static void calc_xdp_pps(struct datarec *r, struct datarec *p, double *xdp_pass,
  571. double *xdp_drop, double *xdp_redirect, double period_)
  572. {
  573. *xdp_pass = 0, *xdp_drop = 0, *xdp_redirect = 0;
  574. if (period_ > 0) {
  575. *xdp_redirect = (r->xdp_redirect - p->xdp_redirect) / period_;
  576. *xdp_pass = (r->xdp_pass - p->xdp_pass) / period_;
  577. *xdp_drop = (r->xdp_drop - p->xdp_drop) / period_;
  578. }
  579. }
  580. static void stats_get_rx_cnt(struct stats_record *stats_rec,
  581. struct stats_record *stats_prev,
  582. unsigned int nr_cpus, struct sample_output *out)
  583. {
  584. struct record *rec, *prev;
  585. double t, pps, drop, err;
  586. int i;
  587. rec = &stats_rec->rx_cnt;
  588. prev = &stats_prev->rx_cnt;
  589. t = calc_period(rec, prev);
  590. for (i = 0; i < nr_cpus; i++) {
  591. struct datarec *r = &rec->cpu[i];
  592. struct datarec *p = &prev->cpu[i];
  593. char str[64];
  594. pps = calc_pps(r, p, t);
  595. drop = calc_drop_pps(r, p, t);
  596. err = calc_errs_pps(r, p, t);
  597. if (!pps && !drop && !err)
  598. continue;
  599. snprintf(str, sizeof(str), "cpu:%d", i);
  600. print_default(" %-18s " FMT_COLUMNf FMT_COLUMNf FMT_COLUMNf
  601. "\n",
  602. str, PPS(pps), DROP(drop), ERR(err));
  603. }
  604. if (out) {
  605. pps = calc_pps(&rec->total, &prev->total, t);
  606. drop = calc_drop_pps(&rec->total, &prev->total, t);
  607. err = calc_errs_pps(&rec->total, &prev->total, t);
  608. out->rx_cnt.pps = pps;
  609. out->rx_cnt.drop = drop;
  610. out->rx_cnt.err = err;
  611. out->totals.rx += pps;
  612. out->totals.drop += drop;
  613. out->totals.err += err;
  614. }
  615. }
  616. static void stats_get_cpumap_enqueue(struct stats_record *stats_rec,
  617. struct stats_record *stats_prev,
  618. unsigned int nr_cpus)
  619. {
  620. struct record *rec, *prev;
  621. double t, pps, drop, err;
  622. int i, to_cpu;
  623. /* cpumap enqueue stats */
  624. for (to_cpu = 0; to_cpu < sample_n_cpus; to_cpu++) {
  625. rec = &stats_rec->enq[to_cpu];
  626. prev = &stats_prev->enq[to_cpu];
  627. t = calc_period(rec, prev);
  628. pps = calc_pps(&rec->total, &prev->total, t);
  629. drop = calc_drop_pps(&rec->total, &prev->total, t);
  630. err = calc_errs_pps(&rec->total, &prev->total, t);
  631. if (pps > 0 || drop > 0) {
  632. char str[64];
  633. snprintf(str, sizeof(str), "enqueue to cpu %d", to_cpu);
  634. if (err > 0)
  635. err = pps / err; /* calc average bulk size */
  636. print_err(drop,
  637. " %-20s " FMT_COLUMNf FMT_COLUMNf __COLUMN(
  638. ".2f") "\n",
  639. str, PPS(pps), DROP(drop), err, "bulk-avg");
  640. }
  641. for (i = 0; i < nr_cpus; i++) {
  642. struct datarec *r = &rec->cpu[i];
  643. struct datarec *p = &prev->cpu[i];
  644. char str[64];
  645. pps = calc_pps(r, p, t);
  646. drop = calc_drop_pps(r, p, t);
  647. err = calc_errs_pps(r, p, t);
  648. if (!pps && !drop && !err)
  649. continue;
  650. snprintf(str, sizeof(str), "cpu:%d->%d", i, to_cpu);
  651. if (err > 0)
  652. err = pps / err; /* calc average bulk size */
  653. print_default(
  654. " %-18s " FMT_COLUMNf FMT_COLUMNf __COLUMN(
  655. ".2f") "\n",
  656. str, PPS(pps), DROP(drop), err, "bulk-avg");
  657. }
  658. }
  659. }
  660. static void stats_get_cpumap_remote(struct stats_record *stats_rec,
  661. struct stats_record *stats_prev,
  662. unsigned int nr_cpus)
  663. {
  664. double xdp_pass, xdp_drop, xdp_redirect;
  665. struct record *rec, *prev;
  666. double t;
  667. int i;
  668. rec = &stats_rec->kthread;
  669. prev = &stats_prev->kthread;
  670. t = calc_period(rec, prev);
  671. calc_xdp_pps(&rec->total, &prev->total, &xdp_pass, &xdp_drop,
  672. &xdp_redirect, t);
  673. if (xdp_pass || xdp_drop || xdp_redirect) {
  674. print_err(xdp_drop,
  675. " %-18s " FMT_COLUMNf FMT_COLUMNf FMT_COLUMNf "\n",
  676. "xdp_stats", PASS(xdp_pass), DROP(xdp_drop),
  677. REDIR(xdp_redirect));
  678. }
  679. for (i = 0; i < nr_cpus; i++) {
  680. struct datarec *r = &rec->cpu[i];
  681. struct datarec *p = &prev->cpu[i];
  682. char str[64];
  683. calc_xdp_pps(r, p, &xdp_pass, &xdp_drop, &xdp_redirect, t);
  684. if (!xdp_pass && !xdp_drop && !xdp_redirect)
  685. continue;
  686. snprintf(str, sizeof(str), "cpu:%d", i);
  687. print_default(" %-16s " FMT_COLUMNf FMT_COLUMNf FMT_COLUMNf
  688. "\n",
  689. str, PASS(xdp_pass), DROP(xdp_drop),
  690. REDIR(xdp_redirect));
  691. }
  692. }
  693. static void stats_get_cpumap_kthread(struct stats_record *stats_rec,
  694. struct stats_record *stats_prev,
  695. unsigned int nr_cpus)
  696. {
  697. struct record *rec, *prev;
  698. double t, pps, drop, err;
  699. int i;
  700. rec = &stats_rec->kthread;
  701. prev = &stats_prev->kthread;
  702. t = calc_period(rec, prev);
  703. pps = calc_pps(&rec->total, &prev->total, t);
  704. drop = calc_drop_pps(&rec->total, &prev->total, t);
  705. err = calc_errs_pps(&rec->total, &prev->total, t);
  706. print_err(drop, " %-20s " FMT_COLUMNf FMT_COLUMNf FMT_COLUMNf "\n",
  707. pps ? "kthread total" : "kthread", PPS(pps), DROP(drop), err,
  708. "sched");
  709. for (i = 0; i < nr_cpus; i++) {
  710. struct datarec *r = &rec->cpu[i];
  711. struct datarec *p = &prev->cpu[i];
  712. char str[64];
  713. pps = calc_pps(r, p, t);
  714. drop = calc_drop_pps(r, p, t);
  715. err = calc_errs_pps(r, p, t);
  716. if (!pps && !drop && !err)
  717. continue;
  718. snprintf(str, sizeof(str), "cpu:%d", i);
  719. print_default(" %-18s " FMT_COLUMNf FMT_COLUMNf FMT_COLUMNf
  720. "\n",
  721. str, PPS(pps), DROP(drop), err, "sched");
  722. }
  723. }
  724. static void stats_get_redirect_cnt(struct stats_record *stats_rec,
  725. struct stats_record *stats_prev,
  726. unsigned int nr_cpus,
  727. struct sample_output *out)
  728. {
  729. struct record *rec, *prev;
  730. double t, pps;
  731. int i;
  732. rec = &stats_rec->redir_err[0];
  733. prev = &stats_prev->redir_err[0];
  734. t = calc_period(rec, prev);
  735. for (i = 0; i < nr_cpus; i++) {
  736. struct datarec *r = &rec->cpu[i];
  737. struct datarec *p = &prev->cpu[i];
  738. char str[64];
  739. pps = calc_pps(r, p, t);
  740. if (!pps)
  741. continue;
  742. snprintf(str, sizeof(str), "cpu:%d", i);
  743. print_default(" %-18s " FMT_COLUMNf "\n", str, REDIR(pps));
  744. }
  745. if (out) {
  746. pps = calc_pps(&rec->total, &prev->total, t);
  747. out->redir_cnt.suc = pps;
  748. out->totals.redir += pps;
  749. }
  750. }
  751. static void stats_get_redirect_err_cnt(struct stats_record *stats_rec,
  752. struct stats_record *stats_prev,
  753. unsigned int nr_cpus,
  754. struct sample_output *out)
  755. {
  756. struct record *rec, *prev;
  757. double t, drop, sum = 0;
  758. int rec_i, i;
  759. for (rec_i = 1; rec_i < XDP_REDIRECT_ERR_MAX; rec_i++) {
  760. char str[64];
  761. rec = &stats_rec->redir_err[rec_i];
  762. prev = &stats_prev->redir_err[rec_i];
  763. t = calc_period(rec, prev);
  764. drop = calc_drop_pps(&rec->total, &prev->total, t);
  765. if (drop > 0 && !out) {
  766. snprintf(str, sizeof(str),
  767. sample_log_level & LL_DEFAULT ? "%s total" :
  768. "%s",
  769. xdp_redirect_err_names[rec_i]);
  770. print_err(drop, " %-18s " FMT_COLUMNf "\n", str,
  771. ERR(drop));
  772. }
  773. for (i = 0; i < nr_cpus; i++) {
  774. struct datarec *r = &rec->cpu[i];
  775. struct datarec *p = &prev->cpu[i];
  776. double drop;
  777. drop = calc_drop_pps(r, p, t);
  778. if (!drop)
  779. continue;
  780. snprintf(str, sizeof(str), "cpu:%d", i);
  781. print_default(" %-16s" FMT_COLUMNf "\n", str,
  782. ERR(drop));
  783. }
  784. sum += drop;
  785. }
  786. if (out) {
  787. out->redir_cnt.err = sum;
  788. out->totals.err += sum;
  789. }
  790. }
  791. static void stats_get_exception_cnt(struct stats_record *stats_rec,
  792. struct stats_record *stats_prev,
  793. unsigned int nr_cpus,
  794. struct sample_output *out)
  795. {
  796. double t, drop, sum = 0;
  797. struct record *rec, *prev;
  798. int rec_i, i;
  799. for (rec_i = 0; rec_i < XDP_ACTION_MAX; rec_i++) {
  800. rec = &stats_rec->exception[rec_i];
  801. prev = &stats_prev->exception[rec_i];
  802. t = calc_period(rec, prev);
  803. drop = calc_drop_pps(&rec->total, &prev->total, t);
  804. /* Fold out errors after heading */
  805. sum += drop;
  806. if (drop > 0 && !out) {
  807. print_always(" %-18s " FMT_COLUMNf "\n",
  808. action2str(rec_i), ERR(drop));
  809. for (i = 0; i < nr_cpus; i++) {
  810. struct datarec *r = &rec->cpu[i];
  811. struct datarec *p = &prev->cpu[i];
  812. char str[64];
  813. double drop;
  814. drop = calc_drop_pps(r, p, t);
  815. if (!drop)
  816. continue;
  817. snprintf(str, sizeof(str), "cpu:%d", i);
  818. print_default(" %-16s" FMT_COLUMNf "\n",
  819. str, ERR(drop));
  820. }
  821. }
  822. }
  823. if (out) {
  824. out->except_cnt.hits = sum;
  825. out->totals.err += sum;
  826. }
  827. }
  828. static void stats_get_devmap_xmit(struct stats_record *stats_rec,
  829. struct stats_record *stats_prev,
  830. unsigned int nr_cpus,
  831. struct sample_output *out)
  832. {
  833. double pps, drop, info, err;
  834. struct record *rec, *prev;
  835. double t;
  836. int i;
  837. rec = &stats_rec->devmap_xmit;
  838. prev = &stats_prev->devmap_xmit;
  839. t = calc_period(rec, prev);
  840. for (i = 0; i < nr_cpus; i++) {
  841. struct datarec *r = &rec->cpu[i];
  842. struct datarec *p = &prev->cpu[i];
  843. char str[64];
  844. pps = calc_pps(r, p, t);
  845. drop = calc_drop_pps(r, p, t);
  846. err = calc_errs_pps(r, p, t);
  847. if (!pps && !drop && !err)
  848. continue;
  849. snprintf(str, sizeof(str), "cpu:%d", i);
  850. info = calc_info_pps(r, p, t);
  851. if (info > 0)
  852. info = (pps + drop) / info; /* calc avg bulk */
  853. print_default(" %-18s" FMT_COLUMNf FMT_COLUMNf FMT_COLUMNf
  854. __COLUMN(".2f") "\n",
  855. str, XMIT(pps), DROP(drop), err, "drv_err/s",
  856. info, "bulk-avg");
  857. }
  858. if (out) {
  859. pps = calc_pps(&rec->total, &prev->total, t);
  860. drop = calc_drop_pps(&rec->total, &prev->total, t);
  861. info = calc_info_pps(&rec->total, &prev->total, t);
  862. if (info > 0)
  863. info = (pps + drop) / info; /* calc avg bulk */
  864. err = calc_errs_pps(&rec->total, &prev->total, t);
  865. out->xmit_cnt.pps = pps;
  866. out->xmit_cnt.drop = drop;
  867. out->xmit_cnt.bavg = info;
  868. out->xmit_cnt.err = err;
  869. out->totals.xmit += pps;
  870. out->totals.drop_xmit += drop;
  871. out->totals.err += err;
  872. }
  873. }
  874. static void stats_get_devmap_xmit_multi(struct stats_record *stats_rec,
  875. struct stats_record *stats_prev,
  876. unsigned int nr_cpus,
  877. struct sample_output *out,
  878. bool xmit_total)
  879. {
  880. double pps, drop, info, err;
  881. struct map_entry *entry;
  882. struct record *r, *p;
  883. double t;
  884. int bkt;
  885. hash_for_each(stats_rec->xmit_map, bkt, entry, node) {
  886. struct map_entry *e, *x = NULL;
  887. char ifname_from[IFNAMSIZ];
  888. char ifname_to[IFNAMSIZ];
  889. const char *fstr, *tstr;
  890. unsigned long prev_time;
  891. struct record beg = {};
  892. __u32 from_idx, to_idx;
  893. char str[128];
  894. __u64 pair;
  895. int i;
  896. prev_time = sample_interval * NANOSEC_PER_SEC;
  897. pair = entry->pair;
  898. from_idx = pair >> 32;
  899. to_idx = pair & 0xFFFFFFFF;
  900. r = &entry->val;
  901. beg.timestamp = r->timestamp - prev_time;
  902. /* Find matching entry from stats_prev map */
  903. hash_for_each_possible(stats_prev->xmit_map, e, node, pair) {
  904. if (e->pair == pair) {
  905. x = e;
  906. break;
  907. }
  908. }
  909. if (x)
  910. p = &x->val;
  911. else
  912. p = &beg;
  913. t = calc_period(r, p);
  914. pps = calc_pps(&r->total, &p->total, t);
  915. drop = calc_drop_pps(&r->total, &p->total, t);
  916. info = calc_info_pps(&r->total, &p->total, t);
  917. if (info > 0)
  918. info = (pps + drop) / info; /* calc avg bulk */
  919. err = calc_errs_pps(&r->total, &p->total, t);
  920. if (out) {
  921. /* We are responsible for filling out totals */
  922. out->totals.xmit += pps;
  923. out->totals.drop_xmit += drop;
  924. out->totals.err += err;
  925. continue;
  926. }
  927. fstr = tstr = NULL;
  928. if (if_indextoname(from_idx, ifname_from))
  929. fstr = ifname_from;
  930. if (if_indextoname(to_idx, ifname_to))
  931. tstr = ifname_to;
  932. snprintf(str, sizeof(str), "xmit %s->%s", fstr ?: "?",
  933. tstr ?: "?");
  934. /* Skip idle streams of redirection */
  935. if (pps || drop || err) {
  936. print_err(drop,
  937. " %-20s " FMT_COLUMNf FMT_COLUMNf FMT_COLUMNf
  938. __COLUMN(".2f") "\n", str, XMIT(pps), DROP(drop),
  939. err, "drv_err/s", info, "bulk-avg");
  940. }
  941. for (i = 0; i < nr_cpus; i++) {
  942. struct datarec *rc = &r->cpu[i];
  943. struct datarec *pc, p_beg = {};
  944. char str[64];
  945. pc = p == &beg ? &p_beg : &p->cpu[i];
  946. pps = calc_pps(rc, pc, t);
  947. drop = calc_drop_pps(rc, pc, t);
  948. err = calc_errs_pps(rc, pc, t);
  949. if (!pps && !drop && !err)
  950. continue;
  951. snprintf(str, sizeof(str), "cpu:%d", i);
  952. info = calc_info_pps(rc, pc, t);
  953. if (info > 0)
  954. info = (pps + drop) / info; /* calc avg bulk */
  955. print_default(" %-18s" FMT_COLUMNf FMT_COLUMNf FMT_COLUMNf
  956. __COLUMN(".2f") "\n", str, XMIT(pps),
  957. DROP(drop), err, "drv_err/s", info, "bulk-avg");
  958. }
  959. }
  960. }
  961. static void stats_print(const char *prefix, int mask, struct stats_record *r,
  962. struct stats_record *p, struct sample_output *out)
  963. {
  964. int nr_cpus = libbpf_num_possible_cpus();
  965. const char *str;
  966. print_always("%-23s", prefix ?: "Summary");
  967. if (mask & SAMPLE_RX_CNT)
  968. print_always(FMT_COLUMNl, RX(out->totals.rx));
  969. if (mask & SAMPLE_REDIRECT_CNT)
  970. print_always(FMT_COLUMNl, REDIR(out->totals.redir));
  971. printf(FMT_COLUMNl,
  972. out->totals.err + out->totals.drop + out->totals.drop_xmit,
  973. "err,drop/s");
  974. if (mask & SAMPLE_DEVMAP_XMIT_CNT ||
  975. mask & SAMPLE_DEVMAP_XMIT_CNT_MULTI)
  976. printf(FMT_COLUMNl, XMIT(out->totals.xmit));
  977. printf("\n");
  978. if (mask & SAMPLE_RX_CNT) {
  979. str = (sample_log_level & LL_DEFAULT) && out->rx_cnt.pps ?
  980. "receive total" :
  981. "receive";
  982. print_err((out->rx_cnt.err || out->rx_cnt.drop),
  983. " %-20s " FMT_COLUMNl FMT_COLUMNl FMT_COLUMNl "\n",
  984. str, PPS(out->rx_cnt.pps), DROP(out->rx_cnt.drop),
  985. ERR(out->rx_cnt.err));
  986. stats_get_rx_cnt(r, p, nr_cpus, NULL);
  987. }
  988. if (mask & SAMPLE_CPUMAP_ENQUEUE_CNT)
  989. stats_get_cpumap_enqueue(r, p, nr_cpus);
  990. if (mask & SAMPLE_CPUMAP_KTHREAD_CNT) {
  991. stats_get_cpumap_kthread(r, p, nr_cpus);
  992. stats_get_cpumap_remote(r, p, nr_cpus);
  993. }
  994. if (mask & SAMPLE_REDIRECT_CNT) {
  995. str = out->redir_cnt.suc ? "redirect total" : "redirect";
  996. print_default(" %-20s " FMT_COLUMNl "\n", str,
  997. REDIR(out->redir_cnt.suc));
  998. stats_get_redirect_cnt(r, p, nr_cpus, NULL);
  999. }
  1000. if (mask & SAMPLE_REDIRECT_ERR_CNT) {
  1001. str = (sample_log_level & LL_DEFAULT) && out->redir_cnt.err ?
  1002. "redirect_err total" :
  1003. "redirect_err";
  1004. print_err(out->redir_cnt.err, " %-20s " FMT_COLUMNl "\n", str,
  1005. ERR(out->redir_cnt.err));
  1006. stats_get_redirect_err_cnt(r, p, nr_cpus, NULL);
  1007. }
  1008. if (mask & SAMPLE_EXCEPTION_CNT) {
  1009. str = out->except_cnt.hits ? "xdp_exception total" :
  1010. "xdp_exception";
  1011. print_err(out->except_cnt.hits, " %-20s " FMT_COLUMNl "\n", str,
  1012. HITS(out->except_cnt.hits));
  1013. stats_get_exception_cnt(r, p, nr_cpus, NULL);
  1014. }
  1015. if (mask & SAMPLE_DEVMAP_XMIT_CNT) {
  1016. str = (sample_log_level & LL_DEFAULT) && out->xmit_cnt.pps ?
  1017. "devmap_xmit total" :
  1018. "devmap_xmit";
  1019. print_err(out->xmit_cnt.err || out->xmit_cnt.drop,
  1020. " %-20s " FMT_COLUMNl FMT_COLUMNl FMT_COLUMNl
  1021. __COLUMN(".2f") "\n",
  1022. str, XMIT(out->xmit_cnt.pps),
  1023. DROP(out->xmit_cnt.drop), out->xmit_cnt.err,
  1024. "drv_err/s", out->xmit_cnt.bavg, "bulk-avg");
  1025. stats_get_devmap_xmit(r, p, nr_cpus, NULL);
  1026. }
  1027. if (mask & SAMPLE_DEVMAP_XMIT_CNT_MULTI)
  1028. stats_get_devmap_xmit_multi(r, p, nr_cpus, NULL,
  1029. mask & SAMPLE_DEVMAP_XMIT_CNT);
  1030. if (sample_log_level & LL_DEFAULT ||
  1031. ((sample_log_level & LL_SIMPLE) && sample_err_exp)) {
  1032. sample_err_exp = false;
  1033. printf("\n");
  1034. }
  1035. }
  1036. int sample_setup_maps(struct bpf_map **maps)
  1037. {
  1038. sample_n_cpus = libbpf_num_possible_cpus();
  1039. for (int i = 0; i < MAP_DEVMAP_XMIT_MULTI; i++) {
  1040. sample_map[i] = maps[i];
  1041. switch (i) {
  1042. case MAP_RX:
  1043. case MAP_CPUMAP_KTHREAD:
  1044. case MAP_DEVMAP_XMIT:
  1045. sample_map_count[i] = sample_n_cpus;
  1046. break;
  1047. case MAP_REDIRECT_ERR:
  1048. sample_map_count[i] =
  1049. XDP_REDIRECT_ERR_MAX * sample_n_cpus;
  1050. break;
  1051. case MAP_EXCEPTION:
  1052. sample_map_count[i] = XDP_ACTION_MAX * sample_n_cpus;
  1053. case MAP_CPUMAP_ENQUEUE:
  1054. sample_map_count[i] = sample_n_cpus * sample_n_cpus;
  1055. break;
  1056. default:
  1057. return -EINVAL;
  1058. }
  1059. if (bpf_map__set_max_entries(sample_map[i], sample_map_count[i]) < 0)
  1060. return -errno;
  1061. }
  1062. sample_map[MAP_DEVMAP_XMIT_MULTI] = maps[MAP_DEVMAP_XMIT_MULTI];
  1063. return 0;
  1064. }
  1065. static int sample_setup_maps_mappings(void)
  1066. {
  1067. for (int i = 0; i < MAP_DEVMAP_XMIT_MULTI; i++) {
  1068. size_t size = sample_map_count[i] * sizeof(struct datarec);
  1069. sample_mmap[i] = mmap(NULL, size, PROT_READ | PROT_WRITE,
  1070. MAP_SHARED, bpf_map__fd(sample_map[i]), 0);
  1071. if (sample_mmap[i] == MAP_FAILED)
  1072. return -errno;
  1073. }
  1074. return 0;
  1075. }
  1076. int __sample_init(int mask)
  1077. {
  1078. sigset_t st;
  1079. sigemptyset(&st);
  1080. sigaddset(&st, SIGQUIT);
  1081. sigaddset(&st, SIGINT);
  1082. sigaddset(&st, SIGTERM);
  1083. if (sigprocmask(SIG_BLOCK, &st, NULL) < 0)
  1084. return -errno;
  1085. sample_sig_fd = signalfd(-1, &st, SFD_CLOEXEC | SFD_NONBLOCK);
  1086. if (sample_sig_fd < 0)
  1087. return -errno;
  1088. sample_mask = mask;
  1089. return sample_setup_maps_mappings();
  1090. }
  1091. static int __sample_remove_xdp(int ifindex, __u32 prog_id, int xdp_flags)
  1092. {
  1093. __u32 cur_prog_id = 0;
  1094. int ret;
  1095. if (prog_id) {
  1096. ret = bpf_xdp_query_id(ifindex, xdp_flags, &cur_prog_id);
  1097. if (ret < 0)
  1098. return -errno;
  1099. if (prog_id != cur_prog_id) {
  1100. print_always(
  1101. "Program on ifindex %d does not match installed "
  1102. "program, skipping unload\n",
  1103. ifindex);
  1104. return -ENOENT;
  1105. }
  1106. }
  1107. return bpf_xdp_detach(ifindex, xdp_flags, NULL);
  1108. }
  1109. int sample_install_xdp(struct bpf_program *xdp_prog, int ifindex, bool generic,
  1110. bool force)
  1111. {
  1112. int ret, xdp_flags = 0;
  1113. __u32 prog_id = 0;
  1114. if (sample_xdp_cnt == 32) {
  1115. fprintf(stderr,
  1116. "Total limit for installed XDP programs in a sample reached\n");
  1117. return -ENOTSUP;
  1118. }
  1119. xdp_flags |= !force ? XDP_FLAGS_UPDATE_IF_NOEXIST : 0;
  1120. xdp_flags |= generic ? XDP_FLAGS_SKB_MODE : XDP_FLAGS_DRV_MODE;
  1121. ret = bpf_xdp_attach(ifindex, bpf_program__fd(xdp_prog), xdp_flags, NULL);
  1122. if (ret < 0) {
  1123. ret = -errno;
  1124. fprintf(stderr,
  1125. "Failed to install program \"%s\" on ifindex %d, mode = %s, "
  1126. "force = %s: %s\n",
  1127. bpf_program__name(xdp_prog), ifindex,
  1128. generic ? "skb" : "native", force ? "true" : "false",
  1129. strerror(-ret));
  1130. return ret;
  1131. }
  1132. ret = bpf_xdp_query_id(ifindex, xdp_flags, &prog_id);
  1133. if (ret < 0) {
  1134. ret = -errno;
  1135. fprintf(stderr,
  1136. "Failed to get XDP program id for ifindex %d, removing program: %s\n",
  1137. ifindex, strerror(errno));
  1138. __sample_remove_xdp(ifindex, 0, xdp_flags);
  1139. return ret;
  1140. }
  1141. sample_xdp_progs[sample_xdp_cnt++] =
  1142. (struct xdp_desc){ ifindex, prog_id, xdp_flags };
  1143. return 0;
  1144. }
  1145. static void sample_summary_print(void)
  1146. {
  1147. double num = sample_out.rx_cnt.num;
  1148. if (sample_out.totals.rx) {
  1149. double pkts = sample_out.totals.rx;
  1150. print_always(" Packets received : %'-10llu\n",
  1151. sample_out.totals.rx);
  1152. print_always(" Average packets/s : %'-10.0f\n",
  1153. sample_round(pkts / num));
  1154. }
  1155. if (sample_out.totals.redir) {
  1156. double pkts = sample_out.totals.redir;
  1157. print_always(" Packets redirected : %'-10llu\n",
  1158. sample_out.totals.redir);
  1159. print_always(" Average redir/s : %'-10.0f\n",
  1160. sample_round(pkts / num));
  1161. }
  1162. if (sample_out.totals.drop)
  1163. print_always(" Rx dropped : %'-10llu\n",
  1164. sample_out.totals.drop);
  1165. if (sample_out.totals.drop_xmit)
  1166. print_always(" Tx dropped : %'-10llu\n",
  1167. sample_out.totals.drop_xmit);
  1168. if (sample_out.totals.err)
  1169. print_always(" Errors recorded : %'-10llu\n",
  1170. sample_out.totals.err);
  1171. if (sample_out.totals.xmit) {
  1172. double pkts = sample_out.totals.xmit;
  1173. print_always(" Packets transmitted : %'-10llu\n",
  1174. sample_out.totals.xmit);
  1175. print_always(" Average transmit/s : %'-10.0f\n",
  1176. sample_round(pkts / num));
  1177. }
  1178. }
  1179. void sample_exit(int status)
  1180. {
  1181. size_t size;
  1182. for (int i = 0; i < NUM_MAP; i++) {
  1183. size = sample_map_count[i] * sizeof(**sample_mmap);
  1184. munmap(sample_mmap[i], size);
  1185. }
  1186. while (sample_xdp_cnt--) {
  1187. int i = sample_xdp_cnt, ifindex, xdp_flags;
  1188. __u32 prog_id;
  1189. prog_id = sample_xdp_progs[i].prog_id;
  1190. ifindex = sample_xdp_progs[i].ifindex;
  1191. xdp_flags = sample_xdp_progs[i].flags;
  1192. __sample_remove_xdp(ifindex, prog_id, xdp_flags);
  1193. }
  1194. sample_summary_print();
  1195. close(sample_sig_fd);
  1196. exit(status);
  1197. }
  1198. static int sample_stats_collect(struct stats_record *rec)
  1199. {
  1200. int i;
  1201. if (sample_mask & SAMPLE_RX_CNT)
  1202. map_collect_percpu(sample_mmap[MAP_RX], &rec->rx_cnt);
  1203. if (sample_mask & SAMPLE_REDIRECT_CNT)
  1204. map_collect_percpu(sample_mmap[MAP_REDIRECT_ERR], &rec->redir_err[0]);
  1205. if (sample_mask & SAMPLE_REDIRECT_ERR_CNT) {
  1206. for (i = 1; i < XDP_REDIRECT_ERR_MAX; i++)
  1207. map_collect_percpu(&sample_mmap[MAP_REDIRECT_ERR][i * sample_n_cpus],
  1208. &rec->redir_err[i]);
  1209. }
  1210. if (sample_mask & SAMPLE_CPUMAP_ENQUEUE_CNT)
  1211. for (i = 0; i < sample_n_cpus; i++)
  1212. map_collect_percpu(&sample_mmap[MAP_CPUMAP_ENQUEUE][i * sample_n_cpus],
  1213. &rec->enq[i]);
  1214. if (sample_mask & SAMPLE_CPUMAP_KTHREAD_CNT)
  1215. map_collect_percpu(sample_mmap[MAP_CPUMAP_KTHREAD],
  1216. &rec->kthread);
  1217. if (sample_mask & SAMPLE_EXCEPTION_CNT)
  1218. for (i = 0; i < XDP_ACTION_MAX; i++)
  1219. map_collect_percpu(&sample_mmap[MAP_EXCEPTION][i * sample_n_cpus],
  1220. &rec->exception[i]);
  1221. if (sample_mask & SAMPLE_DEVMAP_XMIT_CNT)
  1222. map_collect_percpu(sample_mmap[MAP_DEVMAP_XMIT], &rec->devmap_xmit);
  1223. if (sample_mask & SAMPLE_DEVMAP_XMIT_CNT_MULTI) {
  1224. if (map_collect_percpu_devmap(bpf_map__fd(sample_map[MAP_DEVMAP_XMIT_MULTI]), rec) < 0)
  1225. return -EINVAL;
  1226. }
  1227. return 0;
  1228. }
  1229. static void sample_summary_update(struct sample_output *out)
  1230. {
  1231. sample_out.totals.rx += out->totals.rx;
  1232. sample_out.totals.redir += out->totals.redir;
  1233. sample_out.totals.drop += out->totals.drop;
  1234. sample_out.totals.drop_xmit += out->totals.drop_xmit;
  1235. sample_out.totals.err += out->totals.err;
  1236. sample_out.totals.xmit += out->totals.xmit;
  1237. sample_out.rx_cnt.num++;
  1238. }
  1239. static void sample_stats_print(int mask, struct stats_record *cur,
  1240. struct stats_record *prev, char *prog_name)
  1241. {
  1242. struct sample_output out = {};
  1243. if (mask & SAMPLE_RX_CNT)
  1244. stats_get_rx_cnt(cur, prev, 0, &out);
  1245. if (mask & SAMPLE_REDIRECT_CNT)
  1246. stats_get_redirect_cnt(cur, prev, 0, &out);
  1247. if (mask & SAMPLE_REDIRECT_ERR_CNT)
  1248. stats_get_redirect_err_cnt(cur, prev, 0, &out);
  1249. if (mask & SAMPLE_EXCEPTION_CNT)
  1250. stats_get_exception_cnt(cur, prev, 0, &out);
  1251. if (mask & SAMPLE_DEVMAP_XMIT_CNT)
  1252. stats_get_devmap_xmit(cur, prev, 0, &out);
  1253. else if (mask & SAMPLE_DEVMAP_XMIT_CNT_MULTI)
  1254. stats_get_devmap_xmit_multi(cur, prev, 0, &out,
  1255. mask & SAMPLE_DEVMAP_XMIT_CNT);
  1256. sample_summary_update(&out);
  1257. stats_print(prog_name, mask, cur, prev, &out);
  1258. }
  1259. void sample_switch_mode(void)
  1260. {
  1261. sample_log_level ^= LL_DEBUG - 1;
  1262. }
  1263. static int sample_signal_cb(void)
  1264. {
  1265. struct signalfd_siginfo si;
  1266. int r;
  1267. r = read(sample_sig_fd, &si, sizeof(si));
  1268. if (r < 0)
  1269. return -errno;
  1270. switch (si.ssi_signo) {
  1271. case SIGQUIT:
  1272. sample_switch_mode();
  1273. printf("\n");
  1274. break;
  1275. default:
  1276. printf("\n");
  1277. return 1;
  1278. }
  1279. return 0;
  1280. }
  1281. /* Pointer swap trick */
  1282. static void swap(struct stats_record **a, struct stats_record **b)
  1283. {
  1284. struct stats_record *tmp;
  1285. tmp = *a;
  1286. *a = *b;
  1287. *b = tmp;
  1288. }
  1289. static int sample_timer_cb(int timerfd, struct stats_record **rec,
  1290. struct stats_record **prev)
  1291. {
  1292. char line[64] = "Summary";
  1293. int ret;
  1294. __u64 t;
  1295. ret = read(timerfd, &t, sizeof(t));
  1296. if (ret < 0)
  1297. return -errno;
  1298. swap(prev, rec);
  1299. ret = sample_stats_collect(*rec);
  1300. if (ret < 0)
  1301. return ret;
  1302. if (sample_xdp_cnt == 2 && !(sample_mask & SAMPLE_SKIP_HEADING)) {
  1303. char fi[IFNAMSIZ];
  1304. char to[IFNAMSIZ];
  1305. const char *f, *t;
  1306. f = t = NULL;
  1307. if (if_indextoname(sample_xdp_progs[0].ifindex, fi))
  1308. f = fi;
  1309. if (if_indextoname(sample_xdp_progs[1].ifindex, to))
  1310. t = to;
  1311. snprintf(line, sizeof(line), "%s->%s", f ?: "?", t ?: "?");
  1312. }
  1313. sample_stats_print(sample_mask, *rec, *prev, line);
  1314. return 0;
  1315. }
  1316. int sample_run(int interval, void (*post_cb)(void *), void *ctx)
  1317. {
  1318. struct timespec ts = { interval, 0 };
  1319. struct itimerspec its = { ts, ts };
  1320. struct stats_record *rec, *prev;
  1321. struct pollfd pfd[2] = {};
  1322. int timerfd, ret;
  1323. if (!interval) {
  1324. fprintf(stderr, "Incorrect interval 0\n");
  1325. return -EINVAL;
  1326. }
  1327. sample_interval = interval;
  1328. /* Pretty print numbers */
  1329. setlocale(LC_NUMERIC, "en_US.UTF-8");
  1330. timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);
  1331. if (timerfd < 0)
  1332. return -errno;
  1333. timerfd_settime(timerfd, 0, &its, NULL);
  1334. pfd[0].fd = sample_sig_fd;
  1335. pfd[0].events = POLLIN;
  1336. pfd[1].fd = timerfd;
  1337. pfd[1].events = POLLIN;
  1338. ret = -ENOMEM;
  1339. rec = alloc_stats_record();
  1340. if (!rec)
  1341. goto end;
  1342. prev = alloc_stats_record();
  1343. if (!prev)
  1344. goto end_rec;
  1345. ret = sample_stats_collect(rec);
  1346. if (ret < 0)
  1347. goto end_rec_prev;
  1348. for (;;) {
  1349. ret = poll(pfd, 2, -1);
  1350. if (ret < 0) {
  1351. if (errno == EINTR)
  1352. continue;
  1353. else
  1354. break;
  1355. }
  1356. if (pfd[0].revents & POLLIN)
  1357. ret = sample_signal_cb();
  1358. else if (pfd[1].revents & POLLIN)
  1359. ret = sample_timer_cb(timerfd, &rec, &prev);
  1360. if (ret)
  1361. break;
  1362. if (post_cb)
  1363. post_cb(ctx);
  1364. }
  1365. end_rec_prev:
  1366. free_stats_record(prev);
  1367. end_rec:
  1368. free_stats_record(rec);
  1369. end:
  1370. close(timerfd);
  1371. return ret;
  1372. }
  1373. const char *get_driver_name(int ifindex)
  1374. {
  1375. struct ethtool_drvinfo drv = {};
  1376. char ifname[IF_NAMESIZE];
  1377. static char drvname[32];
  1378. struct ifreq ifr = {};
  1379. int fd, r = 0;
  1380. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1381. if (fd < 0)
  1382. return "[error]";
  1383. if (!if_indextoname(ifindex, ifname))
  1384. goto end;
  1385. drv.cmd = ETHTOOL_GDRVINFO;
  1386. safe_strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
  1387. ifr.ifr_data = (void *)&drv;
  1388. r = ioctl(fd, SIOCETHTOOL, &ifr);
  1389. if (r)
  1390. goto end;
  1391. safe_strncpy(drvname, drv.driver, sizeof(drvname));
  1392. close(fd);
  1393. return drvname;
  1394. end:
  1395. r = errno;
  1396. close(fd);
  1397. return r == EOPNOTSUPP ? "loopback" : "[error]";
  1398. }
  1399. int get_mac_addr(int ifindex, void *mac_addr)
  1400. {
  1401. char ifname[IF_NAMESIZE];
  1402. struct ifreq ifr = {};
  1403. int fd, r;
  1404. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1405. if (fd < 0)
  1406. return -errno;
  1407. if (!if_indextoname(ifindex, ifname)) {
  1408. r = -errno;
  1409. goto end;
  1410. }
  1411. safe_strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
  1412. r = ioctl(fd, SIOCGIFHWADDR, &ifr);
  1413. if (r) {
  1414. r = -errno;
  1415. goto end;
  1416. }
  1417. memcpy(mac_addr, ifr.ifr_hwaddr.sa_data, 6 * sizeof(char));
  1418. end:
  1419. close(fd);
  1420. return r;
  1421. }
  1422. __attribute__((constructor)) static void sample_ctor(void)
  1423. {
  1424. if (libbpf_set_strict_mode(LIBBPF_STRICT_ALL) < 0) {
  1425. fprintf(stderr, "Failed to set libbpf strict mode: %s\n",
  1426. strerror(errno));
  1427. /* Just exit, nothing to cleanup right now */
  1428. exit(EXIT_FAIL_BPF);
  1429. }
  1430. }