Skip to content

Commit c14ac94

Browse files
soheilhydavem330
authored andcommitted
sock: enable timestamping using control messages
Currently, SOL_TIMESTAMPING can only be enabled using setsockopt. This is very costly when users want to sample writes to gather tx timestamps. Add support for enabling SO_TIMESTAMPING via control messages by using tsflags added in `struct sockcm_cookie` (added in the previous patches in this series) to set the tx_flags of the last skb created in a sendmsg. With this patch, the timestamp recording bits in tx_flags of the skbuff is overridden if SO_TIMESTAMPING is passed in a cmsg. Please note that this is only effective for overriding the recording timestamps flags. Users should enable timestamp reporting (e.g., SOF_TIMESTAMPING_SOFTWARE | SOF_TIMESTAMPING_OPT_ID) using socket options and then should ask for SOF_TIMESTAMPING_TX_* using control messages per sendmsg to sample timestamps for each write. 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 ad1e46a commit c14ac94

File tree

16 files changed

+93
-49
lines changed

16 files changed

+93
-49
lines changed

drivers/net/tun.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,8 @@ static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
861861
goto drop;
862862

863863
if (skb->sk && sk_fullsock(skb->sk)) {
864-
sock_tx_timestamp(skb->sk, &skb_shinfo(skb)->tx_flags);
864+
sock_tx_timestamp(skb->sk, skb->sk->sk_tsflags,
865+
&skb_shinfo(skb)->tx_flags);
865866
sw_tx_timestamp(skb);
866867
}
867868

include/net/ipv6.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,8 @@ int ip6_append_data(struct sock *sk,
867867
int odd, struct sk_buff *skb),
868868
void *from, int length, int transhdrlen, int hlimit,
869869
int tclass, struct ipv6_txoptions *opt, struct flowi6 *fl6,
870-
struct rt6_info *rt, unsigned int flags, int dontfrag);
870+
struct rt6_info *rt, unsigned int flags, int dontfrag,
871+
const struct sockcm_cookie *sockc);
871872

872873
int ip6_push_pending_frames(struct sock *sk);
873874

@@ -884,7 +885,8 @@ struct sk_buff *ip6_make_skb(struct sock *sk,
884885
void *from, int length, int transhdrlen,
885886
int hlimit, int tclass, struct ipv6_txoptions *opt,
886887
struct flowi6 *fl6, struct rt6_info *rt,
887-
unsigned int flags, int dontfrag);
888+
unsigned int flags, int dontfrag,
889+
const struct sockcm_cookie *sockc);
888890

889891
static inline struct sk_buff *ip6_finish_skb(struct sock *sk)
890892
{

include/net/sock.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,19 +2057,21 @@ static inline void sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
20572057
sk->sk_stamp = skb->tstamp;
20582058
}
20592059

2060-
void __sock_tx_timestamp(const struct sock *sk, __u8 *tx_flags);
2060+
void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags);
20612061

20622062
/**
20632063
* sock_tx_timestamp - checks whether the outgoing packet is to be time stamped
20642064
* @sk: socket sending this packet
2065+
* @tsflags: timestamping flags to use
20652066
* @tx_flags: completed with instructions for time stamping
20662067
*
20672068
* Note : callers should take care of initial *tx_flags value (usually 0)
20682069
*/
2069-
static inline void sock_tx_timestamp(const struct sock *sk, __u8 *tx_flags)
2070+
static inline void sock_tx_timestamp(const struct sock *sk, __u16 tsflags,
2071+
__u8 *tx_flags)
20702072
{
2071-
if (unlikely(sk->sk_tsflags))
2072-
__sock_tx_timestamp(sk, tx_flags);
2073+
if (unlikely(tsflags))
2074+
__sock_tx_timestamp(tsflags, tx_flags);
20732075
if (unlikely(sock_flag(sk, SOCK_WIFI_STATUS)))
20742076
*tx_flags |= SKBTX_WIFI_STATUS;
20752077
}

net/can/raw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
755755
if (err < 0)
756756
goto free_skb;
757757

758-
sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
758+
sock_tx_timestamp(sk, sk->sk_tsflags, &skb_shinfo(skb)->tx_flags);
759759

760760
skb->dev = dev;
761761
skb->sk = sk;

net/ipv4/ping.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,15 +737,14 @@ static int ping_v4_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
737737
/* no remote port */
738738
}
739739

740+
ipc.sockc.tsflags = sk->sk_tsflags;
740741
ipc.addr = inet->inet_saddr;
741742
ipc.opt = NULL;
742743
ipc.oif = sk->sk_bound_dev_if;
743744
ipc.tx_flags = 0;
744745
ipc.ttl = 0;
745746
ipc.tos = -1;
746747

747-
sock_tx_timestamp(sk, &ipc.tx_flags);
748-
749748
if (msg->msg_controllen) {
750749
err = ip_cmsg_send(sk, msg, &ipc, false);
751750
if (unlikely(err)) {
@@ -768,6 +767,8 @@ static int ping_v4_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
768767
rcu_read_unlock();
769768
}
770769

770+
sock_tx_timestamp(sk, ipc.sockc.tsflags, &ipc.tx_flags);
771+
771772
saddr = ipc.addr;
772773
ipc.addr = faddr = daddr;
773774

net/ipv4/raw.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ int raw_rcv(struct sock *sk, struct sk_buff *skb)
339339

340340
static int raw_send_hdrinc(struct sock *sk, struct flowi4 *fl4,
341341
struct msghdr *msg, size_t length,
342-
struct rtable **rtp,
343-
unsigned int flags)
342+
struct rtable **rtp, unsigned int flags,
343+
const struct sockcm_cookie *sockc)
344344
{
345345
struct inet_sock *inet = inet_sk(sk);
346346
struct net *net = sock_net(sk);
@@ -379,7 +379,7 @@ static int raw_send_hdrinc(struct sock *sk, struct flowi4 *fl4,
379379

380380
skb->ip_summed = CHECKSUM_NONE;
381381

382-
sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
382+
sock_tx_timestamp(sk, sockc->tsflags, &skb_shinfo(skb)->tx_flags);
383383

384384
skb->transport_header = skb->network_header;
385385
err = -EFAULT;
@@ -540,6 +540,7 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
540540
daddr = inet->inet_daddr;
541541
}
542542

543+
ipc.sockc.tsflags = sk->sk_tsflags;
543544
ipc.addr = inet->inet_saddr;
544545
ipc.opt = NULL;
545546
ipc.tx_flags = 0;
@@ -638,10 +639,10 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
638639

639640
if (inet->hdrincl)
640641
err = raw_send_hdrinc(sk, &fl4, msg, len,
641-
&rt, msg->msg_flags);
642+
&rt, msg->msg_flags, &ipc.sockc);
642643

643644
else {
644-
sock_tx_timestamp(sk, &ipc.tx_flags);
645+
sock_tx_timestamp(sk, ipc.sockc.tsflags, &ipc.tx_flags);
645646

646647
if (!ipc.addr)
647648
ipc.addr = fl4.daddr;

net/ipv4/tcp.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -428,13 +428,13 @@ void tcp_init_sock(struct sock *sk)
428428
}
429429
EXPORT_SYMBOL(tcp_init_sock);
430430

431-
static void tcp_tx_timestamp(struct sock *sk, struct sk_buff *skb)
431+
static void tcp_tx_timestamp(struct sock *sk, u16 tsflags, struct sk_buff *skb)
432432
{
433-
if (sk->sk_tsflags) {
433+
if (sk->sk_tsflags || tsflags) {
434434
struct skb_shared_info *shinfo = skb_shinfo(skb);
435435
struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
436436

437-
sock_tx_timestamp(sk, &shinfo->tx_flags);
437+
sock_tx_timestamp(sk, tsflags, &shinfo->tx_flags);
438438
if (shinfo->tx_flags & SKBTX_ANY_TSTAMP)
439439
shinfo->tskey = TCP_SKB_CB(skb)->seq + skb->len - 1;
440440
tcb->txstamp_ack = !!(shinfo->tx_flags & SKBTX_ACK_TSTAMP);
@@ -959,7 +959,7 @@ static ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset,
959959
offset += copy;
960960
size -= copy;
961961
if (!size) {
962-
tcp_tx_timestamp(sk, skb);
962+
tcp_tx_timestamp(sk, sk->sk_tsflags, skb);
963963
goto out;
964964
}
965965

@@ -1079,6 +1079,7 @@ int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
10791079
{
10801080
struct tcp_sock *tp = tcp_sk(sk);
10811081
struct sk_buff *skb;
1082+
struct sockcm_cookie sockc;
10821083
int flags, err, copied = 0;
10831084
int mss_now = 0, size_goal, copied_syn = 0;
10841085
bool sg;
@@ -1121,6 +1122,15 @@ int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
11211122
/* 'common' sending to sendq */
11221123
}
11231124

1125+
sockc.tsflags = sk->sk_tsflags;
1126+
if (msg->msg_controllen) {
1127+
err = sock_cmsg_send(sk, msg, &sockc);
1128+
if (unlikely(err)) {
1129+
err = -EINVAL;
1130+
goto out_err;
1131+
}
1132+
}
1133+
11241134
/* This should be in poll */
11251135
sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
11261136

@@ -1239,7 +1249,7 @@ int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
12391249

12401250
copied += copy;
12411251
if (!msg_data_left(msg)) {
1242-
tcp_tx_timestamp(sk, skb);
1252+
tcp_tx_timestamp(sk, sockc.tsflags, skb);
12431253
goto out;
12441254
}
12451255

net/ipv4/udp.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,12 +1027,11 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
10271027
*/
10281028
connected = 1;
10291029
}
1030-
ipc.addr = inet->inet_saddr;
10311030

1031+
ipc.sockc.tsflags = sk->sk_tsflags;
1032+
ipc.addr = inet->inet_saddr;
10321033
ipc.oif = sk->sk_bound_dev_if;
10331034

1034-
sock_tx_timestamp(sk, &ipc.tx_flags);
1035-
10361035
if (msg->msg_controllen) {
10371036
err = ip_cmsg_send(sk, msg, &ipc, sk->sk_family == AF_INET6);
10381037
if (unlikely(err)) {
@@ -1059,6 +1058,8 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
10591058
saddr = ipc.addr;
10601059
ipc.addr = faddr = daddr;
10611060

1061+
sock_tx_timestamp(sk, ipc.sockc.tsflags, &ipc.tx_flags);
1062+
10621063
if (ipc.opt && ipc.opt->opt.srr) {
10631064
if (!daddr)
10641065
return -EINVAL;

net/ipv6/icmp.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ static void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info)
400400
struct icmp6hdr tmp_hdr;
401401
struct flowi6 fl6;
402402
struct icmpv6_msg msg;
403+
struct sockcm_cookie sockc_unused = {0};
403404
int iif = 0;
404405
int addr_type = 0;
405406
int len;
@@ -527,7 +528,7 @@ static void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info)
527528
len + sizeof(struct icmp6hdr),
528529
sizeof(struct icmp6hdr), hlimit,
529530
np->tclass, NULL, &fl6, (struct rt6_info *)dst,
530-
MSG_DONTWAIT, np->dontfrag);
531+
MSG_DONTWAIT, np->dontfrag, &sockc_unused);
531532
if (err) {
532533
ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS);
533534
ip6_flush_pending_frames(sk);
@@ -566,6 +567,7 @@ static void icmpv6_echo_reply(struct sk_buff *skb)
566567
int hlimit;
567568
u8 tclass;
568569
u32 mark = IP6_REPLY_MARK(net, skb->mark);
570+
struct sockcm_cookie sockc_unused = {0};
569571

570572
saddr = &ipv6_hdr(skb)->daddr;
571573

@@ -617,7 +619,7 @@ static void icmpv6_echo_reply(struct sk_buff *skb)
617619
err = ip6_append_data(sk, icmpv6_getfrag, &msg, skb->len + sizeof(struct icmp6hdr),
618620
sizeof(struct icmp6hdr), hlimit, tclass, NULL, &fl6,
619621
(struct rt6_info *)dst, MSG_DONTWAIT,
620-
np->dontfrag);
622+
np->dontfrag, &sockc_unused);
621623

622624
if (err) {
623625
ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTERRORS);

net/ipv6/ip6_output.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,8 @@ static int __ip6_append_data(struct sock *sk,
12581258
int getfrag(void *from, char *to, int offset,
12591259
int len, int odd, struct sk_buff *skb),
12601260
void *from, int length, int transhdrlen,
1261-
unsigned int flags, int dontfrag)
1261+
unsigned int flags, int dontfrag,
1262+
const struct sockcm_cookie *sockc)
12621263
{
12631264
struct sk_buff *skb, *skb_prev = NULL;
12641265
unsigned int maxfraglen, fragheaderlen, mtu, orig_mtu;
@@ -1329,7 +1330,7 @@ static int __ip6_append_data(struct sock *sk,
13291330
csummode = CHECKSUM_PARTIAL;
13301331

13311332
if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_RAW) {
1332-
sock_tx_timestamp(sk, &tx_flags);
1333+
sock_tx_timestamp(sk, sockc->tsflags, &tx_flags);
13331334
if (tx_flags & SKBTX_ANY_SW_TSTAMP &&
13341335
sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)
13351336
tskey = sk->sk_tskey++;
@@ -1565,7 +1566,8 @@ int ip6_append_data(struct sock *sk,
15651566
int odd, struct sk_buff *skb),
15661567
void *from, int length, int transhdrlen, int hlimit,
15671568
int tclass, struct ipv6_txoptions *opt, struct flowi6 *fl6,
1568-
struct rt6_info *rt, unsigned int flags, int dontfrag)
1569+
struct rt6_info *rt, unsigned int flags, int dontfrag,
1570+
const struct sockcm_cookie *sockc)
15691571
{
15701572
struct inet_sock *inet = inet_sk(sk);
15711573
struct ipv6_pinfo *np = inet6_sk(sk);
@@ -1593,7 +1595,8 @@ int ip6_append_data(struct sock *sk,
15931595

15941596
return __ip6_append_data(sk, fl6, &sk->sk_write_queue, &inet->cork.base,
15951597
&np->cork, sk_page_frag(sk), getfrag,
1596-
from, length, transhdrlen, flags, dontfrag);
1598+
from, length, transhdrlen, flags, dontfrag,
1599+
sockc);
15971600
}
15981601
EXPORT_SYMBOL_GPL(ip6_append_data);
15991602

@@ -1752,7 +1755,7 @@ struct sk_buff *ip6_make_skb(struct sock *sk,
17521755
int hlimit, int tclass,
17531756
struct ipv6_txoptions *opt, struct flowi6 *fl6,
17541757
struct rt6_info *rt, unsigned int flags,
1755-
int dontfrag)
1758+
int dontfrag, const struct sockcm_cookie *sockc)
17561759
{
17571760
struct inet_cork_full cork;
17581761
struct inet6_cork v6_cork;
@@ -1779,7 +1782,7 @@ struct sk_buff *ip6_make_skb(struct sock *sk,
17791782
err = __ip6_append_data(sk, fl6, &queue, &cork.base, &v6_cork,
17801783
&current->task_frag, getfrag, from,
17811784
length + exthdrlen, transhdrlen + exthdrlen,
1782-
flags, dontfrag);
1785+
flags, dontfrag, sockc);
17831786
if (err) {
17841787
__ip6_flush_pending_frames(sk, &queue, &cork, &v6_cork);
17851788
return ERR_PTR(err);

net/ipv6/ping.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
6262
struct dst_entry *dst;
6363
struct rt6_info *rt;
6464
struct pingfakehdr pfh;
65+
struct sockcm_cookie junk = {0};
6566

6667
pr_debug("ping_v6_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
6768

@@ -144,7 +145,7 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
144145
err = ip6_append_data(sk, ping_getfrag, &pfh, len,
145146
0, hlimit,
146147
np->tclass, NULL, &fl6, rt,
147-
MSG_DONTWAIT, np->dontfrag);
148+
MSG_DONTWAIT, np->dontfrag, &junk);
148149

149150
if (err) {
150151
ICMP6_INC_STATS(sock_net(sk), rt->rt6i_idev,

net/ipv6/raw.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -822,8 +822,7 @@ static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
822822
if (fl6.flowi6_oif == 0)
823823
fl6.flowi6_oif = sk->sk_bound_dev_if;
824824

825-
sockc.tsflags = 0;
826-
825+
sockc.tsflags = sk->sk_tsflags;
827826
if (msg->msg_controllen) {
828827
opt = &opt_space;
829828
memset(opt, 0, sizeof(struct ipv6_txoptions));
@@ -901,7 +900,7 @@ static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
901900
lock_sock(sk);
902901
err = ip6_append_data(sk, raw6_getfrag, &rfv,
903902
len, 0, hlimit, tclass, opt, &fl6, (struct rt6_info *)dst,
904-
msg->msg_flags, dontfrag);
903+
msg->msg_flags, dontfrag, &sockc);
905904

906905
if (err)
907906
ip6_flush_pending_frames(sk);

net/ipv6/udp.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@ int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
12481248
fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
12491249

12501250
fl6.flowi6_mark = sk->sk_mark;
1251-
sockc.tsflags = 0;
1251+
sockc.tsflags = sk->sk_tsflags;
12521252

12531253
if (msg->msg_controllen) {
12541254
opt = &opt_space;
@@ -1324,7 +1324,7 @@ int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
13241324
skb = ip6_make_skb(sk, getfrag, msg, ulen,
13251325
sizeof(struct udphdr), hlimit, tclass, opt,
13261326
&fl6, (struct rt6_info *)dst,
1327-
msg->msg_flags, dontfrag);
1327+
msg->msg_flags, dontfrag, &sockc);
13281328
err = PTR_ERR(skb);
13291329
if (!IS_ERR_OR_NULL(skb))
13301330
err = udp_v6_send_skb(skb, &fl6);
@@ -1351,7 +1351,8 @@ int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
13511351
err = ip6_append_data(sk, getfrag, msg, ulen,
13521352
sizeof(struct udphdr), hlimit, tclass, opt, &fl6,
13531353
(struct rt6_info *)dst,
1354-
corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, dontfrag);
1354+
corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, dontfrag,
1355+
&sockc);
13551356
if (err)
13561357
udp_v6_flush_pending_frames(sk);
13571358
else if (!corkreq)

net/l2tp/l2tp_ip6.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ static int l2tp_ip6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
627627
err = ip6_append_data(sk, ip_generic_getfrag, msg,
628628
ulen, transhdrlen, hlimit, tclass, opt,
629629
&fl6, (struct rt6_info *)dst,
630-
msg->msg_flags, dontfrag);
630+
msg->msg_flags, dontfrag, &sockc_unused);
631631
if (err)
632632
ip6_flush_pending_frames(sk);
633633
else if (!(msg->msg_flags & MSG_MORE))

0 commit comments

Comments
 (0)