Changeset 2e8aa42
- Timestamp:
- 12/09/05 13:28:36 (16 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:
- 95db152
- Parents:
- 0ef6536
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/format_wag.c
r0ef6536 r2e8aa42 77 77 78 78 static struct libtrace_format_t wag; 79 static struct libtrace_format_t wag_trace; 79 80 80 81 #define CONNINFO libtrace->format_data->conn_info … … 126 127 static int wag_init_input(struct libtrace_t *libtrace) { 127 128 struct stat buf; 128 struct sockaddr_un unix_sock;129 //struct sockaddr_un unix_sock; 129 130 libtrace->format_data = (struct libtrace_format_data_t *) 130 131 calloc(1,sizeof(struct libtrace_format_data_t)); 131 132 CONNINFO.path = libtrace->uridata; 133 134 if (stat(CONNINFO.path,&buf) == -1 ) { 135 perror("stat"); 136 return 0; 137 } 138 if (S_ISCHR(buf.st_mode)) { 139 libtrace->sourcetype = DEVICE; 140 141 INPUT.fd = open(CONNINFO.path, O_RDONLY); 142 143 } else { 144 fprintf(stderr, "%s is not a valid char device, exiting\n", 145 CONNINFO.path); 146 return 0; 147 148 } 149 return 1; 150 } 151 152 static int wtf_init_input(struct libtrace_t *libtrace) { 153 struct stat buf; 154 struct sockaddr_un unix_sock; 155 156 libtrace->format_data = (struct libtrace_format_data_t *) 157 calloc(1,sizeof(struct libtrace_format_data_t)); 158 CONNINFO.path = libtrace->uridata; 159 132 160 if (!strncmp(CONNINFO.path,"-",1)) { 133 161 // STDIN … … 136 164 137 165 } else { 166 167 168 // Do we need this socket stuff at all?? 169 // If we do, put it into wag_init_input as it uses 170 // INPUT.fd 171 172 /* 138 173 if (stat(CONNINFO.path,&buf) == -1 ) { 139 174 perror("stat"); … … 161 196 } 162 197 } else { 198 */ 163 199 // TRACE 164 200 libtrace->sourcetype = TRACE; … … 172 208 O_LARGEFILE), "r"); 173 209 174 }210 //} 175 211 } 176 212 return 1; 177 213 } 178 214 179 static int wag_init_output(struct libtrace_out_t *libtrace) { 215 216 static int wtf_init_output(struct libtrace_out_t *libtrace) { 180 217 char *filemode = 0; 181 218 libtrace->format_data = (struct libtrace_format_data_out_t *) … … 192 229 libtrace->uridata, 193 230 O_CREAT | O_LARGEFILE | O_WRONLY, 194 S_IRUSR | S_IWUSR ), filemode);231 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP), filemode); 195 232 } 196 233 … … 198 235 } 199 236 200 static int w ag_config_output(struct libtrace_out_t *libtrace, int argc, char *argv[]) {237 static int wtf_config_output(struct libtrace_out_t *libtrace, int argc, char *argv[]) { 201 238 #if HAVE_ZLIB 202 239 int opt; … … 227 264 228 265 static int wag_fin_input(struct libtrace_t *libtrace) { 266 close(INPUT.fd); 267 return 0; 268 } 269 270 static int wtf_fin_input(struct libtrace_t *libtrace) { 229 271 LIBTRACE_CLOSE(INPUT.file); 230 272 return 0; 231 273 } 232 274 233 static int w ag_fin_output(struct libtrace_out_t *libtrace) {275 static int wtf_fin_output(struct libtrace_out_t *libtrace) { 234 276 LIBTRACE_CLOSE(OUTPUT.file); 235 277 return 0; … … 238 280 static int wag_read(struct libtrace_t *libtrace, void *buffer, size_t len) { 239 281 int numbytes; 282 int framesize; 240 283 assert(libtrace); 241 284 … … 243 286 buffer = malloc(len); 244 287 245 while(1) { 246 switch(libtrace->sourcetype) { 247 case DEVICE: 248 if ((numbytes=read(INPUT.fd, 249 buffer, 250 len)) == -1) { 251 perror("read"); 252 return -1; 253 } 254 break; 255 default: 256 if ((numbytes=LIBTRACE_READ(INPUT.file, 257 buffer, 258 len)) == -1) { 259 perror("libtrace_read"); 260 return -1; 261 } 262 } 263 break; 264 } 265 return numbytes; 266 288 // read in wag_frame_hdr 289 if ((numbytes = read(INPUT.fd, 290 buffer, 291 sizeof(struct wag_frame_hdr))) != sizeof(struct wag_frame_hdr)) { 292 return -1; 293 } 294 295 framesize = ntohs(((struct wag_frame_hdr *)buffer)->size); 296 297 if (framesize > len) { 298 return -1; 299 } 300 301 // read in remainder of packet 302 if((numbytes = read(INPUT.fd, 303 buffer + sizeof(struct wag_frame_hdr), 304 framesize - sizeof(struct wag_frame_hdr))) != 305 (framesize - sizeof(struct wag_frame_hdr))) { 306 307 return -1; 308 309 } 310 311 return framesize; 267 312 } 268 313 … … 270 315 static int wag_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { 271 316 int numbytes; 317 318 char buf[RP_BUFSIZE]; 319 if (packet->buf_control == EXTERNAL) { 320 packet->buf_control = PACKET; 321 packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); 322 } 323 324 325 packet->trace = libtrace; 326 327 if ((numbytes = wag_read(libtrace, (void *)packet->buffer, RP_BUFSIZE)) <= 0) { 328 329 return numbytes; 330 } 331 332 333 // memcpy(packet->buffer, buf, numbytes); 334 335 packet->status.type = RT_DATA; 336 packet->status.message = 0; 337 packet->size = numbytes; 338 packet->header = packet->buffer; 339 packet->payload = packet->buffer + trace_get_framing_length(packet); 340 return numbytes; 341 } 342 343 static int wtf_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { 344 int numbytes; 345 void *buffer = packet->buffer; 346 void *buffer2 = packet->buffer; 347 int framesize; 272 348 int size; 273 char buf[RP_BUFSIZE]; 274 int read_required = 0; 275 276 void *buffer = 0; 277 278 if (packet->buf_control == EXTERNAL) { 279 packet->buf_control = PACKET; 280 packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); 281 } 282 packet->trace = libtrace; 283 buffer = packet->buffer; 284 285 286 do { 287 if (tracefifo_out_available(libtrace->fifo) == 0 || read_required) { 288 if ((numbytes = wag_read(libtrace,buf,RP_BUFSIZE)) <= 0) { 289 return numbytes; 290 } 291 assert(libtrace->fifo); 292 tracefifo_write(libtrace->fifo,buf,numbytes); 293 read_required = 0; 294 } 295 // read in wag_frame_hdr 296 if ((numbytes = tracefifo_out_read(libtrace->fifo, 297 buffer, 298 sizeof(struct wag_frame_hdr))) 299 == 0 ) { 300 tracefifo_out_reset(libtrace->fifo); 301 read_required = 1; 302 continue; 303 } 304 305 size = ntohs(((struct wag_frame_hdr *)buffer)->size); 306 307 // wag isn't in network byte order yet 308 //size = htons(size); 309 //printf("%d %d\n",size,htons(size)); 310 311 // read in full packet 312 if((numbytes = tracefifo_out_read(libtrace->fifo,buffer,size)) == 0) { 313 tracefifo_out_reset(libtrace->fifo); 314 read_required = 1; 315 continue; 316 } 317 318 // have the whole packet 319 tracefifo_out_update(libtrace->fifo,size); 320 tracefifo_ack_update(libtrace->fifo,size); 321 322 packet->status.type = RT_DATA; 323 packet->status.message = 0; 324 packet->header = packet->buffer; 325 packet->payload = packet->buffer + trace_get_framing_length(packet); 326 packet->size = numbytes; 327 return numbytes; 328 } while(1); 329 } 330 331 static int wag_write_packet(struct libtrace_out_t *libtrace, const struct libtrace_packet_t *packet) { 349 350 if (packet->buf_control == EXTERNAL) { 351 packet->buf_control = PACKET; 352 packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); 353 } 354 355 356 if ((numbytes = LIBTRACE_READ(INPUT.file, buffer, sizeof(struct wag_frame_hdr))) == -1) { 357 perror("libtrace_read"); 358 return -1; 359 } 360 361 if (numbytes == 0) { 362 return 0; 363 } 364 365 framesize = ntohs(((struct wag_frame_hdr *)buffer)->size); 366 buffer2 = buffer + sizeof(struct wag_frame_hdr); 367 size = framesize - sizeof(struct wag_frame_hdr); 368 assert(size < LIBTRACE_PACKET_BUFSIZE); 369 370 371 if ((numbytes=LIBTRACE_READ(INPUT.file, buffer2, size)) != size) { 372 perror("libtrace read"); 373 return -1; 374 } 375 376 packet->status.type = RT_DATA; 377 packet->status.message = 0; 378 packet->size = framesize; 379 packet->header = packet->buffer; 380 packet->payload = packet->buffer + trace_get_framing_length(packet); 381 return framesize; 382 383 } 384 385 static int wtf_write_packet(struct libtrace_out_t *libtrace, const struct libtrace_packet_t *packet) { 332 386 int numbytes =0 ; 333 if (packet->trace->format != &wag ) {334 fprintf(stderr,"Cannot convert from wag t o %s format yet\n",387 if (packet->trace->format != &wag_trace) { 388 fprintf(stderr,"Cannot convert from wag trace format to %s format yet\n", 335 389 packet->trace->format->name); 336 390 return -1; 337 391 } 338 if ((numbytes = LIBTRACE_WRITE(OUTPUT.file, packet->buffer, packet->size)) == 0) { 392 393 /* We could just read from packet->buffer, but I feel it is more technically correct 394 * to read from the header and payload pointers 395 */ 396 if ((numbytes = LIBTRACE_WRITE(OUTPUT.file, packet->header, trace_get_framing_length(packet))) == 0) { 339 397 perror("libtrace_write"); 340 398 return -1; 341 399 } 400 if ((numbytes = LIBTRACE_WRITE(OUTPUT.file, packet->payload, 401 packet->size - trace_get_framing_length(packet))) == 0) { 402 perror("libtrace_write"); 403 return -1; 404 } 342 405 return numbytes; 343 406 } 344 407 345 408 static void *wag_get_link(const struct libtrace_packet_t *packet) { 346 return (void *)packet->payload;347 409 /* 348 410 struct wag_data_frame *wagptr = (struct wag_data_frame *)packet->buffer; … … 350 412 return (void*)payload; 351 413 */ 414 return (void *)packet->payload; 352 415 } 353 416 … … 357 420 358 421 static int8_t wag_get_direction(const struct libtrace_packet_t *packet) { 359 struct wag_data_frame *wagptr = (struct wag_data_frame *)packet-> header;422 struct wag_data_frame *wagptr = (struct wag_data_frame *)packet->buffer; 360 423 if (wagptr->hdr.type == 0) { 361 424 return wagptr->hdr.subtype; … … 365 428 366 429 static uint64_t wag_get_erf_timestamp(const struct libtrace_packet_t *packet) { 367 struct wag_data_frame *wagptr = (struct wag_data_frame *)packet-> header;430 struct wag_data_frame *wagptr = (struct wag_data_frame *)packet->buffer; 368 431 uint64_t timestamp = 0; 369 timestamp = (((uint64_t)wagptr->ts.secs) << 32) + wagptr->ts.subsecs; 370 //timestamp |= (uint64_t)wagptr->ts.secs<<32; 371 //timestamp = ((timestamp%44000000)*(UINT_MAX/44000000)) 372 // | ((timestamp/44000000)<<32); 432 timestamp = ((uint64_t)(ntohl(wagptr->ts.secs)) << 32) | (uint64_t)(ntohl(wagptr->ts.subsecs)); 373 433 return timestamp; 374 434 } 375 435 376 436 static int wag_get_capture_length(const struct libtrace_packet_t *packet) { 377 struct wag_data_frame *wagptr = (struct wag_data_frame *)packet-> header;437 struct wag_data_frame *wagptr = (struct wag_data_frame *)packet->buffer; 378 438 //return (wagptr->hdr.size); 379 439 return ntohs(wagptr->hdr.size); … … 381 441 382 442 static int wag_get_wire_length(const struct libtrace_packet_t *packet) { 383 struct wag_data_frame *wagptr = (struct wag_data_frame *)packet-> header;443 struct wag_data_frame *wagptr = (struct wag_data_frame *)packet->buffer; 384 444 //return (wagptr->hdr.size); 385 445 return ntohs(wagptr->hdr.size); 386 446 } 387 447 388 static int wag_get_framing_length(const struct libtrace_packet_t *packet UNUSED) {448 static int wag_get_framing_length(const struct libtrace_packet_t *packet) { 389 449 return sizeof(struct wag_data_frame); 390 450 } … … 406 466 printf("Supported input URIs:\n"); 407 467 printf("\twag:/dev/wagn\n"); 408 printf("\twag:/path/to/trace.wag\n");409 printf("\twag:/path/to/trace.wag.gz\n");410 468 printf("\n"); 411 469 printf("\te.g.: wag:/dev/wag0\n"); 412 printf("\te.g.: wag:/tmp/trace.wag.gz\n");413 470 printf("\n"); 414 471 printf("Supported output URIs:\n"); 415 printf("\tnone\n"); 472 printf("\tNone\n"); 473 printf("\n"); 474 } 475 476 static void wtf_help() { 477 printf("wag trace format module: $Revision$\n"); 478 printf("Supported input URIs:\n"); 479 printf("\twtf:/path/to/trace.wag\n"); 480 printf("\twtf:/path/to/trace.wag.gz\n"); 481 printf("\n"); 482 printf("\te.g.: wtf:/tmp/trace.wag.gz\n"); 483 printf("\n"); 484 printf("Supported output URIs:\n"); 485 printf("\twtf:/path/to/trace.wag\n"); 486 printf("\twtf:/path/to/trace.wag.gz\n"); 487 printf("\n"); 488 printf("\te.g.: wtf:/tmp/trace.wag.gz\n"); 416 489 printf("\n"); 417 490 } … … 420 493 "wag", 421 494 "$Id$", 422 "w ag",495 "wtf", 423 496 wag_init_input, /* init_input */ 424 wag_init_output,/* init_output */425 wag_config_output,/* config_output */497 NULL, /* init_output */ 498 NULL, /* config_output */ 426 499 wag_fin_input, /* fin_input */ 427 wag_fin_output,/* fin_output */500 NULL, /* fin_output */ 428 501 wag_read_packet, /* read_packet */ 429 wag_write_packet,/* write_packet */502 NULL, /* write_packet */ 430 503 wag_get_link, /* get_link */ 431 504 wag_get_link_type, /* get_link_type */ … … 444 517 }; 445 518 519 /* wtf stands for Wag Trace Format */ 520 521 static struct libtrace_format_t wag_trace = { 522 "wtf", 523 "$Id$", 524 "wtf", 525 wtf_init_input, /* init_input */ 526 wtf_init_output, /* init_output */ 527 wtf_config_output, /* config_output */ 528 wtf_fin_input, /* fin_input */ 529 wtf_fin_output, /* fin_output */ 530 wtf_read_packet, /* read_packet */ 531 wtf_write_packet, /* write_packet */ 532 wag_get_link, /* get_link */ 533 wag_get_link_type, /* get_link_type */ 534 wag_get_direction, /* get_direction */ 535 NULL, /* set_direction */ 536 wag_get_erf_timestamp, /* get_erf_timestamp */ 537 NULL, /* get_timeval */ 538 NULL, /* get_seconds */ 539 wag_get_capture_length, /* get_capture_length */ 540 wag_get_wire_length, /* get_wire_length */ 541 wag_get_framing_length, /* get_framing_length */ 542 NULL, /* set_capture_length */ 543 wag_get_fd, /* get_fd */ 544 wag_event_trace, /* trace_event */ 545 wtf_help /* help */ 546 }; 547 548 446 549 void __attribute__((constructor)) wag_constructor() { 447 550 register_format(&wag); 448 } 551 register_format(&wag_trace); 552 }
Note: See TracChangeset
for help on using the changeset viewer.