summaryrefslogtreecommitdiffstats
path: root/source4/torture/raw/pingpong.c
blob: 61f1d6b39bf6dd04b6dc330d263f9930df6f5b02 (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
/* 
   Unix SMB/CIFS implementation.

   ping pong test

   Copyright (C) Ronnie Sahlberg 2007

   Significantly based on and borrowed from lockbench.c by
   Copyright (C) Andrew Tridgell 2006
   
   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/>.
*/

/*
   filename is specified by
	 --option=torture:filename=...

   number of locks is specified by
	 --option=torture:num_locks=...

   locktimeout is specified in ms by
	 --option=torture:locktimeout=...

       default is 100 seconds
       if set to 0 pingpong will instead loop trying the lock operation
       over and over until it completes.

   reading from the file can be enabled with
	 --option=torture:read=true

   writing to the file can be enabled with
	 --option=torture:write=true

*/
#include "includes.h"
#include "system/time.h"
#include "system/filesys.h"
#include "libcli/libcli.h"
#include "torture/util.h"
#include "torture/raw/proto.h"

static void lock_byte(struct smbcli_state *cli, int fd, int offset, int lock_timeout)
{
	union smb_lock io;
	struct smb_lock_entry lock;
	NTSTATUS status;

try_again:
	ZERO_STRUCT(lock);
	io.lockx.in.ulock_cnt = 0;
	io.lockx.in.lock_cnt = 1;

	lock.count = 1;
	lock.offset = offset;
	lock.pid = cli->tree->session->pid;
	io.lockx.level = RAW_LOCK_LOCKX;
	io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
	io.lockx.in.timeout = lock_timeout;
	io.lockx.in.locks = &lock;
	io.lockx.in.file.fnum = fd;

	status = smb_raw_lock(cli->tree, &io);

	/* If we don't use timeouts and we got file lock conflict
	   just try the lock again.
	*/
	if (lock_timeout==0) {
		if ( (NT_STATUS_EQUAL(NT_STATUS_FILE_LOCK_CONFLICT, status))
		   ||(NT_STATUS_EQUAL(NT_STATUS_LOCK_NOT_GRANTED, status)) ) {
			goto try_again;
		}
	}

	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0,("Lock failed\n"));
		exit(1);
	}
}

static void unlock_byte(struct smbcli_state *cli, int fd, int offset)
{
	union smb_lock io;
	struct smb_lock_entry lock;
	NTSTATUS status;

	ZERO_STRUCT(lock);
	io.lockx.in.ulock_cnt = 1;
	io.lockx.in.lock_cnt = 0;

	lock.count = 1;
	lock.offset = offset;
	lock.pid = cli->tree->session->pid;
	io.lockx.level = RAW_LOCK_LOCKX;
	io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
	io.lockx.in.timeout = 100000;
	io.lockx.in.locks = &lock;
	io.lockx.in.file.fnum = fd;

	status = smb_raw_lock(cli->tree, &io);

	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0,("Unlock failed\n"));
		exit(1);
	}
}

static void write_byte(struct smbcli_state *cli, int fd, uint8_t c, int offset)
{
	union smb_write io;
	NTSTATUS status;

	io.generic.level = RAW_WRITE_WRITEX;
	io.writex.in.file.fnum = fd;
	io.writex.in.offset = offset;
	io.writex.in.wmode = 0;
	io.writex.in.remaining = 0;
	io.writex.in.count = 1;
	io.writex.in.data = &c;

	status = smb_raw_write(cli->tree, &io);
	if (!NT_STATUS_IS_OK(status)) {
		printf("write failed\n");
		exit(1);
	}
}	

static void read_byte(struct smbcli_state *cli, int fd, uint8_t *c, int offset)
{
	union smb_read io;
	NTSTATUS status;

	io.generic.level = RAW_READ_READX;
	io.readx.in.file.fnum = fd;
	io.readx.in.mincnt = 1;
	io.readx.in.maxcnt = 1;
	io.readx.in.offset = offset;
	io.readx.in.remaining = 0;
	io.readx.in.read_for_execute = false;
	io.readx.out.data = c;

	status = smb_raw_read(cli->tree, &io);
	if (!NT_STATUS_IS_OK(status)) {
		printf("read failed\n");
		exit(1);
	}
}	

/* 
   ping pong
*/
bool torture_ping_pong(struct torture_context *torture)
{
	const char *fn;
	int num_locks;
	TALLOC_CTX *mem_ctx = talloc_new(torture);
	static bool do_reads;
	static bool do_writes;
	int lock_timeout;
	int fd;
	struct smbcli_state *cli;
	int i;
	uint8_t incr=0, last_incr=0;
	uint8_t *val;
	int count, loops;
	struct timeval start;

	fn = torture_setting_string(torture, "filename", NULL);
	if (fn == NULL) {
		DEBUG(0,("You must specify the filename using --option=torture:filename=...\n"));
		return false;
	}

	num_locks = torture_setting_int(torture, "num_locks", -1);
	if (num_locks == -1) {
		DEBUG(0,("You must specify num_locks using --option=torture:num_locks=...\n"));
		return false;
	}

	do_reads     = torture_setting_bool(torture, "read", false);
	do_writes    = torture_setting_bool(torture, "write", false);
	lock_timeout =  torture_setting_int(torture, "lock_timeout", 100000);

	if (!torture_open_connection(&cli, torture, 0)) {
		DEBUG(0,("Could not open connection\n"));
		return false;
	}

	fd = smbcli_open(cli->tree, fn, O_RDWR|O_CREAT, DENY_NONE);
	if (fd == -1) {
		printf("Failed to open %s\n", fn);
		exit(1);
	}

	write_byte(cli, fd, 0, num_locks);
	lock_byte(cli, fd, 0, lock_timeout);


	start = timeval_current();
	val = talloc_zero_array(mem_ctx, uint8_t, num_locks);
	i = 0;
	count = 0;
	loops = 0;
	while (1) {
		lock_byte(cli, fd, (i+1)%num_locks, lock_timeout);

		if (do_reads) {
			uint8_t c;
			read_byte(cli, fd, &c, i);
			incr   = c-val[i];
			val[i] = c;			
		}

		if (do_writes) {
			uint8_t c = val[i] + 1;
			write_byte(cli, fd, c, i);
		}

		unlock_byte(cli, fd, i);

		i = (i+1)%num_locks;
		count++;
		if (loops>num_locks && incr!=last_incr) {
			last_incr = incr;
			printf("data increment = %u\n", incr);
			fflush(stdout);
		}
		if (timeval_elapsed(&start) > 1.0) {
			printf("%8u locks/sec\r", 
			       (unsigned)(2*count/timeval_elapsed(&start)));
			fflush(stdout);
			start = timeval_current();
			count=0;
		}
		loops++;
	}
}