Skip to content

Commit ba0fe45

Browse files
committed
fix ut_test_md
Signed-off-by: sweeywu <sweetwx6@gmail.com>
1 parent f348a85 commit ba0fe45

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

docs/proposal/ut_test.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,13 @@ change are understandable. This may include API specs (though not always
7777
required) or even code snippets. If there's any ambiguity about HOW your
7878
proposal will be implemented, this is the place to discuss them.
7979
-->
80-
### sendmsg.c
80+
81+
### sendmsg.c
8182
### mount and set up
8283
#### mount
84+
8385
- include the sockhash map in workload_sendmsg.c
86+
8487
```c
8588
struct {
8689
__uint(type, BPF_MAP_TYPE_SOCKHASH);
@@ -90,8 +93,10 @@ struct {
9093
__uint(map_flags, 0);
9194
} map_of_kmesh_socket SEC(".maps");
9295
```
96+
9397
- in workload_test.go
9498
- load the eBPF program into the kernel
99+
95100
```go
96101
//load the eBPF program
97102
spec := loadAndPrepSpec(t, path.Join(*testPath, objFilePath))
@@ -103,7 +108,9 @@ struct {
103108
// Load the eBPF collection into the kernel
104109
coll, err = ebpf.NewCollection(spec)
105110
```
111+
106112
- Lookup the sockhash map and attach the sk_msg eBPF program to the map
113+
107114
```go
108115
sockMap := coll.Maps["km_socket"]
109116
t.Log(sockMap.Type())
@@ -115,7 +122,9 @@ struct {
115122
Program: prog,
116123
})
117124
```
125+
118126
#### set up
127+
119128
- Establish a network connection, obtain the file descriptor (fd), insert the fd into the map, and sending messages through this fd will trigger the eBPF program
120129
```go
121130
localIP := get_local_ipv4(t)
@@ -164,6 +173,7 @@ struct {
164173
t.Logf("Update successful for key: %+v, fd: %d", tupleKey, fd32)
165174
}
166175
```
176+
167177
### test
168178

169179
- For the sendmsg program, there are key steps:
@@ -177,15 +187,19 @@ struct {
177187
- The current plan is to write a test that directly verifies whether TLV is written into the message header.
178188
- Validation method: Check whether TLV is correctly written into the message header.
179189
- Validation method
190+
180191
```go
181192
buf := make([]byte, 64)
182193
n, _ := ln.Accept().Read(buf)
183194
t.Logf("Received data: %x", buf[:n])
184195
```
196+
185197
### cgroup_sock.c
186198
### mount and set up
187199
#### mount
200+
188201
- in workload_test.go
202+
189203
```go
190204
// mount cgroup2
191205
mount_cgroup2(t, cgroupPath)
@@ -195,21 +209,26 @@ struct {
195209
defer coll.Close()
196210
defer lk.Close()
197211
```
212+
198213
#### set up
214+
199215
```go
200216
conn, err := net.Dial("tcp4", "...")
201217
if err != nil {
202218
t.Fatalf("Dial failed: %v", err)
203219
}
204220
defer conn.Close()
205221
```
222+
206223
### test
224+
207225
- Currently, connect4 and connect6 each have 5 test points.
208226
- 1
209227
- handle_kmesh_manage_process(&kmesh_ctx) internally calls bpf_map_update_elem(&map_of_manager, &key, &value, BPF_ANY); or err = bpf_map_delete_elem(&map_of_manager, &key); for verification.
210228
- When the destination address is CONTROL_CMD_IP: ENABLE_KMESH_PORT, it adds its netns_cookie to the map; when the destination address is CONTROL_CMD_IP: DISABLE_KMESH_PORT, it deletes its netns_cookie from the map.
211229
- Validation method:
212230
- Verify the addition when inserting.
231+
213232
```go
214233
kmManageMap := coll.Maps["km_manage"]
215234
if kmManageMap == nil {
@@ -236,7 +255,9 @@ defer conn.Close()
236255
t.Fatalf("Expected 1 entry in km_manage map, but got %d", count)
237256
}
238257
```
258+
239259
- Validation when deleting
260+
240261
```go
241262
iter = kmManageMap.Iterate()
242263
count = 0
@@ -251,12 +272,14 @@ defer conn.Close()
251272
t.Fatalf("Expected 0 entry in km_manage map, but got %d", count)
252273
}
253274
```
275+
254276
- Notes
255277
- Here it may be necessary to mock storage = bpf_sk_storage_get(&map_of_sock_storage, sk, 0, BPF_LOCAL_STORAGE_GET_F_CREATE); inside workload_cgroup_sock_test.c.
256278
- 2
257279
- The function sock_traffic_control(&kmesh_ctx) is critical and internally includes
258280
- frontend_v = map_lookup_frontend(&frontend_k); Consider how to return frontend_v; this must return a value.
259281
- By constructing a key-value pair so that the map contains this k-v, it can be found; construct the frontend map.
282+
260283
```go
261284
//frontend map
262285
type ip_addr struct {
@@ -286,11 +309,13 @@ defer conn.Close()
286309
log.Fatalf("Update failed: %v", err)
287310
}
288311
```
312+
289313
- frontend_manager(kmesh_ctx, frontend_v); internally includes
290314
- kmesh_map_lookup_elem(&map_of_service, key)
291315
- 2.1: can find:
292316
- 2.1.1:
293317
- Test point: waypoint == true in service_value
318+
294319
```go
295320
type service_value struct {
296321
PrioEndpointCount [7]uint32
@@ -307,18 +332,22 @@ s_val := service_value{
307332
WaypointPort: 55555, //Build
308333
}
309334
```
335+
310336
- At this point, you need to monitor the forwarded address so that the forwarded connection will not be rejected
311337

312338
### Alternatives
339+
313340
```go
314341
test:=localIp+":"+strconv.Itoa(htons(55555))
315342
- _, err = net.Listen("tcp4", "10.30.0.124:985")
316343
```
344+
317345
- 2.2.2:
318346
- 2.2: If not found, perform kmesh_map_lookup_elem(&map_of_backend, key); this must return a value
319347
- 2.2.1:
320348
- Test point: waypoint == true in backend_value
321349
- Construction:
350+
322351
```go
323352
type backend_value struct {
324353
Addr ip_addr
@@ -342,14 +371,18 @@ if err := BackendMap.Update(&b_key, &b_val, ebpf.UpdateAny); err != nil {
342371
log.Fatalf("Update failed: %v", err)
343372
}
344373
```
374+
345375
- This traffic is also forwarded, so the forwarded address must be listened to in advance.
376+
346377
```go
347378
testIpPort := localIP + ":" + strconv.Itoa(htons(testPort))
348379
_, err = net.Listen("tcp4", testIpPort)
349380
```
381+
350382
- 2.2.2
351383
- Test point: waypoint == false in backend_value
352384
- Construction:
385+
353386
```go
354387
type backend_value struct {
355388
Addr ip_addr
@@ -369,7 +402,9 @@ if err := BackendMap.Update(&b_key, &b_val, ebpf.UpdateAny); err != nil {
369402
log.Fatalf("Update failed: %v", err)
370403
}
371404
```
405+
372406
- Note: The UpstreamID in the constructed value must match the key length used later when populating km_backend or the key length used in km_service. For example:
407+
373408
```go
374409
type backend_key struct {
375410
BackendUID uint32
@@ -387,7 +422,9 @@ s_key := service_key{
387422
ServiceID: 1,
388423
}
389424
```
425+
390426
- Validation method:
427+
391428
```go
392429
expectedIP := localIP
393430
expectedPort := strconv.Itoa(htons(testPort))
@@ -396,16 +433,18 @@ expectedIP := localIP
396433
t.Fatalf("Expected redirected to %s:%s, but got %s:%s", expectedIP, expectedPort, host, port)
397434
}
398435
```
436+
399437
### cgroup_skb.c
438+
400439
- It is a type supported by bpf_prog_run and can be tested using the first framework.
401440
- Since it uses the same __sk_buff parameters as tc triggers, it can be written by following that example.
441+
402442
```c
403443
#include "ut_common.h"
404444
#include <linux/bpf.h>
405445
#include <linux/if_ether.h>
406446
#include <linux/in.h>
407447
#include <netinet/tcp.h>
408-
409448
#include "workload/cgroup_skb.c"
410449
struct tcp_probe_info mock_info;
411450
bool mock_ringbuf_called = false;
@@ -504,6 +543,7 @@ int test_ingress_check(struct __sk_buff *ctx)
504543
test_finish();
505544
}
506545
```
546+
507547
<!--
508548
What other approaches did you consider, and why did you rule them out? These do
509549
not need to be as detailed as the proposal, but should include enough

0 commit comments

Comments
 (0)