pptp.h 8.64 KB
Newer Older
1
/*
2
 * Aurelio Colosimo for CERN, 2011 -- GNU LGPL v2.1 or later
3
 */
4

5 6 7 8 9 10
#ifndef __PTP_PROTO_H__
#define __PTP_PROTO_H__

#include <stdint.h>
#include <stdarg.h>
#include <arch/arch.h> /* ntohs and so on */
11 12 13
#include <pptp/lib.h>
#include <pptp/ieee1588_types.h>
#include <pptp/constants.h>
Aurelio Colosimo's avatar
Aurelio Colosimo committed
14

15 16

/*
17
 * Runtime options. Default values can be overridden by command line.
18 19
 */
struct pp_runtime_opts {
20
	ClockQuality clock_quality;
21
	TimeInternal inbound_latency, outbound_latency;
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	Integer32 max_rst; /* Maximum number of nanoseconds to reset */
	Integer32 max_dly; /* Maximum number of nanoseconds of delay */
	Integer32 ttl;
	char *unicast_addr;
	UInteger32	slave_only:1,
			no_adjust:1,
			display_stats:1,
			csv_stats:1,
			ethernet_mode:1,
			e2e_mode:1,
			ofst_first_updated:1,
			no_rst_clk:1,
			use_syslog:1;
	Integer16 ap, ai;
	Integer16 s;
	Integer16 max_foreign_records;
	Integer16 cur_utc_ofst;
	Integer8 announce_intvl;
	Integer8 sync_intvl;
	UInteger8 prio1;
	UInteger8 prio2;
	UInteger8 domain_number;
	char *iface_name;
	void *arch_opts;

	/*TODO ARCH: arch_opts, for arch-gnu-linux, might include the following:
	 * int log_fd;
	 * char *record_file; [PP_PATH_MAX]
	 * FILE *record_fp;
	 * char *file; [PP_PATH_MAX]
	 */
53
};
54
extern struct pp_runtime_opts default_rt_opts; /* preinited with defaults */
55 56

/*
57 58
 * Communication channel. Is the abstraction of a posix socket, so that
 * this struct is platform independent
59
 */
60
struct pp_channel {
61
	union {
62
		int fd;		/* Posix wants fid descriptor */
63 64 65 66 67
		void *custom;	/* Other archs want other stuff */
	};
	void *arch_data;	/* Other arch-private info, if any */
	unsigned char addr[6];	/* Our own MAC address */
	unsigned char peer[6];	/* Our peer's MAC address */
68
	int pkt_present;
69 70
};

71 72 73
/*
 * Foreign master record. Used to manage Foreign masters
 */
74
struct pp_frgn_master {
75 76 77 78 79 80 81
	PortIdentity port_identity;
	UInteger16 ann_messages;

	//This one is not in the spec
	MsgAnnounce ann;
	MsgHeader hdr;
};
82 83

/*
84 85
 * Timer. Struct which contains timestamp of timer start and desired interval
 * for timer expiration computation. Both are seconds
86
 */
87
struct pp_timer {
88 89 90 91
	uint32_t start;
	uint32_t interval;
};

92
/*
93 94
 * Net Path. Struct which contains the network configuration parameters and
 * the event/general channels (sockets on most platforms, see above)
95
 */
96
struct pp_net_path {
97 98 99 100 101 102
	struct pp_channel evt_ch;
	struct pp_channel gen_ch;
	Integer32 ucast_addr;
	Integer32 mcast_addr;
	Integer32 peer_mcast_addr;
};
103

104
/*
105
 * Structure for the standard protocol
106 107 108
 */
struct pp_instance {
	int state;
109
	int next_state, next_delay, is_new_state; /* set by state processing */
110 111 112
	void *arch_data;		/* if arch needs it */
	void *ext_data;			/* if protocol ext needs it */
	struct pp_runtime_opts *rt_opts;
113
	struct pp_net_path *net_path;
Aurelio Colosimo's avatar
Aurelio Colosimo committed
114 115 116 117 118 119 120

	/* Data sets */
	DSDefault *defaultDS;
	DSCurrent *currentDS;
	DSParent *parentDS;
	DSPort *portDS;
	DSTimeProperties *timePropertiesDS;
121
	struct pp_timer *timers[PP_TIMER_ARRAY_SIZE];
122

123
	/* FIXME: check the variables from now on. Now inherited from ptpd src*/
124 125 126 127 128
	UInteger16 number_foreign_records;
	Integer16  max_foreign_records;
	Integer16  foreign_record_i;
	Integer16  foreign_record_best;
	Boolean  record_update;
129
	struct pp_frgn_master *frgn_master;
130
	Octet *buf_out;
131
	Octet *buf_in;	/* FIXME really useful? Probably not*/
132 133
	TimeInternal sync_receive_time;
	UInteger16 recv_sync_sequence_id;
134 135 136
	TimeInternal last_sync_corr_field;
	TimeInternal last_pdelay_req_corr_field;
	TimeInternal last_pdelay_resp_corr_field;
137 138 139 140
	TimeInternal pdelay_req_send_time;
	TimeInternal pdelay_req_receive_time;
	TimeInternal pdelay_resp_send_time;
	TimeInternal pdelay_resp_receive_time;
141 142 143
	TimeInternal delay_req_send_time;
	TimeInternal delay_req_receive_time;
	Integer8 log_min_delay_req_interval;
144

145 146 147 148 149 150 151 152 153 154 155 156
	union {
		MsgSync  sync;
		MsgFollowUp  follow;
		MsgDelayReq  req;
		MsgDelayResp resp;
		MsgPDelayReq  preq;
		MsgPDelayResp  presp;
		MsgPDelayRespFollowUp  prespfollow;
		MsgManagement  manage;
		MsgAnnounce  announce;
		MsgSignaling signaling;
	} msg_tmp;
157 158 159 160 161
	UInteger16 *sent_seq_id; /* sequence id of the last message sent of the
				  * same type
				  */
	MsgHeader msg_tmp_header;
	MsgHeader pdelay_req_hdr;
162
	MsgHeader delay_req_hdr;
163 164 165 166
	UInteger32
		is_from_self:1,
		is_from_cur_par:1,
		waiting_for_follow:1;
167 168
};

169 170 171 172 173
#define DSDEF(x) ((x)->defaultDS)
#define DSCUR(x) ((x)->currentDS)
#define DSPAR(x) ((x)->parentDS)
#define DSPOR(x) ((x)->portDS)
#define DSPRO(x) ((x)->timePropertiesDS)
174 175 176 177


/* The channel for an instance must be created and possibly destroyed. */
extern int pp_open_instance(struct pp_instance *ppi,
178
			    struct pp_runtime_opts *rt_opts);
179 180 181 182 183

extern int pp_close_instance(struct pp_instance *ppi);

extern int pp_parse_cmdline(struct pp_instance *ppi, int argc, char **argv);

184
/* Network stuff */
185
extern int pp_net_init(struct pp_instance *ppi);
186
extern int pp_net_shutdown(struct pp_instance *ppi);
187 188
extern int pp_recv_packet(struct pp_instance *ppi, void *pkt, int len);
extern int pp_send_packet(struct pp_instance *ppi, void *pkt, int len);
189

190
/* Timers */
191
extern int pp_timer_init(struct pp_instance *ppi); /* initializes timer common
192
						      structure */
193
extern int pp_timer_start(uint32_t interval, struct pp_timer *tm);
194
extern int pp_timer_stop(struct pp_timer *tm);
195
extern int pp_timer_expired(struct pp_timer *tm); /* returns 1 when expired */
196

197
/* Servo */
198
extern void pp_init_clock(struct pp_instance *ppi);
199 200 201 202
extern void pp_update_offset(struct pp_instance *ppi,
			     TimeInternal *send_time,
			     TimeInternal *recv_time,
			     TimeInternal *correctionField);
203 204 205
			/* FIXME: offset_from_master_filter: put it in ppi */
extern void pp_update_clock(struct pp_instance *ppi);

206

207 208
/* bmc.c */
extern void m1(struct pp_instance *ppi);
209 210 211 212
extern void s1(struct pp_instance *ppi, MsgHeader *header, MsgAnnounce *ann);
extern UInteger8 bmc(struct pp_instance *ppi,
		     struct pp_frgn_master *frgn_master,
		     struct pp_runtime_opts *rt_opts);
213 214

/* msg.c */
215 216
extern void msg_pack_header(struct pp_instance *ppi, void *buf);
extern void msg_unpack_header(struct pp_instance *ppi, void *buf);
217
void *msg_copy_header(MsgHeader *dest, MsgHeader *src);
218 219
extern void msg_pack_sync(struct pp_instance *ppi, void *buf,
			  Timestamp *orig_tstamp);
220
extern void msg_unpack_sync(void *buf, MsgSync *sync);
221
extern void msg_pack_announce(struct pp_instance *ppi, void *buf);
222
extern void msg_unpack_announce(void *buf, MsgAnnounce *ann);
223 224
extern void msg_pack_follow_up(struct pp_instance *ppi, void *buf,
			       Timestamp *prec_orig_tstamp);
225
extern void msg_unpack_follow_up(void *buf, MsgFollowUp *flwup);
226 227
extern void msg_pack_pdelay_req(struct pp_instance *ppi, void *buf,
				Timestamp *orig_tstamp);
228
extern void msg_unpack_pdelay_req(void *buf, MsgPDelayReq *pdelay_req);
229 230
extern void msg_pack_delay_req(struct pp_instance *ppi, void *buf,
			       Timestamp *orig_tstamp);
231
extern void msg_unpack_delay_req(void *buf, MsgDelayReq *delay_req);
232 233
extern void msg_pack_delay_resp(struct pp_instance *ppi, void *buf,
				MsgHeader *hdr, Timestamp *rcv_tstamp);
234
extern void msg_unpack_delay_resp(void *buf, MsgDelayResp *resp);
235 236
extern void msg_pack_pdelay_resp(struct pp_instance *ppi, void *buf,
				 MsgHeader *hdr, Timestamp *req_rec_tstamp);
237
extern void msg_unpack_pdelay_resp(void *buf, MsgPDelayResp *presp);
238 239 240
extern void msg_pack_pdelay_resp_followup(struct pp_instance *ppi, void *buf,
					  MsgHeader *hdr,
					  Timestamp *resp_orig_tstamp);
241 242 243
extern void msg_unpack_pdelay_resp_followup(void *buf,
	MsgPDelayRespFollowUp *presp_follow);

244 245 246 247

/* arith.c */
/* FIXME: add prefix in function name? */
extern void int64_to_TimeInternal(Integer64 bigint, TimeInternal *internal);
248 249
extern int from_TimeInternal(TimeInternal *internal, Timestamp *external);
extern int to_TimeInternal(TimeInternal *internal, Timestamp *external);
250 251 252 253
extern void add_TimeInternal(TimeInternal *r, TimeInternal *x, TimeInternal *y);
extern void sub_TimeInternal(TimeInternal *r, TimeInternal *x, TimeInternal *y);


254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
/* Get a timestamp */
extern void pp_get_stamp(uint32_t *sptr);

/*
 * The state machine itself is an array of these structures.
 */

struct pp_state_table_item {
	int state;
	int (*f1)(struct pp_instance *ppi, uint8_t *packet, int plen);
	int (*f2)(struct pp_instance *ppi, uint8_t *packet, int plen);
};

extern struct pp_state_table_item pp_state_table[]; /* 0-terminated */

269

270

271
/* Use a typedef, to avoid long prototypes */
272 273 274 275
typedef int pp_action(struct pp_instance *ppi, uint8_t *packet, int plen);

/* Standard state-machine functions */
extern pp_action pp_initializing, pp_faulty, pp_disabled, pp_listening,
276 277
		 pp_pre_master, pp_master, pp_passive, pp_uncalibrated,
		 pp_slave;
278 279 280 281 282 283 284 285 286 287 288

/* The engine */
extern int pp_state_machine(struct pp_instance *ppi, uint8_t *packet, int plen);

/*
 * FIXME
 * Whe talk raw sockets on PP_PROTO_NR.
 */
#define PP_PROTO_NR	0xcccc

#endif /* __PTP_PROTO_H__ */