关于把C程序连接到核心的设置, --核心开发入门

关于把C程序连接到核心的设置
作者: xie_minix
1.设置文件/sys/arch/i386/conf/Makefile
(下面以把sample.c链入核心)
在OBJS=...后加入sample.o,在CFLIG=...处加入源文件路径 $S/net/sample.c。
当然这里假定sample.c是放在/sys/net目录下的。
还有在对象文件和源文件对应的说明中也要加入该描述。
即以下两行说明:sample.o: $S/net/sample.c
${NORMAL_C}
2.sample.c的设计规范
必须包括以下头文件:
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <machine/cpu.h>
以上头文件是在编网络相关核心例程必须要包括的。且必须按照顺序。
在进行网络底层设计时还要包括一些相关的头文件。对于我们要进行的sample.c需要有以下头文件:
#include <net/if.h>
#include <net/if_types.h>
#include <net/if_llc.h>
#include <net/route.h>
#include <net/netisr.h>
#include <net/sample.h> /*这是自己的头文件,可以把自己需要的一些结构或预定义放入到该文件中*/
现在我们用sample.c来实现一个简单的监测网络数据包接收总数的统计。
/* /sys/net/sample.c 核心内实现简单监测packet进入的数量 */
/* 作者:xie_minix */
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <machine/cpu.h>

#include <net/if.h>
#include <net/if_types.h>
#include <net/if_llc.h>
#include <net/route.h>
#include <net/netisr.h>
#include <net/sample.h>

int sample(ifp)
struct ifnet *ifp;
{
return (int)(ifp->if_ipackets); /*ifp是网络适配器的核心数据结构,内记录了各统计数据,if_ibytes是该网卡的input packets总数*/
}
整个函数功能只是返回该网卡的接收包数。
下面是sample.h头文件,在头文件中必须申明该函数的原型。
/* /sys/net/sample.h */
/* 作者:xie_minix */
#ifndef _NET_SAMPLE_H_
#define _NET_SAMPLE_H_

#ifdef _KERNEL
int sample(struct ifnet *);
#endif
#endif
该文件只是简单的对在核心内使用的函数做了一个申明。
要调用该函数,必须知道ifnet.ifnet在/sys/net/if_ethersubr.c中比较常用。因此我们可以考虑在此调用该函数。
注:if_ethersubr.c是以太网通用代码。
要引用sample(ifp)函电数,必须在if_ethersubr.c中加入头文件:
#include <net/sample.h>
调用可以安排在ether_input()函数中,比如放到判断网卡是否激活后面。即:
if ((ifp->if_flags & IFF_UP)==0) {
m_freem(m);
return;
}
在下面加入:
tmp_sample=sample(ifp);
printf("device %c input packets:%d",ifp->if_xname,tmp_sample);
当然临时变量tmp_sample在input_ether()函数内要先申明。
即:
ether_input(ifp,eh,m)
{... 其他的申明变量
int tmp_sample; /*只要此行就行了。*/
...
这只是例子。其实要得到进入的包数,可以直接printf("device %c input packets:%d",ifp->if_xname,ifp->if_ipackets);
就能取得包进入的数量。
此外,顺便讲一下编译核心。
以i386结构体系为例:
cd /sys/arch/i386/conf
config 你的设置文件
cd ../compile/你的设置文件
make depend
make
mv /netbsd /netbsd.old 这是netbsd. mv /bsd /bsd.old 这是openbsd
mv netbsd /netbsd 或 mv bsd /bsd 和上一行一样
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值