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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
|
/*
* teamnl.c - Team device Netlink tool
* Copyright (C) 2012-2015 Jiri Pirko <jiri@resnulli.us>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <sys/signalfd.h>
#include <signal.h>
#include <unistd.h>
#include <team.h>
#include <private/misc.h>
#define CMD_PARAM_MAX_CNT 8
struct cmd_ctx {
int argc;
char **argv;
char *port_devname_arg;
char *array_index_arg;
bool port_ifindex_present;
uint32_t port_ifindex;
bool array_index_present;
uint32_t array_index;
};
typedef int (*run_cmd_t)(char *cmd_name, struct team_handle *th,
struct cmd_ctx *cmd_ctx);
struct cmd_type {
char *name;
char *params[CMD_PARAM_MAX_CNT];
run_cmd_t run_cmd;
};
static int run_cmd_ports(char *cmd_name, struct team_handle *th,
struct cmd_ctx *cmd_ctx)
{
struct team_port *port;
char buf[120];
bool trunc;
team_for_each_port(port, th) {
trunc = team_port_str(port, buf, sizeof(buf));
printf("%s %s\n", buf, trunc ? "<trunc>" : "");
}
return 0;
}
static int run_cmd_options(char *cmd_name, struct team_handle *th,
struct cmd_ctx *cmd_ctx)
{
struct team_option *option;
char buf[120];
bool trunc;
team_for_each_option(option, th) {
trunc = team_option_str(th, option, buf, sizeof(buf));
printf("%s %s\n", buf, trunc ? "<trunc>" : "");
}
return 0;
}
static struct team_option *__find_option(struct team_handle *th, char *opt_name,
struct cmd_ctx *cmd_ctx)
{
if (cmd_ctx->array_index_present && cmd_ctx->port_ifindex_present)
return team_get_option(th, "npa", opt_name,
cmd_ctx->port_ifindex,
cmd_ctx->array_index);
else if (cmd_ctx->array_index_present)
return team_get_option(th, "na", opt_name,
cmd_ctx->array_index);
else if (cmd_ctx->port_ifindex_present)
return team_get_option(th, "np", opt_name,
cmd_ctx->port_ifindex);
else
return team_get_option(th, "n", opt_name);
}
#define BUFSIZSTEP 1024
static int run_cmd_getoption(char *cmd_name, struct team_handle *th,
struct cmd_ctx *cmd_ctx)
{
struct team_option *option;
char *buf = NULL;
char *tmpbuf;
size_t bufsiz = 0;
bool trunc;
if (cmd_ctx->argc < 1) {
fprintf(stderr, "%s: Option name as a command line parameter expected.\n",
cmd_name);
return -EINVAL;
}
option = __find_option(th, cmd_ctx->argv[0], cmd_ctx);
if (!option)
return -ENOENT;
do {
bufsiz += BUFSIZSTEP;
tmpbuf = realloc(buf, bufsiz);
if (!tmpbuf) {
free(buf);
return -ENOMEM;
}
buf = tmpbuf;
trunc = team_option_value_str(option, buf, bufsiz);
} while(trunc);
printf("%s\n", buf);
free(buf);
return 0;
}
static int run_cmd_setoption(char *cmd_name, struct team_handle *th,
struct cmd_ctx *cmd_ctx)
{
struct team_option *option;
if (cmd_ctx->argc < 1) {
fprintf(stderr, "%s: Option name as a command line parameter expected.\n",
cmd_name);
return -EINVAL;
}
if (cmd_ctx->argc < 2) {
fprintf(stderr, "%s: Option value as a command line parameter expected.\n",
cmd_name);
return -EINVAL;
}
option = __find_option(th, cmd_ctx->argv[0], cmd_ctx);
if (!option)
return -ENOENT;
return team_set_option_value_from_string(th, option, cmd_ctx->argv[1]);
}
static int run_main_loop(struct team_handle *th)
{
fd_set rfds;
fd_set rfds_tmp;
int fdmax;
int ret;
sigset_t mask;
int sfd;
int tfd;
int err = 0;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGQUIT);
ret = sigprocmask(SIG_BLOCK, &mask, NULL);
if (ret == -1) {
fprintf(stderr, "Failed to set blocked signals\n");
return -errno;
}
sfd = signalfd(-1, &mask, 0);
if (sfd == -1) {
fprintf(stderr, "Failed to open signalfd\n");
return -errno;
}
FD_ZERO(&rfds);
FD_SET(sfd, &rfds);
fdmax = sfd;
tfd = team_get_event_fd(th);
FD_SET(tfd, &rfds);
if (tfd > fdmax)
fdmax = tfd;
fdmax++;
for (;;) {
rfds_tmp = rfds;
ret = select(fdmax, &rfds_tmp, NULL, NULL, NULL);
if (ret == -1) {
fprintf(stderr, "Select failed\n");
err = -errno;
goto out;
}
if (FD_ISSET(sfd, &rfds_tmp)) {
struct signalfd_siginfo fdsi;
ssize_t len;
len = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
if (len != sizeof(struct signalfd_siginfo)) {
fprintf(stderr, "Unexpected data length came from signalfd\n");
err = -EINVAL;
goto out;
}
switch (fdsi.ssi_signo) {
case SIGINT:
case SIGQUIT:
case SIGTERM:
goto out;
default:
fprintf(stderr, "Read unexpected signal\n");
err = -EINVAL;
goto out;
}
}
if (FD_ISSET(tfd, &rfds_tmp)) {
err = team_handle_events(th);
if (err) {
fprintf(stderr, "Team handle events failed\n");
return err;
}
}
}
out:
close(sfd);
return err;
}
enum {
MONITOR_STYLE_CHANGED,
MONITOR_STYLE_ALL,
};
struct monitor_priv {
unsigned int style;
};
static bool __should_show(struct monitor_priv *mpriv, bool changed)
{
if (mpriv->style == MONITOR_STYLE_ALL)
return true;
if (mpriv->style == MONITOR_STYLE_CHANGED && changed)
return true;
return false;
}
static void monitor_port_list(struct team_handle *th,
struct monitor_priv *mpriv)
{
struct team_port *port;
char buf[120];
bool trunc;
bool skip = true;
team_for_each_port(port, th) {
if (__should_show(mpriv, team_is_port_changed(port))) {
skip = false;
break;
}
}
if (skip)
return;
printf("ports:\n");
team_for_each_port(port, th) {
if (!__should_show(mpriv, team_is_port_changed(port)))
continue;
trunc = team_port_str(port, buf, sizeof(buf));
printf(" %s %s\n", buf, trunc ? "..." : "");
}
}
static void monitor_option_list(struct team_handle *th,
struct monitor_priv *mpriv)
{
struct team_option *option;
char buf[120];
bool trunc;
bool skip = true;
team_for_each_option(option, th) {
if (__should_show(mpriv, team_is_option_changed(option))) {
skip = false;
break;
}
}
if (skip)
return;
printf("options:\n");
team_for_each_option(option, th) {
if (!__should_show(mpriv, team_is_option_changed(option)))
continue;
trunc = team_option_str(th, option, buf, sizeof(buf));
printf(" %s%s%s\n", buf, trunc ? "..." : "",
team_is_option_changed(option) ? " changed" : "");
}
}
static void monitor_ifinfo_list(struct team_handle *th,
struct monitor_priv *mpriv)
{
struct team_ifinfo *ifinfo;
char buf[120];
bool trunc;
bool skip = true;
team_for_each_ifinfo(ifinfo, th) {
if (__should_show(mpriv, team_is_ifinfo_changed(ifinfo))) {
skip = false;
break;
}
}
if (skip)
return;
printf("ifinfos:\n");
team_for_each_ifinfo(ifinfo, th) {
if (!__should_show(mpriv, team_is_ifinfo_changed(ifinfo)))
continue;
trunc = team_ifinfo_str(ifinfo, buf, sizeof(buf));
printf(" %s %s\n", buf, trunc ? "..." : "");
}
}
static int debug_change_handler_func(struct team_handle *th, void *priv,
team_change_type_mask_t type_mask)
{
struct monitor_priv *mpriv = priv;
if (type_mask & TEAM_PORT_CHANGE)
monitor_port_list(th, mpriv);
if (type_mask & TEAM_OPTION_CHANGE)
monitor_option_list(th, mpriv);
if (type_mask & TEAM_IFINFO_CHANGE)
monitor_ifinfo_list(th, mpriv);
return 0;
}
static const struct team_change_handler debug_change_handler = {
.func = debug_change_handler_func,
.type_mask = TEAM_PORT_CHANGE | TEAM_OPTION_CHANGE | TEAM_IFINFO_CHANGE,
};
static int run_cmd_monitor(char *cmd_name, struct team_handle *th,
struct cmd_ctx *cmd_ctx)
{
struct monitor_priv mpriv;
int err;
mpriv.style = MONITOR_STYLE_CHANGED;
if (cmd_ctx->argc > 0) {
char *monitor_style_str = cmd_ctx->argv[0];
if (!strncmp(monitor_style_str, "all",
strlen(monitor_style_str))) {
mpriv.style = MONITOR_STYLE_ALL;
} else if (!strncmp(monitor_style_str, "changed",
strlen(monitor_style_str))) {
mpriv.style = MONITOR_STYLE_CHANGED;
} else {
fprintf(stderr, "Unknown monitor style \"%s\"\n",
monitor_style_str);
return -EINVAL;
}
}
err = team_change_handler_register(th, &debug_change_handler, &mpriv);
if (err) {
fprintf(stderr, "Failed to register change handler\n");
return err;
}
err = run_main_loop(th);
team_change_handler_unregister(th, &debug_change_handler, &mpriv);
return err;
}
static struct cmd_type cmd_types[] = {
{
.name = "ports",
.params = { NULL },
.run_cmd = run_cmd_ports,
},
{
.name = "options",
.params = { NULL },
.run_cmd = run_cmd_options,
},
{
.name = "getoption",
.params = { "OPT_NAME", NULL },
.run_cmd = run_cmd_getoption,
},
{
.name = "setoption",
.params = { "OPT_NAME", "OPT_VALUE", NULL },
.run_cmd = run_cmd_setoption,
},
{
.name = "monitor",
.params = { "OPT_STYLE", NULL },
.run_cmd = run_cmd_monitor,
},
};
#define CMD_TYPE_COUNT ARRAY_SIZE(cmd_types)
static int process_port_devname_arg(struct team_handle *th,
struct cmd_ctx *cmd_ctx)
{
uint32_t port_ifindex;
struct team_port *port;
if (!cmd_ctx->port_devname_arg)
return 0;
port_ifindex = team_ifname2ifindex(th, cmd_ctx->port_devname_arg);
if (!port_ifindex) {
fprintf(stderr, "Netdevice \"%s\" not found.\n",
cmd_ctx->port_devname_arg);
return -ENODEV;
}
team_for_each_port(port, th) {
if (port_ifindex == team_get_port_ifindex(port)) {
cmd_ctx->port_ifindex_present = true;
cmd_ctx->port_ifindex = port_ifindex;
return 0;
}
}
fprintf(stderr, "Netdevice \"%s\" is not port of this team.\n",
cmd_ctx->port_devname_arg);
return -ENODEV;
}
static int process_array_index_arg(struct team_handle *th,
struct cmd_ctx *cmd_ctx)
{
uint32_t array_index;
unsigned long int tmp;
char *endptr;
if (!cmd_ctx->array_index_arg)
return 0;
tmp = strtoul(cmd_ctx->array_index_arg, &endptr, 10);
if (tmp == ULONG_MAX) {
fprintf(stderr, "Failed to parse array index.\n");
return -errno;
}
if (strlen(endptr) != 0) {
fprintf(stderr, "Failed to parse array index.\n");
return -EINVAL;
}
array_index = tmp;
if (tmp != array_index) {
fprintf(stderr, "Array index too big.\n");
return -ERANGE;
}
cmd_ctx->array_index_present = true;
cmd_ctx->array_index = array_index;
return 0;
}
static int process_args(struct team_handle *th, struct cmd_ctx *cmd_ctx)
{
int err;
err = process_port_devname_arg(th, cmd_ctx);
if (err)
return err;
return process_array_index_arg(th, cmd_ctx);
}
static int call_cmd(char *team_devname, char *cmd_name,
struct cmd_ctx *cmd_ctx, run_cmd_t run_cmd)
{
struct team_handle *th;
uint32_t ifindex;
int err;
th = team_alloc();
if (!th) {
fprintf(stderr, "Team alloc failed.\n");
return -ENOMEM;
}
ifindex = team_ifname2ifindex(th, team_devname);
if (!ifindex) {
fprintf(stderr, "Netdevice \"%s\" not found.\n", team_devname);
err = -ENODEV;
goto team_free;
}
err = team_init(th, ifindex);
if (err) {
fprintf(stderr, "Team init failed.\n");
goto team_free;
}
err = process_args(th, cmd_ctx);
if (err)
goto team_free;
err = run_cmd(cmd_name, th, cmd_ctx);
team_free:
team_free(th);
return err;
}
static void print_help(const char *argv0) {
int i, j;
printf(
"%s [options] teamdevname command [command args]\n"
"\t-h --help Show this help\n"
"\t-p --port_name team slave port name\n"
"\t-a --array_index team option array index\n",
argv0);
printf("Commands:\n");
for (i = 0; i < CMD_TYPE_COUNT; i++) {
printf("\t%s", cmd_types[i].name);
for (j = 0; cmd_types[i].params[j]; j++)
printf(" %s", cmd_types[i].params[j]);
printf("\n");
}
}
int main(int argc, char **argv)
{
char *argv0 = argv[0];
char *team_devname;
char *cmd_name;
struct cmd_ctx cmd_ctx;
static const struct option long_options[] = {
{ "help", no_argument, NULL, 'h' },
{ "port_name", required_argument, NULL, 'p' },
{ "array_index", required_argument, NULL, 'a' },
{ NULL, 0, NULL, 0 }
};
int opt;
int err;
int i;
int res = EXIT_FAILURE;
memset(&cmd_ctx, 0, sizeof(cmd_ctx));
while ((opt = getopt_long(argc, argv, "hp:a:",
long_options, NULL)) >= 0) {
switch(opt) {
case 'h':
print_help(argv0);
return EXIT_SUCCESS;
case 'p':
free(cmd_ctx.port_devname_arg);
cmd_ctx.port_devname_arg = strdup(optarg);
break;
case 'a':
free(cmd_ctx.array_index_arg);
cmd_ctx.array_index_arg = strdup(optarg);
break;
case '?':
fprintf(stderr, "unknown option.\n");
print_help(argv0);
return EXIT_FAILURE;
default:
fprintf(stderr, "unknown option \"%c\".\n", opt);
print_help(argv0);
return EXIT_FAILURE;
}
}
if (optind >= argc) {
fprintf(stderr, "No team device specified.\n");
printf("\n");
print_help(argv0);
goto errout;
}
if (optind + 1 >= argc) {
fprintf(stderr, "No command specified.\n");
printf("\n");
print_help(argv0);
goto errout;
}
argv += optind;
team_devname = *argv++;
cmd_name = *argv++;
argc -= optind + 2;
cmd_ctx.argc = argc;
cmd_ctx.argv = argv;
for (i = 0; i < CMD_TYPE_COUNT; i++) {
if (strncmp(cmd_types[i].name, cmd_name, strlen(cmd_name)))
continue;
err = call_cmd(team_devname, cmd_name, &cmd_ctx,
cmd_types[i].run_cmd);
if (err) {
fprintf(stderr, "Command failed: %s\n", strerror(-err));
goto errout;
}
break;
}
if (i == CMD_TYPE_COUNT) {
fprintf(stderr, "Unknown command \"%s\".\n", cmd_name);
printf("\n");
print_help(argv0);
goto errout;
}
res = EXIT_SUCCESS;
errout:
free(cmd_ctx.port_devname_arg);
free(cmd_ctx.array_index_arg);
return res;
}
|