+/*
+ * 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 void
+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)))
+ {
+ clib_error_report (error);
+ return;
+ }
+
+ 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;
+}
+
+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);
+
+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;
+
+ /* Wait for Godot... */
+ f64 sleep_duration = 10;
+
+ while (1)
+ {
+ do_stat_segment_updates (sm);
+ vlib_process_suspend (vm, sleep_duration);
+ }
+ return 0; /* or not */
+}
+
+static clib_error_t *
+statseg_init (vlib_main_t * vm)
+{
+ stat_segment_main_t *sm = &stat_segment_main;
+ clib_error_t *error;
+
+ /* dependent on unix_input_init */
+ if ((error = vlib_call_init_function (vm, unix_input_init)))
+ return error;
+
+ if (sm->socket_name)
+ stats_segment_socket_init ();
+
+ return 0;
+}
+
+static clib_error_t *
+statseg_config (vlib_main_t * vm, unformat_input_t * input)
+{
+ stat_segment_main_t *sm = &stat_segment_main;
+
+ /* set default socket file name when statseg config stanza is empty. */
+ sm->socket_name = format (0, "%s", STAT_SEGMENT_SOCKET_FILE);
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (input, "socket-name %s", &sm->socket_name))
+ ;
+ else if (unformat (input, "default"))
+ sm->socket_name = format (0, "%s", STAT_SEGMENT_SOCKET_FILE);
+ else
+ if (unformat
+ (input, "size %U", unformat_memory_size, &sm->memory_size))
+ ;
+ else if (unformat (input, "per-node-counters on"))
+ sm->node_counters_enabled = 1;
+ else if (unformat (input, "per-node-counters off"))
+ sm->node_counters_enabled = 0;
+ else
+ return clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, input);
+ }
+ return 0;
+}
+
+VLIB_INIT_FUNCTION (statseg_init);
+VLIB_EARLY_CONFIG_FUNCTION (statseg_config, "statseg");
+
+/* *INDENT-OFF* */
+VLIB_REGISTER_NODE (stat_segment_collector, static) =
+{
+.function = stat_segment_collector_process,
+.name = "statseg-collector-process",
+.type = VLIB_NODE_TYPE_PROCESS,
+};
+
+/* *INDENT-ON* */