1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-01-11 09:17:52 +03:00

util: virnetlink: Add wrapper for 'nlmsg_alloc_simple'

The function is used in many places and fails only on allocation
failures. Since trying to recover from allocation failure of a small
buffer by reporting error doesn't make sense add a wrapper for
'nlmsg_alloc_simple' which will 'abort()' on failure and replace all
allocations of netlink message with the new helper.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Laine Stump <laine@redhat.com>
This commit is contained in:
Peter Krempa 2021-02-24 09:51:19 +01:00
parent bbc25f0d03
commit 7a0b625ea2
6 changed files with 32 additions and 59 deletions

View File

@ -1596,11 +1596,7 @@ virNetDevSetVfConfig(const char *ifname, int vf,
if (!macaddr && vlanid < 0)
return -1;
nl_msg = nlmsg_alloc_simple(RTM_SETLINK, NLM_F_REQUEST);
if (!nl_msg) {
virReportOOMError();
return rc;
}
nl_msg = virNetlinkMsgNew(RTM_SETLINK, NLM_F_REQUEST);
if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0)
goto buffer_too_small;
@ -3132,11 +3128,7 @@ virNetDevGetFamilyId(const char *family_name,
unsigned int recvbuflen;
int ret = -1;
if (!(nl_msg = nlmsg_alloc_simple(GENL_ID_CTRL,
NLM_F_REQUEST | NLM_F_ACK))) {
virReportOOMError();
goto cleanup;
}
nl_msg = virNetlinkMsgNew(GENL_ID_CTRL, NLM_F_REQUEST | NLM_F_ACK);
if (nlmsg_append(nl_msg, &gmsgh, sizeof(gmsgh), NLMSG_ALIGNTO) < 0)
goto cleanup;
@ -3220,11 +3212,7 @@ virNetDevSwitchdevFeature(const char *ifname,
if ((rv = virNetDevGetFamilyId(DEVLINK_GENL_NAME, &family_id)) <= 0)
return rv;
if (!(nl_msg = nlmsg_alloc_simple(family_id,
NLM_F_REQUEST | NLM_F_ACK))) {
virReportOOMError();
goto cleanup;
}
nl_msg = virNetlinkMsgNew(family_id, NLM_F_REQUEST | NLM_F_ACK);
if (nlmsg_append(nl_msg, &gmsgh, sizeof(gmsgh), NLMSG_ALIGNTO) < 0)
goto cleanup;

View File

@ -1036,13 +1036,9 @@ virNetDevBridgeFDBAddDel(const virMacAddr *mac, const char *ifname,
if (!(ndm.ndm_state & (NUD_PERMANENT | NUD_REACHABLE)))
ndm.ndm_state |= NUD_PERMANENT;
nl_msg = nlmsg_alloc_simple(isAdd ? RTM_NEWNEIGH : RTM_DELNEIGH,
NLM_F_REQUEST |
(isAdd ? (NLM_F_CREATE | NLM_F_EXCL) : 0));
if (!nl_msg) {
virReportOOMError();
return -1;
}
nl_msg = virNetlinkMsgNew(isAdd ? RTM_NEWNEIGH : RTM_DELNEIGH,
NLM_F_REQUEST |
(isAdd ? (NLM_F_CREATE | NLM_F_EXCL) : 0));
if (nlmsg_append(nl_msg, &ndm, sizeof(ndm), NLMSG_ALIGNTO) < 0)
goto buffer_too_small;

View File

@ -107,11 +107,8 @@ virNetDevCreateNetlinkAddressMessage(int messageType,
if ((ifindex = if_nametoindex(ifname)) == 0)
return NULL;
if (!(nlmsg = nlmsg_alloc_simple(messageType,
NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL))) {
virReportOOMError();
return NULL;
}
nlmsg = virNetlinkMsgNew(messageType,
NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
memset(&ifa, 0, sizeof(ifa));
@ -323,12 +320,8 @@ virNetDevIPRouteAdd(const char *ifname,
if ((ifindex = if_nametoindex(ifname)) == 0)
return -1;
if (!(nlmsg = nlmsg_alloc_simple(RTM_NEWROUTE,
NLM_F_REQUEST | NLM_F_CREATE |
NLM_F_EXCL))) {
virReportOOMError();
return -1;
}
nlmsg = virNetlinkMsgNew(RTM_NEWROUTE,
NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
memset(&rtmsg, 0, sizeof(rtmsg));

View File

@ -689,11 +689,7 @@ virNetDevVPortProfileOpSetLink(const char *ifname, int ifindex,
? virUUIDFormat(hostUUID, hostUUIDStr)
: "(unspecified)"));
nl_msg = nlmsg_alloc_simple(RTM_SETLINK, NLM_F_REQUEST);
if (!nl_msg) {
virReportOOMError();
return rc;
}
nl_msg = virNetlinkMsgNew(RTM_SETLINK, NLM_F_REQUEST);
if (nlmsg_append(nl_msg, &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0)
goto buffer_too_small;

View File

@ -133,6 +133,18 @@ static virNetlinkHandle *placeholder_nlhandle;
/* Function definitions */
struct nl_msg *
virNetlinkMsgNew(int nlmsgtype,
int nlmsgflags)
{
struct nl_msg *ret;
if (!(ret = nlmsg_alloc_simple(nlmsgtype, nlmsgflags)))
abort();
return ret;
}
/**
* virNetlinkStartup:
*
@ -511,11 +523,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
ifinfo.ifi_index = ifindex;
nl_msg = nlmsg_alloc_simple(RTM_GETLINK, NLM_F_REQUEST);
if (!nl_msg) {
virReportOOMError();
return -1;
}
nl_msg = virNetlinkMsgNew(RTM_GETLINK, NLM_F_REQUEST);
NETLINK_MSG_APPEND(nl_msg, sizeof(ifinfo), &ifinfo);
@ -595,12 +603,8 @@ virNetlinkNewLink(const char *ifname,
return -1;
}
nl_msg = nlmsg_alloc_simple(RTM_NEWLINK,
NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
if (!nl_msg) {
virReportOOMError();
return -1;
}
nl_msg = virNetlinkMsgNew(RTM_NEWLINK,
NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
NETLINK_MSG_APPEND(nl_msg, sizeof(ifinfo), &ifinfo);
@ -684,11 +688,7 @@ virNetlinkDelLink(const char *ifname, virNetlinkTalkFallback fallback)
unsigned int resp_len = 0;
int error = 0;
nl_msg = nlmsg_alloc_simple(RTM_DELLINK, NLM_F_REQUEST);
if (!nl_msg) {
virReportOOMError();
return -1;
}
nl_msg = virNetlinkMsgNew(RTM_DELLINK, NLM_F_REQUEST);
NETLINK_MSG_APPEND(nl_msg, sizeof(ifinfo), &ifinfo);
@ -738,11 +738,7 @@ virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
unsigned int resp_len = 0;
int error = 0;
nl_msg = nlmsg_alloc_simple(RTM_GETNEIGH, NLM_F_DUMP | NLM_F_REQUEST);
if (!nl_msg) {
virReportOOMError();
return -1;
}
nl_msg = virNetlinkMsgNew(RTM_GETNEIGH, NLM_F_DUMP | NLM_F_REQUEST);
NETLINK_MSG_APPEND(nl_msg, sizeof(ndinfo), &ndinfo);

View File

@ -29,6 +29,10 @@
typedef struct nl_msg virNetlinkMsg;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNetlinkMsg, nlmsg_free);
struct nl_msg *
virNetlinkMsgNew(int nlmsgtype,
int nlmsgflags);
#else
struct nl_msg;