/** * \brief Creates and returns the TV instance for a Management thread(MGMT). * This function supports only custom slot functions and hence a * function pointer should be sent as an argument. * * \param name Name of this TV instance * \param fn_p Pointer to function when \"slots\" is of type \"custom\" * \param mucond Flag to indicate whether to initialize the condition * and the mutex variables for this newly created TV. * * \retval the newly created TV instance, or NULL on error */ ThreadVars *TmThreadCreateMgmtThread(char *name, void *(fn_p)(void *), int mucond) { ThreadVars *tv = NULL; tv = TmThreadCreate(name, NULL, NULL, NULL, NULL, "custom", fn_p, mucond); TmThreadSetCPU(tv, MANAGEMENT_CPU_SET); if (tv != NULL) tv->type = TVT_MGMT; return tv; }
/** * \brief Creates and returns a TV instance for a Packet Processing Thread. * This function doesn't support custom slots, and hence shouldn't be * supplied \"custom\" as its slot type. All PPT threads are created * with a mucond(see TmThreadCreate declaration) of 0. Hence the tv * conditional variables are not used to kill the thread. * * \param name Name of this TV instance * \param inq_name Incoming queue name * \param inqh_name Incoming queue handler name as set by TmqhSetup() * \param outq_name Outgoing queue name * \param outqh_name Outgoing queue handler as set by TmqhSetup() * \param slots String representation for the slot function to be used * * \retval the newly created TV instance, or NULL on error */ ThreadVars *TmThreadCreatePacketHandler(char *name, char *inq_name, char *inqh_name, char *outq_name, char *outqh_name, char *slots) { ThreadVars *tv = NULL; tv = TmThreadCreate(name, inq_name, inqh_name, outq_name, outqh_name, slots, NULL, 0); if (tv != NULL) tv->type = TVT_PPT; return tv; }
int RunModeSetLiveCaptureAuto(DetectEngineCtx *de_ctx, ConfigIfaceParserFunc ConfigParser, ConfigIfaceThreadsCountFunc ModThreadsCount, char *recv_mod_name, char *decode_mod_name, char *thread_name, const char *live_dev) { /* Available cpus */ uint16_t ncpus = UtilCpuGetNumProcessorsOnline(); int nlive = LiveGetDeviceCount(); TmModule *tm_module; char tname[16]; int thread; if ((nlive <= 1) && (live_dev != NULL)) { void *aconf; SCLogDebug("live_dev %s", live_dev); aconf = ConfigParser(live_dev); if (aconf == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Single dev: Failed to allocate config"); exit(EXIT_FAILURE); } if (ModThreadsCount(aconf) > 1) { SCLogWarning(SC_ERR_UNIMPLEMENTED, "'Auto' running mode does not honor 'threads'" " variable (set on '%s'). Please use another mode as" " 'autofp' or 'worker'", live_dev); } /* create the threads */ ThreadVars *tv_receive = TmThreadCreatePacketHandler(recv_mod_name, "packetpool", "packetpool", "pickup-queue", "simple", "pktacqloop"); if (tv_receive == NULL) { SCLogError(SC_ERR_THREAD_CREATE, "TmThreadsCreate failed"); exit(EXIT_FAILURE); } tm_module = TmModuleGetByName(recv_mod_name); if (tm_module == NULL) { SCLogError(SC_ERR_INVALID_VALUE, "TmModuleGetByName failed for %s", recv_mod_name); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_receive, tm_module, aconf); TmThreadSetCPU(tv_receive, RECEIVE_CPU_SET); if (TmThreadSpawn(tv_receive) != TM_ECODE_OK) { SCLogError(SC_ERR_THREAD_SPAWN, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } } else { SCLogInfo("Using %d live device(s).", nlive); for (thread = 0; thread < nlive; thread++) { char *live_dev = LiveGetDeviceName(thread); char *tnamec = NULL; void *aconf; if (live_dev == NULL) { SCLogError(SC_ERR_INVALID_VALUE, "Multidev: Failed to lookup live dev %d", thread); exit(EXIT_FAILURE); } SCLogDebug("live_dev %s", live_dev); aconf = ConfigParser(live_dev); if (aconf == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Failed to allocate config for %s (%d)", live_dev, thread); exit(EXIT_FAILURE); } if (ModThreadsCount(aconf) > 1) { SCLogWarning(SC_ERR_UNIMPLEMENTED, "'Auto' running mode does not honor 'threads'" " variable (set on '%s'). Please use another mode as" " 'autofp' or 'worker'", live_dev); } snprintf(tname, sizeof(tname),"%s-%s", thread_name, live_dev); tnamec = SCStrdup(tname); if (tnamec == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Can't allocate thread name"); exit(EXIT_FAILURE); } /* create the threads */ ThreadVars *tv_receive = TmThreadCreatePacketHandler(tnamec, "packetpool", "packetpool", "pickup-queue", "simple", "pktacqloop"); if (tv_receive == NULL) { SCLogError(SC_ERR_RUNMODE, "TmThreadsCreate failed"); exit(EXIT_FAILURE); } tm_module = TmModuleGetByName(recv_mod_name); if (tm_module == NULL) { SCLogError(SC_ERR_INVALID_VALUE, "TmModuleGetByName failed for %s", recv_mod_name); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_receive, tm_module, (void *)aconf); TmThreadSetCPU(tv_receive, RECEIVE_CPU_SET); if (TmThreadSpawn(tv_receive) != TM_ECODE_OK) { SCLogError(SC_ERR_INVALID_VALUE, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } } } #if defined(__SC_CUDA_SUPPORT__) if (PatternMatchDefaultMatcher() == MPM_B2G_CUDA) { ThreadVars *tv_decode1 = TmThreadCreatePacketHandler("Decode", "pickup-queue", "simple", "decode-queue1", "simple", "1slot"); if (tv_decode1 == NULL) { SCLogError(SC_ERR_RUNMODE, "TmThreadsCreate failed for Decode1"); exit(EXIT_FAILURE); } tm_module = TmModuleGetByName(decode_mod_name); if (tm_module == NULL) { SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName %s failed", decode_mod_name); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_decode1, tm_module, NULL); TmThreadSetCPU(tv_decode1, DECODE_CPU_SET); if (TmThreadSpawn(tv_decode1) != TM_ECODE_OK) { SCLogError(SC_ERR_RUNMODE, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } ThreadVars *tv_cuda_PB = TmThreadCreate("CUDA_PB", "decode-queue1", "simple", "cuda-pb-queue1", "simple", "custom", SCCudaPBTmThreadsSlot1, 0); if (tv_cuda_PB == NULL) { SCLogError(SC_ERR_RUNMODE, "TmThreadsCreate failed for CUDA_PB"); exit(EXIT_FAILURE); } tv_cuda_PB->type = TVT_PPT; tm_module = TmModuleGetByName("CudaPacketBatcher"); if (tm_module == NULL) { SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName CudaPacketBatcher failed"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_cuda_PB, tm_module, (void *)de_ctx); TmThreadSetCPU(tv_cuda_PB, DETECT_CPU_SET); if (TmThreadSpawn(tv_cuda_PB) != TM_ECODE_OK) { SCLogError(SC_ERR_THREAD_SPAWN, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } ThreadVars *tv_stream1 = TmThreadCreatePacketHandler("Stream1", "cuda-pb-queue1", "simple", "stream-queue1", "simple", "1slot"); if (tv_stream1 == NULL) { SCLogError(SC_ERR_RUNMODE, "TmThreadsCreate failed for Stream1"); exit(EXIT_FAILURE); } tm_module = TmModuleGetByName("StreamTcp"); if (tm_module == NULL) { SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName StreamTcp failed"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_stream1, tm_module, NULL); TmThreadSetCPU(tv_stream1, STREAM_CPU_SET); if (TmThreadSpawn(tv_stream1) != TM_ECODE_OK) { SCLogError(SC_ERR_THREAD_SPAWN, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } } else { ThreadVars *tv_decode1 = TmThreadCreatePacketHandler("Decode & Stream", "pickup-queue", "simple", "stream-queue1", "simple", "varslot"); if (tv_decode1 == NULL) { SCLogError(SC_ERR_RUNMODE, "TmThreadsCreate failed for Decode1"); exit(EXIT_FAILURE); } tm_module = TmModuleGetByName(decode_mod_name); if (tm_module == NULL) { SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName %s failed", decode_mod_name); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_decode1, tm_module, NULL); tm_module = TmModuleGetByName("StreamTcp"); if (tm_module == NULL) { SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName StreamTcp failed"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_decode1, tm_module, NULL); TmThreadSetCPU(tv_decode1, DECODE_CPU_SET); if (TmThreadSpawn(tv_decode1) != TM_ECODE_OK) { SCLogError(SC_ERR_RUNMODE, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } } #else ThreadVars *tv_decode1 = TmThreadCreatePacketHandler("Decode & Stream", "pickup-queue", "simple", "stream-queue1", "simple", "varslot"); if (tv_decode1 == NULL) { SCLogError(SC_ERR_RUNMODE, "TmThreadsCreate failed for Decode1"); exit(EXIT_FAILURE); } tm_module = TmModuleGetByName(decode_mod_name); if (tm_module == NULL) { SCLogError(SC_ERR_INVALID_VALUE, "TmModuleGetByName %s failed", decode_mod_name); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_decode1, tm_module, NULL); tm_module = TmModuleGetByName("StreamTcp"); if (tm_module == NULL) { SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName StreamTcp failed"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_decode1, tm_module, NULL); TmThreadSetCPU(tv_decode1, DECODE_CPU_SET); if (TmThreadSpawn(tv_decode1) != TM_ECODE_OK) { SCLogError(SC_ERR_RUNMODE, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } #endif /* always create at least one thread */ int thread_max = TmThreadGetNbThreads(DETECT_CPU_SET); if (thread_max == 0) thread_max = ncpus * threading_detect_ratio; if (thread_max < 1) thread_max = 1; for (thread = 0; thread < thread_max; thread++) { snprintf(tname, sizeof(tname),"Detect%"PRIu16, thread+1); char *thread_name = SCStrdup(tname); if (thread_name == NULL) { SCLogError(SC_ERR_MEM_ALLOC, "Can't allocate thread name"); exit(EXIT_FAILURE); } ThreadVars *tv_detect_ncpu = TmThreadCreatePacketHandler(thread_name, "stream-queue1", "simple", "verdict-queue", "simple", "1slot"); if (tv_detect_ncpu == NULL) { SCLogError(SC_ERR_RUNMODE, "TmThreadsCreate failed"); exit(EXIT_FAILURE); } tm_module = TmModuleGetByName("Detect"); if (tm_module == NULL) { SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName Detect failed"); exit(EXIT_FAILURE); } TmSlotSetFuncAppendDelayed(tv_detect_ncpu, tm_module, (void *)de_ctx, de_ctx->delayed_detect); TmThreadSetCPU(tv_detect_ncpu, DETECT_CPU_SET); char *thread_group_name = SCStrdup("Detect"); if (thread_group_name == NULL) { SCLogError(SC_ERR_RUNMODE, "Error allocating memory"); exit(EXIT_FAILURE); } tv_detect_ncpu->thread_group_name = thread_group_name; if (TmThreadSpawn(tv_detect_ncpu) != TM_ECODE_OK) { SCLogError(SC_ERR_THREAD_SPAWN, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } } ThreadVars *tv_rreject = TmThreadCreatePacketHandler("RespondReject", "verdict-queue", "simple", "alert-queue", "simple", "1slot"); if (tv_rreject == NULL) { SCLogError(SC_ERR_RUNMODE, "TmThreadsCreate failed"); exit(EXIT_FAILURE); } tm_module = TmModuleGetByName("RespondReject"); if (tm_module == NULL) { SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName for RespondReject failed"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_rreject, tm_module, NULL); TmThreadSetCPU(tv_rreject, REJECT_CPU_SET); if (TmThreadSpawn(tv_rreject) != TM_ECODE_OK) { SCLogError(SC_ERR_RUNMODE, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } ThreadVars *tv_outputs = TmThreadCreatePacketHandler("Outputs", "alert-queue", "simple", "packetpool", "packetpool", "varslot"); if (tv_outputs == NULL) { SCLogError(SC_ERR_RUNMODE, "TmThreadCreatePacketHandler for Outputs failed"); exit(EXIT_FAILURE); } SetupOutputs(tv_outputs); TmThreadSetCPU(tv_outputs, OUTPUT_CPU_SET); if (TmThreadSpawn(tv_outputs) != TM_ECODE_OK) { SCLogError(SC_ERR_RUNMODE, "TmThreadSpawn failed"); exit(EXIT_FAILURE); } return 0; }
/* * \brief RunModeFilePcapAuto set up the following thread packet handlers: * - Receive thread (from pcap file) * - Decode thread * - Stream thread * - Detect: If we have only 1 cpu, it will setup one Detect thread * If we have more than one, it will setup num_cpus - 1 * starting from the second cpu available. * - Outputs thread * By default the threads will use the first cpu available * except the Detection threads if we have more than one cpu. * * \param de_ctx Pointer to the Detection Engine. * * \retval 0 If all goes well. (If any problem is detected the engine will * exit()). */ int RunModeFilePcapAuto(DetectEngineCtx *de_ctx) { SCEnter(); char tname[16]; uint16_t cpu = 0; TmModule *tm_module; int cuda = 0; RunModeInitialize(); /* Available cpus */ uint16_t ncpus = UtilCpuGetNumProcessorsOnline(); char *file = NULL; if (ConfGet("pcap-file.file", &file) == 0) { SCLogError(SC_ERR_RUNMODE, "Failed retrieving pcap-file from Conf"); exit(EXIT_FAILURE); } SCLogDebug("file %s", file); TimeModeSetOffline(); #if defined(__SC_CUDA_SUPPORT__) if (PatternMatchDefaultMatcher() == MPM_B2G_CUDA) { cuda = 1; } #endif if (cuda == 0) { /* create the threads */ ThreadVars *tv_receivepcap = TmThreadCreatePacketHandler("ReceivePcapFile", "packetpool", "packetpool", "detect-queue1", "simple", "pktacqloop"); if (tv_receivepcap == NULL) { printf("ERROR: TmThreadsCreate failed\n"); exit(EXIT_FAILURE); } tm_module = TmModuleGetByName("ReceivePcapFile"); if (tm_module == NULL) { printf("ERROR: TmModuleGetByName failed for ReceivePcap\n"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_receivepcap, tm_module, file); TmThreadSetCPU(tv_receivepcap, RECEIVE_CPU_SET); tm_module = TmModuleGetByName("DecodePcapFile"); if (tm_module == NULL) { printf("ERROR: TmModuleGetByName DecodePcap failed\n"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_receivepcap, tm_module, NULL); tm_module = TmModuleGetByName("StreamTcp"); if (tm_module == NULL) { printf("ERROR: TmModuleGetByName StreamTcp failed\n"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_receivepcap, tm_module, (void *)de_ctx); TmThreadSetCPU(tv_receivepcap, DECODE_CPU_SET); if (TmThreadSpawn(tv_receivepcap) != TM_ECODE_OK) { printf("ERROR: TmThreadSpawn failed\n"); exit(EXIT_FAILURE); } #if defined(__SC_CUDA_SUPPORT__) } else { /* create the threads */ ThreadVars *tv_receivepcap = TmThreadCreatePacketHandler("ReceivePcapFile", "packetpool", "packetpool", "cuda-pb", "simple", "pktacqloop"); if (tv_receivepcap == NULL) { printf("ERROR: TmThreadsCreate failed\n"); exit(EXIT_FAILURE); } tm_module = TmModuleGetByName("ReceivePcapFile"); if (tm_module == NULL) { printf("ERROR: TmModuleGetByName failed for ReceivePcap\n"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_receivepcap, tm_module, file); TmThreadSetCPU(tv_receivepcap, RECEIVE_CPU_SET); tm_module = TmModuleGetByName("DecodePcapFile"); if (tm_module == NULL) { printf("ERROR: TmModuleGetByName DecodePcap failed\n"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_receivepcap, tm_module, NULL); TmThreadSetCPU(tv_receivepcap, DECODE_CPU_SET); if (TmThreadSpawn(tv_receivepcap) != TM_ECODE_OK) { printf("ERROR: TmThreadSpawn failed\n"); exit(EXIT_FAILURE); } ThreadVars *tv_cuda_PB = TmThreadCreate("CUDA_PB", "cuda-pb", "simple", "detect-queue1", "simple", "custom", SCCudaPBTmThreadsSlot1, 0); if (tv_cuda_PB == NULL) { printf("ERROR: TmThreadsCreate failed for CUDA_PB\n"); exit(EXIT_FAILURE); } tv_cuda_PB->type = TVT_PPT; tm_module = TmModuleGetByName("CudaPacketBatcher"); if (tm_module == NULL) { printf("ERROR: TmModuleGetByName CudaPacketBatcher failed\n"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_cuda_PB, tm_module, de_ctx); tm_module = TmModuleGetByName("StreamTcp"); if (tm_module == NULL) { printf("ERROR: TmModuleGetByName StreamTcp failed\n"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_cuda_PB, tm_module, NULL); if (TmThreadSpawn(tv_cuda_PB) != TM_ECODE_OK) { printf("ERROR: TmThreadSpawn failed\n"); exit(EXIT_FAILURE); } #endif } /* start with cpu 1 so that if we're creating an odd number of detect * threads we're not creating the most on CPU0. */ if (ncpus > 0) cpu = 1; /* always create at least one thread */ int thread_max = TmThreadGetNbThreads(DETECT_CPU_SET); if (thread_max == 0) thread_max = ncpus * threading_detect_ratio; if (thread_max < 1) thread_max = 1; int thread; for (thread = 0; thread < thread_max; thread++) { snprintf(tname, sizeof(tname), "Detect%"PRIu16, thread+1); char *thread_name = SCStrdup(tname); if (unlikely(thread_name == NULL)) { printf("ERROR: Can not strdup thread name\n"); exit(EXIT_FAILURE); } SCLogDebug("Assigning %s affinity to cpu %u", thread_name, cpu); ThreadVars *tv_detect_ncpu = TmThreadCreatePacketHandler(thread_name, "detect-queue1", "simple", "alert-queue1", "simple", "1slot"); if (tv_detect_ncpu == NULL) { printf("ERROR: TmThreadsCreate failed\n"); exit(EXIT_FAILURE); } tm_module = TmModuleGetByName("Detect"); if (tm_module == NULL) { printf("ERROR: TmModuleGetByName Detect failed\n"); exit(EXIT_FAILURE); } TmSlotSetFuncAppend(tv_detect_ncpu, tm_module, (void *)de_ctx); TmThreadSetCPU(tv_detect_ncpu, DETECT_CPU_SET); char *thread_group_name = SCStrdup("Detect"); if (unlikely(thread_group_name == NULL)) { printf("Error allocating memory\n"); exit(EXIT_FAILURE); } tv_detect_ncpu->thread_group_name = thread_group_name; if (TmThreadSpawn(tv_detect_ncpu) != TM_ECODE_OK) { printf("ERROR: TmThreadSpawn failed\n"); exit(EXIT_FAILURE); } if ((cpu + 1) == ncpus) cpu = 0; else cpu++; } ThreadVars *tv_outputs = TmThreadCreatePacketHandler("Outputs", "alert-queue1", "simple", "packetpool", "packetpool", "varslot"); if (tv_outputs == NULL) { printf("ERROR: TmThreadCreatePacketHandler for Outputs failed\n"); exit(EXIT_FAILURE); } SetupOutputs(tv_outputs); TmThreadSetCPU(tv_outputs, OUTPUT_CPU_SET); if (TmThreadSpawn(tv_outputs) != TM_ECODE_OK) { printf("ERROR: TmThreadSpawn failed\n"); exit(EXIT_FAILURE); } return 0; }