Skip to content

Commit edf7468

Browse files
committed
Merge branch 'ynl-small-recv'
Jakub Kicinski says: ==================== tools: ynl: add --dbg-small-recv for easier kernel testing When testing netlink dumps I usually hack some user space up to constrain its user space buffer size (iproute2, ethtool or ynl). Netlink will try to fill the messages up, so since these apps use large buffers by default, the dumps are rarely fragmented. I was hoping to figure out a way to create a selftest for dump testing, but so far I have no idea how to do that in a useful and generic way. Until someone does that, make manual dump testing easier with YNL. Create a special option for limiting the buffer size, so I don't have to make the same edits each time, and maybe others will benefit, too :) Example: $ ./cli.py [...] --dbg-small-recv >/dev/null Recv: read 3712 bytes, 29 messages nl_len = 128 (112) nl_flags = 0x0 nl_type = 19 [...] nl_len = 128 (112) nl_flags = 0x0 nl_type = 19 Recv: read 3968 bytes, 31 messages nl_len = 128 (112) nl_flags = 0x0 nl_type = 19 [...] nl_len = 128 (112) nl_flags = 0x0 nl_type = 19 Recv: read 532 bytes, 5 messages nl_len = 128 (112) nl_flags = 0x0 nl_type = 19 [...] nl_len = 128 (112) nl_flags = 0x0 nl_type = 19 nl_len = 20 (4) nl_flags = 0x2 nl_type = 3 Now let's make the DONE not fit in the last message: $ ./cli.py [...] --dbg-small-recv 4499 >/dev/null Recv: read 3712 bytes, 29 messages nl_len = 128 (112) nl_flags = 0x0 nl_type = 19 [...] nl_len = 128 (112) nl_flags = 0x0 nl_type = 19 Recv: read 4480 bytes, 35 messages nl_len = 128 (112) nl_flags = 0x0 nl_type = 19 [...] nl_len = 128 (112) nl_flags = 0x0 nl_type = 19 Recv: read 20 bytes, 1 messages nl_len = 20 (4) nl_flags = 0x2 nl_type = 3 A real test would also have to check the messages are complete and not duplicated. That part has to be done manually right now. Note that the first message is always conservatively sized by the kernel. Still, I think this is good enough to be useful. v2: - patch 2: - move the recv_size setting up - change the default to 0 so that cli.py doesn't have to worry what the "unset" value is v1: https://lore.kernel.org/all/20240301230542.116823-1-kuba@kernel.org/ ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents b206acf + c011187 commit edf7468

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

tools/net/ynl/cli.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ def main():
3838
const=Netlink.NLM_F_APPEND)
3939
parser.add_argument('--process-unknown', action=argparse.BooleanOptionalAction)
4040
parser.add_argument('--output-json', action='store_true')
41+
parser.add_argument('--dbg-small-recv', default=0, const=4000,
42+
action='store', nargs='?', type=int)
4143
args = parser.parse_args()
4244

4345
def output(msg):
@@ -53,7 +55,10 @@ def output(msg):
5355
if args.json_text:
5456
attrs = json.loads(args.json_text)
5557

56-
ynl = YnlFamily(args.spec, args.schema, args.process_unknown)
58+
ynl = YnlFamily(args.spec, args.schema, args.process_unknown,
59+
recv_size=args.dbg_small_recv)
60+
if args.dbg_small_recv:
61+
ynl.set_recv_dbg(True)
5762

5863
if args.ntf:
5964
ynl.ntf_subscribe(args.ntf)

tools/net/ynl/lib/ynl.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import socket
88
import struct
99
from struct import Struct
10+
import sys
1011
import yaml
1112
import ipaddress
1213
import uuid
@@ -84,6 +85,10 @@ def __str__(self):
8485
return f"Netlink error: {os.strerror(-self.nl_msg.error)}\n{self.nl_msg}"
8586

8687

88+
class ConfigError(Exception):
89+
pass
90+
91+
8792
class NlAttr:
8893
ScalarFormat = namedtuple('ScalarFormat', ['native', 'big', 'little'])
8994
type_formats = {
@@ -213,11 +218,11 @@ def cmd(self):
213218
return self.nl_type
214219

215220
def __repr__(self):
216-
msg = f"nl_len = {self.nl_len} ({len(self.raw)}) nl_flags = 0x{self.nl_flags:x} nl_type = {self.nl_type}\n"
221+
msg = f"nl_len = {self.nl_len} ({len(self.raw)}) nl_flags = 0x{self.nl_flags:x} nl_type = {self.nl_type}"
217222
if self.error:
218-
msg += '\terror: ' + str(self.error)
223+
msg += '\n\terror: ' + str(self.error)
219224
if self.extack:
220-
msg += '\textack: ' + repr(self.extack)
225+
msg += '\n\textack: ' + repr(self.extack)
221226
return msg
222227

223228

@@ -400,7 +405,8 @@ def lookup(self, name):
400405

401406

402407
class YnlFamily(SpecFamily):
403-
def __init__(self, def_path, schema=None, process_unknown=False):
408+
def __init__(self, def_path, schema=None, process_unknown=False,
409+
recv_size=0):
404410
super().__init__(def_path, schema)
405411

406412
self.include_raw = False
@@ -415,6 +421,17 @@ def __init__(self, def_path, schema=None, process_unknown=False):
415421
except KeyError:
416422
raise Exception(f"Family '{self.yaml['name']}' not supported by the kernel")
417423

424+
self._recv_dbg = False
425+
# Note that netlink will use conservative (min) message size for
426+
# the first dump recv() on the socket, our setting will only matter
427+
# from the second recv() on.
428+
self._recv_size = recv_size if recv_size else 131072
429+
# Netlink will always allocate at least PAGE_SIZE - sizeof(skb_shinfo)
430+
# for a message, so smaller receive sizes will lead to truncation.
431+
# Note that the min size for other families may be larger than 4k!
432+
if self._recv_size < 4000:
433+
raise ConfigError()
434+
418435
self.sock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, self.nlproto.proto_num)
419436
self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_CAP_ACK, 1)
420437
self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_EXT_ACK, 1)
@@ -438,6 +455,17 @@ def ntf_subscribe(self, mcast_name):
438455
self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_ADD_MEMBERSHIP,
439456
mcast_id)
440457

458+
def set_recv_dbg(self, enabled):
459+
self._recv_dbg = enabled
460+
461+
def _recv_dbg_print(self, reply, nl_msgs):
462+
if not self._recv_dbg:
463+
return
464+
print("Recv: read", len(reply), "bytes,",
465+
len(nl_msgs.msgs), "messages", file=sys.stderr)
466+
for nl_msg in nl_msgs:
467+
print(" ", nl_msg, file=sys.stderr)
468+
441469
def _encode_enum(self, attr_spec, value):
442470
enum = self.consts[attr_spec['enum']]
443471
if enum.type == 'flags' or attr_spec.get('enum-as-flags', False):
@@ -799,11 +827,12 @@ def handle_ntf(self, decoded):
799827
def check_ntf(self):
800828
while True:
801829
try:
802-
reply = self.sock.recv(128 * 1024, socket.MSG_DONTWAIT)
830+
reply = self.sock.recv(self._recv_size, socket.MSG_DONTWAIT)
803831
except BlockingIOError:
804832
return
805833

806834
nms = NlMsgs(reply)
835+
self._recv_dbg_print(reply, nms)
807836
for nl_msg in nms:
808837
if nl_msg.error:
809838
print("Netlink error in ntf!?", os.strerror(-nl_msg.error))
@@ -854,8 +883,9 @@ def _op(self, method, vals, flags=None, dump=False):
854883
done = False
855884
rsp = []
856885
while not done:
857-
reply = self.sock.recv(128 * 1024)
886+
reply = self.sock.recv(self._recv_size)
858887
nms = NlMsgs(reply, attr_space=op.attr_set)
888+
self._recv_dbg_print(reply, nms)
859889
for nl_msg in nms:
860890
if nl_msg.extack:
861891
self._decode_extack(msg, op, nl_msg.extack)

0 commit comments

Comments
 (0)