2011-11-13 00:41:20 +08:00
|
|
|
/**
|
2012-03-21 20:57:05 +08:00
|
|
|
* @file ptp4l.c
|
|
|
|
* @brief PTP Boundary Clock main program
|
2011-11-13 00:41:20 +08:00
|
|
|
* @note Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-02-22 01:18:08 +08:00
|
|
|
#include <limits.h>
|
2011-11-13 00:41:20 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "clock.h"
|
2011-12-31 16:18:25 +08:00
|
|
|
#include "config.h"
|
2014-06-12 00:07:08 +08:00
|
|
|
#include "ntpshm.h"
|
2012-08-25 15:19:29 +08:00
|
|
|
#include "pi.h"
|
2011-11-13 00:41:20 +08:00
|
|
|
#include "print.h"
|
2012-08-25 15:24:08 +08:00
|
|
|
#include "raw.h"
|
2012-03-17 21:53:10 +08:00
|
|
|
#include "sk.h"
|
2011-11-13 00:41:20 +08:00
|
|
|
#include "transport.h"
|
2012-11-26 03:30:14 +08:00
|
|
|
#include "udp6.h"
|
2013-09-30 02:57:00 +08:00
|
|
|
#include "uds.h"
|
2012-08-05 20:11:18 +08:00
|
|
|
#include "util.h"
|
2012-12-10 17:28:28 +08:00
|
|
|
#include "version.h"
|
2011-11-13 00:41:20 +08:00
|
|
|
|
2012-10-18 19:15:06 +08:00
|
|
|
int assume_two_step = 0;
|
2012-03-21 03:16:58 +08:00
|
|
|
|
2011-11-13 00:41:20 +08:00
|
|
|
static void usage(char *progname)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"\nusage: %s [options]\n\n"
|
2012-08-22 00:00:14 +08:00
|
|
|
" Delay Mechanism\n\n"
|
2012-04-05 18:04:11 +08:00
|
|
|
" -A Auto, starting with E2E\n"
|
|
|
|
" -E E2E, delay request-response (default)\n"
|
|
|
|
" -P P2P, peer delay mechanism\n\n"
|
2012-08-22 00:00:14 +08:00
|
|
|
" Network Transport\n\n"
|
2011-11-13 00:41:20 +08:00
|
|
|
" -2 IEEE 802.3\n"
|
|
|
|
" -4 UDP IPV4 (default)\n"
|
|
|
|
" -6 UDP IPV6\n\n"
|
|
|
|
" Time Stamping\n\n"
|
2012-08-21 01:56:03 +08:00
|
|
|
" -H HARDWARE (default)\n"
|
|
|
|
" -S SOFTWARE\n"
|
|
|
|
" -L LEGACY HW\n\n"
|
2011-11-13 00:41:20 +08:00
|
|
|
" Other Options\n\n"
|
2011-12-31 16:18:25 +08:00
|
|
|
" -f [file] read configuration from 'file'\n"
|
2011-11-13 00:41:20 +08:00
|
|
|
" -i [dev] interface device to use, for example 'eth0'\n"
|
|
|
|
" (may be specified multiple times)\n"
|
2012-05-10 02:46:16 +08:00
|
|
|
" -p [dev] PTP hardware clock device to use, default auto\n"
|
2012-01-08 18:46:48 +08:00
|
|
|
" (ignored for SOFTWARE/LEGACY HW time stamping)\n"
|
2012-08-21 01:56:03 +08:00
|
|
|
" -s slave only mode (overrides configuration file)\n"
|
|
|
|
" -l [num] set the logging level to 'num'\n"
|
2012-12-10 17:28:28 +08:00
|
|
|
" -m print messages to stdout\n"
|
2012-08-21 01:56:08 +08:00
|
|
|
" -q do not print messages to the syslog\n"
|
2012-12-10 17:28:28 +08:00
|
|
|
" -v prints the software version and exits\n"
|
2012-08-21 01:56:03 +08:00
|
|
|
" -h prints this message and exits\n"
|
2012-01-08 18:46:48 +08:00
|
|
|
"\n",
|
2012-05-10 02:46:16 +08:00
|
|
|
progname);
|
2011-11-13 00:41:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2015-10-18 23:19:43 +08:00
|
|
|
char *config = NULL, *req_phc = NULL, *progname;
|
2015-08-23 19:59:38 +08:00
|
|
|
int c, err = -1;
|
2014-08-14 21:56:06 +08:00
|
|
|
struct interface *iface;
|
2015-08-23 19:59:38 +08:00
|
|
|
struct clock *clock = NULL;
|
|
|
|
struct config *cfg;
|
2015-10-21 16:48:43 +08:00
|
|
|
int phc_index = -1, print_level;
|
2011-11-13 00:41:20 +08:00
|
|
|
|
2014-07-08 22:14:20 +08:00
|
|
|
if (handle_term_signals())
|
2012-08-28 03:07:38 +08:00
|
|
|
return -1;
|
|
|
|
|
2015-08-23 19:59:38 +08:00
|
|
|
cfg = config_create();
|
|
|
|
if (!cfg) {
|
2015-08-13 00:34:04 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-11-13 00:41:20 +08:00
|
|
|
/* Process the command line arguments. */
|
|
|
|
progname = strrchr(argv[0], '/');
|
|
|
|
progname = progname ? 1+progname : argv[0];
|
2012-12-10 17:28:28 +08:00
|
|
|
while (EOF != (c = getopt(argc, argv, "AEP246HSLf:i:p:sl:mqvh"))) {
|
2011-11-13 00:41:20 +08:00
|
|
|
switch (c) {
|
2012-08-21 01:56:03 +08:00
|
|
|
case 'A':
|
2015-08-16 03:03:02 +08:00
|
|
|
if (config_set_int(cfg, "delay_mechanism", DM_AUTO))
|
2015-08-23 19:59:38 +08:00
|
|
|
goto out;
|
2012-08-21 01:56:03 +08:00
|
|
|
break;
|
|
|
|
case 'E':
|
2015-08-16 03:03:02 +08:00
|
|
|
if (config_set_int(cfg, "delay_mechanism", DM_E2E))
|
2015-08-23 19:59:38 +08:00
|
|
|
goto out;
|
2012-08-21 01:56:03 +08:00
|
|
|
break;
|
|
|
|
case 'P':
|
2015-08-16 03:03:02 +08:00
|
|
|
if (config_set_int(cfg, "delay_mechanism", DM_P2P))
|
2015-08-23 19:59:38 +08:00
|
|
|
goto out;
|
2012-08-21 01:56:03 +08:00
|
|
|
break;
|
2011-11-13 00:41:20 +08:00
|
|
|
case '2':
|
2015-08-16 02:52:09 +08:00
|
|
|
if (config_set_int(cfg, "network_transport",
|
|
|
|
TRANS_IEEE_802_3))
|
2015-08-23 19:59:38 +08:00
|
|
|
goto out;
|
2011-11-13 00:41:20 +08:00
|
|
|
break;
|
|
|
|
case '4':
|
2015-08-16 02:52:09 +08:00
|
|
|
if (config_set_int(cfg, "network_transport",
|
|
|
|
TRANS_UDP_IPV4))
|
2015-08-23 19:59:38 +08:00
|
|
|
goto out;
|
2011-11-13 00:41:20 +08:00
|
|
|
break;
|
|
|
|
case '6':
|
2015-08-16 02:52:09 +08:00
|
|
|
if (config_set_int(cfg, "network_transport",
|
|
|
|
TRANS_UDP_IPV6))
|
2015-08-23 19:59:38 +08:00
|
|
|
goto out;
|
2011-11-13 00:41:20 +08:00
|
|
|
break;
|
2012-08-21 01:56:03 +08:00
|
|
|
case 'H':
|
2015-08-16 03:45:19 +08:00
|
|
|
if (config_set_int(cfg, "time_stamping", TS_HARDWARE))
|
2015-08-23 19:59:38 +08:00
|
|
|
goto out;
|
2012-04-05 18:04:11 +08:00
|
|
|
break;
|
2012-08-21 01:56:03 +08:00
|
|
|
case 'S':
|
2015-08-16 03:45:19 +08:00
|
|
|
if (config_set_int(cfg, "time_stamping", TS_SOFTWARE))
|
2015-08-23 19:59:38 +08:00
|
|
|
goto out;
|
2012-08-21 01:56:03 +08:00
|
|
|
break;
|
|
|
|
case 'L':
|
2015-08-16 03:45:19 +08:00
|
|
|
if (config_set_int(cfg, "time_stamping", TS_LEGACY_HW))
|
2015-08-23 19:59:38 +08:00
|
|
|
goto out;
|
2012-04-05 18:04:11 +08:00
|
|
|
break;
|
2011-12-31 16:18:25 +08:00
|
|
|
case 'f':
|
|
|
|
config = optarg;
|
|
|
|
break;
|
2011-11-13 00:41:20 +08:00
|
|
|
case 'i':
|
2015-08-23 19:59:38 +08:00
|
|
|
if (!config_create_interface(optarg, cfg))
|
|
|
|
goto out;
|
2011-11-13 00:41:20 +08:00
|
|
|
break;
|
2012-08-21 01:56:03 +08:00
|
|
|
case 'p':
|
|
|
|
req_phc = optarg;
|
2011-12-30 18:27:02 +08:00
|
|
|
break;
|
2012-08-21 01:56:03 +08:00
|
|
|
case 's':
|
2015-08-16 01:16:32 +08:00
|
|
|
if (config_set_int(cfg, "slaveOnly", 1)) {
|
2015-08-23 19:59:38 +08:00
|
|
|
goto out;
|
2015-08-16 01:16:32 +08:00
|
|
|
}
|
2011-12-28 02:18:52 +08:00
|
|
|
break;
|
2012-08-21 01:56:03 +08:00
|
|
|
case 'l':
|
2015-08-14 04:25:39 +08:00
|
|
|
if (get_arg_val_i(c, optarg, &print_level,
|
2013-06-04 13:01:04 +08:00
|
|
|
PRINT_LEVEL_MIN, PRINT_LEVEL_MAX))
|
2015-08-23 19:59:38 +08:00
|
|
|
goto out;
|
2015-08-14 04:25:39 +08:00
|
|
|
config_set_int(cfg, "logging_level", print_level);
|
2011-11-13 00:41:20 +08:00
|
|
|
break;
|
2012-12-10 17:28:28 +08:00
|
|
|
case 'm':
|
2015-08-14 04:33:23 +08:00
|
|
|
config_set_int(cfg, "verbose", 1);
|
2012-12-10 17:28:28 +08:00
|
|
|
break;
|
2012-01-08 18:46:48 +08:00
|
|
|
case 'q':
|
2015-08-14 04:28:40 +08:00
|
|
|
config_set_int(cfg, "use_syslog", 0);
|
2012-01-08 18:46:48 +08:00
|
|
|
break;
|
|
|
|
case 'v':
|
2012-12-10 17:28:28 +08:00
|
|
|
version_show(stdout);
|
|
|
|
return 0;
|
2011-11-13 00:41:20 +08:00
|
|
|
case 'h':
|
|
|
|
usage(progname);
|
|
|
|
return 0;
|
|
|
|
case '?':
|
|
|
|
usage(progname);
|
2015-08-23 19:59:38 +08:00
|
|
|
goto out;
|
2011-11-13 00:41:20 +08:00
|
|
|
default:
|
|
|
|
usage(progname);
|
2015-08-23 19:59:38 +08:00
|
|
|
goto out;
|
2011-11-13 00:41:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 19:59:38 +08:00
|
|
|
if (config && (c = config_read(config, cfg))) {
|
2012-11-08 01:20:29 +08:00
|
|
|
return c;
|
2012-08-21 01:57:11 +08:00
|
|
|
}
|
2015-08-13 00:47:00 +08:00
|
|
|
|
2015-08-16 20:24:39 +08:00
|
|
|
print_set_progname(progname);
|
|
|
|
print_set_verbose(config_get_int(cfg, NULL, "verbose"));
|
|
|
|
print_set_syslog(config_get_int(cfg, NULL, "use_syslog"));
|
|
|
|
print_set_level(config_get_int(cfg, NULL, "logging_level"));
|
|
|
|
|
2015-08-13 00:47:00 +08:00
|
|
|
assume_two_step = config_get_int(cfg, NULL, "assume_two_step");
|
2015-08-12 18:02:52 +08:00
|
|
|
sk_check_fupsync = config_get_int(cfg, NULL, "check_fup_sync");
|
2015-08-13 00:47:32 +08:00
|
|
|
sk_tx_timeout = config_get_int(cfg, NULL, "tx_timestamp_timeout");
|
2015-08-13 00:47:00 +08:00
|
|
|
|
2015-08-16 03:56:59 +08:00
|
|
|
if (config_get_int(cfg, NULL, "clock_servo") == CLOCK_SERVO_NTPSHM) {
|
2015-08-15 20:29:04 +08:00
|
|
|
config_set_int(cfg, "kernel_leap", 0);
|
2015-08-15 20:37:24 +08:00
|
|
|
config_set_int(cfg, "sanity_freq_limit", 0);
|
2014-06-20 22:32:51 +08:00
|
|
|
}
|
2012-08-21 01:57:11 +08:00
|
|
|
|
2015-08-23 19:59:38 +08:00
|
|
|
if (STAILQ_EMPTY(&cfg->interfaces)) {
|
2012-03-27 17:50:54 +08:00
|
|
|
fprintf(stderr, "no interface specified\n");
|
2011-11-13 00:41:20 +08:00
|
|
|
usage(progname);
|
2015-08-23 19:59:38 +08:00
|
|
|
goto out;
|
2011-11-13 00:41:20 +08:00
|
|
|
}
|
2012-05-10 02:46:16 +08:00
|
|
|
|
|
|
|
/* determine PHC Clock index */
|
2015-08-23 19:59:38 +08:00
|
|
|
iface = STAILQ_FIRST(&cfg->interfaces);
|
2015-08-15 20:11:17 +08:00
|
|
|
if (config_get_int(cfg, NULL, "free_running")) {
|
2012-09-05 19:40:21 +08:00
|
|
|
phc_index = -1;
|
2015-08-16 03:45:19 +08:00
|
|
|
} else if (config_get_int(cfg, NULL, "time_stamping") == TS_SOFTWARE ||
|
|
|
|
config_get_int(cfg, NULL, "time_stamping") == TS_LEGACY_HW) {
|
2012-05-10 02:46:16 +08:00
|
|
|
phc_index = -1;
|
|
|
|
} else if (req_phc) {
|
|
|
|
if (1 != sscanf(req_phc, "/dev/ptp%d", &phc_index)) {
|
|
|
|
fprintf(stderr, "bad ptp device string\n");
|
2015-08-23 19:59:38 +08:00
|
|
|
goto out;
|
2012-05-10 02:46:16 +08:00
|
|
|
}
|
2014-08-14 21:56:06 +08:00
|
|
|
} else if (iface->ts_info.valid) {
|
|
|
|
phc_index = iface->ts_info.phc_index;
|
2012-11-14 05:35:04 +08:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "ptp device not specified and\n"
|
|
|
|
"automatic determination is not\n"
|
|
|
|
"supported. please specify ptp device\n");
|
2015-08-23 19:59:38 +08:00
|
|
|
goto out;
|
2012-05-10 02:46:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (phc_index >= 0) {
|
2012-07-08 16:44:05 +08:00
|
|
|
pr_info("selected /dev/ptp%d as PTP clock", phc_index);
|
2011-11-13 00:41:20 +08:00
|
|
|
}
|
|
|
|
|
2015-10-18 22:26:49 +08:00
|
|
|
clock = clock_create(cfg->n_interfaces > 1 ? CLOCK_TYPE_BOUNDARY :
|
|
|
|
CLOCK_TYPE_ORDINARY, cfg, phc_index);
|
2011-11-13 00:41:20 +08:00
|
|
|
if (!clock) {
|
|
|
|
fprintf(stderr, "failed to create a clock\n");
|
2015-08-23 19:59:38 +08:00
|
|
|
goto out;
|
2011-11-13 00:41:20 +08:00
|
|
|
}
|
|
|
|
|
2015-08-23 19:59:38 +08:00
|
|
|
err = 0;
|
|
|
|
|
2014-07-08 22:14:20 +08:00
|
|
|
while (is_running()) {
|
2011-11-13 00:41:20 +08:00
|
|
|
if (clock_poll(clock))
|
|
|
|
break;
|
|
|
|
}
|
2015-08-23 19:59:38 +08:00
|
|
|
out:
|
|
|
|
if (clock)
|
|
|
|
clock_destroy(clock);
|
|
|
|
config_destroy(cfg);
|
|
|
|
return err;
|
2011-11-13 00:41:20 +08:00
|
|
|
}
|