bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net-next 0/2] inet_diag: add cgroup attribute and filter
@ 2020-04-30 15:51 Dmitry Yakunin
  2020-04-30 15:51 ` [PATCH net-next 1/2] inet_diag: add cgroup id attribute Dmitry Yakunin
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Dmitry Yakunin @ 2020-04-30 15:51 UTC (permalink / raw)
  To: davem, netdev; +Cc: khlebnikov, tj, cgroups, bpf

This patch series extends inet diag with cgroup v2 ID attribute and
filter. Which allows investigate sockets on per cgroup basis. Patch for
ss is already sent to iproute2-next mailing list.

Dmitry Yakunin (2):
  inet_diag: add cgroup id attribute
  inet_diag: add support for cgroup filter

 include/linux/inet_diag.h      |  6 +++++-
 include/uapi/linux/inet_diag.h |  2 ++
 net/ipv4/inet_diag.c           | 38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 45 insertions(+), 1 deletion(-)

-- 
2.7.4


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH net-next 1/2] inet_diag: add cgroup id attribute
  2020-04-30 15:51 [PATCH net-next 0/2] inet_diag: add cgroup attribute and filter Dmitry Yakunin
@ 2020-04-30 15:51 ` Dmitry Yakunin
  2020-04-30 15:51 ` [PATCH net-next 2/2] inet_diag: add support for cgroup filter Dmitry Yakunin
  2020-04-30 19:55 ` [PATCH net-next 0/2] inet_diag: add cgroup attribute and filter David Miller
  2 siblings, 0 replies; 5+ messages in thread
From: Dmitry Yakunin @ 2020-04-30 15:51 UTC (permalink / raw)
  To: davem, netdev; +Cc: khlebnikov, tj, cgroups, bpf

This patch adds cgroup v2 ID to common inet diag message attributes.
Cgroup v2 ID is kernfs ID (ino or ino+gen). This attribute allows filter
inet diag output by cgroup ID obtained by name_to_handle_at() syscall.
When net_cls or net_prio cgroup is activated this ID is equal to 1 (root
cgroup ID) for newly created sockets.

Some notes about this ID:

1) gets initialized in socket() syscall
2) incoming socket gets ID from listening socket
   (not during accept() syscall)
3) not changed when process get moved to another cgroup
4) can point to deleted cgroup (refcounting)

v2:
  - use CONFIG_SOCK_CGROUP_DATA instead if CONFIG_CGROUPS

v3:
  - fix attr size by using nla_total_size_64bit() (Eric Dumazet)
  - more detailed commit message (Konstantin Khlebnikov)

Signed-off-by: Dmitry Yakunin <zeil@yandex-team.ru>
Reviewed-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
Acked-By: Tejun Heo <tj@kernel.org>
---
 include/linux/inet_diag.h      | 6 +++++-
 include/uapi/linux/inet_diag.h | 1 +
 net/ipv4/inet_diag.c           | 7 +++++++
 3 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/include/linux/inet_diag.h b/include/linux/inet_diag.h
index ce9ed1c..0ef2d80 100644
--- a/include/linux/inet_diag.h
+++ b/include/linux/inet_diag.h
@@ -71,7 +71,11 @@ static inline size_t inet_diag_msg_attrs_size(void)
 		+ nla_total_size(1)  /* INET_DIAG_SKV6ONLY */
 #endif
 		+ nla_total_size(4)  /* INET_DIAG_MARK */
-		+ nla_total_size(4); /* INET_DIAG_CLASS_ID */
+		+ nla_total_size(4)  /* INET_DIAG_CLASS_ID */
+#ifdef CONFIG_SOCK_CGROUP_DATA
+		+ nla_total_size_64bit(sizeof(u64))  /* INET_DIAG_CGROUP_ID */
+#endif
+		;
 }
 int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
 			     struct inet_diag_msg *r, int ext,
diff --git a/include/uapi/linux/inet_diag.h b/include/uapi/linux/inet_diag.h
index 57cc429..c9b1e55 100644
--- a/include/uapi/linux/inet_diag.h
+++ b/include/uapi/linux/inet_diag.h
@@ -157,6 +157,7 @@ enum {
 	INET_DIAG_MD5SIG,
 	INET_DIAG_ULP_INFO,
 	INET_DIAG_SK_BPF_STORAGES,
+	INET_DIAG_CGROUP_ID,
 	__INET_DIAG_MAX,
 };
 
diff --git a/net/ipv4/inet_diag.c b/net/ipv4/inet_diag.c
index 5d50aad..9c4c315 100644
--- a/net/ipv4/inet_diag.c
+++ b/net/ipv4/inet_diag.c
@@ -162,6 +162,13 @@ int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
 			goto errout;
 	}
 
+#ifdef CONFIG_SOCK_CGROUP_DATA
+	if (nla_put_u64_64bit(skb, INET_DIAG_CGROUP_ID,
+			      cgroup_id(sock_cgroup_ptr(&sk->sk_cgrp_data)),
+			      INET_DIAG_PAD))
+		goto errout;
+#endif
+
 	r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
 	r->idiag_inode = sock_i_ino(sk);
 
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH net-next 2/2] inet_diag: add support for cgroup filter
  2020-04-30 15:51 [PATCH net-next 0/2] inet_diag: add cgroup attribute and filter Dmitry Yakunin
  2020-04-30 15:51 ` [PATCH net-next 1/2] inet_diag: add cgroup id attribute Dmitry Yakunin
@ 2020-04-30 15:51 ` Dmitry Yakunin
  2020-04-30 19:55 ` [PATCH net-next 0/2] inet_diag: add cgroup attribute and filter David Miller
  2 siblings, 0 replies; 5+ messages in thread
From: Dmitry Yakunin @ 2020-04-30 15:51 UTC (permalink / raw)
  To: davem, netdev; +Cc: khlebnikov, tj, cgroups, bpf

This patch adds ability to filter sockets based on cgroup v2 ID.
Such filter is helpful in ss utility for filtering sockets by
cgroup pathname.

Signed-off-by: Dmitry Yakunin <zeil@yandex-team.ru>
Reviewed-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
---
 include/uapi/linux/inet_diag.h |  1 +
 net/ipv4/inet_diag.c           | 31 +++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/include/uapi/linux/inet_diag.h b/include/uapi/linux/inet_diag.h
index c9b1e55..e6f183e 100644
--- a/include/uapi/linux/inet_diag.h
+++ b/include/uapi/linux/inet_diag.h
@@ -96,6 +96,7 @@ enum {
 	INET_DIAG_BC_MARK_COND,
 	INET_DIAG_BC_S_EQ,
 	INET_DIAG_BC_D_EQ,
+	INET_DIAG_BC_CGROUP_COND,   /* u64 cgroup v2 ID */
 };
 
 struct inet_diag_hostcond {
diff --git a/net/ipv4/inet_diag.c b/net/ipv4/inet_diag.c
index 9c4c315..0034092 100644
--- a/net/ipv4/inet_diag.c
+++ b/net/ipv4/inet_diag.c
@@ -43,6 +43,9 @@ struct inet_diag_entry {
 	u16 userlocks;
 	u32 ifindex;
 	u32 mark;
+#ifdef CONFIG_SOCK_CGROUP_DATA
+	u64 cgroup_id;
+#endif
 };
 
 static DEFINE_MUTEX(inet_diag_table_mutex);
@@ -682,6 +685,16 @@ static int inet_diag_bc_run(const struct nlattr *_bc,
 				yes = 0;
 			break;
 		}
+#ifdef CONFIG_SOCK_CGROUP_DATA
+		case INET_DIAG_BC_CGROUP_COND: {
+			u64 cgroup_id;
+
+			cgroup_id = get_unaligned((const u64 *)(op + 1));
+			if (cgroup_id != entry->cgroup_id)
+				yes = 0;
+			break;
+		}
+#endif
 		}
 
 		if (yes) {
@@ -732,6 +745,9 @@ int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
 		entry.mark = inet_rsk(inet_reqsk(sk))->ir_mark;
 	else
 		entry.mark = 0;
+#ifdef CONFIG_SOCK_CGROUP_DATA
+	entry.cgroup_id = cgroup_id(sock_cgroup_ptr(&sk->sk_cgrp_data));
+#endif
 
 	return inet_diag_bc_run(bc, &entry);
 }
@@ -821,6 +837,15 @@ static bool valid_markcond(const struct inet_diag_bc_op *op, int len,
 	return len >= *min_len;
 }
 
+#ifdef CONFIG_SOCK_CGROUP_DATA
+static bool valid_cgroupcond(const struct inet_diag_bc_op *op, int len,
+			     int *min_len)
+{
+	*min_len += sizeof(u64);
+	return len >= *min_len;
+}
+#endif
+
 static int inet_diag_bc_audit(const struct nlattr *attr,
 			      const struct sk_buff *skb)
 {
@@ -863,6 +888,12 @@ static int inet_diag_bc_audit(const struct nlattr *attr,
 			if (!valid_markcond(bc, len, &min_len))
 				return -EINVAL;
 			break;
+#ifdef CONFIG_SOCK_CGROUP_DATA
+		case INET_DIAG_BC_CGROUP_COND:
+			if (!valid_cgroupcond(bc, len, &min_len))
+				return -EINVAL;
+			break;
+#endif
 		case INET_DIAG_BC_AUTO:
 		case INET_DIAG_BC_JMP:
 		case INET_DIAG_BC_NOP:
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH net-next 0/2] inet_diag: add cgroup attribute and filter
  2020-04-30 15:51 [PATCH net-next 0/2] inet_diag: add cgroup attribute and filter Dmitry Yakunin
  2020-04-30 15:51 ` [PATCH net-next 1/2] inet_diag: add cgroup id attribute Dmitry Yakunin
  2020-04-30 15:51 ` [PATCH net-next 2/2] inet_diag: add support for cgroup filter Dmitry Yakunin
@ 2020-04-30 19:55 ` David Miller
  2020-05-01  5:54   ` Konstantin Khlebnikov
  2 siblings, 1 reply; 5+ messages in thread
From: David Miller @ 2020-04-30 19:55 UTC (permalink / raw)
  To: zeil; +Cc: netdev, khlebnikov, tj, cgroups, bpf

From: Dmitry Yakunin <zeil@yandex-team.ru>
Date: Thu, 30 Apr 2020 18:51:13 +0300

> This patch series extends inet diag with cgroup v2 ID attribute and
> filter. Which allows investigate sockets on per cgroup basis. Patch for
> ss is already sent to iproute2-next mailing list.

Ok, this looks fine, series applied.

Although I wish you could have done something like only emit the cgroup
attribute if it is a non-default value (zero, or whatever it is).

Every time a new socket attribute is added, it makes long dumps more
and more expensive.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net-next 0/2] inet_diag: add cgroup attribute and filter
  2020-04-30 19:55 ` [PATCH net-next 0/2] inet_diag: add cgroup attribute and filter David Miller
@ 2020-05-01  5:54   ` Konstantin Khlebnikov
  0 siblings, 0 replies; 5+ messages in thread
From: Konstantin Khlebnikov @ 2020-05-01  5:54 UTC (permalink / raw)
  To: David Miller, zeil; +Cc: netdev, tj, cgroups, bpf

On 30/04/2020 22.55, David Miller wrote:
> From: Dmitry Yakunin <zeil@yandex-team.ru>
> Date: Thu, 30 Apr 2020 18:51:13 +0300
> 
>> This patch series extends inet diag with cgroup v2 ID attribute and
>> filter. Which allows investigate sockets on per cgroup basis. Patch for
>> ss is already sent to iproute2-next mailing list.
> 
> Ok, this looks fine, series applied.
> 
> Although I wish you could have done something like only emit the cgroup
> attribute if it is a non-default value (zero, or whatever it is).
> 
> Every time a new socket attribute is added, it makes long dumps more
> and more expensive.
> 

Maybe then put it under condition

	if (ext & (1 << (INET_DIAG_CLASS_ID - 1)) ||
	    ext & (1 << (INET_DIAG_TCLASS - 1))) {

like legacy cgroup id INET_DIAG_CLASS_ID above.

(userspace requests it by INET_DIAG_TCLASS because INET_DIAG_CLASS_ID does not fit into field)

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-05-01  5:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-30 15:51 [PATCH net-next 0/2] inet_diag: add cgroup attribute and filter Dmitry Yakunin
2020-04-30 15:51 ` [PATCH net-next 1/2] inet_diag: add cgroup id attribute Dmitry Yakunin
2020-04-30 15:51 ` [PATCH net-next 2/2] inet_diag: add support for cgroup filter Dmitry Yakunin
2020-04-30 19:55 ` [PATCH net-next 0/2] inet_diag: add cgroup attribute and filter David Miller
2020-05-01  5:54   ` Konstantin Khlebnikov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).