在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

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

    文章

    1402

    瀏覽量

    40902
  • TCP
    TCP
    +關注

    關注

    8

    文章

    1394

    瀏覽量

    80073

原文標題:用eBPF寫TCP擁塞控制算法

文章出處:【微信號:LinuxDev,微信公眾號:Linux閱碼場】歡迎添加關注!文章轉載請注明出處。

收藏 人收藏

    評論

    相關推薦

    PID控制算法的C語言實現:PID算法原理

    的是,在我所接觸的控制算法當中,PID 控制算法又是最簡單,最能體現反饋思想的控制算法,可謂經典
    發表于 02-26 15:24

    TCP協議的性能測試與評估方法

    的、基于字節流的傳輸層通信協議。它通過三次握手建立連接,使用序列號和確認應答機制保證數據的有序傳輸,并通過滑動窗口機制控制數據流量,以避免網絡擁塞。 性能測試指標 吞吐量(Throughput) :衡量單位時間內成功傳輸的數據量,通常以Mbps或
    的頭像 發表于 01-22 10:03 ?828次閱讀

    如何優化TCP協議的性能

    優化TCP協議的性能可以從多個方面入手,以下是一些關鍵的策略和方法: 一、調整TCP參數 TCP窗口大小 : 重要性 :TCP窗口大小是衡量TCP
    的頭像 發表于 01-22 09:52 ?570次閱讀

    TCP協議的安全性分析

    使用確認機制來確保數據段被正確接收。如果一個段丟失,發送方將重新發送該段。 流量控制TCP使用窗口大小來控制發送方發送數據的速率,以避免接收方被過多的數據淹沒。 擁塞
    的頭像 發表于 01-22 09:48 ?405次閱讀

    TCP協議與UDP協議的區別

    1. 連接性 TCP(傳輸控制協議) : 面向連接 :在數據傳輸之前,TCP需要建立一個連接,這通過三次握手過程完成。 可靠性 :一旦連接建立,TCP確保數據的可靠傳輸,通過確認和重傳
    的頭像 發表于 01-22 09:44 ?477次閱讀

    TCP協議是什么

    在網絡通信的廣闊領域中,TCP(Transmission Control Protocol,傳輸控制協議)扮演著舉足輕重的角色。作為TCP/IP協議族中的核心協議之一,TCP位于網絡層
    的頭像 發表于 10-09 13:54 ?1295次閱讀

    何用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>屏幕背光

    EtherCAT主站轉Modbus TCP協議轉換網關

    JM-ECTM-TCP網關實現Modbus TCP網絡與EtherCAT網絡的互連互通。該網關可實現雙向數據交換,實現EtherCAT設備和Modbus TCP控制器的數據交互。
    的頭像 發表于 09-07 17:26 ?585次閱讀
    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 ?528次閱讀
    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 ?550次閱讀
    EtherCAT主站ModBus <b class='flag-5'>TCP</b>協議網關(YC-ECTM-<b class='flag-5'>TCP</b>)

    tcp和udp的區別和聯系

    一、引言 在現代網絡通信中,數據傳輸是至關重要的。為了確保數據的可靠傳輸,網絡協議發揮著關鍵作用。傳輸控制協議(TCP)和用戶數據報協議(UDP)是兩種常用的網絡協議,它們在許多應用場景中發
    的頭像 發表于 08-16 11:06 ?847次閱讀

    神經網絡如何用無監督算法訓練

    標記數據的處理尤為有效,能夠充分利用互聯網上的海量數據資源。以下將詳細探討神經網絡如何用無監督算法進行訓練,包括常見的無監督學習算法、訓練過程、應用及挑戰。
    的頭像 發表于 07-09 18:06 ?1201次閱讀

    BLDC電機控制算法詳解

    算法。本文將詳細介紹BLDC電機的控制算法,包括電速算法、電流環控制算法、磁場導向
    的頭像 發表于 06-14 10:49 ?1678次閱讀

    運動控制算法有哪些

    運動控制算法是機器人學和自動化領域中的核心技術之一,它們負責規劃和執行機器人或自動化設備的精確運動。以下是一些常見的運動控制算法,以及它們的基本原理和應用場景。 PID
    的頭像 發表于 06-13 09:17 ?3613次閱讀

    手把手教你排序算法怎么

    今天以直接插入排序算法,給大家分享一下排序算法的實現思路,主要包含以下部分內容:插入排序介紹插入排序算法實現手把手教你排序算法怎么寫在添加新的記錄時,使用順序查找的方式找到其要插入的位
    的頭像 發表于 06-04 08:03 ?939次閱讀
    手把手教你排序<b class='flag-5'>算法</b>怎么<b class='flag-5'>寫</b>
    主站蜘蛛池模板: 亚洲第一福利网站 | 丁香六月婷婷精品免费观看 | 国产精品久久久久久久久免费 | 国产www色 | 天堂网免费 | 天天做天天爱天天爽综合区 | 五月婷婷俺也去开心 | 狠狠色丁香九九婷婷综合五月 | 天天操天天干天天透 | 国产一级特黄老妇女大片免费 | 8050网| 色天使美国 | 日不卡在线 | 成 人色 网 站 欧美大片在线观看 | 男女交性无遮挡免费视频 | 一区二区三区久久 | fenfencao在线观看免费视频 | 久久国产热 | 欧美最猛黑人xxxx黑人猛交黄 | 日本口工福利漫画无遮挡 | 欧美性喷潮 | 成人v片| 在线h网站 | 久久久久综合中文字幕 | 国产精品夜夜春夜夜爽久久 | 国产大片免费观看中文字幕 | 扒开末成年粉嫩的小缝强文 | 国产精品久久久久久久久 | 日本有色视频 | bbbb毛片免费看 | 年轻护士3的滋味 | 天天摸天天碰中文字幕 | 婷婷激情综合网 | 夜夜爽一区二区三区精品 | 亚洲成在线| 夜夜操夜夜骑 | 色婷婷一区 | 性精品| 三级国产在线 | 亚洲成片在线观看12345ba | 一个人看的www片免费高清视频 |