vlib: store buffer memory information in the buffer_main

Currently, buffer index is calculated as a offset to the physmem
region shifted by log2_cacheline size.

When DPDK is used we "hack" physmem data with information taken from
dpdk mempool. This makes physmem code not usable with DPDK.

This change makes buffer memory start and size independent of physmem
basically allowing physmem to be used when DPDK plugin is loaded.

Change-Id: Ieb399d398f147583b9baab467152a352d58c9c31
Signed-off-by: Damjan Marion <damarion@cisco.com>
diff --git a/src/vlib/buffer.c b/src/vlib/buffer.c
index e50064a..b2a095c 100644
--- a/src/vlib/buffer.c
+++ b/src/vlib/buffer.c
@@ -46,6 +46,8 @@
 #include <vlib/vlib.h>
 #include <vlib/unix/unix.h>
 
+vlib_buffer_callbacks_t *vlib_buffer_callbacks = 0;
+
 uword
 vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
 				       vlib_buffer_t * b_first)
@@ -584,11 +586,6 @@
 
   dst = alloc_buffers;
 
-  /* wait with buffer memory allocation as long as possible
-     in case external buffer manager takes over */
-  if (PREDICT_FALSE (vm->os_physmem_alloc_aligned == 0))
-    unix_physmem_init (vm, 0 /* fail_if_physical_memory_not_present */ );
-
   n_filled = fill_free_list (vm, free_list, n_alloc_buffers);
   if (n_filled == 0)
     return 0;
@@ -944,6 +941,36 @@
   return copied;
 }
 
+void
+vlib_buffer_add_mem_range (vlib_main_t * vm, uword start, uword size)
+{
+  vlib_buffer_main_t *bm = vm->buffer_main;
+
+  if (bm->buffer_mem_size == 0)
+    {
+      bm->buffer_mem_start = start;
+      bm->buffer_mem_size = size;
+    }
+  else if (start < bm->buffer_mem_start)
+    {
+      bm->buffer_mem_size += bm->buffer_mem_start - start;
+      bm->buffer_mem_start = start;
+      if (size > bm->buffer_mem_size)
+	bm->buffer_mem_size = size;
+    }
+  else if (start > bm->buffer_mem_start)
+    {
+      uword new_size = start - bm->buffer_mem_start + size;
+      if (new_size > bm->buffer_mem_size)
+	bm->buffer_mem_size = new_size;
+    }
+
+  if ((u64) bm->buffer_mem_size >
+      ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES)))
+    {
+      clib_panic ("buffer memory size out of range!");
+    }
+}
 
 static u8 *
 format_vlib_buffer_free_list (u8 * s, va_list * va)
@@ -1011,6 +1038,7 @@
 vlib_buffer_cb_init (struct vlib_main_t *vm)
 {
   vlib_buffer_main_t *bm = vm->buffer_main;
+
   bm->cb.vlib_buffer_alloc_cb = &vlib_buffer_alloc_internal;
   bm->cb.vlib_buffer_alloc_from_free_list_cb =
     &vlib_buffer_alloc_from_free_list_internal;
@@ -1018,25 +1046,6 @@
   bm->cb.vlib_buffer_free_no_next_cb = &vlib_buffer_free_no_next_internal;
   bm->cb.vlib_buffer_delete_free_list_cb =
     &vlib_buffer_delete_free_list_internal;
-  bm->extern_buffer_mgmt = 0;
-}
-
-int
-vlib_buffer_cb_register (struct vlib_main_t *vm, vlib_buffer_callbacks_t * cb)
-{
-  vlib_buffer_main_t *bm = vm->buffer_main;
-  if (bm->extern_buffer_mgmt)
-    return -1;
-
-#define _(x) bm->cb.x = cb->x
-  _(vlib_buffer_alloc_cb);
-  _(vlib_buffer_alloc_from_free_list_cb);
-  _(vlib_buffer_free_cb);
-  _(vlib_buffer_free_no_next_cb);
-  _(vlib_buffer_delete_free_list_cb);
-#undef _
-  bm->extern_buffer_mgmt = 1;
-  return 0;
 }
 
 /** @endcond */