Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bpftool sync 2024-10-10 #165

Merged
merged 11 commits into from
Oct 10, 2024
2 changes: 1 addition & 1 deletion BPF-CHECKPOINT-COMMIT
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b408473ea01b2e499d23503e2bf898416da9d7ac
b24d7f0da6ef5a23456a301eaf51b170f961d4ae
2 changes: 1 addition & 1 deletion CHECKPOINT-COMMIT
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2ad6d23f465a4f851e3bcf6d74c315ce7b2c205b
b24d7f0da6ef5a23456a301eaf51b170f961d4ae
4 changes: 2 additions & 2 deletions docs/bpftool-gen.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ bpftool gen skeleton *FILE*

- **example__load**.
This function creates maps, loads and verifies BPF programs, initializes
global data maps. It corresponds to libppf's **bpf_object__load**\ ()
global data maps. It corresponds to libbpf's **bpf_object__load**\ ()
API.

- **example__open_and_load** combines **example__open** and
Expand Down Expand Up @@ -172,7 +172,7 @@ bpftool gen min_core_btf *INPUT* *OUTPUT* *OBJECT* [*OBJECT*...]
CO-RE based application, turning the application portable to different
kernel versions.

Check examples bellow for more information how to use it.
Check examples below for more information on how to use it.

bpftool gen help
Print short help message.
Expand Down
2 changes: 2 additions & 0 deletions docs/bpftool-net.rst
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ EXAMPLES
|

::

tc:
lo(1) tcx/ingress tc_prog prog_id 29

Expand All @@ -197,4 +198,5 @@ EXAMPLES
|

::

tc:
25 changes: 12 additions & 13 deletions include/uapi/linux/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -2851,7 +2851,7 @@ union bpf_attr {
* **TCP_SYNCNT**, **TCP_USER_TIMEOUT**, **TCP_NOTSENT_LOWAT**,
* **TCP_NODELAY**, **TCP_MAXSEG**, **TCP_WINDOW_CLAMP**,
* **TCP_THIN_LINEAR_TIMEOUTS**, **TCP_BPF_DELACK_MAX**,
* **TCP_BPF_RTO_MIN**.
* **TCP_BPF_RTO_MIN**, **TCP_BPF_SOCK_OPS_CB_FLAGS**.
* * **IPPROTO_IP**, which supports *optname* **IP_TOS**.
* * **IPPROTO_IPV6**, which supports the following *optname*\ s:
* **IPV6_TCLASS**, **IPV6_AUTOFLOWLABEL**.
Expand Down Expand Up @@ -5519,11 +5519,12 @@ union bpf_attr {
* **-EOPNOTSUPP** if the hash calculation failed or **-EINVAL** if
* invalid arguments are passed.
*
* void *bpf_kptr_xchg(void *map_value, void *ptr)
* void *bpf_kptr_xchg(void *dst, void *ptr)
* Description
* Exchange kptr at pointer *map_value* with *ptr*, and return the
* old value. *ptr* can be NULL, otherwise it must be a referenced
* pointer which will be released when this helper is called.
* Exchange kptr at pointer *dst* with *ptr*, and return the old value.
* *dst* can be map value or local kptr. *ptr* can be NULL, otherwise
* it must be a referenced pointer which will be released when this helper
* is called.
* Return
* The old value of kptr (which can be NULL). The returned pointer
* if not NULL, is a reference which must be released using its
Expand Down Expand Up @@ -6046,11 +6047,6 @@ enum {
BPF_F_MARK_ENFORCE = (1ULL << 6),
};

/* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */
enum {
BPF_F_INGRESS = (1ULL << 0),
};

/* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */
enum {
BPF_F_TUNINFO_IPV6 = (1ULL << 0),
Expand Down Expand Up @@ -6197,10 +6193,12 @@ enum {
BPF_F_BPRM_SECUREEXEC = (1ULL << 0),
};

/* Flags for bpf_redirect_map helper */
/* Flags for bpf_redirect and bpf_redirect_map helpers */
enum {
BPF_F_BROADCAST = (1ULL << 3),
BPF_F_EXCLUDE_INGRESS = (1ULL << 4),
BPF_F_INGRESS = (1ULL << 0), /* used for skb path */
BPF_F_BROADCAST = (1ULL << 3), /* used for XDP path */
BPF_F_EXCLUDE_INGRESS = (1ULL << 4), /* used for XDP path */
#define BPF_F_REDIRECT_FLAGS (BPF_F_INGRESS | BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS)
};

#define __bpf_md_ptr(type, name) \
Expand Down Expand Up @@ -7080,6 +7078,7 @@ enum {
TCP_BPF_SYN = 1005, /* Copy the TCP header */
TCP_BPF_SYN_IP = 1006, /* Copy the IP[46] and TCP header */
TCP_BPF_SYN_MAC = 1007, /* Copy the MAC, IP[46], and TCP header */
TCP_BPF_SOCK_OPS_CB_FLAGS = 1008, /* Get or Set TCP sock ops flags */
};

enum {
Expand Down
87 changes: 79 additions & 8 deletions src/btf.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct sort_datum {
int type_rank;
const char *sort_name;
const char *own_name;
__u64 disambig_hash;
};

static const char *btf_int_enc_str(__u8 encoding)
Expand Down Expand Up @@ -561,9 +562,10 @@ static const char *btf_type_sort_name(const struct btf *btf, __u32 index, bool f
case BTF_KIND_ENUM64: {
int name_off = t->name_off;

/* Use name of the first element for anonymous enums if allowed */
if (!from_ref && !t->name_off && btf_vlen(t))
name_off = btf_enum(t)->name_off;
if (!from_ref && !name_off && btf_vlen(t))
name_off = btf_kind(t) == BTF_KIND_ENUM64 ?
btf_enum64(t)->name_off :
btf_enum(t)->name_off;

return btf__name_by_offset(btf, name_off);
}
Expand All @@ -583,20 +585,88 @@ static const char *btf_type_sort_name(const struct btf *btf, __u32 index, bool f
return NULL;
}

static __u64 hasher(__u64 hash, __u64 val)
{
return hash * 31 + val;
}

static __u64 btf_name_hasher(__u64 hash, const struct btf *btf, __u32 name_off)
{
if (!name_off)
return hash;

return hasher(hash, str_hash(btf__name_by_offset(btf, name_off)));
}

static __u64 btf_type_disambig_hash(const struct btf *btf, __u32 id, bool include_members)
{
const struct btf_type *t = btf__type_by_id(btf, id);
int i;
size_t hash = 0;

hash = btf_name_hasher(hash, btf, t->name_off);

switch (btf_kind(t)) {
case BTF_KIND_ENUM:
case BTF_KIND_ENUM64:
for (i = 0; i < btf_vlen(t); i++) {
__u32 name_off = btf_is_enum(t) ?
btf_enum(t)[i].name_off :
btf_enum64(t)[i].name_off;

hash = btf_name_hasher(hash, btf, name_off);
}
break;
case BTF_KIND_STRUCT:
case BTF_KIND_UNION:
if (!include_members)
break;
for (i = 0; i < btf_vlen(t); i++) {
const struct btf_member *m = btf_members(t) + i;

hash = btf_name_hasher(hash, btf, m->name_off);
/* resolve field type's name and hash it as well */
hash = hasher(hash, btf_type_disambig_hash(btf, m->type, false));
}
break;
case BTF_KIND_TYPE_TAG:
case BTF_KIND_CONST:
case BTF_KIND_PTR:
case BTF_KIND_VOLATILE:
case BTF_KIND_RESTRICT:
case BTF_KIND_TYPEDEF:
case BTF_KIND_DECL_TAG:
hash = hasher(hash, btf_type_disambig_hash(btf, t->type, include_members));
break;
case BTF_KIND_ARRAY: {
struct btf_array *arr = btf_array(t);

hash = hasher(hash, arr->nelems);
hash = hasher(hash, btf_type_disambig_hash(btf, arr->type, include_members));
break;
}
default:
break;
}
return hash;
}

static int btf_type_compare(const void *left, const void *right)
{
const struct sort_datum *d1 = (const struct sort_datum *)left;
const struct sort_datum *d2 = (const struct sort_datum *)right;
int r;

if (d1->type_rank != d2->type_rank)
return d1->type_rank < d2->type_rank ? -1 : 1;

r = strcmp(d1->sort_name, d2->sort_name);
r = d1->type_rank - d2->type_rank;
r = r ?: strcmp(d1->sort_name, d2->sort_name);
r = r ?: strcmp(d1->own_name, d2->own_name);
if (r)
return r;

return strcmp(d1->own_name, d2->own_name);
if (d1->disambig_hash != d2->disambig_hash)
return d1->disambig_hash < d2->disambig_hash ? -1 : 1;

return d1->index - d2->index;
}

static struct sort_datum *sort_btf_c(const struct btf *btf)
Expand All @@ -617,6 +687,7 @@ static struct sort_datum *sort_btf_c(const struct btf *btf)
d->type_rank = btf_type_rank(btf, i, false);
d->sort_name = btf_type_sort_name(btf, i, false);
d->own_name = btf__name_by_offset(btf, t->name_off);
d->disambig_hash = btf_type_disambig_hash(btf, i, true);
}

qsort(datums, n, sizeof(struct sort_datum), btf_type_compare);
Expand Down
10 changes: 5 additions & 5 deletions src/feature.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ static void probe_unprivileged_disabled(void)
{
long res;

/* No support for C-style ouptut */
/* No support for C-style output */

res = read_procfs("/proc/sys/kernel/unprivileged_bpf_disabled");
if (json_output) {
Expand Down Expand Up @@ -225,7 +225,7 @@ static void probe_jit_enable(void)
{
long res;

/* No support for C-style ouptut */
/* No support for C-style output */

res = read_procfs("/proc/sys/net/core/bpf_jit_enable");
if (json_output) {
Expand Down Expand Up @@ -255,7 +255,7 @@ static void probe_jit_harden(void)
{
long res;

/* No support for C-style ouptut */
/* No support for C-style output */

res = read_procfs("/proc/sys/net/core/bpf_jit_harden");
if (json_output) {
Expand Down Expand Up @@ -285,7 +285,7 @@ static void probe_jit_kallsyms(void)
{
long res;

/* No support for C-style ouptut */
/* No support for C-style output */

res = read_procfs("/proc/sys/net/core/bpf_jit_kallsyms");
if (json_output) {
Expand All @@ -311,7 +311,7 @@ static void probe_jit_limit(void)
{
long res;

/* No support for C-style ouptut */
/* No support for C-style output */

res = read_procfs("/proc/sys/net/core/bpf_jit_limit");
if (json_output) {
Expand Down
11 changes: 7 additions & 4 deletions src/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,9 @@ static void __show_dev_tc_bpf(const struct ip_devname_ifindex *dev,
if (prog_flags[i] || json_output) {
NET_START_ARRAY("prog_flags", "%s ");
for (j = 0; prog_flags[i] && j < 32; j++) {
if (!(prog_flags[i] & (1 << j)))
if (!(prog_flags[i] & (1U << j)))
continue;
NET_DUMP_UINT_ONLY(1 << j);
NET_DUMP_UINT_ONLY(1U << j);
}
NET_END_ARRAY("");
}
Expand All @@ -497,9 +497,9 @@ static void __show_dev_tc_bpf(const struct ip_devname_ifindex *dev,
if (link_flags[i] || json_output) {
NET_START_ARRAY("link_flags", "%s ");
for (j = 0; link_flags[i] && j < 32; j++) {
if (!(link_flags[i] & (1 << j)))
if (!(link_flags[i] & (1U << j)))
continue;
NET_DUMP_UINT_ONLY(1 << j);
NET_DUMP_UINT_ONLY(1U << j);
}
NET_END_ARRAY("");
}
Expand Down Expand Up @@ -880,6 +880,9 @@ static void show_link_netfilter(void)
nf_link_count++;
}

if (!nf_link_info)
return;

qsort(nf_link_info, nf_link_count, sizeof(*nf_link_info), netfilter_link_compar);

for (id = 0; id < nf_link_count; id++) {
Expand Down
Loading