Blame SOURCES/0001-fcoemon-link-buffer-resize-fix.patch

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