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
|
/*
* Copyright (C) 2007 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 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
FILE_LICENCE ( GPL2_OR_LATER );
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <ipxe/iobuf.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
/** @file
*
* Data transfer interfaces
*
*/
/**
* Dummy transfer metadata
*
* This gets passed to xfer_interface::deliver_iob() and equivalents
* when no metadata is available.
*/
static struct xfer_metadata dummy_metadata;
/*****************************************************************************
*
* Data transfer interface operations
*
*/
/**
* Send redirection event
*
* @v intf Data transfer interface
* @v type New location type
* @v args Remaining arguments depend upon location type
* @ret rc Return status code
*/
int xfer_vredirect ( struct interface *intf, int type, va_list args ) {
struct interface *dest;
xfer_vredirect_TYPE ( void * ) *op =
intf_get_dest_op_no_passthru ( intf, xfer_vredirect, &dest );
void *object = intf_object ( dest );
int rc;
DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " redirect\n",
INTF_INTF_DBG ( intf, dest ) );
if ( op ) {
rc = op ( object, type, args );
} else {
/* Default is to reopen the interface as instructed */
rc = xfer_vreopen ( dest, type, args );
}
if ( rc != 0 ) {
DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " redirect "
"failed: %s\n", INTF_INTF_DBG ( intf, dest ),
strerror ( rc ) );
}
intf_put ( dest );
return rc;
}
/**
* Check flow control window
*
* @v intf Data transfer interface
* @ret len Length of window
*/
size_t xfer_window ( struct interface *intf ) {
struct interface *dest;
xfer_window_TYPE ( void * ) *op =
intf_get_dest_op ( intf, xfer_window, &dest );
void *object = intf_object ( dest );
size_t len;
if ( op ) {
len = op ( object );
} else {
/* Default is to provide an unlimited window */
len = ~( ( size_t ) 0 );
}
intf_put ( dest );
return len;
}
/**
* Allocate I/O buffer
*
* @v intf Data transfer interface
* @v len I/O buffer payload length
* @ret iobuf I/O buffer
*/
struct io_buffer * xfer_alloc_iob ( struct interface *intf, size_t len ) {
struct interface *dest;
xfer_alloc_iob_TYPE ( void * ) *op =
intf_get_dest_op ( intf, xfer_alloc_iob, &dest );
void *object = intf_object ( dest );
struct io_buffer *iobuf;
DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " alloc_iob %zd\n",
INTF_INTF_DBG ( intf, dest ), len );
if ( op ) {
iobuf = op ( object, len );
} else {
/* Default is to allocate an I/O buffer with no
* reserved space.
*/
iobuf = alloc_iob ( len );
}
if ( ! iobuf ) {
DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " alloc_iob "
"failed\n", INTF_INTF_DBG ( intf, dest ) );
}
intf_put ( dest );
return iobuf;
}
/**
* Deliver datagram
*
* @v intf Data transfer interface
* @v iobuf Datagram I/O buffer
* @v meta Data transfer metadata
* @ret rc Return status code
*/
int xfer_deliver ( struct interface *intf,
struct io_buffer *iobuf,
struct xfer_metadata *meta ) {
struct interface *dest;
xfer_deliver_TYPE ( void * ) *op =
intf_get_dest_op ( intf, xfer_deliver, &dest );
void *object = intf_object ( dest );
int rc;
DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " deliver %zd\n",
INTF_INTF_DBG ( intf, dest ), iob_len ( iobuf ) );
if ( op ) {
rc = op ( object, iobuf, meta );
} else {
/* Default is to discard the I/O buffer */
free_iob ( iobuf );
rc = -EPIPE;
}
if ( rc != 0 ) {
DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT
" deliver failed: %s\n",
INTF_INTF_DBG ( intf, dest ), strerror ( rc ) );
}
intf_put ( dest );
return rc;
}
/*****************************************************************************
*
* Data transfer interface helper functions
*
*/
/**
* Send redirection event
*
* @v intf Data transfer interface
* @v type New location type
* @v ... Remaining arguments depend upon location type
* @ret rc Return status code
*/
int xfer_redirect ( struct interface *intf, int type, ... ) {
va_list args;
int rc;
va_start ( args, type );
rc = xfer_vredirect ( intf, type, args );
va_end ( args );
return rc;
}
/**
* Deliver datagram as I/O buffer without metadata
*
* @v intf Data transfer interface
* @v iobuf Datagram I/O buffer
* @ret rc Return status code
*/
int xfer_deliver_iob ( struct interface *intf, struct io_buffer *iobuf ) {
return xfer_deliver ( intf, iobuf, &dummy_metadata );
}
/**
* Deliver datagram as raw data
*
* @v intf Data transfer interface
* @v iobuf Datagram I/O buffer
* @ret rc Return status code
*/
int xfer_deliver_raw ( struct interface *intf, const void *data, size_t len ) {
struct io_buffer *iobuf;
iobuf = xfer_alloc_iob ( intf, len );
if ( ! iobuf )
return -ENOMEM;
memcpy ( iob_put ( iobuf, len ), data, len );
return xfer_deliver_iob ( intf, iobuf );
}
/**
* Deliver formatted string
*
* @v intf Data transfer interface
* @v format Format string
* @v args Arguments corresponding to the format string
* @ret rc Return status code
*/
int xfer_vprintf ( struct interface *intf, const char *format,
va_list args ) {
size_t len;
va_list args_tmp;
va_copy ( args_tmp, args );
len = vsnprintf ( NULL, 0, format, args );
{
char buf[len + 1];
vsnprintf ( buf, sizeof ( buf ), format, args_tmp );
va_end ( args_tmp );
return xfer_deliver_raw ( intf, buf, len );
}
}
/**
* Deliver formatted string
*
* @v intf Data transfer interface
* @v format Format string
* @v ... Arguments corresponding to the format string
* @ret rc Return status code
*/
int xfer_printf ( struct interface *intf, const char *format, ... ) {
va_list args;
int rc;
va_start ( args, format );
rc = xfer_vprintf ( intf, format, args );
va_end ( args );
return rc;
}
/**
* Seek to position
*
* @v intf Data transfer interface
* @v offset Offset to new position
* @v whence Basis for new position
* @ret rc Return status code
*/
int xfer_seek ( struct interface *intf, off_t offset, int whence ) {
struct io_buffer *iobuf;
struct xfer_metadata meta = {
.offset = offset,
.whence = whence,
};
DBGC ( INTF_COL ( intf ), "INTF " INTF_FMT " seek %s+%ld\n",
INTF_DBG ( intf ), whence_text ( whence ), offset );
/* Allocate and send a zero-length data buffer */
iobuf = xfer_alloc_iob ( intf, 0 );
if ( ! iobuf )
return -ENOMEM;
return xfer_deliver ( intf, iobuf, &meta );
}
|