From mboxrd@z Thu Jan 1 00:00:00 1970 From: Serhey Popovych Subject: [PATCH iproute2 2/3] utils: ll_map: Update name and type for existing entry Date: Wed, 20 Dec 2017 09:37:30 +0200 Message-ID: <1513755451-9800-3-git-send-email-serhe.popovych@gmail.com> References: <1513755451-9800-1-git-send-email-serhe.popovych@gmail.com> To: netdev@vger.kernel.org Return-path: Received: from mail-lf0-f68.google.com ([209.85.215.68]:37854 "EHLO mail-lf0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932317AbdLTHiH (ORCPT ); Wed, 20 Dec 2017 02:38:07 -0500 Received: by mail-lf0-f68.google.com with SMTP id a12so22910964lfe.4 for ; Tue, 19 Dec 2017 23:38:06 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id a9sm3722880lfg.12.2017.12.19.23.38.04 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Dec 2017 23:38:05 -0800 (PST) In-Reply-To: <1513755451-9800-1-git-send-email-serhe.popovych@gmail.com> Sender: netdev-owner@vger.kernel.org List-ID: In case of we update existing entry we need not only rehash but also update name in existing entry. Need to update device type too since cached interface might be deleted and new with same index, but different type added (e.g. eth0 and ppp0). Reuse new entry initialization path to avoid duplications. Signed-off-by: Serhey Popovych --- lib/ll_map.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/lib/ll_map.c b/lib/ll_map.c index f65614f..abe7bdc 100644 --- a/lib/ll_map.c +++ b/lib/ll_map.c @@ -10,6 +10,7 @@ * */ +#include #include #include #include @@ -85,6 +86,7 @@ int ll_remember_index(const struct sockaddr_nl *who, struct ifinfomsg *ifi = NLMSG_DATA(n); struct ll_cache *im; struct rtattr *tb[IFLA_MAX+1]; + bool rehash; if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK) return 0; @@ -109,29 +111,30 @@ int ll_remember_index(const struct sockaddr_nl *who, if (im) { /* change to existing entry */ - if (strcmp(im->name, ifname) != 0) { + rehash = strcmp(im->name, ifname); + if (rehash) hlist_del(&im->name_hash); - h = namehash(ifname) & (IDXMAP_SIZE - 1); - hlist_add_head(&im->name_hash, &name_head[h]); - } + } else { + im = malloc(sizeof(*im) + strlen(ifname) + 1); + if (!im) + return 0; + im->index = ifi->ifi_index; - im->flags = ifi->ifi_flags; - return 0; + h = ifi->ifi_index & (IDXMAP_SIZE - 1); + hlist_add_head(&im->idx_hash, &idx_head[h]); + + rehash = true; } - im = malloc(sizeof(*im) + strlen(ifname) + 1); - if (im == NULL) - return 0; - im->index = ifi->ifi_index; - strcpy(im->name, ifname); im->type = ifi->ifi_type; im->flags = ifi->ifi_flags; - h = ifi->ifi_index & (IDXMAP_SIZE - 1); - hlist_add_head(&im->idx_hash, &idx_head[h]); + if (rehash) { + h = namehash(ifname) & (IDXMAP_SIZE - 1); + hlist_add_head(&im->name_hash, &name_head[h]); - h = namehash(ifname) & (IDXMAP_SIZE - 1); - hlist_add_head(&im->name_hash, &name_head[h]); + strcpy(im->name, ifname); + } return 0; } -- 1.7.10.4