From patchwork Fri Jul 7 15:39:41 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Hemminger X-Patchwork-Id: 785567 X-Patchwork-Delegate: shemminger@vyatta.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3x3zLz6bpPz9s4s for ; Sat, 8 Jul 2017 01:39:55 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=networkplumber-org.20150623.gappssmtp.com header.i=@networkplumber-org.20150623.gappssmtp.com header.b="zLMb8NCx"; dkim-atps=neutral Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751927AbdGGPjv (ORCPT ); Fri, 7 Jul 2017 11:39:51 -0400 Received: from mail-pf0-f177.google.com ([209.85.192.177]:36475 "EHLO mail-pf0-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751023AbdGGPju (ORCPT ); Fri, 7 Jul 2017 11:39:50 -0400 Received: by mail-pf0-f177.google.com with SMTP id q86so18881472pfl.3 for ; Fri, 07 Jul 2017 08:39:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=ieg/NxnAMRm8Ephfxht/j5BzpxdklOAHqLh9OA9jKZ4=; b=zLMb8NCxDH0g8E4paoWHE28WyaM3cS2AE5vnjRnqizev7v62ZjngmiRx1TIRS/Y1Pu 5Cl3Him70a0kVu5fSecl6W8CxiSZE2j9APbsZzjcHlSPjLxG4QmLgpzCe38+tco9LEx3 6z6DgsDpVyFKGGKTS/yNirmfuGyYq+MnVe1JXjXH379P8y6NxJ/2kMbFuaVMEKdJ/PEH FevhvqrOTmrLRIbIfVpcCXMP0t8GJydJOVNS+ERqvD3UQoxpEdmoAbxhddnupeT0hu84 vZTHueSap8D7+zM5ulBv5/8s9/oB7vhy5BybA9mj2HTkRRgxhB/Pex6I789X5h6P+1Rt M6zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=ieg/NxnAMRm8Ephfxht/j5BzpxdklOAHqLh9OA9jKZ4=; b=Xp6A2nTlQmlVlmVsTxt3dvtYoJ5PFxFWaRYh/czd0XHAuB5pjj46oJ/2Fu0KEAigUy Y+59OxdfmDKaJAHHVvzlFp6nmQrLWTcJ4ll73Pdx15EsiHHEMpf7dD73oBmUIwWwbRK8 1XK4k/DT6Gioavrv81+SKwIRon5TLl42oFesW1akG8OtXGo95EbMYx0tmhZKj6cxYEQj anchW/iO0t1HI0xZ210AjDfyeuspgqGe3bCd/Nf7aQAtWg3BXV8635wASWGTyTdZaNPp xe0Ogo72W3UpnkPfdfACkIzfBLhJHcOGqxUAxvUxqXAW+c6Rjw99vrmEjFONYoDP4HrQ 9lXw== X-Gm-Message-State: AIVw1117xJmB6/IBaC+A/dV0h5TkSA+JwIVIhVLPIjIWzHBdrQmzhIGi EigjpkeIMf0cmhWsR+4RzA== X-Received: by 10.99.109.201 with SMTP id i192mr2128529pgc.204.1499441989523; Fri, 07 Jul 2017 08:39:49 -0700 (PDT) Received: from xeon-e3.wavecable.com (76-14-207-240.or.wavecable.com. [76.14.207.240]) by smtp.gmail.com with ESMTPSA id z6sm7326149pff.46.2017.07.07.08.39.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 07 Jul 2017 08:39:47 -0700 (PDT) From: Stephen Hemminger To: netdev@vger.kernel.org Cc: lorenzo@google.com, Stephen Hemminger Subject: [PATCH iproute2] ip: change flag names to an array Date: Fri, 7 Jul 2017 08:39:41 -0700 Message-Id: <20170707153941.5427-1-stephen@networkplumber.org> X-Mailer: git-send-email 2.11.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org For the most of the address flags, use a table of bit values rather than open coding every value. This allows for easier inevitable expansion of flags. This also fixes the missing stable-privacy flag. Signed-off-by: Stephen Hemminger --- ip/ipaddress.c | 152 ++++++++++++++++++++++++--------------------------------- 1 file changed, 65 insertions(+), 87 deletions(-) diff --git a/ip/ipaddress.c b/ip/ipaddress.c index f06f5829fb61..b4efb9fedcd2 100644 --- a/ip/ipaddress.c +++ b/ip/ipaddress.c @@ -1013,14 +1013,67 @@ static unsigned int get_ifa_flags(struct ifaddrmsg *ifa, ifa->ifa_flags; } + +static const char *ifa_flag_names[] = { + "secondary", + "nodad", + "optimistic", + "dadfailed", + "home", + "deprecated", + "tentative", + "permanent", + "mngtmpaddr", + "noprefixroute", + "autojoin", + "stable-privacy", +}; + +static void print_ifa_flags(FILE *fp, const struct ifaddrmsg *ifa, unsigned int flags) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(ifa_flag_names); i++) { + unsigned int mask = 1u << i; + + if (mask == IFA_F_PERMANENT) { + if (!(flags & mask)) + fprintf(fp, "dynamic "); + } else if (flags & mask) { + if (mask == IFA_F_SECONDARY) { + if (ifa->ifa_family == AF_INET6) + fprintf(fp, "temporary "); + else + fprintf(fp, "secondary "); + } else + fprintf(fp, "%s ", ifa_flag_names[i]); + } + + flags &= ~mask; + } + + if (flags) + fprintf(fp, "flags %02x ", flags); + +} + +static unsigned int get_ifa_flag_mask(const char *name) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(ifa_flag_names); i++) { + if (!strcmp(name, ifa_flag_names[i])) + return 1u << i; + } + return 0; +} + int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) { FILE *fp = arg; struct ifaddrmsg *ifa = NLMSG_DATA(n); int len = n->nlmsg_len; - int deprecated = 0; - /* Use local copy of ifa_flags to not interfere with filtering code */ unsigned int ifa_flags; struct rtattr *rta_tb[IFA_MAX+1]; @@ -1040,6 +1093,7 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa))); + ifa_flags = get_ifa_flags(ifa, rta_tb[IFA_FLAGS]); if (!rta_tb[IFA_LOCAL]) @@ -1145,52 +1199,9 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, rta_tb[IFA_ANYCAST])); } fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1))); - if (ifa_flags & IFA_F_SECONDARY) { - ifa_flags &= ~IFA_F_SECONDARY; - if (ifa->ifa_family == AF_INET6) - fprintf(fp, "temporary "); - else - fprintf(fp, "secondary "); - } - if (ifa_flags & IFA_F_TENTATIVE) { - ifa_flags &= ~IFA_F_TENTATIVE; - fprintf(fp, "tentative "); - } - if (ifa_flags & IFA_F_DEPRECATED) { - ifa_flags &= ~IFA_F_DEPRECATED; - deprecated = 1; - fprintf(fp, "deprecated "); - } - if (ifa_flags & IFA_F_HOMEADDRESS) { - ifa_flags &= ~IFA_F_HOMEADDRESS; - fprintf(fp, "home "); - } - if (ifa_flags & IFA_F_NODAD) { - ifa_flags &= ~IFA_F_NODAD; - fprintf(fp, "nodad "); - } - if (ifa_flags & IFA_F_MANAGETEMPADDR) { - ifa_flags &= ~IFA_F_MANAGETEMPADDR; - fprintf(fp, "mngtmpaddr "); - } - if (ifa_flags & IFA_F_NOPREFIXROUTE) { - ifa_flags &= ~IFA_F_NOPREFIXROUTE; - fprintf(fp, "noprefixroute "); - } - if (ifa_flags & IFA_F_MCAUTOJOIN) { - ifa_flags &= ~IFA_F_MCAUTOJOIN; - fprintf(fp, "autojoin "); - } - if (!(ifa_flags & IFA_F_PERMANENT)) - fprintf(fp, "dynamic "); - else - ifa_flags &= ~IFA_F_PERMANENT; - if (ifa_flags & IFA_F_DADFAILED) { - ifa_flags &= ~IFA_F_DADFAILED; - fprintf(fp, "dadfailed "); - } - if (ifa_flags) - fprintf(fp, "flags %02x ", ifa_flags); + + print_ifa_flags(fp, ifa, ifa_flags); + if (rta_tb[IFA_LABEL]) fprintf(fp, "%s", rta_getattr_str(rta_tb[IFA_LABEL])); if (rta_tb[IFA_CACHEINFO]) { @@ -1206,7 +1217,7 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, if (ci->ifa_prefered == INFINITY_LIFE_TIME) fprintf(fp, "forever"); else { - if (deprecated) + if (ifa_flags & IFA_F_DEPRECATED) fprintf(fp, "%dsec", ci->ifa_prefered); else fprintf(fp, "%usec", ci->ifa_prefered); @@ -1570,6 +1581,7 @@ static int ipaddr_list_flush_or_save(int argc, char **argv, int action) struct nlmsg_chain _ainfo = { NULL, NULL}, *ainfo = NULL; struct nlmsg_list *l; char *filter_dev = NULL; + unsigned int mask; int no_link = 0; ipaddr_reset_filter(oneline, 0); @@ -1612,49 +1624,15 @@ static int ipaddr_list_flush_or_save(int argc, char **argv, int action) } else if (strcmp(*argv, "dynamic") == 0) { filter.flags &= ~IFA_F_PERMANENT; filter.flagmask |= IFA_F_PERMANENT; - } else if (strcmp(*argv, "permanent") == 0) { - filter.flags |= IFA_F_PERMANENT; - filter.flagmask |= IFA_F_PERMANENT; - } else if (strcmp(*argv, "secondary") == 0 || - strcmp(*argv, "temporary") == 0) { + } else if (strcmp(*argv, "temporary") == 0) { filter.flags |= IFA_F_SECONDARY; filter.flagmask |= IFA_F_SECONDARY; } else if (strcmp(*argv, "primary") == 0) { filter.flags &= ~IFA_F_SECONDARY; filter.flagmask |= IFA_F_SECONDARY; - } else if (strcmp(*argv, "tentative") == 0) { - filter.flags |= IFA_F_TENTATIVE; - filter.flagmask |= IFA_F_TENTATIVE; - } else if (strcmp(*argv, "-tentative") == 0) { - filter.flags &= ~IFA_F_TENTATIVE; - filter.flagmask |= IFA_F_TENTATIVE; - } else if (strcmp(*argv, "deprecated") == 0) { - filter.flags |= IFA_F_DEPRECATED; - filter.flagmask |= IFA_F_DEPRECATED; - } else if (strcmp(*argv, "-deprecated") == 0) { - filter.flags &= ~IFA_F_DEPRECATED; - filter.flagmask |= IFA_F_DEPRECATED; - } else if (strcmp(*argv, "home") == 0) { - filter.flags |= IFA_F_HOMEADDRESS; - filter.flagmask |= IFA_F_HOMEADDRESS; - } else if (strcmp(*argv, "nodad") == 0) { - filter.flags |= IFA_F_NODAD; - filter.flagmask |= IFA_F_NODAD; - } else if (strcmp(*argv, "mngtmpaddr") == 0) { - filter.flags |= IFA_F_MANAGETEMPADDR; - filter.flagmask |= IFA_F_MANAGETEMPADDR; - } else if (strcmp(*argv, "noprefixroute") == 0) { - filter.flags |= IFA_F_NOPREFIXROUTE; - filter.flagmask |= IFA_F_NOPREFIXROUTE; - } else if (strcmp(*argv, "autojoin") == 0) { - filter.flags |= IFA_F_MCAUTOJOIN; - filter.flagmask |= IFA_F_MCAUTOJOIN; - } else if (strcmp(*argv, "dadfailed") == 0) { - filter.flags |= IFA_F_DADFAILED; - filter.flagmask |= IFA_F_DADFAILED; - } else if (strcmp(*argv, "-dadfailed") == 0) { - filter.flags &= ~IFA_F_DADFAILED; - filter.flagmask |= IFA_F_DADFAILED; + } else if ((mask = get_ifa_flag_mask(*argv)) != 0) { + filter.flags |= mask; + filter.flagmask |= mask; } else if (strcmp(*argv, "label") == 0) { NEXT_ARG(); filter.label = *argv;