Skip to content

Commit 5d2e5f2

Browse files
xemuldavem330
authored andcommitted
sock_diag: Introduce the meminfo nla core (v2)
Add a routine that dumps memory-related values of a socket. It's made as an array to make it possible to add more stuff here later without breaking compatibility. Since v1: The SK_MEMINFO_ constants are in userspace visible part of sock_diag.h, the rest is under __KERNEL__. Signed-off-by: Pavel Emelyanov <xemul@parallels.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 288461e commit 5d2e5f2

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

include/linux/sock_diag.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,22 @@ struct sock_diag_req {
1010
__u8 sdiag_protocol;
1111
};
1212

13+
enum {
14+
SK_MEMINFO_RMEM_ALLOC,
15+
SK_MEMINFO_RCVBUF,
16+
SK_MEMINFO_WMEM_ALLOC,
17+
SK_MEMINFO_SNDBUF,
18+
SK_MEMINFO_FWD_ALLOC,
19+
SK_MEMINFO_WMEM_QUEUED,
20+
SK_MEMINFO_OPTMEM,
21+
22+
SK_MEMINFO_VARS,
23+
};
24+
1325
#ifdef __KERNEL__
1426
struct sk_buff;
1527
struct nlmsghdr;
28+
struct sock;
1629

1730
struct sock_diag_handler {
1831
__u8 family;
@@ -28,6 +41,8 @@ void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlms
2841
int sock_diag_check_cookie(void *sk, __u32 *cookie);
2942
void sock_diag_save_cookie(void *sk, __u32 *cookie);
3043

44+
int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attr);
45+
3146
extern struct sock *sock_diag_nlsk;
3247
#endif /* KERNEL */
3348
#endif

net/core/sock_diag.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <net/netlink.h>
55
#include <net/net_namespace.h>
66
#include <linux/module.h>
7+
#include <linux/rtnetlink.h>
8+
#include <net/sock.h>
79

810
#include <linux/inet_diag.h>
911
#include <linux/sock_diag.h>
@@ -31,6 +33,27 @@ void sock_diag_save_cookie(void *sk, __u32 *cookie)
3133
}
3234
EXPORT_SYMBOL_GPL(sock_diag_save_cookie);
3335

36+
int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype)
37+
{
38+
__u32 *mem;
39+
40+
mem = RTA_DATA(__RTA_PUT(skb, attrtype, SK_MEMINFO_VARS * sizeof(__u32)));
41+
42+
mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk);
43+
mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf;
44+
mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk);
45+
mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf;
46+
mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc;
47+
mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued;
48+
mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc);
49+
50+
return 0;
51+
52+
rtattr_failure:
53+
return -EMSGSIZE;
54+
}
55+
EXPORT_SYMBOL_GPL(sock_diag_put_meminfo);
56+
3457
void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
3558
{
3659
mutex_lock(&sock_diag_table_mutex);

0 commit comments

Comments
 (0)