- Timestamp:
- 02/04/10 14:31:38 (12 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:
- 8488c15
- Parents:
- 5952ff0
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/format_dag25.c
rcce868c r43c00e5 2 2 * This file is part of libtrace 3 3 * 4 * Copyright (c) 2007,2008 The University of Waikato, Hamilton, New Zealand. 5 * Authors: Daniel Lawson 4 * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton, 5 * New Zealand. 6 * 7 * Authors: Daniel Lawson 6 8 * Perry Lorier 7 * Shane Alcock8 * 9 * Shane Alcock 10 * 9 11 * All rights reserved. 10 12 * 11 * This code has been developed by the University of Waikato WAND 13 * This code has been developed by the University of Waikato WAND 12 14 * research group. For further information please see http://www.wand.net.nz/ 13 15 * … … 29 31 * 30 32 */ 33 31 34 #define _GNU_SOURCE 32 35 … … 64 67 #endif 65 68 69 /* This format deals with DAG cards that are using drivers from the 2.5 version 70 * onwards, including 3.X. 71 * 72 * DAG is a LIVE capture format. 73 * 74 * This format does support writing, provided the DAG card that you are using 75 * has transmit (Tx) support. Additionally, packets read using this format 76 * are in the ERF format, so can easily be written as ERF traces without 77 * losing any data. 78 */ 79 66 80 67 81 #define DATA(x) ((struct dag_format_data_t *)x->format_data) … … 74 88 static struct libtrace_format_t dag; 75 89 90 /* A DAG device - a DAG device can support multiple streams (and therefore 91 * multiple input traces) so each trace needs to refer to a device */ 76 92 struct dag_dev_t { 77 //pthread_mutex_t dag_mutex; 78 char * dev_name; 79 int fd; 80 uint16_t ref_count; 81 struct dag_dev_t *prev; 82 struct dag_dev_t *next; 93 char * dev_name; /* Device name */ 94 int fd; /* File descriptor */ 95 uint16_t ref_count; /* Number of input / output traces 96 that are using this device */ 97 struct dag_dev_t *prev; /* Pointer to the previous device in 98 the device list */ 99 struct dag_dev_t *next; /* Pointer to the next device in the 100 device list */ 83 101 }; 84 102 103 /* "Global" data that is stored for each DAG output trace */ 85 104 struct dag_format_data_out_t { 105 /* The DAG device being used for writing */ 86 106 struct dag_dev_t *device; 107 /* The DAG stream that is being written on */ 87 108 unsigned int dagstream; 109 /* Boolean flag indicating whether the stream is currently attached */ 88 110 int stream_attached; 89 uint8_t *bottom; 90 uint8_t *top; 91 uint32_t processed; 92 uint64_t drops; 111 /* The amount of data waiting to be transmitted, in bytes */ 93 112 uint64_t waiting; 113 /* A buffer to hold the data to be transmittted */ 94 114 uint8_t *txbuffer; 95 115 }; 96 116 117 /* "Global" data that is stored for each DAG input trace */ 97 118 struct dag_format_data_t { 119 120 /* Data required for regular DUCK reporting */ 98 121 struct { 122 /* Timestamp of the last DUCK report */ 99 123 uint32_t last_duck; 124 /* The number of seconds between each DUCK report */ 100 125 uint32_t duck_freq; 126 /* Timestamp of the last packet read from the DAG card */ 101 127 uint32_t last_pkt; 128 /* Dummy trace to ensure DUCK packets are dealt with using the 129 * DUCK format functions */ 102 130 libtrace_t *dummy_duck; 103 131 } duck; 104 132 133 /* The DAG device that we are reading from */ 105 134 struct dag_dev_t *device; 135 /* The DAG stream that we are reading from */ 106 136 unsigned int dagstream; 137 /* Boolean flag indicating whether the stream is currently attached */ 107 138 int stream_attached; 139 /* Pointer to the first unread byte in the DAG memory hole */ 108 140 uint8_t *bottom; 141 /* Pointer to the last unread byte in the DAG memory hole */ 109 142 uint8_t *top; 143 /* The amount of data processed thus far from the bottom pointer */ 110 144 uint32_t processed; 145 /* The number of packets that have been dropped */ 111 146 uint64_t drops; 112 147 }; 113 148 149 /* To be thread-safe, we're going to need a mutex for operating on the list 150 * of DAG devices */ 114 151 pthread_mutex_t open_dag_mutex; 152 153 /* The list of DAG devices that have been opened by libtrace. 154 * 155 * We can only open each DAG device once, but we might want to read from 156 * multiple streams. Therefore, we need to maintain a list of devices that we 157 * have opened (with ref counts!) so that we don't try to open a device too 158 * many times or close a device that we're still using */ 115 159 struct dag_dev_t *open_dags = NULL; 116 160 117 /* Dag erf ether packets have a 2 byte padding before the packet 118 * so that the ip header is aligned on a 32 bit boundary. 119 */ 161 /* Returns the amount of padding between the ERF header and the start of the 162 * captured packet data */ 120 163 static int dag_get_padding(const libtrace_packet_t *packet) 121 164 { 165 /* ERF Ethernet records have a 2 byte padding before the packet itself 166 * so that the IP header is aligned on a 32 bit boundary. 167 */ 122 168 if (packet->trace->format->type==TRACE_FORMAT_ERF) { 123 169 dag_record_t *erfptr = (dag_record_t *)packet->header; … … 137 183 } 138 184 185 /* Attempts to determine if the given filename refers to a DAG device */ 139 186 static int dag_probe_filename(const char *filename) 140 187 { … … 152 199 } 153 200 201 /* Initialises the DAG output data structure */ 154 202 static void dag_init_format_out_data(libtrace_out_t *libtrace) { 155 203 libtrace->format_data = (struct dag_format_data_out_t *) malloc(sizeof(struct dag_format_data_out_t)); 156 204 // no DUCK on output 157 205 FORMAT_DATA_OUT->stream_attached = 0; 158 FORMAT_DATA_OUT->drops = 0;159 206 FORMAT_DATA_OUT->device = NULL; 160 207 FORMAT_DATA_OUT->dagstream = 0; 161 FORMAT_DATA_OUT->processed = 0;162 FORMAT_DATA_OUT->bottom = NULL;163 FORMAT_DATA_OUT->top = NULL;164 208 FORMAT_DATA_OUT->waiting = 0; 165 209 166 210 } 167 211 212 /* Initialises the DAG input data structure */ 168 213 static void dag_init_format_data(libtrace_t *libtrace) { 169 214 libtrace->format_data = (struct dag_format_data_t *) … … 182 227 } 183 228 184 /* NOTE: This function assumes the open_dag_mutex is held by the caller */ 229 /* Determines if there is already an entry for the given DAG device in the 230 * device list and increments the reference count for that device, if found. 231 * 232 * NOTE: This function assumes the open_dag_mutex is held by the caller */ 185 233 static struct dag_dev_t *dag_find_open_device(char *dev_name) { 186 234 struct dag_dev_t *dag_dev; … … 203 251 } 204 252 205 /* NOTE: This function assumes the open_dag_mutex is held by the caller */ 253 /* Closes a DAG device and removes it from the device list. 254 * 255 * Attempting to close a DAG device that has a non-zero reference count will 256 * cause an assertion failure! 257 * 258 * NOTE: This function assumes the open_dag_mutex is held by the caller */ 206 259 static void dag_close_device(struct dag_dev_t *dev) { 207 260 /* Need to remove from the device list */ … … 225 278 } 226 279 280 281 /* Opens a new DAG device for writing and adds it to the DAG device list 282 * 283 * NOTE: this function should only be called when opening a DAG device for 284 * writing - there is little practical difference between this and the 285 * function below that covers the reading case, but we need the output trace 286 * object to report errors properly so the two functions take slightly 287 * different arguments. This is really lame and there should be a much better 288 * way of doing this. 289 * 290 * NOTE: This function assumes the open_dag_mutex is held by the caller 291 */ 227 292 static struct dag_dev_t *dag_open_output_device(libtrace_out_t *libtrace, char *dev_name) { 228 293 struct stat buf; … … 230 295 struct dag_dev_t *new_dev; 231 296 297 /* Make sure the device exists */ 232 298 if (stat(dev_name, &buf) == -1) { 233 299 trace_set_err_out(libtrace,errno,"stat(%s)",dev_name); … … 235 301 } 236 302 303 /* Make sure it is the appropriate type of device */ 237 304 if (S_ISCHR(buf.st_mode)) { 305 /* Try opening the DAG device */ 238 306 if((fd = dag_open(dev_name)) < 0) { 239 307 trace_set_err_out(libtrace,errno,"Cannot open DAG %s", … … 247 315 } 248 316 317 /* Add the device to our device list - it is just a doubly linked 318 * list with no inherent ordering; just tack the new one on the front 319 */ 249 320 new_dev = (struct dag_dev_t *)malloc(sizeof(struct dag_dev_t)); 250 321 new_dev->fd = fd; … … 262 333 } 263 334 264 /* NOTE: This function assumes the open_dag_mutex is held by the caller */ 335 /* Opens a new DAG device for reading and adds it to the DAG device list 336 * 337 * NOTE: this function should only be called when opening a DAG device for 338 * reading - there is little practical difference between this and the 339 * function above that covers the writing case, but we need the input trace 340 * object to report errors properly so the two functions take slightly 341 * different arguments. This is really lame and there should be a much better 342 * way of doing this. 343 * 344 * NOTE: This function assumes the open_dag_mutex is held by the caller */ 265 345 static struct dag_dev_t *dag_open_device(libtrace_t *libtrace, char *dev_name) { 266 346 struct stat buf; … … 268 348 struct dag_dev_t *new_dev; 269 349 350 /* Make sure the device exists */ 270 351 if (stat(dev_name, &buf) == -1) { 271 352 trace_set_err(libtrace,errno,"stat(%s)",dev_name); … … 273 354 } 274 355 356 /* Make sure it is the appropriate type of device */ 275 357 if (S_ISCHR(buf.st_mode)) { 358 /* Try opening the DAG device */ 276 359 if((fd = dag_open(dev_name)) < 0) { 277 360 trace_set_err(libtrace,errno,"Cannot open DAG %s", … … 285 368 } 286 369 370 /* Add the device to our device list - it is just a doubly linked 371 * list with no inherent ordering; just tack the new one on the front 372 */ 287 373 new_dev = (struct dag_dev_t *)malloc(sizeof(struct dag_dev_t)); 288 374 new_dev->fd = fd; … … 300 386 } 301 387 388 /* Creates and initialises a DAG output trace */ 302 389 static int dag_init_output(libtrace_out_t *libtrace) { 303 390 char *dag_dev_name = NULL; … … 315 402 316 403 dag_init_format_out_data(libtrace); 404 /* Grab the mutex while we're likely to be messing with the device 405 * list */ 317 406 pthread_mutex_lock(&open_dag_mutex); 407 408 /* Specific streams are signified using a comma in the libtrace URI, 409 * e.g. dag:/dev/dag0,1 refers to stream 1 on the dag0 device. 410 * 411 * If no stream is specified, we will write using stream 1 */ 318 412 if ((scan = strchr(libtrace->uridata,',')) == NULL) { 319 413 dag_dev_name = strdup(libtrace->uridata); … … 325 419 FORMAT_DATA_OUT->dagstream = stream; 326 420 421 /* See if our DAG device is already open */ 327 422 dag_device = dag_find_open_device(dag_dev_name); 328 423 … … 331 426 dag_device = dag_open_output_device(libtrace, dag_dev_name); 332 427 } else { 428 /* Otherwise, just use the existing one */ 333 429 free(dag_dev_name); 334 430 dag_dev_name = NULL; 335 431 } 336 432 433 /* Make sure we have successfully opened a DAG device */ 337 434 if (dag_device == NULL) { 338 435 if (dag_dev_name) { 339 436 free(dag_dev_name); 340 437 } 438 pthread_mutex_unlock(&open_dag_mutex); 341 439 return -1; 342 440 } … … 347 445 } 348 446 447 /* Creates and initialises a DAG input trace */ 349 448 static int dag_init_input(libtrace_t *libtrace) { 350 449 char *dag_dev_name = NULL; … … 354 453 355 454 dag_init_format_data(libtrace); 455 /* Grab the mutex while we're likely to be messing with the device 456 * list */ 356 457 pthread_mutex_lock(&open_dag_mutex); 458 459 460 /* Specific streams are signified using a comma in the libtrace URI, 461 * e.g. dag:/dev/dag0,2 refers to stream 2 on the dag0 device. 462 * 463 * If no stream is specified, we will read from stream 0 */ 357 464 if ((scan = strchr(libtrace->uridata,',')) == NULL) { 358 465 dag_dev_name = strdup(libtrace->uridata); … … 365 472 FORMAT_DATA->dagstream = stream; 366 473 474 /* See if our DAG device is already open */ 367 475 dag_device = dag_find_open_device(dag_dev_name); 368 476 … … 371 479 dag_device = dag_open_device(libtrace, dag_dev_name); 372 480 } else { 481 /* Otherwise, just use the existing one */ 373 482 free(dag_dev_name); 374 483 dag_dev_name = NULL; 375 484 } 376 485 486 /* Make sure we have successfully opened a DAG device */ 377 487 if (dag_device == NULL) { 378 488 if (dag_dev_name) 379 489 free(dag_dev_name); 380 490 dag_dev_name = NULL; 491 pthread_mutex_unlock(&open_dag_mutex); 381 492 return -1; 382 493 } … … 388 499 } 389 500 501 /* Configures a DAG input trace */ 390 502 static int dag_config_input(libtrace_t *libtrace, trace_option_t option, 391 503 void *data) { … … 393 505 switch(option) { 394 506 case TRACE_OPTION_META_FREQ: 507 /* This option is used to specify the frequency of DUCK 508 * updates */ 395 509 DUCK.duck_freq = *(int *)data; 396 510 return 0; 397 511 case TRACE_OPTION_SNAPLEN: 512 /* Tell the card our new snap length */ 398 513 snprintf(conf_str, 4096, "varlen slen=%i", *(int *)data); 399 514 if (dag_configure(FORMAT_DATA->device->fd, … … 407 522 return -1; 408 523 case TRACE_OPTION_FILTER: 524 /* We don't yet support pushing filters into DAG 525 * cards */ 409 526 return -1; 410 527 case TRACE_OPTION_EVENT_REALTIME: 528 /* Live capture is always going to be realtime */ 411 529 return -1; 412 530 } 413 531 return -1; 414 532 } 533 534 /* Starts a DAG output trace */ 415 535 static int dag_start_output(libtrace_out_t *libtrace) { 416 536 struct timeval zero, nopoll; … … 422 542 nopoll = zero; 423 543 544 /* Attach and start the DAG stream */ 545 424 546 if (dag_attach_stream(FORMAT_DATA_OUT->device->fd, 425 547 FORMAT_DATA_OUT->dagstream, 0, 1048576) < 0) { … … 444 566 } 445 567 568 /* Starts a DAG input trace */ 446 569 static int dag_start_input(libtrace_t *libtrace) { 447 570 struct timeval zero, nopoll; … … 454 577 nopoll = zero; 455 578 456 457 579 /* Attach and start the DAG stream */ 458 580 if (dag_attach_stream(FORMAT_DATA->device->fd, 459 581 FORMAT_DATA->dagstream, 0, 0) < 0) { … … 468 590 } 469 591 FORMAT_DATA->stream_attached = 1; 592 470 593 /* We don't want the dag card to do any sleeping */ 471 594 dag_set_stream_poll(FORMAT_DATA->device->fd, … … 474 597 475 598 /* Should probably flush the memory hole now */ 476 477 599 do { 478 600 top = dag_advance_stream(FORMAT_DATA->device->fd, … … 491 613 } 492 614 615 /* Pauses a DAG output trace */ 493 616 static int dag_pause_output(libtrace_out_t *libtrace) { 617 618 /* Stop and detach the stream */ 494 619 if (dag_stop_stream(FORMAT_DATA_OUT->device->fd, 495 620 FORMAT_DATA_OUT->dagstream) < 0) { … … 506 631 } 507 632 633 /* Pauses a DAG input trace */ 508 634 static int dag_pause_input(libtrace_t *libtrace) { 635 636 /* Stop and detach the stream */ 509 637 if (dag_stop_stream(FORMAT_DATA->device->fd, 510 638 FORMAT_DATA->dagstream) < 0) { … … 521 649 } 522 650 651 /* Closes a DAG input trace */ 523 652 static int dag_fin_input(libtrace_t *libtrace) { 653 /* Need the lock, since we're going to be handling the device list */ 524 654 pthread_mutex_lock(&open_dag_mutex); 655 656 /* Detach the stream if we are not paused */ 525 657 if (FORMAT_DATA->stream_attached) 526 658 dag_pause_input(libtrace); 527 659 FORMAT_DATA->device->ref_count --; 528 660 661 /* Close the DAG device if there are no more references to it */ 529 662 if (FORMAT_DATA->device->ref_count == 0) 530 663 dag_close_device(FORMAT_DATA->device); … … 536 669 } 537 670 671 /* Closes a DAG output trace */ 538 672 static int dag_fin_output(libtrace_out_t *libtrace) { 539 // commit any outstanding traffic in the txbuffer 673 674 /* Commit any outstanding traffic in the txbuffer */ 540 675 if (FORMAT_DATA_OUT->waiting) { 541 676 dag_tx_stream_commit_bytes(FORMAT_DATA_OUT->device->fd, FORMAT_DATA_OUT->dagstream, … … 543 678 } 544 679 545 / / wait until the buffer is nearly clear before exiting the program, as we546 // will lose packets otherwise680 /* Wait until the buffer is nearly clear before exiting the program, 681 * as we will lose packets otherwise */ 547 682 dag_tx_get_stream_space(FORMAT_DATA_OUT->device->fd, 548 683 FORMAT_DATA_OUT->dagstream, … … 551 686 ); 552 687 688 /* Need the lock, since we're going to be handling the device list */ 553 689 pthread_mutex_lock(&open_dag_mutex); 690 691 /* Detach the stream if we are not paused */ 554 692 if (FORMAT_DATA_OUT->stream_attached) 555 693 dag_pause_output(libtrace); 556 694 FORMAT_DATA_OUT->device->ref_count --; 557 695 696 /* Close the DAG device if there are no more references to it */ 558 697 if (FORMAT_DATA_OUT->device->ref_count == 0) 559 698 dag_close_device(FORMAT_DATA_OUT->device); … … 563 702 } 564 703 704 /* Extracts DUCK information from the DAG card and produces a DUCK packet */ 565 705 static int dag_get_duckinfo(libtrace_t *libtrace, 566 706 libtrace_packet_t *packet) { 707 708 /* Allocate memory for the DUCK data */ 567 709 if (packet->buf_control == TRACE_CTRL_EXTERNAL || 568 710 !packet->buffer) { … … 576 718 } 577 719 720 /* DUCK doesn't have a format header */ 578 721 packet->header = 0; 579 722 packet->payload = packet->buffer; 580 723 581 724 /* No need to check if we can get DUCK or not - we're modern 582 * enough */725 * enough so just grab the DUCK info */ 583 726 if ((ioctl(FORMAT_DATA->device->fd, DAGIOCDUCK, 584 727 (duckinf_t *)packet->payload) < 0)) { … … 588 731 589 732 packet->type = TRACE_RT_DUCK_2_5; 733 734 /* Set the packet's tracce to point at a DUCK trace, so that the 735 * DUCK format functions will be called on the packet rather than the 736 * DAG ones */ 590 737 if (!DUCK.dummy_duck) 591 738 DUCK.dummy_duck = trace_create_dead("rt:localhost:3434"); … … 594 741 } 595 742 743 /* Determines the amount of data available to read from the DAG card */ 596 744 static int dag_available(libtrace_t *libtrace) { 597 745 uint32_t diff = FORMAT_DATA->top - FORMAT_DATA->bottom; 746 598 747 /* If we've processed more than 4MB of data since we last called 599 748 * dag_advance_stream, then we should call it again to allow the … … 601 750 if (diff >= dag_record_size && FORMAT_DATA->processed < 4 * 1024 * 1024) 602 751 return diff; 752 753 /* Update the top and bottom pointers */ 603 754 FORMAT_DATA->top = dag_advance_stream(FORMAT_DATA->device->fd, 604 755 FORMAT_DATA->dagstream, 605 756 &(FORMAT_DATA->bottom)); 757 606 758 if (FORMAT_DATA->top == NULL) { 607 759 trace_set_err(libtrace, errno, "dag_advance_stream failed!"); … … 613 765 } 614 766 767 /* Returns a pointer to the start of the next complete ERF record */ 615 768 static dag_record_t *dag_get_record(libtrace_t *libtrace) { 616 769 dag_record_t *erfptr = NULL; … … 629 782 } 630 783 784 /* Converts a buffer containing a recently read DAG packet record into a 785 * libtrace packet */ 631 786 static int dag_prepare_packet(libtrace_t *libtrace, libtrace_packet_t *packet, 632 787 void *buffer, libtrace_rt_types_t rt_type, uint32_t flags) { 633 788 634 789 dag_record_t *erfptr; 635 790 791 /* If the packet previously owned a buffer that is not the buffer 792 * that contains the new packet data, we're going to need to free the 793 * old one to avoid memory leaks */ 636 794 if (packet->buffer != buffer && 637 795 packet->buf_control == TRACE_CTRL_PACKET) { … … 639 797 } 640 798 799 /* Set the buffer owner appropriately */ 641 800 if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) { 642 801 packet->buf_control = TRACE_CTRL_PACKET; … … 644 803 packet->buf_control = TRACE_CTRL_EXTERNAL; 645 804 805 /* Update the packet pointers and type appropriately */ 646 806 erfptr = (dag_record_t *)buffer; 647 807 packet->buffer = erfptr; … … 650 810 651 811 if (erfptr->flags.rxerror == 1) { 652 /* rxerror means the payload is corrupt - drop it812 /* rxerror means the payload is corrupt - drop the payload 653 813 * by tweaking rlen */ 654 814 packet->payload = NULL; … … 663 823 } 664 824 825 /* Update the dropped packets counter */ 826 665 827 /* No loss counter for DSM coloured records - have to use 666 828 * some other API */ 667 829 if (erfptr->type == TYPE_DSM_COLOR_ETH) { 668 830 /* TODO */ 669 831 } else { 832 /* Use the ERF loss counter */ 670 833 DATA(libtrace)->drops += ntohs(erfptr->lctr); 671 834 } … … 682 845 */ 683 846 847 /* Pushes an ERF record onto the transmit stream */ 684 848 static int dag_dump_packet(libtrace_out_t *libtrace, 685 849 dag_record_t *erfptr, unsigned int pad, void *buffer) { 686 //int numbytes = 0;687 850 int size; 688 851 … … 702 865 703 866 /* 704 * Copy the header separately to the body, as we can't guarantee they are705 * in contiguous memory867 * Copy the header separately to the body, as we can't guarantee they 868 * are in contiguous memory 706 869 */ 707 870 memcpy(FORMAT_DATA_OUT->txbuffer + FORMAT_DATA_OUT->waiting,erfptr,(dag_record_size + pad)); … … 711 874 712 875 /* 713 * Copy our incoming packet into the outgoing buffer, and increment our waiting count 876 * Copy our incoming packet into the outgoing buffer, and increment 877 * our waiting count 714 878 */ 715 879 size = ntohs(erfptr->rlen)-(dag_record_size + pad); … … 718 882 719 883 /* 720 * if our output buffer has more than 16 Mebibytes in it, commit those bytes and721 * reset the waiting count to 0.722 * Note: dag_fin_output will also call dag_tx_stream_commit_bytes() in case723 * there is still data in the buffer at program exit.884 * If our output buffer has more than 16 Mebibytes in it, commit those 885 * bytes and reset the waiting count to 0. 886 * Note: dag_fin_output will also call dag_tx_stream_commit_bytes() in 887 * case there is still data in the buffer at program exit. 724 888 */ 725 889 … … 734 898 } 735 899 900 /* Attempts to determine a suitable ERF type for a given packet. Returns true 901 * if one is found, false otherwise */ 736 902 static bool find_compatible_linktype(libtrace_out_t *libtrace, 737 libtrace_packet_t *packet )903 libtrace_packet_t *packet, char *type) 738 904 { 739 905 // Keep trying to simplify the packet until we can find … … 741 907 742 908 do { 743 chartype=libtrace_to_erf_type(trace_get_link_type(packet));909 *type=libtrace_to_erf_type(trace_get_link_type(packet)); 744 910 745 911 // Success 746 if ( type != (char)-1)912 if (*type != (char)-1) 747 913 return true; 748 914 … … 760 926 } 761 927 928 /* Writes a packet to the provided DAG output trace */ 762 929 static int dag_write_packet(libtrace_out_t *libtrace, libtrace_packet_t *packet) { 763 930 /* 764 * This is heavily borrowed from erf_write_packet(). Yes, CnP coding sucks,765 * s orry about that.931 * This is heavily borrowed from erf_write_packet(). Yes, CnP coding 932 * sucks, sorry about that. 766 933 */ 767 934 unsigned int pad = 0; … … 769 936 void *payload = packet->payload; 770 937 dag_record_t *header = (dag_record_t *)packet->header; 771 772 FORMAT_DATA_OUT->processed ++; 938 char erf_type = 0; 939 773 940 if(!packet->header) { 774 // No header, probably an RT packet. Lifted from erf_write_packet(). 941 /* No header, probably an RT packet. Lifted from 942 * erf_write_packet(). */ 775 943 return -1; 776 944 } … … 779 947 780 948 /* 781 * if the payload is null, adjust the rlen. Discussion of this is949 * If the payload is null, adjust the rlen. Discussion of this is 782 950 * attached to erf_write_packet() 783 951 */ … … 796 964 /* Build up a new packet header from the existing header */ 797 965 798 // Simplify the packet first - if we can't do this, break early 799 if (!find_compatible_linktype(libtrace,packet)) 966 /* Simplify the packet first - if we can't do this, break 967 * early */ 968 if (!find_compatible_linktype(libtrace,packet,&erf_type)) 800 969 return -1; 801 970 … … 811 980 erfhdr.flags.iface = trace_get_direction(packet); 812 981 813 erfhdr.type = libtrace_to_erf_type(trace_get_link_type(packet));982 erfhdr.type = erf_type; 814 983 815 984 /* Packet length (rlen includes format overhead) */ … … 825 994 826 995 827 /* loss counter. Can't do this */996 /* Loss counter. Can't do this */ 828 997 erfhdr.lctr = 0; 829 998 /* Wire length, does not include padding! */ … … 841 1010 } 842 1011 1012 /* Reads the next available packet from a DAG card, in a BLOCKING fashion 1013 * 1014 * If DUCK reporting is enabled, the packet returned may be a DUCK update 1015 */ 843 1016 static int dag_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { 844 1017 int size = 0; … … 848 1021 uint32_t flags = 0; 849 1022 850 if (DUCK.last_pkt - DUCK.last_duck > DUCK.duck_freq && 1023 /* Check if we're due for a DUCK report */ 1024 if (DUCK.last_pkt - DUCK.last_duck > DUCK.duck_freq && 851 1025 DUCK.duck_freq != 0) { 852 1026 size = dag_get_duckinfo(libtrace, packet); … … 859 1033 } 860 1034 1035 /* Don't let anyone try to free our DAG memory hole! */ 861 1036 flags |= TRACE_PREP_DO_NOT_OWN_BUFFER; 862 1037 1038 /* If the packet buffer is currently owned by libtrace, free it so 1039 * that we can set the packet to point into the DAG memory hole */ 863 1040 if (packet->buf_control == TRACE_CTRL_PACKET) { 864 1041 free(packet->buffer); … … 866 1043 } 867 1044 1045 /* Grab a full ERF record */ 868 1046 do { 869 1047 numbytes = dag_available(libtrace); … … 876 1054 } while (erfptr == NULL); 877 1055 878 / /dag_form_packet(erfptr, packet);1056 /* Prepare the libtrace packet */ 879 1057 if (dag_prepare_packet(libtrace, packet, erfptr, TRACE_RT_DATA_ERF, 880 1058 flags)) 881 1059 return -1; 1060 1061 /* Update the DUCK timer */ 882 1062 tv = trace_get_timeval(packet); 883 1063 DUCK.last_pkt = tv.tv_sec; … … 887 1067 } 888 1068 1069 /* Attempts to read a packet from a DAG card in a NON-BLOCKING fashion. If a 1070 * packet is available, we will return a packet event. Otherwise we will 1071 * return a SLEEP event (as we cannot select on the DAG file descriptor). 1072 */ 889 1073 static libtrace_eventobj_t trace_event_dag(libtrace_t *trace, 890 1074 libtrace_packet_t *packet) { … … 907 1091 erfptr = dag_get_record(trace); 908 1092 if (erfptr == NULL) { 909 /* No packet available */1093 /* No packet available - sleep for a very short time */ 910 1094 event.type = TRACE_EVENT_SLEEP; 911 1095 event.seconds = 0.0001; 912 1096 break; 913 1097 } 914 //dag_form_packet(erfptr, packet);915 1098 if (dag_prepare_packet(trace, packet, erfptr, 916 1099 TRACE_RT_DATA_ERF, flags)) { … … 922 1105 event.size = trace_get_capture_length(packet) + 923 1106 trace_get_framing_length(packet); 1107 1108 /* XXX trace_read_packet() normally applies the following 1109 * config options for us, but this function is called via 1110 * trace_event() so we have to do it ourselves */ 1111 924 1112 if (trace->filter) { 925 1113 if (trace_apply_filter(trace->filter, packet)) { … … 946 1134 } 947 1135 1136 /* Gets the number of dropped packets */ 948 1137 static uint64_t dag_get_dropped_packets(libtrace_t *trace) { 949 1138 if (trace->format_data == NULL) … … 952 1141 } 953 1142 1143 /* Prints some semi-useful help text about the DAG format module */ 954 1144 static void dag_help(void) { 955 1145 printf("dag format module: $Revision$\n");
Note: See TracChangeset
for help on using the changeset viewer.