aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/test_cpp.cpp
blob: abc2a56ab261642cc1efccf7010894be62c53f2f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
#include <iostream>
#include <unistd.h>
#include <linux/bpf.h>
#include <linux/btf.h>
#include <bpf/libbpf.h>
#include <bpf/bpf.h>
#include <bpf/btf.h>

#ifndef _Bool
#define _Bool bool
#endif
#include "test_core_extern.skel.h"
#include "struct_ops_module.skel.h"

template <typename T>
class Skeleton {
private:
	T *skel;
public:
	Skeleton(): skel(nullptr) { }

	~Skeleton() { if (skel) T::destroy(skel); }

	int open(const struct bpf_object_open_opts *opts = nullptr)
	{
		int err;

		if (skel)
			return -EBUSY;

		skel = T::open(opts);
		err = libbpf_get_error(skel);
		if (err) {
			skel = nullptr;
			return err;
		}

		return 0;
	}

	int load() { return T::load(skel); }

	int attach() { return T::attach(skel); }

	void detach() { return T::detach(skel); }

	const T* operator->() const { return skel; }

	T* operator->() { return skel; }

	const T *get() const { return skel; }
};

static void dump_printf(void *ctx, const char *fmt, va_list args)
{
}

static void try_skeleton_template()
{
	Skeleton<test_core_extern> skel;
	std::string prog_name;
	int err;
	LIBBPF_OPTS(bpf_object_open_opts, opts);

	err = skel.open(&opts);
	if (err) {
		fprintf(stderr, "Skeleton open failed: %d\n", err);
		return;
	}

	skel->data->kern_ver = 123;
	skel->data->int_val = skel->data->ushort_val;

	err = skel.load();
	if (err) {
		fprintf(stderr, "Skeleton load failed: %d\n", err);
		return;
	}

	if (!skel->kconfig->CONFIG_BPF_SYSCALL)
		fprintf(stderr, "Seems like CONFIG_BPF_SYSCALL isn't set?!\n");

	err = skel.attach();
	if (err) {
		fprintf(stderr, "Skeleton attach failed: %d\n", err);
		return;
	}

	prog_name = bpf_program__name(skel->progs.handle_sys_enter);
	if (prog_name != "handle_sys_enter")
		fprintf(stderr, "Unexpected program name: %s\n", prog_name.c_str());

	bpf_link__destroy(skel->links.handle_sys_enter);
	skel->links.handle_sys_enter = bpf_program__attach(skel->progs.handle_sys_enter);

	skel.detach();

	/* destructor will destroy underlying skeleton */
}

int main(int argc, char *argv[])
{
	struct btf_dump_opts opts = { };
	struct test_core_extern *skel;
	struct struct_ops_module *skel2;
	struct btf *btf;
	int fd;

	try_skeleton_template();

	/* libbpf.h */
	libbpf_set_print(NULL);

	/* bpf.h */
	bpf_prog_get_fd_by_id(0);

	/* btf.h */
	btf = btf__new(NULL, 0);
	if (!libbpf_get_error(btf))
		btf_dump__new(btf, dump_printf, nullptr, &opts);

	/* BPF skeleton */
	skel = test_core_extern__open_and_load();
	test_core_extern__destroy(skel);

	skel2 = struct_ops_module__open_and_load();
	struct_ops_module__destroy(skel2);

	fd = bpf_enable_stats(BPF_STATS_RUN_TIME);
	if (fd < 0)
		std::cout << "FAILED to enable stats: " << fd << std::endl;
	else
		::close(fd);

	std::cout << "DONE!" << std::endl;

	return 0;
}
deletions'>-6/+33 2016-11-17mwifiex: fix memory leak in mwifiex_save_hidden_ssid_channels()Ricky Liang1-0/+4 2016-11-17ssb: Fix error routine when fallback SPROM failsLarry Finger1-0/+1 2016-11-17rtlwifi: Use dev_kfree_skb_irq instead of kfree_skbWei Yongjun1-1/+1 2016-11-17cw1200: fix bogus maybe-uninitialized warningArnd Bergmann1-5/+3 2016-11-17wireless: fix bogus maybe-uninitialized warningArnd Bergmann4-7/+7 2016-11-17p54: memset(0) whole arrayJiri Slaby1-1/+1 2016-11-17brcmfmac: print name of connect status eventRafał Miłecki3-3/+6 2016-11-17rsi: Host to device command frame vap_capabilites modified with new field vap...Prameela Rani Garnepudi3-4/+18 2016-11-17rsi: Fix memory leak in module unloadPrameela Rani Garnepudi1-0/+11 2016-11-17wlcore: Allow scans when in AP modeJames Minor1-0/+2 2016-11-17rtlwifi: Remove address of Free Software FoundationLarry Finger4-16/+0 2016-11-17rtlwifi: rtl8192c: Remove address of Free Software FoundationLarry Finger6-24/+0 2016-11-17rtlwifi: rtl8188ee: Remove address of Free Software FoundationLarry Finger4-16/+0 2016-11-17rtlwifi: rtl8723ae: Remove address of Free Software FoundationLarry Finger4-16/+0 2016-11-17rtlwifi: rtl8192cu: Remove address of Free Software FoundationLarry Finger20-80/+0 2016-11-17rtlwifi: rtl8192ce: Remove address of Free Software FoundationLarry Finger18-72/+0 2016-11-17rtlwifi: rtl8192se: Remove address of Free Software FoundationLarry Finger20-80/+0 2016-11-17rtlwifi: rtl8192de: Remove address of Free Software FoundationLarry Finger20-80/+0 2016-11-17mwifiex: report wakeup for wowlanRajat Jain2-0/+9 2016-11-17wlcore: Add RX_BA_WIN_SIZE_CHANGE_EVENT eventMaxim Altshul3-1/+31 2016-11-17wlcore: Pass win_size taken from ieee80211_sta to FWMaxim Altshul3-5/+9 2016-11-15ath10k: use the right length of "background"Nicolas Iooss1-1/+1 2016-11-15ath10k: remove extraneous error message in tx allocMohammed Shafi Shajakhan1-3/+1 2016-11-15ath10k: clean up HTT tx buffer allocation and freeMohammed Shafi Shajakhan1-25/+50 2016-11-15ath10k: fix failure to send NULL func frame for 10.4Mohammed Shafi Shajakhan2-6/+2 2016-11-15ath9k: Switch to using mac80211 intermediate software queues.Toke Høiland-Jørgensen8-235/+163 2016-11-15ath9k_htc: fix minor mistakes in dev_err messagesColin Ian King1-3/+3 2016-11-15ath9k: parse the device configuration from an OF nodeMartin Blumenstingl1-0/+42 2016-11-15ath9k: add a helper to get the string representation of ath_bus_typeMartin Blumenstingl2-0/+13