From patchwork Mon Jan 2 05:04:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arun Ramadoss X-Patchwork-Id: 38000 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp4019350wrt; Sun, 1 Jan 2023 21:11:38 -0800 (PST) X-Google-Smtp-Source: AMrXdXsAEu5084j8WGSHirf3XMqm7J2RnS7TQZrK191qEF66+7kxdvcvmFkqunvKxYag+rCBAURv X-Received: by 2002:a17:906:99d6:b0:7c1:7c3a:5c65 with SMTP id s22-20020a17090699d600b007c17c3a5c65mr34305634ejn.4.1672636297939; Sun, 01 Jan 2023 21:11:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672636297; cv=none; d=google.com; s=arc-20160816; b=ZxKkZrtLsf3UWirFKm/0E+H6yDM5WF4vu6x5OYtCSTCFeM2jOWlPE9NebQem5cYgo3 ZOZUh5Is92gO6CWTBrGgVDer5aBOnBwi3OKGt3sJa3w79AUkLbpLvo+37m48J9uK1nZD JEK5l/kN14jbAZVNTqIA9RX97Zcx4hJ1TwwvCImuVAGmbNYiC6MEoP6dBmDQmJ36VS4v k1WK6RYGQ69RLD/kMnna108fSvq64M4gVjHF5bR1KA4YZQFjTedK2Y1let2ebbMn0/sv 7GCYbqWzVVbw73RhJgPlkvOs2tfesjpn/QOWoTNtGvqT6nzMaEcFGBiL0P/6I71KucTE VMVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Guv9i3IBMAcyy6vGd2Rh2K7r9Sr+srDgZmP+tXa6fOo=; b=r2RhxLbwTqSA9FAH56mwxs5ogg03hvL1Wr2A3Rt6VutBbabj8IztDphgmH/rsgK/J5 OT+GhSX3J15pdUL3ARiK8Lf/BI0djCg52XbcUAdgb0b9+BoFPWNOmnBTMO+0TvwavzoC Xug7PF4f+wDx6Ssf866kNej4RUYGFY/sxLaMUa8Wz5gXQoDZF6D0h0QLRy8cQaUuK+tb 1JF2skS0IdlQnf0aZ833phV5svrJForHk+o1JiHPCfV/N6Y74Bi8ChHJbaGG9igei19o PZRIzQeNr89XCu3lJPf5MGamvIdofnTpn3yDckqPLEH8Pz0JJqCcxgbPOnVLVf24zXYQ eUdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microchip.com header.s=mchp header.b=IR9ZrirL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qf1-20020a1709077f0100b007c18928418bsi26276491ejc.158.2023.01.01.21.11.14; Sun, 01 Jan 2023 21:11:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@microchip.com header.s=mchp header.b=IR9ZrirL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230206AbjABFHF (ORCPT + 99 others); Mon, 2 Jan 2023 00:07:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230271AbjABFGt (ORCPT ); Mon, 2 Jan 2023 00:06:49 -0500 Received: from esa.microchip.iphmx.com (esa.microchip.iphmx.com [68.232.154.123]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9736F1CF; Sun, 1 Jan 2023 21:06:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1672636005; x=1704172005; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=TBiocUlMd4lgD3UieKC9lMwBVyukIc3JWqKRD/9Iuek=; b=IR9ZrirLIW/yOOFhEi6ve/hPu+7OrdJ2eU8YG7g6+hOhhNHvovQMbGlP UeCQQXqJzSc36jN8aj7gZnK+NIOZnXIUUwqDlYLq1TnmhFOsTtRTYlqrU tD6gYxAKCmpqeblPd3qfwhaIuu1IDrR7d0PX4724gTH/V8I3yfzigIxbW TlkUNe0pwGa04p55OmoqrsZOWGBIhMmjmyPw0DLOAyzGHNoSQ0hDkkbUI lTuZo0Y7IOiJgZhYrlIH3jknZ6xW1+LzewuiEErQAbkJujrAdL/S59+DZ gbnvY831gQq0OwiXoLqTHLaZjof1mbzqKqlFfqMI00+xCW9Jg1kLKLY16 w==; X-IronPort-AV: E=Sophos;i="5.96,293,1665471600"; d="scan'208";a="190347482" Received: from unknown (HELO email.microchip.com) ([170.129.1.10]) by esa4.microchip.iphmx.com with ESMTP/TLS/AES256-SHA256; 01 Jan 2023 22:06:45 -0700 Received: from chn-vm-ex04.mchp-main.com (10.10.85.152) by chn-vm-ex02.mchp-main.com (10.10.85.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.16; Sun, 1 Jan 2023 22:06:44 -0700 Received: from CHE-LT-I17769U.microchip.com (10.10.115.15) by chn-vm-ex04.mchp-main.com (10.10.85.152) with Microsoft SMTP Server id 15.1.2507.16 via Frontend Transport; Sun, 1 Jan 2023 22:06:38 -0700 From: Arun Ramadoss To: , CC: , , , , , , , , , , , , , , Subject: [Patch net-next v6 06/13] net: ptp: add helper for one-step P2P clocks Date: Mon, 2 Jan 2023 10:34:52 +0530 Message-ID: <20230102050459.31023-7-arun.ramadoss@microchip.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20230102050459.31023-1-arun.ramadoss@microchip.com> References: <20230102050459.31023-1-arun.ramadoss@microchip.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1753886278950543013?= X-GMAIL-MSGID: =?utf-8?q?1753886278950543013?= From: Christian Eggers For P2P delay measurement, the ingress time stamp of the PDelay_Req is required for the correction field of the PDelay_Resp. The application echoes back the correction field of the PDelay_Req when sending the PDelay_Resp. Some hardware (like the ZHAW InES PTP time stamping IP core) subtracts the ingress timestamp autonomously from the correction field, so that the hardware only needs to add the egress timestamp on tx. Other hardware (like the Microchip KSZ9563) reports the ingress time stamp via an interrupt and requires that the software provides this time stamp via tail-tag on tx. In order to avoid introducing a further application interface for this, the driver can simply emulate the behavior of the InES device and subtract the ingress time stamp in software from the correction field. On egress, the correction field can either be kept as it is (and the time stamp field in the tail-tag is set to zero) or move the value from the correction field back to the tail-tag. Changing the correction field requires updating the UDP checksum (if UDP is used as transport). Signed-off-by: Christian Eggers Co-developed-by: Arun Ramadoss Signed-off-by: Arun Ramadoss --- v1 -> v2 - Fixed compilation issue when PTP_CLASSIFY not selected in menuconfig as reported by kernel test robot --- include/linux/ptp_classify.h | 71 ++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/include/linux/ptp_classify.h b/include/linux/ptp_classify.h index 2b6ea36ad162..6e5869c2504c 100644 --- a/include/linux/ptp_classify.h +++ b/include/linux/ptp_classify.h @@ -10,8 +10,12 @@ #ifndef _PTP_CLASSIFY_H_ #define _PTP_CLASSIFY_H_ +#include #include +#include #include +#include +#include #define PTP_CLASS_NONE 0x00 /* not a PTP event message */ #define PTP_CLASS_V1 0x01 /* protocol version 1 */ @@ -129,6 +133,67 @@ static inline u8 ptp_get_msgtype(const struct ptp_header *hdr, return msgtype; } +/** + * ptp_check_diff8 - Computes new checksum (when altering a 64-bit field) + * @old: old field value + * @new: new field value + * @oldsum: previous checksum + * + * This function can be used to calculate a new checksum when only a single + * field is changed. Similar as ip_vs_check_diff*() in ip_vs.h. + * + * Return: Updated checksum + */ +static inline __wsum ptp_check_diff8(__be64 old, __be64 new, __wsum oldsum) +{ + __be64 diff[2] = { ~old, new }; + + return csum_partial(diff, sizeof(diff), oldsum); +} + +/** + * ptp_header_update_correction - Update PTP header's correction field + * @skb: packet buffer + * @type: type of the packet (see ptp_classify_raw()) + * @hdr: ptp header + * @correction: new correction value + * + * This updates the correction field of a PTP header and updates the UDP + * checksum (if UDP is used as transport). It is needed for hardware capable of + * one-step P2P that does not already modify the correction field of Pdelay_Req + * event messages on ingress. + */ +static inline +void ptp_header_update_correction(struct sk_buff *skb, unsigned int type, + struct ptp_header *hdr, s64 correction) +{ + __be64 correction_old; + struct udphdr *uhdr; + + /* previous correction value is required for checksum update. */ + memcpy(&correction_old, &hdr->correction, sizeof(correction_old)); + + /* write new correction value */ + put_unaligned_be64((u64)correction, &hdr->correction); + + switch (type & PTP_CLASS_PMASK) { + case PTP_CLASS_IPV4: + case PTP_CLASS_IPV6: + /* locate udp header */ + uhdr = (struct udphdr *)((char *)hdr - sizeof(struct udphdr)); + break; + default: + return; + } + + /* update checksum */ + uhdr->check = csum_fold(ptp_check_diff8(correction_old, + hdr->correction, + ~csum_unfold(uhdr->check))); + if (!uhdr->check) + uhdr->check = CSUM_MANGLED_0; +} + /** * ptp_msg_is_sync - Evaluates whether the given skb is a PTP Sync message * @skb: packet buffer @@ -166,5 +231,11 @@ static inline bool ptp_msg_is_sync(struct sk_buff *skb, unsigned int type) { return false; } + +static inline +void ptp_header_update_correction(struct sk_buff *skb, unsigned int type, + struct ptp_header *hdr, s64 correction) +{ +} #endif #endif /* _PTP_CLASSIFY_H_ */