12 #include <netinet/in.h>
14 #include <sys/socket.h>
15 #include <sys/types.h>
22 #define snf_create pcap_create
23 #define snf_platform_finddevs pcap_platform_finddevs
27 snf_set_datalink(pcap_t *p, int dlt)
34 snf_pcap_stats(pcap_t *p, struct pcap_stat *ps)
36 struct snf_ring_stats stats;
39 if ((rc = snf_ring_getstats(p->md.snf_ring, &stats))) {
40 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snf_get_stats: %s",
44 ps->ps_recv = stats.ring_pkt_recv + stats.ring_pkt_overflow;
45 ps->ps_drop = stats.ring_pkt_overflow;
46 ps->ps_ifdrop = stats.nic_pkt_overflow + stats.nic_pkt_bad;
51 snf_platform_cleanup(pcap_t *p)
56 snf_ring_close(p->md.snf_ring);
57 snf_close(p->md.snf_handle);
58 pcap_cleanup_live_common(p);
62 snf_getnonblock(pcap_t *p, char *errbuf)
64 return (p->md.snf_timeout == 0);
68 snf_setnonblock(pcap_t *p, int nonblock, char *errbuf)
71 p->md.snf_timeout = 0;
73 if (p->md.timeout <= 0)
74 p->md.snf_timeout = -1; /* forever */
76 p->md.snf_timeout = p->md.timeout;
81 #define _NSEC_PER_SEC 1000000000
85 snf_timestamp_to_timeval(const int64_t ts_nanosec)
90 return (struct timeval) { 0, 0 };
91 tv.tv_sec = ts_nanosec / _NSEC_PER_SEC;
92 tv.tv_usec = (ts_nanosec % _NSEC_PER_SEC) / 1000;
97 snf_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
99 struct pcap_pkthdr hdr;
100 int i, flags, err, caplen, n;
101 struct snf_recv_req req;
107 while (n < cnt || cnt < 0) {
109 * Has "pcap_breakloop()" been called?
120 err = snf_ring_recv(p->md.snf_ring, p->md.snf_timeout, &req);
123 if (err == EBUSY || err == EAGAIN)
128 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snf_read: %s",
135 if (caplen > p->snapshot)
136 caplen = p->snapshot;
138 if ((p->fcode.bf_insns == NULL) ||
139 bpf_filter(p->fcode.bf_insns, req.pkt_addr, req.length, caplen)) {
140 hdr.ts = snf_timestamp_to_timeval(req.timestamp);
142 hdr.len = req.length;
143 callback(user, &hdr, req.pkt_addr);
151 snf_setfilter(pcap_t *p, struct bpf_program *fp)
156 strncpy(p->errbuf, "setfilter: No filter specified",
161 /* Make our private copy of the filter */
163 if (install_bpf_program(p, fp) < 0)
172 snf_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
174 strlcpy(p->errbuf, "Sending packets isn't supported with snf",
180 snf_activate(pcap_t* p)
182 char *device = p->opt.source;
183 const char *nr = NULL;
187 if (device == NULL) {
188 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
189 "device is NULL: %s", pcap_strerror(errno));
193 /* In Libpcap, we set pshared by default if NUM_RINGS is set to > 1.
194 * Since libpcap isn't thread-safe */
195 if ((nr = getenv("SNF_NUM_RINGS")) && *nr && atoi(nr) > 1)
196 flags |= SNF_F_PSHARED;
200 err = snf_open(p->md.snf_boardnum,
201 0, /* let SNF API parse SNF_NUM_RINGS, if set */
202 NULL, /* default RSS, or use SNF_RSS_FLAGS env */
203 0, /* default to SNF_DATARING_SIZE from env */
204 flags, /* may want pshared */
207 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
208 "snf_open failed: %s", pcap_strerror(err));
212 err = snf_ring_open(p->md.snf_handle, &p->md.snf_ring);
214 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
215 "snf_ring_open failed: %s", pcap_strerror(err));
219 if (p->md.timeout <= 0)
220 p->md.snf_timeout = -1;
222 p->md.snf_timeout = p->md.timeout;
224 err = snf_start(p->md.snf_handle);
226 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
227 "snf_start failed: %s", pcap_strerror(err));
232 * "select()" and "poll()" don't work on snf descriptors.
234 p->selectable_fd = -1;
235 p->linktype = DLT_EN10MB;
236 p->read_op = snf_read;
237 p->inject_op = snf_inject;
238 p->setfilter_op = snf_setfilter;
239 p->setdirection_op = NULL; /* Not implemented.*/
240 p->set_datalink_op = snf_set_datalink;
241 p->getnonblock_op = snf_getnonblock;
242 p->setnonblock_op = snf_setnonblock;
243 p->stats_op = snf_pcap_stats;
244 p->cleanup_op = snf_platform_cleanup;
245 p->md.stat.ps_recv = 0;
246 p->md.stat.ps_drop = 0;
247 p->md.stat.ps_ifdrop = 0;
252 snf_platform_finddevs(pcap_if_t **devlistp, char *errbuf)
255 * There are no platform-specific devices since each device
256 * exists as a regular Ethernet device.
262 snf_create(const char *device, char *ebuf)
266 struct snf_ifaddrs *ifaddrs, *ifa;
269 if (snf_init(SNF_VERSION_API))
273 * Match a given interface name to our list of interface names, from
274 * which we can obtain the intended board number
276 if (snf_getifaddrs(&ifaddrs) || ifaddrs == NULL)
278 devlen = strlen(device) + 1;
281 if (!strncmp(device, ifa->snf_ifa_name, devlen)) {
282 boardnum = ifa->snf_ifa_boardnum;
285 ifa = ifa->snf_ifa_next;
287 snf_freeifaddrs(ifaddrs);
291 * If we can't find the device by name, support the name "snfX"
292 * and "snf10gX" where X is the board number.
294 if (sscanf(device, "snf10g%d", &boardnum) != 1 &&
295 sscanf(device, "snf%d", &boardnum) != 1)
299 p = pcap_create_common(device, ebuf);
303 p->activate_op = snf_activate;
304 p->md.snf_boardnum = boardnum;