tls: handle attepts to renegotiate hs
[vpp.git] / src / vppinfra / unix-misc.c
index 2928369..e0591ff 100644 (file)
 
 #include <vppinfra/error.h>
 #include <vppinfra/os.h>
+#include <vppinfra/bitmap.h>
 #include <vppinfra/unix.h>
+#include <vppinfra/format.h>
+#ifdef __linux__
+#include <vppinfra/linux/sysfs.h>
+#endif
 
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/uio.h>           /* writev */
 #include <fcntl.h>
 #include <stdio.h>             /* for sprintf */
+#include <limits.h>
+
+__clib_export __thread uword __os_thread_index = 0;
+__clib_export __thread uword __os_numa_index = 0;
 
 clib_error_t *
-unix_file_n_bytes (char *file, uword * result)
+clib_file_n_bytes (char *file, uword * result)
 {
   struct stat s;
 
@@ -62,7 +71,7 @@ unix_file_n_bytes (char *file, uword * result)
 }
 
 clib_error_t *
-unix_file_read_contents (char *file, u8 * result, uword n_bytes)
+clib_file_read_contents (char *file, u8 * result, uword n_bytes)
 {
   int fd = -1;
   uword n_done, n_left;
@@ -105,20 +114,20 @@ done:
   return error;
 }
 
-clib_error_t *
-unix_file_contents (char *file, u8 ** result)
+__clib_export clib_error_t *
+clib_file_contents (char *file, u8 ** result)
 {
   uword n_bytes;
   clib_error_t *error = 0;
   u8 *v;
 
-  if ((error = unix_file_n_bytes (file, &n_bytes)))
+  if ((error = clib_file_n_bytes (file, &n_bytes)))
     return error;
 
   v = 0;
   vec_resize (v, n_bytes);
 
-  error = unix_file_read_contents (file, v, n_bytes);
+  error = clib_file_read_contents (file, v, n_bytes);
 
   if (error)
     vec_free (v);
@@ -128,6 +137,37 @@ unix_file_contents (char *file, u8 ** result)
   return error;
 }
 
+__clib_export u8 *
+clib_file_get_resolved_basename (char *fmt, ...)
+{
+  va_list va;
+  char *p, buffer[PATH_MAX];
+  u8 *link, *s = 0;
+  int r;
+
+  va_start (va, fmt);
+  link = va_format (0, fmt, &va);
+  va_end (va);
+  vec_add1 (link, 0);
+
+  r = readlink ((char *) link, buffer, sizeof (buffer) - 1);
+  vec_free (link);
+
+  if (r < 1)
+    return 0;
+
+  buffer[r] = 0;
+  p = buffer + r - 1;
+  while (p > buffer && p[-1] != '/')
+    p--;
+
+  while (p[0])
+    vec_add1 (s, p++[0]);
+
+  vec_add1 (s, 0);
+  return s;
+}
+
 clib_error_t *
 unix_proc_file_contents (char *file, u8 ** result)
 {
@@ -155,7 +195,7 @@ unix_proc_file_contents (char *file, u8 ** result)
 
       if (bytes == 0)
        {
-         _vec_len (rv) = pos;
+         vec_set_len (rv, pos);
          break;
        }
       pos += bytes;
@@ -168,7 +208,7 @@ unix_proc_file_contents (char *file, u8 ** result)
 
 void os_panic (void) __attribute__ ((weak));
 
-void
+__clib_export void
 os_panic (void)
 {
   abort ();
@@ -188,14 +228,14 @@ void os_puts (u8 * string, uword string_length, uword is_error)
 void
 os_puts (u8 * string, uword string_length, uword is_error)
 {
-  int cpu = os_get_cpu_number ();
-  int ncpus = os_get_ncpus ();
+  int cpu = os_get_thread_index ();
+  int nthreads = os_get_nthreads ();
   char buf[64];
   int fd = is_error ? 2 : 1;
   struct iovec iovs[2];
   int n_iovs = 0;
 
-  if (ncpus > 1)
+  if (nthreads > 1)
     {
       snprintf (buf, sizeof (buf), "%d: ", cpu);
 
@@ -212,25 +252,79 @@ os_puts (u8 * string, uword string_length, uword is_error)
     ;
 }
 
-void os_out_of_memory (void) __attribute__ ((weak));
-void
+__clib_export __clib_weak void
 os_out_of_memory (void)
 {
   os_panic ();
 }
 
-uword os_get_cpu_number (void) __attribute__ ((weak));
-uword
-os_get_cpu_number (void)
+__clib_export __clib_weak uword
+os_get_nthreads (void)
 {
+  return 1;
+}
+
+__clib_export clib_bitmap_t *
+os_get_online_cpu_core_bitmap ()
+{
+#if __linux__
+  return clib_sysfs_read_bitmap ("/sys/devices/system/cpu/online");
+#else
   return 0;
+#endif
 }
 
-uword os_get_ncpus (void) __attribute__ ((weak));
-uword
-os_get_ncpus (void)
+__clib_export clib_bitmap_t *
+os_get_online_cpu_node_bitmap ()
 {
-  return 1;
+#if __linux__
+  return clib_sysfs_read_bitmap ("/sys/devices/system/node/online");
+#else
+  return 0;
+#endif
+}
+__clib_export clib_bitmap_t *
+os_get_cpu_on_node_bitmap (int node)
+{
+#if __linux__
+  return clib_sysfs_read_bitmap ("/sys/devices/system/node/node%u/cpulist",
+                                node);
+#else
+  return 0;
+#endif
+}
+
+__clib_export clib_bitmap_t *
+os_get_cpu_with_memory_bitmap ()
+{
+#if __linux__
+  return clib_sysfs_read_bitmap ("/sys/devices/system/node/has_memory");
+#else
+  return 0;
+#endif
+}
+
+__clib_export int
+os_get_cpu_phys_core_id (int cpu_id)
+{
+#if __linux
+  int core_id = -1;
+  clib_error_t *err;
+  u8 *p;
+
+  p =
+    format (0, "/sys/devices/system/cpu/cpu%u/topology/core_id%c", cpu_id, 0);
+  err = clib_sysfs_read ((char *) p, "%d", &core_id);
+  vec_free (p);
+  if (err)
+    {
+      clib_error_free (err);
+      return -1;
+    }
+  return core_id;
+#else
+  return -1;
+#endif
 }
 
 /*