blob: 37468e6e87ce910fec252cd7c9c59fd8faa1d137 [file] [log] [blame]
Stephen Hemmingerd04bc302012-08-01 15:23:49 -07001/*
2 * brmonitor.c "br monitor"
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
29static void usage(void) __attribute__((noreturn));
30int prefix_banner;
31
32static void usage(void)
33{
34 fprintf(stderr, "Usage: br monitor\n");
35 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
49int accept_msg(const struct sockaddr_nl *who,
50 struct nlmsghdr *n, void *arg)
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
71 case 15:
72 return show_mark(fp, n);
73
74 default:
75 return 0;
76 }
77
78
79}
80
81int do_monitor(int argc, char **argv)
82{
83 char *file = NULL;
84 unsigned groups = ~RTMGRP_TC;
85 int llink=0;
86 int lneigh=0;
87
88 rtnl_close(&rth);
89
90 while (argc > 0) {
91 if (matches(*argv, "file") == 0) {
92 NEXT_ARG();
93 file = *argv;
94 } else if (matches(*argv, "link") == 0) {
95 llink=1;
96 groups = 0;
97 } else if (matches(*argv, "fdb") == 0) {
98 lneigh = 1;
99 groups = 0;
100 } else if (strcmp(*argv, "all") == 0) {
101 groups = ~RTMGRP_TC;
102 prefix_banner=1;
103 } else if (matches(*argv, "help") == 0) {
104 usage();
105 } else {
106 fprintf(stderr, "Argument \"%s\" is unknown, try \"br monitor help\".\n", *argv);
107 exit(-1);
108 }
109 argc--; argv++;
110 }
111
112 if (llink)
113 groups |= nl_mgrp(RTNLGRP_LINK);
114
115 if (lneigh) {
116 groups |= nl_mgrp(RTNLGRP_NEIGH);
117 }
118
119 if (file) {
120 FILE *fp;
121 fp = fopen(file, "r");
122 if (fp == NULL) {
123 perror("Cannot fopen");
124 exit(-1);
125 }
126 return rtnl_from_file(fp, accept_msg, stdout);
127 }
128
129 if (rtnl_open(&rth, groups) < 0)
130 exit(1);
131 ll_init_map(&rth);
132
133 if (rtnl_listen(&rth, accept_msg, stdout) < 0)
134 exit(2);
135
136 return 0;
137}
138