conf: return hwtimestamp data in struct

This commit is contained in:
Miroslav Lichvar 2017-01-19 12:11:32 +01:00
parent b198d76676
commit 7a937c7652
4 changed files with 37 additions and 40 deletions

24
conf.c
View file

@ -223,13 +223,7 @@ static char *leapsec_tz = NULL;
/* Name of the user to which will be dropped root privileges. */ /* Name of the user to which will be dropped root privileges. */
static char *user; static char *user;
typedef struct { /* Array of CNF_HwTsInterface */
char *name;
double tx_comp;
double rx_comp;
} HwTs_Interface;
/* Array of HwTs_Interface */
static ARR_Instance hwts_interfaces; static ARR_Instance hwts_interfaces;
typedef struct { typedef struct {
@ -333,7 +327,7 @@ CNF_Initialise(int r)
{ {
restarted = r; restarted = r;
hwts_interfaces = ARR_CreateInstance(sizeof (HwTs_Interface)); hwts_interfaces = ARR_CreateInstance(sizeof (CNF_HwTsInterface));
init_sources = ARR_CreateInstance(sizeof (IPAddr)); init_sources = ARR_CreateInstance(sizeof (IPAddr));
ntp_sources = ARR_CreateInstance(sizeof (NTP_Source)); ntp_sources = ARR_CreateInstance(sizeof (NTP_Source));
@ -360,7 +354,7 @@ CNF_Finalise(void)
unsigned int i; unsigned int i;
for (i = 0; i < ARR_GetSize(hwts_interfaces); i++) for (i = 0; i < ARR_GetSize(hwts_interfaces); i++)
Free(((HwTs_Interface *)ARR_GetElement(hwts_interfaces, i))->name); Free(((CNF_HwTsInterface *)ARR_GetElement(hwts_interfaces, i))->name);
ARR_DestroyInstance(hwts_interfaces); ARR_DestroyInstance(hwts_interfaces);
for (i = 0; i < ARR_GetSize(ntp_sources); i++) for (i = 0; i < ARR_GetSize(ntp_sources); i++)
@ -1251,7 +1245,7 @@ parse_tempcomp(char *line)
static void static void
parse_hwtimestamp(char *line) parse_hwtimestamp(char *line)
{ {
HwTs_Interface *iface; CNF_HwTsInterface *iface;
char *p; char *p;
int n; int n;
@ -1968,17 +1962,11 @@ CNF_GetInitStepThreshold(void)
/* ================================================== */ /* ================================================== */
int int
CNF_GetHwTsInterface(unsigned int index, char **name, double *tx_comp, double *rx_comp) CNF_GetHwTsInterface(unsigned int index, CNF_HwTsInterface **iface)
{ {
HwTs_Interface *iface;
if (index >= ARR_GetSize(hwts_interfaces)) if (index >= ARR_GetSize(hwts_interfaces))
return 0; return 0;
iface = ARR_GetElement(hwts_interfaces, index); *iface = (CNF_HwTsInterface *)ARR_GetElement(hwts_interfaces, index);
*name = iface->name;
*tx_comp = iface->tx_comp;
*rx_comp = iface->rx_comp;
return 1; return 1;
} }

8
conf.h
View file

@ -119,6 +119,12 @@ extern char *CNF_GetHwclockFile(void);
extern int CNF_GetInitSources(void); extern int CNF_GetInitSources(void);
extern double CNF_GetInitStepThreshold(void); extern double CNF_GetInitStepThreshold(void);
extern int CNF_GetHwTsInterface(unsigned int index, char **name, double *tx_comp, double *rx_comp); typedef struct {
char *name;
double tx_comp;
double rx_comp;
} CNF_HwTsInterface;
extern int CNF_GetHwTsInterface(unsigned int index, CNF_HwTsInterface **iface);
#endif /* GOT_CONF_H */ #endif /* GOT_CONF_H */

View file

@ -365,9 +365,8 @@ NIO_Initialise(int family)
NIO_Linux_Initialise(); NIO_Linux_Initialise();
#else #else
if (1) { if (1) {
double tx_comp, rx_comp; CNF_HwTsInterface *conf_iface;
char *name; if (CNF_GetHwTsInterface(0, &conf_iface))
if (CNF_GetHwTsInterface(0, &name, &tx_comp, &rx_comp))
LOG_FATAL(LOGF_NtpIO, "HW timestamping not supported"); LOG_FATAL(LOGF_NtpIO, "HW timestamping not supported");
} }
#endif #endif

View file

@ -91,7 +91,7 @@ static int permanent_ts_options;
/* ================================================== */ /* ================================================== */
static int static int
add_interface(const char *name, double tx_comp, double rx_comp) add_interface(CNF_HwTsInterface *conf_iface)
{ {
struct ethtool_ts_info ts_info; struct ethtool_ts_info ts_info;
struct hwtstamp_config ts_config; struct hwtstamp_config ts_config;
@ -103,7 +103,7 @@ add_interface(const char *name, double tx_comp, double rx_comp)
/* Check if the interface was not already added */ /* Check if the interface was not already added */
for (i = 0; i < ARR_GetSize(interfaces); i++) { for (i = 0; i < ARR_GetSize(interfaces); i++) {
if (!strcmp(name, ((struct Interface *)ARR_GetElement(interfaces, i))->name)) if (!strcmp(conf_iface->name, ((struct Interface *)ARR_GetElement(interfaces, i))->name))
return 1; return 1;
} }
@ -114,7 +114,8 @@ add_interface(const char *name, double tx_comp, double rx_comp)
memset(&req, 0, sizeof (req)); memset(&req, 0, sizeof (req));
memset(&ts_info, 0, sizeof (ts_info)); memset(&ts_info, 0, sizeof (ts_info));
if (snprintf(req.ifr_name, sizeof (req.ifr_name), "%s", name) >= sizeof (req.ifr_name)) { if (snprintf(req.ifr_name, sizeof (req.ifr_name), "%s", conf_iface->name) >=
sizeof (req.ifr_name)) {
close(sock_fd); close(sock_fd);
return 0; return 0;
} }
@ -163,7 +164,7 @@ add_interface(const char *name, double tx_comp, double rx_comp)
iface = ARR_GetNewElement(interfaces); iface = ARR_GetNewElement(interfaces);
snprintf(iface->name, sizeof (iface->name), "%s", name); snprintf(iface->name, sizeof (iface->name), "%s", conf_iface->name);
iface->if_index = if_index; iface->if_index = if_index;
iface->phc_fd = phc_fd; iface->phc_fd = phc_fd;
@ -172,12 +173,12 @@ add_interface(const char *name, double tx_comp, double rx_comp)
iface->l2_udp4_ntp_start = 42; iface->l2_udp4_ntp_start = 42;
iface->l2_udp6_ntp_start = 62; iface->l2_udp6_ntp_start = 62;
iface->tx_comp = tx_comp; iface->tx_comp = conf_iface->tx_comp;
iface->rx_comp = rx_comp; iface->rx_comp = conf_iface->rx_comp;
iface->clock = HCL_CreateInstance(); iface->clock = HCL_CreateInstance();
DEBUG_LOG(LOGF_NtpIOLinux, "Enabled HW timestamping on %s", name); DEBUG_LOG(LOGF_NtpIOLinux, "Enabled HW timestamping on %s", iface->name);
return 1; return 1;
} }
@ -185,18 +186,22 @@ add_interface(const char *name, double tx_comp, double rx_comp)
/* ================================================== */ /* ================================================== */
static int static int
add_all_interfaces(double tx_comp, double rx_comp) add_all_interfaces(CNF_HwTsInterface *conf_iface_all)
{ {
CNF_HwTsInterface conf_iface;
struct ifaddrs *ifaddr, *ifa; struct ifaddrs *ifaddr, *ifa;
int r; int r;
conf_iface = *conf_iface_all;
if (getifaddrs(&ifaddr)) { if (getifaddrs(&ifaddr)) {
DEBUG_LOG(LOGF_NtpIOLinux, "getifaddrs() failed : %s", strerror(errno)); DEBUG_LOG(LOGF_NtpIOLinux, "getifaddrs() failed : %s", strerror(errno));
return 0; return 0;
} }
for (r = 0, ifa = ifaddr; ifa; ifa = ifa->ifa_next) { for (r = 0, ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
if (add_interface(ifa->ifa_name, tx_comp, rx_comp)) conf_iface.name = ifa->ifa_name;
if (add_interface(&conf_iface))
r = 1; r = 1;
} }
@ -242,8 +247,7 @@ update_interface_speed(struct Interface *iface)
void void
NIO_Linux_Initialise(void) NIO_Linux_Initialise(void)
{ {
double tx_comp, rx_comp; CNF_HwTsInterface *conf_iface;
char *name;
unsigned int i; unsigned int i;
int hwts; int hwts;
@ -252,18 +256,18 @@ NIO_Linux_Initialise(void)
/* Enable HW timestamping on specified interfaces. If "*" was specified, try /* Enable HW timestamping on specified interfaces. If "*" was specified, try
all interfaces. If no interface was specified, enable SW timestamping. */ all interfaces. If no interface was specified, enable SW timestamping. */
for (i = hwts = 0; CNF_GetHwTsInterface(i, &name, &tx_comp, &rx_comp); i++) { for (i = hwts = 0; CNF_GetHwTsInterface(i, &conf_iface); i++) {
if (!strcmp("*", name)) if (!strcmp("*", conf_iface->name))
continue; continue;
if (!add_interface(name, tx_comp, rx_comp)) if (!add_interface(conf_iface))
LOG_FATAL(LOGF_NtpIO, "Could not enable HW timestamping on %s", name); LOG_FATAL(LOGF_NtpIO, "Could not enable HW timestamping on %s", conf_iface->name);
hwts = 1; hwts = 1;
} }
for (i = 0; CNF_GetHwTsInterface(i, &name, &tx_comp, &rx_comp); i++) { for (i = 0; CNF_GetHwTsInterface(i, &conf_iface); i++) {
if (strcmp("*", name)) if (strcmp("*", conf_iface->name))
continue; continue;
if (add_all_interfaces(tx_comp, rx_comp)) if (add_all_interfaces(conf_iface))
hwts = 1; hwts = 1;
break; break;
} }