From patchwork Sat Jun 12 08:20:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Loic Poulain X-Patchwork-Id: 459152 Delivered-To: patch@linaro.org Received: by 2002:a02:735a:0:0:0:0:0 with SMTP id a26csp1612476jae; Sat, 12 Jun 2021 01:13:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwqxT04Ifmw5QIxiSkNH3ea+d6Kch6OCUgiW6kuggRbalHJiKOfT1U1n4bi1wlgK808X4Xo X-Received: by 2002:a05:6402:1d0f:: with SMTP id dg15mr7572781edb.137.1623485587976; Sat, 12 Jun 2021 01:13:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623485587; cv=none; d=google.com; s=arc-20160816; b=dyw5fe/lnhhLtUYWz6gUhpvpWs7W9OK28B2AlEHL0+bfQoG948VpGqic2HkmlfBLWQ laI2SHHLE6dutRLgCW/i62zjJUpSt4yCwNV4QqTgxGgCdQps/mw69T0DSYxkHEXhGbZi lKPvjuFKuz/JVOptBx53l+C435h62AE2nPRPXOrSlZrxcLV+oayoY9PHmYgzqAKkh4TC LBoXSSiB62/9sSYPQCnj2DAw7yyy2KLh5EIKralnGKty9ryx4cfUj306lz0Mqf+x+0D2 z0+4lSPV0n7d4B17wbfoW2EPQmK/wDOF0bAdTO6bmsLasIHld+XdReD545nv/1wWh8xt VsAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=pAaCMxF9sS+QVyZSSeidUbHEHKD3loHR2IcQcTMXKSQ=; b=fSMlm2EUlEtdSbQY1DLBOkyGwSGAqiVnL+eoSYK1j3CzS6wz8zuBzdyKa93Dx8uLkR ok4/KcT+VGGfbbK9mYvFGd5oeGA12iQ7/Cap6V12iD1TsQvF+S0x0PmqE/MzeBwpPKl9 CHetgJx8b6y3TSHi/oKUL+awhL9zQ5dIQ6RzGbo0/JyEghQ2Gyi8z8wAg5jaq6zyC/gx ReLHl3s5RqeDCaIpZKMfy/XyjuLNzx8hgIXQRDEGV/XkflV6iRWdxfMeqfLAznZqRQab /uh6lAX7xNG920YJ+4y5ZMdBgxIXxAcCk95aIZhbbp/OyqFCsdmreBUg+mQHv6c9ogJl 107A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Oh6mIdyM; spf=pass (google.com: domain of netdev-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=netdev-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h1si6728008ejo.462.2021.06.12.01.13.07; Sat, 12 Jun 2021 01:13:07 -0700 (PDT) Received-SPF: pass (google.com: domain of netdev-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Oh6mIdyM; spf=pass (google.com: domain of netdev-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=netdev-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231147AbhFLIOY (ORCPT + 8 others); Sat, 12 Jun 2021 04:14:24 -0400 Received: from mail-wr1-f54.google.com ([209.85.221.54]:40484 "EHLO mail-wr1-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229584AbhFLIOU (ORCPT ); Sat, 12 Jun 2021 04:14:20 -0400 Received: by mail-wr1-f54.google.com with SMTP id y7so8417303wrh.7 for ; Sat, 12 Jun 2021 01:12:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=pAaCMxF9sS+QVyZSSeidUbHEHKD3loHR2IcQcTMXKSQ=; b=Oh6mIdyMQ1qBMCPK6uUu/n/2a0Jp4mWKyJTLupCiWzpzjiQZEQzA52c5C07O2gqdW3 46JUJylcyIPz5w6zzhUGwTKBggf/szzEiWu1plETy5zey1xS8bZXjcu8baGtN8ePmJlQ JTmXjwTjrarZ0GPGOkWmyCrfc3kmqrRT2A/8AyjW+gExiFw4ixro64tfAGSKWxgvi0yL JOWgeddrr3wYVOpg+tGeuOyb/u9Lw5ZiJkUIV4MXUE9Oq6f/Rf7xFJr2mznlfjXrcqNl SWNpN9RIZYV899FOJUh/2zjwAq7XX8Ym7Kn4JCvWhP+zmjfsYP4WIeYXDzA1rkC3KmwC EYlw== 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=pAaCMxF9sS+QVyZSSeidUbHEHKD3loHR2IcQcTMXKSQ=; b=S38t18c5128Yo9qgPx012D2TrriCa4rEq6AqO+8QdsymBerRaxhumfNzOwKKEMzW9w 9wXR3oPMYaveI9DvY0wb0r7ONNqkp/R7igmwBhiQXEnlCz77Xakq+JlcW0losFXti2XT yWUdMsbj0DDkCFpnTM4QFqyYMXKpVv8ayuIBI4tArKiap6aJl6A9MX69FYhvpBwyqR26 JZf3Z7q8o2GCcrJGlrlpTKE32iF7yD6Gx+JVWqi6qjc/EoodBxRP7junCoWVRN0wi44Z UJlQM9VXgbNqwUsnZToVfZNcfWx9qaSUtM2TrXEUyjt7WsEnNCOluFCwafwsFxZDayxW E/iQ== X-Gm-Message-State: AOAM532nGE+CGOSodXeBjGTz53tj4CBnCf6huAXI5VaUbqvuYqj+yEvS Y3HBdIlK2Dv/BlSoeKG/pvtbvQ== X-Received: by 2002:adf:efc3:: with SMTP id i3mr7985579wrp.356.1623485480095; Sat, 12 Jun 2021 01:11:20 -0700 (PDT) Received: from localhost.localdomain ([2a01:e0a:82c:5f0:85ed:406e:1bc4:a268]) by smtp.gmail.com with ESMTPSA id w13sm10619313wrc.31.2021.06.12.01.11.19 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 12 Jun 2021 01:11:19 -0700 (PDT) From: Loic Poulain To: kuba@kernel.org, davem@davemloft.net Cc: netdev@vger.kernel.org, m.chetan.kumar@intel.com, johannes.berg@intel.com, leon@kernel.org, ryazanov.s.a@gmail.com, parav@nvidia.com Subject: [PATCH net-next v3 1/4] rtnetlink: add alloc() method to rtnl_link_ops Date: Sat, 12 Jun 2021 10:20:54 +0200 Message-Id: <1623486057-13075-2-git-send-email-loic.poulain@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1623486057-13075-1-git-send-email-loic.poulain@linaro.org> References: <1623486057-13075-1-git-send-email-loic.poulain@linaro.org> Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Johannes Berg In order to make rtnetlink ops that can create different kinds of devices, like what we want to add to the WWAN framework, the priv_size and setup parameters aren't quite sufficient. Make this easier to manage by allowing ops to allocate their own netdev via an @alloc method that gets the tb netlink data. Signed-off-by: Johannes Berg Signed-off-by: Sergey Ryazanov --- include/net/rtnetlink.h | 8 ++++++++ net/core/rtnetlink.c | 19 ++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) -- 2.7.4 diff --git a/include/net/rtnetlink.h b/include/net/rtnetlink.h index 479f60e..384e800 100644 --- a/include/net/rtnetlink.h +++ b/include/net/rtnetlink.h @@ -37,6 +37,9 @@ static inline int rtnl_msg_family(const struct nlmsghdr *nlh) * @maxtype: Highest device specific netlink attribute number * @policy: Netlink policy for device specific attribute validation * @validate: Optional validation function for netlink/changelink parameters + * @alloc: netdev allocation function, can be %NULL and is then used + * in place of alloc_netdev_mqs(), in this case @priv_size + * and @setup are unused. Returns a netdev or ERR_PTR(). * @priv_size: sizeof net_device private space * @setup: net_device setup function * @newlink: Function for configuring and registering a new device @@ -63,6 +66,11 @@ struct rtnl_link_ops { const char *kind; size_t priv_size; + struct net_device *(*alloc)(struct nlattr *tb[], + const char *ifname, + unsigned char name_assign_type, + unsigned int num_tx_queues, + unsigned int num_rx_queues); void (*setup)(struct net_device *dev); bool netns_refund; diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index cd87c76..92c3e43 100644 --- a/net/core/rtnetlink.c +++ b/net/core/rtnetlink.c @@ -376,12 +376,12 @@ int __rtnl_link_register(struct rtnl_link_ops *ops) if (rtnl_link_ops_get(ops->kind)) return -EEXIST; - /* The check for setup is here because if ops + /* The check for alloc/setup is here because if ops * does not have that filled up, it is not possible * to use the ops for creating device. So do not * fill up dellink as well. That disables rtnl_dellink. */ - if (ops->setup && !ops->dellink) + if ((ops->alloc || ops->setup) && !ops->dellink) ops->dellink = unregister_netdevice_queue; list_add_tail(&ops->list, &link_ops); @@ -3165,8 +3165,17 @@ struct net_device *rtnl_create_link(struct net *net, const char *ifname, return ERR_PTR(-EINVAL); } - dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, - ops->setup, num_tx_queues, num_rx_queues); + if (ops->alloc) { + dev = ops->alloc(tb, ifname, name_assign_type, + num_tx_queues, num_rx_queues); + if (IS_ERR(dev)) + return dev; + } else { + dev = alloc_netdev_mqs(ops->priv_size, ifname, + name_assign_type, ops->setup, + num_tx_queues, num_rx_queues); + } + if (!dev) return ERR_PTR(-ENOMEM); @@ -3399,7 +3408,7 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, return -EOPNOTSUPP; } - if (!ops->setup) + if (!ops->alloc && !ops->setup) return -EOPNOTSUPP; if (!ifname[0]) { From patchwork Sat Jun 12 08:20:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Loic Poulain X-Patchwork-Id: 459154 Delivered-To: patch@linaro.org Received: by 2002:a02:735a:0:0:0:0:0 with SMTP id a26csp1612484jae; Sat, 12 Jun 2021 01:13:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwbryE47PUcxBJ1q4eO4rFwbrKLJ4RYoAvnMUfYxYpa4hU5/lN1cGFEU9gvjEzyU0jq08A4 X-Received: by 2002:a17:906:fa93:: with SMTP id lt19mr7290896ejb.54.1623485588775; Sat, 12 Jun 2021 01:13:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623485588; cv=none; d=google.com; s=arc-20160816; b=gope9sa89d4pM3zzQLA8tQdqlPvkPvjCRlbWAmZwK6Aoc/QhJ+Ql7PJP/uZONO+IJ1 9zzxYz9+sNIfIQTyI1wgI3qn8WMYrPp2u1KOXrWsne5716pNgb23jr5ccbSutTaO5gk1 hPEo/mo9x5fEmvX569IxWc8Z0DzdPYSL4XsIMI2MmGEYxAbyoZB+yVv4xnudOPSvktFt tZ2AabIV7QJ8vWlEs7iWTsMJM0mdmdZJnGmL3xh2M7gJi8rWj06s0yapoN1wQ8u0LTHY +4DhfHH/dlTIeJeNgpwMT6vswRhA/OXEn+DqwyPXDV3Ta8XPBUknOfdJvFY/qdO48RzY 4UtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=d82HDQRFWnDbNi3qCp9COZeIUQc4rHUgiYVZqY067LM=; b=pZGdxrpngrScKXdci9Nj5pa8wK7Yi7N3w0hJg2aeUsekvj7pfImNMHieHbfAEQejee oN55Wv0SfpEi/KVYp7ngKkNv1WgWkamtxw6w98a092X/0SuKN3GYw6pyD9+uu4hzrhgy piHHaokEsgsEAmW+Vtak+xjtf4EJ3Of5Gotd9ONv/pcvK9Mw9CnWlYqzr20BYwHhQJud id6b+c1qXvY2rLtCNrvUaa2N+uk2WdZtLfoeQQEg1c0DrRKq0FxIZx2SdnkIsysnKWE2 gvft0AO4khGMkNuNDhNW8P/k4ZEdcxq5C+/UdFDZu6zPbcw4lZkBOspdJlmLcAL6D1HO wC/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=R6A2j60Y; spf=pass (google.com: domain of netdev-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=netdev-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h1si6728008ejo.462.2021.06.12.01.13.08; Sat, 12 Jun 2021 01:13:08 -0700 (PDT) Received-SPF: pass (google.com: domain of netdev-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=R6A2j60Y; spf=pass (google.com: domain of netdev-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=netdev-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231168AbhFLIOf (ORCPT + 8 others); Sat, 12 Jun 2021 04:14:35 -0400 Received: from mail-wr1-f47.google.com ([209.85.221.47]:34376 "EHLO mail-wr1-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231153AbhFLIOd (ORCPT ); Sat, 12 Jun 2021 04:14:33 -0400 Received: by mail-wr1-f47.google.com with SMTP id q5so8437383wrm.1 for ; Sat, 12 Jun 2021 01:12:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=d82HDQRFWnDbNi3qCp9COZeIUQc4rHUgiYVZqY067LM=; b=R6A2j60YLlbeT3YrQ2AiDVrCQIraxuNnUKNiXbFzDcmEJJakInW5Fpowq4zg/uFfFl lw/hRSlF1kYbt1qvfKB1eG0l/P3hbe3POyNY+Zd9Y2SAz0PpdN/RpRVjsK1Q81iSjv7b gS7nyqaNQG019gyHW3bz2gL8e+ZIygrD47pBeBl+vkAiurO4/ZKpqQZY+1nffrZCeUBS msOE1siKqHUpWFr4DF5sqL3hc6qoTIpS7H0FLjeynhLDpijNOxwOuQYjicGma8jO8zzo fh9ZAehBMiqZv/JPzZzAivMIXZIY9w2xnvU71i8sBtBwOwKE4Z0XypyyMG61+7Ojbm97 8NNw== 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=d82HDQRFWnDbNi3qCp9COZeIUQc4rHUgiYVZqY067LM=; b=PpPhwz2fl3ViK+FWXzsJ+QaHtvIlMXCopEmah/cS8OmbNeCqIxTD5MeQnxY0yfiEWM PaCLF5+REKnTCs0pR7XsLYPzCjRLHPNqyXnb7RkDNsOsBKYE8N1sRab8DUtZjxyIaId2 oumQZhYWAu4gjmeavnOUfPh6cjB+iOgCz9sWSUXAkw1e+MIf++4rolJkT7/D4zNnQrcY vbNFxU19zJ9ZYdE75Nf5YVHu889UYuOvElC6DVxBgMdeXH7bB2wIvIfJu7hqGYdjoVqu 26nhBnzQf9d8HykErRYqZK6e9LnbS3y+xRGe0xm/H1EjJg0DXSCT1GzduK9niKQqqwEs HZEg== X-Gm-Message-State: AOAM531r+HbwPkGIT9VCDcXvthw4d5DALU8StghrlJ0WlxEykjLJDAOZ x7/kWotHNr0Xn/r0h1xNAlTScg== X-Received: by 2002:adf:fd90:: with SMTP id d16mr7979750wrr.35.1623485481147; Sat, 12 Jun 2021 01:11:21 -0700 (PDT) Received: from localhost.localdomain ([2a01:e0a:82c:5f0:85ed:406e:1bc4:a268]) by smtp.gmail.com with ESMTPSA id w13sm10619313wrc.31.2021.06.12.01.11.20 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 12 Jun 2021 01:11:20 -0700 (PDT) From: Loic Poulain To: kuba@kernel.org, davem@davemloft.net Cc: netdev@vger.kernel.org, m.chetan.kumar@intel.com, johannes.berg@intel.com, leon@kernel.org, ryazanov.s.a@gmail.com, parav@nvidia.com, Loic Poulain Subject: [PATCH net-next v3 2/4] rtnetlink: add IFLA_PARENT_[DEV|DEV_BUS]_NAME Date: Sat, 12 Jun 2021 10:20:55 +0200 Message-Id: <1623486057-13075-3-git-send-email-loic.poulain@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1623486057-13075-1-git-send-email-loic.poulain@linaro.org> References: <1623486057-13075-1-git-send-email-loic.poulain@linaro.org> Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Johannes Berg In some cases, for example in the upcoming WWAN framework changes, there's no natural "parent netdev", so sometimes dummy netdevs are created or similar. IFLA_PARENT_DEV_NAME is a new attribute intended to contain a device (sysfs, struct device) name that can be used instead when creating a new netdev, if the rtnetlink family implements it. As suggested by Parav Pandit, we also introduce IFLA_PARENT_DEV_BUS_NAME attribute in order to uniquely identify a device on the system (with bus/name pair). ip-link(8) support for the generic parent device attributes will help us avoid code duplication, so no other link type will require a custom code to handle the parent name attribute. E.g. the WWAN interface creation command will looks like this: $ ip link add wwan0-1 parent-dev wwan0 type wwan channel-id 1 So, some future subsystem (or driver) FOO will have an interface creation command that looks like this: $ ip link add foo1-3 parent-dev foo1 type foo bar-id 3 baz-type Y Below is an example of dumping link info of a random device with these new attributes: $ ip --details link show wlp0s20f3 4: wlp0s20f3: mtu 1500 qdisc noqueue state UP mode DORMANT group default qlen 1000 ... parent_bus pci parent_dev 0000:00:14.3 Co-developed-by: Sergey Ryazanov Signed-off-by: Sergey Ryazanov Co-developed-by: Loic Poulain Signed-off-by: Loic Poulain Suggested-by: Sergey Ryazanov Signed-off-by: Johannes Berg --- include/uapi/linux/if_link.h | 7 +++++++ net/core/rtnetlink.c | 10 ++++++++++ 2 files changed, 17 insertions(+) -- 2.7.4 diff --git a/include/uapi/linux/if_link.h b/include/uapi/linux/if_link.h index a5a7f0e..4882e81 100644 --- a/include/uapi/linux/if_link.h +++ b/include/uapi/linux/if_link.h @@ -341,6 +341,13 @@ enum { IFLA_ALT_IFNAME, /* Alternative ifname */ IFLA_PERM_ADDRESS, IFLA_PROTO_DOWN_REASON, + + /* device (sysfs) name as parent, used instead + * of IFLA_LINK where there's no parent netdev + */ + IFLA_PARENT_DEV_NAME, + IFLA_PARENT_DEV_BUS_NAME, + __IFLA_MAX }; diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index 92c3e43..170e97f 100644 --- a/net/core/rtnetlink.c +++ b/net/core/rtnetlink.c @@ -1821,6 +1821,16 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb, if (rtnl_fill_prop_list(skb, dev)) goto nla_put_failure; + if (dev->dev.parent && + nla_put_string(skb, IFLA_PARENT_DEV_NAME, + dev_name(dev->dev.parent))) + goto nla_put_failure; + + if (dev->dev.parent && dev->dev.parent->bus && + nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME, + dev->dev.parent->bus->name)) + goto nla_put_failure; + nlmsg_end(skb, nlh); return 0; From patchwork Sat Jun 12 08:20:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Loic Poulain X-Patchwork-Id: 459151 Delivered-To: patch@linaro.org Received: by 2002:a02:735a:0:0:0:0:0 with SMTP id a26csp1612121jae; Sat, 12 Jun 2021 01:12:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxazkUsEmidSdjKilothzjzdHqKfHIQtRQV0c7g0jDfHK+dkUIK6q+pzXjbZy3lcrNd7cwy X-Received: by 2002:a17:906:2892:: with SMTP id o18mr6810888ejd.124.1623485539772; Sat, 12 Jun 2021 01:12:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623485539; cv=none; d=google.com; s=arc-20160816; b=OzR10lVZti534BvJdepBnKgWV2atj9tYu5yd+xQ5n2Et9jtkYGxQ3ibUHf8HfRCaM7 MmxB7mTpzhTxr91Iw2LOVQD5+uj5Tt51Id2Ejh/Mn8teECm+ef3b0ZzfVvdYv9vF2Eep AtMNx9CXQF3XG3+xwzI6AVvjUyQeNGixAbcZ4HTrg1PbjSq2gP4wRJYVJWe2vlcmTS1I /x+4BxbiZC409rnQFxftiu7GUhMwNRu/sPb24hZeYSm0T8Fn1nPok8TfT9YMnrB6Is4S FrBYf40GCEYhfxJNX/n7u0Wfmo2GUxC5kmIrrDAA1O/C3X99YTCt8U/44shmsBX6rZSJ v56g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=LpDw1ywlkqO/sA3iexzsm4a1dAIYNK1Yt9SF1X9NPZk=; b=SUzOmC4uQ8us2EAcsfyW47bHVVeTQFM31VmlmiZNIDxNqmsH7AJ/I6fS58fuSN0tfU wC/zpgH3eZthvlzlUYmcXVqoM+KByO/mdL84cHvshJR2LwPQxwL4rsA4hTBjX0uWjAuK K1CV2A1Tu2pkcm8ru8kbV6ljNWt05kLY/d2mPN1hNs0tmFDMWQ8f/1vMZMCXzwBwaqbn nBZbp/ktJqQQZGH0OxcLuzVRO71rYktszJKPq9togqnocTM6C1OhqpZrX5S/Y8iBAGDR vD3aT0JrFspripUIiO0DmElsOz/iPmgUv1VW0QeipKFc/qdMgPeTyasMdpbQHAL6w++R qFQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=cKLKpg+p; spf=pass (google.com: domain of netdev-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=netdev-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t18si6416752eju.726.2021.06.12.01.12.19; Sat, 12 Jun 2021 01:12:19 -0700 (PDT) Received-SPF: pass (google.com: domain of netdev-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=cKLKpg+p; spf=pass (google.com: domain of netdev-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=netdev-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230526AbhFLINh (ORCPT + 8 others); Sat, 12 Jun 2021 04:13:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229584AbhFLINg (ORCPT ); Sat, 12 Jun 2021 04:13:36 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E00AC061574 for ; Sat, 12 Jun 2021 01:11:24 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id c9so8416209wrt.5 for ; Sat, 12 Jun 2021 01:11:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=LpDw1ywlkqO/sA3iexzsm4a1dAIYNK1Yt9SF1X9NPZk=; b=cKLKpg+pZVwdUi+c4vNgpj84hvDXk1G3iakF8HMDZHv1K+paYCK92WQdCP3dMc1ZzW fPk7mMY4RAK1EufgY2W2HqTIftNVYUkLgVZx4l+ezUEt9L1B6UoFhkYBqrggF19GnSjp ZHv3/O2HMT9AvRr3+L8sulld/zVvdhyoye0/1nE9olEA9uMkvHIAsLLK1ygCdDbWbfof FQANp2xvk7qmT0GCNcze4QR76IF1RECxTVQ0de3U4FvsLPS/cMPYWO4nsKlxY1433qyy j0zUvIl5cdhp/sh95/ie97kUKIzjxwgExPlWwuvgwau/T8KGaq0gMu6jSBOMP/8GnyBq gW8w== 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=LpDw1ywlkqO/sA3iexzsm4a1dAIYNK1Yt9SF1X9NPZk=; b=oSLh92oW1XXtdt5R2pf7BUWEpfJr0gejBjODT+L0CVj31R2AGl6AOsnxYB+0ldgMoT fGwQIDsM4ALXGHZDE/46DvELOAxdCmjj62YJIucojIelKfIn43AiGkW7L4n4xivtnizr PoUHSamNvhpMR6mrZaoGIJIOhjsxhQ9g9Qx744vMBY/nM1yAsYzqstbdRdijcLJ9PgU0 LuN4yPiAwUtrv33YPkzGUGPHSkc6pkD+cvxFvzO9uPn/TMnhVkd1LAMm/nASzZXw+bW3 4cNrborG6m61momEilAO4PRdAuSRG40hwq14RFvDBJ5m8feEKD4J9pkmET3KwGzOxDvC PlQQ== X-Gm-Message-State: AOAM533oyO070Ti1qHGK8BAjzUUaDM1yi4sukkeau8NGo6elsBj835SW MLJYCMUC1I7PYr6VM+bnHK0b8Q== X-Received: by 2002:adf:e48c:: with SMTP id i12mr1843306wrm.186.1623485482446; Sat, 12 Jun 2021 01:11:22 -0700 (PDT) Received: from localhost.localdomain ([2a01:e0a:82c:5f0:85ed:406e:1bc4:a268]) by smtp.gmail.com with ESMTPSA id w13sm10619313wrc.31.2021.06.12.01.11.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 12 Jun 2021 01:11:21 -0700 (PDT) From: Loic Poulain To: kuba@kernel.org, davem@davemloft.net Cc: netdev@vger.kernel.org, m.chetan.kumar@intel.com, johannes.berg@intel.com, leon@kernel.org, ryazanov.s.a@gmail.com, parav@nvidia.com, Loic Poulain Subject: [PATCH net-next v3 3/4] wwan: add interface creation support Date: Sat, 12 Jun 2021 10:20:56 +0200 Message-Id: <1623486057-13075-4-git-send-email-loic.poulain@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1623486057-13075-1-git-send-email-loic.poulain@linaro.org> References: <1623486057-13075-1-git-send-email-loic.poulain@linaro.org> Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Johannes Berg Add support to create (and destroy) interfaces via a new rtnetlink kind "wwan". The responsible driver has to use the new wwan_register_ops() to make this possible. Signed-off-by: Johannes Berg Signed-off-by: Sergey Ryazanov Signed-off-by: Loic Poulain --- drivers/net/wwan/wwan_core.c | 245 +++++++++++++++++++++++++++++++++++++++++-- include/linux/wwan.h | 24 +++++ include/uapi/linux/wwan.h | 16 +++ net/core/rtnetlink.c | 1 + 4 files changed, 279 insertions(+), 7 deletions(-) create mode 100644 include/uapi/linux/wwan.h -- 2.7.4 diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c index 45a41ae..7e72804 100644 --- a/drivers/net/wwan/wwan_core.c +++ b/drivers/net/wwan/wwan_core.c @@ -14,6 +14,8 @@ #include #include #include +#include +#include /* Maximum number of minors in use */ #define WWAN_MAX_MINORS (1 << MINORBITS) @@ -35,10 +37,16 @@ static int wwan_major; * * @id: WWAN device unique ID. * @dev: Underlying device. + * @port_id: Current available port ID to pick. + * @ops: wwan device ops + * @ops_ctxt: context to pass to ops */ struct wwan_device { unsigned int id; struct device dev; + atomic_t port_id; + const struct wwan_ops *ops; + void *ops_ctxt; }; /** @@ -102,7 +110,8 @@ static const struct device_type wwan_dev_type = { static int wwan_dev_parent_match(struct device *dev, const void *parent) { - return (dev->type == &wwan_dev_type && dev->parent == parent); + return (dev->type == &wwan_dev_type && + (dev->parent == parent || dev == parent)); } static struct wwan_device *wwan_dev_get_by_parent(struct device *parent) @@ -116,6 +125,23 @@ static struct wwan_device *wwan_dev_get_by_parent(struct device *parent) return to_wwan_dev(dev); } +static int wwan_dev_name_match(struct device *dev, const void *name) +{ + return dev->type == &wwan_dev_type && + strcmp(dev_name(dev), name) == 0; +} + +static struct wwan_device *wwan_dev_get_by_name(const char *name) +{ + struct device *dev; + + dev = class_find_device(wwan_class, NULL, name, wwan_dev_name_match); + if (!dev) + return ERR_PTR(-ENODEV); + + return to_wwan_dev(dev); +} + /* This function allocates and registers a new WWAN device OR if a WWAN device * already exist for the given parent, it gets a reference and return it. * This function is not exported (for now), it is called indirectly via @@ -180,9 +206,14 @@ static void wwan_remove_dev(struct wwan_device *wwandev) /* WWAN device is created and registered (get+add) along with its first * child port, and subsequent port registrations only grab a reference * (get). The WWAN device must then be unregistered (del+put) along with - * its latest port, and reference simply dropped (put) otherwise. + * its last port, and reference simply dropped (put) otherwise. In the + * same fashion, we must not unregister it when the ops are still there. */ - ret = device_for_each_child(&wwandev->dev, NULL, is_wwan_child); + if (wwandev->ops) + ret = 1; + else + ret = device_for_each_child(&wwandev->dev, NULL, is_wwan_child); + if (!ret) device_unregister(&wwandev->dev); else @@ -750,26 +781,226 @@ static const struct file_operations wwan_port_fops = { .llseek = noop_llseek, }; +/** + * wwan_register_ops - register WWAN device ops + * @parent: Device to use as parent and shared by all WWAN ports and + * created netdevs + * @ops: operations to register + * @ctxt: context to pass to operations + * + * Returns: 0 on success, a negative error code on failure + */ +int wwan_register_ops(struct device *parent, const struct wwan_ops *ops, + void *ctxt) +{ + struct wwan_device *wwandev; + + if (WARN_ON(!parent || !ops)) + return -EINVAL; + + wwandev = wwan_create_dev(parent); + if (!wwandev) + return -ENOMEM; + + if (WARN_ON(wwandev->ops)) { + wwan_remove_dev(wwandev); + return -EBUSY; + } + + if (!try_module_get(ops->owner)) { + wwan_remove_dev(wwandev); + return -ENODEV; + } + + wwandev->ops = ops; + wwandev->ops_ctxt = ctxt; + + return 0; +} +EXPORT_SYMBOL_GPL(wwan_register_ops); + +/** + * wwan_unregister_ops - remove WWAN device ops + * @parent: Device to use as parent and shared by all WWAN ports and + * created netdevs + */ +void wwan_unregister_ops(struct device *parent) +{ + struct wwan_device *wwandev = wwan_dev_get_by_parent(parent); + bool has_ops; + + if (WARN_ON(IS_ERR(wwandev))) + return; + + has_ops = wwandev->ops; + + /* put the reference obtained by wwan_dev_get_by_parent(), + * we should still have one (that the owner is giving back + * now) due to the ops being assigned, check that below + * and return if not. + */ + put_device(&wwandev->dev); + + if (WARN_ON(!has_ops)) + return; + + module_put(wwandev->ops->owner); + + wwandev->ops = NULL; + wwandev->ops_ctxt = NULL; + wwan_remove_dev(wwandev); +} +EXPORT_SYMBOL_GPL(wwan_unregister_ops); + +static int wwan_rtnl_validate(struct nlattr *tb[], struct nlattr *data[], + struct netlink_ext_ack *extack) +{ + if (!data) + return -EINVAL; + + if (!tb[IFLA_PARENT_DEV_NAME]) + return -EINVAL; + + if (!data[IFLA_WWAN_LINK_ID]) + return -EINVAL; + + return 0; +} + +static struct device_type wwan_type = { .name = "wwan" }; + +static struct net_device *wwan_rtnl_alloc(struct nlattr *tb[], + const char *ifname, + unsigned char name_assign_type, + unsigned int num_tx_queues, + unsigned int num_rx_queues) +{ + const char *devname = nla_data(tb[IFLA_PARENT_DEV_NAME]); + struct wwan_device *wwandev = wwan_dev_get_by_name(devname); + struct net_device *dev; + + if (IS_ERR(wwandev)) + return ERR_CAST(wwandev); + + /* only supported if ops were registered (not just ports) */ + if (!wwandev->ops) { + dev = ERR_PTR(-EOPNOTSUPP); + goto out; + } + + dev = alloc_netdev_mqs(wwandev->ops->priv_size, ifname, name_assign_type, + wwandev->ops->setup, num_tx_queues, num_rx_queues); + + if (dev) { + SET_NETDEV_DEV(dev, &wwandev->dev); + SET_NETDEV_DEVTYPE(dev, &wwan_type); + } + +out: + /* release the reference */ + put_device(&wwandev->dev); + return dev; +} + +static int wwan_rtnl_newlink(struct net *src_net, struct net_device *dev, + struct nlattr *tb[], struct nlattr *data[], + struct netlink_ext_ack *extack) +{ + struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent); + u32 link_id = nla_get_u32(data[IFLA_WWAN_LINK_ID]); + int ret; + + if (IS_ERR(wwandev)) + return PTR_ERR(wwandev); + + /* shouldn't have a netdev (left) with us as parent so WARN */ + if (WARN_ON(!wwandev->ops)) { + ret = -EOPNOTSUPP; + goto out; + } + + if (wwandev->ops->newlink) + ret = wwandev->ops->newlink(wwandev->ops_ctxt, dev, + link_id, extack); + else + ret = register_netdevice(dev); + +out: + /* release the reference */ + put_device(&wwandev->dev); + return ret; +} + +static void wwan_rtnl_dellink(struct net_device *dev, struct list_head *head) +{ + struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent); + + if (IS_ERR(wwandev)) + return; + + /* shouldn't have a netdev (left) with us as parent so WARN */ + if (WARN_ON(!wwandev->ops)) + goto out; + + if (wwandev->ops->dellink) + wwandev->ops->dellink(wwandev->ops_ctxt, dev, head); + else + unregister_netdevice(dev); + +out: + /* release the reference */ + put_device(&wwandev->dev); +} + +static const struct nla_policy wwan_rtnl_policy[IFLA_WWAN_MAX + 1] = { + [IFLA_WWAN_LINK_ID] = { .type = NLA_U32 }, +}; + +static struct rtnl_link_ops wwan_rtnl_link_ops __read_mostly = { + .kind = "wwan", + .maxtype = __IFLA_WWAN_MAX, + .alloc = wwan_rtnl_alloc, + .validate = wwan_rtnl_validate, + .newlink = wwan_rtnl_newlink, + .dellink = wwan_rtnl_dellink, + .policy = wwan_rtnl_policy, +}; + static int __init wwan_init(void) { + int err; + + err = rtnl_link_register(&wwan_rtnl_link_ops); + if (err) + return err; + wwan_class = class_create(THIS_MODULE, "wwan"); - if (IS_ERR(wwan_class)) - return PTR_ERR(wwan_class); + if (IS_ERR(wwan_class)) { + err = PTR_ERR(wwan_class); + goto unregister; + } /* chrdev used for wwan ports */ wwan_major = __register_chrdev(0, 0, WWAN_MAX_MINORS, "wwan_port", &wwan_port_fops); if (wwan_major < 0) { - class_destroy(wwan_class); - return wwan_major; + err = wwan_major; + goto destroy; } return 0; + +destroy: + class_destroy(wwan_class); +unregister: + rtnl_link_unregister(&wwan_rtnl_link_ops); + return err; } static void __exit wwan_exit(void) { __unregister_chrdev(wwan_major, 0, WWAN_MAX_MINORS, "wwan_port"); + rtnl_link_unregister(&wwan_rtnl_link_ops); class_destroy(wwan_class); } diff --git a/include/linux/wwan.h b/include/linux/wwan.h index fa33cc1..430a3a0 100644 --- a/include/linux/wwan.h +++ b/include/linux/wwan.h @@ -7,6 +7,7 @@ #include #include #include +#include /** * enum wwan_port_type - WWAN port types @@ -116,4 +117,27 @@ void wwan_port_txon(struct wwan_port *port); */ void *wwan_port_get_drvdata(struct wwan_port *port); +/** + * struct wwan_ops - WWAN device ops + * @owner: module owner of the WWAN ops + * @priv_size: size of private netdev data area + * @setup: set up a new netdev + * @newlink: register the new netdev + * @dellink: remove the given netdev + */ +struct wwan_ops { + struct module *owner; + unsigned int priv_size; + void (*setup)(struct net_device *dev); + int (*newlink)(void *ctxt, struct net_device *dev, + u32 if_id, struct netlink_ext_ack *extack); + void (*dellink)(void *ctxt, struct net_device *dev, + struct list_head *head); +}; + +int wwan_register_ops(struct device *parent, const struct wwan_ops *ops, + void *ctxt); + +void wwan_unregister_ops(struct device *parent); + #endif /* __WWAN_H */ diff --git a/include/uapi/linux/wwan.h b/include/uapi/linux/wwan.h new file mode 100644 index 0000000..32a2720 --- /dev/null +++ b/include/uapi/linux/wwan.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only WITH Linux-syscall-note */ +/* + * Copyright (C) 2021 Intel Corporation. + */ +#ifndef _UAPI_WWAN_H_ +#define _UAPI_WWAN_H_ + +enum { + IFLA_WWAN_UNSPEC, + IFLA_WWAN_LINK_ID, /* u32 */ + + __IFLA_WWAN_MAX +}; +#define IFLA_WWAN_MAX (__IFLA_WWAN_MAX - 1) + +#endif /* _UAPI_WWAN_H_ */ diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index 170e97f..5baa86b 100644 --- a/net/core/rtnetlink.c +++ b/net/core/rtnetlink.c @@ -1890,6 +1890,7 @@ static const struct nla_policy ifla_policy[IFLA_MAX+1] = { [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT }, [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED }, [IFLA_NEW_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1), + [IFLA_PARENT_DEV_NAME] = { .type = NLA_NUL_STRING }, }; static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { From patchwork Sat Jun 12 08:20:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Loic Poulain X-Patchwork-Id: 459150 Delivered-To: patch@linaro.org Received: by 2002:a02:735a:0:0:0:0:0 with SMTP id a26csp1612116jae; Sat, 12 Jun 2021 01:12:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyKx+2bSFAOFwxu139RILJz2pLtMJbn/f1S1/93OnUIVBC241/kkaQhWlUEWi7TCT4h/xk2 X-Received: by 2002:a17:906:b210:: with SMTP id p16mr6834185ejz.100.1623485539365; Sat, 12 Jun 2021 01:12:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623485539; cv=none; d=google.com; s=arc-20160816; b=zy6/3Qr3phlJHne+USwckAYjjLctMe/OixRkLQPkOXZIliIjAwYEWxQEu0V4aVsd90 iaITUEy0seOShTq8BckAG4k8PsdwuawnbFjZipc0NgseUZ2AyDrLiwy3Yy0jS4DcrC6A lGLtjBqlgedG+ez7qW1TLEkRalcOPSUppWsuX388whcpQv36lN4AIZhh9ZjzvrPurZjA R9PdGCwIscbbS1yi9j4viIZVLXyDoX+QB2H+SfX20UJCFL24yUWgsoK3gxH8VFLSOxpO diQnut51MWFjIZyzk9+4uRCNpqGRMldOdMM6P3u/9m4gzfRvSx9cHHGwTkN9hCCPzM1J Jz3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=MZ5iWwPms9MLNf+qVmLdkwv/Ob0NqXbhp+LkxYNHn+E=; b=A8PDDx/weN7awbVZ5ygfvN+0IYzZiL2iGuHQQ6X4BVnb9iKyQuO4ebP137wNnNxjZq zpxmiEXEGhzhIF6hPK8YZAspwkB3Q3uKgnSVw+igP3MIJaviL+U9bTQ1A9f4E3zbVVaE L1rh5lnDi+FIiuciZ6gIAUGVeovfge+oIdfS7chkVqYZ60KQuEIbO231fQU9l1wDbELn DIt1b50Ytvt+6bRFrdCgIde11o20Age6S/yGODRFbj+QqIaIY/EHsnOifZZ9Fi6JozGC wfT2rouXuhQP4rMGIt/qCInGA8vV1Lpf1pJJmA2c3Jd764/lcH5ovDECtMgTA5l4eeU7 ugPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=IvPeNEsA; spf=pass (google.com: domain of netdev-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=netdev-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t18si6416752eju.726.2021.06.12.01.12.18; Sat, 12 Jun 2021 01:12:19 -0700 (PDT) Received-SPF: pass (google.com: domain of netdev-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=IvPeNEsA; spf=pass (google.com: domain of netdev-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=netdev-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230229AbhFLIN0 (ORCPT + 8 others); Sat, 12 Jun 2021 04:13:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59824 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229584AbhFLINY (ORCPT ); Sat, 12 Jun 2021 04:13:24 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23636C061767 for ; Sat, 12 Jun 2021 01:11:25 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id n7so2304040wri.3 for ; Sat, 12 Jun 2021 01:11:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=MZ5iWwPms9MLNf+qVmLdkwv/Ob0NqXbhp+LkxYNHn+E=; b=IvPeNEsA67XvBPQeeiok9BDqD6Dxhw2jL4VeLi4rFLd0XhCJDtj8RcedfXvcqi1piV 3WDBlMv+pKvUfq6tc1eacREtb0uAtW6h/lnX+X790TUhboRVb1tBqQWPLpZ9aVaRjgi2 E2JcwoVidLjIJ/3UAjQJHyZqk0gaHKqxN9hmX55+TstyVmMklYt9Ck2Id9h8N8O8DFfs Di7AUuOkiypDhvr5HlX9z6CZGJL0Q8LW1V8FeDT+gWBmLv/dsdOqoBSt9Jo+be1jiyRu fntKztloAxY3a6uxpyuotYkCVs143+KUKlUlgUJSk9d3BBvI1f58pRxuJKxEdT6Grg+2 mAtw== 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=MZ5iWwPms9MLNf+qVmLdkwv/Ob0NqXbhp+LkxYNHn+E=; b=c6TovFtWFd9IK/55fmf/m0ElUSoj1WTDGZyU0hLBJaiuRBni2xZOeIukDYDhjq/FOg k9MwAJD1Kampuw+6DpECH49iYtPwb9n6MT2LvvuC0PkoPVJm6cVqHPoWbiSbtdMO27qQ w1RgZhXRFPcHWWed99IBO2MQj6AM9la4dXqbCYXYD6z5hv5v/NVDOCsiDJLxCRqn9oLR GmJcZiqDtniq9H9AY33x8TiBZVdlQhc9X2gNRvff5rG2V4JGbx0dghSWxbGHuHhMd8nI 5ko0e94j01gkleyHn/Oi25ZSff/DaO1BBiuJUkKE7K5m6p+T3+ipOe4sJdVvwzgm+JBq sNCw== X-Gm-Message-State: AOAM532zrbnoYzjy1r6oxktCDUyZs2H/dVK+b1PDaovaGdgOu4Nol/sR nolH7qscdbxK6ydYntmdlzxNGw== X-Received: by 2002:adf:f7c3:: with SMTP id a3mr8098451wrq.253.1623485483628; Sat, 12 Jun 2021 01:11:23 -0700 (PDT) Received: from localhost.localdomain ([2a01:e0a:82c:5f0:85ed:406e:1bc4:a268]) by smtp.gmail.com with ESMTPSA id w13sm10619313wrc.31.2021.06.12.01.11.22 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 12 Jun 2021 01:11:23 -0700 (PDT) From: Loic Poulain To: kuba@kernel.org, davem@davemloft.net Cc: netdev@vger.kernel.org, m.chetan.kumar@intel.com, johannes.berg@intel.com, leon@kernel.org, ryazanov.s.a@gmail.com, parav@nvidia.com, Loic Poulain Subject: [PATCH net-next v3 4/4] net: mhi_net: Register wwan_ops for link creation Date: Sat, 12 Jun 2021 10:20:57 +0200 Message-Id: <1623486057-13075-5-git-send-email-loic.poulain@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1623486057-13075-1-git-send-email-loic.poulain@linaro.org> References: <1623486057-13075-1-git-send-email-loic.poulain@linaro.org> Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Register wwan_ops for link management via wwan rtnetlink. This is only basic support for now, since we only support creating one single link (link-0), but is useful to validate new wwan rtnetlink interface. For backward compatibity support, we still register a default netdev at probe time, except if 'create_default_iface' module parameter is set to false. This has been tested with iproute2 and mbimcli: $ ip link add dev wwan0-0 parentdev-name wwan0 type wwan linkid 0 $ mbimcli -p -d /dev/wwan0p2MBIM --connect apn=free $ ip link set dev wwan0-0 up $ ip addr add dev wwan0 ${IP} $ ip route replace default via ${IP} $ ping 8.8.8.8 ... Signed-off-by: Loic Poulain --- drivers/net/Kconfig | 1 + drivers/net/mhi/net.c | 123 ++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 101 insertions(+), 23 deletions(-) -- 2.7.4 diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig index 4da68ba..30d6e2f 100644 --- a/drivers/net/Kconfig +++ b/drivers/net/Kconfig @@ -431,6 +431,7 @@ config VSOCKMON config MHI_NET tristate "MHI network driver" depends on MHI_BUS + select WWAN_CORE help This is the network driver for MHI bus. It can be used with QCOM based WWAN modems (like SDX55). Say Y or M. diff --git a/drivers/net/mhi/net.c b/drivers/net/mhi/net.c index 0d8293a..64af1e5 100644 --- a/drivers/net/mhi/net.c +++ b/drivers/net/mhi/net.c @@ -11,6 +11,7 @@ #include #include #include +#include #include "mhi.h" @@ -18,6 +19,12 @@ #define MHI_NET_MAX_MTU 0xffff #define MHI_NET_DEFAULT_MTU 0x4000 +/* When set to false, the default netdev (link 0) is not created, and it's up + * to user to create the link (via wwan rtnetlink). + */ +static bool create_default_iface = true; +module_param(create_default_iface, bool, 0); + struct mhi_device_info { const char *netname; const struct mhi_net_proto *proto; @@ -295,32 +302,33 @@ static void mhi_net_rx_refill_work(struct work_struct *work) schedule_delayed_work(&mhi_netdev->rx_refill, HZ / 2); } -static struct device_type wwan_type = { - .name = "wwan", -}; - -static int mhi_net_probe(struct mhi_device *mhi_dev, - const struct mhi_device_id *id) +static int mhi_net_newlink(void *ctxt, struct net_device *ndev, u32 if_id, + struct netlink_ext_ack *extack) { - const struct mhi_device_info *info = (struct mhi_device_info *)id->driver_data; - struct device *dev = &mhi_dev->dev; + const struct mhi_device_info *info; + struct mhi_device *mhi_dev = ctxt; struct mhi_net_dev *mhi_netdev; - struct net_device *ndev; int err; - ndev = alloc_netdev(sizeof(*mhi_netdev), info->netname, - NET_NAME_PREDICTABLE, mhi_net_setup); - if (!ndev) - return -ENOMEM; + info = (struct mhi_device_info *)mhi_dev->id->driver_data; + + /* For now we only support one link (link context 0), driver must be + * reworked to break 1:1 relationship for net MBIM and to forward setup + * call to rmnet(QMAP) otherwise. + */ + if (if_id != 0) + return -EINVAL; + + if (dev_get_drvdata(&mhi_dev->dev)) + return -EBUSY; mhi_netdev = netdev_priv(ndev); - dev_set_drvdata(dev, mhi_netdev); + + dev_set_drvdata(&mhi_dev->dev, mhi_netdev); mhi_netdev->ndev = ndev; mhi_netdev->mdev = mhi_dev; mhi_netdev->skbagg_head = NULL; mhi_netdev->proto = info->proto; - SET_NETDEV_DEV(ndev, &mhi_dev->dev); - SET_NETDEV_DEVTYPE(ndev, &wwan_type); INIT_DELAYED_WORK(&mhi_netdev->rx_refill, mhi_net_rx_refill_work); u64_stats_init(&mhi_netdev->stats.rx_syncp); @@ -334,7 +342,10 @@ static int mhi_net_probe(struct mhi_device *mhi_dev, /* Number of transfer descriptors determines size of the queue */ mhi_netdev->rx_queue_sz = mhi_get_free_desc_count(mhi_dev, DMA_FROM_DEVICE); - err = register_netdev(ndev); + if (extack) + err = register_netdevice(ndev); + else + err = register_netdev(ndev); if (err) goto out_err; @@ -347,23 +358,89 @@ static int mhi_net_probe(struct mhi_device *mhi_dev, return 0; out_err_proto: - unregister_netdev(ndev); + unregister_netdevice(ndev); out_err: free_netdev(ndev); return err; } -static void mhi_net_remove(struct mhi_device *mhi_dev) +static void mhi_net_dellink(void *ctxt, struct net_device *ndev, + struct list_head *head) { - struct mhi_net_dev *mhi_netdev = dev_get_drvdata(&mhi_dev->dev); + struct mhi_net_dev *mhi_netdev = netdev_priv(ndev); + struct mhi_device *mhi_dev = ctxt; - unregister_netdev(mhi_netdev->ndev); + if (head) + unregister_netdevice_queue(ndev, head); + else + unregister_netdev(ndev); - mhi_unprepare_from_transfer(mhi_netdev->mdev); + mhi_unprepare_from_transfer(mhi_dev); kfree_skb(mhi_netdev->skbagg_head); - free_netdev(mhi_netdev->ndev); + dev_set_drvdata(&mhi_dev->dev, NULL); +} + +const struct wwan_ops mhi_wwan_ops = { + .owner = THIS_MODULE, + .priv_size = sizeof(struct mhi_net_dev), + .setup = mhi_net_setup, + .newlink = mhi_net_newlink, + .dellink = mhi_net_dellink, +}; + +static int mhi_net_probe(struct mhi_device *mhi_dev, + const struct mhi_device_id *id) +{ + const struct mhi_device_info *info = (struct mhi_device_info *)id->driver_data; + struct mhi_controller *cntrl = mhi_dev->mhi_cntrl; + struct net_device *ndev; + int err; + + err = wwan_register_ops(&cntrl->mhi_dev->dev, &mhi_wwan_ops, mhi_dev); + if (err) + return err; + + if (!create_default_iface) + return 0; + + /* Create a default interface which is used as either RMNET real-dev, + * MBIM link 0 or ip link 0) + */ + ndev = alloc_netdev(sizeof(struct mhi_net_dev), info->netname, + NET_NAME_PREDICTABLE, mhi_net_setup); + if (!ndev) { + err = -ENOMEM; + goto err_unregister; + } + + SET_NETDEV_DEV(ndev, &mhi_dev->dev); + + err = mhi_net_newlink(mhi_dev, ndev, 0, NULL); + if (err) + goto err_release; + + return 0; + +err_release: + free_netdev(ndev); +err_unregister: + wwan_unregister_ops(&cntrl->mhi_dev->dev); + + return err; +} + +static void mhi_net_remove(struct mhi_device *mhi_dev) +{ + struct mhi_net_dev *mhi_netdev = dev_get_drvdata(&mhi_dev->dev); + struct mhi_controller *cntrl = mhi_dev->mhi_cntrl; + + /* rtnetlink takes care of removing remaining links */ + wwan_unregister_ops(&cntrl->mhi_dev->dev); + + if (create_default_iface) + mhi_net_dellink(mhi_dev, mhi_netdev->ndev, NULL); } static const struct mhi_device_info mhi_hwip0 = {