当前位置:网站首页>微秒级 TCP 时间戳
微秒级 TCP 时间戳
2022-06-29 03:32:00 【dog250】
TCP 时间精度目前还在 ms 级,这让 TCP 很难适应 IDC 这种 RTT 在 50 us 量级的短程网络,这种网络中 RTO 同样也应该控制在 us 级别。
此外,与早期 TCP 的 burst 发送模式不同,如今普遍低延迟需求场景下,burst 是 bufferbloat 的元凶,因此普遍过渡到了 pacing 模式发送。ms 级时间精度支持的 pacing 只能应对 MBps 级的带宽。
Linux TCP pacing timer 采用 hrtimer 已经支持了 ns 级的定时器,但 TCP 时间戳选项却连 us 级都不到,依然在 ms 级,这使时间戳选项无法被利用参与 pacing 的采集和计算。
我修改了 Linux 内核代码,使其 TCP 时间戳选项精度提升至 us 级。在发送端,做几如下修改即可:
-static inline u32 tcp_skb_timestamp(const struct sk_buff *skb)
-{
- return div_u64(skb->skb_mstamp_ns, NSEC_PER_SEC / TCP_TS_HZ);
-}
-
/* provide the departure time in us unit */
static inline u64 tcp_skb_timestamp_us(const struct sk_buff *skb)
{
return div_u64(skb->skb_mstamp_ns, NSEC_PER_USEC);
}
+static inline u32 tcp_skb_timestamp(const struct sk_buff *skb)
+{
+ return tcp_skb_timestamp_us(skb);
+}
这代码并非全部,只是举个例子,意思是把所有 ms 级的时间戳都改成 us 级别。
对于接收端,为了避开 ms 级别的 PAWS 检查,做如下 trick :
#!/usr/local/bin/stap -g
%{
#include <net/tcp.h>
%}
function disable_ts(opt_rx:long, skbp:long)
%{
struct tcp_options_received *opt = (struct tcp_options_received *)STAP_ARG_opt_rx;
struct sk_buff *skb = (struct sk_buff *)STAP_ARG_skbp;
struct sock *sk = skb->sk;
if (opt->saw_tstamp && opt->rcv_tsecr && sk) {
struct tcp_sock *tp = tcp_sk(sk);
opt->rcv_tsecr -= tp->tsoffset;
}
opt->saw_tstamp = 0;
%}
function enable_ts(skp:long)
%{
struct sock *sk = (struct sock *)STAP_ARG_skp;
struct tcp_sock *tp = tcp_sk(sk);
if (tp && tp->rx_opt.rcv_tsval) {
tp->rx_opt.saw_tstamp = 1;
}
%}
probe kernel.function("tcp_validate_incoming").return
{
enable_ts($sk);
}
probe kernel.function("tcp_parse_options").return
{
disable_ts($opt_rx, $skb);
}
上述 stap 代码为了避开 tcp_paws_discard 检查,看下面注释:
static bool tcp_validate_incoming(struct sock *sk, struct sk_buff *skb,
const struct tcphdr *th, int syn_inerr)
{
struct tcp_sock *tp = tcp_sk(sk);
bool rst_seq_match = false;
/* RFC1323: H1. Apply PAWS check first. */
if (tcp_fast_parse_options(sock_net(sk), skb, th, tp) &&
tp->rx_opt.saw_tstamp && // 需要将 saw_tstamp 设置为0,最后在 validate 返回前将其再置回 1。
tcp_paws_discard(sk, skb)) {
if (!th->rst) {
NET_INC_STATS(sock_net(sk), LINUX_MIB_PAWSESTABREJECTED);
if (!tcp_oow_rate_limited(sock_net(sk), skb,
LINUX_MIB_TCPACKSKIPPEDPAWS,
&tp->last_oow_ack_time))
tcp_send_dupack(sk, skb);
goto discard;
}
/* Reset is accepted even if it did not pass PAWS. */
}
....
如此事可成。再抓包就可发现相邻发送的两个 skb 的 tsval 不再一样了。
不改之前 1ms 内发出的包时间戳都是相同的,如今至少精度提升了 1000 倍,虽仍不能应对 pacing 预打标,但应对 RTO 计算够了。
提升了 TCP 时间精度除了使 TCP 对 IDC 网络更敏感以及更适应 pacing 之外,还能协助接收端同步 pacing,对于视频流中继来说,这种机制有效保持发送 pacing ,起到整形的效果。通过观察时间戳选项的 tsval 字段,可以明确知晓发送 pacing ,进而决定中继 pacing。
同时,us 级时间精度能让发送端的采样计算更加精准,从而提升拥塞控制算法的分辨率。
为什么一开始 TCP 时间不是 us 精度呢?无论 RTO 还是时间戳选项都不是。
除历史原因之外,还有维持状态机封闭性完备性的原因,具体看 RFC1323 的 ‘4.2.2 Timestamp Clock’:
这就是答案。
但如今这些理由弱化了,在我看来用 RST 关连接可能更直接些。也就不用照顾 MSL,时间戳回绕这些事。
事实上,虽然 RFC1323 建议使用时间戳选项来“优化”连接,但 Linux TCP 基本不用它来计算 RTT,而是采用额外的持续打标法来采集。
做一个私有协议是困难的,最简单的方案就是拿现有 TCP 修改,也就是做了双边完事。第一步先把时间精度提上去。
浙江温州皮鞋湿,下雨进水不会胖。
边栏推荐
- 目前市面上增额终身寿险利率最高的产品是哪个?
- 为什么信息化 ≠ 数字化?终于有人讲明白了
- 分享 60 个神级 VS Code 插件
- 想当设备管理师?满足这三个报考条件就可以
- [data update] NPU development data based on 3568 development board is fully upgraded
- [interview guide] AI algorithm interview
- Geth --- Error: authentication needed: password or unlock
- How to understand MySQL indexes?
- [tcapulusdb knowledge base] Introduction to tcapulusdb restrictions
- Web GIS 航拍实现的智慧园区数字孪生应用
猜你喜欢

Web APIs 高阶函数 丨黑马程序员

MATALB signal processing - signal transformation (6)
![[tcapulusdb] I wish you all a healthy Dragon Boat Festival!](/img/f8/d790cc38a0e1c4d6c3bd27d7158c4a.png)
[tcapulusdb] I wish you all a healthy Dragon Boat Festival!

4种分布式session解决方案

88. (cesium chapter) cesium aggregation diagram

为什么信息化 ≠ 数字化?终于有人讲明白了

【TcaplusDB知识库】TcaplusDB数据导入介绍

MySQL advanced SQL statement (Part 2)

【TcaplusDB知识库】TcaplusDB表数据缓写介绍

2D human posture estimation deeppose
随机推荐
Problem - ADB shellerror: insufficient permissions for device: verify udev rules
[tcapulusdb] I wish you all a healthy Dragon Boat Festival!
[yunyuanyuan] it's so hot. Why don't you come and understand it?
[dynamic planning] change exchange
Shell script to count files, then remove oldest files
vim配置与使用
seekbar 自定义图片上下左右显示不全 / bitmapToDrawable / bitmapToDrawable互转 / paddingStart/paddingEnd /thumbOffset
Unable to locate program input point [email protected]
20款IDEA 神级插件 效率提升 30 倍,写代码必备
基于redis实现的扣减库存
Restore the binary search tree [simulate according to the meaning of the question - > find the problem - > analyze the problem - > see the bidding]
Digital twin application of smart Park Based on Web GIS aerial photography
MATALB signal processing - signal transformation (7)
Farrowtech's wireless sensor adopts the nanobeacon Bluetooth beacon technology of orange group Microelectronics
Wechat applet development Basics
leetcode:560. Subarray with and K
如何理解MySQL的索引?
Installation and deployment of sw-x framework
SSH无密码登陆
Yyds dry inventory difference between bazel and gradle tools