r23792: convert Samba4 to GPLv3
[kai/samba.git] / source4 / torture / nbt / winsbench.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    WINS benchmark test
5
6    Copyright (C) Andrew Tridgell 2005
7    
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.
12    
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.
17    
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/>.
20 */
21
22 #include "includes.h"
23 #include "lib/events/events.h"
24 #include "lib/socket/socket.h"
25 #include "libcli/resolve/resolve.h"
26 #include "system/network.h"
27 #include "lib/socket/netif.h"
28 #include "torture/torture.h"
29 #include "torture/nbt/proto.h"
30
31 struct wins_state {
32         int num_names;
33         bool *registered;
34         int pass_count;
35         int fail_count;
36         const char *wins_server;
37         const char *my_ip;
38         uint32_t ttl;
39 };
40
41 struct idx_state {
42         int idx;
43         struct wins_state *state;
44 };
45
46 struct nbt_name generate_name(TALLOC_CTX *tctx, int idx)
47 {
48         struct nbt_name name;
49         name.name       = talloc_asprintf(tctx, "WINSBench%6u", idx);
50         name.type       = 0x4;
51         name.scope      = NULL;
52         return name;
53 }
54
55 static void register_handler(struct nbt_name_request *req)
56 {
57         struct idx_state *istate = talloc_get_type(req->async.private, struct idx_state);
58         struct wins_state *state = istate->state;
59         struct nbt_name_register io;
60         NTSTATUS status;
61
62         status = nbt_name_register_recv(req, istate, &io);
63         if (!NT_STATUS_IS_OK(status) || io.out.rcode != NBT_RCODE_OK) {
64                 state->fail_count++;
65         } else {
66                 state->pass_count++;
67                 state->registered[istate->idx] = true;
68         }
69         talloc_free(istate);    
70 }
71
72 /*
73   generate a registration
74 */
75 static void generate_register(struct nbt_name_socket *nbtsock, struct wins_state *state, int idx)
76 {
77         struct nbt_name_register io;
78         TALLOC_CTX *tmp_ctx = talloc_new(state);
79         struct nbt_name_request *req;
80         struct idx_state *istate;
81
82         istate = talloc(nbtsock, struct idx_state);
83         istate->idx = idx;
84         istate->state = state;
85
86         io.in.name            = generate_name(tmp_ctx, idx);
87         io.in.dest_addr       = state->wins_server;
88         io.in.address         = state->my_ip;
89         io.in.nb_flags        = NBT_NODE_H;
90         io.in.register_demand = false;
91         io.in.broadcast       = false;
92         io.in.multi_homed     = false;
93         io.in.ttl             = state->ttl;
94         io.in.timeout         = 2;
95         io.in.retries         = 1;
96
97         req = nbt_name_register_send(nbtsock, &io);
98
99         req->async.fn = register_handler;
100         req->async.private = istate;
101
102         talloc_free(tmp_ctx);
103 }
104
105
106 static void release_handler(struct nbt_name_request *req)
107 {
108         struct idx_state *istate = talloc_get_type(req->async.private, struct idx_state);
109         struct wins_state *state = istate->state;
110         struct nbt_name_release io;
111         NTSTATUS status;
112
113         status = nbt_name_release_recv(req, istate, &io);
114         if (state->registered[istate->idx] && 
115             (!NT_STATUS_IS_OK(status) || io.out.rcode != NBT_RCODE_OK)) {
116                 state->fail_count++;
117         } else {
118                 state->pass_count++;
119                 state->registered[istate->idx] = false;
120         }
121         talloc_free(istate);    
122 }
123
124 /*
125   generate a name release
126 */
127 static void generate_release(struct nbt_name_socket *nbtsock, struct wins_state *state, int idx)
128 {
129         struct nbt_name_release io;
130         TALLOC_CTX *tmp_ctx = talloc_new(state);
131         struct nbt_name_request *req;
132         struct idx_state *istate;
133
134         istate = talloc(nbtsock, struct idx_state);
135         istate->idx = idx;
136         istate->state = state;
137
138         io.in.name            = generate_name(tmp_ctx, idx);
139         io.in.dest_addr       = state->wins_server;
140         io.in.address         = state->my_ip;
141         io.in.nb_flags        = NBT_NODE_H;
142         io.in.broadcast       = false;
143         io.in.timeout         = 2;
144         io.in.retries         = 1;
145
146         req = nbt_name_release_send(nbtsock, &io);
147
148         req->async.fn = release_handler;
149         req->async.private = istate;
150
151         talloc_free(tmp_ctx);
152 }
153
154
155 static void query_handler(struct nbt_name_request *req)
156 {
157         struct idx_state *istate = talloc_get_type(req->async.private, struct idx_state);
158         struct wins_state *state = istate->state;
159         struct nbt_name_query io;
160         NTSTATUS status;
161
162         status = nbt_name_query_recv(req, istate, &io);
163         if (!NT_STATUS_IS_OK(status) && state->registered[istate->idx]) {
164                 state->fail_count++;
165         } else {
166                 state->pass_count++;
167         }
168         talloc_free(istate);    
169 }
170
171 /*
172   generate a name query
173 */
174 static void generate_query(struct nbt_name_socket *nbtsock, struct wins_state *state, int idx)
175 {
176         struct nbt_name_query io;
177         TALLOC_CTX *tmp_ctx = talloc_new(state);
178         struct nbt_name_request *req;
179         struct idx_state *istate;
180
181         istate = talloc(nbtsock, struct idx_state);
182         istate->idx = idx;
183         istate->state = state;
184
185         io.in.name            = generate_name(tmp_ctx, idx);
186         io.in.dest_addr       = state->wins_server;
187         io.in.broadcast       = false;
188         io.in.wins_lookup     = true;
189         io.in.timeout         = 2;
190         io.in.retries         = 1;
191
192         req = nbt_name_query_send(nbtsock, &io);
193
194         req->async.fn = query_handler;
195         req->async.private = istate;
196
197         talloc_free(tmp_ctx);
198 }
199
200 /*
201   generate one WINS request
202 */
203 static void generate_request(struct nbt_name_socket *nbtsock, struct wins_state *state, int idx)
204 {
205         if (random() % 5 == 0) {
206                 generate_register(nbtsock, state, idx);
207                 return;
208         }
209
210         if (random() % 20 == 0) {
211                 generate_release(nbtsock, state, idx);
212                 return;
213         }
214
215         generate_query(nbtsock, state, idx);
216 }
217
218 /*
219   benchmark simple name queries
220 */
221 static bool bench_wins(struct torture_context *tctx)
222 {
223         struct nbt_name_socket *nbtsock = nbt_name_socket_init(tctx, NULL);
224         int num_sent=0;
225         struct timeval tv = timeval_current();
226         bool ret = true;
227         int timelimit = torture_setting_int(tctx, "timelimit", 5);
228         struct wins_state *state;
229         extern int torture_entries;
230         struct socket_address *my_ip;
231         struct nbt_name name;
232         const char *address;
233
234         if (!torture_nbt_get_name(tctx, &name, &address))
235                 return false;
236
237         state = talloc_zero(nbtsock, struct wins_state);
238
239         state->num_names = torture_entries;
240         state->registered = talloc_zero_array(state, bool, state->num_names);
241         state->wins_server = address;
242         state->my_ip = talloc_strdup(tctx, iface_best_ip(address));
243         state->ttl = timelimit;
244
245         my_ip = socket_address_from_strings(nbtsock, nbtsock->sock->backend_name, 
246                                             state->my_ip, 0);
247
248         socket_listen(nbtsock->sock, my_ip, 0, 0);
249
250         torture_comment(tctx, "Running for %d seconds\n", timelimit);
251         while (timeval_elapsed(&tv) < timelimit) {
252                 while (num_sent - (state->pass_count+state->fail_count) < 10) {
253                         generate_request(nbtsock, state, num_sent % state->num_names);
254                         num_sent++;
255                         if (num_sent % 50 == 0) {
256                                 if (torture_setting_bool(tctx, "progress", true)) {
257                                         torture_comment(tctx, "%.1f queries per second (%d failures)  \r", 
258                                                state->pass_count / timeval_elapsed(&tv),
259                                                state->fail_count);
260                                         fflush(stdout);
261                                 }
262                         }
263                 }
264
265                 event_loop_once(nbtsock->event_ctx);
266         }
267
268         while (num_sent != (state->pass_count + state->fail_count)) {
269                 event_loop_once(nbtsock->event_ctx);
270         }
271
272         torture_comment(tctx, "%.1f queries per second (%d failures)  \n", 
273                state->pass_count / timeval_elapsed(&tv),
274                state->fail_count);
275
276         talloc_free(nbtsock);
277         return ret;
278 }
279
280
281 /*
282   benchmark how fast a WINS server can respond to a mixture of
283   registration/refresh/release and name query requests
284 */
285 struct torture_suite *torture_bench_wins(void)
286 {
287         struct torture_suite *suite = torture_suite_create(
288                                                                                 talloc_autofree_context(),
289                                                                                 "BENCH-WINS");
290
291         torture_suite_add_simple_test(suite, "wins", bench_wins);
292
293         return suite;
294 }