12 #include <netinet/in.h>
14 #include <sys/socket.h>
15 #include <sys/types.h>
19 #if SNF_VERSION_API >= 0x0003
20 #define SNF_HAVE_INJECT_API
27 * Private data for capturing on SNF devices.
30 snf_handle_t snf_handle
; /* opaque device handle */
31 snf_ring_t snf_ring
; /* opaque device ring handle */
32 #ifdef SNF_HAVE_INJECT_API
33 snf_inject_t snf_inj
; /* inject handle, if inject is used */
40 snf_set_datalink(pcap_t
*p
, int dlt
)
47 snf_pcap_stats(pcap_t
*p
, struct pcap_stat
*ps
)
49 struct snf_ring_stats stats
;
50 struct pcap_snf
*snfps
= p
->priv
;
53 if ((rc
= snf_ring_getstats(snfps
->snf_ring
, &stats
))) {
54 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "snf_get_stats: %s",
58 ps
->ps_recv
= stats
.ring_pkt_recv
+ stats
.ring_pkt_overflow
;
59 ps
->ps_drop
= stats
.ring_pkt_overflow
;
60 ps
->ps_ifdrop
= stats
.nic_pkt_overflow
+ stats
.nic_pkt_bad
;
65 snf_platform_cleanup(pcap_t
*p
)
67 struct pcap_snf
*ps
= p
->priv
;
69 #ifdef SNF_HAVE_INJECT_API
71 snf_inject_close(ps
->snf_inj
);
73 snf_ring_close(ps
->snf_ring
);
74 snf_close(ps
->snf_handle
);
75 pcap_cleanup_live_common(p
);
79 snf_getnonblock(pcap_t
*p
, char *errbuf
)
81 struct pcap_snf
*ps
= p
->priv
;
83 return (ps
->snf_timeout
== 0);
87 snf_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
89 struct pcap_snf
*ps
= p
->priv
;
94 if (p
->opt
.timeout
<= 0)
95 ps
->snf_timeout
= -1; /* forever */
97 ps
->snf_timeout
= p
->opt
.timeout
;
102 #define _NSEC_PER_SEC 1000000000
106 snf_timestamp_to_timeval(const int64_t ts_nanosec
, const int tstamp_precision
)
112 return (struct timeval
) { 0, 0 };
114 tv
.tv_sec
= ts_nanosec
/ _NSEC_PER_SEC
;
115 tv_nsec
= (ts_nanosec
% _NSEC_PER_SEC
);
117 /* libpcap expects tv_usec to be nanos if using nanosecond precision. */
118 if (tstamp_precision
== PCAP_TSTAMP_PRECISION_NANO
)
119 tv
.tv_usec
= tv_nsec
;
121 tv
.tv_usec
= tv_nsec
/ 1000;
127 snf_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
129 struct pcap_snf
*ps
= p
->priv
;
130 struct pcap_pkthdr hdr
;
131 int i
, flags
, err
, caplen
, n
;
132 struct snf_recv_req req
;
133 int nonblock
, timeout
;
139 timeout
= ps
->snf_timeout
;
140 while (n
< cnt
|| PACKET_COUNT_IS_UNLIMITED(cnt
)) {
142 * Has "pcap_breakloop()" been called?
153 err
= snf_ring_recv(ps
->snf_ring
, timeout
, &req
);
156 if (err
== EBUSY
|| err
== EAGAIN
) {
159 else if (err
== EINTR
) {
164 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "snf_read: %s",
171 if (caplen
> p
->snapshot
)
172 caplen
= p
->snapshot
;
174 if ((p
->fcode
.bf_insns
== NULL
) ||
175 bpf_filter(p
->fcode
.bf_insns
, req
.pkt_addr
, req
.length
, caplen
)) {
176 hdr
.ts
= snf_timestamp_to_timeval(req
.timestamp
, p
->opt
.tstamp_precision
);
178 hdr
.len
= req
.length
;
179 callback(user
, &hdr
, req
.pkt_addr
);
183 /* After one successful packet is received, we won't block
184 * again for that timeout. */
192 snf_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
197 strncpy(p
->errbuf
, "setfilter: No filter specified",
202 /* Make our private copy of the filter */
204 if (install_bpf_program(p
, fp
) < 0)
211 snf_inject(pcap_t
*p
, const void *buf _U_
, size_t size _U_
)
213 #ifdef SNF_HAVE_INJECT_API
214 struct pcap_snf
*ps
= p
->priv
;
216 if (ps
->snf_inj
== NULL
) {
217 rc
= snf_inject_open(ps
->snf_boardnum
, 0, &ps
->snf_inj
);
219 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
220 "snf_inject_open: %s", pcap_strerror(rc
));
225 rc
= snf_inject_send(ps
->snf_inj
, -1, 0, buf
, size
);
230 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "snf_inject_send: %s",
235 strlcpy(p
->errbuf
, "Sending packets isn't supported with this snf version",
242 snf_activate(pcap_t
* p
)
244 struct pcap_snf
*ps
= p
->priv
;
245 char *device
= p
->opt
.device
;
246 const char *nr
= NULL
;
248 int flags
= -1, ring_id
= -1;
250 if (device
== NULL
) {
251 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
252 "device is NULL: %s", pcap_strerror(errno
));
256 /* In Libpcap, we set pshared by default if NUM_RINGS is set to > 1.
257 * Since libpcap isn't thread-safe */
258 if ((nr
= getenv("SNF_FLAGS")) && *nr
)
259 flags
= strtol(nr
, NULL
, 0);
260 else if ((nr
= getenv("SNF_NUM_RINGS")) && *nr
&& atoi(nr
) > 1)
261 flags
= SNF_F_PSHARED
;
265 err
= snf_open(ps
->snf_boardnum
,
266 0, /* let SNF API parse SNF_NUM_RINGS, if set */
267 NULL
, /* default RSS, or use SNF_RSS_FLAGS env */
268 0, /* default to SNF_DATARING_SIZE from env */
269 flags
, /* may want pshared */
272 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
273 "snf_open failed: %s", pcap_strerror(err
));
277 if ((nr
= getenv("SNF_PCAP_RING_ID")) && *nr
) {
278 ring_id
= (int) strtol(nr
, NULL
, 0);
280 err
= snf_ring_open_id(ps
->snf_handle
, ring_id
, &ps
->snf_ring
);
282 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
283 "snf_ring_open_id(ring=%d) failed: %s",
284 ring_id
, pcap_strerror(err
));
288 if (p
->opt
.timeout
<= 0)
289 ps
->snf_timeout
= -1;
291 ps
->snf_timeout
= p
->opt
.timeout
;
293 err
= snf_start(ps
->snf_handle
);
295 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
296 "snf_start failed: %s", pcap_strerror(err
));
301 * "select()" and "poll()" don't work on snf descriptors.
303 p
->selectable_fd
= -1;
304 p
->linktype
= DLT_EN10MB
;
305 p
->read_op
= snf_read
;
306 p
->inject_op
= snf_inject
;
307 p
->setfilter_op
= snf_setfilter
;
308 p
->setdirection_op
= NULL
; /* Not implemented.*/
309 p
->set_datalink_op
= snf_set_datalink
;
310 p
->getnonblock_op
= snf_getnonblock
;
311 p
->setnonblock_op
= snf_setnonblock
;
312 p
->stats_op
= snf_pcap_stats
;
313 p
->cleanup_op
= snf_platform_cleanup
;
314 #ifdef SNF_HAVE_INJECT_API
320 #define MAX_DESC_LENGTH 128
322 snf_findalldevs(pcap_if_t
**devlistp
, char *errbuf
)
324 pcap_if_t
*devlist
= NULL
,*curdev
,*prevdev
;
325 pcap_addr_t
*curaddr
;
326 struct snf_ifaddrs
*ifaddrs
, *ifa
;
327 char desc
[MAX_DESC_LENGTH
];
330 if (snf_init(SNF_VERSION_API
))
333 if (snf_getifaddrs(&ifaddrs
) || ifaddrs
== NULL
)
335 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
336 "snf_getifaddrs: %s", pcap_strerror(errno
));
343 * Allocate a new entry
345 curdev
= (pcap_if_t
*)malloc(sizeof(pcap_if_t
));
346 if (curdev
== NULL
) {
347 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
348 "snf_findalldevs malloc: %s", pcap_strerror(errno
));
351 if (devlist
== NULL
) /* save first entry */
354 prevdev
->next
= curdev
;
359 curdev
->name
= strdup(ifa
->snf_ifa_name
);
360 if (curdev
->name
== NULL
) {
361 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
362 "snf_findalldevs strdup: %s", pcap_strerror(errno
));
366 (void)pcap_snprintf(desc
,MAX_DESC_LENGTH
,"Myricom snf%d",
367 ifa
->snf_ifa_portnum
);
368 curdev
->description
= strdup(desc
);
369 if (curdev
->description
== NULL
) {
370 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
371 "snf_findalldevs strdup1: %s", pcap_strerror(errno
));
376 curdev
->addresses
= NULL
;
379 curaddr
= (pcap_addr_t
*)malloc(sizeof(pcap_addr_t
));
380 if (curaddr
== NULL
) {
381 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
382 "snf_findalldevs malloc1: %s", pcap_strerror(errno
));
383 free(curdev
->description
);
388 curdev
->addresses
= curaddr
;
389 curaddr
->next
= NULL
;
390 curaddr
->addr
= (struct sockaddr
*)malloc(sizeof(struct sockaddr_storage
));
391 if (curaddr
->addr
== NULL
) {
392 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
393 "malloc2: %s", pcap_strerror(errno
));
394 free(curdev
->description
);
400 curaddr
->addr
->sa_family
= AF_INET
;
401 curaddr
->netmask
= NULL
;
402 curaddr
->broadaddr
= NULL
;
403 curaddr
->dstaddr
= NULL
;
404 curaddr
->next
= NULL
;
407 ifa
= ifa
->snf_ifa_next
;
409 snf_freeifaddrs(ifaddrs
);
413 * There are no platform-specific devices since each device
414 * exists as a regular Ethernet device.
420 snf_create(const char *device
, char *ebuf
, int *is_ours
)
424 struct snf_ifaddrs
*ifaddrs
, *ifa
;
428 if (snf_init(SNF_VERSION_API
)) {
429 /* Can't initialize the API, so no SNF devices */
435 * Match a given interface name to our list of interface names, from
436 * which we can obtain the intended board number
438 if (snf_getifaddrs(&ifaddrs
) || ifaddrs
== NULL
) {
439 /* Can't get SNF addresses */
443 devlen
= strlen(device
) + 1;
446 if (!strncmp(device
, ifa
->snf_ifa_name
, devlen
)) {
447 boardnum
= ifa
->snf_ifa_boardnum
;
450 ifa
= ifa
->snf_ifa_next
;
452 snf_freeifaddrs(ifaddrs
);
456 * If we can't find the device by name, support the name "snfX"
457 * and "snf10gX" where X is the board number.
459 if (sscanf(device
, "snf10g%d", &boardnum
) != 1 &&
460 sscanf(device
, "snf%d", &boardnum
) != 1) {
461 /* Nope, not a supported name */
467 /* OK, it's probably ours. */
470 p
= pcap_create_common(ebuf
, sizeof (struct pcap_snf
));
476 * We support microsecond and nanosecond time stamps.
478 p
->tstamp_precision_count
= 2;
479 p
->tstamp_precision_list
= malloc(2 * sizeof(u_int
));
480 if (p
->tstamp_precision_list
== NULL
) {
481 pcap_snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
482 pcap_strerror(errno
));
486 p
->tstamp_precision_list
[0] = PCAP_TSTAMP_PRECISION_MICRO
;
487 p
->tstamp_precision_list
[1] = PCAP_TSTAMP_PRECISION_NANO
;
489 p
->activate_op
= snf_activate
;
490 ps
->snf_boardnum
= boardnum
;