- Timestamp:
- 12/09/10 10:55:02 (10 years ago)
- Branches:
- 4.0.1-hotfixes, cachetimestamps, develop, dpdk-ndag, etsilive, getfragoff, help, libtrace4, master, ndag_format, pfring, rc-4.0.1, rc-4.0.2, rc-4.0.3, rc-4.0.4, ringdecrementfix, ringperformance, ringtimestampfixes
- Children:
- 88346e2
- Parents:
- 0c10e11
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/tracesplit/tracesplit.c
r0896e76 r9672090 11 11 #include <time.h> 12 12 13 /* Global variables */ 14 struct libtrace_out_t *output = NULL; 15 uint64_t count=UINT64_MAX; 16 uint64_t bytes=UINT64_MAX; 17 uint64_t starttime=0; 18 uint64_t endtime=UINT64_MAX; 19 uint64_t interval=UINT64_MAX; 20 double firsttime=0; 21 uint64_t pktcount=0; 22 uint64_t totbytes=0; 23 uint64_t totbyteslast=0; 24 uint64_t maxfiles = UINT64_MAX; 25 uint64_t filescreated = 0; 26 uint16_t snaplen = 0; 27 int verbose=0; 28 int compress_level=-1; 29 trace_option_compresstype_t compress_type = TRACE_OPTION_COMPRESSTYPE_NONE; 30 char *output_base = NULL; 31 32 13 33 static char *strdupcat(char *str,char *app) 14 34 { … … 28 48 { 29 49 printf("Usage:\n" 30 "%s flags inputuri outputuri\n"50 "%s flags inputuri [inputuri ... ] outputuri\n" 31 51 "-f --filter=bpf only output packets that match filter\n" 32 52 "-c --count=n split every n packets\n" … … 53 73 } 54 74 75 76 /* Return values: 77 * 1 = continue reading packets 78 * 0 = stop reading packets, cos we're done 79 * -1 = stop reading packets, we've got an error 80 */ 81 static int per_packet(libtrace_packet_t *packet) { 82 83 if (trace_get_link_type(packet) == ~0U) { 84 fprintf(stderr, "Halted due to being unable to determine linktype - input trace may be corrupt.\n"); 85 return -1; 86 } 87 88 if (snaplen>0) { 89 trace_set_capture_length(packet,snaplen); 90 } 91 92 if (trace_get_seconds(packet)<starttime) { 93 return 1; 94 } 95 96 if (trace_get_seconds(packet)>endtime) { 97 return 0; 98 } 99 100 if (firsttime==0) { 101 time_t now = trace_get_seconds(packet); 102 if (starttime != 0) { 103 firsttime=now-((now - starttime)%interval); 104 } 105 else { 106 firsttime=now; 107 } 108 } 109 110 if (output && trace_get_seconds(packet)>firsttime+interval) { 111 trace_destroy_output(output); 112 output=NULL; 113 firsttime+=interval; 114 } 115 116 if (output && pktcount%count==0) { 117 trace_destroy_output(output); 118 output=NULL; 119 } 120 121 pktcount++; 122 totbytes+=trace_get_capture_length(packet); 123 if (output && totbytes-totbyteslast>=bytes) { 124 trace_destroy_output(output); 125 output=NULL; 126 totbyteslast=totbytes; 127 } 128 if (!output) { 129 char *buffer; 130 bool need_ext=false; 131 if (maxfiles <= filescreated) { 132 return 0; 133 } 134 buffer=strdup(output_base); 135 if (interval!=UINT64_MAX && maxfiles>1) { 136 buffer=strdupcat(buffer,"-"); 137 buffer=strdupcati(buffer,(uint64_t)firsttime); 138 need_ext=true; 139 } 140 if (count!=UINT64_MAX && maxfiles>1) { 141 buffer=strdupcat(buffer,"-"); 142 buffer=strdupcati(buffer,(uint64_t)pktcount); 143 need_ext=true; 144 } 145 if (bytes!=UINT64_MAX && maxfiles>1) { 146 static int filenum=0; 147 buffer=strdupcat(buffer,"-"); 148 buffer=strdupcati(buffer,(uint64_t)++filenum); 149 need_ext=true; 150 } 151 if (need_ext) { 152 if (compress_level!=0) 153 buffer=strdupcat(buffer,".gz"); 154 } 155 if (verbose>1) { 156 fprintf(stderr,"%s:",buffer); 157 if (count!=UINT64_MAX) 158 fprintf(stderr," count=%" PRIu64,pktcount); 159 if (bytes!=UINT64_MAX) 160 fprintf(stderr," bytes=%" PRIu64,bytes); 161 if (interval!=UINT64_MAX) { 162 time_t filetime = firsttime; 163 fprintf(stderr," time=%s",ctime(&filetime)); 164 } 165 else { 166 fprintf(stderr,"\n"); 167 } 168 } 169 output=trace_create_output(buffer); 170 if (trace_is_err_output(output)) { 171 trace_perror_output(output,"%s",buffer); 172 free(buffer); 173 return -1; 174 } 175 if (compress_level!=-1) { 176 if (trace_config_output(output, 177 TRACE_OPTION_OUTPUT_COMPRESS, 178 &compress_level)==-1) { 179 trace_perror_output(output,"Unable to set compression level"); 180 } 181 } 182 183 if (trace_config_output(output, 184 TRACE_OPTION_OUTPUT_COMPRESSTYPE, 185 &compress_type) == -1) { 186 trace_perror_output(output, "Unable to set compression type"); 187 } 188 189 trace_start_output(output); 190 if (trace_is_err_output(output)) { 191 trace_perror_output(output,"%s",buffer); 192 free(buffer); 193 return -1; 194 } 195 free(buffer); 196 filescreated ++; 197 } 198 199 /* Some traces we have are padded (usually with 0x00), so 200 * lets sort that out now and truncate them properly 201 */ 202 203 if (trace_get_capture_length(packet) 204 > trace_get_wire_length(packet)) { 205 trace_set_capture_length(packet,trace_get_wire_length(packet)); 206 } 207 208 if (trace_write_packet(output,packet)==-1) { 209 trace_perror_output(output,"write_packet"); 210 return -1; 211 } 212 213 return 1; 214 215 } 216 55 217 int main(int argc, char *argv[]) 56 218 { 57 /* All these variables are getting silly */219 char *compress_type_str=NULL; 58 220 struct libtrace_filter_t *filter=NULL; 59 struct libtrace_out_t *output = NULL;60 221 struct libtrace_t *input; 61 222 struct libtrace_packet_t *packet = trace_create_packet(); 62 223 struct sigaction sigact; 63 uint64_t count=UINT64_MAX; 64 uint64_t bytes=UINT64_MAX; 65 uint64_t starttime=0; 66 uint64_t endtime=UINT64_MAX; 67 uint64_t interval=UINT64_MAX; 68 double firsttime=0; 69 uint64_t pktcount=0; 70 uint64_t totbytes=0; 71 uint64_t totbyteslast=0; 72 uint64_t maxfiles = UINT64_MAX; 73 uint64_t filescreated = 0; 74 uint16_t snaplen = 0; 75 int verbose=0; 76 int compress_level=-1; 77 char *compress_type_str=NULL; 78 trace_option_compresstype_t compress_type = TRACE_OPTION_COMPRESSTYPE_NONE; 79 224 int i; 225 80 226 if (argc<2) { 81 227 usage(argv[0]); … … 182 328 } 183 329 330 output_base = argv[argc - 1]; 331 184 332 sigact.sa_handler = cleanup_signal; 185 333 sigemptyset(&sigact.sa_mask); … … 190 338 191 339 output=NULL; 192 input=trace_create(argv[optind]);193 if (trace_is_err(input)) {194 trace_perror(input,"%s",argv[optind]);195 return 1;196 }197 198 if (trace_start(input)==-1) {199 trace_perror(input,"%s",argv[optind]);200 return 1;201 }202 340 203 341 signal(SIGINT,&cleanup_signal); 204 342 signal(SIGTERM,&cleanup_signal); 205 343 206 while(!done) { 344 for (i = optind; i < argc - 1; i++) { 345 346 347 input = trace_create(argv[i]); 207 348 208 if (trace_read_packet(input,packet)<1) { 349 if (trace_is_err(input)) { 350 trace_perror(input,"%s",argv[i]); 351 return 1; 352 } 353 354 if (filter && trace_config(input, TRACE_OPTION_FILTER, filter) == 1) { 355 trace_perror(input, "Configuring filter for %s", 356 argv[i]); 357 return 1; 358 } 359 360 if (trace_start(input)==-1) { 361 trace_perror(input,"%s",argv[i]); 362 return 1; 363 } 364 365 while (trace_read_packet(input,packet)>0) { 366 if (per_packet(packet) < 1) 367 done = 1; 368 if (done) 369 break; 370 } 371 372 if (done) 209 373 break; 210 }211 212 if (trace_get_link_type(packet) == ~0U) {213 fprintf(stderr, "Halted due to being unable to determine linktype - input trace may be corrupt.\n");374 375 if (trace_is_err(input)) { 376 trace_perror(input,"Reading packets"); 377 trace_destroy(input); 214 378 break; 215 379 } 216 380 217 if (snaplen>0) { 218 trace_set_capture_length(packet,snaplen); 219 } 220 221 if (filter && !trace_apply_filter(filter,packet)) { 222 continue; 223 } 224 225 if (trace_get_seconds(packet)<starttime) { 226 continue; 227 } 228 229 if (trace_get_seconds(packet)>endtime) { 230 break; 231 } 232 233 if (firsttime==0) { 234 time_t now = trace_get_seconds(packet); 235 if (starttime != 0) { 236 firsttime=now-((now - starttime)%interval); 237 } 238 else { 239 firsttime=now; 240 } 241 } 242 243 if (output && trace_get_seconds(packet)>firsttime+interval) { 244 trace_destroy_output(output); 245 output=NULL; 246 firsttime+=interval; 247 } 248 249 if (output && pktcount%count==0) { 250 trace_destroy_output(output); 251 output=NULL; 252 } 253 254 pktcount++; 255 totbytes+=trace_get_capture_length(packet); 256 if (output && totbytes-totbyteslast>=bytes) { 257 trace_destroy_output(output); 258 output=NULL; 259 totbyteslast=totbytes; 260 } 261 if (!output) { 262 char *buffer; 263 bool need_ext=false; 264 if (maxfiles <= filescreated) { 265 break; 266 } 267 buffer=strdup(argv[optind+1]); 268 if (interval!=UINT64_MAX && maxfiles>1) { 269 buffer=strdupcat(buffer,"-"); 270 buffer=strdupcati(buffer,(uint64_t)firsttime); 271 need_ext=true; 272 } 273 if (count!=UINT64_MAX && maxfiles>1) { 274 buffer=strdupcat(buffer,"-"); 275 buffer=strdupcati(buffer,(uint64_t)pktcount); 276 need_ext=true; 277 } 278 if (bytes!=UINT64_MAX && maxfiles>1) { 279 static int filenum=0; 280 buffer=strdupcat(buffer,"-"); 281 buffer=strdupcati(buffer,(uint64_t)++filenum); 282 need_ext=true; 283 } 284 if (need_ext) { 285 if (compress_level!=0) 286 buffer=strdupcat(buffer,".gz"); 287 } 288 if (verbose>1) { 289 fprintf(stderr,"%s:",buffer); 290 if (count!=UINT64_MAX) 291 fprintf(stderr," count=%" PRIu64,pktcount); 292 if (bytes!=UINT64_MAX) 293 fprintf(stderr," bytes=%" PRIu64,bytes); 294 if (interval!=UINT64_MAX) { 295 time_t filetime = firsttime; 296 fprintf(stderr," time=%s",ctime(&filetime)); 297 } 298 else { 299 fprintf(stderr,"\n"); 300 } 301 } 302 output=trace_create_output(buffer); 303 if (trace_is_err_output(output)) { 304 trace_perror_output(output,"%s",buffer); 305 free(buffer); 306 break; 307 } 308 if (compress_level!=-1) { 309 if (trace_config_output(output, 310 TRACE_OPTION_OUTPUT_COMPRESS, 311 &compress_level)==-1) { 312 trace_perror_output(output,"Unable to set compression level"); 313 } 314 } 315 316 if (trace_config_output(output, 317 TRACE_OPTION_OUTPUT_COMPRESSTYPE, 318 &compress_type) == -1) { 319 trace_perror_output(output, "Unable to set compression type"); 320 } 321 322 trace_start_output(output); 323 if (trace_is_err_output(output)) { 324 trace_perror_output(output,"%s",buffer); 325 free(buffer); 326 break; 327 } 328 free(buffer); 329 filescreated ++; 330 } 331 332 /* Some traces we have are padded (usually with 0x00), so 333 * lets sort that out now and truncate them properly 334 */ 335 336 if (trace_get_capture_length(packet) 337 > trace_get_wire_length(packet)) { 338 trace_set_capture_length(packet,trace_get_wire_length(packet)); 339 } 340 341 if (trace_write_packet(output,packet)==-1) { 342 trace_perror_output(output,"write_packet"); 343 break; 344 } 345 346 } 347 348 if (trace_is_err(input)) { 349 trace_perror(input, "Reading packets"); 381 trace_destroy(input); 350 382 } 351 383 … … 366 398 } 367 399 368 trace_destroy(input);369 400 if (output) 370 401 trace_destroy_output(output);
Note: See TracChangeset
for help on using the changeset viewer.