From boote@internet2.edu  Tue Aug 27 10:27:32 2002
Return-Path: <boote@internet2.edu>
Received: from mx1.FreeBSD.org (mx1.FreeBSD.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 694A537B400
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 27 Aug 2002 10:27:32 -0700 (PDT)
Received: from mail.internet2.edu (mail.internet2.edu [209.211.239.218])
	by mx1.FreeBSD.org (Postfix) with ESMTP id C9CC443E6E
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 27 Aug 2002 10:27:31 -0700 (PDT)
	(envelope-from boote@internet2.edu)
Received: from localhost (localhost [127.0.0.1])
	by mail.internet2.edu (Postfix) with ESMTP
	id BFB0B5EE94; Tue, 27 Aug 2002 13:27:30 -0400 (EDT)
Received: from internet2.edu (localhost [127.0.0.1])
	by mail.internet2.edu (Postfix) with ESMTP
	id 872AC5EE85; Tue, 27 Aug 2002 13:27:29 -0400 (EDT)
Message-Id: <3D6BB680.65E122C@internet2.edu>
Date: Tue, 27 Aug 2002 11:27:28 -0600
From: "Jeff W. Boote" <boote@internet2.edu>
Reply-To: "Jeff W. Boote" <boote@internet2.edu>
To: FreeBSD-gnats-submit@freebsd.org
Subject: ntp_gettime returns time in wrong scale

>Number:         42089
>Category:       kern
>Synopsis:       ntp_gettime returns time in wrong scale
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    gnn
>State:          analyzed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Aug 27 10:30:03 PDT 2002
>Closed-Date:    
>Last-Modified:  Sat Jul 28 21:24:39 UTC 2012
>Originator:     Jeff W. Boote
>Release:        FreeBSD 4.5-RELEASE i386
>Organization:
Internet2 - http://www.internet2.edu/
>Environment:
System: FreeBSD cronus.boote.net 4.5-RELEASE FreeBSD 4.5-RELEASE #0: Mon Jan 28
14:31:56 GMT 2002 murray@builder.freebsdmall.com:/usr/src/sys/compile/GENERIC
i386


	
>Description:
	
The fractional component of the ntptimeval.time structure is always currently
returned in nanoseconds from the ntp_gettime system call.  It should be
returned in microseconds if the STA_NANO flag is not set.  (See the
ntp_adjtime() system call.)

I have verified that this is the case in a recent exchange with David Mills.
(the ntp author)


http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=3D651789.CE68D7D%40internet2.edu&rnum=1&prev=/groups%3Fq%3Dfreebsd%2Bntp%2BSTA_NANO%26ie%3DISO-8859-1%26hl%3Den

>How-To-Repeat:
	
	The bug can be demonstrated using the /usr/sbin/ntptime command
	line utility. First call ntptime to put the clock in "microsecond"
	mode:
		% /usr/sbin/ntptime -M	# as root
		(Ignore the timestamps reported this time around because
		they are reported before the actual call to ntp_adjtime().)

	Now, call ntptime again:
		% /usr/sbin/ntptime

	The output should look similar to:
>Fix:

	
The simplest fix would be to hardcode the value of STA_NANO so that it is
always returned from ntp_adjtime()'s timex.status field. However, as Mills
points out, NTPv3 would no longer work then. However - NTPv3 will not currently
work either since NTPv3 only understands microseconds, and FreeBSD is not
currently doing microseconds...

The next "simplest" fix would be to scale the return to microseconds unless
STA_NANO is set. Basically, you would just need to re-include the code from the
original nanokernel distribution from Mills.
>Release-Note:
>Audit-Trail:

From: John Hay <jhay@icomtek.csir.co.za>
To: boote@internet2.edu (Jeff W. Boote)
Cc: FreeBSD-gnats-submit@FreeBSD.ORG
Subject: Re: kern/42089: ntp_gettime returns time in wrong scale
Date: Tue, 27 Aug 2002 21:43:14 +0200 (SAT)

 > 	
 > The fractional component of the ntptimeval.time structure is always currently
 > returned in nanoseconds from the ntp_gettime system call.  It should be
 > returned in microseconds if the STA_NANO flag is not set.  (See the
 > ntp_adjtime() system call.)
 > 
 > I have verified that this is the case in a recent exchange with David Mills.
 > (the ntp author)
 > 
 > 
 > http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=3D651789.CE68D7D%40internet2.edu&rnum=1&prev=/groups%3Fq%3Dfreebsd%2Bntp%2BSTA_NANO%26ie%3DISO-8859-1%26hl%3Den
 
 That is not exactly what I read in that thread.
 
 > The next "simplest" fix would be to scale the return to microseconds unless
 > STA_NANO is set. Basically, you would just need to re-include the code from the
 > original nanokernel distribution from Mills.
 
 You are missing the part where Dave said that on FreeBSD NTP_NANO should
 be defined. The code you are talking about looks like this:
 
 #ifdef NTP_NANO
         ntv.time.tv_sec = atv.tv_sec;
         ntv.time.tv_nsec = atv.tv_nsec;
 #else
         if (!(time_status & STA_NANO))
                 atv.tv_nsec /= 1000;
         ntv.time.tv_sec = atv.tv_sec;
         ntv.time.tv_usec = atv.tv_nsec;
 #endif /* NTP_NANO */
 
 But if NTP_NANO is defined we will end up with exactly the code that is
 currently in FreeBSD or do I miss something?
 
 Why don't you rather determine if the time element in struct ntptimeval
 is of type struct timespec or struct timeval?
 
 John
 -- 
 John Hay -- John.Hay@icomtek.csir.co.za / jhay@FreeBSD.org
Responsible-Changed-From-To: freebsd-bugs->roberto 
Responsible-Changed-By: kris 
Responsible-Changed-When: Fri Jul 18 15:15:53 PDT 2003 
Responsible-Changed-Why:  
Assign to ntp maintainer 

http://www.freebsd.org/cgi/query-pr.cgi?pr=42089 
Responsible-Changed-From-To: roberto->phk 
Responsible-Changed-By: roberto 
Responsible-Changed-When: Mon Sep 1 13:39:48 PDT 2003 
Responsible-Changed-Why:  
Poul-Henning knows the kernel part of ntp far more than me. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=42089 
Responsible-Changed-From-To: phk->freebsd-bugs 
Responsible-Changed-By: eadler 
Responsible-Changed-When: Sat Sep 24 17:44:12 UTC 2011 
Responsible-Changed-Why:  
return to the pool (approved by phk) 

http://www.freebsd.org/cgi/query-pr.cgi?pr=42089 
State-Changed-From-To: open->analyzed 
State-Changed-By: gnn 
State-Changed-When: Sat Jul 28 21:20:28 UTC 2012 
State-Changed-Why:  
I don't think this should work at all. The flag mentioned is read only. 


Responsible-Changed-From-To: freebsd-bugs->gnn 
Responsible-Changed-By: gnn 
Responsible-Changed-When: Sat Jul 28 21:20:28 UTC 2012 
Responsible-Changed-Why:  
I don't think this should work at all. The flag mentioned is read only. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=42089 
>Unformatted:
 >cronus# ntptime
 >ntp_gettime() returns code 0 (OK)
 >  time c11532aa.adff5000  Mon, Aug 26 2002 17:05:14.679, (.141188861),
 >  maximum error 675321 us, estimated error 6559 us, TAI offset 0
 >ntp_adjtime() returns code 0 (OK)
 >  modes 0x0 (),
 >  offset 3975.000 us, frequency 83.724 ppm, interval 1 s,
 >  maximum error 675321 us, estimated error 6559 us,
 >  status 0x1 (PLL),
 >  time constant 8, precision 0.000 us, tolerance 496 ppm,
 
 You will notice that in the date stamp the time is listed as:
 	17:05:14.679 - so the fractional portion is reported as .679
 from the "prettydate" function.  However, the fractional part being
 reported directly from ntp_gettime in the ()'s is .141188861
 
 It is overflowing in prettydate because "prettydate" expects usecs, and
 ntp_gettime is reporting nanoseconds. This is easy to see if you look
 at /usr/src/sys/kern/kern_ntptime.c - it calls nanotime() to return this value.
 
