1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
3 */
4
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <stdarg.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <regex.h>
12 #include <fnmatch.h>
13
14 #include <rte_eal.h>
15 #include <rte_log.h>
16 #include <rte_per_lcore.h>
17
18 #include "eal_private.h"
19
20 struct rte_log_dynamic_type {
21 const char *name;
22 uint32_t loglevel;
23 };
24
25 /** The rte_log structure. */
26 static struct rte_logs {
27 uint32_t type; /**< Bitfield with enabled logs. */
28 uint32_t level; /**< Log level. */
29 FILE *file; /**< Output file set by rte_openlog_stream, or NULL. */
30 size_t dynamic_types_len;
31 struct rte_log_dynamic_type *dynamic_types;
32 } rte_logs = {
33 .type = ~0,
34 .level = RTE_LOG_DEBUG,
35 };
36
37 struct rte_eal_opt_loglevel {
38 /** Next list entry */
39 TAILQ_ENTRY(rte_eal_opt_loglevel) next;
40 /** Compiled regular expression obtained from the option */
41 regex_t re_match;
42 /** Globbing pattern option */
43 char *pattern;
44 /** Log level value obtained from the option */
45 uint32_t level;
46 };
47
48 TAILQ_HEAD(rte_eal_opt_loglevel_list, rte_eal_opt_loglevel);
49
50 /** List of valid EAL log level options */
51 static struct rte_eal_opt_loglevel_list opt_loglevel_list =
52 TAILQ_HEAD_INITIALIZER(opt_loglevel_list);
53
54 /* Stream to use for logging if rte_logs.file is NULL */
55 static FILE *default_log_stream;
56
57 /**
58 * This global structure stores some information about the message
59 * that is currently being processed by one lcore
60 */
61 struct log_cur_msg {
62 uint32_t loglevel; /**< log level - see rte_log.h */
63 uint32_t logtype; /**< log type - see rte_log.h */
64 };
65
66 /* per core log */
67 static RTE_DEFINE_PER_LCORE(struct log_cur_msg, log_cur_msg);
68
69 /* default logs */
70
71 /* Change the stream that will be used by logging system */
72 int
rte_openlog_stream(FILE * f)73 rte_openlog_stream(FILE *f)
74 {
75 rte_logs.file = f;
76 return 0;
77 }
78
79 FILE *
rte_log_get_stream(void)80 rte_log_get_stream(void)
81 {
82 FILE *f = rte_logs.file;
83
84 if (f == NULL) {
85 /*
86 * Grab the current value of stderr here, rather than
87 * just initializing default_log_stream to stderr. This
88 * ensures that we will always use the current value
89 * of stderr, even if the application closes and
90 * reopens it.
91 */
92 return default_log_stream ? : stderr;
93 }
94 return f;
95 }
96
97 /* Set global log level */
98 void
rte_log_set_global_level(uint32_t level)99 rte_log_set_global_level(uint32_t level)
100 {
101 rte_logs.level = (uint32_t)level;
102 }
103
104 /* Get global log level */
105 uint32_t
rte_log_get_global_level(void)106 rte_log_get_global_level(void)
107 {
108 return rte_logs.level;
109 }
110
111 int
rte_log_get_level(uint32_t type)112 rte_log_get_level(uint32_t type)
113 {
114 if (type >= rte_logs.dynamic_types_len)
115 return -1;
116
117 return rte_logs.dynamic_types[type].loglevel;
118 }
119
120 bool
rte_log_can_log(uint32_t logtype,uint32_t level)121 rte_log_can_log(uint32_t logtype, uint32_t level)
122 {
123 int log_level;
124
125 if (level > rte_log_get_global_level())
126 return false;
127
128 log_level = rte_log_get_level(logtype);
129 if (log_level < 0)
130 return false;
131
132 if (level > (uint32_t)log_level)
133 return false;
134
135 return true;
136 }
137
138 int
rte_log_set_level(uint32_t type,uint32_t level)139 rte_log_set_level(uint32_t type, uint32_t level)
140 {
141 if (type >= rte_logs.dynamic_types_len)
142 return -1;
143 if (level > RTE_LOG_DEBUG)
144 return -1;
145
146 rte_logs.dynamic_types[type].loglevel = level;
147
148 return 0;
149 }
150
151 /* set log level by regular expression */
152 int
rte_log_set_level_regexp(const char * regex,uint32_t level)153 rte_log_set_level_regexp(const char *regex, uint32_t level)
154 {
155 regex_t r;
156 size_t i;
157
158 if (level > RTE_LOG_DEBUG)
159 return -1;
160
161 if (regcomp(&r, regex, 0) != 0)
162 return -1;
163
164 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
165 if (rte_logs.dynamic_types[i].name == NULL)
166 continue;
167 if (regexec(&r, rte_logs.dynamic_types[i].name, 0,
168 NULL, 0) == 0)
169 rte_logs.dynamic_types[i].loglevel = level;
170 }
171
172 regfree(&r);
173
174 return 0;
175 }
176
177 /*
178 * Save the type string and the loglevel for later dynamic
179 * logtypes which may register later.
180 */
rte_log_save_level(int priority,const char * regex,const char * pattern)181 static int rte_log_save_level(int priority,
182 const char *regex, const char *pattern)
183 {
184 struct rte_eal_opt_loglevel *opt_ll = NULL;
185
186 opt_ll = malloc(sizeof(*opt_ll));
187 if (opt_ll == NULL)
188 goto fail;
189
190 opt_ll->level = priority;
191
192 if (regex) {
193 opt_ll->pattern = NULL;
194 if (regcomp(&opt_ll->re_match, regex, 0) != 0)
195 goto fail;
196 } else if (pattern) {
197 opt_ll->pattern = strdup(pattern);
198 if (opt_ll->pattern == NULL)
199 goto fail;
200 } else
201 goto fail;
202
203 TAILQ_INSERT_HEAD(&opt_loglevel_list, opt_ll, next);
204 return 0;
205 fail:
206 free(opt_ll);
207 return -1;
208 }
209
rte_log_save_regexp(const char * regex,int tmp)210 int rte_log_save_regexp(const char *regex, int tmp)
211 {
212 return rte_log_save_level(tmp, regex, NULL);
213 }
214
215 /* set log level based on globbing pattern */
216 int
rte_log_set_level_pattern(const char * pattern,uint32_t level)217 rte_log_set_level_pattern(const char *pattern, uint32_t level)
218 {
219 size_t i;
220
221 if (level > RTE_LOG_DEBUG)
222 return -1;
223
224 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
225 if (rte_logs.dynamic_types[i].name == NULL)
226 continue;
227
228 if (fnmatch(pattern, rte_logs.dynamic_types[i].name, 0) == 0)
229 rte_logs.dynamic_types[i].loglevel = level;
230 }
231
232 return 0;
233 }
234
rte_log_save_pattern(const char * pattern,int priority)235 int rte_log_save_pattern(const char *pattern, int priority)
236 {
237 return rte_log_save_level(priority, NULL, pattern);
238 }
239
240 /* get the current loglevel for the message being processed */
rte_log_cur_msg_loglevel(void)241 int rte_log_cur_msg_loglevel(void)
242 {
243 return RTE_PER_LCORE(log_cur_msg).loglevel;
244 }
245
246 /* get the current logtype for the message being processed */
rte_log_cur_msg_logtype(void)247 int rte_log_cur_msg_logtype(void)
248 {
249 return RTE_PER_LCORE(log_cur_msg).logtype;
250 }
251
252 static int
rte_log_lookup(const char * name)253 rte_log_lookup(const char *name)
254 {
255 size_t i;
256
257 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
258 if (rte_logs.dynamic_types[i].name == NULL)
259 continue;
260 if (strcmp(name, rte_logs.dynamic_types[i].name) == 0)
261 return i;
262 }
263
264 return -1;
265 }
266
267 /* register an extended log type, assuming table is large enough, and id
268 * is not yet registered.
269 */
270 static int
__rte_log_register(const char * name,int id)271 __rte_log_register(const char *name, int id)
272 {
273 char *dup_name = strdup(name);
274
275 if (dup_name == NULL)
276 return -ENOMEM;
277
278 rte_logs.dynamic_types[id].name = dup_name;
279 rte_logs.dynamic_types[id].loglevel = RTE_LOG_INFO;
280
281 return id;
282 }
283
284 /* register an extended log type */
285 int
rte_log_register(const char * name)286 rte_log_register(const char *name)
287 {
288 struct rte_log_dynamic_type *new_dynamic_types;
289 int id, ret;
290
291 id = rte_log_lookup(name);
292 if (id >= 0)
293 return id;
294
295 new_dynamic_types = realloc(rte_logs.dynamic_types,
296 sizeof(struct rte_log_dynamic_type) *
297 (rte_logs.dynamic_types_len + 1));
298 if (new_dynamic_types == NULL)
299 return -ENOMEM;
300 rte_logs.dynamic_types = new_dynamic_types;
301
302 ret = __rte_log_register(name, rte_logs.dynamic_types_len);
303 if (ret < 0)
304 return ret;
305
306 rte_logs.dynamic_types_len++;
307
308 return ret;
309 }
310
311 /* Register an extended log type and try to pick its level from EAL options */
312 int
rte_log_register_type_and_pick_level(const char * name,uint32_t level_def)313 rte_log_register_type_and_pick_level(const char *name, uint32_t level_def)
314 {
315 struct rte_eal_opt_loglevel *opt_ll;
316 uint32_t level = level_def;
317 int type;
318
319 type = rte_log_register(name);
320 if (type < 0)
321 return type;
322
323 TAILQ_FOREACH(opt_ll, &opt_loglevel_list, next) {
324 if (opt_ll->level > RTE_LOG_DEBUG)
325 continue;
326
327 if (opt_ll->pattern) {
328 if (fnmatch(opt_ll->pattern, name, 0) == 0)
329 level = opt_ll->level;
330 } else {
331 if (regexec(&opt_ll->re_match, name, 0, NULL, 0) == 0)
332 level = opt_ll->level;
333 }
334 }
335
336 rte_logs.dynamic_types[type].loglevel = level;
337
338 return type;
339 }
340
341 struct logtype {
342 uint32_t log_id;
343 const char *logtype;
344 };
345
346 static const struct logtype logtype_strings[] = {
347 {RTE_LOGTYPE_EAL, "lib.eal"},
348 {RTE_LOGTYPE_MALLOC, "lib.malloc"},
349 {RTE_LOGTYPE_RING, "lib.ring"},
350 {RTE_LOGTYPE_MEMPOOL, "lib.mempool"},
351 {RTE_LOGTYPE_TIMER, "lib.timer"},
352 {RTE_LOGTYPE_PMD, "pmd"},
353 {RTE_LOGTYPE_HASH, "lib.hash"},
354 {RTE_LOGTYPE_LPM, "lib.lpm"},
355 {RTE_LOGTYPE_KNI, "lib.kni"},
356 {RTE_LOGTYPE_ACL, "lib.acl"},
357 {RTE_LOGTYPE_POWER, "lib.power"},
358 {RTE_LOGTYPE_METER, "lib.meter"},
359 {RTE_LOGTYPE_SCHED, "lib.sched"},
360 {RTE_LOGTYPE_PORT, "lib.port"},
361 {RTE_LOGTYPE_TABLE, "lib.table"},
362 {RTE_LOGTYPE_PIPELINE, "lib.pipeline"},
363 {RTE_LOGTYPE_MBUF, "lib.mbuf"},
364 {RTE_LOGTYPE_CRYPTODEV, "lib.cryptodev"},
365 {RTE_LOGTYPE_EFD, "lib.efd"},
366 {RTE_LOGTYPE_EVENTDEV, "lib.eventdev"},
367 {RTE_LOGTYPE_GSO, "lib.gso"},
368 {RTE_LOGTYPE_USER1, "user1"},
369 {RTE_LOGTYPE_USER2, "user2"},
370 {RTE_LOGTYPE_USER3, "user3"},
371 {RTE_LOGTYPE_USER4, "user4"},
372 {RTE_LOGTYPE_USER5, "user5"},
373 {RTE_LOGTYPE_USER6, "user6"},
374 {RTE_LOGTYPE_USER7, "user7"},
375 {RTE_LOGTYPE_USER8, "user8"}
376 };
377
378 /* Logging should be first initializer (before drivers and bus) */
RTE_INIT_PRIO(rte_log_init,LOG)379 RTE_INIT_PRIO(rte_log_init, LOG)
380 {
381 uint32_t i;
382
383 rte_log_set_global_level(RTE_LOG_DEBUG);
384
385 rte_logs.dynamic_types = calloc(RTE_LOGTYPE_FIRST_EXT_ID,
386 sizeof(struct rte_log_dynamic_type));
387 if (rte_logs.dynamic_types == NULL)
388 return;
389
390 /* register legacy log types */
391 for (i = 0; i < RTE_DIM(logtype_strings); i++)
392 __rte_log_register(logtype_strings[i].logtype,
393 logtype_strings[i].log_id);
394
395 rte_logs.dynamic_types_len = RTE_LOGTYPE_FIRST_EXT_ID;
396 }
397
398 static const char *
loglevel_to_string(uint32_t level)399 loglevel_to_string(uint32_t level)
400 {
401 switch (level) {
402 case 0: return "disabled";
403 case RTE_LOG_EMERG: return "emerg";
404 case RTE_LOG_ALERT: return "alert";
405 case RTE_LOG_CRIT: return "critical";
406 case RTE_LOG_ERR: return "error";
407 case RTE_LOG_WARNING: return "warning";
408 case RTE_LOG_NOTICE: return "notice";
409 case RTE_LOG_INFO: return "info";
410 case RTE_LOG_DEBUG: return "debug";
411 default: return "unknown";
412 }
413 }
414
415 /* dump global level and registered log types */
416 void
rte_log_dump(FILE * f)417 rte_log_dump(FILE *f)
418 {
419 size_t i;
420
421 fprintf(f, "global log level is %s\n",
422 loglevel_to_string(rte_log_get_global_level()));
423
424 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
425 if (rte_logs.dynamic_types[i].name == NULL)
426 continue;
427 fprintf(f, "id %zu: %s, level is %s\n",
428 i, rte_logs.dynamic_types[i].name,
429 loglevel_to_string(rte_logs.dynamic_types[i].loglevel));
430 }
431 }
432
433 /*
434 * Generates a log message The message will be sent in the stream
435 * defined by the previous call to rte_openlog_stream().
436 */
437 int
rte_vlog(uint32_t level,uint32_t logtype,const char * format,va_list ap)438 rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
439 {
440 FILE *f = rte_log_get_stream();
441 int ret;
442
443 if (logtype >= rte_logs.dynamic_types_len)
444 return -1;
445 if (!rte_log_can_log(logtype, level))
446 return 0;
447
448 /* save loglevel and logtype in a global per-lcore variable */
449 RTE_PER_LCORE(log_cur_msg).loglevel = level;
450 RTE_PER_LCORE(log_cur_msg).logtype = logtype;
451
452 ret = vfprintf(f, format, ap);
453 fflush(f);
454 return ret;
455 }
456
457 /*
458 * Generates a log message The message will be sent in the stream
459 * defined by the previous call to rte_openlog_stream().
460 * No need to check level here, done by rte_vlog().
461 */
462 int
rte_log(uint32_t level,uint32_t logtype,const char * format,...)463 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
464 {
465 va_list ap;
466 int ret;
467
468 va_start(ap, format);
469 ret = rte_vlog(level, logtype, format, ap);
470 va_end(ap);
471 return ret;
472 }
473
474 /*
475 * Called by environment-specific initialization functions.
476 */
477 void
eal_log_set_default(FILE * default_log)478 eal_log_set_default(FILE *default_log)
479 {
480 default_log_stream = default_log;
481
482 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
483 RTE_LOG(NOTICE, EAL,
484 "Debug dataplane logs available - lower performance\n");
485 #endif
486 }
487