Blob Blame History Raw
From 47c1f956e7f212aec0eec174c3fb5801eae5d5bc Mon Sep 17 00:00:00 2001
From: Chris Leech <cleech@redhat.com>
Date: Thu, 24 Jan 2019 18:16:47 -0800
Subject: [PATCH 1/1] fcoemon: link buffer resize fix

---
 fcoemon.c | 54 ++++++++++++++++++------------------------------------
 1 file changed, 18 insertions(+), 36 deletions(-)

diff --git a/fcoemon.c b/fcoemon.c
index bf73a0d4c89e..ff8a8b869ba4 100644
--- a/fcoemon.c
+++ b/fcoemon.c
@@ -328,7 +328,6 @@ static int fcm_link_socket;
 static int fcm_link_seq;
 static void fcm_link_recv(void *);
 static void fcm_link_getlink(void);
-static int fcm_link_buf_check(size_t);
 static void clear_dcbd_info(struct fcm_netif *ff);
 static int fcoe_vid_from_ifname(const char *ifname);
 
@@ -354,8 +353,7 @@ char progname[20];
  * large enough to fit and expand it if we ever do a read that almost fills it.
  */
 static char *fcm_link_buf;
-static size_t fcm_link_buf_size = 4096;	/* initial size */
-static const size_t fcm_link_buf_fuzz = 300;	/* "almost full" remainder */
+static size_t fcm_link_buf_size = 8192;	/* initial size */
 
 /*
  * A value must be surrounded by quates, e.g. "x".
@@ -1856,8 +1854,22 @@ static void fcm_link_recv(UNUSED void *arg)
 	size_t plen;
 	size_t rlen;
 
+	/* check to make sure our receive buffer is large enough,
+	 * or scale it up as needed */
+	rc = recv(fcm_link_socket, NULL, 0, MSG_PEEK | MSG_TRUNC);
+	if (rc > fcm_link_buf_size) {
+		FCM_LOG_DBG("resizing link buf to %d bytes\n", rc);
+		void *resize = realloc(fcm_link_buf, rc);
+		if (resize) {
+			fcm_link_buf = resize;
+			fcm_link_buf_size = rc;
+		} else {
+			FCM_LOG_ERR(errno, "Failed to allocate link buffer");
+		}
+	}
+
 	buf = fcm_link_buf;
-	rc = read(fcm_link_socket, buf, fcm_link_buf_size);
+	rc = recv(fcm_link_socket, buf, fcm_link_buf_size, 0);
 	if (rc <= 0) {
 		if (rc < 0)
 			FCM_LOG_ERR(errno, "Error reading from "
@@ -1866,11 +1878,6 @@ static void fcm_link_recv(UNUSED void *arg)
 		return;
 	}
 
-	if (fcm_link_buf_check(rc)) {
-		fcm_link_getlink();
-		return;
-	}
-
 	hp = (struct nlmsghdr *)buf;
 	rlen = rc;
 	for (hp = (struct nlmsghdr *)buf; NLMSG_OK(hp, rlen);
@@ -1935,34 +1942,9 @@ static void fcm_link_getlink(void)
 	msg.nl.nlmsg_pid = getpid();
 	msg.ifi.ifi_family = AF_UNSPEC;
 	msg.ifi.ifi_type = ARPHRD_ETHER;
-	rc = write(fcm_link_socket, &msg, sizeof(msg));
+	rc = send(fcm_link_socket, &msg, sizeof(msg), 0);
 	if (rc < 0)
-		FCM_LOG_ERR(errno, "write error");
-}
-
-/*
- * Check for whether buffer needs to grow based on amount read.
- * Free's the old buffer so don't use that after this returns non-zero.
- */
-static int fcm_link_buf_check(size_t read_len)
-{
-	char *buf;
-	size_t len = read_len;
-
-	if (len > fcm_link_buf_size - fcm_link_buf_fuzz) {
-		len = fcm_link_buf_size;
-		len = len + len / 2;	/* grow by 50% */
-		buf = malloc(len);
-		if (buf != NULL) {
-			free(fcm_link_buf);
-			fcm_link_buf = buf;
-			fcm_link_buf_size = len;
-			return 1;
-		} else {
-			FCM_LOG_ERR(errno, "failed to allocate link buffer");
-		}
-	}
-	return 0;
+		FCM_LOG_ERR(errno, "send error");
 }
 
 static void fcm_fcoe_init(void)
-- 
2.17.2