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
  34int show_stats;
  35int show_details;
  36int show_raw;
  37int show_graph;
  38int timestamp;
  39
  40int batch_mode;
  41int use_iec;
  42int force;
  43bool use_names;
  44int json;
  45int color;
  46int oneline;
  47
  48static char *conf_file;
  49
  50struct rtnl_handle rth;
  51
  52static void *BODY;      /* cached handle dlopen(NULL) */
  53static struct qdisc_util *qdisc_list;
  54static struct filter_util *filter_list;
  55
  56static int print_noqopt(struct qdisc_util *qu, FILE *f,
  57                        struct rtattr *opt)
  58{
  59        if (opt && RTA_PAYLOAD(opt))
  60                fprintf(f, "[Unknown qdisc, optlen=%u] ",
  61                        (unsigned int) RTA_PAYLOAD(opt));
  62        return 0;
  63}
  64
  65static int parse_noqopt(struct qdisc_util *qu, int argc, char **argv,
  66                        struct nlmsghdr *n, const char *dev)
  67{
  68        if (argc) {
  69                fprintf(stderr,
  70                        "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n",
  71                        qu->id, *argv);
  72                return -1;
  73        }
  74        return 0;
  75}
  76
  77static int print_nofopt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 fhandle)
  78{
  79        if (opt && RTA_PAYLOAD(opt))
  80                fprintf(f, "fh %08x [Unknown filter, optlen=%u] ",
  81                        fhandle, (unsigned int) RTA_PAYLOAD(opt));
  82        else if (fhandle)
  83                fprintf(f, "fh %08x ", fhandle);
  84        return 0;
  85}
  86
  87static int parse_nofopt(struct filter_util *qu, char *fhandle,
  88                        int argc, char **argv, struct nlmsghdr *n)
  89{
  90        __u32 handle;
  91
  92        if (argc) {
  93                fprintf(stderr,
  94                        "Unknown filter \"%s\", hence option \"%s\" is unparsable\n",
  95                        qu->id, *argv);
  96                return -1;
  97        }
  98        if (fhandle) {
  99                struct tcmsg *t = NLMSG_DATA(n);
 100
 101                if (get_u32(&handle, fhandle, 16)) {
 102                        fprintf(stderr, "Unparsable filter ID \"%s\"\n", fhandle);
 103                        return -1;
 104                }
 105                t->tcm_handle = handle;
 106        }
 107        return 0;
 108}
 109
 110struct qdisc_util *get_qdisc_kind(const char *str)
 111{
 112        void *dlh;
 113        char buf[256];
 114        struct qdisc_util *q;
 115
 116        for (q = qdisc_list; q; q = q->next)
 117                if (strcmp(q->id, str) == 0)
 118                        return q;
 119
 120        snprintf(buf, sizeof(buf), "%s/q_%s.so", get_tc_lib(), str);
 121        dlh = dlopen(buf, RTLD_LAZY);
 122        if (!dlh) {
 123                /* look in current binary, only open once */
 124                dlh = BODY;
 125                if (dlh == NULL) {
 126                        dlh = BODY = dlopen(NULL, RTLD_LAZY);
 127                        if (dlh == NULL)
 128                                goto noexist;
 129                }
 130        }
 131
 132        snprintf(buf, sizeof(buf), "%s_qdisc_util", str);
 133        q = dlsym(dlh, buf);
 134        if (q == NULL)
 135                goto noexist;
 136
 137reg:
 138        q->next = qdisc_list;
 139        qdisc_list = q;
 140        return q;
 141
 142noexist:
 143        q = calloc(1, sizeof(*q));
 144        if (q) {
 145                q->id = strdup(str);
 146                q->parse_qopt = parse_noqopt;
 147                q->print_qopt = print_noqopt;
 148                goto reg;
 149        }
 150        return q;
 151}
 152
 153
 154struct filter_util *get_filter_kind(const char *str)
 155{
 156        void *dlh;
 157        char buf[256];
 158        struct filter_util *q;
 159
 160        for (q = filter_list; q; q = q->next)
 161                if (strcmp(q->id, str) == 0)
 162                        return q;
 163
 164        snprintf(buf, sizeof(buf), "%s/f_%s.so", get_tc_lib(), str);
 165        dlh = dlopen(buf, RTLD_LAZY);
 166        if (dlh == NULL) {
 167                dlh = BODY;
 168                if (dlh == NULL) {
 169                        dlh = BODY = dlopen(NULL, RTLD_LAZY);
 170                        if (dlh == NULL)
 171                                goto noexist;
 172                }
 173        }
 174
 175        snprintf(buf, sizeof(buf), "%s_filter_util", str);
 176        q = dlsym(dlh, buf);
 177        if (q == NULL)
 178                goto noexist;
 179
 180reg:
 181        q->next = filter_list;
 182        filter_list = q;
 183        return q;
 184noexist:
 185        q = calloc(1, sizeof(*q));
 186        if (q) {
 187                strncpy(q->id, str, 15);
 188                q->parse_fopt = parse_nofopt;
 189                q->print_fopt = print_nofopt;
 190                goto reg;
 191        }
 192        return q;
 193}
 194
 195static void usage(void)
 196{
 197        fprintf(stderr,
 198                "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
 199                "       tc [-force] -batch filename\n"
 200                "where  OBJECT := { qdisc | class | filter | chain |\n"
 201                "                   action | monitor | exec }\n"
 202                "       OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[aw] |\n"
 203                "                   -o[neline] | -j[son] | -p[retty] | -c[olor]\n"
 204                "                   -b[atch] [filename] | -n[etns] name | -N[umeric] |\n"
 205                "                    -nm | -nam[es] | { -cf | -conf } path }\n");
 206}
 207
 208static int do_cmd(int argc, char **argv)
 209{
 210        if (matches(*argv, "qdisc") == 0)
 211                return do_qdisc(argc-1, argv+1);
 212        if (matches(*argv, "class") == 0)
 213                return do_class(argc-1, argv+1);
 214        if (matches(*argv, "filter") == 0)
 215                return do_filter(argc-1, argv+1);
 216        if (matches(*argv, "chain") == 0)
 217                return do_chain(argc-1, argv+1);
 218        if (matches(*argv, "actions") == 0)
 219                return do_action(argc-1, argv+1);
 220        if (matches(*argv, "monitor") == 0)
 221                return do_tcmonitor(argc-1, argv+1);
 222        if (matches(*argv, "exec") == 0)
 223                return do_exec(argc-1, argv+1);
 224        if (matches(*argv, "help") == 0) {
 225                usage();
 226                return 0;
 227        }
 228
 229        fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n",
 230                *argv);
 231        return -1;
 232}
 233
 234static int batch(const char *name)
 235{
 236        char *line = NULL;
 237        size_t len = 0;
 238        int ret = 0;
 239
 240        batch_mode = 1;
 241        if (name && strcmp(name, "-") != 0) {
 242                if (freopen(name, "r", stdin) == NULL) {
 243                        fprintf(stderr,
 244                                "Cannot open file \"%s\" for reading: %s\n",
 245                                name, strerror(errno));
 246                        return -1;
 247                }
 248        }
 249
 250        tc_core_init();
 251
 252        if (rtnl_open(&rth, 0) < 0) {
 253                fprintf(stderr, "Cannot open rtnetlink\n");
 254                return -1;
 255        }
 256
 257        cmdlineno = 0;
 258        while (getcmdline(&line, &len, stdin) != -1) {
 259                char *largv[100];
 260                int largc;
 261
 262                largc = makeargs(line, largv, 100);
 263                if (largc == 0)
 264                        continue;       /* blank line */
 265
 266                if (do_cmd(largc, largv)) {
 267                        fprintf(stderr, "Command failed %s:%d\n",
 268                                name, cmdlineno);
 269                        ret = 1;
 270                        if (!force)
 271                                break;
 272                }
 273                fflush(stdout);
 274        }
 275
 276        free(line);
 277        rtnl_close(&rth);
 278        return ret;
 279}
 280
 281
 282int main(int argc, char **argv)
 283{
 284        int ret;
 285        char *batch_file = NULL;
 286
 287        while (argc > 1) {
 288                if (argv[1][0] != '-')
 289                        break;
 290                if (matches(argv[1], "-stats") == 0 ||
 291                         matches(argv[1], "-statistics") == 0) {
 292                        ++show_stats;
 293                } else if (matches(argv[1], "-details") == 0) {
 294                        ++show_details;
 295                } else if (matches(argv[1], "-raw") == 0) {
 296                        ++show_raw;
 297                } else if (matches(argv[1], "-pretty") == 0) {
 298                        ++pretty;
 299                } else if (matches(argv[1], "-graph") == 0) {
 300                        show_graph = 1;
 301                } else if (matches(argv[1], "-Version") == 0) {
 302                        printf("tc utility, iproute2-%s\n", version);
 303                        return 0;
 304                } else if (matches(argv[1], "-iec") == 0) {
 305                        ++use_iec;
 306                } else if (matches(argv[1], "-help") == 0) {
 307                        usage();
 308                        return 0;
 309                } else if (matches(argv[1], "-force") == 0) {
 310                        ++force;
 311                } else if (matches(argv[1], "-batch") == 0) {
 312                        argc--; argv++;
 313                        if (argc <= 1)
 314                                usage();
 315                        batch_file = argv[1];
 316                } else if (matches(argv[1], "-netns") == 0) {
 317                        NEXT_ARG();
 318                        if (netns_switch(argv[1]))
 319                                return -1;
 320                } else if (matches(argv[1], "-Numeric") == 0) {
 321                        ++numeric;
 322                } else if (matches(argv[1], "-names") == 0 ||
 323                                matches(argv[1], "-nm") == 0) {
 324                        use_names = true;
 325                } else if (matches(argv[1], "-cf") == 0 ||
 326                                matches(argv[1], "-conf") == 0) {
 327                        NEXT_ARG();
 328                        conf_file = argv[1];
 329                } else if (matches_color(argv[1], &color)) {
 330                } else if (matches(argv[1], "-timestamp") == 0) {
 331                        timestamp++;
 332                } else if (matches(argv[1], "-tshort") == 0) {
 333                        ++timestamp;
 334                        ++timestamp_short;
 335                } else if (matches(argv[1], "-json") == 0) {
 336                        ++json;
 337                } else if (matches(argv[1], "-oneline") == 0) {
 338                        ++oneline;
 339                } else {
 340                        fprintf(stderr,
 341                                "Option \"%s\" is unknown, try \"tc -help\".\n",
 342                                argv[1]);
 343                        return -1;
 344                }
 345                argc--; argv++;
 346        }
 347
 348        _SL_ = oneline ? "\\" : "\n";
 349
 350        check_enable_color(color, json);
 351
 352        if (batch_file)
 353                return batch(batch_file);
 354
 355        if (argc <= 1) {
 356                usage();
 357                return 0;
 358        }
 359
 360        tc_core_init();
 361        if (rtnl_open(&rth, 0) < 0) {
 362                fprintf(stderr, "Cannot open rtnetlink\n");
 363                exit(1);
 364        }
 365
 366        if (use_names && cls_names_init(conf_file)) {
 367                ret = -1;
 368                goto Exit;
 369        }
 370
 371        ret = do_cmd(argc-1, argv+1);
 372Exit:
 373        rtnl_close(&rth);
 374
 375        if (use_names)
 376                cls_names_uninit();
 377
 378        return ret;
 379}
 380