1998-04-22 12:58:34 +00:00
|
|
|
/*
|
|
|
|
* BIRD Library
|
|
|
|
*
|
2004-05-31 21:02:09 +00:00
|
|
|
* (c) 1998--2004 Martin Mares <mj@ucw.cz>
|
1998-04-22 12:58:34 +00:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _BIRD_BIRDLIB_H_
|
|
|
|
#define _BIRD_BIRDLIB_H_
|
|
|
|
|
2023-01-20 13:07:38 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2022-04-14 14:51:18 +00:00
|
|
|
#include "sysdep/config.h"
|
2016-04-12 09:14:54 +00:00
|
|
|
#include "lib/alloca.h"
|
2023-08-15 10:31:28 +00:00
|
|
|
#include "lib/macro.h"
|
2009-02-26 13:23:54 +00:00
|
|
|
|
1998-04-22 12:58:34 +00:00
|
|
|
/* Ugly structure offset handling macros */
|
|
|
|
|
2022-09-07 11:54:20 +00:00
|
|
|
#define SAME_TYPE(a, b) ({ int _ = ((a) != (b)); !_; })
|
2023-01-20 13:07:38 +00:00
|
|
|
#define TYPE_CAST(from, to, what) ( SAME_TYPE(((from) NULL), (what)), ((to) (what)))
|
|
|
|
|
|
|
|
#ifdef offsetof
|
|
|
|
#define OFFSETOF offsetof
|
|
|
|
#else
|
2009-07-23 20:21:17 +00:00
|
|
|
#define OFFSETOF(s, i) ((size_t) &((s *)0)->i)
|
2023-01-20 13:07:38 +00:00
|
|
|
#endif
|
|
|
|
|
2023-11-24 11:11:21 +00:00
|
|
|
#define SKIP_BACK(s, i, p) ({ \
|
|
|
|
typeof(p) _orig = p; \
|
|
|
|
s *_ptr = ((s *)((char *)_orig - OFFSETOF(s, i))); \
|
|
|
|
SAME_TYPE(&_ptr->i, _orig); \
|
|
|
|
_ptr; })
|
2004-06-01 10:28:25 +00:00
|
|
|
#define BIRD_ALIGN(s, a) (((s)+a-1)&~(a-1))
|
2022-04-14 14:51:18 +00:00
|
|
|
#define CPU_STRUCT_ALIGN (MAX_(_Alignof(void*), _Alignof(u64)))
|
2022-04-10 12:11:46 +00:00
|
|
|
#define BIRD_CPU_ALIGN(s) BIRD_ALIGN((s), CPU_STRUCT_ALIGN)
|
1998-05-03 16:43:39 +00:00
|
|
|
|
2022-04-14 14:51:18 +00:00
|
|
|
/* Structure item alignment macros */
|
|
|
|
|
|
|
|
#define PADDING_NAME(id) _padding_##id
|
|
|
|
#define PADDING_(id, sz) u8 PADDING_NAME(id)[sz]
|
|
|
|
|
|
|
|
#if CPU_POINTER_ALIGNMENT == 4
|
|
|
|
#define PADDING(id, n32, n64) PADDING_(id, n32)
|
|
|
|
#elif CPU_POINTER_ALIGNMENT == 8
|
|
|
|
#define PADDING(id, n32, n64) PADDING_(id, n64)
|
|
|
|
#else
|
|
|
|
#error "Strange CPU pointer alignment: " CPU_POINTER_ALIGNMENT
|
|
|
|
#endif
|
|
|
|
|
1999-02-05 21:29:19 +00:00
|
|
|
/* Utility macros */
|
|
|
|
|
2013-11-22 20:59:43 +00:00
|
|
|
#define MIN_(a,b) (((a)<(b))?(a):(b))
|
|
|
|
#define MAX_(a,b) (((a)>(b))?(a):(b))
|
2013-10-05 18:12:28 +00:00
|
|
|
|
|
|
|
#ifndef PARSER
|
2013-11-19 21:33:48 +00:00
|
|
|
#undef MIN
|
|
|
|
#undef MAX
|
2013-11-22 20:59:43 +00:00
|
|
|
#define MIN(a,b) MIN_(a,b)
|
|
|
|
#define MAX(a,b) MAX_(a,b)
|
2011-03-13 10:33:50 +00:00
|
|
|
#endif
|
|
|
|
|
2020-04-05 01:24:46 +00:00
|
|
|
#define ROUND_DOWN_POW2(a,b) ((a) & ~((b)-1))
|
|
|
|
#define ROUND_UP_POW2(a,b) (((a)+((b)-1)) & ~((b)-1))
|
|
|
|
|
2015-11-24 12:47:28 +00:00
|
|
|
#define U64(c) UINT64_C(c)
|
2002-11-13 08:47:19 +00:00
|
|
|
#define ABS(a) ((a)>=0 ? (a) : -(a))
|
2015-06-08 00:20:43 +00:00
|
|
|
#define DELTA(a,b) (((a)>=(b))?(a)-(b):(b)-(a))
|
2000-04-12 13:21:23 +00:00
|
|
|
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
|
2016-12-07 14:36:15 +00:00
|
|
|
#define BYTES(n) ((((uint) (n)) + 7) / 8)
|
2016-01-26 10:48:58 +00:00
|
|
|
#define CALL(fn, args...) ({ if (fn) fn(args); })
|
2019-08-20 17:12:59 +00:00
|
|
|
#define ADVANCE(w, r, l) ({ r -= (l); w += (l); })
|
1998-07-09 19:36:05 +00:00
|
|
|
|
2015-12-16 09:25:12 +00:00
|
|
|
static inline int uint_cmp(uint i1, uint i2)
|
|
|
|
{ return (int)(i1 > i2) - (int)(i1 < i2); }
|
|
|
|
|
|
|
|
static inline int u64_cmp(u64 i1, u64 i2)
|
|
|
|
{ return (int)(i1 > i2) - (int)(i1 < i2); }
|
|
|
|
|
2015-02-24 15:19:14 +00:00
|
|
|
|
|
|
|
/* Bitfield macros */
|
|
|
|
|
|
|
|
/* b is u32 array (or ptr), l is size of it in bits (multiple of 32), p is 0..(l-1) */
|
2014-06-26 09:58:57 +00:00
|
|
|
#define BIT32_VAL(p) (((u32) 1) << ((p) % 32))
|
|
|
|
#define BIT32_TEST(b,p) ((b)[(p)/32] & BIT32_VAL(p))
|
|
|
|
#define BIT32_SET(b,p) ((b)[(p)/32] |= BIT32_VAL(p))
|
|
|
|
#define BIT32_CLR(b,p) ((b)[(p)/32] &= ~BIT32_VAL(p))
|
|
|
|
#define BIT32_ZERO(b,l) memset((b), 0, (l)/8)
|
|
|
|
|
2019-01-24 21:34:33 +00:00
|
|
|
/* The same, but counting bits from MSB */
|
|
|
|
#define BIT32R_VAL(p) ((((u32) 1) << 31) >> ((p) % 32))
|
|
|
|
#define BIT32R_TEST(b,p) ((b)[(p)/32] & BIT32R_VAL(p))
|
|
|
|
#define BIT32R_SET(b,p) ((b)[(p)/32] |= BIT32R_VAL(p))
|
|
|
|
#define BIT32R_CLR(b,p) ((b)[(p)/32] &= ~BIT32R_VAL(p))
|
|
|
|
#define BIT32R_ZERO(b,l) memset((b), 0, (l)/8)
|
|
|
|
|
2023-08-15 10:31:28 +00:00
|
|
|
/* Short Bitmask Constructor */
|
|
|
|
#define BIT32_ALL_HELPER(x) (1 << (x)) |
|
|
|
|
#define BIT32_ALL(...) (MACRO_FOREACH(BIT32_ALL_HELPER, __VA_ARGS__) 0)
|
|
|
|
|
1999-03-29 19:13:36 +00:00
|
|
|
#ifndef NULL
|
|
|
|
#define NULL ((void *) 0)
|
|
|
|
#endif
|
|
|
|
|
2004-06-05 09:01:12 +00:00
|
|
|
/* Macros for gcc attributes */
|
1998-05-03 16:43:39 +00:00
|
|
|
|
|
|
|
#define NORET __attribute__((noreturn))
|
2021-06-19 18:50:18 +00:00
|
|
|
#define USE_RESULT __atribute__((warn_unused_result))
|
2004-05-31 21:02:09 +00:00
|
|
|
#define UNUSED __attribute__((unused))
|
2016-04-28 16:01:40 +00:00
|
|
|
#define PACKED __attribute__((packed))
|
2020-04-27 20:33:10 +00:00
|
|
|
#define NONNULL(...) __attribute__((nonnull((__VA_ARGS__))))
|
2023-10-26 21:09:20 +00:00
|
|
|
#define CLEANUP(fun) __attribute__((cleanup(fun)))
|
1998-04-22 12:58:34 +00:00
|
|
|
|
2021-06-06 15:23:45 +00:00
|
|
|
#define STATIC_ASSERT(EXP) _Static_assert(EXP, #EXP)
|
|
|
|
#define STATIC_ASSERT_MSG(EXP,MSG) _Static_assert(EXP, MSG)
|
2021-04-10 15:33:28 +00:00
|
|
|
|
2013-10-05 18:12:28 +00:00
|
|
|
/* Microsecond time */
|
|
|
|
|
|
|
|
typedef s64 btime;
|
2017-11-28 16:06:10 +00:00
|
|
|
//typedef s64 bird_clock_t;
|
2013-10-05 18:12:28 +00:00
|
|
|
|
2017-06-20 13:55:39 +00:00
|
|
|
#define S_ * (btime) 1000000
|
|
|
|
#define MS_ * (btime) 1000
|
|
|
|
#define US_ * (btime) 1
|
2013-10-05 18:12:28 +00:00
|
|
|
#define TO_S /1000000
|
|
|
|
#define TO_MS /1000
|
|
|
|
#define TO_US /1
|
2023-04-06 18:18:04 +00:00
|
|
|
#define TO_NS * (btime) 1000
|
2013-10-05 18:12:28 +00:00
|
|
|
|
|
|
|
#ifndef PARSER
|
2013-11-22 20:59:43 +00:00
|
|
|
#define S S_
|
|
|
|
#define MS MS_
|
|
|
|
#define US US_
|
2017-06-20 13:55:39 +00:00
|
|
|
#define NS /1000
|
2013-10-05 18:12:28 +00:00
|
|
|
#endif
|
|
|
|
|
2017-11-28 16:06:10 +00:00
|
|
|
#define TIME_INFINITY ((s64) 0x7fffffffffffffff)
|
|
|
|
|
2013-10-05 18:12:28 +00:00
|
|
|
|
2014-10-02 09:41:34 +00:00
|
|
|
/* Rate limiting */
|
|
|
|
|
2024-05-07 10:09:52 +00:00
|
|
|
struct tbf_config {
|
|
|
|
u16 burst; /* Max number of tokens */
|
|
|
|
u16 rate; /* Rate of replenishment (tokens / sec) */
|
|
|
|
};
|
|
|
|
|
2014-10-02 09:41:34 +00:00
|
|
|
struct tbf {
|
2017-11-28 16:06:10 +00:00
|
|
|
btime timestamp; /* Last update */
|
|
|
|
u64 count; /* Available micro-tokens */
|
|
|
|
u32 drop; /* Number of failed request since last successful */
|
2024-05-07 10:09:52 +00:00
|
|
|
struct tbf_config cf; /* Configuration */
|
|
|
|
};
|
|
|
|
|
|
|
|
enum tbf_targets {
|
|
|
|
TBF_INVALID = 0,
|
2024-05-09 11:37:25 +00:00
|
|
|
TBF_PKT,
|
|
|
|
TBF_LSA,
|
|
|
|
TBF_RTE,
|
2024-05-07 10:09:52 +00:00
|
|
|
TBF_ALL
|
|
|
|
};
|
|
|
|
|
|
|
|
struct logging_rate_targets {
|
|
|
|
enum tbf_targets target;
|
|
|
|
struct logging_rate_targets *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct cmd_logging_rate_info {
|
2024-05-09 11:37:25 +00:00
|
|
|
int all_protos;
|
|
|
|
struct tbf_config *tbfc;
|
|
|
|
struct logging_rate_targets *targets;
|
2024-05-07 10:09:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct table_spec {
|
|
|
|
const void *ptr;
|
|
|
|
int patt;
|
2014-10-02 09:41:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Default TBF values for rate limiting log messages */
|
2024-05-07 10:09:52 +00:00
|
|
|
#define TBF_DEFAULT_LOG_LIMITS { .cf.rate = 1, .cf.burst = 5 }
|
2014-10-02 09:41:34 +00:00
|
|
|
|
2017-11-28 16:06:10 +00:00
|
|
|
int tbf_limit(struct tbf *f);
|
2014-10-02 09:41:34 +00:00
|
|
|
|
|
|
|
|
1998-04-23 14:01:15 +00:00
|
|
|
/* Logging and dying */
|
|
|
|
|
2013-10-05 18:12:28 +00:00
|
|
|
typedef struct buffer {
|
|
|
|
byte *start;
|
|
|
|
byte *pos;
|
|
|
|
byte *end;
|
|
|
|
} buffer;
|
|
|
|
|
2023-08-16 13:05:36 +00:00
|
|
|
#define LOG_BUFFER_SIZE 2560
|
|
|
|
|
2024-01-28 20:42:39 +00:00
|
|
|
enum log_buffer_pos {
|
|
|
|
LBP_TIMESTAMP = 0,
|
2024-01-28 22:01:39 +00:00
|
|
|
LBP_UDP_HEADER,
|
2024-01-28 20:42:39 +00:00
|
|
|
LBP_THREAD_ID,
|
|
|
|
LBP_CLASS,
|
|
|
|
LBP_MSG,
|
|
|
|
LBP__MAX,
|
|
|
|
LBPP_TERMINAL,
|
|
|
|
};
|
|
|
|
|
2023-08-16 13:05:36 +00:00
|
|
|
typedef struct log_buffer {
|
|
|
|
struct buffer buf;
|
2024-01-28 20:42:39 +00:00
|
|
|
byte *pos[LBP__MAX+1];
|
2023-08-16 13:05:36 +00:00
|
|
|
int class;
|
|
|
|
char block[LOG_BUFFER_SIZE];
|
|
|
|
} log_buffer;
|
|
|
|
|
2013-10-05 18:12:28 +00:00
|
|
|
#define STACK_BUFFER_INIT(buf,size) \
|
|
|
|
do { \
|
|
|
|
buf.start = alloca(size); \
|
|
|
|
buf.pos = buf.start; \
|
|
|
|
buf.end = buf.start + size; \
|
|
|
|
} while(0)
|
|
|
|
|
2004-06-05 09:05:12 +00:00
|
|
|
#define log log_msg
|
2023-08-16 13:05:36 +00:00
|
|
|
void log_prepare(log_buffer *buf, int class);
|
|
|
|
void log_commit(log_buffer *buf);
|
2014-12-03 09:57:31 +00:00
|
|
|
void log_msg(const char *msg, ...);
|
|
|
|
void log_rl(struct tbf *rl, const char *msg, ...);
|
|
|
|
void die(const char *msg, ...) NORET;
|
|
|
|
void bug(const char *msg, ...) NORET;
|
1998-04-23 14:01:15 +00:00
|
|
|
|
|
|
|
#define L_DEBUG "\001" /* Debugging messages */
|
1998-12-20 14:24:35 +00:00
|
|
|
#define L_TRACE "\002" /* Protocol tracing */
|
|
|
|
#define L_INFO "\003" /* Informational messages */
|
|
|
|
#define L_REMOTE "\004" /* Remote protocol errors */
|
2001-02-20 09:49:19 +00:00
|
|
|
#define L_WARN "\005" /* Local warnings */
|
|
|
|
#define L_ERR "\006" /* Local errors */
|
|
|
|
#define L_AUTH "\007" /* Authorization failed etc. */
|
|
|
|
#define L_FATAL "\010" /* Fatal errors */
|
|
|
|
#define L_BUG "\011" /* BIRD bugs */
|
1998-05-03 16:43:39 +00:00
|
|
|
|
2016-11-09 15:36:34 +00:00
|
|
|
void debug(const char *msg, ...); /* Printf to debug output */
|
2023-01-12 16:40:53 +00:00
|
|
|
void debug_safe(const char *msg); /* Printf to debug output, async-safe */
|
1998-04-23 14:01:15 +00:00
|
|
|
|
2023-05-05 07:39:13 +00:00
|
|
|
/* Internal thread ID, useful for logging */
|
|
|
|
extern _Atomic uint max_thread_id;
|
2023-05-03 17:01:19 +00:00
|
|
|
extern _Thread_local uint this_thread_id;
|
2023-05-05 07:39:13 +00:00
|
|
|
#define THIS_THREAD_ID (this_thread_id ?: (this_thread_id = atomic_fetch_add_explicit(&max_thread_id, 1, memory_order_acq_rel)))
|
|
|
|
|
2023-05-03 17:01:19 +00:00
|
|
|
|
1998-04-23 14:01:15 +00:00
|
|
|
/* Debugging */
|
|
|
|
|
2000-03-07 21:04:14 +00:00
|
|
|
#if defined(LOCAL_DEBUG) || defined(GLOBAL_DEBUG)
|
1998-05-26 21:36:17 +00:00
|
|
|
#define DBG(x, y...) debug(x, ##y)
|
2022-07-15 12:57:02 +00:00
|
|
|
#define DBGL(x, y...) debug(x "\n", ##y)
|
|
|
|
#elif defined(DEBUG_TO_LOG)
|
|
|
|
#define DBG(...) do { } while (0)
|
|
|
|
#define DBGL(...) log(L_DEBUG __VA_ARGS__)
|
1998-04-23 14:01:15 +00:00
|
|
|
#else
|
2022-07-15 12:57:02 +00:00
|
|
|
#define DBG(...) do { } while(0)
|
|
|
|
#define DBGL(...) do { } while (0)
|
1998-04-23 14:01:15 +00:00
|
|
|
#endif
|
|
|
|
|
2019-08-01 12:25:01 +00:00
|
|
|
#define ASSERT_DIE(x) do { if (!(x)) bug("Assertion '%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
|
|
|
|
|
2019-08-14 14:22:39 +00:00
|
|
|
#define EXPENSIVE_CHECK(x) /* intentionally left blank */
|
|
|
|
|
1998-12-20 14:24:35 +00:00
|
|
|
#ifdef DEBUGGING
|
2019-08-01 12:25:01 +00:00
|
|
|
#define ASSERT(x) ASSERT_DIE(x)
|
|
|
|
#define ASSUME(x) ASSERT_DIE(x)
|
2019-08-14 14:22:39 +00:00
|
|
|
#ifdef ENABLE_EXPENSIVE_CHECKS
|
|
|
|
#undef EXPENSIVE_CHECK
|
|
|
|
#define EXPENSIVE_CHECK(x) ASSERT_DIE(x)
|
|
|
|
#endif
|
1998-12-20 14:24:35 +00:00
|
|
|
#else
|
2018-01-10 15:17:37 +00:00
|
|
|
#define ASSERT(x) do { if (!(x)) log(L_BUG "Assertion '%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
|
2019-08-01 12:25:01 +00:00
|
|
|
#define ASSUME(x) /* intentionally left blank */
|
1998-12-20 14:24:35 +00:00
|
|
|
#endif
|
|
|
|
|
2019-08-14 14:22:39 +00:00
|
|
|
|
2019-01-23 16:08:27 +00:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
asm(
|
|
|
|
".pushsection \".debug_gdb_scripts\", \"MS\",@progbits,1\n"
|
|
|
|
".byte 1\n" /* Python */
|
|
|
|
".asciz \"bird-gdb.py\"\n"
|
|
|
|
".popsection\n"
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
|
2023-05-18 13:55:45 +00:00
|
|
|
|
1999-08-17 20:47:40 +00:00
|
|
|
/* Pseudorandom numbers */
|
|
|
|
|
|
|
|
u32 random_u32(void);
|
2021-04-01 17:20:13 +00:00
|
|
|
void random_init(void);
|
2021-04-07 23:15:17 +00:00
|
|
|
void random_bytes(void *buf, size_t size);
|
1999-08-17 20:47:40 +00:00
|
|
|
|
2023-05-18 13:55:45 +00:00
|
|
|
|
|
|
|
/* Hashing */
|
|
|
|
|
|
|
|
/* Constant parameter for non-parametrized hashes */
|
|
|
|
#define HASH_PARAM 2902958171u
|
|
|
|
|
|
|
|
/* Precomputed powers of HASH_PARAM */
|
|
|
|
#define HASH_PARAM1 ((u64) HASH_PARAM)
|
|
|
|
#define HASH_PARAM2 (HASH_PARAM1 * HASH_PARAM)
|
|
|
|
#define HASH_PARAM3 (HASH_PARAM2 * HASH_PARAM)
|
|
|
|
#define HASH_PARAM4 (HASH_PARAM3 * HASH_PARAM)
|
|
|
|
|
|
|
|
/* Reduce intermediate 64-bit value to final 32-bit value */
|
|
|
|
static inline u32 hash_value(u64 a)
|
|
|
|
{ return ((u32) a) ^ ((u32) (a >> 32)); }
|
|
|
|
|
|
|
|
static inline u64 u32_hash0(u32 v, u32 p, u64 acc)
|
|
|
|
{ return (acc + v) * p; }
|
|
|
|
|
|
|
|
static inline u64 u64_hash0(u64 v, u32 p, u64 acc)
|
|
|
|
{ return u32_hash0(v >> 32, p, u32_hash0(v, p, acc)); }
|
|
|
|
|
|
|
|
static inline u32 u64_hash(u64 v)
|
|
|
|
{ return hash_value(u64_hash0(v, HASH_PARAM, 0)); }
|
|
|
|
|
1998-04-22 12:58:34 +00:00
|
|
|
#endif
|