dpdk: remove rte_mbuf modifications at many places in the code
[vpp.git] / vlib / vlib / unix / util.c
index 7a3a2bf..edc3e59 100644 (file)
 #include <dirent.h>
 
 clib_error_t *
-foreach_directory_file (char * dir_name,
-                       clib_error_t * (* f) (void * arg, u8 * path_name, u8 * file_name),
-                       void * arg,
+foreach_directory_file (char *dir_name,
+                       clib_error_t * (*f) (void *arg, u8 * path_name,
+                                            u8 * file_name), void *arg,
                        int scan_dirs)
 {
-  DIR * d;
-  struct dirent * e;
-  clib_error_t * error = 0;
-  u8 * s, * t;
+  DIR *d;
+  struct dirent *e;
+  clib_error_t *error = 0;
+  u8 *s, *t;
 
   d = opendir (dir_name);
-  if (! d)
+  if (!d)
     {
       if (errno == ENOENT)
-        return 0;
+       return 0;
       return clib_error_return_unix (0, "open `%s'", dir_name);
     }
 
@@ -68,13 +68,12 @@ foreach_directory_file (char * dir_name,
   while (1)
     {
       e = readdir (d);
-      if (! e)
+      if (!e)
        break;
       if (scan_dirs)
        {
          if (e->d_type == DT_DIR
-             && (! strcmp (e->d_name, ".")
-                 || ! strcmp (e->d_name, "..")))
+             && (!strcmp (e->d_name, ".") || !strcmp (e->d_name, "..")))
            continue;
        }
       else
@@ -100,10 +99,11 @@ foreach_directory_file (char * dir_name,
 }
 
 clib_error_t *
-vlib_sysfs_write (char * file_name, char * fmt, ...)
+vlib_sysfs_write (char *file_name, char *fmt, ...)
 {
-  u8 * s;
+  u8 *s;
   int fd;
+  clib_error_t *error = 0;
 
   fd = open (file_name, O_WRONLY);
   if (fd < 0)
@@ -115,18 +115,18 @@ vlib_sysfs_write (char * file_name, char * fmt, ...)
   va_end (va);
 
   if (write (fd, s, vec_len (s)) < 0)
-    return clib_error_return_unix (0, "write `%s'", file_name);
+    error = clib_error_return_unix (0, "write `%s'", file_name);
 
   vec_free (s);
   close (fd);
-  return 0;
+  return error;
 }
 
 clib_error_t *
-vlib_sysfs_read (char * file_name, char * fmt, ...)
+vlib_sysfs_read (char *file_name, char *fmt, ...)
 {
   unformat_input_t input;
-  u8 * s = 0;
+  u8 *s = 0;
   int fd;
   ssize_t sz;
   uword result;
@@ -135,18 +135,18 @@ vlib_sysfs_read (char * file_name, char * fmt, ...)
   if (fd < 0)
     return clib_error_return_unix (0, "open `%s'", file_name);
 
-  vec_validate(s, 4095);
+  vec_validate (s, 4095);
 
-  sz = read(fd, s, vec_len (s));
+  sz = read (fd, s, vec_len (s));
   if (sz < 0)
     {
-      close(fd);
-      vec_free(s);
+      close (fd);
+      vec_free (s);
       return clib_error_return_unix (0, "read `%s'", file_name);
     }
 
-  _vec_len(s) = sz;
-  unformat_init_vector(&input, s);
+  _vec_len (s) = sz;
+  unformat_init_vector (&input, s);
 
   va_list va;
   va_start (va, fmt);
@@ -163,27 +163,69 @@ vlib_sysfs_read (char * file_name, char * fmt, ...)
 }
 
 u8 *
-vlib_sysfs_link_to_name(char * link)
+vlib_sysfs_link_to_name (char *link)
 {
   char *p, buffer[64];
   unformat_input_t in;
   u8 *s = 0;
   int r;
 
-  r = readlink(link, buffer, sizeof(buffer) - 1);
+  r = readlink (link, buffer, sizeof (buffer) - 1);
 
   if (r < 0)
     return 0;
 
   buffer[r] = 0;
-  p = strrchr(buffer, '/');
+  p = strrchr (buffer, '/');
 
   if (!p)
     return 0;
 
-  unformat_init_string (&in, p+1, strlen (p+1));
-  unformat(&in, "%s", &s);
+  unformat_init_string (&in, p + 1, strlen (p + 1));
+  if (unformat (&in, "%s", &s) != 1)
+    clib_unix_warning ("no string?");
   unformat_free (&in);
 
   return s;
 }
+
+int
+vlib_sysfs_get_free_hugepages (unsigned int numa_node, int page_size)
+{
+  struct stat sb;
+  u8 *p = 0;
+  int r = -1;
+
+  p = format (p, "/sys/devices/system/node/node%u%c", numa_node, 0);
+
+  if (stat ((char *) p, &sb) == 0)
+    {
+      if (S_ISDIR (sb.st_mode) == 0)
+       goto done;
+    }
+  else if (numa_node == 0)
+    {
+      vec_reset_length (p);
+      p = format (p, "/sys/kernel/mm%c", 0);
+      if (stat ((char *) p, &sb) < 0 || S_ISDIR (sb.st_mode) == 0)
+       goto done;
+    }
+  else
+    goto done;
+
+  _vec_len (p) -= 1;
+  p = format (p, "/hugepages/hugepages-%ukB/free_hugepages%c", page_size, 0);
+  vlib_sysfs_read ((char *) p, "%d", &r);
+
+done:
+  vec_free (p);
+  return r;
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */