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 [-timestamp [-tshort] 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 (timestamp) 45 print_timestamp(fp); 46 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; 76 unsigned int groups = nl_mgrp(RTNLGRP_TC); 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) { 94 FILE *fp = fopen(file, "r"); 95 int ret; 96 97 if (fp == NULL) { 98 perror("Cannot fopen"); 99 exit(-1); 100 } 101 102 ret = rtnl_from_file(fp, accept_tcmsg, stdout); 103 fclose(fp); 104 return ret; 105 } 106 107 if (rtnl_open(&rth, groups) < 0) 108 exit(1); 109 110 ll_init_map(&rth); 111 112 if (rtnl_listen(&rth, accept_tcmsg, (void *)stdout) < 0) { 113 rtnl_close(&rth); 114 exit(2); 115 } 116 117 rtnl_close(&rth); 118 exit(0); 119 } 120