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
This commit is contained in:
Harald Welte 2021-04-27 21:44:34 +02:00
parent 069967be86
commit ce53e03dc9
2 changed files with 28 additions and 0 deletions

View File

@ -122,5 +122,7 @@ const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *sockaddr);
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) */
/*! @} */

View File

@ -1776,6 +1776,32 @@ char *osmo_sockaddr_to_str_buf(char *buf, size_t buf_len,
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 */