From patchwork Mon Jun 6 12:58:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans Dedecker X-Patchwork-Id: 630881 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from arrakis.dune.hu (caladan.dune.hu [78.24.191.180]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rNZWs4Z2Yz9t5V for ; Mon, 6 Jun 2016 22:58:49 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=t54kQ0Ty; dkim-atps=neutral Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 99333B91FF8; Mon, 6 Jun 2016 14:58:42 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on arrakis.dune.hu X-Spam-Level: X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,FREEMAIL_FROM, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.1 Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP; Mon, 6 Jun 2016 14:58:42 +0200 (CEST) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 179A4B91FF2 for ; Mon, 6 Jun 2016 14:58:41 +0200 (CEST) X-policyd-weight: using cached result; rate:hard: -7 Received: from mail-wm0-f68.google.com (mail-wm0-f68.google.com [74.125.82.68]) by arrakis.dune.hu (Postfix) with ESMTPS for ; Mon, 6 Jun 2016 14:58:40 +0200 (CEST) Received: by mail-wm0-f68.google.com with SMTP id n184so16299822wmn.1 for ; Mon, 06 Jun 2016 05:58:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=2dR5fnm9g34OQYOQ0LqBmPzzjq0xlUoYSWrNw94tSmY=; b=t54kQ0TyFbFitdS7pDgIZ8+M1CGq8Pvtn/PJif/4nmt+RzTkYi5PCNX/fOVKrit667 QTE1M0rFybXw5r5HIr/LppI7CQM4dR+oJdgc98H5CIWsqNmQPOPGaGoELCd6iBrOykxt lwYUrfP/vh71TtXBa8swS4yMG5/fWG7y5zDChKdPhzZNA1/3Pw3lg9yv+aOG7hshvleQ PQNE24FZXdfc+KxODkqQ24mcAWg8pKenSqyxfeeRxKd9duRsTQNRcvtP1/giUFLYUfI6 9qSeHaUuPQkQp86v2O94gpJWmY4Ug4aCO0KjSY6MujeIsODsNaO2wdp1+83broP92POm AXEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=2dR5fnm9g34OQYOQ0LqBmPzzjq0xlUoYSWrNw94tSmY=; b=S7vN3ykXsQGtn67gvL0t3MMT/3ofkN87yWPaY6r5u7K+vjevcVbhsI/mCUQCvj+yw4 RloBvvEGSD2/VT4G4ZTXeIPUwZyCV2s+Kk6mgdvnX91UepfWjqyJHFV+cKt90jJxwOcH KjUK2zyT8d9bsSxUbQWbHXuZUInUUDFeCIJLd/lcMQ2cCWu0u3RNepipoFBW4ZAdLiHr XxGo1sWN+c+dR8MtjONpfkba8pY6+5WlCxnptBUGUQQ9XZ7LLPjcaU1QST87By7e6jvw 9IB3cu73Wf0SFEtOtPuq1lpLf8u5Q6vXZqZgSw230llLyC7wJGOFk5/W+dYDPSkMYjwB 4R+A== X-Gm-Message-State: ALyK8tIUeA4TIoLboLmYZZUlgdg6nfMte3qlU+eNPs7IXu3T1t/Wv4+f+AzUeh2Zk9U+2Q== X-Received: by 10.28.218.10 with SMTP id r10mr11326263wmg.18.1465217920373; Mon, 06 Jun 2016 05:58:40 -0700 (PDT) Received: from cplx43.eu.thmulti.com ([141.11.62.7]) by smtp.gmail.com with ESMTPSA id b189sm14127828wmh.5.2016.06.06.05.58.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 06 Jun 2016 05:58:39 -0700 (PDT) From: Hans Dedecker To: lede-dev@lists.infradead.org, openwrt-devel@lists.openwrt.org Date: Mon, 6 Jun 2016 14:58:31 +0200 Message-Id: <1465217913-20861-1-git-send-email-dedeckeh@gmail.com> X-Mailer: git-send-email 1.9.1 Subject: [OpenWrt-Devel] [PATCH v2 netifd 1/3] device: Fix device find failure in avl list due to device name change X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Hans Dedecker , steven@midlink.org MIME-Version: 1.0 Errors-To: openwrt-devel-bounces@lists.openwrt.org Sender: "openwrt-devel" As device name is used as key in avl list a device name change will break the avl find logic. Function device_set_ifname offers api to set the device name and re-inserts the avl node in the list when the avl key value is changed. Signed-off-by: Hans Dedecker --- v1 -> v2 : Fix debug print in device_init_virtual alias.c | 9 +++------ device.c | 29 +++++++++++++++++++++++++---- device.h | 1 + vlan.c | 11 +++++++---- 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/alias.c b/alias.c index e4bb700..6b938ca 100644 --- a/alias.c +++ b/alias.c @@ -59,13 +59,10 @@ static void alias_set_device(struct alias_device *alias, struct device *dev) alias->dev.hidden = !dev; if (dev) { device_set_ifindex(&alias->dev, dev->ifindex); - strcpy(alias->dev.ifname, dev->ifname); - device_broadcast_event(&alias->dev, DEV_EVENT_UPDATE_IFNAME); + device_set_ifname(&alias->dev, dev->ifname); device_add_user(&alias->dep, dev); - } else { - alias->dev.ifname[0] = 0; - device_broadcast_event(&alias->dev, DEV_EVENT_UPDATE_IFNAME); - } + } else + device_set_ifname(&alias->dev, ""); } static int diff --git a/device.c b/device.c index 06c4820..a4d804e 100644 --- a/device.c +++ b/device.c @@ -422,14 +422,14 @@ void device_init_virtual(struct device *dev, const struct device_type *type, con assert(dev); assert(type); - if (name) - strncpy(dev->ifname, name, IFNAMSIZ); - - D(DEVICE, "Initialize device '%s'\n", dev->ifname); + D(DEVICE, "Initialize device '%s'\n", name ? name : ""); INIT_SAFE_LIST(&dev->users); INIT_SAFE_LIST(&dev->aliases); dev->type = type; + if (name) + device_set_ifname(dev, name); + if (!dev->set_state) dev->set_state = set_device_state; } @@ -582,6 +582,27 @@ void device_set_ifindex(struct device *dev, int ifindex) device_broadcast_event(dev, DEV_EVENT_UPDATE_IFINDEX); } +int device_set_ifname(struct device *dev, const char *name) +{ + int ret = 0; + + if (!strcmp(dev->ifname, name)) + return 0; + + if (dev->avl.key) + avl_delete(&devices, &dev->avl); + + strncpy(dev->ifname, name, IFNAMSIZ); + + if (dev->avl.key) + ret = avl_insert(&devices, &dev->avl); + + if (ret == 0) + device_broadcast_event(dev, DEV_EVENT_UPDATE_IFNAME); + + return ret; +} + static int device_refcount(struct device *dev) { struct list_head *list; diff --git a/device.h b/device.h index 77a2fef..b069a2b 100644 --- a/device.h +++ b/device.h @@ -250,6 +250,7 @@ void device_broadcast_event(struct device *dev, enum device_event ev); void device_set_present(struct device *dev, bool state); void device_set_link(struct device *dev, bool state); void device_set_ifindex(struct device *dev, int ifindex); +int device_set_ifname(struct device *dev, const char *name); void device_refresh_present(struct device *dev); int device_claim(struct device_user *dep); void device_release(struct device_user *dep); diff --git a/vlan.c b/vlan.c index ac434ce..7f8697b 100644 --- a/vlan.c +++ b/vlan.c @@ -63,8 +63,11 @@ static int vlan_set_device_state(struct device *dev, bool up) static void vlan_dev_set_name(struct vlan_device *vldev, struct device *dev) { + char name[IFNAMSIZ]; + vldev->dev.hidden = dev->hidden; - snprintf(vldev->dev.ifname, IFNAMSIZ, "%s.%d", dev->ifname, vldev->id); + snprintf(name, IFNAMSIZ, "%s.%d", dev->ifname, vldev->id); + device_set_ifname(&vldev->dev, name); } static void vlan_dev_cb(struct device_user *dep, enum device_event ev) @@ -81,7 +84,6 @@ static void vlan_dev_cb(struct device_user *dep, enum device_event ev) break; case DEV_EVENT_UPDATE_IFNAME: vlan_dev_set_name(vldev, dep->dev); - device_broadcast_event(&vldev->dev, ev); break; case DEV_EVENT_TOPO_CHANGE: /* Propagate topo changes */ @@ -124,9 +126,10 @@ static struct device *get_vlan_device(struct device *dev, int id, bool create) return NULL; vldev->id = id; - vlan_dev_set_name(vldev, dev); - device_init(&vldev->dev, &vlan_type, vldev->dev.ifname); + device_init(&vldev->dev, &vlan_type, NULL); + + vlan_dev_set_name(vldev, dev); vldev->dev.default_config = true; vldev->set_state = vldev->dev.set_state;