2012-03-17 21:53:10 +08:00
|
|
|
/**
|
|
|
|
* @file sk.c
|
|
|
|
* @brief Implements protocol independent socket methods.
|
|
|
|
* @note Copyright (C) 2012 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.
|
|
|
|
*/
|
|
|
|
#include <errno.h>
|
2018-11-13 20:16:08 +08:00
|
|
|
#include <time.h>
|
2012-03-17 21:53:10 +08:00
|
|
|
#include <linux/net_tstamp.h>
|
|
|
|
#include <linux/sockios.h>
|
2012-05-10 02:46:16 +08:00
|
|
|
#include <linux/ethtool.h>
|
2012-03-17 21:53:10 +08:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <unistd.h>
|
2013-01-09 08:21:26 +08:00
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <stdlib.h>
|
2013-04-05 23:52:14 +08:00
|
|
|
#include <poll.h>
|
2012-03-17 21:53:10 +08:00
|
|
|
|
2014-04-22 22:00:59 +08:00
|
|
|
#include "address.h"
|
|
|
|
#include "ether.h"
|
2014-11-01 19:27:14 +08:00
|
|
|
#include "missing.h"
|
2012-03-17 21:53:10 +08:00
|
|
|
#include "print.h"
|
|
|
|
#include "sk.h"
|
|
|
|
|
2012-08-05 20:09:13 +08:00
|
|
|
/* globals */
|
|
|
|
|
2013-04-05 23:52:14 +08:00
|
|
|
int sk_tx_timeout = 1;
|
2013-08-29 16:54:55 +08:00
|
|
|
int sk_check_fupsync;
|
2018-08-29 04:05:28 +08:00
|
|
|
enum hwts_filter_mode sk_hwts_filter_mode = HWTS_FILTER_NORMAL;
|
2012-08-05 20:09:13 +08:00
|
|
|
|
2012-03-17 21:53:10 +08:00
|
|
|
/* private methods */
|
|
|
|
|
2018-08-29 04:05:28 +08:00
|
|
|
static void init_ifreq(struct ifreq *ifreq, struct hwtstamp_config *cfg,
|
|
|
|
const char *device)
|
2012-03-17 21:53:10 +08:00
|
|
|
{
|
2018-08-29 04:05:28 +08:00
|
|
|
memset(ifreq, 0, sizeof(*ifreq));
|
|
|
|
memset(cfg, 0, sizeof(*cfg));
|
2012-03-17 21:53:10 +08:00
|
|
|
|
2018-08-29 04:05:28 +08:00
|
|
|
strncpy(ifreq->ifr_name, device, sizeof(ifreq->ifr_name) - 1);
|
2012-03-17 21:53:10 +08:00
|
|
|
|
2018-08-29 04:05:28 +08:00
|
|
|
ifreq->ifr_data = (void *) cfg;
|
|
|
|
}
|
2012-03-17 21:53:10 +08:00
|
|
|
|
2018-08-29 04:05:28 +08:00
|
|
|
static int hwts_init(int fd, const char *device, int rx_filter,
|
|
|
|
int rx_filter2, int tx_type)
|
|
|
|
{
|
|
|
|
struct ifreq ifreq;
|
|
|
|
struct hwtstamp_config cfg;
|
|
|
|
int err;
|
2012-03-17 21:53:10 +08:00
|
|
|
|
2018-08-29 04:05:28 +08:00
|
|
|
init_ifreq(&ifreq, &cfg, device);
|
2012-03-17 21:53:10 +08:00
|
|
|
|
2018-08-29 04:05:28 +08:00
|
|
|
switch (sk_hwts_filter_mode) {
|
|
|
|
case HWTS_FILTER_CHECK:
|
|
|
|
err = ioctl(fd, SIOCGHWTSTAMP, &ifreq);
|
|
|
|
if (err < 0) {
|
|
|
|
pr_err("ioctl SIOCGHWTSTAMP failed: %m");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HWTS_FILTER_FULL:
|
|
|
|
cfg.tx_type = tx_type;
|
|
|
|
cfg.rx_filter = HWTSTAMP_FILTER_ALL;
|
|
|
|
err = ioctl(fd, SIOCSHWTSTAMP, &ifreq);
|
|
|
|
if (err < 0) {
|
|
|
|
pr_err("ioctl SIOCSHWTSTAMP failed: %m");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HWTS_FILTER_NORMAL:
|
|
|
|
cfg.tx_type = tx_type;
|
|
|
|
cfg.rx_filter = rx_filter;
|
|
|
|
err = ioctl(fd, SIOCSHWTSTAMP, &ifreq);
|
|
|
|
if (err < 0) {
|
|
|
|
pr_info("driver rejected most general HWTSTAMP filter");
|
2012-03-17 21:53:10 +08:00
|
|
|
|
2018-08-29 04:05:28 +08:00
|
|
|
init_ifreq(&ifreq, &cfg, device);
|
|
|
|
cfg.tx_type = tx_type;
|
|
|
|
cfg.rx_filter = rx_filter2;
|
|
|
|
|
|
|
|
err = ioctl(fd, SIOCSHWTSTAMP, &ifreq);
|
|
|
|
if (err < 0) {
|
|
|
|
pr_err("ioctl SIOCSHWTSTAMP failed: %m");
|
|
|
|
return err;
|
|
|
|
}
|
2012-03-17 21:53:10 +08:00
|
|
|
}
|
2018-08-29 04:05:28 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cfg.tx_type != tx_type ||
|
|
|
|
(cfg.rx_filter != rx_filter &&
|
|
|
|
cfg.rx_filter != rx_filter2 &&
|
|
|
|
cfg.rx_filter != HWTSTAMP_FILTER_ALL)) {
|
|
|
|
pr_debug("tx_type %d not %d", cfg.tx_type, tx_type);
|
|
|
|
pr_debug("rx_filter %d not %d or %d", cfg.rx_filter, rx_filter,
|
|
|
|
rx_filter2);
|
|
|
|
pr_err("The current filter does not match the required");
|
|
|
|
return -1;
|
2012-03-17 21:53:10 +08:00
|
|
|
}
|
|
|
|
|
2012-10-26 02:55:28 +08:00
|
|
|
return 0;
|
2012-03-17 21:53:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* public methods */
|
|
|
|
|
2016-07-30 03:54:44 +08:00
|
|
|
int sk_interface_fd(void)
|
|
|
|
{
|
|
|
|
int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
if (fd < 0) {
|
|
|
|
pr_err("socket failed: %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2014-02-08 00:52:03 +08:00
|
|
|
int sk_interface_index(int fd, const char *name)
|
2012-03-17 21:53:10 +08:00
|
|
|
{
|
|
|
|
struct ifreq ifreq;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
memset(&ifreq, 0, sizeof(ifreq));
|
2014-09-17 17:11:15 +08:00
|
|
|
strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name) - 1);
|
2012-03-17 21:53:10 +08:00
|
|
|
err = ioctl(fd, SIOCGIFINDEX, &ifreq);
|
|
|
|
if (err < 0) {
|
|
|
|
pr_err("ioctl SIOCGIFINDEX failed: %m");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
return ifreq.ifr_ifindex;
|
|
|
|
}
|
|
|
|
|
2013-08-29 16:54:55 +08:00
|
|
|
int sk_general_init(int fd)
|
|
|
|
{
|
|
|
|
int on = sk_check_fupsync ? 1 : 0;
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS, &on, sizeof(on)) < 0) {
|
|
|
|
pr_err("ioctl SO_TIMESTAMPNS failed: %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-08 00:52:03 +08:00
|
|
|
int sk_get_ts_info(const char *name, struct sk_ts_info *sk_info)
|
2012-05-10 02:46:16 +08:00
|
|
|
{
|
|
|
|
#ifdef ETHTOOL_GET_TS_INFO
|
|
|
|
struct ethtool_ts_info info;
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd, err;
|
|
|
|
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
info.cmd = ETHTOOL_GET_TS_INFO;
|
2012-11-14 05:35:04 +08:00
|
|
|
strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
|
2012-05-10 02:46:16 +08:00
|
|
|
ifr.ifr_data = (char *) &info;
|
|
|
|
fd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
|
|
|
if (fd < 0) {
|
|
|
|
pr_err("socket failed: %m");
|
2012-11-14 05:35:04 +08:00
|
|
|
goto failed;
|
2012-05-10 02:46:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
err = ioctl(fd, SIOCETHTOOL, &ifr);
|
|
|
|
if (err < 0) {
|
|
|
|
pr_err("ioctl SIOCETHTOOL failed: %m");
|
|
|
|
close(fd);
|
2012-11-14 05:35:04 +08:00
|
|
|
goto failed;
|
2012-05-10 02:46:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
2012-11-14 05:35:04 +08:00
|
|
|
/* copy the necessary data to sk_info */
|
|
|
|
memset(sk_info, 0, sizeof(struct sk_ts_info));
|
|
|
|
sk_info->valid = 1;
|
|
|
|
sk_info->phc_index = info.phc_index;
|
|
|
|
sk_info->so_timestamping = info.so_timestamping;
|
|
|
|
sk_info->tx_types = info.tx_types;
|
|
|
|
sk_info->rx_filters = info.rx_filters;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
failed:
|
2012-11-15 03:09:55 +08:00
|
|
|
#endif
|
2012-11-14 05:35:04 +08:00
|
|
|
/* clear data and ensure it is not marked valid */
|
|
|
|
memset(sk_info, 0, sizeof(struct sk_ts_info));
|
|
|
|
return -1;
|
2012-05-10 02:46:16 +08:00
|
|
|
}
|
|
|
|
|
ptp4l: Add IPoIB interface support for ptp4l
The current implementation of ptp4l always assumes 6 octets MAC
address, which is correct for Ethernet interfaces but not for IPoIB
interfaces (that have 20 octets MAC), therefore running ptp4l over
IPoIB interface does not function correctly.
In Infiniband, every interface has three identifiers:
GUID, GID, and LID.
The GUID is similar in concept to a MAC address. From RFC4392:
The EUI-64 portion of a GID is referred to as the Global Unique
Identifier (GUID) and is the only persistent identifier of a port.
Therefore, to support IPoIB interfaces, the GUID of the port should
be used instead of the MAC.
This patch checks the interface type before creating the clock identity,
for Infiniband ports, it retrieves the GUID of the port using sysfs
and use it to create the clock identity.
sysfs method was chosen since the GUID is the 6 lsb bytes of
the 20 byte device address, and SIOCGIFHWADDR ioctl call returns
the 14 msb bytes of the device address, so it is not possible to
get the GUID using SIOCGIFHWADDR ioctl call.
[ RC: fixed trivial coding style error, space after switch keyword. ]
Signed-off-by: Feras Daoud <ferasda@mellanox.com>
Reviewed-by: Alex Vesker <valex@mellanox.com>
2017-08-09 23:27:32 +08:00
|
|
|
static int sk_interface_guidaddr(const char *name, unsigned char *guid)
|
|
|
|
{
|
|
|
|
char file_name[64], buf[64], addr[8];
|
|
|
|
FILE *f;
|
|
|
|
char *err;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
snprintf(file_name, sizeof buf, "/sys/class/net/%s/address", name);
|
|
|
|
f = fopen(file_name, "r");
|
|
|
|
if (!f) {
|
|
|
|
pr_err("failed to open %s: %m", buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the file position to the beginning of the GUID */
|
|
|
|
res = fseek(f, GUID_OFFSET, SEEK_SET);
|
|
|
|
if (res) {
|
|
|
|
pr_err("fseek failed: %m");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fgets(buf, sizeof buf, f);
|
|
|
|
if (err == NULL) {
|
|
|
|
pr_err("fseek failed: %m");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
|
|
|
|
&addr[0], &addr[1], &addr[2], &addr[3],
|
|
|
|
&addr[4], &addr[5], &addr[6], &addr[7]);
|
|
|
|
if (res != GUID_LEN) {
|
|
|
|
pr_err("sscanf failed: %m");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(guid, addr, GUID_LEN);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
fclose(f);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-04-22 22:00:59 +08:00
|
|
|
int sk_interface_macaddr(const char *name, struct address *mac)
|
2012-03-17 21:53:10 +08:00
|
|
|
{
|
|
|
|
struct ifreq ifreq;
|
ptp4l: Add IPoIB interface support for ptp4l
The current implementation of ptp4l always assumes 6 octets MAC
address, which is correct for Ethernet interfaces but not for IPoIB
interfaces (that have 20 octets MAC), therefore running ptp4l over
IPoIB interface does not function correctly.
In Infiniband, every interface has three identifiers:
GUID, GID, and LID.
The GUID is similar in concept to a MAC address. From RFC4392:
The EUI-64 portion of a GID is referred to as the Global Unique
Identifier (GUID) and is the only persistent identifier of a port.
Therefore, to support IPoIB interfaces, the GUID of the port should
be used instead of the MAC.
This patch checks the interface type before creating the clock identity,
for Infiniband ports, it retrieves the GUID of the port using sysfs
and use it to create the clock identity.
sysfs method was chosen since the GUID is the 6 lsb bytes of
the 20 byte device address, and SIOCGIFHWADDR ioctl call returns
the 14 msb bytes of the device address, so it is not possible to
get the GUID using SIOCGIFHWADDR ioctl call.
[ RC: fixed trivial coding style error, space after switch keyword. ]
Signed-off-by: Feras Daoud <ferasda@mellanox.com>
Reviewed-by: Alex Vesker <valex@mellanox.com>
2017-08-09 23:27:32 +08:00
|
|
|
int err, fd, type;
|
2012-03-17 21:53:10 +08:00
|
|
|
|
|
|
|
memset(&ifreq, 0, sizeof(ifreq));
|
2014-09-17 17:11:15 +08:00
|
|
|
strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name) - 1);
|
2012-03-17 21:53:10 +08:00
|
|
|
|
|
|
|
fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
if (fd < 0) {
|
|
|
|
pr_err("socket failed: %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioctl(fd, SIOCGIFHWADDR, &ifreq);
|
|
|
|
if (err < 0) {
|
|
|
|
pr_err("ioctl SIOCGIFHWADDR failed: %m");
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-10-16 17:40:12 +08:00
|
|
|
close(fd);
|
|
|
|
|
ptp4l: Add IPoIB interface support for ptp4l
The current implementation of ptp4l always assumes 6 octets MAC
address, which is correct for Ethernet interfaces but not for IPoIB
interfaces (that have 20 octets MAC), therefore running ptp4l over
IPoIB interface does not function correctly.
In Infiniband, every interface has three identifiers:
GUID, GID, and LID.
The GUID is similar in concept to a MAC address. From RFC4392:
The EUI-64 portion of a GID is referred to as the Global Unique
Identifier (GUID) and is the only persistent identifier of a port.
Therefore, to support IPoIB interfaces, the GUID of the port should
be used instead of the MAC.
This patch checks the interface type before creating the clock identity,
for Infiniband ports, it retrieves the GUID of the port using sysfs
and use it to create the clock identity.
sysfs method was chosen since the GUID is the 6 lsb bytes of
the 20 byte device address, and SIOCGIFHWADDR ioctl call returns
the 14 msb bytes of the device address, so it is not possible to
get the GUID using SIOCGIFHWADDR ioctl call.
[ RC: fixed trivial coding style error, space after switch keyword. ]
Signed-off-by: Feras Daoud <ferasda@mellanox.com>
Reviewed-by: Alex Vesker <valex@mellanox.com>
2017-08-09 23:27:32 +08:00
|
|
|
/* Get interface type */
|
|
|
|
type = ifreq.ifr_hwaddr.sa_family;
|
|
|
|
switch (type) {
|
|
|
|
case ARPHRD_INFINIBAND:
|
|
|
|
err = sk_interface_guidaddr(name, mac->sll.sll_addr);
|
|
|
|
if (err) {
|
|
|
|
pr_err("fail to get address using sysfs: %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mac->sll.sll_halen = EUI64;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
memcpy(mac->sll.sll_addr, &ifreq.ifr_hwaddr.sa_data, MAC_LEN);
|
|
|
|
mac->sll.sll_halen = EUI48;
|
|
|
|
}
|
|
|
|
|
2015-08-29 16:28:51 +08:00
|
|
|
mac->sll.sll_family = AF_PACKET;
|
|
|
|
mac->len = sizeof(mac->sll);
|
2012-03-17 21:53:10 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-22 22:00:59 +08:00
|
|
|
int sk_interface_addr(const char *name, int family, struct address *addr)
|
2013-01-09 08:21:26 +08:00
|
|
|
{
|
|
|
|
struct ifaddrs *ifaddr, *i;
|
2014-04-22 22:00:59 +08:00
|
|
|
int result = -1;
|
|
|
|
|
2013-01-09 08:21:26 +08:00
|
|
|
if (getifaddrs(&ifaddr) == -1) {
|
|
|
|
pr_err("getifaddrs failed: %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (i = ifaddr; i; i = i->ifa_next) {
|
|
|
|
if (i->ifa_addr && family == i->ifa_addr->sa_family &&
|
|
|
|
strcmp(name, i->ifa_name) == 0)
|
|
|
|
{
|
|
|
|
switch (family) {
|
|
|
|
case AF_INET:
|
2014-04-22 22:00:59 +08:00
|
|
|
addr->len = sizeof(addr->sin);
|
2014-09-17 17:11:16 +08:00
|
|
|
memcpy(&addr->sin, i->ifa_addr, addr->len);
|
2013-01-09 08:21:26 +08:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2014-04-22 22:00:59 +08:00
|
|
|
addr->len = sizeof(addr->sin6);
|
2014-09-17 17:11:16 +08:00
|
|
|
memcpy(&addr->sin6, i->ifa_addr, addr->len);
|
2013-01-09 08:21:26 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
2014-04-22 22:00:59 +08:00
|
|
|
result = 0;
|
2013-01-09 08:21:26 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-08-20 23:41:29 +08:00
|
|
|
freeifaddrs(ifaddr);
|
2013-01-09 08:21:26 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-11-01 19:27:14 +08:00
|
|
|
static short sk_events = POLLPRI;
|
|
|
|
static short sk_revents = POLLPRI;
|
|
|
|
|
2012-03-17 21:53:10 +08:00
|
|
|
int sk_receive(int fd, void *buf, int buflen,
|
2014-04-22 22:00:59 +08:00
|
|
|
struct address *addr, struct hw_timestamp *hwts, int flags)
|
2012-03-17 21:53:10 +08:00
|
|
|
{
|
|
|
|
char control[256];
|
2013-04-05 23:52:14 +08:00
|
|
|
int cnt = 0, res = 0, level, type;
|
2012-03-17 21:53:10 +08:00
|
|
|
struct cmsghdr *cm;
|
|
|
|
struct iovec iov = { buf, buflen };
|
|
|
|
struct msghdr msg;
|
2013-08-29 16:54:55 +08:00
|
|
|
struct timespec *sw, *ts = NULL;
|
2012-03-17 21:53:10 +08:00
|
|
|
|
|
|
|
memset(control, 0, sizeof(control));
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
2014-04-22 22:00:59 +08:00
|
|
|
if (addr) {
|
|
|
|
msg.msg_name = &addr->ss;
|
|
|
|
msg.msg_namelen = sizeof(addr->ss);
|
|
|
|
}
|
2012-03-17 21:53:10 +08:00
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
msg.msg_control = control;
|
|
|
|
msg.msg_controllen = sizeof(control);
|
|
|
|
|
2013-04-05 23:52:14 +08:00
|
|
|
if (flags == MSG_ERRQUEUE) {
|
2014-11-01 19:27:14 +08:00
|
|
|
struct pollfd pfd = { fd, sk_events, 0 };
|
2013-04-05 23:52:14 +08:00
|
|
|
res = poll(&pfd, 1, sk_tx_timeout);
|
|
|
|
if (res < 1) {
|
2013-10-29 02:56:27 +08:00
|
|
|
pr_err(res ? "poll for tx timestamp failed: %m" :
|
|
|
|
"timed out while polling for tx timestamp");
|
|
|
|
pr_err("increasing tx_timestamp_timeout may correct "
|
|
|
|
"this issue, but it is likely caused by a driver bug");
|
2013-04-05 23:52:14 +08:00
|
|
|
return res;
|
2014-11-01 19:27:14 +08:00
|
|
|
} else if (!(pfd.revents & sk_revents)) {
|
2013-10-29 02:56:27 +08:00
|
|
|
pr_err("poll for tx timestamp woke up on non ERR event");
|
2013-04-05 23:52:14 +08:00
|
|
|
return -1;
|
2012-03-17 21:53:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-05 23:52:14 +08:00
|
|
|
cnt = recvmsg(fd, &msg, flags);
|
2019-03-19 10:42:48 +08:00
|
|
|
if (cnt < 0)
|
2013-04-05 23:52:14 +08:00
|
|
|
pr_err("recvmsg%sfailed: %m",
|
|
|
|
flags == MSG_ERRQUEUE ? " tx timestamp " : " ");
|
2012-03-18 15:36:38 +08:00
|
|
|
|
2012-03-17 21:53:10 +08:00
|
|
|
for (cm = CMSG_FIRSTHDR(&msg); cm != NULL; cm = CMSG_NXTHDR(&msg, cm)) {
|
|
|
|
level = cm->cmsg_level;
|
|
|
|
type = cm->cmsg_type;
|
|
|
|
if (SOL_SOCKET == level && SO_TIMESTAMPING == type) {
|
|
|
|
if (cm->cmsg_len < sizeof(*ts) * 3) {
|
|
|
|
pr_warning("short SO_TIMESTAMPING message");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ts = (struct timespec *) CMSG_DATA(cm);
|
2013-08-29 16:54:55 +08:00
|
|
|
}
|
|
|
|
if (SOL_SOCKET == level && SO_TIMESTAMPNS == type) {
|
|
|
|
if (cm->cmsg_len < sizeof(*sw)) {
|
|
|
|
pr_warning("short SO_TIMESTAMPNS message");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sw = (struct timespec *) CMSG_DATA(cm);
|
2018-03-12 20:36:31 +08:00
|
|
|
hwts->sw = timespec_to_tmv(*sw);
|
2012-03-17 21:53:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 22:00:59 +08:00
|
|
|
if (addr)
|
|
|
|
addr->len = msg.msg_namelen;
|
|
|
|
|
2012-03-17 21:53:10 +08:00
|
|
|
if (!ts) {
|
|
|
|
memset(&hwts->ts, 0, sizeof(hwts->ts));
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (hwts->type) {
|
|
|
|
case TS_SOFTWARE:
|
2018-03-12 20:36:30 +08:00
|
|
|
hwts->ts = timespec_to_tmv(ts[0]);
|
2012-03-17 21:53:10 +08:00
|
|
|
break;
|
|
|
|
case TS_HARDWARE:
|
2012-11-30 04:01:16 +08:00
|
|
|
case TS_ONESTEP:
|
2018-02-16 01:16:15 +08:00
|
|
|
case TS_P2P1STEP:
|
2018-03-12 20:36:30 +08:00
|
|
|
hwts->ts = timespec_to_tmv(ts[2]);
|
2012-03-17 21:53:10 +08:00
|
|
|
break;
|
|
|
|
case TS_LEGACY_HW:
|
2018-03-12 20:36:30 +08:00
|
|
|
hwts->ts = timespec_to_tmv(ts[1]);
|
2012-03-17 21:53:10 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2018-10-24 21:07:06 +08:00
|
|
|
int sk_set_priority(int fd, int family, uint8_t dscp)
|
2016-07-12 19:52:48 +08:00
|
|
|
{
|
2018-10-24 21:07:06 +08:00
|
|
|
int level, optname, tos;
|
2016-07-12 19:52:48 +08:00
|
|
|
socklen_t tos_len;
|
|
|
|
|
2018-10-24 21:07:06 +08:00
|
|
|
switch (family) {
|
|
|
|
case AF_INET:
|
|
|
|
level = IPPROTO_IP;
|
|
|
|
optname = IP_TOS;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
level = IPPROTO_IPV6;
|
|
|
|
optname = IPV6_TCLASS;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-07-12 19:52:48 +08:00
|
|
|
tos_len = sizeof(tos);
|
2018-10-24 21:07:06 +08:00
|
|
|
if (getsockopt(fd, level, optname, &tos, &tos_len) < 0) {
|
2016-07-12 19:52:48 +08:00
|
|
|
tos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clear old DSCP value */
|
|
|
|
tos &= ~0xFC;
|
|
|
|
|
|
|
|
/* set new DSCP value */
|
|
|
|
tos |= dscp<<2;
|
|
|
|
tos_len = sizeof(tos);
|
2018-10-24 21:07:06 +08:00
|
|
|
if (setsockopt(fd, level, optname, &tos, tos_len) < 0) {
|
2016-07-12 19:52:48 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-08 00:52:03 +08:00
|
|
|
int sk_timestamping_init(int fd, const char *device, enum timestamp_type type,
|
2012-10-25 21:19:31 +08:00
|
|
|
enum transport_type transport)
|
2012-03-17 21:53:10 +08:00
|
|
|
{
|
2018-04-06 23:49:16 +08:00
|
|
|
int err, filter1, filter2 = 0, flags, tx_type = HWTSTAMP_TX_ON;
|
2012-03-17 21:53:10 +08:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case TS_SOFTWARE:
|
|
|
|
flags = SOF_TIMESTAMPING_TX_SOFTWARE |
|
|
|
|
SOF_TIMESTAMPING_RX_SOFTWARE |
|
|
|
|
SOF_TIMESTAMPING_SOFTWARE;
|
|
|
|
break;
|
|
|
|
case TS_HARDWARE:
|
2012-11-30 04:01:16 +08:00
|
|
|
case TS_ONESTEP:
|
2018-02-16 01:16:15 +08:00
|
|
|
case TS_P2P1STEP:
|
2012-03-17 21:53:10 +08:00
|
|
|
flags = SOF_TIMESTAMPING_TX_HARDWARE |
|
|
|
|
SOF_TIMESTAMPING_RX_HARDWARE |
|
|
|
|
SOF_TIMESTAMPING_RAW_HARDWARE;
|
|
|
|
break;
|
|
|
|
case TS_LEGACY_HW:
|
|
|
|
flags = SOF_TIMESTAMPING_TX_HARDWARE |
|
|
|
|
SOF_TIMESTAMPING_RX_HARDWARE |
|
|
|
|
SOF_TIMESTAMPING_SYS_HARDWARE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-10-26 02:55:28 +08:00
|
|
|
if (type != TS_SOFTWARE) {
|
|
|
|
filter1 = HWTSTAMP_FILTER_PTP_V2_EVENT;
|
2018-02-16 01:16:15 +08:00
|
|
|
switch (type) {
|
|
|
|
case TS_SOFTWARE:
|
|
|
|
tx_type = HWTSTAMP_TX_OFF;
|
|
|
|
break;
|
|
|
|
case TS_HARDWARE:
|
|
|
|
case TS_LEGACY_HW:
|
|
|
|
tx_type = HWTSTAMP_TX_ON;
|
|
|
|
break;
|
|
|
|
case TS_ONESTEP:
|
|
|
|
tx_type = HWTSTAMP_TX_ONESTEP_SYNC;
|
|
|
|
break;
|
|
|
|
case TS_P2P1STEP:
|
|
|
|
tx_type = HWTSTAMP_TX_ONESTEP_P2P;
|
|
|
|
break;
|
|
|
|
}
|
2012-10-26 02:55:28 +08:00
|
|
|
switch (transport) {
|
|
|
|
case TRANS_UDP_IPV4:
|
|
|
|
case TRANS_UDP_IPV6:
|
|
|
|
filter2 = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
|
|
|
|
break;
|
|
|
|
case TRANS_IEEE_802_3:
|
|
|
|
filter2 = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
|
|
|
|
break;
|
|
|
|
case TRANS_DEVICENET:
|
|
|
|
case TRANS_CONTROLNET:
|
|
|
|
case TRANS_PROFINET:
|
|
|
|
case TRANS_UDS:
|
|
|
|
return -1;
|
|
|
|
}
|
2018-08-29 04:05:28 +08:00
|
|
|
err = hwts_init(fd, device, filter1, filter2, tx_type);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2012-10-26 02:55:28 +08:00
|
|
|
}
|
2012-03-17 21:53:10 +08:00
|
|
|
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING,
|
|
|
|
&flags, sizeof(flags)) < 0) {
|
|
|
|
pr_err("ioctl SO_TIMESTAMPING failed: %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-01 19:27:14 +08:00
|
|
|
flags = 1;
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_SELECT_ERR_QUEUE,
|
|
|
|
&flags, sizeof(flags)) < 0) {
|
|
|
|
pr_warning("%s: SO_SELECT_ERR_QUEUE: %m", device);
|
|
|
|
sk_events = 0;
|
|
|
|
sk_revents = POLLERR;
|
|
|
|
}
|
|
|
|
|
2013-08-29 16:54:55 +08:00
|
|
|
/* Enable the sk_check_fupsync option, perhaps. */
|
|
|
|
if (sk_general_init(fd)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-03-17 21:53:10 +08:00
|
|
|
return 0;
|
|
|
|
}
|