On 1/18/2023 4:36 PM, Jakub Kicinski wrote: > Add a CLI sample which can take in arbitrary request > in JSON format, convert it to Netlink and do the inverse > for output. > > It's meant as a development tool primarily and perhaps > for selftests which need to tickle netlink in a special way. > > Signed-off-by: Jakub Kicinski <kuba@xxxxxxxxxx> > --- > tools/net/ynl/samples/cli.py | 47 +++ > tools/net/ynl/samples/ynl.py | 534 +++++++++++++++++++++++++++++++++++ > 2 files changed, 581 insertions(+) > create mode 100755 tools/net/ynl/samples/cli.py > create mode 100644 tools/net/ynl/samples/ynl.py > > diff --git a/tools/net/ynl/samples/cli.py b/tools/net/ynl/samples/cli.py > new file mode 100755 > index 000000000000..b27159c70710 > --- /dev/null > +++ b/tools/net/ynl/samples/cli.py > @@ -0,0 +1,47 @@ > +#!/usr/bin/env python > +# SPDX-License-Identifier: BSD-3-Clause > + > +import argparse > +import json > +import pprint > +import time > + > +from ynl import YnlFamily > + > + > +def main(): > + parser = argparse.ArgumentParser(description='YNL CLI sample') > + parser.add_argument('--spec', dest='spec', type=str, required=True) > + parser.add_argument('--schema', dest='schema', type=str) > + parser.add_argument('--json', dest='json_text', type=str) > + parser.add_argument('--do', dest='do', type=str) > + parser.add_argument('--dump', dest='dump', type=str) > + parser.add_argument('--sleep', dest='sleep', type=int) > + parser.add_argument('--subscribe', dest='ntf', type=str) > + args = parser.parse_args() > + > + attrs = {} > + if args.json_text: > + attrs = json.loads(args.json_text) > + > + ynl = YnlFamily(args.spec, args.schema) > + > + if args.ntf: > + ynl.ntf_subscribe(args.ntf) > + > + if args.sleep: > + time.sleep(args.sleep) > + > + if args.do or args.dump: > + method = getattr(ynl, args.do if args.do else args.dump) > + > + reply = method(attrs, dump=bool(args.dump)) > + pprint.PrettyPrinter().pprint(reply) > + > + if args.ntf: > + ynl.check_ntf() > + pprint.PrettyPrinter().pprint(ynl.async_msg_queue) > + > + > +if __name__ == "__main__": > + main() > diff --git a/tools/net/ynl/samples/ynl.py b/tools/net/ynl/samples/ynl.py > new file mode 100644 > index 000000000000..b71523d71d46 > --- /dev/null > +++ b/tools/net/ynl/samples/ynl.py > @@ -0,0 +1,534 @@ > +# SPDX-License-Identifier: BSD-3-Clause > + > +import functools > +import jsonschema > +import os > +import random > +import socket > +import struct > +import yaml > + > +# > +# Generic Netlink code which should really be in some library, but I can't quickly find one. > +# > + There is pyroute2, but it might be overkill, and I recall it seeming to do some things a bit inconsistently.