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. More...
 
static int xferbuf_ensure_size (struct xfer_buffer *xferbuf, size_t len)
 Ensure that data transfer buffer is large enough for the specified size. More...
 
int xferbuf_write (struct xfer_buffer *xferbuf, size_t offset, const void *data, size_t len)
 Write to data transfer buffer. More...
 
int xferbuf_read (struct xfer_buffer *xferbuf, size_t offset, void *data, size_t len)
 Read from data transfer buffer. More...
 
int xferbuf_deliver (struct xfer_buffer *xferbuf, struct io_buffer *iobuf, struct xfer_metadata *meta)
 Add received data to data transfer buffer. More...
 
static int xferbuf_malloc_realloc (struct xfer_buffer *xferbuf, size_t len)
 Reallocate malloc()-based data buffer. More...
 
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. More...
 
static void xferbuf_malloc_read (struct xfer_buffer *xferbuf, size_t offset, void *data, size_t len)
 Read data from malloc()-based data buffer. More...
 
static int xferbuf_umalloc_realloc (struct xfer_buffer *xferbuf, size_t len)
 Reallocate umalloc()-based data buffer. More...
 
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. More...
 
static void xferbuf_umalloc_read (struct xfer_buffer *xferbuf, size_t offset, void *data, size_t len)
 Read data from umalloc()-based data buffer. More...
 
struct xfer_bufferxfer_buffer (struct interface *intf)
 Get underlying data transfer buffer. More...
 

Variables

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

Detailed Description

Data transfer buffer.

Definition in file xferbuf.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ xferbuf_free()

void xferbuf_free ( struct xfer_buffer xferbuf)

Free data transfer buffer.

Parameters
xferbufData transfer buffer

Definition at line 58 of file xferbuf.c.

58  {
59 
60  xferbuf->op->realloc ( xferbuf, 0 );
61  xferbuf->len = 0;
62  xferbuf->pos = 0;
63 }
size_t pos
Current offset within data.
Definition: xferbuf.h:25
size_t len
Size of data.
Definition: xferbuf.h:23
struct xfer_buffer_operations * op
Data transfer buffer operations.
Definition: xferbuf.h:27
int(* realloc)(struct xfer_buffer *xferbuf, size_t len)
Reallocate data buffer.
Definition: xferbuf.h:38

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_ensure_size()

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.

72  {
73  int rc;
74 
75  /* If buffer is already large enough, do nothing */
76  if ( len <= xferbuf->len )
77  return 0;
78 
79  /* Extend buffer */
80  if ( ( rc = xferbuf->op->realloc ( xferbuf, len ) ) != 0 ) {
81  DBGC ( xferbuf, "XFERBUF %p could not extend buffer to "
82  "%zd bytes: %s\n", xferbuf, len, strerror ( rc ) );
83  return rc;
84  }
85  xferbuf->len = len;
86 
87  return 0;
88 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
size_t len
Size of data.
Definition: xferbuf.h:23
struct xfer_buffer_operations * op
Data transfer buffer operations.
Definition: xferbuf.h:27
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
uint32_t len
Length.
Definition: ena.h:14
int(* realloc)(struct xfer_buffer *xferbuf, size_t len)
Reallocate data buffer.
Definition: xferbuf.h:38

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

Referenced by xferbuf_write().

◆ xferbuf_write()

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.

99  {
100  size_t max_len;
101  int rc;
102 
103  /* Check for overflow */
104  max_len = ( offset + len );
105  if ( max_len < offset )
106  return -EOVERFLOW;
107 
108  /* Ensure buffer is large enough to contain this write */
109  if ( ( rc = xferbuf_ensure_size ( xferbuf, max_len ) ) != 0 )
110  return rc;
111 
112  /* Copy data to buffer */
113  profile_start ( &xferbuf_write_profiler );
114  xferbuf->op->write ( xferbuf, offset, data, len );
115  profile_stop ( &xferbuf_write_profiler );
116 
117  return 0;
118 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static void size_t size_t max_len
Definition: entropy.h:153
static void profile_stop(struct profiler *profiler)
Stop profiling.
Definition: profile.h:171
void(* write)(struct xfer_buffer *xferbuf, size_t offset, const void *data, size_t len)
Write data to buffer.
Definition: xferbuf.h:50
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
static void profile_start(struct profiler *profiler)
Start profiling.
Definition: profile.h:158
struct xfer_buffer_operations * op
Data transfer buffer operations.
Definition: xferbuf.h:27
#define EOVERFLOW
Value too large to be stored in data type.
Definition: errno.h:609
uint32_t len
Length.
Definition: ena.h:14
static int xferbuf_ensure_size(struct xfer_buffer *xferbuf, size_t len)
Ensure that data transfer buffer is large enough for the specified size.
Definition: xferbuf.c:72
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12

References data, EOVERFLOW, len, max_len, offset, 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().

◆ xferbuf_read()

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.

129  {
130 
131  /* Check that read is within buffer range */
132  if ( ( offset > xferbuf->len ) ||
133  ( len > ( xferbuf->len - offset ) ) )
134  return -ENOENT;
135 
136  /* Copy data from buffer */
137  profile_start ( &xferbuf_read_profiler );
138  xferbuf->op->read ( xferbuf, offset, data, len );
139  profile_stop ( &xferbuf_read_profiler );
140 
141  return 0;
142 }
void(* read)(struct xfer_buffer *xferbuf, size_t offset, void *data, size_t len)
Read data from buffer.
Definition: xferbuf.h:63
#define ENOENT
No such file or directory.
Definition: errno.h:514
static void profile_stop(struct profiler *profiler)
Stop profiling.
Definition: profile.h:171
size_t len
Size of data.
Definition: xferbuf.h:23
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
static void profile_start(struct profiler *profiler)
Start profiling.
Definition: profile.h:158
struct xfer_buffer_operations * op
Data transfer buffer operations.
Definition: xferbuf.h:27
uint32_t len
Length.
Definition: ena.h:14
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12

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

Referenced by peerblk_decrypt_read().

◆ xferbuf_deliver()

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.

153  {
154  size_t len = iob_len ( iobuf );
155  size_t pos;
156  int rc;
157 
158  /* Start profiling */
159  profile_start ( &xferbuf_deliver_profiler );
160 
161  /* Calculate new buffer position */
162  pos = xferbuf->pos;
163  if ( meta->flags & XFER_FL_ABS_OFFSET )
164  pos = 0;
165  pos += meta->offset;
166 
167  /* Write data to buffer */
168  if ( ( rc = xferbuf_write ( xferbuf, pos, iobuf->data, len ) ) != 0 )
169  goto done;
170 
171  /* Update current buffer position */
172  xferbuf->pos = ( pos + len );
173 
174  done:
175  free_iob ( iobuf );
176  profile_stop ( &xferbuf_deliver_profiler );
177  return rc;
178 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define XFER_FL_ABS_OFFSET
Offset is absolute.
Definition: xfer.h:47
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:145
size_t pos
Current offset within data.
Definition: xferbuf.h:25
static void profile_stop(struct profiler *profiler)
Stop profiling.
Definition: profile.h:171
int xferbuf_write(struct xfer_buffer *xferbuf, size_t offset, const void *data, size_t len)
Write to data transfer buffer.
Definition: xferbuf.c:98
static void profile_start(struct profiler *profiler)
Start profiling.
Definition: profile.h:158
int meta(WINDOW *, bool)
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:151
uint32_t len
Length.
Definition: ena.h:14
void * data
Start of data.
Definition: iobuf.h:44
struct bofm_section_header done
Definition: bofm_test.c:46

References io_buffer::data, done, free_iob(), iob_len(), len, meta(), 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().

◆ xferbuf_malloc_realloc()

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.

187  {
188  void *new_data;
189 
190  new_data = realloc ( xferbuf->data, len );
191  if ( ! new_data )
192  return -ENOSPC;
193  xferbuf->data = new_data;
194  return 0;
195 }
void * data
Data.
Definition: xferbuf.h:21
#define ENOSPC
No space left on device.
Definition: errno.h:549
uint32_t len
Length.
Definition: ena.h:14
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition: malloc.c:521

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

◆ xferbuf_malloc_write()

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.

206  {
207 
208  memcpy ( ( xferbuf->data + offset ), data, len );
209 }
void * data
Data.
Definition: xferbuf.h:21
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
uint32_t len
Length.
Definition: ena.h:14
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12

References data, xfer_buffer::data, len, memcpy(), and offset.

◆ xferbuf_malloc_read()

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.

220  {
221 
222  memcpy ( data, ( xferbuf->data + offset ), len );
223 }
void * data
Data.
Definition: xferbuf.h:21
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
uint32_t len
Length.
Definition: ena.h:14
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12

References data, xfer_buffer::data, len, memcpy(), and offset.

◆ xferbuf_umalloc_realloc()

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.

239  {
240  userptr_t *udata = xferbuf->data;
241  userptr_t new_udata;
242 
243  new_udata = urealloc ( *udata, len );
244  if ( ! new_udata )
245  return -ENOSPC;
246  *udata = new_udata;
247  return 0;
248 }
void * data
Data.
Definition: xferbuf.h:21
userptr_t urealloc(userptr_t userptr, size_t new_size)
Reallocate external memory.
#define ENOSPC
No space left on device.
Definition: errno.h:549
uint32_t len
Length.
Definition: ena.h:14
unsigned long userptr_t
A pointer to a user buffer.
Definition: uaccess.h:33

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

◆ xferbuf_umalloc_write()

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.

259  {
260  userptr_t *udata = xferbuf->data;
261 
262  copy_to_user ( *udata, offset, data, len );
263 }
void * data
Data.
Definition: xferbuf.h:21
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
static __always_inline void copy_to_user(userptr_t dest, off_t dest_off, const void *src, size_t len)
Copy data to user buffer.
Definition: uaccess.h:324
uint32_t len
Length.
Definition: ena.h:14
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
unsigned long userptr_t
A pointer to a user buffer.
Definition: uaccess.h:33

References copy_to_user(), data, xfer_buffer::data, len, and offset.

◆ xferbuf_umalloc_read()

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.

274  {
275  userptr_t *udata = xferbuf->data;
276 
277  copy_from_user ( data, *udata, offset, len );
278 }
void * data
Data.
Definition: xferbuf.h:21
static __always_inline void copy_from_user(void *dest, userptr_t src, off_t src_off, size_t len)
Copy data from user buffer.
Definition: uaccess.h:337
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
uint32_t len
Length.
Definition: ena.h:14
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
unsigned long userptr_t
A pointer to a user buffer.
Definition: uaccess.h:33

References copy_from_user(), data, xfer_buffer::data, len, and offset.

◆ xfer_buffer()

struct xfer_buffer* xfer_buffer ( struct interface intf)

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.

301  {
302  struct interface *dest;
303  xfer_buffer_TYPE ( void * ) *op =
304  intf_get_dest_op ( intf, xfer_buffer, &dest );
305  void *object = intf_object ( dest );
306  struct interface *xfer_deliver_dest;
307  struct xfer_buffer *xferbuf;
308 
309  /* Check that this operation is provided by the same interface
310  * which handles xfer_deliver().
311  */
312  ( void ) intf_get_dest_op ( intf, xfer_deliver, &xfer_deliver_dest );
313 
314  if ( op && ( dest == xfer_deliver_dest ) ) {
315  xferbuf = op ( object );
316  } else {
317  /* Default is to not have a data transfer buffer */
318  xferbuf = NULL;
319  }
320 
321  intf_put ( xfer_deliver_dest );
322  intf_put ( dest );
323  return xferbuf;
324 }
A data transfer buffer.
Definition: xferbuf.h:19
void * intf_object(struct interface *intf)
Get pointer to object containing object interface.
Definition: interface.c:154
An object interface.
Definition: interface.h:109
#define xfer_buffer_TYPE(object_type)
Definition: xferbuf.h:102
static void * dest
Definition: strings.h:176
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition: xfer.c:193
static uint16_t struct vmbus_xfer_pages_operations * op
Definition: netvsc.h:327
void intf_put(struct interface *intf)
Decrement reference count on an object interface.
Definition: interface.c:144
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
#define intf_get_dest_op(intf, type, dest)
Get object interface destination and operation method.
Definition: interface.h:214

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().

Variable Documentation

◆ __profiler

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.

◆ xferbuf_malloc_operations

struct xfer_buffer_operations xferbuf_malloc_operations
Initial value:
= {
}
static int xferbuf_malloc_realloc(struct xfer_buffer *xferbuf, size_t len)
Reallocate malloc()-based data buffer.
Definition: xferbuf.c:187
static void xferbuf_malloc_read(struct xfer_buffer *xferbuf, size_t offset, void *data, size_t len)
Read data from malloc()-based data buffer.
Definition: xferbuf.c:219
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.
Definition: xferbuf.c:205

malloc()-based data buffer operations

Definition at line 226 of file xferbuf.c.

Referenced by __attribute__().

◆ xferbuf_umalloc_operations

struct xfer_buffer_operations xferbuf_umalloc_operations
Initial value:
= {
}
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.
Definition: xferbuf.c:258
static int xferbuf_umalloc_realloc(struct xfer_buffer *xferbuf, size_t len)
Reallocate umalloc()-based data buffer.
Definition: xferbuf.c:239
static void xferbuf_umalloc_read(struct xfer_buffer *xferbuf, size_t offset, void *data, size_t len)
Read data from umalloc()-based data buffer.
Definition: xferbuf.c:273

umalloc()-based data buffer operations

Definition at line 281 of file xferbuf.c.