netlink: cleanup netlink_writer code

* Remove unused nlattr_add_nla() - that's a duplicate of nlattr_add_raw().
* Calculate alignment only once in nlmsg_reserve_data_raw()

MFC after:	2 weeks
This commit is contained in:
Alexander V. Chernikov 2023-03-07 17:42:27 +00:00
parent 1867702942
commit 5c8277ec25

View File

@ -124,17 +124,19 @@ nlattr_set_len(const struct nl_writer *nw, int off)
static inline void *
nlmsg_reserve_data_raw(struct nl_writer *nw, size_t sz)
{
if (__predict_false(nw->offset + NETLINK_ALIGN(sz) > nw->alloc_len)) {
if (!nlmsg_refill_buffer(nw, NETLINK_ALIGN(sz)))
sz = NETLINK_ALIGN(sz);
if (__predict_false(nw->offset + sz > nw->alloc_len)) {
if (!nlmsg_refill_buffer(nw, sz))
return (NULL);
}
void *data_ptr = &nw->data[nw->offset];
nw->offset += NLMSG_ALIGN(sz);
nw->offset += sz;
return (data_ptr);
}
#define nlmsg_reserve_object(_ns, _t) ((_t *)nlmsg_reserve_data_raw(_ns, NLA_ALIGN(sizeof(_t))))
#define nlmsg_reserve_object(_ns, _t) ((_t *)nlmsg_reserve_data_raw(_ns, sizeof(_t)))
#define nlmsg_reserve_data(_ns, _sz, _t) ((_t *)nlmsg_reserve_data_raw(_ns, _sz))
static inline int
@ -163,27 +165,6 @@ _nlmsg_reserve_attr(struct nl_writer *nw, uint16_t nla_type, uint16_t sz)
}
#define nlmsg_reserve_attr(_ns, _at, _t) ((_t *)_nlmsg_reserve_attr(_ns, _at, NLA_ALIGN(sizeof(_t))))
static inline bool
nlattr_add_nla(struct nl_writer *nw, const struct nlattr *nla_src)
{
MPASS(nla_src->nla_len >= sizeof(struct nlattr));
int required_len = NLA_ALIGN(nla_src->nla_len);
if (__predict_false(nw->offset + required_len > nw->alloc_len)) {
if (!nlmsg_refill_buffer(nw, required_len))
return (false);
}
struct nlattr *nla = (struct nlattr *)(&nw->data[nw->offset]);
if ((nla_src->nla_len % 4) != 0) {
/* clear padding bytes */
bzero((char *)nla + nla_src->nla_len - 4, 4);
}
memcpy(nla, nla_src, nla_src->nla_len);
nw->offset += required_len;
return (true);
}
static inline bool
nlattr_add(struct nl_writer *nw, int attr_type, int attr_len, const void *data)
{