phc2sys, phc_ctl: Re-factor common code.

The two programs, phc2sys and phc_ctl, use nearly identical subroutines to
open an instance of a dynamic posix clock.  In anticipation of yet a third
program with similar needs, this patch refactors the common code into the
utilities.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
master
Richard Cochran 2019-07-07 18:13:39 -07:00
parent 46b673792a
commit d32758aa09
5 changed files with 52 additions and 77 deletions

View File

@ -55,10 +55,10 @@ all: $(PRG)
ptp4l: $(OBJ) ptp4l: $(OBJ)
nsm: config.o filter.o hash.o mave.o mmedian.o msg.o nsm.o print.o raw.o \ nsm: config.o filter.o hash.o mave.o mmedian.o msg.o nsm.o phc.o print.o raw.o \
rtnl.o sk.o transport.o tlv.o tsproc.o udp.o udp6.o uds.o util.o version.o rtnl.o sk.o transport.o tlv.o tsproc.o udp.o udp6.o uds.o util.o version.o
pmc: config.o hash.o msg.o pmc.o pmc_common.o print.o raw.o sk.o tlv.o \ pmc: config.o hash.o msg.o phc.o pmc.o pmc_common.o print.o raw.o sk.o tlv.o \
transport.o udp.o udp6.o uds.o util.o version.o transport.o udp.o udp6.o uds.o util.o version.o
phc2sys: clockadj.o clockcheck.o config.o hash.o linreg.o msg.o ntpshm.o \ phc2sys: clockadj.o clockcheck.o config.o hash.o linreg.o msg.o ntpshm.o \
@ -69,14 +69,14 @@ hwstamp_ctl: hwstamp_ctl.o version.o
phc_ctl: phc_ctl.o phc.o sk.o util.o clockadj.o sysoff.o print.o version.o phc_ctl: phc_ctl.o phc.o sk.o util.o clockadj.o sysoff.o print.o version.o
snmp4lptp: config.o hash.o msg.o pmc_common.o print.o raw.o sk.o \ snmp4lptp: config.o hash.o msg.o phc.o pmc_common.o print.o raw.o sk.o \
snmp4lptp.o tlv.o transport.o udp.o udp6.o uds.o util.o snmp4lptp.o tlv.o transport.o udp.o udp6.o uds.o util.o
$(CC) $^ $(LDFLAGS) $(LOADLIBES) $(LDLIBS) $(snmplib) -o $@ $(CC) $^ $(LDFLAGS) $(LOADLIBES) $(LDLIBS) $(snmplib) -o $@
snmp4lptp.o: snmp4lptp.c snmp4lptp.o: snmp4lptp.c
$(CC) $(CPPFLAGS) $(CFLAGS) $(snmpflg) -c $< $(CC) $(CPPFLAGS) $(CFLAGS) $(snmpflg) -c $<
timemaster: print.o rtnl.o sk.o timemaster.o util.o version.o timemaster: phc.o print.o rtnl.o sk.o timemaster.o util.o version.o
version.o: .version version.sh $(filter-out version.d,$(DEPEND)) version.o: .version version.sh $(filter-out version.d,$(DEPEND))

View File

@ -135,40 +135,6 @@ static int run_pmc_port_properties(struct node *node, int timeout,
unsigned int port, unsigned int port,
int *state, int *tstamping, char *iface); int *state, int *tstamping, char *iface);
static clockid_t clock_open(char *device, int *phc_index)
{
struct sk_ts_info ts_info;
char phc_device[19];
int clkid;
/* check if device is CLOCK_REALTIME */
if (!strcasecmp(device, "CLOCK_REALTIME"))
return CLOCK_REALTIME;
/* check if device is valid phc device */
clkid = phc_open(device);
if (clkid != CLOCK_INVALID)
return clkid;
/* check if device is a valid ethernet device */
if (sk_get_ts_info(device, &ts_info) || !ts_info.valid) {
fprintf(stderr, "unknown clock %s: %m\n", device);
return CLOCK_INVALID;
}
if (ts_info.phc_index < 0) {
fprintf(stderr, "interface %s does not have a PHC\n", device);
return CLOCK_INVALID;
}
sprintf(phc_device, "/dev/ptp%d", ts_info.phc_index);
clkid = phc_open(phc_device);
if (clkid == CLOCK_INVALID)
fprintf(stderr, "cannot open %s: %m\n", device);
*phc_index = ts_info.phc_index;
return clkid;
}
static struct servo *servo_add(struct node *node, struct clock *clock) static struct servo *servo_add(struct node *node, struct clock *clock)
{ {
double ppb; double ppb;
@ -210,7 +176,7 @@ static struct clock *clock_add(struct node *node, char *device)
int phc_index = -1; int phc_index = -1;
if (device) { if (device) {
clkid = clock_open(device, &phc_index); clkid = posix_clock_open(device, &phc_index);
if (clkid == CLOCK_INVALID) if (clkid == CLOCK_INVALID)
return NULL; return NULL;
} }
@ -371,7 +337,7 @@ static void clock_reinit(struct node *node, struct clock *clock, int new_state)
/* Check if phc index changed */ /* Check if phc index changed */
if (!sk_get_ts_info(clock->device, &ts_info) && if (!sk_get_ts_info(clock->device, &ts_info) &&
clock->phc_index != ts_info.phc_index) { clock->phc_index != ts_info.phc_index) {
clkid = clock_open(clock->device, &phc_index); clkid = posix_clock_open(clock->device, &phc_index);
if (clkid == CLOCK_INVALID) if (clkid == CLOCK_INVALID)
return; return;

View File

@ -110,39 +110,6 @@ static int64_t calculate_offset(struct timespec *ts1,
return offset; return offset;
} }
static clockid_t clock_open(char *device)
{
struct sk_ts_info ts_info;
char phc_device[19];
int clkid;
/* check if device is CLOCK_REALTIME */
if (!strcasecmp(device, "CLOCK_REALTIME"))
return CLOCK_REALTIME;
/* check if device is valid phc device */
clkid = phc_open(device);
if (clkid != CLOCK_INVALID)
return clkid;
/* check if device is a valid ethernet device */
if (sk_get_ts_info(device, &ts_info) || !ts_info.valid) {
pr_err("unknown clock %s: %m", device);
return CLOCK_INVALID;
}
if (ts_info.phc_index < 0) {
pr_err("interface %s does not have a PHC", device);
return CLOCK_INVALID;
}
sprintf(phc_device, "/dev/ptp%d", ts_info.phc_index);
clkid = phc_open(phc_device);
if (clkid == CLOCK_INVALID)
pr_err("cannot open %s for %s: %m", phc_device, device);
return clkid;
}
static void usage(const char *progname) static void usage(const char *progname)
{ {
fprintf(stderr, fprintf(stderr,
@ -503,10 +470,10 @@ static int run_cmds(clockid_t clkid, int cmdc, char *cmdv[])
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
const char *progname; int c, cmdc, junk, print_level = LOG_INFO, result;
char **cmdv, *default_cmdv[] = { "caps" }; char **cmdv, *default_cmdv[] = { "caps" };
int c, result, cmdc; int use_syslog = 1, verbose = 1;
int print_level = LOG_INFO, verbose = 1, use_syslog = 1; const char *progname;
clockid_t clkid; clockid_t clkid;
install_handler(SIGALRM, handle_alarm); install_handler(SIGALRM, handle_alarm);
@ -558,7 +525,7 @@ int main(int argc, char *argv[])
cmdc = argc - optind - 1; cmdc = argc - optind - 1;
} }
clkid = clock_open(argv[optind]); clkid = posix_clock_open(argv[optind], &junk);
if (clkid == CLOCK_INVALID) if (clkid == CLOCK_INVALID)
return -1; return -1;

34
util.c
View File

@ -25,6 +25,7 @@
#include <string.h> #include <string.h>
#include "address.h" #include "address.h"
#include "phc.h"
#include "print.h" #include "print.h"
#include "sk.h" #include "sk.h"
#include "util.h" #include "util.h"
@ -171,6 +172,39 @@ char *portaddr2str(struct PortAddress *addr)
return buf; return buf;
} }
clockid_t posix_clock_open(char *device, int *phc_index)
{
struct sk_ts_info ts_info;
char phc_device[19];
int clkid;
/* check if device is CLOCK_REALTIME */
if (!strcasecmp(device, "CLOCK_REALTIME")) {
return CLOCK_REALTIME;
}
/* check if device is valid phc device */
clkid = phc_open(device);
if (clkid != CLOCK_INVALID) {
return clkid;
}
/* check if device is a valid ethernet device */
if (sk_get_ts_info(device, &ts_info) || !ts_info.valid) {
pr_err("unknown clock %s: %m", device);
return CLOCK_INVALID;
}
if (ts_info.phc_index < 0) {
pr_err("interface %s does not have a PHC", device);
return CLOCK_INVALID;
}
snprintf(phc_device, sizeof(phc_device), "/dev/ptp%d", ts_info.phc_index);
clkid = phc_open(phc_device);
if (clkid == CLOCK_INVALID) {
pr_err("cannot open %s for %s: %m", phc_device, device);
}
*phc_index = ts_info.phc_index;
return clkid;
}
int str2addr(enum transport_type type, const char *s, struct address *addr) int str2addr(enum transport_type type, const char *s, struct address *addr)
{ {
unsigned char mac[MAC_LEN]; unsigned char mac[MAC_LEN];

8
util.h
View File

@ -103,6 +103,14 @@ char *pid2str(struct PortIdentity *id);
char *portaddr2str(struct PortAddress *addr); char *portaddr2str(struct PortAddress *addr);
/**
* Opens a dynamic posix clock by name.
* @param device The PHC character device or network interface to open.
* @param phc_index Returns the PHC index, if any.
* @return A valid clock ID on success or CLOCK_INVALID otherwise.
*/
clockid_t posix_clock_open(char *device, int *phc_index);
/** /**
* Compare two port identities for equality. * Compare two port identities for equality.
* *