blob: d81916a288dea7cb027d2afc5d307874d8119e4c [file] [log] [blame]
Damjan Marion8973b072022-03-01 15:51:18 +01001/* SPDX-License-Identifier: Apache-2.0
2 * Copyright(c) 2022 Cisco Systems, Inc.
3 */
4
5#include <vlib/vlib.h>
6#include <vlib/unix/unix.h>
7#include <vlib/stats/stats.h>
8
9#define STAT_SEGMENT_SOCKET_FILENAME "stats.sock"
10
11static void
12vector_rate_collector_fn (vlib_stats_collector_data_t *d)
13{
14 vlib_main_t *this_vlib_main;
15 counter_t **counters = d->entry->data;
16 counter_t *cb = counters[0];
17 f64 vector_rate = 0.0;
18 u32 i, n_threads = vlib_get_n_threads ();
19
20 vlib_stats_validate_counter_vector (d->entry_index, n_threads - 1);
21
22 for (i = 0; i < n_threads; i++)
23 {
24 f64 this_vector_rate;
25 this_vlib_main = vlib_get_main_by_index (i);
26
27 this_vector_rate = vlib_internal_node_vector_rate (this_vlib_main);
28 vlib_clear_internal_node_vector_rate (this_vlib_main);
29
30 cb[i] = this_vector_rate;
31 vector_rate += this_vector_rate;
32 }
33
34 /* And set the system average rate */
35 vector_rate /= (f64) (i > 1 ? i - 1 : 1);
36 vlib_stats_set_gauge (d->private_data, vector_rate);
37}
38
39clib_error_t *
40vlib_stats_init (vlib_main_t *vm)
41{
42 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
43 vlib_stats_shared_header_t *shared_header;
44 vlib_stats_collector_reg_t reg = {};
45
46 void *oldheap;
47 uword memory_size, sys_page_sz;
48 int mfd;
49 char *mem_name = "stat segment";
50 void *heap, *memaddr;
51
52 memory_size = sm->memory_size;
53 if (memory_size == 0)
54 memory_size = STAT_SEGMENT_DEFAULT_SIZE;
55
56 if (sm->log2_page_sz == CLIB_MEM_PAGE_SZ_UNKNOWN)
57 sm->log2_page_sz = CLIB_MEM_PAGE_SZ_DEFAULT;
58
59 mfd = clib_mem_vm_create_fd (sm->log2_page_sz, mem_name);
60
61 if (mfd == -1)
62 return clib_error_return (0, "stat segment memory fd failure: %U",
63 format_clib_error, clib_mem_get_last_error ());
64 /* Set size */
65 if ((ftruncate (mfd, memory_size)) == -1)
66 {
67 close (mfd);
68 return clib_error_return (0, "stat segment ftruncate failure");
69 }
70
71 memaddr = clib_mem_vm_map_shared (0, memory_size, mfd, 0, mem_name);
72
73 if (memaddr == CLIB_MEM_VM_MAP_FAILED)
74 return clib_error_return (0, "stat segment mmap failure");
75
76 sys_page_sz = clib_mem_get_page_size ();
77
78 heap =
79 clib_mem_create_heap (((u8 *) memaddr) + sys_page_sz,
80 memory_size - sys_page_sz, 1 /* locked */, mem_name);
81 sm->heap = heap;
82 sm->memfd = mfd;
83
84 sm->directory_vector_by_name = hash_create_string (0, sizeof (uword));
85 sm->shared_header = shared_header = memaddr;
86
87 shared_header->version = STAT_SEGMENT_VERSION;
88 shared_header->base = memaddr;
89
90 sm->stat_segment_lockp = clib_mem_alloc (sizeof (clib_spinlock_t));
91 clib_spinlock_init (sm->stat_segment_lockp);
92
93 oldheap = clib_mem_set_heap (sm->heap);
94
95 /* Set up the name to counter-vector hash table */
96 sm->directory_vector = 0;
97
98 shared_header->epoch = 1;
99
100 /* Scalar stats and node counters */
101 vec_validate (sm->directory_vector, STAT_COUNTERS - 1);
102#define _(E, t, n, p) \
103 strcpy (sm->directory_vector[STAT_COUNTER_##E].name, p "/" #n); \
104 sm->directory_vector[STAT_COUNTER_##E].type = STAT_DIR_TYPE_##t;
105 foreach_stat_segment_counter_name
106#undef _
107 /* Save the vector in the shared segment, for clients */
108 shared_header->directory_vector = sm->directory_vector;
109
110 clib_mem_set_heap (oldheap);
111
112 vlib_stats_register_mem_heap (heap);
113
114 reg.collect_fn = vector_rate_collector_fn;
115 reg.private_data = vlib_stats_add_gauge ("/sys/vector_rate");
116 reg.entry_index =
117 vlib_stats_add_counter_vector ("/sys/vector_rate_per_worker");
118 vlib_stats_register_collector_fn (&reg);
119 vlib_stats_validate_counter_vector (reg.entry_index, vlib_get_n_threads ());
120
121 return 0;
122}
123
124static clib_error_t *
125statseg_config (vlib_main_t *vm, unformat_input_t *input)
126{
127 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
128 sm->update_interval = 10.0;
129
130 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
131 {
132 if (unformat (input, "socket-name %s", &sm->socket_name))
133 ;
134 /* DEPRECATE: default (does nothing) */
135 else if (unformat (input, "default"))
136 ;
137 else if (unformat (input, "size %U", unformat_memory_size,
138 &sm->memory_size))
139 ;
140 else if (unformat (input, "page-size %U", unformat_log2_page_size,
141 &sm->log2_page_sz))
142 ;
143 else if (unformat (input, "per-node-counters on"))
144 sm->node_counters_enabled = 1;
145 else if (unformat (input, "per-node-counters off"))
146 sm->node_counters_enabled = 0;
147 else if (unformat (input, "update-interval %f", &sm->update_interval))
148 ;
149 else
150 return clib_error_return (0, "unknown input `%U'",
151 format_unformat_error, input);
152 }
153
154 /*
155 * NULL-terminate socket name string
156 * clib_socket_init()->socket_config() use C str*
157 */
158 if (vec_len (sm->socket_name))
159 vec_terminate_c_string (sm->socket_name);
160
161 return 0;
162}
163
164VLIB_EARLY_CONFIG_FUNCTION (statseg_config, "statseg");
165
166/*
167 * Accept connection on the socket and exchange the fd for the shared
168 * memory segment.
169 */
170static clib_error_t *
171stats_socket_accept_ready (clib_file_t *uf)
172{
173 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
174 clib_error_t *err;
175 clib_socket_t client = { 0 };
176
177 err = clib_socket_accept (sm->socket, &client);
178 if (err)
179 {
180 clib_error_report (err);
181 return err;
182 }
183
184 /* Send the fd across and close */
185 err = clib_socket_sendmsg (&client, 0, 0, &sm->memfd, 1);
186 if (err)
187 clib_error_report (err);
188 clib_socket_close (&client);
189
190 return 0;
191}
192
193static clib_error_t *
194stats_segment_socket_init (void)
195{
196 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
197 clib_error_t *error;
198 clib_socket_t *s = clib_mem_alloc (sizeof (clib_socket_t));
199
200 memset (s, 0, sizeof (clib_socket_t));
201 s->config = (char *) sm->socket_name;
202 s->flags = CLIB_SOCKET_F_IS_SERVER | CLIB_SOCKET_F_SEQPACKET |
203 CLIB_SOCKET_F_ALLOW_GROUP_WRITE | CLIB_SOCKET_F_PASSCRED;
204
205 if ((error = clib_socket_init (s)))
206 return error;
207
208 clib_file_t template = { 0 };
209 template.read_function = stats_socket_accept_ready;
210 template.file_descriptor = s->fd;
211 template.description = format (0, "stats segment listener %s", s->config);
212 clib_file_add (&file_main, &template);
213
214 sm->socket = s;
215
216 return 0;
217}
218
219static clib_error_t *
220stats_segment_socket_exit (vlib_main_t *vm)
221{
222 /*
223 * cleanup the listener socket on exit.
224 */
225 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
226 unlink ((char *) sm->socket_name);
227 return 0;
228}
229
230VLIB_MAIN_LOOP_EXIT_FUNCTION (stats_segment_socket_exit);
231
232static clib_error_t *
233statseg_init (vlib_main_t *vm)
234{
235 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
236
237 /* set default socket file name when statseg config stanza is empty. */
238 if (!vec_len (sm->socket_name))
239 sm->socket_name = format (0, "%s/%s%c", vlib_unix_get_runtime_dir (),
240 STAT_SEGMENT_SOCKET_FILENAME, 0);
241 return stats_segment_socket_init ();
242}
243
244VLIB_INIT_FUNCTION (statseg_init) = {
245 .runs_after = VLIB_INITS ("unix_input_init", "linux_epoll_input_init"),
246};