blob: 76e7d477be662a59339e501bd59c89f33c07c8aa [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
38static 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
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -080049static int accept_msg(const struct sockaddr_nl *who,
50 struct nlmsghdr *n, void *arg)
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070051{
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
Cong Wang4a4ee612012-12-11 22:23:10 +000071 case RTM_NEWMDB:
72 case RTM_DELMDB:
73 if (prefix_banner)
74 fprintf(fp, "[MDB]");
75 return print_mdb(who, n, arg);
76
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070077 case 15:
78 return show_mark(fp, n);
79
80 default:
81 return 0;
82 }
Stephen Hemminger38df7ac2012-10-29 17:48:55 -070083
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070084
85}
86
87int do_monitor(int argc, char **argv)
88{
89 char *file = NULL;
90 unsigned groups = ~RTMGRP_TC;
91 int llink=0;
92 int lneigh=0;
Cong Wang4a4ee612012-12-11 22:23:10 +000093 int lmdb=0;
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070094
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;
Cong Wang4a4ee612012-12-11 22:23:10 +0000107 } else if (matches(*argv, "mdb") == 0) {
108 lmdb = 1;
109 groups = 0;
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700110 } 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 {
Chris Webb90698172012-08-16 21:42:37 +0100116 fprintf(stderr, "Argument \"%s\" is unknown, try \"bridge monitor help\".\n", *argv);
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700117 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
Cong Wang4a4ee612012-12-11 22:23:10 +0000129 if (lmdb) {
130 groups |= nl_mgrp(RTNLGRP_MDB);
131 }
132
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700133 if (file) {
134 FILE *fp;
Petr Písař10184742013-09-25 09:45:45 +0200135 int err;
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700136 fp = fopen(file, "r");
137 if (fp == NULL) {
138 perror("Cannot fopen");
139 exit(-1);
140 }
Petr Písař10184742013-09-25 09:45:45 +0200141 err = rtnl_from_file(fp, accept_msg, stdout);
142 fclose(fp);
143 return err;
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700144 }
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