0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

如何用eBPF写TCP拥塞控制算法?

Linux阅码场 ? 来源:csdn ? 作者:dog250 ? 2020-12-26 09:44 ? 次阅读
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

其实不想用这个题目的,只因为TCP相关的东西比较吸引人的眼球,这篇文章的主题还是eBPF,而不是TCP。

用eBPF写TCP拥塞控制算法只是本文所讲内容的一个再平凡不过的例子。

先看两个问题,或者说是两个痛点:

内核越来越策略化。

内核接口不稳定。

分别简单说一下。

所谓内核策略化就是说越来越多的灵巧的算法,小tricks等灵活多变的代码进入内核,举例来讲,包括但不限于以下这些:

TCP拥塞控制算法。

TC排队规则,数据包调度算法。

各种查找的哈希算法。

这部分策略化的代码几乎都是用“回调函数”实现的,这在另一方面烘托了Linux内核也是模块化设计的,且机制和策略分离,当需要一种新的算法时,只需要register一组新的回调函数即可。

然而,…

然而不够完美,因为上述第2点,“内核接口不稳定”!即每一个内核版本的数据结构以及API都是不兼容的。

这意味着什么?

这意味着,即便是高度封装好的算法模块代码,也需要为不同版本的Linux内核维护一套代码,当涉及内核模块由于版本问题不得不升级时,数据结构和api的适配工作往往是耗时且出力不讨好的。

但其实,很多算法根本就是与内核数据结构,内核api这些无关的。

两个内核版本,数据结构只是字段变化了位置,新增了字段,更新了字段名字,即便如此,不得不对算法模块进行重新编译…

如果能在模块载入内核的时候,对函数和数据结构字段进行重定位就好了!

我们的目标是,一次编写,多次运行。

又是Facebook走在了前面,来自Facebook的BPF CO-RE(Compile Once – Run Everywhere):
http://vger.kernel.org/bpfconf2019_talks/bpf-core.pdf
没错,eBPF,就是它!

我们看下其描述:

BPF CO-RE talk discussed issues that developers currently run into when developing, testing, deploying, and running BPF applications at scale, taking Facebook’s experience as an example. Today, most types of BPF programs access internal kernel structures, which necessitates the need to compile BPF program’s C code “on the fly” on every single production machine due to changing struct/union layouts and definitions inside kernel. This causes many problems and inconveniences, starting from the need to have kernel sources available everywhere and in sync with running kernel, which is a hassle to set up and maintain. Reliance on embedded LLVM/Clang for compilation means big application binary size, increased memory usage, and some rare, but impactful production issues due to increased resource usage due to compilation. With current approach testing BPF programs against multitude of production kernels is a stressful, time-consuming, and error-prone process. The goal of BPF CO-RE is to solve all of those issues and move BPF app development flow closer to typical experience, one would expect when developing applications: compile BPF code once and distribute it as a binary. Having a good way to validate that BPF application will run without issues on all active kernels is also a must.

The complexity hides in the need to adjust compiled BPF assembly code to every specific kernel in production, as memory layout of kernel data structures changes between kernel versions and even different kernel build configurations. BPF CO-RE solution relies on self-describing kernel providing BTF type information and layout (ability to produce it was recently committed upstream). With the help from Clang compiler emitting special relocations during BPF compilation and with libbpf as a dynamic loader, it’s possible to reconciliate correct field offsets just before loading BPF program into kernel. As BPF programs are often required to work without modification (i.e., re-compilation) on multiple kernel versions/configurations with incompatible internal changes, there is a way to specify conditional BPF logic based on actual kernel version and configuration, also using relocations emitted from Clang. Not having to rely on kernel headers significantly improves the testing story and makes it possible to have a good tooling support to do pre-validation before deploying to production.

There are still issues which will have to be worked around for now. There is currently no good way to extract #define macro from kernel, so this has to be dealt with by copy/pasting the necessary definitions manually. Code directly relying on size of structs/unions has to be avoided as well, as it isn’t relocatable in general case. While there are some raw ideas how to solve issues like that in the future, BPF CO-RE developers prioritize providing basic mechanisms to allow “Compile Once - Run Everywhere” approach and significantly improve testing and pre-validation experience through better tooling, enabled by BPF CO-RE. As existing applications are adapted to BPF CO-RE, there will be new learning and better understanding of additional facilities that need to be provided to provide best developer experience possible.

该机制可以:

用eBPF的一组字节码实现内核模块的一组回调函数。

对使用到的内核数据结构字段进行重定位,适配当前内核的对应偏移。

后果就是:

很多内核算法模块可以用eBPF来编写了。

Linux 5.6用TCP拥塞控制算法举了一例,我们看一下:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=09903869f69f

可以看到,这个eBPF程序是与内核版本无关的,你可以看到它的tcp_sock结构体的定义:

struct tcp_sock { struct inet_connection_sock inet_conn; __u32 rcv_nxt; __u32 snd_nxt; __u32 snd_una; __u8 ecn_flags; __u32 delivered; __u32 delivered_ce; __u32 snd_cwnd; __u32 snd_cwnd_cnt; __u32 snd_cwnd_clamp; __u32 snd_ssthresh; __u8 syn_data:1, /* SYN includes data */ syn_fastopen:1, /* SYN includes Fast Open option */ syn_fastopen_exp:1,/* SYN includes Fast Open exp. option */ syn_fastopen_ch:1, /* Active TFO re-enabling probe */ syn_data_acked:1,/* data in SYN is acked by SYN-ACK */ save_syn:1, /* Save headers of SYN packet */ is_cwnd_limited:1,/* forward progress limited by snd_cwnd? */ syn_smc:1; /* SYN includes SMC */ __u32 max_packets_out; __u32 lsndtime; __u32 prior_cwnd;} __attribute__((preserve_access_index));

这里注意到两点:

该结构体并非内核头文件里的对应结构体,它只包含了内核对应结构体里TCP CC算法用到的字段,它是内核对应同名结构体的子集。

preserve_access_index属性表示eBPF字节码在载入的时候,会对这个结构体里的字段进行重定向,满足当前内核版本的同名结构体字段的偏移。

我们在看下eBPF实现的TCP CC回调函数是个什么样子:

BPF_TCP_OPS_3(tcp_reno_cong_avoid, void, struct sock *, sk, __u32, ack, __u32, acked){ struct tcp_sock *tp = tcp_sk(sk); if (!tcp_is_cwnd_limited(sk)) return; /* In "safe" area, increase. */ if (tcp_in_slow_start(tp)) { acked = tcp_slow_start(tp, acked); if (!acked) return; } /* In dangerous area, increase slowly. */ tcp_cong_avoid_ai(tp, tp->snd_cwnd, acked);}... SEC(".struct_ops")struct tcp_congestion_ops dctcp = { .init = (void *)dctcp_init, .in_ack_event = (void *)dctcp_update_alpha, .cwnd_event = (void *)dctcp_cwnd_event, .ssthresh = (void *)dctcp_ssthresh, .cong_avoid = (void *)tcp_reno_cong_avoid, .undo_cwnd = (void *)dctcp_cwnd_undo, .set_state = (void *)dctcp_state, .flags = TCP_CONG_NEEDS_ECN, .name = "bpf_dctcp",};

没啥特殊的,几乎和内核模块的写法一样,唯一不同的是:

它和内核版本无关了。你用llvm/clang编译出来.o字节码将可以被载入到所有的内核。

它让人感觉这是在用户态编程

是的,这就是在用户态写的TCP CC算法,eBPF字节码的对应verifier会对你的代码进行校验,它不允许可以crash内核的eBPF代码载入,你的危险代码几乎无法通过verify。

如果你想搞明白这一切背后是怎么做到的,看两个文件就够了:

net/ipv4/bpf_tcp_ca.c

kernel/bpf/bpf_struct_ops.c

当然,经理不会知道这意味着什么。

浙江温州皮鞋湿,下雨进水不会胖。

原文标题:用eBPF写TCP拥塞控制算法

文章出处:【微信公众号:Linuxer】欢迎添加关注!文章转载请注明出处。

责任编辑:haq

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 内核
    +关注

    关注

    3

    文章

    1423

    浏览量

    41667
  • TCP
    TCP
    +关注

    关注

    8

    文章

    1410

    浏览量

    81601

原文标题:用eBPF写TCP拥塞控制算法

文章出处:【微信号:LinuxDev,微信公众号:Linux阅码场】欢迎添加关注!文章转载请注明出处。

收藏 人收藏
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

    评论

    相关推荐
    热点推荐

    基于eBPF的Kubernetes网络异常检测系统

    作为一名在云原生领域深耕多年的运维工程师,我见过太多因为网络问题导致的生产事故。传统的监控手段往往是事后诸葛亮,当你发现问题时,用户已经在抱怨了。今天,我将分享如何利用 eBPF 这一革命性技术,构建一套能够实时检测 Kubernetes 网络异常的系统。
    的头像 发表于 07-24 14:09 ?194次阅读

    什么是Modbus TCP协议

    Modbus TCP是一种基于TCP/IP协议的Modbus通信协议,用于在客户机和服务器之间进行数据通信。它常用于工业自动化控制、电力监控与管理、温湿度监测等领域。Modbus TCP
    的头像 发表于 07-23 17:18 ?820次阅读
    什么是Modbus <b class='flag-5'>TCP</b>协议

    何用AI负载为SONiC交换机调整ECN水线

    显式拥塞通知(ECN)是计算机网络中的一种机制,它允许发送设备明确地通知接收设备网络拥塞,而不是依赖于传统的“丢包”方法。在传统的TCP/IP网络中,当路由器或交换机出现拥塞时,它会丢
    的头像 发表于 07-11 14:12 ?1691次阅读
    如<b class='flag-5'>何用</b>AI负载为SONiC交换机调整ECN水线

    RDMA简介7之可靠传输

    网络无损,需要进行严格的流量控制拥塞管理。流量控制指通过调整发送端的发送速率,确保接收端能够处理并接收所有数据包。RoCE v2使用了IEEE 802.11Qbb中提出的基于优先级的流量控制
    发表于 06-13 10:01

    PID控制算法的C语言实现:PID算法原理

    的是,在我所接触的控制算法当中,PID 控制算法又是最简单,最能体现反馈思想的控制算法,可谓经典
    发表于 02-26 15:24

    TCP协议的性能测试与评估方法

    的、基于字节流的传输层通信协议。它通过三次握手建立连接,使用序列号和确认应答机制保证数据的有序传输,并通过滑动窗口机制控制数据流量,以避免网络拥塞。 性能测试指标 吞吐量(Throughput) :衡量单位时间内成功传输的数据量,通常以Mbps或
    的头像 发表于 01-22 10:03 ?1716次阅读

    如何优化TCP协议的性能

    优化TCP协议的性能可以从多个方面入手,以下是一些关键的策略和方法: 一、调整TCP参数 TCP窗口大小 : 重要性 :TCP窗口大小是衡量TCP
    的头像 发表于 01-22 09:52 ?1073次阅读

    TCP协议的安全性分析

    使用确认机制来确保数据段被正确接收。如果一个段丢失,发送方将重新发送该段。 流量控制TCP使用窗口大小来控制发送方发送数据的速率,以避免接收方被过多的数据淹没。 拥塞
    的头像 发表于 01-22 09:48 ?827次阅读

    TCP协议与UDP协议的区别

    1. 连接性 TCP(传输控制协议) : 面向连接 :在数据传输之前,TCP需要建立一个连接,这通过三次握手过程完成。 可靠性 :一旦连接建立,TCP确保数据的可靠传输,通过确认和重传
    的头像 发表于 01-22 09:44 ?1008次阅读

    TCP协议是什么

    在网络通信的广阔领域中,TCP(Transmission Control Protocol,传输控制协议)扮演着举足轻重的角色。作为TCP/IP协议族中的核心协议之一,TCP位于网络层
    的头像 发表于 10-09 13:54 ?2001次阅读

    何用Jacinto内部的GPtimer输出PWM信号控制屏幕背光

    电子发烧友网站提供《如何用Jacinto内部的GPtimer输出PWM信号控制屏幕背光.pdf》资料免费下载
    发表于 09-29 10:25 ?0次下载
    如<b class='flag-5'>何用</b>Jacinto内部的GPtimer输出PWM信号<b class='flag-5'>控制</b>屏幕背光

    MODBUS TCP 转 CANOpen

    产品概述 SG-TCP-COE-210 网关可以实现将 CANOpen 接口设备连接到 MODBUS TCP 网络中。用户不需要了解具体的 CANOpen 和 Modbus TCP 协议即可实现
    的头像 发表于 09-24 13:59 ?561次阅读
    MODBUS <b class='flag-5'>TCP</b> 转 CANOpen

    EtherCAT主站转Modbus TCP协议转换网关

    JM-ECTM-TCP网关实现Modbus TCP网络与EtherCAT网络的互连互通。该网关可实现双向数据交换,实现EtherCAT设备和Modbus TCP控制器的数据交互。
    的头像 发表于 09-07 17:26 ?767次阅读
    EtherCAT主站转Modbus <b class='flag-5'>TCP</b>协议转换网关

    EtherCAT转Modbus TCP协议网关(JM-ECT-TCP

    JM-ECT-TCP网关实现EtherCAT网络与Modbus TCP网络之间的数据通讯,即将Modbus TCP设备转换为EtherCAT设备。
    的头像 发表于 09-07 17:05 ?702次阅读
    EtherCAT转Modbus <b class='flag-5'>TCP</b>协议网关(JM-ECT-<b class='flag-5'>TCP</b>)

    EtherCAT主站ModBus TCP协议网关(YC-ECTM-TCP

    远创智控YC-ECTM-TCP型网关实现Modbus TCP网络与EtherCAT网络的互连互通。该网关可实现双向数据交换,实现EtherCAT设备和Modbus TCP控制器的数据交
    的头像 发表于 08-25 09:38 ?757次阅读
    EtherCAT主站ModBus <b class='flag-5'>TCP</b>协议网关(YC-ECTM-<b class='flag-5'>TCP</b>)