mirror of
https://github.com/eunomia-bpf/bpf-developer-tutorial.git
synced 2026-02-03 02:04:30 +08:00
379 lines
33 KiB
HTML
379 lines
33 KiB
HTML
<!DOCTYPE HTML>
|
||
<html lang="en" class="light" dir="ltr">
|
||
<head>
|
||
<!-- Book generated using mdBook -->
|
||
<meta charset="UTF-8">
|
||
<title>lesson 0-introduce - bpf-developer-tutorial</title>
|
||
|
||
|
||
<!-- Custom HTML head -->
|
||
|
||
<meta name="description" content="">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<meta name="theme-color" content="#ffffff">
|
||
|
||
<link rel="icon" href="../favicon.svg">
|
||
<link rel="shortcut icon" href="../favicon.png">
|
||
<link rel="stylesheet" href="../css/variables.css">
|
||
<link rel="stylesheet" href="../css/general.css">
|
||
<link rel="stylesheet" href="../css/chrome.css">
|
||
<link rel="stylesheet" href="../css/print.css" media="print">
|
||
|
||
<!-- Fonts -->
|
||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
||
<link rel="stylesheet" href="../fonts/fonts.css">
|
||
|
||
<!-- Highlight.js Stylesheets -->
|
||
<link rel="stylesheet" href="../highlight.css">
|
||
<link rel="stylesheet" href="../tomorrow-night.css">
|
||
<link rel="stylesheet" href="../ayu-highlight.css">
|
||
|
||
<!-- Custom theme stylesheets -->
|
||
|
||
</head>
|
||
<body class="sidebar-visible no-js">
|
||
<div id="body-container">
|
||
<!-- Provide site root to javascript -->
|
||
<script>
|
||
var path_to_root = "../";
|
||
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
||
</script>
|
||
|
||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
||
<script>
|
||
try {
|
||
var theme = localStorage.getItem('mdbook-theme');
|
||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
||
|
||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
||
}
|
||
|
||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
||
}
|
||
} catch (e) { }
|
||
</script>
|
||
|
||
<!-- Set the theme before any content is loaded, prevents flash -->
|
||
<script>
|
||
var theme;
|
||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
||
if (theme === null || theme === undefined) { theme = default_theme; }
|
||
var html = document.querySelector('html');
|
||
html.classList.remove('light')
|
||
html.classList.add(theme);
|
||
var body = document.querySelector('body');
|
||
body.classList.remove('no-js')
|
||
body.classList.add('js');
|
||
</script>
|
||
|
||
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
|
||
|
||
<!-- Hide / unhide sidebar before it is displayed -->
|
||
<script>
|
||
var body = document.querySelector('body');
|
||
var sidebar = null;
|
||
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
|
||
if (document.body.clientWidth >= 1080) {
|
||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
||
sidebar = sidebar || 'visible';
|
||
} else {
|
||
sidebar = 'hidden';
|
||
}
|
||
sidebar_toggle.checked = sidebar === 'visible';
|
||
body.classList.remove('sidebar-visible');
|
||
body.classList.add("sidebar-" + sidebar);
|
||
</script>
|
||
|
||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
||
<div class="sidebar-scrollbox">
|
||
<ol class="chapter"><li class="chapter-item expanded affix "><a href="../https://github.com/eunomia-bpf/bpf-developer-tutorial.html">https://github.com/eunomia-bpf/bpf-developer-tutorial</a></li><li class="chapter-item expanded affix "><li class="part-title">入门文档</li><li class="chapter-item expanded "><a href="../0-introduce/index.html" class="active"><strong aria-hidden="true">1.</strong> lesson 0-introduce</a></li><li class="chapter-item expanded "><a href="../1-helloworld/index.html"><strong aria-hidden="true">2.</strong> lesson 1-helloworld</a></li><li class="chapter-item expanded "><a href="../2-kprobe-unlink/index.html"><strong aria-hidden="true">3.</strong> lesson 2-kprobe-unlink</a></li><li class="chapter-item expanded "><a href="../3-fentry-unlink/index.html"><strong aria-hidden="true">4.</strong> lesson 3-fentry-unlink</a></li><li class="chapter-item expanded "><a href="../4-opensnoop/index.html"><strong aria-hidden="true">5.</strong> lesson 4-opensnoop</a></li><li class="chapter-item expanded "><a href="../5-uprobe-bashreadline/index.html"><strong aria-hidden="true">6.</strong> lesson 5-uprobe-bashreadline</a></li><li class="chapter-item expanded "><a href="../6-sigsnoop/index.html"><strong aria-hidden="true">7.</strong> lesson 6-sigsnoop</a></li><li class="chapter-item expanded "><a href="../7-execsnoop/index.html"><strong aria-hidden="true">8.</strong> lesson 7-execsnoop</a></li><li class="chapter-item expanded "><a href="../8-exitsnoop/index.html"><strong aria-hidden="true">9.</strong> lesson 8-execsnoop</a></li><li class="chapter-item expanded "><a href="../9-runqlat/index.html"><strong aria-hidden="true">10.</strong> lesson 9-runqlat</a></li><li class="chapter-item expanded "><a href="../10-hardirqs/index.html"><strong aria-hidden="true">11.</strong> lesson 10-hardirqs</a></li><li class="chapter-item expanded affix "><li class="part-title">进阶文档和示例</li><li class="chapter-item expanded "><a href="../11-bootstrap/index.html"><strong aria-hidden="true">12.</strong> lesson 11-bootstrap</a></li><li class="chapter-item expanded "><a href="../12-profile/index.html"><strong aria-hidden="true">13.</strong> lesson 12-profile</a></li><li class="chapter-item expanded "><a href="../13-tcpconnlat/index.html"><strong aria-hidden="true">14.</strong> lesson 13-tcpconnlat</a></li><li class="chapter-item expanded "><a href="../14-tcpstates/index.html"><strong aria-hidden="true">15.</strong> lesson 14-tcpstates</a></li><li class="chapter-item expanded "><a href="../15-javagc/index.html"><strong aria-hidden="true">16.</strong> lesson 15-javagc</a></li><li class="chapter-item expanded "><a href="../16-memleak/index.html"><strong aria-hidden="true">17.</strong> lesson 16-memleak</a></li><li class="chapter-item expanded "><a href="../17-biopattern/index.html"><strong aria-hidden="true">18.</strong> lesson 17-biopattern</a></li><li class="chapter-item expanded "><a href="../18-further-reading/index.html"><strong aria-hidden="true">19.</strong> lesson 18-further-reading</a></li><li class="chapter-item expanded "><a href="../19-lsm-connect/index.html"><strong aria-hidden="true">20.</strong> lesson 19-lsm-connect</a></li><li class="chapter-item expanded "><a href="../20-tc/index.html"><strong aria-hidden="true">21.</strong> lesson 20-tc</a></li><li class="chapter-item expanded "><a href="../21-xdp/index.html"><strong aria-hidden="true">22.</strong> lesson 21-xdp</a></li><li class="chapter-item expanded affix "><li class="part-title">高级主题</li><li class="chapter-item expanded "><a href="../22-android/index.html"><strong aria-hidden="true">23.</strong> 在 Android 上使用 eBPF 程序</a></li><li class="chapter-item expanded "><a href="../30-sslsniff/index.html"><strong aria-hidden="true">24.</strong> 使用 uprobe 捕获多种库的 SSL/TLS 明文数据</a></li><li class="chapter-item expanded "><a href="../23-http/index.html"><strong aria-hidden="true">25.</strong> 使用 eBPF socket filter 或 syscall trace 追踪 HTTP 请求和其他七层协议</a></li><li class="chapter-item expanded "><a href="../29-sockops/index.html"><strong aria-hidden="true">26.</strong> 使用 sockops 加速网络请求转发</a></li><li class="chapter-item expanded "><a href="../24-hide/index.html"><strong aria-hidden="true">27.</strong> 使用 eBPF 隐藏进程或文件信息</a></li><li class="chapter-item expanded "><a href="../25-signal/index.html"><strong aria-hidden="true">28.</strong> 使用 bpf_send_signal 发送信号终止进程</a></li><li class="chapter-item expanded "><a href="../26-sudo/index.html"><strong aria-hidden="true">29.</strong> 使用 eBPF 添加 sudo 用户</a></li><li class="chapter-item expanded "><a href="../27-replace/index.html"><strong aria-hidden="true">30.</strong> 使用 eBPF 替换任意程序读取或写入的文本</a></li><li class="chapter-item expanded "><a href="../28-detach/index.html"><strong aria-hidden="true">31.</strong> BPF 的生命周期:使用 Detached 模式在用户态应用退出后持续运行 eBPF 程序</a></li><li class="chapter-item expanded "><a href="../18-further-reading/ebpf-security.zh.html"><strong aria-hidden="true">32.</strong> eBPF 运行时的安全性与面临的挑战</a></li><li class="chapter-item expanded "><a href="../34-syscall/index.html"><strong aria-hidden="true">33.</strong> 使用 eBPF 修改系统调用参数</a></li><li class="chapter-item expanded "><a href="../35-user-ringbuf/index.html"><strong aria-hidden="true">34.</strong> eBPF开发实践:使用 user ring buffer 向内核异步发送信息</a></li><li class="chapter-item expanded "><a href="../36-userspace-ebpf/index.html"><strong aria-hidden="true">35.</strong> 用户空间 eBPF 运行时:深度解析与应用实践</a></li><li class="chapter-item expanded "><a href="../37-uprobe-rust/index.html"><strong aria-hidden="true">36.</strong> 使用 uprobe 追踪 Rust 应用程序</a></li><li class="chapter-item expanded "><a href="../38-btf-uprobe/index.html"><strong aria-hidden="true">37.</strong> 借助 eBPF 和 BTF,让用户态也能一次编译、到处运行</a></li><li class="chapter-item expanded affix "><li class="part-title">bcc 和 bpftrace 教程与文档</li><li class="chapter-item expanded "><a href="../bcc-documents/kernel-versions.html"><strong aria-hidden="true">38.</strong> BPF Features by Linux Kernel Version</a></li><li class="chapter-item expanded "><a href="../bcc-documents/kernel_config.html"><strong aria-hidden="true">39.</strong> Kernel Configuration for BPF Features</a></li><li class="chapter-item expanded "><a href="../bcc-documents/reference_guide.html"><strong aria-hidden="true">40.</strong> bcc Reference Guide</a></li><li class="chapter-item expanded "><a href="../bcc-documents/special_filtering.html"><strong aria-hidden="true">41.</strong> Special Filtering</a></li><li class="chapter-item expanded "><a href="../bcc-documents/tutorial.html"><strong aria-hidden="true">42.</strong> bcc Tutorial</a></li><li class="chapter-item expanded "><a href="../bcc-documents/tutorial_bcc_python_developer.html"><strong aria-hidden="true">43.</strong> bcc Python Developer Tutorial</a></li><li class="chapter-item expanded "><a href="../bpftrace-tutorial/index.html"><strong aria-hidden="true">44.</strong> bpftrace Tutorial</a></li></ol>
|
||
</div>
|
||
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
|
||
<div class="sidebar-resize-indicator"></div>
|
||
</div>
|
||
</nav>
|
||
|
||
<!-- Track and set sidebar scroll position -->
|
||
<script>
|
||
var sidebarScrollbox = document.querySelector('#sidebar .sidebar-scrollbox');
|
||
sidebarScrollbox.addEventListener('click', function(e) {
|
||
if (e.target.tagName === 'A') {
|
||
sessionStorage.setItem('sidebar-scroll', sidebarScrollbox.scrollTop);
|
||
}
|
||
}, { passive: true });
|
||
var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
|
||
sessionStorage.removeItem('sidebar-scroll');
|
||
if (sidebarScrollTop) {
|
||
// preserve sidebar scroll position when navigating via links within sidebar
|
||
sidebarScrollbox.scrollTop = sidebarScrollTop;
|
||
} else {
|
||
// scroll sidebar to current active section when navigating via "next/previous chapter" buttons
|
||
var activeSection = document.querySelector('#sidebar .active');
|
||
if (activeSection) {
|
||
activeSection.scrollIntoView({ block: 'center' });
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<div id="page-wrapper" class="page-wrapper">
|
||
|
||
<div class="page">
|
||
<div id="menu-bar-hover-placeholder"></div>
|
||
<div id="menu-bar" class="menu-bar sticky">
|
||
<div class="left-buttons">
|
||
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
||
<i class="fa fa-bars"></i>
|
||
</label>
|
||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
||
<i class="fa fa-paint-brush"></i>
|
||
</button>
|
||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
||
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
|
||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
||
</ul>
|
||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
||
<i class="fa fa-search"></i>
|
||
</button>
|
||
</div>
|
||
|
||
<h1 class="menu-title">bpf-developer-tutorial</h1>
|
||
|
||
<div class="right-buttons">
|
||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
||
<i id="print-button" class="fa fa-print"></i>
|
||
</a>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
<div id="search-wrapper" class="hidden">
|
||
<form id="searchbar-outer" class="searchbar-outer">
|
||
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
||
</form>
|
||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
||
<div id="searchresults-header" class="searchresults-header"></div>
|
||
<ul id="searchresults">
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
||
<script>
|
||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
||
});
|
||
</script>
|
||
|
||
<div id="content" class="content">
|
||
<main>
|
||
<h1 id="ebpf-示例教程-0核心概念与工具简介"><a class="header" href="#ebpf-示例教程-0核心概念与工具简介">eBPF 示例教程 0:核心概念与工具简介</a></h1>
|
||
<p>这是一个全面的 eBPF 开发教程的第一部分,旨在通过实用的 eBPF 开发指导您从初学者到高级用户。它涵盖了基本概念、实际代码示例以及在现代系统中的应用。我们将不再专注于传统工具如 BCC,而是使用现代框架如 <code>libbpf</code>、<code>Cilium</code>、<code>libbpf-rs</code> 和 eunomia-bpf,并提供 <code>C</code>、<code>Go</code> 和 <code>Rust</code> 的示例。</p>
|
||
<p>本教程的主要目标是提供清晰简洁的 eBPF 工具示例(起步只需 20 行代码!),帮助开发者快速掌握基本的 eBPF 开发技术。每个示例都是独立的,可以在目录结构中找到,每个目录代表一个独立的 eBPF 工具。您还可以访问我们的教程代码仓库 <a href="https://github.com/eunomia-bpf/bpf-developer-tutorial">https://github.com/eunomia-bpf/bpf-developer-tutorial</a> 或网站 <a href="https://eunomia.dev/tutorials/">https://eunomia.dev/tutorials/</a> 获取更多示例和完整的教程源代码。</p>
|
||
<h2 id="ebpf-简介安全高效的内核扩展"><a class="header" href="#ebpf-简介安全高效的内核扩展">eBPF 简介:安全高效的内核扩展</a></h2>
|
||
<p>eBPF(扩展的 Berkeley Packet Filter)是一项突破性的技术,允许开发者在内核空间中安全高效地运行小型程序。与传统方法需要修改内核源代码或加载新模块不同,eBPF 使得动态定制和优化网络行为成为可能,且不会中断系统操作。这种灵活性和高效性使 eBPF 成为克服传统网络栈限制的关键技术。</p>
|
||
<h3 id="ebpf-的强大之处是什么"><a class="header" href="#ebpf-的强大之处是什么">eBPF 的强大之处是什么?</a></h3>
|
||
<ul>
|
||
<li><strong>直接内核交互</strong>:eBPF 程序在内核中执行,与系统级事件如网络包、系统调用或追踪点交互。</li>
|
||
<li><strong>安全执行</strong>:eBPF 通过验证器在程序运行前检查其逻辑,防止潜在的内核崩溃或安全漏洞。</li>
|
||
<li><strong>最低开销</strong>:eBPF 通过使用即时编译器(JIT),将 eBPF 字节码转换为针对特定架构的优化机器码,实现近原生执行速度。</li>
|
||
</ul>
|
||
<h2 id="ebpf过去现在与未来"><a class="header" href="#ebpf过去现在与未来">eBPF:过去、现在与未来</a></h2>
|
||
<h3 id="过去可编程网络的变革"><a class="header" href="#过去可编程网络的变革">过去:可编程网络的变革</a></h3>
|
||
<p>eBPF 于 2014 年推出,彻底改变了开发者处理网络的方式,允许小型可编程内核空间应用程序实时处理数据包。通过钩住关键内核点,eBPF 使得在网络包到达时应用自定义逻辑成为可能,从而提高了效率和灵活性。这使得组织能够在不需要自定义驱动程序或修改内核的情况下定制网络行为,为云原生和数据中心环境创造了理想的解决方案。</p>
|
||
<h3 id="现在满足现代计算需求的多功能框架"><a class="header" href="#现在满足现代计算需求的多功能框架">现在:满足现代计算需求的多功能框架</a></h3>
|
||
<p>eBPF 已发展为一个多功能框架,超越了其最初的网络用途,现在涵盖了可观测性、追踪、安全性,甚至系统资源管理。eBPF 程序可以动态钩住内核事件,赋予开发者精确控制系统行为和性能优化的能力,而无需修改内核或重启系统。这使得 eBPF 成为系统管理员和开发者监控、优化和保护环境的必备工具。</p>
|
||
<p>以下是 eBPF 目前广泛应用的一些关键领域:</p>
|
||
<ul>
|
||
<li>
|
||
<p><strong>网络</strong>:eBPF 提供内核中实时、高速的数据包过滤和处理,允许创建自定义协议解析器和网络策略,无需新驱动程序或系统重启。这在云和数据中心环境中实现了高效的网络管理。</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>可观测性</strong>:eBPF 使开发者能够通过收集自定义指标和执行内核级数据聚合来深入了解系统行为。通过利用内核追踪点和函数调用,eBPF 有助于识别性能问题和定位难以发现的错误。</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>追踪与分析</strong>:eBPF 提供强大的追踪和分析能力,通过附加到内核函数、追踪点甚至用户空间探针,使开发者能够深入了解系统和应用程序的行为,从而优化性能和解决复杂的系统问题。</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>安全</strong>:eBPF 在实时安全监控中发挥重要作用。它能够深入检查系统调用、网络流量和其他内核活动,帮助执行动态安全策略和检测异常行为,为基础设施提供高效的保护。</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>调度器优化</strong>:eBPF 越来越多地用于增强 CPU 调度,能够监控 CPU 负载并优化任务在核心之间的分配。这可以更有效地利用 CPU 资源,提高系统响应能力。</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>HID(人机接口设备)驱动增强</strong>:开发者使用 eBPF 优化键盘、鼠标和触摸屏等设备的 HID 驱动程序。通过为处理输入事件添加自定义逻辑,eBPF 提高了对延迟敏感应用的响应速度。</p>
|
||
</li>
|
||
</ul>
|
||
<p>各行业组织已大规模采用 eBPF:</p>
|
||
<ul>
|
||
<li><strong>Google</strong>:使用 eBPF 进行安全审计、数据包处理、实时性能监控以及优化其庞大基础设施的 CPU 调度。</li>
|
||
<li><strong>Netflix</strong>:利用 eBPF 进行网络流量分析,确保流媒体服务的高可用性和性能。</li>
|
||
<li><strong>Android</strong>:应用 eBPF 优化网络使用、功耗和资源分配,提升数百万设备的性能和电池寿命。</li>
|
||
<li><strong>S&P Global</strong>:通过 <strong>Cilium</strong> 使用 eBPF 管理跨多个云和本地系统的网络,确保可扩展性和安全性。</li>
|
||
<li><strong>Shopify</strong>:与 <strong>Falco</strong> 一起实施 eBPF 进行入侵检测,增强其电子商务平台的安全性。</li>
|
||
<li><strong>Cloudflare</strong>:使用 eBPF 进行网络可观测性、安全监控和性能优化,保护全球数百万网站。</li>
|
||
</ul>
|
||
<p>eBPF 能够动态调整系统行为并扩展到用户空间,使其成为现代计算不可或缺的技术。无论是优化网络流量、提升安全性,还是增强系统性能,eBPF 都能帮助开发者高效、安全地应对实时需求。</p>
|
||
<p>除了其内核模式运行时,eBPF 还可以扩展到用户空间。例如,<a href="https://github.com/eunomia-bpf/bpftime">bpftime</a> 是一个用户空间 eBPF 运行时,允许在用户空间应用中进行高性能追踪、性能分析和插件支持。这种 eBPF 向用户空间的扩展有助于在各种超越内核级任务的用例中提高灵活性和性能。</p>
|
||
<h3 id="未来ebpf-的扩展潜力"><a class="header" href="#未来ebpf-的扩展潜力">未来:eBPF 的扩展潜力</a></h3>
|
||
<p>展望未来,预计 eBPF 将成为操作系统更为重要的一部分。重点将放在提升其灵活性、模块化和易用性上,使其能够应用于更广泛的场景。内存管理、并发机制的创新以及与用户空间应用的更好集成已在路上。已经有项目在编译 Linux 内核的关键部分到 BPF 指令集,这可能彻底改变内核开发和分析的方式。</p>
|
||
<p>动态栈、更好的用户空间可观测性工具(例如快速 Uprobes 和特定语言的栈行走器)以及更安全的程序终止机制等进展将继续增强 eBPF 的可靠性并扩展其使用场景。此外,新工具和库将简化 eBPF 开发,降低内核和应用开发者的入门门槛。</p>
|
||
<h2 id="开始学习教程"><a class="header" href="#开始学习教程">开始学习教程</a></h2>
|
||
<p>本教程提供实用的 eBPF 开发实践,涵盖从初级到高级的主题。我们专注于在可观测性、网络和安全等领域的动手示例,使用 <code>libbpf</code>、<code>libbpf-rs</code> 和 <code>eunomia-bpf</code> 等框架,并提供 C、Go 和 Rust 的示例。</p>
|
||
<h3 id="本教程适合谁"><a class="header" href="#本教程适合谁">本教程适合谁?</a></h3>
|
||
<ul>
|
||
<li><strong>开发者</strong> 希望实现自定义内核解决方案。</li>
|
||
<li><strong>系统管理员</strong> 旨在提升性能和安全性。</li>
|
||
<li><strong>技术爱好者</strong> 探索前沿的内核技术。</li>
|
||
</ul>
|
||
<h3 id="你将学到什么"><a class="header" href="#你将学到什么">你将学到什么?</a></h3>
|
||
<ul>
|
||
<li><strong>核心概念</strong>:eBPF 基础知识及其与 Linux 内核的集成。</li>
|
||
<li><strong>实用技能</strong>:编写和部署 eBPF 程序。</li>
|
||
<li><strong>高级主题</strong>:探索 eBPF 在安全、追踪和未来创新方面的应用。</li>
|
||
</ul>
|
||
<hr />
|
||
<h2 id="目录"><a class="header" href="#目录">目录</a></h2>
|
||
<ol>
|
||
<li>
|
||
<p><strong>eBPF 简介</strong><br />
|
||
基本概念和入门所需的工具。</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>初学者示例</strong><br />
|
||
简单的程序,如“Hello World”及使用 kprobe 和 uprobe 进行基础追踪。</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>可观测性</strong><br />
|
||
侧重于使用 eBPF 监控网络流量、文件操作和进程行为的示例。</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>网络</strong><br />
|
||
侧重于修改和优化网络流量的示例,如 XDP、TC 和 socket。</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>安全</strong><br />
|
||
用于隐藏进程和文件、发送信号杀死进程以及跟踪进程事件以增强安全性的程序。</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>高级用例</strong><br />
|
||
涉及性能分析、调度器优化和用户空间 eBPF(如 bpftime)的复杂示例。</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>深入主题</strong><br />
|
||
探索 eBPF 在 Android 上的应用、使用 eBPF 进行网络加速以及通过系统调用修改来保护系统。</p>
|
||
</li>
|
||
</ol>
|
||
<h2 id="如何使用-ebpf-编程"><a class="header" href="#如何使用-ebpf-编程">如何使用 eBPF 编程</a></h2>
|
||
<p>从头编写 eBPF 程序可能较为复杂。为简化这一过程,LLVM 于 2015 年引入了将高级语言代码编译为 eBPF 字节码的能力。自那时起,eBPF 社区构建了像 <code>libbpf</code> 这样的库来管理这些程序。这些库帮助将 eBPF 字节码加载到内核中并执行基本任务。Linux 内核源代码中 <code>samples/bpf/</code> 目录包含了众多 eBPF 示例。</p>
|
||
<p>典型的 eBPF 程序包含两个部分:内核空间代码(<code>*_kern.c</code>)和用户空间代码(<code>*_user.c</code>)。内核空间代码定义逻辑,而用户空间代码负责加载和与内核交互。然而,像 <code>libbpf-bootstrap</code> 和 Go eBPF 库这样的工具简化了这一过程,允许一次性编译和更容易的开发。</p>
|
||
<h3 id="ebpf-开发工具"><a class="header" href="#ebpf-开发工具">eBPF 开发工具</a></h3>
|
||
<ul>
|
||
<li><strong>BCC</strong>:一个基于 Python 的工具链,简化了 eBPF 程序的编写、编译和加载。它提供了许多预构建的追踪工具,但在依赖和兼容性方面存在一些限制。</li>
|
||
<li><strong>eBPF Go 库</strong>:一个 Go 库,解耦了获取 eBPF 字节码的过程与加载和管理 eBPF 程序的过程。</li>
|
||
<li><strong>libbpf-bootstrap</strong>:基于 <code>libbpf</code> 的现代脚手架,提供了高效的工作流用于编写 eBPF 程序,提供简单的一次性编译过程以生成可重用的字节码。</li>
|
||
<li><strong>eunomia-bpf</strong>:一个用于编写仅包含内核空间代码的 eBPF 程序的工具链。它通过动态加载 eBPF 程序简化了 eBPF 程序的开发。</li>
|
||
</ul>
|
||
<p>这些工具有助于减少开发 eBPF 程序的复杂性,使开发者更容易优化系统性能、安全性和可观测性。</p>
|
||
<h2 id="学习-ebpf-开发的一些技巧"><a class="header" href="#学习-ebpf-开发的一些技巧">学习 eBPF 开发的一些技巧</a></h2>
|
||
<p>本文不会提供更详细的 eBPF 原理介绍,但以下是一个学习计划和参考资料,可能对您有帮助:</p>
|
||
<h3 id="ebpf-简介5-7-小时"><a class="header" href="#ebpf-简介5-7-小时">eBPF 简介(5-7 小时)</a></h3>
|
||
<ul>
|
||
<li>使用 Google 或其他搜索引擎搜索:eBPF</li>
|
||
<li>询问类似 ChatGPT 的工具:什么是 eBPF?</li>
|
||
</ul>
|
||
<p>推荐:</p>
|
||
<ul>
|
||
<li>阅读 eBPF 介绍:<a href="https://ebpf.io/">https://ebpf.io/</a>(30 分钟)</li>
|
||
<li>简要了解 eBPF 内核相关文档:<a href="https://docs.ebpf.io/">https://docs.ebpf.io/</a>(了解技术细节的查询来源,30 分钟)</li>
|
||
</ul>
|
||
<p>回答三个问题:</p>
|
||
<ol>
|
||
<li>了解 eBPF 是什么?我们为什么需要它?难道不能使用内核模块吗?</li>
|
||
<li>它有哪些功能?它在 Linux 内核中能做什么?eBPF 程序和助手函数有哪些类型(不需要全部了解,但需要知道在哪里查找)?</li>
|
||
<li>它能用于哪些场景?例如,可以在哪些情况下使用?网络、安全、可观测性?</li>
|
||
</ol>
|
||
<h3 id="理解如何开发-ebpf-程序10-15-小时"><a class="header" href="#理解如何开发-ebpf-程序10-15-小时">理解如何开发 eBPF 程序(10-15 小时)</a></h3>
|
||
<p>了解并尝试 eBPF 开发框架:</p>
|
||
<ul>
|
||
<li>bpftrace 教程:<a href="https://eunomia.dev/tutorials/bpftrace-tutorial/">https://eunomia.dev/tutorials/bpftrace-tutorial/</a>(尝试,1 小时)</li>
|
||
<li>使用 BCC 开发各种工具的示例:<a href="https://github.com/iovisor/bcc/blob/master/docs/tutorial_bcc_python_developer.md">https://github.com/iovisor/bcc/blob/master/docs/tutorial_bcc_python_developer.md</a>(运行,3-4 小时)</li>
|
||
<li>libbpf 的一些示例:<a href="https://github.com/libbpf/libbpf-bootstrap">https://github.com/libbpf/libbpf-bootstrap</a>(运行任何有趣的示例并阅读源代码,2 小时)</li>
|
||
<li>教程:<a href="https://github.com/eunomia-bpf/bpf-developer-tutorial">https://github.com/eunomia-bpf/bpf-developer-tutorial</a>(阅读第 1-10 部分,3-4 小时)</li>
|
||
</ul>
|
||
<p>其他开发框架:Go 或 Rust 语言,请自行搜索和尝试(0-2 小时)</p>
|
||
<p>如果有问题或想了解的内容,无论是否与本项目相关,都可以在该项目的讨论区开始讨论。</p>
|
||
<p>回答一些问题并尝试一些实验(2-5 小时):</p>
|
||
<ol>
|
||
<li>如何开发最简单的 eBPF 程序?</li>
|
||
<li>如何使用 eBPF 追踪内核功能或函数?有很多方法,提供相应的代码示例;</li>
|
||
<li>用户模式和内核模式之间的通信解决方案有哪些?如何将信息从用户模式发送到内核模式?如何将信息从内核模式传递到用户模式?提供代码示例;</li>
|
||
<li>编写您自己的 eBPF 程序以实现某个功能;</li>
|
||
<li>在 eBPF 程序的整个生命周期中,用户模式和内核模式分别做了什么?</li>
|
||
</ol>
|
||
<h2 id="参考资料"><a class="header" href="#参考资料">参考资料</a></h2>
|
||
<ul>
|
||
<li>eBPF 介绍:<a href="https://ebpf.io/">https://ebpf.io/</a></li>
|
||
<li>BPF 编译器集合(BCC):<a href="https://github.com/iovisor/bcc">https://github.com/iovisor/bcc</a></li>
|
||
<li>eunomia-bpf:<a href="https://github.com/eunomia-bpf/eunomia-bpf">https://github.com/eunomia-bpf/eunomia-bpf</a></li>
|
||
</ul>
|
||
<p>您还可以访问我们的教程代码仓库 <a href="https://github.com/eunomia-bpf/bpf-developer-tutorial">https://github.com/eunomia-bpf/bpf-developer-tutorial</a> 或网站 <a href="https://eunomia.dev/tutorials/">https://eunomia.dev/tutorials/</a> 获取更多示例和完整的教程源代码。所有内容均为开源。我们将继续分享更多关于 eBPF 开发实践的内容,帮助您更好地理解和掌握 eBPF 技术。</p>
|
||
|
||
</main>
|
||
|
||
<nav class="nav-wrapper" aria-label="Page navigation">
|
||
<!-- Mobile navigation buttons -->
|
||
<a rel="prev" href="../https://github.com/eunomia-bpf/bpf-developer-tutorial.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
||
<i class="fa fa-angle-left"></i>
|
||
</a>
|
||
|
||
<a rel="next prefetch" href="../1-helloworld/index.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||
<i class="fa fa-angle-right"></i>
|
||
</a>
|
||
|
||
<div style="clear: both"></div>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
|
||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
||
<a rel="prev" href="../https://github.com/eunomia-bpf/bpf-developer-tutorial.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
||
<i class="fa fa-angle-left"></i>
|
||
</a>
|
||
|
||
<a rel="next prefetch" href="../1-helloworld/index.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||
<i class="fa fa-angle-right"></i>
|
||
</a>
|
||
</nav>
|
||
|
||
</div>
|
||
|
||
|
||
|
||
|
||
<script>
|
||
window.playground_copyable = true;
|
||
</script>
|
||
|
||
|
||
<script src="../elasticlunr.min.js"></script>
|
||
<script src="../mark.min.js"></script>
|
||
<script src="../searcher.js"></script>
|
||
|
||
<script src="../clipboard.min.js"></script>
|
||
<script src="../highlight.js"></script>
|
||
<script src="../book.js"></script>
|
||
|
||
<!-- Custom JS scripts -->
|
||
|
||
|
||
</div>
|
||
</body>
|
||
</html>
|