]>
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>
22 snf_set_datalink(pcap_t
*p
, int dlt
)
29 snf_pcap_stats(pcap_t
*p
, struct pcap_stat
*ps
)
31 struct snf_ring_stats stats
;
34 if ((rc
= snf_ring_getstats(p
->md
.snf_ring
, &stats
))) {
35 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "snf_get_stats: %s",
39 ps
->ps_recv
= stats
.ring_pkt_recv
+ stats
.ring_pkt_overflow
;
40 ps
->ps_drop
= stats
.ring_pkt_overflow
;
41 ps
->ps_ifdrop
= stats
.nic_pkt_overflow
+ stats
.nic_pkt_bad
;
46 snf_platform_cleanup(pcap_t
*p
)
51 snf_ring_close(p
->md
.snf_ring
);
52 snf_close(p
->md
.snf_handle
);
53 pcap_cleanup_live_common(p
);
57 snf_getnonblock(pcap_t
*p
, char *errbuf
)
59 return (p
->md
.snf_timeout
== 0);
63 snf_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
66 p
->md
.snf_timeout
= 0;
68 if (p
->md
.timeout
<= 0)
69 p
->md
.snf_timeout
= -1; /* forever */
71 p
->md
.snf_timeout
= p
->md
.timeout
;
76 #define _NSEC_PER_SEC 1000000000
80 snf_timestamp_to_timeval(const int64_t ts_nanosec
)
85 return (struct timeval
) { 0, 0 };
86 tv
.tv_sec
= ts_nanosec
/ _NSEC_PER_SEC
;
87 tv
.tv_usec
= (ts_nanosec
% _NSEC_PER_SEC
) / 1000;
92 snf_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
94 struct pcap_pkthdr hdr
;
95 int i
, flags
, err
, caplen
, n
;
96 struct snf_recv_req req
;
102 while (n
< cnt
|| cnt
< 0) {
104 * Has "pcap_breakloop()" been called?
115 err
= snf_ring_recv(p
->md
.snf_ring
, p
->md
.snf_timeout
, &req
);
118 if (err
== EBUSY
|| err
== EAGAIN
)
123 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "snf_read: %s",
130 if (caplen
> p
->snapshot
)
131 caplen
= p
->snapshot
;
133 if ((p
->fcode
.bf_insns
== NULL
) ||
134 bpf_filter(p
->fcode
.bf_insns
, req
.pkt_addr
, req
.length
, caplen
)) {
135 hdr
.ts
= snf_timestamp_to_timeval(req
.timestamp
);
137 hdr
.len
= req
.length
;
138 callback(user
, &hdr
, req
.pkt_addr
);
146 snf_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
151 strncpy(p
->errbuf
, "setfilter: No filter specified",
156 /* Make our private copy of the filter */
158 if (install_bpf_program(p
, fp
) < 0)
167 snf_inject(pcap_t
*p
, const void *buf _U_
, size_t size _U_
)
169 strlcpy(p
->errbuf
, "Sending packets isn't supported with snf",
175 snf_activate(pcap_t
* p
)
177 char *device
= p
->opt
.source
;
178 const char *nr
= NULL
;
182 if (device
== NULL
) {
183 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
184 "device is NULL: %s", pcap_strerror(errno
));
188 /* In Libpcap, we set pshared by default if NUM_RINGS is set to > 1.
189 * Since libpcap isn't thread-safe */
190 if ((nr
= getenv("SNF_NUM_RINGS")) && *nr
&& atoi(nr
) > 1)
191 flags
|= SNF_F_PSHARED
;
195 err
= snf_open(p
->md
.snf_boardnum
,
196 0, /* let SNF API parse SNF_NUM_RINGS, if set */
197 NULL
, /* default RSS, or use SNF_RSS_FLAGS env */
198 0, /* default to SNF_DATARING_SIZE from env */
199 flags
, /* may want pshared */
202 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
203 "snf_open failed: %s", pcap_strerror(err
));
207 err
= snf_ring_open(p
->md
.snf_handle
, &p
->md
.snf_ring
);
209 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
210 "snf_ring_open failed: %s", pcap_strerror(err
));
214 if (p
->md
.timeout
<= 0)
215 p
->md
.snf_timeout
= -1;
217 p
->md
.snf_timeout
= p
->md
.timeout
;
219 err
= snf_start(p
->md
.snf_handle
);
221 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
222 "snf_start failed: %s", pcap_strerror(err
));
227 * "select()" and "poll()" don't work on snf descriptors.
229 p
->selectable_fd
= -1;
230 p
->linktype
= DLT_EN10MB
;
231 p
->read_op
= snf_read
;
232 p
->inject_op
= snf_inject
;
233 p
->setfilter_op
= snf_setfilter
;
234 p
->setdirection_op
= NULL
; /* Not implemented.*/
235 p
->set_datalink_op
= snf_set_datalink
;
236 p
->getnonblock_op
= snf_getnonblock
;
237 p
->setnonblock_op
= snf_setnonblock
;
238 p
->stats_op
= snf_pcap_stats
;
239 p
->cleanup_op
= snf_platform_cleanup
;
240 p
->md
.stat
.ps_recv
= 0;
241 p
->md
.stat
.ps_drop
= 0;
242 p
->md
.stat
.ps_ifdrop
= 0;
247 snf_findalldevs(pcap_if_t
**devlistp
, char *errbuf
)
250 * There are no platform-specific devices since each device
251 * exists as a regular Ethernet device.
257 snf_create(const char *device
, char *ebuf
, int *is_ours
)
261 struct snf_ifaddrs
*ifaddrs
, *ifa
;
264 if (snf_init(SNF_VERSION_API
)) {
265 /* Can't initialize the API, so no SNF devices */
271 * Match a given interface name to our list of interface names, from
272 * which we can obtain the intended board number
274 if (snf_getifaddrs(&ifaddrs
) || ifaddrs
== NULL
) {
275 /* Can't get SNF addresses */
279 devlen
= strlen(device
) + 1;
282 if (!strncmp(device
, ifa
->snf_ifa_name
, devlen
)) {
283 boardnum
= ifa
->snf_ifa_boardnum
;
286 ifa
= ifa
->snf_ifa_next
;
288 snf_freeifaddrs(ifaddrs
);
292 * If we can't find the device by name, support the name "snfX"
293 * and "snf10gX" where X is the board number.
295 if (sscanf(device
, "snf10g%d", &boardnum
) != 1 &&
296 sscanf(device
, "snf%d", &boardnum
) != 1) {
297 /* Nope, not a supported name */
303 /* OK, it's probably ours. */
306 p
= pcap_create_common(device
, ebuf
);
310 p
->activate_op
= snf_activate
;
311 p
->md
.snf_boardnum
= boardnum
;