@@ -50,7 +50,6 @@ struct {
50
50
__uint (max_entries , 2048 );
51
51
} flow_target_map SEC (".maps" );
52
52
53
-
54
53
// struct each_flow_target {
55
54
// __uint(type, BPF_MAP_TYPE_HASH);
56
55
// __uint(map_flags, BPF_F_NO_PREALLOC);
@@ -202,22 +201,126 @@ struct {
202
201
__uint (pinning , LIBBPF_PIN_BY_NAME );
203
202
} rt_target_map SEC (".maps" );
204
203
205
- // struct lan_mac_cache_key {
206
- // u8 ip[16];
207
- // u8 l3_protocol;
208
- // u8 _pad[3];
209
- // };
204
+ struct rt_cache_key {
205
+ struct in6_addr local_addr ;
206
+ struct in6_addr remote_addr ;
207
+ } __rt_cache_key ;
210
208
211
- // struct lan_mac_cache {
212
- // u8 mac[6];
213
- // u8 _pad[2];
214
- // };
209
+ struct rt_cache_value {
210
+ union {
211
+ __u32 mark_value ;
212
+ __u32 ifindex ;
213
+ };
214
+ } __rt_cache_value ;
215
215
216
- // struct {
217
- // __uint(type, BPF_MAP_TYPE_LRU_HASH);
218
- // __type(key, struct lan_mac_cache_key);
219
- // __type(value, struct lan_mac_cache);
220
- // __uint(max_entries, 65535);
221
- // } ip_mac_tab SEC(".maps");
216
+ #define WAN_CACHE 0
217
+ #define LAN_CACHE 1
218
+
219
+ // 缓存
220
+ struct each_cache_hash {
221
+ __uint (type , BPF_MAP_TYPE_LRU_HASH );
222
+ __type (key , struct rt_cache_key );
223
+ __type (value , struct rt_cache_value );
224
+ __uint (max_entries , 65536 );
225
+ } __each_cache_map SEC (".maps" );
226
+
227
+ // flow <-> 对应规则 map
228
+ struct {
229
+ __uint (type , BPF_MAP_TYPE_ARRAY_OF_MAPS );
230
+ __type (key , u32 );
231
+ __uint (max_entries , 4 );
232
+ __uint (pinning , LIBBPF_PIN_BY_NAME );
233
+ __array (values , struct each_cache_hash );
234
+ } rt_cache_map SEC (".maps" );
235
+
236
+ static __always_inline int setting_cache_in_wan (const struct route_context * context , u32 ifindex ) {
237
+ #define BPF_LOG_TOPIC "setting_cache_in_wan"
238
+ struct rt_cache_key search_key = {0 };
239
+ u32 key = LAN_CACHE ;
240
+ COPY_ADDR_FROM (search_key .local_addr .in6_u .u6_addr8 , context -> daddr .in6_u .u6_addr8 );
241
+ COPY_ADDR_FROM (search_key .remote_addr .in6_u .u6_addr8 , context -> saddr .in6_u .u6_addr8 );
242
+
243
+ void * lan_cache = bpf_map_lookup_elem (& rt_cache_map , & key );
244
+ if (lan_cache ) {
245
+ struct rt_cache_value * target = bpf_map_lookup_elem (lan_cache , & search_key );
246
+ if (target ) {
247
+ // if (context->l3_protocol == LANDSCAPE_IPV4_TYPE) {
248
+ // bpf_log_info("Already cached %pI4 -> %pI4", search_key.local_addr.in6_u.u6_addr8,
249
+ // search_key.remote_addr.in6_u.u6_addr8);
250
+ // } else {
251
+ // bpf_log_info("Already cached %pI6 -> %pI6", search_key.local_addr.in6_u.u6_addr8,
252
+ // search_key.remote_addr.in6_u.u6_addr8);
253
+ // }
254
+ return TC_ACT_OK ;
255
+ }
256
+ }
257
+
258
+ key = WAN_CACHE ;
259
+ void * wan_cache = bpf_map_lookup_elem (& rt_cache_map , & key );
260
+ if (wan_cache ) {
261
+ struct rt_cache_value * target = bpf_map_lookup_elem (wan_cache , & search_key );
262
+ if (target ) {
263
+ target -> ifindex = ifindex ;
264
+ } else {
265
+ struct rt_cache_value new_target_cache = {0 };
266
+ new_target_cache .ifindex = ifindex ;
267
+ bpf_map_update_elem (wan_cache , & search_key , & new_target_cache , BPF_ANY );
268
+ }
269
+
270
+ // if (context->l3_protocol == LANDSCAPE_IPV4_TYPE) {
271
+ // bpf_log_info("cache %pI4 -> %pI4", search_key.local_addr.in6_u.u6_addr8,
272
+ // search_key.remote_addr.in6_u.u6_addr8);
273
+ // } else {
274
+ // bpf_log_info("cache %pI6 -> %pI6", search_key.local_addr.in6_u.u6_addr8,
275
+ // search_key.remote_addr.in6_u.u6_addr8);
276
+ // }
277
+ } else {
278
+ bpf_log_info ("could not find wan_cache: %d" , key );
279
+ }
280
+
281
+ return TC_ACT_OK ;
282
+ #undef BPF_LOG_TOPIC
283
+ }
284
+
285
+ static __always_inline int setting_cache_in_lan (const struct route_context * context ,
286
+ u32 flow_mark ) {
287
+ #define BPF_LOG_TOPIC "setting_cache_in_lan"
288
+ struct rt_cache_key search_key = {0 };
289
+ u32 key = WAN_CACHE ;
290
+ COPY_ADDR_FROM (search_key .local_addr .in6_u .u6_addr8 , context -> saddr .in6_u .u6_addr8 );
291
+ COPY_ADDR_FROM (search_key .remote_addr .in6_u .u6_addr8 , context -> daddr .in6_u .u6_addr8 );
292
+
293
+ void * wan_cache = bpf_map_lookup_elem (& rt_cache_map , & key );
294
+ if (wan_cache ) {
295
+ struct rt_cache_value * target = bpf_map_lookup_elem (wan_cache , & search_key );
296
+ if (target ) {
297
+ return TC_ACT_OK ;
298
+ }
299
+ }
300
+
301
+ key = LAN_CACHE ;
302
+ void * lan_cache = bpf_map_lookup_elem (& rt_cache_map , & key );
303
+ if (lan_cache ) {
304
+ struct rt_cache_value * target = bpf_map_lookup_elem (lan_cache , & search_key );
305
+ if (target ) {
306
+ target -> mark_value = flow_mark ;
307
+ } else {
308
+ struct rt_cache_value new_target_cache = {0 };
309
+ new_target_cache .mark_value = flow_mark ;
310
+ bpf_map_update_elem (lan_cache , & search_key , & new_target_cache , BPF_ANY );
311
+ }
312
+
313
+ // if (context->l3_protocol == LANDSCAPE_IPV4_TYPE) {
314
+ // bpf_log_info("cache %pI4 -> %pI4", search_key.local_addr.in6_u.u6_addr8,
315
+ // search_key.remote_addr.in6_u.u6_addr8);
316
+ // } else {
317
+ // bpf_log_info("cache %pI6 -> %pI6", search_key.local_addr.in6_u.u6_addr8,
318
+ // search_key.remote_addr.in6_u.u6_addr8);
319
+ // }
320
+ }
321
+
322
+ return TC_ACT_OK ;
323
+ #undef BPF_LOG_TOPIC
324
+ }
222
325
223
326
#endif /* __LD_FLOW_H__ */
0 commit comments