Change in libosmocore[master]: socket: Introduce osmo_sock_set_dscp() to set socket DSCP value

laforge gerrit-no-reply at lists.osmocom.org
Wed Apr 28 06:51:49 UTC 2021


laforge has submitted this change. ( https://gerrit.osmocom.org/c/libosmocore/+/23920 )

Change subject: socket: Introduce osmo_sock_set_dscp() to set socket DSCP value
......................................................................

socket: Introduce osmo_sock_set_dscp() to set socket DSCP value

At least on Linux, sockets have a IP_TOS socket option that can be
configured to set the TOS.  However, TOS (of RFC791) was replaced
by the DSCP (of RFC2474) in 1998.

As the DCSP bits are only the upper 6 bits of the TOS bits, let's
introduce a helper to get, mask and set the DSCP values in the TOS
bits.

Related: OS#5136, SYS#5427
Change-Id: Ia4ba389a5b7e3e9d5f17a742a900d6fd68c08e40
---
M include/osmocom/core/socket.h
M src/socket.c
2 files changed, 28 insertions(+), 0 deletions(-)

Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved



diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h
index 0443c06..92cd202 100644
--- a/include/osmocom/core/socket.h
+++ b/include/osmocom/core/socket.h
@@ -122,5 +122,7 @@
 char *osmo_sockaddr_to_str_buf(char *buf, size_t buf_len,
 			       const struct osmo_sockaddr *sockaddr);
 
+int osmo_sock_set_dscp(int fd, uint8_t dscp);
+
 #endif /* (!EMBEDDED) */
 /*! @} */
diff --git a/src/socket.c b/src/socket.c
index 229f72e..095dee6 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -1776,6 +1776,32 @@
 	return buf;
 }
 
+/*! Set the DSCP (differentiated services code point) of a socket.
+ *  \param[in] dscp DSCP value in range 0..63
+ *  \returns 0 on success; negative on error. */
+int osmo_sock_set_dscp(int fd, uint8_t dscp)
+{
+	uint8_t tos;
+	socklen_t tos_len = sizeof(tos);
+	int rc;
+
+	/* DSCP is a 6-bit value stored in the upper 6 bits of the 8-bit TOS */
+	if (dscp > 63)
+		return -EINVAL;
+
+	/* read the original value */
+	rc = getsockopt(fd, IPPROTO_IP, IP_TOS, &tos, &tos_len);
+	if (rc < 0)
+		return rc;
+
+	/* mask-in the DSCP into the upper 6 bits */
+	tos &= 0x03;
+	tos |= dscp << 2;
+
+	/* and write it back to the kernel */
+	return setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
+}
+
 
 #endif /* HAVE_SYS_SOCKET_H */
 

-- 
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/23920
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: Ia4ba389a5b7e3e9d5f17a742a900d6fd68c08e40
Gerrit-Change-Number: 23920
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210428/2350208d/attachment.htm>


More information about the gerrit-log mailing list