aboutsummaryrefslogtreecommitdiff
path: root/lib/libvirtio/p9.c
blob: e9ba22886c06c9966b6dcf6127aa8f941a39d54e (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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/******************************************************************************
 * Copyright (c) 2011 IBM Corporation
 * All rights reserved.
 * This program and the accompanying materials
 * are made available under the terms of the BSD License
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * Contributors:
 *     IBM Corporation - initial implementation
 *****************************************************************************/

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <byteorder.h>
#include "p9.h"


/* Protocol stack marshaling. */
uint8_t *sp;

#define GET_08(s,i)	(s)[(i)]
#define GET_16(s,i)	le16_to_cpu(*(uint16_t*)(&(s)[(i)]))
#define GET_32(s,i)	le32_to_cpu(*(uint32_t*)(&(s)[(i)]))
#define GET_64(s,i)	le64_to_cpu(*(uint64_t*)(&(s)[(i)]))

#define SET_08(s,i,v)	(s)[(i)] = (v)
#define SET_16(s,i,v)	*(uint16_t*)(&(s)[(i)]) = cpu_to_le16(v)
#define SET_32(s,i,v)	*(uint32_t*)(&(s)[(i)]) = cpu_to_le32(v)
#define SET_64(s,i,v)	*(uint64_t*)(&(s)[(i)]) = cpu_to_le64(v)

#define PUT_08(v)	sp[0] = (v);sp+=1
#define PUT_16(v)	*(uint16_t*)(&sp[0]) = cpu_to_le16(v);sp+=2
#define PUT_32(v)	*(uint32_t*)(&sp[0]) = cpu_to_le32(v);sp+=4
#define PUT_64(v)	*(uint64_t*)(&sp[0]) = cpu_to_le64(v);sp+=8

#define PUT_HD(m,t)	PUT_32(0);PUT_08(m);PUT_16(t)
#define PUT_SN(v,n)	PUT_16(n);memcpy(sp,(v),(n));sp+=n
#define PUT_ST(v)	PUT_16(strlen(v));memcpy(sp,(v),strlen(v));\
				sp+=strlen(v)

#define GET_SIZE	(sp - tx)


/* General defines. */
#define MIN(a,b)	((a)>(b)?(b):(a))

#define NOTAG		((uint16_t)~0)
#define NOFID		((uint32_t)~0)
#define TAG		1
#define BUF_SIZE	(8*1024)

#define VERSION			"9P2000.u"
#define UNKNOWN_VER		"unknown"

#define MSG_SIZE		0
#define MSG_ID			4
#define MSG_ERR			0x6b
#define MSG_ERR_STR		9
#define MSG_ERR_STR_LEN		7
#define MSG_TAG			5
#define MSG_VER_MSIZE		7
#define MSG_VER_STR_LEN		11
#define MSG_VER_STR		13
#define MSG_WALK_TX_ELMT	15
#define MSG_WALK_RX_ELMT	7
#define MSG_SIZE		0
#define MSG_WALK_MAX_ELMT	16
#define MSG_QID_SIZE		13
#define MSG_WALK_RX_HDR_SIZE	9
#define MSG_OPEN_IOUNIT		20
#define MSG_OPEN_MODE_MASK	0x5f
#define MSG_READ_COUNT		7
#define MSG_READ_DATA		11
#define MSG_STAT_LEN		42
#define MSG_STAT_TYPE		17

#define T_VERSION	100
#define R_VERSION	(T_VERSION + 1)
#define T_ATTACH	104
#define R_ATTACH	(T_ATTACH + 1)
#define T_ERROR		106
#define R_ERROR		(T_ERROR + 1)
#define T_WALK		110
#define R_WALK		(T_WALK + 1)
#define T_OPEN		112
#define R_OPEN		(T_OPEN + 1)
#define T_READ		116
#define R_READ		(T_READ + 1)
#define T_CLUNK		120
#define R_CLUNK		(T_CLUNK + 1)
#define T_STAT		124
#define R_STAT		(T_STAT + 1)

static p9_transact_t transact;
static void *transact_opaque;
static uint8_t *tx;
static uint8_t *rx;


/**
 * p9_reg_transport
 *
 * Registers a transport function for use by the P9 protocol. The transport
 * connects the P9 Client (this library) to a server instance.
 *
 * @param transact_func[in]	Function pointer to type p9_transact_t.
 * @param tx_buffer[in]		TX buffer, must be 8k in size.
 * @param rx_buffer[in]		RX buffer, must be 8k in size.
 */
void p9_reg_transport(p9_transact_t transact_func, void *opaque,
		      uint8_t *tx_buffer, uint8_t *rx_buffer)
{
	transact = transact_func;
	transact_opaque = opaque;
	tx = tx_buffer;
	rx = rx_buffer;
}

/**
 * reset_buffers
 *
 * Reset the RX and TX buffers to BUF_SIZE (8k) and reset the Stack Pointer
 * for the TX buffer, which is referenced by the PUT_* macro's.
 */
void reset_buffers(void)
{
	memset(tx, 0, BUF_SIZE);
	memset(rx, 0, BUF_SIZE);
	sp = tx;
}

/**
 * p9_transaction
 *
 * Perform a transaction (send/recv) over the registered transport.
 *
 * @param connection[in|out]	Connection object.
 * @return	0 = success, -ve = error.
 */
int p9_transaction(p9_connection_t *connection)
{
	int rc;
	uint32_t tx_size = GET_SIZE;
	uint32_t rx_size = connection->message_size;

	if (transact == NULL) {
		return P9_NO_TRANSPORT;
	}
	if (tx == NULL || rx == NULL) {
		return P9_NO_BUFFER;
	}
	if (connection->message_size > BUF_SIZE) {
		return P9_MSG_SIZE_TOO_BIG;
	}
	if (tx_size > connection->message_size) {
		return P9_MSG_TOO_LONG;
	}

	SET_32(tx, MSG_SIZE, tx_size);
	rc = transact(transact_opaque, tx, tx_size, rx, &rx_size);

	if (rc != 0) {
		return P9_TRANSPORT_ERROR;
	}
	if (GET_16(tx, MSG_TAG) != GET_16(rx, MSG_TAG)) {
		return P9_UNEXPECTED_TAG;
	}
	if (GET_08(rx, MSG_ID) == MSG_ERR) {
		char error_string[200];

		memset(error_string, 0, 200);
		strncpy(error_string, (char *)&rx[MSG_ERR_STR],
				MIN(200 - 1, GET_16(rx, MSG_ERR_STR_LEN)));
#ifndef TEST
		printf("\nError: %s\n", error_string);
#endif
		return P9_R_ERROR;
	}
	if ((GET_08(tx, MSG_ID) + 1) != GET_08(rx, MSG_ID)) {
		return P9_UNEXPECTED_MSG;
	}

	return 0;
}

/**
 * p9_version
 *
 * Called to start a session. Negotiates the maximum message size for the
 * P9 protocol.
 *
 * @param connection[in|out]	Connection object, contains message_size.
 * @return	0 = success, -ve = error.
 *
 * @remark
 * size[4] Tversion tag[2] msize[4] version[s]
 * size[4] Rversion tag[2] msize[4] version[s]
 */
int p9_version(p9_connection_t *connection)
{
	int rc;
	char *ver_str;
	int ver_len;

	reset_buffers();

	/* Build message. */
	PUT_HD(T_VERSION, NOTAG);
	PUT_32(connection->message_size);
	PUT_ST(VERSION);

	/* Send message. */
	rc = p9_transaction(connection);
	if (rc != 0) {
		return rc;
	}

	/* Handle response. */
	connection->message_size = MIN(connection->message_size,
			GET_32(rx, MSG_VER_MSIZE));

	ver_str = (char *)&rx[MSG_VER_STR];
	ver_len = GET_16(rx, MSG_VER_STR_LEN);
	if (strncmp(UNKNOWN_VER, ver_str, ver_len) == 0) {
		return P9_UNKNOWN_VERSION;
	}


	return 0;
}

/**
 * p9_attach
 *
 * Called to open a connection for a user to a file tree on the server. There
 * is no authorisation undertaken (NOFID).
 *
 * @param connection[in|out]	Connection object, contains uname and aname as
 * 	well as the connection fid and returned qid.
 * @return	0 = success, -ve = error.
 *
 * @remark
 * size[4] Tattach tag[2] fid[4] afid[4] uname[s] aname[s] n_uname[4]
 * size[4] Rattach tag[2] qid[13]
 */
int p9_attach(p9_connection_t *connection)
{
	int rc;
	unsigned length = 19 + strlen(connection->uname) + strlen(connection->aname);

	if (length > connection->message_size) {
		return P9_MSG_TOO_LONG;
	}

	reset_buffers();

	/* Build message. */
	PUT_HD(T_ATTACH, TAG);
	PUT_32(connection->fid);	
	PUT_32(NOFID);
	PUT_ST(connection->uname);
	PUT_ST(connection->aname);
	PUT_32(~0); /* ??? */

	/* Send message. */
	rc = p9_transaction(connection);
	if (rc != 0) {
		return rc;
	}


	return 0;
}

/**
 * p9_clunk
 *
 * Called when closing a file or connection (or after failed opens). Tells the
 * server that the supplied fid is no longer needed by this client.
 *
 * @param connection[in|out]	Connection object.
 * @param fid[in]	Fid to be clunked (released) on the server.
 * @return	0 = success, -ve = error.
 *
 * @remark
 * size[4] Tclunk tag[2] fid[4]
 * size[4] Rclunk tag[2]
 */
int p9_clunk(p9_connection_t *connection, uint32_t fid)
{
	int rc;

	reset_buffers();

	/* Build message. */
	PUT_HD(T_CLUNK, TAG);
	PUT_32(fid);

	/* Send message. */
	rc = p9_transaction(connection);
	if (rc != 0) {
		return rc;
	}


	return 0;
}

/**
 * p9_walk
 *
 * Walk the provided path to a file (or directory) starting at the directory
 * indicated by fid and assigning new_fid to the last successfully walked
 * element. If not all elements of the path can be walked then the pos
 * pointer is set to the part of the path following the last successful
 * walked element. The function can be called again to walk the remainder
 * of the path (or produce an error).
 *
 * @param connection[in]	Connection object.
 * @param fid[in]	Fid to start walk from, must be directory or root (from
 * 	call to p9_attach).
 * @param new_fid[in]	Fid to be used for the last walked element.
 * @param pos[in|out]	Position in path that remains to be walked. If the
 * 	path was completely walked without error this will point to the NULL
 * 	at the end of path.
 * @return	1 = partial walk, 0 = success, -ve = error.
 *
 * @remark
 * size[4] Twalk tag[2] fid[4] newfid[4] nwname[2] nwname*(wname[s])
 * size[4] Rwalk tag[2] nwqid[2] nwqid*(qid[13])
 */
int p9_walk(p9_connection_t *connection, uint32_t fid, uint32_t new_fid,
		uint8_t **pos)
{
	int rc;
	const char *path = (const char *)*pos;
	uint8_t *s_tok;
	uint8_t *e_tok;
	int element_count = 0;

	if (path == NULL) {
		*pos = NULL;
		return P9_NULL_PATH;
	}

	reset_buffers();

	/* Build message. */
	PUT_HD(T_WALK, TAG);	/* Length to 0, set later. */
	PUT_32(fid);
	PUT_32(new_fid);
	PUT_16(0);		/* Element count to 0, set later. */

	/* Get elements from path, and append to message. */
	s_tok = (uint8_t *)path;
	e_tok = s_tok;

	while (*s_tok != 0) {
		while (*s_tok == '/') {
			s_tok++;
		}
		e_tok = s_tok;
		while ((*e_tok != '/') && (*e_tok != 0)) {
			e_tok++;
		}

		/* Check the element is OK. */
		if (strncmp(".", (const char *)s_tok, (e_tok - s_tok)) == 0) {
			/* Don't send ".", continue to next. */
			s_tok = e_tok;
			continue;
		}
		unsigned tx_size = (e_tok - s_tok + 2 + GET_SIZE);
		unsigned rx_size = ((element_count + 1) * MSG_QID_SIZE
				+ MSG_WALK_RX_HDR_SIZE);
		if ((tx_size > connection->message_size)
			|| (rx_size > connection->message_size)) {
			/*
			 * Element makes TX msg too long OR expected RX msg
			 * too long. Move pos to previous element and do
			 * partial walk.
			 */
			e_tok = s_tok;
			if (*(e_tok - 1) == '/') {
				e_tok--;
			}
			break;
		}

		/* Add the element to the message. */
		PUT_SN(s_tok, e_tok - s_tok);
		element_count++;

		/* Server supports no more than 16 elements, partial walk. */
		if (element_count == MSG_WALK_MAX_ELMT) {
			break;
		}

		/* Ready to find the next element. */
		s_tok = e_tok;
	}

	if ((element_count == 0) && (strlen(path) > 0)) {
		return P9_PATH_ELEMENT_TOO_LONG;
	}

	*pos = e_tok;

	/* Update counts and then send message. */
	SET_16(tx, MSG_WALK_TX_ELMT, element_count);
	rc = p9_transaction(connection);
	if (rc != 0) {
		return rc;
	}

	/* Check for special return conditions. */
	if (element_count != GET_16(rx, MSG_WALK_RX_ELMT)) {
		/* Find the last element successfully walked */
		s_tok = (uint8_t *)path;
		e_tok = s_tok;
		element_count = GET_16(rx, MSG_WALK_RX_ELMT);

		while (element_count--) {
			while (*s_tok == '/') {
				s_tok++;
			}

			e_tok = s_tok;

			while ((*e_tok != '/') && (*e_tok != 0)) {
				e_tok++;
			}

			s_tok = e_tok;
		}

		*pos = e_tok;
	}
	if (**pos != 0) {
		rc = P9_PARTIAL_WALK;
	}


	return rc;
}

/**
 * p9_open
 *
 * Opens the file represented by fid with associated mode bit mask. The iounit
 * size returned from the server is written to the connection object.
 *
 * @param file[in|out]	File object, contains fid for file.
 * @param mode[in]	Mode to open with. Bit's 0=R, 1=W, 2=RW, 3=EX, 4=Trunc
 * 	and 6=Delete on Close.
 * @return	0 = success, -ve = error.
 *
 * @remark
 * size[4] Topen tag[2] fid[4] mode[1]
 * size[4] Ropen tag[2] qid[13] iounit[4]
 */
int p9_open(p9_file_t *file, uint8_t mode)
{
	int rc;
	p9_connection_t *connection = file->connection;

	reset_buffers();
	file->iounit = 0;

	/* Build message. */
	PUT_HD(T_OPEN, TAG);
	PUT_32(file->fid);
	PUT_08(mode & MSG_OPEN_MODE_MASK);

	/* Send message. */
	rc = p9_transaction(connection);
	if (rc != 0) {
		return rc;
	}

	/* Handle response. */
	file->iounit = GET_32(rx, MSG_OPEN_IOUNIT);


	return 0;
}

/**
 * p9_read
 *
 * Reads the file in to buffer.
 *
 * @param file[in]	File object, contains fid for file.
 * @param buffer[out]	Buffer for data.
 * @param count[in]	Number of bytes to read (less bytes than requested
 * 	 may be read).
 * @param offset[in]	Offset in file to read from.
 * @return	Bytes read, -ve = error.
 *
 * @remark
 * size[4] Tread tag[2] fid[4] offset[8] count[4]
 * size[4] Rread tag[2] count[4] data[count]
 */
int p9_read(p9_file_t *file, uint8_t *buffer,
		uint32_t count, uint64_t offset)
{
	int rc;
	p9_connection_t *connection = file->connection;
	uint32_t got;

	reset_buffers();
	count = MIN((connection->message_size - MSG_READ_DATA), count);

	/* Build message. */
	PUT_HD(T_READ, TAG);
	PUT_32(file->fid);
	PUT_64(offset);
	PUT_32(count);

	/* Send message. */
	rc = p9_transaction(connection);
	if (rc != 0) {
		return rc;
	}
	got = GET_32(rx, MSG_READ_COUNT);
	if (got > count) {
		return P9_READ_UNEXPECTED_DATA;
	}

	/* Handle response. */
	memcpy(buffer, &rx[MSG_READ_DATA], got);

	return got;
}

/**
 * p9_stat
 *
 * Stat's the fid and writes the type and length to the file object.
 *
 * @param file[in|out]	File object, contains fid for file.
 * @return	0 = success, -ve = error.
 *
 * @remark
 * size[4] Tstat tag[2] fid[4]
 * size[4] Rstat tag[2] size[2] stat[n]
 */
int p9_stat(p9_file_t *file)
{
	int rc;
	p9_connection_t *connection = file->connection;

	reset_buffers();
	file->length = 0;
	file->type = 0;

	/* Build message. */
	PUT_HD(T_STAT, TAG);
	PUT_32(file->fid);

	/* Send message. */
	rc = p9_transaction(connection);
	if (rc != 0) {
		return rc;
	}

	/* Handle response. */
	file->length = GET_64(rx, MSG_STAT_LEN);
	file->type = GET_08(rx, MSG_STAT_TYPE);


	return 0;
}