2186fe2977b81f8bcbdb89725e506807a08302f4
[IRC.git] / Robust / src / Benchmarks / SingleTM / Intruder / Intruder.java
1 /* =============================================================================
2  *
3  * intruder.java
4  *
5  * =============================================================================
6  *
7  * Copyright (C) Stanford University, 2006.  All Rights Reserved.
8  * Author: Chi Cao Minh
9  *
10  * =============================================================================
11  *
12  * For the license of bayes/sort.h and bayes/sort.c, please see the header
13  * of the files.
14  * 
15  * ------------------------------------------------------------------------
16  * 
17  * For the license of kmeans, please see kmeans/LICENSE.kmeans
18  * 
19  * ------------------------------------------------------------------------
20  * 
21  * For the license of ssca2, please see ssca2/COPYRIGHT
22  * 
23  * ------------------------------------------------------------------------
24  * 
25  * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
26  * header of the files.
27  * 
28  * ------------------------------------------------------------------------
29  * 
30  * For the license of lib/rbtree.h and lib/rbtree.c, please see
31  * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
32  * 
33  * ------------------------------------------------------------------------
34  * 
35  * Unless otherwise noted, the following license applies to STAMP files:
36  * 
37  * Copyright (c) 2007, Stanford University
38  * All rights reserved.
39  * 
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions are
42  * met:
43  * 
44  *     * Redistributions of source code must retain the above copyright
45  *       notice, this list of conditions and the following disclaimer.
46  * 
47  *     * Redistributions in binary form must reproduce the above copyright
48  *       notice, this list of conditions and the following disclaimer in
49  *       the documentation and/or other materials provided with the
50  *       distribution.
51  * 
52  *     * Neither the name of Stanford University nor the names of its
53  *       contributors may be used to endorse or promote products derived
54  *       from this software without specific prior written permission.
55  * 
56  * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
57  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
66  * THE POSSIBILITY OF SUCH DAMAGE.
67  *
68  * =============================================================================
69  */
70 #define PARAM_ATTACK 'a'
71 #define PARAM_LENGTH 'l'
72 #define PARAM_NUM    'n'
73 #define PARAM_SEED   's'
74 #define PARAM_THREAD 't'
75
76 #define PARAM_DEFAULT_ATTACK 10
77 #define PARAM_DEFAULT_LENGTH 16
78 #define PARAM_DEFAULT_NUM (1 << 20)
79 #define PARAM_DEFAULT_SEED 1
80 #define PARAM_DEFAULT_THREAD 1
81
82 public class Intruder extends Thread {
83
84     int percentAttack;
85     int maxDataLength;
86     int numFlow;
87     int randomSeed;
88     int numThread;
89
90     int threadID;
91     Arg argument;
92
93
94     public Intruder(String[] argv) 
95     {
96         parseArg(argv);
97     }
98
99     public Intruder(int myID,Arg a)
100     {
101         argument = a;
102         threadID = myID;
103     }
104
105   private void setDefaultParams() {
106     percentAttack = PARAM_DEFAULT_ATTACK;
107     maxDataLength = PARAM_DEFAULT_LENGTH;
108     numFlow       = PARAM_DEFAULT_NUM;
109     randomSeed    = PARAM_DEFAULT_SEED;
110     numThread     = PARAM_DEFAULT_THREAD;
111   }
112
113
114 /* =============================================================================
115  * displayUsage
116  * =============================================================================
117  */
118     private void displayUsage()
119     {   
120         System.out.print  ("Usage: Intruder [options]\n");
121         System.out.println("\nOptions:                            (defaults)\n");
122         System.out.print  ("    a <UINT>   Percent [a]ttack     ");
123         System.out.print  ("    l <UINT>   Max data [l]ength    ");
124         System.out.print  ("    n <UINT>   [n]umber of flows    ");
125         System.out.print  ("    s <UINT>   Random [s]eed        ");
126         System.out.print  ("    t <UINT>   Number of [t]hreads  ");
127         System.exit(1);
128     }
129
130
131 /* =============================================================================
132  * parseArgs
133  * =============================================================================
134  */
135     private void parseArg(String[] argv) 
136     {
137         int i=0;
138         String arg;
139         boolean opterr = false;
140
141         setDefaultParams();
142
143         while ( i< argv.length) {
144
145             if(argv[i].charAt(0) == '-') {
146                 arg = argv[i++];
147                 //check options
148                 if(arg.equals("-a")) {
149                     percentAttack = Integer.parseInt(argv[i++]);
150                 }
151                 else if(arg.equals("-l")) {
152                     maxDataLength = Integer.parseInt(argv[i++]);
153                 } 
154                 else if(arg.equals("-n")) {
155                     numFlow = Integer.parseInt(argv[i++]);
156                 }
157                 else if(arg.equals("-s")) {
158                     randomSeed = Integer.parseInt(argv[i++]);
159                 }
160                 else if(arg.equals("-t")) {
161                     numThread = Integer.parseInt(argv[i++]);
162                 }
163                 else {
164                     System.out.println("Non-option argument: " + argv[i]);
165                     opterr = true;
166                 }
167             }
168         }
169         if(opterr) {
170             displayUsage();
171         }
172     }
173
174     
175
176
177 /* =============================================================================
178  * processPackets
179  * =============================================================================
180  */
181     public void processPackets(Arg argPtr)
182     {
183         // TM_THREAD_ENTER();
184         
185         Stream streamPtr = argPtr.streamPtr;
186         Decoder decoderPtr = argPtr.decoderPtr;
187         Vector_t[] errorVectors = argPtr.errorVectors;
188       
189
190         Detector detectorPtr = new Detector();
191         detectorPtr.addPreprocessor(2);
192
193         Vector_t errorVectorPtr = errorVectors[threadID];
194
195         while(true) {
196             Packet packetPtr;
197                 
198             atomic {
199                 packetPtr = streamPtr.getPacket();
200             }
201
202             if(packetPtr == null) {
203                 break;
204             }
205             int flowId = packetPtr.flowId;
206             int error;
207             atomic {
208                 error = decoderPtr.process(packetPtr,(packetPtr.length));
209             }
210
211             if (error != 0) {
212                 /*
213                  * Currently, stream_generate() does not create these errors.
214                  */
215               System.out.println("Here?"+error);
216             }
217             byte[] data;
218             int[] decodedFlowId = new int[1];
219             
220             atomic {
221                 data = decoderPtr.getComplete(decodedFlowId);
222             }
223
224             if(data != null) {
225               int err = detectorPtr.process(data);
226               
227               if(err != 0) {
228                 boolean status = errorVectorPtr.vector_pushBack(new Integer(decodedFlowId[0]));
229                 if(!status) {
230                   System.out.println("Assertion in Intruder.processPacket");
231                 }
232               }
233             }
234         }
235
236         // TM_THREAD_EXIT();
237     
238     }
239
240     public void run()
241     {
242         Barrier.enterBarrier();
243         processPackets(argument);
244         Barrier.enterBarrier();
245     }
246         
247 /* =============================================================================
248  * main
249  * =============================================================================
250  */
251
252     public static void main(String[] argv)
253     {
254         
255         /*
256          * Initialization
257          */
258
259         ERROR er = new ERROR();
260
261         
262         Intruder in = new Intruder(argv);   // parsing argv
263
264         Barrier.setBarrier(in.numThread);
265
266         System.out.println("Percent attack  =   " + in.percentAttack);
267         System.out.println("Max data length =   " + in.maxDataLength);
268         System.out.println("Num flow        =   " + in.numFlow);
269         System.out.println("Random seed     =   " + in.randomSeed);
270
271         Dictionary dictionaryPtr = new Dictionary();
272         Stream streamPtr = new Stream(in.percentAttack);
273         int numAttack = streamPtr.generate(dictionaryPtr,in.numFlow,in.randomSeed,in.maxDataLength);
274
275         System.out.println("Num Attack      =   " + numAttack);
276
277         Decoder decoderPtr = new Decoder();
278         Vector_t[] errorVectors = new Vector_t[in.numThread];
279
280         int i;
281
282         for(i =0;i< in.numThread;i++) {
283           errorVectors[i] = new Vector_t(in.numFlow);
284         }
285
286         Arg arg = new Arg();
287
288         arg.streamPtr = streamPtr;
289         arg.decoderPtr = decoderPtr;
290         arg.errorVectors = errorVectors;
291
292         in.argument = arg;
293
294         // Run transactions
295
296         Intruder[] intruders = new Intruder[in.numThread];
297
298         for(i=1; i<in.numThread;i++) {
299             intruders[i] = new Intruder(i,arg);
300         }
301         in.threadID = 0;
302
303
304         for(i = 1; i< in.numThread;i++) {
305             intruders[i].start();
306         }
307
308         long start = System.currentTimeMillis();
309
310         Barrier.enterBarrier();
311         in.processPackets(in.argument);
312         Barrier.enterBarrier();
313
314         long finish = System.currentTimeMillis();
315         long elapsed = finish - start;
316
317         System.out.println("TIME=" + elapsed);
318
319         // finish
320         //
321         // Check solution
322
323         int numFound = 0;
324
325         for(i =0;i<in.numThread;i++) {
326             Vector_t errorVectorPtr = errorVectors[i];
327             int e;
328             int numError = errorVectorPtr.vector_getSize();
329             //System.out.println("numError = " + numError);
330             numFound += numError;
331             for (e = 0; e< numError; e++) {
332                 int flowId = ((Integer)errorVectorPtr.vector_at(e)).intValue();
333                 boolean status = streamPtr.isAttack(flowId);
334                 
335                 if(status == false) {
336                     System.out.println("Assertion in check solution");
337                     System.exit(1);
338                 }
339             }
340         }
341
342         System.out.println("Num found       = " + numFound);
343
344         if(numFound != numAttack) {
345             System.out.println("Assertion in check solution");
346             System.exit(1);
347         }
348         
349         System.out.println("Finished");
350         System.exit(0);
351     }
352
353 }
354
355 /* =============================================================================
356  *
357  * End of intruder.java
358  *
359  * =============================================================================
360  */