forked from richardcochran/linuxptp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.c
662 lines (562 loc) · 17.4 KB
/
config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/**
* @file config.c
* @note Copyright (C) 2011 Richard Cochran <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <ctype.h>
#include <float.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "ether.h"
#include "print.h"
#include "util.h"
enum config_section {
GLOBAL_SECTION,
PORT_SECTION,
UNKNOWN_SECTION,
};
static enum parser_result parse_section_line(char *s, enum config_section *section)
{
if (!strcasecmp(s, "[global]")) {
*section = GLOBAL_SECTION;
} else if (s[0] == '[') {
char c;
*section = PORT_SECTION;
/* Replace square brackets with white space. */
while (0 != (c = *s)) {
if (c == '[' || c == ']')
*s = ' ';
s++;
}
} else
return NOT_PARSED;
return PARSED_OK;
}
static enum parser_result parse_pod_setting(const char *option,
const char *value,
struct port_defaults *pod)
{
int val;
unsigned int uval;
enum parser_result r;
if (!strcmp(option, "delayAsymmetry")) {
r = get_ranged_int(value, &val, INT_MIN, INT_MAX);
if (r != PARSED_OK)
return r;
pod->asymmetry = (Integer64) val << 16;
} else if (!strcmp(option, "logAnnounceInterval")) {
r = get_ranged_int(value, &val, INT8_MIN, INT8_MAX);
if (r != PARSED_OK)
return r;
pod->logAnnounceInterval = val;
} else if (!strcmp(option, "logSyncInterval")) {
r = get_ranged_int(value, &val, INT8_MIN, INT8_MAX);
if (r != PARSED_OK)
return r;
pod->logSyncInterval = val;
} else if (!strcmp(option, "logMinDelayReqInterval")) {
r = get_ranged_int(value, &val, INT8_MIN, INT8_MAX);
if (r != PARSED_OK)
return r;
pod->logMinDelayReqInterval = val;
} else if (!strcmp(option, "logMinPdelayReqInterval")) {
r = get_ranged_int(value, &val, INT8_MIN, INT8_MAX);
if (r != PARSED_OK)
return r;
pod->logMinPdelayReqInterval = val;
} else if (!strcmp(option, "announceReceiptTimeout")) {
r = get_ranged_uint(value, &uval, 2, UINT8_MAX);
if (r != PARSED_OK)
return r;
pod->announceReceiptTimeout = uval;
} else if (!strcmp(option, "syncReceiptTimeout")) {
r = get_ranged_uint(value, &uval, 0, UINT8_MAX);
if (r != PARSED_OK)
return r;
pod->syncReceiptTimeout = uval;
} else if (!strcmp(option, "transportSpecific")) {
r = get_ranged_uint(value, &uval, 0, 0x0F);
if (r != PARSED_OK)
return r;
pod->transportSpecific = uval << 4;
} else if (!strcmp(option, "path_trace_enabled")) {
r = get_ranged_int(value, &val, 0, 1);
if (r != PARSED_OK)
return r;
pod->path_trace_enabled = val;
} else if (!strcmp(option, "follow_up_info")) {
r = get_ranged_int(value, &val, 0, 1);
if (r != PARSED_OK)
return r;
pod->follow_up_info = val;
} else if (!strcmp(option, "neighborPropDelayThresh")) {
r = get_ranged_uint(value, &uval, 0, UINT32_MAX);
if (r != PARSED_OK)
return r;
pod->neighborPropDelayThresh = uval;
} else if (!strcmp(option, "fault_badpeernet_interval")) {
pod->flt_interval_pertype[FT_BAD_PEER_NETWORK].type = FTMO_LINEAR_SECONDS;
if (!strcasecmp("ASAP", value)) {
pod->flt_interval_pertype[FT_BAD_PEER_NETWORK].val = 0;
} else {
r = get_ranged_int(value, &val, INT32_MIN, INT32_MAX);
if (r != PARSED_OK)
return r;
pod->flt_interval_pertype[FT_BAD_PEER_NETWORK].val = val;
}
} else if (!strcmp(option, "fault_reset_interval")) {
pod->flt_interval_pertype[FT_UNSPECIFIED].type = FTMO_LOG2_SECONDS;
if (!strcasecmp("ASAP", value)) {
pod->flt_interval_pertype[FT_UNSPECIFIED].val = FRI_ASAP;
} else {
r = get_ranged_int(value, &val, INT8_MIN, INT8_MAX);
if (r != PARSED_OK)
return r;
pod->flt_interval_pertype[FT_UNSPECIFIED].val = val;
}
} else
return NOT_PARSED;
return PARSED_OK;
}
static enum parser_result parse_port_setting(const char *option,
const char *value,
struct config *cfg,
int p)
{
enum parser_result r;
r = parse_pod_setting(option, value, &cfg->iface[p].pod);
if (r != NOT_PARSED)
return r;
if (!strcmp(option, "network_transport")) {
if (!strcasecmp("L2", value))
cfg->iface[p].transport = TRANS_IEEE_802_3;
else if (!strcasecmp("UDPv4", value))
cfg->iface[p].transport = TRANS_UDP_IPV4;
else if (!strcasecmp("UDPv6", value))
cfg->iface[p].transport = TRANS_UDP_IPV6;
else
return BAD_VALUE;
} else if (!strcmp(option, "delay_mechanism")) {
if (!strcasecmp("Auto", value))
cfg->iface[p].dm = DM_AUTO;
else if (!strcasecmp("E2E", value))
cfg->iface[p].dm = DM_E2E;
else if (!strcasecmp("P2P", value))
cfg->iface[p].dm = DM_P2P;
else
return BAD_VALUE;
} else
return NOT_PARSED;
return PARSED_OK;
}
static int count_char(const char *str, char c)
{
int num = 0;
char s;
while ((s = *(str++))) {
if (s == c)
num++;
}
return num;
}
static enum parser_result parse_global_setting(const char *option,
const char *value,
struct config *cfg)
{
double df;
int i, val, cfg_ignore = cfg->cfg_ignore;
unsigned int uval;
unsigned char mac[MAC_LEN];
unsigned char oui[OUI_LEN];
struct defaultDS *dds = &cfg->dds.dds;
struct port_defaults *pod = &cfg->pod;
enum parser_result r;
r = parse_pod_setting(option, value, pod);
if (r != NOT_PARSED)
return r;
if (!strcmp(option, "twoStepFlag")) {
r = get_ranged_int(value, &val, 0, 1);
if (r != PARSED_OK)
return r;
if (val)
dds->flags |= DDS_TWO_STEP_FLAG;
else
dds->flags &= ~DDS_TWO_STEP_FLAG;
} else if (!strcmp(option, "slaveOnly")) {
r = get_ranged_int(value, &val, 0, 1);
if (r != PARSED_OK)
return r;
if (!(cfg_ignore & CFG_IGNORE_SLAVEONLY)) {
if (val)
dds->flags |= DDS_SLAVE_ONLY;
else
dds->flags &= ~DDS_SLAVE_ONLY;
}
} else if (!strcmp(option, "priority1")) {
r = get_ranged_uint(value, &uval, 0, UINT8_MAX);
if (r != PARSED_OK)
return r;
dds->priority1 = uval;
} else if (!strcmp(option, "priority2")) {
r = get_ranged_uint(value, &uval, 0, UINT8_MAX);
if (r != PARSED_OK)
return r;
dds->priority2 = uval;
} else if (!strcmp(option, "domainNumber")) {
r = get_ranged_uint(value, &uval, 0, 127);
if (r != PARSED_OK)
return r;
dds->domainNumber = uval;
} else if (!strcmp(option, "clockClass")) {
r = get_ranged_uint(value, &uval, 0, UINT8_MAX);
if (r != PARSED_OK)
return r;
if (!(cfg_ignore & CFG_IGNORE_SLAVEONLY))
dds->clockQuality.clockClass = uval;
} else if (!strcmp(option, "clockAccuracy")) {
r = get_ranged_uint(value, &uval, 0, UINT8_MAX);
if (r != PARSED_OK)
return r;
dds->clockQuality.clockAccuracy = uval;
} else if (!strcmp(option, "offsetScaledLogVariance")) {
r = get_ranged_uint(value, &uval, 0, UINT16_MAX);
if (r != PARSED_OK)
return r;
dds->clockQuality.offsetScaledLogVariance = uval;
} else if (!strcmp(option, "free_running")) {
r = get_ranged_int(value, &val, 0, 1);
if (r != PARSED_OK)
return r;
cfg->dds.free_running = val;
} else if (!strcmp(option, "freq_est_interval")) {
r = get_ranged_int(value, &val, 0, INT_MAX);
if (r != PARSED_OK)
return r;
cfg->dds.freq_est_interval = val;
pod->freq_est_interval = val;
} else if (!strcmp(option, "assume_two_step")) {
r = get_ranged_int(value, &val, 0, 1);
if (r != PARSED_OK)
return r;
*cfg->assume_two_step = val;
} else if (!strcmp(option, "tx_timestamp_timeout")) {
r = get_ranged_int(value, &val, 1, INT_MAX);
if (r != PARSED_OK)
return r;
*cfg->tx_timestamp_timeout = val;
} else if (!strcmp(option, "check_fup_sync")) {
r = get_ranged_int(value, &val, 0, 1);
if (r != PARSED_OK)
return r;
*cfg->check_fup_sync = val;
} else if (!strcmp(option, "pi_proportional_const")) {
r = get_ranged_double(value, &df, 0.0, DBL_MAX);
if (r != PARSED_OK)
return r;
*cfg->pi_proportional_const = df;
} else if (!strcmp(option, "pi_integral_const")) {
r = get_ranged_double(value, &df, 0.0, DBL_MAX);
if (r != PARSED_OK)
return r;
*cfg->pi_integral_const = df;
} else if (!strcmp(option, "pi_proportional_scale")) {
r = get_ranged_double(value, &df, 0.0, DBL_MAX);
if (r != PARSED_OK)
return r;
*cfg->pi_proportional_scale = df;
} else if (!strcmp(option, "pi_proportional_exponent")) {
r = get_ranged_double(value, &df, -DBL_MAX, DBL_MAX);
if (r != PARSED_OK)
return r;
*cfg->pi_proportional_exponent = df;
} else if (!strcmp(option, "pi_proportional_norm_max")) {
r = get_ranged_double(value, &df, DBL_MIN, 1.0);
if (r != PARSED_OK)
return r;
*cfg->pi_proportional_norm_max = df;
} else if (!strcmp(option, "pi_integral_scale")) {
r = get_ranged_double(value, &df, 0.0, DBL_MAX);
if (r != PARSED_OK)
return r;
*cfg->pi_integral_scale = df;
} else if (!strcmp(option, "pi_integral_exponent")) {
r = get_ranged_double(value, &df, -DBL_MAX, DBL_MAX);
if (r != PARSED_OK)
return r;
*cfg->pi_integral_exponent = df;
} else if (!strcmp(option, "pi_integral_norm_max")) {
r = get_ranged_double(value, &df, DBL_MIN, 2.0);
if (r != PARSED_OK)
return r;
*cfg->pi_integral_norm_max = df;
} else if (!strcmp(option, "pi_offset_const")) {
r = get_ranged_double(value, &df, 0.0, DBL_MAX);
if (r != PARSED_OK)
return r;
*cfg->pi_offset_const = df;
} else if (!strcmp(option, "pi_f_offset_const")) {
r = get_ranged_double(value, &df, 0.0, DBL_MAX);
if (r != PARSED_OK)
return r;
*cfg->pi_f_offset_const = df;
} else if (!strcmp(option, "pi_max_frequency")) {
r = get_ranged_int(value, &val, 0, INT_MAX);
if (r != PARSED_OK)
return r;
*cfg->pi_max_frequency = val;
} else if (!strcmp(option, "ptp_dst_mac")) {
if (MAC_LEN != sscanf(value, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
&mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]))
return BAD_VALUE;
for (i = 0; i < MAC_LEN; i++)
cfg->ptp_dst_mac[i] = mac[i];
} else if (!strcmp(option, "p2p_dst_mac")) {
if (MAC_LEN != sscanf(value, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
&mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]))
return BAD_VALUE;
for (i = 0; i < MAC_LEN; i++)
cfg->p2p_dst_mac[i] = mac[i];
} else if (!strcmp(option, "udp6_scope")) {
r = get_ranged_uint(value, &uval, 0x00, 0x0F);
if (r != PARSED_OK)
return r;
*cfg->udp6_scope = uval;
} else if (!strcmp(option, "logging_level")) {
r = get_ranged_int(value, &val,
PRINT_LEVEL_MIN, PRINT_LEVEL_MAX);
if (r != PARSED_OK)
return r;
if (!(cfg_ignore & CFG_IGNORE_PRINT_LEVEL)) {
cfg->print_level = val;
}
} else if (!strcmp(option, "verbose")) {
r = get_ranged_int(value, &val, 0, 1);
if (r != PARSED_OK)
return r;
if (!(cfg_ignore & CFG_IGNORE_VERBOSE))
cfg->verbose = val;
} else if (!strcmp(option, "use_syslog")) {
r = get_ranged_int(value, &val, 0, 1);
if (r != PARSED_OK)
return r;
if (!(cfg_ignore & CFG_IGNORE_USE_SYSLOG))
cfg->use_syslog = val;
} else if (!strcmp(option, "time_stamping")) {
if (!(cfg_ignore & CFG_IGNORE_TIMESTAMPING)) {
if (0 == strcasecmp("hardware", value))
cfg->timestamping = TS_HARDWARE;
else if (0 == strcasecmp("software", value))
cfg->timestamping = TS_SOFTWARE;
else if (0 == strcasecmp("legacy", value))
cfg->timestamping = TS_LEGACY_HW;
else
return BAD_VALUE;
}
} else if (!strcmp(option, "delay_mechanism")) {
if (!(cfg_ignore & CFG_IGNORE_DM)) {
if (0 == strcasecmp("E2E", value))
cfg->dm = DM_E2E;
else if (0 == strcasecmp("P2P", value))
cfg->dm = DM_P2P;
else if (0 == strcasecmp("Auto", value))
cfg->dm = DM_AUTO;
else
return BAD_VALUE;
}
} else if (!strcmp(option, "network_transport")) {
if (!(cfg_ignore & CFG_IGNORE_TRANSPORT)) {
if (!strcasecmp("UDPv4", value))
cfg->transport = TRANS_UDP_IPV4;
else if (!strcasecmp("UDPv6", value))
cfg->transport = TRANS_UDP_IPV6;
else if (!strcasecmp("L2", value))
cfg->transport = TRANS_IEEE_802_3;
else
return BAD_VALUE;
}
} else if (!strcmp(option, "clock_servo")) {
if (!strcasecmp("pi", value))
cfg->clock_servo = CLOCK_SERVO_PI;
else
return BAD_VALUE;
} else if (!strcmp(option, "productDescription")) {
if (count_char(value, ';') != 2)
return BAD_VALUE;
if (static_ptp_text_set(&cfg->dds.clock_desc.productDescription, value) != 0)
return BAD_VALUE;
} else if (!strcmp(option, "revisionData")) {
if (count_char(value, ';') != 2)
return BAD_VALUE;
if (static_ptp_text_set(&cfg->dds.clock_desc.revisionData, value) != 0)
return BAD_VALUE;
} else if (!strcmp(option, "userDescription")) {
if (static_ptp_text_set(&cfg->dds.clock_desc.userDescription, value) != 0)
return BAD_VALUE;
} else if (!strcmp(option, "manufacturerIdentity")) {
if (OUI_LEN != sscanf(value, "%hhx:%hhx:%hhx",
&oui[0], &oui[1], &oui[2]))
return BAD_VALUE;
for (i = 0; i < OUI_LEN; i++)
cfg->dds.clock_desc.manufacturerIdentity[i] = oui[i];
} else if (!strcmp(option, "summary_interval")) {
r = get_ranged_int(value, &val, INT_MIN, INT_MAX);
if (r != PARSED_OK)
return r;
cfg->dds.stats_interval = val;
} else if (!strcmp(option, "kernel_leap")) {
r = get_ranged_int(value, &val, 0, 1);
if (r != PARSED_OK)
return r;
cfg->dds.kernel_leap = val;
} else if (!strcmp(option, "timeSource")) {
r = get_ranged_int(value, &val, 0x10, 0xfe);
if (r != PARSED_OK)
return r;
cfg->dds.time_source = val;
} else
return NOT_PARSED;
return PARSED_OK;
}
static enum parser_result parse_setting_line(char *line, char **option, char **value)
{
*option = line;
while (!isspace(line[0])) {
if (line[0] == '\0')
return NOT_PARSED;
line++;
}
while (isspace(line[0])) {
line[0] = '\0';
line++;
}
*value = line;
return PARSED_OK;
}
int config_read(char *name, struct config *cfg)
{
enum config_section current_section = UNKNOWN_SECTION;
enum parser_result parser_res;
FILE *fp;
char buf[1024], *line, *c, *option, *value;
int current_port = 0, line_num;
fp = 0 == strncmp(name, "-", 2) ? stdin : fopen(name, "r");
if (!fp) {
fprintf(stderr, "failed to open configuration file %s: %m\n", name);
return -1;
}
for (line_num = 1; fgets(buf, sizeof(buf), fp); line_num++) {
c = buf;
/* skip whitespace characters */
while (isspace(*c))
c++;
/* ignore empty lines and comments */
if (*c == '#' || *c == '\n' || *c == '\0')
continue;
line = c;
/* remove trailing whitespace characters and \n */
c += strlen(line) - 1;
while (c > line && (*c == '\n' || isspace(*c)))
*c-- = '\0';
if (parse_section_line(line, ¤t_section) == PARSED_OK) {
if (current_section == PORT_SECTION) {
char port[17];
if (1 != sscanf(line, " %16s", port)) {
fprintf(stderr, "could not parse port name on line %d\n",
line_num);
goto parse_error;
}
current_port = config_create_interface(port, cfg);
if (current_port < 0)
goto parse_error;
}
continue;
}
switch (current_section) {
case GLOBAL_SECTION:
case PORT_SECTION:
if (parse_setting_line(line, &option, &value)) {
fprintf(stderr, "could not parse line %d in %s section\n",
line_num,
current_section == GLOBAL_SECTION ?
"global" : cfg->iface[current_port].name);
goto parse_error;
}
if (current_section == GLOBAL_SECTION)
parser_res = parse_global_setting(option, value, cfg);
else
parser_res = parse_port_setting(option, value, cfg, current_port);
switch (parser_res) {
case PARSED_OK:
break;
case NOT_PARSED:
fprintf(stderr, "unknown option %s at line %d in %s section\n",
option, line_num,
current_section == GLOBAL_SECTION ?
"global" : cfg->iface[current_port].name);
goto parse_error;
case BAD_VALUE:
fprintf(stderr, "%s is a bad value for option %s at line %d\n",
value, option, line_num);
goto parse_error;
case MALFORMED:
fprintf(stderr, "%s is a malformed value for option %s at line %d\n",
value, option, line_num);
goto parse_error;
case OUT_OF_RANGE:
fprintf(stderr, "%s is an out of range value for option %s at line %d\n",
value, option, line_num);
goto parse_error;
}
break;
case UNKNOWN_SECTION:
fprintf(stderr, "line %d is not in a section\n", line_num);
goto parse_error;
default:
continue;
}
}
fclose(fp);
return 0;
parse_error:
fprintf(stderr, "failed to parse configuration file %s\n", name);
fclose(fp);
return -2;
}
/* returns the number matching that interface, or -1 on failure */
int config_create_interface(char *name, struct config *cfg)
{
struct interface *iface;
int i;
if (cfg->nports >= MAX_PORTS) {
fprintf(stderr, "more than %d ports specified\n", MAX_PORTS);
return -1;
}
iface = &cfg->iface[cfg->nports];
/* only create each interface once (by name) */
for(i = 0; i < cfg->nports; i++) {
if (0 == strncmp(name, cfg->iface[i].name, MAX_IFNAME_SIZE))
return i;
}
strncpy(iface->name, name, MAX_IFNAME_SIZE);
iface->dm = cfg->dm;
iface->transport = cfg->transport;
memcpy(&iface->pod, &cfg->pod, sizeof(cfg->pod));
sk_get_ts_info(name, &iface->ts_info);
cfg->nports++;
return i;
}