+ sm->directory_vector[STAT_COUNTER_HEARTBEAT].value++;
+}
+
+/*
+ * Accept connection on the socket and exchange the fd for the shared
+ * memory segment.
+ */
+static clib_error_t *
+stats_socket_accept_ready (clib_file_t * uf)
+{
+ stat_segment_main_t *sm = &stat_segment_main;
+ clib_error_t *err;
+ clib_socket_t client = { 0 };
+
+ err = clib_socket_accept (sm->socket, &client);
+ if (err)
+ {
+ clib_error_report (err);
+ return err;
+ }
+
+ /* Send the fd across and close */
+ err = clib_socket_sendmsg (&client, 0, 0, &sm->memfd, 1);
+ if (err)
+ clib_error_report (err);
+ clib_socket_close (&client);
+
+ return 0;
+}
+
+static clib_error_t *
+stats_segment_socket_init (void)
+{
+ stat_segment_main_t *sm = &stat_segment_main;
+ clib_error_t *error;
+ clib_socket_t *s = clib_mem_alloc (sizeof (clib_socket_t));
+
+ memset (s, 0, sizeof (clib_socket_t));
+ s->config = (char *) sm->socket_name;
+ s->flags = CLIB_SOCKET_F_IS_SERVER | CLIB_SOCKET_F_SEQPACKET |
+ CLIB_SOCKET_F_ALLOW_GROUP_WRITE | CLIB_SOCKET_F_PASSCRED;
+
+ if ((error = clib_socket_init (s)))
+ return error;
+
+ clib_file_t template = { 0 };
+ template.read_function = stats_socket_accept_ready;
+ template.file_descriptor = s->fd;
+ template.description = format (0, "stats segment listener %s", s->config);
+ clib_file_add (&file_main, &template);
+
+ sm->socket = s;
+
+ return 0;
+}
+
+static clib_error_t *
+stats_segment_socket_exit (vlib_main_t * vm)
+{
+ /*
+ * cleanup the listener socket on exit.
+ */
+ stat_segment_main_t *sm = &stat_segment_main;
+ unlink ((char *) sm->socket_name);
+ return 0;
+}
+
+VLIB_MAIN_LOOP_EXIT_FUNCTION (stats_segment_socket_exit);
+
+/* Overrides weak reference in vlib:node_cli.c */
+f64
+vlib_get_stat_segment_update_rate (void)
+{
+ return stat_segment_main.update_interval;
+}
+
+static uword
+stat_segment_collector_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
+ vlib_frame_t * f)
+{
+ stat_segment_main_t *sm = &stat_segment_main;
+
+ while (1)
+ {
+ do_stat_segment_updates (sm);
+ vlib_process_suspend (vm, sm->update_interval);
+ }
+ return 0; /* or not */
+}
+
+static clib_error_t *
+statseg_init (vlib_main_t * vm)
+{
+ stat_segment_main_t *sm = &stat_segment_main;
+
+ /* set default socket file name when statseg config stanza is empty. */
+ if (!vec_len (sm->socket_name))
+ sm->socket_name = format (0, "%s/%s%c", vlib_unix_get_runtime_dir (),
+ STAT_SEGMENT_SOCKET_FILENAME, 0);
+ return stats_segment_socket_init ();
+}
+
+/* *INDENT-OFF* */
+VLIB_INIT_FUNCTION (statseg_init) =
+{
+ .runs_after = VLIB_INITS("unix_input_init"),
+};
+/* *INDENT-ON* */
+
+clib_error_t *
+stat_segment_register_gauge (u8 * name, stat_segment_update_fn update_fn,
+ u32 caller_index)
+{
+ stat_segment_main_t *sm = &stat_segment_main;
+ stat_segment_shared_header_t *shared_header = sm->shared_header;
+ void *oldheap;
+ stat_segment_directory_entry_t e;
+ stat_segment_gauges_pool_t *gauge;
+
+ ASSERT (shared_header);
+
+ u32 vector_index = lookup_hash_index (name);
+
+ if (vector_index != STAT_SEGMENT_INDEX_INVALID) /* Already registered */
+ return clib_error_return (0, "%v is already registered", name);
+
+ memset (&e, 0, sizeof (e));
+ e.type = STAT_DIR_TYPE_SCALAR_INDEX;
+ memcpy (e.name, name, vec_len (name));
+
+ oldheap = vlib_stats_push_heap (NULL);
+ vlib_stat_segment_lock ();
+ vector_index = vlib_stats_create_counter (&e, oldheap);
+
+ shared_header->directory_offset =
+ stat_segment_offset (shared_header, sm->directory_vector);
+
+ vlib_stat_segment_unlock ();
+ clib_mem_set_heap (oldheap);
+
+ /* Back on our own heap */
+ pool_get (sm->gauges, gauge);
+ gauge->fn = update_fn;
+ gauge->caller_index = caller_index;
+ gauge->directory_index = vector_index;
+
+ return NULL;
+}
+
+clib_error_t *
+stat_segment_register_state_counter (u8 * name, u32 * index)
+{
+ stat_segment_main_t *sm = &stat_segment_main;
+ stat_segment_shared_header_t *shared_header = sm->shared_header;
+ void *oldheap;
+ stat_segment_directory_entry_t e;
+
+ ASSERT (shared_header);
+ ASSERT (vlib_get_thread_index () == 0);
+
+ u32 vector_index = lookup_hash_index (name);
+
+ if (vector_index != STAT_SEGMENT_INDEX_INVALID) /* Already registered */
+ return clib_error_return (0, "%v is already registered", name);
+
+ memset (&e, 0, sizeof (e));
+ e.type = STAT_DIR_TYPE_SCALAR_INDEX;
+ memcpy (e.name, name, vec_len (name));
+
+ oldheap = vlib_stats_push_heap (NULL);
+ vlib_stat_segment_lock ();
+
+ vector_index = vlib_stats_create_counter (&e, oldheap);
+
+ shared_header->directory_offset =
+ stat_segment_offset (shared_header, sm->directory_vector);
+
+ vlib_stat_segment_unlock ();
+ clib_mem_set_heap (oldheap);
+
+ *index = vector_index;
+ return 0;
+}
+
+clib_error_t *
+stat_segment_deregister_state_counter (u32 index)
+{
+ stat_segment_main_t *sm = &stat_segment_main;
+ stat_segment_shared_header_t *shared_header = sm->shared_header;
+ stat_segment_directory_entry_t *e;
+ void *oldheap;
+
+ ASSERT (shared_header);
+
+ if (index > vec_len (sm->directory_vector))
+ return clib_error_return (0, "%u index does not exist", index);
+
+ e = &sm->directory_vector[index];
+ if (e->type != STAT_DIR_TYPE_SCALAR_INDEX)
+ return clib_error_return (0, "%u index cannot be deleted", index);
+
+ oldheap = vlib_stats_push_heap (NULL);
+ vlib_stat_segment_lock ();
+
+ vlib_stats_delete_counter (index, oldheap);
+
+ vlib_stat_segment_unlock ();
+ clib_mem_set_heap (oldheap);
+
+ return 0;
+}
+
+void
+stat_segment_set_state_counter (u32 index, u64 value)
+{
+ stat_segment_main_t *sm = &stat_segment_main;
+
+ ASSERT (index < vec_len (sm->directory_vector));
+ sm->directory_vector[index].index = value;