Skip to content

Commit bc31c8b

Browse files
author
Michael Agun
committed
Document API changes in docs/
1 parent 9c71c7d commit bc31c8b

File tree

2 files changed

+98
-12
lines changed

2 files changed

+98
-12
lines changed

docs/PerfEventArray.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
This document describes the in-progress support for the bpf map type BPF_MAP_TYPE_PERF_EVENT_ARRAY ([#658](https://github.com/microsoft/ebpf-for-windows/issues/658)).
44

5+
NOTE: With [#4640](https://github.com/microsoft/ebpf-for-windows/pull/4640) The default behavior has been changed to be linux-compatible.
6+
- Code expecting asynchonous callbacks should switch to `ebpf_perf_buffer__new` with `EBPF_PERFBUF_FLAG_AUTO_CALLBACK` set in the opts flags.
7+
- The synchronous API has not been implemented yet, so `perf_buffer__new` will return a not implemented error.
8+
59
## Background
610

711
On Linux there are two map types for sending large amounts of data from BPF programs to user space.
@@ -93,11 +97,13 @@ typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
9397
struct perf_buffer_opts {
9498
size_t sz;
9599
};
96-
#define perf_buffer_opts__last_field sz
97100
98101
/**
99102
* @brief **perf_buffer__new()** creates BPF perfbuffer manager for a specified
100103
* BPF_PERF_EVENT_ARRAY map
104+
*
105+
* @note This currently returns NULL because the synchronous API is not implemented yet.
106+
*
101107
* @param map_fd FD of BPF_PERF_EVENT_ARRAY BPF map that will be used by BPF
102108
* code to send data over to user-space
103109
* @param page_cnt number of memory pages allocated for each per-CPU buffer. Should be set to 0.
@@ -118,4 +124,49 @@ perf_buffer__new(int map_fd, size_t page_cnt,
118124
* @param[in] rb Pointer to perf buffer manager to be freed.
119125
*/
120126
void perf_buffer__free(struct perf_buffer *pb);
127+
128+
129+
//
130+
// Windows-specific Perf Buffer APIs
131+
//
132+
133+
/**
134+
* @brief Windows-specific perf buffer options structure.
135+
*/
136+
struct ebpf_perf_buffer_opts
137+
{
138+
size_t sz; /* size of this struct, for forward/backward compatibility */
139+
uint64_t flags; /* perf buffer option flags */
140+
};
141+
142+
/**
143+
* @brief Perf buffer option flags (Windows-specific).
144+
*/
145+
enum ebpf_perf_buffer_flags
146+
{
147+
EBPF_PERFBUF_FLAG_AUTO_CALLBACK = (uint64_t)1 << 0, /* Automatically invoke callback for each record */
148+
};
149+
typedef void (*perf_buffer_sample_fn)(void* ctx, int cpu, void* data, uint32_t size);
150+
typedef void (*perf_buffer_lost_fn)(void* ctx, int cpu, uint64_t cnt);
151+
152+
/**
153+
* @brief Create a new perf buffer manager with Windows-specific options.
154+
*
155+
* @param[in] map_fd File descriptor of BPF_MAP_TYPE_PERF_EVENT_ARRAY map.
156+
* @param[in] page_cnt Number of memory pages allocated for each per-CPU buffer. Should be set to 0.
157+
* @param[in] sample_cb Function called on each received data record.
158+
* @param[in] lost_cb Function called when record loss has occurred.
159+
* @param[in] ctx User-provided context passed into sample_cb and lost_cb.
160+
* @param[in] opts Windows-specific perf buffer manager options.
161+
*
162+
* @returns Pointer to perf buffer manager on success, null on error.
163+
*/
164+
_Ret_maybenull_ struct perf_buffer*
165+
ebpf_perf_buffer__new(
166+
int map_fd,
167+
size_t page_cnt,
168+
perf_buffer_sample_fn sample_cb,
169+
perf_buffer_lost_fn lost_cb,
170+
_In_opt_ void* ctx,
171+
_In_opt_ const struct ebpf_perf_buffer_opts* opts) EBPF_NO_EXCEPT;
121172
```

docs/RingBuffer.md

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# eBPF Ring Buffer Map
22

3+
NOTE: With [#4640](https://github.com/microsoft/ebpf-for-windows/pull/4640) The default behavior has been changed to be linux-compatible.
4+
- Code expecting asynchonous callbacks should switch to `ebpf_ring_buffer__new` with `EBPF_RINGBUF_FLAG_AUTO_CALLBACK` set in the opts flags.
5+
- The synchronous API has not been implemented yet, so `ring_buffer__new` will return a not implemented error.
6+
37
ebpf-for-windows exposes the [libbpf.h](/include/bpf/libbpf.h) interface for user-mode code.
48

59
*More documentation on user-mode API to be added later.*
@@ -114,22 +118,13 @@ typedef int (*ring_buffer_sample_fn)(_Inout_ void *ctx, _In_reads_bytes_(size) v
114118
115119
struct ring_buffer_opts {
116120
size_t sz; /* size of this struct, for forward/backward compatiblity */
117-
uint64_t flags; /* ring buffer option flags */
118-
};
119-
120-
// Ring buffer manager options.
121-
// - The default behaviour is currently automatic callbacks, but may change in the future per #4142.
122-
// - Only specify one of AUTO_CALLBACKS or NO_AUTO_CALLBACKS - specifying both is not allowed.
123-
enum ring_buffer_flags {
124-
RINGBUF_FLAG_AUTO_CALLBACK = (uint64_t)1 << 0 /* Automatically invoke callback for each record */
125-
RINGBUF_FLAG_NO_AUTO_CALLBACK = (uint64_t)2 << 0 /* Don't automatically invoke callback for each record */
126121
};
127122
128-
#define ring_buffer_opts__last_field sz
129-
130123
/**
131124
* @brief Creates a new ring buffer manager.
132125
*
126+
* @note This currently returns NULL because the synchronous API is not implemented yet.
127+
*
133128
* Only one consumer can be attached at a time, so it should not be called multiple times on an fd.
134129
*
135130
* If the return value is NULL the error will be returned in errno.
@@ -167,6 +162,46 @@ int ring_buffer__poll(_In_ struct ring_buffer *rb, int timeout_ms);
167162
* @param[in] rb Pointer to ring buffer manager to be freed.
168163
*/
169164
void ring_buffer__free(_Frees_ptr_opt_ struct ring_buffer *rb);
165+
166+
167+
//
168+
// Windows-specific Ring Buffer APIs
169+
//
170+
171+
/**
172+
* @brief Windows-specific ring buffer options structure.
173+
*
174+
* This structure extends ring_buffer_opts with Windows-specific fields.
175+
* The first field(s) must match ring_buffer_opts exactly for compatibility.
176+
*/
177+
struct ebpf_ring_buffer_opts
178+
{
179+
size_t sz; /* Size of this struct, for forward/backward compatibility (must match ring_buffer_opts). */
180+
uint64_t flags; /* Windows-specific ring buffer option flags. */
181+
};
182+
183+
/* Ring buffer option flags. */
184+
enum ebpf_ring_buffer_flags
185+
{
186+
EBPF_RINGBUF_FLAG_AUTO_CALLBACK = (uint64_t)1 << 0, /* Automatically invoke callback for each record. */
187+
};
188+
189+
/**
190+
* @brief Creates a new ring buffer manager (Windows-specific with flags support).
191+
*
192+
* @param[in] map_fd File descriptor to ring buffer map.
193+
* @param[in] sample_cb Pointer to ring buffer notification callback function.
194+
* @param[in] ctx Pointer to sample_cb callback function context.
195+
* @param[in] opts Ring buffer options with flags support.
196+
*
197+
* @returns Pointer to ring buffer manager, or NULL on error.
198+
*/
199+
_Ret_maybenull_ struct ring_buffer*
200+
ebpf_ring_buffer__new(
201+
int map_fd,
202+
ring_buffer_sample_fn sample_cb,
203+
_In_opt_ void* ctx,
204+
_In_opt_ const struct ebpf_ring_buffer_opts* opts) EBPF_NO_EXCEPT;
170205
```
171206

172207
### New ebpf APIs for mapped memory consumer

0 commit comments

Comments
 (0)