diff --git a/Common/commuapinl.h b/Common/commuapinl.h index e3323fcfc..36e87b261 100755 --- a/Common/commuapinl.h +++ b/Common/commuapinl.h @@ -8,6 +8,10 @@ +/**********************NETLINK MODULE CONFIG:being**************************/ +//#define NLDEBUG_ACK_COOKIES +/**********************NETLINK MODULE CONFIG:end****************************/ + /* netlink proto*/ #define NETLINK_COMMMAX_MIN_FAMILY 21 #define NETLINK_COMMCFG 22 @@ -41,9 +45,9 @@ enum pdeliverynetlink_groups { /*netlink pdelivery msg type*/ enum pdelivmsgtype{ PDNL_BASE = 0x10,/*netlink 保留控制消息*/ - PDNLGRP_REQUEST, /*用户态发送给内核态的请求消息,用于查看pdiliv模块本身的状态及配置,与业务无关*/ + NLMSG_PDELIV_DEBUGFS = 0x11, /*用户态发送给内核态的请求消息,用于查看pdiliv模块本身的状态及配置,与业务无关*/ NLMSG_RECV_RAW_PKT,/*上送DPI的报文消息*/ - NLMSG_DEBUGFS, + PDNLGRP_REQUEST, NLMGS_PDELIVERY_MAX_TYPE, }; @@ -72,9 +76,8 @@ enum pdelivattr{ enum commcfgmsgtype{ //COMMMSGNL_BASE = NLMSG_MIN_TYPE,/*netlink 保留控制消息*/ COMMMSGNL_BASE = 0x10,/*netlink 保留控制消息*/ + COMMNMSG_CFG_DEBUGFS = 0x11,/*keep the same with NLMSG_PDELIV_DEBUGFS */ COMMNMSG_POLICYCONF, - COMMNMSG_USER, - COMMNMSG_DEBUGFS, /*for trace*/ TRACE_CFG_POLICY = 0x20, TRACE_CFG_MAX = 0X25, @@ -84,9 +87,25 @@ enum commcfgmsgtype{ COMMCFG_NLMSG_MAX_TYPE, }; +#if 0 +struct pdeliv_debugfs { + unsigned int rev_succ; + unsigned int rev_fail; + unsigned int pde_succ; + unsigned int pde_fail; + }; +#endif +struct netlink_debugfs { + unsigned char cfg_rev_succ; + unsigned char cfg_rev_fail; + unsigned char pde_rev_succ; + unsigned char pde_rev_fail; + unsigned char pde_pkt_succ; + unsigned char pde_pkt_fail; + }; #endif /* _UAPI__LINUX_COMMNETLINK_H */ diff --git a/Common/policyconf.h b/Common/policyconf.h index bf3c74aac..c6220713e 100755 --- a/Common/policyconf.h +++ b/Common/policyconf.h @@ -19,6 +19,7 @@ enum { POLICYCONFA_DSTPORT,/* used in NLMSG_POLICYCONF*/ POLICYCONFA_PROTO, /* used in NLMSG_POLICYCONF*/ POLICYCONFA_ACTION,/* used in NLMSG_POLICYCONF*/ + NLDEBUG_MSG_TYPE, __POLICYCONFA_MAX }; #define POLICYCONFA_MAX (__POLICYCONFA_MAX - 1) diff --git a/Platform/modules/cfgrcv/cfgrcv_kinit.c b/Platform/modules/cfgrcv/cfgrcv_kinit.c index 890dd84fa..71d058779 100755 --- a/Platform/modules/cfgrcv/cfgrcv_kinit.c +++ b/Platform/modules/cfgrcv/cfgrcv_kinit.c @@ -10,24 +10,15 @@ #include "../netlink_api/libnetlink_k.h" #include "../../../Common/commuapinl.h" -#define strict_strtoul kstrtoul - -/**************************************************************************************************************/ -#include -#define NLMSG_TAIL(nmsg) \ - ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len))) -#define RTA_LENGTH(len) (RTA_ALIGN(sizeof(struct rtattr)) + (len)) -#define RTA_ALIGNTO 4U -#define RTA_ALIGN(len) ( ((len)+RTA_ALIGNTO-1) & ~(RTA_ALIGNTO-1) ) -struct policyconfmsg { - __u8 policy_id; -}; -/**************************************************************************************************************/ - struct netlinkk_cfg g_upmnlcfg; +int cfgnl_unicast(struct sk_buff *skb, u32 portid) +{ + return(commnl_unicast(g_upmnlcfg.sk, skb, portid)); +} + #if 0 int nl_upm_data_ready(struct sk_buff *skb, struct nlmsghdr *nlh) @@ -75,44 +66,20 @@ failure: } #endif -int upm_rcv_policycfg(struct sk_buff *skb, struct nlmsghdr *nlh) +int cfgrcv_debugfs(struct sk_buff *skb, struct nlmsghdr *nlh,struct netlink_ext_ack *extack) { - printk(KERN_INFO "upm_rcv_policycfg, From pid: %d\n", nlh->nlmsg_pid); + int ret = 0; - /**************************************************************************************************************/ - char *pload; - struct nlattr *rta; - char* load; + printk(KERN_INFO "cfgrcv_debugfs, From pid: %d\n", nlh->nlmsg_pid); - pload = nlmsg_data(nlh) + NLMSG_ALIGN(sizeof(struct policyconfmsg)); - rta = (struct nlattr *)pload; - load = RTA_DATA(rta); - unsigned long fk_type; - - printk("***************************rta->nla_type=%d********************\n", rta->nla_type); //nla_type - printk("**************************load=*****%s**********\n",(char *)load); //string printf - /**************************************************************************************************************/ - strict_strtoul(load, 10, &fk_type); - /**************************************************************************************************************/ - switch(nlh->nlmsg_type) - { - //case COMMNMSG_POLICYCONF:/**/ - case NLMSG_DEBUGFS: - if(nlh->nlmsg_type == NLMSG_DEBUGFS) - { - debugfs_pkt_num_stati(&g_upmnlcfg, nlh, fk_type); - printk("*****************biduichengong***************\n"); - return 0; - } - break; +#ifdef NLDEBUG_ACK_COOKIES + ret = debugfs_pkt_num_stati(&g_upmnlcfg, nlh,extack); +#else + ret = debugfs_pkt_num_stati_witisend(&g_upmnlcfg, nlh,extack); +#endif - default: - printk(KERN_INFO "libnetlink Unknow msgtype recieved!\n"); - return 0; - } - /**************************************************************************************************************/ - - return 0; + printk("*****************biduichengong***************\n"); + return ret; } @@ -134,8 +101,7 @@ int __init upm_init(void) } /*do msg process register*/ - cfg_msgtype_register(COMMNMSG_POLICYCONF,upm_rcv_policycfg,NULL,NULL); - cfg_msgtype_register(NLMSG_DEBUGFS,upm_rcv_policycfg,NULL,NULL); + cfg_msgtype_register(COMMNMSG_CFG_DEBUGFS,cfgrcv_debugfs,NULL,NULL); return 0; } @@ -148,6 +114,7 @@ void __exit upm_exit(void) return; } +EXPORT_SYMBOL_GPL(cfgnl_unicast); module_init(upm_init); module_exit(upm_exit); diff --git a/Platform/modules/netlink_api/build_netlinkall.sh b/Platform/modules/netlink_api/build_netlinkall.sh index 6a9535693..9db26b92a 100644 --- a/Platform/modules/netlink_api/build_netlinkall.sh +++ b/Platform/modules/netlink_api/build_netlinkall.sh @@ -1,5 +1,12 @@ #!/bin/sh echo '---------build kernel_ko:-------' + +cd ../../modules/netlink_api +echo 'clean netlink_api..' +make clean +make +echo 'build netlink_api.ko sucess!' +echo '---------------------------' cd ../../modules/cfgrcv echo 'clean cfgrcv..' make clean diff --git a/Platform/modules/netlink_api/libnetlink_k.c b/Platform/modules/netlink_api/libnetlink_k.c index e023c1925..115e59703 100755 --- a/Platform/modules/netlink_api/libnetlink_k.c +++ b/Platform/modules/netlink_api/libnetlink_k.c @@ -10,6 +10,19 @@ #include "libnetlink_k.h" #include "../../../Common/commuapinl.h" +#define strict_strtoul kstrtoul + + +/**************************************************************************************************************/ +#include +#define NLMSG_TAIL(nmsg) \ + ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len))) +#define RTA_LENGTH(len) (RTA_ALIGN(sizeof(struct rtattr)) + (len)) +#define RTA_ALIGNTO 4U +#define RTA_ALIGN(len) ( ((len)+RTA_ALIGNTO-1) & ~(RTA_ALIGNTO-1) ) +/**************************************************************************************************************/ + + struct commnl_msgtype_process pdelivnl_msg_handlers[NLMGS_PDELIVERY_MAX_TYPE]; struct commnl_msgtype_process cfgnl_msg_handlers[COMMCFG_NLMSG_MAX_TYPE]; struct recv_debugfs recv_dfs; @@ -174,14 +187,14 @@ void printk_ipaddress(unsigned int address) int nl_cfg_data_ready(struct sk_buff *skb, struct nlmsghdr *nlh, struct netlink_ext_ack *extack) { - int ret = -1; + int ret = 0; printk(KERN_DEBUG "nl_cfg_data_ready() nlmsg_type = %d begin.\n",nlh->nlmsg_type); if(NULL != cfgnl_msg_handlers[nlh->nlmsg_type].doit) { cfgnl_msg_handlers[nlh->nlmsg_type].cfg_recv_succ++; - ret = cfgnl_msg_handlers[nlh->nlmsg_type].doit(skb, nlh); + ret = cfgnl_msg_handlers[nlh->nlmsg_type].doit(skb, nlh,extack); recv_dfs.cfg_recv_succ++; } @@ -196,18 +209,19 @@ int nl_cfg_data_ready(struct sk_buff *skb, struct nlmsghdr *nlh, struct netlink_ printk(KERN_DEBUG "nl_cfg_data_ready() nlmsg_type = %d end.\n",nlh->nlmsg_type); - return 0; + return ret; } int nl_pdeliv_data_ready(struct sk_buff *skb, struct nlmsghdr *nlh, struct netlink_ext_ack *extack) { - int ret = -1; + int ret = 0; + printk(KERN_DEBUG "nl_pdeliv_data_ready() nlmsg_type = %d begin.\n",nlh->nlmsg_type); if(NULL != pdelivnl_msg_handlers[nlh->nlmsg_type].doit) { pdelivnl_msg_handlers[nlh->nlmsg_type].cfg_recv_succ++; - ret = pdelivnl_msg_handlers[nlh->nlmsg_type].doit(skb, nlh); + ret = pdelivnl_msg_handlers[nlh->nlmsg_type].doit(skb, nlh,extack); recv_dfs.pde_recv_succ++; } @@ -220,7 +234,7 @@ int nl_pdeliv_data_ready(struct sk_buff *skb, struct nlmsghdr *nlh, struct netli printk(KERN_DEBUG "nl_pdeliv_data_ready() nlmsg_type = %d end.\n",nlh->nlmsg_type); - return 0; + return ret; } static void libcfgnl_rcv(struct sk_buff *skb) @@ -302,27 +316,77 @@ int commnl_unicast(struct sock *sk, struct sk_buff *skb, u32 portid) } +int debugfs_pkt_num_stati(struct netlinkk_cfg *g_nlcfg, struct nlmsghdr *nlh,struct netlink_ext_ack *extack) +{ + unsigned char * cookies = "test cookies"; + int len = 0; + + printk("debugfs_pkt_num_stati.\r\n"); -int debugfs_pkt_num_stati(struct netlinkk_cfg *g_nlcfg, struct nlmsghdr *nlh, unsigned long fk_type) + len = strlen(cookies); + + if(len > sizeof(extack->cookie) )/*check len*/ + { + printk("debugfs_pkt_num_stati:cookies oversize the netlinkt cookies.\r\n"); + extack->cookie_len = sizeof(extack->cookie); + } + else + { + extack->cookie_len = len; + } + + memcpy(extack->cookie, cookies, extack->cookie_len);/*becare:size must small than 20 u8!!!!!!!!!!*/ + + printk("------------------debugfs_pkt_num_stati ack type=%d,seq=%d,extack->cookie_len=%d.\n", + nlh->nlmsg_type,nlh->nlmsg_seq,extack->cookie_len); + + return 0; +} + +int debugfs_pkt_num_stati_witisend(struct netlinkk_cfg *g_nlcfg, struct nlmsghdr *nlh,struct netlink_ext_ack *extack) { int err; struct sk_buff *out_skb; struct nlmsghdr *out_nlh; unsigned int length = sizeof(struct netlink_debugfs); + char *pload; + struct nlattr *rta; + char* load; + unsigned long fk_type; + struct netlink_debugfs netlink_dfs_temp ={0}; + + pload = nlmsg_data(nlh); + rta = (struct nlattr *)pload; + load = RTA_DATA(rta); + + printk("***************************rta->nla_type=%d********************\n", rta->nla_type); //nla_type + printk("**************************load=*****%s**********\n",(char *)load); //string printf + strict_strtoul(load, 10, &fk_type); + printk("***************************fk_type%lu********************\n", fk_type); + out_skb = nlmsg_new(length, GFP_KERNEL); if (!out_skb) goto failure; - out_nlh = nlmsg_put(out_skb, 0, 0, NLMSG_DEBUGFS, length, 0); + out_nlh = nlmsg_put(out_skb,nlh->nlmsg_pid, nlh->nlmsg_seq, 0x11, length, 0); if (!out_nlh) goto failure; - g_nlcfg->netlink_dfs.cfg_rev_succ = cfgnl_msg_handlers[fk_type].cfg_recv_succ; - g_nlcfg->netlink_dfs.cfg_rev_fail = cfgnl_msg_handlers[fk_type].cfg_recv_fail; - g_nlcfg->netlink_dfs.pde_rev_succ = pdelivnl_msg_handlers[fk_type].cfg_recv_succ; - g_nlcfg->netlink_dfs.pde_rev_fail = pdelivnl_msg_handlers[fk_type].cfg_recv_fail; + netlink_dfs_temp.cfg_rev_succ = cfgnl_msg_handlers[fk_type].cfg_recv_succ; + netlink_dfs_temp.cfg_rev_fail = cfgnl_msg_handlers[fk_type].cfg_recv_fail; + netlink_dfs_temp.pde_rev_succ = pdelivnl_msg_handlers[fk_type].cfg_recv_succ; + netlink_dfs_temp.pde_rev_fail = pdelivnl_msg_handlers[fk_type].cfg_recv_fail; + netlink_dfs_temp.pde_pkt_fail = g_nlcfg->netlink_dfs.pde_pkt_fail; + netlink_dfs_temp.pde_pkt_succ = g_nlcfg->netlink_dfs.pde_pkt_succ; - memcpy(nlmsg_data(out_nlh), &(g_nlcfg->netlink_dfs), length); - printk("******************************g_nlcfg->pdeliv_dfs.pde_fail=%d****************\n",g_nlcfg->netlink_dfs.pde_pkt_fail); - printk("******************************g_nlcfg->pdeliv_dfs.pde_succ=%d****************\n",g_nlcfg->netlink_dfs.pde_pkt_succ); + memcpy(nlmsg_data(out_nlh), &(netlink_dfs_temp), length); + printk("******************************g_nlcfg->pdeliv_dfs.cfg_rev_succ=%d****************\n",g_nlcfg->netlink_dfs.cfg_rev_succ); + printk("******************************g_nlcfg->pdeliv_dfs.cfg_rev_fail=%d****************\n",g_nlcfg->netlink_dfs.cfg_rev_fail); + printk("******************************g_nlcfg->pdeliv_dfs.pde_rev_succ=%d****************\n",g_nlcfg->netlink_dfs.pde_rev_succ); + printk("******************************g_nlcfg->pdeliv_dfs.pde_rev_fail=%d****************\n",g_nlcfg->netlink_dfs.pde_rev_fail); + printk("******************************g_nlcfg->pdeliv_dfs.pde_pkt_succ=%d****************\n",g_nlcfg->netlink_dfs.pde_pkt_succ); + printk("******************************g_nlcfg->pdeliv_dfs.pde_pkt_fail=%d****************\n",g_nlcfg->netlink_dfs.pde_pkt_fail); + + + printk("------------------debugfs_pkt_num_stati ack msg_type %d,seq=%d!\n",out_nlh->nlmsg_type,nlh->nlmsg_seq); err = commnl_unicast(g_nlcfg->sk, out_skb, nlh->nlmsg_pid); if (err > 0) err = 0; @@ -361,8 +425,12 @@ EXPORT_SYMBOL_GPL(commnl_unicast); EXPORT_SYMBOL_GPL(printk_mem); EXPORT_SYMBOL_GPL(printk_ipaddress); EXPORT_SYMBOL_GPL(debugfs_pkt_num_stati); +EXPORT_SYMBOL_GPL(debugfs_pkt_num_stati_witisend); EXPORT_SYMBOL_GPL(nf_nlmsg_multicast); + + + int __init libnetlink_k_init(void) { printk(KERN_INFO "insmod netlink.ko...\n"); diff --git a/Platform/modules/netlink_api/libnetlink_k.h b/Platform/modules/netlink_api/libnetlink_k.h index 106448363..bffb1bd2c 100755 --- a/Platform/modules/netlink_api/libnetlink_k.h +++ b/Platform/modules/netlink_api/libnetlink_k.h @@ -6,6 +6,7 @@ #include #include #include +#include "../../../Common/commuapinl.h" struct recv_debugfs { unsigned int cfg_recv_succ; @@ -15,16 +16,7 @@ struct recv_debugfs { }; -struct netlink_debugfs { - unsigned int cfg_rev_succ; - unsigned int cfg_rev_fail; - unsigned int pde_rev_succ; - unsigned int pde_rev_fail; - - unsigned int pde_pkt_succ; - unsigned int pde_pkt_fail; - }; struct netlinkk_cfg { @@ -37,9 +29,9 @@ struct netlinkk_cfg struct netlink_debugfs netlink_dfs; }; -typedef int (*commnl_doit_func)(struct sk_buff *, struct nlmsghdr *); -typedef int (*commnl_dumpit_func)(struct sk_buff *, struct netlink_callback *); -typedef u16 (*commnl_calcit_func)(struct sk_buff *, struct nlmsghdr *); +typedef int (*commnl_doit_func)(struct sk_buff *, struct nlmsghdr *,struct netlink_ext_ack *); +typedef int (*commnl_dumpit_func)(struct sk_buff *, struct netlink_callback *,struct netlink_ext_ack *); +typedef u16 (*commnl_calcit_func)(struct sk_buff *, struct nlmsghdr *,struct netlink_ext_ack *); struct commnl_msgtype_process { @@ -89,7 +81,10 @@ extern void libnetlinkk_exit(struct netlinkk_cfg *g_nlcfg); extern int commnl_unicast(struct sock *sk, struct sk_buff *skb, u32 portid); -extern int debugfs_pkt_num_stati(struct netlinkk_cfg *g_nlcfg, struct nlmsghdr *nlh, unsigned long fk_value); +extern int debugfs_pkt_num_stati(struct netlinkk_cfg *g_nlcfg, + struct nlmsghdr *nlh,struct netlink_ext_ack *extack); +extern int debugfs_pkt_num_stati_witisend(struct netlinkk_cfg *g_nlcfg, + struct nlmsghdr *nlh,struct netlink_ext_ack *extack); extern int nf_nlmsg_multicast(struct netlinkk_cfg *g_nlcfg, struct sk_buff *skb); diff --git a/Platform/modules/pdelivery/pdeliverynl_kinit.c b/Platform/modules/pdelivery/pdeliverynl_kinit.c index 8de60184f..56b5d3ade 100644 --- a/Platform/modules/pdelivery/pdeliverynl_kinit.c +++ b/Platform/modules/pdelivery/pdeliverynl_kinit.c @@ -9,19 +9,7 @@ #include "../netlink_api/libnetlink_k.h" #include "../../../Common/commuapinl.h" -#define strict_strtoul kstrtoul -/**************************************************************************************************************/ -#include -#define NLMSG_TAIL(nmsg) \ - ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len))) -#define RTA_LENGTH(len) (RTA_ALIGN(sizeof(struct rtattr)) + (len)) -#define RTA_ALIGNTO 4U -#define RTA_ALIGN(len) ( ((len)+RTA_ALIGNTO-1) & ~(RTA_ALIGNTO-1) ) -struct policyconfmsg { - __u8 policy_id; -}; -/**************************************************************************************************************/ //int nf_nlmsg_multicast(struct netlinkk_cfg *g_nlcfg, struct sk_buff *skb); unsigned int pdelivery_hook_func(void *priv, @@ -42,39 +30,25 @@ static struct nf_hook_ops upm_nfho = { }; -int pdeliv_rcv_stat(struct sk_buff *skb, struct nlmsghdr *nlh) +int pdeliv_rcv_stat(struct sk_buff *skb, struct nlmsghdr *nlh,struct netlink_ext_ack * ext_ack) { - printk(KERN_INFO "pdeliv_rcv_stat, From: %d\n", nlh->nlmsg_pid); - - /**************************************************************************************************************/ - char *pload; - struct nlattr *rta; - char* load; - unsigned long fk_type; - - pload = nlmsg_data(nlh) + NLMSG_ALIGN(sizeof(struct policyconfmsg)); - rta = (struct nlattr *)pload; - load = RTA_DATA(rta); - - printk("***************************rta->nla_type=%d********************\n", rta->nla_type); //nla_type - printk("**************************load=*****%s**********\n",(char *)load); //string printf - strict_strtoul(load, 10, &fk_type); - printk("***************************fk_type%d********************\n", fk_type); - /**************************************************************************************************************/ + int ret = 0; /**************************************************************************************************************/ switch(nlh->nlmsg_type) { //case COMMNMSG_POLICYCONF:/**/ - case NLMSG_DEBUGFS: - if(nlh->nlmsg_type == NLMSG_DEBUGFS) - { - debugfs_pkt_num_stati(&g_nlcfg, nlh, fk_type); - printk("*****************biduichengong********%d*******\n", nlh->nlmsg_type); - return 0; - } - break; + case NLMSG_PDELIV_DEBUGFS: + +#ifdef NLDEBUG_ACK_COOKIES + ret = debugfs_pkt_num_stati(&g_nlcfg, nlh, ext_ack); +#else + ret = debugfs_pkt_num_stati_witisend(&g_nlcfg, nlh,ext_ack); +#endif + printk("*****************biduichengong********%d*******\n", nlh->nlmsg_type); + return ret; + break; default: printk(KERN_INFO "libnetlink Unknow msgtype recieved!\n"); @@ -111,7 +85,7 @@ int __init pdelivery_init(void) /*do msg process register*/ pdeliv_msgtype_register(PDNLGRP_REQUEST,pdeliv_rcv_stat,NULL,NULL); - pdeliv_msgtype_register(NLMSG_DEBUGFS,pdeliv_rcv_stat,NULL,NULL); + pdeliv_msgtype_register(NLMSG_PDELIV_DEBUGFS,pdeliv_rcv_stat,NULL,NULL); return 0; } @@ -167,6 +141,10 @@ unsigned int pdelivery_hook_func(void *priv, out_payload = nlmsg_data(out_nlh); memcpy(out_payload, payload,payload_len);/**/ + + /*example:use nla_put to add other attr here. ***********************************/ + /*int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data);*/ + /********************************************************************************/ #if 1 printk(KERN_INFO "%02x %02x %02x %02x %02x %02x %02x %02x\r\n", @@ -178,16 +156,6 @@ unsigned int pdelivery_hook_func(void *priv, ret = nf_nlmsg_multicast(&g_nlcfg, out_skb); -#if 0 - ret = nlmsg_multicast(g_nlcfg.sk,out_skb,0,PDNLGRP_ALLRAW,0); - if (ret < 0) - { - printk(KERN_INFO "Error while sending pkt to user, err id: %d\n", ret); - } - g_nlcfg.pkt_delev_num ++; - -#endif - //g_nlcfg.pkt_delev_num ++; printk(KERN_INFO "pdelivery_hook_func() end.\n"); return NF_ACCEPT;/*must return a value*/ diff --git a/Platform/user/cfg2kernel/cfgchannel_sample.c b/Platform/user/cfg2kernel/cfgchannel_sample.c index e4b888d6c..1f99789f3 100644 --- a/Platform/user/cfg2kernel/cfgchannel_sample.c +++ b/Platform/user/cfg2kernel/cfgchannel_sample.c @@ -10,74 +10,91 @@ #include "policyconf.h" #include "commuapinl.h" -struct upmnl_handle cfgnlh; - -#if 0 -static int process_msg(struct pdelivnl_ctrl_data *ctrl, - struct nlmsghdr *n, void *arg) -{ -// FILE *fp = arg; - - printf("pdelivery main process_msg begin:\r\n"); - - - //if (timestamp) - //print_timestamp(fp); - - switch (n->nlmsg_type) { - case NLMSG_RECV_RAW_PKT: - printf("netlink msg type RAW_pkt:\r\n"); - printf_pkt(NLMSG_DATA(n),n->nlmsg_len-NLMSG_HDRLEN); - - break; - - default: - return 0; - } - - return 0; -} +#ifdef NLDEBUG_ACK_COOKIES +#define CFG_DEBUG_ACK_COOKIES #endif -int set_user_policy_waitack() +struct upmnl_handle cfgnlh; + + +int set_cfg_debug_waitack() { - unsigned char srcip[]={"17"};/*需要发送的数据*/ - int srcip_len = strlen((const char *)srcip); + struct nlmsghdr *ack = NULL; + struct nlmsghdr **answer = &ack; + unsigned char debug_msg_type[]={"17"};/*需要发送的数据*/ + int msg_len = strlen((const char *)debug_msg_type); struct { struct nlmsghdr n; - struct policyconfmsg ncm; char buf[1024]; } req = { - .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct policyconfmsg)), - .n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK, - //.n.nlmsg_type = COMMNMSG_POLICYCONF, - .n.nlmsg_type = COMMNMSG_DEBUGFS, - .ncm.policy_id = 17, + .n.nlmsg_len = NLMSG_LENGTH(0), +#ifdef CFG_DEBUG_ACK_COOKIES + .n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,/*set NLM_F_ACK:use kernel auto ack*/ +#else + .n.nlmsg_flags = NLM_F_REQUEST,/*not use kernel auto ack*/ +#endif + .n.nlmsg_type = COMMNMSG_CFG_DEBUGFS, .n.nlmsg_pid = getpid(), }; - printf("set_user_policy_waitack\r\n"); + printf("set_cfg_debug_waitack\r\n"); /*可选属性*/ - commnl_addattr_l(&req.n, sizeof(req), POLICYCONFA_SRCIP, srcip, srcip_len); + commnl_addattr_l(&req.n, sizeof(req), NLDEBUG_MSG_TYPE, debug_msg_type, msg_len); - printf("srcip_len =%d.req.n.nlmsg_type =%d\r\n",srcip_len,req.n.nlmsg_type); + printf("cfg send debugfs msg to kernel:srcip_len =%d.req.n.nlmsg_type =%d\r\n",msg_len,req.n.nlmsg_type); /*发送组装好的netlink消息*/ - if (commcfg_talk(&req.n, NULL) < 0) + if (commcfg_talk(&req.n, answer) < 0) + { + printf("set_cfg_debug_waitack rcv ack msg faild.\r\n"); return -2; + } + else{ + printf("set_cfg_debug_waitack rcv ack msg sucess.\r\n"); + } + + if(*answer != NULL) + { + printf("set_cfg_debug_waitack rcv answer.\r\n"); + + } + else{ + printf("set_cfg_debug_waitack rcv answer error.\r\n"); + return -3; + } + + +#ifdef CFG_DEBUG_ACK_COOKIES + /*recv answer*/ + if((*answer)->nlmsg_type == NLMSG_ERROR){ + nl_debugfs_extack(*answer); + } + else{ + printf("set_cfg_debug_waitack nlmsg_type is=0x%x,not debugfsmsg ack.\r\n",ack->nlmsg_type); + } +#else + if((*answer)->nlmsg_type == COMMNMSG_CFG_DEBUGFS){ + nl_debugfs(*answer); + } + else{ + printf("set_cfg_debug_waitack nlmsg_type is=0x%x,not COMMNMSG_CFG_DEBUGFS ack.\r\n",ack->nlmsg_type); + } +#endif + + return 0; } -int set_user_policy_nowaitack() +int set_cfg_debug_nowaitack() { - unsigned char srcip[64]={"192.168.1.1"};/*需要发送的数据*/ - int srcip_len = strlen((const char *)srcip); + unsigned char debug_msg_type[]={"17"};/*需要发送的数据*/ + int msg_len = strlen((const char *)debug_msg_type); struct { struct nlmsghdr n; @@ -86,17 +103,17 @@ int set_user_policy_nowaitack() } req = { .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct policyconfmsg)), .n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK, - .n.nlmsg_type = COMMNMSG_POLICYCONF, + .n.nlmsg_type = COMMNMSG_CFG_DEBUGFS, .ncm.policy_id = 17, .n.nlmsg_pid = getpid(), }; - printf("set_user_policy_nowaitack\r\n"); + printf("set_cfg_debug_nowaitack\r\n"); /*可选属性*/ - commnl_addattr_l(&req.n, sizeof(req), POLICYCONFA_SRCIP, srcip, srcip_len); + commnl_addattr_l(&req.n, sizeof(req), NLDEBUG_MSG_TYPE, debug_msg_type, msg_len); - printf("srcip_len =%d.req.n.nlmsg_type =%d\r\n",srcip_len,req.n.nlmsg_type); + printf("msg_len =%d.req.n.nlmsg_type =%d\r\n",msg_len,req.n.nlmsg_type); /*发送组装好的netlink消息*/ if (commcfg_send(&req.n) < 0) @@ -123,13 +140,15 @@ int main( int argc, char **argv) } /*下发配置到内核态*/ - ret = set_user_policy_waitack(); + //ret = set_cfg_debug_nowaitack(); + ret = set_cfg_debug_waitack(); if(ret < 0) { - printf("set_user_policy failed.\r\n"); + printf("set_cfg_debug_waitack failed.\r\n"); return -1; } + /*关闭netlink通道*/ commcfgnl_close(); diff --git a/Platform/user/netlink_uapi/libnetlinku.c b/Platform/user/netlink_uapi/libnetlinku.c index ec25a31ba..faa51ef1b 100644 --- a/Platform/user/netlink_uapi/libnetlinku.c +++ b/Platform/user/netlink_uapi/libnetlinku.c @@ -15,6 +15,14 @@ #include "libnetlinku.h" #include "commuapinl.h" +#ifndef SOL_NETLINK +#define SOL_NETLINK 270 +#endif + +#ifndef NETLINK_EXT_ACK +#define NETLINK_EXT_ACK 11 +#endif + #if 0 struct nlumsg_list { struct list_head list; @@ -201,12 +209,13 @@ int commnl_open_byproto( unsigned int subscriptions,int protocol) goto ERR; } -#if 0 /* Older kernels may no support extended ACK reporting */ - setsockopt(upmh->fd, SOL_NETLINK, NETLINK_EXT_ACK, - &one, sizeof(one)); -#endif - + if (setsockopt(upmh->fd, SOL_NETLINK, NETLINK_EXT_ACK, + &one, sizeof(one))){ + perror("NETLINK_EXT_ACK"); + goto ERR; + } + memset(&upmh->local, 0, sizeof(upmh->local)); upmh->local.nl_family = AF_NETLINK; upmh->local.nl_pid = getpid(); @@ -338,15 +347,16 @@ int pdelivnl_send(int group,struct nlmsghdr *n) static int __commnl_recvmsg(int fd, struct msghdr *msg, int flags) { - int len; - unsigned int num = COMMNL_TALKRCV_TIMEOUT;/*10秒超时*/ + int len = 0; + unsigned int num = COMMNL_TALKRCV_TIMEOUT;/*timeout*/ do { len = recvmsg(fd, msg, flags); +#if 1 num --; if(num ==0) break; - +#endif printf("__rtnl_recvmsg return num %d.\r\n",num); } while (len < 0 && (errno == EINTR || errno == EAGAIN));/*超时*/ @@ -370,7 +380,6 @@ static int commnl_recvmsg(int fd, struct msghdr *msg, char **answer) struct iovec *iov = msg->msg_iov; char *buf; int len; - struct netlink_debugfs *debugfs_statis; struct nlmsghdr *recv_hdr; @@ -402,22 +411,7 @@ static int commnl_recvmsg(int fd, struct msghdr *msg, char **answer) *answer = buf; else free(buf); - - recv_hdr = *answer; - printf("*******************answer******%d****************\n", *answer); - - if(recv_hdr->nlmsg_type == COMMNMSG_DEBUGFS) - { - debugfs_statis = NLMSG_DATA(*answer); - - printf("******debugfs_statis->cfg_rev_succ = %d******\n",debugfs_statis->cfg_rev_succ); - printf("******debugfs_statis->cfg_rev_fail = %d******\n",debugfs_statis->cfg_rev_fail); - printf("******debugfs_statis->pde_rev_succ = %d******\n",debugfs_statis->pde_rev_succ); - printf("******debugfs_statis->pde_rev_fail = %d******\n",debugfs_statis->pde_rev_fail); - printf("******debugfs_statis->pde_pkt_succ = %d******\n",debugfs_statis->pde_pkt_succ); - printf("******debugfs_statis->pde_pkt_fail = %d******\n",debugfs_statis->pde_pkt_fail); - - } + recv_hdr = (struct nlmsghdr *)(*answer); return len; } @@ -452,6 +446,9 @@ static int __commnl_talk_iov(struct upmnl_handle *rtnl, struct iovec *iov, perror("Cannot talk to rtnetlink"); return -1; } + else{ + printf("__commnl_talk_iov send msg sucess.\r\n"); + } /* change msg to use the response iov */ msg.msg_iov = &riov; @@ -459,11 +456,15 @@ static int __commnl_talk_iov(struct upmnl_handle *rtnl, struct iovec *iov, i = 0; while (1) { next: + printf("talk recvmsg-ing...\r\n"); status = commnl_recvmsg(rtnl->fd, &msg, &buf); ++i; if (status < 0) + { + printf("__commnl_talk_iov: commnl_recvmsg return error=%d...\r\n",status); return status; + } if (msg.msg_namelen != sizeof(nladdr)) { fprintf(stderr, @@ -494,10 +495,13 @@ next: /* Don't forget to skip that message. */ status -= NLMSG_ALIGN(len); h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len)); + printf("__commnl_talk_iov recv a msg,but not to me:h->nlmsg_pid=%d,local_pid=%d,h->nlmsg_seq=%d,seq=%d.\r\n", + h->nlmsg_pid,rtnl->local.nl_pid,h->nlmsg_seq,seq); continue; } if (h->nlmsg_type == NLMSG_ERROR) { + printf("nltalk recv a ack msg.\r\n"); struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h); int error = err->error; @@ -518,9 +522,7 @@ next: } if (answer) - { *answer = (struct nlmsghdr *)buf; - } else free(buf); @@ -538,6 +540,7 @@ next: status -= NLMSG_ALIGN(len); h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len)); + printf("goto next nlmsghdr"); } free(buf); @@ -607,6 +610,8 @@ int pdelivnl_listen(int group, void *jarg) { struct upmnl_handle *nl = NULL; + + printf("pdelivnl_listen:.\r\n"); nl = commnl_get_handle(NETLINK_PDELIVERY,group); if(nl == NULL) @@ -635,7 +640,7 @@ int conmnl_listen(struct upmnl_handle *nl, char buf[16384]; char cmsgbuf[BUFSIZ]; - printf("pdelivnl_listen begin.\r\n"); + printf("conmnl_listen begin.\r\n"); #if 0 if (nl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) { @@ -651,12 +656,13 @@ int conmnl_listen(struct upmnl_handle *nl, printf("pdelivnl_listen recvmsg....\r\n"); iov.iov_len = sizeof(buf); - status = recvmsg(nl->fd, &msg, MSG_WAITALL); + //status = recvmsg(nl->fd, &msg, MSG_WAITALL); + status = recvmsg(nl->fd, &msg, MSG_WAITFORONE); if (status < 0) { if (errno == EINTR || errno == EAGAIN)/*超时*/ continue; - fprintf(stderr, "netlink receive error %s (%d)\n", + fprintf(stderr, "netlink receive error: %s (%d)\n", strerror(errno), errno); if (errno == ENOBUFS) continue; @@ -779,3 +785,64 @@ int pdeliv_main(pdelivnl_listen_filter_t process_pkt) return 0; } + + +void nl_debugfs(struct nlmsghdr *n) +{ + struct netlink_debugfs *debugfs_statis =NULL; + int msg_len = 0; + + debugfs_statis = (struct netlink_debugfs *)NLMSG_DATA(n);/*msg data*/ + msg_len = (n)->nlmsg_len - NLMSG_HDRLEN; /*msg_len*/ + + printf("******debugfs_statis->cfg_rev_succ = %d******\n",debugfs_statis->cfg_rev_succ); + printf("******debugfs_statis->cfg_rev_fail = %d******\n",debugfs_statis->cfg_rev_fail); + printf("******debugfs_statis->pde_rev_succ = %d******\n",debugfs_statis->pde_rev_succ); + printf("******debugfs_statis->pde_rev_fail = %d******\n",debugfs_statis->pde_rev_fail); + printf("******debugfs_statis->pde_pkt_succ = %d******\n",debugfs_statis->pde_pkt_succ); + printf("******debugfs_statis->pde_pkt_fail = %d******\n",debugfs_statis->pde_pkt_fail); + + return; +} + + +void nl_debugfs_extack(struct nlmsghdr *n) +{ + //struct netlink_debugfs *debugfs_statis =NULL; + char * rcv_cookies = NULL; + int msg_len = 0; + struct nlmsgerr *errmsg = NULL; + struct rtattr * cookies = NULL; + int cookies_len = 0; + char * ptr; + + printf("nl_debugfs_extack said:\r\nmsgtype=%d,msglen=%d\r\n",n->nlmsg_type,n->nlmsg_len); + + errmsg = (struct nlmsgerr *)NLMSG_DATA(n);/*msg data*/ + msg_len = (n)->nlmsg_len - NLMSG_HDRLEN; /*msg_len*/ + + printf("ack result=%d,ack msgtype:%d\r\n",errmsg->error,errmsg->msg.nlmsg_type); + + if(errmsg->error == 0)/*ack result is SUCESS*/ + { + ptr = (char * )errmsg + sizeof(struct nlmsgerr); + cookies = (struct rtattr *)ptr; + //if(NLMSGERR_ATTR_COOKIE == cookies->rta_type) + { + cookies_len = cookies->rta_len; + + rcv_cookies = RTA_DATA(cookies); + printf("nl_debugfs_extack rcv cookies:%s.\r\n",rcv_cookies); + + } + + + } + else{ + printf("nl_debugfs kernel said fail,error =%d.\r\n",errmsg->error); + } + + + return; +} + diff --git a/Platform/user/netlink_uapi/libnetlinku.h b/Platform/user/netlink_uapi/libnetlinku.h index 0bf8a05bd..6ca254e8c 100644 --- a/Platform/user/netlink_uapi/libnetlinku.h +++ b/Platform/user/netlink_uapi/libnetlinku.h @@ -10,7 +10,7 @@ with attribute type. */ -#define COMMNL_TALKRCV_TIMEOUT (1) /*超时时间10毫秒*1次*/ +#define COMMNL_TALKRCV_TIMEOUT (2) /*超时时间10毫秒*1次*/ struct rtattr { unsigned short rta_len; @@ -156,6 +156,7 @@ int commnl_talk(struct upmnl_handle *nl, struct nlmsghdr *n, struct nlmsghdr **answer); int commcfg_talk(struct nlmsghdr *n, struct nlmsghdr **answer); +int pdeliv_talk(int group,struct nlmsghdr *n, struct nlmsghdr **answer); /****************************************************************/ /*函数功能:发送配置消息,不等待内核应答。*/ @@ -165,19 +166,12 @@ int commcfg_talk(struct nlmsghdr *n, struct nlmsghdr **answer); /*输出参数: */ /*返回值:0成功;< 0,失败 */ /****************************************************************/ -int commnl_send(struct upmnl_handle *nl, struct nlmsghdr *n); +int commcfg_send(struct nlmsghdr *n); +int pdelivnl_send(int group,struct nlmsghdr *n); int pdeliv_main(pdelivnl_listen_filter_t process_pkt); -struct netlink_debugfs { - unsigned int cfg_rev_succ; - unsigned int cfg_rev_fail; - - unsigned int pde_rev_succ; - unsigned int pde_rev_fail; - - unsigned int pde_pkt_succ; - unsigned int pde_pkt_fail; - }; +void nl_debugfs(struct nlmsghdr *n); +void nl_debugfs_extack(struct nlmsghdr *n); diff --git a/Platform/user/pdeliv_u/pdelivery_main.c b/Platform/user/pdeliv_u/pdelivery_main.c index de67c5be3..f2ddb7962 100644 --- a/Platform/user/pdeliv_u/pdelivery_main.c +++ b/Platform/user/pdeliv_u/pdelivery_main.c @@ -12,46 +12,70 @@ struct upmnl_handle cfgnlh; -int set_user_policy() +#ifdef NLDEBUG_ACK_COOKIES +#define PDELIV_DEBUG_ACK_COOKIES +#endif + +int set_pdeliv_debug_waitack() { unsigned char debug[]={"17"};/*需要发送的数据*/ int debug_len = strlen((const char *)debug); + struct nlmsghdr *ack = NULL; + struct nlmsghdr **answer = &ack; struct { struct nlmsghdr n; - struct policyconfmsg ncm; char buf[1024]; } req = { - .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct policyconfmsg)), + .n.nlmsg_len = NLMSG_LENGTH(0), +#ifdef PDELIV_DEBUG_ACK_COOKIES .n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK, - .n.nlmsg_type = COMMNMSG_DEBUGFS, - .ncm.policy_id =17, +#else + .n.nlmsg_flags = NLM_F_REQUEST, +#endif + .n.nlmsg_type = NLMSG_PDELIV_DEBUGFS, .n.nlmsg_pid = getpid(), }; - struct nlmsghdr **debugfs_nl; - struct pdeliv_debugfs *debugfs_statis; - struct nlmsghdr *recv_hdr; + /*可选属性*/ - commnl_addattr_l(&req.n, sizeof(req), POLICYCONFA_SRCIP, debug, debug_len); + commnl_addattr_l(&req.n, sizeof(req), NLDEBUG_MSG_TYPE, debug, debug_len); /*发送组装好的netlink消息*/ - if (pdeliv_talk(1, &req.n, NULL) < 0) + if (pdeliv_talk(1, &req.n, answer) < 0) { + printf("set_user_policy_waitack rcv ack msg faild.\r\n"); return -2; } -/* + else{ + printf("set_user_policy_waitack rcv ack msg sucess.\r\n"); + } - if(recv_hdr->nlmsg_type == COMMNMSG_DEBUGFS) - { - debugfs_statis = NLMSG_DATA(recv_hdr); - printf("******debugfs_statis->pde_fail = %d******\n",debugfs_statis->pde_fail); - printf("******debugfs_statis->pde_succ = %d******\n",debugfs_statis->pde_succ); - printf("******debugfs_statis->rev_fail = %d******\n",debugfs_statis->rev_fail); - printf("******debugfs_statis->rev_succ = %d******\n",debugfs_statis->rev_succ); + if(*answer != NULL) + { + printf("set_user_policy_waitack rcv answer.\r\n"); + + } + else{ + printf("set_user_policy_waitack rcv answer error.\r\n"); + return -3; } + + +#ifdef PDELIV_DEBUG_ACK_COOKIES + /*recv answer*/ + if((*answer)->nlmsg_type == NLMSG_ERROR){ + nl_debugfs_extack(*answer); + } +#else + /*recv answer*/ + if((*answer)->nlmsg_type == NLMSG_PDELIV_DEBUGFS) + { + nl_debugfs(*answer); + } +#endif + -*/ return 0; } @@ -71,10 +95,10 @@ int main( int argc, char **argv) } /*下发配置到内核态*/ - ret = set_user_policy(); + ret = set_pdeliv_debug_waitack(); if(ret < 0) { - printf("set_user_policy failed.\r\n"); + printf("set_pdeliv_debug_waitack failed.\r\n"); return -1; }