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

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