blob: f00e0a6b942fb94db3326a55a2d6dcb618468d36 [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
Vadim Kochan27b14f22015-01-13 20:14:23 +020077 case NLMSG_TSTAMP:
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070078 return show_mark(fp, n);
79
80 default:
81 return 0;
82 }
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070083}
84
85int do_monitor(int argc, char **argv)
86{
87 char *file = NULL;
88 unsigned groups = ~RTMGRP_TC;
89 int llink=0;
90 int lneigh=0;
Cong Wang4a4ee612012-12-11 22:23:10 +000091 int lmdb=0;
Stephen Hemmingerd04bc302012-08-01 15:23:49 -070092
93 rtnl_close(&rth);
94
95 while (argc > 0) {
96 if (matches(*argv, "file") == 0) {
97 NEXT_ARG();
98 file = *argv;
99 } else if (matches(*argv, "link") == 0) {
100 llink=1;
101 groups = 0;
102 } else if (matches(*argv, "fdb") == 0) {
103 lneigh = 1;
104 groups = 0;
Cong Wang4a4ee612012-12-11 22:23:10 +0000105 } else if (matches(*argv, "mdb") == 0) {
106 lmdb = 1;
107 groups = 0;
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700108 } else if (strcmp(*argv, "all") == 0) {
109 groups = ~RTMGRP_TC;
110 prefix_banner=1;
111 } else if (matches(*argv, "help") == 0) {
112 usage();
113 } else {
Chris Webb90698172012-08-16 21:42:37 +0100114 fprintf(stderr, "Argument \"%s\" is unknown, try \"bridge monitor help\".\n", *argv);
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700115 exit(-1);
116 }
117 argc--; argv++;
118 }
119
120 if (llink)
121 groups |= nl_mgrp(RTNLGRP_LINK);
122
123 if (lneigh) {
124 groups |= nl_mgrp(RTNLGRP_NEIGH);
125 }
126
Cong Wang4a4ee612012-12-11 22:23:10 +0000127 if (lmdb) {
128 groups |= nl_mgrp(RTNLGRP_MDB);
129 }
130
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700131 if (file) {
132 FILE *fp;
Petr Písař10184742013-09-25 09:45:45 +0200133 int err;
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700134 fp = fopen(file, "r");
135 if (fp == NULL) {
136 perror("Cannot fopen");
137 exit(-1);
138 }
Petr Písař10184742013-09-25 09:45:45 +0200139 err = rtnl_from_file(fp, accept_msg, stdout);
140 fclose(fp);
141 return err;
Stephen Hemmingerd04bc302012-08-01 15:23:49 -0700142 }
143
144 if (rtnl_open(&rth, groups) < 0)
145 exit(1);
146 ll_init_map(&rth);
147
148 if (rtnl_listen(&rth, accept_msg, stdout) < 0)
149 exit(2);
150
151 return 0;
152}
153