NTP_simulation,NTP开放源码 的模拟配置问题,官方描述不正确

所用的ntp 开发包是 ntp-dev-4.2.7p56

一、问题描述

       NTP的模拟一直想用原汁原味的源代码来模拟,可惜一直卡在配置问题上,没有进展近两个星期。最终还是看代码修改了一条源代码的配置才搞定了,开源代码就是没有一个完善的文档,以及版本问题跟新系统。

        用的是ntp.conf是官方网站描述的配置释例:

# Client configuration 
    disable kernel
    server pogo
    driftfile ./ntp.drift
    statsdir ./ntpstats/
    filegen loopstats type day enable
    filegen peerstats type day enable

    # Simulation configuration
    simulate {
        simulation_duration = 86400
        beep_delay = 3600

        # Server 1
	server = louie.udel.edu {
	    server_offset = 0 
            duration = 50000 {
		freq_offset = 400
		wander = 1.0
		jitter = 0.001
		prop_delay = 0.001
		proc_delay = 0.001
	    }
            duration = 6400 {
		freq_offset = 200
		wander = 1.0
		jitter = 0.001
		prop_delay = 0.001
		proc_delay = 0.001
	    }
	}

        # Server 2
	server = baldwin.udel.edu {
	    server_offset = 0.02
	    duration = 10000 {
		freq_offset = 400
		wander = 1.0
		jitter = 0.001
		prop_delay = 0.5
		proc_delay = 0.001
	    }
	    duration = 60000 {
		freq_offset = 200
		wander = 1.0
		jitter = 0.05
		prop_delay = 0.005
		proc_delay = 0.001
	    }
	}
 }

存为/etc/ntp.conf,运行 ./ntpdsim后有如下提示:
18 Dec 16:38:41 ntpdsim[4867]: proto: precision = 0.200 usec
18 Dec 16:38:41 ntpdsim[4867]: line 3 column 5 syntax error, unexpected T_Server, expecting T_EOC
Configuring Simulator...
Some ntpd-specific commands in the configuration file will be ignored.
ERROR!! I couldn't find a "simulate" block for configuring the simulator.
	Check your configuration file.

也就是说server 后面缺少一个结束符。

二、解决办法

1)检查关键字是否正确

    开始怀疑 是不是 sever 写错了,因该写成T_Server

     阅读ntp_keyword.h中 keyword_text[181], T_EOC对应于NULL。T_Server 对应于server 字符串,T_Sim_Duration对应于 "simulation_duration",因此那些token对应的字符串可以在keyword_text中找到。同时,keyword-gen.c是生存ntp_keyword.h的原文件,struct key_tok  ntp_keywords[] 将Token和对应的脚本文件变量字符串 一一对应。


struct key_tok ntp_keywords[] = {
{ "...",                T_Ellipsis,             FOLLBY_TOKEN },
{ "allpeers",           T_Allpeers,             FOLLBY_TOKEN },
{ "automax",            T_Automax,              FOLLBY_TOKEN },
{ "broadcast",          T_Broadcast,            FOLLBY_STRING },
{ "broadcastclient",    T_Broadcastclient,      FOLLBY_TOKEN },
{ "broadcastdelay",     T_Broadcastdelay,       FOLLBY_TOKEN },
……
/* simulator commands */
{ "simulate",           T_Simulate,             FOLLBY_TOKEN },
{ "simulation_duration",T_Sim_Duration,         FOLLBY_TOKEN },
{ "beep_delay",         T_Beep_Delay,           FOLLBY_TOKEN },
{ "duration",           T_Duration,             FOLLBY_TOKEN },
{ "server_offset",      T_Server_Offset,        FOLLBY_TOKEN },
{ "freq_offset",        T_Freq_Offset,          FOLLBY_TOKEN },
{ "wander",             T_Wander,               FOLLBY_TOKEN },
{ "jitter",             T_Jitter,               FOLLBY_TOKEN },
{ "prop_delay",         T_Prop_Delay,           FOLLBY_TOKEN },
{ "proc_delay",         T_Proc_Delay,           FOLLBY_TOKEN },
};
     验证关键字是正确的,没有错。

2) T_EOC的纠正

     在ntp_scanner.c中,有一个is_EOC函数。来判断是否是T_EOC类型。

static int
is_EOC(
        int ch
        )
{
        if ((old_config_style && (ch == '\n')) ||
            (!old_config_style && (ch == ';')))
                return 1;
        return 0;
}

   而在 ntp_config.c 中

int old_cofig_style==1;

  也就是在配置文件中\n相当于T_EOC,可是一直解析不对

   将old_config_style=0,即启用 ;作为T_EOC类型。并修改配置文件:


       #client configuration 
    disable kernel;
    server pogo;
    driftfile ./ntp.drift;
    statsdir ./ntpstats/;
    filegen loopstats type day enable;
    filegen peerstats type day enable;

    # Simulation configuration
    simulate {

        simulation_duration=86400;
        beep_delay = 3600;
       # Beep_Delay = 3600 EOC

    server = 10.2.2.23 {
            server_offset = 0;
            duration = 50000 {
                freq_offset = 400;
                wander = 1.0;
                jitter = 0.001;
                prop_delay = 0.001;
                proc_delay = 0.001;
            }
            duration = 6400 {
                freq_offset = 200;
                wander = 1.0;
                jitter = 0.001;
                prop_delay = 0.001;
                proc_delay = 0.001;
            }
        }

        # Server 2
        server = baldwin.udel.edu {
            server_offset = 0.02;
            duration = 10000 {
                freq_offset = 400;
                wander = 1.0;
                jitter = 0.001;
                prop_delay = 0.5;
                proc_delay = 0.001;
            }
            duration = 60001 {
                freq_offset = 200;
                wander = 1.0;
                jitter = 0.05;
                prop_delay = 0.005;
                proc_delay = 0.001;
            }
        }
    }
最后得到如下正确输出:

 

Received packet from 128.4.1.24 on 127.0.0.1
WARNING: e->time 81169 comes before sim_time 81170 (gap -0.982291)
WARNING: e->time + gap 81168 comes before local_time 81171
Received packet from 10.2.2.23 on 127.0.0.1
WARNING: e->time 81202 comes before sim_time 81203 (gap -0.982835)
WARNING: e->time + gap 81201 comes before local_time 81204
Received packet from 128.4.1.24 on 127.0.0.1
WARNING: e->time 81233 comes before sim_time 81234 (gap -0.982523)
WARNING: e->time + gap 81232 comes before local_time 81235
Received packet from 10.2.2.23 on 127.0.0.1
WARNING: e->time 81266 comes before sim_time 81267 (gap -0.981338)
WARNING: e->time + gap 81265 comes before local_time 81268
Received packet from 128.4.1.24 on 127.0.0.1
WARNING: e->time 81299 comes before sim_time 81300 (gap -0.982363)
WARNING: e->time + gap 81298 comes before local_time 81301
Received packet from 10.2.2.23 on 127.0.0.1
WARNING: e->time 81333 comes before sim_time 81334 (gap -0.981446)
WARNING: e->time + gap 81332 comes before local_time 81335


  终于成功了,看来还是要自己研究代码了!


你可能感兴趣的:(NTP_simulation,NTP开放源码 的模拟配置问题,官方描述不正确)