You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rspamd_symcache.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef RSPAMD_SYMBOLS_CACHE_H
  17. #define RSPAMD_SYMBOLS_CACHE_H
  18. #include "config.h"
  19. #include "ucl.h"
  20. #include <lua.h>
  21. #include <event.h>
  22. struct rspamd_task;
  23. struct rspamd_config;
  24. struct rspamd_symcache;
  25. struct rspamd_worker;
  26. struct rspamd_symcache_item;
  27. typedef void (*symbol_func_t)(struct rspamd_task *task,
  28. struct rspamd_symcache_item *item,
  29. gpointer user_data);
  30. enum rspamd_symbol_type {
  31. SYMBOL_TYPE_NORMAL = (1 << 0),
  32. SYMBOL_TYPE_VIRTUAL = (1 << 1),
  33. SYMBOL_TYPE_CALLBACK = (1 << 2),
  34. SYMBOL_TYPE_GHOST = (1 << 3),
  35. SYMBOL_TYPE_SKIPPED = (1 << 4),
  36. SYMBOL_TYPE_COMPOSITE = (1 << 5),
  37. SYMBOL_TYPE_CLASSIFIER = (1 << 6),
  38. SYMBOL_TYPE_FINE = (1 << 7),
  39. SYMBOL_TYPE_EMPTY = (1 << 8), /* Allow execution on empty tasks */
  40. SYMBOL_TYPE_PREFILTER = (1 << 9),
  41. SYMBOL_TYPE_POSTFILTER = (1 << 10),
  42. SYMBOL_TYPE_NOSTAT = (1 << 11), /* Skip as statistical symbol */
  43. SYMBOL_TYPE_IDEMPOTENT = (1 << 12), /* Symbol cannot change metric */
  44. SYMBOL_TYPE_SQUEEZED = (1 << 13), /* Symbol is squeezed inside Lua */
  45. SYMBOL_TYPE_TRIVIAL = (1 << 14), /* Symbol is trivial */
  46. SYMBOL_TYPE_MIME_ONLY = (1 << 15), /* Symbol is mime only */
  47. };
  48. /**
  49. * Abstract structure for saving callback data for symbols
  50. */
  51. struct rspamd_abstract_callback_data {
  52. guint64 magic;
  53. char data[];
  54. };
  55. /**
  56. * Creates new cache structure
  57. * @return
  58. */
  59. struct rspamd_symcache* rspamd_symcache_new (struct rspamd_config *cfg);
  60. /**
  61. * Remove the cache structure syncing data if needed
  62. * @param cache
  63. */
  64. void rspamd_symcache_destroy (struct rspamd_symcache *cache);
  65. /**
  66. * Saves symbols cache to disk if possible
  67. * @param cache
  68. */
  69. void rspamd_symcache_save (struct rspamd_symcache *cache);
  70. /**
  71. * Load symbols cache from file, must be called _after_ init_symbols_cache
  72. */
  73. gboolean rspamd_symcache_init (struct rspamd_symcache *cache);
  74. /**
  75. * Generic function to register a symbol
  76. * @param cache
  77. * @param name
  78. * @param weight
  79. * @param priority
  80. * @param func
  81. * @param user_data
  82. * @param type
  83. * @param parent
  84. */
  85. gint rspamd_symcache_add_symbol (struct rspamd_symcache *cache,
  86. const gchar *name,
  87. gint priority,
  88. symbol_func_t func,
  89. gpointer user_data,
  90. enum rspamd_symbol_type type,
  91. gint parent);
  92. /**
  93. * Add callback to be executed whenever symbol has peak value
  94. * @param cache
  95. * @param cbref
  96. */
  97. void rspamd_symcache_set_peak_callback (struct rspamd_symcache *cache,
  98. gint cbref);
  99. /**
  100. * Add delayed condition to the specific symbol in cache. So symbol can be absent
  101. * to the moment of addition
  102. * @param cache
  103. * @param id id of symbol
  104. * @param L lua state pointer
  105. * @param cbref callback reference (returned by luaL_ref)
  106. * @return TRUE if condition has been added
  107. */
  108. gboolean rspamd_symcache_add_condition_delayed (struct rspamd_symcache *cache,
  109. const gchar *sym,
  110. lua_State *L, gint cbref);
  111. /**
  112. * Find symbol in cache by id and returns its id resolving virtual symbols if
  113. * applicable
  114. * @param cache
  115. * @param name
  116. * @return id of symbol or (-1) if a symbol has not been found
  117. */
  118. gint rspamd_symcache_find_symbol (struct rspamd_symcache *cache,
  119. const gchar *name);
  120. /**
  121. * Get statistics for a specific symbol
  122. * @param cache
  123. * @param name
  124. * @param frequency
  125. * @param tm
  126. * @return
  127. */
  128. gboolean rspamd_symcache_stat_symbol (struct rspamd_symcache *cache,
  129. const gchar *name,
  130. gdouble *frequency,
  131. gdouble *freq_stddev,
  132. gdouble *tm,
  133. guint *nhits);
  134. /**
  135. * Find symbol in cache by its id
  136. * @param cache
  137. * @param id
  138. * @return symbol's name or NULL
  139. */
  140. const gchar * rspamd_symcache_symbol_by_id (struct rspamd_symcache *cache,
  141. gint id);
  142. /**
  143. * Returns number of symbols registered in symbols cache
  144. * @param cache
  145. * @return number of symbols in the cache
  146. */
  147. guint rspamd_symcache_stats_symbols_count (struct rspamd_symcache *cache);
  148. /**
  149. * Call function for cached symbol using saved callback
  150. * @param task task object
  151. * @param cache symbols cache
  152. * @param saved_item pointer to currently saved item
  153. */
  154. gboolean rspamd_symcache_process_symbols (struct rspamd_task *task,
  155. struct rspamd_symcache *cache,
  156. gint stage);
  157. /**
  158. * Validate cache items against theirs weights defined in metrics
  159. * @param cache symbols cache
  160. * @param cfg configuration
  161. * @param strict do strict checks - symbols MUST be described in metrics
  162. */
  163. gboolean rspamd_symcache_validate (struct rspamd_symcache *cache,
  164. struct rspamd_config *cfg,
  165. gboolean strict);
  166. /**
  167. * Return statistics about the cache as ucl object (array of objects one per item)
  168. * @param cache
  169. * @return
  170. */
  171. ucl_object_t *rspamd_symcache_counters (struct rspamd_symcache *cache);
  172. /**
  173. * Start cache reloading
  174. * @param cache
  175. * @param ev_base
  176. */
  177. void rspamd_symcache_start_refresh (struct rspamd_symcache *cache,
  178. struct event_base *ev_base,
  179. struct rspamd_worker *w);
  180. /**
  181. * Increases counter for a specific symbol
  182. * @param cache
  183. * @param symbol
  184. */
  185. void rspamd_symcache_inc_frequency (struct rspamd_symcache *cache,
  186. const gchar *symbol);
  187. /**
  188. * Add dependency relation between two symbols identified by id (source) and
  189. * a symbolic name (destination). Destination could be virtual or real symbol.
  190. * Callback destinations are not yet supported.
  191. * @param id_from source symbol
  192. * @param to destination name
  193. */
  194. void rspamd_symcache_add_dependency (struct rspamd_symcache *cache,
  195. gint id_from, const gchar *to);
  196. /**
  197. * Add delayed dependency that is resolved on cache post-load routine
  198. * @param cache
  199. * @param from
  200. * @param to
  201. */
  202. void rspamd_symcache_add_delayed_dependency (struct rspamd_symcache *cache,
  203. const gchar *from, const gchar *to);
  204. /**
  205. * Disable specific symbol in the cache
  206. * @param cache
  207. * @param symbol
  208. */
  209. void rspamd_symcache_disable_symbol_perm (struct rspamd_symcache *cache,
  210. const gchar *symbol);
  211. /**
  212. * Enable specific symbol in the cache
  213. * @param cache
  214. * @param symbol
  215. */
  216. void rspamd_symcache_enable_symbol_perm (struct rspamd_symcache *cache,
  217. const gchar *symbol);
  218. /**
  219. * Get abstract callback data for a symbol (or its parent symbol)
  220. * @param cache cache object
  221. * @param symbol symbol name
  222. * @return abstract callback data or NULL if symbol is absent or has no data attached
  223. */
  224. struct rspamd_abstract_callback_data* rspamd_symcache_get_cbdata (
  225. struct rspamd_symcache *cache, const gchar *symbol);
  226. /**
  227. * Process settings for task
  228. * @param task
  229. * @param cache
  230. * @return
  231. */
  232. gboolean rspamd_symcache_process_settings (struct rspamd_task *task,
  233. struct rspamd_symcache *cache);
  234. /**
  235. * Checks if a symbol specified has been checked (or disabled)
  236. * @param task
  237. * @param cache
  238. * @param symbol
  239. * @return
  240. */
  241. gboolean rspamd_symcache_is_checked (struct rspamd_task *task,
  242. struct rspamd_symcache *cache,
  243. const gchar *symbol);
  244. /**
  245. * Returns checksum for all cache items
  246. * @param cache
  247. * @return
  248. */
  249. guint64 rspamd_symcache_get_cksum (struct rspamd_symcache *cache);
  250. /**
  251. * Checks if a symbols is enabled (not checked and conditions return true if present)
  252. * @param task
  253. * @param cache
  254. * @param symbol
  255. * @return
  256. */
  257. gboolean rspamd_symcache_is_symbol_enabled (struct rspamd_task *task,
  258. struct rspamd_symcache *cache,
  259. const gchar *symbol);
  260. /**
  261. * Enable this symbol for task
  262. * @param task
  263. * @param cache
  264. * @param symbol
  265. * @return TRUE if a symbol has been enabled (not executed before)
  266. */
  267. gboolean rspamd_symcache_enable_symbol (struct rspamd_task *task,
  268. struct rspamd_symcache *cache,
  269. const gchar *symbol);
  270. /**
  271. * Enable this symbol for task
  272. * @param task
  273. * @param cache
  274. * @param symbol
  275. * @return TRUE if a symbol has been disabled (not executed before)
  276. */
  277. gboolean rspamd_symcache_disable_symbol (struct rspamd_task *task,
  278. struct rspamd_symcache *cache,
  279. const gchar *symbol);
  280. /**
  281. * Process specific function for each cache element (in order they are added)
  282. * @param cache
  283. * @param func
  284. * @param ud
  285. */
  286. void rspamd_symcache_foreach (struct rspamd_symcache *cache,
  287. void (*func) (gint /* id */, const gchar * /* name */,
  288. gint /* flags */, gpointer /* userdata */),
  289. gpointer ud);
  290. /**
  291. * Returns the current item being processed (if any)
  292. * @param task
  293. * @return
  294. */
  295. struct rspamd_symcache_item *rspamd_symcache_get_cur_item (struct rspamd_task *task);
  296. /**
  297. * Replaces the current item being processed.
  298. * Returns the current item being processed (if any)
  299. * @param task
  300. * @param item
  301. * @return
  302. */
  303. struct rspamd_symcache_item *rspamd_symcache_set_cur_item (struct rspamd_task *task,
  304. struct rspamd_symcache_item *item);
  305. /**
  306. * Finalize the current async element potentially calling its deps
  307. */
  308. void rspamd_symcache_finalize_item (struct rspamd_task *task,
  309. struct rspamd_symcache_item *item);
  310. /*
  311. * Increase number of async events pending for an item
  312. */
  313. guint rspamd_symcache_item_async_inc_full (struct rspamd_task *task,
  314. struct rspamd_symcache_item *item,
  315. const gchar *subsystem,
  316. const gchar *loc);
  317. #define rspamd_symcache_item_async_inc(task, item, subsystem) \
  318. rspamd_symcache_item_async_inc_full(task, item, subsystem, G_STRLOC)
  319. /*
  320. * Decrease number of async events pending for an item, asserts if no events pending
  321. */
  322. guint rspamd_symcache_item_async_dec_full (struct rspamd_task *task,
  323. struct rspamd_symcache_item *item,
  324. const gchar *subsystem,
  325. const gchar *loc);
  326. #define rspamd_symcache_item_async_dec(task, item, subsystem) \
  327. rspamd_symcache_item_async_dec_full(task, item, subsystem, G_STRLOC)
  328. /**
  329. * Decrease number of async events pending for an item, asserts if no events pending
  330. * If no events are left, this function calls `rspamd_symbols_cache_finalize_item` and returns TRUE
  331. * @param task
  332. * @param item
  333. * @return
  334. */
  335. gboolean rspamd_symcache_item_async_dec_check_full (struct rspamd_task *task,
  336. struct rspamd_symcache_item *item,
  337. const gchar *subsystem,
  338. const gchar *loc);
  339. #define rspamd_symcache_item_async_dec_check(task, item, subsystem) \
  340. rspamd_symcache_item_async_dec_check_full(task, item, subsystem, G_STRLOC)
  341. #endif