1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
/*
* Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#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>
/** @file
*
* Data transfer buffer
*
*/
/** Data delivery profiler */
static struct profiler xferbuf_deliver_profiler __profiler =
{ .name = "xferbuf.deliver" };
/** Data write profiler */
static struct profiler xferbuf_write_profiler __profiler =
{ .name = "xferbuf.write" };
/** Data read profiler */
static struct profiler xferbuf_read_profiler __profiler =
{ .name = "xferbuf.read" };
/**
* Free data transfer buffer
*
* @v xferbuf Data transfer buffer
*/
void xferbuf_free ( struct xfer_buffer *xferbuf ) {
xferbuf->op->realloc ( xferbuf, 0 );
xferbuf->len = 0;
xferbuf->pos = 0;
}
/**
* Ensure that data transfer buffer is large enough for the specified size
*
* @v xferbuf Data transfer buffer
* @v len Required minimum size
* @ret rc Return status code
*/
static int xferbuf_ensure_size ( struct xfer_buffer *xferbuf, size_t len ) {
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;
}
/**
* Write to data transfer buffer
*
* @v xferbuf Data transfer buffer
* @v offset Starting offset
* @v data Data to write
* @v len Length of data
*/
int xferbuf_write ( struct xfer_buffer *xferbuf, size_t offset,
const void *data, size_t len ) {
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;
}
/**
* Read from data transfer buffer
*
* @v xferbuf Data transfer buffer
* @v offset Starting offset
* @v data Data to write
* @v len Length of data
*/
int xferbuf_read ( struct xfer_buffer *xferbuf, size_t offset,
void *data, size_t len ) {
/* 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;
}
/**
* Add received data to data transfer buffer
*
* @v xferbuf Data transfer buffer
* @v iobuf I/O buffer
* @v meta Data transfer metadata
* @ret rc Return status code
*/
int xferbuf_deliver ( struct xfer_buffer *xferbuf, struct io_buffer *iobuf,
struct xfer_metadata *meta ) {
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;
}
/**
* Reallocate malloc()-based data buffer
*
* @v xferbuf Data transfer buffer
* @v len New length (or zero to free buffer)
* @ret rc Return status code
*/
static int xferbuf_malloc_realloc ( struct xfer_buffer *xferbuf, size_t len ) {
void *new_data;
new_data = realloc ( xferbuf->data, len );
if ( ! new_data )
return -ENOSPC;
xferbuf->data = new_data;
return 0;
}
/**
* Write data to malloc()-based data buffer
*
* @v xferbuf Data transfer buffer
* @v offset Starting offset
* @v data Data to copy
* @v len Length of data
*/
static void xferbuf_malloc_write ( struct xfer_buffer *xferbuf, size_t offset,
const void *data, size_t len ) {
memcpy ( ( xferbuf->data + offset ), data, len );
}
/**
* Read data from malloc()-based data buffer
*
* @v xferbuf Data transfer buffer
* @v offset Starting offset
* @v data Data to read
* @v len Length of data
*/
static void xferbuf_malloc_read ( struct xfer_buffer *xferbuf, size_t offset,
void *data, size_t len ) {
memcpy ( data, ( xferbuf->data + offset ), len );
}
/** malloc()-based data buffer operations */
struct xfer_buffer_operations xferbuf_malloc_operations = {
.realloc = xferbuf_malloc_realloc,
.write = xferbuf_malloc_write,
.read = xferbuf_malloc_read,
};
/**
* Reallocate umalloc()-based data buffer
*
* @v xferbuf Data transfer buffer
* @v len New length (or zero to free buffer)
* @ret rc Return status code
*/
static int xferbuf_umalloc_realloc ( struct xfer_buffer *xferbuf, size_t len ) {
userptr_t *udata = xferbuf->data;
userptr_t new_udata;
new_udata = urealloc ( *udata, len );
if ( ! new_udata )
return -ENOSPC;
*udata = new_udata;
return 0;
}
/**
* Write data to umalloc()-based data buffer
*
* @v xferbuf Data transfer buffer
* @v offset Starting offset
* @v data Data to copy
* @v len Length of data
*/
static void xferbuf_umalloc_write ( struct xfer_buffer *xferbuf, size_t offset,
const void *data, size_t len ) {
userptr_t *udata = xferbuf->data;
copy_to_user ( *udata, offset, data, len );
}
/**
* Read data from umalloc()-based data buffer
*
* @v xferbuf Data transfer buffer
* @v offset Starting offset
* @v data Data to read
* @v len Length of data
*/
static void xferbuf_umalloc_read ( struct xfer_buffer *xferbuf, size_t offset,
void *data, size_t len ) {
userptr_t *udata = xferbuf->data;
copy_from_user ( data, *udata, offset, len );
}
/** umalloc()-based data buffer operations */
struct xfer_buffer_operations xferbuf_umalloc_operations = {
.realloc = xferbuf_umalloc_realloc,
.write = xferbuf_umalloc_write,
.read = xferbuf_umalloc_read,
};
/**
* Get underlying data transfer buffer
*
* @v interface Data transfer interface
* @ret xferbuf Data 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.
*/
struct xfer_buffer * xfer_buffer ( struct interface *intf ) {
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().
*/
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;
}
|