Skip to content

Commit 29cf020

Browse files
committed
Update nfpcapd - Rework a lot of code. Remove lock for NewNode() in hot path
1 parent 437fa2f commit 29cf020

6 files changed

Lines changed: 366 additions & 332 deletions

File tree

src/libnffile/util.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ int InitLog(unsigned want_syslog, const char *name, char *facility, unsigned ver
196196
verbose = verbose_log;
197197
if (want_syslog == NOSYSLOG) {
198198
use_syslog = 0;
199-
if (verbose > 1) {
199+
if (verbose) {
200200
LogInfo("Verbose log level: %u", verbose);
201201
}
202202
return 1;
@@ -264,7 +264,7 @@ void LogInfo(char *format, ...) {
264264
va_end(var_args);
265265
syslog(LOG_INFO, "%s", string);
266266
dbg_printf("%s\n", string);
267-
} else {
267+
} else if (verbose) {
268268
va_start(var_args, format);
269269
vsnprintf(string, 511, format, var_args);
270270
fprintf(stderr, "%s\n", string);

src/nfpcapd/flowdump.c

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ static int StorePcapFlow(flowParam_t *flowParam, struct FlowNode *Node) {
142142
if (flowParam->extendedFlow) {
143143
UpdateRecordSize(EXipInfoSize);
144144
PushExtension(recordHeader, EXipInfo, ipInfo);
145-
ipInfo->minTTL = Node->hotNode.minTTL;
146-
ipInfo->maxTTL = Node->hotNode.maxTTL;
145+
ipInfo->minTTL = Node->coldNode.minTTL;
146+
ipInfo->maxTTL = Node->coldNode.maxTTL;
147147
ipInfo->fragmentFlags = Node->coldNode.fragmentFlags;
148148

149149
if (Node->coldNode.vlanID) {
@@ -358,37 +358,44 @@ __attribute__((noreturn)) void *flow_thread(void *thread_data) {
358358
// init flow source
359359
fs->dataBlock = WriteBlock(fs->nffile, NULL);
360360
fs->bad_packets = 0;
361-
while (1) {
361+
int done = 0;
362+
while (!done) {
362363
struct FlowNode *Node = Pop_Node(flowParam->NodeList);
363-
if (Node->hotNode.signal == SIGNAL_SYNC) {
364-
// Flush Exporter Stat to file
365-
FlushExporterStats(fs);
366-
// flush current block and close file
367-
fs->dataBlock = WriteBlock(fs->nffile, fs->dataBlock);
368-
CloseFlowFile(flowParam, Node->timestamp);
369-
fs->nffile = OpenNewFile(SetUniqueTmpName(fs->tmpFileName), CREATOR_NFPCAPD, compress, NOT_ENCRYPTED);
370-
if (!fs->nffile) {
371-
LogError("Fatal: OpenNewFile() failed for ident: %s", fs->Ident);
372-
pthread_kill(flowParam->parent, SIGUSR1);
364+
switch (Node->nodeType) {
365+
case FLOW_NODE:
366+
StorePcapFlow(flowParam, Node);
373367
break;
374-
}
375-
SetIdent(fs->nffile, fs->Ident);
376-
377-
// Dump all exporters to the buffer for new file
378-
FlushStdRecords(fs);
379-
380-
} else if (Node->hotNode.signal == SIGNAL_DONE) {
381-
// Flush Exporter Stat to file
382-
FlushExporterStats(fs);
383-
// flush current block and close file
384-
FlushBlock(fs->nffile, fs->dataBlock);
385-
CloseFlowFile(flowParam, Node->timestamp);
386-
break;
387-
} else if (Node->hotNode.nodeType == FLOW_NODE) {
388-
StorePcapFlow(flowParam, Node);
389-
} else {
390-
// skip this node
368+
case SIGNAL_NODE_SYNC:
369+
dbg_printf("Received signal_node_sync\n");
370+
// Flush Exporter Stat to file
371+
FlushExporterStats(fs);
372+
// flush current block and close file
373+
fs->dataBlock = WriteBlock(fs->nffile, fs->dataBlock);
374+
CloseFlowFile(flowParam, Node->timestamp);
375+
fs->nffile = OpenNewFile(SetUniqueTmpName(fs->tmpFileName), CREATOR_NFPCAPD, compress, NOT_ENCRYPTED);
376+
if (!fs->nffile) {
377+
LogError("Fatal: OpenNewFile() failed for ident: %s", fs->Ident);
378+
pthread_kill(flowParam->parent, SIGUSR1);
379+
break;
380+
}
381+
SetIdent(fs->nffile, fs->Ident);
382+
383+
// Dump all exporters to the buffer for new file
384+
FlushStdRecords(fs);
385+
break;
386+
case SIGNAL_NODE_DONE:
387+
dbg_printf("Received signal_node_done\n");
388+
// Flush Exporter Stat to file
389+
FlushExporterStats(fs);
390+
// flush current block and close file
391+
FlushBlock(fs->nffile, fs->dataBlock);
392+
CloseFlowFile(flowParam, Node->timestamp);
393+
done = 1;
394+
break;
395+
default:
396+
LogError("Unknown node type: %u\n", Node->nodeType);
391397
}
398+
392399
Free_Node(Node);
393400
}
394401

src/nfpcapd/flowsend.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,22 @@ __attribute__((noreturn)) void *sendflow_thread(void *thread_data) {
238238
pcapd_header->lastSequence = 1;
239239

240240
printRecord = flowParam->printRecord;
241-
while (1) {
241+
int done = 0;
242+
while (!done) {
242243
struct FlowNode *Node = Pop_Node(flowParam->NodeList);
243-
if (Node->hotNode.signal == SIGNAL_SYNC) {
244-
// skip
245-
} else if (Node->hotNode.signal == SIGNAL_DONE) {
246-
CloseSender(flowParam, Node->timestamp);
247-
break;
248-
} else {
249-
ProcessFlow(flowParam, Node);
244+
switch (Node->nodeType) {
245+
case FLOW_NODE:
246+
ProcessFlow(flowParam, Node);
247+
break;
248+
case SIGNAL_NODE_SYNC:
249+
// skip
250+
break;
251+
case SIGNAL_NODE_DONE:
252+
CloseSender(flowParam, Node->timestamp);
253+
done = 1;
254+
break;
255+
default:
256+
LogError("Unknown node type: %u\n", Node->nodeType);
250257
}
251258
Free_Node(Node);
252259
}

0 commit comments

Comments
 (0)