2 __RCSID("$NetBSD: pcap-snf.c,v 1.3 2015/03/31 21:39:42 christos Exp $");
4 /* $NetBSD: pcap-snf.c,v 1.3 2015/03/31 21:39:42 christos Exp $ */
10 #include <sys/param.h>
17 #include <netinet/in.h>
19 #include <sys/socket.h>
20 #include <sys/types.h>
24 #if SNF_VERSION_API >= 0x0003
25 #define SNF_HAVE_INJECT_API
32 * Private data for capturing on SNF devices.
35 snf_handle_t snf_handle
; /* opaque device handle */
36 snf_ring_t snf_ring
; /* opaque device ring handle */
37 #ifdef SNF_HAVE_INJECT_API
38 snf_inject_t snf_inj
; /* inject handle, if inject is used */
45 snf_set_datalink(pcap_t
*p
, int dlt
)
52 snf_pcap_stats(pcap_t
*p
, struct pcap_stat
*ps
)
54 struct snf_ring_stats stats
;
55 struct pcap_snf
*snfps
= p
->priv
;
58 if ((rc
= snf_ring_getstats(snfps
->snf_ring
, &stats
))) {
59 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "snf_get_stats: %s",
63 ps
->ps_recv
= stats
.ring_pkt_recv
+ stats
.ring_pkt_overflow
;
64 ps
->ps_drop
= stats
.ring_pkt_overflow
;
65 ps
->ps_ifdrop
= stats
.nic_pkt_overflow
+ stats
.nic_pkt_bad
;
70 snf_platform_cleanup(pcap_t
*p
)
72 struct pcap_snf
*ps
= p
->priv
;
77 #ifdef SNF_HAVE_INJECT_API
79 snf_inject_close(ps
->snf_inj
);
81 snf_ring_close(ps
->snf_ring
);
82 snf_close(ps
->snf_handle
);
83 pcap_cleanup_live_common(p
);
87 snf_getnonblock(pcap_t
*p
, char *errbuf
)
89 struct pcap_snf
*ps
= p
->priv
;
91 return (ps
->snf_timeout
== 0);
95 snf_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
97 struct pcap_snf
*ps
= p
->priv
;
102 if (p
->opt
.timeout
<= 0)
103 ps
->snf_timeout
= -1; /* forever */
105 ps
->snf_timeout
= p
->opt
.timeout
;
110 #define _NSEC_PER_SEC 1000000000
114 snf_timestamp_to_timeval(const int64_t ts_nanosec
, const int tstamp_precision
)
120 return (struct timeval
) { 0, 0 };
122 tv
.tv_sec
= ts_nanosec
/ _NSEC_PER_SEC
;
123 tv_nsec
= (ts_nanosec
% _NSEC_PER_SEC
);
125 /* libpcap expects tv_usec to be nanos if using nanosecond precision. */
126 if (tstamp_precision
== PCAP_TSTAMP_PRECISION_NANO
)
127 tv
.tv_usec
= tv_nsec
;
129 tv
.tv_usec
= tv_nsec
/ 1000;
135 snf_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
137 struct pcap_snf
*ps
= p
->priv
;
138 struct pcap_pkthdr hdr
;
139 int i
, flags
, err
, caplen
, n
;
140 struct snf_recv_req req
;
141 int nonblock
, timeout
;
147 timeout
= ps
->snf_timeout
;
148 while (n
< cnt
|| PACKET_COUNT_IS_UNLIMITED(cnt
)) {
150 * Has "pcap_breakloop()" been called?
161 err
= snf_ring_recv(ps
->snf_ring
, timeout
, &req
);
164 if (err
== EBUSY
|| err
== EAGAIN
) {
167 else if (err
== EINTR
) {
172 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "snf_read: %s",
179 if (caplen
> p
->snapshot
)
180 caplen
= p
->snapshot
;
182 if ((p
->fcode
.bf_insns
== NULL
) ||
183 bpf_filter(p
->fcode
.bf_insns
, req
.pkt_addr
, req
.length
, caplen
)) {
184 hdr
.ts
= snf_timestamp_to_timeval(req
.timestamp
, p
->opt
.tstamp_precision
);
186 hdr
.len
= req
.length
;
187 callback(user
, &hdr
, req
.pkt_addr
);
191 /* After one successful packet is received, we won't block
192 * again for that timeout. */
200 snf_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
205 strncpy(p
->errbuf
, "setfilter: No filter specified",
210 /* Make our private copy of the filter */
212 if (install_bpf_program(p
, fp
) < 0)
219 snf_inject(pcap_t
*p
, const void *buf _U_
, size_t size _U_
)
221 #ifdef SNF_HAVE_INJECT_API
222 struct pcap_snf
*ps
= p
->priv
;
224 if (ps
->snf_inj
== NULL
) {
225 rc
= snf_inject_open(ps
->snf_boardnum
, 0, &ps
->snf_inj
);
227 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
228 "snf_inject_open: %s", pcap_strerror(rc
));
233 rc
= snf_inject_send(ps
->snf_inj
, -1, 0, buf
, size
);
238 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "snf_inject_send: %s",
243 strlcpy(p
->errbuf
, "Sending packets isn't supported with this snf version",
250 snf_activate(pcap_t
* p
)
252 struct pcap_snf
*ps
= p
->priv
;
253 char *device
= p
->opt
.source
;
254 const char *nr
= NULL
;
256 int flags
= -1, ring_id
= -1;
258 if (device
== NULL
) {
259 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
260 "device is NULL: %s", pcap_strerror(errno
));
264 /* In Libpcap, we set pshared by default if NUM_RINGS is set to > 1.
265 * Since libpcap isn't thread-safe */
266 if ((nr
= getenv("SNF_FLAGS")) && *nr
)
267 flags
= strtol(nr
, NULL
, 0);
268 else if ((nr
= getenv("SNF_NUM_RINGS")) && *nr
&& atoi(nr
) > 1)
269 flags
= SNF_F_PSHARED
;
273 err
= snf_open(ps
->snf_boardnum
,
274 0, /* let SNF API parse SNF_NUM_RINGS, if set */
275 NULL
, /* default RSS, or use SNF_RSS_FLAGS env */
276 0, /* default to SNF_DATARING_SIZE from env */
277 flags
, /* may want pshared */
280 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
281 "snf_open failed: %s", pcap_strerror(err
));
285 if ((nr
= getenv("SNF_PCAP_RING_ID")) && *nr
) {
286 ring_id
= (int) strtol(nr
, NULL
, 0);
288 err
= snf_ring_open_id(ps
->snf_handle
, ring_id
, &ps
->snf_ring
);
290 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
291 "snf_ring_open_id(ring=%d) failed: %s",
292 ring_id
, pcap_strerror(err
));
296 if (p
->opt
.timeout
<= 0)
297 ps
->snf_timeout
= -1;
299 ps
->snf_timeout
= p
->opt
.timeout
;
301 err
= snf_start(ps
->snf_handle
);
303 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
304 "snf_start failed: %s", pcap_strerror(err
));
309 * "select()" and "poll()" don't work on snf descriptors.
311 p
->selectable_fd
= -1;
312 p
->linktype
= DLT_EN10MB
;
313 p
->read_op
= snf_read
;
314 p
->inject_op
= snf_inject
;
315 p
->setfilter_op
= snf_setfilter
;
316 p
->setdirection_op
= NULL
; /* Not implemented.*/
317 p
->set_datalink_op
= snf_set_datalink
;
318 p
->getnonblock_op
= snf_getnonblock
;
319 p
->setnonblock_op
= snf_setnonblock
;
320 p
->stats_op
= snf_pcap_stats
;
321 p
->cleanup_op
= snf_platform_cleanup
;
322 #ifdef SNF_HAVE_INJECT_API
328 #define MAX_DESC_LENGTH 128
330 snf_findalldevs(pcap_if_t
**devlistp
, char *errbuf
)
332 pcap_if_t
*devlist
= NULL
,*curdev
,*prevdev
;
333 pcap_addr_t
*curaddr
;
334 struct snf_ifaddrs
*ifaddrs
, *ifa
;
335 char desc
[MAX_DESC_LENGTH
];
338 if (snf_init(SNF_VERSION_API
))
341 if (snf_getifaddrs(&ifaddrs
) || ifaddrs
== NULL
)
343 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
344 "snf_getifaddrs: %s", pcap_strerror(errno
));
351 * Allocate a new entry
353 curdev
= (pcap_if_t
*)malloc(sizeof(pcap_if_t
));
354 if (curdev
== NULL
) {
355 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
356 "snf_findalldevs malloc: %s", pcap_strerror(errno
));
359 if (devlist
== NULL
) /* save first entry */
362 prevdev
->next
= curdev
;
367 curdev
->name
= strdup(ifa
->snf_ifa_name
);
368 if (curdev
->name
== NULL
) {
369 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
370 "snf_findalldevs strdup: %s", pcap_strerror(errno
));
374 (void)snprintf(desc
,MAX_DESC_LENGTH
,"Myricom snf%d",
375 ifa
->snf_ifa_portnum
);
376 curdev
->description
= strdup(desc
);
377 if (curdev
->description
== NULL
) {
378 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
379 "snf_findalldevs strdup1: %s", pcap_strerror(errno
));
384 curdev
->addresses
= NULL
;
387 curaddr
= (pcap_addr_t
*)malloc(sizeof(pcap_addr_t
));
388 if (curaddr
== NULL
) {
389 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
390 "snf_findalldevs malloc1: %s", pcap_strerror(errno
));
391 free(curdev
->description
);
396 curdev
->addresses
= curaddr
;
397 curaddr
->next
= NULL
;
398 curaddr
->addr
= (struct sockaddr
*)malloc(sizeof(struct sockaddr_storage
));
399 if (curaddr
->addr
== NULL
) {
400 (void)snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
401 "malloc2: %s", pcap_strerror(errno
));
402 free(curdev
->description
);
408 curaddr
->addr
->sa_family
= AF_INET
;
409 curaddr
->netmask
= NULL
;
410 curaddr
->broadaddr
= NULL
;
411 curaddr
->dstaddr
= NULL
;
412 curaddr
->next
= NULL
;
415 ifa
= ifa
->snf_ifa_next
;
417 snf_freeifaddrs(ifaddrs
);
421 * There are no platform-specific devices since each device
422 * exists as a regular Ethernet device.
428 snf_create(const char *device
, char *ebuf
, int *is_ours
)
432 struct snf_ifaddrs
*ifaddrs
, *ifa
;
436 if (snf_init(SNF_VERSION_API
)) {
437 /* Can't initialize the API, so no SNF devices */
443 * Match a given interface name to our list of interface names, from
444 * which we can obtain the intended board number
446 if (snf_getifaddrs(&ifaddrs
) || ifaddrs
== NULL
) {
447 /* Can't get SNF addresses */
451 devlen
= strlen(device
) + 1;
454 if (!strncmp(device
, ifa
->snf_ifa_name
, devlen
)) {
455 boardnum
= ifa
->snf_ifa_boardnum
;
458 ifa
= ifa
->snf_ifa_next
;
460 snf_freeifaddrs(ifaddrs
);
464 * If we can't find the device by name, support the name "snfX"
465 * and "snf10gX" where X is the board number.
467 if (sscanf(device
, "snf10g%d", &boardnum
) != 1 &&
468 sscanf(device
, "snf%d", &boardnum
) != 1) {
469 /* Nope, not a supported name */
475 /* OK, it's probably ours. */
478 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_snf
));
484 * We support microsecond and nanosecond time stamps.
486 p
->tstamp_precision_count
= 2;
487 p
->tstamp_precision_list
= malloc(2 * sizeof(u_int
));
488 if (p
->tstamp_precision_list
== NULL
) {
489 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
490 pcap_strerror(errno
));
491 if (p
->tstamp_type_list
!= NULL
)
492 free(p
->tstamp_type_list
);
496 p
->tstamp_precision_list
[0] = PCAP_TSTAMP_PRECISION_MICRO
;
497 p
->tstamp_precision_list
[1] = PCAP_TSTAMP_PRECISION_NANO
;
499 p
->activate_op
= snf_activate
;
500 ps
->snf_boardnum
= boardnum
;