From: Denis Kenzior <denkenz@gmail.com>
To: iwd@lists.linux.dev
Cc: Denis Kenzior <denkenz@gmail.com>
Subject: [PATCH 3/3] monitor: Update to the new ell l_netlink_send API
Date: Fri, 26 Jul 2024 17:37:45 -0500 [thread overview]
Message-ID: <20240726223754.159303-3-denkenz@gmail.com> (raw)
In-Reply-To: <20240726223754.159303-1-denkenz@gmail.com>
---
monitor/main.c | 88 ++++++++++++++------------------------------------
1 file changed, 24 insertions(+), 64 deletions(-)
diff --git a/monitor/main.c b/monitor/main.c
index 32f5ec4fc7f3..c560db775caa 100644
--- a/monitor/main.c
+++ b/monitor/main.c
@@ -260,22 +260,6 @@ static struct l_genl *genl_lookup(const char *ifname)
return genl;
}
-static size_t rta_add(void *rta_buf, unsigned short type, uint16_t len,
- const void *data)
-{
- unsigned short rta_len = RTA_LENGTH(len);
- struct rtattr *rta = rta_buf;
-
- memset(RTA_DATA(rta), 0, RTA_SPACE(len));
-
- rta->rta_len = rta_len;
- rta->rta_type = type;
- if (len)
- memcpy(RTA_DATA(rta), data, len);
-
- return RTA_SPACE(len);
-}
-
static bool rta_linkinfo_kind(struct rtattr *rta, unsigned short len,
const char* kind)
{
@@ -304,10 +288,9 @@ static struct l_netlink *rtm_interface_send_message(struct l_netlink *rtnl,
{
size_t nlmon_type_len = strlen(NLMON_TYPE);
unsigned short ifname_len = 0;
- size_t bufsize;
- struct ifinfomsg *rtmmsg;
- void *rta_buf;
- struct rtattr *linkinfo_rta;
+ struct l_netlink_message *nlm;
+ struct ifinfomsg ifi;
+ uint16_t flags = 0;
if (ifname) {
ifname_len = strlen(ifname) + 1;
@@ -316,64 +299,41 @@ static struct l_netlink *rtm_interface_send_message(struct l_netlink *rtnl,
return NULL;
}
+ if (!L_IN_SET(rtm_msg_type, RTM_NEWLINK, RTM_DELLINK, RTM_GETLINK))
+ return NULL;
+
if (!rtnl)
rtnl = l_netlink_new(NETLINK_ROUTE);
if (!rtnl)
return NULL;
- bufsize = NLMSG_LENGTH(sizeof(struct ifinfomsg)) +
- RTA_SPACE(ifname_len) + RTA_SPACE(0) +
- RTA_SPACE(nlmon_type_len);
-
- rtmmsg = l_malloc(bufsize);
- memset(rtmmsg, 0, bufsize);
-
- rtmmsg->ifi_family = AF_UNSPEC;
- rtmmsg->ifi_change = ~0;
-
- rta_buf = rtmmsg + 1;
-
- if (ifname)
- rta_buf += rta_add(rta_buf, IFLA_IFNAME, ifname_len, ifname);
-
- linkinfo_rta = rta_buf;
-
- rta_buf += rta_add(rta_buf, IFLA_LINKINFO, 0, NULL);
- rta_buf += rta_add(rta_buf, IFLA_INFO_KIND, nlmon_type_len, NLMON_TYPE);
-
- linkinfo_rta->rta_len = rta_buf - (void *) linkinfo_rta;
+ memset(&ifi, 0, sizeof(ifi));
+ ifi.ifi_family = AF_UNSPEC;
+ ifi.ifi_change = ~0;
switch (rtm_msg_type) {
case RTM_NEWLINK:
- rtmmsg->ifi_flags = IFF_UP | IFF_ALLMULTI | IFF_NOARP;
-
- l_netlink_send(rtnl, RTM_NEWLINK, NLM_F_CREATE|NLM_F_EXCL,
- rtmmsg, rta_buf - (void *) rtmmsg, callback,
- user_data, destroy);
+ ifi.ifi_flags = IFF_UP | IFF_ALLMULTI | IFF_NOARP;
+ flags = NLM_F_CREATE | NLM_F_EXCL;
break;
-
- case RTM_DELLINK:
- rta_buf += rta_add(rta_buf, IFLA_IFNAME, ifname_len, ifname);
-
- l_netlink_send(rtnl, RTM_DELLINK, 0, rtmmsg,
- rta_buf - (void *)rtmmsg, callback, user_data,
- destroy);
- break;
-
case RTM_GETLINK:
- l_netlink_send(rtnl, RTM_GETLINK, NLM_F_DUMP, rtmmsg,
- rta_buf - (void *)rtmmsg, callback, user_data,
- destroy);
- break;
-
- default:
- l_netlink_destroy(rtnl);
- rtnl = NULL;
+ flags = NLM_F_DUMP;
break;
}
- l_free(rtmmsg);
+ nlm = l_netlink_message_new(rtm_msg_type, flags);;
+ l_netlink_message_add_header(nlm, &ifi, sizeof(ifi));
+
+ if (ifname)
+ l_netlink_message_append(nlm, IFLA_IFNAME, ifname, ifname_len);
+
+ l_netlink_message_enter_nested(nlm, IFLA_LINKINFO);
+ l_netlink_message_append(nlm, IFLA_INFO_KIND,
+ NLMON_TYPE, nlmon_type_len);
+ l_netlink_message_leave_nested(nlm);
+
+ l_netlink_send(rtnl, nlm, callback, user_data, destroy);
return rtnl;
}
--
2.45.2
next prev parent reply other threads:[~2024-07-26 22:37 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-26 22:37 [PATCH 1/3] wired: Update to the new l_netlink_send API Denis Kenzior
2024-07-26 22:37 ` [PATCH 2/3] netdev: " Denis Kenzior
2024-07-26 22:37 ` Denis Kenzior [this message]
2024-07-29 11:41 ` [PATCH 1/3] wired: " James Prestwood
2024-07-30 15:53 ` Denis Kenzior
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240726223754.159303-3-denkenz@gmail.com \
--to=denkenz@gmail.com \
--cc=iwd@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox