bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 net] inet_diag: add cgroup id attribute
@ 2020-04-03  9:56 Dmitry Yakunin
  2020-04-03 13:38 ` Tejun Heo
  2020-04-03 23:08 ` David Miller
  0 siblings, 2 replies; 7+ messages in thread
From: Dmitry Yakunin @ 2020-04-03  9:56 UTC (permalink / raw)
  To: davem, netdev; +Cc: khlebnikov, 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>
---
 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 c91cf2d..0696f86 100644
--- a/include/linux/inet_diag.h
+++ b/include/linux/inet_diag.h
@@ -66,7 +66,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 a1ff345..dc87ad6 100644
--- a/include/uapi/linux/inet_diag.h
+++ b/include/uapi/linux/inet_diag.h
@@ -154,6 +154,7 @@ enum {
 	INET_DIAG_CLASS_ID,	/* request as INET_DIAG_TCLASS */
 	INET_DIAG_MD5SIG,
 	INET_DIAG_ULP_INFO,
+	INET_DIAG_CGROUP_ID,
 	__INET_DIAG_MAX,
 };
 
diff --git a/net/ipv4/inet_diag.c b/net/ipv4/inet_diag.c
index 8c83775..17e3c52 100644
--- a/net/ipv4/inet_diag.c
+++ b/net/ipv4/inet_diag.c
@@ -161,6 +161,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] 7+ messages in thread

* Re: [PATCH v3 net] inet_diag: add cgroup id attribute
  2020-04-03  9:56 [PATCH v3 net] inet_diag: add cgroup id attribute Dmitry Yakunin
@ 2020-04-03 13:38 ` Tejun Heo
  2020-04-03 13:41   ` Tejun Heo
  2020-04-03 14:37   ` Konstantin Khlebnikov
  2020-04-03 23:08 ` David Miller
  1 sibling, 2 replies; 7+ messages in thread
From: Tejun Heo @ 2020-04-03 13:38 UTC (permalink / raw)
  To: Dmitry Yakunin; +Cc: davem, netdev, khlebnikov, cgroups, bpf

On Fri, Apr 03, 2020 at 12:56:27PM +0300, Dmitry Yakunin wrote:
> 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)

How would this work with things like inetd? Would it make sense to associate the
socket on the first actual send/recv?

Thanks.

-- 
tejun

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

* Re: [PATCH v3 net] inet_diag: add cgroup id attribute
  2020-04-03 13:38 ` Tejun Heo
@ 2020-04-03 13:41   ` Tejun Heo
  2020-04-03 14:37   ` Konstantin Khlebnikov
  1 sibling, 0 replies; 7+ messages in thread
From: Tejun Heo @ 2020-04-03 13:41 UTC (permalink / raw)
  To: Dmitry Yakunin; +Cc: davem, netdev, khlebnikov, cgroups, bpf

On Fri, Apr 03, 2020 at 09:38:17AM -0400, Tejun Heo wrote:
> On Fri, Apr 03, 2020 at 12:56:27PM +0300, Dmitry Yakunin wrote:
> > 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)
> 
> How would this work with things like inetd? Would it make sense to associate the
> socket on the first actual send/recv?

Oh, it's not a problem with your patch as you're just following the associated
ptr, so we can have that discussion separately. Looks good to me from cgroup
side. Please feel free to add my acked-by.

Thanks.

-- 
tejun

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

* Re: [PATCH v3 net] inet_diag: add cgroup id attribute
  2020-04-03 13:38 ` Tejun Heo
  2020-04-03 13:41   ` Tejun Heo
@ 2020-04-03 14:37   ` Konstantin Khlebnikov
  2020-04-03 14:45     ` Tejun Heo
  1 sibling, 1 reply; 7+ messages in thread
From: Konstantin Khlebnikov @ 2020-04-03 14:37 UTC (permalink / raw)
  To: Tejun Heo, Dmitry Yakunin; +Cc: davem, netdev, cgroups, bpf

On 03/04/2020 16.38, Tejun Heo wrote:
> On Fri, Apr 03, 2020 at 12:56:27PM +0300, Dmitry Yakunin wrote:
>> 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)
> 
> How would this work with things like inetd? Would it make sense to associate the
> socket on the first actual send/recv?

First send/recv seems too intrusive.
Setsockopt to change association to current cgroup (or by id) seems more reasonable.

Systemd variant of inetd handles sockets as separate units and probably
creates own cgroups for them.

> 
> Thanks.
> 

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

* Re: [PATCH v3 net] inet_diag: add cgroup id attribute
  2020-04-03 14:37   ` Konstantin Khlebnikov
@ 2020-04-03 14:45     ` Tejun Heo
  2020-04-03 14:56       ` Konstantin Khlebnikov
  0 siblings, 1 reply; 7+ messages in thread
From: Tejun Heo @ 2020-04-03 14:45 UTC (permalink / raw)
  To: Konstantin Khlebnikov; +Cc: Dmitry Yakunin, davem, netdev, cgroups, bpf

On Fri, Apr 03, 2020 at 05:37:17PM +0300, Konstantin Khlebnikov wrote:
> > How would this work with things like inetd? Would it make sense to associate the
> > socket on the first actual send/recv?
> 
> First send/recv seems too intrusive.

Intrusive in terms of?

> Setsockopt to change association to current cgroup (or by id) seems more reasonable.

I'm not sure about exposing it as an explicit interface.

Thanks.

-- 
tejun

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

* Re: [PATCH v3 net] inet_diag: add cgroup id attribute
  2020-04-03 14:45     ` Tejun Heo
@ 2020-04-03 14:56       ` Konstantin Khlebnikov
  0 siblings, 0 replies; 7+ messages in thread
From: Konstantin Khlebnikov @ 2020-04-03 14:56 UTC (permalink / raw)
  To: Tejun Heo; +Cc: Dmitry Yakunin, davem, netdev, cgroups, bpf

On 03/04/2020 17.45, Tejun Heo wrote:
> On Fri, Apr 03, 2020 at 05:37:17PM +0300, Konstantin Khlebnikov wrote:
>>> How would this work with things like inetd? Would it make sense to associate the
>>> socket on the first actual send/recv?
>>
>> First send/recv seems too intrusive.
> 
> Intrusive in terms of?

In term of adding more code to networking fast paths.

> 
>> Setsockopt to change association to current cgroup (or by id) seems more reasonable.
> 
> I'm not sure about exposing it as an explicit interface.

Yep, it's better to create thing in right place from the beginning.
Current behaviour isn't bad, just not obvious (and barely documented).
That's why I've asked Dmitry to add these notes.

> 
> Thanks.
> 

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

* Re: [PATCH v3 net] inet_diag: add cgroup id attribute
  2020-04-03  9:56 [PATCH v3 net] inet_diag: add cgroup id attribute Dmitry Yakunin
  2020-04-03 13:38 ` Tejun Heo
@ 2020-04-03 23:08 ` David Miller
  1 sibling, 0 replies; 7+ messages in thread
From: David Miller @ 2020-04-03 23:08 UTC (permalink / raw)
  To: zeil; +Cc: netdev, khlebnikov, cgroups, bpf

From: Dmitry Yakunin <zeil@yandex-team.ru>
Date: Fri, 3 Apr 2020 12:56:27 +0300

> 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>

As a new feature, this should be resubmitted when net-next opens back
up.  Thank you.

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

end of thread, other threads:[~2020-04-03 23:08 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-03  9:56 [PATCH v3 net] inet_diag: add cgroup id attribute Dmitry Yakunin
2020-04-03 13:38 ` Tejun Heo
2020-04-03 13:41   ` Tejun Heo
2020-04-03 14:37   ` Konstantin Khlebnikov
2020-04-03 14:45     ` Tejun Heo
2020-04-03 14:56       ` Konstantin Khlebnikov
2020-04-03 23:08 ` David Miller

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).