23
23
#include " bpftime.hpp"
24
24
#include " bpftime_shm.hpp"
25
25
#include " bpftime_internal.h"
26
+ #include " extension/userspace_xdp.h"
26
27
#include < spdlog/spdlog.h>
27
28
#include < vector>
28
29
#include < bpftime_shm_internal.hpp>
@@ -104,17 +105,17 @@ uint64_t bpf_get_current_uid_gid(uint64_t, uint64_t, uint64_t, uint64_t,
104
105
105
106
uint64_t bpftime_ktime_get_ns (uint64_t , uint64_t , uint64_t , uint64_t , uint64_t )
106
107
{
107
- auto now = std::chrono::steady_clock::now ();
108
- auto ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(now);
109
- return ns.time_since_epoch ().count ();
108
+ auto now = std::chrono::steady_clock::now ();
109
+ auto ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(now);
110
+ return ns.time_since_epoch ().count ();
110
111
}
111
112
112
113
uint64_t bpftime_get_current_comm (uint64_t buf, uint64_t size, uint64_t ,
113
114
uint64_t , uint64_t )
114
115
{
115
116
static std::string filename_buf;
116
117
117
- if (filename_buf.empty ()) {
118
+ if (unlikely ( filename_buf.empty () )) {
118
119
char strbuf[PATH_MAX];
119
120
120
121
auto len = readlink (" /proc/self/exe" , strbuf,
@@ -219,6 +220,7 @@ uint64_t bpf_ringbuf_submit(uint64_t data, uint64_t flags, uint64_t, uint64_t,
219
220
bpftime_ringbuf_submit (fd, (void *)(uintptr_t )data, false );
220
221
return 0 ;
221
222
}
223
+
222
224
uint64_t bpf_ringbuf_discard (uint64_t data, uint64_t flags, uint64_t , uint64_t ,
223
225
uint64_t )
224
226
{
@@ -328,6 +330,7 @@ uint64_t bpftime_tail_call(uint64_t ctx, uint64_t prog_array, uint64_t index)
328
330
close (to_call_fd);
329
331
return run_opts.retval ;
330
332
}
333
+
331
334
uint64_t bpftime_get_attach_cookie (uint64_t ctx, uint64_t , uint64_t , uint64_t ,
332
335
uint64_t )
333
336
{
@@ -340,6 +343,95 @@ uint64_t bpftime_get_attach_cookie(uint64_t ctx, uint64_t, uint64_t, uint64_t,
340
343
return 0 ;
341
344
}
342
345
}
346
+
347
+ uint64_t bpftime_get_smp_processor_id ()
348
+ {
349
+ int cpu = sched_getcpu ();
350
+ if (cpu == -1 ) {
351
+ SPDLOG_ERROR (" sched_getcpu error" );
352
+ return 0 ; // unlikely
353
+ }
354
+ return (uint64_t )cpu;
355
+ }
356
+
357
+ // From https://github.com/microsoft/ebpf-for-windows
358
+ int64_t bpftime_csum_diff (const void *from, int from_size, const void *to,
359
+ int to_size, int seed)
360
+ {
361
+ int csum_diff = -EINVAL;
362
+
363
+ if ((from_size % 4 != 0 ) || (to_size % 4 != 0 )) {
364
+ // size of buffers should be a multiple of 4.
365
+ goto Exit;
366
+ }
367
+
368
+ csum_diff = seed;
369
+ if (to != NULL ) {
370
+ for (int i = 0 ; i < to_size / 2 ; i++) {
371
+ csum_diff += (uint16_t )(*((uint16_t *)to + i));
372
+ }
373
+ }
374
+ if (from != NULL ) {
375
+ for (int i = 0 ; i < from_size / 2 ; i++) {
376
+ csum_diff += (uint16_t )(~*((uint16_t *)from + i));
377
+ }
378
+ }
379
+
380
+ // Adding 16-bit unsigned integers or their one's complement will
381
+ // produce a positive 32-bit integer, unless the length of the buffers
382
+ // is so long, that the signed 32 bit output overflows and produces a
383
+ // negative result.
384
+ if (csum_diff < 0 ) {
385
+ csum_diff = -EINVAL;
386
+ }
387
+ Exit:
388
+ return csum_diff;
389
+ }
390
+
391
+ #define ETH_HLEN 14 /* Total octets in header. */
392
+
393
+ long bpftime_xdp_adjust_head (struct xdp_md_userspace *xdp, int offset)
394
+ {
395
+ // We don't use xdp meta data
396
+ uint64_t data = xdp->data + offset;
397
+ if (unlikely (data > xdp->data_end - ETH_HLEN) || data > xdp->buffer_end )
398
+ return -EINVAL;
399
+ if (data < xdp->buffer_start ) {
400
+ // move the data so the buffer can place the new header
401
+ memmove (reinterpret_cast <void *>(xdp->buffer_start +
402
+ (xdp->buffer_start - data)),
403
+ reinterpret_cast <void *>(xdp->data ),
404
+ xdp->data_end - xdp->data );
405
+ data = xdp->buffer_start ;
406
+ }
407
+ xdp->data = data;
408
+ return 0 ;
409
+ }
410
+
411
+ long bpftime_xdp_adjust_tail (struct xdp_md_userspace *xdp_md, int delta)
412
+ {
413
+ // We don't use xdp meta data
414
+ uint64_t data = xdp_md->data_end + delta;
415
+ if (data < xdp_md->data || data < xdp_md->buffer_start ||
416
+ data > xdp_md->buffer_end ) {
417
+ return -EINVAL;
418
+ }
419
+ xdp_md->data_end = data;
420
+ return 0 ;
421
+ }
422
+
423
+ long bpftime_xdp_load_bytes (struct xdp_md_userspace *xdp_md, __u32 offset,
424
+ void *buf, __u32 len)
425
+ {
426
+ // We don't support fragmented packets
427
+ uint64_t data = xdp_md->data + offset;
428
+ if (data + len > xdp_md->data_end ) {
429
+ return -EINVAL;
430
+ }
431
+ memcpy (buf, reinterpret_cast <void *>(data), len);
432
+ return 0 ;
433
+ }
434
+
343
435
} // extern "C"
344
436
345
437
namespace bpftime
@@ -643,6 +735,30 @@ const bpftime_helper_group kernel_helper_group = {
643
735
.name = " bpf_probe_read" ,
644
736
.fn = (void *)bpftime_probe_read,
645
737
} },
738
+ { BPF_FUNC_get_smp_processor_id,
739
+ bpftime_helper_info{
740
+ .index = BPF_FUNC_get_smp_processor_id,
741
+ .name = " bpf_get_smp_processor_id" ,
742
+ .fn = (void *)bpftime_get_smp_processor_id,
743
+ } },
744
+ { BPF_FUNC_csum_diff,
745
+ bpftime_helper_info{
746
+ .index = BPF_FUNC_csum_diff,
747
+ .name = " bpf_csum_diff" ,
748
+ .fn = (void *)bpftime_csum_diff,
749
+ } },
750
+ { BPF_FUNC_xdp_adjust_head,
751
+ bpftime_helper_info{
752
+ .index = BPF_FUNC_xdp_adjust_head,
753
+ .name = " bpf_xdp_adjust_head" ,
754
+ .fn = (void *)bpftime_xdp_adjust_head,
755
+ } },
756
+ { BPF_FUNC_xdp_adjust_tail,
757
+ bpftime_helper_info{
758
+ .index = BPF_FUNC_xdp_adjust_tail,
759
+ .name = " bpf_xdp_adjust_tail" ,
760
+ .fn = (void *)bpftime_xdp_adjust_tail,
761
+ } },
646
762
{ BPF_FUNC_probe_read_kernel,
647
763
bpftime_helper_info{
648
764
.index = BPF_FUNC_probe_read_kernel,
0 commit comments