From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nelio Laranjeiro Subject: [PATCH 2/2] lib: add request Netlink messages Date: Tue, 13 Mar 2018 13:28:27 +0100 Message-ID: <9a637b4ddc33ff2cc7f5311f9a69643dc5abdc13.1520943890.git.nelio.laranjeiro@6wind.com> References: Cc: dev@dpdk.org To: Pascal Mazon Return-path: Received: from mail-wr0-f193.google.com (mail-wr0-f193.google.com [209.85.128.193]) by dpdk.org (Postfix) with ESMTP id BEF2C6D45 for ; Tue, 13 Mar 2018 13:29:46 +0100 (CET) Received: by mail-wr0-f193.google.com with SMTP id l8so7792213wrg.5 for ; Tue, 13 Mar 2018 05:29:46 -0700 (PDT) In-Reply-To: In-Reply-To: References: List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" rte_nl_send() generate a message without request. In some situation, the drivers needs to retrieve information from the Kernel before requesting modifications. Cc: Pascal Mazon Signed-off-by: Nelio Laranjeiro --- lib/librte_netlink/rte_netlink.c | 45 ++++++++++++++++++++++++++++++ lib/librte_netlink/rte_netlink.h | 1 + lib/librte_netlink/rte_netlink_version.map | 1 + 3 files changed, 47 insertions(+) diff --git a/lib/librte_netlink/rte_netlink.c b/lib/librte_netlink/rte_netlink.c index a4ed07d30..a2020f6f8 100644 --- a/lib/librte_netlink/rte_netlink.c +++ b/lib/librte_netlink/rte_netlink.c @@ -125,6 +125,51 @@ rte_nl_send(int nlsk_fd, struct nlmsghdr *nh) } /** + * Send a request message to the kernel on the netlink socket. + * + * @param[in] nlsk_fd + * The netlink socket file descriptor used for communication. + * @param[in] nh + * The netlink message send to the kernel. + * @param[in] req + * Pointer to the request structure. + * @param[in] len + * Length of the request in bytes. + * + * @return + * the number of sent bytes on success, -1 otherwise. + */ +int +rte_nl_request(int nlsk_fd, struct nlmsghdr *nh, void *req, int len) +{ + /* man 7 netlink EXAMPLE */ + struct sockaddr_nl sa = { + .nl_family = AF_NETLINK, + }; + struct iovec iov[2] = { + { .iov_base = nh, .iov_len = sizeof(*nh), }, + { .iov_base = req, .iov_len = len, }, + }; + struct msghdr msg = { + .msg_name = &sa, + .msg_namelen = sizeof(sa), + .msg_iov = iov, + .msg_iovlen = 2, + }; + int send_bytes; + + nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */ + nh->nlmsg_seq = (uint32_t)rte_rand(); + send_bytes = sendmsg(nlsk_fd, &msg, 0); + if (send_bytes < 0) { + RTE_LOG(ERR, PMD, "Failed to send netlink message: %s (%d)\n", + strerror(errno), errno); + return -1; + } + return send_bytes; +} + +/** * Check that the kernel sends an appropriate ACK in response * to an rte_nl_send(). * diff --git a/lib/librte_netlink/rte_netlink.h b/lib/librte_netlink/rte_netlink.h index 29f7d64c5..9af03e738 100644 --- a/lib/librte_netlink/rte_netlink.h +++ b/lib/librte_netlink/rte_netlink.h @@ -28,6 +28,7 @@ struct nlmsg { int rte_nl_init(uint32_t nl_groups); int rte_nl_final(int nlsk_fd); int rte_nl_send(int nlsk_fd, struct nlmsghdr *nh); +int rte_nl_request(int nlsk_fd, struct nlmsghdr *nh, void *req, int len); int rte_nl_recv(int nlsk_fd, int (*callback)(struct nlmsghdr *, void *), void *arg); int rte_nl_recv_ack(int nlsk_fd); diff --git a/lib/librte_netlink/rte_netlink_version.map b/lib/librte_netlink/rte_netlink_version.map index 5aa9b6228..a52254b36 100644 --- a/lib/librte_netlink/rte_netlink_version.map +++ b/lib/librte_netlink/rte_netlink_version.map @@ -4,6 +4,7 @@ DPDK_18.05 { rte_nl_init; rte_nl_final; rte_nl_send; + rte_nl_request; rte_nl_recv; rte_nl_recv_ack; rte_nlattr_add; -- 2.11.0