38 #ifdef NAPATECH_ENABLE_BYPASS
44 typedef struct FlowStatsCounters_
46 uint16_t active_bypass_flows;
47 uint16_t total_bypass_flows;
51 static int bypass_supported;
52 int NapatechIsBypassSupported(
void)
54 return bypass_supported;
62 int NapatechGetNumAdapters(
void)
67 static int num_adapters = -1;
69 if (num_adapters == -1) {
70 if ((status = NT_InfoOpen(&hInfo,
"InfoStream")) != NT_SUCCESS) {
75 hInfoSys.cmd = NT_INFO_CMD_READ_SYSTEM;
76 if ((status = NT_InfoRead(hInfo, &hInfoSys)) != NT_SUCCESS) {
81 num_adapters = hInfoSys.u.system.data.numAdapters;
97 int NapatechVerifyBypassSupport(
void)
101 int num_adapters = NapatechGetNumAdapters();
102 SCLogInfo(
"Found %d Napatech adapters.", num_adapters);
103 NtFlowStream_t hFlowStream;
110 for (adapter = 0; adapter < num_adapters; ++adapter) {
114 NT_FlowOpenAttrInit(&attr);
115 NT_FlowOpenAttrSetAdapterNo(&attr, adapter);
117 snprintf(flow_name,
sizeof(flow_name),
"Flow stream %d", adapter );
118 SCLogInfo(
"Opening flow programming stream: %s\n", flow_name);
119 if ((status = NT_FlowOpen_Attr(&hFlowStream, flow_name, &attr)) != NT_SUCCESS) {
120 SCLogWarning(
"Napatech bypass functionality not supported by the FPGA version on "
121 "adapter %d - disabling support.",
123 bypass_supported = 0;
126 NT_FlowClose(hFlowStream);
129 bypass_supported = 1;
130 return bypass_supported;
144 static void UpdateFlowStats(
146 NtInfoStream_t hInfo,
147 NtStatStream_t hstat_stream,
148 FlowStatsCounters flow_counters,
152 NtStatistics_t hStat;
155 uint64_t programed = 0;
156 uint64_t removed = 0;
159 for (adapter = 0; adapter < NapatechGetNumAdapters(); ++adapter) {
160 hStat.cmd = NT_STATISTICS_READ_CMD_FLOW_V0;
161 hStat.u.flowData_v0.clear = clear_stats;
162 hStat.u.flowData_v0.adapterNo = adapter;
163 if ((status = NT_StatRead(hstat_stream, &hStat)) != NT_SUCCESS) {
167 programed = hStat.u.flowData_v0.learnDone;
168 removed = hStat.u.flowData_v0.unlearnDone
169 + hStat.u.flowData_v0.automaticUnlearnDone
170 + hStat.u.flowData_v0.timeoutUnlearnDone;
173 StatsSetUI64(
tv, flow_counters.active_bypass_flows, programed - removed);
208 #define MAX_HOSTBUFFERS 8
219 static uint16_t TestStreamConfig(
220 NtInfoStream_t hInfo,
221 NtStatStream_t hstat_stream,
225 uint16_t num_active = 0;
227 for (uint16_t inst = 0; inst < num_inst; ++inst) {
232 memset(&stat, 0,
sizeof (NtStatistics_t));
235 stat.cmd = NT_STATISTICS_READ_CMD_USAGE_DATA_V0;
236 stat.u.usageData_v0.streamid = (uint8_t)
stream_config[inst].stream_id;
238 if ((status = NT_StatRead(hstat_stream, &stat)) != NT_SUCCESS) {
243 if (stat.u.usageData_v0.data.numHostBufferUsed > 0) {
274 NtInfoStream_t hInfo,
275 NtStatStream_t hstat_stream,
276 uint16_t num_streams,
283 int enable_stream_stats,
292 NtInfo_t hStreamInfo;
293 NtStatistics_t hStat;
296 hStreamInfo.cmd = NT_INFO_CMD_READ_STREAM;
297 if ((status = NT_InfoRead(hInfo, &hStreamInfo)) != NT_SUCCESS) {
303 if ((num_active = TestStreamConfig(hInfo, hstat_stream,
stream_config, num_streams)) == 0) {
309 uint16_t inst_id = 0;
310 uint32_t stream_cnt = 0;
311 for (stream_cnt = 0; stream_cnt < num_streams; ++stream_cnt) {
312 while (inst_id < num_streams) {
319 if (inst_id == num_streams)
323 memset(&hStat, 0,
sizeof (NtStatistics_t));
324 hStat.cmd = NT_STATISTICS_READ_CMD_USAGE_DATA_V0;
325 hStat.u.usageData_v0.streamid = (uint8_t)
stream_config[inst_id].stream_id;
327 if ((status = NT_StatRead(hstat_stream, &hStat)) != NT_SUCCESS) {
334 uint64_t rx_pkts_total = 0;
335 uint64_t rx_byte_total = 0;
336 uint64_t drop_pkts_total = 0;
337 uint64_t drop_byte_total = 0;
339 for (uint32_t hbCount = 0; hbCount < hStat.u.usageData_v0.data.numHostBufferUsed; hbCount++) {
341 rxPktsStart[stream_id] += hStat.u.usageData_v0.data.hb[hbCount].stat.rx.frames;
342 rxByteStart[stream_id] += hStat.u.usageData_v0.data.hb[hbCount].stat.rx.bytes;
343 dropPktStart[stream_id] += hStat.u.usageData_v0.data.hb[hbCount].stat.drop.frames;
344 dropByteStart[stream_id] += hStat.u.usageData_v0.data.hb[hbCount].stat.drop.bytes;
347 rx_pkts_total += hStat.u.usageData_v0.data.hb[hbCount].stat.rx.frames;
348 rx_byte_total += hStat.u.usageData_v0.data.hb[hbCount].stat.rx.bytes;
349 drop_pkts_total += hStat.u.usageData_v0.data.hb[hbCount].stat.drop.frames;
350 drop_byte_total += hStat.u.usageData_v0.data.hb[hbCount].stat.drop.bytes;
360 if (enable_stream_stats) {
371 for (stream_id = 0; stream_id < num_streams; ++stream_id) {
373 #ifndef NAPATECH_ENABLE_BYPASS
382 #ifndef NAPATECH_ENABLE_BYPASS
396 memset(&hStat, 0,
sizeof (NtStatistics_t));
398 #ifdef NAPATECH_ENABLE_BYPASS
399 hStat.cmd = NT_STATISTICS_READ_CMD_QUERY_V3;
400 hStat.u.query_v3.clear = 0;
403 hStat.cmd = NT_STATISTICS_READ_CMD_QUERY_V2;
404 hStat.u.query_v2.clear = 0;
407 if ((status = NT_StatRead(hstat_stream, &hStat)) != NT_SUCCESS) {
408 if (status == NT_STATUS_TIMEOUT) {
409 SCLogInfo(
"Statistics timed out - will retry next time.");
417 #ifdef NAPATECH_ENABLE_BYPASS
420 uint64_t total_dispatch_host_pkts = 0;
421 uint64_t total_dispatch_host_byte = 0;
422 uint64_t total_dispatch_drop_pkts = 0;
423 uint64_t total_dispatch_drop_byte = 0;
424 uint64_t total_dispatch_fwd_pkts = 0;
425 uint64_t total_dispatch_fwd_byte = 0;
427 for (adapter = 0; adapter < NapatechGetNumAdapters(); ++adapter) {
428 total_dispatch_host_pkts += hStat.u.query_v3.data.adapter.aAdapters[adapter].color.aColor[0].pkts;
429 total_dispatch_host_byte += hStat.u.query_v3.data.adapter.aAdapters[adapter].color.aColor[0].octets;
431 total_dispatch_drop_pkts += hStat.u.query_v3.data.adapter.aAdapters[adapter].color.aColor[1].pkts
432 + hStat.u.query_v3.data.adapter.aAdapters[adapter].color.aColor[3].pkts;
433 total_dispatch_drop_byte += hStat.u.query_v3.data.adapter.aAdapters[adapter].color.aColor[1].octets
434 + hStat.u.query_v3.data.adapter.aAdapters[adapter].color.aColor[3].octets;
436 total_dispatch_fwd_pkts += hStat.u.query_v3.data.adapter.aAdapters[adapter].color.aColor[2].pkts
437 + hStat.u.query_v3.data.adapter.aAdapters[adapter].color.aColor[4].pkts;
438 total_dispatch_fwd_byte += hStat.u.query_v3.data.adapter.aAdapters[adapter].color.aColor[2].octets
439 + hStat.u.query_v3.data.adapter.aAdapters[adapter].color.aColor[4].octets;
442 + hStat.u.query_v3.data.adapter.aAdapters[adapter].color.aColor[1].pkts
443 + hStat.u.query_v3.data.adapter.aAdapters[adapter].color.aColor[2].pkts
444 + hStat.u.query_v3.data.adapter.aAdapters[adapter].color.aColor[3].pkts;
447 + hStat.u.query_v3.data.adapter.aAdapters[adapter].color.aColor[1].octets
448 + hStat.u.query_v3.data.adapter.aAdapters[adapter].color.aColor[2].octets;
478 static void *NapatechStatsLoop(
void *arg)
483 NtInfoStream_t hInfo;
484 NtStatStream_t hstat_stream;
486 int enable_stream_stats = 0;
489 if (
ConfGetBool(
"napatech.inline", &is_inline) == 0) {
493 if (
ConfGetBool(
"napatech.enable-stream-stats", &enable_stream_stats) == 0) {
495 enable_stream_stats = 0;
502 if ((status = NT_InfoOpen(&hInfo,
"StatsLoopInfoStream")) != NT_SUCCESS) {
507 if ((status = NT_StatOpen(&hstat_stream,
"StatsLoopStatsStream")) != NT_SUCCESS) {
512 NtStatistics_t hStat;
513 memset(&hStat, 0,
sizeof (NtStatistics_t));
515 #ifdef NAPATECH_ENABLE_BYPASS
516 hStat.cmd = NT_STATISTICS_READ_CMD_QUERY_V3;
517 hStat.u.query_v3.clear = 1;
519 hStat.cmd = NT_STATISTICS_READ_CMD_QUERY_V2;
520 hStat.u.query_v2.clear = 1;
523 if ((status = NT_StatRead(hstat_stream, &hStat)) != NT_SUCCESS) {
529 memset(&total_counters, 0,
sizeof(total_counters));
532 memset(&dispatch_host, 0,
sizeof(dispatch_host));
535 memset(&dispatch_drop, 0,
sizeof(dispatch_drop));
538 memset(&dispatch_fwd, 0,
sizeof(dispatch_fwd));
557 if (enable_stream_stats) {
558 for (
int i = 0; i < stream_cnt; ++i) {
561 FatalError(
"Failed to allocate memory for NAPATECH stream counter.");
564 snprintf(pkts_buf, 32,
"napa%d.pkts",
stream_config[i].stream_id);
569 FatalError(
"Failed to allocate memory for NAPATECH stream counter.");
571 snprintf(byte_buf, 32,
"napa%d.bytes",
stream_config[i].stream_id);
574 char *drop_pkts_buf =
SCCalloc(1, 32);
575 if (
unlikely(drop_pkts_buf == NULL)) {
576 FatalError(
"Failed to allocate memory for NAPATECH stream counter.");
578 snprintf(drop_pkts_buf, 32,
"napa%d.drop_pkts",
stream_config[i].stream_id);
581 char *drop_byte_buf =
SCCalloc(1, 32);
582 if (
unlikely(drop_byte_buf == NULL)) {
583 FatalError(
"Failed to allocate memory for NAPATECH stream counter.");
585 snprintf(drop_byte_buf, 32,
"napa%d.drop_byte",
stream_config[i].stream_id);
590 #ifdef NAPATECH_ENABLE_BYPASS
591 FlowStatsCounters flow_counters;
592 if (bypass_supported) {
605 #ifdef NAPATECH_ENABLE_BYPASS
606 if (bypass_supported) {
620 if (enable_stream_stats) {
621 for (
int i = 0; i < stream_cnt; ++i) {
631 UpdateFlowStats(
tv, hInfo, hstat_stream, flow_counters, 1);
635 uint32_t num_active = UpdateStreamStats(
tv, hInfo, hstat_stream,
637 dispatch_host, dispatch_drop, dispatch_fwd,
638 is_inline, enable_stream_stats, stream_counters);
641 SCLogInfo(
"num_active: %d, stream_cnt: %d", num_active, stream_cnt);
642 SCLogWarning(
"Some or all of the configured streams are not created. Proceeding with "
649 SCLogDebug(
"NapatechStatsLoop THV_KILL detected");
653 UpdateStreamStats(
tv, hInfo, hstat_stream,
655 dispatch_host, dispatch_drop, dispatch_fwd,
656 is_inline, enable_stream_stats,
659 #ifdef NAPATECH_ENABLE_BYPASS
660 if (bypass_supported) {
661 UpdateFlowStats(
tv, hInfo, hstat_stream, flow_counters, 0);
670 if ((status = NT_InfoClose(hInfo)) != NT_SUCCESS) {
676 if ((status = NT_StatClose(hstat_stream)) != NT_SUCCESS) {
689 #define MAX_HOSTBUFFER 4
690 #define MAX_STREAMS 256
691 #define HB_HIGHWATER 2048 //1982
703 static bool RegisteredStream(uint16_t stream_id, uint16_t num_registered,
706 for (uint16_t reg_id = 0; reg_id < num_registered; ++reg_id) {
707 if (stream_id == registered_streams[reg_id].stream_id) {
719 static uint32_t CountWorkerThreads(
void)
721 int worker_count = 0;
730 if (strcmp(affinity->
val,
"decode-cpu-set") == 0 ||
731 strcmp(affinity->
val,
"stream-cpu-set") == 0 ||
732 strcmp(affinity->
val,
"reject-cpu-set") == 0 ||
733 strcmp(affinity->
val,
"output-cpu-set") == 0) {
737 if (strcmp(affinity->
val,
"worker-cpu-set") == 0) {
747 if (strncmp(lnode->
val,
"all", 4) == 0) {
750 FatalError(
"Only one Napatech port specifier type allowed.");
754 }
else if ((end_str = strchr(lnode->
val,
'-'))) {
757 FatalError(
"Only one Napatech port specifier type allowed.");
764 " worker range start: '%s'",
769 " worker range end: '%s'",
770 (end_str != NULL) ? (
const char *)(end_str + 1) :
"Null");
774 " worker range start: '%d' is greater than end: '%d'",
777 worker_count = end - start + 1;
782 FatalError(
"Napatech port range specifiers cannot be combined with "
783 "individual stream specifiers.");
807 char error_buffer[80];
808 NtStatStream_t hstat_stream;
809 NtStatistics_t hStat;
810 NtInfoStream_t info_stream;
812 uint16_t instance_cnt = 0;
813 int use_all_streams = 0;
814 int set_cpu_affinity = 0;
816 uint16_t stream_id = 0;
826 if (
ConfGetBool(
"napatech.use-all-streams", &use_all_streams) == 0) {
831 if ((status = NT_InfoOpen(&info_stream,
"SuricataStreamInfo")) != NT_SUCCESS) {
836 if ((status = NT_StatOpen(&hstat_stream,
"StatsStream")) != NT_SUCCESS) {
841 if (use_all_streams) {
842 info.cmd = NT_INFO_CMD_READ_STREAM;
843 if ((status = NT_InfoRead(info_stream, &info)) != NT_SUCCESS) {
848 while (instance_cnt < info.u.stream.data.count) {
856 memset(&hStat, 0,
sizeof (NtStatistics_t));
859 hStat.cmd = NT_STATISTICS_READ_CMD_USAGE_DATA_V0;
860 hStat.u.usageData_v0.streamid = (uint8_t) stream_id;
862 if ((status = NT_StatRead(hstat_stream, &hStat)) != NT_SUCCESS) {
864 NT_ExplainError(status, error_buffer,
sizeof (error_buffer));
865 SCLogError(
"NT_StatRead() failed: %s\n", error_buffer);
869 if (hStat.u.usageData_v0.data.numHostBufferUsed == 0) {
881 (void)
ConfGetBool(
"threading.set-cpu-affinity", &set_cpu_affinity);
884 end = CountWorkerThreads() - 1;
888 if ((ntstreams =
ConfGetNode(
"napatech.streams")) == NULL) {
889 SCLogError(
"Failed retrieving napatech.streams from Config");
891 SCLogError(
"if set-cpu-affinity: no in conf then napatech.streams must be "
905 if (stream == NULL) {
906 SCLogError(
"Couldn't Parse Stream Configuration");
910 char *end_str = strchr(stream->
val,
'-');
913 SCLogError(
"Only one Napatech stream range specifier allowed.");
919 (
const char *)stream->
val) < 0) {
921 "stream id start: '%s'",
926 "stream id end: '%s'",
927 (end_str != NULL) ? (
const char *)(end_str + 1) :
"Null");
931 FatalError(
"Napatech range and individual specifiers cannot be combined.");
935 10, 0, (
const char *)stream->
val) < 0) {
946 for (stream_id = start; stream_id <= end; ++stream_id) {
951 memset(&hStat, 0,
sizeof (NtStatistics_t));
954 hStat.cmd = NT_STATISTICS_READ_CMD_USAGE_DATA_V0;
955 hStat.u.usageData_v0.streamid =
958 if ((status = NT_StatRead(hstat_stream, &hStat)) != NT_SUCCESS) {
963 if (hStat.u.usageData_v0.data.numHostBufferUsed > 0) {
971 if ((status = NT_StatClose(hstat_stream)) != NT_SUCCESS) {
976 if ((status = NT_InfoClose(info_stream)) != NT_SUCCESS) {
984 static void *NapatechBufMonitorLoop(
void *arg)
988 NtInfo_t hStreamInfo;
989 NtStatistics_t hStat;
990 NtInfoStream_t hInfo;
991 NtStatStream_t hstat_stream;
994 const uint32_t alertInterval = 25;
996 #ifndef NAPATECH_ENABLE_BYPASS
1007 if ((status = NT_InfoOpen(&hInfo,
"InfoStream")) != NT_SUCCESS) {
1012 if ((status = NT_StatOpen(&hstat_stream,
"StatsStream")) != NT_SUCCESS) {
1018 hStreamInfo.cmd = NT_INFO_CMD_READ_STREAM;
1019 if ((status = NT_InfoRead(hInfo, &hStreamInfo)) != NT_SUCCESS) {
1026 if (num_registered == -1) {
1033 SCLogDebug(
"NapatechBufMonitorLoop THV_KILL detected");
1040 hStreamInfo.cmd = NT_INFO_CMD_READ_STREAM;
1041 if ((status = NT_InfoRead(hInfo, &hStreamInfo)) != NT_SUCCESS) {
1046 char pktCntStr[4096];
1047 memset(pktCntStr, 0,
sizeof (pktCntStr));
1049 uint32_t stream_id = 0;
1050 uint32_t stream_cnt = 0;
1051 uint32_t num_streams = hStreamInfo.u.stream.data.count;
1053 for (stream_cnt = 0; stream_cnt < num_streams; ++stream_cnt) {
1058 hStat.cmd = NT_STATISTICS_READ_CMD_USAGE_DATA_V0;
1059 hStat.u.usageData_v0.streamid = (uint8_t) stream_id;
1061 if ((status = NT_StatRead(hstat_stream, &hStat)) != NT_SUCCESS) {
1066 if (hStat.u.usageData_v0.data.numHostBufferUsed == 0) {
1070 }
while (hStat.u.usageData_v0.data.numHostBufferUsed == 0);
1072 if (RegisteredStream(stream_id, num_registered, registered_streams)) {
1074 #ifndef NAPATECH_ENABLE_BYPASS
1075 ave_OB_fill_level[stream_id] = 0;
1078 ave_HB_fill_level[stream_id] = 0;
1080 for (uint32_t hb_count = 0; hb_count < hStat.u.usageData_v0.data.numHostBufferUsed; hb_count++) {
1082 #ifndef NAPATECH_ENABLE_BYPASS
1083 OB_fill_level[hb_count] =
1084 ((100 * hStat.u.usageData_v0.data.hb[hb_count].onboardBuffering.used) /
1085 hStat.u.usageData_v0.data.hb[hb_count].onboardBuffering.size);
1087 if (OB_fill_level[hb_count] > 100) {
1088 OB_fill_level[hb_count] = 100;
1091 uint32_t bufSize = hStat.u.usageData_v0.data.hb[hb_count].enQueuedAdapter / 1024
1092 + hStat.u.usageData_v0.data.hb[hb_count].deQueued / 1024
1093 + hStat.u.usageData_v0.data.hb[hb_count].enQueued / 1024
1096 HB_fill_level[hb_count] = (uint32_t)
1097 ((100 * hStat.u.usageData_v0.data.hb[hb_count].deQueued / 1024) /
1100 #ifndef NAPATECH_ENABLE_BYPASS
1101 ave_OB_fill_level[stream_id] += OB_fill_level[hb_count];
1104 ave_HB_fill_level[stream_id] += HB_fill_level[hb_count];
1107 #ifndef NAPATECH_ENABLE_BYPASS
1108 ave_OB_fill_level[stream_id] /= hStat.u.usageData_v0.data.numHostBufferUsed;
1111 ave_HB_fill_level[stream_id] /= hStat.u.usageData_v0.data.numHostBufferUsed;
1114 if (ave_HB_fill_level[stream_id] >= (HB_alert_level[stream_id] + alertInterval)) {
1116 while (ave_HB_fill_level[stream_id] >= HB_alert_level[stream_id] + alertInterval) {
1117 HB_alert_level[stream_id] += alertInterval;
1119 SCLogPerf(
"nt%d - Increasing Host Buffer Fill Level : %4d%%",
1120 stream_id, ave_HB_fill_level[stream_id] - 1);
1123 if (HB_alert_level[stream_id] > 0) {
1124 if ((ave_HB_fill_level[stream_id] <= (HB_alert_level[stream_id] - alertInterval))) {
1125 SCLogPerf(
"nt%d - Decreasing Host Buffer Fill Level: %4d%%",
1126 stream_id, ave_HB_fill_level[stream_id]);
1128 while (ave_HB_fill_level[stream_id] <= (HB_alert_level[stream_id] - alertInterval)) {
1129 if ((HB_alert_level[stream_id]) > 0) {
1130 HB_alert_level[stream_id] -= alertInterval;
1136 #ifndef NAPATECH_ENABLE_BYPASS
1138 if (ave_OB_fill_level[stream_id] >= (OB_alert_level[stream_id] + alertInterval)) {
1139 while (ave_OB_fill_level[stream_id] >= OB_alert_level[stream_id] + alertInterval) {
1140 OB_alert_level[stream_id] += alertInterval;
1143 SCLogPerf(
"nt%d - Increasing Adapter SDRAM Fill Level: %4d%%",
1144 stream_id, ave_OB_fill_level[stream_id]);
1147 if (OB_alert_level[stream_id] > 0) {
1148 if ((ave_OB_fill_level[stream_id] <= (OB_alert_level[stream_id] - alertInterval))) {
1149 SCLogPerf(
"nt%d - Decreasing Adapter SDRAM Fill Level : %4d%%",
1150 stream_id, ave_OB_fill_level[stream_id]);
1152 while (ave_OB_fill_level[stream_id] <= (OB_alert_level[stream_id] - alertInterval)) {
1153 if ((OB_alert_level[stream_id]) > 0) {
1154 OB_alert_level[stream_id] -= alertInterval;
1165 if ((status = NT_InfoClose(hInfo)) != NT_SUCCESS) {
1171 if ((status = NT_StatClose(hstat_stream)) != NT_SUCCESS) {
1191 "custom", NapatechStatsLoop, 0);
1193 if (stats_tv == NULL) {
1194 FatalError(
"Error creating a thread for NapatechStats - Killing engine.");
1198 FatalError(
"Failed to spawn thread for NapatechStats - Killing engine.");
1201 #ifdef NAPATECH_ENABLE_BYPASS
1202 if (bypass_supported) {
1203 SCLogInfo(
"Napatech bypass functionality enabled.");
1210 "custom", NapatechBufMonitorLoop, 0);
1212 if (buf_monitor_tv == NULL) {
1213 FatalError(
"Error creating a thread for NapatechBufMonitor - Killing engine.");
1217 FatalError(
"Failed to spawn thread for NapatechBufMonitor - Killing engine.");
1225 uint32_t status = 0;
1226 static NtConfigStream_t hconfig;
1229 snprintf(ntpl_cmd, 64,
"setup[numanode=%d] = streamid == %d", numa, stream);
1231 NtNtplInfo_t ntpl_info;
1233 if ((status = NT_ConfigOpen(&hconfig,
"ConfigStream")) != NT_SUCCESS) {
1238 if ((status = NT_NTPL(hconfig, ntpl_cmd, &ntpl_info, NT_NTPL_PARSER_VALIDATE_NORMAL)) == NT_SUCCESS) {
1239 status = ntpl_info.ntplId;
1249 static uint32_t NapatechSetHashmode(
void)
1251 uint32_t status = 0;
1252 const char *hash_mode;
1253 static NtConfigStream_t hconfig;
1255 NtNtplInfo_t ntpl_info;
1257 uint32_t filter_id = 0;
1260 ConfGet(
"napatech.hashmode", &hash_mode);
1262 snprintf(ntpl_cmd, 64,
"hashmode = %s", hash_mode);
1265 if ((status = NT_ConfigOpen(&hconfig,
"ConfigStream")) != NT_SUCCESS) {
1270 if ((status = NT_NTPL(hconfig, ntpl_cmd, &ntpl_info,
1271 NT_NTPL_PARSER_VALIDATE_NORMAL)) == NT_SUCCESS) {
1272 filter_id = ntpl_info.ntplId;
1273 SCLogConfig(
"Napatech hashmode: %s ID: %d", hash_mode, status);
1282 static uint32_t GetStreamNUMAs(uint32_t stream_id,
int stream_numas[])
1284 NtStatistics_t hStat;
1285 NtStatStream_t hstat_stream;
1289 stream_numas[i] = -1;
1291 if ((status = NT_StatOpen(&hstat_stream,
"StatsStream")) != NT_SUCCESS) {
1296 char pktCntStr[4096];
1297 memset(pktCntStr, 0,
sizeof (pktCntStr));
1301 hStat.cmd = NT_STATISTICS_READ_CMD_USAGE_DATA_V0;
1302 hStat.u.usageData_v0.streamid = (uint8_t) stream_id;
1304 if ((status = NT_StatRead(hstat_stream, &hStat)) != NT_SUCCESS) {
1309 for (uint32_t hb_id = 0; hb_id < hStat.u.usageData_v0.data.numHostBufferUsed; ++hb_id) {
1310 stream_numas[hb_id] = hStat.u.usageData_v0.data.hb[hb_id].numaNode;
1313 return hStat.u.usageData_v0.data.numHostBufferUsed;
1316 static int NapatechSetFilter(NtConfigStream_t hconfig,
char *ntpl_cmd)
1319 int local_filter_id = 0;
1321 NtNtplInfo_t ntpl_info;
1322 if ((status = NT_NTPL(hconfig, ntpl_cmd, &ntpl_info,
1323 NT_NTPL_PARSER_VALIDATE_NORMAL)) == NT_SUCCESS) {
1324 SCLogConfig(
"NTPL filter assignment \"%s\" returned filter id %4d",
1325 ntpl_cmd, local_filter_id);
1331 return local_filter_id;
1336 uint32_t status = 0;
1337 static NtConfigStream_t hconfig;
1339 NtNtplInfo_t ntpl_info;
1341 if ((status = NT_ConfigOpen(&hconfig,
"ConfigStream")) != NT_SUCCESS) {
1346 snprintf(ntpl_cmd, 64,
"delete = all");
1347 if ((status = NT_NTPL(hconfig, ntpl_cmd, &ntpl_info,
1348 NT_NTPL_PARSER_VALIDATE_NORMAL)) == NT_SUCCESS) {
1349 status = ntpl_info.ntplId;
1355 NT_ConfigClose(hconfig);
1363 #define PORTS_SPEC_SIZE 64
1365 struct ports_spec_s {
1372 ports_spec.all =
false;
1377 NtConfigStream_t hconfig;
1380 #ifdef NAPATECH_ENABLE_BYPASS
1384 char span_ports[128];
1385 memset(span_ports, 0,
sizeof(span_ports));
1387 if (
ConfGetBool(
"napatech.inline", &is_inline) == 0) {
1391 NapatechSetHashmode();
1393 if ((status = NT_ConfigOpen(&hconfig,
"ConfigStream")) != NT_SUCCESS) {
1398 if (first_stream == last_stream) {
1399 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1400 "Setup[state=inactive] = StreamId == %d",
1403 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1404 "Setup[state=inactive] = StreamId == (%d..%d)",
1405 first_stream, last_stream);
1407 NapatechSetFilter(hconfig, ntpl_cmd);
1409 #ifdef NAPATECH_ENABLE_BYPASS
1411 SCLogInfo(
"Napatech Hardware Bypass enabled.");
1415 SCLogInfo(
"Napatech Hardware Bypass requested in conf but is not available.");
1418 SCLogInfo(
"Napatech Hardware Bypass disabled.");
1423 SCLogInfo(
"Napatech configured for inline mode.");
1426 SCLogInfo(
"Napatech configured for passive (non-inline) mode.");
1432 if ((ntports =
ConfGetNode(
"napatech.ports")) == NULL) {
1433 FatalError(
"Failed retrieving napatech.ports from Conf");
1441 SCLogInfo(
"Listening on the following Napatech ports:");
1447 FatalError(
"Couldn't Parse Port Configuration");
1451 #ifdef NAPATECH_ENABLE_BYPASS
1452 if (strchr(port->
val,
'-')) {
1456 FatalError(
"Invalid value '%s' in napatech.ports specification in conf file.",
1461 strchr(port->
val,
'-') + 1) == -1) {
1462 FatalError(
"Invalid value '%s' in napatech.ports specification in conf file.",
1466 if (ports_spec.first[iteration] == ports_spec.second[iteration]) {
1469 "Error with napatech.ports in conf file. When running in inline "
1470 "mode the two ports specifying a segment must be different.");
1473 is_span_port[ports_spec.first[iteration]] = 1;
1475 if (strlen(span_ports) == 0) {
1476 snprintf(span_ports,
sizeof (span_ports),
"%d", ports_spec.first[iteration]);
1479 snprintf(temp,
sizeof(temp),
",%d", ports_spec.first[iteration]);
1480 strlcat(span_ports, temp,
sizeof(span_ports));
1487 SCLogError(
"Invalid napatech.ports specification in conf file.");
1488 SCLogError(
"Two ports on a segment must reside on the same adapter. port %d "
1489 "is on adapter %d, port %d is on adapter %d.",
1490 ports_spec.first[iteration],
1492 ports_spec.second[iteration],
1498 if (ports_spec.first[iteration] == ports_spec.second[iteration]) {
1499 SCLogInfo(
" span_port: %d", ports_spec.first[iteration]);
1501 SCLogInfo(
" %s: %d - %d", is_inline ?
"inline_ports" :
"tap_ports", ports_spec.first[iteration], ports_spec.second[iteration]);
1504 if (iteration == 0) {
1505 if (ports_spec.first[iteration] == ports_spec.second[iteration]) {
1506 snprintf(ports_spec.str, sizeof (ports_spec.str),
"%d", ports_spec.first[iteration]);
1508 snprintf(ports_spec.str, sizeof (ports_spec.str),
"%d,%d", ports_spec.first[iteration], ports_spec.second[iteration]);
1512 if (ports_spec.first[iteration] == ports_spec.second[iteration]) {
1513 snprintf(temp,
sizeof(temp),
",%d", ports_spec.first[iteration]);
1515 snprintf(temp,
sizeof(temp),
",%d,%d", ports_spec.first[iteration], ports_spec.second[iteration]);
1517 strlcat(ports_spec.str, temp,
sizeof(ports_spec.str));
1520 FatalError(
"When using hardware flow bypass ports must be specified as segments. "
1521 "E.g. ports: [0-1, 0-2]");
1525 if (strncmp(port->
val,
"all", 3) == 0) {
1528 FatalError(
"Only one Napatech port specifier type is allowed.");
1532 ports_spec.all =
true;
1533 snprintf(ports_spec.str, sizeof (ports_spec.str),
"all");
1534 }
else if (strchr(port->
val,
'-')) {
1537 FatalError(
"Only one Napatech port specifier is allowed when hardware bypass "
1538 "is disabled. (E.g. ports: [0-4], NOT ports: [0-1,2-3])");
1543 FatalError(
"Invalid value '%s' in napatech.ports specification in conf file.",
1548 strchr(port->
val,
'-') + 1) == -1) {
1549 FatalError(
"Invalid value '%s' in napatech.ports specification in conf file.",
1553 snprintf(ports_spec.str, sizeof (ports_spec.str),
"(%d..%d)", ports_spec.first[iteration], ports_spec.second[iteration]);
1557 FatalError(
"Napatech port range specifiers cannot be combined with individual "
1558 "stream specifiers.");
1563 FatalError(
"Invalid value '%s' in napatech.ports specification in conf file.",
1568 if (iteration == 0) {
1569 snprintf(ports_spec.str, sizeof (ports_spec.str),
"%s", port->
val);
1571 strlcat(ports_spec.str,
",",
sizeof(ports_spec.str));
1572 strlcat(ports_spec.str, port->
val,
sizeof(ports_spec.str));
1579 #ifdef NAPATECH_ENABLE_BYPASS
1580 if (bypass_supported) {
1582 char inline_setup_cmd[512];
1583 if (first_stream == last_stream) {
1584 snprintf(inline_setup_cmd,
sizeof (ntpl_cmd),
1585 "Setup[TxDescriptor=Dyn;TxPorts=%s;RxCRC=False;TxPortPos=112;UseWL=True] = StreamId == %d",
1586 ports_spec.str, first_stream);
1588 snprintf(inline_setup_cmd,
sizeof (ntpl_cmd),
1589 "Setup[TxDescriptor=Dyn;TxPorts=%s;RxCRC=False;TxPortPos=112;UseWL=True] = StreamId == (%d..%d)",
1590 ports_spec.str, first_stream, last_stream);
1592 NapatechSetFilter(hconfig, inline_setup_cmd);
1595 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1596 "assign[priority=3;streamid=(%d..%d);colormask=0x10000000;"
1597 "Descriptor=DYN3,length=24,colorbits=32,Offset0=Layer3Header[0],Offset1=Layer4Header[0]]= %s%s",
1598 first_stream, last_stream, ports_spec.all ?
"" :
"port==", ports_spec.str);
1599 NapatechSetFilter(hconfig, ntpl_cmd);
1602 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1603 "assign[priority=2;streamid=(%d..%d);colormask=0x11000000;"
1604 "Descriptor=DYN3,length=24,colorbits=32,Offset0=Layer3Header[0],Offset1=Layer4Header[0]"
1605 "]= %s%s and (Layer3Protocol==IPV4)",
1606 first_stream, last_stream, ports_spec.all ?
"" :
"port==", ports_spec.str);
1607 NapatechSetFilter(hconfig, ntpl_cmd);
1610 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1611 "assign[priority=2;streamid=(%d..%d);colormask=0x14000000;"
1612 "Descriptor=DYN3,length=24,colorbits=32,Offset0=Layer3Header[0],Offset1=Layer4Header[0]]= %s%s and (Layer3Protocol==IPV6)",
1613 first_stream, last_stream, ports_spec.all ?
"" :
"port==", ports_spec.str);
1614 NapatechSetFilter(hconfig, ntpl_cmd);
1616 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1617 "assign[priority=2;streamid=(%d..%d);colormask=0x10100000;"
1618 "Descriptor=DYN3,length=24,colorbits=32,Offset0=Layer3Header[0],Offset1=Layer4Header[0]]= %s%s and (Layer4Protocol==TCP)",
1619 first_stream, last_stream, ports_spec.all ?
"" :
"port==", ports_spec.str);
1620 NapatechSetFilter(hconfig, ntpl_cmd);
1622 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1623 "assign[priority=2;streamid=(%d..%d);colormask=0x10200000;"
1624 "Descriptor=DYN3,length=24,colorbits=32,Offset0=Layer3Header[0],Offset1=Layer4Header[0]"
1625 "]= %s%s and (Layer4Protocol==UDP)",
1626 first_stream, last_stream, ports_spec.all ?
"" :
"port==", ports_spec.str);
1627 NapatechSetFilter(hconfig, ntpl_cmd);
1629 if (strlen(span_ports) > 0) {
1630 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1631 "assign[priority=2;streamid=(%d..%d);colormask=0x00001000;"
1632 "Descriptor=DYN3,length=24,colorbits=32,Offset0=Layer3Header[0],Offset1=Layer4Header[0]"
1634 first_stream, last_stream, span_ports);
1635 NapatechSetFilter(hconfig, ntpl_cmd);
1638 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1639 "KeyType[name=KT%u]={sw_32_32,sw_16_16}",
1640 NAPATECH_KEYTYPE_IPV4);
1641 NapatechSetFilter(hconfig, ntpl_cmd);
1643 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1644 "KeyDef[name=KDEF%u;KeyType=KT%u;ipprotocolfield=OUTER]=(Layer3Header[12]/32/32,Layer4Header[0]/16/16)",
1645 NAPATECH_KEYTYPE_IPV4, NAPATECH_KEYTYPE_IPV4);
1646 NapatechSetFilter(hconfig, ntpl_cmd);
1648 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1649 "KeyType[name=KT%u]={32,32,16,16}",
1650 NAPATECH_KEYTYPE_IPV4_SPAN);
1651 NapatechSetFilter(hconfig, ntpl_cmd);
1653 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1654 "KeyDef[name=KDEF%u;KeyType=KT%u;ipprotocolfield=OUTER;keysort=sorted]=(Layer3Header[12]/32,Layer3Header[16]/32,Layer4Header[0]/16,Layer4Header[2]/16)",
1655 NAPATECH_KEYTYPE_IPV4_SPAN, NAPATECH_KEYTYPE_IPV4_SPAN);
1656 NapatechSetFilter(hconfig, ntpl_cmd);
1659 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1660 "KeyType[name=KT%u]={sw_128_128,sw_16_16}",
1661 NAPATECH_KEYTYPE_IPV6);
1662 NapatechSetFilter(hconfig, ntpl_cmd);
1664 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1665 "KeyDef[name=KDEF%u;KeyType=KT%u;ipprotocolfield=OUTER]=(Layer3Header[8]/128/128,Layer4Header[0]/16/16)",
1666 NAPATECH_KEYTYPE_IPV6, NAPATECH_KEYTYPE_IPV6);
1667 NapatechSetFilter(hconfig, ntpl_cmd);
1670 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1671 "KeyType[name=KT%u]={128,128,16,16}",
1672 NAPATECH_KEYTYPE_IPV6_SPAN);
1673 NapatechSetFilter(hconfig, ntpl_cmd);
1675 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1676 "KeyDef[name=KDEF%u;KeyType=KT%u;ipprotocolfield=OUTER;keysort=sorted]=(Layer3Header[8]/128,Layer3Header[24]/128,Layer4Header[0]/16,Layer4Header[2]/16)",
1677 NAPATECH_KEYTYPE_IPV6_SPAN, NAPATECH_KEYTYPE_IPV6_SPAN);
1678 NapatechSetFilter(hconfig, ntpl_cmd);
1682 char ports_ntpl_a[64];
1683 char ports_ntpl_b[64];
1684 memset(ports_ntpl_a, 0,
sizeof(ports_ntpl_a));
1685 memset(ports_ntpl_b, 0,
sizeof(ports_ntpl_b));
1687 for (pair = 0; pair < iteration; ++pair) {
1690 if (!is_span_port[ports_spec.first[pair]]) {
1691 snprintf(port_str,
sizeof(port_str),
"%s%u ", strlen(ports_ntpl_a) == 0 ?
"" :
",", ports_spec.first[pair]);
1692 strlcat(ports_ntpl_a, port_str,
sizeof(ports_ntpl_a));
1694 snprintf(port_str,
sizeof(port_str),
"%s%u ", strlen(ports_ntpl_b) == 0 ?
"" :
",", ports_spec.second[pair]);
1695 strlcat(ports_ntpl_b, port_str,
sizeof(ports_ntpl_b));
1699 if (strlen(ports_ntpl_a) > 0) {
1701 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1702 "assign[priority=1;streamid=drop;colormask=0x1]=(Layer3Protocol==IPV4)and(port == %s)and(Key(KDEF%u,KeyID=%u)==%u)",
1704 NAPATECH_KEYTYPE_IPV4,
1705 NAPATECH_KEYTYPE_IPV4,
1706 NAPATECH_FLOWTYPE_DROP);
1707 NapatechSetFilter(hconfig, ntpl_cmd);
1710 if (strlen(ports_ntpl_b) > 0) {
1712 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1713 "assign[priority=1;streamid=drop;colormask=0x1]=(Layer3Protocol==IPV4)and(port == %s)and(Key(KDEF%u,KeyID=%u,fieldaction=swap)==%u)",
1715 NAPATECH_KEYTYPE_IPV4,
1716 NAPATECH_KEYTYPE_IPV4,
1717 NAPATECH_FLOWTYPE_DROP);
1718 NapatechSetFilter(hconfig, ntpl_cmd);
1721 if (strlen(span_ports) > 0) {
1723 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1724 "assign[priority=1;streamid=drop;colormask=0x1]=(Layer3Protocol==IPV4)and(port == %s)and(Key(KDEF%u,KeyID=%u)==%u)",
1726 NAPATECH_KEYTYPE_IPV4_SPAN,
1727 NAPATECH_KEYTYPE_IPV4_SPAN,
1728 NAPATECH_FLOWTYPE_DROP);
1729 NapatechSetFilter(hconfig, ntpl_cmd);
1733 for (pair = 0; pair < iteration; ++pair) {
1735 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1736 "assign[priority=1;streamid=drop;DestinationPort=%d;colormask=0x2]=(Layer3Protocol==IPV4)and(port == %d)and(Key(KDEF%u,KeyID=%u)==%u)",
1737 ports_spec.second[pair],
1738 ports_spec.first[pair],
1739 NAPATECH_KEYTYPE_IPV4,
1740 NAPATECH_KEYTYPE_IPV4,
1741 NAPATECH_FLOWTYPE_PASS);
1742 NapatechSetFilter(hconfig, ntpl_cmd);
1744 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1745 "assign[priority=1;streamid=drop;DestinationPort=%d;colormask=0x2]=(Layer3Protocol==IPV4)and(port == %d)and(Key(KDEF%u,KeyID=%u,fieldaction=swap)==%u)",
1746 ports_spec.first[pair],
1747 ports_spec.second[pair],
1748 NAPATECH_KEYTYPE_IPV4,
1749 NAPATECH_KEYTYPE_IPV4,
1750 NAPATECH_FLOWTYPE_PASS);
1751 NapatechSetFilter(hconfig, ntpl_cmd);
1755 if (strlen(ports_ntpl_a) > 0) {
1757 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1758 "assign[priority=1;streamid=drop;colormask=0x1]=(Layer3Protocol==IPV6)and(port == %s)and(Key(KDEF%u,KeyID=%u)==%u)",
1760 NAPATECH_KEYTYPE_IPV6,
1761 NAPATECH_KEYTYPE_IPV6,
1762 NAPATECH_FLOWTYPE_DROP);
1763 NapatechSetFilter(hconfig, ntpl_cmd);
1766 if (strlen(ports_ntpl_b) > 0) {
1768 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1769 "assign[priority=1;streamid=drop;colormask=0x1]=(Layer3Protocol==IPV6)and(port == %s)and(Key(KDEF%u,KeyID=%u,fieldaction=swap)==%u)",
1771 NAPATECH_KEYTYPE_IPV6,
1772 NAPATECH_KEYTYPE_IPV6,
1773 NAPATECH_FLOWTYPE_DROP);
1774 NapatechSetFilter(hconfig, ntpl_cmd);
1777 if (strlen(span_ports) > 0) {
1779 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1780 "assign[priority=1;streamid=drop;colormask=0x1]=(Layer3Protocol==IPV6)and(port == %s)and(Key(KDEF%u,KeyID=%u)==%u)",
1782 NAPATECH_KEYTYPE_IPV6_SPAN,
1783 NAPATECH_KEYTYPE_IPV6_SPAN,
1784 NAPATECH_FLOWTYPE_DROP);
1785 NapatechSetFilter(hconfig, ntpl_cmd);
1789 for (pair = 0; pair < iteration; ++pair) {
1790 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1791 "assign[priority=1;streamid=drop;DestinationPort=%d;colormask=0x4]=(Layer3Protocol==IPV6)and(port==%d)and(Key(KDEF%u,KeyID=%u)==%u)",
1792 ports_spec.second[pair],
1793 ports_spec.first[pair],
1794 NAPATECH_KEYTYPE_IPV6,
1795 NAPATECH_KEYTYPE_IPV6,
1796 NAPATECH_FLOWTYPE_PASS);
1797 NapatechSetFilter(hconfig, ntpl_cmd);
1799 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1800 "assign[priority=1;streamid=drop;DestinationPort=%d;colormask=0x4]=(Layer3Protocol==IPV6)and(port==%d)and(Key(KDEF%u,KeyID=%u,fieldaction=swap)==%u)",
1801 ports_spec.first[pair],
1802 ports_spec.second[pair],
1803 NAPATECH_KEYTYPE_IPV6,
1804 NAPATECH_KEYTYPE_IPV6,
1805 NAPATECH_FLOWTYPE_PASS);
1806 NapatechSetFilter(hconfig, ntpl_cmd);
1811 FatalError(
"Napatech Inline operation not supported by this FPGA version.");
1815 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
"assign[streamid=(%d..%d);colormask=0x0] = %s%s",
1816 first_stream, last_stream, ports_spec.all ?
"" :
"port==", ports_spec.str);
1817 NapatechSetFilter(hconfig, ntpl_cmd);
1822 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
"assign[streamid=(%d..%d)] = %s%s",
1823 first_stream, last_stream, ports_spec.all ?
"" :
"port==", ports_spec.str);
1824 NapatechSetFilter(hconfig, ntpl_cmd);
1831 for (stream_id = first_stream; stream_id < last_stream; ++stream_id) {
1835 uint32_t num_host_buffers = GetStreamNUMAs(stream_id, numa_nodes);
1837 snprintf(temp1, 256,
" stream %d: ", stream_id);
1839 for (uint32_t hb_id = 0; hb_id < num_host_buffers; ++hb_id) {
1840 snprintf(temp2, 256,
"%d ", numa_nodes[hb_id]);
1841 strlcat(temp1, temp2,
sizeof(temp1));
1847 if (first_stream == last_stream) {
1848 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1849 "Setup[state=active] = StreamId == %d",
1852 snprintf(ntpl_cmd,
sizeof (ntpl_cmd),
1853 "Setup[state=active] = StreamId == (%d..%d)",
1854 first_stream, last_stream);
1856 NapatechSetFilter(hconfig, ntpl_cmd);
1858 NT_ConfigClose(hconfig);
1863 #endif // HAVE_NAPATECH