Jamal Hadi Salim | 5bec348 | 2006-08-08 11:55:15 -0700 | [diff] [blame] | 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 | { |
Eric Dumazet | 32a6fbe | 2015-09-23 16:40:04 -0700 | [diff] [blame] | 33 | fprintf(stderr, "Usage: tc [-timestamp [-tshort] monitor\n"); |
Jamal Hadi Salim | 5bec348 | 2006-08-08 11:55:15 -0700 | [diff] [blame] | 34 | exit(-1); |
| 35 | } |
| 36 | |
| 37 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 38 | static int accept_tcmsg(const struct sockaddr_nl *who, |
Nicolas Dichtel | 0628cdd | 2015-05-20 16:19:58 +0200 | [diff] [blame] | 39 | struct rtnl_ctrl_data *ctrl, |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 40 | struct nlmsghdr *n, void *arg) |
Jamal Hadi Salim | 5bec348 | 2006-08-08 11:55:15 -0700 | [diff] [blame] | 41 | { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 42 | FILE *fp = (FILE *)arg; |
Jamal Hadi Salim | 5bec348 | 2006-08-08 11:55:15 -0700 | [diff] [blame] | 43 | |
Eric Dumazet | 32a6fbe | 2015-09-23 16:40:04 -0700 | [diff] [blame] | 44 | if (timestamp) |
| 45 | print_timestamp(fp); |
| 46 | |
Jamal Hadi Salim | 5bec348 | 2006-08-08 11:55:15 -0700 | [diff] [blame] | 47 | 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 | |
| 72 | int do_tcmonitor(int argc, char **argv) |
| 73 | { |
| 74 | struct rtnl_handle rth; |
| 75 | char *file = NULL; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 76 | unsigned int groups = nl_mgrp(RTNLGRP_TC); |
Jamal Hadi Salim | 5bec348 | 2006-08-08 11:55:15 -0700 | [diff] [blame] | 77 | |
| 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 Hemminger | e49b51d | 2015-12-30 17:19:04 -0800 | [diff] [blame] | 94 | FILE *fp = fopen(file, "r"); |
| 95 | int ret; |
| 96 | |
Jamal Hadi Salim | 5bec348 | 2006-08-08 11:55:15 -0700 | [diff] [blame] | 97 | if (fp == NULL) { |
| 98 | perror("Cannot fopen"); |
| 99 | exit(-1); |
| 100 | } |
Stephen Hemminger | e49b51d | 2015-12-30 17:19:04 -0800 | [diff] [blame] | 101 | |
| 102 | ret = rtnl_from_file(fp, accept_tcmsg, stdout); |
| 103 | fclose(fp); |
| 104 | return ret; |
Jamal Hadi Salim | 5bec348 | 2006-08-08 11:55:15 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | if (rtnl_open(&rth, groups) < 0) |
| 108 | exit(1); |
| 109 | |
| 110 | ll_init_map(&rth); |
| 111 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 112 | if (rtnl_listen(&rth, accept_tcmsg, (void *)stdout) < 0) { |
Jamal Hadi Salim | 5bec348 | 2006-08-08 11:55:15 -0700 | [diff] [blame] | 113 | rtnl_close(&rth); |
| 114 | exit(2); |
| 115 | } |
| 116 | |
| 117 | rtnl_close(&rth); |
| 118 | exit(0); |
| 119 | } |