/* libpcap expects tv_usec to be nanos if using nanosecond precision. */
if (tstamp_precision == PCAP_TSTAMP_PRECISION_NANO)
tv.tv_usec = tv_nsec;
else
tv.tv_usec = tv_nsec / 1000;
return tv;
}
static int
snf_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
{
struct pcap_snf *ps = p->priv;
struct pcap_pkthdr hdr;
int i, flags, err, caplen, n;
struct snf_recv_req req;
int nonblock, timeout;
if (!p)
return -1;
/*
* This can conceivably process more than INT_MAX packets,
* which would overflow the packet count, causing it either
* to look like a negative number, and thus cause us to
* return a value that looks like an error, or overflow
* back into positive territory, and thus cause us to
* return a too-low count.
*
* Therefore, if the packet count is unlimited, we clip
* it at INT_MAX; this routine is not expected to
* process packets indefinitely, so that's not an issue.
*/
if (PACKET_COUNT_IS_UNLIMITED(cnt))
cnt = INT_MAX;
n = 0;
timeout = ps->snf_timeout;
while (n < cnt) {
/*
* Has "pcap_breakloop()" been called?
*/
if (p->break_loop) {
if (n == 0) {
p->break_loop = 0;
return (-2);
} else {
return (n);
}
}
static int
snf_activate(pcap_t* p)
{
struct pcap_snf *ps = p->priv;
char *device = p->opt.device;
const char *nr = NULL;
int err;
int flags = -1, ring_id = -1;
if (device == NULL) {
snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "device is NULL");
return -1;
}
/* In Libpcap, we set pshared by default if NUM_RINGS is set to > 1.
* Since libpcap isn't thread-safe */
if ((nr = getenv("SNF_FLAGS")) && *nr)
flags = strtol(nr, NULL, 0);
else if ((nr = getenv("SNF_NUM_RINGS")) && *nr && atoi(nr) > 1)
flags = SNF_F_PSHARED;
else
nr = NULL;
/* Allow pcap_set_buffer_size() to set dataring_size.
* Default is zero which allows setting from env SNF_DATARING_SIZE.
* pcap_set_buffer_size() is in bytes while snf_open() accepts values
* between 0 and 1048576 in Megabytes. Values in this range are
* mapped to 1MB.
*/
err = snf_open(ps->snf_boardnum,
0, /* let SNF API parse SNF_NUM_RINGS, if set */
NULL, /* default RSS, or use SNF_RSS_FLAGS env */
(p->opt.buffer_size > 0 && p->opt.buffer_size < 1048576) ? 1048576 : p->opt.buffer_size, /* default to SNF_DATARING_SIZE from env */
flags, /* may want pshared */
&ps->snf_handle);
if (err != 0) {
pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
err, "snf_open failed");
return -1;
}
/*
* Turn a negative snapshot value (invalid), a snapshot value of
* 0 (unspecified), or a value bigger than the normal maximum
* value, into the maximum allowed value.
*
* If some application really *needs* a bigger snapshot
* length, we should just increase MAXIMUM_SNAPLEN.
*/
if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
p->snapshot = MAXIMUM_SNAPLEN;
if (snf_getifaddrs(&ifaddrs) || ifaddrs == NULL)
{
pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
errno, "snf_getifaddrs");
return (-1);
}
if ((nr = getenv("SNF_FLAGS")) && *nr) {
errno = 0;
merge = strtol(nr, NULL, 0);
if (errno) {
(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
"snf_getifaddrs: SNF_FLAGS is not a valid number");
return (-1);
}
merge = merge & SNF_F_AGGREGATE_PORTMASK;
}
for (ifa = ifaddrs; ifa != NULL; ifa = ifa->snf_ifa_next) {
/*
* Myricom SNF adapter ports may appear as regular
* network interfaces, which would already have been
* added to the list of adapters by pcapint_platform_finddevs()
* if this isn't an SNF-only version of libpcap.
*
* Our create routine intercepts pcap_create() calls for
* those interfaces and arranges that they will be
* opened using the SNF API instead.
*
* So if we already have an entry for the device, we
* don't add an additional entry for it, we just
* update the description for it, if any, to indicate
* which snfN device it is. Otherwise, we add an entry
* for it.
*
* In either case, if SNF_F_AGGREGATE_PORTMASK is set
* in SNF_FLAGS, we add this port to the bitmask
* of ports, which we use to generate a device
* we can use to capture on all ports.
*
* Generate the description string. If port aggregation
* is set, use 2^{port number} as the unit number,
* rather than {port number}.
*
* XXX - do entries in this list have IP addresses for
* the port? If so, should we add them to the
* entry for the device, if they're not already in the
* list of IP addresses for the device?
*/
(void)snprintf(desc,MAX_DESC_LENGTH,"Myricom %ssnf%d",
merge ? "Merge Bitmask Port " : "",
merge ? 1 << ifa->snf_ifa_portnum : ifa->snf_ifa_portnum);
/*
* Add the port to the bitmask.
*/
if (merge)
allports |= 1 << ifa->snf_ifa_portnum;
/*
* See if there's already an entry for the device
* with the name ifa->snf_ifa_name.
*/
dev = pcapint_find_dev(devlistp, ifa->snf_ifa_name);
if (dev != NULL) {
/*
* Yes. Update its description.
*/
char *desc_str;
desc_str = strdup(desc);
if (desc_str == NULL) {
pcapint_fmt_errmsg_for_errno(errbuf,
PCAP_ERRBUF_SIZE, errno,
"snf_findalldevs strdup");
return -1;
}
free(dev->description);
dev->description = desc_str;
} else {
/*
* No. Add an entry for it.
*
* XXX - is there a notion of "up" or "running",
* and can we determine whether something's
* plugged into the adapter and set
* PCAP_IF_CONNECTION_STATUS_CONNECTED or
* PCAP_IF_CONNECTION_STATUS_DISCONNECTED?
*/
dev = pcapint_add_dev(devlistp, ifa->snf_ifa_name, 0, desc,
errbuf);
if (dev == NULL)
return -1;
#ifdef _WIN32
/*
* On Windows, fill in IP# from device name
*/
ret = inet_pton(AF_INET, dev->name, &addr.sin_addr);
if (ret == 1) {
/*
* Successful conversion of device name
* to IPv4 address.
*/
addr.sin_family = AF_INET;
if (pcapint_add_addr_to_dev(dev, &addr, sizeof(addr),
NULL, 0, NULL, 0, NULL, 0, errbuf) == -1)
return -1;
} else if (ret == -1) {
/*
* Error.
*/
pcapint_fmt_errmsg_for_errno(errbuf,
PCAP_ERRBUF_SIZE, errno,
"sinf_findalldevs inet_pton");
return -1;
}
#endif _WIN32
}
}
snf_freeifaddrs(ifaddrs);
/*
* Create a snfX entry if port aggregation is enabled
*/
if (merge) {
/*
* Add a new entry with all ports bitmask
*/
(void)snprintf(name,MAX_DESC_LENGTH,"snf%d",allports);
(void)snprintf(desc,MAX_DESC_LENGTH,"Myricom Merge Bitmask All Ports snf%d",
allports);
/*
* XXX - is there any notion of "up" and "running" that
* would apply to this device, given that it handles
* multiple ports?
*
* Presumably, there's no notion of "connected" vs.
* "disconnected", as "is this plugged into a network?"
* would be a per-port property.
*/
if (pcapint_add_dev(devlistp, name,
PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE, desc,
errbuf) == NULL)
return (-1);
/*
* XXX - should we give it a list of addresses with all
* the addresses for all the ports?
*/
}
if (snf_init(SNF_VERSION_API)) {
/* Can't initialize the API, so no SNF devices */
*is_ours = 0;
return NULL;
}
/*
* Match a given interface name to our list of interface names, from
* which we can obtain the intended board number
*/
if (snf_getifaddrs(&ifaddrs) || ifaddrs == NULL) {
/* Can't get SNF addresses */
*is_ours = 0;
return NULL;
}
devlen = strlen(device) + 1;
ifa = ifaddrs;
while (ifa) {
if (strncmp(device, ifa->snf_ifa_name, devlen) == 0) {
boardnum = ifa->snf_ifa_boardnum;
break;
}
ifa = ifa->snf_ifa_next;
}
snf_freeifaddrs(ifaddrs);
if (ifa == NULL) {
/*
* If we can't find the device by name, support the name "snfX"
* and "snf10gX" where X is the board number.
*/
if (sscanf(device, "snf10g%d", &boardnum) != 1 &&
sscanf(device, "snf%d", &boardnum) != 1) {
/* Nope, not a supported name */
*is_ours = 0;
return NULL;
}
}
/* OK, it's probably ours. */
*is_ours = 1;
p = PCAP_CREATE_COMMON(ebuf, struct pcap_snf);
if (p == NULL)
return NULL;
ps = p->priv;
/*
* We support microsecond and nanosecond time stamps.
*/
p->tstamp_precision_list = malloc(2 * sizeof(u_int));
if (p->tstamp_precision_list == NULL) {
pcapint_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno,
"malloc");
pcap_close(p);
return NULL;
}
p->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO;
p->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO;
p->tstamp_precision_count = 2;