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