mirror of
https://github.com/eunomia-bpf/bpf-developer-tutorial.git
synced 2026-02-04 02:34:16 +08:00
809 lines
59 KiB
HTML
809 lines
59 KiB
HTML
<!DOCTYPE HTML>
|
||
<html lang="en" class="light" dir="ltr">
|
||
<head>
|
||
<!-- Book generated using mdBook -->
|
||
<meta charset="UTF-8">
|
||
<title>使用 eBPF socket filter 或 syscall trace 追踪 HTTP 请求和其他七层协议 - 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"><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" class="active"><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-socket-filter-或-syscall-trace-追踪-http-请求等七层协议---ebpf-实践教程"><a class="header" href="#通过-ebpf-socket-filter-或-syscall-trace-追踪-http-请求等七层协议---ebpf-实践教程">通过 eBPF socket filter 或 syscall trace 追踪 HTTP 请求等七层协议 - eBPF 实践教程</a></h1>
|
||
<p>在当今的技术环境中,随着微服务、云原生应用和复杂的分布式系统的崛起,系统的可观测性已成为确保其健康、性能和安全的关键要素。特别是在微服务架构中,应用程序的组件可能分布在多个容器和服务器上,这使得传统的监控方法往往难以提供足够的深度和广度来全面了解系统的行为。这就是为什么观测七层协议,如 HTTP、gRPC、MQTT 等,变得尤为重要。</p>
|
||
<p>七层协议为我们提供了关于应用程序如何与其他服务和组件交互的详细信息。在微服务环境中,了解这些交互是至关重要的,因为它们经常是性能瓶颈、故障和安全问题的根源。然而,监控这些协议并不简单。传统的网络监控工具,如 tcpdump,虽然在捕获网络流量方面非常有效,但在处理七层协议的复杂性和动态性时,它们往往显得力不从心。</p>
|
||
<p>这正是 eBPF 技术发挥作用的地方。eBPF 允许开发者和运维人员深入到系统的内核层,实时观测和分析系统的行为,而无需对应用程序代码进行任何修改或插入埋点。这为我们提供了一个独特的机会,可以更简单、更高效地处理应用层流量,特别是在微服务环境中。</p>
|
||
<p>在本教程中,我们将深入探讨以下内容:</p>
|
||
<ul>
|
||
<li>追踪七层协议,如 HTTP,以及与其相关的挑战。</li>
|
||
<li>eBPF 的 socket filter 和 syscall 追踪:这两种技术如何帮助我们在不同的内核层次追踪 HTTP 网络请求数据,以及这两种方法的优势和局限性。</li>
|
||
<li>eBPF 实践教程:如何开发一个 eBPF 程序,使用 eBPF socket filter 或 syscall 追踪来捕获和分析 HTTP 流量</li>
|
||
</ul>
|
||
<p>随着网络流量的增加和应用程序的复杂性增加,对七层协议的深入了解变得越来越重要。通过本教程,您将获得必要的知识和工具,以便更有效地监控和分析您的网络流量,从而为您的应用程序和服务器提供最佳的性能。</p>
|
||
<p>本文是 eBPF 开发者教程的一部分,更详细的内容可以在这里找到:<a href="https://eunomia.dev/tutorials/">https://eunomia.dev/tutorials/</a> 源代码在 <a href="https://github.com/eunomia-bpf/bpf-developer-tutorial">GitHub 仓库</a> 中开源。</p>
|
||
<h2 id="追踪-http-http2-等七层协议的挑战"><a class="header" href="#追踪-http-http2-等七层协议的挑战">追踪 HTTP, HTTP/2 等七层协议的挑战</a></h2>
|
||
<p>在现代的网络环境中,七层协议不仅仅局限于 HTTP。实际上,有许多七层协议,如 HTTP/2, gRPC, MQTT, WebSocket, AMQP 和 SMTP,它们都在不同的应用场景中发挥着关键作用。这些协议为我们提供了关于应用程序如何与其他服务和组件交互的详细信息。但是,追踪这些协议并不是一个简单的任务,尤其是在复杂的分布式系统中。</p>
|
||
<ol>
|
||
<li>
|
||
<p><strong>多样性和复杂性</strong>:每种七层协议都有其特定的设计和工作原理。例如,gRPC 使用了 HTTP/2 作为其传输协议,并支持多种语言。而 MQTT 是为低带宽和不可靠的网络设计的轻量级发布/订阅消息传输协议。</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>动态性</strong>:许多七层协议都是动态的,这意味着它们的行为可能会根据网络条件、应用需求或其他因素而变化。</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>加密和安全性</strong>:随着安全意识的增强,许多七层协议都采用了加密技术,如 TLS/SSL。这为追踪和分析带来了额外的挑战,因为需要解密流量才能进行深入的分析。</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>高性能需求</strong>:在高流量的生产环境中,捕获和分析七层协议的流量可能会对系统性能产生影响。传统的网络监控工具可能无法处理大量的并发会话。</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>数据的完整性和连续性</strong>:与 tcpdump 这样的工具只捕获单独的数据包不同,追踪七层协议需要捕获完整的会话,这可能涉及多个数据包。这要求工具能够正确地重组和解析这些数据包,以提供连续的会话视图。</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>代码侵入性</strong>:为了深入了解七层协议的行为,开发人员可能需要修改应用程序代码以添加监控功能。这不仅增加了开发和维护的复杂性,而且可能会影响应用程序的性能。</p>
|
||
</li>
|
||
</ol>
|
||
<p>正如上文所述,eBPF 提供了一个强大的解决方案,允许我们在内核层面捕获和分析七层协议的流量,而无需对应用程序进行任何修改。这种方法为我们提供了一个独特的机会,可以更简单、更高效地处理应用层流量,特别是在微服务和分布式环境中。</p>
|
||
<p>在处理网络流量和系统行为时,选择在内核态而非用户态进行处理有其独特的优势。首先,内核态处理可以直接访问系统资源和硬件,从而提供更高的性能和效率。其次,由于内核是操作系统的核心部分,它可以提供对系统行为的全面视图,而不受任何用户空间应用程序的限制。</p>
|
||
<p>**无插桩追踪("zero-instrumentation observability")**的优势如下:</p>
|
||
<ol>
|
||
<li><strong>性能开销小</strong>:由于不需要修改或添加额外的代码到应用程序中,所以对性能的影响最小化。</li>
|
||
<li><strong>透明性</strong>:开发者和运维人员不需要知道应用程序的内部工作原理,也不需要访问源代码。</li>
|
||
<li><strong>灵活性</strong>:可以轻松地在不同的环境和应用程序中部署和使用,无需进行任何特定的配置或修改。</li>
|
||
<li><strong>安全性</strong>:由于不需要修改应用程序代码,所以降低了引入潜在安全漏洞的风险。</li>
|
||
</ol>
|
||
<p>利用 eBPF 在内核态进行无插桩追踪,我们可以实时捕获和分析系统的行为,而不需要对应用程序进行任何修改。这种方法不仅提供了对系统深入的洞察力,而且确保了最佳的性能和效率。这是为什么 eBPF 成为现代可观测性工具的首选技术,特别是在需要高性能和低延迟的生产环境中。</p>
|
||
<h2 id="ebpf-中的-socket-filter-与-syscall-追踪深入解析与比较"><a class="header" href="#ebpf-中的-socket-filter-与-syscall-追踪深入解析与比较">eBPF 中的 socket filter 与 syscall 追踪:深入解析与比较</a></h2>
|
||
<h3 id="ebpf-socket-filter"><a class="header" href="#ebpf-socket-filter"><strong>eBPF Socket Filter</strong></a></h3>
|
||
<p><strong>是什么?</strong>
|
||
eBPF socket filter 是经典的 Berkeley Packet Filter (BPF) 的扩展,允许在内核中直接进行更高级的数据包过滤。它在套接字层操作,使得可以精细地控制哪些数据包被用户空间应用程序处理。</p>
|
||
<p><strong>主要特点:</strong></p>
|
||
<ul>
|
||
<li><strong>性能</strong>:通过在内核中直接处理数据包,eBPF socket filters 减少了用户和内核空间之间的上下文切换的开销。</li>
|
||
<li><strong>灵活性</strong>:eBPF socket filters 可以附加到任何套接字,为各种协议和套接字类型提供了通用的数据包过滤机制。</li>
|
||
<li><strong>可编程性</strong>:开发者可以编写自定义的 eBPF 程序来定义复杂的过滤逻辑,超越简单的数据包匹配。</li>
|
||
</ul>
|
||
<p><strong>用途:</strong></p>
|
||
<ul>
|
||
<li><strong>流量控制</strong>:根据自定义条件限制或优先处理流量。</li>
|
||
<li><strong>安全性</strong>:在它们到达用户空间应用程序之前丢弃恶意数据包。</li>
|
||
<li><strong>监控</strong>:捕获特定数据包进行分析,而不影响其它流量。</li>
|
||
</ul>
|
||
<h3 id="ebpf-syscall-tracing"><a class="header" href="#ebpf-syscall-tracing"><strong>eBPF Syscall Tracing</strong></a></h3>
|
||
<p><strong>是什么?</strong>
|
||
使用 eBPF 进行的系统调用跟踪允许监视和操作应用程序发出的系统调用。系统调用是用户空间应用程序与内核交互的主要机制,因此跟踪它们可以深入了解应用程序的行为。</p>
|
||
<p><strong>主要特点:</strong></p>
|
||
<ul>
|
||
<li><strong>粒度</strong>:eBPF 允许跟踪特定的系统调用,甚至是这些系统调用中的特定参数。</li>
|
||
<li><strong>低开销</strong>:与其他跟踪方法相比,eBPF 系统调用跟踪旨在具有最小的性能影响。</li>
|
||
<li><strong>安全性</strong>:内核验证 eBPF 程序,以确保它们不会损害系统稳定性。</li>
|
||
</ul>
|
||
<p><strong>工作原理:</strong>
|
||
eBPF 系统调用跟踪通常涉及将 eBPF 程序附加到与系统调用相关的 tracepoints 或 kprobes。当跟踪的系统调用被调用时,执行 eBPF 程序,允许收集数据或甚至修改系统调用参数。</p>
|
||
<h3 id="ebpf-的-socket-filter-和-syscall-追踪的对比"><a class="header" href="#ebpf-的-socket-filter-和-syscall-追踪的对比">eBPF 的 socket filter 和 syscall 追踪的对比</a></h3>
|
||
<div class="table-wrapper"><table><thead><tr><th>项目</th><th>eBPF Socket Filter</th><th>eBPF Syscall Tracing</th></tr></thead><tbody>
|
||
<tr><td><strong>操作层</strong></td><td>套接字层,主要处理从套接字接收或发送的网络数据包</td><td>系统调用层,监视和可能更改应用程序发出的系统调用的行为</td></tr>
|
||
<tr><td><strong>主要用途</strong></td><td>主要用于网络数据包的过滤、监控和操作</td><td>用于性能分析、安全监控和系统调用交互的调试</td></tr>
|
||
<tr><td><strong>粒度</strong></td><td>专注于单个网络数据包</td><td>可以监视与网络无关的广泛的系统活动</td></tr>
|
||
<tr><td><strong>追踪 HTTP 流量</strong></td><td>可以用于过滤和捕获通过套接字传递的 HTTP 数据包</td><td>可以跟踪与网络操作相关的系统调用</td></tr>
|
||
</tbody></table>
|
||
</div>
|
||
<p>总之,eBPF 的 socket filter 和 syscall 追踪都可以用于追踪 HTTP 流量,但 socket filters 更直接且更适合此目的。然而,如果您对应用程序如何与系统交互的更广泛的上下文感兴趣(例如,哪些系统调用导致了 HTTP 流量),那么系统调用跟踪将是非常有价值的。在许多高级的可观察性设置中,这两种工具可能会同时使用,以提供系统和网络行为的全面视图。</p>
|
||
<h2 id="使用-ebpf-socket-filter-来捕获-http-流量"><a class="header" href="#使用-ebpf-socket-filter-来捕获-http-流量">使用 eBPF socket filter 来捕获 HTTP 流量</a></h2>
|
||
<p>eBPF 代码由用户态和内核态组成,这里主要关注于内核态代码。这是使用 eBPF socket filter 技术来在内核中捕获HTTP流量的主要逻辑,完整代码如下:</p>
|
||
<pre><code class="language-c">SEC("socket")
|
||
int socket_handler(struct __sk_buff *skb)
|
||
{
|
||
struct so_event *e;
|
||
__u8 verlen;
|
||
__u16 proto;
|
||
__u32 nhoff = ETH_HLEN;
|
||
__u32 ip_proto = 0;
|
||
__u32 tcp_hdr_len = 0;
|
||
__u16 tlen;
|
||
__u32 payload_offset = 0;
|
||
__u32 payload_length = 0;
|
||
__u8 hdr_len;
|
||
|
||
bpf_skb_load_bytes(skb, 12, &proto, 2);
|
||
proto = __bpf_ntohs(proto);
|
||
if (proto != ETH_P_IP)
|
||
return 0;
|
||
|
||
if (ip_is_fragment(skb, nhoff))
|
||
return 0;
|
||
|
||
// ip4 header lengths are variable
|
||
// access ihl as a u8 (linux/include/linux/skbuff.h)
|
||
bpf_skb_load_bytes(skb, ETH_HLEN, &hdr_len, sizeof(hdr_len));
|
||
hdr_len &= 0x0f;
|
||
hdr_len *= 4;
|
||
|
||
/* verify hlen meets minimum size requirements */
|
||
if (hdr_len < sizeof(struct iphdr))
|
||
{
|
||
return 0;
|
||
}
|
||
|
||
bpf_skb_load_bytes(skb, nhoff + offsetof(struct iphdr, protocol), &ip_proto, 1);
|
||
|
||
if (ip_proto != IPPROTO_TCP)
|
||
{
|
||
return 0;
|
||
}
|
||
|
||
tcp_hdr_len = nhoff + hdr_len;
|
||
bpf_skb_load_bytes(skb, nhoff + 0, &verlen, 1);
|
||
bpf_skb_load_bytes(skb, nhoff + offsetof(struct iphdr, tot_len), &tlen, sizeof(tlen));
|
||
|
||
__u8 doff;
|
||
bpf_skb_load_bytes(skb, tcp_hdr_len + offsetof(struct __tcphdr, ack_seq) + 4, &doff, sizeof(doff)); // read the first byte past __tcphdr->ack_seq, we can't do offsetof bit fields
|
||
doff &= 0xf0; // clean-up res1
|
||
doff >>= 4; // move the upper 4 bits to low
|
||
doff *= 4; // convert to bytes length
|
||
|
||
payload_offset = ETH_HLEN + hdr_len + doff;
|
||
payload_length = __bpf_ntohs(tlen) - hdr_len - doff;
|
||
|
||
char line_buffer[7];
|
||
if (payload_length < 7 || payload_offset < 0)
|
||
{
|
||
return 0;
|
||
}
|
||
bpf_skb_load_bytes(skb, payload_offset, line_buffer, 7);
|
||
bpf_printk("%d len %d buffer: %s", payload_offset, payload_length, line_buffer);
|
||
if (bpf_strncmp(line_buffer, 3, "GET") != 0 &&
|
||
bpf_strncmp(line_buffer, 4, "POST") != 0 &&
|
||
bpf_strncmp(line_buffer, 3, "PUT") != 0 &&
|
||
bpf_strncmp(line_buffer, 6, "DELETE") != 0 &&
|
||
bpf_strncmp(line_buffer, 4, "HTTP") != 0)
|
||
{
|
||
return 0;
|
||
}
|
||
|
||
/* reserve sample from BPF ringbuf */
|
||
e = bpf_ringbuf_reserve(&rb, sizeof(*e), 0);
|
||
if (!e)
|
||
return 0;
|
||
|
||
e->ip_proto = ip_proto;
|
||
bpf_skb_load_bytes(skb, nhoff + hdr_len, &(e->ports), 4);
|
||
e->pkt_type = skb->pkt_type;
|
||
e->ifindex = skb->ifindex;
|
||
|
||
e->payload_length = payload_length;
|
||
bpf_skb_load_bytes(skb, payload_offset, e->payload, MAX_BUF_SIZE);
|
||
|
||
bpf_skb_load_bytes(skb, nhoff + offsetof(struct iphdr, saddr), &(e->src_addr), 4);
|
||
bpf_skb_load_bytes(skb, nhoff + offsetof(struct iphdr, daddr), &(e->dst_addr), 4);
|
||
bpf_ringbuf_submit(e, 0);
|
||
|
||
return skb->len;
|
||
}
|
||
</code></pre>
|
||
<p>当分析这段eBPF程序时,我们将按照每个代码块的内容来详细解释,并提供相关的背景知识:</p>
|
||
<pre><code class="language-c">SEC("socket")
|
||
int socket_handler(struct __sk_buff *skb)
|
||
{
|
||
// ...
|
||
}
|
||
</code></pre>
|
||
<p>这是eBPF程序的入口点,它定义了一个名为 <code>socket_handler</code> 的函数,它会被内核用于处理传入的网络数据包。这个函数位于一个名为 <code>socket</code> 的 eBPF 节(section)中,表明这个程序用于套接字处理。</p>
|
||
<pre><code class="language-c">struct so_event *e;
|
||
__u8 verlen;
|
||
__u16 proto;
|
||
__u32 nhoff = ETH_HLEN;
|
||
__u32 ip_proto = 0;
|
||
__u32 tcp_hdr_len = 0;
|
||
__u16 tlen;
|
||
__u32 payload_offset = 0;
|
||
__u32 payload_length = 0;
|
||
__u8 hdr_len;
|
||
</code></pre>
|
||
<p>在这个代码块中,我们定义了一些变量来存储在处理数据包时需要的信息。这些变量包括了<code>struct so_event *e</code>用于存储事件信息,<code>verlen</code>、<code>proto</code>、<code>nhoff</code>、<code>ip_proto</code>、<code>tcp_hdr_len</code>、<code>tlen</code>、<code>payload_offset</code>、<code>payload_length</code>、<code>hdr_len</code>等用于存储数据包信息的变量。</p>
|
||
<ul>
|
||
<li><code>struct so_event *e;</code>:这是一个指向<code>so_event</code>结构体的指针,用于存储捕获到的事件信息。该结构体的具体定义在程序的其他部分。</li>
|
||
<li><code>__u8 verlen;</code>、<code>__u16 proto;</code>、<code>__u32 nhoff = ETH_HLEN;</code>:这些变量用于存储各种信息,例如协议类型、数据包偏移量等。<code>nhoff</code>初始化为以太网帧头部的长度,通常为14字节,因为以太网帧头部包括目标MAC地址、源MAC地址和帧类型字段。</li>
|
||
<li><code>__u32 ip_proto = 0;</code>:这个变量用于存储IP协议的类型,初始化为0。</li>
|
||
<li><code>__u32 tcp_hdr_len = 0;</code>:这个变量用于存储TCP头部的长度,初始化为0。</li>
|
||
<li><code>__u16 tlen;</code>:这个变量用于存储IP数据包的总长度。</li>
|
||
<li><code>__u32 payload_offset = 0;</code>、<code>__u32 payload_length = 0;</code>:这两个变量用于存储HTTP请求的载荷(payload)的偏移量和长度。</li>
|
||
<li><code>__u8 hdr_len;</code>:这个变量用于存储IP头部的长度。</li>
|
||
</ul>
|
||
<pre><code class="language-c">bpf_skb_load_bytes(skb, 12, &proto, 2);
|
||
proto = __bpf_ntohs(proto);
|
||
if (proto != ETH_P_IP)
|
||
return 0;
|
||
</code></pre>
|
||
<p>在这里,代码从数据包中加载了以太网帧的类型字段,这个字段告诉我们数据包使用的网络层协议。然后,使用<code>__bpf_ntohs</code>函数将网络字节序的类型字段转换为主机字节序。接下来,代码检查类型字段是否等于IPv4的以太网帧类型(0x0800)。如果不等于,说明这个数据包不是IPv4数据包,直接返回0,放弃处理。</p>
|
||
<p>这里需要了解以下几个概念:</p>
|
||
<ul>
|
||
<li>以太网帧(Ethernet Frame):是数据链路层(第二层)的协议,用于在局域网中传输数据帧。以太网帧通常包括目标MAC地址、源MAC地址和帧类型字段。</li>
|
||
<li>网络字节序(Network Byte Order):网络协议通常使用大端字节序(Big-Endian)来表示数据。因此,需要将从网络中接收到的数据转换为主机字节序,以便在主机上正确解释数据。</li>
|
||
<li>IPv4帧类型(ETH_P_IP):表示以太网帧中包含的协议类型字段,0x0800表示IPv4。</li>
|
||
</ul>
|
||
<pre><code class="language-c">if (ip_is_fragment(skb, nhoff))
|
||
return 0;
|
||
</code></pre>
|
||
<p>这一部分的代码检查是否处理IP分片。IP分片是将较大的IP数据包分割成多个小片段以进行传输的机制。在这里,如果数据包是IP分片,则直接返回0,表示不处理分片,只处理完整的数据包。</p>
|
||
<pre><code class="language-c">static inline int ip_is_fragment(struct __sk_buff *skb, __u32 nhoff)
|
||
{
|
||
__u16 frag_off;
|
||
|
||
bpf_skb_load_bytes(skb, nhoff + offsetof(struct iphdr, frag_off), &frag_off, 2);
|
||
frag_off = __bpf_ntohs(frag_off);
|
||
return frag_off & (IP_MF | IP_OFFSET);
|
||
}
|
||
</code></pre>
|
||
<p>上述代码是一个辅助函数,用于检查传入的IPv4数据包是否为IP分片。IP分片是一种机制,当IP数据包的大小超过了网络的最大传输单元(MTU),路由器会将其分割成多个较小的片段,以便在网络上进行传输。这个函数的目的是检查数据包的分片标志(Fragmentation Flag)以及片偏移(Fragment Offset)字段,以确定是否为分片。</p>
|
||
<p>下面是代码的逐行解释:</p>
|
||
<ol>
|
||
<li><code>__u16 frag_off;</code>:定义一个16位无符号整数变量<code>frag_off</code>,用于存储片偏移字段的值。</li>
|
||
<li><code>bpf_skb_load_bytes(skb, nhoff + offsetof(struct iphdr, frag_off), &frag_off, 2);</code>:这行代码使用<code>bpf_skb_load_bytes</code>函数从数据包中加载IPv4头部的片偏移字段(<code>frag_off</code>),并加载2个字节。<code>nhoff</code>是IPv4头部在数据包中的偏移量,<code>offsetof(struct iphdr, frag_off)</code>用于计算片偏移字段在IPv4头部中的偏移量。</li>
|
||
<li><code>frag_off = __bpf_ntohs(frag_off);</code>:将加载的片偏移字段从网络字节序(Big-Endian)转换为主机字节序。网络协议通常使用大端字节序表示数据,而主机可能使用大端或小端字节序。这里将片偏移字段转换为主机字节序,以便进一步处理。</li>
|
||
<li><code>return frag_off & (IP_MF | IP_OFFSET);</code>:这行代码通过使用位运算检查片偏移字段的值,以确定是否为IP分片。具体来说,它使用位与运算符<code>&</code>将片偏移字段与两个标志位进行位与运算:
|
||
<ul>
|
||
<li><code>IP_MF</code>:表示"更多分片"标志(More Fragments)。如果这个标志位被设置为1,表示数据包是分片的一部分,还有更多分片。</li>
|
||
<li><code>IP_OFFSET</code>:表示片偏移字段。如果片偏移字段不为0,表示数据包是分片的一部分,且具有片偏移值。
|
||
如果这两个标志位中的任何一个被设置为1,那么结果就不为零,说明数据包是IP分片。如果都为零,说明数据包不是分片。</li>
|
||
</ul>
|
||
</li>
|
||
</ol>
|
||
<p>需要注意的是,IP头部的片偏移字段以8字节为单位,所以实际的片偏移值需要左移3位来得到字节偏移。此外,IP头部的"更多分片"标志(IP_MF)表示数据包是否有更多的分片,通常与片偏移字段一起使用来指示整个数据包的分片情况。这个函数只关心这两个标志位,如果其中一个标志被设置,就认为是IP分片。</p>
|
||
<pre><code class="language-c">bpf_skb_load_bytes(skb, ETH_HLEN, &hdr_len, sizeof(hdr_len));
|
||
hdr_len &= 0x0f;
|
||
hdr_len *= 4;
|
||
</code></pre>
|
||
<p>这一部分的代码从数据包中加载IP头部的长度字段。IP头部长度字段包含了IP头部的长度信息,以4字节为单位,需要将其转换为字节数。这里通过按位与和乘以4来进行转换。</p>
|
||
<p>需要了解:</p>
|
||
<ul>
|
||
<li>IP头部(IP Header):IP头部包含了关于数据包的基本信息,如源IP地址、目标IP地址、协议类型和头部校验和等。头部长度字段(IHL,Header Length)表示IP头部的长度,以4字节为单位,通常为20字节(5个4字节的字)。</li>
|
||
</ul>
|
||
<pre><code class="language-c">if (hdr_len < sizeof(struct iphdr))
|
||
{
|
||
return 0;
|
||
}
|
||
</code></pre>
|
||
<p>这段代码检查IP头部的长度是否满足最小长度要求,通常IP头部的最小长度是20字节。如果IP头部的长度小于20字节,说明数据包不完整或损坏,直接返回0,放弃处理。</p>
|
||
<p>需要了解:</p>
|
||
<ul>
|
||
<li><code>struct iphdr</code>:这是Linux内核中定义的结构体,表示IPv4头部的格式。它包括了版本、头部长度、服务类型、总长度、</li>
|
||
</ul>
|
||
<p>标识符、标志位、片偏移、生存时间、协议、头部校验和、源IP地址和目标IP地址等字段。</p>
|
||
<pre><code class="language-c">bpf_skb_load_bytes(skb, nhoff + offsetof(struct iphdr, protocol), &ip_proto, 1);
|
||
if (ip_proto != IPPROTO_TCP)
|
||
{
|
||
return 0;
|
||
}
|
||
</code></pre>
|
||
<p>在这里,代码从数据包中加载IP头部中的协议字段,以确定数据包使用的传输层协议。然后,它检查协议字段是否为TCP协议(IPPROTO_TCP)。如果不是TCP协议,说明不是HTTP请求或响应,直接返回0。</p>
|
||
<p>需要了解:</p>
|
||
<ul>
|
||
<li>传输层协议:IP头部中的协议字段指示了数据包所使用的传输层协议,例如TCP、UDP或ICMP。</li>
|
||
</ul>
|
||
<pre><code class="language-c">tcp_hdr_len = nhoff + hdr_len;
|
||
</code></pre>
|
||
<p>这行代码计算了TCP头部的偏移量。它将以太网帧头部的长度(<code>nhoff</code>)与IP头部的长度(<code>hdr_len</code>)相加,得到TCP头部的起始位置。</p>
|
||
<pre><code class="language-c">bpf_skb_load_bytes(skb, nhoff + 0, &verlen, 1);
|
||
</code></pre>
|
||
<p>这行代码从数据包中加载TCP头部的第一个字节,该字节包含了TCP头部长度信息。这个长度字段以4字节为单位,需要进行后续的转换。</p>
|
||
<pre><code class="language-c">bpf_skb_load_bytes(skb, nhoff + offsetof(struct iphdr, tot_len), &tlen, sizeof(tlen));
|
||
</code></pre>
|
||
<p>这行代码从数据包中加载IP头部的总长度字段。IP头部总长度字段表示整个IP数据包的长度,包括IP头部和数据部分。</p>
|
||
<pre><code class="language-c">__u8 doff;
|
||
bpf_skb_load_bytes(skb, tcp_hdr_len + offsetof(struct __tcphdr, ack_seq) + 4, &doff, sizeof(doff));
|
||
doff &= 0xf0;
|
||
doff >>= 4;
|
||
doff *= 4;
|
||
</code></pre>
|
||
<p>这段代码用于计算TCP头部的长度。它加载TCP头部中的数据偏移字段(Data Offset,也称为头部长度字段),该字段表示TCP头部的长度以4字节为单位。代码将偏移字段的高四位清零,然后将其右移4位,最后乘以4,得到TCP头部的实际长度。</p>
|
||
<p>需要了解:</p>
|
||
<ul>
|
||
<li>TCP头部(TCP Header):TCP头部包含了TCP协议相关的信息,如源端口、目标端口、序列号、确认号、标志位(如SYN、ACK、FIN等)、窗口大小和校验和等。</li>
|
||
</ul>
|
||
<pre><code class="language-c">payload_offset = ETH_HLEN + hdr_len + doff;
|
||
payload_length = __bpf_ntohs(tlen) - hdr_len - doff;
|
||
</code></pre>
|
||
<p>这两行代码计算HTTP请求的载荷(payload)的偏移量和长度。它们将以太网帧头部长度、IP头部长度和TCP头部长度相加,得到HTTP请求的数据部分的偏移量,然后通过减去总长度、IP头部长度和TCP头部长度,计算出HTTP请求数据的长度。</p>
|
||
<p>需要了解:</p>
|
||
<ul>
|
||
<li>HTTP请求载荷(Payload):HTTP请求中包含的实际数据部分,通常是HTTP请求头和请求体。</li>
|
||
</ul>
|
||
<pre><code class="language-c">char line_buffer[7];
|
||
if (payload_length < 7 || payload_offset < 0)
|
||
{
|
||
return 0;
|
||
}
|
||
bpf_skb_load_bytes(skb, payload_offset, line_buffer, 7);
|
||
bpf_printk("%d len %d buffer: %s", payload_offset, payload_length, line_buffer);
|
||
</code></pre>
|
||
<p>这部分代码用于加载HTTP请求行的前7个字节,存储在名为<code>line_buffer</code>的字符数组中。然后,它检查HTTP请求数据的长度是否小于7字节或偏移量是否为负数,如果满足这些条件,说明HTTP请求不完整,直接返回0。最后,它使用<code>bpf_printk</code>函数将HTTP请求行的内容打印到内核日志中,以供调试和分析。</p>
|
||
<pre><code class="language-c">if (bpf_strncmp(line_buffer, 3, "GET") != 0 &&
|
||
bpf_strncmp(line_buffer, 4, "POST") != 0 &&
|
||
bpf_strncmp(line_buffer, 3, "PUT") != 0 &&
|
||
bpf_strncmp(line_buffer, 6, "DELETE") != 0 &&
|
||
bpf_strncmp(line_buffer, 4, "HTTP") != 0)
|
||
{
|
||
return 0;
|
||
}
|
||
</code></pre>
|
||
<blockquote>
|
||
<p>注意:bpf_strncmp 这个内核 helper 在 5.17 版本中才被引入,如果你的内核版本低于 5.17,可以手动匹配字符串来实现相同的功能。</p>
|
||
</blockquote>
|
||
<p>这段代码使用<code>bpf_strncmp</code>函数比较<code>line_buffer</code>中的数据与HTTP请求方法(GET、POST、PUT、DELETE、HTTP)是否匹配。如果不匹配,说明不是HTTP请求,直接返回0,放弃处理。</p>
|
||
<pre><code class="language-c">e = bpf_ringbuf_reserve(&rb, sizeof(*e), 0);
|
||
if (!e)
|
||
return 0;
|
||
</code></pre>
|
||
<p>这部分代码尝试从BPF环形缓冲区中保留一块内存以存储事件信息。如果无法保留内存块,返回0。BPF环形缓冲区用于在eBPF程序和用户空间之间传递事件数据。</p>
|
||
<p>需要了解:</p>
|
||
<ul>
|
||
<li>BPF环形缓冲区:BPF环形缓冲区是一种在eBPF程序和用户空间之间传递数据的机制。它可以用来存储事件信息,以便用户空间应用程序进行进一步处理或分析。</li>
|
||
</ul>
|
||
<pre><code class="language-c">e->ip_proto = ip_proto;
|
||
bpf_skb_load_bytes(skb, nhoff + hdr_len, &(e->ports), 4);
|
||
e->pkt_type = skb->pkt_type;
|
||
e->ifindex = skb->ifindex;
|
||
|
||
e->payload_length = payload_length;
|
||
bpf_skb_load_bytes(skb, payload_offset, e->payload, MAX_BUF_SIZE);
|
||
|
||
bpf_skb_load_bytes(skb, nhoff + offsetof(struct iphdr, saddr), &(e->src_addr), 4);
|
||
bpf_skb_load_bytes(skb, nhoff + offsetof(struct iphdr, daddr), &(e->dst_addr), 4);
|
||
bpf_ringbuf_submit(e, 0);
|
||
|
||
return skb->len;
|
||
</code></pre>
|
||
<p>最后,这段代码将捕获到的事件信息存储在<code>e</code>结构体中,并将</p>
|
||
<p>其提交到BPF环形缓冲区。它包括了捕获的IP协议、源端口和目标端口、数据包类型、接口索引、载荷长度、源IP地址和目标IP地址等信息。最后,它返回数据包的长度,表示成功处理了数据包。</p>
|
||
<p>这段代码主要用于将捕获的事件信息存储起来,以便后续的处理和分析。 BPF环形缓冲区用于将这些信息传递到用户空间,供用户空间应用程序进一步处理或记录。</p>
|
||
<p>总结:这段eBPF程序的主要任务是捕获HTTP请求,它通过解析数据包的以太网帧、IP头部和TCP头部来确定数据包是否包含HTTP请求,并将有关请求的信息存储在<code>so_event</code>结构体中,然后提交到BPF环形缓冲区。这是一种高效的方法,可以在内核层面捕获HTTP流量,适用于网络监控和安全分析等应用。</p>
|
||
<h3 id="潜在缺陷"><a class="header" href="#潜在缺陷">潜在缺陷</a></h3>
|
||
<p>上述代码也存在一些潜在的缺陷,其中一个主要缺陷是它无法处理跨多个数据包的URL。</p>
|
||
<ul>
|
||
<li>跨包URL:代码中通过解析单个数据包来检查HTTP请求中的URL,如果HTTP请求的URL跨足够多的数据包,那么只会检查第一个数据包中的URL部分。这会导致丢失或部分记录那些跨多个数据包的长URL。</li>
|
||
</ul>
|
||
<p>解决这个问题的方法通常需要对多个数据包进行重新组装,以还原完整的HTTP请求。这可能需要在eBPF程序中实现数据包的缓存和组装逻辑,并在检测到HTTP请求结束之前等待并收集所有相关数据包。这需要更复杂的逻辑和额外的内存来处理跨多个数据包的情况。</p>
|
||
<h3 id="用户态代码"><a class="header" href="#用户态代码">用户态代码</a></h3>
|
||
<p>用户态代码的主要目的是创建一个原始套接字(raw socket),然后将先前在内核中定义的eBPF程序附加到该套接字上,从而允许eBPF程序捕获和处理从该套接字接收到的网络数据包,例如:</p>
|
||
<pre><code class="language-c"> /* Create raw socket for localhost interface */
|
||
sock = open_raw_sock(interface);
|
||
if (sock < 0) {
|
||
err = -2;
|
||
fprintf(stderr, "Failed to open raw socket\n");
|
||
goto cleanup;
|
||
}
|
||
|
||
/* Attach BPF program to raw socket */
|
||
prog_fd = bpf_program__fd(skel->progs.socket_handler);
|
||
if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd, sizeof(prog_fd))) {
|
||
err = -3;
|
||
fprintf(stderr, "Failed to attach to raw socket\n");
|
||
goto cleanup;
|
||
}
|
||
</code></pre>
|
||
<ol>
|
||
<li><code>sock = open_raw_sock(interface);</code>:这行代码调用了一个自定义的函数<code>open_raw_sock</code>,该函数用于创建一个原始套接字。原始套接字允许用户态应用程序直接处理网络数据包,而不经过协议栈的处理。函数<code>open_raw_sock</code>可能需要一个参数 <code>interface</code>,用于指定网络接口,以便确定从哪个接口接收数据包。如果创建套接字失败,它将返回一个负数,否则返回套接字的文件描述符<code>sock</code>。</li>
|
||
<li>如果<code>sock</code>的值小于0,表示打开原始套接字失败,那么将<code>err</code>设置为-2,并在标准错误流上输出一条错误信息。</li>
|
||
<li><code>prog_fd = bpf_program__fd(skel->progs.socket_handler);</code>:这行代码获取之前在eBPF程序定义中的套接字过滤器程序(<code>socket_handler</code>)的文件描述符,以便后续将它附加到套接字上。<code>skel</code>是一个eBPF程序对象的指针,可以通过它来访问程序集合。</li>
|
||
<li><code>setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd, sizeof(prog_fd))</code>:这行代码使用<code>setsockopt</code>系统调用将eBPF程序附加到原始套接字。它设置了<code>SO_ATTACH_BPF</code>选项,将eBPF程序的文件描述符传递给该选项,以便内核知道要将哪个eBPF程序应用于这个套接字。如果附加成功,套接字将开始捕获和处理从中接收到的网络数据包。</li>
|
||
<li>如果<code>setsockopt</code>失败,它将<code>err</code>设置为-3,并在标准错误流上输出一条错误信息。</li>
|
||
</ol>
|
||
<h3 id="编译运行"><a class="header" href="#编译运行">编译运行</a></h3>
|
||
<p>完整的源代码可以在 <a href="https://github.com/eunomia-bpf/bpf-developer-tutorial/tree/main/src/23-http">https://github.com/eunomia-bpf/bpf-developer-tutorial/tree/main/src/23-http</a> 中找到。关于如何安装依赖,请参考:<a href="https://eunomia.dev/tutorials/11-bootstrap/">https://eunomia.dev/tutorials/11-bootstrap/</a> 编译运行上述代码:</p>
|
||
<pre><code class="language-console">$ git submodule update --init --recursive
|
||
$ make
|
||
BPF .output/sockfilter.bpf.o
|
||
GEN-SKEL .output/sockfilter.skel.h
|
||
CC .output/sockfilter.o
|
||
BINARY sockfilter
|
||
$ sudo ./sockfilter
|
||
...
|
||
</code></pre>
|
||
<p>在另外一个窗口中,使用 python 启动一个简单的 web server:</p>
|
||
<pre><code class="language-console">python3 -m http.server
|
||
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
|
||
127.0.0.1 - - [18/Sep/2023 01:05:52] "GET / HTTP/1.1" 200 -
|
||
</code></pre>
|
||
<p>可以使用 curl 发起请求:</p>
|
||
<pre><code class="language-c">$ curl http://0.0.0.0:8000/
|
||
<!DOCTYPE HTML>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<title>Directory listing for /</title>
|
||
....
|
||
</code></pre>
|
||
<p>在 eBPF 程序中,可以看到打印出了 HTTP 请求的内容:</p>
|
||
<pre><code class="language-console">127.0.0.1:34552(src) -> 127.0.0.1:8000(dst)
|
||
payload: GET / HTTP/1.1
|
||
Host: 0.0.0.0:8000
|
||
User-Agent: curl/7.88.1
|
||
...
|
||
127.0.0.1:8000(src) -> 127.0.0.1:34552(dst)
|
||
payload: HTTP/1.0 200 OK
|
||
Server: SimpleHTTP/0.6 Python/3.11.4
|
||
...
|
||
</code></pre>
|
||
<p>分别包含了请求和响应的内容。</p>
|
||
<h2 id="使用-ebpf-syscall-tracepoint-来捕获-http-流量"><a class="header" href="#使用-ebpf-syscall-tracepoint-来捕获-http-流量">使用 eBPF syscall tracepoint 来捕获 HTTP 流量</a></h2>
|
||
<p>eBPF 提供了一种强大的机制,允许我们在内核级别追踪系统调用。在这个示例中,我们将使用 eBPF 追踪 accept 和 read 系统调用,以捕获 HTTP 流量。由于篇幅有限,这里我们仅仅对代码框架做简要的介绍。</p>
|
||
<pre><code class="language-c">struct
|
||
{
|
||
__uint(type, BPF_MAP_TYPE_HASH);
|
||
__uint(max_entries, 4096);
|
||
__type(key, u64);
|
||
__type(value, struct accept_args_t);
|
||
} active_accept_args_map SEC(".maps");
|
||
|
||
// 定义在 accept 系统调用入口的追踪点
|
||
SEC("tracepoint/syscalls/sys_enter_accept")
|
||
int sys_enter_accept(struct trace_event_raw_sys_enter *ctx)
|
||
{
|
||
u64 id = bpf_get_current_pid_tgid();
|
||
// ... 获取和存储 accept 调用的参数
|
||
bpf_map_update_elem(&active_accept_args_map, &id, &accept_args, BPF_ANY);
|
||
return 0;
|
||
}
|
||
|
||
// 定义在 accept 系统调用退出的追踪点
|
||
SEC("tracepoint/syscalls/sys_exit_accept")
|
||
int sys_exit_accept(struct trace_event_raw_sys_exit *ctx)
|
||
{
|
||
// ... 处理 accept 调用的结果
|
||
struct accept_args_t *args =
|
||
bpf_map_lookup_elem(&active_accept_args_map, &id);
|
||
// ... 获取和存储 accept 调用获得的 socket 文件描述符
|
||
__u64 pid_fd = ((__u64)pid << 32) | (u32)ret_fd;
|
||
bpf_map_update_elem(&conn_info_map, &pid_fd, &conn_info, BPF_ANY);
|
||
// ...
|
||
}
|
||
|
||
struct
|
||
{
|
||
__uint(type, BPF_MAP_TYPE_HASH);
|
||
__uint(max_entries, 4096);
|
||
__type(key, u64);
|
||
__type(value, struct data_args_t);
|
||
} active_read_args_map SEC(".maps");
|
||
|
||
// 定义在 read 系统调用入口的追踪点
|
||
SEC("tracepoint/syscalls/sys_enter_read")
|
||
int sys_enter_read(struct trace_event_raw_sys_enter *ctx)
|
||
{
|
||
// ... 获取和存储 read 调用的参数
|
||
bpf_map_update_elem(&active_read_args_map, &id, &read_args, BPF_ANY);
|
||
return 0;
|
||
}
|
||
|
||
// 辅助函数,检查是否为 HTTP 连接
|
||
static inline bool is_http_connection(const char *line_buffer, u64 bytes_count)
|
||
{
|
||
// ... 检查数据是否为 HTTP 请求或响应
|
||
}
|
||
|
||
// 辅助函数,处理读取的数据
|
||
static inline void process_data(struct trace_event_raw_sys_exit *ctx,
|
||
u64 id, const struct data_args_t *args, u64 bytes_count)
|
||
{
|
||
// ... 处理读取的数据,检查是否为 HTTP 流量,并发送事件
|
||
if (is_http_connection(line_buffer, bytes_count))
|
||
{
|
||
// ...
|
||
bpf_probe_read_kernel(&event.msg, read_size, args->buf);
|
||
// ...
|
||
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU,
|
||
&event, sizeof(struct socket_data_event_t));
|
||
}
|
||
}
|
||
|
||
// 定义在 read 系统调用退出的追踪点
|
||
SEC("tracepoint/syscalls/sys_exit_read")
|
||
int sys_exit_read(struct trace_event_raw_sys_exit *ctx)
|
||
{
|
||
// ... 处理 read 调用的结果
|
||
struct data_args_t *read_args = bpf_map_lookup_elem(&active_read_args_map, &id);
|
||
if (read_args != NULL)
|
||
{
|
||
process_data(ctx, id, read_args, bytes_count);
|
||
}
|
||
// ...
|
||
return 0;
|
||
}
|
||
|
||
char _license[] SEC("license") = "GPL";
|
||
</code></pre>
|
||
<p>这段代码简要展示了如何使用eBPF追踪Linux内核中的系统调用来捕获HTTP流量。以下是对代码的hook位置和流程的详细解释,以及需要hook哪些系统调用来实现完整的请求追踪:</p>
|
||
<h3 id="hook-位置和流程"><a class="header" href="#hook-位置和流程"><strong>Hook 位置和流程</strong></a></h3>
|
||
<ul>
|
||
<li>
|
||
<p>该代码使用了eBPF的Tracepoint功能,具体来说,它定义了一系列的eBPF程序,并将它们绑定到了特定的系统调用的Tracepoint上,以捕获这些系统调用的入口和退出事件。</p>
|
||
</li>
|
||
<li>
|
||
<p>首先,它定义了两个eBPF哈希映射(<code>active_accept_args_map</code>和<code>active_read_args_map</code>)来存储系统调用参数。这些映射用于跟踪<code>accept</code>和<code>read</code>系统调用。</p>
|
||
</li>
|
||
<li>
|
||
<p>接着,它定义了多个Tracepoint追踪程序,其中包括:</p>
|
||
<ul>
|
||
<li><code>sys_enter_accept</code>:定义在<code>accept</code>系统调用的入口处,用于捕获<code>accept</code>系统调用的参数,并将它们存储在哈希映射中。</li>
|
||
<li><code>sys_exit_accept</code>:定义在<code>accept</code>系统调用的退出处,用于处理<code>accept</code>系统调用的结果,包括获取和存储新的套接字文件描述符以及建立连接的相关信息。</li>
|
||
<li><code>sys_enter_read</code>:定义在<code>read</code>系统调用的入口处,用于捕获<code>read</code>系统调用的参数,并将它们存储在哈希映射中。</li>
|
||
<li><code>sys_exit_read</code>:定义在<code>read</code>系统调用的退出处,用于处理<code>read</code>系统调用的结果,包括检查读取的数据是否为HTTP流量,如果是,则发送事件。</li>
|
||
</ul>
|
||
</li>
|
||
<li>
|
||
<p>在<code>sys_exit_accept</code>和<code>sys_exit_read</code>中,还涉及一些数据处理和事件发送的逻辑,例如检查数据是否为HTTP连接,组装事件数据,并使用<code>bpf_perf_event_output</code>将事件发送到用户空间供进一步处理。</p>
|
||
</li>
|
||
</ul>
|
||
<h3 id="需要-hook-的完整系统调用"><a class="header" href="#需要-hook-的完整系统调用"><strong>需要 Hook 的完整系统调用</strong></a></h3>
|
||
<p>要实现完整的HTTP请求追踪,通常需要hook的系统调用包括:</p>
|
||
<ul>
|
||
<li><code>socket</code>:用于捕获套接字创建,以追踪新的连接。</li>
|
||
<li><code>bind</code>:用于获取绑定的端口信息。</li>
|
||
<li><code>listen</code>:用于开始监听连接请求。</li>
|
||
<li><code>accept</code>:用于接受连接请求,获取新的套接字文件描述符。</li>
|
||
<li><code>read</code>:用于捕获接收到的数据,以检查其中是否包含 HTTP 请求。</li>
|
||
<li><code>write</code>:用于捕获发送的数据,以检查其中是否包含 HTTP 响应。</li>
|
||
</ul>
|
||
<p>上述代码已经涵盖了<code>accept</code>和<code>read</code>系统调用的追踪。要完整实现HTTP请求的追踪,还需要hook其他系统调用,并实现相应的逻辑来处理这些系统调用的参数和结果。</p>
|
||
<p>完整的源代码可以在 <a href="https://github.com/eunomia-bpf/bpf-developer-tutorial/tree/main/src/23-http">https://github.com/eunomia-bpf/bpf-developer-tutorial/tree/main/src/23-http</a> 中找到。</p>
|
||
<h2 id="总结"><a class="header" href="#总结">总结</a></h2>
|
||
<p>在当今复杂的技术环境中,系统的可观测性变得至关重要,特别是在微服务和云原生应用程序的背景下。本文探讨了如何利用eBPF技术来追踪七层协议,以及在这个过程中可能面临的挑战和解决方案。以下是对本文内容的总结:</p>
|
||
<ol>
|
||
<li>
|
||
<p><strong>背景介绍</strong>:</p>
|
||
<ul>
|
||
<li>现代应用程序通常由多个微服务和分布式组件组成,因此观测整个系统的行为至关重要。</li>
|
||
<li>七层协议(如HTTP、gRPC、MQTT等)提供了深入了解应用程序交互的详细信息,但监控这些协议通常具有挑战性。</li>
|
||
</ul>
|
||
</li>
|
||
<li>
|
||
<p><strong>eBPF技术的作用</strong>:</p>
|
||
<ul>
|
||
<li>eBPF允许开发者在不修改或插入应用程序代码的情况下,深入内核层来实时观测和分析系统行为。</li>
|
||
<li>eBPF技术为监控七层协议提供了一个强大的工具,特别适用于微服务环境。</li>
|
||
</ul>
|
||
</li>
|
||
<li>
|
||
<p><strong>追踪七层协议</strong>:</p>
|
||
<ul>
|
||
<li>本文介绍了如何追踪HTTP等七层协议的挑战,包括协议的复杂性和动态性。</li>
|
||
<li>传统的网络监控工具难以应对七层协议的复杂性。</li>
|
||
</ul>
|
||
</li>
|
||
<li>
|
||
<p><strong>eBPF的应用</strong>:</p>
|
||
<ul>
|
||
<li>eBPF提供两种主要方法来追踪七层协议:socket filter和syscall trace。</li>
|
||
<li>这两种方法可以帮助捕获HTTP等协议的网络请求数据,并分析它们。</li>
|
||
</ul>
|
||
</li>
|
||
<li>
|
||
<p><strong>eBPF实践教程</strong>:</p>
|
||
<ul>
|
||
<li>本文提供了一个实际的eBPF教程,演示如何使用eBPF socket filter或syscall trace来捕获和分析HTTP流量。</li>
|
||
<li>教程内容包括开发eBPF程序、使用eBPF工具链和实施HTTP请求的追踪。</li>
|
||
</ul>
|
||
</li>
|
||
</ol>
|
||
<p>通过这篇文章,读者可以获得深入了解如何使用eBPF技术来追踪七层协议,尤其是HTTP流量的知识。这将有助于更好地监控和分析网络流量,从而提高应用程序性能和安全性。如果您希望学习更多关于 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/zh/tutorials/">https://eunomia.dev/zh/tutorials/</a> 以获取更多示例和完整的教程。</p>
|
||
<blockquote>
|
||
<p>原文地址:<a href="https://eunomia.dev/zh/tutorials/23-http/">https://eunomia.dev/zh/tutorials/23-http/</a> 转载请注明出处。</p>
|
||
</blockquote>
|
||
|
||
</main>
|
||
|
||
<nav class="nav-wrapper" aria-label="Page navigation">
|
||
<!-- Mobile navigation buttons -->
|
||
<a rel="prev" href="../30-sslsniff/index.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="../29-sockops/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="../30-sslsniff/index.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="../29-sockops/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>
|