Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 1 | /* |
Chris Webb | 9069817 | 2012-08-16 21:42:37 +0100 | [diff] [blame] | 2 | * brmonitor.c "bridge monitor" |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 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)); |
| 30 | int prefix_banner; |
| 31 | |
| 32 | static void usage(void) |
| 33 | { |
Cong Wang | 176659e | 2012-12-14 13:59:32 +0800 | [diff] [blame] | 34 | fprintf(stderr, "Usage: bridge monitor [file | link | fdb | mdb | all]\n"); |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 35 | exit(-1); |
| 36 | } |
| 37 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 38 | static int accept_msg(const struct sockaddr_nl *who, |
Nicolas Dichtel | 0628cdd | 2015-05-20 16:19:58 +0200 | [diff] [blame] | 39 | struct rtnl_ctrl_data *ctrl, |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 40 | struct nlmsghdr *n, void *arg) |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 41 | { |
| 42 | FILE *fp = arg; |
| 43 | |
| 44 | if (timestamp) |
| 45 | print_timestamp(fp); |
| 46 | |
| 47 | switch (n->nlmsg_type) { |
| 48 | case RTM_NEWLINK: |
| 49 | case RTM_DELLINK: |
| 50 | if (prefix_banner) |
| 51 | fprintf(fp, "[LINK]"); |
| 52 | |
| 53 | return print_linkinfo(who, n, arg); |
| 54 | |
| 55 | case RTM_NEWNEIGH: |
| 56 | case RTM_DELNEIGH: |
| 57 | if (prefix_banner) |
| 58 | fprintf(fp, "[NEIGH]"); |
| 59 | return print_fdb(who, n, arg); |
| 60 | |
Cong Wang | 4a4ee61 | 2012-12-11 22:23:10 +0000 | [diff] [blame] | 61 | case RTM_NEWMDB: |
| 62 | case RTM_DELMDB: |
| 63 | if (prefix_banner) |
| 64 | fprintf(fp, "[MDB]"); |
| 65 | return print_mdb(who, n, arg); |
| 66 | |
Vadim Kochan | 27b14f2 | 2015-01-13 20:14:23 +0200 | [diff] [blame] | 67 | case NLMSG_TSTAMP: |
Vadim Kochan | ddb1129 | 2015-01-13 20:14:24 +0200 | [diff] [blame] | 68 | print_nlmsg_timestamp(fp, n); |
| 69 | return 0; |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 70 | |
| 71 | default: |
| 72 | return 0; |
| 73 | } |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | int do_monitor(int argc, char **argv) |
| 77 | { |
| 78 | char *file = NULL; |
| 79 | unsigned groups = ~RTMGRP_TC; |
| 80 | int llink=0; |
| 81 | int lneigh=0; |
Cong Wang | 4a4ee61 | 2012-12-11 22:23:10 +0000 | [diff] [blame] | 82 | int lmdb=0; |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 83 | |
| 84 | rtnl_close(&rth); |
| 85 | |
| 86 | while (argc > 0) { |
| 87 | if (matches(*argv, "file") == 0) { |
| 88 | NEXT_ARG(); |
| 89 | file = *argv; |
| 90 | } else if (matches(*argv, "link") == 0) { |
| 91 | llink=1; |
| 92 | groups = 0; |
| 93 | } else if (matches(*argv, "fdb") == 0) { |
| 94 | lneigh = 1; |
| 95 | groups = 0; |
Cong Wang | 4a4ee61 | 2012-12-11 22:23:10 +0000 | [diff] [blame] | 96 | } else if (matches(*argv, "mdb") == 0) { |
| 97 | lmdb = 1; |
| 98 | groups = 0; |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 99 | } else if (strcmp(*argv, "all") == 0) { |
| 100 | groups = ~RTMGRP_TC; |
| 101 | prefix_banner=1; |
| 102 | } else if (matches(*argv, "help") == 0) { |
| 103 | usage(); |
| 104 | } else { |
Chris Webb | 9069817 | 2012-08-16 21:42:37 +0100 | [diff] [blame] | 105 | fprintf(stderr, "Argument \"%s\" is unknown, try \"bridge monitor help\".\n", *argv); |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 106 | exit(-1); |
| 107 | } |
| 108 | argc--; argv++; |
| 109 | } |
| 110 | |
| 111 | if (llink) |
| 112 | groups |= nl_mgrp(RTNLGRP_LINK); |
| 113 | |
| 114 | if (lneigh) { |
| 115 | groups |= nl_mgrp(RTNLGRP_NEIGH); |
| 116 | } |
| 117 | |
Cong Wang | 4a4ee61 | 2012-12-11 22:23:10 +0000 | [diff] [blame] | 118 | if (lmdb) { |
| 119 | groups |= nl_mgrp(RTNLGRP_MDB); |
| 120 | } |
| 121 | |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 122 | if (file) { |
| 123 | FILE *fp; |
Petr Písař | 1018474 | 2013-09-25 09:45:45 +0200 | [diff] [blame] | 124 | int err; |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 125 | fp = fopen(file, "r"); |
| 126 | if (fp == NULL) { |
| 127 | perror("Cannot fopen"); |
| 128 | exit(-1); |
| 129 | } |
Petr Písař | 1018474 | 2013-09-25 09:45:45 +0200 | [diff] [blame] | 130 | err = rtnl_from_file(fp, accept_msg, stdout); |
| 131 | fclose(fp); |
| 132 | return err; |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 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 | } |
| 144 | |