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 <pascal.ma...@6wind.com>

Signed-off-by: Nelio Laranjeiro <nelio.laranje...@6wind.com>
---
 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

Reply via email to