2007-04-27 00:44:52 +02:00
|
|
|
#ifndef _GPXE_IOBUF_H
|
|
|
|
#define _GPXE_IOBUF_H
|
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* I/O buffers
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <gpxe/list.h>
|
|
|
|
|
2007-05-18 16:45:16 +02:00
|
|
|
/**
|
|
|
|
* I/O buffer alignment
|
|
|
|
*
|
|
|
|
* I/O buffers allocated via alloc_iob() are guaranteed to be
|
|
|
|
* physically aligned to this boundary. Some cards cannot DMA across
|
|
|
|
* a 4kB boundary. With a standard Ethernet MTU, aligning to a 2kB
|
|
|
|
* boundary is sufficient to guarantee no 4kB boundary crossings. For
|
|
|
|
* a jumbo Ethernet MTU, a packet may be larger than 4kB anyway.
|
|
|
|
*/
|
|
|
|
#define IOB_ALIGN 2048
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Minimum I/O buffer length
|
|
|
|
*
|
|
|
|
* alloc_iob() will round up the allocated length to this size if
|
|
|
|
* necessary. This is used on behalf of hardware that is not capable
|
|
|
|
* of auto-padding.
|
|
|
|
*/
|
|
|
|
#define IOB_ZLEN 64
|
|
|
|
|
2007-04-27 00:44:52 +02:00
|
|
|
/**
|
|
|
|
* A persistent I/O buffer
|
|
|
|
*
|
|
|
|
* This data structure encapsulates a long-lived I/O buffer. The
|
|
|
|
* buffer may be passed between multiple owners, queued for possible
|
|
|
|
* retransmission, etc.
|
|
|
|
*/
|
|
|
|
struct io_buffer {
|
|
|
|
/** List of which this buffer is a member
|
|
|
|
*
|
|
|
|
* The list must belong to the current owner of the buffer.
|
|
|
|
* Different owners may maintain different lists (e.g. a
|
|
|
|
* retransmission list for TCP).
|
|
|
|
*/
|
|
|
|
struct list_head list;
|
|
|
|
|
|
|
|
/** Start of the buffer */
|
|
|
|
void *head;
|
|
|
|
/** Start of data */
|
|
|
|
void *data;
|
|
|
|
/** End of data */
|
|
|
|
void *tail;
|
|
|
|
/** End of the buffer */
|
|
|
|
void *end;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reserve space at start of I/O buffer
|
|
|
|
*
|
2007-05-18 16:45:16 +02:00
|
|
|
* @v iobuf I/O buffer
|
2007-04-27 00:44:52 +02:00
|
|
|
* @v len Length to reserve
|
|
|
|
* @ret data Pointer to new start of buffer
|
|
|
|
*/
|
2007-05-18 16:45:16 +02:00
|
|
|
static inline void * iob_reserve ( struct io_buffer *iobuf, size_t len ) {
|
|
|
|
iobuf->data += len;
|
|
|
|
iobuf->tail += len;
|
|
|
|
return iobuf->data;
|
2007-04-27 00:44:52 +02:00
|
|
|
}
|
2007-09-17 19:38:04 +02:00
|
|
|
#define iob_reserve( iobuf, len ) ( { \
|
|
|
|
void *__result; \
|
|
|
|
__result = iob_reserve ( (iobuf), (len) ); \
|
|
|
|
assert ( (iobuf)->tail <= (iobuf)->end ); \
|
|
|
|
__result; } )
|
2007-04-27 00:44:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add data to start of I/O buffer
|
|
|
|
*
|
2007-05-18 16:45:16 +02:00
|
|
|
* @v iobuf I/O buffer
|
2007-04-27 00:44:52 +02:00
|
|
|
* @v len Length to add
|
|
|
|
* @ret data Pointer to new start of buffer
|
|
|
|
*/
|
2007-05-18 16:45:16 +02:00
|
|
|
static inline void * iob_push ( struct io_buffer *iobuf, size_t len ) {
|
|
|
|
iobuf->data -= len;
|
|
|
|
return iobuf->data;
|
2007-04-27 00:44:52 +02:00
|
|
|
}
|
2007-09-17 19:38:04 +02:00
|
|
|
#define iob_push( iobuf, len ) ( { \
|
|
|
|
void *__result; \
|
|
|
|
__result = iob_push ( (iobuf), (len) ); \
|
|
|
|
assert ( (iobuf)->data >= (iobuf)->head ); \
|
|
|
|
__result; } )
|
2007-04-27 00:44:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove data from start of I/O buffer
|
|
|
|
*
|
2007-05-18 16:45:16 +02:00
|
|
|
* @v iobuf I/O buffer
|
2007-04-27 00:44:52 +02:00
|
|
|
* @v len Length to remove
|
|
|
|
* @ret data Pointer to new start of buffer
|
|
|
|
*/
|
2007-05-18 16:45:16 +02:00
|
|
|
static inline void * iob_pull ( struct io_buffer *iobuf, size_t len ) {
|
|
|
|
iobuf->data += len;
|
|
|
|
assert ( iobuf->data <= iobuf->tail );
|
|
|
|
return iobuf->data;
|
2007-04-27 00:44:52 +02:00
|
|
|
}
|
2007-09-17 19:38:04 +02:00
|
|
|
#define iob_pull( iobuf, len ) ( { \
|
|
|
|
void *__result; \
|
|
|
|
__result = iob_pull ( (iobuf), (len) ); \
|
|
|
|
assert ( (iobuf)->data <= (iobuf)->tail ); \
|
|
|
|
__result; } )
|
2007-04-27 00:44:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add data to end of I/O buffer
|
|
|
|
*
|
2007-05-18 16:45:16 +02:00
|
|
|
* @v iobuf I/O buffer
|
2007-04-27 00:44:52 +02:00
|
|
|
* @v len Length to add
|
|
|
|
* @ret data Pointer to newly added space
|
|
|
|
*/
|
2007-05-18 16:45:16 +02:00
|
|
|
static inline void * iob_put ( struct io_buffer *iobuf, size_t len ) {
|
|
|
|
void *old_tail = iobuf->tail;
|
|
|
|
iobuf->tail += len;
|
2007-04-27 00:44:52 +02:00
|
|
|
return old_tail;
|
|
|
|
}
|
2007-09-17 19:38:04 +02:00
|
|
|
#define iob_put( iobuf, len ) ( { \
|
|
|
|
void *__result; \
|
|
|
|
__result = iob_put ( (iobuf), (len) ); \
|
|
|
|
assert ( (iobuf)->tail <= (iobuf)->end ); \
|
|
|
|
__result; } )
|
2007-04-27 00:44:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove data from end of I/O buffer
|
|
|
|
*
|
2007-05-18 16:45:16 +02:00
|
|
|
* @v iobuf I/O buffer
|
2007-04-27 00:44:52 +02:00
|
|
|
* @v len Length to remove
|
|
|
|
*/
|
2007-05-18 16:45:16 +02:00
|
|
|
static inline void iob_unput ( struct io_buffer *iobuf, size_t len ) {
|
|
|
|
iobuf->tail -= len;
|
2007-04-27 00:44:52 +02:00
|
|
|
}
|
2007-09-17 19:38:04 +02:00
|
|
|
#define iob_unput( iobuf, len ) do { \
|
|
|
|
iob_unput ( (iobuf), (len) ); \
|
|
|
|
assert ( (iobuf)->tail >= (iobuf)->data ); \
|
|
|
|
} while ( 0 )
|
2007-04-27 00:44:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Empty an I/O buffer
|
|
|
|
*
|
2007-05-18 16:45:16 +02:00
|
|
|
* @v iobuf I/O buffer
|
2007-04-27 00:44:52 +02:00
|
|
|
*/
|
2007-05-18 16:45:16 +02:00
|
|
|
static inline void iob_empty ( struct io_buffer *iobuf ) {
|
|
|
|
iobuf->tail = iobuf->data;
|
2007-04-27 00:44:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate length of data in an I/O buffer
|
|
|
|
*
|
2007-05-18 16:45:16 +02:00
|
|
|
* @v iobuf I/O buffer
|
2007-04-27 00:44:52 +02:00
|
|
|
* @ret len Length of data in buffer
|
|
|
|
*/
|
2007-05-18 16:45:16 +02:00
|
|
|
static inline size_t iob_len ( struct io_buffer *iobuf ) {
|
|
|
|
return ( iobuf->tail - iobuf->data );
|
2007-04-27 00:44:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate available space at start of an I/O buffer
|
|
|
|
*
|
2007-05-18 16:45:16 +02:00
|
|
|
* @v iobuf I/O buffer
|
2007-04-27 00:44:52 +02:00
|
|
|
* @ret len Length of data available at start of buffer
|
|
|
|
*/
|
2007-05-18 16:45:16 +02:00
|
|
|
static inline size_t iob_headroom ( struct io_buffer *iobuf ) {
|
|
|
|
return ( iobuf->data - iobuf->head );
|
2007-04-27 00:44:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate available space at end of an I/O buffer
|
|
|
|
*
|
2007-05-18 16:45:16 +02:00
|
|
|
* @v iobuf I/O buffer
|
2007-04-27 00:44:52 +02:00
|
|
|
* @ret len Length of data available at end of buffer
|
|
|
|
*/
|
2007-05-18 16:45:16 +02:00
|
|
|
static inline size_t iob_tailroom ( struct io_buffer *iobuf ) {
|
|
|
|
return ( iobuf->end - iobuf->tail );
|
2007-04-27 00:44:52 +02:00
|
|
|
}
|
|
|
|
|
2007-08-20 20:29:23 +02:00
|
|
|
extern struct io_buffer * __malloc alloc_iob ( size_t len );
|
2007-05-18 16:45:16 +02:00
|
|
|
extern void free_iob ( struct io_buffer *iobuf );
|
|
|
|
extern void iob_pad ( struct io_buffer *iobuf, size_t min_len );
|
2007-07-24 18:11:31 +02:00
|
|
|
extern int iob_ensure_headroom ( struct io_buffer *iobuf, size_t len );
|
2007-04-27 00:44:52 +02:00
|
|
|
|
|
|
|
#endif /* _GPXE_IOBUF_H */
|