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
)
81 struct pcap_snf
*ps
= p
->priv
;
83 return (ps
->snf_timeout
== 0);
87 snf_setnonblock(pcap_t
*p
, int nonblock
)
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_list_t
*devlistp
, char *errbuf
)
325 struct snf_ifaddrs
*ifaddrs
, *ifa
;
326 char name
[MAX_DESC_LENGTH
];
327 char desc
[MAX_DESC_LENGTH
];
328 int ret
, allports
= 0, merge
= 0;
329 const char *nr
= NULL
;
331 if (snf_init(SNF_VERSION_API
)) {
332 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
333 "snf_getifaddrs: snf_init failed");
336 if (snf_getifaddrs(&ifaddrs
) || ifaddrs
== NULL
)
338 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
339 "snf_getifaddrs: %s", pcap_strerror(errno
));
342 if ((nr
= getenv("SNF_FLAGS")) && *nr
) {
344 merge
= strtol(nr
, NULL
, 0);
346 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
347 "snf_getifaddrs: SNF_FLAGS is not a valid number");
350 merge
= merge
& SNF_F_AGGREGATE_PORTMASK
;
353 for (ifa
= ifaddrs
; ifa
!= NULL
; ifa
= ifa
->snf_ifa_next
) {
355 * Myricom SNF adapter ports may appear as regular
356 * network interfaces, which would already have been
357 * added to the list of adapters by pcap_platform_finddevs()
358 * if this isn't an SNF-only version of libpcap.
360 * Our create routine intercepts pcap_create() calls for
361 * those interfaces and arranges that they will be
362 * opened using the SNF API instead.
364 * So if we already have an entry for the device, we
365 * don't add an additional entry for it, we just
366 * update the description for it, if any, to indicate
367 * which snfN device it is. Otherwise, we add an entry
370 * In either case, if SNF_F_AGGREGATE_PORTMASK is set
371 * in SNF_FLAGS, we add this port to the bitmask
372 * of ports, which we use to generate a device
373 * we can use to capture on all ports.
375 * Generate the description string. If port aggregation
376 * is set, use 2^{port number} as the unit number,
377 * rather than {port number}.
379 * XXX - do entries in this list have IP addresses for
380 * the port? If so, should we add them to the
381 * entry for the device, if they're not already in the
382 * list of IP addresses for the device?
384 (void)pcap_snprintf(desc
,MAX_DESC_LENGTH
,"Myricom %ssnf%d",
385 merge
? "Merge Bitmask Port " : "",
386 merge
? 1 << ifa
->snf_ifa_portnum
: ifa
->snf_ifa_portnum
);
388 * Add the port to the bitmask.
391 allports
|= 1 << ifa
->snf_ifa_portnum
;
393 * See if there's already an entry for the device
394 * with the name ifa->snf_ifa_name.
396 dev
= find_dev(devlistp
, ifa
->snf_ifa_name
);
399 * Yes. Update its description.
403 desc_str
= strdup(desc
);
404 if (desc_str
== NULL
) {
405 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
406 "snf_findalldevs strdup: %s", pcap_strerror(errno
));
409 free(dev
->description
);
410 dev
->description
= desc_str
;
413 * No. Add an entry for it.
415 dev
= add_dev(devlistp
, ifa
->snf_ifa_name
, 0, desc
,
421 snf_freeifaddrs(ifaddrs
);
423 * Create a snfX entry if port aggregation is enabled
427 * Add a new entry with all ports bitmask
429 (void)pcap_snprintf(name
,MAX_DESC_LENGTH
,"snf%d",allports
);
430 (void)pcap_snprintf(desc
,MAX_DESC_LENGTH
,"Myricom Merge Bitmask All Ports snf%d",
432 if (add_dev(devlistp
, name
, 0, desc
, errbuf
) == NULL
)
435 * XXX - should we give it a list of addresses with all
436 * the addresses for all the ports?
444 snf_create(const char *device
, char *ebuf
, int *is_ours
)
448 struct snf_ifaddrs
*ifaddrs
, *ifa
;
452 if (snf_init(SNF_VERSION_API
)) {
453 /* Can't initialize the API, so no SNF devices */
459 * Match a given interface name to our list of interface names, from
460 * which we can obtain the intended board number
462 if (snf_getifaddrs(&ifaddrs
) || ifaddrs
== NULL
) {
463 /* Can't get SNF addresses */
467 devlen
= strlen(device
) + 1;
470 if (strncmp(device
, ifa
->snf_ifa_name
, devlen
) == 0) {
471 boardnum
= ifa
->snf_ifa_boardnum
;
474 ifa
= ifa
->snf_ifa_next
;
476 snf_freeifaddrs(ifaddrs
);
480 * If we can't find the device by name, support the name "snfX"
481 * and "snf10gX" where X is the board number.
483 if (sscanf(device
, "snf10g%d", &boardnum
) != 1 &&
484 sscanf(device
, "snf%d", &boardnum
) != 1) {
485 /* Nope, not a supported name */
491 /* OK, it's probably ours. */
494 p
= pcap_create_common(ebuf
, sizeof (struct pcap_snf
));
500 * We support microsecond and nanosecond time stamps.
502 p
->tstamp_precision_count
= 2;
503 p
->tstamp_precision_list
= malloc(2 * sizeof(u_int
));
504 if (p
->tstamp_precision_list
== NULL
) {
505 pcap_snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
506 pcap_strerror(errno
));
510 p
->tstamp_precision_list
[0] = PCAP_TSTAMP_PRECISION_MICRO
;
511 p
->tstamp_precision_list
[1] = PCAP_TSTAMP_PRECISION_NANO
;
513 p
->activate_op
= snf_activate
;
514 ps
->snf_boardnum
= boardnum
;
520 * This libpcap build supports only SNF cards, not regular network
525 * There are no regular interfaces, just SNF interfaces.
528 pcap_platform_finddevs(pcap_if_list_t
*devlistp
, char *errbuf
)
534 * Attempts to open a regular interface fail.
537 pcap_create_interface(const char *device
, char *errbuf
)
539 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
540 "This version of libpcap only supports SNF cards");