/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */

/* Version 0.4  */
#ifndef __IOS_IOBUF_H__
#define __IOS_IOBUF_H__
#include <iostypes.h>

/*
 * Discussion:
 * There are two types of objects involved in iobuf management:
 *
 * Iobuf Headers: these are fixed length objects allocated out of 
 * a global pool.   The basic form of a header is shown above, but
 * in addition there may be some debug fields which are useful in
 * debugging, and which are not used during production running.
 * (Definition of debug fields TBD, but probably includes the ident
 * of the caller plus a sub-ident that the caller can use for internal
 * accounting, and a field for the time the iobuf was allocated so
 * that we can detect very "stale" iobufs).
 *
 * Buffers: these are allocated from a set of global pools, each
 * pool representing a set of fixed length buffers.   It is the
 * responsibility of the caller to know what length buffer is needed
 * so we can allocate the buffer out of the matching or next largest 
 * buffer pool.   The alignment of fixed length buffers is such that 
 * we can tell if the 'head' field in an iobuf header is plausible, 
 * so as to avoid trying to free arbitrary memory when de-allocating.
 *
 * Allocation: allocation and free will use pools in a global data 
 * space, which may be a shared segment that appears in the address 
 * space of every interested process, plus a library that does the 
 * allocation and freeing.    Buffers should always be manipulated
 * through this library and process code should not assume that it
 * "knows" about the fields in iobuf headers, since those fields
 * could change.  The API itself will only change in a backwards-
 * compatible way.   We may come up with a mechanism to prevent
 * access to iobufs by unauthorized code (TBD).
 *
 * Since iobufs are used to move data around the system, a given iobuf
 * may be allocated in one process and de-allocated in another.   This
 * means that de-allocation must always be possible without knowledge
 * that is private to the original allocator.   For example, it is fine
 * for the process that allocates an iobuf to use it to transmit data
 * to the process that will finally de-allocate it, but this data should
 * not contain pointers to transient objects belonging to the process 
 * that first allocated the iobuf.  The reason for this restriction 
 * is that we don't in general know when the iobuf will finally be 
 * de-allocated, so we don't know when it will be safe to destroy the
 * transient objects.
 */

/*
 * iobuf header struct
 *
 *   head -->  ----------------- <-- head
 *            |                |
 *            |                |
 *   data --> |XXXXXXXXXXXXXXXX| <-- data
 *            |XXXXXXXXXXXXXXXX|
 *   tail --> |XXXXXXXXXXXXXXXX| <-- data + dataLen
 *            |                |
 *   end  --> |________________| <-- head + bufLen
 */

typedef struct IOSIobuf{
  struct IOSIobuf *next; /* next iobuf in chain */
  struct IOSIobuf *link; /* queue of iobuf chains */
  void *priv;   /* process private */
  u8 *head;     /* head of iobuf */
  u8 *data;     /* start of data within iobuf */
  u16 bufLen;   /* length of buffer */
  u16 dataLen;  /*  data length */
  u16 flags;    /* flags */
  u8 priority;  /* some users need a prority field */
  u8 users;     /* users count */
  void *net;    /* network subsystem private */
  u16 id;       /* ID tag for verification */
  u16 reserved; /* reserved */

  u32 __pool;   /* id of pool from which buffer is allocated */
} IOSIobuf;

/*
 * Flags
 */
#define IOS_IOB_CLONED      0x01

/*
 * ID tags for verification purpose
 */
#define IOS_IOB_ID          0x4942      // 'IB'

/*
 * Macros to describe the current iobuf.   These macros exist so
 * that users should not need to touch iobuf headers directly.  This
 * allows us to change the implementation of iobufs without changing 
 * the API.
 *
 * IOS_IOB_NEXT - pointer to next iobuf in chain
 * IOS_IOB_LINK - pointer to next iobuf chain in queue of chains.
 * IOS_IOB_HEAD - pointer to start of data buffer.
 * IOS_IOB_DATA - pointer to start of data within data buffer.
 * IOS_IOB_TAIL - pointer to tail of data within data buffer.
 * IOS_IOB_DATA_LEN - Current data length
 */
#define IOS_IOB_NEXT(iob)     iob->next
#define IOS_IOB_LINK(iob)     iob->link
#define IOS_IOB_DATA(iob)     iob->data
#define IOS_IOB_HEAD(iob)     iob->head
#define IOS_IOB_TAIL(iob)     (iob->data + iob->dataLen)
#define IOS_IOB_END(iob)      (iob->head + iob->bufLen)
#define IOS_IOB_DATA_LEN(iob) iob->dataLen

/*
 * Optional extensions: these are thrown in here for discussion.  We
 * are not currently committed to providing them:
 *
 * Allocator's ident     - system wide ident identifying who originally
 *                         allocated the iobuf; useful for global debugging.
 *
 * Allocator's sub-ident - an ident internal to a process that may be
 *                         useful in debugging the process.
 *
 * Allocation time       - may be useful for detecting stale iobufs.
 *
 * Callback queue ident  - allows someone who receives an iobuf to signal
 *                         the sender that the iobuf has been disposed of.
 *                         Might be useful for asynchronous events, or
 *                         throttling.
 *
 * Magic                 - a field with a known signature that allows
 *                         code to check that it's really looking at an
 *                         iobuf.
 *
 * Any debug information that needs to be passed at allocation time,
 * will be packaged into a struct to be defined later. This paramter
 * will be ignored, for now.
 */
 typedef struct {
 } IOSIobDebugInfo;

/*
 * Allocate an iobuf with a buffer of the given size.   The
 * fields will have the following values:
 *
 * next    - NULL
 * link    - NULL
 * private - NULL
 * head    - address of associated iobuf
 * data    - same as head
 * tail    - same as head
 * len     - 0
 * flags   - 0
 *
 * If 'size' is not one of the iobuf buffer-pool fixed lengths, the next
 * greater length buffer will be allocated.
 *
 * If 'size' is zero, then a header with no associated buffer is
 * returned, and the head, data and tail fields are zero.
 */
IOSIobuf *IOS_AllocIob(u32 pool, u32 size, IOSIobDebugInfo d);

IOSIobuf *IOS_CloneIob(IOSIobuf *iob);

/*
 * De-allocate an iobuf header and its associated buffer.   If 
 * 'head' is NULL, then no attempt will be made to de-allocate 
 * the iobuf.  The de-allocator code must be able to figure out 
 * which fixed-length iobuf pool the data buffer was allocated 
 * from.
 */
IOSError IOS_FreeIob(IOSIobuf *iobuf);

/*
 * Determines if the given parameter is a valid iobuf
 */
IOSError IOS_IsValidIob(IOSIobuf *iob);

/*
 * De-allocate a chain of iobufs, using their 'next' pointers.
 * iobuf - pointer to first iobuf in chain.
 *
 */
IOSError IOS_FreeIobChain(IOSIobuf *iobuf);

/*
 * Break a chain of iobufs.
 *
 * iob1 is the start of the chain
 * iob2 is a pointer to an iobuf in the chain.   
 *
 * On success, the iobuf header before iob2 will be updated 
 * to be the end of the iob1 chain and iob2 will be returned 
 * as the head of the second chain.
 *
 * This routine can be used in combination with IOS_FreeIobChain() to
 * free up part of an iobuf chain.
 */
IOSIobuf *IOS_BreakIobChain(IOSIobuf *iob1, IOSIobuf *iob2);

/*
 * Enchain iobuf.
 *
 * Add an iobuf to an existing chain of iobufufs.
 *
 * iob - a single iobuf to enchain.
 * iobChain - an existing iobuf chain, one or more buffers linked 
 *        through their 'next' pointers.
 *
 * IOS_ENCHAIN_IOB adds the new iobuf at the end of the existing chain.
 * IOS_PREPEND_IOB adds the new iobuf at the start of the existing chain.
 */
#define IOS_ENCHAIN_IOB(chain, iob)  IOS_EnchainIob(chain, iob)
IOSIobuf *IOS_EnchainIob(IOSIobuf *chain, IOSIobuf *iob);


#define IOS_PREPEND_IOB(chain, iob) \
 IOS_PrependIob(IOSIobuf *chain, IOSIobuf *iob)
IOSIobuf *IOS_PrependIob(IOSIobuf *chain, IOSIobuf *iob);

/*
 * Enqueue iobuf:
 *
 * chain - an iobuf chain to enqueue
 * queue - a queue of one or more iobuf chains, linked through their
 *        'link' pointers.
 *
 * IOS_ENQUEUE_IOB adds a chain at the end of a queue of chains.
 * IOS_PREQUEUE_IOB adds a chain at the start of a queue of chains.
 */
#define IOS_ENQUEUE_IOB(queue, chain) \
                IOS_EnqueueIob(queue, chain);
IOSIobuf *IOS_EnqueueIob(IOSIobuf *queue, IOSIobuf *chain);

#define IOS_PREQUEUE_IOB(queue, chain) \
                IOS_PrequeueIob(queue, chain);
IOSIobuf *IOS_PrequeueIob(IOSIobuf *queue, IOSIobuf *chain);

/* Dequeue iobuf:
 * chain - an iobuf chain to dequeue.
 * queue - a queue of iobuf chains, linked through their 'link' 
 *        pointers.
 * IOS_DEQUEUE_IOB dequeues an iobuf chain.
 */
#define IOS_DEQUEUE_IOB(queue, chain) IOS_DequeueIob(queue, chain)
IOSIobuf *IOS_DequeueIob(IOSIobuf *queue, IOSIobuf *chain);
    
/*
 * Chain data length
 *
 * Tells you the length of the existing data in a chain of one
 * or more iobufs, using the sum of the 'len' fields in the 
 * iobufs.
 */
u32 IOS_GetIobChainDataLen(IOSIobuf *iob);

/*
 * Queue length
 * Tells you the number of chains on a queue */ 
u32 IOS_GetIobQueueLen(IOSIobuf *iob);

/*
 * Iobuf users count management (reference count of the iobuf structure)
 *
 * IOS_IOB_INCREF - increase the users count
 */
#define IOS_IOB_INCREF(iob) { iob->users++; }

/*
 * Iobuf data storage management 
 *
 * IOS_IOB_RESERVE - increase the headroom of an empty iobuf
 */
#define IOS_IOB_RESERVE(iob, len) { iob->data += len; }

/*
 * This function extends the used data area of the iobuf. It reduces the tail 
 * room of the iobuf. A pointer to the first byte of the extra data is returned
 */
u8 * IOS_PutIob (IOSIobuf *iob, u16 len);

/*
 * This function extends the used data area of the buffer at the buffer start. 
 * It reduces the head room of the iobuf. A pointer to the first byte of the 
 * extra data is returned.
 */
u8 * IOS_PushIob (IOSIobuf *iob, u16 len);

/*
 * This function removes data from the start of a buffer, returning the memory 
 * to the head room. A pointer to the next data in the buffer is returned. Once 
 * the data has been pulled, future pushes will overwrite the old data.
 */
u8 * IOS_PullIob (IOSIobuf *iob, u16 len);

/*
 * Max iobuf size
 *
 * Tells you the maximum length of the iobuf associated with a header
 * (not its current data size)
 *
 * iob - an iobuf header with an associated buffer.
 *
 * This routine can be used with IOS_IOB_NEXT to find the maximum data
 * in a chain.
 */
#define IOS_GET_IOB_MAX_BUF(iob) (iob->bufLen)

enum IOSIobPoolId_enum {POOL_DEFAULT, POOL_TEST1};
typedef enum IOSIobPoolId_enum IOSIobPoolId;

IOSError IOS_AccessIobPool(IOSIobPoolId pool);
#endif /* __IOS_IOBUF_H__ */
