summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/interface/xen/xenstore.c
blob: b96982927cc6a7c302158554aab4c25bafad994f (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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
 * Copyright (C) 2014 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.
 */

FILE_LICENCE ( GPL2_OR_LATER );

#include <stdint.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ipxe/io.h>
#include <ipxe/nap.h>
#include <ipxe/malloc.h>
#include <ipxe/xen.h>
#include <ipxe/xenevent.h>
#include <ipxe/xenstore.h>

/*
 * xs_wire.h attempts to define a static error table xsd_errors, which
 * interacts badly with the dynamically generated error numbers used
 * by iPXE.  Prevent this table from being constructed by including
 * errno.h only after including xs_wire.h.
 *
 */
#include <xen/io/xs_wire.h>
#include <errno.h>

/** @file
 *
 * XenStore interface
 *
 */

/** Request identifier */
static uint32_t xenstore_req_id;

/**
 * Send XenStore request raw data
 *
 * @v xen		Xen hypervisor
 * @v data		Data buffer
 * @v len		Length of data
 */
static void xenstore_send ( struct xen_hypervisor *xen, const void *data,
			    size_t len ) {
	struct xenstore_domain_interface *intf = xen->store.intf;
	XENSTORE_RING_IDX prod = readl ( &intf->req_prod );
	XENSTORE_RING_IDX cons;
	XENSTORE_RING_IDX idx;
	const char *bytes = data;
	size_t offset = 0;
	size_t fill;

	DBGCP ( intf, "XENSTORE raw request:\n" );
	DBGCP_HDA ( intf, MASK_XENSTORE_IDX ( prod ), data, len );

	/* Write one byte at a time */
	while ( offset < len ) {

		/* Wait for space to become available */
		while ( 1 ) {
			cons = readl ( &intf->req_cons );
			fill = ( prod - cons );
			if ( fill < XENSTORE_RING_SIZE )
				break;
			DBGC2 ( xen, "." );
			cpu_nap();
			rmb();
		}

		/* Write byte */
		idx = MASK_XENSTORE_IDX ( prod++ );
		writeb ( bytes[offset++], &intf->req[idx] );
	}

	/* Update producer counter */
	wmb();
	writel ( prod, &intf->req_prod );
	wmb();
}

/**
 * Send XenStore request string (excluding terminating NUL)
 *
 * @v xen		Xen hypervisor
 * @v string		String
 */
static void xenstore_send_string ( struct xen_hypervisor *xen,
				   const char *string ) {

	xenstore_send ( xen, string, strlen ( string ) );
}

/**
 * Receive XenStore response raw data
 *
 * @v xen		Xen hypervisor
 * @v data		Data buffer, or NULL to discard data
 * @v len		Length of data
 */
static void xenstore_recv ( struct xen_hypervisor *xen, void *data,
			    size_t len ) {
	struct xenstore_domain_interface *intf = xen->store.intf;
	XENSTORE_RING_IDX cons = readl ( &intf->rsp_cons );
	XENSTORE_RING_IDX prod;
	XENSTORE_RING_IDX idx;
	char *bytes = data;
	size_t offset = 0;
	size_t fill;

	DBGCP ( intf, "XENSTORE raw response:\n" );

	/* Read one byte at a time */
	while ( offset < len ) {

		/* Wait for data to be ready */
		while ( 1 ) {
			prod = readl ( &intf->rsp_prod );
			fill = ( prod - cons );
			if ( fill > 0 )
				break;
			DBGC2 ( xen, "." );
			cpu_nap();
			rmb();
		}

		/* Read byte */
		idx = MASK_XENSTORE_IDX ( cons++ );
		if ( data )
			bytes[offset++] = readb ( &intf->rsp[idx] );
	}
	if ( data )
		DBGCP_HDA ( intf, MASK_XENSTORE_IDX ( cons - len ), data, len );

	/* Update consumer counter */
	writel ( cons, &intf->rsp_cons );
	wmb();
}

/**
 * Send XenStore request
 *
 * @v xen		Xen hypervisor
 * @v type		Message type
 * @v req_id		Request ID
 * @v value		Value, or NULL to omit
 * @v key		Key path components
 * @ret rc		Return status code
 */
static int xenstore_request ( struct xen_hypervisor *xen,
			      enum xsd_sockmsg_type type, uint32_t req_id,
			      const char *value, va_list key ) {
	struct xsd_sockmsg msg;
	struct evtchn_send event;
	const char *string;
	va_list tmp;
	int xenrc;
	int rc;

	/* Construct message header */
	msg.type = type;
	msg.req_id = req_id;
	msg.tx_id = 0;
	msg.len = 0;
	DBGC2 ( xen, "XENSTORE request ID %d type %d ", req_id, type );

	/* Calculate total length */
	va_copy ( tmp, key );
	while ( ( string = va_arg ( tmp, const char * ) ) != NULL ) {
		DBGC2 ( xen, "%s%s", ( msg.len ? "/" : "" ), string );
		msg.len += ( strlen ( string ) + 1 /* '/' or NUL */ );
	}
	va_end ( tmp );
	if ( value ) {
		DBGC2 ( xen, " = \"%s\"", value );
		msg.len += strlen ( value );
	}
	DBGC2 ( xen, "\n" );

	/* Send message */
	xenstore_send ( xen, &msg, sizeof ( msg ) );
	string = va_arg ( key, const char * );
	assert ( string != NULL );
	xenstore_send_string ( xen, string );
	while ( ( string = va_arg ( key, const char * ) ) != NULL ) {
		xenstore_send_string ( xen, "/" );
		xenstore_send_string ( xen, string );
	}
	xenstore_send ( xen, "", 1 ); /* Separating NUL */
	if ( value )
		xenstore_send_string ( xen, value );

	/* Notify the back end */
	event.port = xen->store.port;
	if ( ( xenrc = xenevent_send ( xen, &event ) ) != 0 ) {
		rc = -EXEN ( xenrc );
		DBGC ( xen, "XENSTORE could not notify back end: %s\n",
		       strerror ( rc ) );
		return rc;
	}

	return 0;
}

/**
 * Receive XenStore response
 *
 * @v xen		Xen hypervisor
 * @v req_id		Request ID
 * @v value		Value to fill in
 * @v len		Length to fill in
 * @ret rc		Return status code
 *
 * The caller is responsible for eventually calling free() on the
 * returned value.  Note that the value may comprise multiple
 * NUL-terminated strings concatenated together.  A terminating NUL
 * will always be appended to the returned value.
 */
static int xenstore_response ( struct xen_hypervisor *xen, uint32_t req_id,
			       char **value, size_t *len ) {
	struct xsd_sockmsg msg;
	char *string;
	int rc;

	/* Receive message header */
	xenstore_recv ( xen, &msg, sizeof ( msg ) );
	*len = msg.len;

	/* Allocate space for response */
	*value = zalloc ( msg.len + 1 /* terminating NUL */ );

	/* Receive data.  Do this even if allocation failed, or if the
	 * request ID was incorrect, to avoid leaving data in the
	 * ring.
	 */
	xenstore_recv ( xen, *value, msg.len );

	/* Validate request ID */
	if ( msg.req_id != req_id ) {
		DBGC ( xen, "XENSTORE response ID mismatch (got %d, expected "
		       "%d)\n", msg.req_id, req_id );
		rc = -EPROTO;
		goto err_req_id;
	}

	/* Check for allocation failure */
	if ( ! *value ) {
		DBGC ( xen, "XENSTORE could not allocate %d bytes for "
		       "response\n", msg.len );
		rc = -ENOMEM;
		goto err_alloc;
	}

	/* Check for explicit errors */
	if ( msg.type == XS_ERROR ) {
		DBGC ( xen, "XENSTORE response error \"%s\"\n", *value );
		rc = -EIO;
		goto err_explicit;
	}

	DBGC2 ( xen, "XENSTORE response ID %d\n", req_id );
	if ( DBG_EXTRA ) {
		for ( string = *value ; string < ( *value + msg.len ) ;
		      string += ( strlen ( string ) + 1 /* NUL */ ) ) {
			DBGC2 ( xen, " - \"%s\"\n", string );
		}
	}
	return 0;

 err_explicit:
 err_alloc:
 err_req_id:
	free ( *value );
	*value = NULL;
	return rc;
}

/**
 * Issue a XenStore message
 *
 * @v xen		Xen hypervisor
 * @v type		Message type
 * @v response		Response value to fill in, or NULL to discard
 * @v len		Response length to fill in, or NULL to ignore
 * @v request		Request value, or NULL to omit
 * @v key		Key path components
 * @ret rc		Return status code
 */
static int xenstore_message ( struct xen_hypervisor *xen,
			      enum xsd_sockmsg_type type, char **response,
			      size_t *len, const char *request, va_list key ) {
	char *response_value;
	size_t response_len;
	int rc;

	/* Send request */
	if ( ( rc = xenstore_request ( xen, type, ++xenstore_req_id,
				       request, key ) ) != 0 )
		return rc;

	/* Receive response */
	if ( ( rc = xenstore_response ( xen, xenstore_req_id, &response_value,
					&response_len ) ) != 0 )
		return rc;

	/* Return response, if applicable */
	if ( response ) {
		*response = response_value;
	} else {
		free ( response_value );
	}
	if ( len )
		*len = response_len;

	return 0;
}

/**
 * Read XenStore value
 *
 * @v xen		Xen hypervisor
 * @v value		Value to fill in
 * @v key		Key path components
 * @ret rc		Return status code
 *
 * On a successful return, the caller is responsible for calling
 * free() on the returned value.
 */
static int xenstore_vread ( struct xen_hypervisor *xen, char **value,
			    va_list key ) {

	return xenstore_message ( xen, XS_READ, value, NULL, NULL, key );
}

/**
 * Read XenStore value
 *
 * @v xen		Xen hypervisor
 * @v value		Value to fill in
 * @v ...		Key path components
 * @ret rc		Return status code
 *
 * On a successful return, the caller is responsible for calling
 * free() on the returned value.
 */
__attribute__ (( sentinel )) int
xenstore_read ( struct xen_hypervisor *xen, char **value, ... ) {
	va_list key;
	int rc;

	va_start ( key, value );
	rc = xenstore_vread ( xen, value, key );
	va_end ( key );
	return rc;
}

/**
 * Read XenStore numeric value
 *
 * @v xen		Xen hypervisor
 * @v num		Numeric value to fill in
 * @v ...		Key path components
 * @ret rc		Return status code
 */
__attribute__ (( sentinel )) int
xenstore_read_num ( struct xen_hypervisor *xen, unsigned long *num, ... ) {
	va_list key;
	char *value;
	char *endp;
	int rc;

	/* Try to read text value */
	va_start ( key, num );
	rc = xenstore_vread ( xen, &value, key );
	va_end ( key );
	if ( rc != 0 )
		goto err_read;

	/* Try to parse as numeric value */
	*num = strtoul ( value, &endp, 10 );
	if ( ( *value == '\0' ) || ( *endp != '\0' ) ) {
		DBGC ( xen, "XENSTORE found invalid numeric value \"%s\"\n",
		       value );
		rc = -EINVAL;
		goto err_strtoul;
	}

 err_strtoul:
	free ( value );
 err_read:
	return rc;
}

/**
 * Write XenStore value
 *
 * @v xen		Xen hypervisor
 * @v value		Value
 * @v key		Key path components
 * @ret rc		Return status code
 */
static int xenstore_vwrite ( struct xen_hypervisor *xen, const char *value,
			     va_list key ) {

	return xenstore_message ( xen, XS_WRITE, NULL, NULL, value, key );
}

/**
 * Write XenStore value
 *
 * @v xen		Xen hypervisor
 * @v value		Value
 * @v ...		Key path components
 * @ret rc		Return status code
 */
__attribute__ (( sentinel )) int
xenstore_write ( struct xen_hypervisor *xen, const char *value, ... ) {
	va_list key;
	int rc;

	va_start ( key, value );
	rc = xenstore_vwrite ( xen, value, key );
	va_end ( key );
	return rc;
}

/**
 * Write XenStore numeric value
 *
 * @v xen		Xen hypervisor
 * @v num		Numeric value
 * @v ...		Key path components
 * @ret rc		Return status code
 */
__attribute__ (( sentinel )) int
xenstore_write_num ( struct xen_hypervisor *xen, unsigned long num, ... ) {
	char value[ 21 /* "18446744073709551615" + NUL */ ];
	va_list key;
	int rc;

	/* Construct value */
	snprintf ( value, sizeof ( value ), "%ld", num );

	/* Write value */
	va_start ( key, num );
	rc = xenstore_vwrite ( xen, value, key );
	va_end ( key );
	return rc;
}

/**
 * Delete XenStore value
 *
 * @v xen		Xen hypervisor
 * @v ...		Key path components
 * @ret rc		Return status code
 */
__attribute__ (( sentinel )) int
xenstore_rm ( struct xen_hypervisor *xen, ... ) {
	va_list key;
	int rc;

	va_start ( key, xen );
	rc = xenstore_message ( xen, XS_RM, NULL, NULL, NULL, key );
	va_end ( key );
	return rc;
}

/**
 * Read XenStore directory
 *
 * @v xen		Xen hypervisor
 * @v children		Child key names to fill in
 * @v len		Length of child key names to fill in
 * @v ...		Key path components
 * @ret rc		Return status code
 */
__attribute__ (( sentinel )) int
xenstore_directory ( struct xen_hypervisor *xen, char **children, size_t *len,
		     ... ) {
	va_list key;
	int rc;

	va_start ( key, len );
	rc = xenstore_message ( xen, XS_DIRECTORY, children, len, NULL, key );
	va_end ( key );
	return rc;
}

/**
 * Dump XenStore directory contents (for debugging)
 *
 * @v xen		Xen hypervisor
 * @v key		Key
 */
void xenstore_dump ( struct xen_hypervisor *xen, const char *key ) {
	char *value;
	char *children;
	char *child;
	char *child_key;
	size_t len;
	int rc;

	/* Try to dump current key as a value */
	if ( ( rc = xenstore_read ( xen, &value, key, NULL ) ) == 0 ) {
		DBGC ( xen, "%s = \"%s\"\n", key, value );
		free ( value );
	}

	/* Try to recurse into each child in turn */
	if ( ( rc = xenstore_directory ( xen, &children, &len, key,
					 NULL ) ) == 0 ) {
		for ( child = children ; child < ( children + len ) ;
		      child += ( strlen ( child ) + 1 /* NUL */ ) ) {

			/* Construct child key */
			asprintf ( &child_key, "%s/%s", key, child );
			if ( ! child_key ) {
				DBGC ( xen, "XENSTORE could not allocate child "
				       "key \"%s/%s\"\n", key, child );
				rc = -ENOMEM;
				break;
			}

			/* Recurse into child key, continuing on error */
			xenstore_dump ( xen, child_key );
			free ( child_key );
		}
		free ( children );
	}
}