Allow multiple struct ndpi_detection_module_struct to share some state (#2271)
Add the concept of "global context". Right now every instance of `struct ndpi_detection_module_struct` (we will call it "local context" in this description) is completely independent from each other. This provide optimal performances in multithreaded environment, where we pin each local context to a thread, and each thread to a specific CPU core: we don't have any data shared across the cores. Each local context has, internally, also some information correlating **different** flows; something like: ``` if flow1 (PeerA <-> Peer B) is PROTOCOL_X; then flow2 (PeerC <-> PeerD) will be PROTOCOL_Y ``` To get optimal classification results, both flow1 and flow2 must be processed by the same local context. This is not an issue at all in the far most common scenario where there is only one local context, but it might be impractical in some more complex scenarios. Create the concept of "global context": multiple local contexts can use the same global context and share some data (structures) using it. This way the data correlating multiple flows can be read/write from different local contexts. This is an optional feature, disabled by default. Obviously data structures shared in a global context must be thread safe. This PR updates the code of the LRU implementation to be, optionally, thread safe. Right now, only the LRU caches can be shared; the other main structures (trees and automas) are basically read-only: there is little sense in sharing them. Furthermore, these structures don't have any information correlating multiple flows. Every LRU cache can be shared, independently from the others, via `ndpi_set_config(ndpi_struct, NULL, "lru.$CACHE_NAME.scope", "1")`. It's up to the user to find the right trade-off between performances (i.e. without shared data) and classification results (i.e. with some shared data among the local contexts), depending on the specific traffic patterns and on the algorithms used to balance the flows across the threads/cores/local contexts. Add some basic examples of library initialization in `doc/library_initialization.md`. This code needs libpthread as external dependency. It shouldn't be a big issue; however a configure flag has been added to disable global context support. A new CI job has been added to test it. TODO: we should need to find a proper way to add some tests on multithreaded enviroment... not an easy task... *** API changes *** If you are not interested in this feature, simply add a NULL parameter to any `ndpi_init_detection_module()` calls.
This commit is contained in:
@@ -144,6 +144,34 @@ typedef struct {
|
||||
} nbpf_filter;
|
||||
#endif
|
||||
|
||||
struct ndpi_global_context {
|
||||
|
||||
/* LRU caches */
|
||||
|
||||
/* NDPI_PROTOCOL_OOKLA */
|
||||
int ookla_cache_is_global;
|
||||
struct ndpi_lru_cache *ookla_global_cache;
|
||||
|
||||
/* NDPI_PROTOCOL_BITTORRENT */
|
||||
struct ndpi_lru_cache *bittorrent_global_cache;
|
||||
|
||||
/* NDPI_PROTOCOL_ZOOM */
|
||||
struct ndpi_lru_cache *zoom_global_cache;
|
||||
|
||||
/* NDPI_PROTOCOL_STUN and subprotocols */
|
||||
struct ndpi_lru_cache *stun_global_cache;
|
||||
struct ndpi_lru_cache *stun_zoom_global_cache;
|
||||
|
||||
/* NDPI_PROTOCOL_TLS and subprotocols */
|
||||
struct ndpi_lru_cache *tls_cert_global_cache;
|
||||
|
||||
/* NDPI_PROTOCOL_MINING and subprotocols */
|
||||
struct ndpi_lru_cache *mining_global_cache;
|
||||
|
||||
/* NDPI_PROTOCOL_MSTEAMS */
|
||||
struct ndpi_lru_cache *msteams_global_cache;
|
||||
};
|
||||
|
||||
#define CFG_MAX_LEN 256
|
||||
|
||||
struct ndpi_detection_module_config_struct {
|
||||
@@ -177,20 +205,28 @@ struct ndpi_detection_module_config_struct {
|
||||
|
||||
int ookla_cache_num_entries;
|
||||
int ookla_cache_ttl;
|
||||
int ookla_cache_scope;
|
||||
int bittorrent_cache_num_entries;
|
||||
int bittorrent_cache_ttl;
|
||||
int bittorrent_cache_scope;
|
||||
int zoom_cache_num_entries;
|
||||
int zoom_cache_ttl;
|
||||
int zoom_cache_scope;
|
||||
int stun_cache_num_entries;
|
||||
int stun_cache_ttl;
|
||||
int stun_cache_scope;
|
||||
int tls_cert_cache_num_entries;
|
||||
int tls_cert_cache_ttl;
|
||||
int tls_cert_cache_scope;
|
||||
int mining_cache_num_entries;
|
||||
int mining_cache_ttl;
|
||||
int mining_cache_scope;
|
||||
int msteams_cache_num_entries;
|
||||
int msteams_cache_ttl;
|
||||
int msteams_cache_scope;
|
||||
int stun_zoom_cache_num_entries;
|
||||
int stun_zoom_cache_ttl;
|
||||
int stun_zoom_cache_scope;
|
||||
|
||||
/* Protocols */
|
||||
|
||||
@@ -301,6 +337,7 @@ struct ndpi_detection_module_struct {
|
||||
|
||||
u_int8_t ip_version_limit;
|
||||
|
||||
struct ndpi_global_context *g_ctx;
|
||||
struct ndpi_detection_module_config_struct cfg;
|
||||
|
||||
/* NDPI_PROTOCOL_TINC */
|
||||
|
||||
Reference in New Issue
Block a user