128 lines
4.2 KiB
C
128 lines
4.2 KiB
C
#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 "RAW_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_fetch_add(x, 0, __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)
|
|
|
|
/******************************************************************************
|
|
* fixed_num_array
|
|
******************************************************************************/
|
|
|
|
#include <stdint.h>
|
|
|
|
struct fixed_num_array
|
|
{
|
|
uint64_t elems[128];
|
|
int num;
|
|
int size;
|
|
};
|
|
|
|
void fixed_num_array_init(struct fixed_num_array *array);
|
|
void fixed_num_array_add_elem(struct fixed_num_array *array, uint64_t elem);
|
|
void fixed_num_array_del_elem(struct fixed_num_array *array, uint64_t elem);
|
|
int fixed_num_array_is_full(struct fixed_num_array *array);
|
|
int fixed_num_array_count_elem(struct fixed_num_array *array);
|
|
int fixed_num_array_exist_elem(struct fixed_num_array *array, uint64_t elem);
|
|
int fixed_num_array_index_elem(struct fixed_num_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;
|
|
};
|
|
|
|
void throughput_metrics_inc(struct throughput_metrics *iterm, uint64_t n_pkts, uint64_t n_bytes);
|
|
|
|
/******************************************************************************
|
|
* protocol
|
|
******************************************************************************/
|
|
|
|
struct udp_hdr
|
|
{
|
|
uint16_t uh_sport; /* source port */
|
|
uint16_t uh_dport; /* destination port */
|
|
uint16_t uh_ulen; /* udp length */
|
|
uint16_t uh_sum; /* udp checksum */
|
|
} __attribute__((__packed__));
|
|
|
|
void build_udp_header(const char *l3_hdr, int l3_hdr_len, struct udp_hdr *udp_hdr, uint16_t udp_sport, uint16_t udp_dport, int payload_len);
|
|
void build_ip_header(struct ip *ip_hdr, uint8_t next_protocol, const char *src_addr, const char *dst_addr, uint16_t payload_len);
|
|
void build_ether_header(struct ethhdr *eth_hdr, uint16_t next_protocol, const char *src_mac, const char *dst_mac);
|
|
|
|
/******************************************************************************
|
|
* device
|
|
******************************************************************************/
|
|
|
|
int get_ip_by_device_name(const char *dev_name, char *ip_buff);
|
|
int get_mac_by_device_name(const char *dev_name, char *mac_buff);
|
|
int str_to_mac(const char *str, char *mac_buff);
|
|
|
|
#define CHECKSUM_CARRY(x) (x = (x >> 16) + (x & 0xffff), (~(x + (x >> 16)) & 0xffff))
|
|
|
|
int checksum(uint16_t *addr, int len);
|
|
|
|
#ifdef __cpluscplus
|
|
}
|
|
#endif
|
|
|
|
#endif
|