From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id DF6B3A00C3; Wed, 23 Feb 2022 17:43:51 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id E50A3411AE; Wed, 23 Feb 2022 17:43:45 +0100 (CET) Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2040.outbound.protection.outlook.com [40.107.244.40]) by mails.dpdk.org (Postfix) with ESMTP id D020741145; Wed, 23 Feb 2022 17:43:43 +0100 (CET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nX51ZlXhifqoBln4wSbad/Ca/F+i++tQnaC0qfqf08v19JRJcQDHnfTog6pUGCT/U6cS00PbUFMccPF9VR6YCb7Bd2q3haHlIMWWvKXROukd22xJxZ5Lf1BnmhgihSqbhJ+MQlJ5RgxhFFYbO/STvYnrKuin2KkP6R0rlbIgdIw9xI0UU0cg/zlHpe6EBOhEbFRKGXuzLKoLxwQLD2DJSwEoSwNxi35Ptj35SJPgiSGxNuWrl/0swb0DT8PNc5nR8atOOkC/QyRW/plV24Ph++/ZFonC71NZlf4Iy9uK8RmRcXp0BIMLzI9qqen3xm9CZYbUYSC1rv9qqA32K6K6wg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=TcHi5I83kVIBOFivhvcswbgcSNkbpV2Bqv1b3lkWLZA=; b=FtdWjY8Fmbv4veFhrKttzWqh2xugs9XT3XeWYs1UQZmDu9+/BIafj9oeCWYEoBhRMKSCMe2IKrxr9X836GE8FM6egr3vGk0yOGfwyBh/sPH9C1jnAgwidgfQYPDj2VrwmlXJrVtpmupc5Ta9QjSzd9Q5uLNP9i0DFWizhlGbeoMPGAQ9g44NvNpeW7+uCJjPWHIfhpfiGZDwC+1ieKTwvcn4Pb9OSM81VYIV82bskqoawjU1dubg4nFVIYiE/yfwrqugaWJT1CBn2TIveEZPo3vKIup5kxrCdknbmDPKLnl7Cfwx6i8wq+v3iyTHchMvekXS/2b9pcj2DHAMpA4fag== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.234) smtp.rcpttodomain=ashroe.eu smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=TcHi5I83kVIBOFivhvcswbgcSNkbpV2Bqv1b3lkWLZA=; b=KR5Dx2pQXISiMQLW7C07os4azePck13aNo19x3PNGuO/dFheDHqqXUW73SaVf49HSr2Tmo/BQf2uCtkS9thQO31Rci+/HMfylyKHVRyQ8eIiRzWU4wiT1yS2gwPL2FY73OwcfLc2wnPw7sUtqlIhTay4f4yLQSkH8RbaV9RZ9hSZRBu/2ELAo7xgMSrgVSLG1zu22IFTyIGp+DntKvBZ6cAp5ZyVeGUCuYJp/ST3xrgfz1ZD7TYJ7LLa7y37Ujk7i5xdz2y2jL0sBeG3km6/+aATCdwY9+WCJ5/ba21QFA7MTe9PDCWQU2/7hpbfj3TSgWY49GlXXd3IqdLOiauIaQ== Received: from MW4PR03CA0169.namprd03.prod.outlook.com (2603:10b6:303:8d::24) by MWHPR1201MB2476.namprd12.prod.outlook.com (2603:10b6:300:de::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4995.16; Wed, 23 Feb 2022 16:43:41 +0000 Received: from CO1NAM11FT055.eop-nam11.prod.protection.outlook.com (2603:10b6:303:8d:cafe::b3) by MW4PR03CA0169.outlook.office365.com (2603:10b6:303:8d::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5017.23 via Frontend Transport; Wed, 23 Feb 2022 16:43:41 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.234) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.234 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.234; helo=mail.nvidia.com; Received: from mail.nvidia.com (12.22.5.234) by CO1NAM11FT055.mail.protection.outlook.com (10.13.175.129) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5017.22 via Frontend Transport; Wed, 23 Feb 2022 16:43:41 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by DRHQMAIL101.nvidia.com (10.27.9.10) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Wed, 23 Feb 2022 16:43:40 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.9; Wed, 23 Feb 2022 08:43:38 -0800 Received: from nvidia.com (10.127.8.11) by mail.nvidia.com (10.129.68.9) with Microsoft SMTP Server id 15.2.986.9 via Frontend Transport; Wed, 23 Feb 2022 08:43:37 -0800 From: Dmitry Kozlyuk To: CC: , Viacheslav Ovsiienko , "Matan Azrad" , Ray Kinsella Subject: [PATCH 1/3] common/mlx5: add Netlink event helpers Date: Wed, 23 Feb 2022 18:43:31 +0200 Message-ID: <20220223164333.3834590-2-dkozlyuk@nvidia.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220223164333.3834590-1-dkozlyuk@nvidia.com> References: <20220223164333.3834590-1-dkozlyuk@nvidia.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 62969588-6275-41c3-48b2-08d9f6eba5fd X-MS-TrafficTypeDiagnostic: MWHPR1201MB2476:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: nWoP2jVheeg/ILuilNL0e4fOPwcW+RRKTkTAtaBkw4E4DAb5LL6r0c7D6eofqdlguclkJ7rjPPs1e0edgzW7P2X2MwacO6meY+16e08kN/Y4IoIKZJeQOjeGFS8epeoPOHFHdyJr8SqIIeymT+PKQNonPOjPTUy2yelzb+HT0Co/cPtRAhKXIn47j6g6mhivmPacti6Re+jZIef548Q23ahALMFjwU4d+DvYRyZesRDoMAR5hL7LYxnWYEpT5XVeU8deM6mPBQN4SAtVMNvgIlnnJK45X++wmwkCuyrwnNB7LCSvvv49yu3M141JRgWm7DKL9gBLQzi8nekLmf+uLzGorGWvAh+W0hT9FvP0323BClpLTXNr3sFszEVJwYbKehkVKCNgRlhtPX41MhKrhiTKYzkQCmUrqALbM1W0szdYknKhvuk7Mx3dBk6piL8WuFHyP9hFJtzsiIMBP/l9mBaathoEFqJKhytLokhSmDfJW2OZAQsHFYwMmaf9DYvw+f+/SrTeMKvHrtQmpskvoEarXJnsq3sMGBDSbIdcpHJBYqY49zJPvb4rEhPuKkNG4J6rgaw/Y+2jNhlExTey7mf1VJ47YOQ6xKmdZBlFLGVNfFcuFnUucg7IkgWiLkt1QZPGxVnTxrSBIwYpn3+0XABRrWd8WkW8gUqbsAz73t7LUhUQ9lV9n2WqPnxp7RUR9ZhTSbrMi1V3DxbrRcZRuPkN3ITBzkeaulsBEXGpLaQ9K57bsPoEl/7JfSyfS9xt X-Forefront-Antispam-Report: CIP:12.22.5.234; CTRY:US; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:mail.nvidia.com; PTR:InfoNoRecords; CAT:NONE; SFS:(13230001)(4636009)(40470700004)(36840700001)(46966006)(36860700001)(426003)(6286002)(1076003)(336012)(6916009)(316002)(2616005)(55016003)(86362001)(36756003)(54906003)(5660300002)(40460700003)(83380400001)(26005)(186003)(47076005)(82310400004)(8936002)(2906002)(8676002)(6666004)(81166007)(70206006)(508600001)(4326008)(70586007)(356005)(7696005)(36900700001)(309714004); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Feb 2022 16:43:41.1129 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 62969588-6275-41c3-48b2-08d9f6eba5fd X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[12.22.5.234]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT055.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR1201MB2476 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Introduce mlx5_nl_read_events() to read Netlink events (technically, messages) from a socket that was configured to listen for them via a new mlx5_nl_init() parameter. Add mlx5_nl_parse_link_status_update() helper to extract information from link-related events. This patch is a shared base for later fixes. Cc: stable@dpdk.org Signed-off-by: Dmitry Kozlyuk Reviewed-by: Viacheslav Ovsiienko --- drivers/common/mlx5/linux/mlx5_common_os.c | 2 +- drivers/common/mlx5/linux/mlx5_nl.c | 102 ++++++++++++++++++++- drivers/common/mlx5/linux/mlx5_nl.h | 8 +- drivers/common/mlx5/version.map | 2 + drivers/net/mlx5/linux/mlx5_os.c | 8 +- drivers/net/mlx5/linux/mlx5_vlan_os.c | 2 +- 6 files changed, 116 insertions(+), 8 deletions(-) diff --git a/drivers/common/mlx5/linux/mlx5_common_os.c b/drivers/common/mlx5/linux/mlx5_common_os.c index 0d3e24e04e..25e09bb55b 100644 --- a/drivers/common/mlx5/linux/mlx5_common_os.c +++ b/drivers/common/mlx5/linux/mlx5_common_os.c @@ -487,7 +487,7 @@ mlx5_os_get_ibv_device(const struct rte_pci_addr *addr) static int mlx5_nl_roce_disable(const char *addr) { - int nlsk_fd = mlx5_nl_init(NETLINK_GENERIC); + int nlsk_fd = mlx5_nl_init(NETLINK_GENERIC, 0); int devlink_id; int enable; int ret; diff --git a/drivers/common/mlx5/linux/mlx5_nl.c b/drivers/common/mlx5/linux/mlx5_nl.c index fd4c2d2625..5d04857b38 100644 --- a/drivers/common/mlx5/linux/mlx5_nl.c +++ b/drivers/common/mlx5/linux/mlx5_nl.c @@ -185,19 +185,22 @@ uint32_t atomic_sn; * * @param protocol * Netlink protocol (e.g. NETLINK_ROUTE, NETLINK_RDMA). + * @param groups + * Groups to listen (e.g. RTMGRP_LINK), can be 0. * * @return * A file descriptor on success, a negative errno value otherwise and * rte_errno is set. */ int -mlx5_nl_init(int protocol) +mlx5_nl_init(int protocol, int groups) { int fd; int buf_size; socklen_t opt_size; struct sockaddr_nl local = { .nl_family = AF_NETLINK, + .nl_groups = groups, }; int ret; @@ -1862,3 +1865,100 @@ mlx5_nl_enable_roce_set(int nlsk_fd, int family_id, const char *pci_addr, /* Now, need to reload the driver. */ return mlx5_nl_driver_reload(nlsk_fd, family_id, pci_addr); } + +/** + * Try to parse a Netlink message as a link status update. + * + * @param hdr + * Netlink message header. + * @param[out] ifindex + * Index of the updated interface. + * + * @return + * 0 on success, negative on failure. + */ +int +mlx5_nl_parse_link_status_update(struct nlmsghdr *hdr, uint32_t *ifindex) +{ + struct ifinfomsg *info; + + switch (hdr->nlmsg_type) { + case RTM_NEWLINK: + case RTM_DELLINK: + case RTM_GETLINK: + case RTM_SETLINK: + info = NLMSG_DATA(hdr); + *ifindex = info->ifi_index; + return 0; + } + return -1; +} + +/** + * Read pending events from a Netlink socket. + * + * @param nlsk_fd + * Netlink socket. + * @param cb + * Callback invoked for each of the events. + * @param cb_arg + * User data for the callback. + * + * @return + * 0 on success, including the case when there are no events. + * Negative on failure and rte_errno is set. + */ +int +mlx5_nl_read_events(int nlsk_fd, mlx5_nl_event_cb *cb, void *cb_arg) +{ + char buf[8192]; + struct sockaddr_nl addr; + struct iovec iov = { + .iov_base = buf, + .iov_len = sizeof(buf), + }; + struct msghdr msg = { + .msg_name = &addr, + .msg_namelen = sizeof(addr), + .msg_iov = &iov, + .msg_iovlen = 1, + }; + struct nlmsghdr *hdr; + ssize_t size; + + while (1) { + size = recvmsg(nlsk_fd, &msg, MSG_DONTWAIT); + if (size < 0) { + if (errno == EAGAIN) + return 0; + if (errno == EINTR) + continue; + DRV_LOG(DEBUG, "Failed to receive netlink message: %s", + strerror(errno)); + rte_errno = errno; + return -rte_errno; + } + hdr = (struct nlmsghdr *)buf; + while (size >= (ssize_t)sizeof(*hdr)) { + ssize_t msg_len = hdr->nlmsg_len; + ssize_t data_len = msg_len - sizeof(*hdr); + ssize_t aligned_len; + + if (data_len < 0) { + DRV_LOG(DEBUG, "Netlink message too short"); + rte_errno = EINVAL; + return -rte_errno; + } + aligned_len = NLMSG_ALIGN(msg_len); + if (aligned_len > size) { + DRV_LOG(DEBUG, "Netlink message too long"); + rte_errno = EINVAL; + return -rte_errno; + } + cb(hdr, cb_arg); + hdr = RTE_PTR_ADD(hdr, aligned_len); + size -= aligned_len; + } + } + return 0; +} diff --git a/drivers/common/mlx5/linux/mlx5_nl.h b/drivers/common/mlx5/linux/mlx5_nl.h index 2063c0deeb..0b7552338a 100644 --- a/drivers/common/mlx5/linux/mlx5_nl.h +++ b/drivers/common/mlx5/linux/mlx5_nl.h @@ -11,6 +11,7 @@ #include "mlx5_common.h" +typedef void (mlx5_nl_event_cb)(struct nlmsghdr *hdr, void *user_data); /* VLAN netdev for VLAN workaround. */ struct mlx5_nl_vlan_dev { @@ -30,7 +31,7 @@ struct mlx5_nl_vlan_vmwa_context { }; __rte_internal -int mlx5_nl_init(int protocol); +int mlx5_nl_init(int protocol, int groups); __rte_internal int mlx5_nl_mac_addr_add(int nlsk_fd, unsigned int iface_idx, uint64_t *mac_own, struct rte_ether_addr *mac, uint32_t index); @@ -75,4 +76,9 @@ int mlx5_nl_enable_roce_get(int nlsk_fd, int family_id, const char *pci_addr, int mlx5_nl_enable_roce_set(int nlsk_fd, int family_id, const char *pci_addr, int enable); +__rte_internal +int mlx5_nl_read_events(int nlsk_fd, mlx5_nl_event_cb *cb, void *cb_arg); +__rte_internal +int mlx5_nl_parse_link_status_update(struct nlmsghdr *hdr, uint32_t *ifindex); + #endif /* RTE_PMD_MLX5_NL_H_ */ diff --git a/drivers/common/mlx5/version.map b/drivers/common/mlx5/version.map index 462b7cea5e..d9b7ccacde 100644 --- a/drivers/common/mlx5/version.map +++ b/drivers/common/mlx5/version.map @@ -123,9 +123,11 @@ INTERNAL { mlx5_nl_mac_addr_flush; # WINDOWS_NO_EXPORT mlx5_nl_mac_addr_remove; # WINDOWS_NO_EXPORT mlx5_nl_mac_addr_sync; # WINDOWS_NO_EXPORT + mlx5_nl_parse_link_status_update; # WINDOWS_NO_EXPORT mlx5_nl_port_state; # WINDOWS_NO_EXPORT mlx5_nl_portnum; # WINDOWS_NO_EXPORT mlx5_nl_promisc; # WINDOWS_NO_EXPORT + mlx5_nl_read_events; # WINDOWS_NO_EXPORT mlx5_nl_switch_info; # WINDOWS_NO_EXPORT mlx5_nl_vf_mac_addr_modify; # WINDOWS_NO_EXPORT mlx5_nl_vlan_vmwa_create; # WINDOWS_NO_EXPORT diff --git a/drivers/net/mlx5/linux/mlx5_os.c b/drivers/net/mlx5/linux/mlx5_os.c index bbe05bb837..602473e8f7 100644 --- a/drivers/net/mlx5/linux/mlx5_os.c +++ b/drivers/net/mlx5/linux/mlx5_os.c @@ -1086,7 +1086,7 @@ mlx5_dev_spawn(struct rte_device *dpdk_dev, " old OFED/rdma-core version or firmware configuration"); #endif config->mpls_en = mpls_en; - nl_rdma = mlx5_nl_init(NETLINK_RDMA); + nl_rdma = mlx5_nl_init(NETLINK_RDMA, 0); /* Check port status. */ if (spawn->phys_port <= UINT8_MAX) { /* Legacy Verbs api only support u8 port number. */ @@ -1133,7 +1133,7 @@ mlx5_dev_spawn(struct rte_device *dpdk_dev, priv->mtu = RTE_ETHER_MTU; /* Some internal functions rely on Netlink sockets, open them now. */ priv->nl_socket_rdma = nl_rdma; - priv->nl_socket_route = mlx5_nl_init(NETLINK_ROUTE); + priv->nl_socket_route = mlx5_nl_init(NETLINK_ROUTE, 0); priv->representor = !!switch_info->representor; priv->master = !!switch_info->master; priv->domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID; @@ -2130,8 +2130,8 @@ mlx5_os_pci_probe_pf(struct mlx5_common_device *cdev, * matching ones, gathering into the list. */ struct ibv_device *ibv_match[ret + 1]; - int nl_route = mlx5_nl_init(NETLINK_ROUTE); - int nl_rdma = mlx5_nl_init(NETLINK_RDMA); + int nl_route = mlx5_nl_init(NETLINK_ROUTE, 0); + int nl_rdma = mlx5_nl_init(NETLINK_RDMA, 0); unsigned int i; while (ret-- > 0) { diff --git a/drivers/net/mlx5/linux/mlx5_vlan_os.c b/drivers/net/mlx5/linux/mlx5_vlan_os.c index 005904bdfe..7ee2460a23 100644 --- a/drivers/net/mlx5/linux/mlx5_vlan_os.c +++ b/drivers/net/mlx5/linux/mlx5_vlan_os.c @@ -136,7 +136,7 @@ mlx5_vlan_vmwa_init(struct rte_eth_dev *dev, uint32_t ifindex) return NULL; } rte_spinlock_init(&vmwa->sl); - vmwa->nl_socket = mlx5_nl_init(NETLINK_ROUTE); + vmwa->nl_socket = mlx5_nl_init(NETLINK_ROUTE, 0); if (vmwa->nl_socket < 0) { DRV_LOG(WARNING, "Can not create Netlink socket" -- 2.25.1