iplink: Use ll_name_to_index() instead of if_nametoindex()

While benefit from using ll_name_to_index() with populated
cache can potentially be exploited only in few places
(e.g. bridge fdb/mdb/vlan show routines) there is another
advantage of ll_name_to_index() over plain if_nametoindex():

  in case of if_nametoindex() failure ll_name_to_index()
  will attempt to get index from common name in form "if%d"
  that may be returned from ll_index_to_name().

This makes output from ip(8) coherent with it's input.

Note that most of the code already switched from plain
if_nametoindex() to ll_name_to_index() to cached variant.

Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
This commit is contained in:
Serhey Popovych 2018-01-19 18:44:03 +02:00 committed by Stephen Hemminger
parent 9dc04a4fd2
commit 7a14358b16
13 changed files with 16 additions and 16 deletions

View File

@ -374,7 +374,7 @@ static int fdb_show(int argc, char **argv)
/*we'll keep around filter_dev for older kernels */
if (filter_dev) {
filter_index = if_nametoindex(filter_dev);
filter_index = ll_name_to_index(filter_dev);
if (filter_index == 0) {
fprintf(stderr, "Cannot find device \"%s\"\n",
filter_dev);
@ -463,7 +463,7 @@ static int fdb_modify(int cmd, int flags, int argc, char **argv)
invarg("invalid VNI\n", *argv);
} else if (strcmp(*argv, "via") == 0) {
NEXT_ARG();
via = if_nametoindex(*argv);
via = ll_name_to_index(*argv);
if (via == 0)
invarg("invalid device\n", *argv);
} else if (strcmp(*argv, "self") == 0) {

View File

@ -311,7 +311,7 @@ static int mdb_show(int argc, char **argv)
}
if (filter_dev) {
filter_index = if_nametoindex(filter_dev);
filter_index = ll_name_to_index(filter_dev);
if (filter_index == 0) {
fprintf(stderr, "Cannot find device \"%s\"\n",
filter_dev);

View File

@ -567,7 +567,7 @@ static int vlan_show(int argc, char **argv)
}
if (filter_dev) {
filter_index = if_nametoindex(filter_dev);
filter_index = ll_name_to_index(filter_dev);
if (filter_index == 0) {
fprintf(stderr, "Cannot find device \"%s\"\n",
filter_dev);

View File

@ -177,7 +177,7 @@ static int bond_parse_opt(struct link_util *lu, int argc, char **argv,
addattr8(n, 1024, IFLA_BOND_MODE, mode);
} else if (matches(*argv, "active_slave") == 0) {
NEXT_ARG();
ifindex = if_nametoindex(*argv);
ifindex = ll_name_to_index(*argv);
if (!ifindex)
return -1;
addattr32(n, 1024, IFLA_BOND_ACTIVE_SLAVE, ifindex);
@ -240,7 +240,7 @@ static int bond_parse_opt(struct link_util *lu, int argc, char **argv,
addattr32(n, 1024, IFLA_BOND_ARP_ALL_TARGETS, arp_all_targets);
} else if (matches(*argv, "primary") == 0) {
NEXT_ARG();
ifindex = if_nametoindex(*argv);
ifindex = ll_name_to_index(*argv);
if (!ifindex)
return -1;
addattr32(n, 1024, IFLA_BOND_PRIMARY, ifindex);

View File

@ -792,7 +792,7 @@ int bridge_parse_xstats(struct link_util *lu, int argc, char **argv)
xstats_print_attr = BRIDGE_XSTATS_MCAST;
} else if (strcmp(*argv, "dev") == 0) {
NEXT_ARG();
filter_index = if_nametoindex(*argv);
filter_index = ll_name_to_index(*argv);
if (filter_index == 0) {
fprintf(stderr, "Cannot find device \"%s\"\n",
*argv);

View File

@ -151,7 +151,7 @@ static int vxlan_parse_opt(struct link_util *lu, int argc, char **argv,
NEXT_ARG();
check_duparg(&attrs, IFLA_VXLAN_LINK, "dev", *argv);
link = if_nametoindex(*argv);
link = ll_name_to_index(*argv);
if (link == 0) {
fprintf(stderr, "Cannot find device \"%s\"\n",
*argv);

View File

@ -556,7 +556,7 @@ static int parse_encap_seg6local(struct rtattr *rta, size_t len, int *argcp,
NEXT_ARG();
if (iif_ok++)
duparg2("iif", *argv);
iif = if_nametoindex(*argv);
iif = ll_name_to_index(*argv);
if (!iif)
invarg("\"iif\" interface not found\n", *argv);
rta_addattr32(rta, len, SEG6_LOCAL_IIF, iif);
@ -564,7 +564,7 @@ static int parse_encap_seg6local(struct rtattr *rta, size_t len, int *argcp,
NEXT_ARG();
if (oif_ok++)
duparg2("oif", *argv);
oif = if_nametoindex(*argv);
oif = ll_name_to_index(*argv);
if (!oif)
invarg("\"oif\" interface not found\n", *argv);
rta_addattr32(rta, len, SEG6_LOCAL_OIF, oif);

View File

@ -222,7 +222,7 @@ get_failed:
saddr = get_addr32(*argv);
} else if (!matches(*argv, "dev")) {
NEXT_ARG();
link = if_nametoindex(*argv);
link = ll_name_to_index(*argv);
if (link == 0) {
fprintf(stderr, "Cannot find device \"%s\"\n",
*argv);

View File

@ -231,7 +231,7 @@ get_failed:
memcpy(&laddr, &addr.data, sizeof(laddr));
} else if (!matches(*argv, "dev")) {
NEXT_ARG();
link = if_nametoindex(*argv);
link = ll_name_to_index(*argv);
if (link == 0) {
fprintf(stderr, "Cannot find device \"%s\"\n",
*argv);

View File

@ -193,7 +193,7 @@ get_failed:
memcpy(&laddr, addr.data, sizeof(laddr));
} else if (matches(*argv, "dev") == 0) {
NEXT_ARG();
link = if_nametoindex(*argv);
link = ll_name_to_index(*argv);
if (link == 0)
invarg("\"dev\" is invalid", *argv);
} else if (strcmp(*argv, "ttl") == 0 ||

View File

@ -200,7 +200,7 @@ get_failed:
laddr = get_addr32(*argv);
} else if (matches(*argv, "dev") == 0) {
NEXT_ARG();
link = if_nametoindex(*argv);
link = ll_name_to_index(*argv);
if (link == 0)
invarg("\"dev\" is invalid", *argv);
} else if (strcmp(*argv, "ttl") == 0 ||

View File

@ -137,7 +137,7 @@ get_failed:
saddr = get_addr32(*argv);
} else if (!matches(*argv, "dev")) {
NEXT_ARG();
link = if_nametoindex(*argv);
link = ll_name_to_index(*argv);
if (link == 0) {
fprintf(stderr, "Cannot find device \"%s\"\n",
*argv);

View File

@ -143,7 +143,7 @@ get_failed:
memcpy(&saddr, addr.data, sizeof(saddr));
} else if (!matches(*argv, "dev")) {
NEXT_ARG();
link = if_nametoindex(*argv);
link = ll_name_to_index(*argv);
if (link == 0) {
fprintf(stderr, "Cannot find device \"%s\"\n",
*argv);