-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPF_RINGInterface.cpp
More file actions
160 lines (125 loc) · 4.55 KB
/
Copy pathPF_RINGInterface.cpp
File metadata and controls
160 lines (125 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
*
* (C) 2013-14 - ntop.org
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include "ntop_includes.h"
#ifdef HAVE_PF_RING
#ifdef __APPLE__
#include <uuid/uuid.h>
#endif
/* **************************************************** */
PF_RINGInterface::PF_RINGInterface(const char *name) : NetworkInterface(name) {
u_int flags = ntop->getGlobals()->getPromiscuousMode() ? PF_RING_PROMISC : 0;
flags |= PF_RING_LONG_HEADER;
flags |= PF_RING_DNA_SYMMETRIC_RSS; /* Note that symmetric RSS is ignored by non-DNA drivers */
#ifdef PF_RING_DO_NOT_PARSE
flags |= PF_RING_DO_NOT_PARSE;
#endif
if(ntop->getPrefs()->are_ixia_timestamps_enabled())
flags |= PF_RING_IXIA_TIMESTAMP;
if((pfring_handle = pfring_open(ifname, ntop->getGlobals()->getSnaplen(), flags)) == NULL) {
throw 1;
} else {
u_int32_t version;
pfring_version(pfring_handle, &version);
ntop->getTrace()->traceEvent(TRACE_NORMAL, "Reading packets from PF_RING v.%d.%d.%d interface %s...",
(version & 0xFFFF0000) >> 16, (version & 0x0000FF00) >> 8, version & 0x000000FF,
ifname);
}
pcap_datalink_type = DLT_EN10MB;
pfring_set_direction(pfring_handle, rx_only_direction);
pfring_set_poll_watermark(pfring_handle, 8);
pfring_set_application_name(pfring_handle, (char*)"ntopng");
}
/* **************************************************** */
PF_RINGInterface::~PF_RINGInterface() {
shutdown();
if(pfring_handle)
pfring_close(pfring_handle);
}
/* **************************************************** */
static void* packetPollLoop(void* ptr) {
PF_RINGInterface *iface = (PF_RINGInterface*)ptr;
pfring *pd = iface->get_pfring_handle();
/* Wait until the initialization completes */
while(!iface->isRunning()) sleep(1);
pfring_enable_ring(pd);
while(iface->idle()) { iface->purgeIdle(time(NULL)); sleep(1); }
while(iface->isRunning()) {
if(pfring_is_pkt_available(pd)) {
u_char *buffer;
struct pfring_pkthdr hdr;
if(pfring_recv(pd, &buffer, 0, &hdr, 0 /* wait_for_packet */) > 0) {
try {
if(hdr.ts.tv_sec == 0) gettimeofday(&hdr.ts, NULL);
iface->packet_dissector((const struct pcap_pkthdr *) &hdr, buffer);
} catch(std::bad_alloc& ba) {
static bool oom_warning_sent = false;
if(!oom_warning_sent) {
ntop->getTrace()->traceEvent(TRACE_WARNING, "Not enough memory");
oom_warning_sent = true;
}
}
}
} else {
usleep(1);
iface->purgeIdle(time(NULL));
}
}
ntop->getTrace()->traceEvent(TRACE_NORMAL, "Terminated packet polling for %s", iface->get_name());
return(NULL);
}
/* **************************************************** */
void PF_RINGInterface::startPacketPolling() {
pthread_create(&pollLoop, NULL, packetPollLoop, (void*)this);
NetworkInterface::startPacketPolling();
}
/* **************************************************** */
void PF_RINGInterface::shutdown() {
void *res;
if(running) {
NetworkInterface::shutdown();
if(pfring_handle) pfring_breakloop(pfring_handle);
pthread_join(pollLoop, &res);
}
}
/* **************************************************** */
u_int PF_RINGInterface::getNumDroppedPackets() {
pfring_stat stats;
if(pfring_stats(pfring_handle, &stats) >= 0) {
#if 0
ntop->getTrace()->traceEvent(TRACE_NORMAL, "[%s][Rcvd: %llu][Drops: %llu][DroppedByFilter: %u]",
ifname, stats.recv, stats.drop, stats.droppedbyfilter);
#endif
return(stats.drop);
}
return(0);
}
/* **************************************************** */
bool PF_RINGInterface::set_packet_filter(char *filter) {
if(pfring_set_bpf_filter(pfring_handle, filter) != 0) {
ntop->getTrace()->traceEvent(TRACE_ERROR, "Unable to set filter %s. Filter ignored.\n", filter);
return(false);
} else {
ntop->getTrace()->traceEvent(TRACE_NORMAL, "Packet capture filter set to \"%s\"", filter);
return(true);
}
}
/* **************************************************** */
#endif /* HAVE_PF_RING */