kern_time.c revision 114980
1116742Ssam/*
2116904Ssam * Copyright (c) 1982, 1986, 1989, 1993
3139530Ssam *	The Regents of the University of California.  All rights reserved.
4116742Ssam *
5116742Ssam * Redistribution and use in source and binary forms, with or without
6116742Ssam * modification, are permitted provided that the following conditions
7116742Ssam * are met:
8116742Ssam * 1. Redistributions of source code must retain the above copyright
9116742Ssam *    notice, this list of conditions and the following disclaimer.
10116742Ssam * 2. Redistributions in binary form must reproduce the above copyright
11116742Ssam *    notice, this list of conditions and the following disclaimer in the
12116742Ssam *    documentation and/or other materials provided with the distribution.
13116742Ssam * 3. All advertising materials mentioning features or use of this software
14116904Ssam *    must display the following acknowledgement:
15116904Ssam *	This product includes software developed by the University of
16116742Ssam *	California, Berkeley and its contributors.
17116904Ssam * 4. Neither the name of the University nor the names of its contributors
18116904Ssam *    may be used to endorse or promote products derived from this software
19116904Ssam *    without specific prior written permission.
20116742Ssam *
21116904Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22116904Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23116904Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24116904Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25116904Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26116904Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27116904Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28116904Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29116904Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30116904Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31116904Ssam * SUCH DAMAGE.
32116742Ssam *
33116742Ssam *	@(#)kern_time.c	8.1 (Berkeley) 6/10/93
34116742Ssam * $FreeBSD: head/sys/kern/kern_time.c 114980 2003-05-13 19:21:46Z jhb $
35116742Ssam */
36116742Ssam
37138568Ssam#include "opt_mac.h"
38116742Ssam
39138568Ssam#include <sys/param.h>
40138568Ssam#include <sys/systm.h>
41138568Ssam#include <sys/lock.h>
42138568Ssam#include <sys/mutex.h>
43138568Ssam#include <sys/sysproto.h>
44138568Ssam#include <sys/resourcevar.h>
45138568Ssam#include <sys/signalvar.h>
46138568Ssam#include <sys/kernel.h>
47138568Ssam#include <sys/mac.h>
48138568Ssam#include <sys/sysent.h>
49138568Ssam#include <sys/proc.h>
50138568Ssam#include <sys/time.h>
51138568Ssam#include <sys/timetc.h>
52138568Ssam#include <sys/vnode.h>
53138568Ssam
54138568Ssam#include <vm/vm.h>
55138568Ssam#include <vm/vm_extern.h>
56138568Ssam
57138568Ssamint tz_minuteswest;
58138568Ssamint tz_dsttime;
59138568Ssam
60138568Ssam/*
61138568Ssam * Time of day and interval timer support.
62138568Ssam *
63116742Ssam * These routines provide the kernel entry points to get and set
64116742Ssam * the time-of-day and per-process interval timers.  Subroutines
65116742Ssam * here provide support for adding and subtracting timeval structures
66138568Ssam * and decrementing interval timers, optionally reloading the interval
67138568Ssam * timers when they expire.
68116742Ssam */
69138568Ssam
70138568Ssamstatic int	nanosleep1(struct thread *td, struct timespec *rqt,
71138568Ssam		    struct timespec *rmt);
72138568Ssamstatic int	settime(struct thread *, struct timeval *);
73138568Ssamstatic void	timevalfix(struct timeval *);
74138568Ssamstatic void	no_lease_updatetime(int);
75138568Ssam
76138568Ssamstatic void
77138568Ssamno_lease_updatetime(deltat)
78116742Ssam	int deltat;
79116742Ssam{
80138568Ssam}
81138568Ssam
82138568Ssamvoid (*lease_updatetime)(int)  = no_lease_updatetime;
83116742Ssam
84116742Ssamstatic int
85116742Ssamsettime(struct thread *td, struct timeval *tv)
86116742Ssam{
87116742Ssam	struct timeval delta, tv1, tv2;
88116742Ssam	static struct timeval maxtime, laststep;
89116742Ssam	struct timespec ts;
90138568Ssam	int s;
91138568Ssam
92116742Ssam	s = splclock();
93116742Ssam	microtime(&tv1);
94116742Ssam	delta = *tv;
95120483Ssam	timevalsub(&delta, &tv1);
96138568Ssam
97138568Ssam	/*
98138568Ssam	 * If the system is secure, we do not allow the time to be
99138568Ssam	 * set to a value earlier than 1 second less than the highest
100138568Ssam	 * time we have yet seen. The worst a miscreant can do in
101138568Ssam	 * this circumstance is "freeze" time. He couldn't go
102138568Ssam	 * back to the past.
103138568Ssam	 *
104138568Ssam	 * We similarly do not allow the clock to be stepped more
105138568Ssam	 * than one second, nor more than once per second. This allows
106138568Ssam	 * a miscreant to make the clock march double-time, but no worse.
107138568Ssam	 */
108138568Ssam	if (securelevel_gt(td->td_ucred, 1) != 0) {
109138568Ssam		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
110138568Ssam			/*
111138568Ssam			 * Update maxtime to latest time we've seen.
112138568Ssam			 */
113138568Ssam			if (tv1.tv_sec > maxtime.tv_sec)
114138568Ssam				maxtime = tv1;
115116742Ssam			tv2 = *tv;
116116742Ssam			timevalsub(&tv2, &maxtime);
117119150Ssam			if (tv2.tv_sec < -1) {
118116742Ssam				tv->tv_sec = maxtime.tv_sec - 1;
119116742Ssam				printf("Time adjustment clamped to -1 second\n");
120116742Ssam			}
121116742Ssam		} else {
122116742Ssam			if (tv1.tv_sec == laststep.tv_sec) {
123116742Ssam				splx(s);
124116742Ssam				return (EPERM);
125138568Ssam			}
126138568Ssam			if (delta.tv_sec > 1) {
127138568Ssam				tv->tv_sec = tv1.tv_sec + 1;
128138568Ssam				printf("Time adjustment clamped to +1 second\n");
129116742Ssam			}
130116742Ssam			laststep = *tv;
131116742Ssam		}
132116742Ssam	}
133116742Ssam
134116742Ssam	ts.tv_sec = tv->tv_sec;
135116742Ssam	ts.tv_nsec = tv->tv_usec * 1000;
136116742Ssam	mtx_lock(&Giant);
137138568Ssam	tc_setclock(&ts);
138138568Ssam	(void) splsoftclock();
139116742Ssam	lease_updatetime(delta.tv_sec);
140116742Ssam	splx(s);
141116742Ssam	resettodr();
142138568Ssam	mtx_unlock(&Giant);
143138568Ssam	return (0);
144116742Ssam}
145138568Ssam
146138568Ssam#ifndef _SYS_SYSPROTO_H_
147116742Ssamstruct clock_gettime_args {
148138568Ssam	clockid_t clock_id;
149116742Ssam	struct	timespec *tp;
150138568Ssam};
151138568Ssam#endif
152138568Ssam
153138568Ssam/*
154138568Ssam * MPSAFE
155138568Ssam */
156116742Ssam/* ARGSUSED */
157116742Ssamint
158116742Ssamclock_gettime(struct thread *td, struct clock_gettime_args *uap)
159138568Ssam{
160116742Ssam	struct timespec ats;
161116742Ssam
162116742Ssam	if (uap->clock_id == CLOCK_REALTIME)
163116742Ssam		nanotime(&ats);
164116742Ssam	else if (uap->clock_id == CLOCK_MONOTONIC)
165116742Ssam		nanouptime(&ats);
166138568Ssam	else
167116742Ssam		return (EINVAL);
168116742Ssam	return (copyout(&ats, uap->tp, sizeof(ats)));
169116742Ssam}
170116742Ssam
171116742Ssam#ifndef _SYS_SYSPROTO_H_
172144618Ssamstruct clock_settime_args {
173144618Ssam	clockid_t clock_id;
174144618Ssam	const struct	timespec *tp;
175127877Ssam};
176138568Ssam#endif
177138568Ssam
178138568Ssam/*
179138568Ssam * MPSAFE
180138568Ssam */
181116742Ssam/* ARGSUSED */
182144618Ssamint
183116742Ssamclock_settime(struct thread *td, struct clock_settime_args *uap)
184144618Ssam{
185138568Ssam	struct timeval atv;
186138568Ssam	struct timespec ats;
187144618Ssam	int error;
188144618Ssam
189144618Ssam#ifdef MAC
190144618Ssam	error = mac_check_system_settime(td->td_ucred);
191144618Ssam	if (error)
192144618Ssam		return (error);
193144618Ssam#endif
194144618Ssam	if ((error = suser(td)) != 0)
195144618Ssam		return (error);
196138568Ssam	if (uap->clock_id != CLOCK_REALTIME)
197138568Ssam		return (EINVAL);
198138568Ssam	if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
199138568Ssam		return (error);
200138568Ssam	if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
201138568Ssam		return (EINVAL);
202138568Ssam	/* XXX Don't convert nsec->usec and back */
203138568Ssam	TIMESPEC_TO_TIMEVAL(&atv, &ats);
204138568Ssam	error = settime(td, &atv);
205138568Ssam	return (error);
206138568Ssam}
207138568Ssam
208138568Ssam#ifndef _SYS_SYSPROTO_H_
209138568Ssamstruct clock_getres_args {
210138568Ssam	clockid_t clock_id;
211138568Ssam	struct	timespec *tp;
212138568Ssam};
213138568Ssam#endif
214138568Ssam
215138568Ssamint
216144618Ssamclock_getres(struct thread *td, struct clock_getres_args *uap)
217138568Ssam{
218144618Ssam	struct timespec ts;
219138568Ssam	int error;
220144618Ssam
221138568Ssam	if (uap->clock_id != CLOCK_REALTIME)
222138568Ssam		return (EINVAL);
223144618Ssam	error = 0;
224138568Ssam	if (uap->tp) {
225144618Ssam		ts.tv_sec = 0;
226138568Ssam		/*
227138568Ssam		 * Round up the result of the division cheaply by adding 1.
228144618Ssam		 * Rounding up is especially important if rounding down
229138568Ssam		 * would give 0.  Perfect rounding is unimportant.
230138568Ssam		 */
231144618Ssam		ts.tv_nsec = 1000000000 / tc_getfrequency() + 1;
232138568Ssam		error = copyout(&ts, uap->tp, sizeof(ts));
233138568Ssam	}
234144618Ssam	return (error);
235138568Ssam}
236138568Ssam
237144618Ssamstatic int nanowait;
238138568Ssam
239138568Ssamstatic int
240138568Ssamnanosleep1(struct thread *td, struct timespec *rqt, struct timespec *rmt)
241138568Ssam{
242138568Ssam	struct timespec ts, ts2, ts3;
243138568Ssam	struct timeval tv;
244138568Ssam	int error;
245138568Ssam
246138568Ssam	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
247138568Ssam		return (EINVAL);
248138568Ssam	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
249138568Ssam		return (0);
250138568Ssam	getnanouptime(&ts);
251138568Ssam	timespecadd(&ts, rqt);
252138568Ssam	TIMESPEC_TO_TIMEVAL(&tv, rqt);
253138568Ssam	for (;;) {
254144618Ssam		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
255144618Ssam		    tvtohz(&tv));
256138568Ssam		getnanouptime(&ts2);
257144618Ssam		if (error != EWOULDBLOCK) {
258138568Ssam			if (error == ERESTART)
259144618Ssam				error = EINTR;
260138568Ssam			if (rmt != NULL) {
261144618Ssam				timespecsub(&ts, &ts2);
262138568Ssam				if (ts.tv_sec < 0)
263138568Ssam					timespecclear(&ts);
264144618Ssam				*rmt = ts;
265138568Ssam			}
266138568Ssam			return (error);
267138568Ssam		}
268138568Ssam		if (timespeccmp(&ts2, &ts, >=))
269116742Ssam			return (0);
270144618Ssam		ts3 = ts;
271116742Ssam		timespecsub(&ts3, &ts2);
272116742Ssam		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
273144618Ssam	}
274138568Ssam}
275144618Ssam
276138568Ssam#ifndef _SYS_SYSPROTO_H_
277144618Ssamstruct nanosleep_args {
278144618Ssam	struct	timespec *rqtp;
279144618Ssam	struct	timespec *rmtp;
280144618Ssam};
281144618Ssam#endif
282116742Ssam
283/*
284 * MPSAFE
285 */
286/* ARGSUSED */
287int
288nanosleep(struct thread *td, struct nanosleep_args *uap)
289{
290	struct timespec rmt, rqt;
291	int error;
292
293	error = copyin(uap->rqtp, &rqt, sizeof(rqt));
294	if (error)
295		return (error);
296
297	if (uap->rmtp &&
298	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
299			return (EFAULT);
300	error = nanosleep1(td, &rqt, &rmt);
301	if (error && uap->rmtp) {
302		int error2;
303
304		error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
305		if (error2)
306			error = error2;
307	}
308	return (error);
309}
310
311#ifndef _SYS_SYSPROTO_H_
312struct gettimeofday_args {
313	struct	timeval *tp;
314	struct	timezone *tzp;
315};
316#endif
317/*
318 * MPSAFE
319 */
320/* ARGSUSED */
321int
322gettimeofday(struct thread *td, struct gettimeofday_args *uap)
323{
324	struct timeval atv;
325	struct timezone rtz;
326	int error = 0;
327
328	if (uap->tp) {
329		microtime(&atv);
330		error = copyout(&atv, uap->tp, sizeof (atv));
331	}
332	if (error == 0 && uap->tzp != NULL) {
333		rtz.tz_minuteswest = tz_minuteswest;
334		rtz.tz_dsttime = tz_dsttime;
335		error = copyout(&rtz, uap->tzp, sizeof (rtz));
336	}
337	return (error);
338}
339
340#ifndef _SYS_SYSPROTO_H_
341struct settimeofday_args {
342	struct	timeval *tv;
343	struct	timezone *tzp;
344};
345#endif
346/*
347 * MPSAFE
348 */
349/* ARGSUSED */
350int
351settimeofday(struct thread *td, struct settimeofday_args *uap)
352{
353	struct timeval atv;
354	struct timezone atz;
355	int error = 0;
356
357#ifdef MAC
358	error = mac_check_system_settime(td->td_ucred);
359	if (error)
360		return (error);
361#endif
362	if ((error = suser(td)))
363		return (error);
364	/* Verify all parameters before changing time. */
365	if (uap->tv) {
366		if ((error = copyin(uap->tv, &atv, sizeof(atv))))
367			return (error);
368		if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
369			return (EINVAL);
370	}
371	if (uap->tzp &&
372	    (error = copyin(uap->tzp, &atz, sizeof(atz))))
373		return (error);
374
375	if (uap->tv && (error = settime(td, &atv)))
376		return (error);
377	if (uap->tzp) {
378		tz_minuteswest = atz.tz_minuteswest;
379		tz_dsttime = atz.tz_dsttime;
380	}
381	return (error);
382}
383/*
384 * Get value of an interval timer.  The process virtual and
385 * profiling virtual time timers are kept in the p_stats area, since
386 * they can be swapped out.  These are kept internally in the
387 * way they are specified externally: in time until they expire.
388 *
389 * The real time interval timer is kept in the process table slot
390 * for the process, and its value (it_value) is kept as an
391 * absolute time rather than as a delta, so that it is easy to keep
392 * periodic real-time signals from drifting.
393 *
394 * Virtual time timers are processed in the hardclock() routine of
395 * kern_clock.c.  The real time timer is processed by a timeout
396 * routine, called from the softclock() routine.  Since a callout
397 * may be delayed in real time due to interrupt processing in the system,
398 * it is possible for the real time timeout routine (realitexpire, given below),
399 * to be delayed in real time past when it is supposed to occur.  It
400 * does not suffice, therefore, to reload the real timer .it_value from the
401 * real time timers .it_interval.  Rather, we compute the next time in
402 * absolute time the timer should go off.
403 */
404#ifndef _SYS_SYSPROTO_H_
405struct getitimer_args {
406	u_int	which;
407	struct	itimerval *itv;
408};
409#endif
410/*
411 * MPSAFE
412 */
413int
414getitimer(struct thread *td, struct getitimer_args *uap)
415{
416	struct proc *p = td->td_proc;
417	struct timeval ctv;
418	struct itimerval aitv;
419
420	if (uap->which > ITIMER_PROF)
421		return (EINVAL);
422
423	if (uap->which == ITIMER_REAL) {
424		/*
425		 * Convert from absolute to relative time in .it_value
426		 * part of real time timer.  If time for real time timer
427		 * has passed return 0, else return difference between
428		 * current time and time for the timer to go off.
429		 */
430		PROC_LOCK(p);
431		aitv = p->p_realtimer;
432		PROC_UNLOCK(p);
433		if (timevalisset(&aitv.it_value)) {
434			getmicrouptime(&ctv);
435			if (timevalcmp(&aitv.it_value, &ctv, <))
436				timevalclear(&aitv.it_value);
437			else
438				timevalsub(&aitv.it_value, &ctv);
439		}
440	} else {
441		mtx_lock_spin(&sched_lock);
442		aitv = p->p_stats->p_timer[uap->which];
443		mtx_unlock_spin(&sched_lock);
444	}
445	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
446}
447
448#ifndef _SYS_SYSPROTO_H_
449struct setitimer_args {
450	u_int	which;
451	struct	itimerval *itv, *oitv;
452};
453#endif
454/*
455 * MPSAFE
456 */
457int
458setitimer(struct thread *td, struct setitimer_args *uap)
459{
460	struct proc *p = td->td_proc;
461	struct itimerval aitv, oitv;
462	struct timeval ctv;
463	int error;
464
465	if (uap->itv == NULL) {
466		uap->itv = uap->oitv;
467		return (getitimer(td, (struct getitimer_args *)uap));
468	}
469
470	if (uap->which > ITIMER_PROF)
471		return (EINVAL);
472	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
473		return (error);
474	if (itimerfix(&aitv.it_value))
475		return (EINVAL);
476	if (!timevalisset(&aitv.it_value))
477		timevalclear(&aitv.it_interval);
478	else if (itimerfix(&aitv.it_interval))
479		return (EINVAL);
480
481	if (uap->which == ITIMER_REAL) {
482		PROC_LOCK(p);
483		if (timevalisset(&p->p_realtimer.it_value))
484			callout_stop(&p->p_itcallout);
485		getmicrouptime(&ctv);
486		if (timevalisset(&aitv.it_value)) {
487			callout_reset(&p->p_itcallout, tvtohz(&aitv.it_value),
488			    realitexpire, p);
489			timevaladd(&aitv.it_value, &ctv);
490		}
491		oitv = p->p_realtimer;
492		p->p_realtimer = aitv;
493		PROC_UNLOCK(p);
494		if (timevalisset(&oitv.it_value)) {
495			if (timevalcmp(&oitv.it_value, &ctv, <))
496				timevalclear(&oitv.it_value);
497			else
498				timevalsub(&oitv.it_value, &ctv);
499		}
500	} else {
501		mtx_lock_spin(&sched_lock);
502		oitv = p->p_stats->p_timer[uap->which];
503		p->p_stats->p_timer[uap->which] = aitv;
504		mtx_unlock_spin(&sched_lock);
505	}
506	if (uap->oitv == NULL)
507		return (0);
508	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
509}
510
511/*
512 * Real interval timer expired:
513 * send process whose timer expired an alarm signal.
514 * If time is not set up to reload, then just return.
515 * Else compute next time timer should go off which is > current time.
516 * This is where delay in processing this timeout causes multiple
517 * SIGALRM calls to be compressed into one.
518 * tvtohz() always adds 1 to allow for the time until the next clock
519 * interrupt being strictly less than 1 clock tick, but we don't want
520 * that here since we want to appear to be in sync with the clock
521 * interrupt even when we're delayed.
522 */
523void
524realitexpire(void *arg)
525{
526	struct proc *p;
527	struct timeval ctv, ntv;
528
529	p = (struct proc *)arg;
530	PROC_LOCK(p);
531	psignal(p, SIGALRM);
532	if (!timevalisset(&p->p_realtimer.it_interval)) {
533		timevalclear(&p->p_realtimer.it_value);
534		PROC_UNLOCK(p);
535		return;
536	}
537	for (;;) {
538		timevaladd(&p->p_realtimer.it_value,
539		    &p->p_realtimer.it_interval);
540		getmicrouptime(&ctv);
541		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
542			ntv = p->p_realtimer.it_value;
543			timevalsub(&ntv, &ctv);
544			callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
545			    realitexpire, p);
546			PROC_UNLOCK(p);
547			return;
548		}
549	}
550	/*NOTREACHED*/
551}
552
553/*
554 * Check that a proposed value to load into the .it_value or
555 * .it_interval part of an interval timer is acceptable, and
556 * fix it to have at least minimal value (i.e. if it is less
557 * than the resolution of the clock, round it up.)
558 */
559int
560itimerfix(struct timeval *tv)
561{
562
563	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
564	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
565		return (EINVAL);
566	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
567		tv->tv_usec = tick;
568	return (0);
569}
570
571/*
572 * Decrement an interval timer by a specified number
573 * of microseconds, which must be less than a second,
574 * i.e. < 1000000.  If the timer expires, then reload
575 * it.  In this case, carry over (usec - old value) to
576 * reduce the value reloaded into the timer so that
577 * the timer does not drift.  This routine assumes
578 * that it is called in a context where the timers
579 * on which it is operating cannot change in value.
580 */
581int
582itimerdecr(struct itimerval *itp, int usec)
583{
584
585	if (itp->it_value.tv_usec < usec) {
586		if (itp->it_value.tv_sec == 0) {
587			/* expired, and already in next interval */
588			usec -= itp->it_value.tv_usec;
589			goto expire;
590		}
591		itp->it_value.tv_usec += 1000000;
592		itp->it_value.tv_sec--;
593	}
594	itp->it_value.tv_usec -= usec;
595	usec = 0;
596	if (timevalisset(&itp->it_value))
597		return (1);
598	/* expired, exactly at end of interval */
599expire:
600	if (timevalisset(&itp->it_interval)) {
601		itp->it_value = itp->it_interval;
602		itp->it_value.tv_usec -= usec;
603		if (itp->it_value.tv_usec < 0) {
604			itp->it_value.tv_usec += 1000000;
605			itp->it_value.tv_sec--;
606		}
607	} else
608		itp->it_value.tv_usec = 0;		/* sec is already 0 */
609	return (0);
610}
611
612/*
613 * Add and subtract routines for timevals.
614 * N.B.: subtract routine doesn't deal with
615 * results which are before the beginning,
616 * it just gets very confused in this case.
617 * Caveat emptor.
618 */
619void
620timevaladd(struct timeval *t1, struct timeval *t2)
621{
622
623	t1->tv_sec += t2->tv_sec;
624	t1->tv_usec += t2->tv_usec;
625	timevalfix(t1);
626}
627
628void
629timevalsub(struct timeval *t1, struct timeval *t2)
630{
631
632	t1->tv_sec -= t2->tv_sec;
633	t1->tv_usec -= t2->tv_usec;
634	timevalfix(t1);
635}
636
637static void
638timevalfix(struct timeval *t1)
639{
640
641	if (t1->tv_usec < 0) {
642		t1->tv_sec--;
643		t1->tv_usec += 1000000;
644	}
645	if (t1->tv_usec >= 1000000) {
646		t1->tv_sec++;
647		t1->tv_usec -= 1000000;
648	}
649}
650
651/*
652 * ratecheck(): simple time-based rate-limit checking.
653 */
654int
655ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
656{
657	struct timeval tv, delta;
658	int rv = 0;
659
660	getmicrouptime(&tv);		/* NB: 10ms precision */
661	delta = tv;
662	timevalsub(&delta, lasttime);
663
664	/*
665	 * check for 0,0 is so that the message will be seen at least once,
666	 * even if interval is huge.
667	 */
668	if (timevalcmp(&delta, mininterval, >=) ||
669	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
670		*lasttime = tv;
671		rv = 1;
672	}
673
674	return (rv);
675}
676
677/*
678 * ppsratecheck(): packets (or events) per second limitation.
679 *
680 * Return 0 if the limit is to be enforced (e.g. the caller
681 * should drop a packet because of the rate limitation).
682 *
683 * maxpps of 0 always causes zero to be returned.  maxpps of -1
684 * always causes 1 to be returned; this effectively defeats rate
685 * limiting.
686 *
687 * Note that we maintain the struct timeval for compatibility
688 * with other bsd systems.  We reuse the storage and just monitor
689 * clock ticks for minimal overhead.
690 */
691int
692ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
693{
694	int now;
695
696	/*
697	 * Reset the last time and counter if this is the first call
698	 * or more than a second has passed since the last update of
699	 * lasttime.
700	 */
701	now = ticks;
702	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
703		lasttime->tv_sec = now;
704		*curpps = 1;
705		return (maxpps != 0);
706	} else {
707		(*curpps)++;		/* NB: ignore potential overflow */
708		return (maxpps < 0 || *curpps < maxpps);
709	}
710}
711