summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/net/peermux.c
blob: 634c699929af77f9c0ed5353543bbc962323ea1f (plain)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * Copyright (C) 2015 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 <errno.h>
#include <ipxe/uri.h>
#include <ipxe/xferbuf.h>
#include <ipxe/peerblk.h>
#include <ipxe/peermux.h>

/** @file
 *
 * Peer Content Caching and Retrieval (PeerDist) protocol multiplexer
 *
 */

/**
 * Free PeerDist download multiplexer
 *
 * @v refcnt		Reference count
 */
static void peermux_free ( struct refcnt *refcnt ) {
	struct peerdist_multiplexer *peermux =
		container_of ( refcnt, struct peerdist_multiplexer, refcnt );

	uri_put ( peermux->uri );
	xferbuf_free ( &peermux->buffer );
	free ( peermux );
}

/**
 * Close PeerDist download multiplexer
 *
 * @v peermux		PeerDist download multiplexer
 * @v rc		Reason for close
 */
static void peermux_close ( struct peerdist_multiplexer *peermux, int rc ) {
	unsigned int i;

	/* Stop block download initiation process */
	process_del ( &peermux->process );

	/* Shut down all block downloads */
	for ( i = 0 ; i < PEERMUX_MAX_BLOCKS ; i++ )
		intf_shutdown ( &peermux->block[i].xfer, rc );

	/* Shut down all other interfaces (which may be connected to
	 * the same object).
	 */
	intf_nullify ( &peermux->info ); /* avoid potential loops */
	intf_shutdown ( &peermux->xfer, rc );
	intf_shutdown ( &peermux->info, rc );
}

/**
 * Receive content information
 *
 * @v peermux		PeerDist download multiplexer
 * @v iobuf		I/O buffer
 * @v meta		Data transfer metadata
 * @ret rc		Return status code
 */
static int peermux_info_deliver ( struct peerdist_multiplexer *peermux,
				  struct io_buffer *iobuf,
				  struct xfer_metadata *meta ) {
	int rc;

	/* Add data to buffer */
	if ( ( rc = xferbuf_deliver ( &peermux->buffer, iobuf, meta ) ) != 0 )
		goto err;

	return 0;

 err:
	peermux_close ( peermux, rc );
	return rc;
}

/**
 * Close content information interface
 *
 * @v peermux		PeerDist download multiplexer
 * @v rc		Reason for close
 */
static void peermux_info_close ( struct peerdist_multiplexer *peermux, int rc ){
	struct peerdist_info *info = &peermux->cache.info;
	size_t len;

	/* Terminate download on error */
	if ( rc != 0 )
		goto err;

	/* Successfully closing the content information interface
	 * indicates that the content information has been fully
	 * received, and initiates the actual PeerDist download.
	 */

	/* Shut down content information interface */
	intf_shutdown ( &peermux->info, rc );

	/* Parse content information */
	if ( ( rc = peerdist_info ( info->raw.data, peermux->buffer.len,
				    info ) ) != 0 ) {
		DBGC ( peermux, "PEERMUX %p could not parse content info: %s\n",
		       peermux, strerror ( rc ) );
		goto err;
	}

	/* Notify recipient of total download size */
	len = ( info->trim.end - info->trim.start );
	if ( ( rc = xfer_seek ( &peermux->xfer, len ) ) != 0 ) {
		DBGC ( peermux, "PEERMUX %p could not presize buffer: %s\n",
		       peermux, strerror ( rc ) );
		goto err;
	}
	xfer_seek ( &peermux->xfer, 0 );

	/* Start block download process */
	process_add ( &peermux->process );

	return;

 err:
	peermux_close ( peermux, rc );
}

/**
 * Initiate multiplexed block download
 *
 * @v peermux		PeerDist download multiplexer
 */
static void peermux_step ( struct peerdist_multiplexer *peermux ) {
	struct peerdist_info *info = &peermux->cache.info;
	struct peerdist_info_segment *segment = &peermux->cache.segment;
	struct peerdist_info_block *block = &peermux->cache.block;
	struct peerdist_multiplexed_block *peermblk;
	unsigned int next_segment;
	unsigned int next_block;
	int rc;

	/* Stop initiation process if all block downloads are busy */
	peermblk = list_first_entry ( &peermux->idle,
				      struct peerdist_multiplexed_block, list );
	if ( ! peermblk ) {
		process_del ( &peermux->process );
		return;
	}

	/* Increment block index */
	next_block = ( block->index + 1 );

	/* Move to first/next segment, if applicable */
	if ( next_block >= segment->blocks ) {

		/* Reset block index */
		next_block = 0;

		/* Calculate segment index */
		next_segment = ( segment->info ? ( segment->index + 1 ) : 0 );

		/* If we have finished all segments and have no
		 * remaining block downloads, then we are finished.
		 */
		if ( next_segment >= info->segments ) {
			process_del ( &peermux->process );
			if ( list_empty ( &peermux->busy ) )
				peermux_close ( peermux, 0 );
			return;
		}

		/* Get content information segment */
		if ( ( rc = peerdist_info_segment ( info, segment,
						    next_segment ) ) != 0 ) {
			DBGC ( peermux, "PEERMUX %p could not get segment %d "
			       "information: %s\n", peermux, next_segment,
			       strerror ( rc ) );
			goto err;
		}
	}

	/* Get content information block */
	if ( ( rc = peerdist_info_block ( segment, block, next_block ) ) != 0 ){
		DBGC ( peermux, "PEERMUX %p could not get segment %d block "
		       "%d information: %s\n", peermux, segment->index,
		       next_block, strerror ( rc ) );
		goto err;
	}

	/* Ignore block if it lies entirely outside the trimmed range */
	if ( block->trim.start == block->trim.end ) {
		DBGC ( peermux, "PEERMUX %p skipping segment %d block %d\n",
		       peermux, segment->index, block->index );
		return;
	}

	/* Start downloading this block */
	if ( ( rc = peerblk_open ( &peermblk->xfer, peermux->uri,
				   block ) ) != 0 ) {
		DBGC ( peermux, "PEERMUX %p could not start download for "
		       "segment %d block %d: %s\n", peermux, segment->index,
		       block->index, strerror ( rc ) );
		goto err;
	}

	/* Move to list of busy block downloads */
	list_del ( &peermblk->list );
	list_add_tail ( &peermblk->list, &peermux->busy );

	return;

 err:
	peermux_close ( peermux, rc );
}

/**
 * Receive data from multiplexed block download
 *
 * @v peermblk		PeerDist multiplexed block download
 * @v iobuf		I/O buffer
 * @v meta		Data transfer metadata
 * @ret rc		Return status code
 */
static int peermux_block_deliver ( struct peerdist_multiplexed_block *peermblk,
				   struct io_buffer *iobuf,
				   struct xfer_metadata *meta ) {
	struct peerdist_multiplexer *peermux = peermblk->peermux;

	/* Sanity check: all block downloads must use absolute
	 * positions for all deliveries, since they run concurrently.
	 */
	assert ( meta->flags & XFER_FL_ABS_OFFSET );

	/* We can't use a simple passthrough interface descriptor,
	 * since there are multiple block download interfaces.
	 */
	return xfer_deliver ( &peermux->xfer, iob_disown ( iobuf ), meta );
}

/**
 * Get multiplexed block download underlying data transfer buffer
 *
 * @v peermblk		PeerDist multiplexed download block
 * @ret xferbuf		Data transfer buffer, or NULL on error
 */
static struct xfer_buffer *
peermux_block_buffer ( struct peerdist_multiplexed_block *peermblk ) {
	struct peerdist_multiplexer *peermux = peermblk->peermux;

	/* We can't use a simple passthrough interface descriptor,
	 * since there are multiple block download interfaces.
	 */
	return xfer_buffer ( &peermux->xfer );
}

/**
 * Close multiplexed block download
 *
 * @v peermblk		PeerDist multiplexed block download
 * @v rc		Reason for close
 */
static void peermux_block_close ( struct peerdist_multiplexed_block *peermblk,
				  int rc ) {
	struct peerdist_multiplexer *peermux = peermblk->peermux;

	/* Move to list of idle downloads */
	list_del ( &peermblk->list );
	list_add_tail ( &peermblk->list, &peermux->idle );

	/* If any error occurred, terminate the whole multiplexer */
	if ( rc != 0 ) {
		peermux_close ( peermux, rc );
		return;
	}

	/* Restart data transfer interface */
	intf_restart ( &peermblk->xfer, rc );

	/* Restart block download initiation process */
	process_add ( &peermux->process );
}

/** Data transfer interface operations */
static struct interface_operation peermux_xfer_operations[] = {
	INTF_OP ( intf_close, struct peerdist_multiplexer *, peermux_close ),
};

/** Data transfer interface descriptor */
static struct interface_descriptor peermux_xfer_desc =
	INTF_DESC_PASSTHRU ( struct peerdist_multiplexer, xfer,
			     peermux_xfer_operations, info );

/** Content information interface operations */
static struct interface_operation peermux_info_operations[] = {
	INTF_OP ( xfer_deliver, struct peerdist_multiplexer *,
		  peermux_info_deliver ),
	INTF_OP ( intf_close, struct peerdist_multiplexer *,
		  peermux_info_close ),
};

/** Content information interface descriptor */
static struct interface_descriptor peermux_info_desc =
	INTF_DESC_PASSTHRU ( struct peerdist_multiplexer, info,
			     peermux_info_operations, xfer );

/** Block download data transfer interface operations */
static struct interface_operation peermux_block_operations[] = {
	INTF_OP ( xfer_deliver, struct peerdist_multiplexed_block *,
		  peermux_block_deliver ),
	INTF_OP ( xfer_buffer, struct peerdist_multiplexed_block *,
		  peermux_block_buffer ),
	INTF_OP ( intf_close, struct peerdist_multiplexed_block *,
		  peermux_block_close ),
};

/** Block download data transfer interface descriptor */
static struct interface_descriptor peermux_block_desc =
	INTF_DESC ( struct peerdist_multiplexed_block, xfer,
		    peermux_block_operations );

/** Block download initiation process descriptor */
static struct process_descriptor peermux_process_desc =
	PROC_DESC ( struct peerdist_multiplexer, process, peermux_step );

/**
 * Add PeerDist content-encoding filter
 *
 * @v xfer		Data transfer interface
 * @v info		Content information interface
 * @v uri		Original URI
 * @ret rc		Return status code
 */
int peermux_filter ( struct interface *xfer, struct interface *info,
		     struct uri *uri ) {
	struct peerdist_multiplexer *peermux;
	struct peerdist_multiplexed_block *peermblk;
	unsigned int i;

	/* Allocate and initialise structure */
	peermux = zalloc ( sizeof ( *peermux ) );
	if ( ! peermux )
		return -ENOMEM;
	ref_init ( &peermux->refcnt, peermux_free );
	intf_init ( &peermux->xfer, &peermux_xfer_desc, &peermux->refcnt );
	intf_init ( &peermux->info, &peermux_info_desc, &peermux->refcnt );
	peermux->uri = uri_get ( uri );
	xferbuf_umalloc_init ( &peermux->buffer,
			       &peermux->cache.info.raw.data );
	process_init_stopped ( &peermux->process, &peermux_process_desc,
			       &peermux->refcnt );
	INIT_LIST_HEAD ( &peermux->busy );
	INIT_LIST_HEAD ( &peermux->idle );
	for ( i = 0 ; i < PEERMUX_MAX_BLOCKS ; i++ ) {
		peermblk = &peermux->block[i];
		peermblk->peermux = peermux;
		list_add_tail ( &peermblk->list, &peermux->idle );
		intf_init ( &peermblk->xfer, &peermux_block_desc,
			    &peermux->refcnt );
	}

	/* Attach to parent interfaces, mortalise self, and return */
	intf_plug_plug ( &peermux->xfer, xfer );
	intf_plug_plug ( &peermux->info, info );
	ref_put ( &peermux->refcnt );
	return 0;
}