diff --git a/Common/trace_api.h b/Common/trace_api.h index 319268fef..a24289284 100755 --- a/Common/trace_api.h +++ b/Common/trace_api.h @@ -1,51 +1,10 @@ #ifndef _TRACE_API_H #define _TRACE_API_H -#include -#include - -typedef enum { - TRACE_SUCCESS = 0, - TRACE_FAILURE, - TRACE_PENDING -} trace_ret_t; - -#ifndef u8 -typedef unsigned char u8; -#endif - -#ifndef u16 -typedef unsigned short u16; -#endif - -#ifndef u32 -typedef unsigned int u32; -#endif - - -typedef struct _addr { - u8 family; - union { - struct in_addr ip4; - struct in6_addr ip6; - } addr; -} addr_t; - +#include "trace_def.h" typedef void (*async_cb)(trace_ret_t ret, void *arg); - -typedef struct _trace_policy { - addr_t src; - u16 sport; - addr_t dst; - u16 dport; - u8 protocol; - - //PKT_TUPLE - u16 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, diff --git a/Common/trace_def.h b/Common/trace_def.h new file mode 100755 index 000000000..ca1e65a37 --- /dev/null +++ b/Common/trace_def.h @@ -0,0 +1,44 @@ +#ifndef _TRACE_DEF_H +#define _TRACE_DEF_H + +#include +#include + +typedef enum { + TRACE_SUCCESS = 0, + TRACE_FAILURE, + TRACE_PENDING +} trace_ret_t; + +#ifndef u8 +typedef unsigned char u8; +#endif + +#ifndef u16 +typedef unsigned short u16; +#endif + +#ifndef u32 +typedef unsigned int u32; +#endif + +typedef struct _addr { + u8 family; + union { + struct in_addr ip4; + struct in6_addr ip6; + } addr; +} addr_t; + +typedef struct _trace_policy { + addr_t src; + u16 sport; + addr_t dst; + u16 dport; + u8 protocol; + + //PKT_TUPLE + u16 app_type; +} trace_policy_t; + +#endif \ No newline at end of file diff --git a/Platform/common/trace/trace_msg.h b/Platform/common/trace/trace_msg.h index b1608fd82..929994d65 100755 --- a/Platform/common/trace/trace_msg.h +++ b/Platform/common/trace/trace_msg.h @@ -1,7 +1,7 @@ #ifndef _TRACE_MSG_H #define _TRACE_MSG_H -#include "trace_api.h" +#include "trace_def.h" enum { TRACE_MSG_POLICY_REQ = 0x0, diff --git a/Platform/modules/trace-relay/trace_init.c b/Platform/modules/trace-relay/trace_init.c index 07b251881..890fdb689 100755 --- a/Platform/modules/trace-relay/trace_init.c +++ b/Platform/modules/trace-relay/trace_init.c @@ -1,7 +1,7 @@ #include #include -#include "trace_api.h" +#include "trace_def.h" #include "../netlink_api/libnetlink_k.h" #include "trace_msg.h" #include "commuapinl.h" @@ -12,6 +12,13 @@ static int trace_rcv_policy(struct sk_buff *skb, struct nlmsghdr *nlh) trace_ret_t ret = TRACE_FAILURE; trace_hdr_t *hdr; void *buf; + int len; + struct sk_buff *reply_skb; + struct nlmsghdr *reply_hdr; + void *reply_data; + trace_reply_t *reply; + int reply_ret; + trace_policy_t *policy; printk(KERN_DEBUG"Trace recv policy"); switch (nlh->nlmsg_type) { @@ -21,14 +28,14 @@ static int trace_rcv_policy(struct sk_buff *skb, struct nlmsghdr *nlh) printk(KERN_ERR"Receiving data is null"); break; } - int len = nlmsg_len(nlh); + len = nlmsg_len((const struct nlmsghdr *)nlh); printk(KERN_DEBUG"Receive data of trace is len:%d", len); if (len < TRACE_REQ_SZ) { - printk(KERN_WARNING"Receiving data length:%d is less than length:%d is needed", len, TRACE_REQ_SZ); + printk(KERN_WARNING"Receiving data length:%d is less than length:%ld is needed", len, TRACE_REQ_SZ); } hdr = (trace_hdr_t *)buf; - trace_policy_t *policy = (trace_policy_t *)(buf + 1); + policy = (trace_policy_t *)(buf + 1); ret = TRACE_SUCCESS; break; @@ -38,26 +45,30 @@ static int trace_rcv_policy(struct sk_buff *skb, struct nlmsghdr *nlh) break; } - struct sk_buff *reply_skb = nlmsg_new(TRACE_REPLY_SZ, GFP_KERNEL); + if (ret == TRACE_FAILURE) { + return 1; + } + + reply_skb = nlmsg_new(TRACE_REPLY_SZ, GFP_KERNEL); if (reply_skb == NULL) { printk(KERN_ERR"Allocating skb memory is failure"); goto FAIL; } - struct nlmsghdr *reply_hdr = nlmsg_put(reply_skb, 0, 0, TRACE_MSG_POLICY_REPLY, TRACE_REPLY_SZ, 0); + reply_hdr = nlmsg_put(reply_skb, 0, 0, TRACE_MSG_POLICY_REPLY, TRACE_REPLY_SZ, 0); if (reply_hdr == NULL) { printk(KERN_ERR"Putting length of reply is failure"); goto FAIL; } - void *reply_data = nlmsg_data(reply_hdr); + reply_data = nlmsg_data(reply_hdr); if (reply_data == NULL) { printk(KERN_ERR"Reply data is null"); goto FAIL; } - trace_reply_t *reply = (trace_reply_t *)reply_data; + reply = (trace_reply_t *)reply_data; memcpy(&reply->hdr, hdr, sizeof(*hdr)); reply->result = ret; - int reply_ret = commnl_unicast(NULL, reply_skb, nlh->nlmsg_pid); + reply_ret = commnl_unicast(NULL, reply_skb, nlh->nlmsg_pid); if (reply_ret != 0) { printk(KERN_ERR"Reply message is failure"); goto FAIL; diff --git a/Platform/user/trace/trace-api/trace_api.c b/Platform/user/trace/trace-api/trace_api.c index a1884bf86..a5128ad1d 100755 --- a/Platform/user/trace/trace-api/trace_api.c +++ b/Platform/user/trace/trace-api/trace_api.c @@ -102,7 +102,7 @@ static void *cb_thread(void *arg) ssize_t size; trace_reply_t msg; - return NULL; + return NULL; // todo while(1) { if (g_client_stop) { @@ -185,13 +185,6 @@ 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); @@ -248,46 +241,17 @@ trace_ret_t policy_client_exit() return TRACE_SUCCESS; } -static trace_ret_t __policy_async_exec(const trace_policy_t *in, - async_cb cb, void *arg, uint32_t *seq_out) +static cb_arg_t *add_to_list_and_get(const uint32_t i, const uint32_t seq, const struct sysinfo *info, async_cb cb, void *arg) { - trace_ret_t ret = TRACE_FAILURE; + cb_arg_t *cb_arg = NULL; 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) { + if ((info->uptime - first->t) > MAX_QUEUE_TIMEOUT) { hlist_del(&first->node); tmp_cb = cb_arg->cb; @@ -295,19 +259,17 @@ static trace_ret_t __policy_async_exec(const trace_policy_t *in, // 复用cb_arg cb_arg = first; - } else { - pthread_mutex_unlock((pthread_mutex_t *)&g_sess.hsess_mutex[i]); + } else { SYSLOG_WARN("Hash table:[%u] queue is full", i); - goto END; + goto FAIL; } } 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; + goto FAIL; } } cb_arg->seq = seq; @@ -315,7 +277,7 @@ static trace_ret_t __policy_async_exec(const trace_policy_t *in, cb_arg->cb = cb; INIT_HLIST_NODE(&cb_arg->node); - cb_arg->t = info.uptime; + cb_arg->t = info->uptime; // 新节点加的链表后面 if (hlist_empty((struct hlist_head *)&g_sess.hess[i])) { @@ -325,33 +287,79 @@ static trace_ret_t __policy_async_exec(const trace_policy_t *in, } 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; + + return cb_arg; +FAIL: + if (cb_arg == NULL) { + free(cb_arg); + } + + return NULL; +} + +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; + async_cb tmp_cb = NULL; + void *tmp_arg; + struct sysinfo info; + uint32_t seq, i; + + seq = __sync_add_and_fetch(&g_sessionid, 1); + SYSLOG_DEBUG("The seq of the message is %u", seq); + + cb_arg_t *cb_arg = NULL; + if (cb != NULL) { + 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, errno); + goto END; + } + + prev_last = g_sess.last[i]; + cb_arg = add_to_list_and_get(i, seq, &info, cb, arg); + 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, errno); + goto END; + } + if (cb_arg == NULL) { + SYSLOG_ERR("cbarg which is been added is failure"); + 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); + ret = cfg_channel_send(seq, in); + if (ret != TRACE_SUCCESS) { + SYSLOG_ERR("Sending cfg is failure"); 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 (cb != NULL) { + ret = TRACE_PENDING; + } +END: + if (cb != NULL) { + if ((ret == TRACE_FAILURE) + && (cb_arg != NULL)) { + hlist_del(&cb_arg->node); + free(cb_arg); + + g_sess.last[i] = prev_last; + g_sess.count[i]--; + } } if (tmp_cb != NULL) {