blob: 83142cb21f5440c00b8d5dc651e6a3303ec237b1 [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{
Eric Dumazet32a6fbe2015-09-23 16:40:04 -070033 fprintf(stderr, "Usage: tc [-timestamp [-tshort] monitor\n");
Jamal Hadi Salim5bec3482006-08-08 11:55:15 -070034 exit(-1);
35}
36
37
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -080038static int accept_tcmsg(const struct sockaddr_nl *who,
Nicolas Dichtel0628cdd2015-05-20 16:19:58 +020039 struct rtnl_ctrl_data *ctrl,
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -080040 struct nlmsghdr *n, void *arg)
Jamal Hadi Salim5bec3482006-08-08 11:55:15 -070041{
Stephen Hemminger32a121c2016-03-21 11:48:36 -070042 FILE *fp = (FILE *)arg;
Jamal Hadi Salim5bec3482006-08-08 11:55:15 -070043
Eric Dumazet32a6fbe2015-09-23 16:40:04 -070044 if (timestamp)
45 print_timestamp(fp);
46
Jamal Hadi Salim5bec3482006-08-08 11:55:15 -070047 if (n->nlmsg_type == RTM_NEWTFILTER || n->nlmsg_type == RTM_DELTFILTER) {
48 print_filter(who, n, arg);
49 return 0;
50 }
51 if (n->nlmsg_type == RTM_NEWTCLASS || n->nlmsg_type == RTM_DELTCLASS) {
52 print_class(who, n, arg);
53 return 0;
54 }
55 if (n->nlmsg_type == RTM_NEWQDISC || n->nlmsg_type == RTM_DELQDISC) {
56 print_qdisc(who, n, arg);
57 return 0;
58 }
59 if (n->nlmsg_type == RTM_GETACTION || n->nlmsg_type == RTM_NEWACTION ||
60 n->nlmsg_type == RTM_DELACTION) {
61 print_action(who, n, arg);
62 return 0;
63 }
64 if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP &&
65 n->nlmsg_type != NLMSG_DONE) {
66 fprintf(fp, "Unknown message: length %08d type %08x flags %08x\n",
67 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
68 }
69 return 0;
70}
71
72int do_tcmonitor(int argc, char **argv)
73{
74 struct rtnl_handle rth;
75 char *file = NULL;
Stephen Hemminger32a121c2016-03-21 11:48:36 -070076 unsigned int groups = nl_mgrp(RTNLGRP_TC);
Jamal Hadi Salim5bec3482006-08-08 11:55:15 -070077
78 while (argc > 0) {
79 if (matches(*argv, "file") == 0) {
80 NEXT_ARG();
81 file = *argv;
82 } else {
83 if (matches(*argv, "help") == 0) {
84 usage();
85 } else {
86 fprintf(stderr, "Argument \"%s\" is unknown, try \"tc monitor help\".\n", *argv);
87 exit(-1);
88 }
89 }
90 argc--; argv++;
91 }
92
93 if (file) {
Stephen Hemmingere49b51d2015-12-30 17:19:04 -080094 FILE *fp = fopen(file, "r");
95 int ret;
96
Jamal Hadi Salim5bec3482006-08-08 11:55:15 -070097 if (fp == NULL) {
98 perror("Cannot fopen");
99 exit(-1);
100 }
Stephen Hemmingere49b51d2015-12-30 17:19:04 -0800101
102 ret = rtnl_from_file(fp, accept_tcmsg, stdout);
103 fclose(fp);
104 return ret;
Jamal Hadi Salim5bec3482006-08-08 11:55:15 -0700105 }
106
107 if (rtnl_open(&rth, groups) < 0)
108 exit(1);
109
110 ll_init_map(&rth);
111
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700112 if (rtnl_listen(&rth, accept_tcmsg, (void *)stdout) < 0) {
Jamal Hadi Salim5bec3482006-08-08 11:55:15 -0700113 rtnl_close(&rth);
114 exit(2);
115 }
116
117 rtnl_close(&rth);
118 exit(0);
119}