Skip to content

Commit c6849a3

Browse files
laoardavem330
authored andcommitted
net: init sk_cookie for inet socket
With sk_cookie we can identify a socket, that is very helpful for traceing and statistic, i.e. tcp tracepiont and ebpf. So we'd better init it by default for inet socket. When using it, we just need call atomic64_read(&sk->sk_cookie). Signed-off-by: Yafang Shao <laoar.shao@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent ec8c360 commit c6849a3

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

include/linux/sock_diag.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ void sock_diag_unregister(const struct sock_diag_handler *h);
2525
void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
2626
void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
2727

28+
static inline
29+
void sock_init_cookie(struct sock *sk)
30+
{
31+
u64 res;
32+
33+
res = atomic64_inc_return(&sock_net(sk)->cookie_gen);
34+
atomic64_set(&sk->sk_cookie, res);
35+
}
36+
2837
u64 sock_gen_cookie(struct sock *sk);
2938
int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie);
3039
void sock_diag_save_cookie(struct sock *sk, __u32 *cookie);

net/ipv4/tcp_input.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
#include <linux/errqueue.h>
7979
#include <trace/events/tcp.h>
8080
#include <linux/static_key.h>
81+
#include <linux/sock_diag.h>
8182

8283
int sysctl_tcp_max_orphans __read_mostly = NR_FILE;
8384

@@ -6190,10 +6191,15 @@ struct request_sock *inet_reqsk_alloc(const struct request_sock_ops *ops,
61906191
#if IS_ENABLED(CONFIG_IPV6)
61916192
ireq->pktopts = NULL;
61926193
#endif
6193-
atomic64_set(&ireq->ir_cookie, 0);
61946194
ireq->ireq_state = TCP_NEW_SYN_RECV;
61956195
write_pnet(&ireq->ireq_net, sock_net(sk_listener));
61966196
ireq->ireq_family = sk_listener->sk_family;
6197+
6198+
BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
6199+
offsetof(struct sock, sk_cookie));
6200+
BUILD_BUG_ON(offsetof(struct inet_request_sock, ireq_net) !=
6201+
offsetof(struct sock, sk_net));
6202+
sock_init_cookie((struct sock *)ireq);
61976203
}
61986204

61996205
return req;

0 commit comments

Comments
 (0)