Skip to content

Commit e571a90

Browse files
committed
xdp-log: Do not mention PerfEventArray
The xdp-log example is using aya-log for logging the data. PerfEventArray is described in aya-rs#93 instead. Signed-off-by: Michal Rostecki <vadorovsky@gmail.com>
1 parent f001417 commit e571a90

5 files changed

Lines changed: 100 additions & 189 deletions

File tree

docs/book/start/logging-packets.md

Lines changed: 0 additions & 184 deletions
This file was deleted.

docs/book/start/parsing-packets.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Parsing packets
2+
3+
In the previous chapter, our XDP application ran until Ctrl-C was hit and
4+
permitted all the traffic. Each time a packet was received, the BPF program
5+
created a logged the string "received a packet" for each packet received. In
6+
this chapter we're
7+
going to show how to parse packets.
8+
9+
While we could go all out and extract data all the way up to L7, we'll constrain
10+
our example to L3, and to make things easier, IPv4 only.
11+
12+
!!! example "Source code"
13+
14+
Full code for the example in this chapter is available
15+
[here](https://github.com/aya-rs/book/tree/main/examples/xdp-log)
16+
17+
## Using network types
18+
19+
We're going to log the source ip address of incoming packets. So we'll need to:
20+
21+
* Read the ethernet header to determine if we're dealing with an IPv4 packet,
22+
else terminate parsing.
23+
* Read the source IP Address from the IPv4 header.
24+
25+
We could read the specifications of those protocols and parse manually, but
26+
instead we're going to use the [network-types](https://crates.io/crates/network-types)
27+
crate which provides convenient type definitions for many of the common Internet
28+
protocols.
29+
30+
Let's add it to our eBPF crate by adding a dependency on `network-types` in our
31+
`xdp-log-ebpf/Cargo.toml`:
32+
33+
=== "xdp-log-ebpf/Cargo.toml"
34+
35+
```toml linenums="1"
36+
--8<-- "examples/xdp-log/xdp-log-ebpf/Cargo.toml"
37+
```
38+
39+
## Getting packet data from the context and into the map
40+
41+
`XdpContext` contains two fields that we're going to use: data and data_end,
42+
which are respectively a pointer to the beginning and to the end of the packet.
43+
44+
In order to access the data in the packet and to ensure that we do so in a way
45+
that keeps the eBPF verifier happy, we're going to introduce an helper function
46+
called `ptr_at`. The function ensure that before we access any data, we insert
47+
the bound checks which are required by the verifier.
48+
49+
Finally to access individual fields from the ethernet and IPv4 headers, we're
50+
going to use the memoffset crate, let's add a dependency for it in
51+
`xdp-log-ebpf/Cargo.toml`.
52+
53+
To do this efficiently we'll add a dependency on `memoffset = "0.8"` in our
54+
`myapp-ebpf/Cargo.toml`
55+
56+
!!! tip "Reading fields using `offset_of!`"
57+
58+
As there is limited stack space, it's more memory efficient to use the
59+
`offset_of!` macro to read a single field from a struct, rather than reading
60+
the whole struct and accessing the field by name.
61+
62+
The resulting code looks like this:
63+
64+
```rust linenums="1" title="xdp-log-ebpf/src/main.rs"
65+
--8<-- "examples/xdp-log/xdp-log-ebpf/src/main.rs"
66+
```
67+
68+
1. Create our map.
69+
2. Here's `ptr_at`, which gives ensures packet access is bounds checked.
70+
3. Using `ptr_at` to read our ethernet header.
71+
4. Logging the IP address and port.
72+
73+
Don't forget to rebuild your eBPF program!
74+
75+
## User-space component
76+
77+
Our user-space code doesn't really differ from the previous chapter, but for the
78+
reference, here's the code:
79+
80+
```rust linenums="1" title="xdp-log/src/main.rs"
81+
--8<-- "examples/xdp-log/xdp-log/src/main.rs"
82+
```
83+
84+
## Running the program
85+
86+
As before, the interface can be overwritten by providing the interface name as a
87+
parameter, for example, `RUST_LOG=info cargo xtask run -- --iface wlp2s0`.
88+
89+
```console
90+
$ RUST_LOG=info cargo xtask run
91+
[2022-12-22T11:32:21Z INFO xdp_log] SRC IP: 172.52.22.104, SRC PORT: 443
92+
[2022-12-22T11:32:21Z INFO xdp_log] SRC IP: 172.52.22.104, SRC PORT: 443
93+
[2022-12-22T11:32:21Z INFO xdp_log] SRC IP: 172.52.22.104, SRC PORT: 443
94+
[2022-12-22T11:32:21Z INFO xdp_log] SRC IP: 172.52.22.104, SRC PORT: 443
95+
[2022-12-22T11:32:21Z INFO xdp_log] SRC IP: 234.130.159.162, SRC PORT: 443
96+
```

examples/xdp-log/xdp-log-ebpf/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ use aya_log_ebpf::info;
77
use core::mem;
88
use network_types::{
99
eth::{EthHdr, EtherType},
10-
ip::{Ipv4Hdr, IpProto},
10+
ip::{IpProto, Ipv4Hdr},
1111
tcp::TcpHdr,
1212
udp::UdpHdr,
1313
};
1414

15-
1615
#[panic_handler]
1716
fn panic(_info: &core::panic::PanicInfo) -> ! {
1817
unsafe { core::hint::unreachable_unchecked() }
@@ -40,7 +39,7 @@ unsafe fn ptr_at<T>(ctx: &XdpContext, offset: usize) -> Result<*const T, ()> {
4039
}
4140

4241
fn try_xdp_firewall(ctx: XdpContext) -> Result<u32, ()> {
43-
let ethhdr: *const EthHdr = unsafe { ptr_at(&ctx, 0)? };
42+
let ethhdr: *const EthHdr = unsafe { ptr_at(&ctx, 0)? }; // (3)
4443
match unsafe { (*ethhdr).ether_type } {
4544
EtherType::Ipv4 => {}
4645
_ => return Ok(xdp_action::XDP_PASS),
@@ -63,6 +62,7 @@ fn try_xdp_firewall(ctx: XdpContext) -> Result<u32, ()> {
6362
_ => return Err(()),
6463
};
6564

65+
// (4)
6666
info!(
6767
&ctx,
6868
"SRC IP: {:ipv4}, SRC PORT: {}", source_addr, source_port

examples/xdp-log/xdp-log/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ async fn main() -> Result<(), anyhow::Error> {
3434
// This can happen if you remove all log statements from your eBPF program.
3535
warn!("failed to initialize eBPF logger: {}", e);
3636
}
37-
// (1)
3837
let program: &mut Xdp = bpf.program_mut("xdp").unwrap().try_into()?;
3938
program.load()?;
4039
program.attach(&opt.iface, XdpFlags::default())

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ nav:
6565
- A Simple XDP Program:
6666
- book/start/index.md
6767
- Hello XDP!: book/start/hello-xdp.md
68-
- Logging Packets: book/start/logging-packets.md
68+
- Parsing Packets: book/start/parsing-packets.md
6969
- Dropping Packets: book/start/dropping-packets.md
7070
- Working With Aya:
7171
- book/aya/index.md

0 commit comments

Comments
 (0)