vppinfra: change vlib_register_node so it takes format string for node name

This allows specifying both c string and vector for node name
and removes need for crafting temporary string.

Type: improvement
Change-Id: I0b016cd70aeda0f68eb6f9171c5152f303be7369
Signed-off-by: Damjan Marion <damarion@cisco.com>
diff --git a/src/vlib/node.c b/src/vlib/node.c
index 2c408d6..c6a13fb 100644
--- a/src/vlib/node.c
+++ b/src/vlib/node.c
@@ -328,11 +328,13 @@
   return fn;
 }
 
-static void
-register_node (vlib_main_t * vm, vlib_node_registration_t * r)
+u32
+vlib_register_node (vlib_main_t *vm, vlib_node_registration_t *r, char *fmt,
+		    ...)
 {
   vlib_node_main_t *nm = &vm->node_main;
   vlib_node_t *n;
+  va_list va;
   u32 size;
   int i;
 
@@ -363,11 +365,9 @@
 
   vec_add1 (nm->nodes, n);
 
-  /* Name is always a vector so it can be formatted with %v. */
-  if (clib_mem_is_heap_object (vec_header (r->name)))
-    n->name = vec_dup ((u8 *) r->name);
-  else
-    n->name = format (0, "%s", r->name);
+  va_start (va, fmt);
+  n->name = va_format (0, fmt, &va);
+  va_end (va);
 
   if (!nm->node_by_name)
     nm->node_by_name = hash_create_vec ( /* size */ 32,
@@ -566,13 +566,6 @@
     vec_free (n->runtime_data);
   }
 #undef _
-}
-
-/* Register new packet processing node. */
-u32
-vlib_register_node (vlib_main_t * vm, vlib_node_registration_t * r)
-{
-  register_node (vm, r);
   return r->index;
 }
 
@@ -636,19 +629,18 @@
   static vlib_node_registration_t null_node_reg = {
     .function = null_node_fn,
     .vector_size = sizeof (u32),
-    .name = "null-node",
     .n_errors = 1,
     .error_strings = null_node_error_strings,
   };
 
   /* make sure that node index 0 is not used by
      real node */
-  register_node (vm, &null_node_reg);
+  vlib_register_node (vm, &null_node_reg, "null-node");
 
   r = vgm->node_registrations;
   while (r)
     {
-      register_node (vm, r);
+      vlib_register_node (vm, r, "%s", r->name);
       r = r->next_registration;
     }
 }
@@ -850,14 +842,13 @@
 
   memset (&r, 0, sizeof (r));
 
-  r.name = (char *) format (0, "%s", name, 0);
   r.function = f;
   r.process_log2_n_stack_bytes = log2_n_stack_bytes;
   r.type = VLIB_NODE_TYPE_PROCESS;
 
   vlib_worker_thread_barrier_sync (vm);
 
-  vlib_register_node (vm, &r);
+  vlib_register_node (vm, &r, "%s", name);
   vec_free (r.name);
 
   vlib_worker_thread_node_runtime_update ();
diff --git a/src/vlib/node_funcs.h b/src/vlib/node_funcs.h
index b864fec..61a0857 100644
--- a/src/vlib/node_funcs.h
+++ b/src/vlib/node_funcs.h
@@ -1211,7 +1211,8 @@
 /* Register new packet processing node.  Nodes can be registered
    dynamically via this call or statically via the VLIB_REGISTER_NODE
    macro. */
-u32 vlib_register_node (vlib_main_t * vm, vlib_node_registration_t * r);
+u32 vlib_register_node (vlib_main_t *vm, vlib_node_registration_t *r,
+			char *fmt, ...);
 
 /* Register all node function variants */
 void vlib_register_all_node_march_variants (vlib_main_t *vm);
diff --git a/src/vlib/unix/cli.c b/src/vlib/unix/cli.c
index 03baf3e..c546948 100644
--- a/src/vlib/unix/cli.c
+++ b/src/vlib/unix/cli.c
@@ -2928,11 +2928,9 @@
 	.process_log2_n_stack_bytes = 18,
       };
 
-      r.name = name;
-
       vlib_worker_thread_barrier_sync (vm);
 
-      vlib_register_node (vm, &r);
+      vlib_register_node (vm, &r, "%v", name);
       vec_free (name);
 
       n = vlib_get_node (vm, r.index);