Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp454031pxb; Thu, 12 Nov 2020 07:50:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJw4OsxNyAyU1Z8eFwf0t5INLn7DrhNLA+Bnk5MA/LhBL9awdHOsPDWx2YEzptqp5gMJQOXG X-Received: by 2002:a17:906:6c93:: with SMTP id s19mr32186793ejr.544.1605196239338; Thu, 12 Nov 2020 07:50:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605196239; cv=none; d=google.com; s=arc-20160816; b=rBSLcx/Eu/B2Gma58XlfyXMt7EnOhIpC1PmiXKfj00b4rfyMmM+U02lkUfsvX6teHh ICPXBYApGbAgWjRAN+aI+KWUsh4LYKWNqWIe5RvWOVK7mwiofa2MsAy9spnOvz0hJVuT Yoo9QYVC3TcVDxjGIAOnb6bVTQgtDNHeKD2Zue5LDCG18JG56gRdb3OCSNBWm0QpBhzL w1oT8Tv25KwxhaNnH3+SP+DyHu/+uM4eI3MOBK0BHsnPvEgL/vCu3YKxmwJVYirk4qlV g1Vzr6bzR0aH7R6MoBSgrE0sMvW1Tt1xJ93z63cYrm6WzVDDP5HKz9znL1cCFvkXQz7u aSiQ== 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; bh=BuqtdES72mfdj8Jz0kfNdLNniTlAOwZ7ld1XjHPLUKk=; b=qWrfcWfXEEcOJP8tssDegwDZatXq4Api4dKYj4S9Hb+G6CK3gbbaGRguwijLMA3PCK q1CDdNKKc9Yy7prdmAceEsLgmkDcamUpQr5wWVpY/oDl0x3C6lZdtYXQQK3MkYnljtCD d4rNXKiCBSIJWikTZdP8LpqNKUZxYfBvBfFi4yBBie1RDR8tLb5ElXd/DfR3S+A6gN3A KmPQMBBLVTpn+IkUyaTimGRJJEses44rTeD0KY27JDtsb18VSOSZ1SV8YR1ya6wELw1I 5RhdjEDzoKP7jNX7kcKMiuyjGar4hLesa7AdCUMpDyjUYmTI1NT5B928v+h9WJ0PgM5v eqEg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j3si4277033edh.611.2020.11.12.07.50.16; Thu, 12 Nov 2020 07:50:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728439AbgKLPsx (ORCPT + 99 others); Thu, 12 Nov 2020 10:48:53 -0500 Received: from mailout11.rmx.de ([94.199.88.76]:54152 "EHLO mailout11.rmx.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728416AbgKLPsx (ORCPT ); Thu, 12 Nov 2020 10:48:53 -0500 Received: from kdin01.retarus.com (kdin01.dmz1.retloc [172.19.17.48]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mailout11.rmx.de (Postfix) with ESMTPS id 4CX5cG0Xfjz41lg; Thu, 12 Nov 2020 16:48:46 +0100 (CET) Received: from mta.arri.de (unknown [217.111.95.66]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by kdin01.retarus.com (Postfix) with ESMTPS id 4CX5Zh6hgnz2xCK; Thu, 12 Nov 2020 16:47:24 +0100 (CET) Received: from N95HX1G2.wgnetz.xx (192.168.54.59) by mta.arri.de (192.168.100.104) with Microsoft SMTP Server (TLS) id 14.3.487.0; Thu, 12 Nov 2020 16:40:21 +0100 From: Christian Eggers To: Vladimir Oltean , Jakub Kicinski , Andrew Lunn , Richard Cochran , "Rob Herring" CC: Vivien Didelot , "David S . Miller" , Kurt Kanzenbach , George McCollister , Marek Vasut , Helmut Grohne , Paul Barker , Codrin Ciubotariu , Tristram Ha , Woojung Huh , Microchip Linux Driver Support , Christian Eggers , , , Subject: [PATCH net-next v2 08/11] net: ptp: add helper for one-step P2P clocks Date: Thu, 12 Nov 2020 16:35:34 +0100 Message-ID: <20201112153537.22383-9-ceggers@arri.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201112153537.22383-1-ceggers@arri.de> References: <20201112153537.22383-1-ceggers@arri.de> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [192.168.54.59] X-RMX-ID: 20201112-164726-4CX5Zh6hgnz2xCK-0@kdin01 X-RMX-SOURCE: 217.111.95.66 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This function subtracts the ingress hardware time stamp from 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. Signed-off-by: Christian Eggers --- include/linux/ptp_classify.h | 97 ++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/include/linux/ptp_classify.h b/include/linux/ptp_classify.h index 56b2d7d66177..f27b512e1abd 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 */ @@ -118,6 +122,91 @@ 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_onestep_p2p_move_t2_to_correction - Update PTP header's correction field + * @skb: packet buffer + * @type: type of the packet (see ptp_classify_raw()) + * @hdr: ptp header + * @t2: ingress hardware time stamp + * + * This function subtracts the ingress hardware time stamp from 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_onestep_p2p_move_t2_to_correction(struct sk_buff *skb, + unsigned int type, + struct ptp_header *hdr, + ktime_t t2) +{ + u8 *ptr = skb_mac_header(skb); + struct udphdr *uhdr = NULL; + s64 ns = ktime_to_ns(t2); + __be64 correction_old; + s64 correction; + + /* previous correction value is required for checksum update. */ + memcpy(&correction_old, &hdr->correction, sizeof(correction_old)); + correction = (s64)be64_to_cpu(correction_old); + + /* PTP correction field consists of 32 bit nanoseconds and 16 bit + * fractional nanoseconds. Avoid shifting negative numbers. + */ + if (ns >= 0) + correction -= ns << 16; + else + correction += -ns << 16; + + /* write new correction value */ + put_unaligned_be64((u64)correction, &hdr->correction); + + /* locate udp header */ + if (type & PTP_CLASS_VLAN) + ptr += VLAN_HLEN; + + ptr += ETH_HLEN; + + switch (type & PTP_CLASS_PMASK) { + case PTP_CLASS_IPV4: + ptr += ((struct iphdr *)ptr)->ihl << 2; + uhdr = (struct udphdr *)ptr; + break; + case PTP_CLASS_IPV6: + ptr += IP6_HLEN; + uhdr = (struct udphdr *)ptr; + break; + } + + if (!uhdr) + 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; +} + void __init ptp_classifier_init(void); #else static inline void ptp_classifier_init(void) @@ -140,5 +229,13 @@ static inline u8 ptp_get_msgtype(const struct ptp_header *hdr, */ return 0; } + +static inline +void ptp_onestep_p2p_move_t2_to_correction(struct sk_buff *skb, + unsigned int type, + struct ptp_header *hdr, + ktime_t t2) +{ +} #endif #endif /* _PTP_CLASSIFY_H_ */ -- Christian Eggers Embedded software developer Arnold & Richter Cine Technik GmbH & Co. Betriebs KG Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918 Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477 Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler