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