mirror of
https://github.com/xdp-project/bpf-examples.git
synced 2024-05-06 15:54:53 +00:00
AF_XDP-interaction: Adapt lib_xsk_extend while trying to use
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
This commit is contained in:
@@ -32,6 +32,7 @@
|
||||
#include "common_user_bpf_xdp.h"
|
||||
// #include "common_libbpf.h"
|
||||
|
||||
#include "lib_xsk_extend.h"
|
||||
|
||||
#define NUM_FRAMES 4096
|
||||
#define FRAME_SIZE XSK_UMEM__DEFAULT_FRAME_SIZE
|
||||
@@ -625,7 +626,7 @@ int btf_walk_struct_members(struct btf *btf_obj, __s32 btf_id)
|
||||
return 0;
|
||||
}
|
||||
|
||||
__s32 btf_find_struct(struct btf *btf, char *name, __s64 *size)
|
||||
__s32 btf_find_struct(struct btf *btf, const char *name, __s64 *size)
|
||||
{
|
||||
__s32 btf_id = btf__find_by_name_kind(btf, name, BTF_KIND_STRUCT);
|
||||
__s64 sz = btf__resolve_size(btf, btf_id);
|
||||
@@ -639,15 +640,31 @@ __s32 btf_find_struct(struct btf *btf, char *name, __s64 *size)
|
||||
return btf_id;
|
||||
}
|
||||
|
||||
static const char *name1 = "xdp_hints_mark";
|
||||
static const char *name2 = "xdp_hints_rx_time";
|
||||
|
||||
int btf_info_via_bpf_object(struct bpf_object *bpf_obj)
|
||||
{
|
||||
struct btf *btf = bpf_object__btf(bpf_obj);
|
||||
char *name1 = "xdp_hints_mark";
|
||||
char *name2 = "xdp_hints_rx_time";
|
||||
__s64 size;
|
||||
int err;
|
||||
|
||||
struct xsk_btf_info *xdp_hint_rx_time = NULL;
|
||||
|
||||
btf_find_struct(btf, name1, &size);
|
||||
btf_find_struct(btf, name2, &size);
|
||||
|
||||
err = xsk_btf__init_xdp_hint(btf, name2, &xdp_hint_rx_time);
|
||||
if (err) {
|
||||
fprintf(stderr, "WARN(%d): Cannot xsk_btf__init_xdp_hint\n", err);
|
||||
return err;
|
||||
}
|
||||
if (!xsk_btf__has_field("rx_ktime", xdp_hint_rx_time)) {
|
||||
fprintf(stderr, "WARN: %s doesn't contain member rx_ktime\n",
|
||||
name2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@@ -1,18 +1,23 @@
|
||||
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
|
||||
|
||||
/*
|
||||
* Prototyping new API for userspace xsk/AF_XDP to access XDP-hints, which is
|
||||
* BTF typed info in XDP metadata area (located just before packets headers).
|
||||
*/
|
||||
#include "hashmap.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <bpf/btf.h> /* provided by libbpf */
|
||||
|
||||
int xsk_umem__btf_id(void *umem_data, const struct xsk_umem *umem)
|
||||
int xsk_umem__btf_id(void *umem_pkt_data, const struct xsk_umem *umem)
|
||||
{
|
||||
// if (umem->config.xdp_headroom < sizeof(int))
|
||||
// return -EINVAL;
|
||||
|
||||
return *(int *)(umem_data - sizeof(int));
|
||||
return *(int *)(umem_pkt_data - sizeof(int));
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +25,7 @@ struct xsk_btf_info {
|
||||
struct hashmap map;
|
||||
struct btf *btf;
|
||||
const struct btf_type *type;
|
||||
//__u32 btf_id;
|
||||
};
|
||||
|
||||
struct xsk_btf_entry {
|
||||
@@ -56,33 +62,36 @@ static bool __xsk_equal_fn(const void *k1, const void *k2, void *ctx)
|
||||
return k1 == k2;
|
||||
}
|
||||
|
||||
int xsk_btf__init(__u32 btf_id, struct xsk_btf_info **xbi)
|
||||
int xsk_btf__init_xdp_hint(struct btf *btf_obj,
|
||||
const char *xdp_hints_name,
|
||||
struct xsk_btf_info **xbi)
|
||||
{
|
||||
const struct btf_member *m;
|
||||
const struct btf_type *t;
|
||||
unsigned short vlen;
|
||||
struct btf *btf;
|
||||
int i, id, ret = 0;
|
||||
|
||||
if (!xbi)
|
||||
return -EINVAL;
|
||||
|
||||
ret = btf__get_from_id(btf_id, &btf);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
// ret = btf__get_from_id(btf_id, &btf); // Limits lookups to kernel BTF
|
||||
// if (ret < 0)
|
||||
// return ret;
|
||||
|
||||
id = btf__find_by_name(btf, "xdp_hints");
|
||||
/* Require XDP-hints is defined as a struct */
|
||||
id = btf__find_by_name_kind(btf_obj, xdp_hints_name, BTF_KIND_STRUCT);
|
||||
if (id < 0) {
|
||||
ret = id;
|
||||
goto error_btf;
|
||||
}
|
||||
printf("XXX %s() id:%d\n", __func__, id);
|
||||
|
||||
t = btf__type_by_id(btf, id);
|
||||
t = btf__type_by_id(btf_obj, id);
|
||||
|
||||
if (!BTF_INFO_KFLAG(t->info)) {
|
||||
ret = -EINVAL;
|
||||
goto error_btf;
|
||||
}
|
||||
// if (!BTF_INFO_KFLAG(t->info)) {
|
||||
// ret = -EINVAL;
|
||||
// goto error_btf;
|
||||
// }
|
||||
|
||||
*xbi = malloc(sizeof(**xbi));
|
||||
if (!*xbi) {
|
||||
@@ -102,8 +111,9 @@ int xsk_btf__init(__u32 btf_id, struct xsk_btf_info **xbi)
|
||||
}
|
||||
}
|
||||
|
||||
(*xbi)->btf = btf;
|
||||
(*xbi)->btf = btf_obj;
|
||||
(*xbi)->type = t;
|
||||
// (*xbi)->btf_id = id;
|
||||
|
||||
return ret;
|
||||
|
||||
@@ -112,7 +122,7 @@ error_entry:
|
||||
free(*xbi);
|
||||
|
||||
error_btf:
|
||||
btf__free(btf);
|
||||
// btf__free(btf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -128,6 +138,7 @@ static int __xsk_btf_field_entry(struct xsk_btf_info *xbi, const char *field,
|
||||
for (i = 0; i < vlen; i++, m++) {
|
||||
const struct btf_type *member_type;
|
||||
const char *name = btf__name_by_offset(xbi->btf, m->name_off);
|
||||
printf("XXX %s() i:%d name:%s\n", __func__, i, name);
|
||||
|
||||
if (strcmp(name, field))
|
||||
continue;
|
||||
@@ -155,10 +166,10 @@ bool xsk_btf__has_field(const char *field, struct xsk_btf_info *xbi)
|
||||
if (!xbi)
|
||||
return false;
|
||||
|
||||
return __xsk_btf_field_entry(xbi, field, NULL);
|
||||
return __xsk_btf_field_entry(xbi, field, NULL) ? false : true;
|
||||
}
|
||||
|
||||
void xsk_btf__free(struct xsk_btf_info *xbi)
|
||||
void xsk_btf__free_xdp_hint(struct xsk_btf_info *xbi)
|
||||
{
|
||||
if (!xbi)
|
||||
return;
|
||||
@@ -188,6 +199,7 @@ int xsk_btf__read(void **dest, size_t size, const char *field, struct xsk_btf_in
|
||||
if (entry->size != size)
|
||||
return -EINVAL;
|
||||
|
||||
// XXX should we cache size for main xdp_hints struct?
|
||||
*dest = (void *)((char *)addr - xbi->type->size + entry->offset);
|
||||
return 0;
|
||||
}
|
||||
|
@@ -3,17 +3,21 @@
|
||||
#ifndef __LIB_XSK_EXTEND_H
|
||||
#define __LIB_XSK_EXTEND_H
|
||||
|
||||
#define LIBBPF_API ""
|
||||
//#define LIBBPF_API ""
|
||||
|
||||
LIBBPF_API int xsk_umem__btf_id(void *umem_data, const struct xsk_umem *umem);
|
||||
|
||||
struct xsk_btf_info;
|
||||
|
||||
LIBBPF_API int xsk_btf__init(__u32 btf_id, struct xsk_btf_info **xbi);
|
||||
LIBBPF_API int xsk_btf__init_xdp_hint(struct btf *btf_obj,
|
||||
const char *xdp_hints_name,
|
||||
struct xsk_btf_info **xbi);
|
||||
|
||||
LIBBPF_API void xsk_btf__free_xdp_hint(struct xsk_btf_info *xbi);
|
||||
|
||||
LIBBPF_API int xsk_btf__read(void **dest, size_t size, const char *field, struct xsk_btf_info *xbi,
|
||||
const void *addr);
|
||||
LIBBPF_API bool xsk_btf__has_field(const char *field, struct xsk_btf_info *xbi);
|
||||
LIBBPF_API void xsk_btf__free(struct xsk_btf_info *xbi);
|
||||
|
||||
#define XSK_BTF_READ_INTO(dest, field, xbi, addr) ({ \
|
||||
typeof(dest) *_d; \
|
||||
|
Reference in New Issue
Block a user