Skip to content

Commit

Permalink
Merge pull request #17571 from donaldsharp/fix_bsd_sockopt_problem
Browse files Browse the repository at this point in the history
Fix bsd sockopt problem
  • Loading branch information
ton31337 authored Dec 4, 2024
2 parents 9797f20 + 959dbe2 commit 75e9b1b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
8 changes: 6 additions & 2 deletions lib/sockopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#define HAVE_BSD_STRUCT_IP_MREQ_HACK
#endif

void setsockopt_so_recvbuf(int sock, int size)
int setsockopt_so_recvbuf(int sock, int size)
{
int orig_req = size;

Expand All @@ -34,9 +34,11 @@ void setsockopt_so_recvbuf(int sock, int size)
flog_err(EC_LIB_SOCKET,
"%s: fd %d: SO_RCVBUF set to %d (requested %d)",
__func__, sock, size, orig_req);

return size;
}

void setsockopt_so_sendbuf(const int sock, int size)
int setsockopt_so_sendbuf(const int sock, int size)
{
int orig_req = size;

Expand All @@ -51,6 +53,8 @@ void setsockopt_so_sendbuf(const int sock, int size)
flog_err(EC_LIB_SOCKET,
"%s: fd %d: SO_SNDBUF set to %d (requested %d)",
__func__, sock, size, orig_req);

return size;
}

int getsockopt_so_sendbuf(const int sock)
Expand Down
4 changes: 2 additions & 2 deletions lib/sockopt.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
extern "C" {
#endif

extern void setsockopt_so_recvbuf(int sock, int size);
extern void setsockopt_so_sendbuf(const int sock, int size);
extern int setsockopt_so_recvbuf(int sock, int size);
extern int setsockopt_so_sendbuf(const int sock, int size);
extern int getsockopt_so_sendbuf(const int sock);
extern int getsockopt_so_recvbuf(const int sock);

Expand Down
16 changes: 12 additions & 4 deletions lib/vty.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "northbound_cli.h"
#include "printfrr.h"
#include "json.h"
#include "sockopt.h"

#include <arpa/telnet.h>
#include <termios.h>
Expand Down Expand Up @@ -352,7 +353,7 @@ int vty_out(struct vty *vty, const char *format, ...)
* put the data of collective vty->obuf Linked List items on the
* socket and free the vty->obuf data.
*/
if (vty->vty_buf_size_accumulated >= VTY_MAX_INTERMEDIATE_FLUSH) {
if (vty->vty_buf_size_accumulated >= vty->buf_size_intermediate) {
vty->vty_buf_size_accumulated = 0;
vtysh_flush(vty);
}
Expand Down Expand Up @@ -2157,22 +2158,29 @@ static void vtysh_accept(struct event *thread)
* Increasing the SEND socket buffer size so that the socket can hold
* before sending it to VTY shell.
*/
ret = setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&sndbufsize,
sizeof(sndbufsize));
if (ret < 0) {
ret = setsockopt_so_sendbuf(sock, 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
printf("VTY shell accept\n");
#endif /* VTYSH_DEBUG */

vty = vty_new();

vty->buf_size_set = ret;
if (vty->buf_size_set < VTY_MAX_INTERMEDIATE_FLUSH)
vty->buf_size_intermediate = vty->buf_size_set / 2;
else
vty->buf_size_intermediate = VTY_MAX_INTERMEDIATE_FLUSH;

vty->fd = sock;
vty->wfd = sock;
vty->type = VTY_SHELL_SERV;
Expand Down
3 changes: 3 additions & 0 deletions lib/vty.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ struct vty {
bool mgmt_locked_candidate_ds;
bool mgmt_locked_running_ds;
uint64_t vty_buf_size_accumulated;

int buf_size_set;
uint64_t buf_size_intermediate;
};

static inline void vty_push_context(struct vty *vty, int node, uint64_t id)
Expand Down
6 changes: 3 additions & 3 deletions vtysh/vtysh.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "frrstr.h"
#include "json.h"
#include "ferr.h"
#include "sockopt.h"

DEFINE_MTYPE_STATIC(MVTYSH, VTYSH_CMD, "Vtysh cmd copy");

Expand Down Expand Up @@ -4690,9 +4691,8 @@ static int vtysh_connect(struct vtysh_client *vclient)
* Increasing the RECEIVE socket buffer size so that the socket can hold
* after receving from other process.
*/
ret = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&rcvbufsize,
sizeof(rcvbufsize));
if (ret < 0) {
ret = setsockopt_so_recvbuf(sock, rcvbufsize);
if (ret <= 0) {
#ifdef DEBUG
fprintf(stderr, "Cannot set socket %d rcv buffer size, %s\n",
sock, safe_strerror(errno));
Expand Down

0 comments on commit 75e9b1b

Please sign in to comment.