/* * Copyright (c) 2016-2019 Cisco and/or its affiliates. * Copyright (c) 2019 Arm Limited * Copyright (c) 2010-2017 Intel Corporation and/or its affiliates. * Copyright (c) 2007-2009 Kip Macy kmacy@freebsd.org * Inspired from DPDK rte_ring.h (SPSC only) (derived from freebsd bufring.h). * 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. */ #ifndef __included_ssvm_fifo_h__ #define __included_ssvm_fifo_h__ #include #include #include #include #include #define OOO_SEGMENT_INVALID_INDEX ((u32)~0) #define SVM_FIFO_INVALID_SESSION_INDEX ((u32)~0) #define SVM_FIFO_INVALID_INDEX ((u32)~0) typedef enum svm_fifo_deq_ntf_ { SVM_FIFO_NO_DEQ_NOTIF = 0, /**< No notification requested */ SVM_FIFO_WANT_DEQ_NOTIF = 1, /**< Notify on dequeue */ SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL = 2, /**< Notify on transition from full */ SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY = 4, /**< Notify on transition to empty */ } svm_fifo_deq_ntf_t; typedef enum svm_fifo_flag_ { SVM_FIFO_F_LL_TRACKED = 1 << 0, } svm_fifo_flag_t; typedef enum { SVM_FIFO_EFULL = -2, SVM_FIFO_EEMPTY = -3, SVM_FIFO_EGROW = -4, } svm_fifo_err_t; typedef struct svm_fifo_seg_ { u8 *data; u32 len; } svm_fifo_seg_t; #if SVM_FIFO_TRACE #define svm_fifo_trace_add(_f, _s, _l, _t) \ { \ svm_fifo_trace_elem_t *trace_elt; \ vec_add2(_f->trace, trace_elt, 1); \ trace_elt->offset = _s; \ trace_elt->len = _l; \ trace_elt->action = _t; \ } #else #define svm_fifo_trace_add(_f, _s, _l, _t) #endif u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f); u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose); /** * Load head and tail optimized for consumer * * Internal function. */ static inline void f_load_head_tail_cons (svm_fifo_t * f, u32 * head, u32 * tail) { /* load-relaxed: consumer owned index */ *head = f->shr->head; /* load-acq: consumer foreign index (paired with store-rel in producer) */ *tail = clib_atomic_load_acq_n (&f->shr->tail); } /** Load head and tail optimized for producer * * Internal function */ static inline void f_load_head_tail_prod (svm_fifo_t * f, u32 * head, u32 * tail) { /* load relaxed: producer owned index */ *tail = f->shr->tail; /* load-acq: producer foreign index (paired with store-rel in consumer) */ *head = clib_atomic_load_acq_n (&f->shr->head); } /** * Load head and tail independent of producer/consumer role * * Internal function. */ static inline void f_load_head_tail_all_acq (svm_fifo_t * f, u32 * head, u32 * tail) { /* load-acq : consumer foreign index (paired with store-rel) */ *tail = clib_atomic_load_acq_n (&f->shr->tail); /* load-acq : producer foriegn index (paired with store-rel) */ *head = clib_atomic_load_acq_n (&f->shr->head); } /** * Fifo current size, i.e., number of bytes enqueued * * Internal function. */ static inline u32 f_cursize (svm_fifo_t * f, u32 head, u32 tail) { return tail - head; } /** * Fifo free bytes, i.e., number of free bytes * * Internal function */ static inline u32 f_free_count (svm_fifo_t * f, u32 head, u32 tail) { return (f->shr->size - f_cursize (f, head, tail)); } always_inline u32 f_chunk_end (svm_fifo_chunk_t * c) { return c->start_byte + c->length; } always_inline int f_pos_lt (u32 a, u32 b) { return ((i32) (a - b) < 0); } always_inline int f_pos_leq (u32 a, u32 b) { return ((i32) (a - b) <= 0); } always_inline int f_pos_gt (u32 a, u32 b) { return ((i32) (a - b) > 0); } always_inline int f_pos_geq (u32 a, u32 b) { return ((i32) (a - b) >= 0); } always_inline u8 f_chunk_includes_pos (svm_fifo_chunk_t * c, u32 pos) { return (f_pos_geq (pos, c->start_byte) && f_pos_lt (pos, c->start_byte + c->length)); } always_inline svm_fifo_chunk_t * f_start_cptr (svm_fifo_t *f) { return fs_chunk_ptr (f->fs_hdr, f->shr->start_chunk); } always_inline svm_fifo_chunk_t * f_end_cptr (svm_fifo_t *f) { return fs_chunk_ptr (f->fs_hdr, f->shr->end_chunk); } always_inline svm_fifo_chunk_t * f_head_cptr (svm_fifo_t *f) { return fs_chunk_ptr (f->fs_hdr, f->shr->head_chunk); } always_inline svm_fifo_chunk_t * f_tail_cptr (svm_fifo_t *f) { return fs_chunk_ptr (f->fs_hdr, f->shr->tail_chunk); } always_inline svm_fifo_chunk_t * f_cptr (svm_fifo_t *f, fs_sptr_t cp) { return fs_chunk_ptr (f->fs_hdr, cp); } always_inline fs_sptr_t f_csptr (svm_fifo_t *f, svm_fifo_chunk_t *c) { return fs_chunk_sptr (f->fs_hdr, c); } always_inline void f_csptr_link (svm_fifo_t *f, fs_sptr_t cp, svm_fifo_chunk_t *c) { fs_chunk_ptr (f->fs_hdr, cp)->next = fs_chunk_sptr (f->fs_hdr, c); } /** * Create fifo of requested size * * Allocates fifo on current heap. * * @param size data size in bytes for fifo to be allocated. Will be * rounded to the next highest power-of-two value. * @return pointer to new fifo */ svm_fifo_t *svm_fifo_alloc (u32 size); /** * Initialize fifo * * @param f fifo * @param size size for fifo */ void svm_fifo_init (svm_fifo_t * f, u32 size); /** * Allocate a fifo chunk on heap * * If the chunk is allocated on a fifo segment, this should be called * with the segment's heap pushed. * * @param size chunk size in bytes. Will be rounded to the next highest * power-of-two * @return new chunk or 0 if alloc failed */ svm_fifo_chunk_t *svm_fifo_chunk_alloc (u32 size); /** * Ensure the whole fifo size is writeable * * Allocates enough chunks to cover the whole fifo size. * * @param f fifo */ int svm_fifo_fill_chunk_list (svm_fifo_t * f); /** * Provision and return chunks for number of bytes requested * * Allocates enough chunks to cover the bytes requested and returns them * in the fifo segment array. The number of bytes provisioned may be less * than requested if not enough segments were provided. * * @param f fifo * @param fs array of fifo segments * @param n_segs length of fifo segments array * @param len number of bytes to preallocate * @return number of fifo segments provisioned or error */ int svm_fifo_provision_chunks (svm_fifo_t *f, svm_fifo_seg_t *fs, u32 n_segs, u32 len); /** * Initialize rbtrees used for ooo lookups * * @param f fifo * @param ooo_type type of ooo operation (0 enqueue, 1 dequeue) */ void svm_fifo_init_ooo_lookup (svm_fifo_t * f, u8 ooo_type); /** * Free fifo and associated state * * @param f fifo */ void svm_fifo_free (svm_fifo_t * f); /** * Cleanup fifo chunk lookup rb tree * * The rb tree is allocated in segment heap so this should be called * with it pushed. * * @param f fifo to cleanup */ void svm_fifo_free_chunk_lookup (svm_fifo_t * f); /** * Cleanup fifo ooo data * * The ooo data is allocated in producer process memory. The fifo * segment heap should not be pushed. * * @param f fifo to cleanup */ void svm_fifo_free_ooo_data (svm_fifo_t * f); /** * Init fifo head and tail * * @param f fifo * @param head head value that will be matched to a chunk * @param tail tail value that will be matched to a chunk */ void svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail); /** * Clone fifo * * Clones single/default chunk fifo. It does not work for fifos with * multiple chunks. */ void svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf); /** * Enqueue data to fifo * * Data is enqueued and tail pointer is updated atomically. If
/*
 * Copyright (c) 2020 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.
 */

#include <vppinfra/clib.h>
#include <vppinfra/mem.h>
#include <vppinfra/time.h>
#include <vppinfra/format.h>
#include <vppinfra/clib_error.h>

__clib_export clib_mem_main_t clib_mem_main;

__clib_export void *
clib_mem_vm_map (void *base, uword size, clib_mem_page_sz_t log2_page_sz,
		 char *fmt, ...)
{
  va_list va;
  void *rv;
  u8 *s;

  va_start (va, fmt);
  s = va_format (0, fmt, &va);
  vec_add1 (s, 0);
  rv = clib_mem_vm_map_internal (base, log2_page_sz, size, -1, 0, (char *) s);
  va_end (va);
  vec_free (s);
  return rv;
}

__clib_export void *
clib_mem_vm_map_stack (uword size, clib_mem_page_sz_t