diff --git a/lib/vty.c b/lib/vty.c index d0bbf0e61a5c..0d2580389862 100644 --- a/lib/vty.c +++ b/lib/vty.c @@ -345,8 +345,17 @@ int vty_out(struct vty *vty, const char *format, ...) case VTY_SHELL_SERV: case VTY_FILE: default: + vty->vty_buf_size_written += strlen(filtered); /* print without crlf replacement */ buffer_put(vty->obuf, (uint8_t *)filtered, strlen(filtered)); + /* For every chunk of memory, we invoke vtysh_flush where we + * put the data of collective vty->obuf Linked List items on the + * socket and free the vty->obuf data. + */ + if (vty->vty_buf_size_written >= MAX_INTERMEDIATE_FLUSH) { + vty->vty_buf_size_written = 0; + vtysh_flush(vty); + } break; } @@ -2141,6 +2150,21 @@ static void vtysh_accept(struct event *thread) close(sock); return; } + /* Setting the SND buffer size to 16MB to increase the buffer size + * the socket can hold before sending it to VTY shell + */ + uint32_t sndbufsize = SEND_BUF_MAX; + int ret; + + ret = setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&sndbufsize, + sizeof(sndbufsize)); + if (ret < 0) { + flog_err(EC_LIB_SOCKET, + "Cannot set socket %d send buffer size, %s", sock, + safe_strerror(errno)); + close(sock); + return; + } set_cloexec(sock); #ifdef VTYSH_DEBUG @@ -2227,6 +2251,7 @@ static int vtysh_flush(struct vty *vty) vty_close(vty); return -1; case BUFFER_EMPTY: + vty->vty_buf_size_written = 0; break; } return 0; diff --git a/lib/vty.h b/lib/vty.h index c336a816cc1f..4be77dabd86b 100644 --- a/lib/vty.h +++ b/lib/vty.h @@ -236,6 +236,7 @@ struct vty { uintptr_t mgmt_req_pending_data; bool mgmt_locked_candidate_ds; bool mgmt_locked_running_ds; + uint64_t vty_buf_size_written; }; static inline void vty_push_context(struct vty *vty, int node, uint64_t id) @@ -338,6 +339,12 @@ struct vty_arg { /* Vty read buffer size. */ #define VTY_READ_BUFSIZ 512 +/* Vty max send buffer size (4096 * 4096). */ +#define SEND_BUF_MAX 16777216 + +/* Vty flush intermediate size (32 * 4096). */ +#define MAX_INTERMEDIATE_FLUSH 131072 + /* Directory separator. */ #ifndef DIRECTORY_SEP #define DIRECTORY_SEP '/' diff --git a/vtysh/vtysh.c b/vtysh/vtysh.c index e657aa8af0ac..bd1bfd8424c2 100644 --- a/vtysh/vtysh.c +++ b/vtysh/vtysh.c @@ -4721,6 +4721,22 @@ static int vtysh_connect(struct vtysh_client *vclient) close(sock); return -1; } + /* Setting the RCV buffer size to 16MB to increase the buffer size + * the socket can hold after receving from other process + */ + uint32_t rcvbufsize = RCV_BUF_MAX; + + ret = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&rcvbufsize, + sizeof(rcvbufsize)); + if (ret < 0) { +#ifdef DEBUG + fprintf(stderr, "Cannot set socket %d rcv buffer size, %s\n", + sock, safe_strerror(errno)); +#endif /* DEBUG */ + close(sock); + return -1; + } + vclient->fd = sock; return 0; diff --git a/vtysh/vtysh.h b/vtysh/vtysh.h index b1d57aa3c221..76d4d766482d 100644 --- a/vtysh/vtysh.h +++ b/vtysh/vtysh.h @@ -35,6 +35,7 @@ extern struct event_loop *master; #define VTYSH_PATHD 0x80000 #define VTYSH_PIM6D 0x100000 #define VTYSH_MGMTD 0x200000 +#define RCV_BUF_MAX 16777216 #define VTYSH_WAS_ACTIVE (-2)