]>
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)); | |
d63786c6 | 30 | static int prefix_banner; |
d04bc300 SH |
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 | ||
cd554f2c | 38 | static int accept_msg(struct rtnl_ctrl_data *ctrl, |
d1f28cf1 | 39 | struct nlmsghdr *n, void *arg) |
d04bc300 SH |
40 | { |
41 | FILE *fp = arg; | |
42 | ||
43 | if (timestamp) | |
44 | print_timestamp(fp); | |
45 | ||
46 | switch (n->nlmsg_type) { | |
47 | case RTM_NEWLINK: | |
48 | case RTM_DELLINK: | |
49 | if (prefix_banner) | |
50 | fprintf(fp, "[LINK]"); | |
51 | ||
cd554f2c | 52 | return print_linkinfo(n, arg); |
d04bc300 SH |
53 | |
54 | case RTM_NEWNEIGH: | |
55 | case RTM_DELNEIGH: | |
56 | if (prefix_banner) | |
57 | fprintf(fp, "[NEIGH]"); | |
cd554f2c | 58 | return print_fdb(n, arg); |
d04bc300 | 59 | |
4a4ee616 CW |
60 | case RTM_NEWMDB: |
61 | case RTM_DELMDB: | |
62 | if (prefix_banner) | |
63 | fprintf(fp, "[MDB]"); | |
cd554f2c | 64 | return print_mdb(n, arg); |
4a4ee616 | 65 | |
27b14f2e | 66 | case NLMSG_TSTAMP: |
ddb1129b VK |
67 | print_nlmsg_timestamp(fp, n); |
68 | return 0; | |
d04bc300 SH |
69 | |
70 | default: | |
71 | return 0; | |
72 | } | |
d04bc300 SH |
73 | } |
74 | ||
75 | int do_monitor(int argc, char **argv) | |
76 | { | |
77 | char *file = NULL; | |
df4b043f SH |
78 | unsigned int groups = ~RTMGRP_TC; |
79 | int llink = 0; | |
80 | int lneigh = 0; | |
81 | int lmdb = 0; | |
d04bc300 SH |
82 | |
83 | rtnl_close(&rth); | |
84 | ||
85 | while (argc > 0) { | |
86 | if (matches(*argv, "file") == 0) { | |
87 | NEXT_ARG(); | |
88 | file = *argv; | |
89 | } else if (matches(*argv, "link") == 0) { | |
df4b043f | 90 | llink = 1; |
d04bc300 SH |
91 | groups = 0; |
92 | } else if (matches(*argv, "fdb") == 0) { | |
93 | lneigh = 1; | |
94 | groups = 0; | |
4a4ee616 CW |
95 | } else if (matches(*argv, "mdb") == 0) { |
96 | lmdb = 1; | |
97 | groups = 0; | |
d04bc300 SH |
98 | } else if (strcmp(*argv, "all") == 0) { |
99 | groups = ~RTMGRP_TC; | |
df4b043f | 100 | prefix_banner = 1; |
d04bc300 SH |
101 | } else if (matches(*argv, "help") == 0) { |
102 | usage(); | |
103 | } else { | |
90698170 | 104 | fprintf(stderr, "Argument \"%s\" is unknown, try \"bridge monitor help\".\n", *argv); |
d04bc300 SH |
105 | exit(-1); |
106 | } | |
107 | argc--; argv++; | |
108 | } | |
109 | ||
110 | if (llink) | |
111 | groups |= nl_mgrp(RTNLGRP_LINK); | |
112 | ||
113 | if (lneigh) { | |
114 | groups |= nl_mgrp(RTNLGRP_NEIGH); | |
115 | } | |
116 | ||
4a4ee616 CW |
117 | if (lmdb) { |
118 | groups |= nl_mgrp(RTNLGRP_MDB); | |
119 | } | |
120 | ||
d04bc300 SH |
121 | if (file) { |
122 | FILE *fp; | |
10184744 | 123 | int err; |
df4b043f | 124 | |
d04bc300 SH |
125 | fp = fopen(file, "r"); |
126 | if (fp == NULL) { | |
127 | perror("Cannot fopen"); | |
128 | exit(-1); | |
129 | } | |
10184744 PP |
130 | err = rtnl_from_file(fp, accept_msg, stdout); |
131 | fclose(fp); | |
132 | return err; | |
d04bc300 SH |
133 | } |
134 | ||
135 | if (rtnl_open(&rth, groups) < 0) | |
136 | exit(1); | |
137 | ll_init_map(&rth); | |
138 | ||
139 | if (rtnl_listen(&rth, accept_msg, stdout) < 0) | |
140 | exit(2); | |
141 | ||
142 | return 0; | |
143 | } |