From patchwork Tue Jan 21 01:28:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13945568 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A0F6B70814; Tue, 21 Jan 2025 01:29:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737422960; cv=none; b=OHgzbnj7F6gYkL89tgTCFjnzmcFJtv9C/1aq+IV0DP/DWR01uKUOoOmnUK8LI/xKupSROGSTzQvSTaXBMu+eBs8smUQsHvdtLG23kWAAzcnZHAM8h5u6qBHaH+2l1kuL2GMFk4uR3/OXVyyiioE5nevwA4HPIYmofnUe9Tk1+VQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737422960; c=relaxed/simple; bh=OpVgZbTHsTj6V68AluOERUM8K5utIBu/I7orSr3m6Mg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lKXubMihzHZZ6yaJme+liGko4ybb0FGYQ+LQfwd2uoHZRLRsG6mhVOM7lM6hRVTldCo4Uis19mCNgFSQ5vq9okYDzurSYEugenMVSb/d1c195UsNj6roNQUcwwUPk2S33GZDiL7Uk0+J1xTIC92afbcQLXrC0YwDKnNblTlrNYc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=inlGe7v6; arc=none smtp.client-ip=209.85.214.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="inlGe7v6" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-21680814d42so82348915ad.2; Mon, 20 Jan 2025 17:29:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737422956; x=1738027756; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ktT8LCaPSw6wlCGFys9DzU2r3YJ39A8VYSOpln33MbU=; b=inlGe7v6U6bxdPteE4zWtADp07JKSI5sBWMk2EKlQOy/jRPos8tFo/40+HhbTazMTp zyCEkshOD46FoI7Wm7rYaJK287C02qekaOd4F4YCfJbEF64SOYArvZsunOeqb5RyGqVV zDxbeEm98eQeoQ2HZUai6mZGGRNNBkDWFhmegrrMXTnWRWNdqUllzSH58/jsMJOYOuwN oBoVuEs9oRYaTt/+HTZolcgkalbGAQoybKW+7pWMWyE2gakk1WbzlxxY05cXNGUpsm9V MZwU44neqKooRnvB2eKhDAu4VXbboC28ZIt1pmbdcFExpjsJ7z+m8d2BBIor2p/grolq vDGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737422956; x=1738027756; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ktT8LCaPSw6wlCGFys9DzU2r3YJ39A8VYSOpln33MbU=; b=KonsZC7AJg7QLNJtsdoknKY8eyxg/HW8qcH8p6mXdexb7k/sVIBT2krZqjXa33eR+k F8LZxw+0qY+zeFZdzFPIVXiOq1Hm0ARhhCm2VYLCX9ZFXjYqDnRhv2dxelHaP7R89v35 YQeB9UGuBLLOL1xn286BVlEO4LUHdOpbBHKoATjELynfZUKySGu8awPzmOU/GygQc7Uj 7ZOlfBYLmlkbsUr7JDjCS84WbS6F0zVGcQs+WHKLlmkFk07VKDFpQVFy45t2x3fBG8RV jvOu0gwamTTPMTgnZP7eViIYqqNV9jLpSm0vf4MjNQqCsoagm0Dzv84LGeVruPpA4TVj ertA== X-Forwarded-Encrypted: i=1; AJvYcCUvt9U4ePm8NpS10TIurKgUxENL+e6kg12EUTyC8D+gsYXeRd9gMhGNMTn0iXQT1mJr5ijeq90=@vger.kernel.org X-Gm-Message-State: AOJu0Yx/hQuERWHFIS5XlyWVWvNcREW3Rtmq4HOEQ+vGI+uNQgn8uDuO aPBqMSry8c5YQz4kvVRMeYDhv0TeMh6vkFS9dFnsJt00ciY8Uwuk X-Gm-Gg: ASbGnctUMvOGJ1Gby02b6HJbxBNdBpdZOQGYRjMlP1owVoX9BBjmqX2dq5uLsaHzWt3 o8LKXuBUvKnPOFWiAXgkVfUWGcDmP5GrWJUT1YQUDjbx24AOeuEuPxrem6SLS0Jt381Fr2An/Sd g8EfGSJifzSSv4DNzzuIfq3qg9SwwcWjFb3dHQ7JkdM+SerBFkE5FAKjYHN1YixXeg7/DophynD EyP4C4xsboHR4T27yRr87QmV1s66gR3J450KkB08BG30wCk9GwzFWVeveewKntL1cAGJcRr6tFK ucWXfNBT0yqsAn2OLou3PWDPvP+nQwQT X-Google-Smtp-Source: AGHT+IEHVGeAzU0qxiATSrnfvpUNUmeygYgm4X9uQ41zIioZeY5nXJDWrhQLhVwg4nnG8WNSAxdgIQ== X-Received: by 2002:a05:6a00:1804:b0:725:9f02:489a with SMTP id d2e1a72fcca58-72dafa8382cmr20258676b3a.17.1737422956309; Mon, 20 Jan 2025 17:29:16 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72daba55c13sm7702059b3a.129.2025.01.20.17.29.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jan 2025 17:29:15 -0800 (PST) From: Jason Xing 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [RFC PATCH net-next v6 01/13] net-timestamp: add support for bpf_setsockopt() Date: Tue, 21 Jan 2025 09:28:49 +0800 Message-Id: <20250121012901.87763-2-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250121012901.87763-1-kerneljasonxing@gmail.com> References: <20250121012901.87763-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC Users can write the following code to enable the bpf extension: bpf_setsockopt(skops, SOL_SOCKET, SK_BPF_CB_FLAGS, &flags, sizeof(flags)); Signed-off-by: Jason Xing --- include/net/sock.h | 3 +++ include/uapi/linux/bpf.h | 8 ++++++++ net/core/filter.c | 23 +++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 1 + 4 files changed, 35 insertions(+) diff --git a/include/net/sock.h b/include/net/sock.h index 8036b3b79cd8..7916982343c6 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -303,6 +303,7 @@ struct sk_filter; * @sk_stamp: time stamp of last packet received * @sk_stamp_seq: lock for accessing sk_stamp on 32 bit architectures only * @sk_tsflags: SO_TIMESTAMPING flags + * @sk_bpf_cb_flags: used in bpf_setsockopt() * @sk_use_task_frag: allow sk_page_frag() to use current->task_frag. * Sockets that can be used under memory reclaim should * set this to false. @@ -445,6 +446,8 @@ struct sock { u32 sk_reserved_mem; int sk_forward_alloc; u32 sk_tsflags; +#define SK_BPF_CB_FLAG_TEST(SK, FLAG) ((SK)->sk_bpf_cb_flags & (FLAG)) + u32 sk_bpf_cb_flags; __cacheline_group_end(sock_write_rxtx); __cacheline_group_begin(sock_write_tx); diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 4162afc6b5d0..e629e09b0b31 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -6903,6 +6903,13 @@ enum { BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7F, }; +/* Definitions for bpf_sk_cb_flags */ +enum { + SK_BPF_CB_TX_TIMESTAMPING = 1<<0, + SK_BPF_CB_MASK = (SK_BPF_CB_TX_TIMESTAMPING - 1) | + SK_BPF_CB_TX_TIMESTAMPING +}; + /* List of known BPF sock_ops operators. * New entries can only be added at the end */ @@ -7081,6 +7088,7 @@ enum { TCP_BPF_SYN_IP = 1006, /* Copy the IP[46] and TCP header */ TCP_BPF_SYN_MAC = 1007, /* Copy the MAC, IP[46], and TCP header */ TCP_BPF_SOCK_OPS_CB_FLAGS = 1008, /* Get or Set TCP sock ops flags */ + SK_BPF_CB_FLAGS = 1009, /* Used to set socket bpf flags */ }; enum { diff --git a/net/core/filter.c b/net/core/filter.c index 5b5996901ccc..8e2715b7ac8a 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5222,6 +5222,25 @@ static const struct bpf_func_proto bpf_get_socket_uid_proto = { .arg1_type = ARG_PTR_TO_CTX, }; +static int sk_bpf_set_get_cb_flags(struct sock *sk, char *optval, bool getopt) +{ + u32 sk_bpf_cb_flags; + + if (getopt) { + *(u32 *)optval = sk->sk_bpf_cb_flags; + return 0; + } + + sk_bpf_cb_flags = *(u32 *)optval; + + if (sk_bpf_cb_flags & ~SK_BPF_CB_MASK) + return -EINVAL; + + sk->sk_bpf_cb_flags = sk_bpf_cb_flags; + + return 0; +} + static int sol_socket_sockopt(struct sock *sk, int optname, char *optval, int *optlen, bool getopt) @@ -5238,6 +5257,7 @@ static int sol_socket_sockopt(struct sock *sk, int optname, case SO_MAX_PACING_RATE: case SO_BINDTOIFINDEX: case SO_TXREHASH: + case SK_BPF_CB_FLAGS: if (*optlen != sizeof(int)) return -EINVAL; break; @@ -5247,6 +5267,9 @@ static int sol_socket_sockopt(struct sock *sk, int optname, return -EINVAL; } + if (optname == SK_BPF_CB_FLAGS) + return sk_bpf_set_get_cb_flags(sk, optval, getopt); + if (getopt) { if (optname == SO_BINDTODEVICE) return -EINVAL; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 4162afc6b5d0..6b0a5b787b12 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7081,6 +7081,7 @@ enum { TCP_BPF_SYN_IP = 1006, /* Copy the IP[46] and TCP header */ TCP_BPF_SYN_MAC = 1007, /* Copy the MAC, IP[46], and TCP header */ TCP_BPF_SOCK_OPS_CB_FLAGS = 1008, /* Get or Set TCP sock ops flags */ + SK_BPF_CB_FLAGS = 1009, /* Used to set socket bpf flags */ }; enum { From patchwork Tue Jan 21 01:28:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13945569 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 097562581; Tue, 21 Jan 2025 01:29:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737422965; cv=none; b=fKUrREX7JkZPW/aUGRGu4NC5Df0OVVP1e9BSV5x20xGpRkxQDcoP7qATXiFytuBIFf+IOYtL820pIf0P9f8MMznWSrImEkDvCSM556yGbbdr5aONR+vbZCgd3dbi+HM5Akl0XORTjMPJlLoirOp2k4VJC25l92J14QdeB27y0dI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737422965; c=relaxed/simple; bh=3D6CTiqb/f70GuSUwpWkXCXGc4XWIghQE63JycIPQH8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Q9JHclfx6ewcU3CC4AdXOsEMI67jh27foAANJsa6V9q2ze+pp6ESBaRYQBLlOo8ITvGr2V34I2+3394nY419zUbHvxLyK18ja70xUsGMCojkAWGsCna3zdOF+T1c0m69kiCiccdqa8W+tVGwrNblHiaBg9sk0LaefmqFJ+VV2R4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=QJ7vohYJ; arc=none smtp.client-ip=209.85.216.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QJ7vohYJ" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2ee86a1a92dso6957707a91.1; Mon, 20 Jan 2025 17:29:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737422962; x=1738027762; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=UkstOM3ja7v2blKf63dQgeE3MsZLse2qrlDmnXc41uQ=; b=QJ7vohYJZffbnP2Hhy8PZXv1na5SgAQntU+NpH3/OGlj/9K7+yNnoDoYKeljH9aUbA F+l8Ht+uxgnH3lRmM9LwdDwCWvnlZKNWSKaamGVSmCiy9wXtHxbzHK7YL4Fw1j3/yaFx IVAMIgpxMgOM6UNiFYtEqx6nS8kHY66HAP+Qqa6RFhj2xLlWQQFChKz1SZVn4W6PJomM tBHS4foLqxWNkbGW/4Dshfc3hbNURY0D1zzVOEhtDA3ocRyp9d4+JwHmduHQIKf0vrNy fBhIeKdyWQ/ZMe+G50S9JPTCS5963BmHhDh2ijICv4sU6KpaRK/zg/R8UoJBzqapQvsq pMzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737422962; x=1738027762; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UkstOM3ja7v2blKf63dQgeE3MsZLse2qrlDmnXc41uQ=; b=PPpwzoc4JD+mpIeL9hPlvJYRRwMUL9Z22SZCSh4YO9F2IHekOZ12jIqMODQTEv5z1n iEilrtpIvozSxOk4Y6TvURVD9rXP++laPmV6hDsWB/t9C3/ylYnY8Hcf84/wOzDXDX7L AyWWP1kIs6cc2MhmGsV5fQV2fmLD/wIarwV9IicpUd2sqCJlkRmPHSPpl9DLU2SUhoHD jbeSkx6w7eeghWJMCjaphcpmr0tMH0tjsZ9PcCPPPtME57lNKMvcrmLGVfdJI04A7+xb Vrpg0Q8YiF6BS5ryJ/HIpCWoLY7FoX13nl1mlTwKIOiECqks4KmuEH/KzCJEqZUA3uY7 FBYA== X-Forwarded-Encrypted: i=1; AJvYcCUaCFtA2x91NHhppBFffzjtrLqN6q2mRKgnLIzXa5dK1cZUQhFJ0NJfhwUl5kZiJJYaAxzdJmE=@vger.kernel.org X-Gm-Message-State: AOJu0YzdVpXEX5bUM8B8ACRbbWlGRhs1oetxdZ/UiWvVZ2xIXwIcsck5 uW0lRMEheUbCF6NNSbZJhvThG+9gIzw97D+XyX1et8cw43lyNq2N X-Gm-Gg: ASbGncvrxbrADtouydW0WM3UH6b1chGOFxjI4R1LyCtxv/Fm49ckyva37AmQfDYHsRn 5seSN1oMkngqSCB3RAHzFEmjB3R2bzpY7/Io27OgSF92f5tCi8YeI/OAy3NocK81bNcPrNIL/93 5WGvQQebaTEmcO5OHK9Kfp9BjAHPVQz/x8vYJYXVoYl8qkpDlgOU6wRkG5PSQuLiruErvHospgM ahk0e/qbtZnhdg4QR5BGdp6vC2YTi0TcJ62Rrn5eWs07s20zfS3iz2M6L8WraVG+E9h0k/gDLd3 E6aT5WUqt0cDnfAjylpQUqZkXFXrDFt9 X-Google-Smtp-Source: AGHT+IGeDXkYW3fQISiYo51Qyam3GIbnaV13t/hEAugsGS4mdyuWyKZEVj+uLCmwSTWCNKPoirQ+Vw== X-Received: by 2002:a05:6a00:418d:b0:71e:21:d2d8 with SMTP id d2e1a72fcca58-72daf946789mr23181257b3a.7.1737422962285; Mon, 20 Jan 2025 17:29:22 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72daba55c13sm7702059b3a.129.2025.01.20.17.29.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jan 2025 17:29:21 -0800 (PST) From: Jason Xing 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [RFC PATCH net-next v6 02/13] net-timestamp: prepare for timestamping callbacks use Date: Tue, 21 Jan 2025 09:28:50 +0800 Message-Id: <20250121012901.87763-3-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250121012901.87763-1-kerneljasonxing@gmail.com> References: <20250121012901.87763-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC Later, I would introduce four callback points to report information to user space based on this patch. As to skb initialization here, people can follow these three steps as below to fetch the shared info from the exported skb in the bpf prog: 1. skops_kern = bpf_cast_to_kern_ctx(skops); 2. skb = skops_kern->skb; 3. shinfo = bpf_core_cast(skb->head + skb->end, struct skb_shared_info); More details can be seen in the last selftest patch of the series. Signed-off-by: Jason Xing --- include/net/sock.h | 7 +++++++ net/core/sock.c | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/net/sock.h b/include/net/sock.h index 7916982343c6..6f4d54faba92 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -2923,6 +2923,13 @@ int sock_set_timestamping(struct sock *sk, int optname, struct so_timestamping timestamping); void sock_enable_timestamps(struct sock *sk); +#if defined(CONFIG_CGROUP_BPF) +void bpf_skops_tx_timestamping(struct sock *sk, struct sk_buff *skb, int op); +#else +static inline void bpf_skops_tx_timestamping(struct sock *sk, struct sk_buff *skb, int op) +{ +} +#endif void sock_no_linger(struct sock *sk); void sock_set_keepalive(struct sock *sk); void sock_set_priority(struct sock *sk, u32 priority); diff --git a/net/core/sock.c b/net/core/sock.c index eae2ae70a2e0..e165163521dc 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -948,6 +948,19 @@ int sock_set_timestamping(struct sock *sk, int optname, return 0; } +void bpf_skops_tx_timestamping(struct sock *sk, struct sk_buff *skb, int op) +{ + struct bpf_sock_ops_kern sock_ops; + + memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp)); + sock_ops.op = op; + sock_ops.is_fullsock = 1; + sock_ops.sk = sk; + bpf_skops_init_skb(&sock_ops, skb, 0); + /* Timestamping bpf extension supports only TCP and UDP full socket */ + __cgroup_bpf_run_filter_sock_ops(sk, &sock_ops, CGROUP_SOCK_OPS); +} + void sock_set_keepalive(struct sock *sk) { lock_sock(sk); From patchwork Tue Jan 21 01:28:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13945570 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F12105028C; Tue, 21 Jan 2025 01:29:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737422970; cv=none; b=c1fY/+czYYBtGPXxPuW6qm1yUVWoRSKDIdmMIDs+MFTfFDsvw0yxal5IFmDArNRdvVyE4fvi5M2z0SPdv0q9aMKQgkuxduaPmlFMj9F8iGPtbhYSZ9R/N6Lblv240IVyoULqJIqsmXfcmdH2HnHOFNNmGCVxIeokV7JQ2/O9syk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737422970; c=relaxed/simple; bh=Mt2YWj/OjScewMDsPH2Ntvpf2c4snwR3Pvqpb/tvsRw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=i54DFARfSCSorCqRf5gR3+stfnVBySGhzFxN0Yyx0/slPdOfSiAF5alAJJXA2TCrtsyVDRd6JvdgfG/Zt/O0gz+sw139VOeEBJw3ih3aM+SLzAHC13j/bHYpydvK1iY2SDn1cyZWxGEyWaiWbhuiBQ7Vo411VcRZwpgnlk2SWM0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=edWBTMw9; arc=none smtp.client-ip=209.85.216.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="edWBTMw9" Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-2efd81c7ca4so6790256a91.2; Mon, 20 Jan 2025 17:29:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737422968; x=1738027768; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=K4VJSRGKihhLslplbWEU790/b3AShcED91qmujEVpMY=; b=edWBTMw9mTmhHZ+K1Q+6u3rrvFWW6YhuUp4B1jzp9CPNnJrH6rGIF1sIfRP5WKjSh8 4niZKgV3gNVjTPajEmzT9tN/nnbBcgC+InWX4XGDTRVadD/LGf2v8Ct3QL6sVECUON3/ snidCJzzzRj9zot1s4rMzNiU0ee4R7kl8AHQi16UDMXD6UGw7JHFlLi+udQDoea7cFGY UhBri4hjl4qvWc2h+o+sXvLb5MTKIiYm+wQHMrpCS84IZ2y5VLYBZr9wSVo9v1QSBGL/ t3bx5jwqK+oMZCVsJiIoatbZikIVzFQjcitQwQ5y/NDzSWh4Icz8A02lx/9a43GzrvnX SUdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737422968; x=1738027768; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=K4VJSRGKihhLslplbWEU790/b3AShcED91qmujEVpMY=; b=nrZtHm3/g7cJbbG+LiE2zPLfVnj2WhqJzrTxW9I1PO+9zOAVfJnteJXxXUFlhI35rr OfC6faCpL038kLxY75xOOPaKmWdrQWnp66k5RxDJcvalqK0dWt996/y50OEN8E6zKnss tAbKc3n88Z/Z2Ih+briw/Bghn6YSCqZI3Zr7j190DMxIWG44jhGbHFRoUdsZGvk2QP51 Ua1j/pEU08O1hllaWif+3Uc75wCf4ZM0BR4yCpentd1P4VxtFg/XpyBnSv1zA55OsarT I7D+yEUaLhS8nCGU97trdMncepzrrmCH1H/XQ/c/0My1gwIHx4MgUf3wdpy0TZGFfUQ1 RqXw== X-Forwarded-Encrypted: i=1; AJvYcCWzShdhVV0xaTQpsX5e/Lsq+VgtXvcJTZdXi4+PbL5zogT4ww+ToPgkIeuSsNLtuAjiBlERVKE=@vger.kernel.org X-Gm-Message-State: AOJu0YyZWedgF1F+Z656vHFCV/1E+6MgSdrpMO21JCFd0aDayGQDaEbc EhbP8ysydY44iTlLXBoV/1NbUQAMNT3J2inPK4eRLmriUa9a+8PVxoSmvJbz X-Gm-Gg: ASbGncuY8UH6EEbdXfwQ1oysVP0+ydP7fwq5B8H8vOeAhJNpHobMIPWyKALP+aapgUC RMXBkIJfqK7UGBrep3yllLA1lGrO9BJOEACIDCDNTXAqCitQR1AHZG3Mkp5h+7inqjgJWvcnkwV Wx/SJp7B35A5MdTAcOV9MxS/A9UZ77ZDKd4TzfXwnVFq9OoJK+UZQn1Ym38QNeLygjokqp6oNJ+ EjjLV63+kkGVzkis5FTZH+37Bpp7oWQE/GcQsaQ/GdS/HvGEoT2kJRjbB7gIo8UZbK/qkRTTNc+ hFN2VSPht9oqLu98lcVEChC9E1Qy/9ve X-Google-Smtp-Source: AGHT+IETSQxYuuW7A5kfIf+9Is9ki9CU2MNKqFpuNW+SVmNCoFfcKJv1svuPWG2f0v6Wv94DCVxNEw== X-Received: by 2002:a05:6a00:288e:b0:727:3935:dc83 with SMTP id d2e1a72fcca58-72daf950d22mr18959207b3a.10.1737422968058; Mon, 20 Jan 2025 17:29:28 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72daba55c13sm7702059b3a.129.2025.01.20.17.29.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jan 2025 17:29:27 -0800 (PST) From: Jason Xing 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [RFC PATCH net-next v6 03/13] bpf: stop UDP sock accessing TCP fields in bpf callbacks Date: Tue, 21 Jan 2025 09:28:51 +0800 Message-Id: <20250121012901.87763-4-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250121012901.87763-1-kerneljasonxing@gmail.com> References: <20250121012901.87763-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC Applying the new member allow_tcp_access in the existing callbacks where is_fullsock is set to 1 can help us stop UDP socket accessing struct tcp_sock, or else it could be catastrophe leading to panic. For now, those existing callbacks are used only for TCP. I believe in the short run, we will have timestamping UDP callbacks support. Signed-off-by: Jason Xing --- include/linux/filter.h | 1 + include/net/tcp.h | 1 + net/core/filter.c | 8 ++++---- net/ipv4/tcp_input.c | 2 ++ net/ipv4/tcp_output.c | 2 ++ 5 files changed, 10 insertions(+), 4 deletions(-) diff --git a/include/linux/filter.h b/include/linux/filter.h index a3ea46281595..1b1333a90b4a 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -1508,6 +1508,7 @@ struct bpf_sock_ops_kern { void *skb_data_end; u8 op; u8 is_fullsock; + u8 allow_tcp_access; u8 remaining_opt_len; u64 temp; /* temp and everything after is not * initialized to 0 before calling diff --git a/include/net/tcp.h b/include/net/tcp.h index 5b2b04835688..293047694710 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -2649,6 +2649,7 @@ static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args) memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp)); if (sk_fullsock(sk)) { sock_ops.is_fullsock = 1; + sock_ops.allow_tcp_access = 1; sock_owned_by_me(sk); } diff --git a/net/core/filter.c b/net/core/filter.c index 8e2715b7ac8a..fdd305b4cfbb 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -10381,10 +10381,10 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type, } \ *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ struct bpf_sock_ops_kern, \ - is_fullsock), \ + allow_tcp_access), \ fullsock_reg, si->src_reg, \ offsetof(struct bpf_sock_ops_kern, \ - is_fullsock)); \ + allow_tcp_access)); \ *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \ if (si->dst_reg == si->src_reg) \ *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \ @@ -10469,10 +10469,10 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type, temp)); \ *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ struct bpf_sock_ops_kern, \ - is_fullsock), \ + allow_tcp_access), \ reg, si->dst_reg, \ offsetof(struct bpf_sock_ops_kern, \ - is_fullsock)); \ + allow_tcp_access)); \ *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \ *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ struct bpf_sock_ops_kern, sk),\ diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index eb82e01da911..77185479ed5e 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -169,6 +169,7 @@ static void bpf_skops_parse_hdr(struct sock *sk, struct sk_buff *skb) memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp)); sock_ops.op = BPF_SOCK_OPS_PARSE_HDR_OPT_CB; sock_ops.is_fullsock = 1; + sock_ops.allow_tcp_access = 1; sock_ops.sk = sk; bpf_skops_init_skb(&sock_ops, skb, tcp_hdrlen(skb)); @@ -185,6 +186,7 @@ static void bpf_skops_established(struct sock *sk, int bpf_op, memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp)); sock_ops.op = bpf_op; sock_ops.is_fullsock = 1; + sock_ops.allow_tcp_access = 1; sock_ops.sk = sk; /* sk with TCP_REPAIR_ON does not have skb in tcp_finish_connect */ if (skb) diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index 0e5b9a654254..695749807c09 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -522,6 +522,7 @@ static void bpf_skops_hdr_opt_len(struct sock *sk, struct sk_buff *skb, sock_owned_by_me(sk); sock_ops.is_fullsock = 1; + sock_ops.allow_tcp_access = 1; sock_ops.sk = sk; } @@ -567,6 +568,7 @@ static void bpf_skops_write_hdr_opt(struct sock *sk, struct sk_buff *skb, sock_owned_by_me(sk); sock_ops.is_fullsock = 1; + sock_ops.allow_tcp_access = 1; sock_ops.sk = sk; } From patchwork Tue Jan 21 01:28:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13945571 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8D1FCE571; Tue, 21 Jan 2025 01:29:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737422976; cv=none; b=R4fgq4Lm5tjypzOVAdF2UbhSUXVvA/OvACwccT/WPzu2LHfJvItI6xze7DGIj0pNll1GKscFreqRIAVshpl+bB9vn6Hyw2zHgllT7fTKo8Yw81mfLkeOlbY8PxqQBa2oXdwqnAVTAx5PDGbFzBq98JYDJXeMaGffJPbLYfKOo8c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737422976; c=relaxed/simple; bh=b5dfQu3D8lN/SJ0QMIcSKFAhYjQwLVu/dWkxduUvSb0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=PCLT/Jgbv1Nb8oAF7y6+etgY3dFwrJQDN92RciztBEhqB9eZQLlEU4VVIWZOMfp6dzeqlzj7UCKc6L2dPapuk4MEjc4WKS/VlwJJ/vgSqS+iO7LsRoP9895Wq8rYLI78FRnE3qZrtkD28maNYfpAGboeqBHmFGgSAYJlYkXWRW4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=DC9nw6Rq; arc=none smtp.client-ip=209.85.214.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DC9nw6Rq" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-2164b662090so92272065ad.1; Mon, 20 Jan 2025 17:29:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737422974; x=1738027774; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bpJD72QRUo+fJ8X6WYMEGLfAIe4PuuokV1Ldl6tQY/4=; b=DC9nw6RqBR9gz5JqOy5fzRshF/D5+FoXctgfpGyZIOaJ/KYlEpue9foD/bX3ROnrB0 P4Li6Is8qYaNL/JaBPWcoHmglHoP+A7H+Wx/msIKTARwVwcvP3eOlL0TZbjtycVcfclO bQhoppacHZ8RTJCP7S52kprTY4KwGtEU2ZpDGDrq2YtB8H3fcbrEMJDhkIC51DxjODLQ u6qJaW/VArMOiS12PWYKysgLqvgcCYYk7i2OEOyUi45Es5STWjZ2bmmbE2r3sN+Sg9Z8 2zNqcQWcyqxV+/AYkhLEZpkZgUA1Q4azmQFiGwJvxNVPJ301baurA/2RZbhos/RtL+2B YJfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737422974; x=1738027774; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bpJD72QRUo+fJ8X6WYMEGLfAIe4PuuokV1Ldl6tQY/4=; b=IloBVdCf+keW1nWUJIIX5BfObEZxp1Bykplzxyq4PqXNr1zAKFs1KZPfYPwj4QVOP+ jl6xqxnMnuTfjqrh5FTy3rtDGLqcTmLgOXzftukpgKHQvBoiMXFpgvbZ3UaKt2DJ65Oj 3U+vI3giriYvCvur33sxDabsMCaU1p4Ker25nDO2vMXvK+1Z7eJUUQR6KDWTnAtMH8Zn zlqdJE2FZ3BQf1IE4rNCqDdGQ2v4QRf6GO/opfI5FBIt9Y78xbxxfYxfvOen3G5rn6k7 QWkt/06RiKGME//JjJ8zlCrTbRr+EvVT2qbyiDmI6NkyHhuWpuCyeQWt/Fv8opFeVwSb s98g== X-Forwarded-Encrypted: i=1; AJvYcCWkqWvudtf3rgs/ojO1M7zUzZo384Z/clSCbbm9MzJWu0IOea1hO6/tzzGsWYFQs6QBrI0J0Gg=@vger.kernel.org X-Gm-Message-State: AOJu0Yw1iFtKU5Y69KZO0hfYSbsw+NVf4NN45a1s90MHoMg4BLb/sbal Yo6Vuq2Xqyjds0XD4ahR4ApokIQubd/Ts/Sy+EeoC5QG6dIudb1w X-Gm-Gg: ASbGncv3t5jwSt1oI317AFKsAI+Dx2o1EfkVetywW1sLDVhdlOXsR4tnUDdmL6xEmMT SqtLTdXCuw7iQFnCpYDCOlkGnb5+zhSqWy5F7TLvdpMOiaWOjaLCUQZrofBltEwfHnwEOPhsbv5 6Y8lZIg7FxfPH0R/CpJ/F08bKdS8pJnqqUTQ7k3R5yvYM48F5x4aNbsxkCekPGZWG52HEaJgxHN z1YLkvZcgLG7WrJtdmx2u0YEPx2eb52Wu+re2SZnFSLlT0iQmIG0p10b7SW2ptYXcQihBQXn979 v0HeHHLPy3LQ6Mgh5V8Z0SpheeB0WW4m X-Google-Smtp-Source: AGHT+IEqKwI2ut26NwGXi0jdbWYonXc2CCHEW8opmVpw5BqnaysKWvMVxbrbEskvyae5G/o9V3B9Ag== X-Received: by 2002:a05:6a20:12d0:b0:1e0:d837:c929 with SMTP id adf61e73a8af0-1eb2144d329mr21784668637.9.1737422973770; Mon, 20 Jan 2025 17:29:33 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72daba55c13sm7702059b3a.129.2025.01.20.17.29.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jan 2025 17:29:33 -0800 (PST) From: Jason Xing 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [RFC PATCH net-next v6 04/13] bpf: stop UDP sock accessing TCP fields in sock_op BPF CALLs Date: Tue, 21 Jan 2025 09:28:52 +0800 Message-Id: <20250121012901.87763-5-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250121012901.87763-1-kerneljasonxing@gmail.com> References: <20250121012901.87763-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC In the next round, we will support the UDP proto for SO_TIMESTAMPING bpf extension, so we need to ensure there is no safety problem, which is ususally caused by UDP socket trying to access TCP fields. These approaches can be categorized into two groups: 1. add TCP protocol check 2. add sock op check Signed-off-by: Jason Xing --- net/core/filter.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/net/core/filter.c b/net/core/filter.c index fdd305b4cfbb..934431886876 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5523,6 +5523,11 @@ static int __bpf_setsockopt(struct sock *sk, int level, int optname, return -EINVAL; } +static bool is_locked_tcp_sock_ops(struct bpf_sock_ops_kern *bpf_sock) +{ + return bpf_sock->op <= BPF_SOCK_OPS_WRITE_HDR_OPT_CB; +} + static int _bpf_setsockopt(struct sock *sk, int level, int optname, char *optval, int optlen) { @@ -5673,7 +5678,12 @@ static const struct bpf_func_proto bpf_sock_addr_getsockopt_proto = { BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock, int, level, int, optname, char *, optval, int, optlen) { - return _bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen); + struct sock *sk = bpf_sock->sk; + + if (is_locked_tcp_sock_ops(bpf_sock) && sk_fullsock(sk)) + sock_owned_by_me(sk); + + return __bpf_setsockopt(sk, level, optname, optval, optlen); } static const struct bpf_func_proto bpf_sock_ops_setsockopt_proto = { @@ -5759,6 +5769,7 @@ BPF_CALL_5(bpf_sock_ops_getsockopt, struct bpf_sock_ops_kern *, bpf_sock, int, level, int, optname, char *, optval, int, optlen) { if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP && + bpf_sock->sk->sk_protocol == IPPROTO_TCP && optname >= TCP_BPF_SYN && optname <= TCP_BPF_SYN_MAC) { int ret, copy_len = 0; const u8 *start; @@ -5800,7 +5811,8 @@ BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock, struct sock *sk = bpf_sock->sk; int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS; - if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk)) + if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk) || + sk->sk_protocol != IPPROTO_TCP) return -EINVAL; tcp_sk(sk)->bpf_sock_ops_cb_flags = val; @@ -7609,6 +7621,9 @@ BPF_CALL_4(bpf_sock_ops_load_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock, u8 search_kind, search_len, copy_len, magic_len; int ret; + if (!is_locked_tcp_sock_ops(bpf_sock)) + return -EOPNOTSUPP; + /* 2 byte is the minimal option len except TCPOPT_NOP and * TCPOPT_EOL which are useless for the bpf prog to learn * and this helper disallow loading them also. From patchwork Tue Jan 21 01:28:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13945572 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5452E2581; Tue, 21 Jan 2025 01:29:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737422982; cv=none; b=K7Icxh30Qz3VPdug0wYwe8tialG/+N1fv1MYYnnfTUYJc8+duWNrAN7rz2R5XPLWI1rD+u6KMhbgqTLlLzjtrGiuCz6h4FmC7mMroOZBLGYuf8v35am/fyJ/46ZzXfogY5KIr7/m1NIzsqLQRtE+MfvVJeFLuJ5di0ZDbaPb+qQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737422982; c=relaxed/simple; bh=QApQtC5XUqZW26wLg8a9HCJozP8Mzr+LBUJglp2VALk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WdzEHmb1dV5XsOWg2q9uzixn027YI05UHiJqvgY1+2WeGjq7tBUyaKGzc+Rty3hcHes0PlrG/DQ4L0ml/wsDZcWwmKlJDuYxoRp3RZylNYgxz/yz/8KapsvuJS9Dhy+XDEQHrF6js4tDnMxrDkz8cco+OmT02sgeUv7hoxGdF0s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=jIPl3mhy; arc=none smtp.client-ip=209.85.216.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jIPl3mhy" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2eeb4d643a5so9056866a91.3; Mon, 20 Jan 2025 17:29:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737422980; x=1738027780; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BzNnDEADoQ3Tn/NnieMSGDXghu1f1Kdjynrdkn9Gsf4=; b=jIPl3mhyZFjkhWt9P3XYNMKU8zPoMkHcNk4HlGMopChq/v8YGwMjOwc92WKn2IeHc+ DVfWLIhwoo9vvLfk8/UNv9YK5PeqqX6t+GqbqoBJE2c85jJB2NY25nDgBq0srobxkrwP jhVvU4eTevi6oq0uNDMQbpgI9UARhmoD0IrmnmIRGRDpZ+pzL1WLYRkf9ubJ73qZhAMt OK/264cdqmPlhyCMBHJhmD7td0rLUyEzzH0+Wgi0nVALvZvttcVSger4i0xmC/5cgYWa S3gHT+dGfDBDcii2PJ47AzmmiNsKnAxmw8/ytP2bmmQsrU8b8c/Lo0TXkSsl/QkLukt8 yy0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737422980; x=1738027780; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BzNnDEADoQ3Tn/NnieMSGDXghu1f1Kdjynrdkn9Gsf4=; b=KEIv1H/3e51hZvcWasry+pIkrhIcprNfQaZgGsJvlvebe0p0SJkQY904lhXZt4eX93 Jw6JUAyFxmnvqY0pYcCXUtYWpBHLiRW/WuMro2pjo6AG4vE7cArO3uVDEdCQYNVMVfn+ c5AxYZwN/AyaDxSTrtlE8B1vzTitglRNHIfVijAeMQncmhsMdsLp/5su4WVlvDyH1sgc Yo+J5fz591aB7x0wt7ajrCiGQOLeDaVQOwWBEEg5TfEX7GDWx3iIo8kTf1oLMvGhF9zW OrqIYsb6/mPud9rBW3RLXxAgIFabKu3E39oa/L4uJCHBzA1XI2h9XGXCGDavstHVT5nS rUKA== X-Forwarded-Encrypted: i=1; AJvYcCVGkkI5vSCezl9iFj4pUh0jY9C5s2DUHnd0VLM0ScjGVZLZhVUCBX26tSfbwjv56ZKCYcGM0PI=@vger.kernel.org X-Gm-Message-State: AOJu0YxdQGeLrd29UiQIp11S7s/U4uWnw20zc1DSuYp/DrrS4Wc0hSBZ 8+pojGdrr2BzPNIn1ftICGuIAV52M8ZimJHtBGQqtM0xDaUxooMl X-Gm-Gg: ASbGncs+CUyRoY3kOk1cK03cGF7bUf7R3mkrt/1ez4AJPp0nQ3DjbV/DvMqeZq8eEFV qoMt5uv5bIJTScaKGuBcXtkRz/kWYBWMC2nfdoJgLeaNQjkeT1q9SmE6JYqMRhITGPLcYRUm+Gq a1Q4dmCfKRKRClCarfZ6D8pUtVj/NrSVCGsdi2N40qUrz9U5PHuGISS2kQoVQ/qpYzOO/RBZNoi oYYkt8Gd4PihJ6628tGwlfi3yq5hZriC9J2oJ/GjC8972K4pSTCh21Ne9U0ukVJzIdsxKn7EzQ/ Q4N/y7OXGgUgCfZtr9u4hN2u+HJp9cEj X-Google-Smtp-Source: AGHT+IEj5DA+mxVJutVUlE7lrrQHxt9FDHy5LRYEhBHvspEIRk3xKk+bwLQyIortJ7EF4NbvAPmZlQ== X-Received: by 2002:aa7:8884:0:b0:725:973f:9d53 with SMTP id d2e1a72fcca58-72dafb71bfdmr20326853b3a.15.1737422979582; Mon, 20 Jan 2025 17:29:39 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72daba55c13sm7702059b3a.129.2025.01.20.17.29.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jan 2025 17:29:39 -0800 (PST) From: Jason Xing 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [RFC PATCH net-next v6 05/13] net-timestamp: prepare for isolating two modes of SO_TIMESTAMPING Date: Tue, 21 Jan 2025 09:28:53 +0800 Message-Id: <20250121012901.87763-6-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250121012901.87763-1-kerneljasonxing@gmail.com> References: <20250121012901.87763-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC No functional changes here. I add skb_enable_app_tstamp() to test if the orig_skb matches the usage of application SO_TIMESTAMPING and skb_sw_tstamp_tx() to distinguish the software and hardware timestamp when tsflag is SCM_TSTAMP_SND. Also, I deliberately distinguish the the software and hardware SCM_TSTAMP_SND timestamp by passing 'sw' parameter in order to avoid such a case where hardware may go wrong and pass a NULL hwstamps, which is even though unlikely to happen. If it really happens, bpf prog will finally consider it as a software timestamp. It will be hardly recognized. Let's make the timestamping part more robust. After this patch, I will soon add checks about bpf SO_TIMESTAMPING. In this way, we can support two modes parallelly. Signed-off-by: Jason Xing --- include/linux/skbuff.h | 13 +++++++------ net/core/dev.c | 2 +- net/core/skbuff.c | 32 ++++++++++++++++++++++++++++++-- net/ipv4/tcp_input.c | 3 ++- 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index bb2b751d274a..dfc419281cc9 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -39,6 +39,7 @@ #include #include #include +#include /** * DOC: skb checksums @@ -4533,18 +4534,18 @@ void skb_complete_tx_timestamp(struct sk_buff *skb, void __skb_tstamp_tx(struct sk_buff *orig_skb, const struct sk_buff *ack_skb, struct skb_shared_hwtstamps *hwtstamps, - struct sock *sk, int tstype); + struct sock *sk, bool sw, int tstype); /** - * skb_tstamp_tx - queue clone of skb with send time stamps + * skb_tstamp_tx - queue clone of skb with send HARDWARE timestamps * @orig_skb: the original outgoing packet * @hwtstamps: hardware time stamps, may be NULL if not available * * If the skb has a socket associated, then this function clones the * skb (thus sharing the actual data and optional structures), stores - * the optional hardware time stamping information (if non NULL) or - * generates a software time stamp (otherwise), then queues the clone - * to the error queue of the socket. Errors are silently ignored. + * the optional hardware time stamping information (if non NULL) then + * queues the clone to the error queue of the socket. Errors are + * silently ignored. */ void skb_tstamp_tx(struct sk_buff *orig_skb, struct skb_shared_hwtstamps *hwtstamps); @@ -4565,7 +4566,7 @@ static inline void skb_tx_timestamp(struct sk_buff *skb) { skb_clone_tx_timestamp(skb); if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP) - skb_tstamp_tx(skb, NULL); + __skb_tstamp_tx(skb, NULL, NULL, skb->sk, true, SCM_TSTAMP_SND); } /** diff --git a/net/core/dev.c b/net/core/dev.c index afa2282f2604..d77b8389753e 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -4501,7 +4501,7 @@ int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev) skb_assert_len(skb); if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP)) - __skb_tstamp_tx(skb, NULL, NULL, skb->sk, SCM_TSTAMP_SCHED); + __skb_tstamp_tx(skb, NULL, NULL, skb->sk, true, SCM_TSTAMP_SCHED); /* Disable soft irqs for various locks below. Also * stops preemption for RCU. diff --git a/net/core/skbuff.c b/net/core/skbuff.c index a441613a1e6c..6042961dfc02 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5539,10 +5539,35 @@ void skb_complete_tx_timestamp(struct sk_buff *skb, } EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); +static bool skb_enable_app_tstamp(struct sk_buff *skb, int tstype, bool sw) +{ + int flag; + + switch (tstype) { + case SCM_TSTAMP_SCHED: + flag = SKBTX_SCHED_TSTAMP; + break; + case SCM_TSTAMP_SND: + flag = sw ? SKBTX_SW_TSTAMP : SKBTX_HW_TSTAMP; + break; + case SCM_TSTAMP_ACK: + if (TCP_SKB_CB(skb)->txstamp_ack) + return true; + fallthrough; + default: + return false; + } + + if (skb_shinfo(skb)->tx_flags & flag) + return true; + + return false; +} + void __skb_tstamp_tx(struct sk_buff *orig_skb, const struct sk_buff *ack_skb, struct skb_shared_hwtstamps *hwtstamps, - struct sock *sk, int tstype) + struct sock *sk, bool sw, int tstype) { struct sk_buff *skb; bool tsonly, opt_stats = false; @@ -5551,6 +5576,9 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, if (!sk) return; + if (!skb_enable_app_tstamp(orig_skb, tstype, sw)) + return; + tsflags = READ_ONCE(sk->sk_tsflags); if (!hwtstamps && !(tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) @@ -5599,7 +5627,7 @@ EXPORT_SYMBOL_GPL(__skb_tstamp_tx); void skb_tstamp_tx(struct sk_buff *orig_skb, struct skb_shared_hwtstamps *hwtstamps) { - return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk, + return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk, false, SCM_TSTAMP_SND); } EXPORT_SYMBOL_GPL(skb_tstamp_tx); diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index 77185479ed5e..62252702929d 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -3330,7 +3330,8 @@ static void tcp_ack_tstamp(struct sock *sk, struct sk_buff *skb, if (!before(shinfo->tskey, prior_snd_una) && before(shinfo->tskey, tcp_sk(sk)->snd_una)) { tcp_skb_tsorted_save(skb) { - __skb_tstamp_tx(skb, ack_skb, NULL, sk, SCM_TSTAMP_ACK); + __skb_tstamp_tx(skb, ack_skb, NULL, sk, true, + SCM_TSTAMP_ACK); } tcp_skb_tsorted_restore(skb); } } From patchwork Tue Jan 21 01:28:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13945573 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 428542581; Tue, 21 Jan 2025 01:29:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737422987; cv=none; b=dtqMw7/YxUV/kBHIDhQsMcDjJGTyZXynQwOJyCTIMPOWgV+Fe4P+GDuBG/Dhk4FiZWLdHz9/RpN4gJOzRL05P266aNwaGtmzdejgsxVrCWSTzHnzDMb3vW0HbmsuBNWroVqCrQTi+/abtMrwsWtJHTOe146OxouY18MiyttEm3E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737422987; c=relaxed/simple; bh=3ehC059E8P0YJsa9N9W43mW9N+qn2dsDqB3tRC+oQDI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FZQ4f3033xhuMp7CU+5K1j7vzP7PGMUVZAxPS+GBldwWAx99i9Taf5eXs22mYvOjtCJSsQZg5qD94fBnySrmkyy3RbJUWRTb6zNJAHnmrRcKqFHT3GbNGsMElw+cIsQ9bgSYu97VaumsKlOeg6Jg6YzpV68arQVPIa9Ufhl1Mns= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=RJxXDEtb; arc=none smtp.client-ip=209.85.214.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RJxXDEtb" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2163b0c09afso91358195ad.0; Mon, 20 Jan 2025 17:29:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737422985; x=1738027785; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=MBkByw7MJZnIZPopS1d4zywcttHBuBp3gP2jDMpGN4Q=; b=RJxXDEtbj2tOwt2uBfrmwsuGkVXXweAli9fOL9s/9TkcidkLBWuWjDSgFPyXC+4Di1 4umwaLe9Ec2G8mLm5Wr+gUXFYwo1ASepsODQ8CS1RH0TWBuR5UCifomGtvlWNLSvr9tw rAl/AI3odNvB62UXmjKq74FtFG9viADGKaeRYifxG6pakpkYyIyJIu9FA7ycmtdTiNjQ 0LrYGEipZZ/trViLvsZSrw5WoS/evw3TOYUIyYdUzOlrEurGU1ZZAed4G2/WL4EN6QNs qaGD0KNG93QJzuAgxBTPws9XPthaDEavlpzgsmz+xFUpjz2nnSoRHl8uJQV4u6KwNoO0 0QBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737422985; x=1738027785; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MBkByw7MJZnIZPopS1d4zywcttHBuBp3gP2jDMpGN4Q=; b=IlIU7cnCpeuxIZSBh7OhrI1QxizbKcdLJgb5992XZ99lqa4w2GzHAQUIxCQ5M3ucyY RKTTaM5fjEtxBv5OwkW8MJGYXEDLBYmZqZRW8U/R4bYId2Bo8AxM+3YhScyLfhHT93LY YTMkC/tmDzx3ZK8yje1b5LfdKOm3TgWrJz7Zbmn/mZl3iyV6cvBPa25FbnQcyVxJzll2 xZFJMI6BAiu+fDIYP0qwaLCJqU5Znc7WMmn/TJR9CQ5i3Sq46rVyRG/mYYNfo3lINCMJ bRe//5H6Die+D00tOQwDOWc59adkNcIrrO0C211u5+Dqr6xr6Aazv1cFZLlOBbpkTAS8 grBg== X-Forwarded-Encrypted: i=1; AJvYcCU2kbiWuL3Yy4TfixYyGU/Z+dEaZvV97yJgYj2qldL1ZLevJWOyd0SmF1jToz7/p+lJ6Dtk7xY=@vger.kernel.org X-Gm-Message-State: AOJu0YyxBkcTt6Lti73CFqfR2gGh60IP+PRG2vlF2fvbHsyO3Jo7kdvt +UbQmvQO82B0lSUi2k67E7yRRX9Tb+Re0IvkCsHUUSM++O5wNrma X-Gm-Gg: ASbGncuTplBzf8O2lHQWcog0F166ayB1PMnuSzCa6AuiWWdQz2glChlaRNN0b9kr1h8 alg3oH7eiFLlzOfdK9JneYPSb/Lpp1vNxp/f0RL9nU9xnlrzBy397oyji4HsJbFT5VbT9Gp51O5 quZKdVD2KkGT4xXMYRRAcA7qMsx1QWUS1vn1nYenCk2xeZHdFpHOgN2sI410kpfTK8wXRzPB5nv oCbjT1SPIVfZd9ldWZB/+j7GDdo9v2PDu/YzKMugxVyE6xJX/2WeZaLW/lxnYq1iG2+ASuqDZpB KZtDuQa2Q3wFX7SvTM/jByH+g9+6Q90P X-Google-Smtp-Source: AGHT+IGwlazqOMicQGliwYqr8XbTFYFjPdGvu1jNP+1O6OrknLC+d7A+ejynwcP0fB8mbGFey7LcWA== X-Received: by 2002:a05:6a21:6d8a:b0:1ea:f941:8da0 with SMTP id adf61e73a8af0-1eb214e52damr22026713637.24.1737422985443; Mon, 20 Jan 2025 17:29:45 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72daba55c13sm7702059b3a.129.2025.01.20.17.29.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jan 2025 17:29:44 -0800 (PST) From: Jason Xing 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [RFC PATCH net-next v6 06/13] net-timestamp: support SCM_TSTAMP_SCHED for bpf extension Date: Tue, 21 Jan 2025 09:28:54 +0800 Message-Id: <20250121012901.87763-7-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250121012901.87763-1-kerneljasonxing@gmail.com> References: <20250121012901.87763-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC Introducing SKBTX_BPF is used as an indicator telling us whether the skb should be traced by the bpf prog. Signed-off-by: Jason Xing --- include/linux/skbuff.h | 6 +++++- include/uapi/linux/bpf.h | 5 +++++ net/core/dev.c | 3 ++- net/core/skbuff.c | 23 +++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 5 +++++ 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index dfc419281cc9..35c2e864dd4b 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -490,10 +490,14 @@ enum { /* generate software time stamp when entering packet scheduling */ SKBTX_SCHED_TSTAMP = 1 << 6, + + /* used for bpf extension when a bpf program is loaded */ + SKBTX_BPF = 1 << 7, }; #define SKBTX_ANY_SW_TSTAMP (SKBTX_SW_TSTAMP | \ - SKBTX_SCHED_TSTAMP) + SKBTX_SCHED_TSTAMP | \ + SKBTX_BPF) #define SKBTX_ANY_TSTAMP (SKBTX_HW_TSTAMP | \ SKBTX_HW_TSTAMP_USE_CYCLES | \ SKBTX_ANY_SW_TSTAMP) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index e629e09b0b31..72f93c6e45c1 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7022,6 +7022,11 @@ enum { * by the kernel or the * earlier bpf-progs. */ + BPF_SOCK_OPS_TS_SCHED_OPT_CB, /* Called when skb is passing through + * dev layer when SO_TIMESTAMPING + * feature is on. It indicates the + * recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect diff --git a/net/core/dev.c b/net/core/dev.c index d77b8389753e..4f291459d6b1 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -4500,7 +4500,8 @@ int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev) skb_reset_mac_header(skb); skb_assert_len(skb); - if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP)) + if (unlikely(skb_shinfo(skb)->tx_flags & + (SKBTX_SCHED_TSTAMP | SKBTX_BPF))) __skb_tstamp_tx(skb, NULL, NULL, skb->sk, true, SCM_TSTAMP_SCHED); /* Disable soft irqs for various locks below. Also diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 6042961dfc02..d19d577b996f 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5564,6 +5564,24 @@ static bool skb_enable_app_tstamp(struct sk_buff *skb, int tstype, bool sw) return false; } +static void skb_tstamp_tx_bpf(struct sk_buff *skb, struct sock *sk, int tstype) +{ + int op; + + if (!sk) + return; + + switch (tstype) { + case SCM_TSTAMP_SCHED: + op = BPF_SOCK_OPS_TS_SCHED_OPT_CB; + break; + default: + return; + } + + bpf_skops_tx_timestamping(sk, skb, op); +} + void __skb_tstamp_tx(struct sk_buff *orig_skb, const struct sk_buff *ack_skb, struct skb_shared_hwtstamps *hwtstamps, @@ -5576,6 +5594,11 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, if (!sk) return; + /* bpf extension feature entry */ + if (skb_shinfo(orig_skb)->tx_flags & SKBTX_BPF) + skb_tstamp_tx_bpf(orig_skb, sk, tstype); + + /* application feature entry */ if (!skb_enable_app_tstamp(orig_skb, tstype, sw)) return; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 6b0a5b787b12..891217ab6d2d 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7015,6 +7015,11 @@ enum { * by the kernel or the * earlier bpf-progs. */ + BPF_SOCK_OPS_TS_SCHED_OPT_CB, /* Called when skb is passing through + * dev layer when SO_TIMESTAMPING + * feature is on. It indicates the + * recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect From patchwork Tue Jan 21 01:28:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13945574 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 09E242581; Tue, 21 Jan 2025 01:29:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737422993; cv=none; b=X6Z/kvroezTayS3UIsoItVuffRrTgkaystDUzwhusAL0L0SPf+TUHCw+PZt/KAv5M/u2p5vwyn4MofxG843SlqyPFsVNNMbMrQeyEIfxOwEMRO8CzOrmxzl9jWXNbjN83gifQHkGZklbRwBSgPBHoeV0qkMAeNWO/q5BsfcOY6g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737422993; c=relaxed/simple; bh=6vmDdhI/6JRu9Y9wYIUoQUpl3zcuP+H/1uR8El71qXk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NS/R4oQ0g/0Pc6NYIWtPx2jt4TJyZp4o8L9zd0bmjKb7SXUJn1KSnfJ6DPVRxihrFASucjyD7pM7wD1MUhbzAjeFs2ywYAWO0o4Izlgy+ZnnOGj5/MxwYnDFg+j02kTIsDy35KddfmSzC95cuNmcKswKPMV4bKJlaNjlWpq8a4E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Oo2NXLSP; arc=none smtp.client-ip=209.85.216.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Oo2NXLSP" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2ee786b3277so6432807a91.1; Mon, 20 Jan 2025 17:29:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737422991; x=1738027791; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=eeKfs/0qgWaovT81hfUApgaUKeYeNmpYU4p0yGVSOos=; b=Oo2NXLSPEogTgyRIPmXaQdiklzVM0q+XneD4SoKZTg6QqRpQ/TPpMKiOH5+f59q8dN 0sCDJ3+a5rJ9+3QSsWYVRW8YZzat5GmWMN9IUrhH1VXYsA74x+57Se40bUlkyq9LDfkt 3mdCMwlUSoih7lP14hEhjaeUZHCWrVZDqGlFJ88WnHVcLMAOfrSrLxDUR24NLswFD5QE ou4ubn7+9FcNwhUhmWmxqrmOwkIN0J2GJ2UbZBKlnmyNI43/mn2miYSfteOx6HmqD/xR 4fuu2ED6H+ucHpla0ctiBmFfOlEp/U9AqY1lg18q5X39NwrT8s4XzRlxodtBNUCLmU7B hUZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737422991; x=1738027791; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eeKfs/0qgWaovT81hfUApgaUKeYeNmpYU4p0yGVSOos=; b=G8o/eaH9SttHT0n8kmtGVQmh4p5rXwzN+szLH4brD08QwM3LDGuVPySizbv193bpON ZH0ouYpKLs5EtFcESTAnypoB6gVgbJmsvG1PdR/xi0XBKDNcDUS3bCWMGNgTr7W59cR5 meiPukmttSQ2W5UHH8Gm0cF5Ikj+JTojZpLtccR8t5SXTlZLaFdC1iim+x0W4V7QJwT0 7yyomOObDlO3OUS9dyVEyn3QneIugNV7cvlMGQyUp+3k80noA5MDA3oYLmSlvWoozeG5 iAIyKd+B6uPFvitBTEKWhUR5S6JiBUnqLeyhvNWNYxiTnrvmF/dlx5Guu7br2XgwXPvN e9Kw== X-Forwarded-Encrypted: i=1; AJvYcCUb08RkPHW9pBXNmU7bb9pGYycDDzAUJCWWmA8C6K/W49K0iIyIuGSK9gMswRxIs4EuZaj07Po=@vger.kernel.org X-Gm-Message-State: AOJu0YxqWR8NihjMU4+6VSlXWNwNQcIKsYJJ5h7dbEply5iZoRcOTBIm tRtWkUrtD++0iNikkVoVOYfoZiewmmmgU1pIu3n8LSCrEgD9gGpE X-Gm-Gg: ASbGncuCYEdQ+F4ZN0HDR6MFTLrISyc9RFC641kDnMbaoA8C8eQ3pHFj2nB2kY73srf LzUEkIrdeR3EfISc8jEHCQU7Qre6+9PIQhNMzabE0wYFOjQfGOqZclWS/E/ARTlNUmDs83pH6UZ u+NIFBgBf+yBRUguyrsLQHu7C1f072kxYi56aZPiiD3CVzA0XR/ddvdnx8LUiAHQt7DruaCSwlU pTiWNUa/JqRJwOHdtv8a0TjKcgLY79dlfmDGiDg5s3qAv5S/7uRUUQtcIvPAGk4ZWpcxU6fFfsc 8oSWiIF3C7+0pBPTNfGACrwLuXB3i8kW X-Google-Smtp-Source: AGHT+IHcOxle0YcQ0mJHert+LZBLpxvQUl03Ek+wliQP9DDm2PKmIULfzDPbKnX4QLNXIOj8MwpHuQ== X-Received: by 2002:a05:6a00:2e24:b0:728:8c17:127d with SMTP id d2e1a72fcca58-72daf948568mr25873623b3a.8.1737422991224; Mon, 20 Jan 2025 17:29:51 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72daba55c13sm7702059b3a.129.2025.01.20.17.29.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jan 2025 17:29:50 -0800 (PST) From: Jason Xing 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [RFC PATCH net-next v6 07/13] net-timestamp: support sw SCM_TSTAMP_SND for bpf extension Date: Tue, 21 Jan 2025 09:28:55 +0800 Message-Id: <20250121012901.87763-8-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250121012901.87763-1-kerneljasonxing@gmail.com> References: <20250121012901.87763-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC Support SCM_TSTAMP_SND case. Then we will get the software timestamp when the driver is about to send the skb. Later, I will support the hardware timestamp. Signed-off-by: Jason Xing --- include/linux/skbuff.h | 2 +- include/uapi/linux/bpf.h | 5 +++++ net/core/skbuff.c | 10 ++++++++-- tools/include/uapi/linux/bpf.h | 5 +++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 35c2e864dd4b..de8d3bd311f5 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -4569,7 +4569,7 @@ void skb_tstamp_tx(struct sk_buff *orig_skb, static inline void skb_tx_timestamp(struct sk_buff *skb) { skb_clone_tx_timestamp(skb); - if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP) + if (skb_shinfo(skb)->tx_flags & (SKBTX_SW_TSTAMP | SKBTX_BPF)) __skb_tstamp_tx(skb, NULL, NULL, skb->sk, true, SCM_TSTAMP_SND); } diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 72f93c6e45c1..a6d761f07f67 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7027,6 +7027,11 @@ enum { * feature is on. It indicates the * recorded timestamp. */ + BPF_SOCK_OPS_TS_SW_OPT_CB, /* Called when skb is about to send + * to the nic when SO_TIMESTAMPING + * feature is on. It indicates the + * recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect diff --git a/net/core/skbuff.c b/net/core/skbuff.c index d19d577b996f..288eb9869827 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5564,7 +5564,8 @@ static bool skb_enable_app_tstamp(struct sk_buff *skb, int tstype, bool sw) return false; } -static void skb_tstamp_tx_bpf(struct sk_buff *skb, struct sock *sk, int tstype) +static void skb_tstamp_tx_bpf(struct sk_buff *skb, struct sock *sk, + int tstype, bool sw) { int op; @@ -5575,6 +5576,11 @@ static void skb_tstamp_tx_bpf(struct sk_buff *skb, struct sock *sk, int tstype) case SCM_TSTAMP_SCHED: op = BPF_SOCK_OPS_TS_SCHED_OPT_CB; break; + case SCM_TSTAMP_SND: + if (!sw) + return; + op = BPF_SOCK_OPS_TS_SW_OPT_CB; + break; default: return; } @@ -5596,7 +5602,7 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, /* bpf extension feature entry */ if (skb_shinfo(orig_skb)->tx_flags & SKBTX_BPF) - skb_tstamp_tx_bpf(orig_skb, sk, tstype); + skb_tstamp_tx_bpf(orig_skb, sk, tstype, sw); /* application feature entry */ if (!skb_enable_app_tstamp(orig_skb, tstype, sw)) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 891217ab6d2d..73fc0a95c9ca 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7020,6 +7020,11 @@ enum { * feature is on. It indicates the * recorded timestamp. */ + BPF_SOCK_OPS_TS_SW_OPT_CB, /* Called when skb is about to send + * to the nic when SO_TIMESTAMPING + * feature is on. It indicates the + * recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect From patchwork Tue Jan 21 01:28:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13945575 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0395013B2A4; Tue, 21 Jan 2025 01:29:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737422999; cv=none; b=Qreowv/czB28I2dvLAsTWG7UeNiZ55yhAqvoT0H+rdvuxdTQuQi66tXj8wfsTSQqOCvUlh+te0CkTGhCSKyJHe+OW2W4GIX5x9+R4fjJpYNpEDT2OrCoOuesAP0QUQHIF5KMGXACfZyo4cKi7KDHQFMQo9KDA6XxjDqC3zZnePE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737422999; c=relaxed/simple; bh=OeMn2IqojtMI4IcEEeJOx8GAJ6e6rHinISfToriGhAw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Jae+ZshsbhLkdlBctigKluizMdO5AZUop3wsfDigrDRr+7LaMthzh0UmaTBir5h3CVk0ln2fEF//zxXiDo99OOfJt9Pt9RVLeKhzLhMDSfnf9mrxYZCELvgPiWpdEMSWWA7M/EY8pStDnCtt0N7XxLEsRCrBCbuI7hM2TGcEvUQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=S+pbwcdT; arc=none smtp.client-ip=209.85.216.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="S+pbwcdT" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-2eeb4d643a5so9057231a91.3; Mon, 20 Jan 2025 17:29:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737422997; x=1738027797; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/2bVhj2fiEFwl9RekvLPToYuCRpij9Y8kNfLli/okws=; b=S+pbwcdTNzmWdFlrGNsFKjz6ZxbXGt856doqc2vNrA5TS27HXMa6oyoErvSindkQx4 JpNVO6WaeTuSoPe5/Uvjmtkyi5uR6Cz5a6V8WsxuHOptQjCFfR5uf8zqOvRJ//NfyZMp k1qtzwGr+hqtLsAybuMWuNcXCeDobIRXV5Vrc8ql8+LLa4ICOQj3lTVihztpj8XYaeVb LKnmuxozswAulpCrPG3F8s7ckrewRQ9GkwW6b5vopzUiCiOB51pX1XKKYfJmXNlnIReP L7l7G7tHnRHok7N1AqUKtkLyP0gR/f+YP9j7exfEJ6Fvtli+zIbRXeQ8QNVZk3W9qNix UEyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737422997; x=1738027797; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/2bVhj2fiEFwl9RekvLPToYuCRpij9Y8kNfLli/okws=; b=Ri0MeXP2mpRqAVkaRjmXWJGroFwYuxxVCU5R+5g398fQqRyxeMDUxGplyME/Br7cEq B2SaB1HrTDnG7i/w38/abh84lSS+4mpq7KAxi4kD93xE+bUio1B3zhS6/FSy2iyKTAls t8o+059KNNRz3uFnKoG5FoqAKtiVjVtG9NXA/qaj4ad6h5X2UklI72JOug9+ntZ9WK+v TYop50VqMH2twdiEyedOTKhtqI1RO2GmZ01aeKCke5Djt/a1gaN0HLh0IiR+9/iY7ij7 Au9+COz5w/3ZfJgEnUWuCBHCNfYQcmRCBwS8kf5ODN8filq6XxiBrDSOcT7QGaUn8JL+ klgg== X-Forwarded-Encrypted: i=1; AJvYcCUEI1Nosj88+qg9TsrHvUQrIy73iIs4UUMbJwj5Ebx9uIGz1UZ7bXegkPbJey/m9jVIPyo+juA=@vger.kernel.org X-Gm-Message-State: AOJu0YyIK0Yb2mABqsbNgqIXLprX3DIQSVGa6PVN04CNCmm4fUX3N+Z5 kO81TwO5FyyLP9KG4hR1cfpqu1CJOAgFzS0+2bTcAj6zseWSslSl X-Gm-Gg: ASbGnctU/tw43Cy1jt8TA0YDYvCFVl4APoq0scSE+n0ZVvFW+Tm1Okb/yEWwUFXIOnG EyC9e3k0wWMhY6vydapkfE+80KpZhv6Rz/xQDFs5n8P1vVIcADOUFtQcvHBN+r5N7SJezkbK8EV fCleJWiwmDv/tTbXavCI2biBsz2NUPLZaKzszv+5as37O0nO6ZcOkWVy+nCbX/WI38/KdToIT+J AAImUjhtt2vmOwcHOmqztbEExDPAvjHipmLw+SurezvDrH089Cwzh5q2s3jukEhSLDCriB4n42K pKI2qqeU4rMnY2bXU4M6Glp1bZQibLHU X-Google-Smtp-Source: AGHT+IFGtDW1e9JvBJ4rSLfGwnSFIk/gKwkMhmWQNmEUb2pUGLYuve4/Gg2nYiry0NG6+yFN6jR8TA== X-Received: by 2002:a05:6a00:3c8a:b0:71e:6b8:2f4a with SMTP id d2e1a72fcca58-72dafa45104mr21972382b3a.12.1737422997144; Mon, 20 Jan 2025 17:29:57 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72daba55c13sm7702059b3a.129.2025.01.20.17.29.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jan 2025 17:29:56 -0800 (PST) From: Jason Xing 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [RFC PATCH net-next v6 08/13] net-timestamp: support hw SCM_TSTAMP_SND for bpf extension Date: Tue, 21 Jan 2025 09:28:56 +0800 Message-Id: <20250121012901.87763-9-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250121012901.87763-1-kerneljasonxing@gmail.com> References: <20250121012901.87763-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC In this patch, we finish the hardware part. Then bpf program can fetch the hwstamp from skb directly. To avoid changing so many callers using SKBTX_HW_TSTAMP from drivers, use this simple modification like this patch does to support printing hardware timestamp. Signed-off-by: Jason Xing --- include/linux/skbuff.h | 4 +++- include/uapi/linux/bpf.h | 5 +++++ net/core/skbuff.c | 11 ++++++----- net/dsa/user.c | 2 +- net/socket.c | 2 +- tools/include/uapi/linux/bpf.h | 5 +++++ 6 files changed, 21 insertions(+), 8 deletions(-) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index de8d3bd311f5..df2d790ae36b 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -471,7 +471,7 @@ struct skb_shared_hwtstamps { /* Definitions for tx_flags in struct skb_shared_info */ enum { /* generate hardware time stamp */ - SKBTX_HW_TSTAMP = 1 << 0, + __SKBTX_HW_TSTAMP = 1 << 0, /* generate software time stamp when queueing packet to NIC */ SKBTX_SW_TSTAMP = 1 << 1, @@ -495,6 +495,8 @@ enum { SKBTX_BPF = 1 << 7, }; +#define SKBTX_HW_TSTAMP (__SKBTX_HW_TSTAMP | SKBTX_BPF) + #define SKBTX_ANY_SW_TSTAMP (SKBTX_SW_TSTAMP | \ SKBTX_SCHED_TSTAMP | \ SKBTX_BPF) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index a6d761f07f67..8936e1061e71 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7032,6 +7032,11 @@ enum { * feature is on. It indicates the * recorded timestamp. */ + BPF_SOCK_OPS_TS_HW_OPT_CB, /* Called in hardware phase when + * SO_TIMESTAMPING feature is on. + * It indicates the recorded + * timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 288eb9869827..c769feae5162 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5548,7 +5548,7 @@ static bool skb_enable_app_tstamp(struct sk_buff *skb, int tstype, bool sw) flag = SKBTX_SCHED_TSTAMP; break; case SCM_TSTAMP_SND: - flag = sw ? SKBTX_SW_TSTAMP : SKBTX_HW_TSTAMP; + flag = sw ? SKBTX_SW_TSTAMP : __SKBTX_HW_TSTAMP; break; case SCM_TSTAMP_ACK: if (TCP_SKB_CB(skb)->txstamp_ack) @@ -5565,7 +5565,8 @@ static bool skb_enable_app_tstamp(struct sk_buff *skb, int tstype, bool sw) } static void skb_tstamp_tx_bpf(struct sk_buff *skb, struct sock *sk, - int tstype, bool sw) + int tstype, bool sw, + struct skb_shared_hwtstamps *hwtstamps) { int op; @@ -5577,9 +5578,9 @@ static void skb_tstamp_tx_bpf(struct sk_buff *skb, struct sock *sk, op = BPF_SOCK_OPS_TS_SCHED_OPT_CB; break; case SCM_TSTAMP_SND: + op = sw ? BPF_SOCK_OPS_TS_SW_OPT_CB : BPF_SOCK_OPS_TS_HW_OPT_CB; if (!sw) - return; - op = BPF_SOCK_OPS_TS_SW_OPT_CB; + *skb_hwtstamps(skb) = *hwtstamps; break; default: return; @@ -5602,7 +5603,7 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, /* bpf extension feature entry */ if (skb_shinfo(orig_skb)->tx_flags & SKBTX_BPF) - skb_tstamp_tx_bpf(orig_skb, sk, tstype, sw); + skb_tstamp_tx_bpf(orig_skb, sk, tstype, sw, hwtstamps); /* application feature entry */ if (!skb_enable_app_tstamp(orig_skb, tstype, sw)) diff --git a/net/dsa/user.c b/net/dsa/user.c index 291ab1b4acc4..ae715bf0ae75 100644 --- a/net/dsa/user.c +++ b/net/dsa/user.c @@ -897,7 +897,7 @@ static void dsa_skb_tx_timestamp(struct dsa_user_priv *p, { struct dsa_switch *ds = p->dp->ds; - if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) + if (!(skb_shinfo(skb)->tx_flags & __SKBTX_HW_TSTAMP)) return; if (!ds->ops->port_txtstamp) diff --git a/net/socket.c b/net/socket.c index 262a28b59c7f..70eabb510ce6 100644 --- a/net/socket.c +++ b/net/socket.c @@ -676,7 +676,7 @@ void __sock_tx_timestamp(__u32 tsflags, __u8 *tx_flags) u8 flags = *tx_flags; if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE) { - flags |= SKBTX_HW_TSTAMP; + flags |= __SKBTX_HW_TSTAMP; /* PTP hardware clocks can provide a free running cycle counter * as a time base for virtual clocks. Tell driver to use the diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 73fc0a95c9ca..f1583b5814ea 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7025,6 +7025,11 @@ enum { * feature is on. It indicates the * recorded timestamp. */ + BPF_SOCK_OPS_TS_HW_OPT_CB, /* Called in hardware phase when + * SO_TIMESTAMPING feature is on. + * It indicates the recorded + * timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect From patchwork Tue Jan 21 01:28:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13945576 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C52CE155398; Tue, 21 Jan 2025 01:30:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737423005; cv=none; b=sB0mRIvW5ZWrTwKpFwMPFKDGI4A9mo1FxxObJHAnFVsbVYUE2GC5uw+jgVi+Kz4kuUPFuLSjBegdZybV2vOuA1HBCPqihc0UzWHJBwowpN0PT3k/zq6k7rrfcx7rGvZFjxd+3RXko/TeEbGNVr2LutkHmo/2/MDKYaBnU1o7MP0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737423005; c=relaxed/simple; bh=WTwZJBx4xbLt6HePgDoqCPofAl9YXcziI5sMZAyORm0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pyeMqbZjMhf9yc0bYiOEXiX0nEZqP8a5pRgTina4M1xoxcT/vC9nPnXajA4e6YOi7mgEqXE0Yv112b7OtLHxzFF/3Eju/+hN22n4Epiruz8nsT/onWtapRS8FNdgd7UeVDSFFGgsMiMcon0Ptm169xv1Dmuo3shAWEXFsPJ+zTI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=DY63xtts; arc=none smtp.client-ip=209.85.216.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DY63xtts" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2ee46851b5eso6801823a91.1; Mon, 20 Jan 2025 17:30:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737423003; x=1738027803; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/lv3eDUOPZ0dOtmoJgRwt45XM9WOlETTR3Qxn+7dp+c=; b=DY63xttsCSstugXkFAFKJDzX0fapDjZJTqvf2+kGtSgoCzC4TxlZNCMC1WJofycCll +N00LhCAbjV0vX51jfV4xQx9k5ltvRI+b30hGhLyaXDmeWB9BoXgnk28Pf74U6M0dCIm wZwbONyISyKCvIwd1IGknkSd3mVs1EFGk0bcLGWMNZY1LlfVB9aqDmAgRUdVIg3yZ3Tb tIzXW0a+P5dHfqPe+hn34uzkwXFa6cF4KgGcwlRuDvTRPRXO9u/tfCwqQY19C9gU4gbx YBuRGoBarZpVHhWq8fl2tK6VPEM4+B9MXWQZ42zo2MfBU5NaA3zYU6w+oXaGjBZZ0q+y cQGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737423003; x=1738027803; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/lv3eDUOPZ0dOtmoJgRwt45XM9WOlETTR3Qxn+7dp+c=; b=baat1OedQ3C38xyvib7vUOSXuNtPbgFby7NudaID4m0dBkb85KIeDJP1bY56oja0+N UZvdaEJdBUQw+P9W8QmqLp/KWT7yKatTRi9faGfR3tYOtf2uAFh31sHcFj9VqncQxWcR g224U/CxczVbkE8/qxqoArpDknKBKDsCQnSLh5d9uWCA4UpUvcOHdnUOdApz/pTJk2st GqPdHW8WaOXkdJy43bLtK3b05Ebqcdd0T++2aUdkICASbztAb0fDcneJpLLqYnC8AEA1 vihRQ8tJZZ/jWkXU/Ht0H6WCRCJYvGtT4mf/gnHzjRt1vVDCtiTfmKzEohSDIkiLeZMm ArLg== X-Forwarded-Encrypted: i=1; AJvYcCUYZiC8gdrJtFgf5TiYXT3Jmrsd3QuCfP3YO713d/aYofvZryS745JUW8JhZgeMOQ51gFmhRPA=@vger.kernel.org X-Gm-Message-State: AOJu0Yyt7jQ/rpYbboBuSWnx4SO+HZ652p1dBs7JKyd8J0YasL0Ow4QB mNXOQss+FHjJwbfQmV/8bofSAmOI5M2t07WBnQML5j56q0KEzO+pObZ5IAHT X-Gm-Gg: ASbGncv+ug97oJZ8T4C6YxZg6BlvnMtfPct4kj7RW8ARwb8KY14dIcZ1lmTV0qvrrkN tuSEQzF9+3qZr71ANNTh/zNeQ6vIkzT3msbDaQyPNa85c65iYmyFi+LJZ6NcED1KbW6PRPeFxdv 7tfTdvzMw5ZSp8YRlW1K02OEU69FICyu5WdE3gBIh/TtTn1Y2vjdXmDr0CnTYQ8rp20B4f6iHRY gjpGVYQyM/CkMnvrewkdd236k/P0eeZgIW/K3sihhOE+/+ceM+pViGH0S8aUvTqNp3HrguO+aE5 4UGxVEhvzZawbx3yWQpkOsb2DMwfOemg X-Google-Smtp-Source: AGHT+IE4ubQ0/xPyRn2tqB80u6F7+pifP3Wu/AiUsya034DHZ3EPUjxgEbUoPUXyyLf+b/DlZne5sQ== X-Received: by 2002:a05:6a00:2ea8:b0:725:aa5d:f217 with SMTP id d2e1a72fcca58-72daf94f635mr21839147b3a.7.1737423003001; Mon, 20 Jan 2025 17:30:03 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72daba55c13sm7702059b3a.129.2025.01.20.17.29.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jan 2025 17:30:02 -0800 (PST) From: Jason Xing 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [RFC PATCH net-next v6 09/13] net-timestamp: support SCM_TSTAMP_ACK for bpf extension Date: Tue, 21 Jan 2025 09:28:57 +0800 Message-Id: <20250121012901.87763-10-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250121012901.87763-1-kerneljasonxing@gmail.com> References: <20250121012901.87763-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC Handle the ACK timestamp case. Actually testing SKBTX_BPF flag can work, but we need to Introduce a new txstamp_ack_bpf to avoid cache line misses in tcp_ack_tstamp(). To be more specific, in most cases, normal flows would not access skb_shinfo as txstamp_ack is zero, so that this function won't appear in the hot spot lists. Introducing a new member txstamp_ack_bpf works similarly. Signed-off-by: Jason Xing --- include/net/tcp.h | 3 ++- include/uapi/linux/bpf.h | 5 +++++ net/core/skbuff.c | 3 +++ net/ipv4/tcp_input.c | 3 ++- net/ipv4/tcp_output.c | 5 +++++ tools/include/uapi/linux/bpf.h | 5 +++++ 6 files changed, 22 insertions(+), 2 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index 293047694710..88429e422301 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -959,9 +959,10 @@ struct tcp_skb_cb { __u8 sacked; /* State flags for SACK. */ __u8 ip_dsfield; /* IPv4 tos or IPv6 dsfield */ __u8 txstamp_ack:1, /* Record TX timestamp for ack? */ + txstamp_ack_bpf:1, /* ack timestamp for bpf use */ eor:1, /* Is skb MSG_EOR marked? */ has_rxtstamp:1, /* SKB has a RX timestamp */ - unused:5; + unused:4; __u32 ack_seq; /* Sequence number ACK'd */ union { struct { diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 8936e1061e71..3b9bfc88345c 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7037,6 +7037,11 @@ enum { * It indicates the recorded * timestamp. */ + BPF_SOCK_OPS_TS_ACK_OPT_CB, /* Called when all the skbs are + * acknowledged when SO_TIMESTAMPING + * feature is on. It indicates the + * recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect diff --git a/net/core/skbuff.c b/net/core/skbuff.c index c769feae5162..33340e0b094f 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5582,6 +5582,9 @@ static void skb_tstamp_tx_bpf(struct sk_buff *skb, struct sock *sk, if (!sw) *skb_hwtstamps(skb) = *hwtstamps; break; + case SCM_TSTAMP_ACK: + op = BPF_SOCK_OPS_TS_ACK_OPT_CB; + break; default: return; } diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index 62252702929d..c8945f5be31b 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -3323,7 +3323,8 @@ static void tcp_ack_tstamp(struct sock *sk, struct sk_buff *skb, const struct skb_shared_info *shinfo; /* Avoid cache line misses to get skb_shinfo() and shinfo->tx_flags */ - if (likely(!TCP_SKB_CB(skb)->txstamp_ack)) + if (likely(!TCP_SKB_CB(skb)->txstamp_ack && + !TCP_SKB_CB(skb)->txstamp_ack_bpf)) return; shinfo = skb_shinfo(skb); diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index 695749807c09..fc84ca669b76 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -1556,6 +1556,7 @@ static void tcp_adjust_pcount(struct sock *sk, const struct sk_buff *skb, int de static bool tcp_has_tx_tstamp(const struct sk_buff *skb) { return TCP_SKB_CB(skb)->txstamp_ack || + TCP_SKB_CB(skb)->txstamp_ack_bpf || (skb_shinfo(skb)->tx_flags & SKBTX_ANY_TSTAMP); } @@ -1572,7 +1573,9 @@ static void tcp_fragment_tstamp(struct sk_buff *skb, struct sk_buff *skb2) shinfo2->tx_flags |= tsflags; swap(shinfo->tskey, shinfo2->tskey); TCP_SKB_CB(skb2)->txstamp_ack = TCP_SKB_CB(skb)->txstamp_ack; + TCP_SKB_CB(skb2)->txstamp_ack_bpf = TCP_SKB_CB(skb)->txstamp_ack_bpf; TCP_SKB_CB(skb)->txstamp_ack = 0; + TCP_SKB_CB(skb)->txstamp_ack_bpf = 0; } } @@ -3213,6 +3216,8 @@ void tcp_skb_collapse_tstamp(struct sk_buff *skb, shinfo->tskey = next_shinfo->tskey; TCP_SKB_CB(skb)->txstamp_ack |= TCP_SKB_CB(next_skb)->txstamp_ack; + TCP_SKB_CB(skb)->txstamp_ack_bpf |= + TCP_SKB_CB(next_skb)->txstamp_ack_bpf; } } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index f1583b5814ea..b463aa9c27da 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7030,6 +7030,11 @@ enum { * It indicates the recorded * timestamp. */ + BPF_SOCK_OPS_TS_ACK_OPT_CB, /* Called when all the skbs are + * acknowledged when SO_TIMESTAMPING + * feature is on. It indicates the + * recorded timestamp. + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect From patchwork Tue Jan 21 01:28:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13945577 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A4F1013C8EA; Tue, 21 Jan 2025 01:30:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737423011; cv=none; b=FU1JhEtgblSmX1b1SgZekBjbWgwU6z9nGAVBi8rXYdPxI2remAgYGOCm5Z1qa0TuoZ1ZNi7v8j7RQ4k26ccLvqcDMd40n6e6pZNVu/zm7JD3tuCqiW8ExcN+M+tolDIt5xHBXgjKjjYc5YpbMCSg6e5rNBU0njRVcsm0+CPTHj8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737423011; c=relaxed/simple; bh=qgLgc+vtWhxd3wDw4kFYbDMlFJVL1MhDz8zW3gWKabs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JdAJrk+UeiH3K+C2XPl8gKJT04NatsSZTNEBMBrFMxU4X2kiLT4LKd+Ejo4s3oXW8BGvKBDweVMrsknSAC9Gf6sLm85d5aAtShdEN+YEjrEPV92LAXVCblks+rdyPLoIGPctR2C/Zp/pYTQZywZt9G2BP885LMpk4ajZTmDa1Hg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=AdOG+VBV; arc=none smtp.client-ip=209.85.216.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="AdOG+VBV" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2f13acbe29bso9342662a91.1; Mon, 20 Jan 2025 17:30:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737423009; x=1738027809; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KivH3GOTURvpBe4gz30uCxSbbTX4Z4qxExfXxl7mwmc=; b=AdOG+VBVuo9QcYhNmYMb/bAFOrOiPX0/sb7cXF5AshiJrMNqMG0WoruIi6XNVdOGEU 7OADrCSKzwdd3ApQ5aIuVITfia22VBsD9tx+IE7fWVMGSGV4UQbPQvVL/z8C8pp36Cze EnjLP/MfBSJ6bGl4lLgQj7L0FHz0WZPEhd3wx+q+Qdj8bVA88LuO2JCh/gMjFE6wrpMx r2NudEuulAiP4RGYl+X1clr4kN3FhuGvlP+dSnbN6x2+lERYTgoHJjvYwjCtCJx8tuU7 TCM3K/ZQ4E0YgpphXwtl9x2OMZzFsdonKf6jGNUYh1DxgNdTe7UJ1zTbgj8nCtAgbmQJ GMEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737423009; x=1738027809; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KivH3GOTURvpBe4gz30uCxSbbTX4Z4qxExfXxl7mwmc=; b=vRjOM7CyQlWbCjF4uvD45Xl+6D1DPtzn24qwapyxp5cSlzFbaxBf39mduJd81NEcgG 53ht95pR7Zi4d2iSSltqJvqnq7rV1c6rvB5zA5cr+KbUsNAmF9AkjdJvYwoDsGMZ8VLp J6l1VQpOJWuwvhtc6ffGy36VI54Vxc41CL9QIKZkIKQkZT7L6j41oaREW8N7M/SmJ37y Dd0ZukN7QrtOdc3dLe0OtKCO+ozMjFWPKyTA2Hommn+pQM6A6vgzwsYnQTwUMc2Qc2bN 30t2AulrllYom9DyKYGClyiDOv05eMEgRCVxNp7ysEg72nbLAQ7TrT8AIs4QGpgamCPY zQmQ== X-Forwarded-Encrypted: i=1; AJvYcCWNmnIxsCRX1X63drYAMBiyDMXwBCL9MVmAX1O5+bi6svzGPp3AFsaKOcNEiqZb4f1H9427tMM=@vger.kernel.org X-Gm-Message-State: AOJu0YyXWXuv891FHGDRRVU4algg3k8EA9yTTvvnVz/h5DBIC8L3XjfZ yhyEh/f5HQrR+P2YRhvCrPeZXe5j6b61xjFLLW1jzhmzuDxNzj4N X-Gm-Gg: ASbGncu2BDTQJgOBd2xIwZyaJmFAXdXHJF8MAqRdj645USu7MoG5P3dqaNOipXo3MHO NiaUJ/4TJjMiBEL+LhqV3SF4kN+9ALSZiMxoOHGKatio6Rm6KcgfUbiRmeqa+GxhfioHIlL6yOi N+20ozDc7MaLVBgWs5Lx1VH7X41SdkfYTEE0Ve9JXpmyYr8iX2bKbJyjNE1Pxwo8orl2+l7ZS/K 8figRdJLKzJNWZXiwIjePrkP7UAKgj2Lms2DZcI4uXcEwGsKYc/4MaPmxwMm944vlvdWd8R5EL7 pee8AYpMLhCua+Dclxz6U6je8c18TJmk X-Google-Smtp-Source: AGHT+IGYCM24sXUmp0++3Ui6KWM+HKLLhPpJC2OmIX6jXID0jpZLMxIHzms3EHv9ATL9itib3wjT1Q== X-Received: by 2002:aa7:888e:0:b0:725:de58:b2ea with SMTP id d2e1a72fcca58-72d8c4aef17mr40788001b3a.6.1737423008793; Mon, 20 Jan 2025 17:30:08 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72daba55c13sm7702059b3a.129.2025.01.20.17.30.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jan 2025 17:30:08 -0800 (PST) From: Jason Xing 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [RFC PATCH net-next v6 10/13] net-timestamp: make TCP tx timestamp bpf extension work Date: Tue, 21 Jan 2025 09:28:58 +0800 Message-Id: <20250121012901.87763-11-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250121012901.87763-1-kerneljasonxing@gmail.com> References: <20250121012901.87763-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC Make partial of the feature work finally. After this, user can fully use the bpf prog to trace the tx path for TCP type. Signed-off-by: Jason Xing --- net/ipv4/tcp.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 0d704bda6c41..0a41006b10d1 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -492,6 +492,15 @@ static void tcp_tx_timestamp(struct sock *sk, struct sockcm_cookie *sockc) if (tsflags & SOF_TIMESTAMPING_TX_RECORD_MASK) shinfo->tskey = TCP_SKB_CB(skb)->seq + skb->len - 1; } + + if (SK_BPF_CB_FLAG_TEST(sk, SK_BPF_CB_TX_TIMESTAMPING) && skb) { + struct skb_shared_info *shinfo = skb_shinfo(skb); + struct tcp_skb_cb *tcb = TCP_SKB_CB(skb); + + tcb->txstamp_ack_bpf = 1; + shinfo->tx_flags |= SKBTX_BPF; + shinfo->tskey = TCP_SKB_CB(skb)->seq + skb->len - 1; + } } static bool tcp_stream_is_readable(struct sock *sk, int target) From patchwork Tue Jan 21 01:28:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13945578 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 955295028C; Tue, 21 Jan 2025 01:30:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737423020; cv=none; b=UBEo2kIS5upaLXqmtfBsKWsDBSKDyoD3kfsxVXJbE/nPsWPdhBVWgFN3namVGXkZouy1zQsKRyjH0ldCX7rpgOeJBNbZgvcWIMq3yAso/M5KKkmsNXVB5LX8ZS48q7ni4Tzqmv4aepqlxn1zBGpezitkJsy6FqHf7CIst1MOx8A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737423020; c=relaxed/simple; bh=cdAZTukbPBCnDY0N2KiqkxNh942E06ybv5DIcwFSuUc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Ukf2hs2FvRCWZdW+GOHy63vV9MLeCJtoAW/jP79Q8OxR1PfqnEoLuP7D5EyKGcP5AR6mhRExZfZW/ewXx5Lc0sS1oWijlOjdt6kPMBjKRLRnHMBtz4iPqLeeQWQ31WnDylAZAQNHmp2yzghwfsYqZSia4SX6e429VY6l0EsUJaw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=krG367B/; arc=none smtp.client-ip=209.85.216.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="krG367B/" Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-2ee67e9287fso8670589a91.0; Mon, 20 Jan 2025 17:30:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737423015; x=1738027815; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=EYBgrBe5aOqcvb6kjytL4u8rCsj35g7Iyees/SszcWQ=; b=krG367B/VLWOBA1x0yOD3m6jkKaMo1JVZO9Cd4PEzlyI7mOrnh7u77DJsntu3ZxHHw ztB9+u5xFmkScZr1dQmlKcIPsg7W5bt43IPDdAqnKPiMfcbUzQR+hp/LMUarWLxAIrHz z6UQ+naR80BhJm14TJzPjTDkNX1XJDUenB1JP6mIijc4jG7Jvx6/1la47Kg1UerHSwpN EQDHKKVjYjtE64jCbLlliuO1ZZua0b9Yc+uliO9s4upyMYR0E84OojeRfq8p7GGinwhm lRzRIxRmrYecrov+w1+NM1Ifil2t5QEKJR4pGEGQ4y74EGNAiL1ffOnaPzST6FwqP4Du ddfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737423015; x=1738027815; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EYBgrBe5aOqcvb6kjytL4u8rCsj35g7Iyees/SszcWQ=; b=VjRZGKt9tSLlNH6wmXBXE6kLWlQk8aa3ETEfjz3+tSysZVjYoDCH6rVVpaTejaIIIC yL4qnuPDMTb2HMWF6vnKiZjBCbvRmxdgv40FQCEL7qXwm0zBCexIqWMC89FD9jGYhbB0 i3gT2LX5kkN8izZQ/x8LwZpuxF7zG4wnWsuEWv2puqyH3jUhb+jC5RASPqXafkv/PobF WFPURi6O6ORs0DuriXStkb4O/AaUYavnFAT7M8jMxX8QQ1mySSGxaG4EAuEnJGpyTaSp JgxjuHmyYzhcv+bSINaeopcb9brrVJT4h6+ICL9v05En9BawtxOOU6HdGVxAwsAfO02d 5ZvQ== X-Forwarded-Encrypted: i=1; AJvYcCX60C9BlXYOj5oIvcfe5NHGHuYeA6iz5B9YeN0bHIGsiL5a/xc59pFXtwFGruCMiGybqcV19+k=@vger.kernel.org X-Gm-Message-State: AOJu0Yz6S5lXDSaPiXwWs4wI7pJ8oX2RGdUB/9MfzvAoPpKTigflLwCP mlSxgKcVsl+7OxfwzXh+Y49ryPoZ6d4MApA8BZ7nhqf0gCtdjLGq X-Gm-Gg: ASbGncvp3TyUOOxUqA+OROIzTR7cVTIDaU5LziqktpwYv9vQqrPoBP7hr14X/TCtW6j UUjJI15W5tj/sfBhZ7pMijc4KWQW7C+33vsVphbmsvgejV5bq7Ig1PaecKuunMKGQPD8C9YeRF0 tlz91BPjPzs2nQWT9p/057qUgJ5J7dsoZoBqnUz1i9wcDre2+Em5zEWSj2qZ/Hil9WIZnSbmcBJ tmF1SX5lfX7AvbOR+YJ8URMkh3JgTT6da0INprDYm3WmHbt1kExPpjwBE5PDiTI6LL+6RZ0X/MF r7II2OIYdJowosRUFNqP8CG9AhJRx8A+ X-Google-Smtp-Source: AGHT+IGaGM7CIz3m0+e10Vs8zgk6GkS1V9XYzHPFC6/ENy7zmTtxpvXyVwc8+hGexLXiybIQHDfNBw== X-Received: by 2002:a05:6a00:369a:b0:726:f7c9:7b28 with SMTP id d2e1a72fcca58-72daf9bec24mr25714114b3a.8.1737423014602; Mon, 20 Jan 2025 17:30:14 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72daba55c13sm7702059b3a.129.2025.01.20.17.30.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jan 2025 17:30:14 -0800 (PST) From: Jason Xing 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [RFC PATCH net-next v6 11/13] net-timestamp: add a new callback in tcp_tx_timestamp() Date: Tue, 21 Jan 2025 09:28:59 +0800 Message-Id: <20250121012901.87763-12-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250121012901.87763-1-kerneljasonxing@gmail.com> References: <20250121012901.87763-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC Introduce the callback to correlate tcp_sendmsg timestamp with other three points (SND/SW/ACK). We can let bpf trace the beginning of tcp_sendmsg_locked() and fetch the socket addr, so that in tcp_tx_timestamp() we can correlate the tskey with the socket addr. It is accurate since they are under the protect of socket lock. More details can be found in the selftest. Signed-off-by: Jason Xing --- include/uapi/linux/bpf.h | 3 +++ net/ipv4/tcp.c | 1 + tools/include/uapi/linux/bpf.h | 3 +++ 3 files changed, 7 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 3b9bfc88345c..55c74fa18163 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7042,6 +7042,9 @@ enum { * feature is on. It indicates the * recorded timestamp. */ + BPF_SOCK_OPS_TS_TCP_SND_CB, /* Called when every tcp_sendmsg + * syscall is triggered + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 0a41006b10d1..49e489c346ea 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -500,6 +500,7 @@ static void tcp_tx_timestamp(struct sock *sk, struct sockcm_cookie *sockc) tcb->txstamp_ack_bpf = 1; shinfo->tx_flags |= SKBTX_BPF; shinfo->tskey = TCP_SKB_CB(skb)->seq + skb->len - 1; + bpf_skops_tx_timestamping(sk, skb, BPF_SOCK_OPS_TS_TCP_SND_CB); } } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index b463aa9c27da..38fc04a7ac20 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7035,6 +7035,9 @@ enum { * feature is on. It indicates the * recorded timestamp. */ + BPF_SOCK_OPS_TS_TCP_SND_CB, /* Called when every tcp_sendmsg + * syscall is triggered + */ }; /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect From patchwork Tue Jan 21 01:29:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13945579 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ED1CF1494A8; Tue, 21 Jan 2025 01:30:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737423022; cv=none; b=UBOG1WrmEI2I/39xbKGhG381T4OAHOQw14Bb7DeH6d4sh7F1o2oYVXLu0LP6SlcnZPF8UFdgzMVmLowdEXLUGy4bRDT7VUWs8KYWkaIewYMKq+ykV9G5PnFGhnr0OurTqW1qYVFPRNaYIyO/k9Oi7l0IKyRzR8OIuHwPI5fsJXY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737423022; c=relaxed/simple; bh=KWRASBLMeysksbkTL7LZdVFE/xU9QBN33MSumJPlG70=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jUfpAQlqtTP/S5GVwdf9ZNMzq1YAQQQG1VspRXoA7UHZeUET0tHwdrkjyWCEtlBqqcyMqe1g9SaU7XaGz5kk+iFcq5pls6W1c7JsdOg20igkldpde1ldQhDlovMenzuWkiZXTfXXH1hnohR3/gNpBkqH5AtEbmD60TisO9V7K6E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Ew/i1sSj; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Ew/i1sSj" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-21669fd5c7cso88737795ad.3; Mon, 20 Jan 2025 17:30:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737423020; x=1738027820; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=JaS2bELcjjFkI+HdicRbh+5OP70kiwiRW86TF/UdM6M=; b=Ew/i1sSjKE+6NyRYo25F/1On/Xl7+BAmh4O2BJ/Fv/ZQEhhoZRd3UW0+us5wUcpbhd OGd8Pz0mFKkvqiaEU8oWpB6fAjIvLejMrw7Yip2DXwLdAicE8/vTYsFA4zlyk8KHU/qb 22g84iWSFs2ru8Iukh6ZIniHosSjaDBdrO348MUR+ODgnmK5BLjkkImXuRz67Dh3RiTQ MCdQnNeZ4odNgaQMTPFAUj3iDBeJtGo79WyTQ0RNoLFXPazWLndzx58Oxbqr7tYdwej9 J2BOabIfwJwkMZwwvDUIFLG0BIHlxWbZougVAjh1u80kBg7rmubx7EoM+MPnqxOXRMa5 /ipw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737423020; x=1738027820; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JaS2bELcjjFkI+HdicRbh+5OP70kiwiRW86TF/UdM6M=; b=TP3O95olhMpAWUQqs8b9Ui/oo4/ABYZamF/g6gl3C42RadtCFUcVzSXwEI4AJofVz4 LxXZLgWQtcgs046sJuLj3frHeIk+lA7EUfQsx+97KHwyr4ZvOMXuwx3FgTeVDhsMoqJg zBKA7zi5Xc+nwY1N3MBlDjC1UWrKnGoK8/FkXm8ipvkgpk+5+prQpWBQIGIAyUf3ADF4 3rJCjBd5c37yaVkFypJLz1realbTEo+KB5AtuKRbAmV8hCxGTT52WW1+P94q7xPGucyJ REbB+7J1nrgc2+TJDNNLMC3gC1/kzSwjaZKYjcyEJ5cK6UAip5ykmsd4JU556r5Ckt88 nBhQ== X-Forwarded-Encrypted: i=1; AJvYcCV31Byekcu49Zgjz2TOimA46KYJcercovONfgZlZ/Ws6LblFqlaj/lG5eIaoQX76ta3lQK+BS8=@vger.kernel.org X-Gm-Message-State: AOJu0YxHCuHQJO5HgmzeEfcVxX8ciym0ixCwJbQsRkxj+toreA5zDSQq 2bdBZhXQRcLQ5aqGvuSShpTN92ZJZOL20Kw/bpJDN3ZeSs7G7jNp X-Gm-Gg: ASbGncuyiOIlnmzO9HuUXpZKANe6rrVCZKj0eKANX4Z7In9vLbphIa6LehEcR+2KTlU 7MUh/8SFZk+VIQwgSS1fV4YIT5Q00El5WGMOfGvLtce/7Kc6tbKLFnpotNa0thB5AZKPyjbpwSc IK9pRZYvNLXO+zbwN0HOM1QTB9pWSZBRb+/lC9ogOgjrof8SFlZR5QjCfW/mK5g9KDLt93WafrN sqzW5wdmndQfg1LjwLWX1aC1WX2AAuTQszFzhYJ9wcYCitSVog80selkxZPePpKVaq2hQeSV1cP 7IDNoLNnkInPTLnN8vXL9dIyMcxqEUuy X-Google-Smtp-Source: AGHT+IGgaaNdPylrH1fr9gVs19WE1/ysApCmoMdoUXWPSFWLGg1ysxNs2Jvk7bFG7auWYp8+hMdfNA== X-Received: by 2002:a05:6a20:9150:b0:1e1:1d7c:4cff with SMTP id adf61e73a8af0-1eb215df0a7mr24336858637.37.1737423020283; Mon, 20 Jan 2025 17:30:20 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72daba55c13sm7702059b3a.129.2025.01.20.17.30.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jan 2025 17:30:19 -0800 (PST) From: Jason Xing 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [RFC PATCH net-next v6 12/13] net-timestamp: introduce cgroup lock to avoid affecting non-bpf cases Date: Tue, 21 Jan 2025 09:29:00 +0800 Message-Id: <20250121012901.87763-13-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250121012901.87763-1-kerneljasonxing@gmail.com> References: <20250121012901.87763-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC Introducing the lock to avoid affecting the applications which are not using timestamping bpf feature. Signed-off-by: Jason Xing --- net/core/skbuff.c | 6 ++++-- net/ipv4/tcp.c | 3 ++- net/ipv4/tcp_input.c | 3 ++- net/ipv4/tcp_output.c | 3 ++- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 33340e0b094f..db5b4b653351 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5605,11 +5605,13 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, return; /* bpf extension feature entry */ - if (skb_shinfo(orig_skb)->tx_flags & SKBTX_BPF) + if (cgroup_bpf_enabled(CGROUP_SOCK_OPS) && + skb_shinfo(orig_skb)->tx_flags & SKBTX_BPF) skb_tstamp_tx_bpf(orig_skb, sk, tstype, sw, hwtstamps); /* application feature entry */ - if (!skb_enable_app_tstamp(orig_skb, tstype, sw)) + if (cgroup_bpf_enabled(CGROUP_SOCK_OPS) && + !skb_enable_app_tstamp(orig_skb, tstype, sw)) return; tsflags = READ_ONCE(sk->sk_tsflags); diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 49e489c346ea..d88160af00c4 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -493,7 +493,8 @@ static void tcp_tx_timestamp(struct sock *sk, struct sockcm_cookie *sockc) shinfo->tskey = TCP_SKB_CB(skb)->seq + skb->len - 1; } - if (SK_BPF_CB_FLAG_TEST(sk, SK_BPF_CB_TX_TIMESTAMPING) && skb) { + if (cgroup_bpf_enabled(CGROUP_SOCK_OPS) && + SK_BPF_CB_FLAG_TEST(sk, SK_BPF_CB_TX_TIMESTAMPING) && skb) { struct skb_shared_info *shinfo = skb_shinfo(skb); struct tcp_skb_cb *tcb = TCP_SKB_CB(skb); diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index c8945f5be31b..e30607ba41e5 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -3324,7 +3324,8 @@ static void tcp_ack_tstamp(struct sock *sk, struct sk_buff *skb, /* Avoid cache line misses to get skb_shinfo() and shinfo->tx_flags */ if (likely(!TCP_SKB_CB(skb)->txstamp_ack && - !TCP_SKB_CB(skb)->txstamp_ack_bpf)) + !(cgroup_bpf_enabled(CGROUP_SOCK_OPS) && + TCP_SKB_CB(skb)->txstamp_ack_bpf))) return; shinfo = skb_shinfo(skb); diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index fc84ca669b76..483f19c2083e 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -1556,7 +1556,8 @@ static void tcp_adjust_pcount(struct sock *sk, const struct sk_buff *skb, int de static bool tcp_has_tx_tstamp(const struct sk_buff *skb) { return TCP_SKB_CB(skb)->txstamp_ack || - TCP_SKB_CB(skb)->txstamp_ack_bpf || + (cgroup_bpf_enabled(CGROUP_SOCK_OPS) && + TCP_SKB_CB(skb)->txstamp_ack_bpf) || (skb_shinfo(skb)->tx_flags & SKBTX_ANY_TSTAMP); } From patchwork Tue Jan 21 01:29:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13945580 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E497C3987D; Tue, 21 Jan 2025 01:30:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737423028; cv=none; b=SxTtK5St+fjLez/dxYQZkdcdOfzHoGzP8Gv6utdFQQfcwO3HbeWK8edEeQ1iNcyBxmKt0Ct6duwhtKAxIbg0unXNih37Jw7Qb27+ilJu4ghKvlCoLeyshH6+lenkLhuT8btN4GTFmoSEyBDAj4g0iPJL8tfQC236QI6gk/Rbji0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737423028; c=relaxed/simple; bh=1+Y13KFg6TudVkGfZMxPc913WZTlIJRmj13qG/6FzVI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iwVFtisvSTW4lYyZjj+bkOkndR/fzO+g71ora7RvhU+bfWLWqD0ZREkS2D9xguw04oRWhKW8IhmF1vCMrEShGuW6wiwwMvuKxNPSUYGOg7dRIa00flOg4HzD75V42rEK0Ezlsjp2guRv7oSyGxB5xP2Vsw6A2N0FfuvhQRZG5Ck= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Ri9qlLLJ; arc=none smtp.client-ip=209.85.214.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Ri9qlLLJ" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2166360285dso95429015ad.1; Mon, 20 Jan 2025 17:30:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737423026; x=1738027826; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Z04//87PXGsZ1MumqUfbZNDDq5PygrFIxndVNdxtz4I=; b=Ri9qlLLJxCQLZjsXI9rFgSPEF97ZvqQMCbrbqPm2IYiKVaRJRJ33yg3HrUdXYA9Wd4 fHFfDUtnXpNIX1fPZXem+fmRP/2cpn59Y+Lpu9XHsz83ZsE/8gzRn2mjfJB6WbdNRW+f rp3XRhpKPWNfxv0fLGp7ZfY3YVNluupgAecf1cTU8SRnbC1MHRZa+WMeWxicBPId+TaS DAUARNfD4iR6qDLH0BRq9AfRH94/64xzEsbShHtFrE5Gq3yZHEIbwIlxz+SGoQDqTfJF SK+2byAi+iyQoTYshI0E8MI7EPdq+Aprr1j4VP3bhVcDuNEKEuQkHsdqLqzRSNFpmuxa zNsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737423026; x=1738027826; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Z04//87PXGsZ1MumqUfbZNDDq5PygrFIxndVNdxtz4I=; b=Zbpiz5NYOIT0imTdedQr7bWY6uNuRZGtoUqkHPyHAV+12630wtDQKZRQS8fA9BPa7F ItNMj/kJDi2UrAEbua3AV1fqz01nIT5eUW3cw8yAtIue8v+PGvyp7FnMX5/0tTRgp1tK Q1ZKCbSktbRWE5P2KowoJlfboSZuLCyelMP67KFjzUTffDq16VS91aUim9E2RR+OlYxi 64ABvP6XTnVXC0aUICqXdfJqdaIvGM4ro7MilC+uiBnocmtLRjNzZaBc80b1o/CSrGIL ZxhLmjsHDQNlnUUQ47NrXW8XTSi80g7WeM8/37GiWboQ+nQXTHj0eHXZVA8wmIT+tfsC /BNw== X-Forwarded-Encrypted: i=1; AJvYcCWALbj+ylcR8KrcX1H4UZxfnaaqqvlbNNFnmvT4q7QfbKtZb/pXbGmGbf7sKn36fHlVAUqi+wo=@vger.kernel.org X-Gm-Message-State: AOJu0YyQnwimgokw76oArbEsAajjIJfLOETYJU5OckIEMKPrO8b9dYIl hHULgqr2IwFCK3VrpzurtsatOhakkZexTyZx/Fn8H7Xz/H2tCf1G X-Gm-Gg: ASbGncs8QI1EwnVRJbb6Vs6i/wvdBh9Dd+W8gnX9PL0sKBXw9j8j9pgeT5zy4o1L1vL /6OdrvZ+90TJ2H2eLaJN/FBC8c4JVPNhrgd/3i21KdC0tz1bFbA12kHs4jHg1AYmqv+z9uBZuVx kAX9OBcJ6KowH0I0+J9TluGS50j2+3swnswUNTpAjk9t2NogTdbeIJN3i2L6nvqWh1ue14pa9xB cQB9amuDMon+8wS1jf3fRqZ2UGmwsQ2x2h70TS75C4fqQ3syMegcY0OpRskUew2w4tolUw0svF+ fUT8WtOjSHPcUqs2+Uxljr5E8Bg89QOY X-Google-Smtp-Source: AGHT+IE7SGTlrC+gCZxvKSAFW9KnjvZFGkMbmzeIIAlBYcICp51pI+QyURxQTa0MI/CrDKyJ4jO+1A== X-Received: by 2002:a05:6a20:2450:b0:1e1:aef4:9ce8 with SMTP id adf61e73a8af0-1eb215902e2mr26722986637.28.1737423026181; Mon, 20 Jan 2025 17:30:26 -0800 (PST) Received: from KERNELXING-MC1.tencent.com ([111.201.29.174]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72daba55c13sm7702059b3a.129.2025.01.20.17.30.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jan 2025 17:30:25 -0800 (PST) From: Jason Xing 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, horms@kernel.org Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Jason Xing Subject: [RFC PATCH net-next v6 13/13] bpf: add simple bpf tests in the tx path for so_timestamping feature Date: Tue, 21 Jan 2025 09:29:01 +0800 Message-Id: <20250121012901.87763-14-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250121012901.87763-1-kerneljasonxing@gmail.com> References: <20250121012901.87763-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC Only check if we pass those three key points after we enable the bpf extension for so_timestamping. During each point, we can choose whether to print the current timestamp. Signed-off-by: Jason Xing --- .../bpf/prog_tests/so_timestamping.c | 98 ++++++++ .../selftests/bpf/progs/so_timestamping.c | 227 ++++++++++++++++++ 2 files changed, 325 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/so_timestamping.c create mode 100644 tools/testing/selftests/bpf/progs/so_timestamping.c diff --git a/tools/testing/selftests/bpf/prog_tests/so_timestamping.c b/tools/testing/selftests/bpf/prog_tests/so_timestamping.c new file mode 100644 index 000000000000..bbfa7eb38cfb --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/so_timestamping.c @@ -0,0 +1,98 @@ +#define _GNU_SOURCE +#include +#include +#include +#include + +#include "test_progs.h" +#include "cgroup_helpers.h" +#include "network_helpers.h" + +#include "so_timestamping.skel.h" + +#define CG_NAME "/so-timestamping-test" + +static const char addr4_str[] = "127.0.0.1"; +static const char addr6_str[] = "::1"; +static struct so_timestamping *skel; +static int cg_fd; + +static int create_netns(void) +{ + if (!ASSERT_OK(unshare(CLONE_NEWNET), "create netns")) + return -1; + + if (!ASSERT_OK(system("ip link set dev lo up"), "set lo up")) + return -1; + + return 0; +} + +static void test_tcp(int family) +{ + struct so_timestamping__bss *bss = skel->bss; + char buf[] = "testing testing"; + int sfd = -1, cfd = -1; + int n; + + memset(bss, 0, sizeof(*bss)); + + sfd = start_server(family, SOCK_STREAM, + family == AF_INET6 ? addr6_str : addr4_str, 0, 0); + if (!ASSERT_GE(sfd, 0, "start_server")) + goto out; + + cfd = connect_to_fd(sfd, 0); + if (!ASSERT_GE(cfd, 0, "connect_to_fd_server")) { + close(sfd); + goto out; + } + + n = write(cfd, buf, sizeof(buf)); + if (!ASSERT_EQ(n, sizeof(buf), "send to server")) + goto out; + + ASSERT_EQ(bss->nr_active, 1, "nr_active"); + ASSERT_EQ(bss->nr_snd, 2, "nr_snd"); + ASSERT_EQ(bss->nr_sched, 1, "nr_sched"); + ASSERT_EQ(bss->nr_txsw, 1, "nr_txsw"); + ASSERT_EQ(bss->nr_ack, 1, "nr_ack"); + +out: + if (sfd >= 0) + close(sfd); + if (cfd >= 0) + close(cfd); +} + +void test_so_timestamping(void) +{ + cg_fd = test__join_cgroup(CG_NAME); + if (cg_fd < 0) + return; + + if (create_netns()) + goto done; + + skel = so_timestamping__open(); + if (!ASSERT_OK_PTR(skel, "open skel")) + goto done; + + if (!ASSERT_OK(so_timestamping__load(skel), "load skel")) + goto done; + + if (!ASSERT_OK(so_timestamping__attach(skel), "attach skel")) + goto done; + + skel->links.skops_sockopt = + bpf_program__attach_cgroup(skel->progs.skops_sockopt, cg_fd); + if (!ASSERT_OK_PTR(skel->links.skops_sockopt, "attach cgroup")) + goto done; + + test_tcp(AF_INET6); + test_tcp(AF_INET); + +done: + so_timestamping__destroy(skel); + close(cg_fd); +} diff --git a/tools/testing/selftests/bpf/progs/so_timestamping.c b/tools/testing/selftests/bpf/progs/so_timestamping.c new file mode 100644 index 000000000000..f4708e84c243 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/so_timestamping.c @@ -0,0 +1,227 @@ +#include "vmlinux.h" +#include "bpf_tracing_net.h" +#include +#include +#include +//#include +#include "bpf_misc.h" +#include "bpf_kfuncs.h" + +#define SK_BPF_CB_FLAGS 1009 +#define SK_BPF_CB_TX_TIMESTAMPING 1 + +int nr_active; +int nr_snd; +int nr_passive; +int nr_sched; +int nr_txsw; +int nr_ack; + +struct sockopt_test { + int opt; + int new; +}; + +static const struct sockopt_test sol_socket_tests[] = { + { .opt = SK_BPF_CB_FLAGS, .new = SK_BPF_CB_TX_TIMESTAMPING, }, + { .opt = 0, }, +}; + +struct loop_ctx { + void *ctx; + const struct sock *sk; +}; + +struct sk_stg { + __u64 sendmsg_ns; /* record ts when sendmsg is called */ +}; + +struct { + __uint(type, BPF_MAP_TYPE_SK_STORAGE); + __uint(map_flags, BPF_F_NO_PREALLOC); + __type(key, int); + __type(value, struct sk_stg); +} sk_stg_map SEC(".maps"); + + +struct delay_info { + u64 sendmsg_ns; /* record ts when sendmsg is called */ + u32 sched_delay; /* SCHED_OPT_CB - sendmsg_ns */ + u32 sw_snd_delay; /* SW_OPT_CB - SCHED_OPT_CB */ + u32 ack_delay; /* ACK_OPT_CB - SW_OPT_CB */ +}; + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __type(key, u32); + __type(value, struct delay_info); + __uint(max_entries, 1024); +} time_map SEC(".maps"); + +static u64 delay_tolerance_nsec = 1000000000; /* 1 second as an example */ + +static int bpf_test_sockopt_int(void *ctx, const struct sock *sk, + const struct sockopt_test *t, + int level) +{ + int new, opt, tmp; + + opt = t->opt; + new = t->new; + + if (bpf_setsockopt(ctx, level, opt, &new, sizeof(new))) + return 1; + + if (bpf_getsockopt(ctx, level, opt, &tmp, sizeof(tmp)) || + tmp != new) { + return 1; + } + + return 0; +} + +static int bpf_test_socket_sockopt(__u32 i, struct loop_ctx *lc) +{ + const struct sockopt_test *t; + + if (i >= ARRAY_SIZE(sol_socket_tests)) + return 1; + + t = &sol_socket_tests[i]; + if (!t->opt) + return 1; + + return bpf_test_sockopt_int(lc->ctx, lc->sk, t, SOL_SOCKET); +} + +static int bpf_test_sockopt(void *ctx, const struct sock *sk) +{ + struct loop_ctx lc = { .ctx = ctx, .sk = sk, }; + int n; + + n = bpf_loop(ARRAY_SIZE(sol_socket_tests), bpf_test_socket_sockopt, &lc, 0); + if (n != ARRAY_SIZE(sol_socket_tests)) + return -1; + + return 0; +} + +static bool bpf_test_delay(struct bpf_sock_ops *skops, const struct sock *sk) +{ + struct bpf_sock_ops_kern *skops_kern; + u64 timestamp = bpf_ktime_get_ns(); + struct skb_shared_info *shinfo; + struct delay_info dinfo = {0}; + struct delay_info *val; + struct sk_buff *skb; + struct sk_stg *stg; + u32 delay, tskey; + u64 prior_ts; + + skops_kern = bpf_cast_to_kern_ctx(skops); + skb = skops_kern->skb; + shinfo = bpf_core_cast(skb->head + skb->end, struct skb_shared_info); + tskey = shinfo->tskey; + if (!tskey) + return false; + + if (skops->op == BPF_SOCK_OPS_TS_TCP_SND_CB) { + stg = bpf_sk_storage_get(&sk_stg_map, (void *)sk, 0, 0); + if (!stg) + return false; + dinfo.sendmsg_ns = stg->sendmsg_ns; + val = &dinfo; + goto out; + } + + val = bpf_map_lookup_elem(&time_map, &tskey); + if (!val) + return false; + + switch (skops->op) { + case BPF_SOCK_OPS_TS_SCHED_OPT_CB: + delay = val->sched_delay = timestamp - val->sendmsg_ns; + break; + case BPF_SOCK_OPS_TS_SW_OPT_CB: + prior_ts = val->sched_delay + val->sendmsg_ns; + delay = val->sw_snd_delay = timestamp - prior_ts; + break; + case BPF_SOCK_OPS_TS_ACK_OPT_CB: + prior_ts = val->sw_snd_delay + val->sched_delay + val->sendmsg_ns; + delay = val->ack_delay = timestamp - prior_ts; + break; + } + + if (delay <= 0 || delay >= delay_tolerance_nsec) + return false; + + /* Since it's the last one, remove from the map after latency check */ + if (skops->op == BPF_SOCK_OPS_TS_ACK_OPT_CB) { + bpf_map_delete_elem(&time_map, &tskey); + return true; + } + +out: + bpf_map_update_elem(&time_map, &tskey, val, BPF_ANY); + return true; +} + +SEC("fentry/tcp_sendmsg_locked") +int BPF_PROG(trace_tcp_sendmsg_locked, struct sock *sk, struct msghdr *msg, size_t size) +{ + u64 timestamp = bpf_ktime_get_ns(); + u32 flag = sk->sk_bpf_cb_flags; + struct sk_stg *stg; + + if (!flag) + return 0; + + stg = bpf_sk_storage_get(&sk_stg_map, sk, 0, + BPF_SK_STORAGE_GET_F_CREATE); + if (!stg) + return 0; + + stg->sendmsg_ns = timestamp; + nr_snd += 1; + return 0; +} + +SEC("sockops") +int skops_sockopt(struct bpf_sock_ops *skops) +{ + struct bpf_sock *bpf_sk = skops->sk; + const struct sock *sk; + + if (!bpf_sk) + return 1; + + sk = (struct sock *)bpf_skc_to_tcp_sock(bpf_sk); + if (!sk) + return 1; + + switch (skops->op) { + case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: + nr_active += !bpf_test_sockopt(skops, sk); + break; + case BPF_SOCK_OPS_TS_TCP_SND_CB: + if (bpf_test_delay(skops, sk)) + nr_snd += 1; + break; + case BPF_SOCK_OPS_TS_SCHED_OPT_CB: + if (bpf_test_delay(skops, sk)) + nr_sched += 1; + break; + case BPF_SOCK_OPS_TS_SW_OPT_CB: + if (bpf_test_delay(skops, sk)) + nr_txsw += 1; + break; + case BPF_SOCK_OPS_TS_ACK_OPT_CB: + if (bpf_test_delay(skops, sk)) + nr_ack += 1; + break; + } + + return 1; +} + +char _license[] SEC("license") = "GPL";