2 * pcap-dag.c: Packet capture interface for Endace DAG cards.
4 * Authors: Richard Littin, Sean Irvine ({richard,sean}@reeltwo.com)
5 * Modifications: Jesper Peterson
7 * Stephen Donnelly <stephen.donnelly@endace.com>
21 #if __BYTE_ORDER == __BIG_ENDIAN
22 // Will need SWAPLL().
23 #include "pcap-util.h"
28 #include "dag_config_api.h"
33 * DAG devices have names beginning with "dag", followed by a number
34 * from 0 to DAG_MAX_BOARDS, then optionally a colon and a stream number
35 * from 0 to DAG_STREAM_MAX.
37 #ifndef DAG_MAX_BOARDS
38 #define DAG_MAX_BOARDS 32
41 #define ATM_CELL_SIZE 52
42 #define ATM_HDR_SIZE 4
45 * A header containing additional MTP information.
47 #define MTP2_SENT_OFFSET 0 /* 1 byte */
48 #define MTP2_ANNEX_A_USED_OFFSET 1 /* 1 byte */
49 #define MTP2_LINK_NUMBER_OFFSET 2 /* 2 bytes */
50 #define MTP2_HDR_LEN 4 /* length of the header */
52 #define MTP2_ANNEX_A_NOT_USED 0
53 #define MTP2_ANNEX_A_USED 1
54 #define MTP2_ANNEX_A_USED_UNKNOWN 2
56 /* SunATM pseudo header */
58 unsigned char flags
; /* destination and traffic type */
59 unsigned char vpi
; /* VPI */
60 unsigned short vci
; /* VCI */
64 * Private data for capturing on DAG devices.
67 struct pcap_stat stat
;
68 u_char
*dag_mem_bottom
; /* DAG card current memory bottom pointer */
69 u_char
*dag_mem_top
; /* DAG card current memory top pointer */
70 int dag_fcs_bits
; /* Number of checksum bits from link layer */
71 int dag_flags
; /* Flags */
72 int dag_devnum
; /* This is the N in "dagN" or "dagN:M". */
73 int dag_stream
; /* And this is the M. */
74 int dag_timeout
; /* timeout specified to pcap_open_live.
75 * Same as in linux above, introduce
77 dag_card_ref_t dag_ref
; /* DAG Configuration/Status API card reference */
78 dag_component_t dag_root
; /* DAG CSAPI Root component */
79 attr_uuid_t drop_attr
; /* DAG Stream Drop Attribute handle, if available */
80 uint64_t drop_base
; // Rx stream drop counter initial value.
81 struct timeval required_select_timeout
;
82 /* Timeout caller must use in event loops */
83 uint8_t tx_iface
; // Tx interface number
84 uint8_t tx_align_bytes
; /* If necessary, add trailing padding to an
85 * ERF record to make it a multiple of this
86 * many bytes long. DAG API calls this "ERF
87 * record alignment". */
88 uint8_t terf_fcs_bytes
; // How many FCS bytes TERF is expecting.
91 #define ALIGN_BYTES_DEFAULT 8
92 #define ALIGN_BYTES_9_2 16
93 #define ALIGN_BYTES_MAX ALIGN_BYTES_9_2
95 typedef struct pcap_dag_node
{
96 struct pcap_dag_node
*next
;
101 static pcap_dag_node_t
*pcap_dags
= NULL
;
102 static int atexit_handler_installed
= 0;
104 #define MAX_DAG_PACKET 65536
106 static unsigned char TempPkt
[MAX_DAG_PACKET
];
108 #define TX_ONLY(stream) ((stream) % 2)
109 #define RX_ONLY(stream) (! TX_ONLY(stream))
110 #define RXTX_STR(stream) (TX_ONLY(stream) ? "Tx" : "Rx")
112 static int dag_stats(pcap_t
*p
, struct pcap_stat
*ps
);
113 static int dag_set_datalink(pcap_t
*p
, int dlt
);
114 static int dag_get_datalink(pcap_t
*p
);
115 static int dag_setnonblock(pcap_t
*p
, int nonblock
);
117 // Environment variables that can control behaviour of this libpcap module.
118 #define ENV_RX_FCS_BITS "ERF_FCS_BITS"
119 #define ENV_RX_FCS_NOSTRIP "ERF_DONT_STRIP_FCS"
120 #define ENV_TX_IFACE "ERF_TX_INTERFACE"
123 * Convert the return value of getenv() to an integer using matching stricter
124 * than atoi(). If the environment variable is not set, return the default
125 * value. Otherwise return an integer in the interval [0, INT32_MAX] or -1 on
129 strtouint31(const char *str
, const int32_t defaultval
) {
136 unsigned long val
= strtoul(str
, &endp
, 10);
137 if (*endp
|| val
> INT32_MAX
)
143 delete_pcap_dag(const pcap_t
*p
)
145 pcap_dag_node_t
*curr
= NULL
, *prev
= NULL
;
147 for (prev
= NULL
, curr
= pcap_dags
; curr
!= NULL
&& curr
->p
!= p
; prev
= curr
, curr
= curr
->next
) {
151 if (curr
!= NULL
&& curr
->p
== p
) {
153 prev
->next
= curr
->next
;
155 pcap_dags
= curr
->next
;
161 * Performs a graceful shutdown of the DAG card, frees dynamic memory held
162 * in the pcap_t structure, and closes the file descriptor for the DAG card.
166 dag_platform_cleanup(pcap_t
*p
)
168 struct pcap_dag
*pd
= p
->priv
;
171 * Before stopping a Tx stream wait until the stream buffer has been
172 * drained, otherwise packets that have been buffered but have not yet
173 * been transmitted will be lost.
175 if (TX_ONLY(pd
->dag_stream
))
176 while (dag_get_stream_buffer_level64(p
->fd
, pd
->dag_stream
) > 0)
179 if(dag_stop_stream(p
->fd
, pd
->dag_stream
) < 0)
180 fprintf(stderr
,"dag_stop_stream: %s\n", strerror(errno
));
182 if(dag_detach_stream(p
->fd
, pd
->dag_stream
) < 0)
183 fprintf(stderr
,"dag_detach_stream: %s\n", strerror(errno
));
185 if(pd
->dag_ref
!= NULL
) {
186 dag_config_dispose(pd
->dag_ref
);
188 * Note: we don't need to call close(p->fd) or
189 * dag_close(p->fd), as dag_config_dispose(pd->dag_ref)
192 * Set p->fd to -1 to make sure that's not done.
198 pcapint_cleanup_live_common(p
);
204 while (pcap_dags
!= NULL
) {
205 if (pcap_dags
->pid
== getpid()) {
206 if (pcap_dags
->p
!= NULL
)
207 dag_platform_cleanup(pcap_dags
->p
);
209 delete_pcap_dag(pcap_dags
->p
);
215 new_pcap_dag(pcap_t
*p
)
217 pcap_dag_node_t
*node
= NULL
;
219 if ((node
= malloc(sizeof(pcap_dag_node_t
))) == NULL
) {
223 if (!atexit_handler_installed
) {
224 atexit(atexit_handler
);
225 atexit_handler_installed
= 1;
228 node
->next
= pcap_dags
;
230 node
->pid
= getpid();
238 dag_erf_ext_header_count(const uint8_t *erf
, size_t len
)
240 uint32_t hdr_num
= 0;
243 /* basic sanity checks */
249 /* check if we have any extension headers */
250 if (! (erf
[8] & ERF_TYPE_MORE_EXT
))
253 /* loop over the extension headers */
256 /* sanity check we have enough bytes */
257 if ( len
< (24 + (hdr_num
* 8)) )
260 /* get the header type */
261 hdr_type
= erf
[(16 + (hdr_num
* 8))];
264 } while (hdr_type
& ERF_TYPE_MORE_EXT
);
270 dag_rxtx_mismatch(const char *func
, pcap_t
*p
)
272 const struct pcap_dag
*pd
= p
->priv
;
274 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "%s: device %s is %s-only",
277 RXTX_STR(pd
->dag_stream
));
282 dag_read_notimpl(pcap_t
*p
, int cnt _U_
, pcap_handler callback _U_
, u_char
*user _U_
)
284 return dag_rxtx_mismatch(__func__
, p
);
288 dag_getnonblock_fd_notimpl(pcap_t
*p
)
290 return dag_rxtx_mismatch(__func__
, p
);
294 dag_stats_notimpl(pcap_t
*p
, struct pcap_stat
*ps _U_
)
296 return dag_rxtx_mismatch(__func__
, p
);
300 dag_setnonblock_notimpl(pcap_t
*p
, int nonblock _U_
)
302 return dag_rxtx_mismatch( __func__
, p
);
306 dag_inject_notimpl(pcap_t
*p
, const void *buf _U_
, int size _U_
)
308 return dag_rxtx_mismatch(__func__
, p
);
312 dag_install_bpf_program_notimpl(pcap_t
*p
, struct bpf_program
*fp _U_
)
314 return dag_rxtx_mismatch(__func__
, p
);
318 * Read at most max_packets from the capture stream and call the callback
319 * for each of them. Returns the number of packets handled, PCAP_ERROR if an
320 * error occurred, or PCAP_ERROR_BREAK if we were told to break out of the loop.
323 dag_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
325 struct pcap_dag
*pd
= p
->priv
;
327 unsigned int nonblocking
= pd
->dag_flags
& DAGF_NONBLOCK
;
328 unsigned int num_ext_hdr
= 0;
329 unsigned int ticks_per_second
;
331 /* Get the next bufferful of packets (if necessary). */
332 while (pd
->dag_mem_top
- pd
->dag_mem_bottom
< dag_record_size
) {
335 * Has "pcap_breakloop()" been called?
339 * Yes - clear the flag that indicates that
340 * it has, and return PCAP_ERROR_BREAK to indicate that
341 * we were told to break out of the loop.
344 return PCAP_ERROR_BREAK
;
347 /* dag_advance_stream() will block (unless nonblock is called)
348 * until 64kB of data has accumulated.
349 * If to_ms is set, it will timeout before 64kB has accumulated.
350 * We wait for 64kB because processing a few packets at a time
351 * can cause problems at high packet rates (>200kpps) due
353 * This does mean if to_ms is not specified the capture may 'hang'
354 * for long periods if the data rate is extremely slow (<64kB/sec)
355 * If non-block is specified it will return immediately. The user
356 * is then responsible for efficiency.
358 if ( NULL
== (pd
->dag_mem_top
= dag_advance_stream(p
->fd
, pd
->dag_stream
, &(pd
->dag_mem_bottom
))) ) {
362 if (nonblocking
&& (pd
->dag_mem_top
- pd
->dag_mem_bottom
< dag_record_size
))
364 /* Pcap is configured to process only available packets, and there aren't any, return immediately. */
370 (pd
->dag_mem_top
- pd
->dag_mem_bottom
< dag_record_size
))
372 /* Blocking mode, but timeout set and no data has arrived, return anyway.*/
379 * Process the packets.
381 * This assumes that a single buffer of packets will have
382 * <= INT_MAX packets, so the packet count doesn't overflow.
384 while (pd
->dag_mem_top
- pd
->dag_mem_bottom
>= dag_record_size
) {
386 unsigned short packet_len
= 0;
388 struct pcap_pkthdr pcap_header
;
390 dag_record_t
*header
= (dag_record_t
*)(pd
->dag_mem_bottom
);
392 u_char
*dp
= ((u_char
*)header
); /* + dag_record_size; */
396 * Has "pcap_breakloop()" been called?
400 * Yes - clear the flag that indicates that
401 * it has, and return PCAP_ERROR_BREAK to indicate that
402 * we were told to break out of the loop.
405 return PCAP_ERROR_BREAK
;
408 rlen
= ntohs(header
->rlen
);
409 if (rlen
< dag_record_size
)
411 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
412 "%s: record too small", __func__
);
415 pd
->dag_mem_bottom
+= rlen
;
417 uint8_t erf_type
= header
->type
& ERF_TYPE_MASK
;
419 /* Count lost packets. */
421 /* in these types the color value overwrites the lctr */
422 case ERF_TYPE_COLOR_HDLC_POS
:
423 case ERF_TYPE_COLOR_ETH
:
424 case ERF_TYPE_DSM_COLOR_HDLC_POS
:
425 case ERF_TYPE_DSM_COLOR_ETH
:
426 case ERF_TYPE_COLOR_MC_HDLC_POS
:
427 case ERF_TYPE_COLOR_HASH_ETH
:
428 case ERF_TYPE_COLOR_HASH_POS
:
432 if ( (pd
->drop_attr
== kNullAttributeUuid
) && (header
->lctr
) ) {
433 pd
->stat
.ps_drop
+= ntohs(header
->lctr
);
437 if (erf_type
== ERF_TYPE_PAD
) {
441 num_ext_hdr
= dag_erf_ext_header_count(dp
, rlen
);
443 /* ERF encapsulation */
444 /* The Extensible Record Format is not dropped for this kind of encapsulation,
445 * and will be handled as a pseudo header by the decoding application.
446 * The information carried in the ERF header and in the optional subheader (if present)
447 * could be merged with the libpcap information, to offer a better decoding.
448 * The packet length is
449 * o the length of the packet on the link (header->wlen),
450 * o plus the length of the ERF header (dag_record_size), as the length of the
451 * pseudo header will be adjusted during the decoding,
452 * o plus the length of the optional subheader (if present).
454 * The capture length is header.rlen and the byte stuffing for alignment will be dropped
455 * if the capture length is greater than the packet length.
457 if (p
->linktype
== DLT_ERF
) {
458 packet_len
= ntohs(header
->wlen
) + dag_record_size
;
461 case ERF_TYPE_MC_AAL5
:
462 case ERF_TYPE_MC_ATM
:
463 case ERF_TYPE_MC_HDLC
:
464 case ERF_TYPE_MC_RAW_CHANNEL
:
465 case ERF_TYPE_MC_RAW
:
466 case ERF_TYPE_MC_AAL2
:
467 case ERF_TYPE_COLOR_MC_HDLC_POS
:
468 packet_len
+= 4; /* MC header */
471 case ERF_TYPE_COLOR_HASH_ETH
:
472 case ERF_TYPE_DSM_COLOR_ETH
:
473 case ERF_TYPE_COLOR_ETH
:
475 packet_len
+= 2; /* ETH header */
479 /* Include ERF extension headers */
480 packet_len
+= (8 * num_ext_hdr
);
482 if (caplen
> packet_len
) {
486 /* Other kind of encapsulation according to the header Type */
488 /* Skip over generic ERF header */
489 dp
+= dag_record_size
;
490 /* Skip over extension headers */
491 dp
+= 8 * num_ext_hdr
;
496 if (erf_type
== ERF_TYPE_AAL5
) {
497 packet_len
= ntohs(header
->wlen
);
498 caplen
= rlen
- dag_record_size
;
501 case ERF_TYPE_MC_ATM
:
502 if (erf_type
== ERF_TYPE_MC_ATM
) {
503 caplen
= packet_len
= ATM_CELL_SIZE
;
507 case ERF_TYPE_MC_AAL5
:
508 if (erf_type
== ERF_TYPE_MC_AAL5
) {
509 packet_len
= ntohs(header
->wlen
);
510 caplen
= rlen
- dag_record_size
- 4;
513 /* Skip over extension headers */
514 caplen
-= (8 * num_ext_hdr
);
516 if (erf_type
== ERF_TYPE_ATM
) {
517 caplen
= packet_len
= ATM_CELL_SIZE
;
519 if (p
->linktype
== DLT_SUNATM
) {
520 struct sunatm_hdr
*sunatm
= (struct sunatm_hdr
*)dp
;
521 unsigned long rawatm
;
523 rawatm
= ntohl(*((uint32_t *)dp
));
524 sunatm
->vci
= htons((rawatm
>> 4) & 0xffff);
525 sunatm
->vpi
= (rawatm
>> 20) & 0x00ff;
526 sunatm
->flags
= ((header
->flags
.iface
& 1) ? 0x80 : 0x00) |
527 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(5)) ? 6 :
528 ((sunatm
->vpi
== 0 && sunatm
->vci
== htons(16)) ? 5 :
529 ((dp
[ATM_HDR_SIZE
] == 0xaa &&
530 dp
[ATM_HDR_SIZE
+1] == 0xaa &&
531 dp
[ATM_HDR_SIZE
+2] == 0x03) ? 2 : 1)));
533 } else if (p
->linktype
== DLT_ATM_RFC1483
) {
534 packet_len
-= ATM_HDR_SIZE
;
535 caplen
-= ATM_HDR_SIZE
;
541 case ERF_TYPE_COLOR_HASH_ETH
:
542 case ERF_TYPE_DSM_COLOR_ETH
:
543 case ERF_TYPE_COLOR_ETH
:
545 if ((p
->linktype
!= DLT_EN10MB
) &&
546 (p
->linktype
!= DLT_DOCSIS
))
548 packet_len
= ntohs(header
->wlen
);
549 packet_len
-= (pd
->dag_fcs_bits
>> 3);
550 caplen
= rlen
- dag_record_size
- 2;
551 /* Skip over extension headers */
552 caplen
-= (8 * num_ext_hdr
);
553 if (caplen
> packet_len
) {
559 case ERF_TYPE_COLOR_HASH_POS
:
560 case ERF_TYPE_DSM_COLOR_HDLC_POS
:
561 case ERF_TYPE_COLOR_HDLC_POS
:
562 case ERF_TYPE_HDLC_POS
:
563 if ((p
->linktype
!= DLT_CHDLC
) &&
564 (p
->linktype
!= DLT_PPP_SERIAL
) &&
565 (p
->linktype
!= DLT_FRELAY
))
567 packet_len
= ntohs(header
->wlen
);
568 packet_len
-= (pd
->dag_fcs_bits
>> 3);
569 caplen
= rlen
- dag_record_size
;
570 /* Skip over extension headers */
571 caplen
-= (8 * num_ext_hdr
);
572 if (caplen
> packet_len
) {
577 case ERF_TYPE_COLOR_MC_HDLC_POS
:
578 case ERF_TYPE_MC_HDLC
:
579 if ((p
->linktype
!= DLT_CHDLC
) &&
580 (p
->linktype
!= DLT_PPP_SERIAL
) &&
581 (p
->linktype
!= DLT_FRELAY
) &&
582 (p
->linktype
!= DLT_MTP2
) &&
583 (p
->linktype
!= DLT_MTP2_WITH_PHDR
) &&
584 (p
->linktype
!= DLT_LAPD
))
586 packet_len
= ntohs(header
->wlen
);
587 packet_len
-= (pd
->dag_fcs_bits
>> 3);
588 caplen
= rlen
- dag_record_size
- 4;
589 /* Skip over extension headers */
590 caplen
-= (8 * num_ext_hdr
);
591 if (caplen
> packet_len
) {
594 /* jump the MC_HDLC_HEADER */
596 if (p
->linktype
== DLT_MTP2_WITH_PHDR
) {
597 /* Add the MTP2 Pseudo Header */
598 caplen
+= MTP2_HDR_LEN
;
599 packet_len
+= MTP2_HDR_LEN
;
601 TempPkt
[MTP2_SENT_OFFSET
] = 0;
602 TempPkt
[MTP2_ANNEX_A_USED_OFFSET
] = MTP2_ANNEX_A_USED_UNKNOWN
;
603 *(TempPkt
+MTP2_LINK_NUMBER_OFFSET
) = ((header
->rec
.mc_hdlc
.mc_header
>>16)&0x01);
604 *(TempPkt
+MTP2_LINK_NUMBER_OFFSET
+1) = ((header
->rec
.mc_hdlc
.mc_header
>>24)&0xff);
605 memcpy(TempPkt
+MTP2_HDR_LEN
, dp
, caplen
);
611 if ((p
->linktype
!= DLT_RAW
) &&
612 (p
->linktype
!= DLT_IPV4
))
614 packet_len
= ntohs(header
->wlen
);
615 caplen
= rlen
- dag_record_size
;
616 /* Skip over extension headers */
617 caplen
-= (8 * num_ext_hdr
);
618 if (caplen
> packet_len
) {
624 if ((p
->linktype
!= DLT_RAW
) &&
625 (p
->linktype
!= DLT_IPV6
))
627 packet_len
= ntohs(header
->wlen
);
628 caplen
= rlen
- dag_record_size
;
629 /* Skip over extension headers */
630 caplen
-= (8 * num_ext_hdr
);
631 if (caplen
> packet_len
) {
636 /* These types have no matching 'native' DLT, but can be used with DLT_ERF above */
637 case ERF_TYPE_MC_RAW
:
638 case ERF_TYPE_MC_RAW_CHANNEL
:
639 case ERF_TYPE_IP_COUNTER
:
640 case ERF_TYPE_TCP_FLOW_COUNTER
:
641 case ERF_TYPE_INFINIBAND
:
642 case ERF_TYPE_RAW_LINK
:
643 case ERF_TYPE_INFINIBAND_LINK
:
645 /* Unhandled ERF type.
646 * Ignore rather than generating error
651 } /* ERF encapsulation */
654 * In this libpcap module the two length arguments of
655 * pcapint_filter() (the wire length and the captured length)
656 * can have different values.
658 * The wire length of this packet is packet_len, which is
659 * derived from ERF wlen; the captured length of this packet
660 * is caplen, which is derived from ERF rlen, which in turn
661 * depends on the card/stream slen; the snapshot length
662 * configured for this pcap handle is p->snapshot.
664 if ((p
->fcode
.bf_insns
== NULL
) || pcapint_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
)) {
666 /* convert between timestamp formats */
667 register unsigned long long ts
;
669 #if __BYTE_ORDER == __BIG_ENDIAN
670 ts
= SWAPLL(header
->ts
);
673 #endif // __BYTE_ORDER
675 switch (p
->opt
.tstamp_precision
) {
676 case PCAP_TSTAMP_PRECISION_NANO
:
677 ticks_per_second
= 1000000000;
679 case PCAP_TSTAMP_PRECISION_MICRO
:
681 ticks_per_second
= 1000000;
685 pcap_header
.ts
.tv_sec
= ts
>> 32;
686 ts
= (ts
& 0xffffffffULL
) * ticks_per_second
;
687 ts
+= 0x80000000; /* rounding */
688 pcap_header
.ts
.tv_usec
= ts
>> 32;
689 if (pcap_header
.ts
.tv_usec
>= ticks_per_second
) {
690 pcap_header
.ts
.tv_usec
-= ticks_per_second
;
691 pcap_header
.ts
.tv_sec
++;
694 /* Fill in our own header data */
695 pcap_header
.caplen
= min(caplen
, p
->snapshot
);
696 pcap_header
.len
= packet_len
;
698 /* Count the packet. */
701 /* Call the user supplied callback function */
702 callback(user
, &pcap_header
, dp
);
704 /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
706 if (processed
== cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
))
708 /* Reached the user-specified limit. */
718 * The minimum number of bytes in a valid Ethernet packet from the beginning
719 * of the destination MAC address to the end of the payload, as far as DAG
720 * TERF and IEEE 802.3 are concerned.
722 #define ETH_MINLEN_NOFCS 60
724 // ...and the maximum, as far as DAG TERF is concerned.
725 #define ETH_MAXLEN_NOFCS 9596
728 * The minimum number of bytes in a valid Ethernet header: the destination and
729 * the source MAC addresses, the EtherType. (This does not take 802.1Q or
730 * Q-in-Q into account.)
732 #define ETH_MINLEN_HDRONLY 14
734 // Zero padding source (a bit oversized for ERF_TYPE_ETH purposes).
735 static const uint8_t tx_pad
[ETH_MINLEN_NOFCS
+ 4 + ALIGN_BYTES_MAX
];
738 * Take an Ethernet frame, build an ERF record around it and feed the record
739 * into the [Tx-only] DAG stream. The frame must not include FCS, which is
740 * usually the case for DLT_EN10MB in libpcap.
743 dag_inject(pcap_t
*p
, const void *packet
, const int plen
)
745 struct pcap_dag
*pd
= p
->priv
;
747 if (plen
<= ETH_MINLEN_HDRONLY
|| plen
> ETH_MAXLEN_NOFCS
) {
748 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
749 "invalid packet size %d", plen
);
754 * sizeof(struct dag_record_t) cannot be used because it is much
755 * greater than the specific ERF header in this buffer.
757 const dag_size_t hlen
= dag_record_size
+ 2;
759 * Some DAG hardware (e.g. 7.5G2) automatically pads outgoing Ethernet
760 * frames that are shorter than the minimum length to make them long
761 * enough. Other DAG hardware (e.g. 9.2X2) rejects such frames. Pad
762 * the frame here if necessary and remove this difference from the
765 * If the TERF is expecting Ethernet frames to have a non-zero number
766 * of FCS bytes (typically in order to strip the FCS correctly before
767 * further processing), append a dummy zero FCS of the expected size.
769 const unsigned eth_pad_len
= ETH_MINLEN_NOFCS
770 - min(plen
, ETH_MINLEN_NOFCS
) + pd
->terf_fcs_bytes
;
771 const unsigned rlen
= hlen
+ plen
+ eth_pad_len
;
772 const unsigned erf_pad_len
= rlen
% pd
->tx_align_bytes
?
773 pd
->tx_align_bytes
- rlen
% pd
->tx_align_bytes
:
775 dag_record_t header
= {
776 .type
= ERF_TYPE_ETH
,
778 .rlen
= htons(rlen
+ erf_pad_len
),
779 .wlen
= htons(plen
+ eth_pad_len
),
780 // Silence a -Wmissing-field-initializers from old GCC.
783 DAG_ERF_SET_IFACE(&header
, pd
->tx_iface
);
786 * It is fine to feed less data than a complete ERF record at a time so
787 * long as a complete and well-formed ERF record eventually makes it
788 * into the buffer. This simplifies the process when different parts
789 * of the ERF record come from different memory locations.
791 if (dag_tx_stream_copy_bytes64(p
->fd
, pd
->dag_stream
, (uint8_t *)&header
, hlen
) < 0 ||
792 dag_tx_stream_copy_bytes64(p
->fd
, pd
->dag_stream
, (uint8_t *)packet
, plen
) < 0)
795 * Possibly pad to the minimum packet length and/or append a dummy FCS
796 * and/or pad to the next multiple of the detected alignment unit.
798 const unsigned pad_len
= eth_pad_len
+ erf_pad_len
;
800 dag_tx_stream_copy_bytes64(p
->fd
, pd
->dag_stream
, (uint8_t *)tx_pad
, pad_len
) < 0)
805 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
806 errno
, "dag_tx_stream_copy_bytes64 %s", p
->opt
.device
);
811 dag_activate_tx(pcap_t
*p
)
813 struct pcap_dag
*pd
= p
->priv
;
815 const char * env
= getenv(ENV_TX_IFACE
);
816 int32_t iface
= strtouint31(env
, 0);
818 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
819 "%s: failed parsing %s value \"%s\"",
820 __func__
, ENV_TX_IFACE
, env
);
823 uint32_t ifcount
= dag_config_get_interface_count(pd
->dag_ref
);
824 if ((uint32_t)iface
>= ifcount
) {
825 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
826 "%s: invalid %s value %u: this card has %u interface(s)",
827 __func__
, ENV_TX_IFACE
, iface
, ifcount
);
830 pd
->tx_iface
= (uint8_t)iface
;
832 const dag_card_inf_t
*inf
= dag_pciinfo(p
->fd
);
834 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
835 errno
, "dag_pciinfo");
839 if (inf
->device_code
== PCI_DEVICE_ID_VDAG
) {
840 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
841 "vDAG Tx streams are not supported");
845 // Determine the correct alignment/padding size for the card.
846 switch(inf
->device_code
) {
847 case PCI_DEVICE_ID_DAG9_2X2
:
848 case PCI_DEVICE_ID_DAG9_2SX2
:
849 pd
->tx_align_bytes
= ALIGN_BYTES_9_2
;
852 pd
->tx_align_bytes
= ALIGN_BYTES_DEFAULT
;
855 // Read the TERF FCS size for later use by dag_inject().
856 dag_component_t cfg_comp
= dag_component_get_subcomponent(
857 pd
->dag_root
, kComponentTerf
, 0);
859 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
860 "%s: dag_component_get_subcomponent",
865 attr_uuid_t cfg_uuid
= dag_component_get_config_attribute_uuid(
866 cfg_comp
, kUint32AttributeTerfStripCrc
);
867 if (cfg_uuid
== kNullAttributeUuid
) {
868 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
869 "%s: dag_component_get_config_attribute_uuid",
875 dag_err_t cfg_err
= dag_config_get_uint32_attribute_ex(pd
->dag_ref
,
876 cfg_uuid
, &cfg_uint32
);
877 if (cfg_err
!= kDagErrNone
) {
878 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
879 "%s: dag_config_get_uint32_attribute_ex",
884 switch (cfg_uint32
) {
886 pd
->terf_fcs_bytes
= 0;
889 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
890 "TERF FCS is configured to 16 bits, is this Ethernet?");
893 pd
->terf_fcs_bytes
= 4;
896 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
897 "failed reading TERF FCS configuration");
902 * TODO: It would be nice to verify that the Tx port is
903 * configured for 32-bit Tx FCS, but it is not trivial to
904 * tell the exact subcomponent that has the attribute.
911 * Get a handle for a live capture from the given DAG device. The promisc
912 * flag is ignored because DAG cards are always promiscuous. The to_ms
913 * parameter is used in setting the API polling parameters.
917 static int dag_activate(pcap_t
* p
)
919 struct pcap_dag
*pd
= p
->priv
;
922 char * device
= p
->opt
.device
;
925 struct timeval maxwait
;
929 * dag_create() has validated the device name syntax and stored the
930 * parsed device and stream numbers to p->priv. Validate these values
933 if (pd
->dag_devnum
>= DAG_MAX_BOARDS
) {
934 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
935 "DAG device number %d is too large", pd
->dag_devnum
);
936 ret
= PCAP_ERROR_NO_SUCH_DEVICE
;
939 if (pd
->dag_stream
>= DAG_STREAM_MAX
) {
940 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
941 "DAG stream number %d is too large", pd
->dag_stream
);
942 ret
= PCAP_ERROR_NO_SUCH_DEVICE
;
945 #ifndef ENABLE_DAG_TX
946 if (TX_ONLY(pd
->dag_stream
)) {
948 * dag_findalldevs() does not return any Tx streams, so
949 * PCAP_ERROR_NO_SUCH_DEVICE is more consistent than
950 * PCAP_ERROR_CAPTURE_NOTSUP.
952 ret
= PCAP_ERROR_NO_SUCH_DEVICE
;
953 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "%s: tx (odd numbered) streams not supported for capture", __func__
);
956 #endif // ENABLE_DAG_TX
958 /* setup device parameters */
959 if((pd
->dag_ref
= dag_config_init(device
)) == NULL
) {
961 * XXX - does this reliably set errno?
963 if (errno
== ENOENT
) {
965 * There's nothing more to say, so clear
968 ret
= PCAP_ERROR_NO_SUCH_DEVICE
;
970 } else if (errno
== EPERM
|| errno
== EACCES
) {
971 ret
= PCAP_ERROR_PERM_DENIED
;
972 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
973 "Attempt to open %s failed with %s - additional privileges may be required",
974 device
, (errno
== EPERM
) ? "EPERM" : "EACCES");
977 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
978 errno
, "dag_config_init %s", device
);
983 if((p
->fd
= dag_config_get_card_fd(pd
->dag_ref
)) < 0) {
985 * XXX - does this reliably set errno?
988 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
989 errno
, "dag_config_get_card_fd %s", device
);
993 /* Open requested stream. Can fail if already locked or on error */
994 if (dag_attach_stream64(p
->fd
, pd
->dag_stream
, 0, 0) < 0) {
995 if (errno
== ENOMEM
) {
996 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
997 "dag%u has no memory allocated to %s stream %u",
998 pd
->dag_devnum
, RXTX_STR(pd
->dag_stream
), pd
->dag_stream
);
1000 * dag_findalldevs() does not return streams that do
1001 * not have buffer memory, so PCAP_ERROR_NO_SUCH_DEVICE
1002 * is more consistent than PCAP_ERROR_CAPTURE_NOTSUP.
1004 ret
= PCAP_ERROR_NO_SUCH_DEVICE
;
1006 } else if (errno
== EINVAL
) {
1007 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1008 "dag%u has no %s stream %u",
1009 pd
->dag_devnum
, RXTX_STR(pd
->dag_stream
), pd
->dag_stream
);
1010 ret
= PCAP_ERROR_NO_SUCH_DEVICE
;
1014 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1015 errno
, "dag_attach_stream64");
1019 /* Try to find Stream Drop attribute */
1020 pd
->drop_attr
= kNullAttributeUuid
;
1021 pd
->dag_root
= dag_config_get_root_component(pd
->dag_ref
);
1022 if ( dag_component_get_subcomponent(pd
->dag_root
, kComponentStreamFeatures
, 0) )
1024 pd
->drop_attr
= dag_config_get_indexed_attribute_uuid(pd
->dag_ref
, kUint32AttributeStreamDropCount
, pd
->dag_stream
);
1025 if (pd
->drop_attr
!= kNullAttributeUuid
)
1026 pd
->drop_base
= dag_config_get_uint64_attribute(
1027 pd
->dag_ref
, pd
->drop_attr
);
1030 /* Set up default poll parameters for stream
1031 * Can be overridden by pcap_set_nonblock()
1033 if (dag_get_stream_poll64(p
->fd
, pd
->dag_stream
,
1034 &mindata
, &maxwait
, &poll
) < 0) {
1036 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1037 errno
, "dag_get_stream_poll64");
1041 /* Use the poll time as the required select timeout for callers
1042 * who are using select()/etc. in an event loop waiting for
1043 * packets to arrive.
1045 pd
->required_select_timeout
= poll
;
1046 p
->required_select_timeout
= &pd
->required_select_timeout
;
1049 * Turn a negative snapshot value (invalid), a snapshot value of
1050 * 0 (unspecified), or a value bigger than the normal maximum
1051 * value, into the maximum allowed value.
1053 * If some application really *needs* a bigger snapshot
1054 * length, we should just increase MAXIMUM_SNAPLEN.
1056 if (p
->snapshot
<= 0 || p
->snapshot
> MAXIMUM_SNAPLEN
)
1057 p
->snapshot
= MAXIMUM_SNAPLEN
;
1059 if (p
->opt
.immediate
) {
1060 /* Call callback immediately.
1061 * XXX - is this the right way to p this?
1065 /* Amount of data to collect in Bytes before calling callbacks.
1066 * Important for efficiency, but can introduce latency
1067 * at low packet rates if to_ms not set!
1072 /* Obey opt.timeout (was to_ms) if supplied. This is a good idea!
1073 * Recommend 10-100ms. Calls will time out even if no data arrived.
1075 maxwait
.tv_sec
= p
->opt
.timeout
/1000;
1076 maxwait
.tv_usec
= (p
->opt
.timeout
%1000) * 1000;
1078 if (dag_set_stream_poll64(p
->fd
, pd
->dag_stream
,
1079 mindata
, &maxwait
, &poll
) < 0) {
1081 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1082 errno
, "dag_set_stream_poll64");
1086 if(dag_start_stream(p
->fd
, pd
->dag_stream
) < 0) {
1088 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1089 errno
, "dag_start_stream %s", device
);
1094 * Important! You have to ensure bottom is properly
1095 * initialized to zero on startup, it won't give you
1096 * a compiler warning if you make this mistake!
1098 pd
->dag_mem_bottom
= 0;
1099 pd
->dag_mem_top
= 0;
1102 * Find out how many FCS bits we should strip.
1103 * Assume Rx FCS length to be 32 bits unless the user has
1104 * requested a different value, in which case validate it well.
1106 s
= getenv(ENV_RX_FCS_BITS
);
1107 switch ((n
= strtouint31(s
, 32))) {
1111 pd
->dag_fcs_bits
= n
;
1115 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1116 "%s %s: invalid %s value (%s) in environment",
1117 __func__
, device
, ENV_RX_FCS_BITS
, s
);
1122 * Did the user request that they not be stripped?
1124 s
= getenv(ENV_RX_FCS_NOSTRIP
);
1125 switch ((n
= strtouint31(s
, 0))) {
1129 /* Yes. Note the number of 16-bit words that will be
1131 p
->linktype_ext
= LT_FCS_DATALINK_EXT(pd
->dag_fcs_bits
/16);
1133 /* And don't strip them. */
1134 pd
->dag_fcs_bits
= 0;
1138 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1139 "%s %s: invalid %s value (%s) in environment",
1140 __func__
, device
, ENV_RX_FCS_NOSTRIP
, s
);
1144 if (TX_ONLY(pd
->dag_stream
) && dag_activate_tx(p
) < 0) {
1149 pd
->dag_timeout
= p
->opt
.timeout
;
1151 if (dag_get_datalink(p
) < 0) {
1158 if (new_pcap_dag(p
) < 0) {
1160 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1161 errno
, "new_pcap_dag %s", device
);
1166 * "select()" and "poll()" don't work on DAG device descriptors.
1168 p
->selectable_fd
= -1;
1170 p
->read_op
= RX_ONLY(pd
->dag_stream
) ?
1173 p
->inject_op
= TX_ONLY(pd
->dag_stream
) ?
1176 p
->setfilter_op
= RX_ONLY(pd
->dag_stream
) ?
1177 pcapint_install_bpf_program
:
1178 dag_install_bpf_program_notimpl
;
1179 p
->setdirection_op
= NULL
; /* Not implemented.*/
1180 p
->set_datalink_op
= dag_set_datalink
;
1181 p
->getnonblock_op
= RX_ONLY(pd
->dag_stream
) ?
1182 pcapint_getnonblock_fd
:
1183 dag_getnonblock_fd_notimpl
;
1184 p
->setnonblock_op
= RX_ONLY(pd
->dag_stream
) ?
1186 dag_setnonblock_notimpl
;
1187 p
->stats_op
= RX_ONLY(pd
->dag_stream
) ?
1190 p
->cleanup_op
= dag_platform_cleanup
;
1191 pd
->stat
.ps_drop
= 0;
1192 pd
->stat
.ps_recv
= 0;
1193 pd
->stat
.ps_ifdrop
= 0;
1197 if (dag_stop_stream(p
->fd
, pd
->dag_stream
) < 0) {
1198 fprintf(stderr
,"dag_stop_stream: %s\n", strerror(errno
));
1202 if (dag_detach_stream(p
->fd
, pd
->dag_stream
) < 0)
1203 fprintf(stderr
,"dag_detach_stream: %s\n", strerror(errno
));
1206 dag_config_dispose(pd
->dag_ref
);
1208 * Note: we don't need to call close(p->fd) or dag_close(p->fd),
1209 * as dag_config_dispose(pd->dag_ref) does this.
1211 * Set p->fd to -1 to make sure that's not done.
1218 pcapint_cleanup_live_common(p
);
1223 pcap_t
*dag_create(const char *device
, char *ebuf
, int *is_ours
)
1232 * The nominal libpcap DAG device name format is either "dagN" or
1233 * "dagN:M", as returned from dag_findalldevs().
1235 * First attempt the most basic syntax validation. If the device string
1236 * does not look like a potentially valid DAG device name, reject it
1237 * silently to have pcap_create() try another capture source type.
1241 /* Does this look like a DAG device? */
1243 /* Does it begin with "dag"? */
1244 if (strncmp(cp
, "dag", 3) != 0) {
1245 /* Nope, doesn't begin with "dag" */
1248 /* Yes - is "dag" followed by a number from 0 to DAG_MAX_BOARDS-1 */
1250 devnum
= strtol(cp
, &cpend
, 10);
1251 if (*cpend
== ':') {
1252 /* Followed by a stream number. */
1253 stream
= strtol(++cpend
, &cpend
, 10);
1256 if (cpend
== cp
|| *cpend
!= '\0') {
1257 /* Not followed by a number. */
1262 * OK, it's probably ours, validate the syntax further. From now on
1263 * reject the device string authoritatively with an error message to
1264 * have pcap_create() propagate the failure. Validate the device and
1265 * stream number ranges loosely only.
1268 snprintf (ebuf
, PCAP_ERRBUF_SIZE
,
1269 "DAG device name \"%s\" is invalid", device
);
1271 if (devnum
< 0 || devnum
> INT_MAX
) {
1272 /* Followed by a non-valid number. */
1276 if (stream
< 0 || stream
> INT_MAX
) {
1277 /* Followed by a non-valid stream number. */
1282 * The syntax validation done so far is lax enough to accept some
1283 * device strings that are not actually acceptable in libpcap as
1284 * defined above. The device strings that are acceptable in libpcap
1285 * are a strict subset of the device strings that are acceptable in
1286 * dag_parse_name(), thus using the latter for validation in libpcap
1287 * would not work reliably. Instead from the detected device and
1288 * stream numbers produce the acceptable device string(s) and require
1289 * the input device string to match an acceptable string exactly.
1291 char buf
[DAGNAME_BUFSIZE
];
1292 snprintf(buf
, sizeof(buf
), "dag%ld:%ld", devnum
, stream
);
1293 char acceptable
= ! strcmp(device
, buf
);
1294 if (! acceptable
&& stream
== 0) {
1295 snprintf(buf
, sizeof(buf
), "dag%ld", devnum
);
1296 acceptable
= ! strcmp(device
, buf
);
1302 * The device string syntax is acceptable, save the device and stream
1303 * numbers for dag_activate(), which will do semantic and run-time
1304 * validation and possibly reject the pcap_t using more specific error
1308 p
= PCAP_CREATE_COMMON(ebuf
, struct pcap_dag
);
1312 p
->activate_op
= dag_activate
;
1315 * We claim that we support microsecond and nanosecond time
1318 * XXX Our native precision is 2^-32s, but libpcap doesn't support
1319 * power of two precisions yet. We can convert to either MICRO or NANO.
1321 p
->tstamp_precision_list
= malloc(2 * sizeof(u_int
));
1322 if (p
->tstamp_precision_list
== NULL
) {
1323 pcapint_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
1328 p
->tstamp_precision_list
[0] = PCAP_TSTAMP_PRECISION_MICRO
;
1329 p
->tstamp_precision_list
[1] = PCAP_TSTAMP_PRECISION_NANO
;
1330 p
->tstamp_precision_count
= 2;
1331 struct pcap_dag
*pd
= p
->priv
;
1332 pd
->dag_devnum
= (int)devnum
;
1333 pd
->dag_stream
= (int)stream
;
1338 dag_stats(pcap_t
*p
, struct pcap_stat
*ps
) {
1339 struct pcap_dag
*pd
= p
->priv
;
1340 uint64_t stream_drop
;
1341 dag_err_t dag_error
;
1344 * Packet records received (ps_recv) are counted in dag_read().
1345 * Packet records dropped (ps_drop) are read from Stream Drop attribute if present,
1346 * otherwise integrate the ERF Header lctr counts (if available) in dag_read().
1347 * We are reporting that no records are dropped by the card/driver (ps_ifdrop).
1350 if(pd
->drop_attr
!= kNullAttributeUuid
) {
1351 /* Note this counter is cleared at start of capture and will wrap at UINT_MAX.
1352 * The application is responsible for polling ps_drop frequently enough
1353 * to detect each wrap and integrate total drop with a wider counter */
1354 if ((dag_error
= dag_config_get_uint64_attribute_ex(pd
->dag_ref
, pd
->drop_attr
, &stream_drop
)) == kDagErrNone
) {
1355 pd
->stat
.ps_drop
= (u_int
)(stream_drop
- pd
->drop_base
);
1357 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "reading stream drop attribute: %s",
1358 dag_config_strerror(dag_error
));
1369 dag_device_description(const unsigned dagid
)
1371 static char buf
[128];
1372 snprintf(buf
, sizeof(buf
), "alias for dag%u:0", dagid
);
1377 dag_stream_short_description(const unsigned stream
)
1379 static char buf
[128];
1380 snprintf(buf
, sizeof(buf
), "%s stream %u", RXTX_STR(stream
), stream
);
1385 dag_stream_long_description(const unsigned stream
, const dag_size_t bufsize
,
1386 const dag_card_inf_t
* inf
)
1388 static char buf
[256];
1389 int done
= snprintf(buf
, sizeof(buf
),
1390 "%s stream %u, %" PRIu64
" MiB, %s",
1393 bufsize
/ 1024 / 1024,
1394 inf
? dag_device_name(inf
->device_code
, 1) : "N/A");
1395 if (inf
->device_code
!= PCI_DEVICE_ID_VDAG
)
1396 snprintf(buf
+ done
, sizeof(buf
) - done
,
1398 (inf
&& inf
->brd_rev
< 26) ? ('A' + inf
->brd_rev
) : '?',
1399 inf
? inf
->bus_id
: "N/A");
1404 * Add all DAG devices.
1407 dag_findalldevs(pcap_if_list_t
*devlistp
, char *errbuf
)
1411 const char * description
;
1412 int stream
, rxstreams
;
1413 // A DAG card associates a link status with each physical port, but not
1414 // with the data streams. The number of ports is a matter of hardware,
1415 // the number of streams and how each stream associates with zero or
1416 // more ports is a matter of how the user configures the card. In this
1417 // context libpcap uses the streams only (i.e. "dag0" is a shorthand
1418 // for "dag0:0"), thus the notion of link status does not apply to the
1419 // resulting libpcap DAG capture devices.
1420 const bpf_u_int32 flags
= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE
;
1421 FILE * sysfsinfo
= NULL
;
1423 /* Try all the DAGs 0-DAG_MAX_BOARDS */
1424 for (c
= 0; c
< DAG_MAX_BOARDS
; c
++) {
1425 char name
[DAGNAME_BUFSIZE
]; // libpcap device
1426 snprintf(name
, sizeof(name
), "dag%d", c
);
1427 char dagname
[DAGNAME_BUFSIZE
]; // DAG API device
1428 snprintf(dagname
, sizeof(dagname
), "/dev/dag%d", c
);
1429 if ( (dagfd
= dag_open(dagname
)) >= 0 ) {
1430 // Do not add a shorthand device for stream 0 (dagN) yet -- the
1431 // user can disable any stream in the card configuration.
1432 const dag_card_inf_t
* inf
= dag_pciinfo(dagfd
); // NULL is fine
1433 // The count includes existing streams that have no buffer memory.
1434 rxstreams
= dag_rx_get_stream_count(dagfd
);
1435 if (rxstreams
< 0) {
1436 pcapint_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1437 errno
, "dag_rx_get_stream_count");
1441 #ifdef ENABLE_DAG_TX
1442 txstreams
= dag_tx_get_stream_count(dagfd
);
1443 if (txstreams
< 0) {
1444 pcapint_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1445 errno
, "dag_tx_get_stream_count");
1448 #endif // ENABLE_DAG_TX
1450 stream
< DAG_STREAM_MAX
&& (rxstreams
> 0 || txstreams
> 0);
1452 #ifndef ENABLE_DAG_TX
1453 if (TX_ONLY(stream
))
1455 #endif // ENABLE_DAG_TX
1457 * dag_attach_stream64() was used before to test if the
1458 * stream exists, but it is not the best tool for the
1459 * job because it tries to lock the stream exclusively.
1460 * If the stream is already locked by another process,
1461 * it fails with EBUSY, otherwise it creates a race
1462 * condition for other processes that may be trying to
1463 * lock the same stream at the same time. Therefore
1464 * dag_get_stream_buffer_size64() seems to be a better
1467 dag_ssize_t bufsize
= dag_get_stream_buffer_size64(dagfd
, stream
);
1469 continue; // Does not exist.
1470 // Only streams with buffer memory are usable.
1472 (RX_ONLY(stream
) || inf
->device_code
!= PCI_DEVICE_ID_VDAG
)) {
1473 description
= dag_device_description (c
);
1474 // a conditional shorthand device
1476 pcapint_add_dev(devlistp
, name
, flags
, description
, errbuf
) == NULL
)
1478 // and the stream device
1479 snprintf(name
, sizeof(name
), "dag%d:%d", c
, stream
);
1480 description
= dag_stream_long_description(stream
,
1481 dag_get_stream_buffer_size64(dagfd
, stream
), inf
);
1482 if (pcapint_add_dev(devlistp
, name
, flags
, description
, errbuf
) == NULL
) {
1486 if (RX_ONLY(stream
))
1493 } else if (errno
== EACCES
) {
1494 // The device exists, but the current user privileges are not
1495 // sufficient for dag_open().
1496 // Do not add a shorthand device for stream 0 yet -- same as above.
1497 // Try enumerating the streams using sysfs. The file lists
1498 // all streams (Rx and Tx) that have non-zero amount of buffer
1500 char sysfspath
[PATH_MAX
];
1501 snprintf(sysfspath
, sizeof(sysfspath
), "/sys/devices/virtual/dag/%s/info", name
);
1502 if ((sysfsinfo
= fopen(sysfspath
, "r"))) {
1504 while (fgets(linebuf
, sizeof(linebuf
), sysfsinfo
))
1505 if (1 == sscanf(linebuf
, "Stream %u:", &stream
)) {
1506 #ifndef ENABLE_DAG_TX
1507 if (TX_ONLY(stream
))
1509 #endif // ENABLE_DAG_TX
1510 // a conditional shorthand device
1511 description
= dag_device_description(c
);
1513 pcapint_add_dev(devlistp
, name
, flags
, description
, errbuf
) == NULL
)
1515 // and the stream device
1516 snprintf(name
, sizeof(name
), "dag%u:%u", c
, stream
);
1517 // TODO: Parse and describe the buffer size too.
1518 description
= dag_stream_short_description(stream
);
1519 if (pcapint_add_dev(devlistp
, name
, flags
, description
, errbuf
) == NULL
)
1525 } // errno == EACCES
1539 dag_set_datalink(pcap_t
*p
, int dlt
)
1547 dag_setnonblock(pcap_t
*p
, int nonblock
)
1549 struct pcap_dag
*pd
= p
->priv
;
1551 struct timeval maxwait
;
1552 struct timeval poll
;
1555 * Set non-blocking mode on the FD.
1556 * XXX - is that necessary? If not, don't bother calling it,
1557 * and have a "dag_getnonblock()" function that looks at
1560 if (pcapint_setnonblock_fd(p
, nonblock
) < 0)
1563 if (dag_get_stream_poll64(p
->fd
, pd
->dag_stream
,
1564 &mindata
, &maxwait
, &poll
) < 0) {
1565 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1566 errno
, "dag_get_stream_poll64");
1570 /* Amount of data to collect in Bytes before calling callbacks.
1571 * Important for efficiency, but can introduce latency
1572 * at low packet rates if to_ms not set!
1579 if (dag_set_stream_poll64(p
->fd
, pd
->dag_stream
,
1580 mindata
, &maxwait
, &poll
) < 0) {
1581 pcapint_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1582 errno
, "dag_set_stream_poll64");
1587 pd
->dag_flags
|= DAGF_NONBLOCK
;
1589 pd
->dag_flags
&= ~DAGF_NONBLOCK
;
1595 dag_get_datalink(pcap_t
*p
)
1597 struct pcap_dag
*pd
= p
->priv
;
1600 * There seems to be no trivial way to tell which ERF type(s) a Tx
1601 * stream would accept. Let's assume ERF_TYPE_ETH would work, which
1602 * in libpcap terms means using DLT_EN10MB.
1604 if (TX_ONLY(pd
->dag_stream
))
1605 return (p
->linktype
= DLT_EN10MB
);
1607 int index
=0, dlt_index
=0;
1610 memset(types
, 0, 255);
1612 if (p
->dlt_list
== NULL
&& (p
->dlt_list
= malloc(255*sizeof(*(p
->dlt_list
)))) == NULL
) {
1613 pcapint_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
1620 /* Get list of possible ERF types for this card */
1621 if (dag_get_stream_erf_types(p
->fd
, pd
->dag_stream
, types
, 255) < 0) {
1622 pcapint_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
1623 errno
, "dag_get_stream_erf_types");
1627 while (types
[index
]) {
1629 switch((types
[index
] & ERF_TYPE_MASK
)) {
1631 case ERF_TYPE_HDLC_POS
:
1632 case ERF_TYPE_COLOR_HDLC_POS
:
1633 case ERF_TYPE_DSM_COLOR_HDLC_POS
:
1634 case ERF_TYPE_COLOR_HASH_POS
:
1635 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1636 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1637 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1639 p
->linktype
= DLT_CHDLC
;
1643 case ERF_TYPE_COLOR_ETH
:
1644 case ERF_TYPE_DSM_COLOR_ETH
:
1645 case ERF_TYPE_COLOR_HASH_ETH
:
1647 * This is (presumably) a real Ethernet capture; give it a
1648 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
1649 * that an application can let you choose it, in case you're
1650 * capturing DOCSIS traffic that a Cisco Cable Modem
1651 * Termination System is putting out onto an Ethernet (it
1652 * doesn't put an Ethernet header onto the wire, it puts raw
1653 * DOCSIS frames out on the wire inside the low-level
1654 * Ethernet framing).
1656 p
->dlt_list
[dlt_index
++] = DLT_EN10MB
;
1657 p
->dlt_list
[dlt_index
++] = DLT_DOCSIS
;
1659 p
->linktype
= DLT_EN10MB
;
1664 case ERF_TYPE_MC_ATM
:
1665 case ERF_TYPE_MC_AAL5
:
1666 p
->dlt_list
[dlt_index
++] = DLT_ATM_RFC1483
;
1667 p
->dlt_list
[dlt_index
++] = DLT_SUNATM
;
1669 p
->linktype
= DLT_ATM_RFC1483
;
1672 case ERF_TYPE_COLOR_MC_HDLC_POS
:
1673 case ERF_TYPE_MC_HDLC
:
1674 p
->dlt_list
[dlt_index
++] = DLT_CHDLC
;
1675 p
->dlt_list
[dlt_index
++] = DLT_PPP_SERIAL
;
1676 p
->dlt_list
[dlt_index
++] = DLT_FRELAY
;
1677 p
->dlt_list
[dlt_index
++] = DLT_MTP2
;
1678 p
->dlt_list
[dlt_index
++] = DLT_MTP2_WITH_PHDR
;
1679 p
->dlt_list
[dlt_index
++] = DLT_LAPD
;
1681 p
->linktype
= DLT_CHDLC
;
1685 p
->dlt_list
[dlt_index
++] = DLT_RAW
;
1686 p
->dlt_list
[dlt_index
++] = DLT_IPV4
;
1688 p
->linktype
= DLT_RAW
;
1692 p
->dlt_list
[dlt_index
++] = DLT_RAW
;
1693 p
->dlt_list
[dlt_index
++] = DLT_IPV6
;
1695 p
->linktype
= DLT_RAW
;
1698 case ERF_TYPE_LEGACY
:
1699 case ERF_TYPE_MC_RAW
:
1700 case ERF_TYPE_MC_RAW_CHANNEL
:
1701 case ERF_TYPE_IP_COUNTER
:
1702 case ERF_TYPE_TCP_FLOW_COUNTER
:
1703 case ERF_TYPE_INFINIBAND
:
1704 case ERF_TYPE_RAW_LINK
:
1705 case ERF_TYPE_INFINIBAND_LINK
:
1708 /* Libpcap cannot deal with these types yet */
1709 /* Add no 'native' DLTs, but still covered by DLT_ERF */
1716 p
->dlt_list
[dlt_index
++] = DLT_ERF
;
1718 p
->dlt_count
= dlt_index
;
1721 p
->linktype
= DLT_ERF
;
1728 * This libpcap build supports only DAG cards, not regular network
1733 * There are no regular interfaces, just DAG interfaces.
1736 pcapint_platform_finddevs(pcap_if_list_t
*devlistp _U_
, char *errbuf _U_
)
1742 * Attempts to open a regular interface fail.
1745 pcapint_create_interface(const char *device _U_
, char *errbuf
)
1747 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, PCAP_ENODEV_MESSAGE
, "DAG");
1752 * Libpcap version string.
1755 pcap_lib_version(void)
1757 return (PCAP_VERSION_STRING
" (DAG-only)");