#ifndef _UTILS_H #define _UTILS_H #ifdef __cpluscplus extern "C" { #endif #define MIN(a, b) ((a) > (b) ? (b) : (a)) #define LOG_TAG_SCE "SCE" #define LOG_TAG_POLICY "POLICY" #define LOG_TAG_PKTIO "PACKET_IO" #define LOG_TAG_RAWPKT "DATA_PACKET" #define LOG_TAG_CTRLPKT "CTRL_PACKET" #define LOG_TAG_METRICS "G_METRICS" #define LOG_TAG_SF_METRICS "SF_METRICS" #define LOG_TAG_SF_STATUS "SF_STATUS" #define LOG_TAG_UTILS "UTILS" #define LOG_TAG_HEALTH_CHECK "HEALTH_CHECK" #define LOG_TAG_TIMESTAMP "TIMESTAMP" #define ATOMIC_INC(x) __atomic_fetch_add(x, 1, __ATOMIC_RELAXED) #define ATOMIC_DEC(x) __atomic_fetch_sub(x, 1, __ATOMIC_RELAXED) #define ATOMIC_READ(x) __atomic_load_n(x, __ATOMIC_RELAXED) #define ATOMIC_ZERO(x) __atomic_fetch_and(x, 0, __ATOMIC_RELAXED) #define ATOMIC_ADD(x, y) __atomic_fetch_add(x, y, __ATOMIC_RELAXED) #define ATOMIC_SET(x, y) __atomic_store_n(x, y, __ATOMIC_RELAXED) #define likely(expr) __builtin_expect((expr), 1) #define unlikely(expr) __builtin_expect((expr), 0) #include #include #include /****************************************************************************** * mutable_array ******************************************************************************/ struct mutable_array { uint64_t elems[128]; int num; int size; }; void mutable_array_init(struct mutable_array *array); void mutable_array_add_elem(struct mutable_array *array, uint64_t elem); void mutable_array_del_elem(struct mutable_array *array, uint64_t elem); int mutable_array_is_full(struct mutable_array *array); int mutable_array_count_elem(struct mutable_array *array); int mutable_array_exist_elem(struct mutable_array *array, uint64_t elem); int mutable_array_index_elem(struct mutable_array *array, int index); /****************************************************************************** * sids ******************************************************************************/ typedef uint16_t sid_t; #define MR_SID_LIST_MAXLEN 8 struct sids { int num; sid_t elems[MR_SID_LIST_MAXLEN]; }; void sids_write_once(struct sids *dst, struct sids *src); void sids_copy(struct sids *dst, struct sids *src); /****************************************************************************** * route_ctx ******************************************************************************/ struct route_ctx { char data[64]; int len; }; void route_ctx_write_once(struct route_ctx *dst, struct route_ctx *src); void route_ctx_copy(struct route_ctx *dst, struct route_ctx *src); /****************************************************************************** * throughput_metrics ******************************************************************************/ struct throughput_metrics { uint64_t n_pkts; uint64_t n_bytes; }; static inline void throughput_metrics_inc(struct throughput_metrics *iterm, uint64_t n_pkts, uint64_t n_bytes) { iterm->n_bytes += n_bytes; iterm->n_pkts += n_pkts; } /****************************************************************************** * device ******************************************************************************/ int get_ip_by_device_name(const char *dev_name, char *ip_str); int get_mac_by_device_name(const char *dev_name, char *mac_str); int str_to_mac(const char *mac_str, u_char mac[]); #ifdef __cpluscplus } #endif #endif