mirror_iproute2/ip/iplink_ipoib.c
Matteo Croce 8589eb4efd treewide: refactor help messages
Every tool in the iproute2 package have one or more function to show
an help message to the user. Some of these functions print the help
line by line with a series of printf call, e.g. ip/xfrm_state.c does
60 fprintf calls.
If we group all the calls to a single one and just concatenate strings,
we save a lot of libc calls and thus object size. The size difference
of the compiled binaries calculated with bloat-o-meter is:

        ip/ip:
        add/remove: 0/0 grow/shrink: 5/15 up/down: 103/-4796 (-4693)
        Total: Before=672591, After=667898, chg -0.70%
        ip/rtmon:
        add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-54 (-54)
        Total: Before=48879, After=48825, chg -0.11%
        tc/tc:
        add/remove: 0/2 grow/shrink: 31/10 up/down: 882/-6133 (-5251)
        Total: Before=351912, After=346661, chg -1.49%
        bridge/bridge:
        add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-459 (-459)
        Total: Before=70502, After=70043, chg -0.65%
        misc/lnstat:
        add/remove: 0/1 grow/shrink: 1/0 up/down: 48/-486 (-438)
        Total: Before=9960, After=9522, chg -4.40%
        tipc/tipc:
        add/remove: 0/0 grow/shrink: 1/1 up/down: 18/-62 (-44)
        Total: Before=79182, After=79138, chg -0.06%

While at it, indent some strings which were starting at column 0,
and use tabs where possible, to have a consistent style across helps.

Signed-off-by: Matteo Croce <mcroce@redhat.com>
Signed-off-by: David Ahern <dsahern@gmail.com>
2019-05-20 14:35:07 -07:00

146 lines
3.4 KiB
C

/*
* iplink_ipoib.c IPoIB device support
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* Authors: Or Gerlitz <ogerlitz@mellanox.com>
* copied iflink_vlan.c authored by Patrick McHardy <kaber@trash.net>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/if_link.h>
#include "rt_names.h"
#include "utils.h"
#include "ip_common.h"
static void print_explain(FILE *f)
{
fprintf(f,
"Usage: ... ipoib [ pkey PKEY ]\n"
" [ mode {datagram | connected} ]\n"
" [ umcast {0|1} ]\n"
"\n"
"PKEY := 0x8001-0xffff\n"
);
}
static void explain(void)
{
print_explain(stderr);
}
static int mode_arg(void)
{
fprintf(stderr, "Error: argument of \"mode\" must be \"datagram\"or \"connected\"\n");
return -1;
}
static int ipoib_parse_opt(struct link_util *lu, int argc, char **argv,
struct nlmsghdr *n)
{
__u16 pkey, mode, umcast;
while (argc > 0) {
if (matches(*argv, "pkey") == 0) {
NEXT_ARG();
if (get_u16(&pkey, *argv, 0))
invarg("pkey is invalid", *argv);
addattr_l(n, 1024, IFLA_IPOIB_PKEY, &pkey, 2);
} else if (matches(*argv, "mode") == 0) {
NEXT_ARG();
if (strcmp(*argv, "datagram") == 0)
mode = IPOIB_MODE_DATAGRAM;
else if (strcmp(*argv, "connected") == 0)
mode = IPOIB_MODE_CONNECTED;
else
return mode_arg();
addattr_l(n, 1024, IFLA_IPOIB_MODE, &mode, 2);
} else if (matches(*argv, "umcast") == 0) {
NEXT_ARG();
if (get_u16(&umcast, *argv, 0))
invarg("umcast is invalid", *argv);
addattr_l(n, 1024, IFLA_IPOIB_UMCAST, &umcast, 2);
} else if (matches(*argv, "help") == 0) {
explain();
return -1;
} else {
fprintf(stderr, "ipoib: unknown option \"%s\"?\n", *argv);
explain();
return -1;
}
argc--, argv++;
}
return 0;
}
static void ipoib_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
{
__u16 mode;
if (!tb)
return;
if (!tb[IFLA_IPOIB_PKEY] ||
RTA_PAYLOAD(tb[IFLA_IPOIB_PKEY]) < sizeof(__u16))
return;
__u16 pkey = rta_getattr_u16(tb[IFLA_IPOIB_PKEY]);
if (is_json_context()) {
SPRINT_BUF(b1);
snprintf(b1, sizeof(b1), "%#.4x", pkey);
print_string(PRINT_JSON, "key", NULL, b1);
} else {
fprintf(f, "pkey %#.4x ", pkey);
}
if (!tb[IFLA_IPOIB_MODE] ||
RTA_PAYLOAD(tb[IFLA_IPOIB_MODE]) < sizeof(__u16))
return;
mode = rta_getattr_u16(tb[IFLA_IPOIB_MODE]);
const char *mode_str =
mode == IPOIB_MODE_DATAGRAM ? "datagram" :
mode == IPOIB_MODE_CONNECTED ? "connected" : "unknown";
print_string(PRINT_ANY, "mode", "mode %s ", mode_str);
if (!tb[IFLA_IPOIB_UMCAST] ||
RTA_PAYLOAD(tb[IFLA_IPOIB_UMCAST]) < sizeof(__u16))
return;
__u16 umcast = rta_getattr_u16(tb[IFLA_IPOIB_UMCAST]);
if (is_json_context()) {
SPRINT_BUF(b1);
snprintf(b1, sizeof(b1), "%.4x", umcast);
print_string(PRINT_JSON, "umcast", NULL, b1);
} else {
fprintf(f, "umcast %.4x ", umcast);
}
}
static void ipoib_print_help(struct link_util *lu, int argc, char **argv,
FILE *f)
{
print_explain(f);
}
struct link_util ipoib_link_util = {
.id = "ipoib",
.maxattr = IFLA_IPOIB_MAX,
.parse_opt = ipoib_parse_opt,
.print_opt = ipoib_print_opt,
.print_help = ipoib_print_help,
};