Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0
0002  * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
0003  */
0004 static const char *__doc__ = " XDP RX-queue info extract example\n\n"
0005     "Monitor how many packets per sec (pps) are received\n"
0006     "per NIC RX queue index and which CPU processed the packet\n"
0007     ;
0008 
0009 #include <errno.h>
0010 #include <signal.h>
0011 #include <stdio.h>
0012 #include <stdlib.h>
0013 #include <stdbool.h>
0014 #include <string.h>
0015 #include <unistd.h>
0016 #include <locale.h>
0017 #include <getopt.h>
0018 #include <net/if.h>
0019 #include <time.h>
0020 #include <limits.h>
0021 #include <arpa/inet.h>
0022 #include <linux/if_link.h>
0023 
0024 #include <bpf/bpf.h>
0025 #include <bpf/libbpf.h>
0026 #include "bpf_util.h"
0027 
0028 static int ifindex = -1;
0029 static char ifname_buf[IF_NAMESIZE];
0030 static char *ifname;
0031 static __u32 prog_id;
0032 
0033 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
0034 
0035 static struct bpf_map *stats_global_map;
0036 static struct bpf_map *rx_queue_index_map;
0037 
0038 /* Exit return codes */
0039 #define EXIT_OK     0
0040 #define EXIT_FAIL       1
0041 #define EXIT_FAIL_OPTION    2
0042 #define EXIT_FAIL_XDP       3
0043 #define EXIT_FAIL_BPF       4
0044 #define EXIT_FAIL_MEM       5
0045 
0046 #define FAIL_MEM_SIG        INT_MAX
0047 #define FAIL_STAT_SIG       (INT_MAX - 1)
0048 
0049 static const struct option long_options[] = {
0050     {"help",    no_argument,        NULL, 'h' },
0051     {"dev",     required_argument,  NULL, 'd' },
0052     {"skb-mode",    no_argument,        NULL, 'S' },
0053     {"sec",     required_argument,  NULL, 's' },
0054     {"no-separators", no_argument,      NULL, 'z' },
0055     {"action",  required_argument,  NULL, 'a' },
0056     {"readmem", no_argument,        NULL, 'r' },
0057     {"swapmac", no_argument,        NULL, 'm' },
0058     {"force",   no_argument,        NULL, 'F' },
0059     {0, 0, NULL,  0 }
0060 };
0061 
0062 static void int_exit(int sig)
0063 {
0064     __u32 curr_prog_id = 0;
0065 
0066     if (ifindex > -1) {
0067         if (bpf_xdp_query_id(ifindex, xdp_flags, &curr_prog_id)) {
0068             printf("bpf_xdp_query_id failed\n");
0069             exit(EXIT_FAIL);
0070         }
0071         if (prog_id == curr_prog_id) {
0072             fprintf(stderr,
0073                 "Interrupted: Removing XDP program on ifindex:%d device:%s\n",
0074                 ifindex, ifname);
0075             bpf_xdp_detach(ifindex, xdp_flags, NULL);
0076         } else if (!curr_prog_id) {
0077             printf("couldn't find a prog id on a given iface\n");
0078         } else {
0079             printf("program on interface changed, not removing\n");
0080         }
0081     }
0082 
0083     if (sig == FAIL_MEM_SIG)
0084         exit(EXIT_FAIL_MEM);
0085     else if (sig == FAIL_STAT_SIG)
0086         exit(EXIT_FAIL);
0087 
0088     exit(EXIT_OK);
0089 }
0090 
0091 struct config {
0092     __u32 action;
0093     int ifindex;
0094     __u32 options;
0095 };
0096 enum cfg_options_flags {
0097     NO_TOUCH = 0x0U,
0098     READ_MEM = 0x1U,
0099     SWAP_MAC = 0x2U,
0100 };
0101 #define XDP_ACTION_MAX (XDP_TX + 1)
0102 #define XDP_ACTION_MAX_STRLEN 11
0103 static const char *xdp_action_names[XDP_ACTION_MAX] = {
0104     [XDP_ABORTED]   = "XDP_ABORTED",
0105     [XDP_DROP]  = "XDP_DROP",
0106     [XDP_PASS]  = "XDP_PASS",
0107     [XDP_TX]    = "XDP_TX",
0108 };
0109 
0110 static const char *action2str(int action)
0111 {
0112     if (action < XDP_ACTION_MAX)
0113         return xdp_action_names[action];
0114     return NULL;
0115 }
0116 
0117 static int parse_xdp_action(char *action_str)
0118 {
0119     size_t maxlen;
0120     __u64 action = -1;
0121     int i;
0122 
0123     for (i = 0; i < XDP_ACTION_MAX; i++) {
0124         maxlen = XDP_ACTION_MAX_STRLEN;
0125         if (strncmp(xdp_action_names[i], action_str, maxlen) == 0) {
0126             action = i;
0127             break;
0128         }
0129     }
0130     return action;
0131 }
0132 
0133 static void list_xdp_actions(void)
0134 {
0135     int i;
0136 
0137     printf("Available XDP --action <options>\n");
0138     for (i = 0; i < XDP_ACTION_MAX; i++)
0139         printf("\t%s\n", xdp_action_names[i]);
0140     printf("\n");
0141 }
0142 
0143 static char* options2str(enum cfg_options_flags flag)
0144 {
0145     if (flag == NO_TOUCH)
0146         return "no_touch";
0147     if (flag & SWAP_MAC)
0148         return "swapmac";
0149     if (flag & READ_MEM)
0150         return "read";
0151     fprintf(stderr, "ERR: Unknown config option flags");
0152     int_exit(FAIL_STAT_SIG);
0153     return "unknown";
0154 }
0155 
0156 static void usage(char *argv[])
0157 {
0158     int i;
0159 
0160     printf("\nDOCUMENTATION:\n%s\n", __doc__);
0161     printf(" Usage: %s (options-see-below)\n", argv[0]);
0162     printf(" Listing options:\n");
0163     for (i = 0; long_options[i].name != 0; i++) {
0164         printf(" --%-12s", long_options[i].name);
0165         if (long_options[i].flag != NULL)
0166             printf(" flag (internal value:%d)",
0167                 *long_options[i].flag);
0168         else
0169             printf(" short-option: -%c",
0170                 long_options[i].val);
0171         printf("\n");
0172     }
0173     printf("\n");
0174     list_xdp_actions();
0175 }
0176 
0177 #define NANOSEC_PER_SEC 1000000000 /* 10^9 */
0178 static __u64 gettime(void)
0179 {
0180     struct timespec t;
0181     int res;
0182 
0183     res = clock_gettime(CLOCK_MONOTONIC, &t);
0184     if (res < 0) {
0185         fprintf(stderr, "Error with gettimeofday! (%i)\n", res);
0186         int_exit(FAIL_STAT_SIG);
0187     }
0188     return (__u64) t.tv_sec * NANOSEC_PER_SEC + t.tv_nsec;
0189 }
0190 
0191 /* Common stats data record shared with _kern.c */
0192 struct datarec {
0193     __u64 processed;
0194     __u64 issue;
0195 };
0196 struct record {
0197     __u64 timestamp;
0198     struct datarec total;
0199     struct datarec *cpu;
0200 };
0201 struct stats_record {
0202     struct record stats;
0203     struct record *rxq;
0204 };
0205 
0206 static struct datarec *alloc_record_per_cpu(void)
0207 {
0208     unsigned int nr_cpus = bpf_num_possible_cpus();
0209     struct datarec *array;
0210 
0211     array = calloc(nr_cpus, sizeof(struct datarec));
0212     if (!array) {
0213         fprintf(stderr, "Mem alloc error (nr_cpus:%u)\n", nr_cpus);
0214         int_exit(FAIL_MEM_SIG);
0215     }
0216     return array;
0217 }
0218 
0219 static struct record *alloc_record_per_rxq(void)
0220 {
0221     unsigned int nr_rxqs = bpf_map__max_entries(rx_queue_index_map);
0222     struct record *array;
0223 
0224     array = calloc(nr_rxqs, sizeof(struct record));
0225     if (!array) {
0226         fprintf(stderr, "Mem alloc error (nr_rxqs:%u)\n", nr_rxqs);
0227         int_exit(FAIL_MEM_SIG);
0228     }
0229     return array;
0230 }
0231 
0232 static struct stats_record *alloc_stats_record(void)
0233 {
0234     unsigned int nr_rxqs = bpf_map__max_entries(rx_queue_index_map);
0235     struct stats_record *rec;
0236     int i;
0237 
0238     rec = calloc(1, sizeof(struct stats_record));
0239     if (!rec) {
0240         fprintf(stderr, "Mem alloc error\n");
0241         int_exit(FAIL_MEM_SIG);
0242     }
0243     rec->rxq = alloc_record_per_rxq();
0244     for (i = 0; i < nr_rxqs; i++)
0245         rec->rxq[i].cpu = alloc_record_per_cpu();
0246 
0247     rec->stats.cpu = alloc_record_per_cpu();
0248     return rec;
0249 }
0250 
0251 static void free_stats_record(struct stats_record *r)
0252 {
0253     unsigned int nr_rxqs = bpf_map__max_entries(rx_queue_index_map);
0254     int i;
0255 
0256     for (i = 0; i < nr_rxqs; i++)
0257         free(r->rxq[i].cpu);
0258 
0259     free(r->rxq);
0260     free(r->stats.cpu);
0261     free(r);
0262 }
0263 
0264 static bool map_collect_percpu(int fd, __u32 key, struct record *rec)
0265 {
0266     /* For percpu maps, userspace gets a value per possible CPU */
0267     unsigned int nr_cpus = bpf_num_possible_cpus();
0268     struct datarec values[nr_cpus];
0269     __u64 sum_processed = 0;
0270     __u64 sum_issue = 0;
0271     int i;
0272 
0273     if ((bpf_map_lookup_elem(fd, &key, values)) != 0) {
0274         fprintf(stderr,
0275             "ERR: bpf_map_lookup_elem failed key:0x%X\n", key);
0276         return false;
0277     }
0278     /* Get time as close as possible to reading map contents */
0279     rec->timestamp = gettime();
0280 
0281     /* Record and sum values from each CPU */
0282     for (i = 0; i < nr_cpus; i++) {
0283         rec->cpu[i].processed = values[i].processed;
0284         sum_processed        += values[i].processed;
0285         rec->cpu[i].issue = values[i].issue;
0286         sum_issue        += values[i].issue;
0287     }
0288     rec->total.processed = sum_processed;
0289     rec->total.issue     = sum_issue;
0290     return true;
0291 }
0292 
0293 static void stats_collect(struct stats_record *rec)
0294 {
0295     int fd, i, max_rxqs;
0296 
0297     fd = bpf_map__fd(stats_global_map);
0298     map_collect_percpu(fd, 0, &rec->stats);
0299 
0300     fd = bpf_map__fd(rx_queue_index_map);
0301     max_rxqs = bpf_map__max_entries(rx_queue_index_map);
0302     for (i = 0; i < max_rxqs; i++)
0303         map_collect_percpu(fd, i, &rec->rxq[i]);
0304 }
0305 
0306 static double calc_period(struct record *r, struct record *p)
0307 {
0308     double period_ = 0;
0309     __u64 period = 0;
0310 
0311     period = r->timestamp - p->timestamp;
0312     if (period > 0)
0313         period_ = ((double) period / NANOSEC_PER_SEC);
0314 
0315     return period_;
0316 }
0317 
0318 static __u64 calc_pps(struct datarec *r, struct datarec *p, double period_)
0319 {
0320     __u64 packets = 0;
0321     __u64 pps = 0;
0322 
0323     if (period_ > 0) {
0324         packets = r->processed - p->processed;
0325         pps = packets / period_;
0326     }
0327     return pps;
0328 }
0329 
0330 static __u64 calc_errs_pps(struct datarec *r,
0331                 struct datarec *p, double period_)
0332 {
0333     __u64 packets = 0;
0334     __u64 pps = 0;
0335 
0336     if (period_ > 0) {
0337         packets = r->issue - p->issue;
0338         pps = packets / period_;
0339     }
0340     return pps;
0341 }
0342 
0343 static void stats_print(struct stats_record *stats_rec,
0344             struct stats_record *stats_prev,
0345             int action, __u32 cfg_opt)
0346 {
0347     unsigned int nr_rxqs = bpf_map__max_entries(rx_queue_index_map);
0348     unsigned int nr_cpus = bpf_num_possible_cpus();
0349     double pps = 0, err = 0;
0350     struct record *rec, *prev;
0351     double t;
0352     int rxq;
0353     int i;
0354 
0355     /* Header */
0356     printf("\nRunning XDP on dev:%s (ifindex:%d) action:%s options:%s\n",
0357            ifname, ifindex, action2str(action), options2str(cfg_opt));
0358 
0359     /* stats_global_map */
0360     {
0361         char *fmt_rx = "%-15s %-7d %'-11.0f %'-10.0f %s\n";
0362         char *fm2_rx = "%-15s %-7s %'-11.0f\n";
0363         char *errstr = "";
0364 
0365         printf("%-15s %-7s %-11s %-11s\n",
0366                "XDP stats", "CPU", "pps", "issue-pps");
0367 
0368         rec  =  &stats_rec->stats;
0369         prev = &stats_prev->stats;
0370         t = calc_period(rec, prev);
0371         for (i = 0; i < nr_cpus; i++) {
0372             struct datarec *r = &rec->cpu[i];
0373             struct datarec *p = &prev->cpu[i];
0374 
0375             pps = calc_pps     (r, p, t);
0376             err = calc_errs_pps(r, p, t);
0377             if (err > 0)
0378                 errstr = "invalid-ifindex";
0379             if (pps > 0)
0380                 printf(fmt_rx, "XDP-RX CPU",
0381                     i, pps, err, errstr);
0382         }
0383         pps  = calc_pps     (&rec->total, &prev->total, t);
0384         err  = calc_errs_pps(&rec->total, &prev->total, t);
0385         printf(fm2_rx, "XDP-RX CPU", "total", pps, err);
0386     }
0387 
0388     /* rx_queue_index_map */
0389     printf("\n%-15s %-7s %-11s %-11s\n",
0390            "RXQ stats", "RXQ:CPU", "pps", "issue-pps");
0391 
0392     for (rxq = 0; rxq < nr_rxqs; rxq++) {
0393         char *fmt_rx = "%-15s %3d:%-3d %'-11.0f %'-10.0f %s\n";
0394         char *fm2_rx = "%-15s %3d:%-3s %'-11.0f\n";
0395         char *errstr = "";
0396         int rxq_ = rxq;
0397 
0398         /* Last RXQ in map catch overflows */
0399         if (rxq_ == nr_rxqs - 1)
0400             rxq_ = -1;
0401 
0402         rec  =  &stats_rec->rxq[rxq];
0403         prev = &stats_prev->rxq[rxq];
0404         t = calc_period(rec, prev);
0405         for (i = 0; i < nr_cpus; i++) {
0406             struct datarec *r = &rec->cpu[i];
0407             struct datarec *p = &prev->cpu[i];
0408 
0409             pps = calc_pps     (r, p, t);
0410             err = calc_errs_pps(r, p, t);
0411             if (err > 0) {
0412                 if (rxq_ == -1)
0413                     errstr = "map-overflow-RXQ";
0414                 else
0415                     errstr = "err";
0416             }
0417             if (pps > 0)
0418                 printf(fmt_rx, "rx_queue_index",
0419                        rxq_, i, pps, err, errstr);
0420         }
0421         pps  = calc_pps     (&rec->total, &prev->total, t);
0422         err  = calc_errs_pps(&rec->total, &prev->total, t);
0423         if (pps || err)
0424             printf(fm2_rx, "rx_queue_index", rxq_, "sum", pps, err);
0425     }
0426 }
0427 
0428 
0429 /* Pointer swap trick */
0430 static inline void swap(struct stats_record **a, struct stats_record **b)
0431 {
0432     struct stats_record *tmp;
0433 
0434     tmp = *a;
0435     *a = *b;
0436     *b = tmp;
0437 }
0438 
0439 static void stats_poll(int interval, int action, __u32 cfg_opt)
0440 {
0441     struct stats_record *record, *prev;
0442 
0443     record = alloc_stats_record();
0444     prev   = alloc_stats_record();
0445     stats_collect(record);
0446 
0447     while (1) {
0448         swap(&prev, &record);
0449         stats_collect(record);
0450         stats_print(record, prev, action, cfg_opt);
0451         sleep(interval);
0452     }
0453 
0454     free_stats_record(record);
0455     free_stats_record(prev);
0456 }
0457 
0458 
0459 int main(int argc, char **argv)
0460 {
0461     __u32 cfg_options= NO_TOUCH ; /* Default: Don't touch packet memory */
0462     struct bpf_prog_info info = {};
0463     __u32 info_len = sizeof(info);
0464     int prog_fd, map_fd, opt, err;
0465     bool use_separators = true;
0466     struct config cfg = { 0 };
0467     struct bpf_program *prog;
0468     struct bpf_object *obj;
0469     struct bpf_map *map;
0470     char filename[256];
0471     int longindex = 0;
0472     int interval = 2;
0473     __u32 key = 0;
0474 
0475 
0476     char action_str_buf[XDP_ACTION_MAX_STRLEN + 1 /* for \0 */] = { 0 };
0477     int action = XDP_PASS; /* Default action */
0478     char *action_str = NULL;
0479 
0480     snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
0481 
0482     obj = bpf_object__open_file(filename, NULL);
0483     if (libbpf_get_error(obj))
0484         return EXIT_FAIL;
0485 
0486     prog = bpf_object__next_program(obj, NULL);
0487     bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);
0488 
0489     err = bpf_object__load(obj);
0490     if (err)
0491         return EXIT_FAIL;
0492     prog_fd = bpf_program__fd(prog);
0493 
0494     map =  bpf_object__find_map_by_name(obj, "config_map");
0495     stats_global_map = bpf_object__find_map_by_name(obj, "stats_global_map");
0496     rx_queue_index_map = bpf_object__find_map_by_name(obj, "rx_queue_index_map");
0497     if (!map || !stats_global_map || !rx_queue_index_map) {
0498         printf("finding a map in obj file failed\n");
0499         return EXIT_FAIL;
0500     }
0501     map_fd = bpf_map__fd(map);
0502 
0503     if (!prog_fd) {
0504         fprintf(stderr, "ERR: bpf_prog_load_xattr: %s\n", strerror(errno));
0505         return EXIT_FAIL;
0506     }
0507 
0508     /* Parse commands line args */
0509     while ((opt = getopt_long(argc, argv, "FhSrmzd:s:a:",
0510                   long_options, &longindex)) != -1) {
0511         switch (opt) {
0512         case 'd':
0513             if (strlen(optarg) >= IF_NAMESIZE) {
0514                 fprintf(stderr, "ERR: --dev name too long\n");
0515                 goto error;
0516             }
0517             ifname = (char *)&ifname_buf;
0518             strncpy(ifname, optarg, IF_NAMESIZE);
0519             ifindex = if_nametoindex(ifname);
0520             if (ifindex == 0) {
0521                 fprintf(stderr,
0522                     "ERR: --dev name unknown err(%d):%s\n",
0523                     errno, strerror(errno));
0524                 goto error;
0525             }
0526             break;
0527         case 's':
0528             interval = atoi(optarg);
0529             break;
0530         case 'S':
0531             xdp_flags |= XDP_FLAGS_SKB_MODE;
0532             break;
0533         case 'z':
0534             use_separators = false;
0535             break;
0536         case 'a':
0537             action_str = (char *)&action_str_buf;
0538             strncpy(action_str, optarg, XDP_ACTION_MAX_STRLEN);
0539             break;
0540         case 'r':
0541             cfg_options |= READ_MEM;
0542             break;
0543         case 'm':
0544             cfg_options |= SWAP_MAC;
0545             break;
0546         case 'F':
0547             xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
0548             break;
0549         case 'h':
0550         error:
0551         default:
0552             usage(argv);
0553             return EXIT_FAIL_OPTION;
0554         }
0555     }
0556 
0557     if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
0558         xdp_flags |= XDP_FLAGS_DRV_MODE;
0559 
0560     /* Required option */
0561     if (ifindex == -1) {
0562         fprintf(stderr, "ERR: required option --dev missing\n");
0563         usage(argv);
0564         return EXIT_FAIL_OPTION;
0565     }
0566     cfg.ifindex = ifindex;
0567 
0568     /* Parse action string */
0569     if (action_str) {
0570         action = parse_xdp_action(action_str);
0571         if (action < 0) {
0572             fprintf(stderr, "ERR: Invalid XDP --action: %s\n",
0573                 action_str);
0574             list_xdp_actions();
0575             return EXIT_FAIL_OPTION;
0576         }
0577     }
0578     cfg.action = action;
0579 
0580     /* XDP_TX requires changing MAC-addrs, else HW may drop */
0581     if (action == XDP_TX)
0582         cfg_options |= SWAP_MAC;
0583     cfg.options = cfg_options;
0584 
0585     /* Trick to pretty printf with thousands separators use %' */
0586     if (use_separators)
0587         setlocale(LC_NUMERIC, "en_US");
0588 
0589     /* User-side setup ifindex in config_map */
0590     err = bpf_map_update_elem(map_fd, &key, &cfg, 0);
0591     if (err) {
0592         fprintf(stderr, "Store config failed (err:%d)\n", err);
0593         exit(EXIT_FAIL_BPF);
0594     }
0595 
0596     /* Remove XDP program when program is interrupted or killed */
0597     signal(SIGINT, int_exit);
0598     signal(SIGTERM, int_exit);
0599 
0600     if (bpf_xdp_attach(ifindex, prog_fd, xdp_flags, NULL) < 0) {
0601         fprintf(stderr, "link set xdp fd failed\n");
0602         return EXIT_FAIL_XDP;
0603     }
0604 
0605     err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
0606     if (err) {
0607         printf("can't get prog info - %s\n", strerror(errno));
0608         return err;
0609     }
0610     prog_id = info.id;
0611 
0612     stats_poll(interval, action, cfg_options);
0613     return EXIT_OK;
0614 }