]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
conf: add dscp directive
authorMiroslav Lichvar <mlichvar@redhat.com>
Mon, 3 Aug 2020 15:55:42 +0000 (17:55 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Tue, 4 Aug 2020 10:24:49 +0000 (12:24 +0200)
The directive sets the DSCP value in transmitted NTP packets, which can
be useful in local networks where switches/routers are configured to
prioritise packets with specific DSCP values.

conf.c
conf.h
doc/chrony.conf.adoc
ntp_io.c

diff --git a/conf.c b/conf.c
index 912ac82540d4407a39b5482914eb44bcd49444ef..b3061c96065705a106e68e551ce704d1a36cef72 100644 (file)
--- a/conf.c
+++ b/conf.c
@@ -201,6 +201,9 @@ static char *bind_cmd_iface = NULL;
 /* Path to the Unix domain command socket. */
 static char *bind_cmd_path = NULL;
 
+/* Differentiated Services Code Point (DSCP) in transmitted NTP packets */
+static int ntp_dscp = 0;
+
 /* Path to Samba (ntp_signd) socket. */
 static char *ntp_signd_socket = NULL;
 
@@ -560,6 +563,8 @@ CNF_ParseLine(const char *filename, int number, char *line)
     parse_allow_deny(p, ntp_restrictions, 0);
   } else if (!strcasecmp(command, "driftfile")) {
     parse_string(p, &drift_file);
+  } else if (!strcasecmp(command, "dscp")) {
+    parse_int(p, &ntp_dscp);
   } else if (!strcasecmp(command, "dumpdir")) {
     parse_string(p, &dumpdir);
   } else if (!strcasecmp(command, "dumponexit")) {
@@ -2297,6 +2302,14 @@ CNF_GetBindCommandAddress(int family, IPAddr *addr)
 
 /* ================================================== */
 
+int
+CNF_GetNtpDscp(void)
+{
+  return ntp_dscp;
+}
+
+/* ================================================== */
+
 char *
 CNF_GetNtpSigndSocket(void)
 {
diff --git a/conf.h b/conf.h
index c35f24501e6249538f3818bff435a0b1edbdd796..b1f6e6413b700c36e0c98aa8993891ba17fb7554 100644 (file)
--- a/conf.h
+++ b/conf.h
@@ -83,6 +83,7 @@ extern char *CNF_GetBindNtpInterface(void);
 extern char *CNF_GetBindAcquisitionInterface(void);
 extern char *CNF_GetBindCommandInterface(void);
 extern char *CNF_GetBindCommandPath(void);
+extern int CNF_GetNtpDscp(void);
 extern char *CNF_GetNtpSigndSocket(void);
 extern char *CNF_GetPidFile(void);
 extern REF_LeapMode CNF_GetLeapSecMode(void);
index 51e1d8870cb0bb9a94398e7a4aba51c1674acece..5ea864db86286bef7822014c112e819599f4ab45 100644 (file)
@@ -652,6 +652,19 @@ An example of the directive is:
 bindacqdevice eth0
 ----
 
+[[dscp]]*dscp* _point_::
+The *dscp* directive sets the Differentiated Services Code Point (DSCP) in
+transmitted NTP packets to the specified value. It can improve stability of NTP
+measurements in local networks where switches or routers are configured to
+prioritise forwarding of packets with specific DSCP values. The default value
+is 0 and the maximum value is 63.
++
+An example of the directive (setting the Expedited Forwarding class) is:
++
+----
+dscp 46
+----
+
 [[dumpdir]]*dumpdir* _directory_::
 To compute the rate of gain or loss of time, *chronyd* has to store a
 measurement history for each of the time sources it uses.
index 075f0cf714c70a4231a2016ac380f9a5a38c1378..9d18df84c6f993394c1af34f54f7e3af97fbec4f 100644 (file)
--- a/ntp_io.c
+++ b/ntp_io.c
@@ -83,7 +83,7 @@ static void read_from_socket(int sock_fd, int event, void *anything);
 static int
 open_socket(int family, int local_port, int client_only, IPSockAddr *remote_addr)
 {
-  int sock_fd, sock_flags, events = SCH_FILE_INPUT;
+  int sock_fd, sock_flags, dscp, events = SCH_FILE_INPUT;
   IPSockAddr local_addr;
   char *iface;
 
@@ -111,6 +111,14 @@ open_socket(int family, int local_port, int client_only, IPSockAddr *remote_addr
     return INVALID_SOCK_FD;
   }
 
+  dscp = CNF_GetNtpDscp();
+  if (dscp > 0 && dscp < 64) {
+#ifdef IP_TOS
+    if (!SCK_SetIntOption(sock_fd, IPPROTO_IP, IP_TOS, dscp << 2))
+      ;
+#endif
+  }
+
   if (!client_only && family == IPADDR_INET4 && local_addr.port > 0)
     bound_server_sock_fd4 = local_addr.ip_addr.addr.in4 != INADDR_ANY;