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  SCLogError(SC_ERR_RUNMODE, "napatech.auto-config cannot be used in configuration file at the same time as napatech.use-all-streams.");
124  exit(EXIT_FAILURE);
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  SCLogError(SC_ERR_RUNMODE, "napatech auto-config must be enabled when using napatech.use_hw_bypass.");
133  exit(EXIT_FAILURE);
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  exit(EXIT_FAILURE);
149  }
150  snprintf(plive_dev_buf, 9, "nt%d", stream_config[inst].stream_id);
151 
152  if (auto_config) {
153  if (stream_config[inst].is_active) {
155  "Registering Napatech device: %s - active stream found.",
156  plive_dev_buf);
158  "run /opt/napatech3/bin/ntpl -e \"delete=all\" to delete existing stream");
160  "or disable auto-config in the conf file before running.");
161  exit(EXIT_FAILURE);
162  }
163  } else {
164  SCLogInfo("Registering Napatech device: %s - active stream%sfound.",
165  plive_dev_buf, stream_config[inst].is_active ? " " : " NOT ");
166  }
167  LiveRegisterDevice(plive_dev_buf);
168 
169  if (first_stream == 0xffff) {
170  first_stream = stream_config[inst].stream_id;
171  }
172  last_stream = stream_config[inst].stream_id;
173  }
174 
175  /* Napatech stats come from a separate thread. This will surpress
176  * the counters when suricata exits.
177  */
179  return 0;
180 }
181 
182 static void *NapatechConfigParser(const char *device)
183 {
184  /* Expect device to be of the form nt%d where %d is the stream id to use */
185  int dev_len = strlen(device);
186  if (dev_len < 3 || dev_len > 5) {
188  "Could not parse config for device: %s - invalid length", device);
189  return NULL;
190  }
191 
192  struct NapatechStreamDevConf *conf = SCCalloc(1, sizeof (struct NapatechStreamDevConf));
193  if (unlikely(conf == NULL)) {
195  "Failed to allocate memory for NAPATECH device name.");
196  return NULL;
197  }
198 
199  /* device+2 is a pointer to the beginning of the stream id after the constant nt portion */
200  if (StringParseUint16(&conf->stream_id, 10, 0, device + 2) < 0) {
201  SCLogError(SC_ERR_INVALID_VALUE, "Invalid value for stream_id: %s", device + 2);
202  SCFree(conf);
203  return NULL;
204  }
205 
206  /* Set the host buffer allowance for this stream
207  * Right now we just look at the global default - there is no per-stream hba configuration
208  */
209  if (ConfGetInt("napatech.hba", &conf->hba) == 0) {
210  conf->hba = -1;
211  }
212  return (void *) conf;
213 }
214 
215 static int NapatechGetThreadsCount(void *conf __attribute__((unused)))
216 {
217  /* No matter which live device it is there is no reason to ever use more than 1 thread
218  2 or more thread would cause packet duplication */
219  return 1;
220 }
221 
222 static int NapatechInit(int runmode)
223 {
224  int status;
225 
227  TimeModeSetLive();
228 
229  /* Initialize the API and check version compatibility */
230  if ((status = NT_Init(NTAPI_VERSION)) != NT_SUCCESS) {
232  exit(EXIT_FAILURE);
233  }
234 
235  status = NapatechRegisterDeviceStreams();
236  if (status < 0 || num_configured_streams <= 0) {
238  "Unable to find existing Napatech Streams");
239  exit(EXIT_FAILURE);
240  }
241 
242  struct NapatechStreamDevConf *conf =
243  SCCalloc(1, sizeof (struct NapatechStreamDevConf));
244  if (unlikely(conf == NULL)) {
245  SCLogError(SC_ERR_MEM_ALLOC, "Failed to allocate memory for NAPATECH device.");
246  exit(EXIT_FAILURE);
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 (NapatechInitFlowStreams()) {
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  exit(EXIT_FAILURE);
261  }
262 #else
264  "Napatech Hardware Bypass requested in conf but is not enabled by the software.");
265  exit(EXIT_FAILURE);
266 #endif
267  } else {
268  SCLogInfo("Hardware Bypass is disabled in the conf file.");
269  }
270 
271  /* Start a thread to process the statistics */
273 
274  switch (runmode) {
275  case NT_RUNMODE_WORKERS:
276  status = RunModeSetLiveCaptureWorkers(NapatechConfigParser,
277  NapatechGetThreadsCount,
278  "NapatechStream", "NapatechDecode",
279  thread_name_workers, NULL);
280  break;
281  default:
282  status = -1;
283  }
284 
285  if (status != 0) {
286  SCLogError(SC_ERR_RUNMODE, "Runmode start failed");
287  exit(EXIT_FAILURE);
288  }
289  return 0;
290 }
291 
293 {
294  return NapatechInit(NT_RUNMODE_AUTOFP);
295 }
296 
298 {
299  return NapatechInit(NT_RUNMODE_WORKERS);
300 }
301 
302 #endif
thread_name_workers
const char * thread_name_workers
Definition: runmodes.c:63
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:437
RunModeNapatechAutoFp
int RunModeNapatechAutoFp(void)
Definition: runmode-napatech.c:292
StringParseUint16
int StringParseUint16(uint16_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:324
LiveRegisterDevice
int LiveRegisterDevice(const char *dev)
Add a pcap device for monitoring and create structure.
Definition: util-device.c:124
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:341
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:517
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
NapatechStreamDevConf
Definition: source-napatech.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
LiveDeviceHasNoStats
void LiveDeviceHasNoStats()
Definition: util-device.c:343
RunModeInitialize
void RunModeInitialize(void)
Definition: runmodes.c:907
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:40
NapatechStreamDevConf::hba
intmax_t hba
Definition: source-napatech.h:37
SC_ERR_RUNMODE
@ SC_ERR_RUNMODE
Definition: util-error.h:219
NapatechStreamConfig_
Definition: util-napatech.h:42
__attribute__
enum @31 __attribute__
DNP3 application header.
Definition: detect-engine-analyzer.c:583
NapatechGetStreamConfig
int NapatechGetStreamConfig(NapatechStreamConfig stream_config[])
Reads and parses the stream configuration defined in the config file.
Definition: util-napatech.c:835
util-device.h
util-debug.h
util-cpu.h
source-napatech.h
util-affinity.h
util-time.h
stream_config
TcpStreamCnf stream_config
Definition: stream-tcp.c:119
NAPATECH_ERROR
#define NAPATECH_ERROR(err_type, status)
Definition: util-napatech.h:64
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:217
TimeModeSetLive
void TimeModeSetLive(void)
Definition: util-time.c:97
RunModeNapatechGetDefaultMode
const char * RunModeNapatechGetDefaultMode(void)
Definition: runmode-napatech.c:82
RunModeNapatechWorkers
int RunModeNapatechWorkers(void)
Definition: runmode-napatech.c:297
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:419
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
NapatechStartStats
void NapatechStartStats(void)
Definition: util-napatech.c:1206
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
SCFree
#define SCFree(p)
Definition: util-mem.h:61
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