summaryrefslogtreecommitdiffstats
path: root/src/vlib
diff options
context:
space:
mode:
authorDamjan Marion <damarion@cisco.com>2023-10-06 10:59:32 +0200
committerOle Tr�an <otroan@employees.org>2023-10-09 09:58:37 +0000
commit65dc34bb0bdc1045388d2bcd2e93704a97ac6843 (patch)
tree1ef794fd12a994dc9f52c7512db51de3e66174af /src/vlib
parentaa7b88120ad83a29a05522bed4e5aa71524b8aba (diff)
buffers: buffer allocation improvements
- pass buffer pool name trough va - make buffers naturaly aligned - fix calculation of total number of buffers Type: improvement Change-Id: I6aebf249ebd67823b4632ac08905bfa3aa7d1ee5 Signed-off-by: Damjan Marion <damarion@cisco.com>
Diffstat (limited to 'src/vlib')
-rw-r--r--src/vlib/buffer.c80
-rw-r--r--src/vlib/buffer.h5
2 files changed, 47 insertions, 38 deletions
diff --git a/src/vlib/buffer.c b/src/vlib/buffer.c
index 304d1abd8ea..00686593c6b 100644
--- a/src/vlib/buffer.c
+++ b/src/vlib/buffer.c
@@ -484,16 +484,18 @@ vlib_buffer_alloc_size (uword ext_hdr_size, uword data_size)
}
u8
-vlib_buffer_pool_create (vlib_main_t * vm, char *name, u32 data_size,
- u32 physmem_map_index)
+vlib_buffer_pool_create (vlib_main_t *vm, u32 data_size, u32 physmem_map_index,
+ char *fmt, ...)
{
vlib_buffer_main_t *bm = vm->buffer_main;
vlib_buffer_pool_t *bp;
vlib_physmem_map_t *m = vlib_physmem_get_map (vm, physmem_map_index);
uword start = pointer_to_uword (m->base);
uword size = (uword) m->n_pages << m->log2_page_size;
- uword i, j;
- u32 alloc_size, n_alloc_per_page;
+ uword page_mask = ~pow2_mask (m->log2_page_size);
+ u8 *p;
+ u32 alloc_size;
+ va_list va;
if (vec_len (bm->buffer_pools) >= 255)
return ~0;
@@ -531,48 +533,57 @@ vlib_buffer_pool_create (vlib_main_t * vm, char *name, u32 data_size,
bp->buffer_template.buffer_pool_index = bp->index;
bp->buffer_template.ref_count = 1;
bp->physmem_map_index = physmem_map_index;
- bp->name = format (0, "%s%c", name, 0);
bp->data_size = data_size;
bp->numa_node = m->numa_node;
+ bp->log2_page_size = m->log2_page_size;
+
+ va_start (va, fmt);
+ bp->name = va_format (0, fmt, &va);
+ va_end (va);
vec_validate_aligned (bp->threads, vlib_get_n_threads () - 1,
CLIB_CACHE_LINE_BYTES);
alloc_size = vlib_buffer_alloc_size (bm->ext_hdr_size, data_size);
- n_alloc_per_page = (1ULL << m->log2_page_size) / alloc_size;
+ bp->alloc_size = alloc_size;
/* preallocate buffer indices memory */
- bp->n_buffers = m->n_pages * n_alloc_per_page;
- bp->buffers = clib_mem_alloc_aligned (bp->n_buffers * sizeof (u32),
- CLIB_CACHE_LINE_BYTES);
+ bp->buffers = clib_mem_alloc_aligned (
+ round_pow2 ((size / alloc_size) * sizeof (u32), CLIB_CACHE_LINE_BYTES),
+ CLIB_CACHE_LINE_BYTES);
clib_spinlock_init (&bp->lock);
- for (j = 0; j < m->n_pages; j++)
- for (i = 0; i < n_alloc_per_page; i++)
- {
- u8 *p;
- u32 bi;
-
- p = m->base + (j << m->log2_page_size) + i * alloc_size;
- p += bm->ext_hdr_size;
-
- /*
- * Waste 1 buffer (maximum) so that 0 is never a valid buffer index.
- * Allows various places to ASSERT (bi != 0). Much easier
- * than debugging downstream crashes in successor nodes.
- */
- if (p == m->base)
- continue;
+ p = m->base;
- vlib_buffer_copy_template ((vlib_buffer_t *) p, &bp->buffer_template);
+ /* start with naturally aligned address */
+ p += alloc_size - (uword) p % alloc_size;
- bi = vlib_get_buffer_index (vm, (vlib_buffer_t *) p);
+ /*
+ * Waste 1 buffer (maximum) so that 0 is never a valid buffer index.
+ * Allows various places to ASSERT (bi != 0). Much easier
+ * than debugging downstream crashes in successor nodes.
+ */
+ if (p == m->base)
+ p += alloc_size;
- bp->buffers[bp->n_avail++] = bi;
+ for (; p < (u8 *) m->base + size - alloc_size; p += alloc_size)
+ {
+ vlib_buffer_t *b;
+ u32 bi;
+
+ /* skip if buffer spans across page boundary */
+ if (((uword) p & page_mask) != ((uword) (p + alloc_size) & page_mask))
+ continue;
+
+ b = (vlib_buffer_t *) (p + bm->ext_hdr_size);
+ vlib_buffer_copy_template (b, &bp->buffer_template);
+ bi = vlib_get_buffer_index (vm, b);
+ bp->buffers[bp->n_avail++] = bi;
+ vlib_get_buffer (vm, bi);
+ }
- vlib_get_buffer (vm, bi);
- }
+ bp->n_buffers = bp->n_avail;
return bp->index;
}
@@ -694,7 +705,6 @@ vlib_buffer_main_init_numa_node (struct vlib_main_t *vm, u32 numa_node,
vlib_buffer_main_t *bm = vm->buffer_main;
u32 physmem_map_index;
clib_error_t *error;
- u8 *name = 0;
if (bm->log2_page_size == CLIB_MEM_PAGE_SZ_UNKNOWN)
{
@@ -725,14 +735,12 @@ vlib_buffer_main_init_numa_node (struct vlib_main_t *vm, u32 numa_node,
return error;
buffer_pool_create:
- name = format (name, "default-numa-%d%c", numa_node, 0);
- *index = vlib_buffer_pool_create (vm, (char *) name,
- vlib_buffer_get_default_data_size (vm),
- physmem_map_index);
+ *index =
+ vlib_buffer_pool_create (vm, vlib_buffer_get_default_data_size (vm),
+ physmem_map_index, "default-numa-%d", numa_node);
if (*index == (u8) ~ 0)
error = clib_error_return (0, "maximum number of buffer pools reached");
- vec_free (name);
return error;
diff --git a/src/vlib/buffer.h b/src/vlib/buffer.h
index b548adf4be8..2a5af210330 100644
--- a/src/vlib/buffer.h
+++ b/src/vlib/buffer.h
@@ -452,11 +452,12 @@ typedef struct
CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
uword start;
uword size;
- uword log2_page_size;
+ u8 log2_page_size;
u8 index;
- u32 numa_node;
+ u8 numa_node;
u32 physmem_map_index;
u32 data_size;
+ u32 alloc_size;
u32 n_buffers;
u32 n_avail;
u32 *buffers;
me.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE

#include <vnet/bonding/node.h>
#include <lacp/node.h>

/*
 *  LACP State = NO_PERIODIC
 */
static lacp_fsm_state_t lacp_ptx_state_no_periodic[] = {
  {LACP_ACTION_NO_PERIODIC, LACP_PTX_STATE_NO_PERIODIC},	// event 0 NO_PERIODIC
  {LACP_ACTION_SLOW_PERIODIC, LACP_PTX_STATE_SLOW_PERIODIC},	// event 1 LONG_TIMEOUT
  {LACP_ACTION_NO_PERIODIC, LACP_PTX_STATE_NO_PERIODIC},	// event 2 TIMER_EXPIRED
  {LACP_ACTION_FAST_PERIODIC, LACP_PTX_STATE_FAST_PERIODIC},	// event 3 SHORT_TIMEOUT
};

/*
 *  LACP State = FAST_PERIODIC
 */
static lacp_fsm_state_t lacp_ptx_state_fast_periodic[] = {
  {LACP_ACTION_NO_PERIODIC, LACP_PTX_STATE_NO_PERIODIC},	// event 0 NO_PERIODIC
  {LACP_ACTION_SLOW_PERIODIC, LACP_PTX_STATE_SLOW_PERIODIC},	// event 1 LONG_TIMEOUT
  {LACP_ACTION_TIMER_EXPIRED, LACP_PTX_STATE_PERIODIC_TX},	// event 2 TIMER_EXPIRED
  {LACP_ACTION_FAST_PERIODIC, LACP_PTX_STATE_FAST_PERIODIC},	// event 3 SHORT_TIMEOUT
};

/*
 *  LACP State = SLOW_PERIODIC
 */
static lacp_fsm_state_t lacp_ptx_state_slow_periodic[] = {
  {LACP_ACTION_NO_PERIODIC, LACP_PTX_STATE_NO_PERIODIC},	// event 0 NO_PERIODIC
  {LACP_ACTION_SLOW_PERIODIC, LACP_PTX_STATE_SLOW_PERIODIC},	// event 1 LONG_TIMEOUT
  {LACP_ACTION_TIMER_EXPIRED, LACP_PTX_STATE_PERIODIC_TX},	// event 2 TIMER_EXPIRED
  {LACP_ACTION_FAST_PERIODIC, LACP_PTX_STATE_FAST_PERIODIC},	// event 3 SHORT_TIMEOUT
};

/*
 *  LACP State = PERIODIC_TX
 */
static lacp_fsm_state_t lacp_ptx_state_periodic_tx[] = {
  {LACP_ACTION_NO_PERIODIC, LACP_PTX_STATE_NO_PERIODIC},	// event 0 NO_PERIODIC
  {LACP_NOACTION, LACP_PTX_STATE_PERIODIC_TX},	// event 1 LONG_TIMEOUT
  {LACP_ACTION_TIMER_EXPIRED, LACP_PTX_STATE_PERIODIC_TX},	// event 2 TIMER_EXPIRED
  {LACP_NOACTION, LACP_PTX_STATE_PERIODIC_TX},	// event 3 SHORT_TIMEOUT
};


static lacp_fsm_machine_t lacp_ptx_fsm_table[] = {
  {lacp_ptx_state_no_periodic},
  {lacp_ptx_state_fast_periodic},
  {lacp_ptx_state_slow_periodic},
  {lacp_ptx_state_periodic_tx},
};

lacp_machine_t lacp_ptx_machine = {
  lacp_ptx_fsm_table,
  lacp_ptx_debug_func,
};

int
lacp_ptx_action_no_periodic (void *p1, void *p2)
{
  vlib_main_t *vm = p1;
  slave_if_t *sif = p2;

  lacp_stop_timer (&sif->periodic_timer);
  lacp_ptx_post_short_timeout_event (vm, sif);
  return 0;
}

int
lacp_ptx_action_slow_periodic (void *p1, void *p2)
{
  vlib_main_t *vm = p1;
  slave_if_t *sif = p2;
  u8 timer_expired;
  lacp_main_t *lm = &lacp_main;

  if (!(sif->partner.state & LACP_STATE_LACP_ACTIVITY) &&
      !(sif->actor.state & LACP_STATE_LACP_ACTIVITY))
    lacp_machine_dispatch (&lacp_ptx_machine, vm, sif,
			   LACP_PTX_EVENT_NO_PERIODIC, &sif->ptx_state);
  else
    {
      if (lacp_timer_is_running (sif->periodic_timer) &&
	  lacp_timer_is_expired (lm->vlib_main, sif->periodic_timer))
	timer_expired = 1;
      else
	timer_expired = 0;

      lacp_schedule_periodic_timer (lm->vlib_main, sif);

      if (timer_expired || (sif->partner.state & LACP_STATE_LACP_TIMEOUT))
	lacp_machine_dispatch (&lacp_ptx_machine, vm, sif,
			       LACP_PTX_EVENT_TIMER_EXPIRED, &sif->ptx_state);
    }

  return 0;
}

int
lacp_ptx_action_fast_periodic (void *p1, void *p2)
{
  vlib_main_t *vm = p1;
  slave_if_t *sif = p2;
  u8 timer_expired;
  lacp_main_t *lm = &lacp_main;

  if (!(sif->partner.state & LACP_STATE_LACP_ACTIVITY) &&
      !(sif->actor.state & LACP_STATE_LACP_ACTIVITY))
    lacp_machine_dispatch (&lacp_ptx_machine, vm, sif,
			   LACP_PTX_EVENT_NO_PERIODIC, &sif->ptx_state);
  else
    {
      if (lacp_timer_is_running (sif->periodic_timer) &&
	  lacp_timer_is_expired (lm->vlib_main, sif->periodic_timer))
	timer_expired = 1;
      else
	timer_expired = 0;

      lacp_start_periodic_timer (lm->vlib_main, sif,
				 LACP_FAST_PERIODIC_TIMER);

      if (timer_expired)
	lacp_machine_dispatch (&lacp_ptx_machine, vm, sif,
			       LACP_PTX_EVENT_TIMER_EXPIRED, &sif->ptx_state);

      if (!(sif->partner.state & LACP_STATE_LACP_TIMEOUT))
	lacp_machine_dispatch (&lacp_ptx_machine, vm, sif,
			       LACP_PTX_EVENT_LONG_TIMEOUT, &sif->ptx_state);
    }

  return 0;
}

int
lacp_ptx_action_timer_expired (void *p1, void *p2)
{
  vlib_main_t *vm = p1;
  slave_if_t *sif = p2;

  if (!(sif->partner.state & LACP_STATE_LACP_ACTIVITY) &&
      !(sif->actor.state & LACP_STATE_LACP_ACTIVITY))
    lacp_machine_dispatch (&lacp_ptx_machine, vm, sif,
			   LACP_PTX_EVENT_NO_PERIODIC, &sif->ptx_state);
  else
    {
      sif->ntt = 1;
      lacp_machine_dispatch (&lacp_tx_machine, vm, sif, LACP_TX_EVENT_NTT,
			     &sif->tx_state);
      if (sif->partner.state & LACP_STATE_LACP_TIMEOUT)
	lacp_machine_dispatch (&lacp_ptx_machine, vm, sif,
			       LACP_PTX_EVENT_SHORT_TIMEOUT, &sif->ptx_state);
      else
	lacp_machine_dispatch (&lacp_ptx_machine, vm, sif,
			       LACP_PTX_EVENT_LONG_TIMEOUT, &sif->ptx_state);
    }

  return 0;
}

static u8 *
format_ptx_event (u8 * s, va_list * args)
{
  static lacp_event_struct lacp_ptx_event_array[] = {
#define _(b, s, n) {.bit = b, .str = #s, },
    foreach_lacp_ptx_event
#undef _
    {.str = NULL}
  };
  int e = va_arg (*args, int);
  lacp_event_struct *event_entry = lacp_ptx_event_array;

  if (e >= (sizeof (lacp_ptx_event_array) / sizeof (*event_entry)))
    s = format (s, "Bad event %d", e);
  else
    s = format (s, "%s", event_entry[e].str);

  return s;
}

void
lacp_ptx_debug_func (slave_if_t * sif, int event, int state,
		     lacp_fsm_state_t * transition)
{
  vlib_worker_thread_t *w = vlib_worker_threads + os_get_thread_index ();
  /* *INDENT-OFF* */
  ELOG_TYPE_DECLARE (e) =
    {
      .format = "%s",
      .format_args = "T4",
    };
  /* *INDENT-ON* */
  struct
  {
    u32 event;
  } *ed = 0;

  ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, w->elog_track);
  ed->event =
    elog_string (&vlib_global_main.elog_main, "%U-PTX: %U, %U->%U%c",
		 format_vnet_sw_if_index_name, vnet_get_main (),
		 sif->sw_if_index, format_ptx_event, event,
		 format_ptx_sm_state, state, format_ptx_sm_state,
		 transition->next_state, 0);
}

void
lacp_init_ptx_machine (vlib_main_t * vm, slave_if_t * sif)
{
  lacp_machine_dispatch (&lacp_ptx_machine, vm, sif,
			 LACP_PTX_EVENT_NO_PERIODIC, &sif->ptx_state);
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */