iproute2/tc/tc.c
<<
>>
Prefs
   1/*
   2 * tc.c         "tc" utility frontend.
   3 *
   4 *              This program is free software; you can redistribute it and/or
   5 *              modify it under the terms of the GNU General Public License
   6 *              as published by the Free Software Foundation; either version
   7 *              2 of the License, or (at your option) any later version.
   8 *
   9 * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  10 *
  11 * Fixes:
  12 *
  13 * Petri Mattila <petri@prihateam.fi> 990308: wrong memset's resulted in faults
  14 */
  15
  16#include <stdio.h>
  17#include <stdlib.h>
  18#include <unistd.h>
  19#include <fcntl.h>
  20#include <dlfcn.h>
  21#include <sys/socket.h>
  22#include <netinet/in.h>
  23#include <arpa/inet.h>
  24#include <string.h>
  25#include <errno.h>
  26
  27#include "version.h"
  28#include "utils.h"
  29#include "tc_util.h"
  30#include "tc_common.h"
  31#include "namespace.h"
  32#include "rt_names.h"
  33#include "bpf_util.h"
  34
  35int show_stats;
  36int show_details;
  37int show_raw;
  38int show_graph;
  39int timestamp;
  40
  41int batch_mode;
  42int use_iec;
  43int force;
  44bool use_names;
  45int json;
  46int color;
  47int oneline;
  48int brief;
  49
  50static char *conf_file;
  51
  52struct rtnl_handle rth;
  53
  54static void *BODY;      /* cached handle dlopen(NULL) */
  55static struct qdisc_util *qdisc_list;
  56static struct filter_util *filter_list;
  57
  58static int print_noqopt(struct qdisc_util *qu, FILE *f,
  59                        struct rtattr *opt)
  60{
  61        if (opt && RTA_PAYLOAD(opt))
  62                fprintf(f, "[Unknown qdisc, optlen=%u] ",
  63                        (unsigned int) RTA_PAYLOAD(opt));
  64        return 0;
  65}
  66
  67static int parse_noqopt(struct qdisc_util *qu, int argc, char **argv,
  68                        struct nlmsghdr *n, const char *dev)
  69{
  70        if (argc) {
  71                fprintf(stderr,
  72                        "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n",
  73                        qu->id, *argv);
  74                return -1;
  75        }
  76        return 0;
  77}
  78
  79static int print_nofopt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 fhandle)
  80{
  81        if (opt && RTA_PAYLOAD(opt))
  82                fprintf(f, "fh %08x [Unknown filter, optlen=%u] ",
  83                        fhandle, (unsigned int) RTA_PAYLOAD(opt));
  84        else if (fhandle)
  85                fprintf(f, "fh %08x ", fhandle);
  86        return 0;
  87}
  88
  89static int parse_nofopt(struct filter_util *qu, char *fhandle,
  90                        int argc, char **argv, struct nlmsghdr *n)
  91{
  92        __u32 handle;
  93
  94        if (argc) {
  95                fprintf(stderr,
  96                        "Unknown filter \"%s\", hence option \"%s\" is unparsable\n",
  97                        qu->id, *argv);
  98                return -1;
  99        }
 100        if (fhandle) {
 101                struct tcmsg *t = NLMSG_DATA(n);
 102
 103                if (get_u32(&handle, fhandle, 16)) {
 104                        fprintf(stderr, "Unparsable filter ID \"%s\"\n", fhandle);
 105                        return -1;
 106                }
 107                t->tcm_handle = handle;
 108        }
 109        return 0;
 110}
 111
 112struct qdisc_util *get_qdisc_kind(const char *str)
 113{
 114        void *dlh;
 115        char buf[256];
 116        struct qdisc_util *q;
 117
 118        for (q = qdisc_list; q; q = q->next)
 119                if (strcmp(q->id, str) == 0)
 120                        return q;
 121
 122        snprintf(buf, sizeof(buf), "%s/q_%s.so", get_tc_lib(), str);
 123        dlh = dlopen(buf, RTLD_LAZY);
 124        if (!dlh) {
 125                /* look in current binary, only open once */
 126                dlh = BODY;
 127                if (dlh == NULL) {
 128                        dlh = BODY = dlopen(NULL, RTLD_LAZY);
 129                        if (dlh == NULL)
 130                                goto noexist;
 131                }
 132        }
 133
 134        snprintf(buf, sizeof(buf), "%s_qdisc_util", str);
 135        q = dlsym(dlh, buf);
 136        if (q == NULL)
 137                goto noexist;
 138
 139reg:
 140        q->next = qdisc_list;
 141        qdisc_list = q;
 142        return q;
 143
 144noexist:
 145        q = calloc(1, sizeof(*q));
 146        if (q) {
 147                q->id = strdup(str);
 148                q->parse_qopt = parse_noqopt;
 149                q->print_qopt = print_noqopt;
 150                goto reg;
 151        }
 152        return q;
 153}
 154
 155
 156struct filter_util *get_filter_kind(const char *str)
 157{
 158        void *dlh;
 159        char buf[256];
 160        struct filter_util *q;
 161
 162        for (q = filter_list; q; q = q->next)
 163                if (strcmp(q->id, str) == 0)
 164                        return q;
 165
 166        snprintf(buf, sizeof(buf), "%s/f_%s.so", get_tc_lib(), str);
 167        dlh = dlopen(buf, RTLD_LAZY);
 168        if (dlh == NULL) {
 169                dlh = BODY;
 170                if (dlh == NULL) {
 171                        dlh = BODY = dlopen(NULL, RTLD_LAZY);
 172                        if (dlh == NULL)
 173                                goto noexist;
 174                }
 175        }
 176
 177        snprintf(buf, sizeof(buf), "%s_filter_util", str);
 178        q = dlsym(dlh, buf);
 179        if (q == NULL)
 180                goto noexist;
 181
 182reg:
 183        q->next = filter_list;
 184        filter_list = q;
 185        return q;
 186noexist:
 187        q = calloc(1, sizeof(*q));
 188        if (q) {
 189                strncpy(q->id, str, 15);
 190                q->parse_fopt = parse_nofopt;
 191                q->print_fopt = print_nofopt;
 192                goto reg;
 193        }
 194        return q;
 195}
 196
 197static void usage(void)
 198{
 199        fprintf(stderr,
 200                "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
 201                "       tc [-force] -batch filename\n"
 202                "where  OBJECT := { qdisc | class | filter | chain |\n"
 203                "                   action | monitor | exec }\n"
 204                "       OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[aw] |\n"
 205                "                   -o[neline] | -j[son] | -p[retty] | -c[olor]\n"
 206                "                   -b[atch] [filename] | -n[etns] name | -N[umeric] |\n"
 207                "                    -nm | -nam[es] | { -cf | -conf } path\n"
 208                "                    -br[ief] }\n");
 209}
 210
 211static int do_cmd(int argc, char **argv)
 212{
 213        if (matches(*argv, "qdisc") == 0)
 214                return do_qdisc(argc-1, argv+1);
 215        if (matches(*argv, "class") == 0)
 216                return do_class(argc-1, argv+1);
 217        if (matches(*argv, "filter") == 0)
 218                return do_filter(argc-1, argv+1);
 219        if (matches(*argv, "chain") == 0)
 220                return do_chain(argc-1, argv+1);
 221        if (matches(*argv, "actions") == 0)
 222                return do_action(argc-1, argv+1);
 223        if (matches(*argv, "monitor") == 0)
 224                return do_tcmonitor(argc-1, argv+1);
 225        if (matches(*argv, "exec") == 0)
 226                return do_exec(argc-1, argv+1);
 227        if (matches(*argv, "help") == 0) {
 228                usage();
 229                return 0;
 230        }
 231
 232        fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n",
 233                *argv);
 234        return -1;
 235}
 236
 237static int tc_batch_cmd(int argc, char *argv[], void *data)
 238{
 239        return do_cmd(argc, argv);
 240}
 241
 242static int batch(const char *name)
 243{
 244        int ret;
 245
 246        batch_mode = 1;
 247        tc_core_init();
 248
 249        if (rtnl_open(&rth, 0) < 0) {
 250                fprintf(stderr, "Cannot open rtnetlink\n");
 251                return -1;
 252        }
 253
 254        ret = do_batch(name, force, tc_batch_cmd, NULL);
 255
 256        rtnl_close(&rth);
 257        return ret;
 258}
 259
 260
 261int main(int argc, char **argv)
 262{
 263        const char *libbpf_version;
 264        char *batch_file = NULL;
 265        int ret;
 266
 267        while (argc > 1) {
 268                if (argv[1][0] != '-')
 269                        break;
 270                if (matches(argv[1], "-stats") == 0 ||
 271                         matches(argv[1], "-statistics") == 0) {
 272                        ++show_stats;
 273                } else if (matches(argv[1], "-details") == 0) {
 274                        ++show_details;
 275                } else if (matches(argv[1], "-raw") == 0) {
 276                        ++show_raw;
 277                } else if (matches(argv[1], "-pretty") == 0) {
 278                        ++pretty;
 279                } else if (matches(argv[1], "-graph") == 0) {
 280                        show_graph = 1;
 281                } else if (matches(argv[1], "-Version") == 0) {
 282                        printf("tc utility, iproute2-%s", version);
 283                        libbpf_version = get_libbpf_version();
 284                        if (libbpf_version)
 285                                printf(", libbpf %s", libbpf_version);
 286                        printf("\n");
 287                        return 0;
 288                } else if (matches(argv[1], "-iec") == 0) {
 289                        ++use_iec;
 290                } else if (matches(argv[1], "-help") == 0) {
 291                        usage();
 292                        return 0;
 293                } else if (matches(argv[1], "-force") == 0) {
 294                        ++force;
 295                } else if (matches(argv[1], "-batch") == 0) {
 296                        argc--; argv++;
 297                        if (argc <= 1)
 298                                usage();
 299                        batch_file = argv[1];
 300                } else if (matches(argv[1], "-netns") == 0) {
 301                        NEXT_ARG();
 302                        if (netns_switch(argv[1]))
 303                                return -1;
 304                } else if (matches(argv[1], "-Numeric") == 0) {
 305                        ++numeric;
 306                } else if (matches(argv[1], "-names") == 0 ||
 307                                matches(argv[1], "-nm") == 0) {
 308                        use_names = true;
 309                } else if (matches(argv[1], "-cf") == 0 ||
 310                                matches(argv[1], "-conf") == 0) {
 311                        NEXT_ARG();
 312                        conf_file = argv[1];
 313                } else if (matches_color(argv[1], &color)) {
 314                } else if (matches(argv[1], "-timestamp") == 0) {
 315                        timestamp++;
 316                } else if (matches(argv[1], "-tshort") == 0) {
 317                        ++timestamp;
 318                        ++timestamp_short;
 319                } else if (matches(argv[1], "-json") == 0) {
 320                        ++json;
 321                } else if (matches(argv[1], "-oneline") == 0) {
 322                        ++oneline;
 323                }else if (matches(argv[1], "-brief") == 0) {
 324                        ++brief;
 325                } else {
 326                        fprintf(stderr,
 327                                "Option \"%s\" is unknown, try \"tc -help\".\n",
 328                                argv[1]);
 329                        return -1;
 330                }
 331                argc--; argv++;
 332        }
 333
 334        _SL_ = oneline ? "\\" : "\n";
 335
 336        check_enable_color(color, json);
 337
 338        if (batch_file)
 339                return batch(batch_file);
 340
 341        if (argc <= 1) {
 342                usage();
 343                return 0;
 344        }
 345
 346        tc_core_init();
 347        if (rtnl_open(&rth, 0) < 0) {
 348                fprintf(stderr, "Cannot open rtnetlink\n");
 349                exit(1);
 350        }
 351
 352        if (use_names && cls_names_init(conf_file)) {
 353                ret = -1;
 354                goto Exit;
 355        }
 356
 357        ret = do_cmd(argc-1, argv+1);
 358Exit:
 359        rtnl_close(&rth);
 360
 361        if (use_names)
 362                cls_names_uninit();
 363
 364        return ret;
 365}
 366