1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
/*
* monitor.c - netlink notification monitor
*
* Implementation of "ethtool --monitor" for watching netlink notifications.
*/
#include <errno.h>
#include "../internal.h"
#include "netlink.h"
#include "nlsock.h"
#include "strset.h"
static struct {
uint8_t cmd;
mnl_cb_t cb;
} monitor_callbacks[] = {
{
.cmd = ETHTOOL_MSG_LINKMODES_NTF,
.cb = linkmodes_reply_cb,
},
{
.cmd = ETHTOOL_MSG_LINKINFO_NTF,
.cb = linkinfo_reply_cb,
},
{
.cmd = ETHTOOL_MSG_WOL_NTF,
.cb = wol_reply_cb,
},
{
.cmd = ETHTOOL_MSG_DEBUG_NTF,
.cb = debug_reply_cb,
},
{
.cmd = ETHTOOL_MSG_FEATURES_NTF,
.cb = features_reply_cb,
},
{
.cmd = ETHTOOL_MSG_PRIVFLAGS_NTF,
.cb = privflags_reply_cb,
},
{
.cmd = ETHTOOL_MSG_RINGS_NTF,
.cb = rings_reply_cb,
},
{
.cmd = ETHTOOL_MSG_CHANNELS_NTF,
.cb = channels_reply_cb,
},
{
.cmd = ETHTOOL_MSG_COALESCE_NTF,
.cb = coalesce_reply_cb,
},
{
.cmd = ETHTOOL_MSG_PAUSE_NTF,
.cb = pause_reply_cb,
},
{
.cmd = ETHTOOL_MSG_EEE_NTF,
.cb = eee_reply_cb,
},
{
.cmd = ETHTOOL_MSG_CABLE_TEST_NTF,
.cb = cable_test_ntf_cb,
},
{
.cmd = ETHTOOL_MSG_CABLE_TEST_TDR_NTF,
.cb = cable_test_tdr_ntf_cb,
},
{
.cmd = ETHTOOL_MSG_FEC_NTF,
.cb = fec_reply_cb,
},
{
.cmd = ETHTOOL_MSG_MODULE_NTF,
.cb = module_reply_cb,
},
};
static void clear_filter(struct nl_context *nlctx)
{
unsigned int i;
for (i = 0; i < CMDMASK_WORDS; i++)
nlctx->filter_cmds[i] = 0;
}
static bool test_filter_cmd(const struct nl_context *nlctx, unsigned int cmd)
{
return nlctx->filter_cmds[cmd / 32] & (1U << (cmd % 32));
}
static void set_filter_cmd(struct nl_context *nlctx, unsigned int cmd)
{
nlctx->filter_cmds[cmd / 32] |= (1U << (cmd % 32));
}
static void set_filter_all(struct nl_context *nlctx)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(monitor_callbacks); i++)
set_filter_cmd(nlctx, monitor_callbacks[i].cmd);
}
static int monitor_any_cb(const struct nlmsghdr *nlhdr, void *data)
{
const struct genlmsghdr *ghdr = (const struct genlmsghdr *)(nlhdr + 1);
struct nl_context *nlctx = data;
unsigned int i;
if (!test_filter_cmd(nlctx, ghdr->cmd))
return MNL_CB_OK;
for (i = 0; i < MNL_ARRAY_SIZE(monitor_callbacks); i++)
if (monitor_callbacks[i].cmd == ghdr->cmd)
return monitor_callbacks[i].cb(nlhdr, data);
return MNL_CB_OK;
}
struct monitor_option {
const char *pattern;
uint8_t cmd;
uint32_t info_mask;
};
static struct monitor_option monitor_opts[] = {
{
.pattern = "|--all",
.cmd = 0,
},
{
.pattern = "-s|--change",
.cmd = ETHTOOL_MSG_LINKINFO_NTF,
},
{
.pattern = "-s|--change",
.cmd = ETHTOOL_MSG_LINKMODES_NTF,
},
{
.pattern = "-s|--change",
.cmd = ETHTOOL_MSG_WOL_NTF,
},
{
.pattern = "-s|--change",
.cmd = ETHTOOL_MSG_DEBUG_NTF,
},
{
.pattern = "-k|--show-features|--show-offload|-K|--features|--offload",
.cmd = ETHTOOL_MSG_FEATURES_NTF,
},
{
.pattern = "--show-priv-flags|--set-priv-flags",
.cmd = ETHTOOL_MSG_PRIVFLAGS_NTF,
},
{
.pattern = "-g|--show-ring|-G|--set-ring",
.cmd = ETHTOOL_MSG_RINGS_NTF,
},
{
.pattern = "-l|--show-channels|-L|--set-channels",
.cmd = ETHTOOL_MSG_CHANNELS_NTF,
},
{
.pattern = "-c|--show-coalesce|-C|--coalesce",
.cmd = ETHTOOL_MSG_COALESCE_NTF,
},
{
.pattern = "-a|--show-pause|-A|--pause",
.cmd = ETHTOOL_MSG_PAUSE_NTF,
},
{
.pattern = "--show-eee|--set-eee",
.cmd = ETHTOOL_MSG_EEE_NTF,
},
{
.pattern = "--cable-test",
.cmd = ETHTOOL_MSG_CABLE_TEST_NTF,
},
{
.pattern = "--cable-test-tdr",
.cmd = ETHTOOL_MSG_CABLE_TEST_TDR_NTF,
},
{
.pattern = "--show-module|--set-module",
.cmd = ETHTOOL_MSG_MODULE_NTF,
},
};
static bool pattern_match(const char *s, const char *pattern)
{
const char *opt = pattern;
const char *next;
int slen = strlen(s);
int optlen;
do {
next = opt;
while (*next && *next != '|')
next++;
optlen = next - opt;
if (slen == optlen && !strncmp(s, opt, optlen))
return true;
opt = next;
if (*opt == '|')
opt++;
} while (*opt);
return false;
}
static int parse_monitor(struct cmd_context *ctx)
{
struct nl_context *nlctx = ctx->nlctx;
char **argp = ctx->argp;
int argc = ctx->argc;
const char *opt = "";
bool opt_found;
unsigned int i;
if (*argp && argp[0][0] == '-') {
opt = *argp;
argp++;
argc--;
}
opt_found = false;
clear_filter(nlctx);
for (i = 0; i < MNL_ARRAY_SIZE(monitor_opts); i++) {
if (pattern_match(opt, monitor_opts[i].pattern)) {
unsigned int cmd = monitor_opts[i].cmd;
if (!cmd)
set_filter_all(nlctx);
else
set_filter_cmd(nlctx, cmd);
opt_found = true;
}
}
if (!opt_found) {
fprintf(stderr, "monitoring for option '%s' not supported\n",
*argp);
return -1;
}
if (*argp && strcmp(*argp, WILDCARD_DEVNAME))
ctx->devname = *argp;
return 0;
}
int nl_monitor(struct cmd_context *ctx)
{
struct nl_context *nlctx;
struct nl_socket *nlsk;
uint32_t grpid;
bool is_dev;
int ret;
ret = netlink_init(ctx);
if (ret < 0) {
fprintf(stderr, "Netlink interface initialization failed, option --monitor not supported.\n");
return ret;
}
nlctx = ctx->nlctx;
nlsk = nlctx->ethnl_socket;
grpid = nlctx->ethnl_mongrp;
if (!grpid) {
fprintf(stderr, "multicast group 'monitor' not found\n");
return -EOPNOTSUPP;
}
if (parse_monitor(ctx) < 0)
return 1;
is_dev = ctx->devname && strcmp(ctx->devname, WILDCARD_DEVNAME);
ret = preload_global_strings(nlsk);
if (ret < 0)
return ret;
ret = mnl_socket_setsockopt(nlsk->sk, NETLINK_ADD_MEMBERSHIP,
&grpid, sizeof(grpid));
if (ret < 0)
return ret;
if (is_dev) {
ret = preload_perdev_strings(nlsk, ctx->devname);
if (ret < 0)
goto out_strings;
}
nlctx->filter_devname = ctx->devname;
nlctx->is_monitor = true;
nlsk->port = 0;
nlsk->seq = 0;
fputs("listening...\n", stdout);
fflush(stdout);
ret = nlsock_process_reply(nlsk, monitor_any_cb, nlctx);
out_strings:
cleanup_all_strings();
return ret;
}
void nl_monitor_usage(void)
{
unsigned int i;
const char *p;
fputs(" ethtool --monitor Show kernel notifications\n",
stdout);
fputs(" ( [ --all ]", stdout);
for (i = 1; i < MNL_ARRAY_SIZE(monitor_opts); i++) {
if (!strcmp(monitor_opts[i].pattern, monitor_opts[i - 1].pattern))
continue;
fputs("\n | ", stdout);
for (p = monitor_opts[i].pattern; *p; p++)
if (*p == '|')
fputs(" | ", stdout);
else
fputc(*p, stdout);
}
fputs(" )\n", stdout);
fputs(" [ DEVNAME | * ]\n", stdout);
}
|