2 TODO: add splitting of writes for servers with signing
7 Unix SMB/CIFS implementation.
9 Copyright (C) Andrew Tridgell 1997-1998
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "system/time.h"
27 #include "system/filesys.h"
28 #include "lib/util/dlinklist.h"
29 #include "libcli/libcli.h"
30 #include "libcli/raw/libcliraw.h"
31 #include "torture/torture.h"
32 #include "libcli/libcli.h"
33 #include "torture/util.h"
35 extern int nbench_line_count;
36 static int nbio_id = -1;
38 static BOOL bypass_io;
39 static struct timeval tv_start, tv_end;
40 static int warmup, timelimit;
41 static int in_cleanup;
44 struct ftable *next, *prev;
45 int fd; /* the fd that we got back from the server */
46 int handle; /* the handle in the load file */
49 static struct ftable *ftable;
52 double bytes, warmup_bytes;
56 struct timeval starttime;
59 void nbio_target_rate(double rate)
61 static double last_bytes;
62 static struct timeval last_time;
65 if (last_bytes == 0) {
66 last_bytes = children[nbio_id].bytes;
67 last_time = timeval_current();
71 tdelay = (children[nbio_id].bytes - last_bytes)/(1.0e6*rate) - timeval_elapsed(&last_time);
75 children[nbio_id].max_latency = MAX(children[nbio_id].max_latency, -tdelay);
78 last_time = timeval_current();
79 last_bytes = children[nbio_id].bytes;
82 void nbio_time_reset(void)
84 children[nbio_id].starttime = timeval_current();
87 void nbio_time_delay(double targett)
89 double elapsed = timeval_elapsed(&children[nbio_id].starttime);
90 if (targett > elapsed) {
91 msleep(1000*(targett - elapsed));
92 } else if (elapsed - targett > children[nbio_id].max_latency) {
93 children[nbio_id].max_latency = MAX(elapsed - targett, children[nbio_id].max_latency);
97 double nbio_result(void)
101 for (i=0;i<nprocs;i++) {
102 total += children[i].bytes - children[i].warmup_bytes;
104 return 1.0e-6 * total / timeval_elapsed2(&tv_start, &tv_end);
107 double nbio_latency(void)
110 double max_latency = 0;
111 for (i=0;i<nprocs;i++) {
112 if (children[i].max_latency > max_latency) {
113 max_latency = children[i].max_latency;
114 children[i].max_latency = 0;
122 return children[nbio_id].done;
126 void nb_alarm(int sig)
133 if (nbio_id != -1) return;
135 for (i=0;i<nprocs;i++) {
136 if (children[i].bytes == 0) {
139 lines += children[i].line;
142 t = timeval_elapsed(&tv_start);
144 if (!in_warmup && warmup>0 && t > warmup) {
145 tv_start = timeval_current();
147 for (i=0;i<nprocs;i++) {
148 children[i].warmup_bytes = children[i].bytes;
154 } else if (!in_warmup && !in_cleanup && t > timelimit) {
155 for (i=0;i<nprocs;i++) {
156 children[i].done = 1;
158 tv_end = timeval_current();
165 tv_end = timeval_current();
169 printf("%4d %8d %.2f MB/sec warmup %.0f sec \n",
170 nprocs, lines/nprocs,
172 } else if (in_cleanup) {
173 printf("%4d %8d %.2f MB/sec cleanup %.0f sec \n",
174 nprocs, lines/nprocs,
177 printf("%4d %8d %.2f MB/sec execute %.0f sec latency %.2f msec \n",
178 nprocs, lines/nprocs,
179 nbio_result(), t, nbio_latency() * 1.0e3);
184 signal(SIGALRM, nb_alarm);
188 void nbio_shmem(int n, int t_timelimit, int t_warmup)
191 children = shm_setup(sizeof(*children) * nprocs);
193 printf("Failed to setup shared memory!\n");
196 memset(children, 0, sizeof(*children) * nprocs);
197 timelimit = t_timelimit;
200 tv_start = timeval_current();
203 static struct ftable *find_ftable(int handle)
207 for (f=ftable;f;f=f->next) {
208 if (f->handle == handle) return f;
213 static int find_handle(int handle)
217 children[nbio_id].line = nbench_line_count;
219 f = find_ftable(handle);
223 printf("(%d) ERROR: handle %d was not found\n",
224 nbench_line_count, handle);
227 return -1; /* Not reached */
232 static struct smbcli_state *c;
235 a handler function for oplock break requests
237 static BOOL oplock_handler(struct smbcli_transport *transport, uint16_t tid,
238 uint16_t fnum, uint8_t level, void *private)
240 struct smbcli_tree *tree = private;
241 return smbcli_oplock_ack(tree, fnum, OPLOCK_BREAK_TO_NONE);
244 void nb_setup(struct smbcli_state *cli, int id)
249 printf("skipping I/O\n");
252 smbcli_oplock_handler(cli->transport, oplock_handler, cli->tree);
257 static void check_status(const char *op, NTSTATUS status, NTSTATUS ret)
259 if (!NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(ret)) {
260 printf("[%d] Error: %s should have failed with %s\n",
261 nbench_line_count, op, nt_errstr(status));
265 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(ret)) {
266 printf("[%d] Error: %s should have succeeded - %s\n",
267 nbench_line_count, op, nt_errstr(ret));
271 if (!NT_STATUS_EQUAL(status, ret)) {
272 printf("[%d] Warning: got status %s but expected %s\n",
273 nbench_line_count, nt_errstr(ret), nt_errstr(status));
278 void nb_unlink(const char *fname, int attr, NTSTATUS status)
283 io.unlink.in.pattern = fname;
285 io.unlink.in.attrib = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
286 if (strchr(fname, '*') == 0) {
287 io.unlink.in.attrib |= FILE_ATTRIBUTE_DIRECTORY;
290 ret = smb_raw_unlink(c->tree, &io);
292 check_status("Unlink", status, ret);
296 void nb_createx(const char *fname,
297 uint_t create_options, uint_t create_disposition, int handle,
301 uint32_t desired_access;
307 mem_ctx = talloc_init("raw_open");
309 if (create_options & NTCREATEX_OPTIONS_DIRECTORY) {
310 desired_access = SEC_FILE_READ_DATA;
314 SEC_FILE_WRITE_DATA |
315 SEC_FILE_READ_ATTRIBUTE |
316 SEC_FILE_WRITE_ATTRIBUTE;
317 flags = NTCREATEX_FLAGS_EXTENDED |
318 NTCREATEX_FLAGS_REQUEST_OPLOCK |
319 NTCREATEX_FLAGS_REQUEST_BATCH_OPLOCK;
322 io.ntcreatex.level = RAW_OPEN_NTCREATEX;
323 io.ntcreatex.in.flags = flags;
324 io.ntcreatex.in.root_fid = 0;
325 io.ntcreatex.in.access_mask = desired_access;
326 io.ntcreatex.in.file_attr = 0;
327 io.ntcreatex.in.alloc_size = 0;
328 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE;
329 io.ntcreatex.in.open_disposition = create_disposition;
330 io.ntcreatex.in.create_options = create_options;
331 io.ntcreatex.in.impersonation = 0;
332 io.ntcreatex.in.security_flags = 0;
333 io.ntcreatex.in.fname = fname;
335 ret = smb_raw_open(c->tree, mem_ctx, &io);
337 talloc_free(mem_ctx);
339 check_status("NTCreateX", status, ret);
341 if (!NT_STATUS_IS_OK(ret)) return;
343 f = malloc_p(struct ftable);
345 f->fd = io.ntcreatex.out.file.fnum;
347 DLIST_ADD_END(ftable, f, struct ftable *);
350 void nb_writex(int handle, off_t offset, int size, int ret_size, NTSTATUS status)
357 i = find_handle(handle);
359 if (bypass_io) return;
362 memset(buf, 0xab, size);
364 io.writex.level = RAW_WRITE_WRITEX;
365 io.writex.in.file.fnum = i;
366 io.writex.in.wmode = 0;
367 io.writex.in.remaining = 0;
368 io.writex.in.offset = offset;
369 io.writex.in.count = size;
370 io.writex.in.data = buf;
372 ret = smb_raw_write(c->tree, &io);
376 check_status("WriteX", status, ret);
378 if (NT_STATUS_IS_OK(ret) && io.writex.out.nwritten != ret_size) {
379 printf("[%d] Warning: WriteX got count %d expected %d\n",
381 io.writex.out.nwritten, ret_size);
384 children[nbio_id].bytes += ret_size;
387 void nb_write(int handle, off_t offset, int size, int ret_size, NTSTATUS status)
394 i = find_handle(handle);
396 if (bypass_io) return;
400 memset(buf, 0x12, size);
402 io.write.level = RAW_WRITE_WRITE;
403 io.write.in.file.fnum = i;
404 io.write.in.remaining = 0;
405 io.write.in.offset = offset;
406 io.write.in.count = size;
407 io.write.in.data = buf;
409 ret = smb_raw_write(c->tree, &io);
413 check_status("Write", status, ret);
415 if (NT_STATUS_IS_OK(ret) && io.write.out.nwritten != ret_size) {
416 printf("[%d] Warning: Write got count %d expected %d\n",
418 io.write.out.nwritten, ret_size);
421 children[nbio_id].bytes += ret_size;
425 void nb_lockx(int handle, off_t offset, int size, NTSTATUS status)
430 struct smb_lock_entry lck;
432 i = find_handle(handle);
438 io.lockx.level = RAW_LOCK_LOCKX;
439 io.lockx.in.file.fnum = i;
440 io.lockx.in.mode = 0;
441 io.lockx.in.timeout = 0;
442 io.lockx.in.ulock_cnt = 0;
443 io.lockx.in.lock_cnt = 1;
444 io.lockx.in.locks = &lck;
446 ret = smb_raw_lock(c->tree, &io);
448 check_status("LockX", status, ret);
451 void nb_unlockx(int handle, uint_t offset, int size, NTSTATUS status)
456 struct smb_lock_entry lck;
458 i = find_handle(handle);
464 io.lockx.level = RAW_LOCK_LOCKX;
465 io.lockx.in.file.fnum = i;
466 io.lockx.in.mode = 0;
467 io.lockx.in.timeout = 0;
468 io.lockx.in.ulock_cnt = 1;
469 io.lockx.in.lock_cnt = 0;
470 io.lockx.in.locks = &lck;
472 ret = smb_raw_lock(c->tree, &io);
474 check_status("UnlockX", status, ret);
477 void nb_readx(int handle, off_t offset, int size, int ret_size, NTSTATUS status)
484 i = find_handle(handle);
486 if (bypass_io) return;
490 io.readx.level = RAW_READ_READX;
491 io.readx.in.file.fnum = i;
492 io.readx.in.offset = offset;
493 io.readx.in.mincnt = size;
494 io.readx.in.maxcnt = size;
495 io.readx.in.remaining = 0;
496 io.readx.in.read_for_execute = False;
497 io.readx.out.data = buf;
499 ret = smb_raw_read(c->tree, &io);
503 check_status("ReadX", status, ret);
505 if (NT_STATUS_IS_OK(ret) && io.readx.out.nread != ret_size) {
506 printf("[%d] ERROR: ReadX got count %d expected %d\n",
508 io.readx.out.nread, ret_size);
512 children[nbio_id].bytes += ret_size;
515 void nb_close(int handle, NTSTATUS status)
521 i = find_handle(handle);
523 io.close.level = RAW_CLOSE_CLOSE;
524 io.close.in.file.fnum = i;
525 io.close.in.write_time = 0;
527 ret = smb_raw_close(c->tree, &io);
529 check_status("Close", status, ret);
531 if (NT_STATUS_IS_OK(ret)) {
532 struct ftable *f = find_ftable(handle);
533 DLIST_REMOVE(ftable, f);
538 void nb_rmdir(const char *dname, NTSTATUS status)
545 ret = smb_raw_rmdir(c->tree, &io);
547 check_status("Rmdir", status, ret);
550 void nb_mkdir(const char *dname, NTSTATUS status)
554 io.mkdir.level = RAW_MKDIR_MKDIR;
555 io.mkdir.in.path = dname;
557 /* NOTE! no error checking. Used for base fileset creation */
558 smb_raw_mkdir(c->tree, &io);
561 void nb_rename(const char *old, const char *new, NTSTATUS status)
566 io.generic.level = RAW_RENAME_RENAME;
567 io.rename.in.attrib = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
568 io.rename.in.pattern1 = old;
569 io.rename.in.pattern2 = new;
571 ret = smb_raw_rename(c->tree, &io);
573 check_status("Rename", status, ret);
577 void nb_qpathinfo(const char *fname, int level, NTSTATUS status)
579 union smb_fileinfo io;
583 mem_ctx = talloc_init("nb_qpathinfo");
585 io.generic.level = level;
586 io.generic.in.file.path = fname;
588 ret = smb_raw_pathinfo(c->tree, mem_ctx, &io);
590 talloc_free(mem_ctx);
592 check_status("Pathinfo", status, ret);
596 void nb_qfileinfo(int fnum, int level, NTSTATUS status)
598 union smb_fileinfo io;
603 i = find_handle(fnum);
605 mem_ctx = talloc_init("nb_qfileinfo");
607 io.generic.level = level;
608 io.generic.in.file.fnum = i;
610 ret = smb_raw_fileinfo(c->tree, mem_ctx, &io);
612 talloc_free(mem_ctx);
614 check_status("Fileinfo", status, ret);
617 void nb_sfileinfo(int fnum, int level, NTSTATUS status)
619 union smb_setfileinfo io;
623 if (level != RAW_SFILEINFO_BASIC_INFORMATION) {
624 printf("[%d] Warning: setfileinfo level %d not handled\n", nbench_line_count, level);
630 i = find_handle(fnum);
632 io.generic.level = level;
633 io.generic.in.file.fnum = i;
634 unix_to_nt_time(&io.basic_info.in.create_time, time(NULL));
635 unix_to_nt_time(&io.basic_info.in.access_time, 0);
636 unix_to_nt_time(&io.basic_info.in.write_time, 0);
637 unix_to_nt_time(&io.basic_info.in.change_time, 0);
638 io.basic_info.in.attrib = 0;
640 ret = smb_raw_setfileinfo(c->tree, &io);
642 check_status("Setfileinfo", status, ret);
645 void nb_qfsinfo(int level, NTSTATUS status)
651 mem_ctx = talloc_init("smbcli_dskattr");
653 io.generic.level = level;
654 ret = smb_raw_fsinfo(c->tree, mem_ctx, &io);
656 talloc_free(mem_ctx);
658 check_status("Fsinfo", status, ret);
661 /* callback function used for trans2 search */
662 static BOOL findfirst_callback(void *private, const union smb_search_data *file)
667 void nb_findfirst(const char *mask, int level, int maxcnt, int count, NTSTATUS status)
669 union smb_search_first io;
673 mem_ctx = talloc_init("smbcli_dskattr");
675 io.t2ffirst.level = RAW_SEARCH_TRANS2;
676 io.t2ffirst.data_level = level;
677 io.t2ffirst.in.max_count = maxcnt;
678 io.t2ffirst.in.search_attrib = FILE_ATTRIBUTE_DIRECTORY;
679 io.t2ffirst.in.pattern = mask;
680 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE;
681 io.t2ffirst.in.storage_type = 0;
683 ret = smb_raw_search_first(c->tree, mem_ctx, &io, NULL, findfirst_callback);
685 talloc_free(mem_ctx);
687 check_status("Search", status, ret);
689 if (NT_STATUS_IS_OK(ret) && io.t2ffirst.out.count != count) {
690 printf("[%d] Warning: got count %d expected %d\n",
692 io.t2ffirst.out.count, count);
696 void nb_flush(int fnum, NTSTATUS status)
701 i = find_handle(fnum);
703 io.flush.level = RAW_FLUSH_FLUSH;
704 io.flush.in.file.fnum = i;
706 ret = smb_raw_flush(c->tree, &io);
708 check_status("Flush", status, ret);
711 void nb_sleep(int usec, NTSTATUS status)
716 void nb_deltree(const char *dname)
720 smb_raw_exit(c->session);
723 struct ftable *f = ftable;
724 DLIST_REMOVE(ftable, f);
728 total_deleted = smbcli_deltree(c->tree, dname);
730 if (total_deleted == -1) {
731 printf("Failed to cleanup tree %s - exiting\n", dname);
735 smbcli_rmdir(c->tree, dname);