FSL 1588 PTPD简要分析!

来源:互联网 发布:worktile mac版 编辑:程序博客网 时间:2024/06/05 22:59
转载:http://blog.chinaunix.net/uid-20721651-id-4334175.html
1588v2协议目前主要都是基于ptpd开源代码实现。下面是ptpd开源代码中的主要文件介绍:
arith.c
  Time format conversion routines and additional math functions 
bmc.c
Best master clock selection code 
constants.h
Default values and constants used in ptpdv2 
datatypes.h
Main structures used in ptpdv2 
display.c
General routines for displaying internal data 
fsl_1588.c
Adds Freescale QorIQ 1588 HW assist support in PTPd 
fsl_1588.h
Adds Freescale QorIQ 1588 HW assist support in PTPd 
protocol.c
The code that handles the IEEE-1588 protocol and state machine 
ptpd.c
The main() function for the PTP daemon 

dep/constants_dep.h
Plateform-dependent constants definition 
dep/datatypes_dep.h
Implementation specific datatype 
dep/msg.c
Functions to pack and unpack messages 
dep/net.c
Functions to interact with the network sockets and NIC driver 
dep/ptpd_dep.h
External definitions for inclusion elsewhere 
dep/servo.c
Code which implements the clock servo in software 
dep/startup.c
Code to handle daemon startup, including command line args 
dep/sys.c
Code to call kernel time routines and also display server statistics 
dep/timer.c
The timers which run the state machine 

ptpd.c很简单,在main function,启动ptp engine,定义ptpclock全局变量;
startup.c 主要处理启动时用户输入的相关配置选项;
net.c 很关键,初始化相关socket,收发相关的网络数据;(这个文件对于想学习linux socket编程的同学也是很好的借鉴材料)
protocol.c ptp protocol engine;

protocol() in protocol.c, ptp 1588 states machine;
在协议初始化完成以后,由doState()来完成每个协议阶段的处理。我们主要关注slave模式下的处理,handle(),处理1588报文的通用接口。
首先在handle中要监听端口,收包,其中有两个socket,event and general使用的select系统接口;

在select返回以后,使用netRecvEvent来收1588报文,先收event and general,两种报文不可能同一时刻到达,所以,收到的报文save在ptpClock结构中的msgIbuf中。
这里的netRecvEvent接口,如果使用硬件协助的方式来实现,是需要单独实现的。在fsl 1588中实现比较关键的一块是获取报文中的相关信息,然后调用硬件驱动fsl_1588_get_rx_time,获取报文接收时间。
general or event的报文收到后,开始处理报文,msgUnpackHeader,处理ptp报文相关报头信息。然后根据报头中的messageType分类处理报文。
到此,handle处理完毕。

检查timer是否expired,如果DelayReq timeout,开始处理DelayReq报文。在issueDelayReq中,会获取当前时间,将该事件pack进报文中,然后调用netSendEvent发送,注意这里的获取时间的操作,如果硬件支持是要切换到硬件模式的。我觉得这里有个问题,不应该在这里把硬件时间pack进报文,而是把空间预留出来在mac发包时,打上timestamp。

内核中打开config timestamp的选项,那么内核在处理1588报文是会将timestamp保存出来,供ptpd来使用。
内核中保存了这些timestamp了,但是是如何通过系统调用索引到的,这个机制待后面分析。
0 0
原创粉丝点击