2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2007
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "torture/torture.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/raw/raw_proto.h"
26 #include "system/time.h"
27 #include "system/filesys.h"
28 #include "libcli/libcli.h"
29 #include "torture/util.h"
30 #include "lib/events/events.h"
31 #include "lib/cmdline/popt_common.h"
32 #include "libcli/composite/composite.h"
33 #include "libcli/smb_composite/smb_composite.h"
34 #include "libcli/resolve/resolve.h"
35 #include "param/param.h"
36 #include "torture/raw/proto.h"
38 #define BASEDIR "\\benchopen"
41 static int open_failed
;
42 static int close_failed
;
44 static int num_connected
;
45 static struct tevent_timer
*report_te
;
47 struct benchopen_state
{
48 struct torture_context
*tctx
;
50 struct tevent_context
*ev
;
51 struct smbcli_state
*cli
;
52 struct smbcli_tree
*tree
;
62 union smb_open open_parms
;
64 union smb_close close_parms
;
65 struct smbcli_request
*req_open
;
66 struct smbcli_request
*req_close
;
67 struct smb_composite_connect reconnect
;
68 struct tevent_timer
*te
;
70 /* these are used for reconnections */
71 const char **dest_ports
;
72 const char *dest_host
;
73 const char *called_name
;
74 const char *service_type
;
77 static void next_open(struct benchopen_state
*state
);
78 static void reopen_connection(struct tevent_context
*ev
, struct tevent_timer
*te
,
79 struct timeval t
, void *private_data
);
83 complete an async reconnect
85 static void reopen_connection_complete(struct composite_context
*ctx
)
87 struct benchopen_state
*state
= (struct benchopen_state
*)ctx
->async
.private_data
;
89 struct smb_composite_connect
*io
= &state
->reconnect
;
91 status
= smb_composite_connect_recv(ctx
, state
->mem_ctx
);
92 if (!NT_STATUS_IS_OK(status
)) {
93 talloc_free(state
->te
);
94 state
->te
= tevent_add_timer(state
->ev
, state
->mem_ctx
,
95 timeval_current_ofs(1,0),
96 reopen_connection
, state
);
100 state
->tree
= io
->out
.tree
;
104 DEBUG(0,("[%u] reconnect to %s finished (%u connected)\n",
105 state
->client_num
, state
->dest_host
, num_connected
));
107 state
->open_fnum
= -1;
108 state
->close_fnum
= -1;
117 static void reopen_connection(struct tevent_context
*ev
, struct tevent_timer
*te
,
118 struct timeval t
, void *private_data
)
120 struct benchopen_state
*state
= (struct benchopen_state
*)private_data
;
121 struct composite_context
*ctx
;
122 struct smb_composite_connect
*io
= &state
->reconnect
;
127 if (!torture_get_conn_index(state
->client_num
, state
->mem_ctx
, state
->tctx
, &host
, &share
)) {
128 DEBUG(0,("Can't find host/share for reconnect?!\n"));
132 io
->in
.dest_host
= state
->dest_host
;
133 io
->in
.dest_ports
= state
->dest_ports
;
134 io
->in
.socket_options
= lpcfg_socket_options(state
->tctx
->lp_ctx
);
135 io
->in
.called_name
= state
->called_name
;
136 io
->in
.service
= share
;
137 io
->in
.service_type
= state
->service_type
;
138 io
->in
.credentials
= cmdline_credentials
;
139 io
->in
.fallback_to_anonymous
= false;
140 io
->in
.workgroup
= lpcfg_workgroup(state
->tctx
->lp_ctx
);
141 io
->in
.gensec_settings
= lpcfg_gensec_settings(state
->mem_ctx
, state
->tctx
->lp_ctx
);
142 lpcfg_smbcli_options(state
->tctx
->lp_ctx
, &io
->in
.options
);
143 lpcfg_smbcli_session_options(state
->tctx
->lp_ctx
, &io
->in
.session_options
);
145 /* kill off the remnants of the old connection */
146 talloc_free(state
->tree
);
148 state
->open_fnum
= -1;
149 state
->close_fnum
= -1;
151 ctx
= smb_composite_connect_send(io
, state
->mem_ctx
,
152 lpcfg_resolve_context(state
->tctx
->lp_ctx
),
155 DEBUG(0,("Failed to setup async reconnect\n"));
159 ctx
->async
.fn
= reopen_connection_complete
;
160 ctx
->async
.private_data
= state
;
163 static void open_completed(struct smbcli_request
*req
);
164 static void close_completed(struct smbcli_request
*req
);
167 static void next_open(struct benchopen_state
*state
)
171 state
->pending_file_num
= state
->next_file_num
;
172 state
->next_file_num
= (state
->next_file_num
+1) % (3*nprocs
);
174 DEBUG(2,("[%d] opening %u\n", state
->client_num
, state
->pending_file_num
));
175 state
->open_parms
.ntcreatex
.level
= RAW_OPEN_NTCREATEX
;
176 state
->open_parms
.ntcreatex
.in
.flags
= 0;
177 state
->open_parms
.ntcreatex
.in
.root_fid
.fnum
= 0;
178 state
->open_parms
.ntcreatex
.in
.access_mask
= SEC_RIGHTS_FILE_ALL
;
179 state
->open_parms
.ntcreatex
.in
.file_attr
= FILE_ATTRIBUTE_NORMAL
;
180 state
->open_parms
.ntcreatex
.in
.alloc_size
= 0;
181 state
->open_parms
.ntcreatex
.in
.share_access
= 0;
182 state
->open_parms
.ntcreatex
.in
.open_disposition
= NTCREATEX_DISP_OVERWRITE_IF
;
183 state
->open_parms
.ntcreatex
.in
.create_options
= 0;
184 state
->open_parms
.ntcreatex
.in
.impersonation
= 0;
185 state
->open_parms
.ntcreatex
.in
.security_flags
= 0;
186 state
->open_parms
.ntcreatex
.in
.fname
= fnames
[state
->pending_file_num
];
188 state
->req_open
= smb_raw_open_send(state
->tree
, &state
->open_parms
);
189 state
->req_open
->async
.fn
= open_completed
;
190 state
->req_open
->async
.private_data
= state
;
194 static void next_close(struct benchopen_state
*state
)
196 if (state
->close_fnum
== -1) {
199 DEBUG(2,("[%d] closing %d (fnum[%d])\n",
200 state
->client_num
, state
->close_file_num
, state
->close_fnum
));
201 state
->close_parms
.close
.level
= RAW_CLOSE_CLOSE
;
202 state
->close_parms
.close
.in
.file
.fnum
= state
->close_fnum
;
203 state
->close_parms
.close
.in
.write_time
= 0;
205 state
->req_close
= smb_raw_close_send(state
->tree
, &state
->close_parms
);
206 state
->req_close
->async
.fn
= close_completed
;
207 state
->req_close
->async
.private_data
= state
;
211 called when a open completes
213 static void open_completed(struct smbcli_request
*req
)
215 struct benchopen_state
*state
= (struct benchopen_state
*)req
->async
.private_data
;
216 TALLOC_CTX
*tmp_ctx
= talloc_new(state
->mem_ctx
);
219 status
= smb_raw_open_recv(req
, tmp_ctx
, &state
->open_parms
);
221 talloc_free(tmp_ctx
);
223 state
->req_open
= NULL
;
225 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
) ||
226 NT_STATUS_EQUAL(status
, NT_STATUS_LOCAL_DISCONNECT
) ||
227 NT_STATUS_EQUAL(status
, NT_STATUS_CONNECTION_RESET
)) {
228 talloc_free(state
->tree
);
229 talloc_free(state
->cli
);
233 DEBUG(0,("[%u] reopening connection to %s\n",
234 state
->client_num
, state
->dest_host
));
235 talloc_free(state
->te
);
236 state
->te
= tevent_add_timer(state
->ev
, state
->mem_ctx
,
237 timeval_current_ofs(1,0),
238 reopen_connection
, state
);
242 if (NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
)) {
243 DEBUG(2,("[%d] retrying open %d\n",
244 state
->client_num
, state
->pending_file_num
));
245 state
->open_retries
++;
246 state
->req_open
= smb_raw_open_send(state
->tree
, &state
->open_parms
);
247 state
->req_open
->async
.fn
= open_completed
;
248 state
->req_open
->async
.private_data
= state
;
252 if (!NT_STATUS_IS_OK(status
)) {
254 DEBUG(0,("[%u] open failed %d - %s\n",
255 state
->client_num
, state
->pending_file_num
,
260 state
->close_file_num
= state
->open_file_num
;
261 state
->close_fnum
= state
->open_fnum
;
262 state
->open_file_num
= state
->pending_file_num
;
263 state
->open_fnum
= state
->open_parms
.ntcreatex
.out
.file
.fnum
;
265 DEBUG(2,("[%d] open completed %d (fnum[%d])\n",
266 state
->client_num
, state
->open_file_num
, state
->open_fnum
));
268 if (state
->close_fnum
!= -1) {
276 called when a close completes
278 static void close_completed(struct smbcli_request
*req
)
280 struct benchopen_state
*state
= (struct benchopen_state
*)req
->async
.private_data
;
281 NTSTATUS status
= smbcli_request_simple_recv(req
);
283 state
->req_close
= NULL
;
285 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
) ||
286 NT_STATUS_EQUAL(status
, NT_STATUS_LOCAL_DISCONNECT
) ||
287 NT_STATUS_EQUAL(status
, NT_STATUS_CONNECTION_RESET
)) {
288 talloc_free(state
->tree
);
289 talloc_free(state
->cli
);
293 DEBUG(0,("[%u] reopening connection to %s\n",
294 state
->client_num
, state
->dest_host
));
295 talloc_free(state
->te
);
296 state
->te
= tevent_add_timer(state
->ev
, state
->mem_ctx
,
297 timeval_current_ofs(1,0),
298 reopen_connection
, state
);
302 if (!NT_STATUS_IS_OK(status
)) {
304 DEBUG(0,("[%u] close failed %d (fnum[%d]) - %s\n",
305 state
->client_num
, state
->close_file_num
,
311 DEBUG(2,("[%d] close completed %d (fnum[%d])\n",
312 state
->client_num
, state
->close_file_num
,
316 static void echo_completion(struct smbcli_request
*req
)
318 struct benchopen_state
*state
= (struct benchopen_state
*)req
->async
.private_data
;
319 NTSTATUS status
= smbcli_request_simple_recv(req
);
320 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
) ||
321 NT_STATUS_EQUAL(status
, NT_STATUS_LOCAL_DISCONNECT
) ||
322 NT_STATUS_EQUAL(status
, NT_STATUS_CONNECTION_RESET
)) {
323 talloc_free(state
->tree
);
326 DEBUG(0,("[%u] reopening connection to %s\n",
327 state
->client_num
, state
->dest_host
));
328 talloc_free(state
->te
);
329 state
->te
= tevent_add_timer(state
->ev
, state
->mem_ctx
,
330 timeval_current_ofs(1,0),
331 reopen_connection
, state
);
335 static void report_rate(struct tevent_context
*ev
, struct tevent_timer
*te
,
336 struct timeval t
, void *private_data
)
338 struct benchopen_state
*state
= talloc_get_type(private_data
,
339 struct benchopen_state
);
341 for (i
=0;i
<nprocs
;i
++) {
342 printf("%5u ", (unsigned)(state
[i
].count
- state
[i
].lastcount
));
343 state
[i
].lastcount
= state
[i
].count
;
347 report_te
= tevent_add_timer(ev
, state
, timeval_current_ofs(1, 0),
350 /* send an echo on each interface to ensure it stays alive - this helps
352 for (i
=0;i
<nprocs
;i
++) {
354 struct smbcli_request
*req
;
356 if (!state
[i
].tree
) {
360 p
.in
.repeat_count
= 1;
363 req
= smb_raw_echo_send(state
[i
].tree
->session
->transport
, &p
);
364 req
->async
.private_data
= &state
[i
];
365 req
->async
.fn
= echo_completion
;
372 bool torture_bench_open(struct torture_context
*torture
)
375 TALLOC_CTX
*mem_ctx
= talloc_new(torture
);
377 int timelimit
= torture_setting_int(torture
, "timelimit", 10);
379 struct benchopen_state
*state
;
381 int total_retries
= 0;
385 progress
= torture_setting_bool(torture
, "progress", true);
387 nprocs
= torture_setting_int(torture
, "nprocs", 4);
389 state
= talloc_zero_array(mem_ctx
, struct benchopen_state
, nprocs
);
391 printf("Opening %d connections\n", nprocs
);
392 for (i
=0;i
<nprocs
;i
++) {
393 state
[i
].tctx
= torture
;
394 state
[i
].mem_ctx
= talloc_new(state
);
395 state
[i
].client_num
= i
;
396 state
[i
].ev
= torture
->ev
;
397 if (!torture_open_connection_ev(&state
[i
].cli
, i
, torture
, torture
->ev
)) {
400 talloc_steal(mem_ctx
, state
);
401 state
[i
].tree
= state
[i
].cli
->tree
;
402 state
[i
].dest_host
= talloc_strdup(state
[i
].mem_ctx
,
403 state
[i
].cli
->tree
->session
->transport
->socket
->hostname
);
404 state
[i
].dest_ports
= talloc_array(state
[i
].mem_ctx
,
406 state
[i
].dest_ports
[0] = talloc_asprintf(state
[i
].dest_ports
,
407 "%u", state
[i
].cli
->tree
->session
->transport
->socket
->port
);
408 state
[i
].dest_ports
[1] = NULL
;
409 state
[i
].called_name
= talloc_strdup(state
[i
].mem_ctx
,
410 state
[i
].cli
->tree
->session
->transport
->called
.name
);
411 state
[i
].service_type
= talloc_strdup(state
[i
].mem_ctx
,
412 state
[i
].cli
->tree
->device
);
417 if (!torture_setup_dir(state
[0].cli
, BASEDIR
)) {
421 fnames
= talloc_array(mem_ctx
, char *, 3*nprocs
);
422 for (i
=0;i
<3*nprocs
;i
++) {
423 fnames
[i
] = talloc_asprintf(fnames
, "%s\\file%d.dat", BASEDIR
, i
);
426 for (i
=0;i
<nprocs
;i
++) {
427 /* all connections start with the same file */
428 state
[i
].next_file_num
= 0;
429 state
[i
].open_fnum
= -1;
430 state
[i
].close_fnum
= -1;
431 next_open(&state
[i
]);
434 tv
= timeval_current();
437 report_te
= tevent_add_timer(torture
->ev
, state
, timeval_current_ofs(1, 0),
441 printf("Running for %d seconds\n", timelimit
);
442 while (timeval_elapsed(&tv
) < timelimit
) {
443 tevent_loop_once(torture
->ev
);
446 DEBUG(0,("open failed\n"));
450 DEBUG(0,("open failed\n"));
455 talloc_free(report_te
);
457 for (i
=0;i
<nprocs
;i
++) {
463 minops
= state
[0].count
;
464 for (i
=0;i
<nprocs
;i
++) {
465 total
+= state
[i
].count
;
466 total_retries
+= state
[i
].open_retries
;
467 printf("[%d] %u ops (%u retries)\n",
468 i
, state
[i
].count
, state
[i
].open_retries
);
469 if (state
[i
].count
< minops
) minops
= state
[i
].count
;
471 printf("%.2f ops/second (%d retries)\n",
472 total
/timeval_elapsed(&tv
), total_retries
);
473 if (minops
< 0.5*total
/nprocs
) {
474 printf("Failed: unbalanced open\n");
478 for (i
=0;i
<nprocs
;i
++) {
479 talloc_free(state
[i
].req_open
);
480 talloc_free(state
[i
].req_close
);
481 smb_raw_exit(state
[i
].tree
->session
);
484 smbcli_deltree(state
[0].tree
, BASEDIR
);
485 talloc_free(mem_ctx
);
489 talloc_free(mem_ctx
);