This commit is contained in:
Officeyutong
2024-02-22 13:14:00 +00:00
parent 403aff5b66
commit 55d5e641bf
47 changed files with 1483 additions and 1918 deletions

View File

@@ -192,7 +192,7 @@
</ul>
<p>下面,我们将通过一段代码示例,详细展示如何利用 user ring buffer实现从用户态向内核传送数据并以 kernel ring buffer 相应地从内核态向用户态传送数据。</p>
<h2 id="一实现在用户态和内核态间使用-ring-buffer-传送数据"><a class="header" href="#一实现在用户态和内核态间使用-ring-buffer-传送数据">一、实现:在用户态和内核态间使用 ring buffer 传送数据</a></h2>
<p>借助新的 BPF MAP我们可以实现在用户态和内核态间通过环形缓冲区传送数据。在这个示例中我们将详细说明如何在用户空间创建一个 &quot;用户环形缓冲区&quot; (user ring buffer) 并向其写入数据,然后在内核空间中通过 <code>bpf_user_ringbuf_drain</code> 函数来消费这些数据。同时,我们也会使用 &quot;内核环形缓冲区&quot; (kernel ring buffer) 来从内核空间反馈数据到用户空间。为此,我们需要在用户空间和内核空间分别创建并操作这两个环形缓冲区。</p>
<p>借助新的 BPF MAP我们可以实现在用户态和内核态间通过环形缓冲区传送数据。在这个示例中我们将详细说明如何在用户空间创建一个 "用户环形缓冲区" (user ring buffer) 并向其写入数据,然后在内核空间中通过 <code>bpf_user_ringbuf_drain</code> 函数来消费这些数据。同时,我们也会使用 "内核环形缓冲区" (kernel ring buffer) 来从内核空间反馈数据到用户空间。为此,我们需要在用户空间和内核空间分别创建并操作这两个环形缓冲区。</p>
<p>完整的代码可以在 <a href="https://github.com/eunomia-bpf/bpf-developer-tutorial/tree/main/src/35-user-ringbuf">https://github.com/eunomia-bpf/bpf-developer-tutorial/tree/main/src/35-user-ringbuf</a> 中找到。</p>
<h3 id="创建环形缓冲区"><a class="header" href="#创建环形缓冲区">创建环形缓冲区</a></h3>
<p>在内核空间,我们创建了一个类型为 <code>BPF_MAP_TYPE_USER_RINGBUF</code><code>user_ringbuf</code>,以及一个类型为 <code>BPF_MAP_TYPE_RINGBUF</code><code>kernel_ringbuf</code>。在用户空间,我们创建了一个 <code>struct ring_buffer_user</code> 结构体的实例,并通过 <code>ring_buffer_user__new</code> 函数和对应的操作来管理这个用户环形缓冲区。</p>
@@ -201,7 +201,7 @@
if (!rb)
{
err = -1;
fprintf(stderr, &quot;Failed to create ring buffer\n&quot;);
fprintf(stderr, "Failed to create ring buffer\n");
goto cleanup;
}
user_ringbuf = user_ring_buffer__new(bpf_map__fd(skel-&gt;maps.user_ringbuf), NULL);
@@ -211,25 +211,25 @@
<pre><code class="language-c">// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
#include &quot;vmlinux.h&quot;
#include "vmlinux.h"
#include &lt;bpf/bpf_helpers.h&gt;
#include &lt;bpf/bpf_tracing.h&gt;
#include &lt;bpf/bpf_core_read.h&gt;
#include &quot;user_ringbuf.h&quot;
#include "user_ringbuf.h"
char _license[] SEC(&quot;license&quot;) = &quot;GPL&quot;;
char _license[] SEC("license") = "GPL";
struct
{
__uint(type, BPF_MAP_TYPE_USER_RINGBUF);
__uint(max_entries, 256 * 1024);
} user_ringbuf SEC(&quot;.maps&quot;);
} user_ringbuf SEC(".maps");
struct
{
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 256 * 1024);
} kernel_ringbuf SEC(&quot;.maps&quot;);
} kernel_ringbuf SEC(".maps");
int read = 0;
@@ -255,7 +255,7 @@ do_nothing_cb(struct bpf_dynptr *dynptr, void *context)
return 0;
}
SEC(&quot;tracepoint/syscalls/sys_exit_kill&quot;)
SEC("tracepoint/syscalls/sys_exit_kill")
int kill_exit(struct trace_event_raw_sys_exit *ctx)
{
long num_samples;
@@ -282,9 +282,9 @@ int kill_exit(struct trace_event_raw_sys_exit *ctx)
}
entry-&gt;i = getpid();
strcpy(entry-&gt;comm, &quot;hello&quot;);
strcpy(entry-&gt;comm, "hello");
int read = snprintf(entry-&gt;comm, sizeof(entry-&gt;comm), &quot;%u&quot;, i);
int read = snprintf(entry-&gt;comm, sizeof(entry-&gt;comm), "%u", i);
if (read &lt;= 0)
{
/* Assert on the error path to avoid spamming logs with
@@ -308,8 +308,8 @@ done:
<pre><code class="language-c"> write_samples(user_ringbuf);
/* Process events */
printf(&quot;%-8s %-5s %-16s %-7s %-7s %s\n&quot;,
&quot;TIME&quot;, &quot;EVENT&quot;, &quot;COMM&quot;, &quot;PID&quot;, &quot;PPID&quot;, &quot;FILENAME/EXIT CODE&quot;);
printf("%-8s %-5s %-16s %-7s %-7s %s\n",
"TIME", "EVENT", "COMM", "PID", "PPID", "FILENAME/EXIT CODE");
while (!exiting)
{
err = ring_buffer__poll(rb, 100 /* timeout, ms */);
@@ -321,7 +321,7 @@ done:
}
if (err &lt; 0)
{
printf(&quot;Error polling perf buffer: %d\n&quot;, err);
printf("Error polling perf buffer: %d\n", err);
break;
}
}