]> git.proxmox.com Git - mirror_iproute2.git/blob - bridge/monitor.c
ip: make local functions static
[mirror_iproute2.git] / bridge / monitor.c
1 /*
2 * brmonitor.c "bridge 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: Stephen Hemminger <shemminger@vyatta.com>
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <time.h>
17 #include <sys/socket.h>
18 #include <sys/time.h>
19 #include <net/if.h>
20 #include <netinet/in.h>
21 #include <linux/if_bridge.h>
22 #include <linux/neighbour.h>
23 #include <string.h>
24
25 #include "utils.h"
26 #include "br_common.h"
27
28
29 static void usage(void) __attribute__((noreturn));
30 int prefix_banner;
31
32 static void usage(void)
33 {
34 fprintf(stderr, "Usage: bridge monitor [file | link | fdb | mdb | all]\n");
35 exit(-1);
36 }
37
38 static int show_mark(FILE *fp, const struct nlmsghdr *n)
39 {
40 char *tstr;
41 time_t secs = ((__u32*)NLMSG_DATA(n))[0];
42 long usecs = ((__u32*)NLMSG_DATA(n))[1];
43 tstr = asctime(localtime(&secs));
44 tstr[strlen(tstr)-1] = 0;
45 fprintf(fp, "Timestamp: %s %lu us\n", tstr, usecs);
46 return 0;
47 }
48
49 static int accept_msg(const struct sockaddr_nl *who,
50 struct nlmsghdr *n, void *arg)
51 {
52 FILE *fp = arg;
53
54 if (timestamp)
55 print_timestamp(fp);
56
57 switch (n->nlmsg_type) {
58 case RTM_NEWLINK:
59 case RTM_DELLINK:
60 if (prefix_banner)
61 fprintf(fp, "[LINK]");
62
63 return print_linkinfo(who, n, arg);
64
65 case RTM_NEWNEIGH:
66 case RTM_DELNEIGH:
67 if (prefix_banner)
68 fprintf(fp, "[NEIGH]");
69 return print_fdb(who, n, arg);
70
71 case RTM_NEWMDB:
72 case RTM_DELMDB:
73 if (prefix_banner)
74 fprintf(fp, "[MDB]");
75 return print_mdb(who, n, arg);
76
77 case 15:
78 return show_mark(fp, n);
79
80 default:
81 return 0;
82 }
83
84
85 }
86
87 int do_monitor(int argc, char **argv)
88 {
89 char *file = NULL;
90 unsigned groups = ~RTMGRP_TC;
91 int llink=0;
92 int lneigh=0;
93 int lmdb=0;
94
95 rtnl_close(&rth);
96
97 while (argc > 0) {
98 if (matches(*argv, "file") == 0) {
99 NEXT_ARG();
100 file = *argv;
101 } else if (matches(*argv, "link") == 0) {
102 llink=1;
103 groups = 0;
104 } else if (matches(*argv, "fdb") == 0) {
105 lneigh = 1;
106 groups = 0;
107 } else if (matches(*argv, "mdb") == 0) {
108 lmdb = 1;
109 groups = 0;
110 } else if (strcmp(*argv, "all") == 0) {
111 groups = ~RTMGRP_TC;
112 prefix_banner=1;
113 } else if (matches(*argv, "help") == 0) {
114 usage();
115 } else {
116 fprintf(stderr, "Argument \"%s\" is unknown, try \"bridge monitor help\".\n", *argv);
117 exit(-1);
118 }
119 argc--; argv++;
120 }
121
122 if (llink)
123 groups |= nl_mgrp(RTNLGRP_LINK);
124
125 if (lneigh) {
126 groups |= nl_mgrp(RTNLGRP_NEIGH);
127 }
128
129 if (lmdb) {
130 groups |= nl_mgrp(RTNLGRP_MDB);
131 }
132
133 if (file) {
134 FILE *fp;
135 fp = fopen(file, "r");
136 if (fp == NULL) {
137 perror("Cannot fopen");
138 exit(-1);
139 }
140 return rtnl_from_file(fp, accept_msg, stdout);
141 }
142
143 if (rtnl_open(&rth, groups) < 0)
144 exit(1);
145 ll_init_map(&rth);
146
147 if (rtnl_listen(&rth, accept_msg, stdout) < 0)
148 exit(2);
149
150 return 0;
151 }
152