[PATCH 17/33] util: virnetlink: Add wrapper for 'nlmsg_alloc_simple'

Peter Krempa pkrempa at redhat.com
Wed Feb 24 16:16:52 UTC 2021


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 at redhat.com>
---
 src/util/virnetdev.c             | 18 +++------------
 src/util/virnetdevbridge.c       | 10 +++------
 src/util/virnetdevip.c           | 15 ++++---------
 src/util/virnetdevvportprofile.c |  6 +----
 src/util/virnetlink.c            | 38 ++++++++++++++------------------
 src/util/virnetlink.h            |  4 ++++
 6 files changed, 32 insertions(+), 59 deletions(-)

diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index 2b4c8b6280..d0c76ce26c 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -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;
diff --git a/src/util/virnetdevbridge.c b/src/util/virnetdevbridge.c
index d475e4c43d..7b5ea4fe1d 100644
--- a/src/util/virnetdevbridge.c
+++ b/src/util/virnetdevbridge.c
@@ -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;
diff --git a/src/util/virnetdevip.c b/src/util/virnetdevip.c
index 4eb8ef76d1..83da7bc46d 100644
--- a/src/util/virnetdevip.c
+++ b/src/util/virnetdevip.c
@@ -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));

diff --git a/src/util/virnetdevvportprofile.c b/src/util/virnetdevvportprofile.c
index 5d6c055b32..c0fc04c699 100644
--- a/src/util/virnetdevvportprofile.c
+++ b/src/util/virnetdevvportprofile.c
@@ -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;
diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index 9bd7339054..a06195bd00 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -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);

diff --git a/src/util/virnetlink.h b/src/util/virnetlink.h
index cab685feea..e05c62e635 100644
--- a/src/util/virnetlink.h
+++ b/src/util/virnetlink.h
@@ -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;
-- 
2.29.2




More information about the libvir-list mailing list