From 74cd6a538d4bcad1638e63246faf1bcb4094a798 Mon Sep 17 00:00:00 2001 From: Shi Lei Date: Fri, 7 Sep 2018 16:14:46 +0800 Subject: [PATCH] util: netlink: Replace virNetDevPutExtraHeader with nlmsg_append nlmsg_append from the libnl library provides exactly the same functionality, so we should rely on that instead. This also allows us to drop the aforementioned function completely. Signed-off-by: Shi Lei Reviewed-by: Erik Skultety --- src/util/virnetdev.c | 44 ++++++++++++-------------------------------- 1 file changed, 12 insertions(+), 32 deletions(-) diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c index c4dd354dc3..5d4ad24d53 100644 --- a/src/util/virnetdev.c +++ b/src/util/virnetdev.c @@ -3163,28 +3163,6 @@ virNetDevGetEthtoolFeatures(virBitmapPtr bitmap, # if HAVE_DECL_DEVLINK_CMD_ESWITCH_GET -/** - * virNetDevPutExtraHeader - * reserve and prepare room for an extra header - * This function sets to zero the room that is required to put the extra - * header after the initial Netlink header. This function also increases - * the nlmsg_len field. - * - * @nlh: pointer to Netlink header - * @size: size of the extra header that we want to put - * - * Returns pointer to the start of the extended header - */ -static void * -virNetDevPutExtraHeader(struct nlmsghdr *nlh, - size_t size) -{ - char *ptr = (char *)nlh + nlh->nlmsg_len; - size_t len = NLMSG_ALIGN(size); - nlh->nlmsg_len += len; - return ptr; -} - /** * virNetDevGetFamilyId: @@ -3199,7 +3177,11 @@ virNetDevGetFamilyId(const char *family_name) { struct nl_msg *nl_msg = NULL; struct nlmsghdr *resp = NULL; - struct genlmsghdr* gmsgh = NULL; + struct genlmsghdr gmsgh = { + .cmd = CTRL_CMD_GETFAMILY, + .version = DEVLINK_GENL_VERSION, + .reserved = 0, + }; struct nlattr *tb[CTRL_ATTR_MAX + 1] = {NULL, }; unsigned int recvbuflen; uint32_t family_id = 0; @@ -3210,12 +3192,9 @@ virNetDevGetFamilyId(const char *family_name) goto cleanup; } - if (!(gmsgh = virNetDevPutExtraHeader(nlmsg_hdr(nl_msg), sizeof(struct genlmsghdr)))) + if (nlmsg_append(nl_msg, &gmsgh, sizeof(gmsgh), NLMSG_ALIGNTO) < 0) goto cleanup; - gmsgh->cmd = CTRL_CMD_GETFAMILY; - gmsgh->version = DEVLINK_GENL_VERSION; - if (nla_put_string(nl_msg, CTRL_ATTR_FAMILY_NAME, family_name) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("allocated netlink buffer is too small")); @@ -3262,7 +3241,11 @@ virNetDevSwitchdevFeature(const char *ifname, unsigned int recvbuflen; struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {NULL, }; virPCIDevicePtr pci_device_ptr = NULL; - struct genlmsghdr* gmsgh = NULL; + struct genlmsghdr gmsgh = { + .cmd = DEVLINK_CMD_ESWITCH_GET, + .version = DEVLINK_GENL_VERSION, + .reserved = 0, + }; const char *pci_name; char *pfname = NULL; int is_vf = -1; @@ -3292,12 +3275,9 @@ virNetDevSwitchdevFeature(const char *ifname, goto cleanup; } - if (!(gmsgh = virNetDevPutExtraHeader(nlmsg_hdr(nl_msg), sizeof(struct genlmsghdr)))) + if (nlmsg_append(nl_msg, &gmsgh, sizeof(gmsgh), NLMSG_ALIGNTO) < 0) goto cleanup; - gmsgh->cmd = DEVLINK_CMD_ESWITCH_GET; - gmsgh->version = DEVLINK_GENL_VERSION; - pci_name = virPCIDeviceGetName(pci_device_ptr); if (nla_put(nl_msg, DEVLINK_ATTR_BUS_NAME, strlen("pci")+1, "pci") < 0 || -- GitLab