]>
Commit | Line | Data |
---|---|---|
d04bc300 | 1 | /* |
90698170 | 2 | * brmonitor.c "bridge monitor" |
d04bc300 SH |
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 | { | |
176659e3 | 34 | fprintf(stderr, "Usage: bridge monitor [file | link | fdb | mdb | all]\n"); |
d04bc300 SH |
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 | ||
d1f28cf1 SH |
49 | static int accept_msg(const struct sockaddr_nl *who, |
50 | struct nlmsghdr *n, void *arg) | |
d04bc300 SH |
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 | ||
4a4ee616 CW |
71 | case RTM_NEWMDB: |
72 | case RTM_DELMDB: | |
73 | if (prefix_banner) | |
74 | fprintf(fp, "[MDB]"); | |
75 | return print_mdb(who, n, arg); | |
76 | ||
d04bc300 SH |
77 | case 15: |
78 | return show_mark(fp, n); | |
79 | ||
80 | default: | |
81 | return 0; | |
82 | } | |
38df7ac9 | 83 | |
d04bc300 SH |
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; | |
4a4ee616 | 93 | int lmdb=0; |
d04bc300 SH |
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; | |
4a4ee616 CW |
107 | } else if (matches(*argv, "mdb") == 0) { |
108 | lmdb = 1; | |
109 | groups = 0; | |
d04bc300 SH |
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 { | |
90698170 | 116 | fprintf(stderr, "Argument \"%s\" is unknown, try \"bridge monitor help\".\n", *argv); |
d04bc300 SH |
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 | ||
4a4ee616 CW |
129 | if (lmdb) { |
130 | groups |= nl_mgrp(RTNLGRP_MDB); | |
131 | } | |
132 | ||
d04bc300 SH |
133 | if (file) { |
134 | FILE *fp; | |
10184744 | 135 | int err; |
d04bc300 SH |
136 | fp = fopen(file, "r"); |
137 | if (fp == NULL) { | |
138 | perror("Cannot fopen"); | |
139 | exit(-1); | |
140 | } | |
10184744 PP |
141 | err = rtnl_from_file(fp, accept_msg, stdout); |
142 | fclose(fp); | |
143 | return err; | |
d04bc300 SH |
144 | } |
145 | ||
146 | if (rtnl_open(&rth, groups) < 0) | |
147 | exit(1); | |
148 | ll_init_map(&rth); | |
149 | ||
150 | if (rtnl_listen(&rth, accept_msg, stdout) < 0) | |
151 | exit(2); | |
152 | ||
153 | return 0; | |
154 | } | |
155 |