Commit edf7468d authored by David S. Miller's avatar David S. Miller

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: default avatarDavid S. Miller <davem@davemloft.net>
parents b206acf1 c0111878
...@@ -38,6 +38,8 @@ def main(): ...@@ -38,6 +38,8 @@ def main():
const=Netlink.NLM_F_APPEND) const=Netlink.NLM_F_APPEND)
parser.add_argument('--process-unknown', action=argparse.BooleanOptionalAction) parser.add_argument('--process-unknown', action=argparse.BooleanOptionalAction)
parser.add_argument('--output-json', action='store_true') parser.add_argument('--output-json', action='store_true')
parser.add_argument('--dbg-small-recv', default=0, const=4000,
action='store', nargs='?', type=int)
args = parser.parse_args() args = parser.parse_args()
def output(msg): def output(msg):
...@@ -53,7 +55,10 @@ def main(): ...@@ -53,7 +55,10 @@ def main():
if args.json_text: if args.json_text:
attrs = json.loads(args.json_text) attrs = json.loads(args.json_text)
ynl = YnlFamily(args.spec, args.schema, args.process_unknown) ynl = YnlFamily(args.spec, args.schema, args.process_unknown,
recv_size=args.dbg_small_recv)
if args.dbg_small_recv:
ynl.set_recv_dbg(True)
if args.ntf: if args.ntf:
ynl.ntf_subscribe(args.ntf) ynl.ntf_subscribe(args.ntf)
......
...@@ -7,6 +7,7 @@ import random ...@@ -7,6 +7,7 @@ import random
import socket import socket
import struct import struct
from struct import Struct from struct import Struct
import sys
import yaml import yaml
import ipaddress import ipaddress
import uuid import uuid
...@@ -84,6 +85,10 @@ class NlError(Exception): ...@@ -84,6 +85,10 @@ class NlError(Exception):
return f"Netlink error: {os.strerror(-self.nl_msg.error)}\n{self.nl_msg}" return f"Netlink error: {os.strerror(-self.nl_msg.error)}\n{self.nl_msg}"
class ConfigError(Exception):
pass
class NlAttr: class NlAttr:
ScalarFormat = namedtuple('ScalarFormat', ['native', 'big', 'little']) ScalarFormat = namedtuple('ScalarFormat', ['native', 'big', 'little'])
type_formats = { type_formats = {
...@@ -213,11 +218,11 @@ class NlMsg: ...@@ -213,11 +218,11 @@ class NlMsg:
return self.nl_type return self.nl_type
def __repr__(self): def __repr__(self):
msg = f"nl_len = {self.nl_len} ({len(self.raw)}) nl_flags = 0x{self.nl_flags:x} nl_type = {self.nl_type}\n" msg = f"nl_len = {self.nl_len} ({len(self.raw)}) nl_flags = 0x{self.nl_flags:x} nl_type = {self.nl_type}"
if self.error: if self.error:
msg += '\terror: ' + str(self.error) msg += '\n\terror: ' + str(self.error)
if self.extack: if self.extack:
msg += '\textack: ' + repr(self.extack) msg += '\n\textack: ' + repr(self.extack)
return msg return msg
...@@ -400,7 +405,8 @@ class SpaceAttrs: ...@@ -400,7 +405,8 @@ class SpaceAttrs:
class YnlFamily(SpecFamily): class YnlFamily(SpecFamily):
def __init__(self, def_path, schema=None, process_unknown=False): def __init__(self, def_path, schema=None, process_unknown=False,
recv_size=0):
super().__init__(def_path, schema) super().__init__(def_path, schema)
self.include_raw = False self.include_raw = False
...@@ -415,6 +421,17 @@ class YnlFamily(SpecFamily): ...@@ -415,6 +421,17 @@ class YnlFamily(SpecFamily):
except KeyError: except KeyError:
raise Exception(f"Family '{self.yaml['name']}' not supported by the kernel") raise Exception(f"Family '{self.yaml['name']}' not supported by the kernel")
self._recv_dbg = False
# Note that netlink will use conservative (min) message size for
# the first dump recv() on the socket, our setting will only matter
# from the second recv() on.
self._recv_size = recv_size if recv_size else 131072
# Netlink will always allocate at least PAGE_SIZE - sizeof(skb_shinfo)
# for a message, so smaller receive sizes will lead to truncation.
# Note that the min size for other families may be larger than 4k!
if self._recv_size < 4000:
raise ConfigError()
self.sock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, self.nlproto.proto_num) self.sock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, self.nlproto.proto_num)
self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_CAP_ACK, 1) self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_CAP_ACK, 1)
self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_EXT_ACK, 1) self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_EXT_ACK, 1)
...@@ -438,6 +455,17 @@ class YnlFamily(SpecFamily): ...@@ -438,6 +455,17 @@ class YnlFamily(SpecFamily):
self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_ADD_MEMBERSHIP, self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_ADD_MEMBERSHIP,
mcast_id) mcast_id)
def set_recv_dbg(self, enabled):
self._recv_dbg = enabled
def _recv_dbg_print(self, reply, nl_msgs):
if not self._recv_dbg:
return
print("Recv: read", len(reply), "bytes,",
len(nl_msgs.msgs), "messages", file=sys.stderr)
for nl_msg in nl_msgs:
print(" ", nl_msg, file=sys.stderr)
def _encode_enum(self, attr_spec, value): def _encode_enum(self, attr_spec, value):
enum = self.consts[attr_spec['enum']] enum = self.consts[attr_spec['enum']]
if enum.type == 'flags' or attr_spec.get('enum-as-flags', False): if enum.type == 'flags' or attr_spec.get('enum-as-flags', False):
...@@ -799,11 +827,12 @@ class YnlFamily(SpecFamily): ...@@ -799,11 +827,12 @@ class YnlFamily(SpecFamily):
def check_ntf(self): def check_ntf(self):
while True: while True:
try: try:
reply = self.sock.recv(128 * 1024, socket.MSG_DONTWAIT) reply = self.sock.recv(self._recv_size, socket.MSG_DONTWAIT)
except BlockingIOError: except BlockingIOError:
return return
nms = NlMsgs(reply) nms = NlMsgs(reply)
self._recv_dbg_print(reply, nms)
for nl_msg in nms: for nl_msg in nms:
if nl_msg.error: if nl_msg.error:
print("Netlink error in ntf!?", os.strerror(-nl_msg.error)) print("Netlink error in ntf!?", os.strerror(-nl_msg.error))
...@@ -854,8 +883,9 @@ class YnlFamily(SpecFamily): ...@@ -854,8 +883,9 @@ class YnlFamily(SpecFamily):
done = False done = False
rsp = [] rsp = []
while not done: while not done:
reply = self.sock.recv(128 * 1024) reply = self.sock.recv(self._recv_size)
nms = NlMsgs(reply, attr_space=op.attr_set) nms = NlMsgs(reply, attr_space=op.attr_set)
self._recv_dbg_print(reply, nms)
for nl_msg in nms: for nl_msg in nms:
if nl_msg.extack: if nl_msg.extack:
self._decode_extack(msg, op, nl_msg.extack) self._decode_extack(msg, op, nl_msg.extack)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment