关于ptpd开源软件研究

目的及环境确认

系统为:Ubunto14.04。

目的:实现运输载体为Ethernet的p2p对时。

使用软件包:GitHub项目ptpd

具体安装

安装前准备

安装libtool:

sudo apt-get install libtool

另外由于需要用ethernet作为运输载体,所以要安装libpcap-dev:

sudo apt-get install libpcap-dev

开始安装ptpd

开始执行以下命令实现安装:

autoreconf -vi
./configure --with-pcap-config
Make

无报错就是安装成功。

两台linux机实现E2E对时

在master机执行以下命令:

./ptpd2 -M -i eth0 -C --ptpengine:transport ethernet

在salve机执行以下命令:

./ptpd2 -s -i eth0 -C --ptpengine:transport ethernet

master机输出

2019-08-09 15:56:25.472272 ptpd2[24821].startup (notice)    (___) PTPd version 2.3.2 starting
2019-08-09 15:56:25.472411 ptpd2[24821].startup (info)      (___) Starting ptpd2 daemon with parameters:      ./ptpd2 -M -i eth0 -C 
2019-08-09 15:56:25.472459 ptpd2[24821].startup (info)      (___) Checking configuration
2019-08-09 15:56:25.472899 ptpd2[24821].startup (info)      (___) Configuration OK
2019-08-09 15:56:25.473539 ptpd2[24821].startup (info)      (___) Successfully acquired lock on /var/run/ptpd2.lock
2019-08-09 15:56:25.473759 ptpd2[24821].startup (notice)    (___) PTPd started successfully on eth0 using "masteronly" preset (PID 24821)
2019-08-09 15:56:25.473800 ptpd2[24821].startup (info)      (___) TimingService.PTP0: PTP service init
2019-08-09 15:56:25.768755 ptpd2[24821].eth0 (notice)    (lstn_init) Now in state: PTP_LISTENING
2019-08-09 15:56:35.474163 ptpd2[24821].eth0 (notice)    (lstn_init) TimingService.PTP0: elected best TimingService
2019-08-09 15:56:35.474313 ptpd2[24821].eth0 (info)      (lstn_init) TimingService.PTP0: acquired clock control
2019-08-09 15:56:37.770060 ptpd2[24821].eth0 (notice)    (mst) Now in state: PTP_MASTER, Best master: 000c29fffeddb10b(unknown)/1 (self)

salve机输出:

2019-08-09 16:35:55.286089 ptpd2[29255].startup (notice)    (___) PTPd version 2.3.2 starting
2019-08-09 16:35:55.286674 ptpd2[29255].startup (info)      (___) Starting ptpd2 daemon with parameters:      ./ptpd2 -g -i eth0 -C 
2019-08-09 16:35:55.287380 ptpd2[29255].startup (info)      (___) Checking configuration
2019-08-09 16:35:55.288062 ptpd2[29255].startup (warning)   (___) Warning: Unicast negotiation cannot be used with Ethernet transport

2019-08-09 16:35:55.288715 ptpd2[29255].startup (warning)   (___) Warning: Unicast negotiation can only be used with unicast transmission

2019-08-09 16:35:55.289791 ptpd2[29255].startup (info)      (___) Configuration OK
2019-08-09 16:35:55.290792 ptpd2[29255].startup (info)      (___) Successfully acquired lock on /var/run/ptpd2.lock
2019-08-09 16:35:55.291523 ptpd2[29255].startup (notice)    (___) PTPd started successfully on eth0 using "slaveonly" preset (PID 29255)
2019-08-09 16:35:55.291695 ptpd2[29255].startup (info)      (___) TimingService.PTP0: PTP service init
2019-08-09 16:35:55.293736 ptpd2[29255].eth0 (info)      (init) Observed_drift loaded from kernel: -295187 ppb
2019-08-09 16:35:55.294629 ptpd2[29255].eth0 (notice)    (lstn_init) Now in state: PTP_LISTENING
2019-08-09 16:35:55.421635 ptpd2[29255].eth0 (info)      (lstn_init) New best master selected: 000c29fffeddb10b(unknown)/1
2019-08-09 16:35:55.422385 ptpd2[29255].eth0 (notice)    (slv) Now in state: PTP_SLAVE, Best master: 000c29fffeddb10b(unknown)/1
2019-08-09 16:35:55.422986 ptpd2[29255].eth0 (notice)    (slv) Received first Sync from Master
2019-08-09 16:35:56.428603 ptpd2[29255].eth0 (notice)    (slv) Received first Delay Response from Master

wireshark抓取报文分析

与预期一样。

  1. master机先发sync报文,紧接着发Follow_Up报文
  2. slave机发delay_Req报文。
  3. master回应delay_Req,发delay_Resp报文

对时成功。

两台linux机实现P2P对时

在master机执行以下命令:

./ptpd2 -M -i eth0 -C -P --ptpengine:transport ethernet

在salve机执行以下命令:

./ptpd2 -s -i eth0 -C -P --ptpengine:transport ethernet

wireshark抓取报文分析

master -> sync , follow_up

slave -> Path_Delay_Req

master -> Path_Delay_Resp , Path_Delay_Resp_Follow_Up

master -> announce

master -> Path_Delay_Req

master -> Sync

slave -> Path_Delay_Resp ,Path_Delay_Resp_Follow_up

master ->Follow_Up

从报文中看到,在计算链路延时的时候,主从都发一次链路延时请求。

得到的链路延时。

交叉编译

  • 不包含libpcap包的交叉编译。
. setenv
autoreconf -ifv
./configure --host=powerpc-linux-gnu

#修改makefile
#将LINUX_KERNEL_INCLUDES修改为交换机中的kernel/include路径
#/home/share/JewinH/SVN/E24G4/sdk/mpc8247/kernel/linux-2.6.25/include

#修改ptpd-master目录中的config.h文件中的一句话
#注释下面这句#define  malloc  rpl_malloc

make
#成功后,即可在交换机中跑起来。

  • 包含libpcap包的交叉编译:
把libpcap-1.9.0和ptpd-master放在与setenv同一级目录下。

. setenv

#先去编译libpcap-1.9.0,执行configure,再执行make,make install
./configure --host=powerpc-linux-gnu --with-pcap=linux --prefix=$TOP_DIR/ptpd-master/install_pcap

make
make install 

#这时在ptpd_master文件夹中,多了install_pcap文件夹

#编译ptpd,
cd ptpd_master

vi configure.ac
#修改configure.ac,64,65,70,71行
#将/usr/src/kernels/`uname -r`/include,/usr/src/linux-headers-`uname -r`/include都修改为$(KERNDIR)/include

./configure --host=powerpc-linux-gnu --with-pcap-config=$TOP_DIR/ptpd-master/install_pcap/bin/pcap-config CFLAGS=-I$TOP_DIR/ptpd-master/install_pcap/include/ --prefix=$TOP_DIR/ptpd-master/install_ptpd

#修改ptpd-master目录中的config.h文件中的一句话
#注释下面这句#define  malloc  rpl_malloc

#如果使用动态链接(.so),直接make,然后把install_pcap中的lib/libpcap.so.1.9.0拷贝到交换机的/usr/lib文件夹中,并且新建一个软连接libpcap.so.1指向lib/libpcap.so.1.9.0。

#如果使用静态链接(.a),需要先到install_pcap/lib/中,删除libpcap.so,libpcap.so.1,libpcap.so.1.9.0。

make

make install

#在当前文件夹中,会多出一个install_ptpd文件夹。在该文件夹中就有sbin/ptpd2应用程序。该程序可以在交换机中正常运行。

Debug过程

只是记录尝试的一些方法,及思考过程。

  • 无法使用libpcap:
#在ptpd开源包中,执行./configure --host=powerpc-linux-gnu --with-pcap-config时的打印信息。
#checking pcap/pcap.h usability... no,这句说明在生成的makefile不使用pcap这个库,所以make的时候,不会出现错误,但是libpcap也没有被编进去。
checking for pcap-config... /usr/bin/pcap-config
checking if we want to build with libpcap support... yes, pcap-config
checking pcap/pcap.h usability... no
checking pcap/pcap.h presence... no
checking for pcap/pcap.h... no
checking pcap.h usability... no
checking pcap.h presence... no
checking for pcap.h... no
checking if we can build with libpcap support... no

#为解决上诉问题,首先,我下载了libpcap最新版本,并且交叉编译好,安装好后,在指定pcap-config的路径,具体命令如下:
./configure --host=powerpc-linux-gnu --with-pcap-config=/home/share/JewinH/SVN/E24G4/libpcap-1.9.0/pcap-config

#错误并没有解决,依然报上诉错误。

#既然如此,我尝试修改ptpd中自动生成的makefile文件。
#首先,我对比了交叉编译与不交叉编译时,在PCAP这个变量定义部分的makefile的差别。
#我修改了部分路径,指定一些路径,发现并没有作用,依然提示无法使用。

对比两个Makefile:

无法编进的libpcap(添加了自己的修改):
PATH_PCAP_CONFIG = /home/share/JewinH/SVN/E24G4/mkimage/src/tcpdump/libpcap-1.0.0/pcap-config
PATH_SEPARATOR = :
PCAP_CFLAGS = 
PCAP_CPPFLAGS = -I/home/share/JewinH/SVN/E24G4/sdk/mpc8247/kernel/linux-2.6.25/include
PCAP_LIBS = -I/home/share/JewinH/SVN/E24G4/mkimage/src/tcpdump/libpcap-1.0.0/libpcap.a
PTP_PCAP = -DPTPD_PCAP

-lpcap -L/home/share/JewinH/SVN/E24G4/libpcap-1.9.0

成功编进去的:
PATH_PCAP_CONFIG = /usr/bin/pcap-config
PATH_SEPARATOR = :
PCAP_CFLAGS = 
PCAP_CPPFLAGS =  -I/usr/include
PCAP_LIBS = -L/usr/lib/i386-linux-gnu  -lpcap
PTP_PCAP = -DPTPD_PCAP
  • 查阅资料
#查阅资料,了解到大部分开源包的makefile都是通过autotools来自动生成的。
#./configure就是生成makefile的脚本文件,所以,我们不应该去尝试修改makefile,因为makefile里面的关系是千丝万缕的,是自动化生成工具去生成的,我们应该关注的是,./configure脚本是怎么生成makefile的。
#通过查看视频,阅读官网文档,我们知道$(CFLAGS)是一个预定义变量,通过修改这个参数,就可以在执行$(CC)时,用上这些参数。

#在ptpd目录下,./configure时,添加这些参数就可以了。
#这时,就可以保证,在编译过程中,把libpcap这些库,include进去。同时对应的宏也设置为把libpcap编进去。
CFLAGS=-I/home/share/JewinH/SVN/E24G4/ptpd-master/install_pcap/include LDFLAGS="-static"

#此时的打印信息如下:

checking if we want to build with libpcap support... yes, pcap-config
checking pcap/pcap.h usability... yes
checking pcap/pcap.h presence... no
configure: WARNING: pcap/pcap.h: accepted by the compiler, rejected by the preprocessor!
configure: WARNING: pcap/pcap.h: proceeding with the compiler's result
checking for pcap/pcap.h... yes
checking pcap.h usability... yes
checking pcap.h presence... no
configure: WARNING: pcap.h: accepted by the compiler, rejected by the preprocesso  
▽onfigure: WARNING: pcap.h: proceeding with the compiler's result
checking for pcap.h... yes
checking if we can build with libpcap support... yes

猜你喜欢

转载自blog.csdn.net/qq_38307618/article/details/124184652