[Linuxptp-devel] [PATCH 3/4] [Interface Rate TLV] organization TLV support for interface rate

2022-10-10 Thread SyncMonk Technologies
From: devasishsyncmonk 

adding interface rate TLV as defined by ITU-T G.8275.2 Annex D to
enable master to communicate PTP port interface rate to slave.

Signed-off-by: Greg Armstrong 
Signed-off-by: Leon Goldin 
Signed-off-by: Devasish Dey 
Signed-off-by: Vipin Sharma 

-v1: initial commit
-v2: maintaining alphabetical sequence for the config.
---
 clock.c  |  7 +++
 clock.h  |  7 +++
 config.c |  1 +
 pdt.h|  1 +
 tlv.c|  1 +
 tlv.h| 15 +++
 6 files changed, 32 insertions(+)

diff --git a/clock.c b/clock.c
index 086b1a8..c6708e0 100644
--- a/clock.c
+++ b/clock.c
@@ -137,6 +137,7 @@ struct clock {
struct monitor *slave_event_monitor;
int step_window_counter;
int step_window;
+   bool iface_rate_tlv;
 };
 
 struct clock the_clock;
@@ -1106,6 +1107,7 @@ struct clock *clock_create(enum clock_type type, struct 
config *config,
c->utc_offset = config_get_int(config, NULL, "utc_offset");
c->time_source = config_get_int(config, NULL, "timeSource");
c->step_window = config_get_int(config, NULL, "step_window");
+   c->iface_rate_tlv = config_get_int(config, NULL, "interface_rate_tlv");
 
if (c->free_running) {
c->clkid = CLOCK_INVALID;
@@ -2083,6 +2085,11 @@ struct servo *clock_servo(struct clock *c)
return c->servo;
 }
 
+bool clock_interface_rate_tlv (struct clock *c)
+{
+   return c->iface_rate_tlv;
+}
+
 enum servo_state clock_servo_state(struct clock *c)
 {
return c->servo_state;
diff --git a/clock.h b/clock.h
index 0534f21..b393c3e 100644
--- a/clock.h
+++ b/clock.h
@@ -388,4 +388,11 @@ void clock_check_ts(struct clock *c, uint64_t ts);
  */
 double clock_rate_ratio(struct clock *c);
 
+/**
+ * Obtain interface rate tlv support configuration.
+ * @param c  The clock instance.
+ * @return   The interface_rate_supoort config true if supported, else false.
+ */
+bool clock_interface_rate_tlv(struct clock *c);
+
 #endif
diff --git a/config.c b/config.c
index e454c91..be19143 100644
--- a/config.c
+++ b/config.c
@@ -267,6 +267,7 @@ struct config_item config_tab[] = {
PORT_ITEM_INT("inhibit_delay_req", 0, 0, 1),
PORT_ITEM_INT("inhibit_multicast_service", 0, 0, 1),
GLOB_ITEM_INT("initial_delay", 0, 0, INT_MAX),
+   PORT_ITEM_INT("interface_rate_tlv", 0, 0, 1),
GLOB_ITEM_INT("kernel_leap", 1, 0, 1),
GLOB_ITEM_STR("leapfile", NULL),
PORT_ITEM_INT("logAnnounceInterval", 1, INT8_MIN, INT8_MAX),
diff --git a/pdt.h b/pdt.h
index e46b218..1ad23d4 100644
--- a/pdt.h
+++ b/pdt.h
@@ -39,6 +39,7 @@ typedef uint16_t  UInteger16;
 typedef int32_t   Integer32;
 typedef uint32_t  UInteger32;
 typedef int64_t   Integer64;
+typedef uint64_t  UInteger64;
 typedef uint8_t   Octet;
 
 #endif
diff --git a/tlv.c b/tlv.c
index 1c13460..35bee4f 100644
--- a/tlv.c
+++ b/tlv.c
@@ -35,6 +35,7 @@
(tlv->length < sizeof(struct type) - sizeof(struct TLV))
 
 uint8_t ieee8021_id[3] = { IEEE_802_1_COMMITTEE };
+uint8_t itu_t_id[3] = { ITU_T_COMMITTEE };
 
 static TAILQ_HEAD(tlv_pool, tlv_extra) tlv_pool =
TAILQ_HEAD_INITIALIZER(tlv_pool);
diff --git a/tlv.h b/tlv.h
index 8966696..1dfaac8 100644
--- a/tlv.h
+++ b/tlv.h
@@ -395,6 +395,21 @@ struct tlv_extra {
};
 };
 
+/* Organizationally Unique Identifiers */
+#define ITU_T_COMMITTEE 0x00, 0x19, 0xA7
+extern uint8_t itu_t_id[3];
+
+struct msg_interface_rate_tlv {
+   Enumeration16 type;
+   UInteger16length;
+   Octet id[3];
+   Octet subtype[3];
+   UInteger64interfaceBitPeriod;
+   UInteger16numberOfBitsBeforeTimestamp;
+   UInteger16numberOfBitsAfterTimestamp;
+} PACKED;
+
+
 /**
  * Allocates a new tlv_extra structure.
  * @return  Pointer to a new structure on success or NULL otherwise.
-- 
2.34.1



___
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel


[Linuxptp-devel] [PATCH 1/4] [Interface Rate TLV] function to support get interface speed via ethtool

2022-10-10 Thread SyncMonk Technologies
From: devasishsyncmonk 

When master and slave instance interacting with each other operating
at different interface speed, delay assymetry needs to be compensated
as described in G.8271 appendix V.

In this patch we are adding changes to get the interface speed using
ethtool.

v1: initial commit.
v2: updating comments and data types.

Signed-off-by: Greg Armstrong 
Signed-off-by: Leon Goldin 
Signed-off-by: Devasish Dey 
Signed-off-by: Vipin Sharma 
---
 sk.c | 71 
 sk.h | 20 +
 2 files changed, 91 insertions(+)

diff --git a/sk.c b/sk.c
index 80075be..8525603 100644
--- a/sk.c
+++ b/sk.c
@@ -205,6 +205,77 @@ failed:
return -1;
 }
 
+int sk_get_if_info(const char *name, struct sk_if_info *if_info)
+{
+#ifdef ETHTOOL_GLINKSETTINGS
+   struct ifreq ifr;
+   int fd, err;
+
+   struct {
+   struct ethtool_link_settings req;
+   /*
+* link_mode_data consists of supported[], advertising[],
+* lp_advertising[] with size up to 127 each.
+* The actual size is provided by the kernel.
+*/
+   __u32 link_mode_data[3 * 127];
+   } ecmd;
+
+   memset(&ifr, 0, sizeof(ifr));
+   memset(&ecmd, 0, sizeof(ecmd));
+
+   fd = socket(AF_INET, SOCK_DGRAM, 0);
+   if (fd < 0) {
+   goto failed;
+   }
+
+   ecmd.req.cmd = ETHTOOL_GLINKSETTINGS;
+
+   strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
+   ifr.ifr_data = (char *) &ecmd;
+
+   /* Handshake with kernel to determine number of words for link
+* mode bitmaps. When requested number of bitmap words is not
+* the one expected by kernel, the latter returns the integer
+* opposite of what it is expecting. We request length 0 below
+* (aka. invalid bitmap length) to get this info.
+*/
+   err = ioctl(fd, SIOCETHTOOL, &ifr);
+   if (err < 0) {
+   pr_err("ioctl SIOCETHTOOL failed: %m");
+   close(fd);
+   goto failed;
+   }
+
+   if (ecmd.req.link_mode_masks_nwords >= 0 ||
+   ecmd.req.cmd != ETHTOOL_GLINKSETTINGS) {
+   return 1;
+   }
+   ecmd.req.link_mode_masks_nwords = -ecmd.req.link_mode_masks_nwords;
+
+   err = ioctl(fd, SIOCETHTOOL, &ifr);
+   if (err < 0) {
+   pr_err("ioctl SIOCETHTOOL failed: %m");
+   close(fd);
+   goto failed;
+   }
+
+   close(fd);
+
+   /* copy the necessary data to sk_info */
+   memset(if_info, 0, sizeof(struct sk_if_info));
+   if_info->valid = 1;
+   if_info->speed = ecmd.req.speed;
+
+   return 0;
+failed:
+#endif
+   /* clear data and ensure it is not marked valid */
+   memset(if_info, 0, sizeof(struct sk_if_info));
+   return -1;
+}
+
+
 static int sk_interface_guidaddr(const char *name, unsigned char *guid)
 {
char file_name[64], buf[64], addr[8];
diff --git a/sk.h b/sk.h
index 486dbc4..ea19a8e 100644
--- a/sk.h
+++ b/sk.h
@@ -49,6 +49,17 @@ struct sk_ts_info {
unsigned int rx_filters;
 };
 
+/**
+ * Contains interface information returned by theGLINKSETTINGS ioctl.
+ * @valid:set to non-zero when the info struct contains valid data.
+ * @speed:interface speed.
+ */
+struct sk_if_info {
+   Boolean valid;
+   int speed;
+};
+
+
 /**
  * Obtains a socket suitable for use with sk_interface_index().
  * @return  An open socket on success, -1 otherwise.
@@ -78,6 +89,15 @@ int sk_general_init(int fd);
  */
 int sk_get_ts_info(const char *name, struct sk_ts_info *sk_info);
 
+/**
+ * Obtain supporte interface information
+ * @param name The name of the interface
+ * @param info  Struct containing obtained interface information.
+ * @return  zero on success, negative on failure.
+ */
+int sk_get_if_info(const char *name, struct sk_if_info *sk_info);
+
+
 /**
  * Obtain the MAC address of a network interface.
  * @param name  The name of the interface
-- 
2.34.1



___
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel


[Linuxptp-devel] [PATCH 2/4] [Interface Rate TLV] adding speed field information for interface

2022-10-10 Thread SyncMonk Technologies
From: devasishsyncmonk 

Get the interface speed related information using ethtool

Signed-off-by: Greg Armstrong 
Signed-off-by: Leon Goldin 
Signed-off-by: Devasish Dey 
Signed-off-by: Vipin Sharma 
---
 clock.c |  2 ++
 interface.c | 11 +++
 interface.h | 14 ++
 port.c  |  4 
 4 files changed, 31 insertions(+)

diff --git a/clock.c b/clock.c
index d37bb87..086b1a8 100644
--- a/clock.c
+++ b/clock.c
@@ -1004,6 +1004,8 @@ struct clock *clock_create(enum clock_type type, struct 
config *config,
memset(ts_label, 0, sizeof(ts_label));
if (!rtnl_get_ts_device(interface_name(iface), ts_label))
interface_set_label(iface, ts_label);
+/* Interface speed information */
+interface_get_ifinfo(iface);
interface_get_tsinfo(iface);
if (interface_tsinfo_valid(iface) &&
!interface_tsmodes_supported(iface, required_modes)) {
diff --git a/interface.c b/interface.c
index 6c2630c..9c30499 100644
--- a/interface.c
+++ b/interface.c
@@ -12,6 +12,7 @@ struct interface {
char name[MAX_IFNAME_SIZE + 1];
char ts_label[MAX_IFNAME_SIZE + 1];
struct sk_ts_info ts_info;
+struct sk_if_info if_info;
int vclock;
 };
 
@@ -40,11 +41,21 @@ int interface_get_tsinfo(struct interface *iface)
return sk_get_ts_info(iface->ts_label, &iface->ts_info);
 }
 
+int interface_get_ifinfo(struct interface *iface)
+{
+   return sk_get_if_info(iface->ts_label, &iface->if_info);
+}
+
 const char *interface_label(struct interface *iface)
 {
return iface->ts_label;
 }
 
+bool interface_ifinfo_valid(struct interface *iface)
+{
+   return iface->if_info.valid ? true : false;
+}
+
 const char *interface_name(struct interface *iface)
 {
return iface->name;
diff --git a/interface.h b/interface.h
index 5fc7836..5289a7f 100644
--- a/interface.h
+++ b/interface.h
@@ -40,6 +40,13 @@ void interface_destroy(struct interface *iface);
  */
 int interface_get_tsinfo(struct interface *iface);
 
+/**
+ * Populate the time stamping information of a given interface.
+ * @param iface  The interface of interest.
+ * @return   zero on success, negative on failure.
+ */
+int interface_get_ifinfo(struct interface *iface);
+
 /**
  * Obtain the time stamping label of a network interface.  This can be
  * different from the name of the interface when bonding is in effect.
@@ -77,6 +84,13 @@ void interface_set_label(struct interface *iface, const char 
*label);
  */
 bool interface_tsinfo_valid(struct interface *iface);
 
+/**
+ * Tests whether an interface's interface information is valid or not.
+ * @param iface  The interface of interest.
+ * @return   True if the interface information is valid, false otherwise.
+ */
+bool interface_ifinfo_valid(struct interface *iface);
+
 /**
  * Tests whether an interface supports a set of given time stamping modes.
  * @param iface  The interface of interest.
diff --git a/port.c b/port.c
index 871ad68..ca508a6 100644
--- a/port.c
+++ b/port.c
@@ -2726,6 +2726,10 @@ void port_link_status(void *ctx, int linkup, int 
ts_index)
p->link_status = link_state;
} else {
p->link_status = link_state | LINK_STATE_CHANGED;
+   /* Update Interface speed information on Link up*/
+if (linkup) {
+interface_get_ifinfo(p->iface);
+}
pr_notice("%s: link %s", p->log_name, linkup ? "up" : "down");
}
 
-- 
2.34.1



___
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel


[Linuxptp-devel] [PATCH 4/4] [Interface Rate TLV] adding delay asymmetry calculation

2022-10-10 Thread SyncMonk Technologies
From: devasishsyncmonk 

Delay asymmetry calculation based on the PTP port interface speed of
master obtained from TLV and the slave interface rate obtained by ethtool.

Signed-off-by: Greg Armstrong 
Signed-off-by: Leon Goldin 
Signed-off-by: Devasish Dey 
Signed-off-by: Vipin Sharma 
---
 interface.c   |  9 +
 interface.h   |  7 +++
 port_private.h|  1 +
 port_signaling.c  | 32 
 ptp4l.8   |  7 +++
 unicast_service.c | 35 +++
 6 files changed, 91 insertions(+)

diff --git a/interface.c b/interface.c
index 9c30499..6237e88 100644
--- a/interface.c
+++ b/interface.c
@@ -93,3 +93,12 @@ int interface_get_vclock(struct interface *iface)
 {
return iface->vclock;
 }
+
+uint64_t interface_bitperiod(struct interface *iface)
+{
+   if (!iface->if_info.valid)
+   return 0;
+
+   /* Megabits per secon converted to attoseconds per bit. */
+   return 1ULL/ iface->if_info.speed;
+}
diff --git a/interface.h b/interface.h
index 5289a7f..e1dc0e8 100644
--- a/interface.h
+++ b/interface.h
@@ -113,4 +113,11 @@ void interface_set_vclock(struct interface *iface, int 
vclock);
  */
 int interface_get_vclock(struct interface *iface);
 
+/**
+ * Obtains the interface bit period based on the speed.
+ * @param iface  The interface of interest.
+ * @return   if valid speed return interface bitperiod in atto seconds.
+ */
+uint64_t interface_bitperiod(struct interface *iface);
+
 #endif
diff --git a/port_private.h b/port_private.h
index d27dceb..d1a1e76 100644
--- a/port_private.h
+++ b/port_private.h
@@ -145,6 +145,7 @@ struct port {
UInteger8   versionNumber; /* UInteger4 */
UInteger8   delay_response_counter;
UInteger8   delay_response_timeout;
+   Integer64   portAsymmetry;
struct PortStatsstats;
struct PortServiceStatsservice_stats;
/* foreignMasterDS */
diff --git a/port_signaling.c b/port_signaling.c
index ed217c0..d78acb6 100644
--- a/port_signaling.c
+++ b/port_signaling.c
@@ -103,10 +103,38 @@ static int process_interval_request(struct port *p,
return 0;
 }
 
+static int process_interface_rate(struct port *p,
+ struct msg_interface_rate_tlv *r)
+{
+   Integer64 delayAsymmetry;
+   doublensDelay;
+   Integer64 slaveBitPeriod;
+   Integer64 masterBitPeriod;
+
+   if (clock_interface_rate_tlv (p->clock) &&
+   interface_ifinfo_valid(p->iface)) {
+   slaveBitPeriod = interface_bitperiod(p->iface);
+   masterBitPeriod = r->interfaceBitPeriod;
+
+   /* Delay Asymmetry Calculation */
+   nsDelay = (masterBitPeriod - slaveBitPeriod) / (2 * 1.0e9);
+   delayAsymmetry =
+   (r->numberOfBitsAfterTimestamp - 
r->numberOfBitsBeforeTimestamp)  * nsDelay;
+
+   if (delayAsymmetry != p->portAsymmetry) {
+   p->asymmetry += ((delayAsymmetry - p->portAsymmetry) << 
16);
+   p->portAsymmetry = delayAsymmetry;
+   }
+   }
+   return 0;
+}
+
+
 int process_signaling(struct port *p, struct ptp_message *m)
 {
struct tlv_extra *extra;
struct msg_interval_req_tlv *r;
+   struct msg_interface_rate_tlv *rate;
int err = 0, result;
 
switch (p->state) {
@@ -161,10 +189,14 @@ int process_signaling(struct port *p, struct ptp_message 
*m)
 
case TLV_ORGANIZATION_EXTENSION:
r = (struct msg_interval_req_tlv *) extra->tlv;
+   rate = (struct msg_interface_rate_tlv *) extra->tlv;
 
if (0 == memcmp(r->id, ieee8021_id, 
sizeof(ieee8021_id)) &&
r->subtype[0] == 0 && r->subtype[1] == 0 && 
r->subtype[2] == 2)
err = process_interval_request(p, r);
+   else if (0 == memcmp(r->id, itu_t_id, sizeof(itu_t_id)) 
&&
+   r->subtype[0] == 0 && r->subtype[1] == 0 && 
r->subtype[2] == 2)
+   err = process_interface_rate(p, rate);
break;
}
}
diff --git a/ptp4l.8 b/ptp4l.8
index 1268802..eab4b47 100644
--- a/ptp4l.8
+++ b/ptp4l.8
@@ -695,6 +695,13 @@ The default is 0 (disabled).
 Print messages to the system log if enabled.
 The default is 1 (enabled).
 .TP
+.B interface_rate_tlv
+When the client and server are operating are operating at different interface 
rate,
+delay asymmetry caused due to different interface rate needs to be compensated.
+The server sends its interface rate using interface rate TLV
+as per G.8275.2 Annex D.
+The default is 0 (does not support interface rate tlv).
+
 .B summary_interval
 The time interval in which are printed summary statistics of the clock. It is
 specified as a power of

Re: [Linuxptp-devel] [PATCH v3 1/3] Adding virtual PTP port support

2022-10-10 Thread SyncMonk Tech Services
Hi Richard,

We appreciate your effort and time.

Thanks a lot !!!
SyncMonk Technologies.

On Sun, 9 Oct 2022 at 01:29, Richard Cochran 
wrote:

> On Thu, Oct 06, 2022 at 08:58:31PM +0530, SyncMonk Tech Services wrote:
> > Please let us know if there are any action items on us related to this or
> > other patches raised by SyncMonk.
>
> You need to address the review comments:
>
>As presented, this functionality won't work correctly.  If the ts2phc
>becomes slave as a result of the BMCA, then it must stop adjusting the
>PHC devices!
>
> IOW, ts2phc must run the BCMA.
>
> Also, I just merged a major change/refactoring of ts2phc, and so I
> suspect you will need to rebase.
>
> Thanks,
> Richard
>
___
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel