Mod aaa-12 add trace

RCA:
SOL:
修改人:zhangtao
检视人:zhangtao
This commit is contained in:
zhangtaohz 2019-06-13 10:53:14 +08:00
parent 5c709e2b57
commit 6a96f2fe69
11 changed files with 1297 additions and 0 deletions

View File

@ -0,0 +1,11 @@
ifneq ($(KERNELRELEASE),)
obj-m += trace_api.o
else
include = -I../ -I/data/NetAccessControl/Platform/modules/netlink_api/app_u/lib
libs = -lpthread -L/data/NetAccessControl/Platform/modules/netlink_api/app_u/lib -lnetlinku
all:
gcc -fPIC -shared -g -o libtrace-api-linux.so trace_api.c $(include) $(libs)
gcc -g -o test_trace test_client_main.c $(include) -L./ -ltrace-api-linux
clean:
rm -rf *.o *.so test_trace
endif

View File

@ -0,0 +1,28 @@
#ifndef _COLLECTION_H
#define _COLLECTION_H
#include <stdint.h>
#include "trace_api.h"
#include "list.h"
#define COLLECT_HLIST_CLEAR(pos, n, head, count, member, free_cb) \
{ \
for (uint16_t i = 0; i < count; i++) { \
hlist_for_each_entry_safe(pos, n, &head[i], node) { \
hlist_del(&pos->member); \
free_cb(pos); \
} \
} \
}
static inline trace_ret_t collect_hlist_init(struct hlist_head *h, const uint16_t count)
{
for (uint16_t i = 0; i < count; i++) {
INIT_HLIST_HEAD(&h[i]);
}
return TRACE_SUCCESS;
}
#endif

641
Platform/trace/trace-api/list.h Executable file
View File

@ -0,0 +1,641 @@
#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H
#define container_of(ptr,type,member) ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
//#if defined(__KERNEL__) || defined(_LVM_H_INCLUDE)
//#include <linux/prefetch.h>
/*
* Simple doubly linked list implementation.
*
* Some of the internal functions ("__xxx") are useful when
* manipulating whole lists rather than single entries, as
* sometimes we already know the next/prev entries and we can
* generate better code by using them directly rather than
* using the generic single-entry routines.
*/
typedef struct list_head {
struct list_head *next, *prev;
} list_t;
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
#define INIT_LIST_HEAD(ptr) do { \
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static void inline prefetch(void *p){}
static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
/**
* list_add - add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
/**
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_del(struct list_head *prev, struct list_head *next)
{
next->prev = prev;
prev->next = next;
}
/**
* list_del - deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty on entry does not return true after this, the entry is in an undefined state.
*/
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = (void *) 0;
entry->prev = (void *) 0;
}
/**
* list_replace - replace old entry by new one
* @old : the element to be replaced
* @new : the new element to insert
*
* If @old was empty, it will be overwritten.
*/
static inline void list_replace(struct list_head *old,
struct list_head *new)
{
new->next = old->next;
new->next->prev = new;
new->prev = old->prev;
new->prev->next = new;
}
static inline void list_replace_init(struct list_head *old,
struct list_head *new)
{
list_replace(old, new);
INIT_LIST_HEAD(old);
}
/**
* list_del_init - deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static inline void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
/**
* list_move - delete from one list and add as another's head
* @list: the entry to move
* @head: the head that will precede our entry
*/
static inline void list_move(struct list_head *list, struct list_head *head)
{
__list_del(list->prev, list->next);
list_add(list, head);
}
/**
* list_move_tail - delete from one list and add as another's tail
* @list: the entry to move
* @head: the head that will follow our entry
*/
static inline void list_move_tail(struct list_head *list,
struct list_head *head)
{
__list_del(list->prev, list->next);
list_add_tail(list, head);
}
/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(const struct list_head *head)
{
return head->next == head;
}
static inline void __list_splice(struct list_head *list,
struct list_head *head)
{
struct list_head *first = list->next;
struct list_head *last = list->prev;
struct list_head *at = head->next;
first->prev = head;
head->next = first;
last->next = at;
at->prev = last;
}
/**
* list_splice - join two lists
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice(struct list_head *list, struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head);
}
/**
* list_splice_init - join two lists and reinitialise the emptied list.
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* The list at @list is reinitialised
*/
static inline void list_splice_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head);
INIT_LIST_HEAD(list);
}
}
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
/**
* list_first_entry - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note, that list is expected to be not empty.
*/
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
/**
* list_last_entry - get the last element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note, that list is expected to be not empty.
*/
#define list_last_entry(ptr, type, member) \
list_entry((ptr)->prev, type, member)
/**
* list_first_entry_or_null - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note that if the list is empty, it returns NULL.
*/
#define list_first_entry_or_null(ptr, type, member) \
(!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
/**
* list_next_entry - get the next element in list
* @pos: the type * to cursor
* @member: the name of the list_head within the struct.
*/
#define list_next_entry(pos, member) \
list_entry((pos)->member.next, typeof(*(pos)), member)
/**
* list_prev_entry - get the prev element in list
* @pos: the type * to cursor
* @member: the name of the list_head within the struct.
*/
#define list_prev_entry(pos, member) \
list_entry((pos)->member.prev, typeof(*(pos)), member)
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
/**
* list_for_each_prev - iterate over a list backwards
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; pos != (head); pos = pos->prev)
/**
* list_for_each_safe - iterate over a list safe against removal of list entry
* @pos: the &struct list_head to use as a loop cursor.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
/**
* list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
* @pos: the &struct list_head to use as a loop cursor.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_prev_safe(pos, n, head) \
for (pos = (head)->prev, n = pos->prev; \
pos != (head); \
pos = n, n = pos->prev)
/**
* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* list_for_each_entry_reverse - iterate backwards over list of given type.
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry_reverse(pos, head, member) \
for (pos = list_last_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_prev_entry(pos, member))
/**
* list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
* @pos: the type * to use as a start point
* @head: the head of the list
* @member: the name of the list_head within the struct.
*
* Prepares a pos entry for use as a start point in list_for_each_entry_continue().
*/
#define list_prepare_entry(pos, head, member) \
((pos) ? : list_entry(head, typeof(*pos), member))
/**
* list_for_each_entry_continue - continue iteration over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Continue to iterate over list of given type, continuing after
* the current position.
*/
#define list_for_each_entry_continue(pos, head, member) \
for (pos = list_next_entry(pos, member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* list_for_each_entry_continue_reverse - iterate backwards from the given point
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Start to iterate over list of given type backwards, continuing after
* the current position.
*/
#define list_for_each_entry_continue_reverse(pos, head, member) \
for (pos = list_prev_entry(pos, member); \
&pos->member != (head); \
pos = list_prev_entry(pos, member))
/**
* list_for_each_entry_from - iterate over list of given type from the current point
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate over list of given type, continuing from current position.
*/
#define list_for_each_entry_from(pos, head, member) \
for (; &pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member), \
n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))
/**
* list_for_each_entry_safe_continue - continue list iteration safe against removal
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate over list of given type, continuing after current point,
* safe against removal of list entry.
*/
#define list_for_each_entry_safe_continue(pos, n, head, member) \
for (pos = list_next_entry(pos, member), \
n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))
/**
* list_for_each_entry_safe_from - iterate over list from current point safe against removal
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate over list of given type from current point, safe against
* removal of list entry.
*/
#define list_for_each_entry_safe_from(pos, n, head, member) \
for (n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))
/**
* list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate backwards over list of given type, safe against removal
* of list entry.
*/
#define list_for_each_entry_safe_reverse(pos, n, head, member) \
for (pos = list_last_entry(head, typeof(*pos), member), \
n = list_prev_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_prev_entry(n, member))
/**
* list_safe_reset_next - reset a stale list_for_each_entry_safe loop
* @pos: the loop cursor used in the list_for_each_entry_safe loop
* @n: temporary storage used in list_for_each_entry_safe
* @member: the name of the list_head within the struct.
*
* list_safe_reset_next is not safe to use in general if the list may be
* modified concurrently (eg. the lock is dropped in the loop body). An
* exception to this is if the cursor element (pos) is pinned in the list,
* and list_safe_reset_next is called after re-taking the lock and before
* completing the current iteration of the loop body.
*/
#define list_safe_reset_next(pos, n, member) \
n = list_next_entry(pos, member)
/*
* Double linked lists with a single pointer list head.
* Mostly useful for hash tables where the two pointer list head is
* too wasteful.
* You lose the ability to access the tail in O(1).
*/
#define LIST_POISON1 NULL
#define LIST_POISON2 NULL
#define READ_ONCE(x) (x)
#define WRITE_ONCE(x, val) x=(val)
struct hlist_head {
struct hlist_node *first;
};
struct hlist_node {
struct hlist_node *next, **pprev;
};
#define HLIST_HEAD_INIT { .first = NULL }
#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
static inline void INIT_HLIST_NODE(struct hlist_node *h)
{
h->next = NULL;
h->pprev = NULL;
}
static inline int hlist_unhashed(const struct hlist_node *h)
{
return !h->pprev;
}
static inline int hlist_empty(const struct hlist_head *h)
{
return !READ_ONCE(h->first);
}
static inline void __hlist_del(struct hlist_node *n)
{
struct hlist_node *next = n->next;
struct hlist_node **pprev = n->pprev;
WRITE_ONCE(*pprev, next);
if (next)
next->pprev = pprev;
}
static inline void hlist_del(struct hlist_node *n)
{
__hlist_del(n);
n->next = LIST_POISON1;
n->pprev = LIST_POISON2;
}
static inline void hlist_del_init(struct hlist_node *n)
{
if (!hlist_unhashed(n)) {
__hlist_del(n);
INIT_HLIST_NODE(n);
}
}
static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
{
struct hlist_node *first = h->first;
n->next = first;
if (first)
first->pprev = &n->next;
WRITE_ONCE(h->first, n);
n->pprev = &h->first;
}
/* next must be != NULL */
static inline void hlist_add_before(struct hlist_node *n,
struct hlist_node *next)
{
n->pprev = next->pprev;
n->next = next;
next->pprev = &n->next;
WRITE_ONCE(*(n->pprev), n);
}
static inline void hlist_add_behind(struct hlist_node *n,
struct hlist_node *prev)
{
n->next = prev->next;
WRITE_ONCE(prev->next, n);
n->pprev = &prev->next;
if (n->next)
n->next->pprev = &n->next;
}
/* after that we'll appear to be on some hlist and hlist_del will work */
static inline void hlist_add_fake(struct hlist_node *n)
{
n->pprev = &n->next;
}
static inline int hlist_fake(struct hlist_node *h)
{
return h->pprev == &h->next;
}
/*
* Check whether the node is the only node of the head without
* accessing head:
*/
static inline int
hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
{
return !n->next && n->pprev == &h->first;
}
/*
* Move a list from one list head to another. Fixup the pprev
* reference of the first entry if it exists.
*/
static inline void hlist_move_list(struct hlist_head *old,
struct hlist_head *new)
{
new->first = old->first;
if (new->first)
new->first->pprev = &new->first;
old->first = NULL;
}
#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
#define hlist_for_each(pos, head) \
for (pos = (head)->first; pos ; pos = pos->next)
#define hlist_for_each_safe(pos, n, head) \
for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
pos = n)
#define hlist_entry_safe(ptr, type, member) \
({ typeof(ptr) ____ptr = (ptr); \
____ptr ? hlist_entry(____ptr, type, member) : NULL; \
})
/**
* hlist_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry(pos, head, member) \
for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
pos; \
pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
/**
* hlist_for_each_entry_continue - iterate over a hlist continuing after current point
* @pos: the type * to use as a loop cursor.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_continue(pos, member) \
for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
pos; \
pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
/**
* hlist_for_each_entry_from - iterate over a hlist continuing from current point
* @pos: the type * to use as a loop cursor.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_from(pos, member) \
for (; pos; \
pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
/**
* hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop cursor.
* @n: another &struct hlist_node to use as temporary storage
* @head: the head for your list.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_safe(pos, n, head, member) \
for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
pos && ({ n = pos->member.next; 1; }); \
pos = hlist_entry_safe(n, typeof(*pos), member))
#endif

13
Platform/trace/trace-api/log.h Executable file
View File

@ -0,0 +1,13 @@
#ifndef _LOG_H
#define _LOG_H
#include "syslog.h"
#define SYSLOG_INIT(program_name) openlog(program_name, LOG_CONS | LOG_PERROR |LOG_PID, LOG_USER)
#define SYSLOG_EXIT() closelog();
#define SYSLOG_DEBUG(fmt, ...) syslog(LOG_DEBUG, fmt, ##__VA_ARGS__)
#define SYSLOG_INFO(fmt, ...) syslog(LOG_INFO, fmt, ##__VA_ARGS__)
#define SYSLOG_WARN(fmt, ...) syslog(LOG_WARNING, fmt, ##__VA_ARGS__)
#define SYSLOG_ERR(fmt, ...) syslog(LOG_ERR, fmt, ##__VA_ARGS__)
#endif

View File

@ -0,0 +1,61 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include "log.h"
#include "trace_api.h"
void cb(trace_ret_t ret, void *arg)
{
printf("reply valaue:%u\n", ret);
}
int main(int argc, char **argv)
{
printf("1111\n");
SYSLOG_INIT("test-client");
policy_client_init();
printf("argc num:%d\n", argc);
int count = 1;
if (argc >= 2) {
count = atoi(argv[1]);
}
int async = 1;
if (argc >= 3) {
async = atoi(argv[2]);
}
for (int i = 0; i < count; i++) {
trace_policy_t trace1 = {0};
//trace1.src.addr.ip4.s_addr = i;
trace1.dst.addr.ip4.s_addr = i;
//memset(&trace1, 0, sizeof(trace1));
trace1.src.family = 2;
trace1.dst.family = 2;
if (async == 1) {
printf("async exec\n");
policy_async_exec(&trace1, cb, NULL);
} else {
printf("sync exec\n");
policy_sync_exec(&trace1);
}
printf("send success\n");
}
//policy_client_exit();
sleep(10);
return 0;
}

Binary file not shown.

View File

@ -0,0 +1,432 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <pthread.h>
#include <time.h>
#include <linux/netlink.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/sysinfo.h>
#include "libnetlinku.h"
//#include "policy_client.h"
//#include "policy_common.h"
#include "log.h"
#include "collection.h"
#include "trace_msg.h"
#define HASH_SESS_TAB_BITS 8
#define HASH_SESS_TAB_SIZE (1 << HASH_SESS_TAB_BITS)
#define EXEC_SYNC_WAIT_TIMEOUT 5
#define MAX_QUEUE_COUNT 256
#define MAX_QUEUE_TIMEOUT (EXEC_SYNC_WAIT_TIMEOUT + 2)
#define MAX_TRACE_BUF_SZ (sizeof(struct nlmsghdr) + sizeof(trace_req_t))
#define SESS_HASH_INDEX(seq) (seq >> HASH_SESS_TAB_BITS)
typedef struct _cb_arg {
struct hlist_node node;
uint32_t seq;
void *arg;
long t; // 加入链表的时间
void (*cb)(trace_ret_t ret, void *arg);
} cb_arg_t;
typedef struct _sync_arg {
trace_ret_t ret;
sem_t sem;
} sync_arg_t;
typedef struct _sess {
uint16_t count[HASH_SESS_TAB_SIZE];
struct hlist_head hess[HASH_SESS_TAB_SIZE];
struct hlist_node *last[HASH_SESS_TAB_SIZE];
pthread_mutex_t hsess_mutex[HASH_SESS_TAB_SIZE];
} sess_t;
static int g_pid;
static pthread_t g_client_thread;
static volatile int g_client_stop = 0;
static uint32_t g_sessionid = 0;
static volatile sess_t g_sess = {0};
static int g_channel_open = -1;
static trace_ret_t get_and_del_arg_from_hlist(const uint32_t seq, cb_arg_t **out)
{
int ret = TRACE_FAILURE;
uint32_t i = SESS_HASH_INDEX(seq);
cb_arg_t *pos, *cb_arg = NULL;
struct hlist_node *n;
ret = pthread_mutex_lock((pthread_mutex_t *)&g_sess.hsess_mutex[i]);
if (ret != 0) {
SYSLOG_ERR("Thread locked session:[%u] is failure:%d", i, ret);
goto END;
}
hlist_for_each_entry_safe(pos, n, &g_sess.hess[i], node) {
if (pos->seq != seq) {
continue;
}
hlist_del(&pos->node);
cb_arg = pos;
g_sess.count[i]--;
SYSLOG_DEBUG("Find cb by seq id:%u", seq);
break;
}
ret = pthread_mutex_unlock((pthread_mutex_t *)&g_sess.hsess_mutex[i]);
if (ret != 0) {
SYSLOG_ERR("Thread unlocked session:[%u] is failure:%d", i, ret);
goto END;
}
*out = cb_arg;
ret = TRACE_SUCCESS;
END:
return ret;
}
static void *cb_thread(void *arg)
{
ssize_t size;
trace_reply_t msg;
return NULL;
while(1) {
if (g_client_stop) {
SYSLOG_INFO("Callback thread is stopping");
break;
}
// todo 需要考虑长期没收到消息hash怎么清除数据
// size = msgrcv(g_client_msgid, &msg, sizeof(msg.mtext), g_pid, MSG_NOERROR | IPC_NOWAIT);
if ((size == -1)/* || (size != sizeof(msg.mtext))*/) {
if ((errno != ENOMSG)) {
printf("msg queue receive is failure:%d\n", errno);
}
// usleep(SLEEP_THREAD_TIME); //防止CPU占用过高睡眠一会儿让出CPU todo
continue;
}
cb_arg_t *cb_arg = NULL;
if (get_and_del_arg_from_hlist(msg.hdr.seq, &cb_arg) == TRACE_FAILURE) {
SYSLOG_ERR("Get arg is failure");
break;
}
if (cb_arg == NULL) {
SYSLOG_INFO("The seq:[%u] is not found", msg.hdr.seq);
continue;
}
if (cb_arg->cb != NULL) {
SYSLOG_DEBUG("Execute callback of seq:[%u]", msg.hdr.seq);
cb_arg->cb(msg.result, cb_arg->arg);
} else {
SYSLOG_DEBUG("The callback of seq:[%u] is not set", msg.hdr.seq);
}
free(cb_arg);
}
SYSLOG_INFO("Callback thread is stopped");
return NULL;
}
static trace_ret_t cfg_channel_send(const uint32_t seq, const trace_policy_t *policy)
{
char buf[MAX_TRACE_BUF_SZ + 10];
struct nlmsghdr *hdr = (struct nlmsghdr *)buf;
hdr->nlmsg_len = NLMSG_HDRLEN;
hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
hdr->nlmsg_type = 0x12;
trace_req_t req;
req.hdr.ver = 1;
req.hdr.seq = seq;
memcpy(&req.policy, policy, sizeof(*policy));
commnl_addattr_l(hdr, sizeof(buf), 101, &req, sizeof(trace_req_t));
SYSLOG_DEBUG("Send msg len:%u, msg_flag:%u, msg_type:%u", hdr->nlmsg_len, hdr->nlmsg_flags, hdr->nlmsg_type);
/*发送组装好的netlink消息*/
if (commcfg_talk(hdr, NULL) < 0) {
SYSLOG_ERR("Message(seq:%u) which been sent is failure", seq);
return TRACE_FAILURE;
}
SYSLOG_INFO("Message(seq:%u) which been sent is success", seq);
return TRACE_SUCCESS;
}
static void cfg_channel_close()
{
if (g_channel_open >= 0) {
commcfgnl_close();
}
}
trace_ret_t policy_client_init()
{
int i = 0;
/*
g_sev_msgid = msg_init(MSG_QUEUE_TRACE_SEV_KEY);
if (g_sev_msgid == -1) {
SYSLOG_ERR("Create client msg queue is failure:%d", errno);
goto FAIL;
}
*/
trace_ret_t pm_ret = collect_hlist_init((struct hlist_head *)g_sess.hess, sizeof(g_sess.hess) / sizeof(struct hlist_head));
if (pm_ret != TRACE_SUCCESS) {
SYSLOG_ERR("hlist init is failure:%d", pm_ret);
goto FAIL;
}
for (i = 0; i < sizeof(g_sess.hsess_mutex) / sizeof(pthread_mutex_t); i++) {
int ret = pthread_mutex_init((pthread_mutex_t *)&g_sess.hsess_mutex[i], NULL);
if (ret != 0) {
SYSLOG_ERR("Initial thread:[%d] is failure:%d", i, ret);
goto FAIL;
}
}
g_channel_open = commcfgnl_open();
if(g_channel_open < 0)
{
SYSLOG_ERR("pdelivnl_open fail:%d", g_channel_open);
goto FAIL;
}
int ret = pthread_create(&g_client_thread, NULL, cb_thread, NULL);
if (ret != 0) {
SYSLOG_ERR("Create the thread of callback is failure:%d", ret);
goto FAIL;
}
return TRACE_SUCCESS;
FAIL:
while (i > 0) {
i--;
pthread_mutex_destroy((pthread_mutex_t *)&g_sess.hsess_mutex[i]);
}
cfg_channel_close();
return TRACE_FAILURE;
}
trace_ret_t policy_client_exit()
{
g_client_stop = 1;
pthread_join(g_client_thread, NULL);
for (int i = 0; i < sizeof(g_sess.hsess_mutex) / sizeof(pthread_mutex_t); i++) {
pthread_mutex_destroy((pthread_mutex_t *)&g_sess.hsess_mutex[i]);
}
cb_arg_t *pos;
struct hlist_node *n;
COLLECT_HLIST_CLEAR(pos, n, g_sess.hess, sizeof(g_sess.hess) / sizeof(struct hlist_head), node, free);
cfg_channel_close();
return TRACE_SUCCESS;
}
static trace_ret_t __policy_async_exec(const trace_policy_t *in,
async_cb cb, void *arg, uint32_t *seq_out)
{
trace_ret_t ret = TRACE_FAILURE;
struct hlist_node *prev_last;
struct sysinfo info;
async_cb tmp_cb = NULL;
void *tmp_arg;
//policy_msg_t msg = {0};
uint32_t seq;
/*
msg.mtype = g_pid;
msg.mtext.hdr.version = 1;
msg.mtext.hdr.len = len;
msg.mtext.hdr.sessionid = __sync_add_and_fetch(&g_sessionid, 1);
printf("pid:%lu, sessionid:%u\n", msg.mtype, msg.mtext.hdr.sessionid);
memcpy(&msg.mtext.body.trace, in, len);
*/
seq = __sync_add_and_fetch(&g_sessionid, 1);
SYSLOG_DEBUG("The seq of the message is %u", seq);
uint32_t i = SESS_HASH_INDEX(seq);
if (sysinfo(&info) != 0) {
SYSLOG_ERR("Get current boot time is failure:%d\n", errno);
goto END;
}
int ret_thread = pthread_mutex_lock((pthread_mutex_t *)&g_sess.hsess_mutex[i]);
if (ret_thread != 0) {
SYSLOG_ERR("Async Interface:locked session:[%u] is failure:%d", i, ret);
goto END;
}
cb_arg_t *cb_arg = NULL;
prev_last = g_sess.last[i];
if (g_sess.count[i] >= MAX_QUEUE_COUNT) {
cb_arg_t *first = hlist_entry(g_sess.hess[i].first, cb_arg_t, node);
if ((info.uptime - first->t) > MAX_QUEUE_TIMEOUT) {
hlist_del(&first->node);
tmp_cb = cb_arg->cb;
tmp_arg = cb_arg->arg;
// 复用cb_arg
cb_arg = first;
} else {
pthread_mutex_unlock((pthread_mutex_t *)&g_sess.hsess_mutex[i]);
SYSLOG_WARN("Hash table:[%u] queue is full", i);
goto END;
}
}
if (cb_arg == NULL) {
cb_arg = (cb_arg_t *)malloc(sizeof(*cb_arg));
if (cb_arg == NULL) {
pthread_mutex_unlock((pthread_mutex_t *)&g_sess.hsess_mutex[i]);
SYSLOG_ERR("Allocateing arg of callback is failure:%d\n", errno);
goto END;
}
}
cb_arg->seq = seq;
cb_arg->arg = arg;
cb_arg->cb = cb;
INIT_HLIST_NODE(&cb_arg->node);
cb_arg->t = info.uptime;
// 新节点加的链表后面
if (hlist_empty((struct hlist_head *)&g_sess.hess[i])) {
hlist_add_head(&cb_arg->node, (struct hlist_head *)&g_sess.hess[i]);
} else {
hlist_add_behind(&cb_arg->node, g_sess.last[i]);
}
g_sess.last[i] = &cb_arg->node;
g_sess.count[i]++;
ret_thread = pthread_mutex_unlock((pthread_mutex_t *)&g_sess.hsess_mutex[i]);
if (ret_thread != 0) {
SYSLOG_ERR("Async Interface:unlocked session:[%u] is failure:%d", i, ret);
goto END;
}
cfg_channel_send(seq, in);
/*
if (mq_send(g_mq_sev, &msg, sizeof(msg.mtext)) == -1) {
SYSLOG_ERR("mq send failed:%d\n", errno);
goto END;
}
*/
if (seq_out != NULL) {
*seq_out = seq;
}
ret = TRACE_PENDING;
END:
if (ret == TRACE_FAILURE) {
hlist_del(&cb_arg->node);
free(cb_arg);
g_sess.last[i] = prev_last;
g_sess.count[i]--;
}
if (tmp_cb != NULL) {
tmp_cb(TRACE_FAILURE, tmp_arg);
}
return ret;
}
trace_ret_t policy_async_exec(const trace_policy_t *in,
async_cb cb, void *arg)
{
return __policy_async_exec(in, cb, arg, NULL);
}
static void sync_exec_cb(trace_ret_t ret, void *arg)
{
sync_arg_t *a = (sync_arg_t *)arg;
SYSLOG_DEBUG("sync cb");
a->ret = ret;
if (sem_post(&a->sem) != 0) {
SYSLOG_ERR("Set semaphore is failure:%d", errno);
}
}
trace_ret_t policy_sync_exec(const trace_policy_t *in)
{
trace_ret_t ret = TRACE_FAILURE;
sync_arg_t *arg = (sync_arg_t *)malloc(sizeof(*arg));
if (arg == NULL) {
SYSLOG_ERR("Allocate sync arg is failure:%d", errno);
return TRACE_FAILURE;
}
if (sem_init(&arg->sem, 0, 0) != 0) {
SYSLOG_ERR("Init sem is failure:%d", errno);
goto END1;
}
uint32_t seq;
if (__policy_async_exec(in, sync_exec_cb, arg, &seq) == TRACE_FAILURE) {
SYSLOG_ERR("Exec policy is failure");
goto END2;
}
struct timespec timeout;
if (clock_gettime(CLOCK_REALTIME, &timeout) == -1) {
SYSLOG_ERR("Get current time is failure:%d", errno);
goto END2;
}
timeout.tv_sec += EXEC_SYNC_WAIT_TIMEOUT;
if (sem_timedwait(&arg->sem, &timeout) == -1) {
cb_arg_t *cb_arg = NULL;
ret = get_and_del_arg_from_hlist(seq, &cb_arg);
if (ret == TRACE_SUCCESS) {
if (cb_arg != NULL) {
free(cb_arg);
arg->ret = TRACE_FAILURE;
SYSLOG_ERR("Wait exec result is failure:%d", errno);
}
} else {
SYSLOG_ERR("Get arg is failure");
goto END2;
}
}
SYSLOG_INFO("Sync exec is completely, respone result:%u", arg->ret);
ret = arg->ret;
END2:
sem_destroy(&arg->sem);
END1:
free(arg);
return ret;
}

View File

@ -0,0 +1,14 @@
ifneq ($(KERNELRELEASE),)
obj-m += trace_relay.o
else
PWD := $(shell pwd)
KVER := $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build
KBUILD_EXTRA_SYMBOLS += /data/code/modules/netlink_api/app_k/Module.symvers
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -rf *.o *.mod.c *.ko *.symvers *.order *.makers
endif

View File

@ -0,0 +1,30 @@
#include <linux/module.h>
#include <libnetlink_k.h>
static int trace_rcv_policy(struct sk_buff *skb, struct nlmsghdr *nlh)
{
printk(KERN_DEBUG"trace recv policy1111");
return 0;
}
static int __init trace_init(void)
{
printk(KERN_DEBUG"trace recv policy");
cfg_msgtype_register(0x12, trace_rcv_policy, NULL, NULL);
return 0;
}
static void __exit trace_exit(void)
{
cfg_msgtype_unregister(0x12);
}
module_init(trace_init);
module_exit(trace_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Trace process module");
MODULE_AUTHOR("zhangtao");

44
Platform/trace/trace_api.h Executable file
View File

@ -0,0 +1,44 @@
#ifndef _TRACE_API_H
#define _TRACE_API_H
#include <stdint.h>
#include <linux/in.h>
#include <linux/in6.h>
typedef enum {
TRACE_SUCCESS = 0,
TRACE_FAILURE,
TRACE_PENDING
} trace_ret_t;
typedef struct _addr {
uint8_t family;
union {
struct in_addr ip4;
struct in6_addr ip6;
} addr;
} addr_t;
typedef void (*async_cb)(trace_ret_t ret, void *arg);
typedef struct _trace_policy {
addr_t src;
uint16_t sport;
addr_t dst;
uint16_t dport;
uint8_t protocol;
//PKT_TUPLE
uint16_t app_type;
} trace_policy_t;
trace_ret_t policy_client_init();
trace_ret_t policy_client_exit();
trace_ret_t policy_async_exec(const trace_policy_t *in,
async_cb cb, void *arg);
trace_ret_t policy_sync_exec(const trace_policy_t *in);
#endif

23
Platform/trace/trace_msg.h Executable file
View File

@ -0,0 +1,23 @@
#ifndef _TRACE_MSG_H
#define _TRACE_MSG_H
#include <stdint.h>
#include "trace_api.h"
typedef struct _trace_hdr {
uint32_t ver;
uint32_t seq;
} trace_hdr_t;
typedef struct _trace_req {
trace_hdr_t hdr;
trace_policy_t policy;
} trace_req_t;
typedef struct _trace_reply {
trace_hdr_t hdr;
trace_ret_t result;
} trace_reply_t;
#endif