History

bpf: implement bpf_get_current_cgroup_id() helper

bpf has been used extensively for tracing. For example, bcc contains an almost full set of bpf-based tools to trace kernel and user functions/events. Most tracing tools are currently either filtered based on pid or system-wide. Containers have been used quite extensively in industry and cgroup is often used together to provide resource isolation and protection. Several processes may run inside the same container. It is often desirable to get container-level tracing results as well, e.g. syscall count, function count, I/O activity, etc. This patch implements a new helper, bpf_get_current_cgroup_id(), which will return cgroup id based on the cgroup within which the current task is running. The later patch will provide an example to show that userspace can get the same cgroup id so it could configure a filter or policy in the bpf program based on task cgroup id. The helper is currently implemented for tracing. It can be added to other program types as well when needed.

Yonghong Song committed 8 years ago

Commit bf6fa2c1 parent ea9916e5 files · +26 −1Browse files

Showing the changes to include/uapi/linux (1 of 5 files). Show all

Unified · Split

include/uapi/linux/bpf.h+7−1
@@ -2070,6 +2070,11 @@ union bpf_attr { · 2069 unmodified lines
2070 * **CONFIG_SOCK_CGROUP_DATA** configuration option.2070 * **CONFIG_SOCK_CGROUP_DATA** configuration option.
2071 * Return2071 * Return
2072 * The id is returned or 0 in case the id could not be retrieved.2072 * The id is returned or 0 in case the id could not be retrieved.
2073 *
2074 * u64 bpf_get_current_cgroup_id(void)
2075 * Return
2076 * A 64-bit integer containing the current cgroup id based
2077 * on the cgroup within which the current task is running.
2073 */2078 */
2074#define __BPF_FUNC_MAPPER(FN) \2079#define __BPF_FUNC_MAPPER(FN) \
2075 FN(unspec), \2080 FN(unspec), \
@@ -2151,7 +2156,8 @@ union bpf_attr { · 75 unmodified lines
2151 FN(lwt_seg6_action), \2156 FN(lwt_seg6_action), \
2152 FN(rc_repeat), \2157 FN(rc_repeat), \
2153 FN(rc_keydown), \2158 FN(rc_keydown), \
2154 FN(skb_cgroup_id),2159 FN(skb_cgroup_id), \
2160 FN(get_current_cgroup_id),
2155 2161
2156/* integer value in 'imm' field of BPF_CALL instruction selects which helper2162/* integer value in 'imm' field of BPF_CALL instruction selects which helper
2157 * function eBPF program intends to call2163 * function eBPF program intends to call