From patchwork Thu Feb 15 21:23:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 874128 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Kl8FuVI9"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zj8R65XNJz9t20 for ; Fri, 16 Feb 2018 08:24:02 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1167363AbeBOVYA (ORCPT ); Thu, 15 Feb 2018 16:24:00 -0500 Received: from mail-lf0-f67.google.com ([209.85.215.67]:46232 "EHLO mail-lf0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1161937AbeBOVXy (ORCPT ); Thu, 15 Feb 2018 16:23:54 -0500 Received: by mail-lf0-f67.google.com with SMTP id q194so1444776lfe.13 for ; Thu, 15 Feb 2018 13:23:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=bQjabgc7kctEWzZsO0ACgyvlIB032ljVW3jxbLVdVdI=; b=Kl8FuVI9zPnKC2XyQX2wiYCgdmPazKft4oAIpsA+QQ6NMfm7q4M9LcOEvtyMWZKPf9 2ZTvrD+MZCp6j95CCRO9sgDD3eeSyTCGMLTeo0fwbRm5TMejl997WZ9+UJrVyV3HApqX 7KnJfaME0u+KIuksivB7d+tZp6Qb5lHwDuJz07nSObr+wYpHUgcS+vjZG5kvblpYdfU7 N+yVr0vVmgzPqOD08ysw6qXX4Z8KTsSndhH8GYaL1NiycgMZmgQbfUiBdotx0IR/4JGJ 28mg4Kr77Qq4J0AtUHKuULb2bwpl42oC7z6zEssWqvBJkcQETPFFUhTlVE+w583nERFh mmsw== 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:in-reply-to :references; bh=bQjabgc7kctEWzZsO0ACgyvlIB032ljVW3jxbLVdVdI=; b=GD4SksB89PgdGheksEjGHYbj6PPIwVFSzwUr4mZQcc04PKM/X0j+sPwgxVhR9fyPvN sG5lIdx/fMxWB1gWZI55U7izYYUpNWscXpyz9MPurbTHTEOGnFAhZ4nmnMGcWSoGRdmv g2U9Prx8iw0LkwmFWfGNYd2c9Au9WwfvNmFpIhPnus+WY9PzWE4hobMc/iXLrxE1cJbh fZ9dHMwABRA7PX2HJouYvH8EvXbM6ulwB1ADIweX3EZm7mE8SNjiA+NszdunBlb3K5wk oo/XiOUkKy9MVn8yGEyxgWKwM8+W87tBFiwt6iKSn4tRuxup/PaVO6kyXogPqVXnXc0y I9nA== X-Gm-Message-State: APf1xPCzXPPhK8Yhj/aYovgXmlvm9s/fTcxzGahx4TdHa4PTZ5T04lTb 9FeQ3ARMM9dPf5sjUk4I4WggAw== X-Google-Smtp-Source: AH8x224By1MEnMZ//6LlweCFwhnWDymrlK3elVqZPpVxXxpzAEia9dzCk8pcOYJnmmGxxb09xDYeAw== X-Received: by 10.25.56.22 with SMTP id f22mr2807470lfa.109.1518729833386; Thu, 15 Feb 2018 13:23:53 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id 68sm1979049lfs.6.2018.02.15.13.23.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Feb 2018 13:23:52 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Cc: dsahern@gmail.com Subject: [PATCH iproute2-next v5 1/9] ipaddress: Abstract IFA_LABEL matching code Date: Thu, 15 Feb 2018 23:23:16 +0200 Message-Id: <1518729804-19873-2-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1518729804-19873-1-git-send-email-serhe.popovych@gmail.com> References: <1518729804-19873-1-git-send-email-serhe.popovych@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org There at least two places in ip/ipaddress.c where we match IFA_LABEL against filter.label if that is given. Get rid of "common" if () statement for inet_addr_match_rta() and ifa_label_match_rta(): it is not common because first will check for filter.pfx.family != AF_UNSPEC inside and second for filter.label being non NULL. This allows us to further simplify down code and prepare for ll_idx_n2a() replacement with ll_index_to_name() without 80 columns checkpatch notice. Signed-off-by: Serhey Popovych --- ip/ipaddress.c | 57 +++++++++++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/ip/ipaddress.c b/ip/ipaddress.c index 6990b81..ad69d09 100644 --- a/ip/ipaddress.c +++ b/ip/ipaddress.c @@ -1305,6 +1305,22 @@ static int get_filter(const char *arg) return 0; } +static int ifa_label_match_rta(int ifindex, const struct rtattr *rta) +{ + const char *label; + SPRINT_BUF(b1); + + if (!filter.label) + return 0; + + if (rta) + label = RTA_DATA(rta); + else + label = ll_idx_n2a(ifindex, b1); + + return fnmatch(filter.label, label, 0); +} + int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) { @@ -1343,21 +1359,13 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, return 0; if ((filter.flags ^ ifa_flags) & filter.flagmask) return 0; - if (filter.label) { - SPRINT_BUF(b1); - const char *label; - - if (rta_tb[IFA_LABEL]) - label = RTA_DATA(rta_tb[IFA_LABEL]); - else - label = ll_idx_n2a(ifa->ifa_index, b1); - if (fnmatch(filter.label, label, 0) != 0) - return 0; - } if (filter.family && filter.family != ifa->ifa_family) return 0; + if (ifa_label_match_rta(ifa->ifa_index, rta_tb[IFA_LABEL])) + return 0; + if (inet_addr_match_rta(&filter.pfx, rta_tb[IFA_LOCAL])) return 0; @@ -1713,25 +1721,14 @@ static void ipaddr_filter(struct nlmsg_chain *linfo, struct nlmsg_chain *ainfo) if ((filter.flags ^ ifa_flags) & filter.flagmask) continue; - if (filter.pfx.family || filter.label) { - struct rtattr *rta = - tb[IFA_LOCAL] ? : tb[IFA_ADDRESS]; - - if (inet_addr_match_rta(&filter.pfx, rta)) - continue; - - if (filter.label) { - SPRINT_BUF(b1); - const char *label; - - if (tb[IFA_LABEL]) - label = RTA_DATA(tb[IFA_LABEL]); - else - label = ll_idx_n2a(ifa->ifa_index, b1); - if (fnmatch(filter.label, label, 0) != 0) - continue; - } - } + + if (ifa_label_match_rta(ifa->ifa_index, tb[IFA_LABEL])) + continue; + + if (!tb[IFA_LOCAL]) + tb[IFA_LOCAL] = tb[IFA_ADDRESS]; + if (inet_addr_match_rta(&filter.pfx, tb[IFA_LOCAL])) + continue; ok = 1; break; From patchwork Thu Feb 15 21:23:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 874133 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="kfIeTawB"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zj8RZ2ZSTz9t20 for ; Fri, 16 Feb 2018 08:24:26 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1167351AbeBOVX7 (ORCPT ); Thu, 15 Feb 2018 16:23:59 -0500 Received: from mail-lf0-f65.google.com ([209.85.215.65]:41514 "EHLO mail-lf0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1161516AbeBOVX4 (ORCPT ); Thu, 15 Feb 2018 16:23:56 -0500 Received: by mail-lf0-f65.google.com with SMTP id f136so1464559lff.8 for ; Thu, 15 Feb 2018 13:23:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=7zYLDorUcyF/utrQ6smDvzTzXMlmzI67ee0eaFwC6ro=; b=kfIeTawBrSDQQl98dYbmVE6LHHFpv6tFtMVB0jAbYRj5F//Zs3acaZOcyTLjNl9KJs IYlyuFjP8VNRzWzfxWzf5P6K3pjjATNX/BI8rCQfHHGyGjaIyjddMwYWELir4y5PTHd6 5H2OBYeO90u2bEFQ7Y+g/9YdWwxN2t3JRjs1o0zM/fK/MqNRnItBpesT9fyPBwdaoLBu gW7PJa8YZyRJO2AdVZd/oBIGqcygXSMecirtPvebBYAw5n9oRsvo7EhrGiVPki/7Rbxx QrABkRIHj/Ej5z6qXg1XKfeE9mfdP9XZEdfhgO3CQKguH6l4CsRI9H2fWruYw7DYLIUw LKnw== 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:in-reply-to :references; bh=7zYLDorUcyF/utrQ6smDvzTzXMlmzI67ee0eaFwC6ro=; b=YTkvaxjkU+fcxWUxKmpsFk9z1YdUxLYf+AlLQ0z9vw+DqSVVdxj0nZQ0U4R+O4EfNT dPG+IIZcBBnII/xeKEp3cUYFOj2IDp3YucKMdLO7UoRBqrPRz41JensGbwp5b6oTP0jE PdivrUSNvRKfa7IyLkguVb2rLt8ia38JVnE0pU1JzU0u3XpXaD1z8xQrztg+sHud6ZbM 608uc+xowMTJ/w9EwL6d48xuihUOJ+oy4oO6PKzeWaMwrp0l1VRokAGeUB9oR4iTCFtb HBX8UKlOdeeElvMPYmjnITm5IeIkZaDpkW7D85GyoK8wY+8/7jNcS+JZBh45EDhF4Ff7 83Bg== X-Gm-Message-State: APf1xPCjYljUqwJWTknUzrL0xtoZUmJILwMQZMxtA7Bh2U/NMKsNpYTW vPFOD+VqPCx10Vb7MZwrM7w6Kw== X-Google-Smtp-Source: AH8x2244I74w6+TFs/2d5izryPoM9grpUGrZtx0mJ5rhuUlIC2KIL7LfpYkddMtszqjA+XTz6dHaXQ== X-Received: by 10.25.123.20 with SMTP id w20mr2865484lfc.104.1518729834575; Thu, 15 Feb 2018 13:23:54 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id 68sm1979049lfs.6.2018.02.15.13.23.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Feb 2018 13:23:53 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Cc: dsahern@gmail.com Subject: [PATCH iproute2-next v5 2/9] ipaddress: ll_map: Replace ll_idx_n2a() with ll_index_to_name() Date: Thu, 15 Feb 2018 23:23:17 +0200 Message-Id: <1518729804-19873-3-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1518729804-19873-1-git-send-email-serhe.popovych@gmail.com> References: <1518729804-19873-1-git-send-email-serhe.popovych@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org There is no reentrancy as well as deferred result usage for all cases where ll_idx_n2a() being used: it is safe to use ll_index_to_name() that internally calls ll_idx_n2a() with static buffer to hold result. While there print master network device name using correct color. Signed-off-by: Serhey Popovych --- ip/ipaddress.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/ip/ipaddress.c b/ip/ipaddress.c index ad69d09..0daba8c 100644 --- a/ip/ipaddress.c +++ b/ip/ipaddress.c @@ -813,14 +813,13 @@ int print_linkinfo_brief(const struct sockaddr_nl *who, print_bool(PRINT_ANY, "deleted", "Deleted ", true); if (tb[IFLA_LINK]) { - SPRINT_BUF(b1); int iflink = rta_getattr_u32(tb[IFLA_LINK]); if (iflink == 0) { snprintf(buf, sizeof(buf), "%s@NONE", name); print_null(PRINT_JSON, "link", NULL, NULL); } else { - const char *link = ll_idx_n2a(iflink, b1); + const char *link = ll_index_to_name(iflink); print_string(PRINT_JSON, "link", NULL, link); snprintf(buf, sizeof(buf), "%s@%s", name, link); @@ -957,12 +956,10 @@ int print_linkinfo(const struct sockaddr_nl *who, print_int(PRINT_ANY, "link_index", "@if%d: ", iflink); else { - SPRINT_BUF(b1); - print_string(PRINT_ANY, "link", "@%s: ", - ll_idx_n2a(iflink, b1)); + ll_index_to_name(iflink)); m_flag = ll_index_to_flags(iflink); m_flag = !(m_flag & IFF_UP); } @@ -984,12 +981,13 @@ int print_linkinfo(const struct sockaddr_nl *who, "qdisc %s ", rta_getattr_str(tb[IFLA_QDISC])); if (tb[IFLA_MASTER]) { - SPRINT_BUF(b1); + int master = rta_getattr_u32(tb[IFLA_MASTER]); - print_string(PRINT_ANY, - "master", - "master %s ", - ll_idx_n2a(rta_getattr_u32(tb[IFLA_MASTER]), b1)); + print_color_string(PRINT_ANY, + COLOR_IFNAME, + "master", + "master %s ", + ll_index_to_name(master)); } if (tb[IFLA_OPERSTATE]) @@ -1308,7 +1306,6 @@ static int get_filter(const char *arg) static int ifa_label_match_rta(int ifindex, const struct rtattr *rta) { const char *label; - SPRINT_BUF(b1); if (!filter.label) return 0; @@ -1316,7 +1313,7 @@ static int ifa_label_match_rta(int ifindex, const struct rtattr *rta) if (rta) label = RTA_DATA(rta); else - label = ll_idx_n2a(ifindex, b1); + label = ll_index_to_name(ifindex); return fnmatch(filter.label, label, 0); } From patchwork Thu Feb 15 21:23:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 874135 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="nY8t2vLL"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zj8Rm0JCVz9t20 for ; Fri, 16 Feb 2018 08:24:36 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755949AbeBOVYe (ORCPT ); Thu, 15 Feb 2018 16:24:34 -0500 Received: from mail-lf0-f68.google.com ([209.85.215.68]:39115 "EHLO mail-lf0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1162074AbeBOVX5 (ORCPT ); Thu, 15 Feb 2018 16:23:57 -0500 Received: by mail-lf0-f68.google.com with SMTP id h78so1471873lfg.6 for ; Thu, 15 Feb 2018 13:23:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=+d56mWjErbFYFMlvjKL/wtvLXefcFlS8rm5nthd7avo=; b=nY8t2vLL2VekMRceq4uFwTxSC96X/gQeRwlTKZolTiLdnw/crt5fwRgwZ/8T6czW/E 41DSPMs5vxTW9H2/+oV5+HAbgq+6pYn/RxIxL9Rx1JqYqFyMLDW73HDxO/No6Y/wGQDg eRjVZ1OnIA9n8Mo5fEdXJmcnPb4lLwSOys8GokWOGawwNMJ1bwqoTg5RC7/ZWhTe3bip imBMk18lxy1BN+7FlAxfDdx4CbyryOFC7rRmivydTnD0awVlqoIOo9IWUQvq1Mh25PIh PPxq75geJqt1AJMypm9lC9BqnkBB5yb4f03mPDO1nqt5/+Eo/Gdisd2el62LnmZz5VCM KhSg== 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:in-reply-to :references; bh=+d56mWjErbFYFMlvjKL/wtvLXefcFlS8rm5nthd7avo=; b=hqtuG5lhST6D2Ytff4N93e4mViZ9+fDfGvlyeS+8LLCZQEkdpwJX3djkXgQ2y85bh0 sGyCF5KHJ97+qIGId7NmK7uNi62L5u7T0juU5LCzssHbibmap1p7lVvoiaZFgFQ70Agq lHB1Kgh/DkjWFQh+K4NXpKNZfZxwBt0N//w0wRRBhUYmbe0zJUafBmvxfmJeNz1J8unD VyWixjD9KTHQjjdrZaP3W30Z9EPcCRg3ZVT6QJmdJqS+7Kx9P0L9eyluqEJY437FSLgi frVASk4t2QiU6d0m0es/wHBhbdzvnDss1lqTiNRiu5aCS1XzXx2X6ejJ6/hv6P+0xUwe f6Fg== X-Gm-Message-State: APf1xPB1QUHgwXee4a7IwLPEsdAyX1815nCrVbMSrlqfM/usPhuGM10T CVW7KbOFPstfsCmw29IRRvvCyw== X-Google-Smtp-Source: AH8x22463Q2UTkw9H6x8LwCUSxuoa7Pj3PYR3GoRviLCKOkg60+FTP5E5UYPy5P7YyYWd0qrhnzYcA== X-Received: by 10.25.150.78 with SMTP id y75mr2813138lfd.81.1518729835830; Thu, 15 Feb 2018 13:23:55 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id 68sm1979049lfs.6.2018.02.15.13.23.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Feb 2018 13:23:54 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Cc: dsahern@gmail.com Subject: [PATCH iproute2-next v5 3/9] utils: Reimplement ll_idx_n2a() and introduce ll_idx_a2n() Date: Thu, 15 Feb 2018 23:23:18 +0200 Message-Id: <1518729804-19873-4-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1518729804-19873-1-git-send-email-serhe.popovych@gmail.com> References: <1518729804-19873-1-git-send-email-serhe.popovych@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Now all users of ll_idx_n2a() replaced with ll_index_to_name() we can move it's functionality to ll_index_to_name() and implement index to name conversion using snprintf() and "if%u". Use %u specifier in "if%..." template consistently: network device indexes are always greather than zero. Also introduce ll_idx_n2a() conterpart: ll_idx_a2n() that is used to translate name of the "if%u" form to index using sscanf(). Signed-off-by: Serhey Popovych --- include/ll_map.h | 4 +++- lib/ll_map.c | 31 +++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/include/ll_map.h b/include/ll_map.h index c8474e6..8546ff9 100644 --- a/include/ll_map.h +++ b/include/ll_map.h @@ -8,9 +8,11 @@ int ll_remember_index(const struct sockaddr_nl *who, void ll_init_map(struct rtnl_handle *rth); unsigned ll_name_to_index(const char *name); const char *ll_index_to_name(unsigned idx); -const char *ll_idx_n2a(unsigned idx, char *buf); int ll_index_to_type(unsigned idx); int ll_index_to_flags(unsigned idx); unsigned namehash(const char *str); +const char *ll_idx_n2a(unsigned int idx); +unsigned int ll_idx_a2n(const char *name); + #endif /* __LL_MAP_H__ */ diff --git a/lib/ll_map.c b/lib/ll_map.c index f65614f..0afe689 100644 --- a/lib/ll_map.c +++ b/lib/ll_map.c @@ -136,8 +136,26 @@ int ll_remember_index(const struct sockaddr_nl *who, return 0; } -const char *ll_idx_n2a(unsigned idx, char *buf) +const char *ll_idx_n2a(unsigned int idx) { + static char buf[IFNAMSIZ]; + + snprintf(buf, sizeof(buf), "if%u", idx); + return buf; +} + +unsigned int ll_idx_a2n(const char *name) +{ + unsigned int idx; + + if (sscanf(name, "if%u", &idx) != 1) + return 0; + return idx; +} + +const char *ll_index_to_name(unsigned int idx) +{ + static char buf[IFNAMSIZ]; const struct ll_cache *im; if (idx == 0) @@ -148,18 +166,11 @@ const char *ll_idx_n2a(unsigned idx, char *buf) return im->name; if (if_indextoname(idx, buf) == NULL) - snprintf(buf, IFNAMSIZ, "if%d", idx); + snprintf(buf, IFNAMSIZ, "if%u", idx); return buf; } -const char *ll_index_to_name(unsigned idx) -{ - static char nbuf[IFNAMSIZ]; - - return ll_idx_n2a(idx, nbuf); -} - int ll_index_to_type(unsigned idx) { const struct ll_cache *im; @@ -196,7 +207,7 @@ unsigned ll_name_to_index(const char *name) idx = if_nametoindex(name); if (idx == 0) - sscanf(name, "if%u", &idx); + idx = ll_idx_a2n(name); return idx; } From patchwork Thu Feb 15 21:23:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 874136 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Jp3ikAwE"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zj8Rr1DBsz9t20 for ; Fri, 16 Feb 2018 08:24:40 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755954AbeBOVYd (ORCPT ); Thu, 15 Feb 2018 16:24:33 -0500 Received: from mail-lf0-f67.google.com ([209.85.215.67]:40355 "EHLO mail-lf0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1162336AbeBOVX6 (ORCPT ); Thu, 15 Feb 2018 16:23:58 -0500 Received: by mail-lf0-f67.google.com with SMTP id 37so1467989lfs.7 for ; Thu, 15 Feb 2018 13:23:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=rlpbxyapKCgUkXTAtTgEf1NG9VzGTNeJ5uxTgyAfkYM=; b=Jp3ikAwEVJM+nGhwJ+pF1FmdKaAWG53/idxo8qJoa8qXGf73y2uHGlcB5+KVXtYBEF cKmSuY+vbIRmUefmz0zZwNXQFG85Nfoxk40Sq7Wzquilkj1VpFHwumQH1v9S2t/0SR1L yo4DeSOzwmOxjvYlk4zmmWDd26wQfmDrRvKQ384J0AMmQxPVBAlzXy6p+Gi87PtTxdW9 8KRimUms6vn8m+TTAbY0t//gBa08/ukA6Y+uVSWU295mQgCVlL+yXrr62nBZZa5RS6Oa O8oZi0HxBTX/ZUEfiKrnSwDRiPGBWN6lDLq4l1viw0cYhgQNUcyemwBbRSseQsbSOq0+ Dx6g== 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:in-reply-to :references; bh=rlpbxyapKCgUkXTAtTgEf1NG9VzGTNeJ5uxTgyAfkYM=; b=pZlMd2D/m0pQ6C1YD9DS4xzhL4qX325pXeN3+BFAqao+bzK3Fyx4oIzNtCEpl3yV1Y 47PD3c27Xi/hGtdTnXK7wiSXlIGREty7k0JuV4VevHHH1NxJcC+xtlQEbVB+zR3ZIwgs sRjza+Q3xlZcN+XFPZYoeikBokYoVWjxAFAhVaxZkytwdNCAwF8Uqss9tXzNa7l7q4aU IOh966Xa75CSORLXJ7cDEMneRBxNp01g2X8+1Yg9qzBjpfwwNImgJF8+jtQhdgIAAxeq C9sO2xoKmZiZ3ekGoL2geXUk4yGxQEIO7qwnXVcyK2keshzAcZmM5l09eaH9JkN5BD8k 4HyQ== X-Gm-Message-State: APf1xPAWUPzy9as7jvGX+QvKdIPlaPkwpxL8jrPHtCrFt+Gc3KWDYcnl l1k8snmUwZ8iIf9ce6qGluxpzA== X-Google-Smtp-Source: AH8x225/1rX3pOsCin6JbpqnznZDd72UFo28rrbjXu0QUA0gEnNueLtjmzccu6r7UXBjh8lIS7+V9g== X-Received: by 10.46.33.230 with SMTP id h99mr629lji.47.1518729836993; Thu, 15 Feb 2018 13:23:56 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id 68sm1979049lfs.6.2018.02.15.13.23.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Feb 2018 13:23:56 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Cc: dsahern@gmail.com Subject: [PATCH iproute2-next v5 4/9] ipaddress: Improve print_linkinfo() Date: Thu, 15 Feb 2018 23:23:19 +0200 Message-Id: <1518729804-19873-5-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1518729804-19873-1-git-send-email-serhe.popovych@gmail.com> References: <1518729804-19873-1-git-send-email-serhe.popovych@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org There are few places to improve: 1) return -1 when entry is filtered instead of zero, which means accept entry: ipaddress_list_flush_or_save() the only user of this 2) use ll_idx_n2a() as last resort to translate name to index for "should never happen" cases when cache shouldn't be considered 3) replace open coded access to IFLA_IFNAME attribute data by RTA_DATA() with rta_getattr_str() 4) simplify ifname printing since name is never NULL, thanks to (2). Signed-off-by: Serhey Popovych --- ip/ipaddress.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/ip/ipaddress.c b/ip/ipaddress.c index 0daba8c..6eac370 100644 --- a/ip/ipaddress.c +++ b/ip/ipaddress.c @@ -778,14 +778,14 @@ int print_linkinfo_brief(const struct sockaddr_nl *who, parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); if (tb[IFLA_IFNAME] == NULL) { fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index); - name = ""; + name = ll_idx_n2a(ifi->ifi_index); } else { name = rta_getattr_str(tb[IFLA_IFNAME]); } if (filter.label && (!filter.family || filter.family == AF_PACKET) && - fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)) + fnmatch(filter.label, name, 0)) return -1; if (tb[IFLA_GROUP]) { @@ -887,6 +887,7 @@ int print_linkinfo(const struct sockaddr_nl *who, struct ifinfomsg *ifi = NLMSG_DATA(n); struct rtattr *tb[IFLA_MAX+1]; int len = n->nlmsg_len; + const char *name; unsigned int m_flag = 0; if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK) @@ -897,18 +898,22 @@ int print_linkinfo(const struct sockaddr_nl *who, return -1; if (filter.ifindex && ifi->ifi_index != filter.ifindex) - return 0; + return -1; if (filter.up && !(ifi->ifi_flags&IFF_UP)) - return 0; + return -1; parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); - if (tb[IFLA_IFNAME] == NULL) + if (tb[IFLA_IFNAME] == NULL) { fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index); + name = ll_idx_n2a(ifi->ifi_index); + } else { + name = rta_getattr_str(tb[IFLA_IFNAME]); + } if (filter.label && (!filter.family || filter.family == AF_PACKET) && - fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)) - return 0; + fnmatch(filter.label, name, 0)) + return -1; if (tb[IFLA_GROUP]) { int group = rta_getattr_u32(tb[IFLA_GROUP]); @@ -935,16 +940,7 @@ int print_linkinfo(const struct sockaddr_nl *who, print_bool(PRINT_ANY, "deleted", "Deleted ", true); print_int(PRINT_ANY, "ifindex", "%d: ", ifi->ifi_index); - if (tb[IFLA_IFNAME]) { - print_color_string(PRINT_ANY, - COLOR_IFNAME, - "ifname", "%s", - rta_getattr_str(tb[IFLA_IFNAME])); - } else { - print_null(PRINT_JSON, "ifname", NULL, NULL); - print_color_null(PRINT_FP, COLOR_IFNAME, - "ifname", "%s", ""); - } + print_color_string(PRINT_ANY, COLOR_IFNAME, "ifname", "%s", name); if (tb[IFLA_LINK]) { int iflink = rta_getattr_u32(tb[IFLA_LINK]); From patchwork Thu Feb 15 21:23:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 874134 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="TQTpdEWJ"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zj8Rb44D5z9t20 for ; Fri, 16 Feb 2018 08:24:27 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1162442AbeBOVYZ (ORCPT ); Thu, 15 Feb 2018 16:24:25 -0500 Received: from mail-lf0-f66.google.com ([209.85.215.66]:37126 "EHLO mail-lf0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1162366AbeBOVX7 (ORCPT ); Thu, 15 Feb 2018 16:23:59 -0500 Received: by mail-lf0-f66.google.com with SMTP id f137so1481456lfe.4 for ; Thu, 15 Feb 2018 13:23:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=XLwKpXy1rS3KIle4BBoOkO/8E01s82j58F1jGmZFFRM=; b=TQTpdEWJpqGfT4IVjIsM2EqbLWGDTGQPBc9eTWZL1ehQOq9N1Y8uuuflBIISxIrhJU ErNjoRjtGIcR+rPKb7GBNQX0t9QHpRnBGG+eyHcYxu51+vNFaiuiYw2KES+4QKPi7UB1 eYYm6tg+7/KWLpX/hfZDlcsN3aQX+Ug76naMF0UQyDRk6rOrp10CZZ3qB/Gy0ttUDg4H ShN1ieqqmWoX8MK5tAAecdGjbbzxkuw7U3l9+umxAppW+SoeWM1y7h3T3xb4mu6PMZ7t mfrQsXsQ80cQIOTN/0Ri+EfYuKrTJ5y/7tJi8iMLuBKmQFEGjwHefzWOSNgwKMh8zexW kCZg== 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:in-reply-to :references; bh=XLwKpXy1rS3KIle4BBoOkO/8E01s82j58F1jGmZFFRM=; b=rhUx3bv5XZj/O3eEwKo9hHX2kHx5ncy1KdNCnnusjIWT2q8bOOGoJ7iGtxXA39Bquy Z+NaMcDRF/BB6wsW0IIYsDP8IB4J1Nu/GjAOBiTywKnpK8q94Jp/YVLmNXFQsivEIxsH 11YJF0VO91qb7EbnrgpYDIJMDtsSQ3ROnoHBvtSvxk9iogaAr+szBKY6Q3NfvCTAmnjd Fnx8Okma05/4laUALnxa5hGOPCAHUpk3gq+tpTPlcukr2PLoAAvzI3tUnHKy7k4ckgZB Gg5ZSM+omLQfY+3QRpcA+UBNR2YnPiEuV74qrJEiA2Ma+g2Ym1eHVdDEuO8dOODfIC6Z TEyw== X-Gm-Message-State: APf1xPArBQNF931MN2CWm1QqU5WKugt5uSlkYdAt/pctC82hiEWg7lim XD1CaDcOfuqu6o3wtpUdVyC14A== X-Google-Smtp-Source: AH8x22409PySBsaS1G1cfJE8HwPliA4pD8ZBRm6IFRkS2rIpPdXuVx8R5Z4HL5mF2UZqyCvDe5lP5A== X-Received: by 10.46.2.1 with SMTP id 1mr2808862ljc.0.1518729838054; Thu, 15 Feb 2018 13:23:58 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id 68sm1979049lfs.6.2018.02.15.13.23.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Feb 2018 13:23:57 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Cc: dsahern@gmail.com Subject: [PATCH iproute2-next v5 5/9] ipaddress: Simplify print_linkinfo_brief() and it's usage Date: Thu, 15 Feb 2018 23:23:20 +0200 Message-Id: <1518729804-19873-6-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1518729804-19873-1-git-send-email-serhe.popovych@gmail.com> References: <1518729804-19873-1-git-send-email-serhe.popovych@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Simplify calling code in ipaddr_list_flush_or_save() by introducing intermediate variable of @struct nlmsghdr, drop duplicated code: print_linkinfo_brief() never returns values other than <= 0 so we can move print_selected_addrinfo() outside of each block. Signed-off-by: Serhey Popovych --- ip/ipaddress.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/ip/ipaddress.c b/ip/ipaddress.c index 6eac370..749178d 100644 --- a/ip/ipaddress.c +++ b/ip/ipaddress.c @@ -753,7 +753,7 @@ static void print_link_stats(FILE *fp, struct nlmsghdr *n) } int print_linkinfo_brief(const struct sockaddr_nl *who, - struct nlmsghdr *n, void *arg) + struct nlmsghdr *n, void *arg) { FILE *fp = (FILE *)arg; struct ifinfomsg *ifi = NLMSG_DATA(n); @@ -2013,24 +2013,21 @@ static int ipaddr_list_flush_or_save(int argc, char **argv, int action) ipaddr_filter(&linfo, ainfo); for (l = linfo.head; l; l = l->next) { - int res = 0; - struct ifinfomsg *ifi = NLMSG_DATA(&l->h); + struct nlmsghdr *n = &l->h; + struct ifinfomsg *ifi = NLMSG_DATA(n); + int res; open_json_object(NULL); - if (brief) { - if (print_linkinfo_brief(NULL, &l->h, stdout) == 0) - if (filter.family != AF_PACKET) - print_selected_addrinfo(ifi, - ainfo->head, - stdout); - } else if (no_link || - (res = print_linkinfo(NULL, &l->h, stdout)) >= 0) { - if (filter.family != AF_PACKET) - print_selected_addrinfo(ifi, - ainfo->head, stdout); - if (res > 0 && !do_link && show_stats) - print_link_stats(stdout, &l->h); - } + if (brief) + res = print_linkinfo_brief(NULL, n, stdout); + else if (no_link) + res = 0; + else + res = print_linkinfo(NULL, n, stdout); + if (res >= 0 && filter.family != AF_PACKET) + print_selected_addrinfo(ifi, ainfo->head, stdout); + if (res > 0 && !do_link && show_stats) + print_link_stats(stdout, n); close_json_object(); } fflush(stdout); From patchwork Thu Feb 15 21:23:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 874129 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="QLB9vGJI"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zj8RC0X0yz9t20 for ; Fri, 16 Feb 2018 08:24:07 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1167384AbeBOVYC (ORCPT ); Thu, 15 Feb 2018 16:24:02 -0500 Received: from mail-lf0-f68.google.com ([209.85.215.68]:37129 "EHLO mail-lf0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1161516AbeBOVYA (ORCPT ); Thu, 15 Feb 2018 16:24:00 -0500 Received: by mail-lf0-f68.google.com with SMTP id f137so1481520lfe.4 for ; Thu, 15 Feb 2018 13:24:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=gPB73JIUDUkPvZuyyOg2M9mOoZXhBIFHrvX4NEm+vjw=; b=QLB9vGJI1zxE6+SmrA0BPRvyfQP2dBGQ8je4+SMxUy78QV2rmyda+WrCF8cP/qjItk eZohtC9BSWkFzvC/z6P0+gvwu2ELruhbOcpJ+7STp1IYvUelHkyF7nYj8XshAj9P2oOQ bh/T49UWD3UtQVzQPjzXXwCm8hqTvBbuvzPMiqV5FWui4L1jAQALEBGXSb4ZY8LRAZ5s oDG8bV+Ziq5w+rylI7Ur3usECR7py9//ZhTF8nCY8F8Efp55DSG2kyDAuHCmBPvqKn8f NeDxPWCsrZCeCLpYFfKPpI5SpUy0j+wccZ7OUOPEE987dS9BCa0tG5pNmMt6vkW+KHgG 44cw== 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:in-reply-to :references; bh=gPB73JIUDUkPvZuyyOg2M9mOoZXhBIFHrvX4NEm+vjw=; b=ghGP3xaqjjsySmy6On4q7uWxankl+6880eOhxEM6GiGu9JeOVq1m8avVkWmqEFF930 quFy46rvc3zuRCan28TuE1pVBNRgSvpzeAW1IfOHKtARX/SNmQwHKGB1/fL6+TaQHb9a OkzRBgc7btT0G66tNaF75z+B2OjZzu9bBZxpdxXaRIAR/Y9BN3cGlWkwbIZElkKOtCio ZbOmVMcTNl1gHJZVSl2zuH+mCqVjc5c4/6/9qdsAbmYkp6w9Ks1h+wYHYx2FD/6tZXCH ZQ6FpilhauIOALwqwDrQ/j5ptKkeV/yQzp//NgLJ38r7+ofFtQzmJC8jNHVYZKt+hOhe fQXQ== X-Gm-Message-State: APf1xPB61QA8ptAgvKU8kEYf2KJe5tHTpRk4wnrbFnwG4PgjhevaliU9 xiLtJzHk6SI2+r9GIbjQt1zjfw== X-Google-Smtp-Source: AH8x227W2nvgy90WBZbJKp6rICr8Ix1rl5B2tjrxJXOQdkbyrkM16f+uFxZMKm6Dh/5+cA19XTAy+w== X-Received: by 10.46.126.15 with SMTP id z15mr2783391ljc.103.1518729839251; Thu, 15 Feb 2018 13:23:59 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id 68sm1979049lfs.6.2018.02.15.13.23.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Feb 2018 13:23:58 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Cc: dsahern@gmail.com Subject: [PATCH iproute2-next v5 6/9] lib: Correct object file dependencies Date: Thu, 15 Feb 2018 23:23:21 +0200 Message-Id: <1518729804-19873-7-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1518729804-19873-1-git-send-email-serhe.popovych@gmail.com> References: <1518729804-19873-1-git-send-email-serhe.popovych@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Neither internal libnetlink nor libgenl depends on ll_map.o: prepare for upcoming changes that brings much more cleaner dependency between utils.o and ll_map.o. However ll_map.o depends on libnetlink.o functions so we need to provide libnetlink.a after libutil.a in LIBNETLINK at global Makefile. Tested using make clean && make -j4. No problems so far. Signed-off-by: Serhey Popovych --- Makefile | 2 +- lib/Makefile | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 32587db..b526d3b 100644 --- a/Makefile +++ b/Makefile @@ -59,7 +59,7 @@ YACCFLAGS = -d -t -v SUBDIRS=lib ip tc bridge misc netem genl tipc devlink rdma man -LIBNETLINK=../lib/libnetlink.a ../lib/libutil.a +LIBNETLINK=../lib/libutil.a ../lib/libnetlink.a LDLIBS += $(LIBNETLINK) all: config.mk diff --git a/lib/Makefile b/lib/Makefile index 7b34ed5..bab8cbf 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -3,11 +3,11 @@ include ../config.mk CFLAGS += -fPIC -UTILOBJ = utils.o rt_names.o ll_types.o ll_proto.o ll_addr.o \ +UTILOBJ = utils.o rt_names.o ll_map.o ll_types.o ll_proto.o ll_addr.o \ inet_proto.o namespace.o json_writer.o json_print.o \ names.o color.o bpf.o exec.o fs.o -NLOBJ=libgenl.o ll_map.o libnetlink.o +NLOBJ=libgenl.o libnetlink.o all: libnetlink.a libutil.a From patchwork Thu Feb 15 21:23:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 874130 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="MDLhdlKP"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zj8RJ4ztQz9t20 for ; Fri, 16 Feb 2018 08:24:12 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1167402AbeBOVYK (ORCPT ); Thu, 15 Feb 2018 16:24:10 -0500 Received: from mail-lf0-f66.google.com ([209.85.215.66]:32894 "EHLO mail-lf0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1161937AbeBOVYB (ORCPT ); Thu, 15 Feb 2018 16:24:01 -0500 Received: by mail-lf0-f66.google.com with SMTP id j193so1503519lfe.0 for ; Thu, 15 Feb 2018 13:24:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=MvD3Pyvi2IS2D5SO0k9PWmNivlqCc0Trls8cu+dT0SI=; b=MDLhdlKPpBZeffEYLLJANf1OAK/tfxIZrveSTyNd6VYCPyWSZXXGp7A6GKbljkxPZE dDlx73iv6dWKmb7DdXaJPNFciaGrlvzUQIul208iq+wyhGh+d8CDFoepUIj15paMZgFf pQr5gWJVLuQM4K61f6ikcZ40iJnl5M2SyxwYVOEKi6U3hoErwYlmpD3+8/Xa/ttjDBAA M5WklNQeaellczfI3G8vJM5sKetRGGCbrCuAjaD1Uhclv+b9uKW21mJAsIXzqe3Kwnoc 16WzJAIfj0BtGoBwZNAnhC1EK6+TNmcTPwrHVQZjZzRzoBZqOFWs655yKl0u6cHnMH0N f4og== 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:in-reply-to :references; bh=MvD3Pyvi2IS2D5SO0k9PWmNivlqCc0Trls8cu+dT0SI=; b=V2qgMEbKdGYVBKQfkXo+fd85WmsVyyOk5PLlHcA7pskgpUWHKJaq8rwbxesoHuGdaX YRnclxtARZ830sjxdrvlWdMTaQPm5T73IfcrVtjQ9L9gQJg8vDkc2zqYzwV9x7EoArFp lAL8WKS83mr/CLNKIHBCgEcTstSxfesA/9XwSJpTLBdFcO419mK6LG0rCvsneM4QkxhQ sQoILZEI+9j1I/Rf3B+sl8n8dRbJLwxtTMGkfMvxsCpG/q8tFAP//9nFzJNlB5LsEgK5 4YXcUbKg5ZJuYf1OlxQGpibyuQuj3JdQ3TwKYtMJ0HvpO2TAaQzw+ucBqk9NLkRvHC1O cT7Q== X-Gm-Message-State: APf1xPBHgJ1aEWLb6f2eCNOpBSkIw2UawLBcQAyc/GnFLe/kFje6+b9E pnxIxDNmhSb6TjG8Zte5gx1ZDA== X-Google-Smtp-Source: AH8x226PaQ84IrMg8qyhHVjdJEYNzXxZ7DsXqYvcTzQJfFHoYcn9RQ58RIoRsxhUTBP0PxNMnzBX6w== X-Received: by 10.46.60.24 with SMTP id j24mr2797332lja.140.1518729840260; Thu, 15 Feb 2018 13:24:00 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id 68sm1979049lfs.6.2018.02.15.13.23.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Feb 2018 13:23:59 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Cc: dsahern@gmail.com Subject: [PATCH iproute2-next v5 7/9] utils: Introduce and use get_ifname_rta() Date: Thu, 15 Feb 2018 23:23:22 +0200 Message-Id: <1518729804-19873-8-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1518729804-19873-1-git-send-email-serhe.popovych@gmail.com> References: <1518729804-19873-1-git-send-email-serhe.popovych@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Be consistent in handling of IFLA_IFNAME attribute in all places: if there is no attribute report bug to stderr and use ll_idx_n2a() as last measure to get name in "if%u" format instead of "". Use check_ifname() to validate network device name: this catches both unexpected return from kernel and ll_idx_n2a(). Signed-off-by: Serhey Popovych --- bridge/link.c | 8 ++++---- include/utils.h | 1 + ip/ipaddress.c | 20 ++++++++------------ lib/utils.c | 19 +++++++++++++++++++ 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/bridge/link.c b/bridge/link.c index 870ebe0..a11cbb1 100644 --- a/bridge/link.c +++ b/bridge/link.c @@ -99,9 +99,10 @@ int print_linkinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) { FILE *fp = arg; - int len = n->nlmsg_len; struct ifinfomsg *ifi = NLMSG_DATA(n); struct rtattr *tb[IFLA_MAX+1]; + int len = n->nlmsg_len; + const char *name; len -= NLMSG_LENGTH(sizeof(*ifi)); if (len < 0) { @@ -117,10 +118,9 @@ int print_linkinfo(const struct sockaddr_nl *who, parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len, NLA_F_NESTED); - if (tb[IFLA_IFNAME] == NULL) { - fprintf(stderr, "BUG: nil ifname\n"); + name = get_ifname_rta(ifi->ifi_index, tb[IFLA_IFNAME]); + if (!name) return -1; - } if (n->nlmsg_type == RTM_DELLINK) fprintf(fp, "Deleted "); diff --git a/include/utils.h b/include/utils.h index 867e540..84ca873 100644 --- a/include/utils.h +++ b/include/utils.h @@ -183,6 +183,7 @@ void duparg(const char *, const char *) __attribute__((noreturn)); void duparg2(const char *, const char *) __attribute__((noreturn)); int check_ifname(const char *); int get_ifname(char *, const char *); +const char *get_ifname_rta(int ifindex, const struct rtattr *rta); int matches(const char *arg, const char *pattern); int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits); int inet_addr_match_rta(const inet_prefix *m, const struct rtattr *rta); diff --git a/ip/ipaddress.c b/ip/ipaddress.c index 749178d..08d2576 100644 --- a/ip/ipaddress.c +++ b/ip/ipaddress.c @@ -776,12 +776,10 @@ int print_linkinfo_brief(const struct sockaddr_nl *who, return -1; parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); - if (tb[IFLA_IFNAME] == NULL) { - fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index); - name = ll_idx_n2a(ifi->ifi_index); - } else { - name = rta_getattr_str(tb[IFLA_IFNAME]); - } + + name = get_ifname_rta(ifi->ifi_index, tb[IFLA_IFNAME]); + if (!name) + return -1; if (filter.label && (!filter.family || filter.family == AF_PACKET) && @@ -903,12 +901,10 @@ int print_linkinfo(const struct sockaddr_nl *who, return -1; parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); - if (tb[IFLA_IFNAME] == NULL) { - fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index); - name = ll_idx_n2a(ifi->ifi_index); - } else { - name = rta_getattr_str(tb[IFLA_IFNAME]); - } + + name = get_ifname_rta(ifi->ifi_index, tb[IFLA_IFNAME]); + if (!name) + return -1; if (filter.label && (!filter.family || filter.family == AF_PACKET) && diff --git a/lib/utils.c b/lib/utils.c index d86c2ee..572d42a 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -871,6 +871,25 @@ int get_ifname(char *buf, const char *name) return ret; } +const char *get_ifname_rta(int ifindex, const struct rtattr *rta) +{ + const char *name; + + if (rta) { + name = rta_getattr_str(rta); + } else { + fprintf(stderr, + "BUG: device with ifindex %d has nil ifname\n", + ifindex); + name = ll_idx_n2a(ifindex); + } + + if (check_ifname(name)) + return NULL; + + return name; +} + int matches(const char *cmd, const char *pattern) { int len = strlen(cmd); From patchwork Thu Feb 15 21:23:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 874131 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="G0wSaZwo"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zj8RN1V6tz9t20 for ; Fri, 16 Feb 2018 08:24:16 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1167398AbeBOVYJ (ORCPT ); Thu, 15 Feb 2018 16:24:09 -0500 Received: from mail-lf0-f65.google.com ([209.85.215.65]:34281 "EHLO mail-lf0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1167378AbeBOVYD (ORCPT ); Thu, 15 Feb 2018 16:24:03 -0500 Received: by mail-lf0-f65.google.com with SMTP id l191so1494884lfe.1 for ; Thu, 15 Feb 2018 13:24:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=gcCezGniQG9rCNjNjG28OzVJNEjLhBdXWy1qjlKE50o=; b=G0wSaZwogy0+JjCKjSBNi36rQs2J6CgIVD0cKh74wHdzYBuXvUCIB6Aootg2SPmymo H8w8Iaz1Ofp2tkoRAivnIOiHARebEFmo48JVgwP3Wxpctb3mz9x0WQr9AFZbd+PDIz55 w+IkowO1UuNKozdSPcFCVkD3bg9G8wfD0WZFB0sgNrsATq8q1Lkueb3c+mJEt7cGwEcY OlbHnLqLxDSH3i7gpyqRlgviDuGd1Q8yqMe1kNQB8FRjSmpnAZEXE3P6NkR1J29W7SnL WDsHf+FjuLLjzFbtKHxFygdlJsRUqwQshv1K0EbGpFOp8k68so6Dx29l9/UiJDaVnCq1 UXMA== 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:in-reply-to :references; bh=gcCezGniQG9rCNjNjG28OzVJNEjLhBdXWy1qjlKE50o=; b=krjxMkopq5Bd7YiZiMd6faCNMCe/jTXTpCi9kanlnZHo2KuLji22FZ3l5G2b8L36UY qosxxNKM2kQ7ktk27ssJ1Zps+nNWVC+fflrXl1Vz/N4fPCeiFBPXFtu5QwrKPU/Cphu1 /Jz0p6cTclvIJXc7xGDjJNV2ZpOXWHiHza6ECityL8Kbib4rtpIeSN2OX4paq0Y54zf8 1g5/k1aAHWUH1SiXTyY+4J76tuZERKkJdfImPmqd22WCMWrHi2XSM3HUykYg0ox6BIzK CayQnLO/B774a56S/bLEgux4WLKPEutDxqF+mGI97XnlSmBQjiLlxfl4Brz0x4QiRr7v T13g== X-Gm-Message-State: APf1xPDeEaBn8fbOlqhtO4xxKJNFXRo//J/SdQvtt/jlW6zRMkNiZRwq FtbYg/EBNazWX29LgXpGeUoCcQ== X-Google-Smtp-Source: AH8x225Fq0RQVPSqpFMKNIwHxeMCC5s4xmnWOW570xZVH9qLDftctqmyqK1+lFCJdkib+pEZ8RNGRQ== X-Received: by 10.46.68.194 with SMTP id b63mr2765292ljf.123.1518729841479; Thu, 15 Feb 2018 13:24:01 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id 68sm1979049lfs.6.2018.02.15.13.24.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Feb 2018 13:24:00 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Cc: dsahern@gmail.com Subject: [PATCH iproute2-next v5 8/9] utils: Introduce and use print_name_and_link() to print name@link Date: Thu, 15 Feb 2018 23:23:23 +0200 Message-Id: <1518729804-19873-9-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1518729804-19873-1-git-send-email-serhe.popovych@gmail.com> References: <1518729804-19873-1-git-send-email-serhe.popovych@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org There is at least three places implementing same things: two in ipaddress.c print_linkinfo() & print_linkinfo_brief() and one in bridge/link.c. They are diverge from each other very little: bridge/link.c does not support JSON output at the moment and print_linkinfo_brief() does not handle IFLA_LINK_NETNS case. Introduce and use print_name_and_link() routine to handle name@link output in all possible variations; respect IFLA_LINK_NETNS attribute to handle case when link is in different namespace; use ll_idx_n2a() for interface name instead of "" to share logic with other code (e.g. ll_name_to_index() and ll_index_to_name()) supporting such template. Signed-off-by: Serhey Popovych --- bridge/link.c | 13 +++---------- include/utils.h | 4 ++++ ip/ipaddress.c | 44 ++------------------------------------------ lib/utils.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 52 deletions(-) diff --git a/bridge/link.c b/bridge/link.c index a11cbb1..90c9734 100644 --- a/bridge/link.c +++ b/bridge/link.c @@ -125,20 +125,13 @@ int print_linkinfo(const struct sockaddr_nl *who, if (n->nlmsg_type == RTM_DELLINK) fprintf(fp, "Deleted "); - fprintf(fp, "%d: %s ", ifi->ifi_index, - tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : ""); + fprintf(fp, "%d: ", ifi->ifi_index); + + print_name_and_link("%s: ", COLOR_NONE, name, tb); if (tb[IFLA_OPERSTATE]) print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE])); - if (tb[IFLA_LINK]) { - int iflink = rta_getattr_u32(tb[IFLA_LINK]); - - fprintf(fp, "@%s: ", - iflink ? ll_index_to_name(iflink) : "NONE"); - } else - fprintf(fp, ": "); - print_link_flags(fp, ifi->ifi_flags); if (tb[IFLA_MTU]) diff --git a/include/utils.h b/include/utils.h index 84ca873..75ddb4a 100644 --- a/include/utils.h +++ b/include/utils.h @@ -12,6 +12,7 @@ #include "libnetlink.h" #include "ll_map.h" #include "rtm_map.h" +#include "json_print.h" extern int preferred_family; extern int human_readable; @@ -250,6 +251,9 @@ void print_escape_buf(const __u8 *buf, size_t len, const char *escape); int print_timestamp(FILE *fp); void print_nlmsg_timestamp(FILE *fp, const struct nlmsghdr *n); +unsigned int print_name_and_link(const char *fmt, enum color_attr color, + const char *name, struct rtattr *tb[]); + #define BIT(nr) (1UL << (nr)) #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) diff --git a/ip/ipaddress.c b/ip/ipaddress.c index 08d2576..90cb5e5 100644 --- a/ip/ipaddress.c +++ b/ip/ipaddress.c @@ -760,7 +760,6 @@ int print_linkinfo_brief(const struct sockaddr_nl *who, struct rtattr *tb[IFLA_MAX+1]; int len = n->nlmsg_len; const char *name; - char buf[32] = { 0, }; unsigned int m_flag = 0; if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK) @@ -810,25 +809,7 @@ int print_linkinfo_brief(const struct sockaddr_nl *who, if (n->nlmsg_type == RTM_DELLINK) print_bool(PRINT_ANY, "deleted", "Deleted ", true); - if (tb[IFLA_LINK]) { - int iflink = rta_getattr_u32(tb[IFLA_LINK]); - - if (iflink == 0) { - snprintf(buf, sizeof(buf), "%s@NONE", name); - print_null(PRINT_JSON, "link", NULL, NULL); - } else { - const char *link = ll_index_to_name(iflink); - - print_string(PRINT_JSON, "link", NULL, link); - snprintf(buf, sizeof(buf), "%s@%s", name, link); - m_flag = ll_index_to_flags(iflink); - m_flag = !(m_flag & IFF_UP); - } - } else - snprintf(buf, sizeof(buf), "%s", name); - - print_string(PRINT_FP, NULL, "%-16s ", buf); - print_string(PRINT_JSON, "ifname", NULL, name); + m_flag = print_name_and_link("%-16s ", COLOR_NONE, name, tb); if (tb[IFLA_OPERSTATE]) print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE])); @@ -936,29 +917,8 @@ int print_linkinfo(const struct sockaddr_nl *who, print_bool(PRINT_ANY, "deleted", "Deleted ", true); print_int(PRINT_ANY, "ifindex", "%d: ", ifi->ifi_index); - print_color_string(PRINT_ANY, COLOR_IFNAME, "ifname", "%s", name); - - if (tb[IFLA_LINK]) { - int iflink = rta_getattr_u32(tb[IFLA_LINK]); - if (iflink == 0) - print_null(PRINT_ANY, "link", "@%s: ", "NONE"); - else { - if (tb[IFLA_LINK_NETNSID]) - print_int(PRINT_ANY, - "link_index", "@if%d: ", iflink); - else { - print_string(PRINT_ANY, - "link", - "@%s: ", - ll_index_to_name(iflink)); - m_flag = ll_index_to_flags(iflink); - m_flag = !(m_flag & IFF_UP); - } - } - } else { - print_string(PRINT_FP, NULL, ": ", NULL); - } + m_flag = print_name_and_link("%s: ", COLOR_IFNAME, name, tb); print_link_flags(fp, ifi->ifi_flags, m_flag); if (tb[IFLA_MTU]) diff --git a/lib/utils.c b/lib/utils.c index 572d42a..0f9523c 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -33,6 +33,7 @@ #include "rt_names.h" #include "utils.h" +#include "ll_map.h" #include "namespace.h" int resolve_hosts; @@ -1260,6 +1261,54 @@ int print_timestamp(FILE *fp) return 0; } +unsigned int print_name_and_link(const char *fmt, enum color_attr color, + const char *name, struct rtattr *tb[]) +{ + const char *link = NULL; + unsigned int m_flag = 0; + SPRINT_BUF(b1); + + if (tb[IFLA_LINK]) { + int iflink = rta_getattr_u32(tb[IFLA_LINK]); + + if (iflink) { + if (tb[IFLA_LINK_NETNSID]) { + if (is_json_context()) { + print_int(PRINT_JSON, + "link_index", NULL, iflink); + } else { + link = ll_idx_n2a(iflink); + } + } else { + link = ll_index_to_name(iflink); + + if (is_json_context()) { + print_string(PRINT_JSON, + "link", NULL, link); + link = NULL; + } + + m_flag = ll_index_to_flags(iflink); + m_flag = !(m_flag & IFF_UP); + } + } else { + if (is_json_context()) + print_null(PRINT_JSON, "link", NULL, NULL); + else + link = "NONE"; + } + + if (link) { + snprintf(b1, sizeof(b1), "%s@%s", name, link); + name = b1; + } + } + + print_color_string(PRINT_ANY, color, "ifname", fmt, name); + + return m_flag; +} + int cmdlineno; /* Like glibc getline but handle continuation lines and comments */ From patchwork Thu Feb 15 21:23:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 874132 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="UUL/hVf8"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zj8RR2VV0z9t20 for ; Fri, 16 Feb 2018 08:24:19 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1167394AbeBOVYI (ORCPT ); Thu, 15 Feb 2018 16:24:08 -0500 Received: from mail-lf0-f65.google.com ([209.85.215.65]:32896 "EHLO mail-lf0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1161516AbeBOVYE (ORCPT ); Thu, 15 Feb 2018 16:24:04 -0500 Received: by mail-lf0-f65.google.com with SMTP id j193so1503643lfe.0 for ; Thu, 15 Feb 2018 13:24:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=8sg60nVvkjslEgWgi21vrCOqrVUGL/h220fd6GMhXng=; b=UUL/hVf8AUChE0fsr7VfjQ3N7AJLCY5/g7/ndqRIrWhkmmZHhyRNKoRQI1nj+8UGj7 JHuyplznfheI2AN27meGVvnZeSd/G+BEdsYaFgnCvLuTvVfOyvHXd6ZVbH1pZsgxo6JP hkAOXMwzmdzGzrTn6kUKlIMlbbUMnyXaX84YQy3BqyyDjBrPre0WpedSilsQh9VhsASS z0gkubO8a0fmpzn4nG+61C1oPNuqe9qsn/yE54b16f7yXdGth3hRt+XG92wbxFHPXyEE MnRxp5v6ks2y1PLlHVAwWUBQHW/yhl0gcoL8/D/cQ8wtSNTKDUOghauvVp3+NBb7RqTL X4SA== 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:in-reply-to :references; bh=8sg60nVvkjslEgWgi21vrCOqrVUGL/h220fd6GMhXng=; b=mo73bx8w74U7BJ9sCY8Us3rKKH2e7DmJY+gZa/JsRdl+zRGJm53OXB3lYxMdmIIFa7 rXmOrzrW/8KLAaoh2XlsaZB1c3bV3QexPiQ/q8KInblnQ7M/25yAsyuciUX2Hhd0d/lL A2F6IFDjg6SvFqJdC/zhip9mjoEs+9T6LeaaPsYo17DO1yEEYCLRFll3Ss3dV9lwccCP FypchNaOfD0OO0DWMusl3nwwKJ6/BfHXJTPWth7A08dTgYXZ+otzu7K9kGsSkx8T3LhR MS31QaLimayJdRZvDG+FPxMM1VEyhBTXnxNE1EH1HD9cHn03ZH1hPBB7cx2TMP3r63i4 GTHQ== X-Gm-Message-State: APf1xPB+1HD8rVpQWMZJSnw2zlXHlra7H2KPF14YVava5Gg4+cZ4+9EW oCem87ZSQEYOSzxJWjZs9LsXIA== X-Google-Smtp-Source: AH8x224Qef3boW/x/V/behXHa1sLijIv2M9vaZJxILtok62hbgZtAZazeILE5FYJkyc3lbERFxuz4w== X-Received: by 10.46.80.85 with SMTP id v21mr2813193ljd.96.1518729842526; Thu, 15 Feb 2018 13:24:02 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id 68sm1979049lfs.6.2018.02.15.13.24.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Feb 2018 13:24:01 -0800 (PST) From: Serhey Popovych To: netdev@vger.kernel.org Cc: dsahern@gmail.com Subject: [PATCH iproute2-next v5 9/9] ipaddress: Make print_linkinfo_brief() static Date: Thu, 15 Feb 2018 23:23:24 +0200 Message-Id: <1518729804-19873-10-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1518729804-19873-1-git-send-email-serhe.popovych@gmail.com> References: <1518729804-19873-1-git-send-email-serhe.popovych@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org It shares lot of code with print_linkinfo(): drop duplicated part, change parameters list, make it static and call from print_linkinfo() after common path. While there move SPRINT_BUF() to the function scope from blocks to avoid duplication and use "%s" to print "\n" to help compiler optimize exit for both print_linkinfo_brief() and normal paths. Signed-off-by: Serhey Popovych --- ip/ip_common.h | 2 -- ip/ipaddress.c | 76 ++++++++------------------------------------------------ ip/iplink.c | 5 +--- 3 files changed, 11 insertions(+), 72 deletions(-) diff --git a/ip/ip_common.h b/ip/ip_common.h index 1397d99..e4e628b 100644 --- a/ip/ip_common.h +++ b/ip/ip_common.h @@ -29,8 +29,6 @@ struct link_filter { int get_operstate(const char *name); int print_linkinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg); -int print_linkinfo_brief(const struct sockaddr_nl *who, - struct nlmsghdr *n, void *arg); int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg); int print_addrlabel(const struct sockaddr_nl *who, diff --git a/ip/ipaddress.c b/ip/ipaddress.c index 90cb5e5..1380453 100644 --- a/ip/ipaddress.c +++ b/ip/ipaddress.c @@ -752,63 +752,12 @@ static void print_link_stats(FILE *fp, struct nlmsghdr *n) fprintf(fp, "%s", _SL_); } -int print_linkinfo_brief(const struct sockaddr_nl *who, - struct nlmsghdr *n, void *arg) +static int print_linkinfo_brief(FILE *fp, const char *name, + const struct ifinfomsg *ifi, + struct rtattr *tb[]) { - FILE *fp = (FILE *)arg; - struct ifinfomsg *ifi = NLMSG_DATA(n); - struct rtattr *tb[IFLA_MAX+1]; - int len = n->nlmsg_len; - const char *name; unsigned int m_flag = 0; - if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK) - return -1; - - len -= NLMSG_LENGTH(sizeof(*ifi)); - if (len < 0) - return -1; - - if (filter.ifindex && ifi->ifi_index != filter.ifindex) - return -1; - if (filter.up && !(ifi->ifi_flags&IFF_UP)) - return -1; - - parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); - - name = get_ifname_rta(ifi->ifi_index, tb[IFLA_IFNAME]); - if (!name) - return -1; - - if (filter.label && - (!filter.family || filter.family == AF_PACKET) && - fnmatch(filter.label, name, 0)) - return -1; - - if (tb[IFLA_GROUP]) { - int group = rta_getattr_u32(tb[IFLA_GROUP]); - - if (filter.group != -1 && group != filter.group) - return -1; - } - - if (tb[IFLA_MASTER]) { - int master = rta_getattr_u32(tb[IFLA_MASTER]); - - if (filter.master > 0 && master != filter.master) - return -1; - } else if (filter.master > 0) - return -1; - - if (filter.kind && match_link_kind(tb, filter.kind, 0)) - return -1; - - if (filter.slave_kind && match_link_kind(tb, filter.slave_kind, 1)) - return -1; - - if (n->nlmsg_type == RTM_DELLINK) - print_bool(PRINT_ANY, "deleted", "Deleted ", true); - m_flag = print_name_and_link("%-16s ", COLOR_NONE, name, tb); if (tb[IFLA_OPERSTATE]) @@ -868,6 +817,7 @@ int print_linkinfo(const struct sockaddr_nl *who, int len = n->nlmsg_len; const char *name; unsigned int m_flag = 0; + SPRINT_BUF(b1); if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK) return 0; @@ -916,6 +866,9 @@ int print_linkinfo(const struct sockaddr_nl *who, if (n->nlmsg_type == RTM_DELLINK) print_bool(PRINT_ANY, "deleted", "Deleted ", true); + if (brief) + return print_linkinfo_brief(fp, name, ifi, tb); + print_int(PRINT_ANY, "ifindex", "%d: ", ifi->ifi_index); m_flag = print_name_and_link("%s: ", COLOR_IFNAME, name, tb); @@ -949,7 +902,6 @@ int print_linkinfo(const struct sockaddr_nl *who, print_linkmode(fp, tb[IFLA_LINKMODE]); if (tb[IFLA_GROUP]) { - SPRINT_BUF(b1); int group = rta_getattr_u32(tb[IFLA_GROUP]); print_string(PRINT_ANY, @@ -965,8 +917,6 @@ int print_linkinfo(const struct sockaddr_nl *who, print_link_event(fp, rta_getattr_u32(tb[IFLA_EVENT])); if (!filter.family || filter.family == AF_PACKET || show_details) { - SPRINT_BUF(b1); - print_string(PRINT_FP, NULL, "%s", _SL_); print_string(PRINT_ANY, "link_type", @@ -1066,7 +1016,6 @@ int print_linkinfo(const struct sockaddr_nl *who, rta_getattr_str(tb[IFLA_PHYS_PORT_NAME])); if (tb[IFLA_PHYS_PORT_ID]) { - SPRINT_BUF(b1); print_string(PRINT_ANY, "phys_port_id", "portid %s ", @@ -1077,7 +1026,6 @@ int print_linkinfo(const struct sockaddr_nl *who, } if (tb[IFLA_PHYS_SWITCH_ID]) { - SPRINT_BUF(b1); print_string(PRINT_ANY, "phys_switch_id", "switchid %s ", @@ -1116,7 +1064,7 @@ int print_linkinfo(const struct sockaddr_nl *who, close_json_array(PRINT_JSON, NULL); } - print_string(PRINT_FP, NULL, "\n", NULL); + print_string(PRINT_FP, NULL, "%s", "\n"); fflush(fp); return 1; } @@ -1971,14 +1919,10 @@ static int ipaddr_list_flush_or_save(int argc, char **argv, int action) for (l = linfo.head; l; l = l->next) { struct nlmsghdr *n = &l->h; struct ifinfomsg *ifi = NLMSG_DATA(n); - int res; + int res = 0; open_json_object(NULL); - if (brief) - res = print_linkinfo_brief(NULL, n, stdout); - else if (no_link) - res = 0; - else + if (brief || !no_link) res = print_linkinfo(NULL, n, stdout); if (res >= 0 && filter.family != AF_PACKET) print_selected_addrinfo(ifi, ainfo->head, stdout); diff --git a/ip/iplink.c b/ip/iplink.c index 3d7f7fa..74c377c 100644 --- a/ip/iplink.c +++ b/ip/iplink.c @@ -1075,10 +1075,7 @@ int iplink_get(unsigned int flags, char *name, __u32 filt_mask) return -2; open_json_object(NULL); - if (brief) - print_linkinfo_brief(NULL, answer, stdout); - else - print_linkinfo(NULL, answer, stdout); + print_linkinfo(NULL, answer, stdout); close_json_object(); free(answer);