From patchwork Tue May 6 15:36:34 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris BREZILLON X-Patchwork-Id: 4122441 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 103AABFF02 for ; Tue, 6 May 2014 15:39:18 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 821C3201EF for ; Tue, 6 May 2014 15:39:12 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9ADFD201ED for ; Tue, 6 May 2014 15:39:11 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1WhhQV-0006jJ-2N; Tue, 06 May 2014 15:37:03 +0000 Received: from top.free-electrons.com ([176.31.233.9] helo=mail.free-electrons.com) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1WhhQS-0006OU-8C for linux-arm-kernel@lists.infradead.org; Tue, 06 May 2014 15:37:00 +0000 Received: by mail.free-electrons.com (Postfix, from userid 106) id 6502B1240; Tue, 6 May 2014 17:36:36 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Spam-Level: X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 Received: from localhost.localdomain (col31-4-88-188-83-94.fbx.proxad.net [88.188.83.94]) by mail.free-electrons.com (Postfix) with ESMTPSA id D45A1CFD; Tue, 6 May 2014 17:36:35 +0200 (CEST) From: Boris BREZILLON To: "David S. Miller" Subject: [PATCH] netdev: add support for interface name retrieval from DT aliases Date: Tue, 6 May 2014 17:36:34 +0200 Message-Id: <1399390594-1409-1-git-send-email-boris.brezillon@free-electrons.com> X-Mailer: git-send-email 1.8.3.2 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20140506_083700_589441_EF70126A X-CRM114-Status: GOOD ( 16.71 ) X-Spam-Score: 0.3 (/) Cc: Boris BREZILLON , netdev@vger.kernel.org, Nicolas Ferre , linux-kernel@vger.kernel.org, Olof Johansson , linux-arm-kernel@lists.infradead.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP There is currently no proper way to bind a net interface to a specific name. The interface name is chosen based on the interface type (eth, wlan, ...) and the interfaces already registered (the core codes takes the first unused interface id of the given type). Add support for DT retrieval of the interface id based on DT aliases. The alias name must match the interface type (e.g. ethX if you're defining an ethernet dev alias). Signed-off-by: Boris BREZILLON --- net/core/dev.c | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/net/core/dev.c b/net/core/dev.c index d2c8a06..8f30cb8 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -132,6 +132,7 @@ #include #include #include +#include #include "net-sysfs.h" @@ -960,13 +961,19 @@ EXPORT_SYMBOL(dev_valid_name); * Returns the number of the unit assigned or a negative errno code. */ -static int __dev_alloc_name(struct net *net, const char *name, char *buf) +static int __dev_alloc_name(struct net *net, struct net_device *dev, + const char *name, char *buf) { int i = 0; + int of_id = -EINVAL; const char *p; const int max_netdevices = 8*PAGE_SIZE; unsigned long *inuse; struct net_device *d; + struct device_node *np = NULL; + + if (dev->dev.parent) + np = dev->dev.parent->of_node; p = strnchr(name, IFNAMSIZ-1, '%'); if (p) { @@ -978,11 +985,38 @@ static int __dev_alloc_name(struct net *net, const char *name, char *buf) if (p[1] != 'd' || strchr(p + 2, '%')) return -EINVAL; + if (np) { + strlcpy(buf, name, (size_t)(p + 1 - name)); + of_id = of_alias_get_id(np, buf); + } + /* Use one page as a bit array of possible slots */ inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC); if (!inuse) return -ENOMEM; +#ifdef CONFIG_OF + /* iterate over aliases to reserve interfaces names */ + np = of_find_node_by_path("/aliases"); + if (np) { + struct property *pp; + for_each_property_of_node(np, pp) { + if (!sscanf(pp->name, name, &i)) + continue; + if (i < 0 || i >= max_netdevices) + continue; + + /* avoid cases where sscanf is not exact + * inverse of printf + */ + snprintf(buf, IFNAMSIZ, name, i); + if (!strncmp(buf, pp->name, IFNAMSIZ) && + i != of_id) + set_bit(i, inuse); + } + } +#endif + for_each_netdev(net, d) { if (!sscanf(d->name, name, &i)) continue; @@ -995,7 +1029,10 @@ static int __dev_alloc_name(struct net *net, const char *name, char *buf) set_bit(i, inuse); } - i = find_first_zero_bit(inuse, max_netdevices); + if (of_id >= 0 && !test_bit(of_id, inuse)) + i = of_id; + else + i = find_first_zero_bit(inuse, max_netdevices); free_page((unsigned long) inuse); } @@ -1033,7 +1070,7 @@ int dev_alloc_name(struct net_device *dev, const char *name) BUG_ON(!dev_net(dev)); net = dev_net(dev); - ret = __dev_alloc_name(net, name, buf); + ret = __dev_alloc_name(net, dev, name, buf); if (ret >= 0) strlcpy(dev->name, buf, IFNAMSIZ); return ret; @@ -1047,7 +1084,7 @@ static int dev_alloc_name_ns(struct net *net, char buf[IFNAMSIZ]; int ret; - ret = __dev_alloc_name(net, name, buf); + ret = __dev_alloc_name(net, dev, name, buf); if (ret >= 0) strlcpy(dev->name, buf, IFNAMSIZ); return ret;