| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* tc_monitor.c "tc monitor". |
| 4 |
* |
| 5 |
* Authors: Jamal Hadi Salim |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <unistd.h> |
| 11 |
#include <fcntl.h> |
| 12 |
#include <sys/socket.h> |
| 13 |
#include <netinet/in.h> |
| 14 |
#include <arpa/inet.h> |
| 15 |
#include <string.h> |
| 16 |
#include <time.h> |
| 17 |
#include "rt_names.h" |
| 18 |
#include "utils.h" |
| 19 |
#include "tc_util.h" |
| 20 |
#include "tc_common.h" |
| 21 |
|
| 22 |
|
| 23 |
static void usage(void) __attribute__((noreturn)); |
| 24 |
|
| 25 |
static void usage(void) |
| 26 |
{ |
| 27 |
fprintf(stderr, "Usage: tc [-timestamp [-tshort] monitor\n"); |
| 28 |
exit(-1); |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
static int accept_tcmsg(struct rtnl_ctrl_data *ctrl, |
| 33 |
struct nlmsghdr *n, void *arg) |
| 34 |
{ |
| 35 |
FILE *fp = (FILE *)arg; |
| 36 |
|
| 37 |
if (timestamp) |
| 38 |
print_timestamp(fp); |
| 39 |
|
| 40 |
if (n->nlmsg_type == RTM_NEWTFILTER || |
| 41 |
n->nlmsg_type == RTM_DELTFILTER || |
| 42 |
n->nlmsg_type == RTM_NEWCHAIN || |
| 43 |
n->nlmsg_type == RTM_DELCHAIN) { |
| 44 |
print_filter(n, arg); |
| 45 |
return 0; |
| 46 |
} |
| 47 |
if (n->nlmsg_type == RTM_NEWTCLASS || n->nlmsg_type == RTM_DELTCLASS) { |
| 48 |
print_class(n, arg); |
| 49 |
return 0; |
| 50 |
} |
| 51 |
if (n->nlmsg_type == RTM_NEWQDISC || n->nlmsg_type == RTM_DELQDISC) { |
| 52 |
print_qdisc(n, arg); |
| 53 |
return 0; |
| 54 |
} |
| 55 |
if (n->nlmsg_type == RTM_GETACTION || n->nlmsg_type == RTM_NEWACTION || |
| 56 |
n->nlmsg_type == RTM_DELACTION) { |
| 57 |
print_action(n, arg); |
| 58 |
return 0; |
| 59 |
} |
| 60 |
if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP && |
| 61 |
n->nlmsg_type != NLMSG_DONE) { |
| 62 |
fprintf(stderr, "Unknown message: length %08d type %08x flags %08x\n", |
| 63 |
n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); |
| 64 |
} |
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
int do_tcmonitor(int argc, char **argv) |
| 69 |
{ |
| 70 |
struct rtnl_handle rth; |
| 71 |
char *file = NULL; |
| 72 |
unsigned int groups = nl_mgrp(RTNLGRP_TC); |
| 73 |
|
| 74 |
while (argc > 0) { |
| 75 |
if (matches(*argv, "file") == 0) { |
| 76 |
NEXT_ARG(); |
| 77 |
file = *argv; |
| 78 |
} else { |
| 79 |
if (matches(*argv, "help") == 0) { |
| 80 |
usage(); |
| 81 |
} else { |
| 82 |
fprintf(stderr, "Argument \"%s\" is unknown, try \"tc monitor help\".\n", *argv); |
| 83 |
exit(-1); |
| 84 |
} |
| 85 |
} |
| 86 |
argc--; argv++; |
| 87 |
} |
| 88 |
|
| 89 |
if (file) { |
| 90 |
FILE *fp = fopen(file, "r"); |
| 91 |
int ret; |
| 92 |
|
| 93 |
if (fp == NULL) { |
| 94 |
perror("Cannot fopen"); |
| 95 |
exit(-1); |
| 96 |
} |
| 97 |
|
| 98 |
ret = rtnl_from_file(fp, accept_tcmsg, stdout); |
| 99 |
fclose(fp); |
| 100 |
return ret; |
| 101 |
} |
| 102 |
|
| 103 |
if (rtnl_open(&rth, groups) < 0) |
| 104 |
exit(1); |
| 105 |
|
| 106 |
ll_init_map(&rth); |
| 107 |
|
| 108 |
if (rtnl_listen(&rth, accept_tcmsg, (void *)stdout) < 0) { |
| 109 |
rtnl_close(&rth); |
| 110 |
exit(2); |
| 111 |
} |
| 112 |
|
| 113 |
rtnl_close(&rth); |
| 114 |
exit(0); |
| 115 |
} |