libnl: /data/src/libnl3/src/nl-list-caches.c Source File

libnl 3.7.0

nl-list-caches.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
4 */
5
6 #include <netlink-private/netlink.h>
7 #include <netlink/cli/utils.h>
8
9 static void print_usage(void)
10 {
11 fprintf(stderr, "Usage: nl-list-caches\n");
12 exit(1);
13 }
14
15 static char *id_attr_list(struct nl_object_ops *ops, char *buf, size_t len)
16 {
17 if (ops->oo_attrs2str != NULL)
18 return ops->oo_attrs2str(ops->oo_id_attrs, buf, len);
19 else {
20 memset(buf, 0, len);
21 return buf;
22 }
23 }
24
25 static void print(struct nl_cache_ops *ops, void *arg)
26 {
27 char buf[64];
28
29 printf("%s:\n" \
30 " hdrsize: %d bytes\n" \
31 " protocol: %s\n" \
32 " request-update: %s\n" \
33 " msg-parser: %s\n",
34 ops->co_name, ops->co_hdrsize,
35 nl_nlfamily2str(ops->co_protocol, buf, sizeof(buf)),
36 ops->co_request_update ? "yes" : "no",
37 ops->co_msg_parser ? "yes" : "no");
38
39 if (ops->co_obj_ops) {
40 struct nl_object_ops *obj_ops = ops->co_obj_ops;
41 const char *dump_names[NL_DUMP_MAX+1] = {
42 "brief",
43 "detailed",
44 "stats",
45 };
46 int i;
47
48 printf(" cacheable object:\n" \
49 " name: %s:\n" \
50 " size: %zu bytes\n" \
51 " constructor: %s\n" \
52 " free-data: %s\n" \
53 " clone: %s\n" \
54 " compare: %s\n" \
55 " id attributes: %s\n" \
56 " dump: ",
57 obj_ops->oo_name, obj_ops->oo_size,
58 obj_ops->oo_constructor ? "yes" : "no",
59 obj_ops->oo_free_data ? "yes" : "no",
60 obj_ops->oo_clone ? "yes" : "no",
61 obj_ops->oo_compare ? "yes" : "no",
62 id_attr_list(obj_ops, buf, sizeof(buf)));
63
64 for (i = 0; i <= NL_DUMP_MAX; i++)
65 if (obj_ops->oo_dump[i])
66 printf("%s%s",
67 i == 0 ? "" : ", ",
68 dump_names[i]);
69
70 printf("\n");
71 }
72
73 if (ops->co_genl) {
74 struct genl_ops *genl_ops = ops->co_genl;
75
76 printf(" genl:\n" \
77 " name: %s\n" \
78 " user-hdr: %d\n" \
79 " id: %d\n",
80 genl_ops->o_name, genl_ops->o_hdrsize, genl_ops->o_id);
81
82 if (genl_ops->o_ncmds) {
83 int i;
84
85 printf(" cmds:\n");
86
87 for (i = 0; i < genl_ops->o_ncmds; i++) {
88 struct genl_cmd *cmd = &genl_ops->o_cmds[i];
89
90 printf(" %s:\n"
91 " id: %d\n" \
92 " maxattr: %d\n" \
93 " msg-parser: %s\n" \
94 " attr-policy: %s\n",
95 cmd->c_name, cmd->c_id, cmd->c_maxattr,
96 cmd->c_msg_parser ? "yes" : "no",
97 cmd->c_attr_policy ? "yes" : "no");
98 }
99 }
100 }
101 }
102
103 int main(int argc, char *argv[])
104 {
105 if (argc > 1 && !strcasecmp(argv[1], "-h"))
106 print_usage();
107
108 nl_cache_ops_foreach(print, NULL);
109
110 return 0;
111 }
nl_cache_ops_foreach
void nl_cache_ops_foreach(void(*cb)(struct nl_cache_ops *, void *), void *arg)
Call a function for each registered cache operation.
Definition: cache_mngt.c:212
genl_cmd
Definition of a Generic Netlink command.
Definition: mngt.h:82
genl_cmd::c_attr_policy
struct nla_policy * c_attr_policy
Attribute validation policy, enforced before the callback is called.
Definition: mngt.h:98
genl_cmd::c_id
int c_id
Numeric command identifier (required)
Definition: mngt.h:84
genl_cmd::c_name
char * c_name
Human readable name (required)
Definition: mngt.h:87
genl_cmd::c_msg_parser
int(* c_msg_parser)(struct nl_cache_ops *, struct genl_cmd *, struct genl_info *, void *)
Called whenever a message for this command is received.
Definition: mngt.h:93
genl_cmd::c_maxattr
int c_maxattr
Maximum attribute identifier that the command is prepared to handle.
Definition: mngt.h:90
genl_ops
Definition of a Generic Netlink family.
Definition: mngt.h:127
genl_ops::o_ncmds
int o_ncmds
Number of elements in o_cmds array.
Definition: mngt.h:147
genl_ops::o_hdrsize
unsigned int o_hdrsize
Length of user header.
Definition: mngt.h:129
genl_ops::o_name
char * o_name
Human readable name, used by genl_ops_resolve() to resolve numeric id.
Definition: mngt.h:135
genl_ops::o_id
int o_id
Numeric identifier, automatically filled in by genl_ops_resolve()
Definition: mngt.h:132
genl_ops::o_cmds
struct genl_cmd * o_cmds
Optional array defining the available Generic Netlink commands.
Definition: mngt.h:144