50 #define NETMAP_WITH_LIBS
52 #define DEBUG_NETMAP_USER
55 #include <net/netmap_user.h>
56 #include <libnetmap.h>
69 FatalError(
"Error creating thread %s: Netmap is not enabled. "
70 "Make sure to pass --enable-netmap to configure when building.",
95 #define POLL_TIMEOUT 100
97 #if defined(__linux__)
98 #define POLL_EVENTS (POLLHUP|POLLRDHUP|POLLERR|POLLNVAL)
101 #define IFF_PPROMISC IFF_PROMISC
105 #define POLL_EVENTS (POLLHUP|POLLERR|POLLNVAL)
108 enum { NETMAP_FLAG_ZERO_COPY = 1, NETMAP_FLAG_EXCL_RING_ACCESS = 2 };
114 typedef struct NetmapDevice_
116 struct nmport_d *nmd;
133 typedef struct NetmapThreadVars_
141 struct bpf_program bpf_prog;
156 uint16_t capture_kernel_packets;
157 uint16_t capture_kernel_drops;
160 typedef TAILQ_HEAD(NetmapDeviceList_, NetmapDevice_) NetmapDeviceList;
170 struct nmreq_port_info_get req;
171 struct nmreq_header hdr;
175 char base_name[IFNAMSIZ];
176 strlcpy(base_name, ifname,
sizeof(base_name));
177 if (strlen(base_name) > 0 &&
178 (base_name[strlen(base_name) - 1] ==
'^' || base_name[strlen(base_name) - 1] ==
'*')) {
179 base_name[strlen(base_name) - 1] =
'\0';
185 int fd = open(
"/dev/netmap", O_RDWR);
187 SCLogError(
"%s: open netmap device failed: %s", ifname, strerror(errno));
192 memset(&req, 0,
sizeof(req));
193 memset(&hdr, 0,
sizeof(hdr));
194 hdr.nr_version = NETMAP_API;
195 hdr.nr_reqtype = NETMAP_REQ_PORT_INFO_GET;
196 hdr.nr_body = (uintptr_t)&req;
197 strlcpy(hdr.nr_name, base_name,
sizeof(hdr.nr_name));
199 if (ioctl(fd, NIOCCTRL, &hdr) != 0) {
201 "Query of netmap HW rings count on %s failed; error: %s", ifname, strerror(errno));
206 if (req.nr_rx_rings == req.nr_tx_rings) {
207 rx_rings = req.nr_rx_rings;
217 static void NetmapDestroyDevice(NetmapDevice *pdev)
219 nmport_close(pdev->nmd);
229 static int NetmapClose(NetmapDevice *dev)
231 NetmapDevice *pdev, *tmp;
239 NetmapDestroyDevice(pdev);
253 static void NetmapCloseAll(
void)
255 NetmapDevice *pdev, *tmp;
260 NetmapDestroyDevice(pdev);
277 static int NetmapOpen(
NetmapIfaceSettings *ns, NetmapDevice **pdevice,
int verbose,
int read,
278 bool zerocopy,
bool soft)
283 char base_name[IFNAMSIZ];
285 if (strlen(base_name) > 0 &&
286 (base_name[strlen(base_name)-1] ==
'^' ||
287 base_name[strlen(base_name)-1] ==
'*'))
289 base_name[strlen(base_name)-1] =
'\0';
294 int if_flags = GetIfaceFlags(base_name);
295 if (if_flags == -1) {
297 SCLogError(
"%s: cannot access network interface: %s", base_name, ns->
iface);
303 if ((if_flags & IFF_UP) == 0) {
304 SCLogError(
"%s: interface is down", base_name);
308 if (ns->
promisc && (if_flags & (IFF_PROMISC|IFF_PPROMISC)) == 0) {
309 if_flags |= IFF_PPROMISC;
310 SetIfaceFlags(base_name, if_flags);
314 NetmapDevice *pdev = NULL, *spdev = NULL;
317 SCLogError(
"%s: memory allocation failed", base_name);
324 const int direction = (read != 1);
331 if (direction == spdev->direction && strcmp(ns->
iface, spdev->ifname) == 0) {
332 ring = spdev->ring + 1;
337 const char *opt_R =
"R";
338 const char *opt_T =
"T";
339 const char *opt_x =
"x";
340 const char *opt_z =
"z";
378 snprintf(optstr,
sizeof(optstr),
"%s%s%s", opt_z, opt_x, direction == 0 ? opt_R : opt_T);
381 if (strncmp(ns->
iface,
"netmap:", 7) == 0) {
382 snprintf(devname,
sizeof(devname),
"%s}%d%s%s",
383 ns->
iface, ring, strlen(optstr) ?
"/" :
"", optstr);
384 }
else if (strlen(ns->
iface) > 5 && strncmp(ns->
iface,
"vale", 4) == 0 && isdigit(ns->
iface[4])) {
385 snprintf(devname,
sizeof(devname),
"%s", ns->
iface);
386 }
else if (ring == 0 && ns->
threads == 1) {
388 snprintf(devname,
sizeof(devname),
"netmap:%s%s%s",
389 ns->
iface, strlen(optstr) ?
"/" :
"", optstr);
390 SCLogDebug(
"device with %s-ring enabled (devname): %s", soft ?
"SW" :
"HW", devname);
397 snprintf(devname,
sizeof(devname),
"netmap:%s%d%s%s@conf:host-rings=%d", ns->
iface,
398 ring, strlen(optstr) ?
"/" :
"", optstr, ns->
threads);
401 snprintf(devname,
sizeof(devname),
"netmap:%s%d%s%s", ns->
iface, ring,
402 strlen(optstr) ?
"/" :
"", optstr);
404 SCLogDebug(
"device with SW-ring enabled (devname): %s", devname);
405 }
else if (ring == 0 && soft) {
409 snprintf(devname,
sizeof(devname),
"netmap:%s-%d%s%s@conf:host-rings=%d", ns->
iface,
410 ring, strlen(optstr) ?
"/" :
"", optstr, ns->
threads);
411 SCLogDebug(
"device with HW-ring enabled (devname): %s", devname);
415 snprintf(devname,
sizeof(devname),
"netmap:%s-%d%s%s", ns->
iface, ring,
416 strlen(optstr) ?
"/" :
"", optstr);
417 SCLogDebug(
"device with HW-ring enabled (devname): %s", devname);
424 pdev->nmd = nmport_prepare(devname);
426 if (pdev->nmd != NULL) {
430 pdev->nmd->reg.nr_flags |= NR_NO_TX_POLL;
434 if (nmport_open_desc(pdev->nmd) < 0) {
436 nmport_close(pdev->nmd);
441 if (pdev->nmd == NULL) {
442 if (errno == EINVAL) {
443 if (opt_z[0] ==
'z') {
445 "%s: dev '%s' got EINVAL: going to retry without 'z'", base_name, devname);
448 }
else if (opt_x[0] ==
'x') {
450 "%s: dev '%s' got EINVAL: going to retry without 'x'", base_name, devname);
458 FatalError(
"opening devname %s failed: %s", devname, strerror(errno));
462 SCLogDebug(
"%s -- cur rings: [%d, %d] first rings: [%d, %d]", devname, pdev->nmd->cur_rx_ring,
463 pdev->nmd->cur_tx_ring, pdev->nmd->first_rx_ring, pdev->nmd->first_tx_ring);
464 pdev->nmd->cur_rx_ring = pdev->nmd->first_rx_ring;
465 pdev->nmd->cur_tx_ring = pdev->nmd->first_tx_ring;
467 SCLogInfo(
"%s: %s opened [fd: %d]", devname, ns->
iface, pdev->nmd->fd);
469 pdev->direction = direction;
486 static inline void NetmapDumpCounters(NetmapThreadVars *ntv)
488 StatsAddUI64(ntv->tv, ntv->capture_kernel_packets, ntv->pkts);
489 StatsAddUI64(ntv->tv, ntv->capture_kernel_drops, ntv->drops);
507 if (initdata == NULL) {
512 NetmapThreadVars *ntv =
SCCalloc(1,
sizeof(*ntv));
519 if (ntv->livedev == NULL) {
530 if (strcmp(
"workers", active_runmode) == 0) {
531 ntv->flags |= NETMAP_FLAG_ZERO_COPY;
533 }
else if (strcmp(
"autofp", active_runmode) == 0) {
534 ntv->flags |= NETMAP_FLAG_EXCL_RING_ACCESS;
539 if (NetmapOpen(&aconf->
in, &ntv->ifsrc, 1, 1, (ntv->flags & NETMAP_FLAG_ZERO_COPY) != 0,
545 if (NetmapOpen(&aconf->
out, &ntv->ifdst, 1, 0, (ntv->flags & NETMAP_FLAG_ZERO_COPY) != 0,
559 char errbuf[PCAP_ERRBUF_SIZE];
565 PCAP_NETMASK_UNKNOWN,
567 sizeof(errbuf)) == -1)
569 SCLogError(
"%s: failed to compile BPF \"%s\": %s", ntv->ifsrc->ifname,
585 NetmapClose(ntv->ifdst);
589 NetmapClose(ntv->ifsrc);
604 static TmEcode NetmapWritePacket(NetmapThreadVars *ntv,
Packet *p)
614 if (ntv->flags & NETMAP_FLAG_EXCL_RING_ACCESS) {
625 (void)ioctl(ntv->ifdst->nmd->fd, NIOCTXSYNC, 0);
628 if (write_tries < 3) {
633 if (ntv->flags & NETMAP_FLAG_EXCL_RING_ACCESS) {
636 SCLogDebug(
"failed to send %s -> %s", ntv->ifsrc->ifname, ntv->ifdst->ifname);
641 SCLogDebug(
"sent successfully: %s(%d)->%s(%d) (%u)", ntv->ifsrc->ifname, ntv->ifsrc->ring,
642 ntv->ifdst->ifname, ntv->ifdst->ring,
GET_PKT_LEN(p));
645 (void)ioctl(ntv->ifdst->nmd->fd, NIOCTXSYNC, 0);
646 if (ntv->flags & NETMAP_FLAG_EXCL_RING_ACCESS) {
656 static void NetmapReleasePacket(
Packet *p)
658 NetmapThreadVars *ntv = (NetmapThreadVars *)p->netmap_v.ntv;
661 NetmapWritePacket(ntv, p);
667 static void NetmapProcessPacket(NetmapThreadVars *ntv,
const struct nm_pkthdr *ph)
669 if (ntv->bpf_prog.bf_len) {
670 struct pcap_pkthdr pkthdr = { {0, 0}, ph->len, ph->len };
671 if (pcap_offline_filter(&ntv->bpf_prog, &pkthdr, ph->buf) == 0) {
686 ntv->bytes += ph->len;
688 if (ntv->flags & NETMAP_FLAG_ZERO_COPY) {
701 p->netmap_v.ntv = ntv;
703 SCLogDebug(
"pktlen: %" PRIu32
" (pkt %p, pkt data %p)",
706 (void)TmThreadsSlotProcessPkt(ntv->tv, ntv->slot, p);
715 static TmEcode NetmapReadPackets(
struct nmport_d *d,
int cnt, NetmapThreadVars *ntv)
717 struct nm_pkthdr hdr;
718 int last_ring = d->last_rx_ring - d->first_rx_ring + 1;
719 int cur_ring, got = 0, cur_rx_ring = d->cur_rx_ring;
721 memset(&hdr, 0,
sizeof(hdr));
722 hdr.flags = NM_MORE_PKTS;
727 for (cur_ring = 0; cur_ring < last_ring &&
cnt != got; cur_ring++, cur_rx_ring++) {
728 struct netmap_ring *ring;
730 if (cur_rx_ring > d->last_rx_ring)
731 cur_rx_ring = d->first_rx_ring;
733 ring = NETMAP_RXRING(d->nifp, cur_rx_ring);
736 for (; !nm_ring_empty(ring) &&
cnt != got; got++) {
739 struct netmap_slot *slot;
742 NetmapProcessPacket(ntv, &hdr);
746 slot = &ring->slot[i];
748 d->cur_rx_ring = cur_rx_ring;
750 oldbuf = hdr.buf = (u_char *)NETMAP_BUF(ring, idx);
751 hdr.len = hdr.caplen = slot->len;
754 while (slot->flags & NS_MOREFRAG) {
759 u_int oldlen = slot->len;
760 i = nm_ring_next(ring, i);
761 slot = &ring->slot[i];
762 hdr.len += slot->len;
763 nbuf = (u_char *)NETMAP_BUF(ring, slot->buf_idx);
765 if (oldbuf != NULL && nbuf - oldbuf == ring->nr_buf_size &&
766 oldlen == ring->nr_buf_size) {
767 hdr.caplen += slot->len;
775 ring->head = ring->cur = nm_ring_next(ring, i);
781 NetmapProcessPacket(ntv, &hdr);
794 NetmapThreadVars *ntv = (NetmapThreadVars *)data;
798 fds.fd = ntv->ifsrc->nmd->fd;
820 SCLogError(
"%s: error polling netmap: %s", ntv->ifsrc->ifname, strerror(errno));
826 NetmapDumpCounters(ntv);
830 TmThreadsCaptureHandleTimeout(
tv, NULL);
834 if (
unlikely(fds.revents & POLL_EVENTS)) {
835 if (fds.revents & POLLERR) {
836 SCLogError(
"%s: error reading netmap data via polling: %s", ntv->ifsrc->ifname,
838 }
else if (fds.revents & POLLNVAL) {
839 SCLogError(
"%s: invalid polling request", ntv->ifsrc->ifname);
844 if (
likely(fds.revents & POLLIN)) {
846 NetmapReadPackets(ntv->ifsrc->nmd, -1, ntv);
849 NetmapDumpCounters(ntv);
853 NetmapDumpCounters(ntv);
863 static void ReceiveNetmapThreadExitStats(
ThreadVars *
tv,
void *data)
866 NetmapThreadVars *ntv = (NetmapThreadVars *)data;
868 NetmapDumpCounters(ntv);
869 SCLogPerf(
"%s: (%s) packets %" PRIu64
", dropped %" PRIu64
", bytes %" PRIu64
"",
870 ntv->ifsrc->ifname,
tv->
name,
884 NetmapThreadVars *ntv = (NetmapThreadVars *)data;
887 NetmapClose(ntv->ifsrc);
891 NetmapClose(ntv->ifdst);
894 if (ntv->bpf_prog.bf_insns) {