blob: 0efe0343db0becd181bb2d5376d8f9d0f0ca1b6a [file] [log] [blame]
Jamal Hadi Salim5bec3482006-08-08 11:55:15 -07001/*
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
29static void usage(void) __attribute__((noreturn));
30
31static void usage(void)
32{
33 fprintf(stderr, "Usage: tc monitor\n");
34 exit(-1);
35}
36
37
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -080038static int accept_tcmsg(const struct sockaddr_nl *who,
39 struct nlmsghdr *n, void *arg)
Jamal Hadi Salim5bec3482006-08-08 11:55:15 -070040{
41 FILE *fp = (FILE*)arg;
42
43 if (n->nlmsg_type == RTM_NEWTFILTER || n->nlmsg_type == RTM_DELTFILTER) {
44 print_filter(who, n, arg);
45 return 0;
46 }
47 if (n->nlmsg_type == RTM_NEWTCLASS || n->nlmsg_type == RTM_DELTCLASS) {
48 print_class(who, n, arg);
49 return 0;
50 }
51 if (n->nlmsg_type == RTM_NEWQDISC || n->nlmsg_type == RTM_DELQDISC) {
52 print_qdisc(who, 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(who, 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(fp, "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
68int do_tcmonitor(int argc, char **argv)
69{
70 struct rtnl_handle rth;
71 char *file = NULL;
jamalb64f58b2007-02-25 11:55:19 -050072 unsigned groups = nl_mgrp(RTNLGRP_TC);
Jamal Hadi Salim5bec3482006-08-08 11:55:15 -070073
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;
91 fp = fopen(file, "r");
92 if (fp == NULL) {
93 perror("Cannot fopen");
94 exit(-1);
95 }
96 return rtnl_from_file(fp, accept_tcmsg, (void*)stdout);
97 }
98
99 if (rtnl_open(&rth, groups) < 0)
100 exit(1);
101
102 ll_init_map(&rth);
103
104 if (rtnl_listen(&rth, accept_tcmsg, (void*)stdout) < 0) {
105 rtnl_close(&rth);
106 exit(2);
107 }
108
109 rtnl_close(&rth);
110 exit(0);
111}