Add aaa-12 trace kernel process code

RCA:
SOL:
修改人:zhangtao
检视人:zhangtao
This commit is contained in:
zhangtaohz 2019-06-14 13:54:31 +08:00
parent 3a14b14214
commit 46a20ea517
7 changed files with 102 additions and 19 deletions

View File

@ -1,7 +1,6 @@
#ifndef _TRACE_API_H #ifndef _TRACE_API_H
#define _TRACE_API_H #define _TRACE_API_H
#include <stdint.h>
#include <linux/in.h> #include <linux/in.h>
#include <linux/in6.h> #include <linux/in6.h>
@ -11,8 +10,21 @@ typedef enum {
TRACE_PENDING TRACE_PENDING
} trace_ret_t; } 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 { typedef struct _addr {
uint8_t family; u8 family;
union { union {
struct in_addr ip4; struct in_addr ip4;
struct in6_addr ip6; struct in6_addr ip6;
@ -25,13 +37,13 @@ typedef void (*async_cb)(trace_ret_t ret, void *arg);
typedef struct _trace_policy { typedef struct _trace_policy {
addr_t src; addr_t src;
uint16_t sport; u16 sport;
addr_t dst; addr_t dst;
uint16_t dport; u16 dport;
uint8_t protocol; u8 protocol;
//PKT_TUPLE //PKT_TUPLE
uint16_t app_type; u16 app_type;
} trace_policy_t; } trace_policy_t;
trace_ret_t policy_client_init(); trace_ret_t policy_client_init();

View File

@ -35,7 +35,7 @@ PLAT_LINUX_SRCS = $(COMMON_SRCS)
PLAT_ARM64_SRCS = $(COMMON_SRCS) PLAT_ARM64_SRCS = $(COMMON_SRCS)
# gcc CFLAGS # gcc CFLAGS
PLAT_ARM64_CFLAGS := -I../modules/netlink_api/ -I../../Common PLAT_ARM64_CFLAGS := -I../modules/netlink_api/ -I../../Common -I../common/trace
PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS) PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS)
# this line must be at below of thus, because of... # this line must be at below of thus, because of...

View File

@ -34,7 +34,7 @@ PLAT_LINUX_SRCS = $(COMMON_SRCS)
PLAT_ARM64_SRCS = $(COMMON_SRCS) PLAT_ARM64_SRCS = $(COMMON_SRCS)
# gcc CFLAGS # gcc CFLAGS
PLAT_ARM64_CFLAGS := -fPIC -I../../Common -I../user/netlink_uapi PLAT_ARM64_CFLAGS := -fPIC -I../../Common -I../common/trace -I../user/netlink_uapi
PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS) PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS)

View File

@ -34,7 +34,7 @@ PLAT_LINUX_SRCS = $(COMMON_SRCS)
PLAT_ARM64_SRCS = $(COMMON_SRCS) PLAT_ARM64_SRCS = $(COMMON_SRCS)
# gcc CFLAGS # gcc CFLAGS
PLAT_ARM64_CFLAGS := -I../user/trace/trace-api -I../../Common -I../common PLAT_ARM64_CFLAGS := -I../user/trace/trace-api -I../../Common -I../common/trace
PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS) PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS)

View File

@ -1,13 +1,16 @@
#ifndef _TRACE_MSG_H #ifndef _TRACE_MSG_H
#define _TRACE_MSG_H #define _TRACE_MSG_H
#include <stdint.h>
#include "trace_api.h" #include "trace_api.h"
enum {
TRACE_MSG_POLICY_REQ = 0x0,
TRACE_MSG_POLICY_REPLY
};
typedef struct _trace_hdr { typedef struct _trace_hdr {
uint32_t ver; u32 ver;
uint32_t seq; u32 seq;
} trace_hdr_t; } trace_hdr_t;
typedef struct _trace_req { typedef struct _trace_req {
@ -20,4 +23,7 @@ typedef struct _trace_reply {
trace_ret_t result; trace_ret_t result;
} trace_reply_t; } trace_reply_t;
#define TRACE_REQ_SZ sizeof(trace_req_t)
#define TRACE_REPLY_SZ sizeof(trace_reply_t)
#endif #endif

View File

@ -1,17 +1,83 @@
#include <linux/module.h> #include <linux/module.h>
#include <net/netlink.h>
#include "trace_api.h"
#include "../netlink_api/libnetlink_k.h" #include "../netlink_api/libnetlink_k.h"
#include "trace_msg.h"
static int trace_rcv_policy(struct sk_buff *skb, struct nlmsghdr *nlh) static int trace_rcv_policy(struct sk_buff *skb, struct nlmsghdr *nlh)
{ {
printk(KERN_DEBUG"trace recv policy1111"); trace_ret_t ret = TRACE_FAILURE;
trace_hdr_t *hdr;
void *buf;
printk(KERN_DEBUG"Trace recv policy");
switch (nlh->nlmsg_type) {
case TRACE_MSG_POLICY_REQ:
buf = nlmsg_data(nlh);
if (buf == NULL) {
printk(KERN_ERR"Receiving data is null");
break;
}
int len = nlmsg_len(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);
}
hdr = (trace_hdr_t *)buf;
trace_policy_t *policy = (trace_policy_t *)(buf + 1);
ret = TRACE_SUCCESS;
break;
default:
printk(KERN_WARNING"Unknow msg type:%u", nlh->nlmsg_type);
return 1;
break;
}
struct sk_buff *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);
if (reply_hdr == NULL) {
printk(KERN_ERR"Putting length of reply is failure");
goto FAIL;
}
void *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;
memcpy(&reply->hdr, hdr, sizeof(*hdr));
reply->result = ret;
int reply_ret = commnl_unicast(NULL, reply_skb, nlh->nlmsg_pid);
if (reply_ret != 0) {
printk(KERN_ERR"Reply message is failure");
goto FAIL;
}
nlmsg_free(reply_skb);
return 0; return 0;
FAIL:
if (reply_skb != NULL) {
nlmsg_free(reply_skb);
}
return 1;
} }
static int __init trace_init(void) static int __init trace_init(void)
{ {
printk(KERN_DEBUG"trace recv policy"); printk(KERN_INFO"Trace is initiating");
cfg_msgtype_register(0x12, trace_rcv_policy, NULL, NULL); cfg_msgtype_register(0x12, trace_rcv_policy, NULL, NULL);
printk(KERN_INFO"Trace is initiated");
return 0; return 0;
} }

View File

@ -29,8 +29,7 @@
#define MAX_QUEUE_COUNT 256 #define MAX_QUEUE_COUNT 256
#define MAX_QUEUE_TIMEOUT (EXEC_SYNC_WAIT_TIMEOUT + 2) #define MAX_QUEUE_TIMEOUT (EXEC_SYNC_WAIT_TIMEOUT + 2)
#define MAX_TRACE_BUF_SZ (sizeof(struct nlmsghdr) + sizeof(trace_req_t)) #define TRACE_POLICY_BUF_SZ (sizeof(struct nlmsghdr) + TRACE_REQ_SZ)
#define SESS_HASH_INDEX(seq) (seq >> HASH_SESS_TAB_BITS) #define SESS_HASH_INDEX(seq) (seq >> HASH_SESS_TAB_BITS)
@ -148,7 +147,7 @@ static void *cb_thread(void *arg)
static trace_ret_t cfg_channel_send(const uint32_t seq, const trace_policy_t *policy) static trace_ret_t cfg_channel_send(const uint32_t seq, const trace_policy_t *policy)
{ {
char buf[MAX_TRACE_BUF_SZ + 10]; char buf[TRACE_POLICY_BUF_SZ + 10];
struct nlmsghdr *hdr = (struct nlmsghdr *)buf; struct nlmsghdr *hdr = (struct nlmsghdr *)buf;
hdr->nlmsg_len = NLMSG_HDRLEN; hdr->nlmsg_len = NLMSG_HDRLEN;