]> git.proxmox.com Git - mirror_ovs.git/blame - utilities/nlmon.c
treewide: Convert leading tabs to spaces.
[mirror_ovs.git] / utilities / nlmon.c
CommitLineData
a14bc59f 1/*
e0edde6f 2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
a14bc59f
BP
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
064af421
BP
17#include <config.h>
18#include <errno.h>
19#include <inttypes.h>
20#include <net/if.h>
21#include <poll.h>
22#include <sys/socket.h>
23#include <sys/uio.h>
24#include <stddef.h>
25#include <linux/rtnetlink.h>
26#include "netlink.h"
2fe27d5a 27#include "netlink-socket.h"
6abfc34d 28#include "netnsid.h"
64c96779 29#include "openvswitch/ofpbuf.h"
fd016ae3 30#include "openvswitch/poll-loop.h"
064af421
BP
31#include "timeval.h"
32#include "util.h"
e6211adc 33#include "openvswitch/vlog.h"
064af421
BP
34
35static const struct nl_policy rtnlgrp_link_policy[] = {
36 [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
37 [IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
38};
39
40int
67a4917b 41main(int argc OVS_UNUSED, char *argv[])
064af421 42{
72d32ac0 43 uint64_t buf_stub[4096 / 64];
064af421 44 struct nl_sock *sock;
6abfc34d 45 int nsid;
72d32ac0 46 struct ofpbuf buf;
064af421
BP
47 int error;
48
49 set_program_name(argv[0]);
d5460484 50 vlog_set_levels(NULL, VLF_ANY_DESTINATION, VLL_DBG);
064af421 51
cceb11f5 52 error = nl_sock_create(NETLINK_ROUTE, &sock);
064af421
BP
53 if (error) {
54 ovs_fatal(error, "could not create rtnetlink socket");
55 }
56
cceb11f5
BP
57 error = nl_sock_join_mcgroup(sock, RTNLGRP_LINK);
58 if (error) {
59 ovs_fatal(error, "could not join RTNLGRP_LINK multicast group");
60 }
61
6abfc34d 62 nl_sock_listen_all_nsid(sock, true);
72d32ac0 63 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
064af421 64 for (;;) {
6abfc34d 65 error = nl_sock_recv(sock, &buf, &nsid, false);
064af421
BP
66 if (error == EAGAIN) {
67 /* Nothing to do. */
68 } else if (error == ENOBUFS) {
69 ovs_error(0, "network monitor socket overflowed");
70 } else if (error) {
71 ovs_fatal(error, "error on network monitor socket");
72 } else {
d8256121
BP
73 struct iff_flag {
74 unsigned int flag;
75 const char *name;
76 };
77
78 static const struct iff_flag flags[] = {
79 { IFF_UP, "UP", },
80 { IFF_BROADCAST, "BROADCAST", },
81 { IFF_DEBUG, "DEBUG", },
82 { IFF_LOOPBACK, "LOOPBACK", },
83 { IFF_POINTOPOINT, "POINTOPOINT", },
84 { IFF_NOTRAILERS, "NOTRAILERS", },
85 { IFF_RUNNING, "RUNNING", },
86 { IFF_NOARP, "NOARP", },
87 { IFF_PROMISC, "PROMISC", },
88 { IFF_ALLMULTI, "ALLMULTI", },
89 { IFF_MASTER, "MASTER", },
90 { IFF_SLAVE, "SLAVE", },
91 { IFF_MULTICAST, "MULTICAST", },
92 { IFF_PORTSEL, "PORTSEL", },
93 { IFF_AUTOMEDIA, "AUTOMEDIA", },
94 { IFF_DYNAMIC, "DYNAMIC", },
95 };
96
064af421
BP
97 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
98 struct nlmsghdr *nlh;
99 struct ifinfomsg *iim;
d8256121 100 int i;
064af421 101
72d32ac0
BP
102 nlh = ofpbuf_at(&buf, 0, NLMSG_HDRLEN);
103 iim = ofpbuf_at(&buf, NLMSG_HDRLEN, sizeof *iim);
064af421
BP
104 if (!iim) {
105 ovs_error(0, "received bad rtnl message (no ifinfomsg)");
064af421
BP
106 continue;
107 }
108
72d32ac0 109 if (!nl_policy_parse(&buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
064af421
BP
110 rtnlgrp_link_policy,
111 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
112 ovs_error(0, "received bad rtnl message (policy)");
064af421
BP
113 continue;
114 }
115 printf("netdev %s changed (%s):\n",
116 nl_attr_get_string(attrs[IFLA_IFNAME]),
117 (nlh->nlmsg_type == RTM_NEWLINK ? "RTM_NEWLINK"
118 : nlh->nlmsg_type == RTM_DELLINK ? "RTM_DELLINK"
119 : nlh->nlmsg_type == RTM_GETLINK ? "RTM_GETLINK"
120 : nlh->nlmsg_type == RTM_SETLINK ? "RTM_SETLINK"
121 : "other"));
5a0e4aec 122 printf(" flags:");
d8256121
BP
123 for (i = 0; i < ARRAY_SIZE(flags); i++) {
124 if (iim->ifi_flags & flags[i].flag) {
125 printf(" %s", flags[i].name);
126 }
127 }
128 printf("\n");
6abfc34d 129 if (netnsid_is_remote(nsid)) {
5a0e4aec 130 printf(" netns id: %d\n", nsid);
6abfc34d 131 } else {
5a0e4aec 132 printf(" netns id: local\n");
6abfc34d 133 }
064af421
BP
134 if (attrs[IFLA_MASTER]) {
135 uint32_t idx = nl_attr_get_u32(attrs[IFLA_MASTER]);
136 char ifname[IFNAMSIZ];
137 if (!if_indextoname(idx, ifname)) {
138 strcpy(ifname, "unknown");
139 }
5a0e4aec 140 printf(" master=%"PRIu32" (%s)\n", idx, ifname);
064af421 141 }
064af421
BP
142 }
143
144 nl_sock_wait(sock, POLLIN);
145 poll_block();
146 }
147}
148