platform: merge branch 'bg/sriov-bufsize'

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1413

(cherry picked from commit 227ae41216)
This commit is contained in:
Beniamino Galvani 2022-10-17 10:31:04 +02:00
commit 439f0675f2
6 changed files with 63 additions and 50 deletions

View file

@ -2670,7 +2670,7 @@ _wireguard_read_info(NMPlatform *platform /* used only as logging context */
ifindex,
wireguard_family_id);
msg = nlmsg_alloc();
msg = nlmsg_alloc(0);
if (!genlmsg_put(msg,
NL_AUTO_PORT,
@ -2879,7 +2879,7 @@ _wireguard_create_change_nlmsgs(NMPlatform *platfo
again:
msg = nlmsg_alloc();
msg = nlmsg_alloc(0);
if (!genlmsg_put(msg,
NL_AUTO_PORT,
NL_AUTO_SEQ,
@ -4965,7 +4965,8 @@ _nl_msg_new_link_full(uint16_t nlmsg_type,
const char *ifname,
guint8 family,
unsigned flags_mask,
unsigned flags_set)
unsigned flags_set,
size_t len)
{
nm_auto_nlmsg struct nl_msg *msg = NULL;
const struct ifinfomsg ifi = {
@ -4977,7 +4978,7 @@ _nl_msg_new_link_full(uint16_t nlmsg_type,
nm_assert(NM_IN_SET(nlmsg_type, RTM_DELLINK, RTM_NEWLINK, RTM_GETLINK, RTM_SETLINK));
msg = nlmsg_alloc_simple(nlmsg_type, nlmsg_flags);
msg = nlmsg_alloc_new(len ? nlmsg_total_size(NLMSG_HDRLEN + len) : 0, nlmsg_type, nlmsg_flags);
if (nlmsg_append_struct(msg, &ifi) < 0)
goto nla_put_failure;
@ -4994,7 +4995,7 @@ nla_put_failure:
static struct nl_msg *
_nl_msg_new_link(uint16_t nlmsg_type, uint16_t nlmsg_flags, int ifindex, const char *ifname)
{
return _nl_msg_new_link_full(nlmsg_type, nlmsg_flags, ifindex, ifname, AF_UNSPEC, 0, 0);
return _nl_msg_new_link_full(nlmsg_type, nlmsg_flags, ifindex, ifname, AF_UNSPEC, 0, 0, 0);
}
/* Copied and modified from libnl3's build_addr_msg(). */
@ -5026,7 +5027,7 @@ _nl_msg_new_address(uint16_t nlmsg_type,
nm_assert(NM_IN_SET(family, AF_INET, AF_INET6));
nm_assert(NM_IN_SET(nlmsg_type, RTM_NEWADDR, RTM_DELADDR));
msg = nlmsg_alloc_simple(nlmsg_type, nlmsg_flags);
msg = nlmsg_alloc_new(0, nlmsg_type, nlmsg_flags);
addr_len = family == AF_INET ? sizeof(in_addr_t) : sizeof(struct in6_addr);
@ -5133,7 +5134,7 @@ _nl_msg_new_route(uint16_t nlmsg_type, uint16_t nlmsg_flags, const NMPObject *ob
NM_IN_SET(NMP_OBJECT_GET_TYPE(obj), NMP_OBJECT_TYPE_IP4_ROUTE, NMP_OBJECT_TYPE_IP6_ROUTE));
nm_assert(NM_IN_SET(nlmsg_type, RTM_NEWROUTE, RTM_DELROUTE));
msg = nlmsg_alloc_simple(nlmsg_type, nlmsg_flags);
msg = nlmsg_alloc_new(0, nlmsg_type, nlmsg_flags);
if (nlmsg_append_struct(msg, &rtmsg) < 0)
goto nla_put_failure;
@ -5225,7 +5226,7 @@ _nl_msg_new_routing_rule(uint16_t nlmsg_type,
const guint8 addr_size = nm_utils_addr_family_to_size(routing_rule->addr_family);
guint32 table;
msg = nlmsg_alloc_simple(nlmsg_type, nlmsg_flags);
msg = nlmsg_alloc_new(0, nlmsg_type, nlmsg_flags);
table = routing_rule->table;
@ -5344,7 +5345,7 @@ _nl_msg_new_qdisc(uint16_t nlmsg_type, uint16_t nlmsg_flags, const NMPlatformQdi
.tcm_info = qdisc->info,
};
msg = nlmsg_alloc_simple(nlmsg_type, nlmsg_flags | NMP_NLM_FLAG_F_ECHO);
msg = nlmsg_alloc_new(0, nlmsg_type, nlmsg_flags | NMP_NLM_FLAG_F_ECHO);
if (nlmsg_append_struct(msg, &tcm) < 0)
goto nla_put_failure;
@ -5432,7 +5433,7 @@ _nl_msg_new_tfilter(uint16_t nlmsg_type, uint16_t nlmsg_flags, const NMPlatformT
.tcm_info = tfilter->info,
};
msg = nlmsg_alloc_simple(nlmsg_type, nlmsg_flags | NMP_NLM_FLAG_F_ECHO);
msg = nlmsg_alloc_new(0, nlmsg_type, nlmsg_flags | NMP_NLM_FLAG_F_ECHO);
if (nlmsg_append_struct(msg, &tcm) < 0)
goto nla_put_failure;
@ -7227,7 +7228,7 @@ _nl_msg_new_dump_rtnl(NMPObjectType obj_type, int preferred_addr_family)
nm_assert(klass);
nm_assert(klass->rtm_gettype > 0);
nlmsg = nlmsg_alloc_simple(klass->rtm_gettype, NLM_F_DUMP);
nlmsg = nlmsg_alloc_new(0, klass->rtm_gettype, NLM_F_DUMP);
if (klass->addr_family != AF_UNSPEC) {
/* if the class specifies a particular address family, then it is preferred. */
@ -7272,7 +7273,7 @@ _nl_msg_new_dump_genl_families(void)
{
nm_auto_nlmsg struct nl_msg *nlmsg = NULL;
nlmsg = nlmsg_alloc_size(nlmsg_total_size(GENL_HDRLEN));
nlmsg = nlmsg_alloc(nlmsg_total_size(GENL_HDRLEN));
if (!genlmsg_put(nlmsg,
NL_AUTO_PORT,
@ -8088,7 +8089,8 @@ link_change_flags(NMPlatform *platform, int ifindex, unsigned flags_mask, unsign
nm_platform_link_flags2str(flags_set, s_flags, sizeof(s_flags)),
nm_platform_link_flags2str(flags_mask, s_flags2, sizeof(s_flags2)));
nlmsg = _nl_msg_new_link_full(RTM_NEWLINK, 0, ifindex, NULL, AF_UNSPEC, flags_mask, flags_set);
nlmsg =
_nl_msg_new_link_full(RTM_NEWLINK, 0, ifindex, NULL, AF_UNSPEC, flags_mask, flags_set, 0);
if (!nlmsg)
return -NME_UNSPEC;
return do_change_link(platform, CHANGE_LINK_TYPE_UNSPEC, ifindex, nlmsg, NULL);
@ -8403,16 +8405,23 @@ link_set_sriov_vfs(NMPlatform *platform, int ifindex, const NMPlatformVF *const
{
nm_auto_nlmsg struct nl_msg *nlmsg = NULL;
struct nlattr *list, *info, *vlan_list;
guint i;
guint i = 0;
guint num = 0;
size_t buflen = 0;
nlmsg = _nl_msg_new_link(RTM_NEWLINK, 0, ifindex, NULL);
while (vfs[num])
num++;
/* A single IFLA_VF_INFO shouldn't take more than 200 bytes. */
buflen = (num + 1) * 200;
nlmsg = _nl_msg_new_link_full(RTM_NEWLINK, 0, ifindex, NULL, AF_UNSPEC, 0, 0, buflen);
if (!nlmsg)
g_return_val_if_reached(-NME_BUG);
if (!(list = nla_nest_start(nlmsg, IFLA_VFINFO_LIST)))
goto nla_put_failure;
for (i = 0; vfs[i]; i++) {
for (; vfs[i]; i++) {
const NMPlatformVF *vf = vfs[i];
if (!(info = nla_nest_start(nlmsg, IFLA_VF_INFO)))
@ -8485,6 +8494,11 @@ link_set_sriov_vfs(NMPlatform *platform, int ifindex, const NMPlatformVF *const
return (do_change_link(platform, CHANGE_LINK_TYPE_UNSPEC, ifindex, nlmsg, NULL) >= 0);
nla_put_failure:
_LOGE("error building SR-IOV VFs netlink message: used %u/%zu bytes for %u/%u VFs",
nlmsg_hdr(nlmsg)->nlmsg_len,
buflen,
i,
num);
g_return_val_if_reached(FALSE);
}
@ -8499,8 +8513,14 @@ link_set_bridge_vlans(NMPlatform *platform,
struct bridge_vlan_info vinfo = {};
guint i;
nlmsg =
_nl_msg_new_link_full(vlans ? RTM_SETLINK : RTM_DELLINK, 0, ifindex, NULL, AF_BRIDGE, 0, 0);
nlmsg = _nl_msg_new_link_full(vlans ? RTM_SETLINK : RTM_DELLINK,
0,
ifindex,
NULL,
AF_BRIDGE,
0,
0,
0);
if (!nlmsg)
g_return_val_if_reached(-NME_BUG);
@ -9528,7 +9548,7 @@ tc_delete(NMPlatform *platform,
log_tag = "do-delete-tc";
}
msg = nlmsg_alloc_simple(nlmsg_type, NMP_NLM_FLAG_F_ECHO);
msg = nlmsg_alloc_new(0, nlmsg_type, NMP_NLM_FLAG_F_ECHO);
if (nlmsg_append_struct(msg, &tcm) < 0)
goto nla_put_failure;
@ -10252,7 +10272,7 @@ mptcp_addr_update(NMPlatform *platform, NMOptionBool add, const NMPlatformMptcpA
cmd_str,
nm_platform_mptcp_addr_to_string(addr, sbuf, sizeof(sbuf)));
nlmsg = nlmsg_alloc_size(nlmsg_total_size(GENL_HDRLEN) + 200);
nlmsg = nlmsg_alloc(nlmsg_total_size(GENL_HDRLEN) + 200);
if (!genlmsg_put(nlmsg,
NL_AUTO_PORT,
@ -10343,7 +10363,7 @@ mptcp_addrs_dump(NMPlatform *platform)
return NULL;
}
nlmsg = nlmsg_alloc_size(nlmsg_total_size(GENL_HDRLEN));
nlmsg = nlmsg_alloc(nlmsg_total_size(GENL_HDRLEN));
if (!genlmsg_put(nlmsg,
NL_AUTO_PORT,

View file

@ -364,11 +364,22 @@ nla_reserve(struct nl_msg *msg, int attrtype, int attrlen)
/*****************************************************************************/
/**
* Allocate a new netlink message.
*
* Allocates a new netlink message without any further payload. If @len is zero,
* the maximum payload size is set to the size of one memory page.
*
* @return Newly allocated netlink message or NULL.
*/
struct nl_msg *
nlmsg_alloc_size(size_t len)
nlmsg_alloc(size_t len)
{
struct nl_msg *nm;
if (len == 0)
len = nm_utils_getpagesize();
if (len < sizeof(struct nlmsghdr))
len = sizeof(struct nlmsghdr);
else if (len > UINT32_MAX)
@ -384,38 +395,23 @@ nlmsg_alloc_size(size_t len)
return nm;
}
/**
* Allocate a new netlink message with the default maximum payload size.
*
* Allocates a new netlink message without any further payload. The
* maximum payload size defaults to PAGESIZE or as otherwise specified
* with nlmsg_set_default_size().
*
* @return Newly allocated netlink message or NULL.
*/
struct nl_msg *
nlmsg_alloc(void)
{
return nlmsg_alloc_size(nm_utils_getpagesize());
}
struct nl_msg *
nlmsg_alloc_convert(struct nlmsghdr *hdr)
{
struct nl_msg *nm;
nm = nlmsg_alloc_size(NLMSG_ALIGN(hdr->nlmsg_len));
nm = nlmsg_alloc(NLMSG_ALIGN(hdr->nlmsg_len));
memcpy(nm->nm_nlh, hdr, hdr->nlmsg_len);
return nm;
}
struct nl_msg *
nlmsg_alloc_simple(uint16_t nlmsgtype, uint16_t flags)
nlmsg_alloc_new(size_t size, uint16_t nlmsgtype, uint16_t flags)
{
struct nl_msg *nm;
struct nlmsghdr *new;
nm = nlmsg_alloc();
nm = nlmsg_alloc(size);
new = nm->nm_nlh;
new->nlmsg_type = nlmsgtype;
new->nlmsg_flags = flags;
@ -928,7 +924,7 @@ genl_ctrl_resolve(struct nl_sock *sk, const char *name)
.valid_arg = &response_data,
};
msg = nlmsg_alloc();
msg = nlmsg_alloc(0);
if (!genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, GENL_ID_CTRL, 0, 0, CTRL_CMD_GETFAMILY, 1))
return -ENOMEM;

View file

@ -393,13 +393,11 @@ nla_parse_nested(struct nlattr *tb[],
/*****************************************************************************/
struct nl_msg *nlmsg_alloc(void);
struct nl_msg *nlmsg_alloc_size(size_t max);
struct nl_msg *nlmsg_alloc(size_t len);
struct nl_msg *nlmsg_alloc_convert(struct nlmsghdr *hdr);
struct nl_msg *nlmsg_alloc_simple(uint16_t nlmsgtype, uint16_t flags);
struct nl_msg *nlmsg_alloc_new(size_t size, uint16_t nlmsgtype, uint16_t flags);
void *nlmsg_reserve(struct nl_msg *n, uint32_t len, uint32_t pad);

View file

@ -27,10 +27,9 @@ test_use_symbols(void)
(void (*)(void)) nlmsg_hdr,
(void (*)(void)) nlmsg_reserve,
(void (*)(void)) nla_reserve,
(void (*)(void)) nlmsg_alloc_size,
(void (*)(void)) nlmsg_alloc,
(void (*)(void)) nlmsg_alloc_convert,
(void (*)(void)) nlmsg_alloc_simple,
(void (*)(void)) nlmsg_alloc_new,
(void (*)(void)) nlmsg_alloc,
(void (*)(void)) nlmsg_free,
(void (*)(void)) nlmsg_append,
(void (*)(void)) nlmsg_parse,

View file

@ -86,7 +86,7 @@ _nl80211_alloc_msg(guint16 genl_family_id, int ifindex, int phy, uint8_t cmd, ui
{
nm_auto_nlmsg struct nl_msg *msg = NULL;
msg = nlmsg_alloc();
msg = nlmsg_alloc(0);
genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, genl_family_id, 0, flags, cmd, 0);
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
if (phy != -1)

View file

@ -79,7 +79,7 @@ _nl802154_alloc_msg(guint16 genl_family_id, int ifindex, uint8_t cmd, uint16_t f
{
nm_auto_nlmsg struct nl_msg *msg = NULL;
msg = nlmsg_alloc();
msg = nlmsg_alloc(0);
if (!genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, genl_family_id, 0, flags, cmd, 0))
goto nla_put_failure;
NLA_PUT_U32(msg, NL802154_ATTR_IFINDEX, ifindex);