kfsw-services/src/limits/limits.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <math.h> | ||
| 3 | #include <stdbool.h> | ||
| 4 | #include <stddef.h> | ||
| 5 | #include <stdint.h> | ||
| 6 | #include <stdio.h> | ||
| 7 | #include <stdlib.h> | ||
| 8 | #include <string.h> | ||
| 9 | |||
| 10 | #include <zephyr/fs/fs.h> | ||
| 11 | #include <zephyr/kernel.h> | ||
| 12 | #include <zephyr/sys/byteorder.h> | ||
| 13 | #include <zephyr/sys/util.h> | ||
| 14 | |||
| 15 | #if CONFIG_KFSW_PARAM_CSP | ||
| 16 | #include <csp/csp_iflist.h> | ||
| 17 | #include <kfsw/comms/csp.h> | ||
| 18 | #endif | ||
| 19 | |||
| 20 | #include <kfsw/platform/storage.h> | ||
| 21 | #include <kfsw/platform/time.h> | ||
| 22 | #include <kfsw/services/command.h> | ||
| 23 | #include <kfsw/services/event.h> | ||
| 24 | #include <kfsw/services/fbo.h> | ||
| 25 | #include <kfsw/services/ftp.h> | ||
| 26 | #include <kfsw/services/limits.h> | ||
| 27 | #define KFSW_LOG_MODULE KFSW_LOG_MODULE_LIMITS | ||
| 28 | #include <kfsw/services/log.h> | ||
| 29 | |||
| 30 | /* Parameter, comparison, bound, dwell, node and a 79 character action with its | ||
| 31 | * separators come to about 150 bytes; a read-node prefix adds at most 6. | ||
| 32 | */ | ||
| 33 | #define LIMITS_LINE_BYTES 192U | ||
| 34 | #define LIMITS_FIELDS 5U | ||
| 35 | /* Room for a full rule set with comments between the rules. */ | ||
| 36 | #define LIMITS_FILE_BYTES_MAX (CONFIG_KFSW_LIMITS_MAX_RULES * LIMITS_LINE_BYTES * 2U) | ||
| 37 | /* The scheduler refuses a release due now, so the shortest delay is one second. */ | ||
| 38 | /* | ||
| 39 | * The rules arrive as a file, so a composition without the file transfer | ||
| 40 | * service can only ever run the rules it was compiled with, which is none. | ||
| 41 | */ | ||
| 42 | #if !IS_ENABLED(CONFIG_KFSW_FTP) && !IS_ENABLED(CONFIG_ZTEST) | ||
| 43 | #warning "KFSW_LIMITS without KFSW_FTP: nothing can upload a rule file to this node" | ||
| 44 | #endif | ||
| 45 | |||
| 46 | #define LIMITS_RELEASE_DELAY_S 1U | ||
| 47 | |||
| 48 | BUILD_ASSERT((CONFIG_KFSW_LIMITS_INTERVAL_MS == 0) || | ||
| 49 | ((CONFIG_KFSW_LIMITS_INTERVAL_MS >= KFSW_LIMITS_INTERVAL_MIN_MS) && | ||
| 50 | (CONFIG_KFSW_LIMITS_INTERVAL_MS <= KFSW_LIMITS_INTERVAL_MAX_MS)), | ||
| 51 | "KFSW_LIMITS_INTERVAL_MS must be zero or between 100 ms and an hour"); | ||
| 52 | BUILD_ASSERT(CONFIG_KFSW_LIMITS_MAX_RULES < KFSW_LIMITS_NO_RULE, | ||
| 53 | "a rule index must not collide with the no-rule marker"); | ||
| 54 | |||
| 55 | uint8_t kfsw_limits_enabled = 1U; | ||
| 56 | uint32_t kfsw_limits_interval_ms = CONFIG_KFSW_LIMITS_INTERVAL_MS; | ||
| 57 | |||
| 58 | /* The rule set, the staging copy and the line buffer. Held across the | ||
| 59 | * parameter service, the command registry and the scheduler, so the counters | ||
| 60 | * have their own lock: the parameter sampler takes the table lock first and | ||
| 61 | * then state_lock, and nothing may hold state_lock while it waits for either. | ||
| 62 | */ | ||
| 63 | static K_MUTEX_DEFINE(rules_lock); | ||
| 64 | static struct kfsw_limits_rule rules[CONFIG_KFSW_LIMITS_MAX_RULES]; | ||
| 65 | static struct kfsw_limits_rule staged[CONFIG_KFSW_LIMITS_MAX_RULES]; | ||
| 66 | static size_t rule_count; | ||
| 67 | #if CONFIG_KFSW_PARAM_CSP | ||
| 68 | #define LIMITS_REMOTE_GROUP_MAX 16U | ||
| 69 | /* Remote I/O gets half the interval, capped at 5 s (also for manual sweeps). | ||
| 70 | * Each group has its own deadline and at most 16 names. Name resolution can | ||
| 71 | * exchange once per cache miss, retry reads or walk the list; the one-node | ||
| 72 | * cache can be evicted between groups. The deadline bounds that work, not a | ||
| 73 | * promise of one wire exchange per node. Defer when less than 10 ms remains. | ||
| 74 | */ | ||
| 75 | #define LIMITS_REMOTE_BUDGET_MS 5000U | ||
| 76 | #define LIMITS_REMOTE_MIN_MS 10U | ||
| 77 | static const char *names[LIMITS_REMOTE_GROUP_MAX]; | ||
| 78 | static size_t members[LIMITS_REMOTE_GROUP_MAX]; | ||
| 79 | static struct kfsw_param_value values[LIMITS_REMOTE_GROUP_MAX]; | ||
| 80 | static bool done[CONFIG_KFSW_LIMITS_MAX_RULES]; | ||
| 81 | static size_t remote_resume; | ||
| 82 | #endif | ||
| 83 | static char line[LIMITS_LINE_BYTES]; | ||
| 84 | |||
| 85 | static K_MUTEX_DEFINE(state_lock); | ||
| 86 | static struct kfsw_limits_status state = { | ||
| 87 | .last_fired_index = KFSW_LIMITS_NO_RULE, | ||
| 88 | .error = "", | ||
| 89 | }; | ||
| 90 | static bool initialized; | ||
| 91 | |||
| 92 | static void work_handler(struct k_work *work); | ||
| 93 | static K_WORK_DELAYABLE_DEFINE(limits_work, work_handler); | ||
| 94 | static struct k_work_q limits_queue; | ||
| 95 | static K_THREAD_STACK_DEFINE(limits_stack, CONFIG_KFSW_LIMITS_STACK_SIZE); | ||
| 96 | |||
| 97 | static const struct { | ||
| 98 | const char *text; | ||
| 99 | enum kfsw_limits_cmp cmp; | ||
| 100 | } comparisons[] = { | ||
| 101 | {">", KFSW_LIMITS_GT}, {">=", KFSW_LIMITS_GE}, {"<", KFSW_LIMITS_LT}, | ||
| 102 | {"<=", KFSW_LIMITS_LE}, {"==", KFSW_LIMITS_EQ}, {"!=", KFSW_LIMITS_NE}, | ||
| 103 | }; | ||
| 104 | |||
| 105 | 40 | const char *kfsw_limits_cmp_text(enum kfsw_limits_cmp cmp) | |
| 106 | { | ||
| 107 | 62 | for (size_t index = 0U; index < ARRAY_SIZE(comparisons); index++) { | |
| 108 | 62 | if (comparisons[index].cmp == cmp) { | |
| 109 | 40 | return comparisons[index].text; | |
| 110 | } | ||
| 111 | } | ||
| 112 | ✗ | return "?"; | |
| 113 | } | ||
| 114 | |||
| 115 | 318 | static void saturating_increment(uint32_t *counter) | |
| 116 | { | ||
| 117 | 318 | if (*counter < UINT32_MAX) { | |
| 118 | 318 | (*counter)++; | |
| 119 | } | ||
| 120 | 318 | } | |
| 121 | |||
| 122 | 10 | static void note_failure(void) | |
| 123 | { | ||
| 124 | 10 | k_mutex_lock(&state_lock, K_FOREVER); | |
| 125 | 10 | saturating_increment(&state.failures); | |
| 126 | 10 | k_mutex_unlock(&state_lock); | |
| 127 | 10 | } | |
| 128 | |||
| 129 | /* A parameter value widened to the one type its comparison is done in. */ | ||
| 130 | struct wide { | ||
| 131 | enum { WIDE_UNSIGNED, WIDE_SIGNED, WIDE_FLOAT } kind; | ||
| 132 | union { | ||
| 133 | uint64_t u; | ||
| 134 | int64_t i; | ||
| 135 | double f; | ||
| 136 | }; | ||
| 137 | }; | ||
| 138 | |||
| 139 | /* Comparison of two values that cannot be ordered, a NaN. */ | ||
| 140 | #define ORDER_NONE 2 | ||
| 141 | |||
| 142 | 154 | static bool is_numeric(enum kfsw_param_type type) | |
| 143 | { | ||
| 144 | 154 | return (type <= KFSW_PARAM_DOUBLE); | |
| 145 | } | ||
| 146 | |||
| 147 | 378 | static bool is_float(enum kfsw_param_type type) | |
| 148 | { | ||
| 149 | 378 | return (type == KFSW_PARAM_FLOAT) || (type == KFSW_PARAM_DOUBLE); | |
| 150 | } | ||
| 151 | |||
| 152 | 264 | static void widen(enum kfsw_param_type type, const union kfsw_param_scalar *scalar, | |
| 153 | struct wide *wide) | ||
| 154 | { | ||
| 155 | 264 | wide->kind = WIDE_UNSIGNED; | |
| 156 | 264 | switch (type) { | |
| 157 | ✗ | case KFSW_PARAM_U8: | |
| 158 | case KFSW_PARAM_X8: | ||
| 159 | ✗ | wide->u = scalar->u8; | |
| 160 | ✗ | break; | |
| 161 | 24 | case KFSW_PARAM_U16: | |
| 162 | case KFSW_PARAM_X16: | ||
| 163 | 24 | wide->u = scalar->u16; | |
| 164 | 24 | break; | |
| 165 | 220 | case KFSW_PARAM_U32: | |
| 166 | case KFSW_PARAM_X32: | ||
| 167 | 220 | wide->u = scalar->u32; | |
| 168 | 220 | break; | |
| 169 | ✗ | case KFSW_PARAM_U64: | |
| 170 | case KFSW_PARAM_X64: | ||
| 171 | ✗ | wide->u = scalar->u64; | |
| 172 | ✗ | break; | |
| 173 | ✗ | case KFSW_PARAM_I8: | |
| 174 | ✗ | wide->kind = WIDE_SIGNED; | |
| 175 | /* Widened explicitly: a plain assignment from a signed char is | ||
| 176 | * the shape that usually hides a sign-extension mistake. | ||
| 177 | */ | ||
| 178 | ✗ | wide->i = (int64_t)scalar->i8; | |
| 179 | ✗ | break; | |
| 180 | 8 | case KFSW_PARAM_I16: | |
| 181 | 8 | wide->kind = WIDE_SIGNED; | |
| 182 | 8 | wide->i = scalar->i16; | |
| 183 | 8 | break; | |
| 184 | ✗ | case KFSW_PARAM_I32: | |
| 185 | ✗ | wide->kind = WIDE_SIGNED; | |
| 186 | ✗ | wide->i = scalar->i32; | |
| 187 | ✗ | break; | |
| 188 | ✗ | case KFSW_PARAM_I64: | |
| 189 | ✗ | wide->kind = WIDE_SIGNED; | |
| 190 | ✗ | wide->i = scalar->i64; | |
| 191 | ✗ | break; | |
| 192 | 12 | case KFSW_PARAM_FLOAT: | |
| 193 | 12 | wide->kind = WIDE_FLOAT; | |
| 194 | 12 | wide->f = scalar->f32; | |
| 195 | 12 | break; | |
| 196 | ✗ | case KFSW_PARAM_DOUBLE: | |
| 197 | ✗ | wide->kind = WIDE_FLOAT; | |
| 198 | ✗ | wide->f = scalar->f64; | |
| 199 | ✗ | break; | |
| 200 | ✗ | default: | |
| 201 | ✗ | wide->u = 0U; | |
| 202 | ✗ | break; | |
| 203 | } | ||
| 204 | 264 | } | |
| 205 | |||
| 206 | 132 | static int order(const struct wide *a, const struct wide *b) | |
| 207 | { | ||
| 208 | 132 | switch (a->kind) { | |
| 209 | 4 | case WIDE_SIGNED: | |
| 210 | 4 | return (a->i > b->i) - (a->i < b->i); | |
| 211 | 6 | case WIDE_FLOAT: | |
| 212 | 6 | if (isnan(a->f) || isnan(b->f)) { | |
| 213 | ✗ | return ORDER_NONE; | |
| 214 | } | ||
| 215 | 6 | return (a->f > b->f) - (a->f < b->f); | |
| 216 | 122 | default: | |
| 217 | 122 | return (a->u > b->u) - (a->u < b->u); | |
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | 132 | static bool holds(uint8_t cmp, int ordering) | |
| 222 | { | ||
| 223 | 132 | if (ordering == ORDER_NONE) { | |
| 224 | ✗ | return false; | |
| 225 | } | ||
| 226 | 132 | switch (cmp) { | |
| 227 | 112 | case KFSW_LIMITS_GT: | |
| 228 | 112 | return ordering > 0; | |
| 229 | 4 | case KFSW_LIMITS_GE: | |
| 230 | 4 | return ordering >= 0; | |
| 231 | 4 | case KFSW_LIMITS_LT: | |
| 232 | 4 | return ordering < 0; | |
| 233 | ✗ | case KFSW_LIMITS_LE: | |
| 234 | ✗ | return ordering <= 0; | |
| 235 | 12 | case KFSW_LIMITS_EQ: | |
| 236 | 12 | return ordering == 0; | |
| 237 | ✗ | default: | |
| 238 | ✗ | return ordering != 0; | |
| 239 | } | ||
| 240 | } | ||
| 241 | |||
| 242 | /* Take the next whitespace separated field in place, or NULL at the end. */ | ||
| 243 | 1264 | static char *next_field(char **cursor) | |
| 244 | { | ||
| 245 | 1264 | char *start = *cursor; | |
| 246 | char *end; | ||
| 247 | |||
| 248 | 1268 | while ((*start == ' ') || (*start == '\t')) { | |
| 249 | 4 | start++; | |
| 250 | } | ||
| 251 | 1264 | if (*start == '\0') { | |
| 252 | 142 | *cursor = start; | |
| 253 | 142 | return NULL; | |
| 254 | } | ||
| 255 | 1122 | end = start; | |
| 256 | 4070 | while ((*end != '\0') && (*end != ' ') && (*end != '\t')) { | |
| 257 | 2948 | end++; | |
| 258 | } | ||
| 259 | 1122 | if (*end != '\0') { | |
| 260 | 972 | *end++ = '\0'; | |
| 261 | } | ||
| 262 | 1122 | *cursor = end; | |
| 263 | 1122 | return start; | |
| 264 | } | ||
| 265 | |||
| 266 | 424 | static bool parse_unsigned(const char *text, unsigned long maximum, unsigned long *value) | |
| 267 | { | ||
| 268 | char *end; | ||
| 269 | |||
| 270 | 424 | if ((text[0] < '0') || (text[0] > '9')) { | |
| 271 | 8 | return false; | |
| 272 | } | ||
| 273 | 416 | errno = 0; | |
| 274 | 416 | *value = strtoul(text, &end, 10); | |
| 275 | 416 | return (errno == 0) && (*end == '\0') && (*value <= maximum); | |
| 276 | } | ||
| 277 | |||
| 278 | /* Refuse at load what would fail at the release: the scheduler resolves the | ||
| 279 | * command and parses its arguments when the entry is added, and by then the | ||
| 280 | * operator who wrote the rule is long gone. | ||
| 281 | */ | ||
| 282 | 146 | static const char *check_action(const char *action) | |
| 283 | { | ||
| 284 | char text[KFSW_FBO_SCHEDULE_LINE_MAX]; | ||
| 285 | struct kfsw_command_info info; | ||
| 286 | struct kfsw_command_arg arg; | ||
| 287 | 146 | char *cursor = text; | |
| 288 | char *token; | ||
| 289 | |||
| 290 | 146 | if (strlen(action) >= sizeof(text)) { | |
| 291 | ✗ | return "action is too long"; | |
| 292 | } | ||
| 293 | 146 | strcpy(text, action); | |
| 294 | 146 | token = next_field(&cursor); | |
| 295 | 146 | if ((token == NULL) || (kfsw_command_find(token, &info) != 0)) { | |
| 296 | 2 | return "action is not a registered command"; | |
| 297 | } | ||
| 298 | 152 | for (uint8_t index = 0U; index < info.arg_count; index++) { | |
| 299 | 12 | token = next_field(&cursor); | |
| 300 | 22 | if ((token == NULL) || | |
| 301 | 10 | (kfsw_command_parse_arg(token, info.arg_types[index], &arg) != 0)) { | |
| 302 | 4 | return "action argument is missing or of the wrong type"; | |
| 303 | } | ||
| 304 | } | ||
| 305 | 140 | if (next_field(&cursor) != NULL) { | |
| 306 | 2 | return "action has more arguments than the command takes"; | |
| 307 | } | ||
| 308 | 138 | return NULL; | |
| 309 | } | ||
| 310 | |||
| 311 | 166 | static const char *parse_comparison(const char *text, struct kfsw_limits_rule *rule) | |
| 312 | { | ||
| 313 | 252 | for (size_t index = 0U; index < ARRAY_SIZE(comparisons); index++) { | |
| 314 | 250 | if (strcmp(comparisons[index].text, text) == 0) { | |
| 315 | 164 | rule->cmp = comparisons[index].cmp; | |
| 316 | 164 | return NULL; | |
| 317 | } | ||
| 318 | } | ||
| 319 | 2 | return "unknown comparison"; | |
| 320 | } | ||
| 321 | |||
| 322 | 190 | static const char *parse_read_node(char **name, uint16_t *node) | |
| 323 | { | ||
| 324 | 190 | char *colon = strchr(*name, ':'); | |
| 325 | unsigned long number; | ||
| 326 | 190 | unsigned long maximum = UINT16_MAX; | |
| 327 | |||
| 328 | 190 | if (colon == NULL) { | |
| 329 | 66 | return NULL; | |
| 330 | } | ||
| 331 | 124 | *colon = '\0'; | |
| 332 | #if CONFIG_KFSW_PARAM_CSP | ||
| 333 | 120 | maximum = KFSW_CSP_BROADCAST_ADDRESS - 1U; | |
| 334 | #endif | ||
| 335 | 228 | if (!parse_unsigned(*name, maximum, &number) || (colon[1] == '\0') || | |
| 336 | 108 | (strchr(colon + 1, ':') != NULL)) { | |
| 337 | 18 | return "invalid parameter node prefix"; | |
| 338 | } | ||
| 339 | 106 | *name = colon + 1; | |
| 340 | #if CONFIG_KFSW_PARAM_CSP | ||
| 341 | 102 | if (csp_iflist_get_by_addr((uint16_t)number) != NULL) { | |
| 342 | 2 | number = 0U; | |
| 343 | } | ||
| 344 | #else | ||
| 345 | 4 | if (number != 0U) { | |
| 346 | 2 | return "this build has no remote parameters"; | |
| 347 | } | ||
| 348 | #endif | ||
| 349 | 104 | *node = (uint16_t)number; | |
| 350 | 104 | return NULL; | |
| 351 | } | ||
| 352 | |||
| 353 | 164 | static const char *parse_bound(const char *text, struct kfsw_limits_rule *rule) | |
| 354 | { | ||
| 355 | 164 | struct kfsw_param_value value = {.type = rule->type}; | |
| 356 | |||
| 357 | 164 | if (is_float(rule->type) && | |
| 358 | 6 | ((rule->cmp == KFSW_LIMITS_EQ) || (rule->cmp == KFSW_LIMITS_NE))) { | |
| 359 | 2 | return "equality is refused for a float parameter"; | |
| 360 | } | ||
| 361 | 162 | if (strlen(text) >= sizeof(rule->bound_text)) { | |
| 362 | ✗ | return "bound is too long"; | |
| 363 | } | ||
| 364 | 162 | if (rule->read_node != 0U) { | |
| 365 | 98 | const enum kfsw_param_type types[] = {KFSW_PARAM_I64, KFSW_PARAM_U64, | |
| 366 | KFSW_PARAM_DOUBLE}; | ||
| 367 | 112 | for (size_t i = 0U; i < ARRAY_SIZE(types); i++) { | |
| 368 | 110 | value.type = types[i]; | |
| 369 | 110 | if (kfsw_param_parse_text(text, &value) == 0) { | |
| 370 | 96 | break; | |
| 371 | } | ||
| 372 | } | ||
| 373 | } | ||
| 374 | 162 | if (kfsw_param_parse_text(text, &value) != 0) { | |
| 375 | 6 | return "bound does not fit the type of the parameter"; | |
| 376 | } | ||
| 377 | 156 | if (is_float(value.type) && | |
| 378 | 8 | ((rule->cmp == KFSW_LIMITS_EQ) || (rule->cmp == KFSW_LIMITS_NE))) { | |
| 379 | 4 | return "equality is refused for a float parameter"; | |
| 380 | } | ||
| 381 | 152 | rule->bound = value.scalar; | |
| 382 | 152 | strcpy(rule->bound_text, text); | |
| 383 | 152 | return NULL; | |
| 384 | } | ||
| 385 | |||
| 386 | /* Returns what is wrong with a line, or NULL once @p rule is filled in. */ | ||
| 387 | 194 | static const char *parse_line(char *text, struct kfsw_limits_rule *rule) | |
| 388 | { | ||
| 389 | static const char *const missing = | ||
| 390 | "expected parameter, comparison, bound, dwell, node and an action"; | ||
| 391 | struct kfsw_param_info info; | ||
| 392 | unsigned long number; | ||
| 393 | 194 | char *cursor = text; | |
| 394 | char *fields[LIMITS_FIELDS]; | ||
| 395 | char *action; | ||
| 396 | char *end; | ||
| 397 | const char *reason; | ||
| 398 | |||
| 399 | 194 | memset(rule, 0, sizeof(*rule)); | |
| 400 | 1158 | for (size_t index = 0U; index < LIMITS_FIELDS; index++) { | |
| 401 | 966 | fields[index] = next_field(&cursor); | |
| 402 | 966 | if (fields[index] == NULL) { | |
| 403 | 2 | return missing; | |
| 404 | } | ||
| 405 | } | ||
| 406 | 192 | action = cursor; | |
| 407 | 196 | while ((*action == ' ') || (*action == '\t')) { | |
| 408 | 4 | action++; | |
| 409 | } | ||
| 410 | 192 | end = action + strlen(action); | |
| 411 | 196 | while ((end > action) && ((end[-1] == ' ') || (end[-1] == '\t'))) { | |
| 412 | 4 | *--end = '\0'; | |
| 413 | } | ||
| 414 | 192 | if (*action == '\0') { | |
| 415 | 2 | return missing; | |
| 416 | } | ||
| 417 | |||
| 418 | 190 | reason = parse_read_node(&fields[0], &rule->read_node); | |
| 419 | 190 | if (reason != NULL) { | |
| 420 | 20 | return reason; | |
| 421 | } | ||
| 422 | 170 | if (strlen(fields[0]) > KFSW_PARAM_NAME_MAX) { | |
| 423 | ✗ | return "parameter name is too long"; | |
| 424 | } | ||
| 425 | 170 | rule->type = KFSW_PARAM_INVALID; | |
| 426 | 170 | if (rule->read_node == 0U) { | |
| 427 | 72 | if (kfsw_param_get_info(fields[0], &info) != 0) { | |
| 428 | 2 | return "unknown parameter"; | |
| 429 | } | ||
| 430 | 70 | if (!is_numeric(info.type)) { | |
| 431 | 2 | return "parameter is not a number"; | |
| 432 | } | ||
| 433 | 68 | rule->type = info.type; | |
| 434 | } | ||
| 435 | 166 | strcpy(rule->parameter, fields[0]); | |
| 436 | |||
| 437 | 166 | reason = parse_comparison(fields[1], rule); | |
| 438 | 166 | if (reason != NULL) { | |
| 439 | 2 | return reason; | |
| 440 | } | ||
| 441 | 164 | reason = parse_bound(fields[2], rule); | |
| 442 | 164 | if (reason != NULL) { | |
| 443 | 12 | return reason; | |
| 444 | } | ||
| 445 | |||
| 446 | 152 | if (!parse_unsigned(fields[3], KFSW_LIMITS_DWELL_MAX, &number) || (number == 0U)) { | |
| 447 | 4 | return "dwell must be 1 to 255"; | |
| 448 | } | ||
| 449 | 148 | rule->dwell = (uint8_t)number; | |
| 450 | |||
| 451 | 148 | if (!parse_unsigned(fields[4], UINT16_MAX, &number)) { | |
| 452 | 2 | return "node is not an address"; | |
| 453 | } | ||
| 454 | 146 | rule->node = (uint16_t)number; | |
| 455 | #if !CONFIG_KFSW_COMMAND_CSP | ||
| 456 | 146 | if (rule->node != 0U) { | |
| 457 | ✗ | return "this build has no remote commanding"; | |
| 458 | } | ||
| 459 | #endif | ||
| 460 | |||
| 461 | 146 | reason = check_action(action); | |
| 462 | 146 | if (reason != NULL) { | |
| 463 | 8 | return reason; | |
| 464 | } | ||
| 465 | 138 | strcpy(rule->action, action); | |
| 466 | 138 | return NULL; | |
| 467 | } | ||
| 468 | |||
| 469 | /* Read one line into the shared buffer. A line longer than the buffer is | ||
| 470 | * consumed whole and reported, never split into two rules. | ||
| 471 | */ | ||
| 472 | 396 | static int read_line(struct fs_file_t *file, size_t *bytes, bool *at_end, bool *too_long) | |
| 473 | { | ||
| 474 | 396 | size_t used = 0U; | |
| 475 | |||
| 476 | 396 | *too_long = false; | |
| 477 | 4906 | for (;;) { | |
| 478 | char character; | ||
| 479 | 5302 | ssize_t count = fs_read(file, &character, 1U); | |
| 480 | |||
| 481 | 5302 | if (count < 0) { | |
| 482 | ✗ | return (int)count; | |
| 483 | } | ||
| 484 | 5302 | if (count == 0) { | |
| 485 | 128 | *at_end = true; | |
| 486 | 128 | break; | |
| 487 | } | ||
| 488 | 5174 | if (++(*bytes) > LIMITS_FILE_BYTES_MAX) { | |
| 489 | ✗ | return -EFBIG; | |
| 490 | } | ||
| 491 | 5174 | if (character == '\n') { | |
| 492 | 268 | break; | |
| 493 | } | ||
| 494 | 4906 | if (character == '\r') { | |
| 495 | ✗ | continue; | |
| 496 | } | ||
| 497 | 4906 | if (used + 1U >= sizeof(line)) { | |
| 498 | ✗ | *too_long = true; | |
| 499 | ✗ | continue; | |
| 500 | } | ||
| 501 | 4906 | line[used++] = character; | |
| 502 | } | ||
| 503 | 396 | line[used] = '\0'; | |
| 504 | 396 | return 0; | |
| 505 | } | ||
| 506 | |||
| 507 | /* Parse the whole file into staged[]. Returns -EINVAL with a reason for a bad | ||
| 508 | * line, or another negative errno for a read that failed. | ||
| 509 | */ | ||
| 510 | 186 | static int parse_file(struct fs_file_t *file, size_t *count, uint16_t *error_line, | |
| 511 | const char **reason) | ||
| 512 | { | ||
| 513 | 186 | size_t bytes = 0U; | |
| 514 | 186 | uint16_t number = 0U; | |
| 515 | 186 | bool at_end = false; | |
| 516 | |||
| 517 | 186 | *count = 0U; | |
| 518 | 186 | *error_line = 0U; | |
| 519 | 186 | *reason = NULL; | |
| 520 | 524 | while (!at_end) { | |
| 521 | bool too_long; | ||
| 522 | 396 | char *text = line; | |
| 523 | 396 | int result = read_line(file, &bytes, &at_end, &too_long); | |
| 524 | |||
| 525 | 396 | if (result != 0) { | |
| 526 | 58 | return result; | |
| 527 | } | ||
| 528 | 396 | number++; | |
| 529 | 396 | while ((*text == ' ') || (*text == '\t')) { | |
| 530 | ✗ | text++; | |
| 531 | } | ||
| 532 | 396 | if ((*text == '#') || ((*text == '\0') && !too_long)) { | |
| 533 | 200 | continue; | |
| 534 | } | ||
| 535 | 196 | if (too_long) { | |
| 536 | ✗ | *reason = "line is too long"; | |
| 537 | 196 | } else if (*count == ARRAY_SIZE(staged)) { | |
| 538 | 2 | *reason = "more rules than this build holds"; | |
| 539 | } else { | ||
| 540 | 194 | *reason = parse_line(text, &staged[*count]); | |
| 541 | } | ||
| 542 | 196 | if (*reason != NULL) { | |
| 543 | 58 | *error_line = number; | |
| 544 | 58 | return -EINVAL; | |
| 545 | } | ||
| 546 | 138 | staged[*count].index = (uint8_t)*count; | |
| 547 | 138 | (*count)++; | |
| 548 | } | ||
| 549 | 128 | return 0; | |
| 550 | } | ||
| 551 | |||
| 552 | 196 | int kfsw_limits_load(const char *name) | |
| 553 | { | ||
| 554 | char path[96]; | ||
| 555 | struct fs_file_t file; | ||
| 556 | 196 | const char *reason = NULL; | |
| 557 | 196 | uint16_t error_line = 0U; | |
| 558 | 196 | size_t count = 0U; | |
| 559 | int result; | ||
| 560 | |||
| 561 | 196 | if ((name == NULL) || (memchr(name, '\0', KFSW_FBO_NAME_MAX) == NULL) || | |
| 562 | 194 | (name[0] == '\0') || (strchr(name, '/') != NULL) || (strstr(name, "..") != NULL)) { | |
| 563 | 4 | return -EINVAL; | |
| 564 | } | ||
| 565 | 192 | if (!kfsw_storage_is_ready()) { | |
| 566 | ✗ | return -ENODEV; | |
| 567 | } | ||
| 568 | 192 | (void)snprintf(path, sizeof(path), "%s/%s/%s", KFSW_FTP_STORAGE_ROOT, KFSW_LIMITS_FTP_PATH, | |
| 569 | name); | ||
| 570 | |||
| 571 | 192 | k_mutex_lock(&rules_lock, K_FOREVER); | |
| 572 | 192 | fs_file_t_init(&file); | |
| 573 | 192 | result = fs_open(&file, path, FS_O_READ); | |
| 574 | 192 | if (result == 0) { | |
| 575 | 186 | result = parse_file(&file, &count, &error_line, &reason); | |
| 576 | 186 | (void)fs_close(&file); | |
| 577 | } | ||
| 578 | 192 | if (result == 0) { | |
| 579 | 128 | memcpy(rules, staged, count * sizeof(rules[0])); | |
| 580 | 128 | rule_count = count; | |
| 581 | #if CONFIG_KFSW_PARAM_CSP | ||
| 582 | 74 | remote_resume = 0U; | |
| 583 | #endif | ||
| 584 | 128 | k_mutex_lock(&state_lock, K_FOREVER); | |
| 585 | 128 | state.failed_by = 0U; | |
| 586 | 128 | k_mutex_unlock(&state_lock); | |
| 587 | } | ||
| 588 | 192 | k_mutex_unlock(&rules_lock); | |
| 589 | |||
| 590 | 192 | if (result == 0) { | |
| 591 | 128 | k_mutex_lock(&state_lock, K_FOREVER); | |
| 592 | 128 | state.rules = (uint8_t)count; | |
| 593 | 128 | state.error_line = 0U; | |
| 594 | 128 | state.error = ""; | |
| 595 | 128 | k_mutex_unlock(&state_lock); | |
| 596 | 128 | kfsw_log_info("Limits: %u rules loaded from %s", (unsigned int)count, name); | |
| 597 | 64 | } else if (reason != NULL) { | |
| 598 | 58 | k_mutex_lock(&state_lock, K_FOREVER); | |
| 599 | 58 | saturating_increment(&state.parse_errors); | |
| 600 | 58 | state.error_line = error_line; | |
| 601 | 58 | state.error = reason; | |
| 602 | 58 | k_mutex_unlock(&state_lock); | |
| 603 | 58 | kfsw_log_error("Limits: %s refused, line %u: %s", name, error_line, reason); | |
| 604 | } | ||
| 605 | 192 | return result; | |
| 606 | } | ||
| 607 | |||
| 608 | 76 | static bool release(const struct kfsw_limits_rule *rule) | |
| 609 | { | ||
| 610 | uint8_t payload[3]; | ||
| 611 | uint16_t slot; | ||
| 612 | 76 | int result = kfsw_fbo_schedule_add(KFSW_FBO_TIME_RELATIVE, LIMITS_RELEASE_DELAY_S, | |
| 613 | 76 | rule->node, rule->action, &slot); | |
| 614 | |||
| 615 | /* An identical entry still waiting stands for this release. */ | ||
| 616 | 76 | if (result == -EEXIST) { | |
| 617 | 36 | return true; | |
| 618 | } | ||
| 619 | 40 | if (result != 0) { | |
| 620 | ✗ | note_failure(); | |
| 621 | ✗ | kfsw_log_warning("Limits: rule %u could not release '%s' (%d)", rule->index, | |
| 622 | rule->action, result); | ||
| 623 | ✗ | return false; | |
| 624 | } | ||
| 625 | |||
| 626 | 40 | k_mutex_lock(&state_lock, K_FOREVER); | |
| 627 | 40 | saturating_increment(&state.fires); | |
| 628 | 40 | state.last_fired_index = rule->index; | |
| 629 | 40 | state.last_fired_ms = (uint32_t)MIN(kfsw_time_monotonic_ms(), (uint64_t)UINT32_MAX); | |
| 630 | 40 | k_mutex_unlock(&state_lock); | |
| 631 | |||
| 632 | 40 | kfsw_log_warning("Limits: rule %u fired, %s %s %s, releasing '%s' on node %u", rule->index, | |
| 633 | rule->parameter, kfsw_limits_cmp_text(rule->cmp), rule->bound_text, | ||
| 634 | rule->action, rule->node); | ||
| 635 | 40 | payload[0] = rule->index; | |
| 636 | 40 | sys_put_be16(rule->node, &payload[1]); | |
| 637 | 40 | kfsw_event_emit(KFSW_EVENT_SOURCE_LIMITS, KFSW_EVENT_LIMITS_FIRED, KFSW_EVENT_WARNING, | |
| 638 | payload, sizeof(payload)); | ||
| 639 | 40 | return true; | |
| 640 | } | ||
| 641 | |||
| 642 | 132 | static void apply_reading(struct kfsw_limits_rule *rule, const struct kfsw_param_value *value) | |
| 643 | { | ||
| 644 | struct wide current; | ||
| 645 | struct wide bound; | ||
| 646 | |||
| 647 | 132 | widen(rule->type, &value->scalar, ¤t); | |
| 648 | 132 | widen(rule->type, &rule->bound, &bound); | |
| 649 | 132 | if (!holds(rule->cmp, order(¤t, &bound))) { | |
| 650 | 22 | rule->streak = 0U; | |
| 651 | 22 | rule->latched = false; | |
| 652 | 22 | return; | |
| 653 | } | ||
| 654 | |||
| 655 | 110 | if (rule->streak < UINT8_MAX) { | |
| 656 | 110 | rule->streak++; | |
| 657 | } | ||
| 658 | 110 | if (!rule->latched && (rule->streak >= rule->dwell)) { | |
| 659 | 76 | rule->latched = release(rule); | |
| 660 | } | ||
| 661 | } | ||
| 662 | |||
| 663 | 60 | static void evaluate(struct kfsw_limits_rule *rule) | |
| 664 | { | ||
| 665 | struct kfsw_param_value value; | ||
| 666 | 60 | int result = kfsw_param_get(rule->parameter, &value); | |
| 667 | |||
| 668 | /* No local reading says nothing about the comparison, so streak and latch stay. */ | ||
| 669 | 60 | if (result != 0) { | |
| 670 | ✗ | note_failure(); | |
| 671 | ✗ | kfsw_log_warning("Limits: rule %u cannot read %s (%d)", rule->index, | |
| 672 | rule->parameter, result); | ||
| 673 | ✗ | return; | |
| 674 | } | ||
| 675 | 60 | apply_reading(rule, &value); | |
| 676 | } | ||
| 677 | |||
| 678 | #if CONFIG_KFSW_PARAM_CSP | ||
| 679 | 82 | static void apply_remote_reading(struct kfsw_limits_rule *rule, | |
| 680 | const struct kfsw_param_value *value) | ||
| 681 | { | ||
| 682 | 82 | struct kfsw_param_value bound = {.type = value->type}; | |
| 683 | |||
| 684 | 82 | if ((rule->type != value->type) || !is_numeric(value->type)) { | |
| 685 | 62 | rule->type = KFSW_PARAM_INVALID; | |
| 686 | 120 | if (!is_numeric(value->type) || | |
| 687 | 58 | (is_float(value->type) && | |
| 688 | 60 | ((rule->cmp == KFSW_LIMITS_EQ) || (rule->cmp == KFSW_LIMITS_NE))) || | |
| 689 | 54 | (kfsw_param_parse_text(rule->bound_text, &bound) != 0)) { | |
| 690 | 10 | rule->streak = 0U; | |
| 691 | 10 | note_failure(); | |
| 692 | 10 | kfsw_log_warning("Limits: rule %u cannot bind %u:%s to bound %s", | |
| 693 | rule->index, rule->read_node, rule->parameter, | ||
| 694 | rule->bound_text); | ||
| 695 | 10 | return; | |
| 696 | } | ||
| 697 | 52 | rule->bound = bound.scalar; | |
| 698 | 52 | rule->type = value->type; | |
| 699 | } | ||
| 700 | 72 | apply_reading(rule, value); | |
| 701 | } | ||
| 702 | |||
| 703 | 168 | static size_t pending_groups(void) | |
| 704 | { | ||
| 705 | 168 | size_t groups = 0U; | |
| 706 | |||
| 707 | 656 | for (size_t i = 0U; i < rule_count; i++) { | |
| 708 | 488 | if (done[i]) { | |
| 709 | 238 | continue; | |
| 710 | } | ||
| 711 | 250 | size_t preceding = 0U; | |
| 712 | 1072 | for (size_t j = 0U; j < i; j++) { | |
| 713 | 822 | if (!done[j] && (rules[j].read_node == rules[i].read_node)) { | |
| 714 | 284 | preceding++; | |
| 715 | } | ||
| 716 | } | ||
| 717 | 250 | if ((preceding % LIMITS_REMOTE_GROUP_MAX) == 0U) { | |
| 718 | 212 | groups++; | |
| 719 | } | ||
| 720 | } | ||
| 721 | 168 | return groups; | |
| 722 | } | ||
| 723 | |||
| 724 | 92 | static void read_group(uint16_t node, size_t count, int64_t deadline) | |
| 725 | { | ||
| 726 | 92 | int result = kfsw_param_remote_get_many_until(node, names, count, values, deadline); | |
| 727 | |||
| 728 | 92 | k_mutex_lock(&state_lock, K_FOREVER); | |
| 729 | 92 | if (result != 0) { | |
| 730 | 44 | state.failed_by = node; | |
| 731 | 92 | for (size_t i = 0U; i < count; i++) { | |
| 732 | 48 | saturating_increment(&state.failures); | |
| 733 | 48 | saturating_increment(&state.remote_failures); | |
| 734 | } | ||
| 735 | 48 | } else if (state.failed_by == node) { | |
| 736 | 6 | state.failed_by = 0U; | |
| 737 | } | ||
| 738 | 92 | k_mutex_unlock(&state_lock); | |
| 739 | 92 | if (result != 0) { | |
| 740 | 44 | kfsw_log_warning("Limits: cannot read node %u, %u rules (%d)", node, | |
| 741 | (unsigned int)count, result); | ||
| 742 | } | ||
| 743 | 222 | for (size_t i = 0U; i < count; i++) { | |
| 744 | 130 | struct kfsw_limits_rule *rule = &rules[members[i]]; | |
| 745 | |||
| 746 | 130 | if (result != 0) { | |
| 747 | 48 | rule->streak = 0U; | |
| 748 | } else { | ||
| 749 | 82 | apply_remote_reading(rule, &values[i]); | |
| 750 | } | ||
| 751 | } | ||
| 752 | 92 | } | |
| 753 | |||
| 754 | /* rules_lock protects the scratch buffers and resume position across blocking reads. */ | ||
| 755 | 76 | static size_t evaluate_remote(void) | |
| 756 | { | ||
| 757 | 76 | uint32_t interval = kfsw_limits_interval_ms; | |
| 758 | 76 | uint32_t budget = (interval == 0U) ? LIMITS_REMOTE_BUDGET_MS | |
| 759 | 76 | : MIN(interval / 2U, LIMITS_REMOTE_BUDGET_MS); | |
| 760 | 76 | int64_t deadline = k_uptime_get() + budget; | |
| 761 | 76 | size_t evaluated = 0U; | |
| 762 | size_t groups; | ||
| 763 | |||
| 764 | 168 | while ((groups = pending_groups()) != 0U) { | |
| 765 | 96 | int64_t now = k_uptime_get(); | |
| 766 | 96 | int64_t left = deadline - now; | |
| 767 | 96 | size_t first = remote_resume; | |
| 768 | 96 | size_t count = 0U; | |
| 769 | |||
| 770 | 152 | while (done[first]) { | |
| 771 | 56 | first = (first + 1U) % rule_count; | |
| 772 | } | ||
| 773 | 96 | remote_resume = first; | |
| 774 | 96 | if (left < LIMITS_REMOTE_MIN_MS) { | |
| 775 | 4 | kfsw_log_warning("Limits: remote budget exhausted, deferring rules"); | |
| 776 | 4 | break; | |
| 777 | } | ||
| 778 | 92 | uint16_t node = rules[first].read_node; | |
| 779 | 418 | for (size_t i = 0U; (i < rule_count) && (count < ARRAY_SIZE(names)); i++) { | |
| 780 | 326 | size_t index = (first + i) % rule_count; | |
| 781 | |||
| 782 | 326 | if (!done[index] && (rules[index].read_node == node)) { | |
| 783 | 130 | names[count] = rules[index].parameter; | |
| 784 | 130 | members[count++] = index; | |
| 785 | 130 | done[index] = true; | |
| 786 | } | ||
| 787 | } | ||
| 788 | 92 | int64_t share = MAX(left / (int64_t)groups, LIMITS_REMOTE_MIN_MS); | |
| 789 | 92 | read_group(node, count, now + share); | |
| 790 | 92 | evaluated += count; | |
| 791 | } | ||
| 792 | 76 | return evaluated; | |
| 793 | } | ||
| 794 | #endif | ||
| 795 | |||
| 796 | 116 | int kfsw_limits_sweep(void) | |
| 797 | { | ||
| 798 | 116 | size_t evaluated = 0U; | |
| 799 | |||
| 800 | 116 | k_mutex_lock(&rules_lock, K_FOREVER); | |
| 801 | 116 | if (!kfsw_limits_enabled) { | |
| 802 | /* Dwell counts consecutive sweeps, and a disabled one breaks the run. */ | ||
| 803 | 4 | for (size_t index = 0U; index < rule_count; index++) { | |
| 804 | 2 | rules[index].streak = 0U; | |
| 805 | } | ||
| 806 | 2 | k_mutex_unlock(&rules_lock); | |
| 807 | 2 | return -EACCES; | |
| 808 | } | ||
| 809 | 316 | for (size_t index = 0U; index < rule_count; index++) { | |
| 810 | #if CONFIG_KFSW_PARAM_CSP | ||
| 811 | 160 | done[index] = (rules[index].read_node == 0U); | |
| 812 | #endif | ||
| 813 | 202 | if (rules[index].read_node == 0U) { | |
| 814 | 60 | evaluate(&rules[index]); | |
| 815 | 60 | evaluated++; | |
| 816 | } | ||
| 817 | } | ||
| 818 | #if CONFIG_KFSW_PARAM_CSP | ||
| 819 | 76 | evaluated += evaluate_remote(); | |
| 820 | #endif | ||
| 821 | 114 | k_mutex_unlock(&rules_lock); | |
| 822 | |||
| 823 | 114 | k_mutex_lock(&state_lock, K_FOREVER); | |
| 824 | 114 | saturating_increment(&state.sweeps); | |
| 825 | 114 | k_mutex_unlock(&state_lock); | |
| 826 | 114 | return (int)evaluated; | |
| 827 | } | ||
| 828 | |||
| 829 | 2 | static void work_handler(struct k_work *work) | |
| 830 | { | ||
| 831 | bool again; | ||
| 832 | |||
| 833 | ARG_UNUSED(work); | ||
| 834 | |||
| 835 | 2 | (void)kfsw_limits_sweep(); | |
| 836 | |||
| 837 | 2 | k_mutex_lock(&state_lock, K_FOREVER); | |
| 838 | 2 | again = state.running; | |
| 839 | 2 | k_mutex_unlock(&state_lock); | |
| 840 | 2 | if (again) { | |
| 841 | ✗ | (void)k_work_reschedule_for_queue(&limits_queue, &limits_work, | |
| 842 | ✗ | K_MSEC(kfsw_limits_interval_ms)); | |
| 843 | } | ||
| 844 | 2 | } | |
| 845 | |||
| 846 | 12 | int kfsw_limits_set_interval_ms(uint32_t interval_ms) | |
| 847 | { | ||
| 848 | bool active; | ||
| 849 | |||
| 850 | 12 | if ((interval_ms != 0U) && ((interval_ms < KFSW_LIMITS_INTERVAL_MIN_MS) || | |
| 851 | (interval_ms > KFSW_LIMITS_INTERVAL_MAX_MS))) { | ||
| 852 | ✗ | return -ERANGE; | |
| 853 | } | ||
| 854 | |||
| 855 | 12 | kfsw_limits_interval_ms = interval_ms; | |
| 856 | 12 | k_mutex_lock(&state_lock, K_FOREVER); | |
| 857 | 12 | active = initialized; | |
| 858 | 12 | if (active) { | |
| 859 | 8 | state.running = (interval_ms != 0U); | |
| 860 | } | ||
| 861 | 12 | k_mutex_unlock(&state_lock); | |
| 862 | |||
| 863 | 12 | if (!active) { | |
| 864 | 4 | return 0; | |
| 865 | } | ||
| 866 | 8 | if (interval_ms != 0U) { | |
| 867 | 2 | (void)k_work_reschedule_for_queue(&limits_queue, &limits_work, K_MSEC(interval_ms)); | |
| 868 | } else { | ||
| 869 | 6 | (void)k_work_cancel_delayable(&limits_work); | |
| 870 | } | ||
| 871 | 8 | return 0; | |
| 872 | } | ||
| 873 | |||
| 874 | 4 | int kfsw_limits_init(void) | |
| 875 | { | ||
| 876 | bool first; | ||
| 877 | int result; | ||
| 878 | |||
| 879 | 4 | k_mutex_lock(&state_lock, K_FOREVER); | |
| 880 | 4 | first = !initialized; | |
| 881 | 4 | if (first) { | |
| 882 | 4 | k_work_queue_start(&limits_queue, limits_stack, K_THREAD_STACK_SIZEOF(limits_stack), | |
| 883 | CONFIG_KFSW_LIMITS_PRIORITY, NULL); | ||
| 884 | 4 | initialized = true; | |
| 885 | } | ||
| 886 | 4 | k_mutex_unlock(&state_lock); | |
| 887 | 4 | if (!first) { | |
| 888 | ✗ | return 0; | |
| 889 | } | ||
| 890 | |||
| 891 | /* Same reason as the procedures directory: a rule file has to be able to | ||
| 892 | * arrive before anybody can load one. | ||
| 893 | */ | ||
| 894 | 4 | if (kfsw_storage_is_ready()) { | |
| 895 | char directory[64]; | ||
| 896 | |||
| 897 | 4 | (void)snprintf(directory, sizeof(directory), "%s/%s", KFSW_FTP_STORAGE_ROOT, | |
| 898 | KFSW_LIMITS_FTP_PATH); | ||
| 899 | 4 | result = fs_mkdir(KFSW_FTP_STORAGE_ROOT); | |
| 900 | 4 | if ((result == 0) || (result == -EEXIST)) { | |
| 901 | 4 | result = fs_mkdir(directory); | |
| 902 | } | ||
| 903 | 4 | if ((result != 0) && (result != -EEXIST)) { | |
| 904 | ✗ | kfsw_log_warning("Limits: %s is missing (%d); no rule file can be uploaded", | |
| 905 | directory, result); | ||
| 906 | } | ||
| 907 | } | ||
| 908 | |||
| 909 | /* A restored interval that is out of range gives way to the compiled one. */ | ||
| 910 | 4 | if (kfsw_limits_set_interval_ms(kfsw_limits_interval_ms) != 0) { | |
| 911 | ✗ | (void)kfsw_limits_set_interval_ms(CONFIG_KFSW_LIMITS_INTERVAL_MS); | |
| 912 | } | ||
| 913 | 4 | kfsw_log_info("Limits: ready, sweeping every %u ms", kfsw_limits_interval_ms); | |
| 914 | |||
| 915 | 4 | result = kfsw_limits_load(CONFIG_KFSW_LIMITS_FILE); | |
| 916 | 4 | if ((result == -ENOENT) || (result == -ENODEV)) { | |
| 917 | 4 | kfsw_log_info("Limits: no rule file %s, no rules in force", | |
| 918 | CONFIG_KFSW_LIMITS_FILE); | ||
| 919 | 4 | return 0; | |
| 920 | } | ||
| 921 | ✗ | return result; | |
| 922 | } | ||
| 923 | |||
| 924 | 252 | void kfsw_limits_get_status(struct kfsw_limits_status *status) | |
| 925 | { | ||
| 926 | 252 | if (status == NULL) { | |
| 927 | ✗ | return; | |
| 928 | } | ||
| 929 | 252 | k_mutex_lock(&state_lock, K_FOREVER); | |
| 930 | 252 | *status = state; | |
| 931 | 252 | k_mutex_unlock(&state_lock); | |
| 932 | } | ||
| 933 | |||
| 934 | 80 | void kfsw_limits_visit_rules(kfsw_limits_visitor_t visitor, void *context) | |
| 935 | { | ||
| 936 | 80 | if (visitor == NULL) { | |
| 937 | ✗ | return; | |
| 938 | } | ||
| 939 | 80 | k_mutex_lock(&rules_lock, K_FOREVER); | |
| 940 | 80 | for (size_t index = 0U; index < rule_count; index++) { | |
| 941 | 80 | if (!visitor(&rules[index], context)) { | |
| 942 | 80 | break; | |
| 943 | } | ||
| 944 | } | ||
| 945 | 80 | k_mutex_unlock(&rules_lock); | |
| 946 | } | ||
| 947 |