net/ice/base: sign external device package programming
[dpdk.git] / lib / telemetry / telemetry.c
index 8f48337..f8b0d11 100644 (file)
@@ -27,6 +27,9 @@
 #define MAX_OUTPUT_LEN (1024 * 16)
 #define MAX_CONNECTIONS 10
 
+/** Maximum number of telemetry callbacks. */
+#define TELEMETRY_MAX_CALLBACKS 64
+
 #ifndef RTE_EXEC_ENV_WINDOWS
 static void *
 client_handler(void *socket);
@@ -104,8 +107,10 @@ list_commands(const char *cmd __rte_unused, const char *params __rte_unused,
        int i;
 
        rte_tel_data_start_array(d, RTE_TEL_STRING_VAL);
+       rte_spinlock_lock(&callback_sl);
        for (i = 0; i < num_callbacks; i++)
                rte_tel_data_add_array_string(d, callbacks[i].cmd);
+       rte_spinlock_unlock(&callback_sl);
        return 0;
 }
 
@@ -350,6 +355,7 @@ socket_listener(void *socket)
 {
        while (1) {
                pthread_t th;
+               int rc;
                struct socket *s = (struct socket *)socket;
                int s_accepted = accept(s->sock, NULL, NULL);
                if (s_accepted < 0) {
@@ -366,7 +372,17 @@ socket_listener(void *socket)
                        __atomic_add_fetch(s->num_clients, 1,
                                        __ATOMIC_RELAXED);
                }
-               pthread_create(&th, NULL, s->fn, (void *)(uintptr_t)s_accepted);
+               rc = pthread_create(&th, NULL, s->fn,
+                                   (void *)(uintptr_t)s_accepted);
+               if (rc != 0) {
+                       TMTY_LOG(ERR, "Error with create client thread: %s\n",
+                                strerror(rc));
+                       close(s_accepted);
+                       if (s->num_clients != NULL)
+                               __atomic_sub_fetch(s->num_clients, 1,
+                                                  __ATOMIC_RELAXED);
+                       continue;
+               }
                pthread_detach(th);
        }
        return NULL;
@@ -421,10 +437,23 @@ error:
        return -1;
 }
 
+static void
+set_thread_name(pthread_t id __rte_unused, const char *name __rte_unused)
+{
+#if defined RTE_EXEC_ENV_LINUX && defined __GLIBC__ && defined __GLIBC_PREREQ
+#if __GLIBC_PREREQ(2, 12)
+       pthread_setname_np(id, name);
+#endif
+#elif defined RTE_EXEC_ENV_FREEBSD
+       pthread_set_name_np(id, name);
+#endif
+}
+
 static int
 telemetry_legacy_init(void)
 {
        pthread_t t_old;
+       int rc;
 
        if (num_legacy_callbacks == 1) {
                TMTY_LOG(WARNING, "No legacy callbacks, legacy socket not created\n");
@@ -440,9 +469,18 @@ telemetry_legacy_init(void)
        v1_socket.sock = create_socket(v1_socket.path);
        if (v1_socket.sock < 0)
                return -1;
-       pthread_create(&t_old, NULL, socket_listener, &v1_socket);
+       rc = pthread_create(&t_old, NULL, socket_listener, &v1_socket);
+       if (rc != 0) {
+               TMTY_LOG(ERR, "Error with create legcay socket thread: %s\n",
+                        strerror(rc));
+               close(v1_socket.sock);
+               v1_socket.sock = -1;
+               unlink(v1_socket.path);
+               v1_socket.path[0] = '\0';
+               return -1;
+       }
        pthread_setaffinity_np(t_old, sizeof(*thread_cpuset), thread_cpuset);
-       pthread_setname_np(t_old, "telemetry-v1");
+       set_thread_name(t_old, "telemetry-v1");
        TMTY_LOG(DEBUG, "Legacy telemetry socket initialized ok\n");
        return 0;
 }
@@ -451,6 +489,7 @@ static int
 telemetry_v2_init(void)
 {
        pthread_t t_new;
+       int rc;
 
        v2_socket.num_clients = &v2_clients;
        rte_telemetry_register_cmd("/", list_commands,
@@ -469,9 +508,18 @@ telemetry_v2_init(void)
        v2_socket.sock = create_socket(v2_socket.path);
        if (v2_socket.sock < 0)
                return -1;
-       pthread_create(&t_new, NULL, socket_listener, &v2_socket);
+       rc = pthread_create(&t_new, NULL, socket_listener, &v2_socket);
+       if (rc != 0) {
+               TMTY_LOG(ERR, "Error with create socket thread: %s\n",
+                        strerror(rc));
+               close(v2_socket.sock);
+               v2_socket.sock = -1;
+               unlink(v2_socket.path);
+               v2_socket.path[0] = '\0';
+               return -1;
+       }
        pthread_setaffinity_np(t_new, sizeof(*thread_cpuset), thread_cpuset);
-       pthread_setname_np(t_new, "telemetry-v2");
+       set_thread_name(t_new, "telemetry-v2");
        atexit(unlink_sockets);
 
        return 0;