From patchwork Tue Jun 8 14:07:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Loic Poulain X-Patchwork-Id: 12306869 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5DBDDC47082 for ; Tue, 8 Jun 2021 13:58:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3D05561354 for ; Tue, 8 Jun 2021 13:58:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233122AbhFHOAT (ORCPT ); Tue, 8 Jun 2021 10:00:19 -0400 Received: from mail-wm1-f46.google.com ([209.85.128.46]:41907 "EHLO mail-wm1-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233118AbhFHOAS (ORCPT ); Tue, 8 Jun 2021 10:00:18 -0400 Received: by mail-wm1-f46.google.com with SMTP id l11-20020a05600c4f0bb029017a7cd488f5so1964854wmq.0 for ; Tue, 08 Jun 2021 06:58: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; bh=MRT0SOOsG4AdunyzUvkRb/0kxk6sA/og4uHR2hzJs1A=; b=XzjkCamkIwNRqHEaSCavk9rc4s55UixZta4Uh6IguvzsizZ0oSDS0hrkikYTIROHlS rIXA7M+7Oj0HuEup1okuiZ8MW/DNAChmwEu7wWa6G6pkqwkYZ97cfEDJTZkF0Akst6E8 U7ULoYGh6kZknDXig3JhRbaDErhW1pSjXAywMkg1KcIqSjEr4deTkVDxS3QUpg78kKSx uu11flAkOP8paVnTSBGPp7M9eixd+G5g76nQujPbh20N/pQwNOEeAPu3W87UlwHSy9HP EgNs1EM+n/LnBUWiBlB7dTKkdW4OhYCgY5mhFWX0G28zBsyYs88aA3A+y5KZvb5qAF1j 7fxg== 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; bh=MRT0SOOsG4AdunyzUvkRb/0kxk6sA/og4uHR2hzJs1A=; b=oT/ki/VKNCQYNaRqt3wXZZjcOckH40gQ9U/80Vs3WuWvOGVn5rFYeYg8zqLq8PgYK1 4TaZ5Avfeb3oQj2tg9Qd/meTUdnx8JSkYlfg6WtZYV19Fmw39DIeQ0UMpfoU/KXwQ9/U lAqYxZeXOO9vwoJOwOSDd/uABPETKmVdCIrTVdKPEqxbxfddLMMBaes0ELtxqunCg42/ eRBLYAVCYL+7lfMNFdrD9kYlln5WmMAQvcEJM0UDC+2vidIxtqPe0K7Z6cKUbJlS07h5 w+XZyeF0ruzAb6HZI+OYWzY3YoRJrB3FvbZq2zuqFZ9w+rGkLioHHw9SiNYtwvjo9drS xptQ== X-Gm-Message-State: AOAM531YGr+tAgQhbFN2IODrGBXU0QOMZTGDJkuFyW9fKm1Wu1hw3VV7 uUveEjR6l4fRHr0t3DK7+DFn8QfMuLAaK2AS X-Google-Smtp-Source: ABdhPJz+HfLsbw+0DwoeMLEH120Th6PICkURbvIglmv80mdODujsDRlzb7koKSfn8SaDk4cPyzJJTQ== X-Received: by 2002:a1c:f70b:: with SMTP id v11mr22455348wmh.186.1623160645003; Tue, 08 Jun 2021 06:57:25 -0700 (PDT) Received: from localhost.localdomain ([2a01:e0a:82c:5f0:85ed:406e:1bc4:a268]) by smtp.gmail.com with ESMTPSA id f14sm1956108wmq.10.2021.06.08.06.57.24 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Jun 2021 06:57:24 -0700 (PDT) From: Loic Poulain To: kuba@kernel.org, davem@davemloft.net Cc: netdev@vger.kernel.org, johannes.berg@intel.com, leon@kernel.org, m.chetan.kumar@intel.com, Sergey Ryazanov Subject: [PATCH net-next 1/4] rtnetlink: add alloc() method to rtnl_link_ops Date: Tue, 8 Jun 2021 16:07:04 +0200 Message-Id: <1623161227-29930-1-git-send-email-loic.poulain@linaro.org> X-Mailer: git-send-email 2.7.4 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@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(-) 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 714d5fa..c0c8dec 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); @@ -3177,8 +3177,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); @@ -3411,7 +3420,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 Tue Jun 8 14:07:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Loic Poulain X-Patchwork-Id: 12306873 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A525DC47082 for ; Tue, 8 Jun 2021 13:58:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8DD0561351 for ; Tue, 8 Jun 2021 13:58:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233134AbhFHOAf (ORCPT ); Tue, 8 Jun 2021 10:00:35 -0400 Received: from mail-wm1-f45.google.com ([209.85.128.45]:55273 "EHLO mail-wm1-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233129AbhFHOAf (ORCPT ); Tue, 8 Jun 2021 10:00:35 -0400 Received: by mail-wm1-f45.google.com with SMTP id o127so1895079wmo.4 for ; Tue, 08 Jun 2021 06:58:26 -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=2VD6JQSId+NjhuSw/powVZDfWBuBnQRjpJ5ySxveaVY=; b=M2SULmpsQMlNcPc5y5q9boxImJYfdXoDY8sdDtj1MZbutQK7vn6hIaWibGZjyRLjBm 809Yl8GrGJEVvlBv3gnUjlEyHYwXjb5o/K7CjsV4bnRl4Pksn3w15uOn1gRT5mzRm9Q2 SbHRVYE35aGgdTnVcfIj7KS/UeCG7d9sHVt/mClTgnyTXY6JAXd+UOqtohCp8L+2xKIg 204c5g2g19BnEgN3iaqAOeBAwwLxuax70FPd0COVXJZ9PMEQ3LmURJ1lKlo+lRhJN2Fh QHvdguRzu+xXQqj0Dz00LR6PgA7sNLSIRxcPYIk5BPpRV0Mw5U87qkmOCTGNOmn5cIuk 8Bsw== 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=2VD6JQSId+NjhuSw/powVZDfWBuBnQRjpJ5ySxveaVY=; b=BRdLUQc/hkIAY7J0jJFcIJ/+NO1rARhxGaAlqS8jmom2YCDmiMDWB734XwKy9Ujjdn 1mSZrIxJsXpcrdCOFHECsW8SfY+0f2tSozOnF8jb1RcL3Vvjy58QkphPKiyLpgVgsiQ5 KB8zdsL1FIawn2zFoiJb9GXMkH0YTP1MpMvXolbx8aioPGdJY6m9bNiaXomrpwvEgHvM qMKAzXjoDlhzZ2AK4UIFjzvvRFXMKEGh1JF3hXdfYu06QiGoOMv/eN0X783w09SHue1A GNi5E3qoJR5vE5hxy77ScvYSg7D+b0GuYnoUdJf7kyikv1/hSYn3gEJcb88lWk1WwfK6 bQtA== X-Gm-Message-State: AOAM531jcU0h1scPkTPx8wTenBSBdfK+pwgM2k+kB+wTp1kTJNbrg3Qd Ypu/yVrLTQALmZ4SmfB45o3SJA== X-Google-Smtp-Source: ABdhPJzifcPM1L2c1lMV4Uj5JFT/CENE6xe7ZO8QKnbz87qnjq2q/r5YZurykfVj4scSsXzBL52OZQ== X-Received: by 2002:a1c:a917:: with SMTP id s23mr18037267wme.55.1623160645937; Tue, 08 Jun 2021 06:57:25 -0700 (PDT) Received: from localhost.localdomain ([2a01:e0a:82c:5f0:85ed:406e:1bc4:a268]) by smtp.gmail.com with ESMTPSA id f14sm1956108wmq.10.2021.06.08.06.57.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Jun 2021 06:57:25 -0700 (PDT) From: Loic Poulain To: kuba@kernel.org, davem@davemloft.net Cc: netdev@vger.kernel.org, johannes.berg@intel.com, leon@kernel.org, m.chetan.kumar@intel.com Subject: [PATCH net-next 2/4] rtnetlink: add IFLA_PARENT_DEV_NAME Date: Tue, 8 Jun 2021 16:07:05 +0200 Message-Id: <1623161227-29930-2-git-send-email-loic.poulain@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1623161227-29930-1-git-send-email-loic.poulain@linaro.org> References: <1623161227-29930-1-git-send-email-loic.poulain@linaro.org> Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@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. Suggested-by: Sergey Ryazanov Signed-off-by: Johannes Berg --- include/uapi/linux/if_link.h | 6 ++++++ net/core/rtnetlink.c | 1 + 2 files changed, 7 insertions(+) diff --git a/include/uapi/linux/if_link.h b/include/uapi/linux/if_link.h index cd5b382..0ac1f6a 100644 --- a/include/uapi/linux/if_link.h +++ b/include/uapi/linux/if_link.h @@ -341,6 +341,12 @@ 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_MAX }; diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index c0c8dec..56ac16a 100644 --- a/net/core/rtnetlink.c +++ b/net/core/rtnetlink.c @@ -1878,6 +1878,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 Tue Jun 8 14:07:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Loic Poulain X-Patchwork-Id: 12306871 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9C24DC47082 for ; Tue, 8 Jun 2021 13:58:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 862DF61360 for ; Tue, 8 Jun 2021 13:58:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233127AbhFHOA1 (ORCPT ); Tue, 8 Jun 2021 10:00:27 -0400 Received: from mail-wr1-f45.google.com ([209.85.221.45]:34568 "EHLO mail-wr1-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233118AbhFHOAU (ORCPT ); Tue, 8 Jun 2021 10:00:20 -0400 Received: by mail-wr1-f45.google.com with SMTP id q5so21720034wrm.1 for ; Tue, 08 Jun 2021 06:58:27 -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=WxNx57mPX5CoyU1yFaPVyt2pdIp6epOYJaS+sjEZn20=; b=mr/g80xtX7GL1/5VN9aSPuw0Y4MpWN+kOd02LksE+PxtSqd87doKeNg++AP+q78bH4 grS+epvq34fHMxJzS8x0lpcTcfqJ/OzTK0CtTZ9rIYMiKqwZRLjy/bKDxfTRa8CIpvTk fgyln0OqSEdczz4OuCer3ztXi8DRJ57H2Iqe2d/sF7YIcEuEifDQhbxDOj3oPZS03HdF Qj5HP6boKJexJxNi5Oz7THvtwXigrU5doSs8fJgGT3rNjwnsYgDmJWIVR7liJJrh2Nw6 EJThJ5bepNzyGWZ20fXSHzfo58A68bFG5RbylL7dE5RJ43ijIHSKGVZHP8N0j3NH/XqH ehpA== 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=WxNx57mPX5CoyU1yFaPVyt2pdIp6epOYJaS+sjEZn20=; b=msmotVQiOljiEqfcaAmn2XEEi/2SQFhJvn7ec6xwHFZkEfET1YMpJouv3Q3oX0Xhry VwWdkgU8QPkGfEf3XSYwjQbMwA8W1X+52JCYg4UTheNCktlMbBMB88QLDMlUlllF2Ol1 Ej6eK7uApubK0QyxM74bxtXNmaLt04UhE7AIRSvyPPphNg4aBuXb6oBOaU5yospDZ6mS kH9o7j/f2q/OBjPEnadNwYnLIoQcWWajGmeaV5OedtgcAYa0geP0lSFIseqa77w3rNi6 DEwlSYra1Cw51DjvYS9lV2cnUlMyDfic25cLRiXiD5RARzqE54k+YJh1+Ov3yGgZ1rDF JeOg== X-Gm-Message-State: AOAM531mIKlZnMpe4KXZqGWVdGckC0WovrwLcQ5t47AlM+Rd6Ex8i43m NVpZ2BF4buN2dgSt3A+sSADNHmrATTN/NgmF X-Google-Smtp-Source: ABdhPJxB6Mu9lI05/NJdsijwzEOD3VzzrPUsfa2WgIuPhI8ZuFfGUF5r4mt2McyPuKZ+ElQkeNEwHw== X-Received: by 2002:adf:cd82:: with SMTP id q2mr16298980wrj.258.1623160646983; Tue, 08 Jun 2021 06:57:26 -0700 (PDT) Received: from localhost.localdomain ([2a01:e0a:82c:5f0:85ed:406e:1bc4:a268]) by smtp.gmail.com with ESMTPSA id f14sm1956108wmq.10.2021.06.08.06.57.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Jun 2021 06:57:26 -0700 (PDT) From: Loic Poulain To: kuba@kernel.org, davem@davemloft.net Cc: netdev@vger.kernel.org, johannes.berg@intel.com, leon@kernel.org, m.chetan.kumar@intel.com, Sergey Ryazanov Subject: [PATCH net-next 3/4] rtnetlink: fill IFLA_PARENT_DEV_NAME on link dump Date: Tue, 8 Jun 2021 16:07:06 +0200 Message-Id: <1623161227-29930-3-git-send-email-loic.poulain@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1623161227-29930-1-git-send-email-loic.poulain@linaro.org> References: <1623161227-29930-1-git-send-email-loic.poulain@linaro.org> Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org From: Sergey Ryazanov Return a parent device using the FLA_PARENT_DEV_NAME attribute during links dump. This should help a user figure out which links belong to a particular HW device. E.g. what data channels exists on a specific WWAN modem. Signed-off-by: Sergey Ryazanov --- net/core/rtnetlink.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index 56ac16a..120887c 100644 --- a/net/core/rtnetlink.c +++ b/net/core/rtnetlink.c @@ -1819,6 +1819,11 @@ 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; + nlmsg_end(skb, nlh); return 0; From patchwork Tue Jun 8 14:07:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Loic Poulain X-Patchwork-Id: 12306867 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 961A3C47082 for ; Tue, 8 Jun 2021 13:57:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 75C2E61351 for ; Tue, 8 Jun 2021 13:57:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233097AbhFHN7Z (ORCPT ); Tue, 8 Jun 2021 09:59:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232109AbhFHN7Z (ORCPT ); Tue, 8 Jun 2021 09:59:25 -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 072D5C061574 for ; Tue, 8 Jun 2021 06:57:32 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id a11so19824991wrt.13 for ; Tue, 08 Jun 2021 06:57:31 -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=LZ/Oor1hCv7ilgmqT4IriNNtkYI0IAJIc4BincIec9s=; b=kZRcyUqrZVEvKJaO1Y68gIyZ2pVZf/KPBxSe5PTzeoTJDWSydYeXS6S72MqlYvBy3s LM7lIalybM+dNYsSUkT9qyRxrtgkQO09QesONa6ENLK4/40g6bLAQqaznaopbS4rdO06 WbsscYrz100l1jQzfOWPZ41N3b/LRO53eoL2bszVEty4FtSkGjGierJSGe/1emU0P14C nTLOG99fzMAs9zPnG7PrMejXoEfjPvUVfYXY07LJG5VRvvxZVnuAYjuc7M7zRdqKcjgx 8+E3TGpadXPeVqfwCCF56VHYZMQML2ILkGjvWGyba4xGxroozm+Y+jvPHgge/q1MpqFJ PItg== 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=LZ/Oor1hCv7ilgmqT4IriNNtkYI0IAJIc4BincIec9s=; b=nWmXmPafDhk195ux5bqxNOKKQ6FQ2elhocSSsLYPJ6OP6cK60r4cjBY3TWs2w2FYeh bTntEEZPD/9g0ZeAh4Bs/WyryIi3MvLF3UvXhLxa7kwwp7r0lf2vgwvCvNSTtRgQhRpn K0uGFvA4JMXwUz3Ar5PMxZqjUimgcsA2jNfsFMB50dHytjtwW/WPBqCMBASR9gZiKU1J /DlJocYTEdnTBPAuMKHm/TIe8q4/+7qrRmbp6dYKbByk3I7yUcpO+kJWXMIsJSWxrXBj kH3RkDI5nYKyJ0haHy/1wjnK2jxM+SHfxgtmwBV4HJaCUQcVIv/KvVC92ZbOfxq2JSxd GYHQ== X-Gm-Message-State: AOAM532h2Po2Sx0iN5BYWCXqpGg2BeWEAr+GCZT42gAH0zo0cXQFAgCv 7uw182z8Vgs7wFozOLFBakdKeg== X-Google-Smtp-Source: ABdhPJxD5GISixVxyNdbecIsH9Lz90NmhozopcfcUcoO0O/zCAaszJgJy+xXSmhGQPiXAaXKoPazmQ== X-Received: by 2002:adf:e4cf:: with SMTP id v15mr22977940wrm.162.1623160648078; Tue, 08 Jun 2021 06:57:28 -0700 (PDT) Received: from localhost.localdomain ([2a01:e0a:82c:5f0:85ed:406e:1bc4:a268]) by smtp.gmail.com with ESMTPSA id f14sm1956108wmq.10.2021.06.08.06.57.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Jun 2021 06:57:27 -0700 (PDT) From: Loic Poulain To: kuba@kernel.org, davem@davemloft.net Cc: netdev@vger.kernel.org, johannes.berg@intel.com, leon@kernel.org, m.chetan.kumar@intel.com, Sergey Ryazanov , Loic Poulain Subject: [PATCH net-next 4/4] wwan: add interface creation support Date: Tue, 8 Jun 2021 16:07:07 +0200 Message-Id: <1623161227-29930-4-git-send-email-loic.poulain@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1623161227-29930-1-git-send-email-loic.poulain@linaro.org> References: <1623161227-29930-1-git-send-email-loic.poulain@linaro.org> Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@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 | 229 +++++++++++++++++++++++++++++++++++++++++-- include/linux/wwan.h | 38 +++++++ include/uapi/linux/wwan.h | 16 +++ 3 files changed, 276 insertions(+), 7 deletions(-) create mode 100644 include/uapi/linux/wwan.h diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c index cff04e5..627bf7c 100644 --- a/drivers/net/wwan/wwan_core.c +++ b/drivers/net/wwan/wwan_core.c @@ -13,6 +13,8 @@ #include #include #include +#include +#include #define WWAN_MAX_MINORS 256 /* 256 minors allowed with register_chrdev() */ @@ -34,11 +36,15 @@ 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; }; /** @@ -78,7 +84,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) @@ -92,6 +99,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 @@ -156,9 +180,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 @@ -524,24 +553,210 @@ static const struct file_operations wwan_port_fops = { .llseek = noop_llseek, }; +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); + +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, "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) { + rtnl_link_unregister(&wwan_rtnl_link_ops); unregister_chrdev(wwan_major, "wwan_port"); class_destroy(wwan_class); } diff --git a/include/linux/wwan.h b/include/linux/wwan.h index aa05a25..1116b52 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 @@ -108,4 +109,41 @@ 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); +}; + +/** + * 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); + +/** + * 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); + #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_ */