use RTE_DIM where possible
cover use RTE_DIM where possible
There is macro for computing the number of elements in an array RTE_DIM. But it is not used in many places where it could be.
Based on similar coccinelle script in Linux.
Stephen Hemminger (5):
cocci: add script to use RTE_DIM
app: use RTE_DIM
lib: use RTE_DIM
examples _use RTE_DIM
drivers/net: use RTE_DIM
app/test-flow-perf/main.c | 4 +-- app/test/test_ethdev_link.c | 3 +- app/test/test_security_inline_macsec.c | 47 +++++++++++++------------- devtools/cocci/rte-dim.cocci | 23 +++++++++++++ drivers/net/ark/ark_pktchkr.c | 2 +- drivers/net/ark/ark_pktgen.c | 2 +- drivers/net/bnxt/bnxt_hwrm.c | 12 +++---- drivers/net/e1000/em_rxtx.c | 3 +- drivers/net/iavf/iavf_ipsec_crypto.c | 3 +- drivers/net/igc/igc_ethdev.c | 3 +- drivers/net/ipn3ke/ipn3ke_tm.c | 3 +- drivers/net/ngbe/ngbe_ethdev.c | 6 ++-- drivers/net/octeontx/octeontx_stats.h | 3 +- drivers/net/txgbe/txgbe_ethdev.c | 9 ++--- drivers/net/txgbe/txgbe_ethdev_vf.c | 3 +- examples/l3fwd/main.c | 3 +- examples/qos_sched/init.c | 3 +- lib/cmdline/cmdline_vt100.c | 4 ++- lib/latencystats/rte_latencystats.c | 3 +- 19 files changed, 73 insertions(+), 66 deletions(-) create mode 100644 devtools/cocci/rte-dim.cocci
Signature · 1 line
2.43.0
PATCH 1/5 cocci: add script to use RTE_DIM
New script to find RTE_DIM should be used.
--- devtools/cocci/rte-dim.cocci | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 devtools/cocci/rte-dim.cocci
devtools/cocci/rte-dim.cocci new file +23 -0
@@ -0,0 +1,23 @@ +// SPDX-License-Identifier: BSD-3-Clause +// Use RTE_DIM macro instead of dividing sizeof array with sizeof an elmemnt +// +// Based of Linux kernela array_size.cocci +// +@@ +type T; +T[] E; +@@ +( +| +- (sizeof(E)/sizeof(E[...])) ++ RTE_DIM(E) +| +- (sizeof(E)/sizeof(*E)) ++ RTE_DIM(E) +| +- (sizeof(E)/sizeof(T)) ++ RTE_DIM(E) +| +- RTE_DIM((E)) ++ RTE_DIM(E) +)
Signature · 1 line
2.43.0
PATCH 2/5 app: use RTE_DIM
In reply to Stephen HemmingerUse RTE_DIM instead of computing directly with sizeof. Patch automatically generated via cocci/rte_dim.cocci.
--- app/test-flow-perf/main.c | 4 +-- app/test/test_ethdev_link.c | 3 +- app/test/test_security_inline_macsec.c | 47 +++++++++++++------------- 3 files changed, 25 insertions(+), 29 deletions(-)
app/test-flow-perf/main.c +1 -3
@@ -965,9 +965,7 @@ args_parse(int argc, char **argv) "meter-profile") == 0) { i =0 ; token =strsep (&optarg,",\0" ); -while (token !=NULL && i <sizeof ( - meter_profile_values) / -sizeof (uint64_t )) { +while (token !=NULL && i <RTE_DIM (meter_profile_values)) { meter_profile_values[i++] =atol (token); token =strsep (&optarg,",\0" ); }
app/test/test_ethdev_link.c +1 -2
@@ -135,8 +135,7 @@ test_link_speed_all_values(void) { "Invalid", 50505 } }; -for (i =0 ; i <sizeof (speed_str_map) /sizeof (struct link_speed_t ); - i++) { +for (i =0 ; i <RTE_DIM (speed_str_map); i++) { speed =rte_eth_link_speed_to_str (speed_str_map[i].link_speed); TEST_ASSERT_BUFFERS_ARE_EQUAL(speed_str_map[i].value, speed, strlen(speed_str_map[i].value),
app/test/test_security_inline_macsec.c +23 -24
185 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH 2/5] app: use RTE_DIM
Quoted 5 lines · Stephen Hemminger
Use
RTE_DIMinstead of computing directly with sizeof. Patch automatically generated viacocci/rte_dim.cocci.Signed-off-by Stephen Hemminger <[email protected]>---
RE: [EXTERNAL] [PATCH 2/5] app: use RTE_DIM
In reply to Stephen HemmingerQuoted 9 lines
Use
RTE_DIMinstead of computing directly with sizeof. Patch automatically generated viacocci/rte_dim.cocci.Signed-off-by Stephen Hemminger <[email protected]>--- app/test-flow-perf/main.c | 4 +-- app/test/test_ethdev_link.c | 3 +- app/test/test_security_inline_macsec.c | 47 +++++++++++++------------- 3 files changed, 25 insertions(+), 29 deletions(-)
PATCH 3/5 lib: use RTE_DIM
In reply to Stephen HemmingerUse RTE_DIM instead of computing directly with sizeof. Patch automatically generated via cocci/rte_dim.cocci.
--- lib/cmdline/cmdline_vt100.c | 4 +++- lib/latencystats/rte_latencystats.c | 3 +-- 2 files changed, 4 insertions(+), 3 deletions(-)
lib/cmdline/cmdline_vt100.c +3 -1
@@ -8,6 +8,8 @@#include <stdio.h> #include <string.h> +#include <rte_common.h> +#include "cmdline_vt100.h" const char *cmdline_vt100_commands[] = {
@@ -56,7 +58,7 @@ match_command(char *buf, unsigned int size)size_t cmdlen;unsigned int i =0 ; -for (i=0 ; i<sizeof (cmdline_vt100_commands)/sizeof (const char *) ; i++) { +for (i =0 ; i <RTE_DIM (cmdline_vt100_commands); i++) { cmd = *(cmdline_vt100_commands + i); cmdlen =strnlen (cmd,CMDLINE_VT100_BUF_SIZE );
lib/latencystats/rte_latencystats.c +1 -2
@@ -75,8 +75,7 @@ static const struct latency_stats_nameoff lat_stats_strings[] = { {"jitter_ns", offsetof(struct rte_latency_stats, jitter)}, }; -#define NUM_LATENCY_STATS (sizeof(lat_stats_strings) / \ - sizeof(lat_stats_strings[0])) +#define NUM_LATENCY_STATS RTE_DIM(lat_stats_strings)int32_t rte_latencystats_update (void )
Signature · 1 line
2.43.0
Re: [PATCH 3/5] lib: use RTE_DIM
Quoted 5 lines · Stephen Hemminger
Use
RTE_DIMinstead of computing directly with sizeof. Patch automatically generated viacocci/rte_dim.cocci.Signed-off-by Stephen Hemminger <[email protected]>---
PATCH 4/5 examples _use RTE_DIM
In reply to Stephen HemmingerUse RTE_DIM instead of computing directly with sizeof. Patch automatically generated via cocci/rte_dim.cocci.
--- examples/l3fwd/main.c | 3 +-- examples/qos_sched/init.c | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-)
examples/l3fwd/main.c +1 -2
@@ -116,8 +116,7 @@ static struct lcore_params lcore_params_array_default[] = { };static struct lcore_params * lcore_params = lcore_params_array_default; -static uint16_t nb_lcore_params =sizeof (lcore_params_array_default) / -sizeof (lcore_params_array_default[0 ]); +static uint16_t nb_lcore_params =RTE_DIM (lcore_params_array_default);static struct rte_eth_conf port_conf = { .rxmode = {
examples/qos_sched/init.c +1 -2
@@ -206,8 +206,7 @@ struct rte_sched_subport_params subport_params[MAX_SCHED_SUBPORTS] = { .n_pipes_per_subport_enabled = 4096, .qsize = {64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64}, .pipe_profiles = pipe_profiles, - .n_pipe_profiles = sizeof(pipe_profiles) / -sizeof (struct rte_sched_pipe_params ), + .n_pipe_profiles = RTE_DIM(pipe_profiles), .n_max_pipe_profiles = MAX_SCHED_PIPE_PROFILES, .cman_params = NULL, },
Signature · 1 line
2.43.0
Re: [PATCH 4/5] examples _use RTE_DIM
Quoted 5 lines · Stephen Hemminger
Use
RTE_DIMinstead of computing directly with sizeof. Patch automatically generated viacocci/rte_dim.cocci.Signed-off-by Stephen Hemminger <[email protected]>---
PATCH 5/5 drivers/net: use RTE_DIM
In reply to Stephen HemmingerUse RTE_DIM instead of computing directly with sizeof. Patch automatically generated via cocci/rte_dim.cocci. Code in base/ subdirectory manually excluded.
--- drivers/net/ark/ark_pktchkr.c | 2 +- drivers/net/ark/ark_pktgen.c | 2 +- drivers/net/bnxt/bnxt_hwrm.c | 12 ++++++------ drivers/net/e1000/em_rxtx.c | 3 +-- drivers/net/iavf/iavf_ipsec_crypto.c | 3 +-- drivers/net/igc/igc_ethdev.c | 3 +-- drivers/net/ipn3ke/ipn3ke_tm.c | 3 +-- drivers/net/ngbe/ngbe_ethdev.c | 6 ++---- drivers/net/octeontx/octeontx_stats.h | 3 +-- drivers/net/txgbe/txgbe_ethdev.c | 9 +++------ drivers/net/txgbe/txgbe_ethdev_vf.c | 3 +-- 11 files changed, 19 insertions(+), 30 deletions(-)
drivers/net/ark/ark_pktchkr.c +1 -1
@@ -320,7 +320,7 @@ options(const char *id) {unsigned int i; -for (i =0 ; i <sizeof (toptions) /sizeof (struct OPTIONS ); i++) { +for (i =0 ; i <RTE_DIM (toptions); i++) {if (strcmp (id, toptions[i].opt) ==0 )return &toptions[i]; }
drivers/net/ark/ark_pktgen.c +1 -1
@@ -297,7 +297,7 @@ options(const char *id) {unsigned int i; -for (i =0 ; i <sizeof (toptions) /sizeof (struct OPTIONS ); i++) { +for (i =0 ; i <RTE_DIM (toptions); i++) {if (strcmp (id, toptions[i].opt) ==0 )return &toptions[i]; }
drivers/net/bnxt/bnxt_hwrm.c +6 -6
@@ -72,13 +72,13 @@ const char *media_type[] = { "Unknown", "Twisted Pair","Direct Attached Copper" ,"Fiber" }; -#define MAX_MEDIA_TYPE (sizeof(media_type) / sizeof(const char *)) +#define MAX_MEDIA_TYPE RTE_DIM(media_type)const char *link_status_str[] = {"Down. No link or cable detected." ,"Down. No link, but a cable has been detected." ,"Up." , }; -#define MAX_LINK_STR (sizeof(link_status_str) / sizeof(const char *)) +#define MAX_LINK_STR RTE_DIM(link_status_str)const char *fec_mode[] = { "No active FEC",
@@ -90,13 +90,13 @@ const char *fec_mode[] = { "FEC RS(272,257)" }; -#define MAX_FEC_MODE (sizeof(fec_mode) / sizeof(const char *)) +#define MAX_FEC_MODE RTE_DIM(fec_mode)const char *signal_mode[] = {"NRZ" ,"PAM4" ,"PAM4_112" }; -#define MAX_SIG_MODE (sizeof(signal_mode) / sizeof(const char *)) +#define MAX_SIG_MODE RTE_DIM(signal_mode)/* multi-purpose multi-key table container. * Add a unique entry for a new PHY attribs as per HW CAS.
@@ -226,7 +226,7 @@ struct link_speeds2_tbl { }, }; -#define BNXT_SPEEDS2_TBL_SZ (sizeof(link_speeds2_tbl) / sizeof(*link_speeds2_tbl)) +#define BNXT_SPEEDS2_TBL_SZ RTE_DIM(link_speeds2_tbl)/* In hwrm_phy_qcfg reports trained up speeds in link_speed(offset:0x8[31:16]) */ struct link_speeds_tbl {
@@ -269,7 +269,7 @@ struct link_speeds_tbl { }, }; -#define BNXT_SPEEDS_TBL_SZ (sizeof(link_speeds_tbl) / sizeof(*link_speeds_tbl)) +#define BNXT_SPEEDS_TBL_SZ RTE_DIM(link_speeds_tbl)static const char *bnxt_get_xcvr_type (uint32_t xcvr_identifier_type_tx_lpi_timer) {
drivers/net/e1000/em_rxtx.c +1 -2
@@ -1666,8 +1666,7 @@ em_rctl_bsize(__rte_unused enum e1000_mac_type hwtyp, uint32_t *bufsz)* *************************************************************** */ -for (i =0 ; i !=sizeof (bufsz_to_rctl) /sizeof (bufsz_to_rctl[0 ]); - i++) { +for (i =0 ; i !=RTE_DIM (bufsz_to_rctl); i++) {if (rctl_bsize >= bufsz_to_rctl[i].bufsz) { *bufsz = bufsz_to_rctl[i].bufsz;return bufsz_to_rctl[i].rctl;
drivers/net/iavf/iavf_ipsec_crypto.c +1 -2
@@ -1483,8 +1483,7 @@ iavf_ipsec_crypto_capabilities_get(void *device)* crypto capabilities, except for last element of the array which is * the null termination */ -for (i =0 ; i < ((sizeof (iavf_security_capabilities) / -sizeof (iavf_security_capabilities[0 ])) -1 ); i++) { +for (i =0 ; i < (RTE_DIM (iavf_security_capabilities) -1 ); i++) { iavf_security_capabilities[i].crypto_capabilities = iavf_sctx->crypto_capabilities; }
drivers/net/igc/igc_ethdev.c +1 -2
@@ -188,8 +188,7 @@ static const struct rte_igc_xstats_name_off rte_igc_stats_strings[] = { offsetof(struct igc_hw_stats, icrxdmtc)}, }; -#define IGC_NB_XSTATS (sizeof(rte_igc_stats_strings) / \ - sizeof(rte_igc_stats_strings[0])) +#define IGC_NB_XSTATS RTE_DIM(rte_igc_stats_strings)static int eth_igc_configure (struct rte_eth_dev *dev);static int eth_igc_link_update (struct rte_eth_dev *dev,int wait_to_complete);
drivers/net/ipn3ke/ipn3ke_tm.c +1 -2
@@ -67,8 +67,7 @@ struct ipn3ke_tm_shaper_params_range_type ipn3ke_tm_shaper_params_rang[] = { {512, 1023, 15, 32768, 67108864, 134086656}, }; -#define IPN3KE_TM_SHAPER_RANGE_NUM (sizeof(ipn3ke_tm_shaper_params_rang) / \ - sizeof(struct ipn3ke_tm_shaper_params_range_type)) +#define IPN3KE_TM_SHAPER_RANGE_NUM RTE_DIM(ipn3ke_tm_shaper_params_rang)#define IPN3KE_TM_SHAPER_COMMITTED_RATE_MAX \ (ipn3ke_tm_shaper_params_rang[IPN3KE_TM_SHAPER_RANGE_NUM - 1].high)
drivers/net/ngbe/ngbe_ethdev.c +2 -4
@@ -238,8 +238,7 @@ static const struct rte_ngbe_xstats_name_off rte_ngbe_stats_strings[] = { HW_XSTAT_NAME(rx_xoff_packets, "rx_flow_control_xoff_packets"), }; -#define NGBE_NB_HW_STATS (sizeof(rte_ngbe_stats_strings) / \ - sizeof(rte_ngbe_stats_strings[0])) +#define NGBE_NB_HW_STATS RTE_DIM(rte_ngbe_stats_strings)/* Per-queue statistics */ #define QP_XSTAT (m) {#m, offsetof(struct ngbe_hw_stats, qp[0].m)}
@@ -251,8 +250,7 @@ static const struct rte_ngbe_xstats_name_off rte_ngbe_qp_strings[] = { QP_XSTAT(rx_qp_mc_packets), }; -#define NGBE_NB_QP_STATS (sizeof(rte_ngbe_qp_strings) / \ - sizeof(rte_ngbe_qp_strings[0])) +#define NGBE_NB_QP_STATS RTE_DIM(rte_ngbe_qp_strings)static inline int32_t ngbe_pf_reset_hw (struct ngbe_hw *hw)
drivers/net/octeontx/octeontx_stats.h +1 -2
@@ -36,6 +36,5 @@ struct octeontx_xstats octeontx_bgx_xstats[] = { BGX_XSTAT(tx_pause_packets), }; -#define NUM_BGX_XSTAT \ - (sizeof(octeontx_bgx_xstats) / sizeof(struct octeontx_xstats)) +#define NUM_BGX_XSTAT RTE_DIM(octeontx_bgx_xstats)#endif /* __OCTEONTX_STATS_H__ */
drivers/net/txgbe/txgbe_ethdev.c +3 -6
@@ -269,8 +269,7 @@ static const struct rte_txgbe_xstats_name_off rte_txgbe_stats_strings[] = { HW_XSTAT_NAME(rx_xoff_packets, "rx_flow_control_xoff_packets"), }; -#define TXGBE_NB_HW_STATS (sizeof(rte_txgbe_stats_strings) / \ - sizeof(rte_txgbe_stats_strings[0])) +#define TXGBE_NB_HW_STATS RTE_DIM(rte_txgbe_stats_strings)/* Per-priority statistics */ #define UP_XSTAT (m) {#m, offsetof(struct txgbe_hw_stats, up[0].m)}
@@ -290,8 +289,7 @@ static const struct rte_txgbe_xstats_name_off rte_txgbe_up_strings[] = { UP_XSTAT(tx_up_xon2off_packets), }; -#define TXGBE_NB_UP_STATS (sizeof(rte_txgbe_up_strings) / \ - sizeof(rte_txgbe_up_strings[0])) +#define TXGBE_NB_UP_STATS RTE_DIM(rte_txgbe_up_strings)/* Per-queue statistics */ #define QP_XSTAT (m) {#m, offsetof(struct txgbe_hw_stats, qp[0].m)}
@@ -303,8 +301,7 @@ static const struct rte_txgbe_xstats_name_off rte_txgbe_qp_strings[] = { QP_XSTAT(rx_qp_mc_packets), }; -#define TXGBE_NB_QP_STATS (sizeof(rte_txgbe_qp_strings) / \ - sizeof(rte_txgbe_qp_strings[0])) +#define TXGBE_NB_QP_STATS RTE_DIM(rte_txgbe_qp_strings)static inline int txgbe_is_sfp (struct txgbe_hw *hw)
drivers/net/txgbe/txgbe_ethdev_vf.c +1 -2
@@ -112,8 +112,7 @@ static const struct rte_txgbe_xstats_name_off rte_txgbevf_stats_strings[] = { offsetof(struct txgbevf_hw_stats, qp[7].vfmprc)} }; -#define TXGBEVF_NB_XSTATS (sizeof(rte_txgbevf_stats_strings) / \ - sizeof(rte_txgbevf_stats_strings[0])) +#define TXGBEVF_NB_XSTATS RTE_DIM(rte_txgbevf_stats_strings)/* * Negotiate mailbox API version with the PF.
Signature · 1 line
2.43.0
Re: [PATCH 5/5] drivers/net: use RTE_DIM
Quoted 6 lines · Stephen Hemminger
Use
RTE_DIMinstead of computing directly with sizeof. Patch automatically generated viacocci/rte_dim.cocci. Code in base/ subdirectory manually excluded.Signed-off-by Stephen Hemminger <[email protected]>
Updated ones looks good to me, but I can see a few more, I don't know if you excluded base file one intentionally,
searched as git grep "sizeof.*\[0\]" drivers/net/
-
drivers/net/bnxt/tf_core/cfa_tcam_mgr.h:28:#define ARRAY_SIZE(_array) (sizeof(_array) / sizeof(_array[0]))
-drivers/net/hinic/base/hinic_pmd_nicio.c:449: /sizeof(hinic_hw_rx_buf_size[0]);
-
drivers/net/ice/ice_ethdev.c:330:#define ICE_NB_MBUF_XSTATS (sizeof(ice_mbuf_strings) / sizeof(ice_mbuf_strings[0]))
-
drivers/net/i40e/i40e_ethdev.c:554:#define I40E_NB_MBUF_XSTATS (sizeof(i40e_mbuf_strings) / sizeof(i40e_mbuf_strings[0]))
-drivers/net/i40e/base/i40e_adminq.h:126: if (!((u32)aq_rc <
(sizeof(aq_to_posix) / sizeof((aq_to_posix)[0]))))
-drivers/net/ipn3ke/ipn3ke_representor.c:506: /sizeof(ipn3ke_rpst_hw_port_strings[0]))drivers/net/ipn3ke/ipn3ke_representor.c:517: /sizeof(ipn3ke_rpst_rxq_prio_strings[0]))drivers/net/ipn3ke/ipn3ke_representor.c:530: /sizeof(ipn3ke_rpst_txq_prio_strings[0]))
-drivers/net/ixgbe/base/ixgbe_x550.c:443: for (i = 0; i <sizeof(ixgbe_fw_map) / sizeof(ixgbe_fw_map[0]); ++i) {drivers/net/ixgbe/base/ixgbe_x550.c:700: for (i = 0; i <sizeof(ixgbe_fw_map) / sizeof(ixgbe_fw_map[0]); ++i) {
-drivers/net/ixgbe/base/ixgbe_x550.c:3338: bufsz = sizeof(buf) /sizeof(buf[0]);
-
drivers/net/mlx5/hws/mlx5dr_internal.h:52:#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
Re: [PATCH 5/5] drivers/net: use RTE_DIM
Quoted 13 lines · On Tue, 16 Apr 2024 17:29:25 +0100 Ferruh Yigit <[email protected]> wrote:
Quoted 6 lines · Stephen Hemminger
Use
RTE_DIMinstead of computing directly with sizeof. Patch automatically generated viacocci/rte_dim.cocci. Code in base/ subdirectory manually excluded.Signed-off-by Stephen Hemminger <[email protected]>Updated ones looks good to me, but I can see a few more, I don't know if you excluded base file one intentionally,
searched asgit grep "sizeof.*\[0\]" drivers/net/
Quoted 5 lines
-
drivers/net/bnxt/tf_core/cfa_tcam_mgr.h:28:#defineARRAY_SIZE(_array)(sizeof(_array)/sizeof(_array[0]))-
For the drivers that choose to define and use ARRAY_SIZE() that is fine.
Since base/ directories often come from other upstream repos, I ignored those.
Something about the Intel drivers was causing warnings and the script would not automatically change those.
Re: [PATCH 5/5] drivers/net: use RTE_DIM
Quoted 16 lines · Stephen Hemminger
Quoted 13 lines · On Tue, 16 Apr 2024 17:29:25 +0100 Ferruh Yigit <[email protected]> wrote:
Quoted 6 lines · Stephen Hemminger
Use
RTE_DIMinstead of computing directly with sizeof. Patch automatically generated viacocci/rte_dim.cocci. Code in base/ subdirectory manually excluded.Signed-off-by Stephen Hemminger <[email protected]>Updated ones looks good to me, but I can see a few more, I don't know if you excluded base file one intentionally,
searched asgit grep "sizeof.*\[0\]" drivers/net/
Quoted 8 lines
Quoted 5 lines
-
drivers/net/bnxt/tf_core/cfa_tcam_mgr.h:28:#defineARRAY_SIZE(_array)(sizeof(_array)/sizeof(_array[0]))-
For the drivers that choose to define and use
ARRAY_SIZE()that is fine.
ack
Although it can be updated as following, I don't think it differs #define ARRAY_SIZE(_array) RTE_DIM(_array)
Quoted 2 lines
Since base/ directories often come from other upstream repos, I ignored those.
ack
Quoted 3 lines
Something about the Intel drivers was causing warnings and the script would not automatically change those.
ICE_NB_MBUF_XSTATS, I40E_NB_MBUF_XSTATS & ipn3ke_representor.c ones seems can be updated, manually maybe?