Skip to content

Commit 7c93a88

Browse files
kuba-moodavem330
authored andcommitted
tools: ynl: allow setting recv() size
Make the size of the buffer we use for recv() configurable. The details of the buffer sizing in netlink are somewhat arcane, we could spend a lot of time polishing this API. Let's just leave some hopefully helpful comments for now. This is a for-developers-only feature, anyway. Signed-off-by: Jakub Kicinski <kuba@kernel.org> Reviewed-by: Donald Hunter <donald.hunter@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 7df7231 commit 7c93a88

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

tools/net/ynl/lib/ynl.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ def __str__(self):
8484
return f"Netlink error: {os.strerror(-self.nl_msg.error)}\n{self.nl_msg}"
8585

8686

87+
class ConfigError(Exception):
88+
pass
89+
90+
8791
class NlAttr:
8892
ScalarFormat = namedtuple('ScalarFormat', ['native', 'big', 'little'])
8993
type_formats = {
@@ -400,7 +404,8 @@ def lookup(self, name):
400404

401405

402406
class YnlFamily(SpecFamily):
403-
def __init__(self, def_path, schema=None, process_unknown=False):
407+
def __init__(self, def_path, schema=None, process_unknown=False,
408+
recv_size=0):
404409
super().__init__(def_path, schema)
405410

406411
self.include_raw = False
@@ -415,6 +420,16 @@ def __init__(self, def_path, schema=None, process_unknown=False):
415420
except KeyError:
416421
raise Exception(f"Family '{self.yaml['name']}' not supported by the kernel")
417422

423+
# Note that netlink will use conservative (min) message size for
424+
# the first dump recv() on the socket, our setting will only matter
425+
# from the second recv() on.
426+
self._recv_size = recv_size if recv_size else 131072
427+
# Netlink will always allocate at least PAGE_SIZE - sizeof(skb_shinfo)
428+
# for a message, so smaller receive sizes will lead to truncation.
429+
# Note that the min size for other families may be larger than 4k!
430+
if self._recv_size < 4000:
431+
raise ConfigError()
432+
418433
self.sock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, self.nlproto.proto_num)
419434
self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_CAP_ACK, 1)
420435
self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_EXT_ACK, 1)
@@ -799,7 +814,7 @@ def handle_ntf(self, decoded):
799814
def check_ntf(self):
800815
while True:
801816
try:
802-
reply = self.sock.recv(128 * 1024, socket.MSG_DONTWAIT)
817+
reply = self.sock.recv(self._recv_size, socket.MSG_DONTWAIT)
803818
except BlockingIOError:
804819
return
805820

@@ -854,7 +869,7 @@ def _op(self, method, vals, flags=None, dump=False):
854869
done = False
855870
rsp = []
856871
while not done:
857-
reply = self.sock.recv(128 * 1024)
872+
reply = self.sock.recv(self._recv_size)
858873
nms = NlMsgs(reply, attr_space=op.attr_set)
859874
for nl_msg in nms:
860875
if nl_msg.extack:

0 commit comments

Comments
 (0)