blob: 7db67a2b2af929ac85bcc21b508ea93711fbd4c1 [file] [log] [blame]
Alexandre Lision8af73cb2013-12-10 14:11:20 -05001/*
2 * rdbx_driver.c
3 *
4 * driver for the rdbx implementation (replay database with extended range)
5 *
6 * David A. McGrew
7 * Cisco Systems, Inc.
8 */
9
10/*
11 *
12 * Copyright (c) 2001-2006, Cisco Systems, Inc.
13 * All rights reserved.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 *
19 * Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 *
22 * Redistributions in binary form must reproduce the above
23 * copyright notice, this list of conditions and the following
24 * disclaimer in the documentation and/or other materials provided
25 * with the distribution.
26 *
27 * Neither the name of the Cisco Systems, Inc. nor the names of its
28 * contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
34 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
35 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
36 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
38 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
42 * OF THE POSSIBILITY OF SUCH DAMAGE.
43 *
44 */
45
46#include <stdio.h> /* for printf() */
47#include "getopt_s.h" /* for local getopt() */
48
49#include "rdbx.h"
50
51#ifdef ROC_TEST
52#error "rdbx_t won't work with ROC_TEST - bitmask same size as seq_median"
53#endif
54
55#include "ut_sim.h"
56
57err_status_t
58test_replay_dbx(int num_trials);
59
60double
61rdbx_check_adds_per_second(int num_trials);
62
63void
64usage(char *prog_name) {
65 printf("usage: %s [ -t | -v ]\n", prog_name);
66 exit(255);
67}
68
69int
70main (int argc, char *argv[]) {
71 double rate;
72 err_status_t status;
73 char q;
74 unsigned do_timing_test = 0;
75 unsigned do_validation = 0;
76
77 /* process input arguments */
78 while (1) {
79 q = getopt_s(argc, argv, "tv");
80 if (q == -1)
81 break;
82 switch (q) {
83 case 't':
84 do_timing_test = 1;
85 break;
86 case 'v':
87 do_validation = 1;
88 break;
89 default:
90 usage(argv[0]);
91 }
92 }
93
94 printf("rdbx (replay database w/ extended range) test driver\n"
95 "David A. McGrew\n"
96 "Cisco Systems, Inc.\n");
97
98 if (!do_validation && !do_timing_test)
99 usage(argv[0]);
100
101 if (do_validation) {
102 printf("testing rdbx_t...\n");
103
104 status = test_replay_dbx(1 << 12);
105 if (status) {
106 printf("failed\n");
107 exit(1);
108 }
109 printf("passed\n");
110 }
111
112 if (do_timing_test) {
113 rate = rdbx_check_adds_per_second(1 << 18);
114 printf("rdbx_check/replay_adds per second: %e\n", rate);
115 }
116
117 return 0;
118}
119
120void
121print_rdbx(rdbx_t *rdbx) {
122 printf("rdbx: {%llu, %s}\n",
123 (unsigned long long)(rdbx->index), v128_bit_string(&rdbx->bitmask));
124}
125
126
127/*
128 * rdbx_check_add(rdbx, idx) checks a known-to-be-good idx against
129 * rdbx, then adds it. if a failure is detected (i.e., the check
130 * indicates that the value is already in rdbx) then
131 * err_status_algo_fail is returned.
132 *
133 */
134
135err_status_t
136rdbx_check_add(rdbx_t *rdbx, uint32_t idx) {
137 int delta;
138 xtd_seq_num_t est;
139
140 delta = index_guess(&rdbx->index, &est, idx);
141
142 if (rdbx_check(rdbx, delta) != err_status_ok) {
143 printf("replay_check failed at index %u\n", idx);
144 return err_status_algo_fail;
145 }
146
147 /*
148 * in practice, we'd authenticate the packet containing idx, using
149 * the estimated value est, at this point
150 */
151
152 if (rdbx_add_index(rdbx, delta) != err_status_ok) {
153 printf("rdbx_add_index failed at index %u\n", idx);
154 return err_status_algo_fail;
155 }
156
157 return err_status_ok;
158}
159
160/*
161 * rdbx_check_expect_failure(rdbx_t *rdbx, uint32_t idx)
162 *
163 * checks that a sequence number idx is in the replay database
164 * and thus will be rejected
165 */
166
167err_status_t
168rdbx_check_expect_failure(rdbx_t *rdbx, uint32_t idx) {
169 int delta;
170 xtd_seq_num_t est;
171 err_status_t status;
172
173 delta = index_guess(&rdbx->index, &est, idx);
174
175 status = rdbx_check(rdbx, delta);
176 if (status == err_status_ok) {
177 printf("delta: %d ", delta);
178 printf("replay_check failed at index %u (false positive)\n", idx);
179 return err_status_algo_fail;
180 }
181
182 return err_status_ok;
183}
184
185err_status_t
186rdbx_check_unordered(rdbx_t *rdbx, uint32_t idx) {
187 err_status_t rstat;
188
189 rstat = rdbx_check(rdbx, idx);
190 if ((rstat != err_status_ok) && (rstat != err_status_replay_old)) {
191 printf("replay_check_unordered failed at index %u\n", idx);
192 return err_status_algo_fail;
193 }
194 return err_status_ok;
195}
196
197#define MAX_IDX 160
198
199err_status_t
200test_replay_dbx(int num_trials) {
201 rdbx_t rdbx;
202 uint32_t idx, ircvd;
203 ut_connection utc;
204 err_status_t status;
205 int num_fp_trials;
206
207 status = rdbx_init(&rdbx);
208 if (status) {
209 printf("replay_init failed with error code %d\n", status);
210 exit(1);
211 }
212
213 /*
214 * test sequential insertion
215 */
216 printf("\ttesting sequential insertion...");
217 for (idx=0; idx < num_trials; idx++) {
218 status = rdbx_check_add(&rdbx, idx);
219 if (status)
220 return status;
221 }
222 printf("passed\n");
223
224 /*
225 * test for false positives by checking all of the index
226 * values which we've just added
227 *
228 * note that we limit the number of trials here, since allowing the
229 * rollover counter to roll over would defeat this test
230 */
231 num_fp_trials = num_trials % 0x10000;
232 if (num_fp_trials == 0) {
233 printf("warning: no false positive tests performed\n");
234 }
235 printf("\ttesting for false positives...");
236 for (idx=0; idx < num_fp_trials; idx++) {
237 status = rdbx_check_expect_failure(&rdbx, idx);
238 if (status)
239 return status;
240 }
241 printf("passed\n");
242
243 /* re-initialize */
244 if (rdbx_init(&rdbx) != err_status_ok) {
245 printf("replay_init failed\n");
246 return err_status_init_fail;
247 }
248
249 /*
250 * test non-sequential insertion
251 *
252 * this test covers only fase negatives, since the values returned
253 * by ut_next_index(...) are distinct
254 */
255 ut_init(&utc);
256
257 printf("\ttesting non-sequential insertion...");
258 for (idx=0; idx < num_trials; idx++) {
259 ircvd = ut_next_index(&utc);
260 status = rdbx_check_unordered(&rdbx, ircvd);
261 if (status)
262 return status;
263 }
264 printf("passed\n");
265
266 return err_status_ok;
267}
268
269
270
271#include <time.h> /* for clock() */
272#include <stdlib.h> /* for random() */
273
274double
275rdbx_check_adds_per_second(int num_trials) {
276 uint32_t i;
277 int delta;
278 rdbx_t rdbx;
279 xtd_seq_num_t est;
280 clock_t timer;
281 int failures; /* count number of failures */
282
283 if (rdbx_init(&rdbx) != err_status_ok) {
284 printf("replay_init failed\n");
285 exit(1);
286 }
287
288 failures = 0;
289 timer = clock();
290 for(i=0; i < num_trials; i++) {
291
292 delta = index_guess(&rdbx.index, &est, i);
293
294 if (rdbx_check(&rdbx, delta) != err_status_ok)
295 ++failures;
296 else
297 if (rdbx_add_index(&rdbx, delta) != err_status_ok)
298 ++failures;
299 }
300 timer = clock() - timer;
301
302 printf("number of failures: %d \n", failures);
303
304 return (double) CLOCKS_PER_SEC * num_trials / timer;
305}
306