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