Skip to contentSkip to conversation

bpf: sk lookup, cgroup id helpers in cgroup skb

PATCH Superseded Andrey Ignatov 2 participants 17 messages May 13, 2020 – May 14, 2020

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#1 lore raw

PATCH v2 1/5 bpf: Allow sk lookup helpers in cgroup skb

In reply to Andrey Ignatov

Currently sk lookup helpers are allowed in tc, xdp, sk skb, and cgroup sock_addr programs.

But they would be useful in cgroup skb as well so that for example cgroup skb ingress program can lookup a peer socket a packet comes from on same host and make a decision whether to allow or deny this packet based on the properties of that socket, e.g. cgroup that peer socket belongs to.

Allow the following sk lookup helpers in cgroup skb:
* bpf_sk_lookup_tcp;
* bpf_sk_lookup_udp;
* bpf_sk_release;
* bpf_skc_lookup_tcp.

---
 net/core/filter.c | 8 ++++++++
 1 file changed, 8 insertions(+)
net/core/filter.c +8 -0
index da0634979f53..ccb560c1a1db 100644
@@ -6159,6 +6159,14 @@ cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return &bpf_skb_cgroup_id_proto;
 #endif
 #ifdef CONFIG_INET
+	case BPF_FUNC_sk_lookup_tcp:
+		return &bpf_sk_lookup_tcp_proto;
+	case BPF_FUNC_sk_lookup_udp:
+		return &bpf_sk_lookup_udp_proto;
+	case BPF_FUNC_sk_release:
+		return &bpf_sk_release_proto;
+	case BPF_FUNC_skc_lookup_tcp:
+		return &bpf_skc_lookup_tcp_proto;
 	case BPF_FUNC_tcp_sock:
 		return &bpf_tcp_sock_proto;
 	case BPF_FUNC_get_listener_sock:
Signature · 1 line
2.24.1
#5 lore raw

PATCH v2 3/5 bpf: Introduce bpf_sk_{,ancestor_}cgroup_id helpers

In reply to Andrey Ignatov

With having ability to lookup sockets in cgroup skb programs it becomes useful to access cgroup id of retrieved sockets so that policies can be implemented based on origin cgroup of such socket.

For example, a container running in a cgroup can have cgroup skb ingress program that can lookup peer socket that is sending packets to a process inside the container and decide whether those packets should be allowed or denied based on cgroup id of the peer.

More specifically such ingress program can implement intra-host policy "allow incoming packets only from this same container and not from any other container on same host" w/o relying on source IP addresses since quite often it can be the case that containers share same IP address on the host.

Introduce two new helpers for this use-case: bpf_sk_cgroup_id() and bpf_sk_ancestor_cgroup_id().

These helpers are similar to existing bpf_skb_{,ancestor_}cgroup_id helpers with the only difference that sk is used to get cgroup id instead of skb, and share code with them.

See documentation in UAPI for more details.

---
 include/uapi/linux/bpf.h       | 35 +++++++++++++++++++-
 net/core/filter.c              | 60 +++++++++++++++++++++++++++++-----
 tools/include/uapi/linux/bpf.h | 35 +++++++++++++++++++-
 3 files changed, 119 insertions(+), 11 deletions(-)
include/uapi/linux/bpf.h +34 -1
index bfb31c1be219..e3cbc2790cdf 100644
@@ -3121,6 +3121,37 @@ union bpf_attr {
  * 		0 on success, or a negative error in case of failure:
  *
  *		**-EOVERFLOW** if an overflow happened: The same object will be tried again.
+ *
+ * u64 bpf_sk_cgroup_id(struct bpf_sock *sk)
+ *	Description
+ *		Return the cgroup v2 id of the socket *sk*.
+ *
+ *		*sk* must be a non-**NULL** pointer that was returned from
+ *		**bpf_sk_lookup_xxx**\ (). The format of returned id is same
+ *		as in **bpf_skb_cgroup_id**\ ().
+ *
+ *		This helper is available only if the kernel was compiled with
+ *		the **CONFIG_SOCK_CGROUP_DATA** configuration option.
+ *	Return
+ *		The id is returned or 0 in case the id could not be retrieved.
+ *
+ * u64 bpf_sk_ancestor_cgroup_id(struct bpf_sock *sk, int ancestor_level)
+ *	Description
+ *		Return id of cgroup v2 that is ancestor of cgroup associated
+ *		with the *sk* at the *ancestor_level*.  The root cgroup is at
+ *		*ancestor_level* zero and each step down the hierarchy
+ *		increments the level. If *ancestor_level* == level of cgroup
+ *		associated with *sk*, then return value will be same as that
+ *		of **bpf_sk_cgroup_id**\ ().
+ *
+ *		The helper is useful to implement policies based on cgroups
+ *		that are upper in hierarchy than immediate cgroup associated
+ *		with *sk*.
+ *
+ *		The format of returned id and helper limitations are same as in
+ *		**bpf_sk_cgroup_id**\ ().
+ *	Return
+ *		The id is returned or 0 in case the id could not be retrieved.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -3250,7 +3281,9 @@ union bpf_attr {
 	FN(sk_assign),			\
 	FN(ktime_get_boot_ns),		\
 	FN(seq_printf),			\
-	FN(seq_write),
+	FN(seq_write),			\
+	FN(sk_cgroup_id),		\
+	FN(sk_ancestor_cgroup_id),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
net/core/filter.c +51 -9

101 lines not on this page · raw message

tools/include/uapi/linux/bpf.h +34 -1
index bfb31c1be219..e3cbc2790cdf 100644
@@ -3121,6 +3121,37 @@ union bpf_attr {
  * 		0 on success, or a negative error in case of failure:
  *
  *		**-EOVERFLOW** if an overflow happened: The same object will be tried again.
+ *
+ * u64 bpf_sk_cgroup_id(struct bpf_sock *sk)
+ *	Description
+ *		Return the cgroup v2 id of the socket *sk*.
+ *
+ *		*sk* must be a non-**NULL** pointer that was returned from
+ *		**bpf_sk_lookup_xxx**\ (). The format of returned id is same
+ *		as in **bpf_skb_cgroup_id**\ ().
+ *
+ *		This helper is available only if the kernel was compiled with
+ *		the **CONFIG_SOCK_CGROUP_DATA** configuration option.
+ *	Return
+ *		The id is returned or 0 in case the id could not be retrieved.
+ *
+ * u64 bpf_sk_ancestor_cgroup_id(struct bpf_sock *sk, int ancestor_level)
+ *	Description
+ *		Return id of cgroup v2 that is ancestor of cgroup associated
+ *		with the *sk* at the *ancestor_level*.  The root cgroup is at
+ *		*ancestor_level* zero and each step down the hierarchy
+ *		increments the level. If *ancestor_level* == level of cgroup
+ *		associated with *sk*, then return value will be same as that
+ *		of **bpf_sk_cgroup_id**\ ().
+ *
+ *		The helper is useful to implement policies based on cgroups
+ *		that are upper in hierarchy than immediate cgroup associated
+ *		with *sk*.
+ *
+ *		The format of returned id and helper limitations are same as in
+ *		**bpf_sk_cgroup_id**\ ().
+ *	Return
+ *		The id is returned or 0 in case the id could not be retrieved.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -3250,7 +3281,9 @@ union bpf_attr {
 	FN(sk_assign),			\
 	FN(ktime_get_boot_ns),		\
 	FN(seq_printf),			\
-	FN(seq_write),
+	FN(seq_write),			\
+	FN(sk_cgroup_id),		\
+	FN(sk_ancestor_cgroup_id),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
Signature · 1 line
2.24.1
#11 lore raw

PATCH v2 4/5 selftests/bpf: Add connect_fd_to_fd, connect_wait net helpers

In reply to Andrey Ignatov

Add two new network helpers.

connect_fd_to_fd connects an already created client socket fd to address of server fd. Sometimes it's useful to separate client socket creation and connecting this socket to a server, e.g. if client socket has to be created in a cgroup different from that of server cgroup.

Additionally connect_to_fd is now implemented using connect_fd_to_fd, both helpers don't treat EINPROGRESS as an error and let caller decide how to proceed with it.

connect_wait is a helper to work with non-blocking client sockets so that if connect_to_fd or connect_fd_to_fd returned -1 with errno == EINPROGRESS, caller can wait for connect to finish or for connection timeout. The helper returns -1 on error, 0 on timeout (1sec, hard-coded), and positive number on success.

---
 tools/testing/selftests/bpf/network_helpers.c | 66 +++++++++++++++----
 tools/testing/selftests/bpf/network_helpers.h |  2 +
 2 files changed, 56 insertions(+), 12 deletions(-)
tools/testing/selftests/bpf/network_helpers.c +54 -12
index 0ff64b70b746..542d71ed7f5d 100644
@@ -4,10 +4,14 @@
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
+
+#include <sys/epoll.h>
+
 #include <linux/err.h>
 #include <linux/in.h>
 #include <linux/in6.h>
 
+#include "bpf_util.h"
 #include "network_helpers.h"
 
 #define clean_errno() (errno == 0 ? "None" : strerror(errno))
@@ -77,8 +81,6 @@ static const size_t timeo_optlen = sizeof(timeo_sec);
 
 int connect_to_fd(int family, int type, int server_fd)
 {
-	struct sockaddr_storage addr;
-	socklen_t len = sizeof(addr);
 	int fd;
 
 	fd = socket(family, type, 0);
@@ -87,24 +89,64 @@ int connect_to_fd(int family, int type, int server_fd)
 		return -1;
 	}
 
-	if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo_sec, timeo_optlen)) {
+	if (connect_fd_to_fd(fd, server_fd) < 0 && errno != EINPROGRESS) {
+		close(fd);
+		return -1;
+	}
+
+	return fd;
+}
+
+int connect_fd_to_fd(int client_fd, int server_fd)
+{
+	struct sockaddr_storage addr;
+	socklen_t len = sizeof(addr);
+
+	if (setsockopt(client_fd, SOL_SOCKET, SO_RCVTIMEO, &timeo_sec,
+		       timeo_optlen)) {
 		log_err("Failed to set SO_RCVTIMEO");
-		goto out;
+		return -1;
 	}
 
 	if (getsockname(server_fd, (struct sockaddr *)&addr, &len)) {
 		log_err("Failed to get server addr");
-		goto out;
+		return -1;
 	}
 
-	if (connect(fd, (const struct sockaddr *)&addr, len) < 0) {
-		log_err("Fail to connect to server with family %d", family);
-		goto out;
+	if (connect(client_fd, (const struct sockaddr *)&addr, len) < 0) {
+		if (errno != EINPROGRESS)
+			log_err("Failed to connect to server");
+		return -1;
 	}
 
-	return fd;
+	return 0;
+}
+
+int connect_wait(int fd)
+{
+	struct epoll_event ev = {}, events[2];
+	int timeout_ms = 1000;
+	int efd, nfd;
+
+	efd = epoll_create1(EPOLL_CLOEXEC);
+	if (efd < 0) {
+		log_err("Failed to open epoll fd");
+		return -1;
+	}
+
+	ev.events = EPOLLRDHUP | EPOLLOUT;
+	ev.data.fd = fd;
+
+	if (epoll_ctl(efd, EPOLL_CTL_ADD, fd, &ev) < 0) {
+		log_err("Failed to register fd=%d on epoll fd=%d", fd, efd);
+		close(efd);
+		return -1;
+	}
+
+	nfd = epoll_wait(efd, events, ARRAY_SIZE(events), timeout_ms);
+	if (nfd < 0)
+		log_err("Failed to wait for I/O event on epoll fd=%d", efd);
 
-out:
-	close(fd);
-	return -1;
+	close(efd);
+	return nfd;
 }
tools/testing/selftests/bpf/network_helpers.h +2 -0
index a0be7db4f67d..86914e6e7b53 100644
@@ -35,5 +35,7 @@ extern struct ipv6_packet pkt_v6;
 
 int start_server(int family, int type);
 int connect_to_fd(int family, int type, int server_fd);
+int connect_fd_to_fd(int client_fd, int server_fd);
+int connect_wait(int client_fd);
 
 #endif
Signature · 1 line
2.24.1
#14 lore raw

PATCH v2 5/5 selftests/bpf: Test for sk helpers in cgroup skb

In reply to Andrey Ignatov

Test bpf_sk_lookup_tcp, bpf_sk_release, bpf_sk_cgroup_id and bpf_sk_ancestor_cgroup_id helpers from cgroup skb program.

The test creates a testing cgroup, starts a TCPv6 server inside the cgroup and creates two client sockets: one inside testing cgroup and one outside.

Then it attaches cgroup skb program to the cgroup that checks all TCP segments coming to the server and allows only those coming from the cgroup of the server. If a segment comes from a peer outside of the cgroup, it'll be dropped.

Finally the test checks that client from inside testing cgroup can successfully connect to the server, but client outside the cgroup fails to connect by timeout.

The main goal of the test is to check newly introduced bpf_sk_{,ancestor_}cgroup_id helpers.

It also checks a couple of socket lookup helpers (tcp & release), but lookup helpers were introduced much earlier and covered by other tests. Here it's mostly checked that they can be called from cgroup skb.

---
 .../bpf/prog_tests/cgroup_skb_sk_lookup.c     |  99 +++++++++++++++++
 .../bpf/progs/cgroup_skb_sk_lookup_kern.c     | 105 ++++++++++++++++++
 2 files changed, 204 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c
 create mode 100644 tools/testing/selftests/bpf/progs/cgroup_skb_sk_lookup_kern.c
tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c new file +99 -0
new file mode 100644
index 000000000000..7ae0f49a2118
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2020 Facebook
+
+#include <test_progs.h>
+
+#include "network_helpers.h"
+#include "cgroup_skb_sk_lookup_kern.skel.h"
+
+static void run_lookup_test(int map_fd, int out_sk)
+{
+	int serv_sk = -1, in_sk = -1, serv_in_sk = -1, err;
+	__u32 serv_port_key = 0, duration = 0;
+	struct sockaddr_in6 addr = {};
+	socklen_t addr_len = sizeof(addr);
+
+	serv_sk = start_server(AF_INET6, SOCK_STREAM);
+	if (CHECK(serv_sk < 0, "start_server", "failed to start server\n"))
+		return;
+
+	err = getsockname(serv_sk, (struct sockaddr *)&addr, &addr_len);
+	if (CHECK(err, "getsockname", "errno %d\n", errno))
+		goto cleanup;
+
+	err = bpf_map_update_elem(map_fd, &serv_port_key, &addr.sin6_port, 0);
+	if (CHECK(err < 0, "map_update", "errno %d", errno))
+		goto cleanup;
+
+	/* Client outside of test cgroup should fail to connect by timeout. */
+	err = connect_fd_to_fd(out_sk, serv_sk);
+	if (CHECK(!err || errno != EINPROGRESS, "connect_fd_to_fd",
+		  "unexpected result err %d errno %d\n", err, errno))
+		goto cleanup;
+
+	err = connect_wait(out_sk);
+	if (CHECK(err, "connect_wait", "unexpected result %d\n", err))
+		goto cleanup;
+
+	/* Client inside test cgroup should connect just fine. */
+	in_sk = connect_to_fd(AF_INET6, SOCK_STREAM, serv_sk);
+	if (CHECK(in_sk < 0, "connect_to_fd", "errno %d\n", errno))
+		goto cleanup;
+
+	serv_in_sk = accept(serv_sk, NULL, NULL);
+	if (CHECK(serv_in_sk < 0, "accept", "errno %d\n", errno))
+		goto cleanup;
+
+cleanup:
+	close(serv_in_sk);
+	close(in_sk);
+	close(serv_sk);
+}
+
+static void run_cgroup_bpf_test(const char *cg_path, const char *bpf_file,
+				int out_sk)
+{
+	struct cgroup_skb_sk_lookup_kern *skel;
+	struct bpf_link *link;
+	__u32 duration = 0;
+	int cgfd = -1;
+
+	skel = cgroup_skb_sk_lookup_kern__open_and_load();
+	if (CHECK(!skel, "skel_open_load", "open_load failed\n"))
+		return;
+
+	cgfd = test__join_cgroup(cg_path);
+	if (CHECK(cgfd < 0, "cgroup_join", "cgroup setup failed\n"))
+		goto cleanup;
+
+	link = bpf_program__attach_cgroup(skel->progs.ingress_lookup, cgfd);
+	if (CHECK(IS_ERR(link), "cgroup_attach", "err: %ld\n", PTR_ERR(link)))
+		goto cleanup;
+
+	run_lookup_test(bpf_map__fd(skel->maps.serv_port), out_sk);
+
+	bpf_link__destroy(link);
+
+cleanup:
+	close(cgfd);
+	cgroup_skb_sk_lookup_kern__destroy(skel);
+}
+
+void test_cgroup_skb_sk_lookup(void)
+{
+	const char *bpf_file = "cgroup_skb_sk_lookup_kern.o";
+	const char *cg_path = "/foo";
+	int out_sk;
+
+	/* Create a socket before joining testing cgroup so that its cgroup id
+	 * differs from that of testing cgroup. Moving selftests process to
+	 * testing cgroup won't change cgroup id of an already created socket.
+	 */
+	out_sk = socket(AF_INET6, SOCK_STREAM | SOCK_NONBLOCK, 0);
+	if (CHECK_FAIL(out_sk < 0))
+		return;
+
+	run_cgroup_bpf_test(cg_path, bpf_file, out_sk);
+
+	close(out_sk);
+}
tools/testing/selftests/bpf/progs/cgroup_skb_sk_lookup_kern.c new file +105 -0

105 lines not on this page · raw message

Signature · 1 line
2.24.1
#3 lore raw

PATCH v2 2/5 bpf: Allow skb_ancestor_cgroup_id helper in cgroup skb

In reply to Andrey Ignatov

cgroup skb programs already can use bpf_skb_cgroup_id. Allow bpf_skb_ancestor_cgroup_id as well so that container policies can be implemented for a container that can have sub-cgroups dynamically created, but policies should still be implemented based on cgroup id of container itself not on an id of a sub-cgroup.

---
 net/core/filter.c | 2 ++
 1 file changed, 2 insertions(+)
net/core/filter.c +2 -0
index ccb560c1a1db..f88df77d0ad4 100644
@@ -6157,6 +6157,8 @@ cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 #ifdef CONFIG_SOCK_CGROUP_DATA
 	case BPF_FUNC_skb_cgroup_id:
 		return &bpf_skb_cgroup_id_proto;
+	case BPF_FUNC_skb_ancestor_cgroup_id:
+		return &bpf_skb_ancestor_cgroup_id_proto;
 #endif
 #ifdef CONFIG_INET
 	case BPF_FUNC_sk_lookup_tcp:
Signature · 1 line
2.24.1
#0 lore raw

v2 cover bpf: sk lookup, cgroup id helpers in cgroup skb

v1->v2:
- switch selftests to skeleton.

This patch set allows a bunch of existing sk lookup and skb cgroup id helpers, and adds two new bpf_sk_{,ancestor_}cgroup_id helpers to be used in cgroup skb programs.

It fills the gap to cover a use-case to apply intra-host cgroup-bpf network policy based on a source cgroup a packet comes from.

For example, there can be multiple containers A, B, C running on a host. Every such container runs in its own cgroup that can have multiple sub-cgroups. But all these containers can share some IP addresses.

At the same time container A wants to have a policy for a server S running in it so that only clients from this same container can connect to S, but not from other containers (such as B, C). Source IP address can't be used to decide whether to allow or deny a packet, but it looks reasonable to filter by cgroup id.

The patch set allows to implement the following policy:
* when an ingress packet comes to container's cgroup, lookup peer (client) socket this packet comes from;
* having peer socket, get its cgroup id;
* compare peer cgroup id with self cgroup id and allow packet only if they match, i.e. it comes from same cgroup;
* the "sub-cgroup" part of the story can be addressed by getting not direct cgroup id of the peer socket, but ancestor cgroup id on specified level, similar to existing "ancestor" flavors of cgroup id helpers.

A newly introduced selftest implements such a policy in its basic form to provide a better idea on the use-case.

Patch 1 allows existing sk lookup helpers in cgroup skb. Patch 2 allows skb_ancestor_cgroup_id in cgrou skb. Patch 3 introduces two new helpers to get cgroup id of socket. Patch 4 extends network helpers to use them in the next patch. Patch 5 adds selftest / example of use-case.

Andrey Ignatov (5):
bpf: Allow sk lookup helpers in cgroup skb
bpf: Allow skb_ancestor_cgroup_id helper in cgroup skb
bpf: Introduce bpf_sk_{,ancestor_}cgroup_id helpers selftests/bpf: Add connect_fd_to_fd, connect_wait net helpers selftests/bpf: Test for sk helpers in cgroup skb

 include/uapi/linux/bpf.h                      |  35 +++++-
 net/core/filter.c                             |  70 ++++++++++--
 tools/include/uapi/linux/bpf.h                |  35 +++++-
 tools/testing/selftests/bpf/network_helpers.c |  66 +++++++++--
 tools/testing/selftests/bpf/network_helpers.h |   2 +
 .../bpf/prog_tests/cgroup_skb_sk_lookup.c     |  99 +++++++++++++++++
 .../bpf/progs/cgroup_skb_sk_lookup_kern.c     | 105 ++++++++++++++++++
 7 files changed, 389 insertions(+), 23 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c
 create mode 100644 tools/testing/selftests/bpf/progs/cgroup_skb_sk_lookup_kern.c
Signature · 1 line
2.24.1
Acked-by on v2 1/5
#2 lore raw

Re: [PATCH v2 bpf-next 1/5] bpf: Allow sk lookup helpers in cgroup skb

In reply to Andrey Ignatov
Quoted 9 lines · Andrey Ignatov

Currently sk lookup helpers are allowed in tc, xdp, sk skb, and cgroup sock_addr programs.

But they would be useful in cgroup skb as well so that for example cgroup skb ingress program can lookup a peer socket a packet comes from on same host and make a decision whether to allow or deny this packet based on the properties of that socket, e.g. cgroup that peer socket belongs to.

Quoted 7 lines

Allow the following sk lookup helpers in cgroup skb:
* bpf_sk_lookup_tcp;
* bpf_sk_lookup_udp;
* bpf_sk_release;
* bpf_skc_lookup_tcp.

Acked-by on v2 2/5
#4 lore raw

Re: [PATCH v2 bpf-next 2/5] bpf: Allow skb_ancestor_cgroup_id helper in cgroup skb

In reply to Andrey Ignatov
Quoted 7 lines · Andrey Ignatov

cgroup skb programs already can use bpf_skb_cgroup_id. Allow bpf_skb_ancestor_cgroup_id as well so that container policies can be implemented for a container that can have sub-cgroups dynamically created, but policies should still be implemented based on cgroup id of container itself not on an id of a sub-cgroup.

Acked-by on v2 3/5
#6 lore raw

Re: [PATCH v2 bpf-next 3/5] bpf: Introduce bpf_sk_{,ancestor_}cgroup_id helpers

In reply to Andrey Ignatov
Quoted 18 lines · Andrey Ignatov

With having ability to lookup sockets in cgroup skb programs it becomes useful to access cgroup id of retrieved sockets so that policies can be implemented based on origin cgroup of such socket.

For example, a container running in a cgroup can have cgroup skb ingress program that can lookup peer socket that is sending packets to a process inside the container and decide whether those packets should be allowed or denied based on cgroup id of the peer.

More specifically such ingress program can implement intra-host policy "allow incoming packets only from this same container and not from any other container on same host" w/o relying on source IP addresses since quite often it can be the case that containers share same IP address on the host.

Introduce two new helpers for this use-case: bpf_sk_cgroup_id() and bpf_sk_ancestor_cgroup_id().

Quoted 7 lines

These helpers are similar to existing bpf_skb_{,ancestor_}cgroup_id helpers with the only difference that sk is used to get cgroup id instead of skb, and share code with them.

See documentation in UAPI for more details.

Ack with one nit below.

Quoted 11 lines
---
  include/uapi/linux/bpf.h       | 35 +++++++++++++++++++-
  net/core/filter.c              | 60 +++++++++++++++++++++++++++++-----
  tools/include/uapi/linux/bpf.h | 35 +++++++++++++++++++-
  3 files changed, 119 insertions(+), 11 deletions(-)
include/uapi/linux/bpf.h +0 -0
index bfb31c1be219..e3cbc2790cdf 100644

include/uapi/linux/bpf.h union bpf_attr { · line 3130

Quoted 10 lines
include/uapi/linux/bpf.h +7 -0
@@ -3121,6 +3121,37 @@ union bpf_attr {
   * 		0 on success, or a negative error in case of failure:
   *
   *		**-EOVERFLOW** if an overflow happened: The same object will be tried again.
+ *
+ * u64 bpf_sk_cgroup_id(struct bpf_sock *sk)
+ *	Description
+ *		Return the cgroup v2 id of the socket *sk*.
+ *
+ *		*sk* must be a non-**NULL** pointer that was returned from
+ *		**bpf_sk_lookup_xxx**\ (). The format of returned id is same

It should also include bpf_skc_lookup_tcp(), right?

Quoted 36 lines
include/uapi/linux/bpf.h +27 -1
+ *		as in **bpf_skb_cgroup_id**\ ().
+ *
+ *		This helper is available only if the kernel was compiled with
+ *		the **CONFIG_SOCK_CGROUP_DATA** configuration option.
+ *	Return
+ *		The id is returned or 0 in case the id could not be retrieved.
+ *
+ * u64 bpf_sk_ancestor_cgroup_id(struct bpf_sock *sk, int ancestor_level)
+ *	Description
+ *		Return id of cgroup v2 that is ancestor of cgroup associated
+ *		with the *sk* at the *ancestor_level*.  The root cgroup is at
+ *		*ancestor_level* zero and each step down the hierarchy
+ *		increments the level. If *ancestor_level* == level of cgroup
+ *		associated with *sk*, then return value will be same as that
+ *		of **bpf_sk_cgroup_id**\ ().
+ *
+ *		The helper is useful to implement policies based on cgroups
+ *		that are upper in hierarchy than immediate cgroup associated
+ *		with *sk*.
+ *
+ *		The format of returned id and helper limitations are same as in
+ *		**bpf_sk_cgroup_id**\ ().
+ *	Return
+ *		The id is returned or 0 in case the id could not be retrieved.
   */
  #define __BPF_FUNC_MAPPER(FN)		\
  	FN(unspec),			\
@@ -3250,7 +3281,9 @@ union bpf_attr {
  	FN(sk_assign),			\
  	FN(ktime_get_boot_ns),		\
  	FN(seq_printf),			\
-	FN(seq_write),
+	FN(seq_write),			\
+	FN(sk_cgroup_id),		\
+	FN(sk_ancestor_cgroup_id),
 

[...]

Acked-by on v2 4/5
#12 lore raw

Re: [PATCH v2 bpf-next 4/5] selftests/bpf: Add connect_fd_to_fd, connect_wait net helpers

In reply to Andrey Ignatov
Quoted 11 lines · Andrey Ignatov

Add two new network helpers.

connect_fd_to_fd connects an already created client socket fd to address of server fd. Sometimes it's useful to separate client socket creation and connecting this socket to a server, e.g. if client socket has to be created in a cgroup different from that of server cgroup.

Additionally connect_to_fd is now implemented using connect_fd_to_fd, both helpers don't treat EINPROGRESS as an error and let caller decide how to proceed with it.

Quoted 7 lines

connect_wait is a helper to work with non-blocking client sockets so that if connect_to_fd or connect_fd_to_fd returned -1 with errno == EINPROGRESS, caller can wait for connect to finish or for connection timeout. The helper returns -1 on error, 0 on timeout (1sec, hard-coded), and positive number on success.

Ack with a minor nit below.

Quoted 34 lines
---
  tools/testing/selftests/bpf/network_helpers.c | 66 +++++++++++++++----
  tools/testing/selftests/bpf/network_helpers.h |  2 +
  2 files changed, 56 insertions(+), 12 deletions(-)
tools/testing/selftests/bpf/network_helpers.c +4 -2
index 0ff64b70b746..542d71ed7f5d 100644
@@ -4,10 +4,14 @@
  #include <stdio.h>
  #include <string.h>
  #include <unistd.h>
+
+#include <sys/epoll.h>
+
  #include <linux/err.h>
  #include <linux/in.h>
  #include <linux/in6.h>
 
+#include "bpf_util.h"
  #include "network_helpers.h"
 
  #define clean_errno() (errno == 0 ? "None" : strerror(errno))
@@ -77,8 +81,6 @@ static const size_t timeo_optlen = sizeof(timeo_sec);
 
  int connect_to_fd(int family, int type, int server_fd)
  {
-	struct sockaddr_storage addr;
-	socklen_t len = sizeof(addr);
  	int fd;
 
  	fd = socket(family, type, 0);

tools/testing/selftests/bpf/network_helpers.c int connect_to_fd(int family, int type, int server_fd) · line 93

Quoted 6 lines
tools/testing/selftests/bpf/network_helpers.c +2 -1
@@ -87,24 +89,64 @@ int connect_to_fd(int family, int type, int server_fd)
  		return -1;
  	}
 
-	if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo_sec, timeo_optlen)) {
+	if (connect_fd_to_fd(fd, server_fd) < 0 && errno != EINPROGRESS) {
+		close(fd);

Remote possibility. close(fd) may change error code?

In my opinion, maybe convert the original syscall failure errno to return value and carrying on might be a simpler choice?

Quoted 20 lines
tools/testing/selftests/bpf/network_helpers.c +14 -1
+		return -1;
+	}
+
+	return fd;
+}
+
+int connect_fd_to_fd(int client_fd, int server_fd)
+{
+	struct sockaddr_storage addr;
+	socklen_t len = sizeof(addr);
+
+	if (setsockopt(client_fd, SOL_SOCKET, SO_RCVTIMEO, &timeo_sec,
+		       timeo_optlen)) {
  		log_err("Failed to set SO_RCVTIMEO");
-		goto out;
+		return -1;
  	}
 
  	if (getsockname(server_fd, (struct sockaddr *)&addr, &len)) {
  		log_err("Failed to get server addr");

tools/testing/selftests/bpf/network_helpers.c int connect_to_fd(int family, int type, int server_fd) · line 118

Quoted 10 lines
tools/testing/selftests/bpf/network_helpers.c +4 -4
-		goto out;
+		return -1;
  	}
 
-	if (connect(fd, (const struct sockaddr *)&addr, len) < 0) {
-		log_err("Fail to connect to server with family %d", family);
-		goto out;
+	if (connect(client_fd, (const struct sockaddr *)&addr, len) < 0) {
+		if (errno != EINPROGRESS)
+			log_err("Failed to connect to server");

Not saying it is possible, but any remote possibility log_err() may change error code to EINPROGRESS?

Quoted 6 lines
tools/testing/selftests/bpf/network_helpers.c +3 -1
+		return -1;
  	}
 
-	return fd;
+	return 0;
+}

[...]

#15 lore raw

Re: [PATCH v2 bpf-next 5/5] selftests/bpf: Test for sk helpers in cgroup skb

In reply to Andrey Ignatov
Quoted 80 lines · Andrey Ignatov

Test bpf_sk_lookup_tcp, bpf_sk_release, bpf_sk_cgroup_id and bpf_sk_ancestor_cgroup_id helpers from cgroup skb program.

The test creates a testing cgroup, starts a TCPv6 server inside the cgroup and creates two client sockets: one inside testing cgroup and one outside.

Then it attaches cgroup skb program to the cgroup that checks all TCP segments coming to the server and allows only those coming from the cgroup of the server. If a segment comes from a peer outside of the cgroup, it'll be dropped.

Finally the test checks that client from inside testing cgroup can successfully connect to the server, but client outside the cgroup fails to connect by timeout.

The main goal of the test is to check newly introduced bpf_sk_{,ancestor_}cgroup_id helpers.

It also checks a couple of socket lookup helpers (tcp & release), but lookup helpers were introduced much earlier and covered by other tests. Here it's mostly checked that they can be called from cgroup skb.

---
  .../bpf/prog_tests/cgroup_skb_sk_lookup.c     |  99 +++++++++++++++++
  .../bpf/progs/cgroup_skb_sk_lookup_kern.c     | 105 ++++++++++++++++++
  2 files changed, 204 insertions(+)

create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_skb_sk_lookup_kern.c

tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c new file +43 -0
new file mode 100644
index 000000000000..7ae0f49a2118
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2020 Facebook
+
+#include <test_progs.h>
+
+#include "network_helpers.h"
+#include "cgroup_skb_sk_lookup_kern.skel.h"
+
+static void run_lookup_test(int map_fd, int out_sk)
+{
+	int serv_sk = -1, in_sk = -1, serv_in_sk = -1, err;
+	__u32 serv_port_key = 0, duration = 0;
+	struct sockaddr_in6 addr = {};
+	socklen_t addr_len = sizeof(addr);
+
+	serv_sk = start_server(AF_INET6, SOCK_STREAM);
+	if (CHECK(serv_sk < 0, "start_server", "failed to start server\n"))
+		return;
+
+	err = getsockname(serv_sk, (struct sockaddr *)&addr, &addr_len);
+	if (CHECK(err, "getsockname", "errno %d\n", errno))
+		goto cleanup;
+
+	err = bpf_map_update_elem(map_fd, &serv_port_key, &addr.sin6_port, 0);
+	if (CHECK(err < 0, "map_update", "errno %d", errno))
+		goto cleanup;
+
+	/* Client outside of test cgroup should fail to connect by timeout. */
+	err = connect_fd_to_fd(out_sk, serv_sk);
+	if (CHECK(!err || errno != EINPROGRESS, "connect_fd_to_fd",
+		  "unexpected result err %d errno %d\n", err, errno))
+		goto cleanup;
+
+	err = connect_wait(out_sk);
+	if (CHECK(err, "connect_wait", "unexpected result %d\n", err))
+		goto cleanup;
+
+	/* Client inside test cgroup should connect just fine. */
+	in_sk = connect_to_fd(AF_INET6, SOCK_STREAM, serv_sk);
+	if (CHECK(in_sk < 0, "connect_to_fd", "errno %d\n", errno))
+		goto cleanup;
+
+	serv_in_sk = accept(serv_sk, NULL, NULL);

tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c line 53

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c new file +10 -0
+	if (CHECK(serv_in_sk < 0, "accept", "errno %d\n", errno))
+		goto cleanup;
+
+cleanup:
+	close(serv_in_sk);
+	close(in_sk);
+	close(serv_sk);
+}
+
+static void run_cgroup_bpf_test(const char *cg_path, const char *bpf_file,

You are using skeleton, bpf_file parameter is not needed any more.

Quoted 28 lines
tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c new file +28 -0
+				int out_sk)
+{
+	struct cgroup_skb_sk_lookup_kern *skel;
+	struct bpf_link *link;
+	__u32 duration = 0;
+	int cgfd = -1;
+
+	skel = cgroup_skb_sk_lookup_kern__open_and_load();
+	if (CHECK(!skel, "skel_open_load", "open_load failed\n"))
+		return;
+
+	cgfd = test__join_cgroup(cg_path);
+	if (CHECK(cgfd < 0, "cgroup_join", "cgroup setup failed\n"))
+		goto cleanup;
+
+	link = bpf_program__attach_cgroup(skel->progs.ingress_lookup, cgfd);
+	if (CHECK(IS_ERR(link), "cgroup_attach", "err: %ld\n", PTR_ERR(link)))
+		goto cleanup;
+
+	run_lookup_test(bpf_map__fd(skel->maps.serv_port), out_sk);
+
+	bpf_link__destroy(link);
+
+cleanup:
+	close(cgfd);
+	cgroup_skb_sk_lookup_kern__destroy(skel);
+}
+

[...]

tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c new file +7 -0
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, __u16);
+} serv_port SEC(".maps");

This can be simplified as a global variable, e.g.,

__u16 serv_port = 0;

and use skeleton to access this variable in user space and in kernel you can directly use this variable.

Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c new file +9 -0
+
+
+static inline void set_ip(__u32 *dst, const struct in6_addr *src)
+{
+	dst[0] = src->in6_u.u6_addr32[0];
+	dst[1] = src->in6_u.u6_addr32[1];
+	dst[2] = src->in6_u.u6_addr32[2];
+	dst[3] = src->in6_u.u6_addr32[3];
+}

[...]

#7 lore raw

Re: [PATCH v2 bpf-next 3/5] bpf: Introduce bpf_sk_{,ancestor_}cgroup_id helpers

In reply to Yonghong Song

Yonghong Song <[email protected]> [Thu, 2020-05-14 08:16 -0700]:

Quoted 11 lines · Andrey Ignatov
Quoted 10 lines
include/uapi/linux/bpf.h +1 -0
index bfb31c1be219..e3cbc2790cdf 100644
@@ -3121,6 +3121,37 @@ union bpf_attr {
   * 		0 on success, or a negative error in case of failure:
   *
   *		**-EOVERFLOW** if an overflow happened: The same object will be tried again.
+ *
Quoted 8 lines

include/uapi/linux/bpf.h union bpf_attr { · line 3130

Quoted 6 lines
include/uapi/linux/bpf.h +6 -0
+ * u64 bpf_sk_cgroup_id(struct bpf_sock *sk)
+ *	Description
+ *		Return the cgroup v2 id of the socket *sk*.
+ *
+ *		*sk* must be a non-**NULL** pointer that was returned from
+ *		**bpf_sk_lookup_xxx**\ (). The format of returned id is same

It should also include bpf_skc_lookup_tcp(), right?

From what I see it should not.

cgroup id is available from sk->sk_cgrp_data that is a field of struct sock', i.e. struct sock_common' doesn't have this field.

bpf_skc_lookup_tcp() returns RET_PTR_TO_SOCK_COMMON_OR_NULL and it can be for example struct request_sock that has only struct sock_common member, i.e. it doesn't have cgroup id.

Signature · 1 line
Andrey Ignatov
#8 lore raw

Re: [PATCH v2 bpf-next 3/5] bpf: Introduce bpf_sk_{,ancestor_}cgroup_id helpers

Quoted 21 lines · Andrey Ignatov

Yonghong Song <[email protected]> [Thu, 2020-05-14 08:16 -0700]:

Quoted 11 lines · Andrey Ignatov
Quoted 10 lines
include/uapi/linux/bpf.h +1 -0
index bfb31c1be219..e3cbc2790cdf 100644
@@ -3121,6 +3121,37 @@ union bpf_attr {
    * 		0 on success, or a negative error in case of failure:
    *
    *		**-EOVERFLOW** if an overflow happened: The same object will be tried again.
+ *
Quoted 8 lines

include/uapi/linux/bpf.h union bpf_attr { · line 3130

Quoted 6 lines
include/uapi/linux/bpf.h +6 -0
+ * u64 bpf_sk_cgroup_id(struct bpf_sock *sk)
+ *	Description
+ *		Return the cgroup v2 id of the socket *sk*.
+ *
+ *		*sk* must be a non-**NULL** pointer that was returned from
+ *		**bpf_sk_lookup_xxx**\ (). The format of returned id is same

It should also include bpf_skc_lookup_tcp(), right?

Quoted 8 lines

From what I see it should not.

cgroup id is available from sk->sk_cgrp_data that is a field of struct sock', i.e. struct sock_common' doesn't have this field.

bpf_skc_lookup_tcp() returns RET_PTR_TO_SOCK_COMMON_OR_NULL and it can be for example struct request_sock that has only struct sock_common member, i.e. it doesn't have cgroup id.

So you can do bpf_skc_lookup_tcp() and then do tcp_sock() to get a full socket which will have cgroup_id. I think maybe this is the reason you added bpf_skc_lookup_tcp() in patch #1, right?

If this is the case, maybe rewording a little bit for the description to include bpf_skc_lookup_tcp() + bpf_tcp_sock() as another input to bpf_sk_cgroup_id()?

#13 lore raw

Re: [PATCH v2 bpf-next 4/5] selftests/bpf: Add connect_fd_to_fd, connect_wait net helpers

In reply to Yonghong Song

Yonghong Song <[email protected]> [Thu, 2020-05-14 08:56 -0700]:

Quoted 17 lines · Andrey Ignatov
Quoted 9 lines
tools/testing/selftests/bpf/network_helpers.c +0 -2
@@ -77,8 +81,6 @@ static const size_t timeo_optlen = sizeof(timeo_sec);
  int connect_to_fd(int family, int type, int server_fd)
  {
-	struct sockaddr_storage addr;
-	socklen_t len = sizeof(addr);
  	int fd;
  	fd = socket(family, type, 0);

tools/testing/selftests/bpf/network_helpers.c int connect_to_fd(int family, int type, int server_fd) · line 92

Quoted 5 lines
tools/testing/selftests/bpf/network_helpers.c +2 -1
@@ -87,24 +89,64 @@ int connect_to_fd(int family, int type, int server_fd)
  		return -1;
  	}
-	if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo_sec, timeo_optlen)) {
+	if (connect_fd_to_fd(fd, server_fd) < 0 && errno != EINPROGRESS) {
+		close(fd);

Remote possibility. close(fd) may change error code?

It can in some cases that are rather theoritical in this case (e.g. buggy multi-threaded program closes fd from another thread right before this close()).

But I can save/restore it before/after close just in case.

Quoted 2 lines

In my opinion, maybe convert the original syscall failure errno to return value and carrying on might be a simpler choice?

I wanted to preserve semantics of connect(2) here: return -1 on error, or fd >= 0 on success.

I guess if I save/restore errno it should be fine.

Quoted 23 lines
Quoted 23 lines
tools/testing/selftests/bpf/network_helpers.c +15 -3
+		return -1;
+	}
+
+	return fd;
+}
+
+int connect_fd_to_fd(int client_fd, int server_fd)
+{
+	struct sockaddr_storage addr;
+	socklen_t len = sizeof(addr);
+
+	if (setsockopt(client_fd, SOL_SOCKET, SO_RCVTIMEO, &timeo_sec,
+		       timeo_optlen)) {
  		log_err("Failed to set SO_RCVTIMEO");
-		goto out;
+		return -1;
  	}
  	if (getsockname(server_fd, (struct sockaddr *)&addr, &len)) {
  		log_err("Failed to get server addr");
-		goto out;
+		return -1;
  	}
-	if (connect(fd, (const struct sockaddr *)&addr, len) < 0) {
Quoted 8 lines

tools/testing/selftests/bpf/network_helpers.c int connect_to_fd(int family, int type, int server_fd) · line 118

Quoted 5 lines
tools/testing/selftests/bpf/network_helpers.c +3 -2
-		log_err("Fail to connect to server with family %d", family);
-		goto out;
+	if (connect(client_fd, (const struct sockaddr *)&addr, len) < 0) {
+		if (errno != EINPROGRESS)
+			log_err("Failed to connect to server");

Not saying it is possible, but any remote possibility log_err() may change error code to EINPROGRESS?

To my best knowledge, neither fprintf(3) nor strerror(3) use EINPROGRESS, but since in this case having a reliable way to communicate EINPROGRESS from connect is rather required, I'll save/restore errno for caling log_err.

Signature · 1 line
Andrey Ignatov
#16 lore raw

Re: [PATCH v2 bpf-next 5/5] selftests/bpf: Test for sk helpers in cgroup skb

In reply to Yonghong Song

Yonghong Song <[email protected]> [Thu, 2020-05-14 09:07 -0700]:

Quoted 50 lines · Andrey Ignatov
Quoted 49 lines
tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c new file +47 -0
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2020 Facebook
+
+#include <test_progs.h>
+
+#include "network_helpers.h"
+#include "cgroup_skb_sk_lookup_kern.skel.h"
+
+static void run_lookup_test(int map_fd, int out_sk)
+{
+	int serv_sk = -1, in_sk = -1, serv_in_sk = -1, err;
+	__u32 serv_port_key = 0, duration = 0;
+	struct sockaddr_in6 addr = {};
+	socklen_t addr_len = sizeof(addr);
+
+	serv_sk = start_server(AF_INET6, SOCK_STREAM);
+	if (CHECK(serv_sk < 0, "start_server", "failed to start server\n"))
+		return;
+
+	err = getsockname(serv_sk, (struct sockaddr *)&addr, &addr_len);
+	if (CHECK(err, "getsockname", "errno %d\n", errno))
+		goto cleanup;
+
+	err = bpf_map_update_elem(map_fd, &serv_port_key, &addr.sin6_port, 0);
+	if (CHECK(err < 0, "map_update", "errno %d", errno))
+		goto cleanup;
+
+	/* Client outside of test cgroup should fail to connect by timeout. */
+	err = connect_fd_to_fd(out_sk, serv_sk);
+	if (CHECK(!err || errno != EINPROGRESS, "connect_fd_to_fd",
+		  "unexpected result err %d errno %d\n", err, errno))
+		goto cleanup;
+
+	err = connect_wait(out_sk);
+	if (CHECK(err, "connect_wait", "unexpected result %d\n", err))
+		goto cleanup;
+
+	/* Client inside test cgroup should connect just fine. */
+	in_sk = connect_to_fd(AF_INET6, SOCK_STREAM, serv_sk);
+	if (CHECK(in_sk < 0, "connect_to_fd", "errno %d\n", errno))
+		goto cleanup;
+
+	serv_in_sk = accept(serv_sk, NULL, NULL);
+	if (CHECK(serv_in_sk < 0, "accept", "errno %d\n", errno))
+		goto cleanup;
+
+cleanup:
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c line 53

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/cgroup_skb_sk_lookup.c new file +6 -0
+	close(serv_in_sk);
+	close(in_sk);
+	close(serv_sk);
+}
+
+static void run_cgroup_bpf_test(const char *cg_path, const char *bpf_file,

You are using skeleton, bpf_file parameter is not needed any more.

Oops, forgot to remove it while switching to skeleton. Will fix. Thanks.

Quoted 9 lines

tools/testing/selftests/bpf/progs/cgroup_skb_sk_lookup_kern.c line 25

Quoted 6 lines
tools/testing/selftests/bpf/progs/cgroup_skb_sk_lookup_kern.c new file +6 -0
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, __u16);
+} serv_port SEC(".maps");

This can be simplified as a global variable, e.g.,

Quoted 4 lines

__u16 serv_port = 0;

and use skeleton to access this variable in user space and in kernel you can directly use this variable.

No strong preference here. Ok, will try global var.

Signature · 1 line
Andrey Ignatov
#9 lore raw

Re: [PATCH v2 bpf-next 3/5] bpf: Introduce bpf_sk_{,ancestor_}cgroup_id helpers

In reply to Yonghong Song

Yonghong Song <[email protected]> [Thu, 2020-05-14 10:24 -0700]:

Quoted 33 lines · Andrey Ignatov
Quoted 21 lines

Yonghong Song <[email protected]> [Thu, 2020-05-14 08:16 -0700]:

Quoted 11 lines · Andrey Ignatov
Quoted 10 lines
include/uapi/linux/bpf.h +1 -0
index bfb31c1be219..e3cbc2790cdf 100644
@@ -3121,6 +3121,37 @@ union bpf_attr {
    * 		0 on success, or a negative error in case of failure:
    *
    *		**-EOVERFLOW** if an overflow happened: The same object will be tried again.
+ *
Quoted 8 lines

include/uapi/linux/bpf.h union bpf_attr { · line 3130

Quoted 6 lines
include/uapi/linux/bpf.h +6 -0
+ * u64 bpf_sk_cgroup_id(struct bpf_sock *sk)
+ *	Description
+ *		Return the cgroup v2 id of the socket *sk*.
+ *
+ *		*sk* must be a non-**NULL** pointer that was returned from
+ *		**bpf_sk_lookup_xxx**\ (). The format of returned id is same

It should also include bpf_skc_lookup_tcp(), right?

Quoted 8 lines

From what I see it should not.

cgroup id is available from sk->sk_cgrp_data that is a field of struct sock', i.e. struct sock_common' doesn't have this field.

bpf_skc_lookup_tcp() returns RET_PTR_TO_SOCK_COMMON_OR_NULL and it can be for example struct request_sock that has only struct sock_common member, i.e. it doesn't have cgroup id.

Quoted 7 lines

So you can do bpf_skc_lookup_tcp() and then do tcp_sock() to get a full socket which will have cgroup_id. I think maybe this is the reason you added bpf_skc_lookup_tcp() in patch #1, right?

If this is the case, maybe rewording a little bit for the description to include bpf_skc_lookup_tcp() + bpf_tcp_sock() as another input to bpf_sk_cgroup_id()?

Yeah, this bpf_skc_lookup_tcp() + bpf_tcp_sock() combination should also return a full socket that can be used with the helper.

bpf_sk_fullsock() is one more way to get it.

I'm not sure it's worth listing all possible ways to get full socket since it's 1) easy to miss something; 2) easy to forget to update this list if a new way to get full socket is being added.

What about rephrasing to highlight that it has to be full socket and **bpf_sk_lookup_xxx**\ () is an example of getting it?

For example:

*sk* must be a non-**NULL** pointer to full socket, e.g. one returned from **bpf_sk_lookup_xxx**\ () or **bpf_sk_fullsock**\ ().

Will it be better?

Signature · 1 line
Andrey Ignatov
#10 lore raw

Re: [PATCH v2 bpf-next 3/5] bpf: Introduce bpf_sk_{,ancestor_}cgroup_id helpers

Quoted 54 lines · Andrey Ignatov

Yonghong Song <[email protected]> [Thu, 2020-05-14 10:24 -0700]:

Quoted 33 lines · Andrey Ignatov
Quoted 21 lines

Yonghong Song <[email protected]> [Thu, 2020-05-14 08:16 -0700]:

Quoted 11 lines · Andrey Ignatov
Quoted 10 lines
include/uapi/linux/bpf.h +1 -0
index bfb31c1be219..e3cbc2790cdf 100644
@@ -3121,6 +3121,37 @@ union bpf_attr {
     * 		0 on success, or a negative error in case of failure:
     *
     *		**-EOVERFLOW** if an overflow happened: The same object will be tried again.
+ *
Quoted 8 lines

include/uapi/linux/bpf.h union bpf_attr { · line 3130

Quoted 6 lines
include/uapi/linux/bpf.h +6 -0
+ * u64 bpf_sk_cgroup_id(struct bpf_sock *sk)
+ *	Description
+ *		Return the cgroup v2 id of the socket *sk*.
+ *
+ *		*sk* must be a non-**NULL** pointer that was returned from
+ *		**bpf_sk_lookup_xxx**\ (). The format of returned id is same

It should also include bpf_skc_lookup_tcp(), right?

Quoted 8 lines

From what I see it should not.

cgroup id is available from sk->sk_cgrp_data that is a field of struct sock', i.e. struct sock_common' doesn't have this field.

bpf_skc_lookup_tcp() returns RET_PTR_TO_SOCK_COMMON_OR_NULL and it can be for example struct request_sock that has only struct sock_common member, i.e. it doesn't have cgroup id.

Quoted 7 lines

So you can do bpf_skc_lookup_tcp() and then do tcp_sock() to get a full socket which will have cgroup_id. I think maybe this is the reason you added bpf_skc_lookup_tcp() in patch #1, right?

If this is the case, maybe rewording a little bit for the description to include bpf_skc_lookup_tcp() + bpf_tcp_sock() as another input to bpf_sk_cgroup_id()?

Yeah, this bpf_skc_lookup_tcp() + bpf_tcp_sock() combination should also return a full socket that can be used with the helper.

bpf_sk_fullsock() is one more way to get it.

I'm not sure it's worth listing all possible ways to get full socket since it's 1) easy to miss something; 2) easy to forget to update this list if a new way to get full socket is being added.

What about rephrasing to highlight that it has to be full socket and **bpf_sk_lookup_xxx**\ () is an example of getting it?

Quoted 7 lines

For example:

*sk* must be a non-**NULL** pointer to full socket, e.g. one returned from **bpf_sk_lookup_xxx**\ () or **bpf_sk_fullsock**\ ().

Will it be better?

This should be fine. Thanks!