cd6808516903080dc8d37eb4f2a04135031918b6
[samba.git] / source4 / torture / nbench / nbio.c
1 /*
2   TODO: add splitting of writes for servers with signing
3 */
4
5
6 /* 
7    Unix SMB/CIFS implementation.
8    SMB torture tester
9    Copyright (C) Andrew Tridgell 1997-1998
10    
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 2 of the License, or
14    (at your option) any later version.
15    
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.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26 #include "includes.h"
27 #include "system/time.h"
28 #include "system/filesys.h"
29 #include "lib/util/dlinklist.h"
30 #include "libcli/libcli.h"
31 #include "libcli/raw/libcliraw.h"
32 #include "torture/torture.h"
33 #include "libcli/libcli.h"
34 #include "torture/util.h"
35
36 extern int nbench_line_count;
37 static int nbio_id = -1;
38 static int nprocs;
39 static BOOL bypass_io;
40 static struct timeval tv_start, tv_end;
41 static int warmup, timelimit;
42 static int in_cleanup;
43
44 struct ftable {
45         struct ftable *next, *prev;
46         int fd;     /* the fd that we got back from the server */
47         int handle; /* the handle in the load file */
48 };
49
50 static struct ftable *ftable;
51
52 static struct {
53         double bytes, warmup_bytes;
54         int line;
55         int done;
56 } *children;
57
58 double nbio_result(void)
59 {
60         int i;
61         double total = 0;
62         for (i=0;i<nprocs;i++) {
63                 total += children[i].bytes - children[i].warmup_bytes;
64         }
65         return 1.0e-6 * total / timeval_elapsed2(&tv_start, &tv_end);
66 }
67
68 BOOL nb_tick(void)
69 {
70         return children[nbio_id].done;
71 }
72
73
74 void nb_alarm(int sig)
75 {
76         int i;
77         int lines=0;
78         double t;
79         int in_warmup = 0;
80
81         if (nbio_id != -1) return;
82
83         for (i=0;i<nprocs;i++) {
84                 if (children[i].bytes == 0) {
85                         in_warmup = 1;
86                 }
87                 lines += children[i].line;
88         }
89
90         t = timeval_elapsed(&tv_start);
91
92         if (!in_warmup && warmup>0 && t > warmup) {
93                 tv_start = timeval_current();
94                 warmup = 0;
95                 for (i=0;i<nprocs;i++) {
96                         children[i].warmup_bytes = children[i].bytes;
97                 }
98                 goto next;
99         }
100         if (t < warmup) {
101                 in_warmup = 1;
102         } else if (!in_warmup && !in_cleanup && t > timelimit) {
103                 for (i=0;i<nprocs;i++) {
104                         children[i].done = 1;
105                 }
106                 tv_end = timeval_current();
107                 in_cleanup = 1;
108         }
109         if (t < 1) {
110                 goto next;
111         }
112         if (!in_cleanup) {
113                 tv_end = timeval_current();
114         }
115
116         if (in_warmup) {
117                 printf("%4d  %8d  %.2f MB/sec  warmup %.0f sec   \n", 
118                        nprocs, lines/nprocs, 
119                        nbio_result(), t);
120         } else if (in_cleanup) {
121                 printf("%4d  %8d  %.2f MB/sec  cleanup %.0f sec   \n", 
122                        nprocs, lines/nprocs, 
123                        nbio_result(), t);
124         } else {
125                 printf("%4d  %8d  %.2f MB/sec  execute %.0f sec   \n", 
126                        nprocs, lines/nprocs, 
127                        nbio_result(), t);
128         }
129
130         fflush(stdout);
131 next:
132         signal(SIGALRM, nb_alarm);
133         alarm(1);       
134 }
135
136 void nbio_shmem(int n, int t_timelimit, int t_warmup)
137 {
138         nprocs = n;
139         children = shm_setup(sizeof(*children) * nprocs);
140         if (!children) {
141                 printf("Failed to setup shared memory!\n");
142                 exit(1);
143         }
144         memset(children, 0, sizeof(*children) * nprocs);
145         timelimit = t_timelimit;
146         warmup = t_warmup;
147         in_cleanup = 0;
148         tv_start = timeval_current();
149 }
150
151 static struct ftable *find_ftable(int handle)
152 {
153         struct ftable *f;
154
155         for (f=ftable;f;f=f->next) {
156                 if (f->handle == handle) return f;
157         }
158         return NULL;
159 }
160
161 static int find_handle(int handle)
162 {
163         struct ftable *f;
164
165         children[nbio_id].line = nbench_line_count;
166
167         f = find_ftable(handle);
168         if (f) {
169                 return f->fd;
170         }
171         printf("(%d) ERROR: handle %d was not found\n", 
172                nbench_line_count, handle);
173         exit(1);
174
175         return -1;              /* Not reached */
176 }
177
178
179
180 static struct smbcli_state *c;
181
182 /*
183   a handler function for oplock break requests
184 */
185 static BOOL oplock_handler(struct smbcli_transport *transport, uint16_t tid, 
186                            uint16_t fnum, uint8_t level, void *private)
187 {
188         struct smbcli_tree *tree = private;
189         return smbcli_oplock_ack(tree, fnum, OPLOCK_BREAK_TO_NONE);
190 }
191
192 void nb_setup(struct smbcli_state *cli, int id)
193 {
194         nbio_id = id;
195         c = cli;
196         if (bypass_io)
197                 printf("skipping I/O\n");
198
199         if (cli) {
200                 smbcli_oplock_handler(cli->transport, oplock_handler, cli->tree);
201         }
202 }
203
204
205 static void check_status(const char *op, NTSTATUS status, NTSTATUS ret)
206 {
207         if (!NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(ret)) {
208                 printf("[%d] Error: %s should have failed with %s\n", 
209                        nbench_line_count, op, nt_errstr(status));
210                 exit(1);
211         }
212
213         if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(ret)) {
214                 printf("[%d] Error: %s should have succeeded - %s\n", 
215                        nbench_line_count, op, nt_errstr(ret));
216                 exit(1);
217         }
218
219         if (!NT_STATUS_EQUAL(status, ret)) {
220                 printf("[%d] Warning: got status %s but expected %s\n",
221                        nbench_line_count, nt_errstr(ret), nt_errstr(status));
222         }
223 }
224
225
226 void nb_unlink(const char *fname, int attr, NTSTATUS status)
227 {
228         union smb_unlink io;
229         NTSTATUS ret;
230
231         io.unlink.in.pattern = fname;
232
233         io.unlink.in.attrib = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
234         if (strchr(fname, '*') == 0) {
235                 io.unlink.in.attrib |= FILE_ATTRIBUTE_DIRECTORY;
236         }
237
238         ret = smb_raw_unlink(c->tree, &io);
239
240         check_status("Unlink", status, ret);
241 }
242
243
244 void nb_createx(const char *fname, 
245                 uint_t create_options, uint_t create_disposition, int handle,
246                 NTSTATUS status)
247 {
248         union smb_open io;      
249         uint32_t desired_access;
250         NTSTATUS ret;
251         TALLOC_CTX *mem_ctx;
252         uint_t flags = 0;
253         struct ftable *f;
254
255         mem_ctx = talloc_init("raw_open");
256
257         if (create_options & NTCREATEX_OPTIONS_DIRECTORY) {
258                 desired_access = SEC_FILE_READ_DATA;
259         } else {
260                 desired_access = 
261                         SEC_FILE_READ_DATA | 
262                         SEC_FILE_WRITE_DATA |
263                         SEC_FILE_READ_ATTRIBUTE |
264                         SEC_FILE_WRITE_ATTRIBUTE;
265                 flags = NTCREATEX_FLAGS_EXTENDED |
266                         NTCREATEX_FLAGS_REQUEST_OPLOCK | 
267                         NTCREATEX_FLAGS_REQUEST_BATCH_OPLOCK;
268         }
269
270         io.ntcreatex.level = RAW_OPEN_NTCREATEX;
271         io.ntcreatex.in.flags = flags;
272         io.ntcreatex.in.root_fid = 0;
273         io.ntcreatex.in.access_mask = desired_access;
274         io.ntcreatex.in.file_attr = 0;
275         io.ntcreatex.in.alloc_size = 0;
276         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE;
277         io.ntcreatex.in.open_disposition = create_disposition;
278         io.ntcreatex.in.create_options = create_options;
279         io.ntcreatex.in.impersonation = 0;
280         io.ntcreatex.in.security_flags = 0;
281         io.ntcreatex.in.fname = fname;
282
283         ret = smb_raw_open(c->tree, mem_ctx, &io);
284
285         talloc_free(mem_ctx);
286
287         check_status("NTCreateX", status, ret);
288
289         if (!NT_STATUS_IS_OK(ret)) return;
290
291         f = malloc_p(struct ftable);
292         f->handle = handle;
293         f->fd = io.ntcreatex.out.file.fnum;
294
295         DLIST_ADD_END(ftable, f, struct ftable *);
296 }
297
298 void nb_writex(int handle, off_t offset, int size, int ret_size, NTSTATUS status)
299 {
300         union smb_write io;
301         int i;
302         NTSTATUS ret;
303         uint8_t *buf;
304
305         i = find_handle(handle);
306
307         if (bypass_io) return;
308
309         buf = malloc(size);
310         memset(buf, 0xab, size);
311
312         io.writex.level = RAW_WRITE_WRITEX;
313         io.writex.in.file.fnum = i;
314         io.writex.in.wmode = 0;
315         io.writex.in.remaining = 0;
316         io.writex.in.offset = offset;
317         io.writex.in.count = size;
318         io.writex.in.data = buf;
319
320         ret = smb_raw_write(c->tree, &io);
321
322         free(buf);
323
324         check_status("WriteX", status, ret);
325
326         if (NT_STATUS_IS_OK(ret) && io.writex.out.nwritten != ret_size) {
327                 printf("[%d] Warning: WriteX got count %d expected %d\n", 
328                        nbench_line_count,
329                        io.writex.out.nwritten, ret_size);
330         }       
331
332         children[nbio_id].bytes += ret_size;
333 }
334
335 void nb_write(int handle, off_t offset, int size, int ret_size, NTSTATUS status)
336 {
337         union smb_write io;
338         int i;
339         NTSTATUS ret;
340         uint8_t *buf;
341
342         i = find_handle(handle);
343
344         if (bypass_io) return;
345
346         buf = malloc(size);
347
348         memset(buf, 0x12, size);
349
350         io.write.level = RAW_WRITE_WRITE;
351         io.write.in.file.fnum = i;
352         io.write.in.remaining = 0;
353         io.write.in.offset = offset;
354         io.write.in.count = size;
355         io.write.in.data = buf;
356
357         ret = smb_raw_write(c->tree, &io);
358
359         free(buf);
360
361         check_status("Write", status, ret);
362
363         if (NT_STATUS_IS_OK(ret) && io.write.out.nwritten != ret_size) {
364                 printf("[%d] Warning: Write got count %d expected %d\n", 
365                        nbench_line_count,
366                        io.write.out.nwritten, ret_size);
367         }       
368
369         children[nbio_id].bytes += ret_size;
370 }
371
372
373 void nb_lockx(int handle, off_t offset, int size, NTSTATUS status)
374 {
375         union smb_lock io;
376         int i;
377         NTSTATUS ret;
378         struct smb_lock_entry lck;
379
380         i = find_handle(handle);
381
382         lck.pid = getpid();
383         lck.offset = offset;
384         lck.count = size;
385
386         io.lockx.level = RAW_LOCK_LOCKX;
387         io.lockx.in.file.fnum = i;
388         io.lockx.in.mode = 0;
389         io.lockx.in.timeout = 0;
390         io.lockx.in.ulock_cnt = 0;
391         io.lockx.in.lock_cnt = 1;
392         io.lockx.in.locks = &lck;
393
394         ret = smb_raw_lock(c->tree, &io);
395
396         check_status("LockX", status, ret);
397 }
398
399 void nb_unlockx(int handle, uint_t offset, int size, NTSTATUS status)
400 {
401         union smb_lock io;
402         int i;
403         NTSTATUS ret;
404         struct smb_lock_entry lck;
405
406         i = find_handle(handle);
407
408         lck.pid = getpid();
409         lck.offset = offset;
410         lck.count = size;
411
412         io.lockx.level = RAW_LOCK_LOCKX;
413         io.lockx.in.file.fnum = i;
414         io.lockx.in.mode = 0;
415         io.lockx.in.timeout = 0;
416         io.lockx.in.ulock_cnt = 1;
417         io.lockx.in.lock_cnt = 0;
418         io.lockx.in.locks = &lck;
419
420         ret = smb_raw_lock(c->tree, &io);
421
422         check_status("UnlockX", status, ret);
423 }
424
425 void nb_readx(int handle, off_t offset, int size, int ret_size, NTSTATUS status)
426 {
427         union smb_read io;
428         int i;
429         NTSTATUS ret;
430         uint8_t *buf;
431
432         i = find_handle(handle);
433
434         if (bypass_io) return;
435
436         buf = malloc(size);
437
438         io.readx.level = RAW_READ_READX;
439         io.readx.in.file.fnum = i;
440         io.readx.in.offset    = offset;
441         io.readx.in.mincnt    = size;
442         io.readx.in.maxcnt    = size;
443         io.readx.in.remaining = 0;
444         io.readx.in.read_for_execute = False;
445         io.readx.out.data     = buf;
446                 
447         ret = smb_raw_read(c->tree, &io);
448
449         free(buf);
450
451         check_status("ReadX", status, ret);
452
453         if (NT_STATUS_IS_OK(ret) && io.readx.out.nread != ret_size) {
454                 printf("[%d] ERROR: ReadX got count %d expected %d\n", 
455                        nbench_line_count,
456                        io.readx.out.nread, ret_size);
457                 exit(1);
458         }       
459
460         children[nbio_id].bytes += ret_size;
461 }
462
463 void nb_close(int handle, NTSTATUS status)
464 {
465         NTSTATUS ret;
466         union smb_close io;
467         int i;
468
469         i = find_handle(handle);
470
471         io.close.level = RAW_CLOSE_CLOSE;
472         io.close.in.file.fnum = i;
473         io.close.in.write_time = 0;
474
475         ret = smb_raw_close(c->tree, &io);
476
477         check_status("Close", status, ret);
478
479         if (NT_STATUS_IS_OK(ret)) {
480                 struct ftable *f = find_ftable(handle);
481                 DLIST_REMOVE(ftable, f);
482                 free(f);
483         }
484 }
485
486 void nb_rmdir(const char *dname, NTSTATUS status)
487 {
488         NTSTATUS ret;
489         struct smb_rmdir io;
490
491         io.in.path = dname;
492
493         ret = smb_raw_rmdir(c->tree, &io);
494
495         check_status("Rmdir", status, ret);
496 }
497
498 void nb_mkdir(const char *dname, NTSTATUS status)
499 {
500         union smb_mkdir io;
501
502         io.mkdir.level = RAW_MKDIR_MKDIR;
503         io.mkdir.in.path = dname;
504
505         /* NOTE! no error checking. Used for base fileset creation */
506         smb_raw_mkdir(c->tree, &io);
507 }
508
509 void nb_rename(const char *old, const char *new, NTSTATUS status)
510 {
511         NTSTATUS ret;
512         union smb_rename io;
513
514         io.generic.level = RAW_RENAME_RENAME;
515         io.rename.in.attrib = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
516         io.rename.in.pattern1 = old;
517         io.rename.in.pattern2 = new;
518
519         ret = smb_raw_rename(c->tree, &io);
520
521         check_status("Rename", status, ret);
522 }
523
524
525 void nb_qpathinfo(const char *fname, int level, NTSTATUS status)
526 {
527         union smb_fileinfo io;
528         TALLOC_CTX *mem_ctx;
529         NTSTATUS ret;
530
531         mem_ctx = talloc_init("nb_qpathinfo");
532
533         io.generic.level = level;
534         io.generic.in.file.path = fname;
535
536         ret = smb_raw_pathinfo(c->tree, mem_ctx, &io);
537
538         talloc_free(mem_ctx);
539
540         check_status("Pathinfo", status, ret);
541 }
542
543
544 void nb_qfileinfo(int fnum, int level, NTSTATUS status)
545 {
546         union smb_fileinfo io;
547         TALLOC_CTX *mem_ctx;
548         NTSTATUS ret;
549         int i;
550
551         i = find_handle(fnum);
552
553         mem_ctx = talloc_init("nb_qfileinfo");
554
555         io.generic.level = level;
556         io.generic.in.file.fnum = i;
557
558         ret = smb_raw_fileinfo(c->tree, mem_ctx, &io);
559
560         talloc_free(mem_ctx);
561
562         check_status("Fileinfo", status, ret);
563 }
564
565 void nb_sfileinfo(int fnum, int level, NTSTATUS status)
566 {
567         union smb_setfileinfo io;
568         NTSTATUS ret;
569         int i;
570
571         if (level != RAW_SFILEINFO_BASIC_INFORMATION) {
572                 printf("[%d] Warning: setfileinfo level %d not handled\n", nbench_line_count, level);
573                 return;
574         }
575
576         ZERO_STRUCT(io);
577
578         i = find_handle(fnum);
579
580         io.generic.level = level;
581         io.generic.in.file.fnum = i;
582         unix_to_nt_time(&io.basic_info.in.create_time, time(NULL));
583         unix_to_nt_time(&io.basic_info.in.access_time, 0);
584         unix_to_nt_time(&io.basic_info.in.write_time, 0);
585         unix_to_nt_time(&io.basic_info.in.change_time, 0);
586         io.basic_info.in.attrib = 0;
587
588         ret = smb_raw_setfileinfo(c->tree, &io);
589
590         check_status("Setfileinfo", status, ret);
591 }
592
593 void nb_qfsinfo(int level, NTSTATUS status)
594 {
595         union smb_fsinfo io;
596         TALLOC_CTX *mem_ctx;
597         NTSTATUS ret;
598
599         mem_ctx = talloc_init("smbcli_dskattr");
600
601         io.generic.level = level;
602         ret = smb_raw_fsinfo(c->tree, mem_ctx, &io);
603
604         talloc_free(mem_ctx);
605         
606         check_status("Fsinfo", status, ret);    
607 }
608
609 /* callback function used for trans2 search */
610 static BOOL findfirst_callback(void *private, union smb_search_data *file)
611 {
612         return True;
613 }
614
615 void nb_findfirst(const char *mask, int level, int maxcnt, int count, NTSTATUS status)
616 {
617         union smb_search_first io;
618         TALLOC_CTX *mem_ctx;
619         NTSTATUS ret;
620
621         mem_ctx = talloc_init("smbcli_dskattr");
622
623         io.t2ffirst.level = RAW_SEARCH_TRANS2;
624         io.t2ffirst.data_level = level;
625         io.t2ffirst.in.max_count = maxcnt;
626         io.t2ffirst.in.search_attrib = FILE_ATTRIBUTE_DIRECTORY;
627         io.t2ffirst.in.pattern = mask;
628         io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE;
629         io.t2ffirst.in.storage_type = 0;
630                         
631         ret = smb_raw_search_first(c->tree, mem_ctx, &io, NULL, findfirst_callback);
632
633         talloc_free(mem_ctx);
634
635         check_status("Search", status, ret);
636
637         if (NT_STATUS_IS_OK(ret) && io.t2ffirst.out.count != count) {
638                 printf("[%d] Warning: got count %d expected %d\n", 
639                        nbench_line_count,
640                        io.t2ffirst.out.count, count);
641         }
642 }
643
644 void nb_flush(int fnum, NTSTATUS status)
645 {
646         union smb_flush io;
647         NTSTATUS ret;
648         int i;
649         i = find_handle(fnum);
650
651         io.flush.level          = RAW_FLUSH_FLUSH;
652         io.flush.in.file.fnum   = i;
653
654         ret = smb_raw_flush(c->tree, &io);
655
656         check_status("Flush", status, ret);
657 }
658
659 void nb_sleep(int usec, NTSTATUS status)
660 {
661         usleep(usec);
662 }
663
664 void nb_deltree(const char *dname)
665 {
666         int total_deleted;
667
668         smb_raw_exit(c->session);
669
670         while (ftable) {
671                 struct ftable *f = ftable;
672                 DLIST_REMOVE(ftable, f);
673                 free(f);
674         }
675
676         total_deleted = smbcli_deltree(c->tree, dname);
677
678         if (total_deleted == -1) {
679                 printf("Failed to cleanup tree %s - exiting\n", dname);
680                 exit(1);
681         }
682
683         smbcli_rmdir(c->tree, dname);
684 }
685