iPXE
Functions | Variables
xferbuf.c File Reference

Data transfer buffer. More...

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ipxe/xfer.h>
#include <ipxe/iobuf.h>
#include <ipxe/umalloc.h>
#include <ipxe/profile.h>
#include <ipxe/xferbuf.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
void xferbuf_free (struct xfer_buffer *xferbuf)
 Free data transfer buffer.
static int xferbuf_ensure_size (struct xfer_buffer *xferbuf, size_t len)
 Ensure that data transfer buffer is large enough for the specified size.
int xferbuf_write (struct xfer_buffer *xferbuf, size_t offset, const void *data, size_t len)
 Write to data transfer buffer.
int xferbuf_read (struct xfer_buffer *xferbuf, size_t offset, void *data, size_t len)
 Read from data transfer buffer.
int xferbuf_deliver (struct xfer_buffer *xferbuf, struct io_buffer *iobuf, struct xfer_metadata *meta)
 Add received data to data transfer buffer.
static int xferbuf_malloc_realloc (struct xfer_buffer *xferbuf, size_t len)
 Reallocate malloc()-based data buffer.
static void xferbuf_malloc_write (struct xfer_buffer *xferbuf, size_t offset, const void *data, size_t len)
 Write data to malloc()-based data buffer.
static void xferbuf_malloc_read (struct xfer_buffer *xferbuf, size_t offset, void *data, size_t len)
 Read data from malloc()-based data buffer.
static int xferbuf_umalloc_realloc (struct xfer_buffer *xferbuf, size_t len)
 Reallocate umalloc()-based data buffer.
static void xferbuf_umalloc_write (struct xfer_buffer *xferbuf, size_t offset, const void *data, size_t len)
 Write data to umalloc()-based data buffer.
static void xferbuf_umalloc_read (struct xfer_buffer *xferbuf, size_t offset, void *data, size_t len)
 Read data from umalloc()-based data buffer.
struct xfer_bufferxfer_buffer (struct interface *intf)
 Get underlying data transfer buffer.

Variables

static struct profiler
xferbuf_deliver_profiler 
__profiler
 Data delivery profiler.
struct xfer_buffer_operations xferbuf_malloc_operations
 malloc()-based data buffer operations
struct xfer_buffer_operations xferbuf_umalloc_operations
 umalloc()-based data buffer operations

Detailed Description

Data transfer buffer.

Definition in file xferbuf.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
void xferbuf_free ( struct xfer_buffer xferbuf)

Free data transfer buffer.

Parameters:
xferbufData transfer buffer

Definition at line 58 of file xferbuf.c.

References xfer_buffer::len, xfer_buffer::op, xfer_buffer::pos, and xfer_buffer_operations::realloc.

Referenced by peerblk_reset(), peermux_free(), validator_free(), and validator_xfer_close().

                                                  {

        xferbuf->op->realloc ( xferbuf, 0 );
        xferbuf->len = 0;
        xferbuf->pos = 0;
}
static int xferbuf_ensure_size ( struct xfer_buffer xferbuf,
size_t  len 
) [static]

Ensure that data transfer buffer is large enough for the specified size.

Parameters:
xferbufData transfer buffer
lenRequired minimum size
Return values:
rcReturn status code

Definition at line 72 of file xferbuf.c.

References DBGC, xfer_buffer::len, len, xfer_buffer::op, rc, xfer_buffer_operations::realloc, and strerror().

Referenced by xferbuf_write().

                                                                           {
        int rc;

        /* If buffer is already large enough, do nothing */
        if ( len <= xferbuf->len )
                return 0;

        /* Extend buffer */
        if ( ( rc = xferbuf->op->realloc ( xferbuf, len ) ) != 0 ) {
                DBGC ( xferbuf, "XFERBUF %p could not extend buffer to "
                       "%zd bytes: %s\n", xferbuf, len, strerror ( rc ) );
                return rc;
        }
        xferbuf->len = len;

        return 0;
}
int xferbuf_write ( struct xfer_buffer xferbuf,
size_t  offset,
const void *  data,
size_t  len 
)

Write to data transfer buffer.

Parameters:
xferbufData transfer buffer
offsetStarting offset
dataData to write
lenLength of data

Definition at line 98 of file xferbuf.c.

References EOVERFLOW, len, max_len, xfer_buffer::op, profile_start(), profile_stop(), rc, xfer_buffer_operations::write, and xferbuf_ensure_size().

Referenced by peerblk_decrypt_write(), peerblk_retrieval_rx(), and xferbuf_deliver().

                                                   {
        size_t max_len;
        int rc;

        /* Check for overflow */
        max_len = ( offset + len );
        if ( max_len < offset )
                return -EOVERFLOW;

        /* Ensure buffer is large enough to contain this write */
        if ( ( rc = xferbuf_ensure_size ( xferbuf, max_len ) ) != 0 )
                return rc;

        /* Copy data to buffer */
        profile_start ( &xferbuf_write_profiler );
        xferbuf->op->write ( xferbuf, offset, data, len );
        profile_stop ( &xferbuf_write_profiler );

        return 0;
}
int xferbuf_read ( struct xfer_buffer xferbuf,
size_t  offset,
void *  data,
size_t  len 
)

Read from data transfer buffer.

Parameters:
xferbufData transfer buffer
offsetStarting offset
dataData to write
lenLength of data

Definition at line 128 of file xferbuf.c.

References ENOENT, xfer_buffer::len, xfer_buffer::op, profile_start(), profile_stop(), and xfer_buffer_operations::read.

Referenced by peerblk_decrypt_read().

                                            {

        /* Check that read is within buffer range */
        if ( ( offset > xferbuf->len ) ||
             ( len > ( xferbuf->len - offset ) ) )
                return -ENOENT;

        /* Copy data from buffer */
        profile_start ( &xferbuf_read_profiler );
        xferbuf->op->read ( xferbuf, offset, data, len );
        profile_stop ( &xferbuf_read_profiler );

        return 0;
}
int xferbuf_deliver ( struct xfer_buffer xferbuf,
struct io_buffer iobuf,
struct xfer_metadata meta 
)

Add received data to data transfer buffer.

Parameters:
xferbufData transfer buffer
iobufI/O buffer
metaData transfer metadata
Return values:
rcReturn status code

Definition at line 152 of file xferbuf.c.

References io_buffer::data, done, xfer_metadata::flags, free_iob(), iob_len(), len, xfer_metadata::offset, xfer_buffer::pos, profile_start(), profile_stop(), rc, XFER_FL_ABS_OFFSET, and xferbuf_write().

Referenced by blktrans_deliver(), downloader_deliver(), efi_pxe_tftp_deliver(), peermux_info_deliver(), and validator_xfer_deliver().

                                                   {
        size_t len = iob_len ( iobuf );
        size_t pos;
        int rc;

        /* Start profiling */
        profile_start ( &xferbuf_deliver_profiler );

        /* Calculate new buffer position */
        pos = xferbuf->pos;
        if ( meta->flags & XFER_FL_ABS_OFFSET )
                pos = 0;
        pos += meta->offset;

        /* Write data to buffer */
        if ( ( rc = xferbuf_write ( xferbuf, pos, iobuf->data, len ) ) != 0 )
                goto done;

        /* Update current buffer position */
        xferbuf->pos = ( pos + len );

 done:
        free_iob ( iobuf );
        profile_stop ( &xferbuf_deliver_profiler );
        return rc;
}
static int xferbuf_malloc_realloc ( struct xfer_buffer xferbuf,
size_t  len 
) [static]

Reallocate malloc()-based data buffer.

Parameters:
xferbufData transfer buffer
lenNew length (or zero to free buffer)
Return values:
rcReturn status code

Definition at line 187 of file xferbuf.c.

References xfer_buffer::data, ENOSPC, and realloc().

                                                                              {
        void *new_data;

        new_data = realloc ( xferbuf->data, len );
        if ( ! new_data )
                return -ENOSPC;
        xferbuf->data = new_data;
        return 0;
}
static void xferbuf_malloc_write ( struct xfer_buffer xferbuf,
size_t  offset,
const void *  data,
size_t  len 
) [static]

Write data to malloc()-based data buffer.

Parameters:
xferbufData transfer buffer
offsetStarting offset
dataData to copy
lenLength of data

Definition at line 205 of file xferbuf.c.

References xfer_buffer::data, and memcpy().

                                                                  {

        memcpy ( ( xferbuf->data + offset ), data, len );
}
static void xferbuf_malloc_read ( struct xfer_buffer xferbuf,
size_t  offset,
void *  data,
size_t  len 
) [static]

Read data from malloc()-based data buffer.

Parameters:
xferbufData transfer buffer
offsetStarting offset
dataData to read
lenLength of data

Definition at line 219 of file xferbuf.c.

References xfer_buffer::data, and memcpy().

                                                           {

        memcpy ( data, ( xferbuf->data + offset ), len );
}
static int xferbuf_umalloc_realloc ( struct xfer_buffer xferbuf,
size_t  len 
) [static]

Reallocate umalloc()-based data buffer.

Parameters:
xferbufData transfer buffer
lenNew length (or zero to free buffer)
Return values:
rcReturn status code

Definition at line 239 of file xferbuf.c.

References xfer_buffer::data, ENOSPC, and urealloc().

                                                                               {
        userptr_t *udata = xferbuf->data;
        userptr_t new_udata;

        new_udata = urealloc ( *udata, len );
        if ( ! new_udata )
                return -ENOSPC;
        *udata = new_udata;
        return 0;
}
static void xferbuf_umalloc_write ( struct xfer_buffer xferbuf,
size_t  offset,
const void *  data,
size_t  len 
) [static]

Write data to umalloc()-based data buffer.

Parameters:
xferbufData transfer buffer
offsetStarting offset
dataData to copy
lenLength of data

Definition at line 258 of file xferbuf.c.

References copy_to_user(), and xfer_buffer::data.

                                                                   {
        userptr_t *udata = xferbuf->data;

        copy_to_user ( *udata, offset, data, len );
}
static void xferbuf_umalloc_read ( struct xfer_buffer xferbuf,
size_t  offset,
void *  data,
size_t  len 
) [static]

Read data from umalloc()-based data buffer.

Parameters:
xferbufData transfer buffer
offsetStarting offset
dataData to read
lenLength of data

Definition at line 273 of file xferbuf.c.

References copy_from_user(), and xfer_buffer::data.

                                                            {
        userptr_t *udata = xferbuf->data;

        copy_from_user ( data, *udata, offset, len );
}
struct xfer_buffer* xfer_buffer ( struct interface intf) [read]

Get underlying data transfer buffer.

Parameters:
interfaceData transfer interface
Return values:
xferbufData transfer buffer, or NULL on error

This call will check that the xfer_buffer() handler belongs to the destination interface which also provides xfer_deliver() for this interface.

This is done to prevent accidental accesses to a data transfer buffer which may be located behind a non-transparent datapath via a series of pass-through interfaces.

Definition at line 301 of file xferbuf.c.

References dest, intf_get_dest_op, intf_object(), intf_put(), NULL, op, xfer_buffer_TYPE, and xfer_deliver().

Referenced by http_content_buffer(), peerblk_decrypt(), and peermux_block_buffer().

                                                            {
        struct interface *dest;
        xfer_buffer_TYPE ( void * ) *op =
                intf_get_dest_op ( intf, xfer_buffer, &dest );
        void *object = intf_object ( dest );
        struct interface *xfer_deliver_dest;
        struct xfer_buffer *xferbuf;

        /* Check that this operation is provided by the same interface
         * which handles xfer_deliver().
         */
        ( void ) intf_get_dest_op ( intf, xfer_deliver, &xfer_deliver_dest );

        if ( op && ( dest == xfer_deliver_dest ) ) {
                xferbuf = op ( object );
        } else {
                /* Default is to not have a data transfer buffer */
                xferbuf = NULL;
        }

        intf_put ( xfer_deliver_dest );
        intf_put ( dest );
        return xferbuf;
}

Variable Documentation

struct profiler xferbuf_read_profiler __profiler [static]
Initial value:
        { .name = "xferbuf.deliver" }

Data delivery profiler.

Data read profiler.

Data write profiler.

Definition at line 42 of file xferbuf.c.

Initial value:

malloc()-based data buffer operations

Definition at line 226 of file xferbuf.c.

Referenced by __attribute__().

Initial value:

umalloc()-based data buffer operations

Definition at line 281 of file xferbuf.c.