net/tap: simplify netlink send/receive functions
authorStephen Hemminger <stephen@networkplumber.org>
Fri, 24 Apr 2020 23:36:56 +0000 (16:36 -0700)
committerFerruh Yigit <ferruh.yigit@intel.com>
Mon, 11 May 2020 20:27:39 +0000 (22:27 +0200)
The tap_nl_recv() function does not need to use the full
complex recvmsg() system call, basic recv() will work here.

Ditto for tap_nl_send() full sendmsg is not needed.

Add logic to retry in case EINTR rather than forcing
error handling back in driver or worse to ethdev API.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
Acked-by: Keith Wiles <keith.wiles@intel.com>
drivers/net/tap/tap_netlink.c

index 14bbbec..6422017 100644 (file)
@@ -101,26 +101,17 @@ tap_nl_final(int nlsk_fd)
 int
 tap_nl_send(int nlsk_fd, struct nlmsghdr *nh)
 {
-       /* man 7 netlink EXAMPLE */
-       struct sockaddr_nl sa = {
-               .nl_family = AF_NETLINK,
-       };
-       struct iovec iov = {
-               .iov_base = nh,
-               .iov_len = nh->nlmsg_len,
-       };
-       struct msghdr msg = {
-               .msg_name = &sa,
-               .msg_namelen = sizeof(sa),
-               .msg_iov = &iov,
-               .msg_iovlen = 1,
-       };
        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);
+
+retry:
+       send_bytes = send(nlsk_fd, nh, nh->nlmsg_len, 0);
        if (send_bytes < 0) {
+               if (errno == EINTR)
+                       goto retry;
+
                TAP_LOG(ERR, "Failed to send netlink message: %s (%d)",
                        strerror(errno), errno);
                return -1;
@@ -161,30 +152,22 @@ tap_nl_recv_ack(int nlsk_fd)
 int
 tap_nl_recv(int nlsk_fd, int (*cb)(struct nlmsghdr *, void *arg), void *arg)
 {
-       /* man 7 netlink EXAMPLE */
-       struct sockaddr_nl sa;
        char buf[BUF_SIZE];
-       struct iovec iov = {
-               .iov_base = buf,
-               .iov_len = sizeof(buf),
-       };
-       struct msghdr msg = {
-               .msg_name = &sa,
-               .msg_namelen = sizeof(sa),
-               .msg_iov = &iov,
-               /* One message at a time */
-               .msg_iovlen = 1,
-       };
        int multipart = 0;
        int ret = 0;
 
        do {
                struct nlmsghdr *nh;
-               int recv_bytes = 0;
+               int recv_bytes;
 
-               recv_bytes = recvmsg(nlsk_fd, &msg, 0);
-               if (recv_bytes < 0)
+retry:
+               recv_bytes = recv(nlsk_fd, buf, sizeof(buf), 0);
+               if (recv_bytes < 0) {
+                       if (errno == EINTR)
+                               goto retry;
                        return -1;
+               }
+
                for (nh = (struct nlmsghdr *)buf;
                     NLMSG_OK(nh, (unsigned int)recv_bytes);
                     nh = NLMSG_NEXT(nh, recv_bytes)) {