Skip to content

Commit

Permalink
netlink: use nitems() and roundup(2) from param.h
Browse files Browse the repository at this point in the history
While here style nested includes (kernel ones go first).

Reviewed by:		melifaro
Differential Revision:	https://reviews.freebsd.org/D47557
  • Loading branch information
glebius committed Dec 3, 2024
1 parent fe04834 commit 29f6150
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 36 deletions.
6 changes: 3 additions & 3 deletions sbin/route/route_netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion sys/netinet/ip_carp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
5 changes: 1 addition & 4 deletions sys/netlink/netlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 0 additions & 2 deletions sys/netlink/netlink_ctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
2 changes: 1 addition & 1 deletion sys/netlink/netlink_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions sys/netlink/netlink_message_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
}
Expand All @@ -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) \
}

Expand Down Expand Up @@ -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,
Expand Down
23 changes: 11 additions & 12 deletions sys/netlink/netlink_snl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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)
Expand All @@ -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 */
Expand Down Expand Up @@ -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, \
}

Expand All @@ -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, \
}

Expand All @@ -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, \
}

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion sys/netlink/route/iface.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sys/netlink/route/neigh.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion sys/netlink/route/nexthop.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
2 changes: 1 addition & 1 deletion sys/netlink/route/rt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
2 changes: 1 addition & 1 deletion sys/netpfil/pf/pf_nl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion sys/netpfil/pf/pflow.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion sys/tests/ktest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 29f6150

Please sign in to comment.