git: 29f6150256c2 - main - netlink: use nitems() and roundup(2) from param.h

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Tue, 03 Dec 2024 20:05:02 UTC
The branch main has been updated by glebius:

URL: https://cgit.FreeBSD.org/src/commit/?id=29f6150256c204a53c6f72a03d3df35ec52ccbf6

commit 29f6150256c204a53c6f72a03d3df35ec52ccbf6
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2024-12-03 20:04:39 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2024-12-03 20:04:39 +0000

    netlink: use nitems() and roundup(2) from param.h
    
    While here style nested includes (kernel ones go first).
    
    Reviewed by:            melifaro
    Differential Revision:  https://reviews.freebsd.org/D47557
---
 sbin/route/route_netlink.c           |  6 +++---
 sys/netinet/ip_carp.c                |  2 +-
 sys/netlink/netlink.h                |  5 +----
 sys/netlink/netlink_ctl.h            |  2 --
 sys/netlink/netlink_generic.c        |  2 +-
 sys/netlink/netlink_message_parser.h | 12 ++++++------
 sys/netlink/netlink_snl.h            | 23 +++++++++++------------
 sys/netlink/route/iface.c            |  2 +-
 sys/netlink/route/neigh.c            |  2 +-
 sys/netlink/route/nexthop.c          |  2 +-
 sys/netlink/route/rt.c               |  2 +-
 sys/netpfil/pf/pf_nl.c               |  2 +-
 sys/netpfil/pf/pflow.c               |  3 ++-
 sys/tests/ktest.c                    |  3 ++-
 14 files changed, 32 insertions(+), 36 deletions(-)

diff --git a/sbin/route/route_netlink.c b/sbin/route/route_netlink.c
index 040b97568f20..631c2860b547 100644
--- a/sbin/route/route_netlink.c
+++ b/sbin/route/route_netlink.c
@@ -589,7 +589,7 @@ print_nlmsg_link(struct nl_helper *h, struct nlmsghdr *hdr,
 
 	printf("iface#%u %s ", l.ifi_index, l.ifla_ifname);
 	printf("admin %s ", (l.ifi_flags & IFF_UP) ? "UP" : "DOWN");
-	if (l.ifla_operstate < NL_ARRAY_LEN(operstate))
+	if (l.ifla_operstate < nitems(operstate))
 		printf("oper %s ", operstate[l.ifla_operstate]);
 	if (l.ifla_mtu > 0)
 		printf("mtu %u ", l.ifla_mtu);
@@ -668,7 +668,7 @@ print_nlmsg_neigh(struct nl_helper *h, struct nlmsghdr *hdr,
 	struct snl_parsed_link_simple link = {};
 	get_ifdata(h, attrs.nda_ifindex, &link);
 
-	for (unsigned int i = 0; i < NL_ARRAY_LEN(nudstate); i++) {
+	for (unsigned int i = 0; i < nitems(nudstate); i++) {
 		if ((1 << i) & attrs.ndm_state) {
 			printf("state %s ", nudstate[i]);
 			break;
@@ -768,7 +768,7 @@ monitor_nl(int fib)
 	socklen_t optlen = sizeof(optval);
 	setsockopt(ss_event.fd, SOL_NETLINK, NETLINK_MSG_INFO, &optval, optlen);
 
-	for (unsigned int i = 0; i < NL_ARRAY_LEN(groups); i++) {
+	for (unsigned int i = 0; i < nitems(groups); i++) {
 		int error;
 		int optval = groups[i];
 		socklen_t optlen = sizeof(optval);
diff --git a/sys/netinet/ip_carp.c b/sys/netinet/ip_carp.c
index ab001d346313..133dba45cf36 100644
--- a/sys/netinet/ip_carp.c
+++ b/sys/netinet/ip_carp.c
@@ -2986,7 +2986,7 @@ carp_nl_register(void)
 	MPASS(family_id != 0);
 
 	ret = genl_register_cmds(CARP_NL_FAMILY_NAME, carp_cmds,
-	    NL_ARRAY_LEN(carp_cmds));
+	    nitems(carp_cmds));
 	MPASS(ret);
 }
 
diff --git a/sys/netlink/netlink.h b/sys/netlink/netlink.h
index 2cffa7299ef4..5724385271fc 100644
--- a/sys/netlink/netlink.h
+++ b/sys/netlink/netlink.h
@@ -61,7 +61,7 @@
 #ifndef _NETLINK_NETLINK_H_
 #define _NETLINK_NETLINK_H_
 
-#include <sys/types.h>
+#include <sys/param.h>
 #include <sys/socket.h>
 
 struct sockaddr_nl {
@@ -194,9 +194,6 @@ enum nlmsginfo_attrs {
 };
 
 
-#ifndef roundup2
-#define	roundup2(x, y)	(((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
-#endif
 #define	NL_ITEM_ALIGN_SIZE		sizeof(uint32_t)
 #define	NL_ITEM_ALIGN(_len)		roundup2(_len, NL_ITEM_ALIGN_SIZE)
 #define	NL_ITEM_DATA(_ptr, _off)	((void *)((char *)(_ptr) + _off))
diff --git a/sys/netlink/netlink_ctl.h b/sys/netlink/netlink_ctl.h
index c2ac3e99421e..95b79c763ccd 100644
--- a/sys/netlink/netlink_ctl.h
+++ b/sys/netlink/netlink_ctl.h
@@ -65,8 +65,6 @@ MALLOC_DECLARE(M_NETLINK);
 		((char *)NLA_NEXT(_attr) <= (char *)_end);	\
 		_attr = (_len -= NLA_ALIGN(_attr->nla_len), NLA_NEXT(_attr)))
 
-#define	NL_ARRAY_LEN(_a)	(sizeof(_a) / sizeof((_a)[0]))
-
 #include <netlink/netlink_message_writer.h>
 #include <netlink/netlink_message_parser.h>
 
diff --git a/sys/netlink/netlink_generic.c b/sys/netlink/netlink_generic.c
index 4a3c83da57e1..0f960d79f477 100644
--- a/sys/netlink/netlink_generic.c
+++ b/sys/netlink/netlink_generic.c
@@ -284,7 +284,7 @@ genl_load_all(void *u __unused)
 {
 	NL_VERIFY_PARSERS(all_parsers);
 	ctrl_family_id = genl_register_family(CTRL_FAMILY_NAME, 0, 2, CTRL_ATTR_MAX);
-	genl_register_cmds(CTRL_FAMILY_NAME, nlctrl_cmds, NL_ARRAY_LEN(nlctrl_cmds));
+	genl_register_cmds(CTRL_FAMILY_NAME, nlctrl_cmds, nitems(nlctrl_cmds));
 	ctrl_group_id = genl_register_group(CTRL_FAMILY_NAME, "notify");
 	family_event_tag = EVENTHANDLER_REGISTER(genl_family_event, nlctrl_notify, NULL,
 	    EVENTHANDLER_PRI_ANY);
diff --git a/sys/netlink/netlink_message_parser.h b/sys/netlink/netlink_message_parser.h
index 49bec646b29e..42a59f45b22a 100644
--- a/sys/netlink/netlink_message_parser.h
+++ b/sys/netlink/netlink_message_parser.h
@@ -128,8 +128,8 @@ static const struct nlhdr_parser _name = {			\
 	.nl_hdr_off = sizeof(_t),				\
 	.fp = &((_fp)[0]),					\
 	.np = &((_np)[0]),					\
-	.fp_size = NL_ARRAY_LEN(_fp),				\
-	.np_size = NL_ARRAY_LEN(_np),				\
+	.fp_size = nitems(_fp),					\
+	.np_size = nitems(_np),					\
 	.sp = _sp,						\
 	.post_parse = _pp,					\
 }
@@ -146,14 +146,14 @@ static const struct nlhdr_parser _name = {		\
 	.out_hdr_off = sizeof(_o),			\
 	.fp = &((_fp)[0]),				\
 	.np = &((_np)[0]),				\
-	.fp_size = NL_ARRAY_LEN(_fp),			\
-	.np_size = NL_ARRAY_LEN(_np),			\
+	.fp_size = nitems(_fp),				\
+	.np_size = nitems(_np),				\
 }
 
 #define	NL_DECLARE_ATTR_PARSER_EXT(_name, _np, _pp)	\
 static const struct nlhdr_parser _name = {		\
 	.np = &((_np)[0]),				\
-	.np_size = NL_ARRAY_LEN(_np),			\
+	.np_size = nitems(_np),				\
 	.post_parse = (_pp)				\
 }
 
@@ -308,7 +308,7 @@ nl_verify_parsers(const struct nlhdr_parser **parser, int count)
 #endif
 }
 void nl_verify_parsers(const struct nlhdr_parser **parser, int count);
-#define	NL_VERIFY_PARSERS(_p)	nl_verify_parsers((_p), NL_ARRAY_LEN(_p))
+#define	NL_VERIFY_PARSERS(_p)	nl_verify_parsers((_p), nitems(_p))
 
 static inline int
 nl_parse_nlmsg(struct nlmsghdr *hdr, const struct nlhdr_parser *parser,
diff --git a/sys/netlink/netlink_snl.h b/sys/netlink/netlink_snl.h
index 7151f6221fca..2861ea93bafb 100644
--- a/sys/netlink/netlink_snl.h
+++ b/sys/netlink/netlink_snl.h
@@ -31,6 +31,12 @@
  * Simple Netlink Library
  */
 
+#include <sys/param.h>
+#include <sys/socket.h>
+
+#include <netlink/netlink.h>
+#include <netlink/netlink_bitset.h>
+
 #include <assert.h>
 #include <errno.h>
 #include <stdalign.h>
@@ -41,11 +47,6 @@
 #include <string.h>
 #include <unistd.h>
 
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netlink/netlink.h>
-#include <netlink/netlink_bitset.h>
-
 #define _roundup2(x, y)         (((x)+((y)-1))&(~((y)-1)))
 
 #define NETLINK_ALIGN_SIZE      sizeof(uint32_t)
@@ -68,8 +69,6 @@
 		((char *)NLA_NEXT(_attr) <= _NLA_END(_start, _len));	\
 		_attr =  NLA_NEXT(_attr))
 
-#define	NL_ARRAY_LEN(_a)	(sizeof(_a) / sizeof((_a)[0]))
-
 struct linear_buffer {
 	char			*base;	/* Base allocated memory pointer */
 	uint32_t		offset;	/* Currently used offset */
@@ -166,8 +165,8 @@ static const struct snl_hdr_parser _name = {				\
 	.out_size = _sz_out,						\
 	.fp = &((_fp)[0]),						\
 	.np = &((_np)[0]),						\
-	.fp_size = NL_ARRAY_LEN(_fp),					\
-	.np_size = NL_ARRAY_LEN(_np),					\
+	.fp_size = nitems(_fp),						\
+	.np_size = nitems(_np),						\
 	.cb_post = _cb,							\
 }
 
@@ -179,7 +178,7 @@ static const struct snl_hdr_parser _name = {				\
 	.in_hdr_size = _sz_h_in,					\
 	.out_size = _sz_out,						\
 	.fp = &((_fp)[0]),						\
-	.fp_size = NL_ARRAY_LEN(_fp),					\
+	.fp_size = nitems(_fp),						\
 	.cb_post = _cb,							\
 }
 
@@ -190,7 +189,7 @@ static const struct snl_hdr_parser _name = {				\
 static const struct snl_hdr_parser _name = {				\
 	.out_size = _sz_out,						\
 	.np = &((_np)[0]),						\
-	.np_size = NL_ARRAY_LEN(_np),					\
+	.np_size = nitems(_np),						\
 	.cb_post = _cb,							\
 }
 
@@ -418,7 +417,7 @@ snl_verify_parsers(const struct snl_hdr_parser **parser, int count)
 		}
 	}
 }
-#define	SNL_VERIFY_PARSERS(_p)	snl_verify_parsers((_p), NL_ARRAY_LEN(_p))
+#define	SNL_VERIFY_PARSERS(_p)	snl_verify_parsers((_p), nitems(_p))
 
 static const struct snl_attr_parser *
 find_parser(const struct snl_attr_parser *ps, int pslen, int key)
diff --git a/sys/netlink/route/iface.c b/sys/netlink/route/iface.c
index c19161f5bbb7..86b1f8f1b1bc 100644
--- a/sys/netlink/route/iface.c
+++ b/sys/netlink/route/iface.c
@@ -1522,7 +1522,7 @@ rtnl_ifaces_init(void)
 	    ifnet_link_event, rtnl_handle_iflink, NULL,
 	    EVENTHANDLER_PRI_ANY);
 	NL_VERIFY_PARSERS(all_parsers);
-	rtnl_register_messages(cmd_handlers, NL_ARRAY_LEN(cmd_handlers));
+	rtnl_register_messages(cmd_handlers, nitems(cmd_handlers));
 }
 
 void
diff --git a/sys/netlink/route/neigh.c b/sys/netlink/route/neigh.c
index c5003eddaba1..ec58c6140db8 100644
--- a/sys/netlink/route/neigh.c
+++ b/sys/netlink/route/neigh.c
@@ -589,7 +589,7 @@ void
 rtnl_neighs_init(void)
 {
 	NL_VERIFY_PARSERS(all_parsers);
-	rtnl_register_messages(cmd_handlers, NL_ARRAY_LEN(cmd_handlers));
+	rtnl_register_messages(cmd_handlers, nitems(cmd_handlers));
 	lle_event_p = EVENTHANDLER_REGISTER(lle_event, rtnl_lle_event, NULL,
 	    EVENTHANDLER_PRI_ANY);
 }
diff --git a/sys/netlink/route/nexthop.c b/sys/netlink/route/nexthop.c
index 90ed3497047e..03f1a57fd1e4 100644
--- a/sys/netlink/route/nexthop.c
+++ b/sys/netlink/route/nexthop.c
@@ -1119,5 +1119,5 @@ void
 rtnl_nexthops_init(void)
 {
 	NL_VERIFY_PARSERS(all_parsers);
-	rtnl_register_messages(cmd_handlers, NL_ARRAY_LEN(cmd_handlers));
+	rtnl_register_messages(cmd_handlers, nitems(cmd_handlers));
 }
diff --git a/sys/netlink/route/rt.c b/sys/netlink/route/rt.c
index c9b0f56498ec..14bd73d33411 100644
--- a/sys/netlink/route/rt.c
+++ b/sys/netlink/route/rt.c
@@ -1119,5 +1119,5 @@ void
 rtnl_routes_init(void)
 {
 	NL_VERIFY_PARSERS(all_parsers);
-	rtnl_register_messages(cmd_handlers, NL_ARRAY_LEN(cmd_handlers));
+	rtnl_register_messages(cmd_handlers, nitems(cmd_handlers));
 }
diff --git a/sys/netpfil/pf/pf_nl.c b/sys/netpfil/pf/pf_nl.c
index 68b3659b0ed2..ad7dc97cbc1a 100644
--- a/sys/netpfil/pf/pf_nl.c
+++ b/sys/netpfil/pf/pf_nl.c
@@ -2004,7 +2004,7 @@ pf_nl_register(void)
 	NL_VERIFY_PARSERS(all_parsers);
 
 	family_id = genl_register_family(PFNL_FAMILY_NAME, 0, 2, PFNL_CMD_MAX);
-	genl_register_cmds(PFNL_FAMILY_NAME, pf_cmds, NL_ARRAY_LEN(pf_cmds));
+	genl_register_cmds(PFNL_FAMILY_NAME, pf_cmds, nitems(pf_cmds));
 }
 
 void
diff --git a/sys/netpfil/pf/pflow.c b/sys/netpfil/pf/pflow.c
index 36b528290306..397d720215b2 100644
--- a/sys/netpfil/pf/pflow.c
+++ b/sys/netpfil/pf/pflow.c
@@ -1807,7 +1807,8 @@ pflow_init(void)
 
 	family_id = genl_register_family(PFLOWNL_FAMILY_NAME, 0, 2, PFLOWNL_CMD_MAX);
 	MPASS(family_id != 0);
-	ret = genl_register_cmds(PFLOWNL_FAMILY_NAME, pflow_cmds, NL_ARRAY_LEN(pflow_cmds));
+	ret = genl_register_cmds(PFLOWNL_FAMILY_NAME, pflow_cmds,
+	    nitems(pflow_cmds));
 
 	return (ret ? 0 : ENODEV);
 }
diff --git a/sys/tests/ktest.c b/sys/tests/ktest.c
index cd83a6aaaa3f..694e1f4229b5 100644
--- a/sys/tests/ktest.c
+++ b/sys/tests/ktest.c
@@ -370,7 +370,8 @@ ktest_nl_register(void)
 	family_id = genl_register_family(KTEST_FAMILY_NAME, 0, 1, KTEST_CMD_MAX);
 	MPASS(family_id != 0);
 
-	ret = genl_register_cmds(KTEST_FAMILY_NAME, ktest_cmds, NL_ARRAY_LEN(ktest_cmds));
+	ret = genl_register_cmds(KTEST_FAMILY_NAME, ktest_cmds,
+	    nitems(ktest_cmds));
 	MPASS(ret);
 }