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) {
124  "napatech.auto-config cannot be used in configuration file at the same time as 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) {
133  "napatech auto-config must be enabled when using napatech.use_hw_bypass.");
134  }
135 
136  /* Get the stream ID's either from the conf or by querying Napatech */
138 
139  uint16_t stream_cnt = NapatechGetStreamConfig(stream_config);
140  num_configured_streams = stream_cnt;
141  SCLogDebug("Configuring %d Napatech Streams...", stream_cnt);
142 
143  for (uint16_t inst = 0; inst < stream_cnt; ++inst) {
144  char *plive_dev_buf = SCCalloc(1, 9);
145  if (unlikely(plive_dev_buf == NULL)) {
147  "Failed to allocate memory for NAPATECH stream counter.");
148  }
149  snprintf(plive_dev_buf, 9, "nt%d", stream_config[inst].stream_id);
150 
151  if (auto_config) {
152  if (stream_config[inst].is_active) {
154  "Registering Napatech device: %s - active stream found.",
155  plive_dev_buf);
157  "run /opt/napatech3/bin/ntpl -e \"delete=all\" to delete existing stream");
159  "or disable auto-config in the conf file before running.");
160  }
161  } else {
162  SCLogInfo("Registering Napatech device: %s - active stream%sfound.",
163  plive_dev_buf, stream_config[inst].is_active ? " " : " NOT ");
164  }
165  LiveRegisterDevice(plive_dev_buf);
166 
167  if (first_stream == 0xffff) {
168  first_stream = stream_config[inst].stream_id;
169  }
170  last_stream = stream_config[inst].stream_id;
171  }
172 
173  /* Napatech stats come from a separate thread. This will surpress
174  * the counters when suricata exits.
175  */
177  return 0;
178 }
179 
180 static void *NapatechConfigParser(const char *device)
181 {
182  /* Expect device to be of the form nt%d where %d is the stream id to use */
183  int dev_len = strlen(device);
184  if (dev_len < 3 || dev_len > 5) {
186  "Could not parse config for device: %s - invalid length", device);
187  return NULL;
188  }
189 
190  struct NapatechStreamDevConf *conf = SCCalloc(1, sizeof (struct NapatechStreamDevConf));
191  if (unlikely(conf == NULL)) {
193  "Failed to allocate memory for NAPATECH device name.");
194  return NULL;
195  }
196 
197  /* device+2 is a pointer to the beginning of the stream id after the constant nt portion */
198  if (StringParseUint16(&conf->stream_id, 10, 0, device + 2) < 0) {
199  SCLogError(SC_ERR_INVALID_VALUE, "Invalid value for stream_id: %s", device + 2);
200  SCFree(conf);
201  return NULL;
202  }
203 
204  /* Set the host buffer allowance for this stream
205  * Right now we just look at the global default - there is no per-stream hba configuration
206  */
207  if (ConfGetInt("napatech.hba", &conf->hba) == 0) {
208  conf->hba = -1;
209  } else {
211  "Napatech Host Buffer Allocation (hba) will be deprecated in Suricata v7.0.");
212  }
213  return (void *) conf;
214 }
215 
216 static int NapatechGetThreadsCount(void *conf __attribute__((unused)))
217 {
218  /* No matter which live device it is there is no reason to ever use more than 1 thread
219  2 or more thread would cause packet duplication */
220  return 1;
221 }
222 
223 static int NapatechInit(int runmode)
224 {
225  int status;
226 
228  TimeModeSetLive();
229 
230  /* Initialize the API and check version compatibility */
231  if ((status = NT_Init(NTAPI_VERSION)) != NT_SUCCESS) {
233  exit(EXIT_FAILURE);
234  }
235 
236  status = NapatechRegisterDeviceStreams();
237  if (status < 0 || num_configured_streams <= 0) {
239  "Unable to find existing Napatech Streams");
240  }
241 
242  struct NapatechStreamDevConf *conf =
243  SCCalloc(1, sizeof (struct NapatechStreamDevConf));
244  if (unlikely(conf == NULL)) {
246  "Failed to allocate memory for NAPATECH device.");
247  }
248 
249  if ((ConfGetInt("napatech.hba", &conf->hba) != 0) && (conf->hba > 0)) {
250  SCLogInfo("Host Buffer Allowance: %d", (int) conf->hba);
251  }
252 
253  if (use_hw_bypass) {
254 #ifdef NAPATECH_ENABLE_BYPASS
255  if (NapatechVerifyBypassSupport()) {
256  SCLogInfo("Napatech Hardware Bypass is supported and enabled.");
257  } else {
259  "Napatech Hardware Bypass requested in conf but is not supported by the hardware.");
260  }
261 #else
263  "Napatech Hardware Bypass requested in conf but is not enabled by the software.");
264 #endif
265  } else {
266  SCLogInfo("Hardware Bypass is disabled in the conf file.");
267  }
268 
269  /* Start a thread to process the statistics */
271 
272  switch (runmode) {
273  case NT_RUNMODE_WORKERS:
274  status = RunModeSetLiveCaptureWorkers(NapatechConfigParser,
275  NapatechGetThreadsCount,
276  "NapatechStream", "NapatechDecode",
277  thread_name_workers, NULL);
278  break;
279  default:
280  status = -1;
281  }
282 
283  if (status != 0) {
284  FatalError(SC_ERR_FATAL, "Runmode start failed");
285  }
286  return 0;
287 }
288 
290 {
291  return NapatechInit(NT_RUNMODE_AUTOFP);
292 }
293 
295 {
296  return NapatechInit(NT_RUNMODE_WORKERS);
297 }
298 
299 #endif
thread_name_workers
const char * thread_name_workers
Definition: runmodes.c:80
util-byte.h
tm-threads.h
ConfGetInt
int ConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
Definition: conf.c:392
RunModeNapatechAutoFp
int RunModeNapatechAutoFp(void)
Definition: runmode-napatech.c:289
LiveRegisterDevice
int LiveRegisterDevice(const char *dev)
Add a pcap device for monitoring and create structure.
Definition: util-device.c:125
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:330
SC_ERR_INVALID_VALUE
@ SC_ERR_INVALID_VALUE
Definition: util-error.h:160
SC_ERR_NAPATECH_INIT_FAILED
@ SC_ERR_NAPATECH_INIT_FAILED
Definition: util-error.h:251
ConfGetBool
int ConfGetBool(const char *name, int *val)
Retrieve a configuration value as an boolen.
Definition: conf.c:472
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
NapatechStreamDevConf
Definition: source-napatech.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:296
LiveDeviceHasNoStats
void LiveDeviceHasNoStats()
Definition: util-device.c:343
StringParseUint16
int StringParseUint16(uint16_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:336
RunModeInitialize
void RunModeInitialize(void)
Definition: runmodes.c:946
NapatechStreamDevConf::stream_id
uint16_t stream_id
Definition: source-napatech.h:36
util-runmodes.h
NapatechIsAutoConfigEnabled
bool NapatechIsAutoConfigEnabled(void)
Definition: runmode-napatech.c:70
RUNMODE_NAPATECH
@ RUNMODE_NAPATECH
Definition: runmodes.h:41
NapatechStreamDevConf::hba
intmax_t hba
Definition: source-napatech.h:37
SC_WARN_COMPATIBILITY
@ SC_WARN_COMPATIBILITY
Definition: util-error.h:193
stream_config
TcpStreamCnf stream_config
Definition: stream-tcp.c:114
NapatechStreamConfig_
Definition: util-napatech.h:43
__attribute__
enum @24 __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
source-napatech.h
util-affinity.h
util-time.h
NAPATECH_ERROR
#define NAPATECH_ERROR(err_type, status)
Definition: util-napatech.h:65
util-napatech.h
SC_ERR_NAPATECH_PARSE_CONFIG
@ SC_ERR_NAPATECH_PARSE_CONFIG
Definition: util-error.h:255
conf.h
NapatechGetNumConfiguredStreams
uint16_t NapatechGetNumConfiguredStreams(void)
Definition: runmode-napatech.c:55
runmodes.h
NT_RUNMODE_AUTOFP
#define NT_RUNMODE_AUTOFP
Definition: runmode-napatech.c:41
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:215
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:294
suricata-common.h
RunModeRegisterNewRunMode
void RunModeRegisterNewRunMode(enum RunModes runmode, const char *name, const char *description, int(*RunModeFunc)(void))
Registers a new runmode.
Definition: runmodes.c:460
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:255
NapatechStartStats
void NapatechStartStats(void)
Definition: util-napatech.c:1184
FatalError
#define FatalError(x,...)
Definition: util-debug.h:530
NapatechGetNumFirstStream
uint16_t NapatechGetNumFirstStream(void)
Definition: runmode-napatech.c:60
NapatechUseHWBypass
bool NapatechUseHWBypass(void)
Definition: runmode-napatech.c:75
SC_ERR_NAPATECH_STREAMS_REGISTER_FAILED
@ SC_ERR_NAPATECH_STREAMS_REGISTER_FAILED
Definition: util-error.h:253
NT_RUNMODE_WORKERS
#define NT_RUNMODE_WORKERS
Definition: runmode-napatech.c:42
NapatechGetNumLastStream
uint16_t NapatechGetNumLastStream(void)
Definition: runmode-napatech.c:65
SCLogWarning
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:242
SCFree
#define SCFree(p)
Definition: util-mem.h:61
SC_ERR_FATAL
@ SC_ERR_FATAL
Definition: util-error.h:203
runmode-napatech.h
SC_ERR_MEM_ALLOC
@ SC_ERR_MEM_ALLOC
Definition: util-error.h:31
RunModeNapatechRegister
void RunModeNapatechRegister(void)
Definition: runmode-napatech.c:87
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
MAX_STREAMS
#define MAX_STREAMS
Definition: runmode-napatech.c:48
output.h