1、刚工作时做Linux 流控;后来做安全操作系统;再后来做操作系统加固;现在做TCP 加速。唉!没离开过类Unix!!!但是水平有限。。
全部博文(353)
分类:
2012-03-09 13:28:10
原文地址:libxenctrl 作者:scorpioyu
Ø 简介
libxenctrl是一个C库,它提供了一些简单易用的API,使用户程序可以方便的和Hypervisor进行通信。
它的工作原理很简单,封装了dom0中的/proc/xen/privcmd,/dev/xen/evtchn以及/dev/xen/gntdev提供的IOCTL接口。
Ø 编程步骤:
² /proc/xen/privcmd
1. xc_interface_open()
打开/proc/xen/privcmd,获得文件描述符。
2. xc_sysctl()
填充xen_sysctl结构体,发送ioctl请求。
3. xc_interface_close()
关闭1中打开的文件描述符。
² /dev/xen/evtchn
1. xc_evtchn_open
2. xc_evtchn_notify等函数
evtchn的操作比较简单,不需要填充结构体,一般指明远程Dom id,port即可
3. xc_evtchn_close
² /dev/xen/gntdev
1. xc_gnttab_open
2. xc_gnttab_map_grant_ref等
3. xc_gnttab_close
Ø 编程示例
以/proc/xen/privcmd提供的接口为例,节选自tools/xenmon/setmask.c
int main(int argc, char * argv[])
{
struct xen_sysctl sysctl;
int ret;
int xc_handle = xc_interface_open();
sysctl.cmd = XEN_SYSCTL_tbuf_op; // 由cmd决定sysctl(struct xen_sysctl)中的union
sysctl.interface_version = XEN_SYSCTL_INTERFACE_VERSION;
sysctl.u.tbuf_op.cmd = XEN_SYSCTL_TBUFOP_get_info;
ret = xc_sysctl(xc_handle, &sysctl);
if ( ret != 0 )
{
perror("Failure to get event mask from Xen");
exit(1);
}
else
{
printf("Current event mask: 0x%.8x\n", sysctl.u.tbuf_op.evt_mask);
}
...snip...
xc_interface_close(xc_handle);
return 0;
}
² XEN_SYSCTL Command (IOCTL的主命令为IOCTL_PRIVCMD_HYPERCALL)
No. |
Command |
Comment |
Struct |
1 |
XEN_SYSCTL_readconsole |
Read console content from Xen buffer ring. |
xen_sysctl_readconsole |
2 |
XEN_SYSCTL_tbuf_op |
Get trace buffers machine base address |
xen_sysctl_tbuf_op |
3 |
XEN_SYSCTL_physinfo |
Get physical information about the host machine |
xen_sysctl_physinfo |
4 |
XEN_SYSCTL_sched_id |
Get the ID of the current scheduler. |
xen_sysctl_sched_id |
5 |
XEN_SYSCTL_perfc_op |
Interface for controlling Xen software performance counters. |
xen_sysctl_perfc_op |
6 |
XEN_SYSCTL_getdomaininfolist |
- |
xen_sysctl_getdomaininfolist |
7 |
XEN_SYSCTL_debug_keys |
Inject debug keys into Xen. |
xen_sysctl_debug_keys |
8 |
XEN_SYSCTL_getcpuinfo |
Get physical CPU information. |
xen_sysctl_getcpuinfo |
9 |
XEN_SYSCTL_availheap |
- |
xen_sysctl_availheap |
10 |
XEN_SYSCTL_get_pmstat |
- |
xen_sysctl_get_pmstat |
11 |
XEN_SYSCTL_cpu_hotplug |
- |
xen_sysctl_cpu_hotplug |
12 |
XEN_SYSCTL_pm_op |
Get/set xen power management, include cpufreq governors and related parameters |
xen_sysctl_page_offline_op |
13 |
- |
- |
- |
14 |
XEN_SYSCTL_page_offline_op |
- |
xen_sysctl_page_offline_op |
15 |
XEN_SYSCTL_lockprof_op |
- |
xen_sysctl_lockprof_op |
struct xen_sysctl {
uint32_t cmd;
uint32_t interface_version; /* XEN_SYSCTL_INTERFACE_VERSION */
union {
struct xen_sysctl_readconsole readconsole;
struct xen_sysctl_tbuf_op tbuf_op;
struct xen_sysctl_physinfo physinfo;
struct xen_sysctl_sched_id sched_id;
struct xen_sysctl_perfc_op perfc_op;
struct xen_sysctl_getdomaininfolist getdomaininfolist;
struct xen_sysctl_debug_keys debug_keys;
struct xen_sysctl_getcpuinfo getcpuinfo;
struct xen_sysctl_availheap availheap;
struct xen_sysctl_get_pmstat get_pmstat;
struct xen_sysctl_cpu_hotplug cpu_hotplug;
struct xen_sysctl_pm_op pm_op;
struct xen_sysctl_page_offline_op page_offline;
struct xen_sysctl_lockprof_op lockprof_op;
uint8_t pad[128];
} u;
};