]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-snf.c
12 #include <netinet/in.h>
14 #include <sys/socket.h>
15 #include <sys/types.h>
24 * Private data for capturing on SNF devices.
27 snf_handle_t snf_handle
; /* opaque device handle */
28 snf_ring_t snf_ring
; /* opaque device ring handle */
34 snf_set_datalink(pcap_t
*p
, int dlt
)
41 snf_pcap_stats(pcap_t
*p
, struct pcap_stat
*ps
)
43 struct snf_ring_stats stats
;
46 if ((rc
= snf_ring_getstats(ps
->snf_ring
, &stats
))) {
47 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "snf_get_stats: %s",
51 ps
->ps_recv
= stats
.ring_pkt_recv
+ stats
.ring_pkt_overflow
;
52 ps
->ps_drop
= stats
.ring_pkt_overflow
;
53 ps
->ps_ifdrop
= stats
.nic_pkt_overflow
+ stats
.nic_pkt_bad
;
58 snf_platform_cleanup(pcap_t
*p
)
60 struct pcap_snf
*ps
= p
->priv
;
65 snf_ring_close(ps
->snf_ring
);
66 snf_close(ps
->snf_handle
);
67 pcap_cleanup_live_common(p
);
71 snf_getnonblock(pcap_t
*p
, char *errbuf
)
73 struct pcap_snf
*ps
= p
->priv
;
75 return (ps
->snf_timeout
== 0);
79 snf_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
81 struct pcap_snf
*ps
= p
->priv
;
86 if (p
->opt
.timeout
<= 0)
87 ps
->snf_timeout
= -1; /* forever */
89 ps
->snf_timeout
= p
->opt
.timeout
;
94 #define _NSEC_PER_SEC 1000000000
98 snf_timestamp_to_timeval(const int64_t ts_nanosec
)
103 return (struct timeval
) { 0, 0 };
104 tv
.tv_sec
= ts_nanosec
/ _NSEC_PER_SEC
;
105 tv
.tv_usec
= (ts_nanosec
% _NSEC_PER_SEC
) / 1000;
110 snf_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
112 struct pcap_snf
*ps
= p
->priv
;
113 struct pcap_pkthdr hdr
;
114 int i
, flags
, err
, caplen
, n
;
115 struct snf_recv_req req
;
121 while (n
< cnt
|| PACKET_COUNT_IS_UNLIMITED(cnt
)) {
123 * Has "pcap_breakloop()" been called?
134 err
= snf_ring_recv(ps
->snf_ring
, ps
->snf_timeout
, &req
);
137 if (err
== EBUSY
|| err
== EAGAIN
)
142 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "snf_read: %s",
149 if (caplen
> p
->snapshot
)
150 caplen
= p
->snapshot
;
152 if ((p
->fcode
.bf_insns
== NULL
) ||
153 bpf_filter(p
->fcode
.bf_insns
, req
.pkt_addr
, req
.length
, caplen
)) {
154 hdr
.ts
= snf_timestamp_to_timeval(req
.timestamp
);
156 hdr
.len
= req
.length
;
157 callback(user
, &hdr
, req
.pkt_addr
);
165 snf_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
170 strncpy(p
->errbuf
, "setfilter: No filter specified",
175 /* Make our private copy of the filter */
177 if (install_bpf_program(p
, fp
) < 0)
184 snf_inject(pcap_t
*p
, const void *buf _U_
, size_t size _U_
)
186 strlcpy(p
->errbuf
, "Sending packets isn't supported with snf",
192 snf_activate(pcap_t
* p
)
194 struct pcap_snf
*ps
= p
->priv
;
195 char *device
= p
->opt
.source
;
196 const char *nr
= NULL
;
200 if (device
== NULL
) {
201 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
202 "device is NULL: %s", pcap_strerror(errno
));
206 /* In Libpcap, we set pshared by default if NUM_RINGS is set to > 1.
207 * Since libpcap isn't thread-safe */
208 if ((nr
= getenv("SNF_NUM_RINGS")) && *nr
&& atoi(nr
) > 1)
209 flags
|= SNF_F_PSHARED
;
213 err
= snf_open(ps
->snf_boardnum
,
214 0, /* let SNF API parse SNF_NUM_RINGS, if set */
215 NULL
, /* default RSS, or use SNF_RSS_FLAGS env */
216 0, /* default to SNF_DATARING_SIZE from env */
217 flags
, /* may want pshared */
220 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
221 "snf_open failed: %s", pcap_strerror(err
));
225 err
= snf_ring_open(ps
->snf_handle
, &ps
->snf_ring
);
227 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
228 "snf_ring_open failed: %s", pcap_strerror(err
));
232 if (p
->opt
.timeout
<= 0)
233 ps
->snf_timeout
= -1;
235 ps
->snf_timeout
= p
->opt
.timeout
;
237 err
= snf_start(ps
->snf_handle
);
239 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
240 "snf_start failed: %s", pcap_strerror(err
));
245 * "select()" and "poll()" don't work on snf descriptors.
247 p
->selectable_fd
= -1;
248 p
->linktype
= DLT_EN10MB
;
249 p
->read_op
= snf_read
;
250 p
->inject_op
= snf_inject
;
251 p
->setfilter_op
= snf_setfilter
;
252 p
->setdirection_op
= NULL
; /* Not implemented.*/
253 p
->set_datalink_op
= snf_set_datalink
;
254 p
->getnonblock_op
= snf_getnonblock
;
255 p
->setnonblock_op
= snf_setnonblock
;
256 p
->stats_op
= snf_pcap_stats
;
257 p
->cleanup_op
= snf_platform_cleanup
;
262 snf_findalldevs(pcap_if_t
**devlistp
, char *errbuf
)
265 * There are no platform-specific devices since each device
266 * exists as a regular Ethernet device.
272 snf_create(const char *device
, char *ebuf
, int *is_ours
)
276 struct snf_ifaddrs
*ifaddrs
, *ifa
;
280 if (snf_init(SNF_VERSION_API
)) {
281 /* Can't initialize the API, so no SNF devices */
287 * Match a given interface name to our list of interface names, from
288 * which we can obtain the intended board number
290 if (snf_getifaddrs(&ifaddrs
) || ifaddrs
== NULL
) {
291 /* Can't get SNF addresses */
295 devlen
= strlen(device
) + 1;
298 if (!strncmp(device
, ifa
->snf_ifa_name
, devlen
)) {
299 boardnum
= ifa
->snf_ifa_boardnum
;
302 ifa
= ifa
->snf_ifa_next
;
304 snf_freeifaddrs(ifaddrs
);
308 * If we can't find the device by name, support the name "snfX"
309 * and "snf10gX" where X is the board number.
311 if (sscanf(device
, "snf10g%d", &boardnum
) != 1 &&
312 sscanf(device
, "snf%d", &boardnum
) != 1) {
313 /* Nope, not a supported name */
319 /* OK, it's probably ours. */
322 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_snf
));
327 p
->activate_op
= snf_activate
;
328 ps
->snf_boardnum
= boardnum
;