Skip to content

Commit 783da70

Browse files
Christoph Hellwigdavem330
authored andcommitted
net: add sock_enable_timestamps
Add a helper to directly enable timestamps instead of setting the SO_TIMESTAMP* sockopts from kernel space and going through a fake uaccess. Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 7594888 commit 783da70

3 files changed

Lines changed: 31 additions & 25 deletions

File tree

‎include/net/sock.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2689,6 +2689,7 @@ static inline bool sk_dev_equal_l3scope(struct sock *sk, int dif)
26892689
void sock_def_readable(struct sock *sk);
26902690

26912691
int sock_bindtoindex(struct sock *sk, int ifindex);
2692+
void sock_enable_timestamps(struct sock *sk);
26922693
void sock_no_linger(struct sock *sk);
26932694
void sock_set_priority(struct sock *sk, u32 priority);
26942695
void sock_set_reuseaddr(struct sock *sk);

‎net/core/sock.c‎

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,28 @@ void sock_set_sndtimeo(struct sock *sk, s64 secs)
757757
}
758758
EXPORT_SYMBOL(sock_set_sndtimeo);
759759

760+
static void __sock_set_timestamps(struct sock *sk, bool val, bool new, bool ns)
761+
{
762+
if (val) {
763+
sock_valbool_flag(sk, SOCK_TSTAMP_NEW, new);
764+
sock_valbool_flag(sk, SOCK_RCVTSTAMPNS, ns);
765+
sock_set_flag(sk, SOCK_RCVTSTAMP);
766+
sock_enable_timestamp(sk, SOCK_TIMESTAMP);
767+
} else {
768+
sock_reset_flag(sk, SOCK_RCVTSTAMP);
769+
sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
770+
sock_reset_flag(sk, SOCK_TSTAMP_NEW);
771+
}
772+
}
773+
774+
void sock_enable_timestamps(struct sock *sk)
775+
{
776+
lock_sock(sk);
777+
__sock_set_timestamps(sk, true, false, true);
778+
release_sock(sk);
779+
}
780+
EXPORT_SYMBOL(sock_enable_timestamps);
781+
760782
/*
761783
* This is meant for all protocols to use and covers goings on
762784
* at the socket level. Everything here is generic.
@@ -948,28 +970,17 @@ int sock_setsockopt(struct socket *sock, int level, int optname,
948970
break;
949971

950972
case SO_TIMESTAMP_OLD:
973+
__sock_set_timestamps(sk, valbool, false, false);
974+
break;
951975
case SO_TIMESTAMP_NEW:
976+
__sock_set_timestamps(sk, valbool, true, false);
977+
break;
952978
case SO_TIMESTAMPNS_OLD:
979+
__sock_set_timestamps(sk, valbool, false, true);
980+
break;
953981
case SO_TIMESTAMPNS_NEW:
954-
if (valbool) {
955-
if (optname == SO_TIMESTAMP_NEW || optname == SO_TIMESTAMPNS_NEW)
956-
sock_set_flag(sk, SOCK_TSTAMP_NEW);
957-
else
958-
sock_reset_flag(sk, SOCK_TSTAMP_NEW);
959-
960-
if (optname == SO_TIMESTAMP_OLD || optname == SO_TIMESTAMP_NEW)
961-
sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
962-
else
963-
sock_set_flag(sk, SOCK_RCVTSTAMPNS);
964-
sock_set_flag(sk, SOCK_RCVTSTAMP);
965-
sock_enable_timestamp(sk, SOCK_TIMESTAMP);
966-
} else {
967-
sock_reset_flag(sk, SOCK_RCVTSTAMP);
968-
sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
969-
sock_reset_flag(sk, SOCK_TSTAMP_NEW);
970-
}
982+
__sock_set_timestamps(sk, valbool, true, true);
971983
break;
972-
973984
case SO_TIMESTAMPING_NEW:
974985
sock_set_flag(sk, SOCK_TSTAMP_NEW);
975986
/* fall through */

‎net/rxrpc/local_object.c‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,7 @@ static int rxrpc_open_socket(struct rxrpc_local *local, struct net *net)
189189
}
190190

191191
/* We want receive timestamps. */
192-
opt = 1;
193-
ret = kernel_setsockopt(local->socket, SOL_SOCKET, SO_TIMESTAMPNS_OLD,
194-
(char *)&opt, sizeof(opt));
195-
if (ret < 0) {
196-
_debug("setsockopt failed");
197-
goto error;
198-
}
192+
sock_enable_timestamps(local->socket->sk);
199193
break;
200194

201195
default:

0 commit comments

Comments
 (0)