xref: /freebsd-src/contrib/ntp/include/ntpsim.h (revision 416ba5c74546f32a993436a99516d35008e9f384)
1*2b15cb3dSCy Schubert /* ntpsim.h
2*2b15cb3dSCy Schubert  *
3*2b15cb3dSCy Schubert  * The header file for the ntp discrete event simulator.
4*2b15cb3dSCy Schubert  *
5*2b15cb3dSCy Schubert  * Written By:	Sachin Kamboj
6*2b15cb3dSCy Schubert  *		University of Delaware
7*2b15cb3dSCy Schubert  *		Newark, DE 19711
8*2b15cb3dSCy Schubert  * Copyright (c) 2006
99c2daa00SOllivier Robert  */
109c2daa00SOllivier Robert 
11*2b15cb3dSCy Schubert #ifndef NTPSIM_H
12*2b15cb3dSCy Schubert #define NTPSIM_H
139c2daa00SOllivier Robert 
149c2daa00SOllivier Robert #include <stdio.h>
159c2daa00SOllivier Robert #include <math.h>
16*2b15cb3dSCy Schubert #ifdef HAVE_SYS_SOCKET_H
179c2daa00SOllivier Robert #include <sys/socket.h>
18*2b15cb3dSCy Schubert #endif
199c2daa00SOllivier Robert #include <arpa/inet.h>
209c2daa00SOllivier Robert #include "ntp_syslog.h"
219c2daa00SOllivier Robert #include "ntp_fp.h"
229c2daa00SOllivier Robert #include "ntp.h"
239c2daa00SOllivier Robert #include "ntp_select.h"
249c2daa00SOllivier Robert #include "ntp_malloc.h"
259c2daa00SOllivier Robert #include "ntp_refclock.h"
269c2daa00SOllivier Robert #include "recvbuff.h"
279c2daa00SOllivier Robert #include "ntp_io.h"
289c2daa00SOllivier Robert #include "ntp_stdlib.h"
29*2b15cb3dSCy Schubert #include "ntp_prio_q.h"
309c2daa00SOllivier Robert 
31*2b15cb3dSCy Schubert /* CONSTANTS */
329c2daa00SOllivier Robert 
33*2b15cb3dSCy Schubert #ifdef PI
34*2b15cb3dSCy Schubert # undef PI
35*2b15cb3dSCy Schubert #endif
36*2b15cb3dSCy Schubert #define PI 3.1415926535         /* The world's most famous constant */
37*2b15cb3dSCy Schubert #define SIM_TIME 86400		/* end simulation time */
38*2b15cb3dSCy Schubert #define NET_DLY .001            /* network delay */
39*2b15cb3dSCy Schubert #define PROC_DLY .001		/* processing delay */
40*2b15cb3dSCy Schubert #define BEEP_DLY 3600           /* beep interval (s) */
41*2b15cb3dSCy Schubert 
42*2b15cb3dSCy Schubert 
43*2b15cb3dSCy Schubert /* Discrete Event Queue
44*2b15cb3dSCy Schubert  * --------------------
45*2b15cb3dSCy Schubert  * The NTP simulator is a discrete event simulator.
46*2b15cb3dSCy Schubert  *
47*2b15cb3dSCy Schubert  * Central to this simulator is an event queue which is a priority queue
48*2b15cb3dSCy Schubert  * in which the "priority" is given by the time of arrival of the event.
49*2b15cb3dSCy Schubert  *
50*2b15cb3dSCy Schubert  * A discrete set of events can happen and are stored in the queue to arrive
51*2b15cb3dSCy Schubert  * at a particular time.
529c2daa00SOllivier Robert  */
53*2b15cb3dSCy Schubert 
54*2b15cb3dSCy Schubert /* Possible Discrete Events */
55*2b15cb3dSCy Schubert 
569c2daa00SOllivier Robert typedef enum {
57*2b15cb3dSCy Schubert     BEEP,          /* Event to record simulator stats */
58*2b15cb3dSCy Schubert     CLOCK,         /* Event to advance the clock to the specified time */
59*2b15cb3dSCy Schubert     TIMER,         /* Event that designates a timer interrupt. */
60*2b15cb3dSCy Schubert     PACKET         /* Event that designates arrival of a packet */
619c2daa00SOllivier Robert } funcTkn;
629c2daa00SOllivier Robert 
63*2b15cb3dSCy Schubert 
64*2b15cb3dSCy Schubert /* Event information */
65*2b15cb3dSCy Schubert 
669c2daa00SOllivier Robert typedef struct {
67*2b15cb3dSCy Schubert     double time;       /* Time at which event occurred */
68*2b15cb3dSCy Schubert     funcTkn function;  /* Type of event that occured */
699c2daa00SOllivier Robert     union {
709c2daa00SOllivier Robert         struct pkt evnt_pkt;
719c2daa00SOllivier Robert         struct recvbuf evnt_buf;
72*2b15cb3dSCy Schubert     } buffer;          /* Other data associated with the event */
739c2daa00SOllivier Robert #define ntp_pkt buffer.evnt_pkt
749c2daa00SOllivier Robert #define rcv_buf buffer.evnt_buf
759c2daa00SOllivier Robert } Event;
769c2daa00SOllivier Robert 
779c2daa00SOllivier Robert 
78*2b15cb3dSCy Schubert /* Server Script Information */
79*2b15cb3dSCy Schubert typedef struct script_info_tag script_info;
80*2b15cb3dSCy Schubert struct script_info_tag {
81*2b15cb3dSCy Schubert 	script_info *	link;
82*2b15cb3dSCy Schubert 	double		duration;
83*2b15cb3dSCy Schubert 	double		freq_offset;
84*2b15cb3dSCy Schubert 	double		wander;
85*2b15cb3dSCy Schubert 	double		jitter;
86*2b15cb3dSCy Schubert 	double		prop_delay;
87*2b15cb3dSCy Schubert 	double		proc_delay;
88*2b15cb3dSCy Schubert };
899c2daa00SOllivier Robert 
90*2b15cb3dSCy Schubert typedef DECL_FIFO_ANCHOR(script_info) script_info_fifo;
919c2daa00SOllivier Robert 
929c2daa00SOllivier Robert 
93*2b15cb3dSCy Schubert /* Server Structures */
949c2daa00SOllivier Robert 
95*2b15cb3dSCy Schubert typedef struct server_info_tag server_info;
96*2b15cb3dSCy Schubert struct server_info_tag {
97*2b15cb3dSCy Schubert 	server_info *		link;
98*2b15cb3dSCy Schubert 	double			server_time;
99*2b15cb3dSCy Schubert 	sockaddr_u *		addr;
100*2b15cb3dSCy Schubert 	script_info_fifo *	script;
101*2b15cb3dSCy Schubert 	script_info *		curr_script;
102*2b15cb3dSCy Schubert };
1039c2daa00SOllivier Robert 
104*2b15cb3dSCy Schubert typedef DECL_FIFO_ANCHOR(server_info) server_info_fifo;
1059c2daa00SOllivier Robert 
106*2b15cb3dSCy Schubert 
107*2b15cb3dSCy Schubert /* Simulation control information */
108*2b15cb3dSCy Schubert 
109*2b15cb3dSCy Schubert typedef struct Sim_Info {
110*2b15cb3dSCy Schubert     double sim_time;      /* Time in the simulation */
111*2b15cb3dSCy Schubert     double end_time;      /* Time at which simulation needs to be ended */
112*2b15cb3dSCy Schubert     double beep_delay;    /* Delay between simulation "beeps" at which
113*2b15cb3dSCy Schubert                              simulation  stats are recorded. */
114*2b15cb3dSCy Schubert     int num_of_servers;   /* Number of servers in the simulation */
115*2b15cb3dSCy Schubert     server_info *servers; /* Pointer to array of servers */
116*2b15cb3dSCy Schubert } sim_info;
117*2b15cb3dSCy Schubert 
118*2b15cb3dSCy Schubert 
119*2b15cb3dSCy Schubert /* Local Clock (Client) Variables */
120*2b15cb3dSCy Schubert 
121*2b15cb3dSCy Schubert typedef struct Local_Clock_Info {
122*2b15cb3dSCy Schubert     double local_time;		/* Client disciplined time */
123*2b15cb3dSCy Schubert     double adj;			/* Remaining time correction */
124*2b15cb3dSCy Schubert     double slew;		/* Correction Slew Rate */
125*2b15cb3dSCy Schubert     double last_read_time;	/* Last time the clock was read */
126*2b15cb3dSCy Schubert } local_clock_info;
127*2b15cb3dSCy Schubert 
128*2b15cb3dSCy Schubert extern local_clock_info simclock; /* Local Clock Variables */
129*2b15cb3dSCy Schubert extern sim_info simulation;	  /* Simulation Control Variables */
130*2b15cb3dSCy Schubert 
131*2b15cb3dSCy Schubert /* Function Prototypes */
132*2b15cb3dSCy Schubert 
133*2b15cb3dSCy Schubert int	 ntpsim			(int argc, char *argv[]);
134*2b15cb3dSCy Schubert Event    *event			(double t, funcTkn f);
135*2b15cb3dSCy Schubert void     sim_event_timer	(Event *e);
136*2b15cb3dSCy Schubert int      simulate_server	(sockaddr_u *serv_addr, endpt *inter,
137*2b15cb3dSCy Schubert 				 struct pkt *rpkt);
138*2b15cb3dSCy Schubert void     sim_update_clocks	(Event *e);
139*2b15cb3dSCy Schubert void     sim_event_recv_packet	(Event *e);
140*2b15cb3dSCy Schubert void     sim_event_beep		(Event *e);
141*2b15cb3dSCy Schubert void     abortsim		(char *errmsg);
142*2b15cb3dSCy Schubert double	 gauss			(double, double);
143*2b15cb3dSCy Schubert double	 poisson		(double, double);
144*2b15cb3dSCy Schubert void     create_server_associations(void);
145*2b15cb3dSCy Schubert 
146*2b15cb3dSCy Schubert #endif	/* NTPSIM_H */
147