blob: 9e1ed48c51419250f300759520aab1fe6f15eb81 [file] [log] [blame]
Stephen Hemmingerd04bc302012-08-01 15:23:49 -07001/*
Chris Webb90698172012-08-16 21:42:37 +01002 * brmonitor.c "bridge monitor"
Stephen Hemmingerd04bc302012-08-01 15:23:49 -07003 *
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
29static void usage(void) __attribute__((noreturn));
30int prefix_banner;
31
32static void usage(void)
33{
Cong Wang176659e2012-12-14 13:59:32 +080034 fprintf(stderr, "Usage: bridge monitor [file | link | fdb | mdb | all]\n");
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070035 exit(-1);
36}
37
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -080038static int accept_msg(const struct sockaddr_nl *who,
39 struct nlmsghdr *n, void *arg)
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070040{
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
52 return print_linkinfo(who, n, arg);
53
54 case RTM_NEWNEIGH:
55 case RTM_DELNEIGH:
56 if (prefix_banner)
57 fprintf(fp, "[NEIGH]");
58 return print_fdb(who, n, arg);
59
Cong Wang4a4ee612012-12-11 22:23:10 +000060 case RTM_NEWMDB:
61 case RTM_DELMDB:
62 if (prefix_banner)
63 fprintf(fp, "[MDB]");
64 return print_mdb(who, n, arg);
65
Vadim Kochan27b14f22015-01-13 20:14:23 +020066 case NLMSG_TSTAMP:
Vadim Kochanddb11292015-01-13 20:14:24 +020067 print_nlmsg_timestamp(fp, n);
68 return 0;
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070069
70 default:
71 return 0;
72 }
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070073}
74
75int do_monitor(int argc, char **argv)
76{
77 char *file = NULL;
78 unsigned groups = ~RTMGRP_TC;
79 int llink=0;
80 int lneigh=0;
Cong Wang4a4ee612012-12-11 22:23:10 +000081 int lmdb=0;
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070082
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) {
90 llink=1;
91 groups = 0;
92 } else if (matches(*argv, "fdb") == 0) {
93 lneigh = 1;
94 groups = 0;
Cong Wang4a4ee612012-12-11 22:23:10 +000095 } else if (matches(*argv, "mdb") == 0) {
96 lmdb = 1;
97 groups = 0;
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070098 } else if (strcmp(*argv, "all") == 0) {
99 groups = ~RTMGRP_TC;
100 prefix_banner=1;
101 } else if (matches(*argv, "help") == 0) {
102 usage();
103 } else {
Chris Webb90698172012-08-16 21:42:37 +0100104 fprintf(stderr, "Argument \"%s\" is unknown, try \"bridge monitor help\".\n", *argv);
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700105 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
Cong Wang4a4ee612012-12-11 22:23:10 +0000117 if (lmdb) {
118 groups |= nl_mgrp(RTNLGRP_MDB);
119 }
120
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700121 if (file) {
122 FILE *fp;
Petr Písař10184742013-09-25 09:45:45 +0200123 int err;
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700124 fp = fopen(file, "r");
125 if (fp == NULL) {
126 perror("Cannot fopen");
127 exit(-1);
128 }
Petr Písař10184742013-09-25 09:45:45 +0200129 err = rtnl_from_file(fp, accept_msg, stdout);
130 fclose(fp);
131 return err;
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700132 }
133
134 if (rtnl_open(&rth, groups) < 0)
135 exit(1);
136 ll_init_map(&rth);
137
138 if (rtnl_listen(&rth, accept_msg, stdout) < 0)
139 exit(2);
140
141 return 0;
142}
143