gre/gre6: Unify gre_print_help()

Reduce diff lines between gre and gre6 help printing code.

Use @struct link_util ->id field to print correct link help: all callers
now pass this data structure to gre_print_help().

Get rid of custom print_usage() and usage() functions and use
gre_print_help() directly, return from function on "... type
<help|garbage>" instead of exit(2).

Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com>
Signed-off-by: David Ahern <dsahern@gmail.com>
This commit is contained in:
Serhey Popovych 2018-02-09 08:58:41 +02:00 committed by David Ahern
parent 4aa552eac1
commit ae91205c4d
2 changed files with 67 additions and 80 deletions

View File

@ -23,34 +23,38 @@
#include "ip_common.h" #include "ip_common.h"
#include "tunnel.h" #include "tunnel.h"
static void print_usage(FILE *f) static void gre_print_help(struct link_util *lu, int argc, char **argv, FILE *f)
{ {
fprintf(f, fprintf(f,
"Usage: ... { gre | gretap | erspan } [ remote ADDR ]\n" "Usage: ... %-9s [ remote ADDR ]\n",
" [ local ADDR ]\n" lu->id
" [ [i|o]seq ]\n" );
" [ [i|o]key KEY ]\n" fprintf(f,
" [ [i|o]csum ]\n" " [ local ADDR ]\n"
" [ ttl TTL ]\n" " [ [i|o]seq ]\n"
" [ tos TOS ]\n" " [ [i|o]key KEY ]\n"
" [ [no]pmtudisc ]\n" " [ [i|o]csum ]\n"
" [ [no]ignore-df ]\n" " [ ttl TTL ]\n"
" [ dev PHYS_DEV ]\n" " [ tos TOS ]\n"
" [ noencap ]\n" " [ [no]pmtudisc ]\n"
" [ encap { fou | gue | none } ]\n" " [ [no]ignore-df ]\n"
" [ encap-sport PORT ]\n" " [ dev PHYS_DEV ]\n"
" [ encap-dport PORT ]\n" " [ fwmark MARK ]\n"
" [ [no]encap-csum ]\n" " [ external ]\n"
" [ [no]encap-csum6 ]\n" " [ noencap ]\n"
" [ [no]encap-remcsum ]\n" " [ encap { fou | gue | none } ]\n"
" [ external ]\n" " [ encap-sport PORT ]\n"
" [ fwmark MARK ]\n" " [ encap-dport PORT ]\n"
" [ erspan_ver version ]\n" " [ [no]encap-csum ]\n"
" [ erspan IDX ]\n" " [ [no]encap-csum6 ]\n"
" [ erspan_dir { ingress | egress } ]\n" " [ [no]encap-remcsum ]\n"
" [ erspan_hwid hwid ]\n" " [ erspan_ver version ]\n"
" [ external ]\n" " [ erspan IDX ]\n"
" [ erspan_dir { ingress | egress } ]\n"
" [ erspan_hwid hwid ]\n"
"\n" "\n"
);
fprintf(f,
"Where: ADDR := { IP_ADDRESS | any }\n" "Where: ADDR := { IP_ADDRESS | any }\n"
" TOS := { NUMBER | inherit }\n" " TOS := { NUMBER | inherit }\n"
" TTL := { 1..255 | inherit }\n" " TTL := { 1..255 | inherit }\n"
@ -59,13 +63,6 @@ static void print_usage(FILE *f)
); );
} }
static void usage(void) __attribute__((noreturn));
static void usage(void)
{
print_usage(stderr);
exit(-1);
}
static int gre_parse_opt(struct link_util *lu, int argc, char **argv, static int gre_parse_opt(struct link_util *lu, int argc, char **argv,
struct nlmsghdr *n) struct nlmsghdr *n)
{ {
@ -336,8 +333,10 @@ get_failed:
NEXT_ARG(); NEXT_ARG();
if (get_u16(&erspan_hwid, *argv, 0)) if (get_u16(&erspan_hwid, *argv, 0))
invarg("invalid erspan hwid\n", *argv); invarg("invalid erspan hwid\n", *argv);
} else } else {
usage(); gre_print_help(lu, argc, argv, stderr);
return -1;
}
argc--; argv++; argc--; argv++;
} }
@ -517,12 +516,6 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
IFLA_GRE_ENCAP_DPORT); IFLA_GRE_ENCAP_DPORT);
} }
static void gre_print_help(struct link_util *lu, int argc, char **argv,
FILE *f)
{
print_usage(f);
}
struct link_util gre_link_util = { struct link_util gre_link_util = {
.id = "gre", .id = "gre",
.maxattr = IFLA_GRE_MAX, .maxattr = IFLA_GRE_MAX,

View File

@ -30,34 +30,39 @@
#define DEFAULT_TNL_HOP_LIMIT (64) #define DEFAULT_TNL_HOP_LIMIT (64)
static void print_usage(FILE *f) static void gre_print_help(struct link_util *lu, int argc, char **argv, FILE *f)
{ {
fprintf(f, fprintf(f,
"Usage: ... { ip6gre | ip6gretap | ip6erspan} [ remote ADDR ]\n" "Usage: ... %-9s [ remote ADDR ]\n",
" [ local ADDR ]\n" lu->id
" [ [i|o]seq ]\n" );
" [ [i|o]key KEY ]\n" fprintf(f,
" [ [i|o]csum ]\n" " [ local ADDR ]\n"
" [ hoplimit TTL ]\n" " [ [i|o]seq ]\n"
" [ encaplimit ELIM ]\n" " [ [i|o]key KEY ]\n"
" [ tclass TCLASS ]\n" " [ [i|o]csum ]\n"
" [ flowlabel FLOWLABEL ]\n" " [ hoplimit TTL ]\n"
" [ dscp inherit ]\n" " [ encaplimit ELIM ]\n"
" [ fwmark MARK ]\n" " [ tclass TCLASS ]\n"
" [ dev PHYS_DEV ]\n" " [ flowlabel FLOWLABEL ]\n"
" [ noencap ]\n" " [ dscp inherit ]\n"
" [ encap { fou | gue | none } ]\n" " [ dev PHYS_DEV ]\n"
" [ encap-sport PORT ]\n" " [ fwmark MARK ]\n"
" [ encap-dport PORT ]\n" " [ external ]\n"
" [ [no]encap-csum ]\n" " [ noencap ]\n"
" [ [no]encap-csum6 ]\n" " [ encap { fou | gue | none } ]\n"
" [ [no]encap-remcsum ]\n" " [ encap-sport PORT ]\n"
" [ erspan_ver version ]\n" " [ encap-dport PORT ]\n"
" [ erspan IDX ]\n" " [ [no]encap-csum ]\n"
" [ erspan_dir { ingress | egress } ]\n" " [ [no]encap-csum6 ]\n"
" [ erspan_hwid hwid ]\n" " [ [no]encap-remcsum ]\n"
" [ external ]\n" " [ erspan_ver version ]\n"
" [ erspan IDX ]\n"
" [ erspan_dir { ingress | egress } ]\n"
" [ erspan_hwid hwid ]\n"
"\n" "\n"
);
fprintf(f,
"Where: ADDR := IPV6_ADDRESS\n" "Where: ADDR := IPV6_ADDRESS\n"
" TTL := { 0..255 } (default=%d)\n" " TTL := { 0..255 } (default=%d)\n"
" KEY := { DOTTED_QUAD | NUMBER }\n" " KEY := { DOTTED_QUAD | NUMBER }\n"
@ -69,13 +74,6 @@ static void print_usage(FILE *f)
); );
} }
static void usage(void) __attribute__((noreturn));
static void usage(void)
{
print_usage(stderr);
exit(-1);
}
static int gre_parse_opt(struct link_util *lu, int argc, char **argv, static int gre_parse_opt(struct link_util *lu, int argc, char **argv,
struct nlmsghdr *n) struct nlmsghdr *n)
{ {
@ -384,8 +382,10 @@ get_failed:
NEXT_ARG(); NEXT_ARG();
if (get_u16(&erspan_hwid, *argv, 0)) if (get_u16(&erspan_hwid, *argv, 0))
invarg("invalid erspan hwid\n", *argv); invarg("invalid erspan hwid\n", *argv);
} else } else {
usage(); gre_print_help(lu, argc, argv, stderr);
return -1;
}
argc--; argv++; argc--; argv++;
} }
@ -584,12 +584,6 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
IFLA_GRE_ENCAP_DPORT); IFLA_GRE_ENCAP_DPORT);
} }
static void gre_print_help(struct link_util *lu, int argc, char **argv,
FILE *f)
{
print_usage(f);
}
struct link_util ip6gre_link_util = { struct link_util ip6gre_link_util = {
.id = "ip6gre", .id = "ip6gre",
.maxattr = IFLA_GRE_MAX, .maxattr = IFLA_GRE_MAX,