内核基于netlink的内核态与用户态异步并发消息处理模型

chrome ie内核  时间:2021-01-25  阅读:()

基于 tlink的内核态与用户态异步并发消息处理模型

用户态采用sl c模型初始化时建立多个  linksocket,绑定完成之后,向内核发送握手消息,这样内核可以将已经建立的连接记住,以便后续选择可用的连接发送数据。初始化和握手完成之后,由内核主动向用户态发送数据,用户态主线程在各个so cet句柄上面等待读事件的到来,当检测到读事件时,向线程池提交数据读取和处理任务。这样模拟一个连接池和事件分发模型,保证内核数据及时被读取到用户态程序并处理,能做到并发。

而内核态的etlink在接收数据时本身就是以系统调用的方式提供给业务层的发送接口,因此本身就是异步的,性能不是问题。内核态收到数据时,只需要提交给一个内核线程去处理即可。

原型代码如下:共用头文件

#dfneE TLINK_TET  

#efineAX_D AA_LEN(76  #defn 

AX_PROCSS_COUNT 10#efie MAX_PYL

D1024

d ineAX_PID_CUNT MAX_PROSSCOUN

#dfine MAX_R_DALEN 102deine S

TIC_PEOD 1 24*5 0#e ine MSG_CO

1000#d ine TRE 1用户态

#include

#incl e

nlude

#ncld e

#iclue

#in ude

#iclud

#inlude asm/tpe  .h

#include

#include

  lu de

inc ude

#include "conf.h"struct ndpo it

{unsigned nt pi;

 trct socka r_l s c_add ;stru c  sockadd_nl d  taddr;int ok_f 

st   t mgd mg;

} *endoits=N; nt  is  en_s  (voi);

nt handwitknl(vid)it lose_u er   id);int doslect st uct timeal wai  );

oid* hreadPoc(oid*ed);v  * sendTreadProc(vid*arg);void fluhcount  n ln)void stt  csut(void);vi  tart  dThread oi);

 tat   ntaf =0

 tati  fd_set r  et;

 ta  c strct t evl tout;unsined ch readbffer;sta ic struct time l tno;s  ti   r   timevl oldtimes a ic lon  ccunt =   ptheadmute_tmtex;sta i  long  abtes= 0;stat  c lo  radnum0;static long  endby e  0;int main(in rgc,chr*arg[]

  po    = struc  endpoi  *)mlloc

sizeof(   ruct ndpoint) *AXPD_CNT);reabfer (unsiged har*)mallo c(AX_E_DATA_LEN ; / rate an netlink ocet andbnd.listenuser( if(thred_utex_n t(&mut ,NUL)  =0)

pr ntf   Init mtu err \");r  rn -1 ;

} /  en a han hae msg o thenl. let the knl to  ee       int.

 ndwit l( ; /   eep(6) // art SenThead   ; get  imef y(&oldtime,NUL)tmou .t_sec=1 

m t.tv_usec=0

/ ait or v t fom th knl to d  ach.

h  e (1)

{

o s le ct(tmout);  f(  ab

tes>STATIC_PROID)

{stti   ut );

} //  le (2);

} /close the soc   .

lo _u ser(return 0;

}int lis  en_ser(void)

{int pidont;

 truct edp nt*ed =NULL;

 t uct lmgdr*  hNLL;unsign  int pid =  tpid ); o  idcunt=   pi d unt MAX_PD_CONT i dcut+)

{

  endpoits+ i dont;memst((vo  d* e,   sizef(str t edoin )); d-> ckd =socket(AF_NETLIN,SOC_RW,TLIN_TST);e-id=    +pid unt;i  (0=ed->  d)

{pi = ++ed->pid;

} e d->sr c_ddr.lfamil y

AF_NE TINK;e d->sr c_addr.nl_groups =  ;ed->src_addr.l_p  ed->pid; /

TDO my e th srcddr.nl_i i  alredyin used, te bid w  l eturn a onzr vau .b d(ed->so ckfd, (struc  s kad  )&ed->sr _adr, si o(ed-> r_ddr ;ed->de stdr.l_fm y AFE TINK;e->dest_a r.lp d= 0;ed>dest_dd  l_goups=  ;

/ F ll in the netlink messge pyload /ed>msg.msg_name= (voi *)  ->dest_ddr

ed->m . _namlen s  eof( d>estad  ; printf("  it the  cket%d o p d%d s  ces   l.\n" ed->soc_ded->pid)u l ep(10000);

} retrn 0 }int ha w thknl vo  d{it pdont;st  ct ndpoint*ed=NULs  ruct nlsghdr l NU; or(idcont=0;pi dcount ! AX_P _CTpidcount++)

{

   endoits+ idcount;

d>msg.mg_iovle =1 ;ed>msg.msgiv=mllo (sizef(strutio c )

d->mg.msg_iov->ov_base= mal o(NLMS G_SPAE MAX_PAYLOA));nlh (  truct nlms gdr*e-m .ms giov->iovas ;

lh->nlmsg_le =LMS_SPA(A_PAY LO AD);nl-nl mg_p  =e d->pi;

lh->nlms g_flag 0;e->g.msg_ ov->iov_ln=nlh->nlmsg_ en;

snprintf( chr)NM S_T(lh ,MAX_PAYO AD 1, "ell kl! Thi  i s%d!"ed->pid);   printf "  ning messaero.  . .\n", d>pi  ;se msg(ed>sok_f,&ed->ms,0); f (e>soc_d>mxfd)

{

xfde-sock_d;

} F D_SE T ed->oc_fd &set);//  hrea_t  i

   f(0 =pthr ea dcrae(&ti,NLL &th aProc, (void*)    )

// {

// rint("cre ate  thrad % successfl or th  d: %. n",  id,ed->pid);

// 

 e urn  ;

}in l  e_user vid

nt picout; for(idcount  0;pidot  =MAXP OUN; icont++)

{

 o e(enpoits  id  nt]. ok_f);

} r etr  

 int o e  et(struc   ie   it)

{int picunt;int selcount0;stru  ed it*e d NL;

 trut lmshdr*nlh =NLL;  l cout=se  ct(mxd+1,&  t NLL,NLL, &wai ) if se  count=0)

{ret n 0

}els if sel on   

{printf("se  eted error!\n");re urn-1 

}e  se

{for(p d u  =  ; pid  nt !=

ZJI:台湾CN2/香港高主频服务器7折每月595元起,其他全场8折

ZJI原名维翔主机,是原来Wordpress圈知名主机商家,成立于2011年,2018年9月更名为ZJI,提供香港、日本、美国独立服务器(自营/数据中心直营)租用及VDS、虚拟主机空间、域名注册业务。ZJI今年全新上架了台湾CN2线路服务器,本月针对香港高主频服务器和台湾CN2服务器提供7折优惠码,其他机房及产品提供8折优惠码,优惠后台湾CN2线路E5服务器月付595元起。台湾一型CPU:Inte...

HostYun(25元)俄罗斯CN2广播IP地址

从介绍看啊,新增的HostYun 俄罗斯机房采用的是双向CN2线路,其他的像香港和日本机房,均为国内直连线路,访问质量不错。HostYun商家通用九折优惠码:HostYun内存CPUSSD流量带宽价格(原价)购买地址1G1核10G300G/月200M28元/月购买链接1G1核10G500G/月200M38元/月购买链接1G1核20G900G/月200M68元/月购买链接2G1核30G1500G/月...

特网云,美国独立物理服务器 Atom d525 4G 100M 40G防御 280元/月 香港站群 E3-1200V2 8G 10M 1500元/月

特网云为您提供高速、稳定、安全、弹性的云计算服务计算、存储、监控、安全,完善的云产品满足您的一切所需,深耕云计算领域10余年;我们拥有前沿的核心技术,始终致力于为政府机构、企业组织和个人开发者提供稳定、安全、可靠、高性价比的云计算产品与服务。公司名:珠海市特网科技有限公司官方网站:https://www.56dr.com特网云为您提供高速、稳定、安全、弹性的云计算服务 计算、存储、监控、安全,完善...

chrome ie内核为你推荐
金士顿内存卡真假怎么看金士顿内存卡真伪?租车平台哪个好手机租赁平台哪个好?自然吸气和涡轮增压哪个好汽车涡轮增压好还是自然吸气好苹果x和xr哪个好苹果x,苹果xs,苹果xr,苹果xs max哪个更值得买?炒股软件哪个好股票软件哪个最好用?手机浏览器哪个好用手机哪个浏览器最好用美国国际东西方大学出国留学,美国“野鸡大学”有哪些?51空间登录51空间怎么进qq空间登录器怎样直接登录QQ空间qq空间登录电脑手机上怎么登陆电脑版QQ空间
海外服务器租用 山东vps 双线vps 免费cn域名 site5 t楼 vpsio 网络星期一 回程路由 个人免费主页 万网主机管理 便宜空间 沈阳主机托管 cxz 云服务器比较 qq金券 阿里云邮箱怎么注册 小夜博客 ipower wannacry勒索病毒 更多