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