网络硬时间戳在何时何地打的?

分为几个部分阐述

1、linux时间系统

2、网卡工作原理

3、socket编程里的硬件时间戳选项

4、网络硬时间戳是什么时候打?在哪儿打的?

一、linux时间系统

陈莉君《深入分析linux内核源码》一篇很不错的文章:linux时间系统

linux有两个时钟源,分别是RTC和OS时钟。

RTC独立于操作系统,由电池供电,即使系统断电它也能维护自己的时钟。LINUX系统启动时从其中获得时间初始值。

OS时钟从可编程计数器(如intel的8524)获得时钟。如图1所示的输出脉冲是OS时钟工作的基础,因为是由它产生时钟中断的。

图1 8524工作示意图

图1 时钟机制

二、网卡工作原理

发送数据时,网卡首先侦听介质上是否有载波(载波由电压指示),如果有,则认为其他站点正在传送信息,继续侦听介质。一旦通信介质在一定时间段内(称为帧间缝隙IFG=9.6微秒)是安静的,即没有被其他站点占用,则开始进行帧数据发送,同时继续侦听通信介质,以检测冲突。在发送数据期间。 如果检测到冲突,则立即停止该次发送,并向介质发送一个“阻塞”信号,告知其他站点已经发生冲突,从而丢弃那些可能一直在接收的受到损坏的帧数据,并等待一段随机时间(CSMA/CD确定等待时间的算法是二进制指数退避算法)。在等待一段随机时间后,再进行新的发送。如果重传多次后(大于16次)仍发生冲突,就放弃发送。 接收时,网卡浏览介质上传输的每个帧,如果其长度小于64字节,则认为是冲突碎片。如果接收到的帧不是冲突碎片且目的地址是本地地址,则对帧进行完整性校验,如果帧长度大于1518字节(称为超长帧,可能由错误的LAN驱动程序或干扰造成)或未能通过CRC校验,则认为该帧发生了畸变。通过校验的帧被认为是有效的,网卡将它接收下来进行本地处理。

三、socket编程里的硬件时间戳选项

参考文章:硬件时间戳socket选项解析

The existing interfaces for getting network packages time stamped are:

* SO_TIMESTAMP
  Generate time stamp for each incoming packet using the (not necessarily
  monotonous!) system time. Result is returned via recv_msg() in a
  control message as timeval_r(usec resolution).

* SO_TIMESTAMPNS
  Same time stamping mechanism as SO_TIMESTAMP, but returns result as
  timespec (nsec resolution).

* IP_MULTICAST_LOOP + SO_TIMESTAMP[NS]
  Only for multicasts: approximate send time stamp by receiving the looped
  packet and using its receive time stamp.

The following interface complements the existing ones: receive time
stamps can be generated and returned for arbitrary packets and much
closer to the point where the packet is really sent. Time stamps can
be generated in software (as before) or in hardware (if the hardware
has such a feature).

SO_TIMESTAMPING:

Instructs the socket layer which kind of information is wanted. The
parameter is an integer with some of the following bits set. Setting
other bits is an error and doesn‘t change the current state.

SOF_TIMESTAMPING_TX_HARDWARE:  try to obtain send time stamp in hardware
SOF_TIMESTAMPING_TX_SOFTWARE:  if SOF_TIMESTAMPING_TX_HARDWARE is off or
                               fails, then do it in software
SOF_TIMESTAMPING_RX_HARDWARE:  return the original, unmodified time stamp
                               as generated by the hardware
SOF_TIMESTAMPING_RX_SOFTWARE:  if SOF_TIMESTAMPING_RX_HARDWARE is off or
                               fails, then do it in software
SOF_TIMESTAMPING_RAW_HARDWARE: return original raw hardware time stamp
SOF_TIMESTAMPING_SYS_HARDWARE: return hardware time stamp transformed to
                               the system time base
SOF_TIMESTAMPING_SOFTWARE:     return system time stamp generated in
                               software

SOF_TIMESTAMPING_TX/RX determine how time stamps are generated.
SOF_TIMESTAMPING_RAW/SYS determine how they are reported in the
following control message:

struct scm_timestamping {
 struct timespec systime;
 struct timespec hwtimetrans;
 struct timespec hwtimeraw;
};

recvmsg() can be used to get this control message for regular incoming
packets. For send time stamps the outgoing packet is looped back to
the socket‘s error queue with the send time stamp(s) attached. It can
be received with recvmsg(flags=MSG_ERRQUEUE). The call returns the
original outgoing packet data including all headers preprended down to
and including the link layer, the scm_timestamping control message and
a sock_extended_err control message with ee_errno==ENOMSG and
ee_origin==SO_EE_ORIGIN_TIMESTAMPING. A socket with such a pending
bounced packet is ready for reading as far as select() is concerned.
If the outgoing packet has to be fragmented, then only the first
fragment is time stamped and returned to the sending socket.

All three values correspond to the same event in time, but were
generated in different ways. Each of these values may be empty (= all
zero), in which case no such value was available. If the application
is not interested in some of these values, they can be left blank to
avoid the potential overhead of calculating them.

systime is the value of the system time at that moment. This
corresponds to the value also returned via SO_TIMESTAMP[NS]. If the
time stamp was generated by hardware, then this field is
empty. Otherwise it is filled in if SOF_TIMESTAMPING_SOFTWARE is
set.

hwtimeraw is the original hardware time stamp. Filled in if
SOF_TIMESTAMPING_RAW_HARDWARE is set. No assumptions about its
relation to system time should be made.

hwtimetrans is the hardware time stamp transformed so that it
corresponds as good as possible to system time. This correlation is
not perfect; as a consequence, sorting packets received via different
NICs by their hwtimetrans may differ from the order in which they were
received. hwtimetrans may be non-monotonic even for the same NIC.
Filled in if SOF_TIMESTAMPING_SYS_HARDWARE is set. Requires support
by the network device and will be empty without that support.

SIOCSHWTSTAMP:

Hardware time stamping must also be initialized for each device driver
that is expected to do hardware time stamping. The parameter is defined in
/include/linux/net_tstamp.h as:

struct hwtstamp_config {
 int flags; 
 int tx_type; 
 int rx_filter; 
};

Desired behavior is passed into the kernel and to a specific device by
calling ioctl(SIOCSHWTSTAMP) with a pointer to a struct ifreq whose
ifr_data points to a struct hwtstamp_config. The tx_type and
rx_filter are hints to the driver what it is expected to do. If
the requested fine-grained filtering for incoming packets is not
supported, the driver may time stamp more than just the requested types
of packets.

A driver which supports hardware time stamping shall update the struct
with the actual, possibly more permissive configuration. If the
requested packets cannot be time stamped, then nothing should be
changed and ERANGE shall be returned (in contrast to EINVAL, which
indicates that SIOCSHWTSTAMP is not supported at all).

Only a processes with admin rights may change the configuration. User
space is responsible to ensure that multiple processes don‘t interfere
with each other and that the settings are reset.

enum {
 
 HWTSTAMP_TX_OFF,

HWTSTAMP_TX_ON,
};

enum {
 
 HWTSTAMP_FILTER_NONE,

HWTSTAMP_FILTER_ALL,

HWTSTAMP_FILTER_SOME,

HWTSTAMP_FILTER_PTP_V1_L4_EVENT,

};

DEVICE IMPLEMENTATION

A driver which supports hardware time stamping must support the
SIOCSHWTSTAMP ioctl and update the supplied struct hwtstamp_config with
the actual values as described in the section on SIOCSHWTSTAMP.

Time stamps for received packets must be stored in the skb. To get a pointer
to the shared time stamp structure of the skb call skb_hwtstamps(). Then
set the time stamps in the structure:

struct skb_shared_hwtstamps {
 
 ktime_t hwtstamp;
 ktime_t syststamp;
};

Time stamps for outgoing packets are to be generated as follows:
- In hard_start_xmit(), check if skb_tx(skb)->hardware is set no-zero.
  If yes, then the driver is expected to do hardware time stamping.
- If this is possible for the skb and requested, then declare
  that the driver is doing the time stamping by setting the field
  skb_tx(skb)->in_progress non-zero. You might want to keep a pointer
  to the associated skb for the next step and not free the skb. A driver
  not supporting hardware time stamping doesn‘t do that. A driver must
  never touch sk_buff::tstamp! It is used to store software generated
  time stamps by the network subsystem.
- As soon as the driver has sent the packet and/or obtained a
  hardware time stamp for it, it passes the time stamp back by
  calling skb_hwtstamp_tx() with the original skb, the raw
  hardware time stamp. skb_hwtstamp_tx() clones the original skb and
  adds the timestamps, therefore the original skb has to be freed now.
  If obtaining the hardware time stamp somehow fails, then the driver
  should not fall back to software time stamping. The rationale is that
  this would occur at a later time in the processing pipeline than other
  software time stamping and therefore could lead to unexpected deltas
  between time stamps.
- If the driver did not call set skb_tx(skb)->in_progress, then
  dev_hard_start_xmit() checks whether software time stamping
  is wanted as fallback and potentially generates the time stamp.


网络硬时间戳在何时何地打的?

时间: 2024-08-01 20:18:13

网络硬时间戳在何时何地打的?的相关文章

lua获取网络当前时间戳

使用luasocket local socket = require "socket.core" local server_ip = { "time-nw.nist.gov", "time-a.nist.gov", "time-b.nist.gov", "time.nist.gov", "time-c.nist.gov", "time-d.nist.gov", } f

iOS9网络适配_ATS:改用更安全的HTTPS

iOS9AdaptationTips iOS9适配系列教程[中文在页面下方] (截至2015年9月3日共有6篇,后续还将持续更新.更多iOS开发干货,欢迎关注 微博@iOS程序犭袁) For more infomation ,welcome to follow my twitter English 1. Demo1_You'd better Convert HTTP to HTTPS How to deal with the SSL in iOS9,One solution is to do l

邮件系统网络硬盘功能

1. 什么是网络硬盘? 使您拥有完全私人的.安全的因特网络存贮空间.通过网络硬盘,您可以将各种个人文件存放在互联网上,并具有和本地硬盘一样的文件操作,如删除.复制.移动.查找等.网络硬盘方便“携带”.内容易于修改.安全可靠,并容易与其他同事分享图片以及其他文件.网络硬盘就如一个网络移动硬盘,用户将需要的文件存放在网络硬盘中,只要有网络,无论身处何方,随时随地都可以取出来尽其所用.个人文件的存贮.修改.管理.共享.发送,一切都可在 Web 上轻松解决. TurboMail邮箱提供公共网络硬盘.域网

宽带网络为何又慢又贵

把一个国家的整体网络硬生生的分成电信网通两张网络,不仅没有促进任何竞争,反而人为阻碍了人们的上网体验.无外乎有网友戏称:“你从北京坐飞机去广州,必须先在长江边下飞机,再转乘马车去!” http://baozoumanhua.com/users/15587850/forum_articleshttp://baozoumanhua.com/users/15587850/followingshttp://baozoumanhua.com/users/15587850/talkingshttp://ba

门户已死?不过换个活法重头再来

在移动互联网时代,可能的风口和或许的机遇还有许多,只是首要目的就是让用户们忘记贴在身上的门户标签. 文/张书乐 刊载于<互联网经济>2016年7月刊 2016年7月25日,雅虎正式被美国电信巨头Verizon以48.3亿美元的低价收购.至此,这家开创了"内容免费.广告收费"模式的互联网先驱,在1996年上市后,经历20年,正式走向没落.这是从今年2月,雅虎首席执行官玛丽莎·梅耶尔宣布,该公司将拍卖旗下互联网业务,并将裁减15%的员工之后的"落实"结果.

Reactor模型

Reactor模型 原文地址:http://www.ivaneye.com/2016/07/23/iomodel.html 无处不在的C/S架构 在这个充斥着云的时代,我们使用的软件可以说99%都是C/S架构的! 你发邮件用的Outlook,Foxmail等 你看视频用的优酷,土豆等 你写文档用的Office365,googleDoc,Evernote等 你浏览网页用的IE,Chrome等(B/S是特殊的C/S) ...... C/S架构的软件带来的一个明显的好处就是:只要有网络,你可以在任何地

IT运维面临的3大难题及应对措施

 网络设备 在企业IT基础设施的搭建过程中,底层的网络设备厂商和类型多样且复杂.随之而来的问题是:如何将不同厂商的网络和应用管理产品在界面级.消息 级和数据级集成起来实现统一管理?如何让IT管理员了解到整个网络全局的运行情况.发展趋势和可能存在的故障隐患点,以便及时采取相应措施,实现事前管 理. 科学的运维管理思路告诉我们,首先需要解决的是对IT基础设施的管理,管理范围要能覆盖到机房所有硬件设备.这一点是前提和基础.其次,才是对各种应用系统做到很好的监控.最后,才能为业务系统提供足够的保障.  

高性能Server---Reactor模型

无处不在的C/S架构 在这个充斥着云的时代,我们使用的软件可以说99%都是C/S架构的! 你发邮件用的Outlook,Foxmail等 你看视频用的优酷,土豆等 你写文档用的Office365,googleDoc,Evernote等 你浏览网页用的IE,Chrome等(B/S是特殊的C/S) …… C/S架构的软件带来的一个明显的好处就是:只要有网络,你可以在任何地方干同一件事. 例如:你在家里使用Office365编写了文档.到了公司,只要打开编辑地址就可以看到在家里编写的文档,进行展示或者继

Socket与网站保密应用 (隐藏链接的视频下载)

Flash Socket应用 页面使用 Socket 底层传输数据的方便比传统的 HTTP 协议更隐秘,浏览器基本不对基于 TCP 通信的 Socket 进行监测,因此也无法通过浏览器提供的开发者工具来探测网站的受保护数据.例如,音悦台网站上的高铃 - 爱してる-高清[MV],其视频内容通过 Socket 进行传输,浏览者根本发现不了其视频数据,也无法找到浏览器有缓存到数据. 打开视频网页: http://v.yinyuetai.com/video/37962 但,使用其他工具还是可以找到服务器