xref: /linux/samples/bpf/xdp_sample_user.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1156f886cSKumar Kartikeya Dwivedi // SPDX-License-Identifier: GPL-2.0-only
2156f886cSKumar Kartikeya Dwivedi #ifndef XDP_SAMPLE_USER_H
3156f886cSKumar Kartikeya Dwivedi #define XDP_SAMPLE_USER_H
4156f886cSKumar Kartikeya Dwivedi 
5156f886cSKumar Kartikeya Dwivedi #include <bpf/libbpf.h>
6156f886cSKumar Kartikeya Dwivedi #include <linux/compiler.h>
7156f886cSKumar Kartikeya Dwivedi 
8156f886cSKumar Kartikeya Dwivedi #include "xdp_sample_shared.h"
9156f886cSKumar Kartikeya Dwivedi 
10156f886cSKumar Kartikeya Dwivedi enum stats_mask {
11156f886cSKumar Kartikeya Dwivedi 	_SAMPLE_REDIRECT_MAP         = 1U << 0,
12156f886cSKumar Kartikeya Dwivedi 	SAMPLE_RX_CNT                = 1U << 1,
131d930fd2SKumar Kartikeya Dwivedi 	SAMPLE_REDIRECT_ERR_CNT      = 1U << 2,
14d771e217SKumar Kartikeya Dwivedi 	SAMPLE_CPUMAP_ENQUEUE_CNT    = 1U << 3,
15d771e217SKumar Kartikeya Dwivedi 	SAMPLE_CPUMAP_KTHREAD_CNT    = 1U << 4,
1682c45080SKumar Kartikeya Dwivedi 	SAMPLE_EXCEPTION_CNT         = 1U << 5,
17af93d58cSKumar Kartikeya Dwivedi 	SAMPLE_DEVMAP_XMIT_CNT       = 1U << 6,
181d930fd2SKumar Kartikeya Dwivedi 	SAMPLE_REDIRECT_CNT          = 1U << 7,
191d930fd2SKumar Kartikeya Dwivedi 	SAMPLE_REDIRECT_MAP_CNT      = SAMPLE_REDIRECT_CNT | _SAMPLE_REDIRECT_MAP,
201d930fd2SKumar Kartikeya Dwivedi 	SAMPLE_REDIRECT_ERR_MAP_CNT  = SAMPLE_REDIRECT_ERR_CNT | _SAMPLE_REDIRECT_MAP,
21af93d58cSKumar Kartikeya Dwivedi 	SAMPLE_DEVMAP_XMIT_CNT_MULTI = 1U << 8,
22594a116bSKumar Kartikeya Dwivedi 	SAMPLE_SKIP_HEADING	     = 1U << 9,
23156f886cSKumar Kartikeya Dwivedi };
24156f886cSKumar Kartikeya Dwivedi 
25156f886cSKumar Kartikeya Dwivedi /* Exit return codes */
26156f886cSKumar Kartikeya Dwivedi #define EXIT_OK			0
27156f886cSKumar Kartikeya Dwivedi #define EXIT_FAIL		1
28156f886cSKumar Kartikeya Dwivedi #define EXIT_FAIL_OPTION	2
29156f886cSKumar Kartikeya Dwivedi #define EXIT_FAIL_XDP		3
30156f886cSKumar Kartikeya Dwivedi #define EXIT_FAIL_BPF		4
31156f886cSKumar Kartikeya Dwivedi #define EXIT_FAIL_MEM		5
32156f886cSKumar Kartikeya Dwivedi 
33156f886cSKumar Kartikeya Dwivedi int sample_setup_maps(struct bpf_map **maps);
34156f886cSKumar Kartikeya Dwivedi int __sample_init(int mask);
35156f886cSKumar Kartikeya Dwivedi void sample_exit(int status);
36156f886cSKumar Kartikeya Dwivedi int sample_run(int interval, void (*post_cb)(void *), void *ctx);
37156f886cSKumar Kartikeya Dwivedi 
38156f886cSKumar Kartikeya Dwivedi void sample_switch_mode(void);
39156f886cSKumar Kartikeya Dwivedi int sample_install_xdp(struct bpf_program *xdp_prog, int ifindex, bool generic,
40156f886cSKumar Kartikeya Dwivedi 		       bool force);
41156f886cSKumar Kartikeya Dwivedi void sample_usage(char *argv[], const struct option *long_options,
42156f886cSKumar Kartikeya Dwivedi 		  const char *doc, int mask, bool error);
43156f886cSKumar Kartikeya Dwivedi 
44156f886cSKumar Kartikeya Dwivedi const char *get_driver_name(int ifindex);
45156f886cSKumar Kartikeya Dwivedi int get_mac_addr(int ifindex, void *mac_addr);
46156f886cSKumar Kartikeya Dwivedi 
47156f886cSKumar Kartikeya Dwivedi #pragma GCC diagnostic push
486f670d06SAlexander Lobakin #ifndef __clang__
49156f886cSKumar Kartikeya Dwivedi #pragma GCC diagnostic ignored "-Wstringop-truncation"
506f670d06SAlexander Lobakin #endif
51156f886cSKumar Kartikeya Dwivedi __attribute__((unused))
safe_strncpy(char * dst,const char * src,size_t size)52156f886cSKumar Kartikeya Dwivedi static inline char *safe_strncpy(char *dst, const char *src, size_t size)
53156f886cSKumar Kartikeya Dwivedi {
54156f886cSKumar Kartikeya Dwivedi 	if (!size)
55156f886cSKumar Kartikeya Dwivedi 		return dst;
56156f886cSKumar Kartikeya Dwivedi 	strncpy(dst, src, size - 1);
57156f886cSKumar Kartikeya Dwivedi 	dst[size - 1] = '\0';
58156f886cSKumar Kartikeya Dwivedi 	return dst;
59156f886cSKumar Kartikeya Dwivedi }
60156f886cSKumar Kartikeya Dwivedi #pragma GCC diagnostic pop
61156f886cSKumar Kartikeya Dwivedi 
621d930fd2SKumar Kartikeya Dwivedi #define __attach_tp(name)                                                      \
631d930fd2SKumar Kartikeya Dwivedi 	({                                                                     \
64*61afd3daSAndrii Nakryiko 		if (bpf_program__type(skel->progs.name) != BPF_PROG_TYPE_TRACING)\
651d930fd2SKumar Kartikeya Dwivedi 			return -EINVAL;                                        \
661d930fd2SKumar Kartikeya Dwivedi 		skel->links.name = bpf_program__attach(skel->progs.name);      \
671d930fd2SKumar Kartikeya Dwivedi 		if (!skel->links.name)                                         \
681d930fd2SKumar Kartikeya Dwivedi 			return -errno;                                         \
691d930fd2SKumar Kartikeya Dwivedi 	})
701d930fd2SKumar Kartikeya Dwivedi 
71af93d58cSKumar Kartikeya Dwivedi #define sample_init_pre_load(skel)                                             \
72af93d58cSKumar Kartikeya Dwivedi 	({                                                                     \
73af93d58cSKumar Kartikeya Dwivedi 		skel->rodata->nr_cpus = libbpf_num_possible_cpus();            \
74af93d58cSKumar Kartikeya Dwivedi 		sample_setup_maps((struct bpf_map *[]){                        \
75af93d58cSKumar Kartikeya Dwivedi 			skel->maps.rx_cnt, skel->maps.redir_err_cnt,           \
76af93d58cSKumar Kartikeya Dwivedi 			skel->maps.cpumap_enqueue_cnt,                         \
77af93d58cSKumar Kartikeya Dwivedi 			skel->maps.cpumap_kthread_cnt,                         \
78af93d58cSKumar Kartikeya Dwivedi 			skel->maps.exception_cnt, skel->maps.devmap_xmit_cnt,  \
79af93d58cSKumar Kartikeya Dwivedi 			skel->maps.devmap_xmit_cnt_multi });                   \
80af93d58cSKumar Kartikeya Dwivedi 	})
81af93d58cSKumar Kartikeya Dwivedi 
82156f886cSKumar Kartikeya Dwivedi #define DEFINE_SAMPLE_INIT(name)                                               \
83156f886cSKumar Kartikeya Dwivedi 	static int sample_init(struct name *skel, int mask)                    \
84156f886cSKumar Kartikeya Dwivedi 	{                                                                      \
85156f886cSKumar Kartikeya Dwivedi 		int ret;                                                       \
86156f886cSKumar Kartikeya Dwivedi 		ret = __sample_init(mask);                                     \
87156f886cSKumar Kartikeya Dwivedi 		if (ret < 0)                                                   \
88156f886cSKumar Kartikeya Dwivedi 			return ret;                                            \
891d930fd2SKumar Kartikeya Dwivedi 		if (mask & SAMPLE_REDIRECT_MAP_CNT)                            \
901d930fd2SKumar Kartikeya Dwivedi 			__attach_tp(tp_xdp_redirect_map);                      \
911d930fd2SKumar Kartikeya Dwivedi 		if (mask & SAMPLE_REDIRECT_CNT)                                \
921d930fd2SKumar Kartikeya Dwivedi 			__attach_tp(tp_xdp_redirect);                          \
931d930fd2SKumar Kartikeya Dwivedi 		if (mask & SAMPLE_REDIRECT_ERR_MAP_CNT)                        \
941d930fd2SKumar Kartikeya Dwivedi 			__attach_tp(tp_xdp_redirect_map_err);                  \
951d930fd2SKumar Kartikeya Dwivedi 		if (mask & SAMPLE_REDIRECT_ERR_CNT)                            \
961d930fd2SKumar Kartikeya Dwivedi 			__attach_tp(tp_xdp_redirect_err);                      \
97d771e217SKumar Kartikeya Dwivedi 		if (mask & SAMPLE_CPUMAP_ENQUEUE_CNT)                          \
98d771e217SKumar Kartikeya Dwivedi 			__attach_tp(tp_xdp_cpumap_enqueue);                    \
99d771e217SKumar Kartikeya Dwivedi 		if (mask & SAMPLE_CPUMAP_KTHREAD_CNT)                          \
100d771e217SKumar Kartikeya Dwivedi 			__attach_tp(tp_xdp_cpumap_kthread);                    \
10182c45080SKumar Kartikeya Dwivedi 		if (mask & SAMPLE_EXCEPTION_CNT)                               \
10282c45080SKumar Kartikeya Dwivedi 			__attach_tp(tp_xdp_exception);                         \
103af93d58cSKumar Kartikeya Dwivedi 		if (mask & SAMPLE_DEVMAP_XMIT_CNT)                             \
104af93d58cSKumar Kartikeya Dwivedi 			__attach_tp(tp_xdp_devmap_xmit);                       \
105af93d58cSKumar Kartikeya Dwivedi 		if (mask & SAMPLE_DEVMAP_XMIT_CNT_MULTI)                       \
106af93d58cSKumar Kartikeya Dwivedi 			__attach_tp(tp_xdp_devmap_xmit_multi);                 \
107156f886cSKumar Kartikeya Dwivedi 		return 0;                                                      \
108156f886cSKumar Kartikeya Dwivedi 	}
109156f886cSKumar Kartikeya Dwivedi 
110156f886cSKumar Kartikeya Dwivedi #endif
111