From: Jakub Kicinski <kuba@kernel.org>
To: davem@davemloft.net
Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com,
jiri@resnulli.us, sdf@google.com, nicolas.dichtel@6wind.com,
donald.hunter@gmail.com, Jakub Kicinski <kuba@kernel.org>
Subject: [PATCH net-next 13/15] tools: ynl: stop using mnl socket helpers
Date: Thu, 22 Feb 2024 15:56:12 -0800 [thread overview]
Message-ID: <20240222235614.180876-14-kuba@kernel.org> (raw)
In-Reply-To: <20240222235614.180876-1-kuba@kernel.org>
Most libmnl socket helpers can be replaced by direct calls to
the underlying libc API. We need portid, the netlink manpage
suggests we bind() address of zero.
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
tools/net/ynl/lib/ynl-priv.h | 2 ++
tools/net/ynl/lib/ynl.c | 60 +++++++++++++++++++++++-------------
tools/net/ynl/lib/ynl.h | 2 +-
3 files changed, 42 insertions(+), 22 deletions(-)
diff --git a/tools/net/ynl/lib/ynl-priv.h b/tools/net/ynl/lib/ynl-priv.h
index 658768243d2f..37720e505a11 100644
--- a/tools/net/ynl/lib/ynl-priv.h
+++ b/tools/net/ynl/lib/ynl-priv.h
@@ -35,6 +35,8 @@ enum ynl_parse_result {
YNL_PARSE_CB_OK = 1,
};
+#define YNL_SOCKET_BUFFER_SIZE (1 << 17)
+
#define YNL_ARRAY_SIZE(array) (sizeof(array) ? \
sizeof(array) / sizeof(array[0]) : 0)
diff --git a/tools/net/ynl/lib/ynl.c b/tools/net/ynl/lib/ynl.c
index cc0d9701b145..febb7581062d 100644
--- a/tools/net/ynl/lib/ynl.c
+++ b/tools/net/ynl/lib/ynl.c
@@ -3,10 +3,12 @@
#include <poll.h>
#include <string.h>
#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
#include <linux/types.h>
-
#include <libmnl/libmnl.h>
#include <linux/genetlink.h>
+#include <sys/socket.h>
#include "ynl.h"
@@ -463,7 +465,7 @@ static int ynl_sock_read_msgs(struct ynl_parse_arg *yarg, ynl_parse_cb_t cb)
ssize_t len, rem;
int ret;
- len = mnl_socket_recvfrom(ys->sock, ys->rx_buf, MNL_SOCKET_BUFFER_SIZE);
+ len = recv(ys->socket, ys->rx_buf, YNL_SOCKET_BUFFER_SIZE, 0);
if (len < 0)
return len;
@@ -599,7 +601,7 @@ static int ynl_sock_read_family(struct ynl_sock *ys, const char *family_name)
nlh = ynl_gemsg_start_req(ys, GENL_ID_CTRL, CTRL_CMD_GETFAMILY, 1);
ynl_attr_put_strz(nlh, CTRL_ATTR_FAMILY_NAME, family_name);
- err = mnl_socket_sendto(ys->sock, nlh, nlh->nlmsg_len);
+ err = send(ys->socket, nlh, nlh->nlmsg_len, 0);
if (err < 0) {
perr(ys, "failed to request socket family info");
return err;
@@ -624,38 +626,54 @@ static int ynl_sock_read_family(struct ynl_sock *ys, const char *family_name)
struct ynl_sock *
ynl_sock_create(const struct ynl_family *yf, struct ynl_error *yse)
{
+ struct sockaddr_nl addr;
struct ynl_sock *ys;
+ socklen_t addrlen;
int one = 1;
- ys = malloc(sizeof(*ys) + 2 * MNL_SOCKET_BUFFER_SIZE);
+ ys = malloc(sizeof(*ys) + 2 * YNL_SOCKET_BUFFER_SIZE);
if (!ys)
return NULL;
memset(ys, 0, sizeof(*ys));
ys->family = yf;
ys->tx_buf = &ys->raw_buf[0];
- ys->rx_buf = &ys->raw_buf[MNL_SOCKET_BUFFER_SIZE];
+ ys->rx_buf = &ys->raw_buf[YNL_SOCKET_BUFFER_SIZE];
ys->ntf_last_next = &ys->ntf_first;
- ys->sock = mnl_socket_open(NETLINK_GENERIC);
- if (!ys->sock) {
+ ys->socket = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
+ if (ys->socket < 0) {
__perr(yse, "failed to create a netlink socket");
goto err_free_sock;
}
- if (mnl_socket_setsockopt(ys->sock, NETLINK_CAP_ACK,
- &one, sizeof(one))) {
+ if (setsockopt(ys->socket, SOL_NETLINK, NETLINK_CAP_ACK,
+ &one, sizeof(one))) {
__perr(yse, "failed to enable netlink ACK");
goto err_close_sock;
}
- if (mnl_socket_setsockopt(ys->sock, NETLINK_EXT_ACK,
- &one, sizeof(one))) {
+ if (setsockopt(ys->socket, SOL_NETLINK, NETLINK_EXT_ACK,
+ &one, sizeof(one))) {
__perr(yse, "failed to enable netlink ext ACK");
goto err_close_sock;
}
+ memset(&addr, 0, sizeof(addr));
+ addr.nl_family = AF_NETLINK;
+ if (bind(ys->socket, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+ __perr(yse, "unable to bind to a socket address");
+ goto err_close_sock;;
+ }
+
+ memset(&addr, 0, sizeof(addr));
+ addrlen = sizeof(addr);
+ if (getsockname(ys->socket, (struct sockaddr *)&addr, &addrlen) < 0) {
+ __perr(yse, "unable to read socket address");
+ goto err_close_sock;;
+ }
+ ys->portid = addr.nl_pid;
ys->seq = random();
- ys->portid = mnl_socket_get_portid(ys->sock);
+
if (ynl_sock_read_family(ys, yf->name)) {
if (yse)
@@ -666,7 +684,7 @@ ynl_sock_create(const struct ynl_family *yf, struct ynl_error *yse)
return ys;
err_close_sock:
- mnl_socket_close(ys->sock);
+ close(ys->socket);
err_free_sock:
free(ys);
return NULL;
@@ -676,7 +694,7 @@ void ynl_sock_destroy(struct ynl_sock *ys)
{
struct ynl_ntf_base_type *ntf;
- mnl_socket_close(ys->sock);
+ close(ys->socket);
while ((ntf = ynl_ntf_dequeue(ys)))
ynl_ntf_free(ntf);
free(ys->mcast_groups);
@@ -703,9 +721,9 @@ int ynl_subscribe(struct ynl_sock *ys, const char *grp_name)
return -1;
}
- err = mnl_socket_setsockopt(ys->sock, NETLINK_ADD_MEMBERSHIP,
- &ys->mcast_groups[i].id,
- sizeof(ys->mcast_groups[i].id));
+ err = setsockopt(ys->socket, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
+ &ys->mcast_groups[i].id,
+ sizeof(ys->mcast_groups[i].id));
if (err < 0) {
perr(ys, "Subscribing to multicast group failed");
return -1;
@@ -716,7 +734,7 @@ int ynl_subscribe(struct ynl_sock *ys, const char *grp_name)
int ynl_socket_get_fd(struct ynl_sock *ys)
{
- return mnl_socket_get_fd(ys->sock);
+ return ys->socket;
}
struct ynl_ntf_base_type *ynl_ntf_dequeue(struct ynl_sock *ys)
@@ -788,7 +806,7 @@ int ynl_ntf_check(struct ynl_sock *ys)
*/
struct pollfd pfd = { };
- pfd.fd = mnl_socket_get_fd(ys->sock);
+ pfd.fd = ys->socket;
pfd.events = POLLIN;
err = poll(&pfd, 1, 1);
if (err < 1)
@@ -854,7 +872,7 @@ int ynl_exec(struct ynl_sock *ys, struct nlmsghdr *req_nlh,
{
int err;
- err = mnl_socket_sendto(ys->sock, req_nlh, req_nlh->nlmsg_len);
+ err = send(ys->socket, req_nlh, req_nlh->nlmsg_len, 0);
if (err < 0)
return err;
@@ -907,7 +925,7 @@ int ynl_exec_dump(struct ynl_sock *ys, struct nlmsghdr *req_nlh,
{
int err;
- err = mnl_socket_sendto(ys->sock, req_nlh, req_nlh->nlmsg_len);
+ err = send(ys->socket, req_nlh, req_nlh->nlmsg_len, 0);
if (err < 0)
return err;
diff --git a/tools/net/ynl/lib/ynl.h b/tools/net/ynl/lib/ynl.h
index 4849c142fce0..dbeeef8ce91a 100644
--- a/tools/net/ynl/lib/ynl.h
+++ b/tools/net/ynl/lib/ynl.h
@@ -59,7 +59,7 @@ struct ynl_sock {
/* private: */
const struct ynl_family *family;
- struct mnl_socket *sock;
+ int socket;
__u32 seq;
__u32 portid;
__u16 family_id;
--
2.43.2
next prev parent reply other threads:[~2024-02-22 23:56 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-22 23:55 [PATCH net-next 00/15] tools: ynl: stop using libmnl Jakub Kicinski
2024-02-22 23:56 ` [PATCH net-next 01/15] tools: ynl: give up on libmnl for auto-ints Jakub Kicinski
2024-02-23 13:51 ` Nicolas Dichtel
2024-02-23 14:35 ` Jakub Kicinski
2024-02-23 15:07 ` Nicolas Dichtel
2024-02-23 15:34 ` Donald Hunter
2024-02-22 23:56 ` [PATCH net-next 02/15] tools: ynl: create local attribute helpers Jakub Kicinski
2024-02-23 14:03 ` Nicolas Dichtel
2024-02-23 14:46 ` Jakub Kicinski
2024-02-23 15:09 ` Nicolas Dichtel
2024-02-22 23:56 ` [PATCH net-next 03/15] tools: ynl: create local for_each helpers Jakub Kicinski
2024-02-23 15:16 ` Nicolas Dichtel
2024-02-22 23:56 ` [PATCH net-next 04/15] tools: ynl: create local nlmsg access helpers Jakub Kicinski
2024-02-23 15:20 ` Nicolas Dichtel
2024-02-22 23:56 ` [PATCH net-next 05/15] tools: ynl: create local ARRAY_SIZE() helper Jakub Kicinski
2024-02-23 15:21 ` Nicolas Dichtel
2024-02-22 23:56 ` [PATCH net-next 06/15] tools: ynl: make yarg the first member of struct ynl_dump_state Jakub Kicinski
2024-02-23 15:23 ` Nicolas Dichtel
2024-02-22 23:56 ` [PATCH net-next 07/15] tools: ynl-gen: remove unused parse code Jakub Kicinski
2024-02-23 15:24 ` Nicolas Dichtel
2024-02-22 23:56 ` [PATCH net-next 08/15] tools: ynl: wrap recv() + mnl_cb_run2() into a single helper Jakub Kicinski
2024-02-23 15:33 ` Nicolas Dichtel
2024-02-22 23:56 ` [PATCH net-next 09/15] tools: ynl: use ynl_sock_read_msgs() for ACK handling Jakub Kicinski
2024-02-23 15:35 ` Nicolas Dichtel
2024-02-22 23:56 ` [PATCH net-next 10/15] tools: ynl: stop using mnl_cb_run2() Jakub Kicinski
2024-02-23 15:50 ` Nicolas Dichtel
2024-02-22 23:56 ` [PATCH net-next 11/15] tools: ynl: switch away from mnl_cb_t Jakub Kicinski
2024-02-23 16:04 ` Nicolas Dichtel
2024-02-22 23:56 ` [PATCH net-next 12/15] tools: ynl: switch away from MNL_CB_* Jakub Kicinski
2024-02-23 16:05 ` Nicolas Dichtel
2024-02-22 23:56 ` Jakub Kicinski [this message]
2024-02-23 16:14 ` [PATCH net-next 13/15] tools: ynl: stop using mnl socket helpers Nicolas Dichtel
2024-02-22 23:56 ` [PATCH net-next 14/15] tools: ynl: remove the libmnl dependency Jakub Kicinski
2024-02-23 16:14 ` Nicolas Dichtel
2024-02-22 23:56 ` [PATCH net-next 15/15] tools: ynl: use MSG_DONTWAIT for getting notifications Jakub Kicinski
2024-02-23 16:17 ` Nicolas Dichtel
2024-02-23 16:26 ` [PATCH net-next 00/15] tools: ynl: stop using libmnl Donald Hunter
2024-02-23 16:34 ` Jakub Kicinski
2024-02-26 9:04 ` Donald Hunter
2024-02-26 18:00 ` Jakub Kicinski
2024-02-27 10:49 ` Donald Hunter
2024-02-27 15:56 ` Jakub Kicinski
2024-02-27 16:29 ` Donald Hunter
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=20240222235614.180876-14-kuba@kernel.org \
--to=kuba@kernel.org \
--cc=davem@davemloft.net \
--cc=donald.hunter@gmail.com \
--cc=edumazet@google.com \
--cc=jiri@resnulli.us \
--cc=netdev@vger.kernel.org \
--cc=nicolas.dichtel@6wind.com \
--cc=pabeni@redhat.com \
--cc=sdf@google.com \
/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;
as well as URLs for NNTP newsgroup(s).