Skip to content

Commit 3dd17e6

Browse files
soheilhydavem330
authored andcommitted
sock: accept SO_TIMESTAMPING flags in socket cmsg
Accept SO_TIMESTAMPING in control messages of the SOL_SOCKET level as a basis to accept timestamping requests per write. This implementation only accepts TX recording flags (i.e., SOF_TIMESTAMPING_TX_HARDWARE, SOF_TIMESTAMPING_TX_SOFTWARE, SOF_TIMESTAMPING_TX_SCHED, and SOF_TIMESTAMPING_TX_ACK) in control messages. Users need to set reporting flags (e.g., SOF_TIMESTAMPING_OPT_ID) per socket via socket options. This commit adds a tsflags field in sockcm_cookie which is set in __sock_cmsg_send. It only override the SOF_TIMESTAMPING_TX_* bits in sockcm_cookie.tsflags allowing the control message to override the recording behavior per write, yet maintaining the value of other flags. This patch implements validating the control message and setting tsflags in struct sockcm_cookie. Next commits in this series will actually implement timestamping per write for different protocols. Signed-off-by: Soheil Hassas Yeganeh <[email protected]> Acked-by: Willem de Bruijn <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 6b08492 commit 3dd17e6

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

include/net/sock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,7 @@ void sk_send_sigurg(struct sock *sk);
14181418

14191419
struct sockcm_cookie {
14201420
u32 mark;
1421+
u16 tsflags;
14211422
};
14221423

14231424
int __sock_cmsg_send(struct sock *sk, struct msghdr *msg, struct cmsghdr *cmsg,

include/uapi/linux/net_tstamp.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ enum {
3131
SOF_TIMESTAMPING_LAST
3232
};
3333

34+
/*
35+
* SO_TIMESTAMPING flags are either for recording a packet timestamp or for
36+
* reporting the timestamp to user space.
37+
* Recording flags can be set both via socket options and control messages.
38+
*/
39+
#define SOF_TIMESTAMPING_TX_RECORD_MASK (SOF_TIMESTAMPING_TX_HARDWARE | \
40+
SOF_TIMESTAMPING_TX_SOFTWARE | \
41+
SOF_TIMESTAMPING_TX_SCHED | \
42+
SOF_TIMESTAMPING_TX_ACK)
43+
3444
/**
3545
* struct hwtstamp_config - %SIOCGHWTSTAMP and %SIOCSHWTSTAMP parameter
3646
*

net/core/sock.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,6 +1870,8 @@ EXPORT_SYMBOL(sock_alloc_send_skb);
18701870
int __sock_cmsg_send(struct sock *sk, struct msghdr *msg, struct cmsghdr *cmsg,
18711871
struct sockcm_cookie *sockc)
18721872
{
1873+
u32 tsflags;
1874+
18731875
switch (cmsg->cmsg_type) {
18741876
case SO_MARK:
18751877
if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
@@ -1878,6 +1880,17 @@ int __sock_cmsg_send(struct sock *sk, struct msghdr *msg, struct cmsghdr *cmsg,
18781880
return -EINVAL;
18791881
sockc->mark = *(u32 *)CMSG_DATA(cmsg);
18801882
break;
1883+
case SO_TIMESTAMPING:
1884+
if (cmsg->cmsg_len != CMSG_LEN(sizeof(u32)))
1885+
return -EINVAL;
1886+
1887+
tsflags = *(u32 *)CMSG_DATA(cmsg);
1888+
if (tsflags & ~SOF_TIMESTAMPING_TX_RECORD_MASK)
1889+
return -EINVAL;
1890+
1891+
sockc->tsflags &= ~SOF_TIMESTAMPING_TX_RECORD_MASK;
1892+
sockc->tsflags |= tsflags;
1893+
break;
18811894
default:
18821895
return -EINVAL;
18831896
}

0 commit comments

Comments
 (0)