suricata
runmode-napatech.c
Go to the documentation of this file.
1 /* Copyright (C) 2012-2017 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author nPulse Technologies, LLC.
22  * \author Matt Keeler <mk@npulsetech.com>
23  */
24 
25 #include "suricata-common.h"
26 #include "tm-threads.h"
27 #include "conf.h"
28 #include "runmodes.h"
29 #include "output.h"
30 #include "util-debug.h"
31 #include "util-time.h"
32 #include "util-cpu.h"
33 #include "util-byte.h"
34 #include "util-affinity.h"
35 #include "util-runmodes.h"
36 #include "util-device.h"
37 #include "util-napatech.h"
38 #include "runmode-napatech.h"
39 #include "source-napatech.h" // need NapatechStreamDevConf structure
40 
41 #define NT_RUNMODE_AUTOFP 1
42 #define NT_RUNMODE_WORKERS 2
43 
44 static const char *default_mode = "workers";
45 
46 #ifdef HAVE_NAPATECH
47 
48 #define MAX_STREAMS 256
49 static uint16_t num_configured_streams = 0;
50 static uint16_t first_stream = 0xffff;
51 static uint16_t last_stream = 0xffff;
52 static int auto_config = 0;
53 static int use_hw_bypass = 0;
54 
56 {
57  return num_configured_streams;
58 }
59 
61 {
62  return first_stream;
63 }
64 
66 {
67  return last_stream;
68 }
69 
71 {
72  return (auto_config != 0);
73 }
74 
76 {
77  return (use_hw_bypass != 0);
78 }
79 
80 #endif
81 
83 {
84  return default_mode;
85 }
86 
88 {
89 #ifdef HAVE_NAPATECH
91  "Workers Napatech mode, each thread does all"
92  " tasks from acquisition to logging",
94  return;
95 #endif
96 }
97 
98 
99 #ifdef HAVE_NAPATECH
100 
101 static int NapatechRegisterDeviceStreams(void)
102 {
103  /* Display the configuration mode */
104  int use_all_streams;
105 
106  if (ConfGetBool("napatech.use-all-streams", &use_all_streams) == 0) {
107  SCLogInfo("Could not find napatech.use-all-streams in config file. Defaulting to \"no\".");
108  use_all_streams = 0;
109  }
110 
111  if (ConfGetBool("napatech.auto-config", &auto_config) == 0) {
112  SCLogInfo("napatech.auto-config not found in config file. Defaulting to disabled.");
113  }
114 
115  if (ConfGetBool("napatech.hardware-bypass", &use_hw_bypass) == 0) {
116  SCLogInfo("napatech.hardware-bypass not found in config file. Defaulting to disabled.");
117  }
118 
119  /* use_all_streams uses existing streams created prior to starting Suricata. auto_config
120  * automatically creates streams. Therefore, these two options are mutually exclusive.
121  */
122  if (use_all_streams && auto_config) {
123  FatalError("napatech.auto-config cannot be used in configuration file at the same time as "
124  "napatech.use-all-streams.");
125  }
126 
127  /* to use hardware_bypass we need to configure the streams to be consistent.
128  * with the rest of the configuration. Therefore auto_config is not a valid
129  * option.
130  */
131  if (use_hw_bypass && auto_config == 0) {
132  FatalError("napatech auto-config must be enabled when using napatech.use_hw_bypass.");
133  }
134 
135  /* Get the stream ID's either from the conf or by querying Napatech */
137 
138  uint16_t stream_cnt = NapatechGetStreamConfig(stream_config);
139  num_configured_streams = stream_cnt;
140  SCLogDebug("Configuring %d Napatech Streams...", stream_cnt);
141 
142  for (uint16_t inst = 0; inst < stream_cnt; ++inst) {
143  char *plive_dev_buf = SCCalloc(1, 9);
144  if (unlikely(plive_dev_buf == NULL)) {
145  FatalError("Failed to allocate memory for NAPATECH stream counter.");
146  }
147  snprintf(plive_dev_buf, 9, "nt%d", stream_config[inst].stream_id);
148 
149  if (auto_config) {
150  if (stream_config[inst].is_active) {
151  SCLogError("Registering Napatech device: %s - active stream found.", plive_dev_buf);
152  SCLogError(
153  "run /opt/napatech3/bin/ntpl -e \"delete=all\" to delete existing stream");
154  FatalError("or disable auto-config in the conf file before running.");
155  }
156  } else {
157  SCLogInfo("Registering Napatech device: %s - active stream%sfound.",
158  plive_dev_buf, stream_config[inst].is_active ? " " : " NOT ");
159  }
160  LiveRegisterDevice(plive_dev_buf);
161 
162  if (first_stream == 0xffff) {
163  first_stream = stream_config[inst].stream_id;
164  }
165  last_stream = stream_config[inst].stream_id;
166  }
167 
168  /* Napatech stats come from a separate thread. This will suppress
169  * the counters when suricata exits.
170  */
172  return 0;
173 }
174 
175 static void *NapatechConfigParser(const char *device)
176 {
177  /* Expect device to be of the form nt%d where %d is the stream id to use */
178  int dev_len = strlen(device);
179  if (dev_len < 3 || dev_len > 5) {
180  SCLogError("Could not parse config for device: %s - invalid length", device);
181  return NULL;
182  }
183 
184  struct NapatechStreamDevConf *conf = SCCalloc(1, sizeof (struct NapatechStreamDevConf));
185  if (unlikely(conf == NULL)) {
186  SCLogError("Failed to allocate memory for NAPATECH device name.");
187  return NULL;
188  }
189 
190  /* device+2 is a pointer to the beginning of the stream id after the constant nt portion */
191  if (StringParseUint16(&conf->stream_id, 10, 0, device + 2) < 0) {
192  SCLogError("Invalid value for stream_id: %s", device + 2);
193  SCFree(conf);
194  return NULL;
195  }
196 
197  return (void *) conf;
198 }
199 
200 static int NapatechGetThreadsCount(void *conf __attribute__((unused)))
201 {
202  /* No matter which live device it is there is no reason to ever use more than 1 thread
203  2 or more thread would cause packet duplication */
204  return 1;
205 }
206 
207 static int NapatechInit(int runmode)
208 {
209  int status;
210 
211  TimeModeSetLive();
212 
213  /* Initialize the API and check version compatibility */
214  if ((status = NT_Init(NTAPI_VERSION)) != NT_SUCCESS) {
215  NAPATECH_ERROR(status);
216  exit(EXIT_FAILURE);
217  }
218 
219  status = NapatechRegisterDeviceStreams();
220  if (status < 0 || num_configured_streams <= 0) {
221  FatalError("Unable to find existing Napatech Streams");
222  }
223 
224  struct NapatechStreamDevConf *conf =
225  SCCalloc(1, sizeof (struct NapatechStreamDevConf));
226  if (unlikely(conf == NULL)) {
227  FatalError("Failed to allocate memory for NAPATECH device.");
228  }
229 
230  if (use_hw_bypass) {
231 #ifdef NAPATECH_ENABLE_BYPASS
232  if (NapatechVerifyBypassSupport()) {
233  SCLogInfo("Napatech Hardware Bypass is supported and enabled.");
234  } else {
235  FatalError("Napatech Hardware Bypass requested in conf but is not supported by the "
236  "hardware.");
237  }
238 #else
239  FatalError(
240  "Napatech Hardware Bypass requested in conf but is not enabled by the software.");
241 #endif
242  } else {
243  SCLogInfo("Hardware Bypass is disabled in the conf file.");
244  }
245 
246  /* Start a thread to process the statistics */
248 
249  switch (runmode) {
250  case NT_RUNMODE_WORKERS:
251  status = RunModeSetLiveCaptureWorkers(NapatechConfigParser, NapatechGetThreadsCount,
252  "NapatechStream", "NapatechDecode", thread_name_workers, NULL);
253  break;
254  default:
255  status = -1;
256  }
257 
258  if (status != 0) {
259  FatalError("Runmode start failed");
260  }
261  return 0;
262 }
263 
265 {
266  return NapatechInit(NT_RUNMODE_WORKERS);
267 }
268 
269 #endif
thread_name_workers
const char * thread_name_workers
Definition: runmodes.c:82
util-byte.h
tm-threads.h
LiveRegisterDevice
int LiveRegisterDevice(const char *dev)
Add a pcap device for monitoring and create structure.
Definition: util-device.c:126
RunModeSetLiveCaptureWorkers
int RunModeSetLiveCaptureWorkers(ConfigIfaceParserFunc ConfigParser, ConfigIfaceThreadsCountFunc ModThreadsCount, const char *recv_mod_name, const char *decode_mod_name, const char *thread_name, const char *live_dev)
Definition: util-runmodes.c:321
ConfGetBool
int ConfGetBool(const char *name, int *val)
Retrieve a configuration value as a boolean.
Definition: conf.c:483
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
NapatechStreamDevConf
Definition: source-napatech.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
StringParseUint16
int StringParseUint16(uint16_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:337
NapatechStreamDevConf::stream_id
uint16_t stream_id
Definition: source-napatech.h:36
util-runmodes.h
LiveDeviceHasNoStats
void LiveDeviceHasNoStats(void)
Definition: util-device.c:309
NapatechIsAutoConfigEnabled
bool NapatechIsAutoConfigEnabled(void)
Definition: runmode-napatech.c:70
RUNMODE_NAPATECH
@ RUNMODE_NAPATECH
Definition: runmodes.h:42
stream_config
TcpStreamCnf stream_config
Definition: stream-tcp.c:219
NapatechStreamConfig_
Definition: util-napatech.h:43
__attribute__
enum @22 __attribute__
DNP3 application header.
NapatechGetStreamConfig
int NapatechGetStreamConfig(NapatechStreamConfig stream_config[])
Reads and parses the stream configuration defined in the config file.
Definition: util-napatech.c:804
util-device.h
util-debug.h
util-cpu.h
RunModeRegisterNewRunMode
void RunModeRegisterNewRunMode(enum RunModes runmode, const char *name, const char *description, int(*RunModeFunc)(void), int(*RunModeIsIPSEnabled)(void))
Registers a new runmode.
Definition: runmodes.c:502
source-napatech.h
util-affinity.h
util-time.h
util-napatech.h
conf.h
NapatechGetNumConfiguredStreams
uint16_t NapatechGetNumConfiguredStreams(void)
Definition: runmode-napatech.c:55
runmodes.h
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:224
TimeModeSetLive
void TimeModeSetLive(void)
Definition: util-time.c:99
RunModeNapatechGetDefaultMode
const char * RunModeNapatechGetDefaultMode(void)
Definition: runmode-napatech.c:82
RunModeNapatechWorkers
int RunModeNapatechWorkers(void)
Definition: runmode-napatech.c:264
suricata-common.h
NapatechStartStats
void NapatechStartStats(void)
Definition: util-napatech.c:1185
FatalError
#define FatalError(...)
Definition: util-debug.h:502
NapatechGetNumFirstStream
uint16_t NapatechGetNumFirstStream(void)
Definition: runmode-napatech.c:60
NapatechUseHWBypass
bool NapatechUseHWBypass(void)
Definition: runmode-napatech.c:75
NT_RUNMODE_WORKERS
#define NT_RUNMODE_WORKERS
Definition: runmode-napatech.c:42
NapatechGetNumLastStream
uint16_t NapatechGetNumLastStream(void)
Definition: runmode-napatech.c:65
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
SCFree
#define SCFree(p)
Definition: util-mem.h:61
runmode-napatech.h
RunModeNapatechRegister
void RunModeNapatechRegister(void)
Definition: runmode-napatech.c:87
NAPATECH_ERROR
#define NAPATECH_ERROR(status)
Definition: util-napatech.h:65
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
MAX_STREAMS
#define MAX_STREAMS
Definition: runmode-napatech.c:48
output.h