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-affinity.h"
34 #include "util-runmodes.h"
35 #include "util-device.h"
36 #include "util-napatech.h"
37 #include "runmode-napatech.h"
38 #include "source-napatech.h" // need NapatechStreamDevConf structure
39 
40 #define NT_RUNMODE_AUTOFP 1
41 #define NT_RUNMODE_WORKERS 2
42 
43 static const char *default_mode = "workers";
44 
45 #ifdef HAVE_NAPATECH
46 
47 #define MAX_STREAMS 256
48 static uint16_t num_configured_streams = 0;
49 static uint16_t first_stream = 0xffff;
50 static uint16_t last_stream = 0xffff;
51 static int auto_config = 0;
52 
54 {
55  return num_configured_streams;
56 }
57 
59 {
60  return first_stream;
61 }
62 
64 {
65  return last_stream;
66 }
67 
69 {
70  return (auto_config != 0);
71 }
72 
73 #endif
74 
76 {
77  return default_mode;
78 }
79 
81 {
82 #ifdef HAVE_NAPATECH
84  "Workers Napatech mode, each thread does all"
85  " tasks from acquisition to logging",
87  return;
88 #endif
89 }
90 
91 
92 #ifdef HAVE_NAPATECH
93 
94 static int NapatechRegisterDeviceStreams(void)
95 {
96  /* Display the configuration mode */
97  int use_all_streams;
98 
99  if (ConfGetBool("napatech.use-all-streams", &use_all_streams) == 0) {
100  SCLogInfo("Could not find napatech.use-all-streams in config file. Defaulting to \"no\".");
101  use_all_streams = 0;
102  }
103 
104  if (ConfGetBool("napatech.auto-config", &auto_config) == 0) {
105  SCLogInfo("napatech.auto-config not found in config file. Defaulting to disabled.");
106  }
107 
108  if (use_all_streams && auto_config) {
109  SCLogError(SC_ERR_RUNMODE, "auto-config cannot be used with use-all-streams.");
110  }
111 
112  /* Get the stream ID's either from the conf or by querying Napatech */
114 
115  uint16_t stream_cnt = NapatechGetStreamConfig(stream_config);
116  num_configured_streams = stream_cnt;
117  SCLogDebug("Configuring %d Napatech Streams...", stream_cnt);
118 
119  for (uint16_t inst = 0; inst < stream_cnt; ++inst) {
120  char *plive_dev_buf = SCCalloc(1, 9);
121  if (unlikely(plive_dev_buf == NULL)) {
123  "Failed to allocate memory for NAPATECH stream counter.");
124  exit(EXIT_FAILURE);
125  }
126  snprintf(plive_dev_buf, 9, "nt%d", stream_config[inst].stream_id);
127 
128  if (auto_config) {
129  if (stream_config[inst].is_active) {
131  "Registering Napatech device: %s - active stream found.",
132  plive_dev_buf);
134  "Delete the stream or disable auto-config before running.");
135  exit(EXIT_FAILURE);
136  }
137  } else {
138  SCLogInfo("Registering Napatech device: %s - active stream%sfound.",
139  plive_dev_buf, stream_config[inst].is_active ? " " : " NOT ");
140  }
141  LiveRegisterDevice(plive_dev_buf);
142 
143  if (first_stream == 0xffff) {
144  first_stream = stream_config[inst].stream_id;
145  }
146  last_stream = stream_config[inst].stream_id;
147  }
148 
149  /* Napatech stats come from a separate thread. This will surpress
150  * the counters when suricata exits.
151  */
153  return 0;
154 }
155 
156 static void *NapatechConfigParser(const char *device)
157 {
158  /* Expect device to be of the form nt%d where %d is the stream id to use */
159  int dev_len = strlen(device);
160  if (dev_len < 3 || dev_len > 5) {
162  "Could not parse config for device: %s - invalid length", device);
163  return NULL;
164  }
165 
166  struct NapatechStreamDevConf *conf = SCCalloc(1, sizeof (struct NapatechStreamDevConf));
167  if (unlikely(conf == NULL)) {
169  "Failed to allocate memory for NAPATECH device name.");
170  return NULL;
171  }
172 
173  /* device+5 is a pointer to the beginning of the stream id after the constant nt portion */
174  conf->stream_id = atoi(device + 2);
175 
176  /* Set the host buffer allowance for this stream
177  * Right now we just look at the global default - there is no per-stream hba configuration
178  */
179  if (ConfGetInt("napatech.hba", &conf->hba) == 0) {
180  conf->hba = -1;
181  }
182  return (void *) conf;
183 }
184 
185 static int NapatechGetThreadsCount(void *conf __attribute__((unused)))
186 {
187  /* No matter which live device it is there is no reason to ever use more than 1 thread
188  2 or more thread would cause packet duplication */
189  return 1;
190 }
191 
192 static int NapatechInit(int runmode)
193 {
194  int status;
195 
197  TimeModeSetLive();
198 
199  /* Initialize the API and check version compatibility */
200  if ((status = NT_Init(NTAPI_VERSION)) != NT_SUCCESS) {
202  exit(EXIT_FAILURE);
203  }
204 
205  status = NapatechRegisterDeviceStreams();
206  if (status < 0 || num_configured_streams <= 0) {
208  "Unable to find existing Napatech Streams");
209  exit(EXIT_FAILURE);
210  }
211 
212  struct NapatechStreamDevConf *conf =
213  SCCalloc(1, sizeof (struct NapatechStreamDevConf));
214  if (unlikely(conf == NULL)) {
215  SCLogError(SC_ERR_MEM_ALLOC, "Failed to allocate memory for NAPATECH device.");
216  exit(EXIT_FAILURE);
217  }
218 
219  if ((ConfGetInt("napatech.hba", &conf->hba) != 0) && (conf->hba > 0)) {
220  SCLogInfo("Host Buffer Allowance: %d", (int) conf->hba);
221  }
222 
223  /* Start a thread to process the statistics */
225 
226  switch (runmode) {
227  case NT_RUNMODE_WORKERS:
228  status = RunModeSetLiveCaptureWorkers(NapatechConfigParser,
229  NapatechGetThreadsCount,
230  "NapatechStream", "NapatechDecode",
231  thread_name_workers, NULL);
232  break;
233  default:
234  status = -1;
235  }
236 
237  if (status != 0) {
238  SCLogError(SC_ERR_RUNMODE, "Runmode start failed");
239  exit(EXIT_FAILURE);
240  }
241  return 0;
242 }
243 
245 {
246  return NapatechInit(NT_RUNMODE_AUTOFP);
247 }
248 
250 {
251  return NapatechInit(NT_RUNMODE_WORKERS);
252 }
253 
254 #endif
#define SCLogDebug(...)
Definition: util-debug.h:335
int LiveRegisterDevice(const char *dev)
Add a pcap device for monitoring and create structure.
Definition: util-device.c:111
void RunModeInitialize(void)
Definition: runmodes.c:925
TcpStreamCnf stream_config
Definition: stream-tcp.h:106
void RunModeRegisterNewRunMode(int runmode, const char *name, const char *description, int(*RunModeFunc)(void))
Registers a new runmode.
Definition: runmodes.c:421
#define unlikely(expr)
Definition: util-optimize.h:35
uint16_t NapatechGetNumLastStream(void)
int ConfGetBool(const char *name, int *val)
Retrieve a configuration value as an boolen.
Definition: conf.c:517
void TimeModeSetLive(void)
Definition: util-time.c:82
int NapatechGetStreamConfig(NapatechStreamConfig stream_config[])
const char * RunModeNapatechGetDefaultMode(void)
typedef __attribute__
DNP3 application header.
#define SCCalloc(nm, a)
Definition: util-mem.h:197
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
int RunModeNapatechWorkers(void)
#define NT_RUNMODE_WORKERS
bool NapatechIsAutoConfigEnabled(void)
#define NAPATECH_ERROR(err_type, status)
Definition: util-napatech.h:54
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:254
int ConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
Definition: conf.c:437
void RunModeNapatechRegister(void)
void NapatechStartStats(void)
uint16_t NapatechGetNumFirstStream(void)
#define MAX_STREAMS
int RunModeSetLiveCaptureWorkers(ConfigIfaceParserFunc ConfigParser, ConfigIfaceThreadsCountFunc ModThreadsCount, const char *recv_mod_name, const char *decode_mod_name, const char *thread_name, const char *live_dev)
uint16_t NapatechGetNumConfiguredStreams(void)
#define NT_RUNMODE_AUTOFP
int RunModeNapatechAutoFp(void)
void LiveDeviceHasNoStats()
Definition: util-device.c:312
const char * thread_name_workers
Definition: runmodes.c:63