]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/dpdk/lib/librte_telemetry/telemetry_json.h
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / spdk / dpdk / lib / librte_telemetry / telemetry_json.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Intel Corporation
3 */
4
5 #ifndef _RTE_TELEMETRY_JSON_H_
6 #define _RTE_TELEMETRY_JSON_H_
7
8 #include <inttypes.h>
9 #include <stdarg.h>
10 #include <stdio.h>
11 #include <rte_common.h>
12
13 /**
14 * @file
15 * Internal Telemetry Utility functions
16 *
17 * This file contains small inline functions to make it easier for applications
18 * to build up valid JSON responses to telemetry requests.
19 *
20 ***/
21
22 /**
23 * @internal
24 * Copies a value into a buffer if the buffer has enough available space.
25 * Nothing written to buffer if an overflow ocurs.
26 * This function is not for use for values larger than 1k.
27 */
28 __rte_format_printf(3, 4)
29 static inline int
30 __json_snprintf(char *buf, const int len, const char *format, ...)
31 {
32 char tmp[1024];
33 va_list ap;
34 int ret;
35
36 va_start(ap, format);
37 ret = vsnprintf(tmp, sizeof(tmp), format, ap);
38 va_end(ap);
39 if (ret > 0 && ret < (int)sizeof(tmp) && ret < len) {
40 strcpy(buf, tmp);
41 return ret;
42 }
43 return 0; /* nothing written or modified */
44 }
45
46 /* Copies an empty array into the provided buffer. */
47 static inline int
48 rte_tel_json_empty_array(char *buf, const int len, const int used)
49 {
50 return used + __json_snprintf(buf + used, len - used, "[]");
51 }
52
53 /* Copies an empty object into the provided buffer. */
54 static inline int
55 rte_tel_json_empty_obj(char *buf, const int len, const int used)
56 {
57 return used + __json_snprintf(buf + used, len - used, "{}");
58 }
59
60 /* Copies a string into the provided buffer, in JSON format. */
61 static inline int
62 rte_tel_json_str(char *buf, const int len, const int used, const char *str)
63 {
64 return used + __json_snprintf(buf + used, len - used, "\"%s\"", str);
65 }
66
67 /* Appends a string into the JSON array in the provided buffer. */
68 static inline int
69 rte_tel_json_add_array_string(char *buf, const int len, const int used,
70 const char *str)
71 {
72 int ret, end = used - 1; /* strip off final delimiter */
73 if (used <= 2) /* assume empty, since minimum is '[]' */
74 return __json_snprintf(buf, len, "[\"%s\"]", str);
75
76 ret = __json_snprintf(buf + end, len - end, ",\"%s\"]", str);
77 return ret == 0 ? used : end + ret;
78 }
79
80 /* Appends an integer into the JSON array in the provided buffer. */
81 static inline int
82 rte_tel_json_add_array_int(char *buf, const int len, const int used, int val)
83 {
84 int ret, end = used - 1; /* strip off final delimiter */
85 if (used <= 2) /* assume empty, since minimum is '[]' */
86 return __json_snprintf(buf, len, "[%d]", val);
87
88 ret = __json_snprintf(buf + end, len - end, ",%d]", val);
89 return ret == 0 ? used : end + ret;
90 }
91
92 /* Appends a uint64_t into the JSON array in the provided buffer. */
93 static inline int
94 rte_tel_json_add_array_u64(char *buf, const int len, const int used,
95 uint64_t val)
96 {
97 int ret, end = used - 1; /* strip off final delimiter */
98 if (used <= 2) /* assume empty, since minimum is '[]' */
99 return __json_snprintf(buf, len, "[%"PRIu64"]", val);
100
101 ret = __json_snprintf(buf + end, len - end, ",%"PRIu64"]", val);
102 return ret == 0 ? used : end + ret;
103 }
104
105 /**
106 * Add a new element with uint64_t value to the JSON object stored in the
107 * provided buffer.
108 */
109 static inline int
110 rte_tel_json_add_obj_u64(char *buf, const int len, const int used,
111 const char *name, uint64_t val)
112 {
113 int ret, end = used - 1;
114 if (used <= 2) /* assume empty, since minimum is '{}' */
115 return __json_snprintf(buf, len, "{\"%s\":%"PRIu64"}", name,
116 val);
117
118 ret = __json_snprintf(buf + end, len - end, ",\"%s\":%"PRIu64"}",
119 name, val);
120 return ret == 0 ? used : end + ret;
121 }
122
123 /**
124 * Add a new element with int value to the JSON object stored in the
125 * provided buffer.
126 */
127 static inline int
128 rte_tel_json_add_obj_int(char *buf, const int len, const int used,
129 const char *name, int val)
130 {
131 int ret, end = used - 1;
132 if (used <= 2) /* assume empty, since minimum is '{}' */
133 return __json_snprintf(buf, len, "{\"%s\":%d}", name,
134 val);
135
136 ret = __json_snprintf(buf + end, len - end, ",\"%s\":%d}",
137 name, val);
138 return ret == 0 ? used : end + ret;
139 }
140
141 /**
142 * Add a new element with string value to the JSON object stored in the
143 * provided buffer.
144 */
145 static inline int
146 rte_tel_json_add_obj_str(char *buf, const int len, const int used,
147 const char *name, const char *val)
148 {
149 int ret, end = used - 1;
150 if (used <= 2) /* assume empty, since minimum is '{}' */
151 return __json_snprintf(buf, len, "{\"%s\":\"%s\"}", name, val);
152
153 ret = __json_snprintf(buf + end, len - end, ",\"%s\":\"%s\"}",
154 name, val);
155 return ret == 0 ? used : end + ret;
156 }
157
158 #endif /*_RTE_TELEMETRY_JSON_H_*/