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 E745DA0C43; Tue, 14 Sep 2021 15:04:32 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 658144068F; Tue, 14 Sep 2021 15:04:32 +0200 (CEST) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by mails.dpdk.org (Postfix) with ESMTP id D981B4003C for ; Tue, 14 Sep 2021 15:04:30 +0200 (CEST) Received: from dggeme756-chm.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4H83Nl1GpbzbmSt; Tue, 14 Sep 2021 21:00:23 +0800 (CST) Received: from [10.67.103.128] (10.67.103.128) by dggeme756-chm.china.huawei.com (10.3.19.102) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2308.8; Tue, 14 Sep 2021 21:04:28 +0800 To: Stephen Hemminger CC: Thomas Monjalon , "dev@dpdk.org" , Ferruh Yigit , Declan Doherty , Keith Wiles References: <29b75903-d212-c6e6-eedf-e3bc92ab816a@huawei.com> <3787620.G55ntfxAi9@thomas> <89fa6c37-3ad0-f454-6ab3-cc47592ebba7@huawei.com> <20210913235901.25eb6df3@hermes.local> From: "Min Hu (Connor)" Message-ID: Date: Tue, 14 Sep 2021 21:04:28 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.3.1 MIME-Version: 1.0 In-Reply-To: <20210913235901.25eb6df3@hermes.local> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.67.103.128] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggeme756-chm.china.huawei.com (10.3.19.102) X-CFilter-Loop: Reflected Subject: Re: [dpdk-dev] Questions about rte_eth_link_speed_to_str API 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 Sender: "dev" Thanks Stephen, While I think this option is more clear and simple: +const char * +rte_eth_link_speed_to_str(uint32_t link_speed) +{ +#define SPEED_STRING_LEN 16 + static char name[SPEED_STRING_LEN]; + + if (link_speed == ETH_SPEED_NUM_NONE) + return "None"; + if (link_speed == ETH_SPEED_NUM_UNKNOWN) + return "Unknown"; + if (link_speed < ETH_SPEED_NUM_1G) { + snprintf(name, sizeof(name), "%u Mbps", link_speed); + } else if (link_speed % ETH_SPEED_NUM_1G != 0){ + snprintf(name, sizeof(name), "%.1f Gbps", + (double)link_speed / ETH_SPEED_NUM_1G); + } else { + snprintf(name, sizeof(name), "%u Gbps", + link_speed / ETH_SPEED_NUM_1G); + } + + return (const char *)name; +} How about any others' opinions, thanks. 在 2021/9/14 14:59, Stephen Hemminger 写道: > On Tue, 14 Sep 2021 11:25:44 +0800 > "Min Hu (Connor)" wrote: > >> Thanks Thomas, >> I am not sure if we need to print combined slaves speed. >> How about others' opinion ? @all >> >> BTW, If yes, one possible option may be like that: >> +const char * >> +rte_eth_link_speed_to_str(uint32_t link_speed) >> +{ >> + char name[16]; >> + >> + if (link_speed == ETH_SPEED_NUM_NONE) >> + return "None"; >> + if (link_speed == ETH_SPEED_NUM_NONE) >> + return "Unknown"; >> + if (link_speed < ETH_SPEED_NUM_1G) { >> + snprintf(name, sizeof(name), "%u Mbps", link_speed); >> + } else { >> + snprintf(name, sizeof(name), "%u Mbps", >> + link_speed / ETH_SPEED_NUM_1G); >> + } >> + >> + return name; >> +} >> >> But the float value is difficult to handle, like 2.5 Gbps for show. Any >> advices? >> >> 在 2021/9/13 18:26, Thomas Monjalon 写道: >>> 13/09/2021 10:45, Min Hu (Connor): >>>> Hi all, >>>> I have questions about rte_eth_link_speed_to_str API. >>>> The API converts link speed to string for display, But it only >>>> supports the following speeds, like that: >>>> case ETH_SPEED_NUM_NONE: return "None"; >>>> case ETH_SPEED_NUM_10M: return "10 Mbps"; >>>> case ETH_SPEED_NUM_100M: return "100 Mbps"; >>>> case ETH_SPEED_NUM_1G: return "1 Gbps"; >>>> case ETH_SPEED_NUM_2_5G: return "2.5 Gbps"; >>>> case ETH_SPEED_NUM_5G: return "5 Gbps"; >>>> case ETH_SPEED_NUM_10G: return "10 Gbps"; >>>> case ETH_SPEED_NUM_20G: return "20 Gbps"; >>>> case ETH_SPEED_NUM_25G: return "25 Gbps"; >>>> case ETH_SPEED_NUM_40G: return "40 Gbps"; >>>> case ETH_SPEED_NUM_50G: return "50 Gbps"; >>>> case ETH_SPEED_NUM_56G: return "56 Gbps"; >>>> case ETH_SPEED_NUM_100G: return "100 Gbps"; >>>> case ETH_SPEED_NUM_200G: return "200 Gbps"; >>>> case ETH_SPEED_NUM_UNKNOWN: return "Unknown"; >>>> default: return "Invalid"; >>>> >>>> In some cases, like bonding, for example, three slaves which >>>> link speed are 10Gbps, so link speed of bonding port will be >>>> 30Gbps, but it shows "Invalid". >>>> >>>> Is this reasonable? any comments will be welcome. >>> >>> Is it meaningful to print combined slaves speed? >>> If yes, we can do better then this fixed switch/case logic, >>> it shouldn't be too hard given it is a standard uint32_t value. >>> >>> >>> . >>> > > Since all the values are encoded numerically do some math. > This is what iproute2 has evolved to doing.. > > > int print_color_rate(bool use_iec, enum output_type type, enum color_attr color, > const char *key, const char *fmt, unsigned long long rate) > { > unsigned long kilo = use_iec ? 1024 : 1000; > const char *str = use_iec ? "i" : ""; > static char *units[5] = {"", "K", "M", "G", "T"}; > char *buf; > int rc; > int i; > > if (_IS_JSON_CONTEXT(type)) > return print_color_lluint(type, color, key, "%llu", rate); > > rate <<= 3; /* bytes/sec -> bits/sec */ > > for (i = 0; i < ARRAY_SIZE(units) - 1; i++) { > if (rate < kilo) > break; > if (((rate % kilo) != 0) && rate < 1000*kilo) > break; > rate /= kilo; > } > > rc = asprintf(&buf, "%.0f%s%sbit", (double)rate, units[i], > i > 0 ? str : ""); > if (rc < 0) > return -1; > > rc = print_color_string(type, color, key, fmt, buf); > free(buf); > } > > > . >