6 #include <pcap-stdinc.h>
17 #include <netinet/in.h>
19 #include <sys/socket.h>
20 #include <sys/types.h>
25 #if SNF_VERSION_API >= 0x0003
26 #define SNF_HAVE_INJECT_API
33 * Private data for capturing on SNF devices.
36 snf_handle_t snf_handle
; /* opaque device handle */
37 snf_ring_t snf_ring
; /* opaque device ring handle */
38 #ifdef SNF_HAVE_INJECT_API
39 snf_inject_t snf_inj
; /* inject handle, if inject is used */
46 snf_set_datalink(pcap_t
*p
, int dlt
)
53 snf_pcap_stats(pcap_t
*p
, struct pcap_stat
*ps
)
55 struct snf_ring_stats stats
;
56 struct pcap_snf
*snfps
= p
->priv
;
59 if ((rc
= snf_ring_getstats(snfps
->snf_ring
, &stats
))) {
60 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "snf_get_stats: %s",
64 ps
->ps_recv
= stats
.ring_pkt_recv
+ stats
.ring_pkt_overflow
;
65 ps
->ps_drop
= stats
.ring_pkt_overflow
;
66 ps
->ps_ifdrop
= stats
.nic_pkt_overflow
+ stats
.nic_pkt_bad
;
71 snf_platform_cleanup(pcap_t
*p
)
73 struct pcap_snf
*ps
= p
->priv
;
75 #ifdef SNF_HAVE_INJECT_API
77 snf_inject_close(ps
->snf_inj
);
79 snf_ring_close(ps
->snf_ring
);
80 snf_close(ps
->snf_handle
);
81 pcap_cleanup_live_common(p
);
85 snf_getnonblock(pcap_t
*p
)
87 struct pcap_snf
*ps
= p
->priv
;
89 return (ps
->snf_timeout
== 0);
93 snf_setnonblock(pcap_t
*p
, int nonblock
)
95 struct pcap_snf
*ps
= p
->priv
;
100 if (p
->opt
.timeout
<= 0)
101 ps
->snf_timeout
= -1; /* forever */
103 ps
->snf_timeout
= p
->opt
.timeout
;
108 #define _NSEC_PER_SEC 1000000000
112 snf_timestamp_to_timeval(const int64_t ts_nanosec
, const int tstamp_precision
)
116 const static struct timeval zero_timeval
;
121 tv
.tv_sec
= ts_nanosec
/ _NSEC_PER_SEC
;
122 tv_nsec
= (ts_nanosec
% _NSEC_PER_SEC
);
124 /* libpcap expects tv_usec to be nanos if using nanosecond precision. */
125 if (tstamp_precision
== PCAP_TSTAMP_PRECISION_NANO
)
126 tv
.tv_usec
= tv_nsec
;
128 tv
.tv_usec
= tv_nsec
/ 1000;
134 snf_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
136 struct pcap_snf
*ps
= p
->priv
;
137 struct pcap_pkthdr hdr
;
138 int i
, flags
, err
, caplen
, n
;
139 struct snf_recv_req req
;
140 int nonblock
, timeout
;
146 timeout
= ps
->snf_timeout
;
147 while (n
< cnt
|| PACKET_COUNT_IS_UNLIMITED(cnt
)) {
149 * Has "pcap_breakloop()" been called?
160 err
= snf_ring_recv(ps
->snf_ring
, timeout
, &req
);
163 if (err
== EBUSY
|| err
== EAGAIN
) {
166 else if (err
== EINTR
) {
171 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "snf_read: %s",
178 if (caplen
> p
->snapshot
)
179 caplen
= p
->snapshot
;
181 if ((p
->fcode
.bf_insns
== NULL
) ||
182 bpf_filter(p
->fcode
.bf_insns
, req
.pkt_addr
, req
.length
, caplen
)) {
183 hdr
.ts
= snf_timestamp_to_timeval(req
.timestamp
, p
->opt
.tstamp_precision
);
185 hdr
.len
= req
.length
;
186 callback(user
, &hdr
, req
.pkt_addr
);
190 /* After one successful packet is received, we won't block
191 * again for that timeout. */
199 snf_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
204 strncpy(p
->errbuf
, "setfilter: No filter specified",
209 /* Make our private copy of the filter */
211 if (install_bpf_program(p
, fp
) < 0)
218 snf_inject(pcap_t
*p
, const void *buf _U_
, size_t size _U_
)
220 #ifdef SNF_HAVE_INJECT_API
221 struct pcap_snf
*ps
= p
->priv
;
223 if (ps
->snf_inj
== NULL
) {
224 rc
= snf_inject_open(ps
->snf_boardnum
, 0, &ps
->snf_inj
);
226 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
227 "snf_inject_open: %s", pcap_strerror(rc
));
232 rc
= snf_inject_send(ps
->snf_inj
, -1, 0, buf
, size
);
237 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "snf_inject_send: %s",
242 strlcpy(p
->errbuf
, "Sending packets isn't supported with this snf version",
249 snf_activate(pcap_t
* p
)
251 struct pcap_snf
*ps
= p
->priv
;
252 char *device
= p
->opt
.device
;
253 const char *nr
= NULL
;
255 int flags
= -1, ring_id
= -1;
257 if (device
== NULL
) {
258 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
259 "device is NULL: %s", pcap_strerror(errno
));
263 /* In Libpcap, we set pshared by default if NUM_RINGS is set to > 1.
264 * Since libpcap isn't thread-safe */
265 if ((nr
= getenv("SNF_FLAGS")) && *nr
)
266 flags
= strtol(nr
, NULL
, 0);
267 else if ((nr
= getenv("SNF_NUM_RINGS")) && *nr
&& atoi(nr
) > 1)
268 flags
= SNF_F_PSHARED
;
273 /* Allow pcap_set_buffer_size() to set dataring_size.
274 * Default is zero which allows setting from env SNF_DATARING_SIZE.
275 * pcap_set_buffer_size() is in bytes while snf_open() accepts values
276 * between 0 and 1048576 in Megabytes. Values in this range are
279 err
= snf_open(ps
->snf_boardnum
,
280 0, /* let SNF API parse SNF_NUM_RINGS, if set */
281 NULL
, /* default RSS, or use SNF_RSS_FLAGS env */
282 (p
->opt
.buffer_size
> 0 && p
->opt
.buffer_size
< 1048576) ? 1048576 : p
->opt
.buffer_size
, /* default to SNF_DATARING_SIZE from env */
283 flags
, /* may want pshared */
286 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
287 "snf_open failed: %s", pcap_strerror(err
));
291 if ((nr
= getenv("SNF_PCAP_RING_ID")) && *nr
) {
292 ring_id
= (int) strtol(nr
, NULL
, 0);
294 err
= snf_ring_open_id(ps
->snf_handle
, ring_id
, &ps
->snf_ring
);
296 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
297 "snf_ring_open_id(ring=%d) failed: %s",
298 ring_id
, pcap_strerror(err
));
302 if (p
->opt
.timeout
<= 0)
303 ps
->snf_timeout
= -1;
305 ps
->snf_timeout
= p
->opt
.timeout
;
307 err
= snf_start(ps
->snf_handle
);
309 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
310 "snf_start failed: %s", pcap_strerror(err
));
315 * "select()" and "poll()" don't work on snf descriptors.
318 p
->selectable_fd
= -1;
320 p
->linktype
= DLT_EN10MB
;
321 p
->read_op
= snf_read
;
322 p
->inject_op
= snf_inject
;
323 p
->setfilter_op
= snf_setfilter
;
324 p
->setdirection_op
= NULL
; /* Not implemented.*/
325 p
->set_datalink_op
= snf_set_datalink
;
326 p
->getnonblock_op
= snf_getnonblock
;
327 p
->setnonblock_op
= snf_setnonblock
;
328 p
->stats_op
= snf_pcap_stats
;
329 p
->cleanup_op
= snf_platform_cleanup
;
330 #ifdef SNF_HAVE_INJECT_API
336 #define MAX_DESC_LENGTH 128
338 snf_findalldevs(pcap_if_list_t
*devlistp
, char *errbuf
)
342 struct sockaddr_in addr
;
344 struct snf_ifaddrs
*ifaddrs
, *ifa
;
345 char name
[MAX_DESC_LENGTH
];
346 char desc
[MAX_DESC_LENGTH
];
347 int ret
, allports
= 0, merge
= 0;
348 const char *nr
= NULL
;
350 if (snf_init(SNF_VERSION_API
)) {
351 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
352 "snf_getifaddrs: snf_init failed");
356 if (snf_getifaddrs(&ifaddrs
) || ifaddrs
== NULL
)
358 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
359 "snf_getifaddrs: %s", pcap_strerror(errno
));
362 if ((nr
= getenv("SNF_FLAGS")) && *nr
) {
364 merge
= strtol(nr
, NULL
, 0);
366 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
367 "snf_getifaddrs: SNF_FLAGS is not a valid number");
370 merge
= merge
& SNF_F_AGGREGATE_PORTMASK
;
373 for (ifa
= ifaddrs
; ifa
!= NULL
; ifa
= ifa
->snf_ifa_next
) {
375 * Myricom SNF adapter ports may appear as regular
376 * network interfaces, which would already have been
377 * added to the list of adapters by pcap_platform_finddevs()
378 * if this isn't an SNF-only version of libpcap.
380 * Our create routine intercepts pcap_create() calls for
381 * those interfaces and arranges that they will be
382 * opened using the SNF API instead.
384 * So if we already have an entry for the device, we
385 * don't add an additional entry for it, we just
386 * update the description for it, if any, to indicate
387 * which snfN device it is. Otherwise, we add an entry
390 * In either case, if SNF_F_AGGREGATE_PORTMASK is set
391 * in SNF_FLAGS, we add this port to the bitmask
392 * of ports, which we use to generate a device
393 * we can use to capture on all ports.
395 * Generate the description string. If port aggregation
396 * is set, use 2^{port number} as the unit number,
397 * rather than {port number}.
399 * XXX - do entries in this list have IP addresses for
400 * the port? If so, should we add them to the
401 * entry for the device, if they're not already in the
402 * list of IP addresses for the device?
404 (void)pcap_snprintf(desc
,MAX_DESC_LENGTH
,"Myricom %ssnf%d",
405 merge
? "Merge Bitmask Port " : "",
406 merge
? 1 << ifa
->snf_ifa_portnum
: ifa
->snf_ifa_portnum
);
408 * Add the port to the bitmask.
411 allports
|= 1 << ifa
->snf_ifa_portnum
;
413 * See if there's already an entry for the device
414 * with the name ifa->snf_ifa_name.
416 dev
= find_dev(devlistp
, ifa
->snf_ifa_name
);
419 * Yes. Update its description.
423 desc_str
= strdup(desc
);
424 if (desc_str
== NULL
) {
425 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
426 "snf_findalldevs strdup: %s", pcap_strerror(errno
));
429 free(dev
->description
);
430 dev
->description
= desc_str
;
433 * No. Add an entry for it.
435 dev
= add_dev(devlistp
, ifa
->snf_ifa_name
, 0, desc
,
441 * On Windows, fill in IP# from device name
443 ret
= inet_pton(AF_INET
, dev
->name
, &addr
.sin_addr
);
446 * Successful conversion of device name
449 addr
.sin_family
= AF_INET
;
450 if (add_addr_to_dev(dev
, &addr
, sizeof(addr
),
451 NULL
, 0, NULL
, 0, NULL
, 0, errbuf
) == -1)
453 } else if (ret
== -1) {
457 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,"sinf_findalldevs inet_pton: %s", pcap_strerror(errno
));
463 snf_freeifaddrs(ifaddrs
);
465 * Create a snfX entry if port aggregation is enabled
469 * Add a new entry with all ports bitmask
471 (void)pcap_snprintf(name
,MAX_DESC_LENGTH
,"snf%d",allports
);
472 (void)pcap_snprintf(desc
,MAX_DESC_LENGTH
,"Myricom Merge Bitmask All Ports snf%d",
474 if (add_dev(devlistp
, name
, 0, desc
, errbuf
) == NULL
)
477 * XXX - should we give it a list of addresses with all
478 * the addresses for all the ports?
486 snf_create(const char *device
, char *ebuf
, int *is_ours
)
490 struct snf_ifaddrs
*ifaddrs
, *ifa
;
494 if (snf_init(SNF_VERSION_API
)) {
495 /* Can't initialize the API, so no SNF devices */
501 * Match a given interface name to our list of interface names, from
502 * which we can obtain the intended board number
504 if (snf_getifaddrs(&ifaddrs
) || ifaddrs
== NULL
) {
505 /* Can't get SNF addresses */
509 devlen
= strlen(device
) + 1;
512 if (strncmp(device
, ifa
->snf_ifa_name
, devlen
) == 0) {
513 boardnum
= ifa
->snf_ifa_boardnum
;
516 ifa
= ifa
->snf_ifa_next
;
518 snf_freeifaddrs(ifaddrs
);
522 * If we can't find the device by name, support the name "snfX"
523 * and "snf10gX" where X is the board number.
525 if (sscanf(device
, "snf10g%d", &boardnum
) != 1 &&
526 sscanf(device
, "snf%d", &boardnum
) != 1) {
527 /* Nope, not a supported name */
533 /* OK, it's probably ours. */
536 p
= pcap_create_common(ebuf
, sizeof (struct pcap_snf
));
542 * We support microsecond and nanosecond time stamps.
544 p
->tstamp_precision_count
= 2;
545 p
->tstamp_precision_list
= malloc(2 * sizeof(u_int
));
546 if (p
->tstamp_precision_list
== NULL
) {
547 pcap_snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
548 pcap_strerror(errno
));
552 p
->tstamp_precision_list
[0] = PCAP_TSTAMP_PRECISION_MICRO
;
553 p
->tstamp_precision_list
[1] = PCAP_TSTAMP_PRECISION_NANO
;
555 p
->activate_op
= snf_activate
;
556 ps
->snf_boardnum
= boardnum
;
562 * This libpcap build supports only SNF cards, not regular network
567 * There are no regular interfaces, just SNF interfaces.
570 pcap_platform_finddevs(pcap_if_list_t
*devlistp
, char *errbuf
)
576 * Attempts to open a regular interface fail.
579 pcap_create_interface(const char *device
, char *errbuf
)
581 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
582 "This version of libpcap only supports SNF cards");