xref: /aosp_15_r20/external/libnl/src/nl-list-caches.c (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2003-2009 Thomas Graf <[email protected]>
4  */
5 
6 #include "nl-default.h"
7 
8 #include <netlink/cli/utils.h>
9 
10 #include "nl-priv-dynamic-core/object-api.h"
11 #include "nl-priv-dynamic-core/cache-api.h"
12 
print_usage(void)13 static void print_usage(void)
14 {
15 	fprintf(stderr, "Usage: nl-list-caches\n");
16 	exit(1);
17 }
18 
id_attr_list(struct nl_object_ops * ops,char * buf,size_t len)19 static char *id_attr_list(struct nl_object_ops *ops, char *buf, size_t len)
20 {
21 	if (ops->oo_attrs2str != NULL)
22 		return ops->oo_attrs2str(ops->oo_id_attrs, buf, len);
23 	else {
24 		memset(buf, 0, len);
25 		return buf;
26 	}
27 }
28 
print(struct nl_cache_ops * ops,void * arg)29 static void print(struct nl_cache_ops *ops, void *arg)
30 {
31 	char buf[64];
32 
33 	printf("%s:\n" \
34 	       "    hdrsize: %d bytes\n" \
35 	       "    protocol: %s\n" \
36 	       "    request-update: %s\n" \
37 	       "    msg-parser: %s\n",
38 	       ops->co_name, ops->co_hdrsize,
39 	       nl_nlfamily2str(ops->co_protocol, buf, sizeof(buf)),
40 	       ops->co_request_update ? "yes" : "no",
41 	       ops->co_msg_parser ? "yes" : "no");
42 
43 	if (ops->co_obj_ops) {
44 		struct nl_object_ops *obj_ops = ops->co_obj_ops;
45 		const char *dump_names[NL_DUMP_MAX+1] = {
46 			"brief",
47 			"detailed",
48 			"stats",
49 		};
50 		int i;
51 
52 		printf("    cacheable object:\n" \
53 		       "        name: %s:\n" \
54 		       "        size: %zu bytes\n" \
55 		       "        constructor: %s\n" \
56 		       "        free-data: %s\n" \
57 		       "        clone: %s\n" \
58 		       "        compare: %s\n" \
59 		       "        id attributes: %s\n" \
60 		       "        dump: ",
61 		       obj_ops->oo_name, obj_ops->oo_size,
62 		       obj_ops->oo_constructor ? "yes" : "no",
63 		       obj_ops->oo_free_data ? "yes" : "no",
64 		       obj_ops->oo_clone ? "yes" : "no",
65 		       obj_ops->oo_compare ? "yes" : "no",
66 		       id_attr_list(obj_ops, buf, sizeof(buf)));
67 
68 		for (i = 0; i <= NL_DUMP_MAX; i++)
69 			if (obj_ops->oo_dump[i])
70 				printf("%s%s",
71 				i == 0 ? "" : ", ",
72 				dump_names[i]);
73 
74 		printf("\n");
75 	}
76 
77 	if (ops->co_genl) {
78 		struct genl_ops *genl_ops = ops->co_genl;
79 
80 		printf("    genl:\n" \
81 		       "        name: %s\n" \
82 		       "        user-hdr: %d\n" \
83 		       "        id: %d\n",
84 		       genl_ops->o_name, genl_ops->o_hdrsize, genl_ops->o_id);
85 
86 		if (genl_ops->o_ncmds) {
87 			int i;
88 
89 			printf("        cmds:\n");
90 
91 			for (i = 0; i < genl_ops->o_ncmds; i++) {
92 				struct genl_cmd *cmd = &genl_ops->o_cmds[i];
93 
94 				printf("            %s:\n"
95 				       "                id: %d\n" \
96 				       "                maxattr: %d\n" \
97 				       "                msg-parser: %s\n" \
98 				       "                attr-policy: %s\n",
99 				       cmd->c_name, cmd->c_id, cmd->c_maxattr,
100 				       cmd->c_msg_parser ? "yes" : "no",
101 				       cmd->c_attr_policy ? "yes" : "no");
102 			}
103 		}
104 	}
105 }
106 
main(int argc,char * argv[])107 int main(int argc, char *argv[])
108 {
109 	if (argc > 1 && !strcasecmp(argv[1], "-h"))
110 		print_usage();
111 
112 	nl_cache_ops_foreach(print, NULL);
113 
114 	return 0;
115 }
116