/*
 * Copyright (c) 2016-2019 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.
 */
#ifndef __included_fifo_segment_h__
#define __included_fifo_segment_h__

#include <svm/ssvm.h>
#include <svm/fifo_types.h>
#include <svm/message_queue.h>
#include <svm/svm_fifo.h>

typedef enum
{
  FIFO_SEGMENT_FTYPE_NONE = -1,
  FIFO_SEGMENT_RX_FIFO = 0,
  FIFO_SEGMENT_TX_FIFO,
  FIFO_SEGMENT_N_FTYPES
} fifo_segment_ftype_t;

#define FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE 12	/**< 4kB min fifo size */
#define FIFO_SEGMENT_MIN_FIFO_SIZE 4096		/**< 4kB min fifo size */
#define FIFO_SEGMENT_MAX_FIFO_SIZE (2ULL << 30)	/**< 2GB max fifo size */
#define FIFO_SEGMENT_ALLOC_BATCH_SIZE 32	/* Allocation quantum */

typedef enum fifo_segment_flags_
{
  FIFO_SEGMENT_F_IS_PREALLOCATED = 1 << 0,
  FIFO_SEGMENT_F_WILL_DELETE = 1 << 1,
  FIFO_SEGMENT_F_MEM_LIMIT = 1 << 2,
  FIFO_SEGMENT_F_CUSTOM_USE = 1 << 3,
} fifo_segment_flags_t;

#define foreach_segment_mem_status	\
_(NO_PRESSURE, "No pressure")		\
_(LOW_PRESSURE, "Low pressure")		\
_(HIGH_PRESSURE, "High pressure")	\
_(NO_MEMORY, "No memory")

typedef enum
{
#define _(sym,str)  MEMORY_PRESSURE_##sym,
  foreach_segment_mem_status
#undef _
    MEMORY_N_PRESSURE,
} fifo_segment_mem_status_t;

#if 0
typedef enum fifo_segment_mem_status_
{
  MEMORY_PRESSURE_NO_PRESSURE,
  MEMORY_PRESSURE_LOW_PRESSURE,
  MEMORY_PRESSURE_HIGH_PRESSURE,
  MEMORY_PRESSURE_NO_MEMORY,
} fifo_segment_mem_status_t;
#endif

typedef struct
{
  ssvm_private_t ssvm;		/**< ssvm segment data */
  fifo_segment_header_t *h;	/**< fifo segment data */
  uword max_byte_index;
  u8 n_slices;			/**< number of fifo segment slices */
  fifo_slice_private_t *slices; /**< private slice information */
  svm_msg_q_t *mqs;		/**< private vec of attached mqs */
} fifo_segment_t;

typedef struct
{
  fifo_segment_t *segments;	/**< pool of fifo segments */
  uword next_baseva;		/**< Where to put the next one */
  u32 timeout_in_seconds;	/**< Time to wait during attach */
} fifo_segment_main_t;

typedef struct
{
  ssvm_segment_type_t segment_type;	/**< type of segment requested */
  u32 segment_size;			/**< size of the segment */
  int memfd_fd;				/**< fd for memfd segments */
  char *segment_name;			/**< segment name */
  u32 *new_segment_indices;		/**< return vec of new seg indices */
} fifo_segment_create_args_t;

#define fifo_segment_flags(_fs) _fs->h->flags

int fifo_segment_init (fifo_segment_t * fs);
int fifo_segment_create (fifo_segment_main_t * sm,
			 fifo_segment_create_args_t * a);
int fifo_segment_attach (fifo_segment_main_t * sm,
			 fifo_segment_create_args_t * a);
void fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * fs);
void fifo_segment_cleanup (fifo_segment_t *fs);
fifo_segment_t *fifo_segment_get_segment (fifo_segment_main_t * sm,
					  u32 fs_index);
fifo_segment_t *fifo_segment_get_segment_if_valid (fifo_segment_main_t *sm,
						   u32 segment_index);
u32 fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * fs);
void fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size);

always_inline void *
fifo_segment_ptr (fifo_segment_t *fs, uword offset)
{
  return (void *) ((u8 *) fs->h + offset);
}

always_inline uword
fifo_segment_offset (fifo_segment_t *fs, void *p)
{
  return (uword) ((u8 *) p - (u8 *) fs->h);
}

/**
 * Allocate fifo in fifo segment
 *
 * @param fs		fifo segment for fifo
 * @param data_bytes	size of default fifo chunk in bytes
 * @param ftype		fifo type @ref fifo_segment_ftype_t
 * @return		new fifo or 0 if alloc failed
 */
svm_fifo_t *fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs,
					     u32 slice_index,
					     u32 data_bytes,
					     fifo_segment_ftype_t ftype);
svm_fifo_t *fifo_segment_alloc_fifo_w_offset (fifo_segment_t *fs,
					      uword offset);
svm_fifo_t *fifo_segment_duplicate_fifo (fifo_segment_t *fs, svm_fifo_t *f);

/**
 * Free fifo allocated in fifo segment
 *
 * @param fs		fifo segment for fifo
 * @param f		fifo to be freed
 */
void fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f);

/**
 * Free fifo allocated by external applications
 *
 * @params fs		fifo segment for fifo
 * @param f		fifo to be freed
 */
void fifo_segment_free_client_fifo (fifo_segment_t *fs, svm_fifo_t *f);

void fifo_segment_detach_fifo (fifo_segment_t *fs, svm_fifo_t **f);
void fifo_segment_attach_fifo (fifo_segment_t *fs, svm_fifo_t **f,
			       u32 slice_index);
uword fifo_segment_fifo_offset (svm_fifo_t *f);

/**
 * Allocate message queue on segment
 *
 * @param fs		fifo segment for mq
 * @param mq_index	index in private mqs vector to use to attach
 * @param cfg		configuration for mq
 * @return		attached message queue
 */
svm_msg_q_t *fifo_segment_msg_q_alloc (fifo_segment_t *fs, u32 mq_index,
				       svm_msg_q_cfg_t *cfg);

/**
 *  Attach message queue at fifo segment offset
 *
 *  @param fs		fifo segment for mq
 *  @param offset	offset for shared mq on the segment
 *  @param mq_index	index in private mqs vector to use to attach
 *  @return		attached message queue
 */
svm_msg_q_t *fifo_segment_msg_q_attach (fifo_segment_t *fs, uword offset,
					u32 mq_index);

/**
 *  Discover mqs on mq only segment
 *
 *  @param fs		fifo segment for mq
 *  @param fds  	array of fds is mqs use eventfds
 *  @param n_fds	number of fds
 */
void fifo_segment_msg_qs_discover (fifo_segment_t *fs, int *fds, u32 n_fds);

/**
 * Message queue offset on segment
 *
 * @param fs		fifo segment for mq
 * @param mq_index	index of mq in private mqs vector
 * @return		offset of the shared mq the private mq is attached to
 */
uword fifo_segment_msg_q_offset (fifo_segment_t *fs, u32 mq_index);

/**
 * Try to preallocate fifo headers
 *
 * Tries to preallocate fifo headers and adds them to freelist.
 *
 * @param fs		fifo segment
 * @param batch_size	number of chunks to be allocated
 * @return		0 on success, negative number otherwise
 */
int fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
				     u32 batch_size);

/**
 * Try to preallocate fifo chunks on segment
 *
 * Tries to preallocate chunks of requested size on segment and adds them
 * to chunk freelist.
 *
 * @param fs		fifo segment
 * @param chunk_size	size of chunks to be allocated in bytes
 * @param batch_size	number of chunks to be allocated
 * @return		0 on success, negative number otherwise
 */
int fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
				       u32 chunk_size, u32 batch_size);
/**
 * Pre-allocates fifo pairs in fifo segment
 *
 * The number of fifos pre-allocated is the minimum of the requested number
 * of pairs and the maximum number that fit within the segment. If the maximum
 * is hit, the number of fifo pairs requested is updated by subtracting the
 * number of fifos that have been successfully allocated.
 *
 * @param fs		fifo segment for fifo
 * @param rx_fifo_size	data size of rx fifos
 * @param tx_fifo_size	data size of tx fifos
 * @param n_fifo_pairs	number of pairs requested. Prior to returning, this
 * 			is decremented by the the number of pairs allocated.
 */
void fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
					  u32 rx_fifo_size,
					  u32 tx_fifo_size,
					  u32 * n_fifo_pairs);

/**
 * Allocate chunks in fifo segment
 *
 * @param fsh		fifo segment header
 * @param slice_index	slice where chunks should be alocated
 * @param chunk_size	chunk size needed
 * @return		chunk (or chunks) that cover at least chunk_size bytes
 * 			on success, 0 on failure.
 */
svm_fifo_chunk_t *fsh_alloc_chunk (fifo_segment_header_t * fsh,
				   u32 slice_index, u32 chunk_size);

/**
 * Return chunks to fifo segment
 *
 * @param fsh		fifo segment header
 * @param slice_index	slice where chunks should be returned
 * @param c		pointer to first chunk in 0 terminated linked list
 */
void fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index,
			 svm_fifo_chunk_t * c);

/**
 * Fifo segment has reached mem limit
 *
 * @param fsh           fifo segment header
 * @return              1 (if reached) or 0 (otherwise)
 */
u8 fsh_has_reached_mem_limit (fifo_segment_header_t * fsh);

/**
 * Fifo segment reset mem limit flag
 *
 * @param fs            fifo segment
 */
void fsh_reset_mem_limit (fifo_segment_header_t * fsh);

/**
 * Fifo segment reset mem limit flag
 *
 * @param fs            fifo segment
 * @param size		size requested
 * @return		pointer to memory allocated or 0
 */
void *fifo_segment_alloc (fifo_segment_t *fs, uword size);
/**
 * Fifo segment allocated size
 *
 * Returns fifo segment's allocated size
 *
 * @param fs            fifo segment
 * @return              allocated size in bytes
 */
uword fifo_segment_size (fifo_segment_t * fs);

/**
 * Fifo segment estimate of number of free bytes
 *
 * Returns fifo segment's internal estimate of the number of free bytes.
 * To force a synchronization between the segment and the underlying
 * memory allocator, call @ref fifo_segment_update_free_bytes
 *
 * @param fs		fifo segment
 * @return		free bytes estimate
 */
uword fifo_segment_free_bytes (fifo_segment_t * fs);

/**
 * Fifo segment number of cached bytes
 *
 * Returns fifo segment's number of cached bytes.
 *
 * @param fs            fifo segment
 * @return              cached bytes
 */
uword fifo_segment_cached_bytes (fifo_segment_t * fs);

uword fifo_segment_available_bytes (fifo_segment_t * fs);

/**
 * Number of bytes on chunk free lists
 *
 * @param fs		fifo segment
 * @return		free bytes on chunk free lists
 */
uword fifo_segment_fl_chunk_bytes (fifo_segment_t * fs);
u8 fifo_segment_has_fifos (fifo_segment_t * fs);
svm_fifo_t *fifo_segment_get_slice_fifo_list (fifo_segment_t * fs,
					      u32 slice_index);
u32 fifo_segment_num_fifos (fifo_segment_t * fs);
u32 fifo_segment_num_free_fifos (fifo_segment_t * fs);

svm_fifo_chunk_t *fifo_segment_alloc_chunk_w_slice (fifo_segment_t *fs,
						    u32 slice_index,
						    u32 chunk_size);
void fifo_segment_collect_chunk (fifo_segment_t *fs, u32 slice_index,
				 svm_fifo_chunk_t *c);
uword fifo_segment_chunk_offset (fifo_segment_t *fs, svm_fifo_chunk_t *c);

/**
 * Find number of free chunks of given size
 *
 * @param fs	fifo segment
 * @param size	chunk size of interest or ~0 if all should be counted
 * @return	number of chunks of given size
 */
u32 fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size);

u8 fifo_segment_get_mem_usage (fifo_segment_t * fs);
fifo_segment_mem_status_t fifo_segment_determine_status
  (fifo_segment_header_t * fsh, u8 usage);
fifo_segment_mem_status_t fifo_segment_get_mem_status (fifo_segment_t * fs);

void fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
			     u32 timeout_in_seconds);

format_function_t format_fifo_segment;
format_function_t format_fifo_segment_type;

#endif /* __included_fifo_segment_h__ */

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