From: Henry Jesuiter <henry.jesui...@alcnetworx.de>

In the last years there are several media streaming standards
evolving that are relying on PTP. These standards make requirements
about the DSCP priority of PTP messages. This patch introduces two
new configuration options 'tos_event' and 'tos_general' to address
that issue and to be able to set the DSCP priority separately for
PTP event messages and PTP general messages.

[ RC: - Changed sk.c method to operate on a single socket.
      - Arranged new sk.c method in alphabetical order. ]

Signed-off-by: Henry Jesuiter <henry.jesui...@alcnetworx.de>
Signed-off-by: Richard Cochran <richardcoch...@gmail.com>
---
 config.c    |  2 ++
 default.cfg |  2 ++
 ptp4l.8     | 14 ++++++++++++++
 sk.c        | 23 +++++++++++++++++++++++
 sk.h        |  8 ++++++++
 udp.c       | 11 +++++++++++
 udp6.c      | 11 +++++++++++
 7 files changed, 71 insertions(+)

diff --git a/config.c b/config.c
index 80fa255..908b538 100644
--- a/config.c
+++ b/config.c
@@ -226,6 +226,8 @@ struct config_item config_tab[] = {
        PORT_ITEM_INT("syncReceiptTimeout", 0, 0, UINT8_MAX),
        GLOB_ITEM_INT("timeSource", INTERNAL_OSCILLATOR, 0x10, 0xfe),
        GLOB_ITEM_ENU("time_stamping", TS_HARDWARE, timestamping_enu),
+       GLOB_ITEM_INT("tos_event", 0, 0, 63),
+       GLOB_ITEM_INT("tos_general", 0, 0, 63),
        PORT_ITEM_INT("transportSpecific", 0, 0, 0x0F),
        PORT_ITEM_ENU("tsproc_mode", TSPROC_FILTER, tsproc_enu),
        GLOB_ITEM_INT("twoStepFlag", 1, 0, 1),
diff --git a/default.cfg b/default.cfg
index 67f5b3a..00b3318 100644
--- a/default.cfg
+++ b/default.cfg
@@ -12,6 +12,8 @@ clockAccuracy         0xFE
 offsetScaledLogVariance        0xFFFF
 free_running           0
 freq_est_interval      1
+tos_event              0
+tos_general            0
 #
 # Port Data Set
 #
diff --git a/ptp4l.8 b/ptp4l.8
index 72baf86..a167e2e 100644
--- a/ptp4l.8
+++ b/ptp4l.8
@@ -448,6 +448,20 @@ is only relevant with IPv6 transport.  See RFC 4291.  The 
default is
 Specifies the address of the UNIX domain socket for receiving local
 management messages. The default is /var/run/ptp4l.
 .TP
+.B tos_event
+Defines the Differentiated Services Codepoint (DSCP) to be used for PTP
+event messages. Must be a value between 0 and 63. There are several media
+streaming standards out there that require specific values for this option.
+For example 46 (EF PHB) in AES67 or 48 (CS6 PHB) in RAVENNA. The default
+is 0.
+.TP
+.B tos_general
+Defines the Differentiated Services Codepoint (DSCP) to be used for PTP
+general messages. Must be a value between 0 and 63. There are several media
+streaming standards out there that recommend specific values for this option.
+For example 34 (AF41 PHB) in AES67 or 46 (EF PHB) in RAVENNA. The default
+is 0.
+.TP
 .B logging_level
 The maximum logging level of messages which should be printed.
 The default is 6 (LOG_INFO).
diff --git a/sk.c b/sk.c
index e80f608..ad30f71 100644
--- a/sk.c
+++ b/sk.c
@@ -298,6 +298,29 @@ int sk_receive(int fd, void *buf, int buflen,
        return cnt;
 }
 
+int sk_set_priority(int fd, uint8_t dscp)
+{
+       int tos;
+       socklen_t tos_len;
+
+       tos_len = sizeof(tos);
+       if (getsockopt(fd, SOL_IP, IP_TOS, &tos, &tos_len) < 0) {
+               tos = 0;
+       }
+
+       /* clear old DSCP value */
+       tos &= ~0xFC;
+
+       /* set new DSCP value */
+       tos |= dscp<<2;
+       tos_len = sizeof(tos);
+       if (setsockopt(fd, SOL_IP, IP_TOS, &tos, tos_len) < 0) {
+               return -1;
+       }
+
+       return 0;
+}
+
 int sk_timestamping_init(int fd, const char *device, enum timestamp_type type,
                         enum transport_type transport)
 {
diff --git a/sk.h b/sk.h
index f05d1df..31cc88e 100644
--- a/sk.h
+++ b/sk.h
@@ -94,6 +94,14 @@ int sk_receive(int fd, void *buf, int buflen,
               struct address *addr, struct hw_timestamp *hwts, int flags);
 
 /**
+ * Set DSCP value for socket.
+ * @param fd    An open socket.
+ * @param dscp  The desired DSCP code.
+ * @return Zero on success, negative on failure
+ */
+int sk_set_priority(int fd, uint8_t dscp);
+
+/**
  * Enable time stamping on a given network interface.
  * @param fd          An open socket.
  * @param device      The name of the network interface to configure.
diff --git a/udp.c b/udp.c
index 07277c7..83a665f 100644
--- a/udp.c
+++ b/udp.c
@@ -157,6 +157,7 @@ static int udp_open(struct transport *t, const char *name, 
struct fdarray *fda,
                    enum timestamp_type ts_type)
 {
        struct udp *udp = container_of(t, struct udp, t);
+       uint8_t event_dscp, general_dscp;
        int efd, gfd, ttl;
 
        ttl = config_get_int(t->cfg, name, "udp_ttl");
@@ -186,6 +187,16 @@ static int udp_open(struct transport *t, const char *name, 
struct fdarray *fda,
        if (sk_general_init(gfd))
                goto no_timestamping;
 
+       event_dscp = config_get_int(t->cfg, NULL, "tos_event");
+       general_dscp = config_get_int(t->cfg, NULL, "tos_general");
+
+       if (event_dscp && sk_set_priority(efd, event_dscp)) {
+               pr_warning("Failed to set event DSCP priority.");
+       }
+       if (general_dscp && sk_set_priority(gfd, general_dscp)) {
+               pr_warning("Failed to set general DSCP priority.");
+       }
+
        fda->fd[FD_EVENT] = efd;
        fda->fd[FD_GENERAL] = gfd;
        return 0;
diff --git a/udp6.c b/udp6.c
index 070e762..b185e00 100644
--- a/udp6.c
+++ b/udp6.c
@@ -165,6 +165,7 @@ static int udp6_open(struct transport *t, const char *name, 
struct fdarray *fda,
                    enum timestamp_type ts_type)
 {
        struct udp6 *udp6 = container_of(t, struct udp6, t);
+       uint8_t event_dscp, general_dscp;
        int efd, gfd, hop_limit;
 
        hop_limit = config_get_int(t->cfg, name, "udp_ttl");
@@ -196,6 +197,16 @@ static int udp6_open(struct transport *t, const char 
*name, struct fdarray *fda,
        if (sk_general_init(gfd))
                goto no_timestamping;
 
+       event_dscp = config_get_int(t->cfg, NULL, "tos_event");
+       general_dscp = config_get_int(t->cfg, NULL, "tos_general");
+
+       if (event_dscp && sk_set_priority(efd, event_dscp)) {
+               pr_warning("Failed to set event DSCP priority.");
+       }
+       if (general_dscp && sk_set_priority(gfd, general_dscp)) {
+               pr_warning("Failed to set general DSCP priority.");
+       }
+
        fda->fd[FD_EVENT] = efd;
        fda->fd[FD_GENERAL] = gfd;
        return 0;
-- 
2.1.4


------------------------------------------------------------------------------
Attend Shape: An AT&T Tech Expo July 15-16. Meet us at AT&T Park in San
Francisco, CA to explore cutting-edge tech and listen to tech luminaries
present their vision of the future. This family event has something for
everyone, including kids. Get more information and register today.
http://sdm.link/attshape
_______________________________________________
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel

Reply via email to