netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jason Xing <kerneljasonxing@gmail.com>
To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
	pabeni@redhat.com, dsahern@kernel.org,
	willemdebruijn.kernel@gmail.com, willemb@google.com,
	ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
	martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org,
	yonghong.song@linux.dev, john.fastabend@gmail.com,
	kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com,
	jolsa@kernel.org
Cc: bpf@vger.kernel.org, netdev@vger.kernel.org,
	Jason Xing <kernelxing@tencent.com>
Subject: [PATCH net-next v2 00/12] net-timestamp: bpf extension to equip applications transparently
Date: Sat, 12 Oct 2024 12:06:39 +0800	[thread overview]
Message-ID: <20241012040651.95616-1-kerneljasonxing@gmail.com> (raw)

From: Jason Xing <kernelxing@tencent.com>

A few weeks ago, I planned to extend SO_TIMESTMAMPING feature by using
tracepoint to print information (say, tstamp) so that we can
transparently equip applications with this feature and require no
modification in user side.

Later, we discussed at netconf and agreed that we can use bpf for better
extension, which is mainly suggested by John Fastabend and Willem de
Bruijn. Many thanks here! So I post this series to see if we have a
better solution to extend. My feeling is BPF is a good place to provide
a way to add timestamping by administrators, without having to rebuild
applications. 

This approach mostly relies on existing SO_TIMESTAMPING feature, users
only needs to pass certain flags through bpf_setsocktop() to a separate
tsflags. For TX timestamps, they will be printed during generation
phase. For RX timestamps, we will wait for the moment when recvmsg() is
called.

After this series, we could step by step implement more advanced
functions/flags already in SO_TIMESTAMPING feature for bpf extension.

In this series, I only support TCP protocol which is widely used in
SO_TIMESTAMPING feature.

---
V2
Link: https://lore.kernel.org/all/20241008095109.99918-1-kerneljasonxing@gmail.com/
1. Introduce tsflag requestors so that we are able to extend more in the
future. Besides, it enables TX flags for bpf extension feature separately
without breaking users. It is suggested by Vadim Fedorenko.
2. introduce a static key to control the whole feature. (Willem)
3. Open the gate of bpf_setsockopt for the SO_TIMESTAMPING feature in
some TX/RX cases, not all the cases.

Note:
The main concern we've discussion in V1 thread is how to deal with the
applications using SO_TIMESTAMPING feature? In this series, I allow both
cases to happen at the same time, which indicates that even one
applications setting SO_TIMESTAMPING can still be traced through BPF
program. Please see patch [04/12].


Here is the test output:
1) receive path
iperf3-987305  [008] ...11 179955.200990: bpf_trace_printk: rx: port: 5201:55192, swtimestamp: 1728167973,670426346, hwtimestamp: 0,0
2) xmit path
iperf3-19765   [013] ...11  2021.329602: bpf_trace_printk: tx: port: 47528:5201, key: 1036, timestamp: 1728357067,436678584
iperf3-19765   [013] b..11  2021.329611: bpf_trace_printk: tx: port: 47528:5201, key: 1036, timestamp: 1728357067,436689976
iperf3-19765   [013] ...11  2021.329622: bpf_trace_printk: tx: port: 47528:5201, key: 1036, timestamp: 1728357067,436700739

Here is the full bpf program:
#include <linux/bpf.h>

#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
#include <uapi/linux/net_tstamp.h>

int _version SEC("version") = 1;
char _license[] SEC("license") = "GPL";

# define SO_TIMESTAMPING         37

__section("sockops")
int set_initial_rto(struct bpf_sock_ops *skops)
{
	int op = (int) skops->op;
	u32 sport = 0, dport = 0;
	int flags;

	switch (op) {
	//case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
	case BPF_SOCK_OPS_TCP_CONNECT_CB:
	case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
		flags = SOF_TIMESTAMPING_RX_SOFTWARE |
			SOF_TIMESTAMPING_TX_SCHED | SOF_TIMESTAMPING_TX_ACK | SOF_TIMESTAMPING_TX_SOFTWARE |
			SOF_TIMESTAMPING_OPT_ID | SOF_TIMESTAMPING_OPT_ID_TCP;
		bpf_setsockopt(skops, SOL_SOCKET, SO_TIMESTAMPING, &flags, sizeof(flags));
		bpf_sock_ops_cb_flags_set(skops, BPF_SOCK_OPS_TX_TIMESTAMPING_OPT_CB_FLAG|BPF_SOCK_OPS_RX_TIMESTAMPING_OPT_CB_FLAG);
		break;
	case BPF_SOCK_OPS_TS_SCHED_OPT_CB:
	case BPF_SOCK_OPS_TS_SW_OPT_CB:
	case BPF_SOCK_OPS_TS_ACK_OPT_CB:
		dport = bpf_ntohl(skops->remote_port);
		sport = skops->local_port;
		bpf_printk("tx: port: %u:%u, key: %u, timestamp: %u,%u\n",
			    sport, dport, skops->args[0], skops->args[1], skops->args[2]);
		break;
	case BPF_SOCK_OPS_TS_RX_OPT_CB:
		dport = bpf_ntohl(skops->remote_port);
		sport = skops->local_port;
		bpf_printk("rx: port: %u:%u, swtimestamp: %u,%u, hwtimestamp: %u,%u\n",
			   sport, dport, skops->args[0], skops->args[1], skops->args[2], skops->args[3]);
		break;
	}
	return 1;
}


Jason Xing (12):
  net-timestamp: introduce socket tsflag requestors
  net-timestamp: open gate for bpf_setsockopt
  net-timestamp: reorganize in skb_tstamp_tx_output()
  net-timestamp: add static key to control the whole bpf extension
  net-timestamp: add bpf infrastructure to allow exposing timestamp
    later
  net-timestamp: introduce TS_SCHED_OPT_CB to generate dev xmit
    timestamp
  net-timestamp: introduce TS_SW_OPT_CB to generate driver timestamp
  net-timestamp: introduce TS_ACK_OPT_CB to generate tcp acked timestamp
  net-timestamp: add tx OPT_ID_TCP support for bpf case
  net-timestamp: make bpf for tx timestamp work
  net-timestamp: add bpf framework for rx timestamps
  net-timestamp: add bpf support for rx software/hardware timestamp

 include/linux/tcp.h            |   2 +-
 include/net/ip.h               |   2 +-
 include/net/sock.h             |  19 ++++--
 include/net/tcp.h              |  16 +++++-
 include/uapi/linux/bpf.h       |  28 ++++++++-
 net/can/j1939/socket.c         |   2 +-
 net/core/filter.c              |  39 +++++++++++++
 net/core/skbuff.c              | 102 ++++++++++++++++++++++++++++++---
 net/core/sock.c                |  68 +++++++++++++++-------
 net/ipv4/ip_output.c           |   2 +-
 net/ipv4/ip_sockglue.c         |   2 +-
 net/ipv4/tcp.c                 |  60 ++++++++++++++++++-
 net/ipv6/ip6_output.c          |   2 +-
 net/ipv6/ping.c                |   2 +-
 net/ipv6/raw.c                 |   2 +-
 net/ipv6/udp.c                 |   2 +-
 net/sctp/socket.c              |   2 +-
 net/socket.c                   |   4 +-
 tools/include/uapi/linux/bpf.h |  28 ++++++++-
 19 files changed, 333 insertions(+), 51 deletions(-)

-- 
2.37.3


             reply	other threads:[~2024-10-12  4:07 UTC|newest]

Thread overview: 73+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-12  4:06 Jason Xing [this message]
2024-10-12  4:06 ` [PATCH net-next v2 01/12] net-timestamp: introduce socket tsflag requestors Jason Xing
2024-10-15  1:30   ` Willem de Bruijn
2024-10-15  1:50     ` Jason Xing
2024-10-12  4:06 ` [PATCH net-next v2 02/12] net-timestamp: open gate for bpf_setsockopt Jason Xing
2024-10-15  1:34   ` Willem de Bruijn
2024-10-15  2:05     ` Jason Xing
2024-10-15 21:32   ` Martin KaFai Lau
2024-10-15 21:55     ` Willem de Bruijn
2024-10-22 13:22       ` Jason Xing
2024-10-23  0:06         ` Willem de Bruijn
2024-10-23  3:49           ` Jason Xing
2024-10-16  0:45     ` Jason Xing
2024-10-15 23:54   ` Martin KaFai Lau
2024-10-16  0:49     ` Jason Xing
2024-10-12  4:06 ` [PATCH net-next v2 03/12] net-timestamp: reorganize in skb_tstamp_tx_output() Jason Xing
2024-10-12  4:06 ` [PATCH net-next v2 04/12] net-timestamp: add static key to control the whole bpf extension Jason Xing
2024-10-15  1:36   ` Willem de Bruijn
2024-10-15  2:25     ` Jason Xing
2024-10-16  0:09   ` Martin KaFai Lau
2024-10-16  1:04     ` Jason Xing
2024-10-16  1:32       ` Jason Xing
2024-10-16  6:13         ` Martin KaFai Lau
2024-10-16  6:30           ` Jason Xing
2024-10-16  7:01             ` Martin KaFai Lau
2024-10-16  7:54               ` Jason Xing
2024-10-16  8:31                 ` Martin KaFai Lau
2024-10-16 10:36                   ` Jason Xing
2024-10-17  0:48                     ` Martin KaFai Lau
2024-10-17  2:28                       ` Jason Xing
2024-10-17 20:43                         ` Martin KaFai Lau
2024-10-18  2:52                           ` Jason Xing
2024-10-18  3:05                             ` Jason Xing
2024-10-16  6:31       ` Martin KaFai Lau
2024-10-16  6:45         ` Jason Xing
2024-10-16 13:13           ` Willem de Bruijn
2024-10-16 13:22             ` Jason Xing
2024-10-20 21:51   ` Willem de Bruijn
2024-10-21  3:21     ` Jason Xing
2024-10-21 14:49       ` Willem de Bruijn
2024-10-21 15:05         ` Jason Xing
2024-10-22  0:53     ` Martin KaFai Lau
2024-10-22  2:30       ` Jason Xing
2024-10-23  0:17       ` Willem de Bruijn
2024-10-23  2:31         ` Willem de Bruijn
2024-10-12  4:06 ` [PATCH net-next v2 05/12] net-timestamp: add bpf infrastructure to allow exposing timestamp later Jason Xing
2024-10-12  4:06 ` [PATCH net-next v2 06/12] net-timestamp: introduce TS_SCHED_OPT_CB to generate dev xmit timestamp Jason Xing
2024-10-16  1:01   ` Martin KaFai Lau
2024-10-16  1:24     ` Jason Xing
2024-10-16  5:35       ` Martin KaFai Lau
2024-10-16  6:08         ` Jason Xing
2024-10-12  4:06 ` [PATCH net-next v2 07/12] net-timestamp: introduce TS_SW_OPT_CB to generate driver timestamp Jason Xing
2024-10-12  4:06 ` [PATCH net-next v2 08/12] net-timestamp: introduce TS_ACK_OPT_CB to generate tcp acked timestamp Jason Xing
2024-10-12  4:06 ` [PATCH net-next v2 09/12] net-timestamp: add tx OPT_ID_TCP support for bpf case Jason Xing
2024-10-15  1:38   ` Willem de Bruijn
2024-10-15  2:25     ` Jason Xing
2024-10-15  2:38       ` Willem de Bruijn
2024-10-15  2:59         ` Jason Xing
2024-10-15  8:40   ` kernel test robot
2024-10-15  9:36     ` Jason Xing
2024-10-12  4:06 ` [PATCH net-next v2 10/12] net-timestamp: make bpf for tx timestamp work Jason Xing
2024-10-12  4:06 ` [PATCH net-next v2 11/12] net-timestamp: add bpf framework for rx timestamps Jason Xing
2024-10-15  1:44   ` Willem de Bruijn
2024-10-15  2:18     ` Jason Xing
2024-10-12  4:06 ` [PATCH net-next v2 12/12] net-timestamp: add bpf support for rx software/hardware timestamp Jason Xing
2024-10-12 17:48 ` [PATCH net-next v2 00/12] net-timestamp: bpf extension to equip applications transparently Willem de Bruijn
2024-10-13  3:28   ` Jason Xing
2024-10-13  3:43     ` Jason Xing
2024-10-13  6:05       ` Jason Xing
2024-10-15  1:28     ` Willem de Bruijn
2024-10-15  2:52       ` Jason Xing
2024-10-15  2:59         ` Willem de Bruijn
2024-10-15  3:02           ` Jason Xing

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241012040651.95616-1-kerneljasonxing@gmail.com \
    --to=kerneljasonxing@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=eddyz87@gmail.com \
    --cc=edumazet@google.com \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kernelxing@tencent.com \
    --cc=kpsingh@kernel.org \
    --cc=kuba@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=willemb@google.com \
    --cc=willemdebruijn.kernel@gmail.com \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).