summaryrefslogtreecommitdiff
path: root/source3/torture/nbench.c
blob: 5a83c38a46894b0fae263af4cff2259fa66323db (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
/*
   Unix SMB/CIFS implementation.
   In-memory cache
   Copyright (C) Volker Lendecke 2007

   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 3 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, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "torture/proto.h"

static long long int ival(const char *str)
{
	return strtoll(str, NULL, 0);
}

struct nbench_state {
	struct tevent_context *ev;
	struct cli_state *cli;
	const char *cliname;
	FILE *loadfile;
	struct ftable *ftable;
	void (*bw_report)(size_t nread,
			  size_t nwritten,
			  void *private_data);
	void *bw_report_private;
};

struct lock_info {
	struct lock_info *next, *prev;
	off_t offset;
	int size;
};

struct createx_params {
	char *fname;
	unsigned int cr_options;
	unsigned int cr_disposition;
	int handle;
};

struct ftable {
	struct ftable *next, *prev;
	struct createx_params cp;
	struct lock_info *locks;
	uint16_t fnum; /* the fd that we got back from the server */
};

enum nbench_cmd {
	NBENCH_CMD_NTCREATEX,
	NBENCH_CMD_CLOSE,
	NBENCH_CMD_RENAME,
	NBENCH_CMD_UNLINK,
	NBENCH_CMD_DELTREE,
	NBENCH_CMD_RMDIR,
	NBENCH_CMD_MKDIR,
	NBENCH_CMD_QUERY_PATH_INFORMATION,
	NBENCH_CMD_QUERY_FILE_INFORMATION,
	NBENCH_CMD_QUERY_FS_INFORMATION,
	NBENCH_CMD_SET_FILE_INFORMATION,
	NBENCH_CMD_FIND_FIRST,
	NBENCH_CMD_WRITEX,
	NBENCH_CMD_WRITE,
	NBENCH_CMD_LOCKX,
	NBENCH_CMD_UNLOCKX,
	NBENCH_CMD_READX,
	NBENCH_CMD_FLUSH,
	NBENCH_CMD_SLEEP,
};

struct nbench_cmd_struct {
	char **params;
	int num_params;
	NTSTATUS status;
	enum nbench_cmd cmd;
};

static struct nbench_cmd_struct *nbench_parse(TALLOC_CTX *mem_ctx,
					      const char *line)
{
	struct nbench_cmd_struct *result;
	char *cmd;
	char *status;

	result = TALLOC_P(mem_ctx, struct nbench_cmd_struct);
	if (result == NULL) {
		return NULL;
	}
	result->params = str_list_make_shell(mem_ctx, line, " ");
	if (result->params == NULL) {
		goto fail;
	}
	result->num_params = talloc_array_length(result->params) - 1;
	if (result->num_params < 2) {
		goto fail;
	}
	status = result->params[result->num_params-1];
	if (strncmp(status, "NT_STATUS_", 10) != 0 &&
	    strncmp(status, "0x", 2) != 0) {
		goto fail;
	}
	/* accept numeric or string status codes */
	if (strncmp(status, "0x", 2) == 0) {
		result->status = NT_STATUS(strtoul(status, NULL, 16));
	} else {
		result->status = nt_status_string_to_code(status);
	}

	cmd = result->params[0];

	if (!strcmp(cmd, "NTCreateX")) {
		result->cmd = NBENCH_CMD_NTCREATEX;
	} else if (!strcmp(cmd, "Close")) {
		result->cmd = NBENCH_CMD_CLOSE;
	} else if (!strcmp(cmd, "Rename")) {
		result->cmd = NBENCH_CMD_RENAME;
	} else if (!strcmp(cmd, "Unlink")) {
		result->cmd = NBENCH_CMD_UNLINK;
	} else if (!strcmp(cmd, "Deltree")) {
		result->cmd = NBENCH_CMD_DELTREE;
	} else if (!strcmp(cmd, "Rmdir")) {
		result->cmd = NBENCH_CMD_RMDIR;
	} else if (!strcmp(cmd, "Mkdir")) {
		result->cmd = NBENCH_CMD_MKDIR;
	} else if (!strcmp(cmd, "QUERY_PATH_INFORMATION")) {
		result->cmd = NBENCH_CMD_QUERY_PATH_INFORMATION;
	} else if (!strcmp(cmd, "QUERY_FILE_INFORMATION")) {
		result->cmd = NBENCH_CMD_QUERY_FILE_INFORMATION;
	} else if (!strcmp(cmd, "QUERY_FS_INFORMATION")) {
		result->cmd = NBENCH_CMD_QUERY_FS_INFORMATION;
	} else if (!strcmp(cmd, "SET_FILE_INFORMATION")) {
		result->cmd = NBENCH_CMD_SET_FILE_INFORMATION;
	} else if (!strcmp(cmd, "FIND_FIRST")) {
		result->cmd = NBENCH_CMD_FIND_FIRST;
	} else if (!strcmp(cmd, "WriteX")) {
		result->cmd = NBENCH_CMD_WRITEX;
	} else if (!strcmp(cmd, "Write")) {
		result->cmd = NBENCH_CMD_WRITE;
	} else if (!strcmp(cmd, "LockX")) {
		result->cmd = NBENCH_CMD_LOCKX;
	} else if (!strcmp(cmd, "UnlockX")) {
		result->cmd = NBENCH_CMD_UNLOCKX;
	} else if (!strcmp(cmd, "ReadX")) {
		result->cmd = NBENCH_CMD_READX;
	} else if (!strcmp(cmd, "Flush")) {
		result->cmd = NBENCH_CMD_FLUSH;
	} else if (!strcmp(cmd, "Sleep")) {
		result->cmd = NBENCH_CMD_SLEEP;
	} else {
		goto fail;
	}
	return result;
fail:
	TALLOC_FREE(result);
	return NULL;
}

static struct ftable *ft_find(struct ftable *ftlist, int handle)
{
	while (ftlist != NULL) {
		if (ftlist->cp.handle == handle) {
			return ftlist;
		}
		ftlist = ftlist->next;
	}
	return NULL;
}

struct nbench_cmd_state {
	struct tevent_context *ev;
	struct nbench_state *state;
	struct nbench_cmd_struct *cmd;
	struct ftable *ft;
	bool eof;
};

static void nbench_cmd_done(struct tevent_req *subreq);

static struct tevent_req *nbench_cmd_send(TALLOC_CTX *mem_ctx,
					  struct tevent_context *ev,
					  struct nbench_state *nb_state)
{
	struct tevent_req *req, *subreq;
	struct nbench_cmd_state *state;
	char line[1024];
	size_t len;

	req = tevent_req_create(mem_ctx, &state, struct nbench_cmd_state);
	if (req == NULL) {
		return NULL;
	}
	state->ev = ev;
	state->state = nb_state;

	if (fgets(line, sizeof(line), nb_state->loadfile) == NULL) {
		tevent_req_nterror(req, NT_STATUS_END_OF_FILE);
		return tevent_req_post(req, ev);
	}
	len = strlen(line);
	if (len == 0) {
		tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
		return tevent_req_post(req, ev);
	}
	if (line[len-1] == '\n') {
		line[len-1] = '\0';
	}

	state->cmd = nbench_parse(state, line);
	if (state->cmd == NULL) {
		tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
		return tevent_req_post(req, ev);
	}

	switch (state->cmd->cmd) {
	case NBENCH_CMD_NTCREATEX: {
		uint32_t desired_access;
		uint32_t share_mode;
		unsigned int flags = 0;

		state->ft = talloc(state, struct ftable);
		if (tevent_req_nomem(state->ft, req)) {
			return tevent_req_post(req, ev);
		}

		state->ft->cp.fname = talloc_all_string_sub(
			state->ft, state->cmd->params[1], "client1",
			nb_state->cliname);
		if (tevent_req_nomem(state->ft->cp.fname, req)) {
			return tevent_req_post(req, ev);
		}
		state->ft->cp.cr_options = ival(state->cmd->params[2]);
		state->ft->cp.cr_disposition = ival(state->cmd->params[3]);
		state->ft->cp.handle = ival(state->cmd->params[4]);

		if (state->ft->cp.cr_options & FILE_DIRECTORY_FILE) {
			desired_access = SEC_FILE_READ_DATA;
		} else {
			desired_access =
				SEC_FILE_READ_DATA |
				SEC_FILE_WRITE_DATA |
				SEC_FILE_READ_ATTRIBUTE |
				SEC_FILE_WRITE_ATTRIBUTE;
			flags = EXTENDED_RESPONSE_REQUIRED
				| REQUEST_OPLOCK | REQUEST_BATCH_OPLOCK;
		}
		share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;

		subreq = cli_ntcreate_send(
			state, ev, nb_state->cli, state->ft->cp.fname, flags,
			desired_access, 0, share_mode,
			state->ft->cp.cr_disposition,
			state->ft->cp.cr_options, 0);
		break;
	}
	case NBENCH_CMD_CLOSE: {
		state->ft = ft_find(state->state->ftable,
				    ival(state->cmd->params[1]));
		if (state->ft == NULL) {
			tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
			return tevent_req_post(req, ev);
		}
		subreq = cli_close_send(
			state, ev, nb_state->cli, state->ft->fnum);
		break;
	}
	case NBENCH_CMD_MKDIR: {
		char *fname;
		fname = talloc_all_string_sub(
			state, state->cmd->params[1], "client1",
			nb_state->cliname);
		if (tevent_req_nomem(state->ft->cp.fname, req)) {
			return tevent_req_post(req, ev);
		}
		subreq = cli_mkdir_send(state, ev, nb_state->cli, fname);
		break;
	}
	case NBENCH_CMD_QUERY_PATH_INFORMATION: {
		char *fname;
		fname = talloc_all_string_sub(
			state, state->cmd->params[1], "client1",
			nb_state->cliname);
		if (tevent_req_nomem(state->ft->cp.fname, req)) {
			return tevent_req_post(req, ev);
		}
		subreq = cli_qpathinfo_send(state, ev, nb_state->cli, fname,
					    ival(state->cmd->params[2]),
					    0, nb_state->cli->max_xmit);
		break;
	}
	default:
		tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
		return tevent_req_post(req, ev);
	}

	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, nbench_cmd_done, req);
	return req;
}

static bool status_wrong(struct tevent_req *req, NTSTATUS expected,
			 NTSTATUS status)
{
	if (NT_STATUS_EQUAL(expected, status)) {
		return false;
	}
	if (NT_STATUS_IS_OK(status)) {
		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
	}
	tevent_req_nterror(req, status);
	return true;
}

static void nbench_cmd_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct nbench_cmd_state *state = tevent_req_data(
		req, struct nbench_cmd_state);
	struct nbench_state *nbstate = state->state;
	NTSTATUS status;

	switch (state->cmd->cmd) {
	case NBENCH_CMD_NTCREATEX: {
		struct ftable *ft;
		status = cli_ntcreate_recv(subreq, &state->ft->fnum);
		TALLOC_FREE(subreq);
		if (status_wrong(req, state->cmd->status, status)) {
			return;
		}
		if (!NT_STATUS_IS_OK(status)) {
			tevent_req_done(req);
			return;
		}
		ft = talloc_move(nbstate, &state->ft);
		DLIST_ADD(nbstate->ftable, ft);
		break;
	}
	case NBENCH_CMD_CLOSE: {
		status = cli_close_recv(subreq);
		TALLOC_FREE(subreq);
		if (status_wrong(req, state->cmd->status, status)) {
			return;
		}
		DLIST_REMOVE(state->state->ftable, state->ft);
		TALLOC_FREE(state->ft);
		break;
	}
	case NBENCH_CMD_MKDIR: {
		status = cli_mkdir_recv(subreq);
		TALLOC_FREE(subreq);
		if (status_wrong(req, state->cmd->status, status)) {
			return;
		}
		break;
	}
	case NBENCH_CMD_QUERY_PATH_INFORMATION: {
		status = cli_qpathinfo_recv(subreq, NULL, NULL, NULL);
		TALLOC_FREE(subreq);
		if (status_wrong(req, state->cmd->status, status)) {
			return;
		}
		break;
	}
	default:
		break;
	}
	tevent_req_done(req);
}

static NTSTATUS nbench_cmd_recv(struct tevent_req *req)
{
	return tevent_req_simple_recv_ntstatus(req);
}

static void nbench_done(struct tevent_req *subreq);

static struct tevent_req *nbench_send(
	TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct cli_state *cli,
	const char *cliname, FILE *loadfile,
	void (*bw_report)(size_t nread, size_t nwritten, void *private_data),
	void *bw_report_private)
{
	struct tevent_req *req, *subreq;
	struct nbench_state *state;

	req = tevent_req_create(mem_ctx, &state, struct nbench_state);
	if (req == NULL) {
		return NULL;
	}
	state->ev = ev;
	state->cli = cli;
	state->cliname = cliname;
	state->loadfile = loadfile;
	state->bw_report = bw_report;
	state->bw_report_private = bw_report_private;

	subreq = nbench_cmd_send(state, ev, state);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, nbench_done, req);
	return req;
}

static void nbench_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct nbench_state *state = tevent_req_data(
		req, struct nbench_state);
	NTSTATUS status;

	status = nbench_cmd_recv(subreq);
	TALLOC_FREE(subreq);

	if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
		tevent_req_done(req);
		return;
	}
	if (!NT_STATUS_IS_OK(status)) {
		tevent_req_nterror(req, status);
		return;
	}
	subreq = nbench_cmd_send(state, state->ev, state);
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	tevent_req_set_callback(subreq, nbench_done, req);
}

static NTSTATUS nbench_recv(struct tevent_req *req)
{
	return tevent_req_simple_recv_ntstatus(req);
}

bool run_nbench2(int dummy)
{
	TALLOC_CTX *frame = talloc_stackframe();
	struct tevent_context *ev;
	struct cli_state *cli = NULL;
	FILE *loadfile;
	bool ret = false;
	struct tevent_req *req;
	NTSTATUS status;

	loadfile = fopen("client.txt", "r");
	if (loadfile == NULL) {
		fprintf(stderr, "Could not open \"client.txt\": %s\n",
			strerror(errno));
		return false;
	}
	ev = tevent_context_init(talloc_tos());
	if (ev == NULL) {
		goto fail;
	}
	if (!torture_open_connection(&cli, 0)) {
		goto fail;
	}

	req = nbench_send(talloc_tos(), ev, cli, "client1", loadfile,
			  NULL, NULL);
	if (req == NULL) {
		goto fail;
	}
	if (!tevent_req_poll(req, ev)) {
		goto fail;
	}
	status = nbench_recv(req);
	TALLOC_FREE(req);
	printf("nbench returned %s\n", nt_errstr(status));

	ret = true;
fail:
	if (cli != NULL) {
		torture_close_connection(cli);
	}
	TALLOC_FREE(ev);
	if (loadfile != NULL) {
		fclose(loadfile);
		loadfile = NULL;
	}
	TALLOC_FREE(frame);
	return ret;
}