From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from EUR03-VE1-obe.outbound.protection.outlook.com (mail-eopbgr50081.outbound.protection.outlook.com [40.107.5.81]) by dpdk.org (Postfix) with ESMTP id 657D523A for ; Sun, 6 May 2018 08:06:35 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Mellanox.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=5O3Ne4GPVg8UnALKe66UknCleGk5jEkDjrO5lZ8ayA8=; b=JgsyGJVOnEi9ab3D4o67oK3FX0ua3dV9Bj/Vw/TZIh3PHFMP7lk2JCMzNysF1VDFU+SEwu6JXIc8yloCWsX11B6grCXi9/qd6Jea2RO/lyHnYHIkh0quzgrYqpkDkXO4p1sTHaT+SCNKExyDS/tnCqyfdLWOvxGfgrZEJoZwbbo= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=shahafs@mellanox.com; Received: from mellanox.com (141.226.120.58) by AM6PR05MB4422.eurprd05.prod.outlook.com (2603:10a6:209:43::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.735.16; Sun, 6 May 2018 06:06:33 +0000 From: Shahaf Shuler To: bluca@debian.org Cc: stable@dpdk.org, nelio.laranjeiro@6wind.com, yskoh@mellanox.com, adrien.mazarguil@6wind.com Date: Sun, 6 May 2018 09:05:30 +0300 Message-Id: X-Mailer: git-send-email 2.12.0 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [141.226.120.58] X-ClientProxiedBy: LO2P265CA0008.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:62::20) To AM6PR05MB4422.eurprd05.prod.outlook.com (2603:10a6:209:43::15) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652020)(5600026)(48565401081)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603328)(7153060)(7193020); SRVR:AM6PR05MB4422; X-Microsoft-Exchange-Diagnostics: 1; AM6PR05MB4422; 3:kN4X7+XYahV6f360CnrsFBJdGYyaiZIzKAWHZqqcY1j2fGyAIgSyRpWJzlQWGn8haaLtD6goMJnvc9mANpZQYXrE9Ci5kOMn44mZJHXyb5fpLB837taEZdDveKcZTHMkaff10GXxujV3DTUx+PqO9w6QOKofCkwNsgsuA/1LhdEGjM/RH8MgCQFxAW7dEbQSXhJ/rcaUuDyNk9D3VH60J0fMtF/N/FA2yHHBjKI/4inTH5Ghs0VEL8RHPVONEL32; 25:pkhBRp4PYlOXTbB+wIQitq9R0rZvfdV9FmCtXKxHxZaaZ0qEGab0gTjc2M6foM4amTgSZLdI85uZqVcapvYM+mubbX4eAHMb0+m/N+4/TZKuH9zjRA/U9YDesFsYhL3KyZSwrNoOtFgk7NlLd1yOdGWScuJTbsWawLRVQvRS0UThO0oVEhCZfTUeFGAs0uVeT44+ajqJUT/0N0p/mE5MJiKfY3TlNOfrE2+p8Yv3CvNcB3weGC35ae6uSoV+mwh5nprMJ8Q6U6y9jVL8nH0+bldf+qx0TZ9uRU6IKhJHOVbndygmocTc3ljpC8MQflhUbYbFGaQfdB0xIv4U+aGFng==; 31:BLgzPwIP/Mgl3OVmvJmvrg4P9F0zCNaB0CBFeZgm9rApuWWaCmU6V7cf9CSyGbD+QJ6O0NruHMnj7+ouBCdrGygIzI4biJPrOZNgq8qBC5u+eZSZodC3HIU8P7WR7Z71L1KWVxP8XzIU/jjxSyIBZP9+lYKr+24BoV5aasdB6qfNmkB0hDdc80j/1RannRwKUC8zLsybSQocA9dbH//aNuxii8hpKYFKJmaKWc8CO64= X-MS-TrafficTypeDiagnostic: AM6PR05MB4422: X-LD-Processed: a652971c-7d2e-4d9b-a6a4-d149256f461b,ExtAddr X-Microsoft-Exchange-Diagnostics: 1; AM6PR05MB4422; 20:e458TuLd4W5mceBoajVKPgOAXE4pkcn5y2rpcHnJ5k61bZR9pOB0UZKRZFJAGGrQtPhLnMGgqo3vapFDTrEkCnV2dxkeflu+SGykZVWIT6lKtamyF4T6woYZsON/UCNLDmLMvziLuaNXNHrRsV+Ws/ki+/bTpTYy0/AtUxbrmjobfE3j70wow6njWTB2zqPQX9xlzM0+lnGIl0IykMsUMSixGWTlctTk9fDeuFrvkCfEbIUT2M83TEF1Bt08lJ9pn3cKS2AX8n+M82vOu4bBFo04e0R9AVkJq6kxD/57FzGomX4cOSLO+CIYwMzciQ8/9wkM+8rryVbaAoMxu3PYnx2aHXBqFdnm/ch156r3xmXx2nknNMVR0ij81IjFN0E68gEP6MSTgtSDb0vNne1bvKgA81vcLi4Sqr2++99pI1OeeJZsWVx7LdSP010xS/L0028eIe7VWfCZkCs26rLOZxnu9lH/WStveiW/6Tgm/2UR8JcRHcA4FVqgI/ygz68T; 4:D0dWhHwP4CcPy+pFee5IaMVFGEBxw0DBWIg9wbJxCs/qejD/9EKMzTtERNYd76Bt+FDlRk4TVLUSnqbzB/MRjDgm3mSvxkiy8OQrjV4DM/tgd5F50CWmnIq2MeFsRxyRPoA4Y1wkDzhJt7OngYl5HHPtgzi5DlKlW/piTSZ0dme5tvEDwKO/xzjRTaJX9+nx3wk8v1zgYfiOgEiZoeg05BVwAuz5Qh826EB6Kgj1cOkmzx9858TeL6ouOWgc0W+blHsF0qu+sMQA9BmfhZ29auyxcrGw8taMjTkT+A9YO7UK9rFcwflMNZJnoQKYCrs6RNHD8RyWsDgrB598NoOiNj+reu0JyVirlheBzAUC/+E= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(278428928389397)(85827821059158); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(8211001083)(6040522)(2401047)(8121501046)(5005006)(10201501046)(3002001)(3231254)(944501410)(52105095)(93006095)(93001095)(6055026)(6041310)(20161123564045)(20161123562045)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123558120)(6072148)(201708071742011); SRVR:AM6PR05MB4422; BCL:0; PCL:0; RULEID:; SRVR:AM6PR05MB4422; X-Forefront-PRVS: 06640999CA X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(39380400002)(396003)(346002)(39860400002)(376002)(366004)(189003)(199004)(59450400001)(386003)(69596002)(48376002)(16526019)(26005)(186003)(956004)(97736004)(25786009)(486006)(50466002)(476003)(2616005)(5660300001)(76176011)(11346002)(446003)(52116002)(7696005)(478600001)(51416003)(21086003)(6916009)(118296001)(6116002)(3846002)(106356001)(66066001)(16586007)(8936002)(8676002)(105586002)(2361001)(86362001)(575784001)(316002)(53936002)(2351001)(4326008)(68736007)(305945005)(81166006)(7736002)(81156014)(2906002)(55016002)(36756003)(47776003)(50226002); DIR:OUT; SFP:1101; SCL:1; SRVR:AM6PR05MB4422; H:mellanox.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:1; Received-SPF: None (protection.outlook.com: mellanox.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; AM6PR05MB4422; 23:VCRN3SnBKf0mWLmLYHKox6B0+eN1nTMegv8UbHBtY?= =?us-ascii?Q?7atFN9x0UOCyCL3OPx0N07QVvfr8yJBwh0osgGWUGdmA+B/O84dbHP1+/6W3?= =?us-ascii?Q?3aBqGhpcX0GfbY/uVTLpdS3BY4tJ5CE3GvLdZOErkgJYL9NhYQdvFmRR+77h?= =?us-ascii?Q?nt0OFgi0aiLNuSWFQiSJwgR4O4+MZg5KWRHr9CQto2qGrkJTBio2shogCkIh?= =?us-ascii?Q?OIE3IQamW3bemRDuj0O/F+TIWIevEhAcbg4xDc178AS0f9vaOnyEAK4hWlfZ?= =?us-ascii?Q?4Qpud95EG1ssiNzLG5IhKaAdEewrxj0YdV+i2QEhffQuELIaUXeMg/HNvcyy?= =?us-ascii?Q?MNrg6XrSAyjZltfDmaL5ECX9tdEMnY/C2Z9Ye6GadaPoNJrYEcZHxE4k0dkP?= =?us-ascii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osticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; AM6PR05MB4422; 7:RRxnBwhAFwqsu3JBQnUxnt9t63Mny30u4Rqi+oACGLyRmeTBNUVbn7dH+lJ8+v6kL1K5QFg9E8eycTw89JZW7LwU+KYOjPPOyPFvezwDFj8fsmcmJyN50K8ydl4Hi1yaOGrxTRn5qNHT6ehXi9yBWSQEz50PP+328bu5tb+XLJuHTRY5p4UOvm3SHbUBEwaLyQX3P5si91YV7SZj90VLRk4/bSkAM29UkgEgy7+PCyqVxitA6lMpK/h6rDeBxvmF X-MS-Office365-Filtering-Correlation-Id: e1faf40a-ff40-4897-99ca-08d5b31784ee X-OriginatorOrg: Mellanox.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 May 2018 06:06:33.3017 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e1faf40a-ff40-4897-99ca-08d5b31784ee X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: a652971c-7d2e-4d9b-a6a4-d149256f461b X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR05MB4422 Subject: [dpdk-stable] [PATCH 01/33] net/mlx: control netdevices through ioctl only X-BeenThere: stable@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches for DPDK stable branches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 06 May 2018 06:06:35 -0000 From: Adrien Mazarguil [ backported from upstream commit fc40db997323bb0e9b725a6e8d65eae95372446c ] Several control operations implemented by these PMDs affect netdevices through sysfs, itself subject to file system permission checks enforced by the kernel, which limits their use for most purposes to applications running with root privileges. Since performing the same operations through ioctl() requires fewer capabilities (only CAP_NET_ADMIN) and given the remaining operations are already implemented this way, this patch standardizes on ioctl() and gets rid of redundant code. Signed-off-by: Adrien Mazarguil Reviewed-by: Marcelo Ricardo Leitner --- drivers/net/mlx4/mlx4_ethdev.c | 192 ++------------------------- drivers/net/mlx5/mlx5.h | 2 - drivers/net/mlx5/mlx5_ethdev.c | 255 ++++-------------------------------- drivers/net/mlx5/mlx5_stats.c | 28 +++- 4 files changed, 63 insertions(+), 414 deletions(-) diff --git a/drivers/net/mlx4/mlx4_ethdev.c b/drivers/net/mlx4/mlx4_ethdev.c index 3bc6927313..fbeef16c8f 100644 --- a/drivers/net/mlx4/mlx4_ethdev.c +++ b/drivers/net/mlx4/mlx4_ethdev.c @@ -132,167 +132,6 @@ mlx4_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE]) } /** - * Read from sysfs entry. - * - * @param[in] priv - * Pointer to private structure. - * @param[in] entry - * Entry name relative to sysfs path. - * @param[out] buf - * Data output buffer. - * @param size - * Buffer size. - * - * @return - * Number of bytes read on success, negative errno value otherwise and - * rte_errno is set. - */ -static int -mlx4_sysfs_read(const struct priv *priv, const char *entry, - char *buf, size_t size) -{ - char ifname[IF_NAMESIZE]; - FILE *file; - int ret; - - ret = mlx4_get_ifname(priv, &ifname); - if (ret) - return ret; - - MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path, - ifname, entry); - - file = fopen(path, "rb"); - if (file == NULL) { - rte_errno = errno; - return -rte_errno; - } - ret = fread(buf, 1, size, file); - if ((size_t)ret < size && ferror(file)) { - rte_errno = EIO; - ret = -rte_errno; - } else { - ret = size; - } - fclose(file); - return ret; -} - -/** - * Write to sysfs entry. - * - * @param[in] priv - * Pointer to private structure. - * @param[in] entry - * Entry name relative to sysfs path. - * @param[in] buf - * Data buffer. - * @param size - * Buffer size. - * - * @return - * Number of bytes written on success, negative errno value otherwise and - * rte_errno is set. - */ -static int -mlx4_sysfs_write(const struct priv *priv, const char *entry, - char *buf, size_t size) -{ - char ifname[IF_NAMESIZE]; - FILE *file; - int ret; - - ret = mlx4_get_ifname(priv, &ifname); - if (ret) - return ret; - - MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path, - ifname, entry); - - file = fopen(path, "wb"); - if (file == NULL) { - rte_errno = errno; - return -rte_errno; - } - ret = fwrite(buf, 1, size, file); - if ((size_t)ret < size || ferror(file)) { - rte_errno = EIO; - ret = -rte_errno; - } else { - ret = size; - } - fclose(file); - return ret; -} - -/** - * Get unsigned long sysfs property. - * - * @param priv - * Pointer to private structure. - * @param[in] name - * Entry name relative to sysfs path. - * @param[out] value - * Value output buffer. - * - * @return - * 0 on success, negative errno value otherwise and rte_errno is set. - */ -static int -mlx4_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value) -{ - int ret; - unsigned long value_ret; - char value_str[32]; - - ret = mlx4_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1)); - if (ret < 0) { - DEBUG("cannot read %s value from sysfs: %s", - name, strerror(rte_errno)); - return ret; - } - value_str[ret] = '\0'; - errno = 0; - value_ret = strtoul(value_str, NULL, 0); - if (errno) { - rte_errno = errno; - DEBUG("invalid %s value `%s': %s", name, value_str, - strerror(rte_errno)); - return -rte_errno; - } - *value = value_ret; - return 0; -} - -/** - * Set unsigned long sysfs property. - * - * @param priv - * Pointer to private structure. - * @param[in] name - * Entry name relative to sysfs path. - * @param value - * Value to set. - * - * @return - * 0 on success, negative errno value otherwise and rte_errno is set. - */ -static int -mlx4_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value) -{ - int ret; - MKSTR(value_str, "%lu", value); - - ret = mlx4_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1)); - if (ret < 0) { - DEBUG("cannot write %s `%s' (%lu) to sysfs: %s", - name, value_str, value, strerror(rte_errno)); - return ret; - } - return 0; -} - -/** * Perform ifreq ioctl() on associated Ethernet device. * * @param[in] priv @@ -361,12 +200,12 @@ mlx4_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN]) int mlx4_mtu_get(struct priv *priv, uint16_t *mtu) { - unsigned long ulong_mtu = 0; - int ret = mlx4_get_sysfs_ulong(priv, "mtu", &ulong_mtu); + struct ifreq request; + int ret = mlx4_ifreq(priv, SIOCGIFMTU, &request); if (ret) return ret; - *mtu = ulong_mtu; + *mtu = request.ifr_mtu; return 0; } @@ -385,20 +224,13 @@ int mlx4_mtu_set(struct rte_eth_dev *dev, uint16_t mtu) { struct priv *priv = dev->data->dev_private; - uint16_t new_mtu; - int ret = mlx4_set_sysfs_ulong(priv, "mtu", mtu); + struct ifreq request = { .ifr_mtu = mtu, }; + int ret = mlx4_ifreq(priv, SIOCSIFMTU, &request); if (ret) return ret; - ret = mlx4_mtu_get(priv, &new_mtu); - if (ret) - return ret; - if (new_mtu == mtu) { - priv->mtu = mtu; - return 0; - } - rte_errno = EINVAL; - return -rte_errno; + priv->mtu = mtu; + return 0; } /** @@ -417,14 +249,14 @@ mlx4_mtu_set(struct rte_eth_dev *dev, uint16_t mtu) static int mlx4_set_flags(struct priv *priv, unsigned int keep, unsigned int flags) { - unsigned long tmp = 0; - int ret = mlx4_get_sysfs_ulong(priv, "flags", &tmp); + struct ifreq request; + int ret = mlx4_ifreq(priv, SIOCGIFFLAGS, &request); if (ret) return ret; - tmp &= keep; - tmp |= (flags & (~keep)); - return mlx4_set_sysfs_ulong(priv, "flags", tmp); + request.ifr_flags &= keep; + request.ifr_flags |= flags & ~keep; + return mlx4_ifreq(priv, SIOCSIFFLAGS, &request); } /** diff --git a/drivers/net/mlx5/mlx5.h b/drivers/net/mlx5/mlx5.h index 965c19f21d..da44faaf4f 100644 --- a/drivers/net/mlx5/mlx5.h +++ b/drivers/net/mlx5/mlx5.h @@ -209,8 +209,6 @@ struct priv *mlx5_get_priv(struct rte_eth_dev *dev); int mlx5_is_secondary(void); int priv_get_ifname(const struct priv *, char (*)[IF_NAMESIZE]); int priv_ifreq(const struct priv *, int req, struct ifreq *); -int priv_is_ib_cntr(const char *); -int priv_get_cntr_sysfs(struct priv *, const char *, uint64_t *); int priv_get_num_vfs(struct priv *, uint16_t *); int priv_get_mtu(struct priv *, uint16_t *); int priv_set_flags(struct priv *, unsigned int, unsigned int); diff --git a/drivers/net/mlx5/mlx5_ethdev.c b/drivers/net/mlx5/mlx5_ethdev.c index 666507691c..b73cb53df0 100644 --- a/drivers/net/mlx5/mlx5_ethdev.c +++ b/drivers/net/mlx5/mlx5_ethdev.c @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -173,181 +174,6 @@ priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE]) } /** - * Check if the counter is located on ib counters file. - * - * @param[in] cntr - * Counter name. - * - * @return - * 1 if counter is located on ib counters file , 0 otherwise. - */ -int -priv_is_ib_cntr(const char *cntr) -{ - if (!strcmp(cntr, "out_of_buffer")) - return 1; - return 0; -} - -/** - * Read from sysfs entry. - * - * @param[in] priv - * Pointer to private structure. - * @param[in] entry - * Entry name relative to sysfs path. - * @param[out] buf - * Data output buffer. - * @param size - * Buffer size. - * - * @return - * 0 on success, -1 on failure and errno is set. - */ -static int -priv_sysfs_read(const struct priv *priv, const char *entry, - char *buf, size_t size) -{ - char ifname[IF_NAMESIZE]; - FILE *file; - int ret; - int err; - - if (priv_get_ifname(priv, &ifname)) - return -1; - - if (priv_is_ib_cntr(entry)) { - MKSTR(path, "%s/ports/1/hw_counters/%s", - priv->ibdev_path, entry); - file = fopen(path, "rb"); - } else { - MKSTR(path, "%s/device/net/%s/%s", - priv->ibdev_path, ifname, entry); - file = fopen(path, "rb"); - } - if (file == NULL) - return -1; - ret = fread(buf, 1, size, file); - err = errno; - if (((size_t)ret < size) && (ferror(file))) - ret = -1; - else - ret = size; - fclose(file); - errno = err; - return ret; -} - -/** - * Write to sysfs entry. - * - * @param[in] priv - * Pointer to private structure. - * @param[in] entry - * Entry name relative to sysfs path. - * @param[in] buf - * Data buffer. - * @param size - * Buffer size. - * - * @return - * 0 on success, -1 on failure and errno is set. - */ -static int -priv_sysfs_write(const struct priv *priv, const char *entry, - char *buf, size_t size) -{ - char ifname[IF_NAMESIZE]; - FILE *file; - int ret; - int err; - - if (priv_get_ifname(priv, &ifname)) - return -1; - - MKSTR(path, "%s/device/net/%s/%s", priv->ibdev_path, ifname, entry); - - file = fopen(path, "wb"); - if (file == NULL) - return -1; - ret = fwrite(buf, 1, size, file); - err = errno; - if (((size_t)ret < size) || (ferror(file))) - ret = -1; - else - ret = size; - fclose(file); - errno = err; - return ret; -} - -/** - * Get unsigned long sysfs property. - * - * @param priv - * Pointer to private structure. - * @param[in] name - * Entry name relative to sysfs path. - * @param[out] value - * Value output buffer. - * - * @return - * 0 on success, -1 on failure and errno is set. - */ -static int -priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value) -{ - int ret; - unsigned long value_ret; - char value_str[32]; - - ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1)); - if (ret == -1) { - DEBUG("cannot read %s value from sysfs: %s", - name, strerror(errno)); - return -1; - } - value_str[ret] = '\0'; - errno = 0; - value_ret = strtoul(value_str, NULL, 0); - if (errno) { - DEBUG("invalid %s value `%s': %s", name, value_str, - strerror(errno)); - return -1; - } - *value = value_ret; - return 0; -} - -/** - * Set unsigned long sysfs property. - * - * @param priv - * Pointer to private structure. - * @param[in] name - * Entry name relative to sysfs path. - * @param value - * Value to set. - * - * @return - * 0 on success, -1 on failure and errno is set. - */ -static int -priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value) -{ - int ret; - MKSTR(value_str, "%lu", value); - - ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1)); - if (ret == -1) { - DEBUG("cannot write %s `%s' (%lu) to sysfs: %s", - name, value_str, value, strerror(errno)); - return -1; - } - return 0; -} - -/** * Perform ifreq ioctl() on associated Ethernet device. * * @param[in] priv @@ -390,20 +216,25 @@ priv_get_num_vfs(struct priv *priv, uint16_t *num_vfs) { /* The sysfs entry name depends on the operating system. */ const char **name = (const char *[]){ - "device/sriov_numvfs", - "device/mlx5_num_vfs", + "sriov_numvfs", + "mlx5_num_vfs", NULL, }; - int ret; do { - unsigned long ulong_num_vfs; + int n; + FILE *file; + MKSTR(path, "%s/device/%s", priv->ibdev_path, *name); - ret = priv_get_sysfs_ulong(priv, *name, &ulong_num_vfs); - if (!ret) - *num_vfs = ulong_num_vfs; - } while (*(++name) && ret); - return ret; + file = fopen(path, "rb"); + if (!file) + continue; + n = fscanf(file, "%" SCNu16, num_vfs); + fclose(file); + if (n == 1) + return 0; + } while (*(++name)); + return -1; } /** @@ -420,35 +251,12 @@ priv_get_num_vfs(struct priv *priv, uint16_t *num_vfs) int priv_get_mtu(struct priv *priv, uint16_t *mtu) { - unsigned long ulong_mtu; + struct ifreq request; + int ret = priv_ifreq(priv, SIOCGIFMTU, &request); - if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1) - return -1; - *mtu = ulong_mtu; - return 0; -} - -/** - * Read device counter from sysfs. - * - * @param priv - * Pointer to private structure. - * @param name - * Counter name. - * @param[out] cntr - * Counter output buffer. - * - * @return - * 0 on success, -1 on failure and errno is set. - */ -int -priv_get_cntr_sysfs(struct priv *priv, const char *name, uint64_t *cntr) -{ - unsigned long ulong_ctr; - - if (priv_get_sysfs_ulong(priv, name, &ulong_ctr) == -1) - return -1; - *cntr = ulong_ctr; + if (ret) + return ret; + *mtu = request.ifr_mtu; return 0; } @@ -466,15 +274,9 @@ priv_get_cntr_sysfs(struct priv *priv, const char *name, uint64_t *cntr) static int priv_set_mtu(struct priv *priv, uint16_t mtu) { - uint16_t new_mtu; + struct ifreq request = { .ifr_mtu = mtu, }; - if (priv_set_sysfs_ulong(priv, "mtu", mtu) || - priv_get_mtu(priv, &new_mtu)) - return -1; - if (new_mtu == mtu) - return 0; - errno = EINVAL; - return -1; + return priv_ifreq(priv, SIOCSIFMTU, &request); } /** @@ -493,13 +295,14 @@ priv_set_mtu(struct priv *priv, uint16_t mtu) int priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags) { - unsigned long tmp; + struct ifreq request; + int ret = priv_ifreq(priv, SIOCGIFFLAGS, &request); - if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1) - return -1; - tmp &= keep; - tmp |= (flags & (~keep)); - return priv_set_sysfs_ulong(priv, "flags", tmp); + if (ret) + return ret; + request.ifr_flags &= keep; + request.ifr_flags |= flags & ~keep; + return priv_ifreq(priv, SIOCSIFFLAGS, &request); } /** diff --git a/drivers/net/mlx5/mlx5_stats.c b/drivers/net/mlx5/mlx5_stats.c index 378472a704..eb9c65dcc9 100644 --- a/drivers/net/mlx5/mlx5_stats.c +++ b/drivers/net/mlx5/mlx5_stats.c @@ -3,8 +3,11 @@ * Copyright 2015 Mellanox. */ +#include #include #include +#include +#include #include #include @@ -19,6 +22,7 @@ struct mlx5_counter_ctrl { char dpdk_name[RTE_ETH_XSTATS_NAME_SIZE]; /* Name of the counter on the device table. */ char ctr_name[RTE_ETH_XSTATS_NAME_SIZE]; + uint32_t ib:1; /**< Nonzero for IB counters. */ }; static const struct mlx5_counter_ctrl mlx5_counters_init[] = { @@ -93,6 +97,7 @@ static const struct mlx5_counter_ctrl mlx5_counters_init[] = { { .dpdk_name = "rx_out_of_buffer", .ctr_name = "out_of_buffer", + .ib = 1, }, { .dpdk_name = "tx_packets_phy", @@ -143,13 +148,24 @@ priv_read_dev_counters(struct priv *priv, uint64_t *stats) return -1; } for (i = 0; i != xstats_n; ++i) { - if (priv_is_ib_cntr(mlx5_counters_init[i].ctr_name)) - priv_get_cntr_sysfs(priv, - mlx5_counters_init[i].ctr_name, - &stats[i]); - else + if (mlx5_counters_init[i].ib) { + FILE *file; + MKSTR(path, "%s/ports/1/hw_counters/%s", + priv->ibdev_path, + mlx5_counters_init[i].ctr_name); + + file = fopen(path, "rb"); + if (file) { + int n = fscanf(file, "%" SCNu64, &stats[i]); + + fclose(file); + if (n != 1) + stats[i] = 0; + } + } else { stats[i] = (uint64_t) et_stats->data[xstats_ctrl->dev_table_idx[i]]; + } } return 0; } @@ -232,7 +248,7 @@ priv_xstats_init(struct priv *priv) } } for (j = 0; j != xstats_n; ++j) { - if (priv_is_ib_cntr(mlx5_counters_init[j].ctr_name)) + if (mlx5_counters_init[j].ib) continue; if (xstats_ctrl->dev_table_idx[j] >= dev_stats_n) { WARN("counter \"%s\" is not recognized", -- 2.12.0