18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci/**
48c2ecf20Sopenharmony_ci * ibumad BPF sample kernel side
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or
78c2ecf20Sopenharmony_ci * modify it under the terms of version 2 of the GNU General Public
88c2ecf20Sopenharmony_ci * License as published by the Free Software Foundation.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Copyright(c) 2018 Ira Weiny, Intel Corporation
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#define KBUILD_MODNAME "ibumad_count_pkts_by_class"
148c2ecf20Sopenharmony_ci#include <uapi/linux/bpf.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <bpf/bpf_helpers.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistruct bpf_map_def SEC("maps") read_count = {
208c2ecf20Sopenharmony_ci	.type        = BPF_MAP_TYPE_ARRAY,
218c2ecf20Sopenharmony_ci	.key_size    = sizeof(u32), /* class; u32 required */
228c2ecf20Sopenharmony_ci	.value_size  = sizeof(u64), /* count of mads read */
238c2ecf20Sopenharmony_ci	.max_entries = 256, /* Room for all Classes */
248c2ecf20Sopenharmony_ci};
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistruct bpf_map_def SEC("maps") write_count = {
278c2ecf20Sopenharmony_ci	.type        = BPF_MAP_TYPE_ARRAY,
288c2ecf20Sopenharmony_ci	.key_size    = sizeof(u32), /* class; u32 required */
298c2ecf20Sopenharmony_ci	.value_size  = sizeof(u64), /* count of mads written */
308c2ecf20Sopenharmony_ci	.max_entries = 256, /* Room for all Classes */
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#undef DEBUG
348c2ecf20Sopenharmony_ci#ifndef DEBUG
358c2ecf20Sopenharmony_ci#undef bpf_printk
368c2ecf20Sopenharmony_ci#define bpf_printk(fmt, ...)
378c2ecf20Sopenharmony_ci#endif
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/* Taken from the current format defined in
408c2ecf20Sopenharmony_ci * include/trace/events/ib_umad.h
418c2ecf20Sopenharmony_ci * and
428c2ecf20Sopenharmony_ci * /sys/kernel/debug/tracing/events/ib_umad/ib_umad_read/format
438c2ecf20Sopenharmony_ci * /sys/kernel/debug/tracing/events/ib_umad/ib_umad_write/format
448c2ecf20Sopenharmony_ci */
458c2ecf20Sopenharmony_cistruct ib_umad_rw_args {
468c2ecf20Sopenharmony_ci	u64 pad;
478c2ecf20Sopenharmony_ci	u8 port_num;
488c2ecf20Sopenharmony_ci	u8 sl;
498c2ecf20Sopenharmony_ci	u8 path_bits;
508c2ecf20Sopenharmony_ci	u8 grh_present;
518c2ecf20Sopenharmony_ci	u32 id;
528c2ecf20Sopenharmony_ci	u32 status;
538c2ecf20Sopenharmony_ci	u32 timeout_ms;
548c2ecf20Sopenharmony_ci	u32 retires;
558c2ecf20Sopenharmony_ci	u32 length;
568c2ecf20Sopenharmony_ci	u32 qpn;
578c2ecf20Sopenharmony_ci	u32 qkey;
588c2ecf20Sopenharmony_ci	u8 gid_index;
598c2ecf20Sopenharmony_ci	u8 hop_limit;
608c2ecf20Sopenharmony_ci	u16 lid;
618c2ecf20Sopenharmony_ci	u16 attr_id;
628c2ecf20Sopenharmony_ci	u16 pkey_index;
638c2ecf20Sopenharmony_ci	u8 base_version;
648c2ecf20Sopenharmony_ci	u8 mgmt_class;
658c2ecf20Sopenharmony_ci	u8 class_version;
668c2ecf20Sopenharmony_ci	u8 method;
678c2ecf20Sopenharmony_ci	u32 flow_label;
688c2ecf20Sopenharmony_ci	u16 mad_status;
698c2ecf20Sopenharmony_ci	u16 class_specific;
708c2ecf20Sopenharmony_ci	u32 attr_mod;
718c2ecf20Sopenharmony_ci	u64 tid;
728c2ecf20Sopenharmony_ci	u8 gid[16];
738c2ecf20Sopenharmony_ci	u32 dev_index;
748c2ecf20Sopenharmony_ci	u8 traffic_class;
758c2ecf20Sopenharmony_ci};
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ciSEC("tracepoint/ib_umad/ib_umad_read_recv")
788c2ecf20Sopenharmony_ciint on_ib_umad_read_recv(struct ib_umad_rw_args *ctx)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	u64 zero = 0, *val;
818c2ecf20Sopenharmony_ci	u8 class = ctx->mgmt_class;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	bpf_printk("ib_umad read recv : class 0x%x\n", class);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	val = bpf_map_lookup_elem(&read_count, &class);
868c2ecf20Sopenharmony_ci	if (!val) {
878c2ecf20Sopenharmony_ci		bpf_map_update_elem(&read_count, &class, &zero, BPF_NOEXIST);
888c2ecf20Sopenharmony_ci		val = bpf_map_lookup_elem(&read_count, &class);
898c2ecf20Sopenharmony_ci		if (!val)
908c2ecf20Sopenharmony_ci			return 0;
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	(*val) += 1;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	return 0;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ciSEC("tracepoint/ib_umad/ib_umad_read_send")
988c2ecf20Sopenharmony_ciint on_ib_umad_read_send(struct ib_umad_rw_args *ctx)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	u64 zero = 0, *val;
1018c2ecf20Sopenharmony_ci	u8 class = ctx->mgmt_class;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	bpf_printk("ib_umad read send : class 0x%x\n", class);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	val = bpf_map_lookup_elem(&read_count, &class);
1068c2ecf20Sopenharmony_ci	if (!val) {
1078c2ecf20Sopenharmony_ci		bpf_map_update_elem(&read_count, &class, &zero, BPF_NOEXIST);
1088c2ecf20Sopenharmony_ci		val = bpf_map_lookup_elem(&read_count, &class);
1098c2ecf20Sopenharmony_ci		if (!val)
1108c2ecf20Sopenharmony_ci			return 0;
1118c2ecf20Sopenharmony_ci	}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	(*val) += 1;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	return 0;
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ciSEC("tracepoint/ib_umad/ib_umad_write")
1188c2ecf20Sopenharmony_ciint on_ib_umad_write(struct ib_umad_rw_args *ctx)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	u64 zero = 0, *val;
1218c2ecf20Sopenharmony_ci	u8 class = ctx->mgmt_class;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	bpf_printk("ib_umad write : class 0x%x\n", class);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	val = bpf_map_lookup_elem(&write_count, &class);
1268c2ecf20Sopenharmony_ci	if (!val) {
1278c2ecf20Sopenharmony_ci		bpf_map_update_elem(&write_count, &class, &zero, BPF_NOEXIST);
1288c2ecf20Sopenharmony_ci		val = bpf_map_lookup_elem(&write_count, &class);
1298c2ecf20Sopenharmony_ci		if (!val)
1308c2ecf20Sopenharmony_ci			return 0;
1318c2ecf20Sopenharmony_ci	}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	(*val) += 1;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	return 0;
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cichar _license[] SEC("license") = "GPL";
139