xref: /linux/tools/bpf/bpftool/netlink_dumper.h (revision f6f3bac08ff9855d803081a353a1fafaa8845739)
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (C) 2018 Facebook
3 
4 #ifndef _NETLINK_DUMPER_H_
5 #define _NETLINK_DUMPER_H_
6 
7 #define NET_START_OBJECT				\
8 {							\
9 	if (json_output)				\
10 		jsonw_start_object(json_wtr);		\
11 }
12 
13 #define NET_START_OBJECT_NESTED(name)			\
14 {							\
15 	if (json_output) {				\
16 		jsonw_name(json_wtr, name);		\
17 		jsonw_start_object(json_wtr);		\
18 	} else {					\
19 		fprintf(stderr, "%s {", name);		\
20 	}						\
21 }
22 
23 #define NET_START_OBJECT_NESTED2			\
24 {							\
25 	if (json_output)				\
26 		jsonw_start_object(json_wtr);		\
27 	else						\
28 		fprintf(stderr, "{");			\
29 }
30 
31 #define NET_END_OBJECT_NESTED				\
32 {							\
33 	if (json_output)				\
34 		jsonw_end_object(json_wtr);		\
35 	else						\
36 		fprintf(stderr, "}");			\
37 }
38 
39 #define NET_END_OBJECT					\
40 {							\
41 	if (json_output)				\
42 		jsonw_end_object(json_wtr);		\
43 }
44 
45 #define NET_END_OBJECT_FINAL				\
46 {							\
47 	if (json_output)				\
48 		jsonw_end_object(json_wtr);		\
49 	else						\
50 		fprintf(stderr, "\n");			\
51 }
52 
53 #define NET_START_ARRAY(name, newline)			\
54 {							\
55 	if (json_output) {				\
56 		jsonw_name(json_wtr, name);		\
57 		jsonw_start_array(json_wtr);		\
58 	} else {					\
59 		fprintf(stderr, "%s [%s", name, newline);\
60 	}						\
61 }
62 
63 #define NET_END_ARRAY(endstr)				\
64 {							\
65 	if (json_output)				\
66 		jsonw_end_array(json_wtr);		\
67 	else						\
68 		fprintf(stderr, "]%s", endstr);		\
69 }
70 
71 #define NET_DUMP_UINT(name, val)			\
72 {							\
73 	if (json_output)				\
74 		jsonw_uint_field(json_wtr, name, val);	\
75 	else						\
76 		fprintf(stderr, "%s %d ", name, val);	\
77 }
78 
79 #define NET_DUMP_LLUINT(name, val)			\
80 {							\
81 	if (json_output)				\
82 		jsonw_lluint_field(json_wtr, name, val);\
83 	else						\
84 		fprintf(stderr, "%s %lld ", name, val);	\
85 }
86 
87 #define NET_DUMP_STR(name, str)				\
88 {							\
89 	if (json_output)				\
90 		jsonw_string_field(json_wtr, name, str);\
91 	else						\
92 		fprintf(stderr, "%s %s ", name, str);	\
93 }
94 
95 #define NET_DUMP_STR_ONLY(str)				\
96 {							\
97 	if (json_output)				\
98 		jsonw_string(json_wtr, str);		\
99 	else						\
100 		fprintf(stderr, "%s ", str);		\
101 }
102 
103 #endif
104