Skip to content

Commit

Permalink
Add _peek_bufsize to NetlinkSocketBase so recv can use it
Browse files Browse the repository at this point in the history
NetlinkSocketBase.recv uses DEFAULT_RCVBUF to fetch things like ip links
via netlink sockets. It happed in the last cycles that with new NICs
with higher VF numbers or similar we have to change the buffer size
(see svinota#751 or svinota#813).
A better solution (thanks to one or our experts) can be to check the
necessary buffer size before receiving the sockets.

Fixes svinota#1044

Change-Id: I87c75e4d424653e5a29408b3ac2ba8504cb2db49
  • Loading branch information
elajkat committed Oct 18, 2022
1 parent 86a0af0 commit 4431f33
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pyroute2/netlink/nlsocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
import traceback
import warnings
from functools import partial
from socket import MSG_PEEK, SO_RCVBUF, SO_SNDBUF, SOCK_DGRAM, SOL_SOCKET
from socket import MSG_PEEK, MSG_TRUNC, MSG_DONTWAIT, SO_RCVBUF, SO_SNDBUF, SOCK_DGRAM, SOL_SOCKET

from pyroute2 import config
from pyroute2.common import DEFAULT_RCVBUF, AddrPool
Expand Down Expand Up @@ -1002,6 +1002,12 @@ def get_policy_map(self, policy=None):

return ret

def _peek_bufsize(self, socket_descriptor):
data = bytearray()
bufsize, _ = socket_descriptor.recvfrom_into(
data, 0, MSG_DONTWAIT | MSG_PEEK | MSG_TRUNC)
return bufsize

def sendto(self, *argv, **kwarg):
return self._sendto(*argv, **kwarg)

Expand All @@ -1011,7 +1017,7 @@ def recv(self, *argv, **kwarg):
if isinstance(data_in, Exception):
raise data_in
return data_in
return self._sock.recv(*argv, **kwarg)
return self._sock.recv(self._peek_bufsize(self._sock), **kwarg)

def recv_into(self, data, *argv, **kwarg):
if self.input_from_buffer_queue:
Expand Down

0 comments on commit 4431f33

Please sign in to comment.