Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

udp: add win32 qos support #186

Merged
merged 1 commit into from
Jan 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 61 additions & 2 deletions src/udp/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@
#include <re_sa.h>
#include <re_net.h>
#include <re_udp.h>

#ifdef WIN32
typedef UINT32 QOS_FLOWID, *PQOS_FLOWID;
#ifndef QOS_NON_ADAPTIVE_FLOW
#define QOS_NON_ADAPTIVE_FLOW 0x00000002
#endif
#include <winsock2.h>
#include <qos2.h>
#endif

#define DEBUG_MODULE "udp"
#define DEBUG_LEVEL 5
Expand Down Expand Up @@ -67,6 +74,11 @@ struct udp_sock {
bool conn; /**< Connected socket flag */
size_t rxsz; /**< Maximum receive chunk size */
size_t rx_presz; /**< Preallocated rx buffer size */
#ifdef WIN32
HANDLE qos; /**< QOS subsystem handle */
QOS_FLOWID qos_id; /**< QOS IPv4 flow id */
QOS_FLOWID qos_id6; /**< QOS IPv6 flow id */
#endif
};

/** Defines a UDP helper */
Expand Down Expand Up @@ -115,6 +127,15 @@ static void udp_destructor(void *data)

list_flush(&us->helpers);

#ifdef WIN32
if (us->qos && us->qos_id)
(void)QOSRemoveSocketFromFlow(us->qos, 0, us->qos_id, 0);
if (us->qos && us->qos_id6)
(void)QOSRemoveSocketFromFlow(us->qos, 0, us->qos_id6, 0);
if (us->qos)
(void)QOSCloseHandle(us->qos);
#endif

if (-1 != us->fd) {
fd_close(us->fd);
(void)close(us->fd);
Expand Down Expand Up @@ -643,10 +664,48 @@ int udp_settos(struct udp_sock *us, uint8_t tos)
{
int err = 0;
int v = tos;

#ifdef WIN32
QOS_VERSION qos_version = { 1 , 0 };
QOS_TRAFFIC_TYPE qos_type = QOSTrafficTypeBestEffort;
if (tos >= 32) /* >= DSCP_CS1 */
qos_type = QOSTrafficTypeBackground;
if (tos >= 40) /* >= DSCP_AF11 */
qos_type = QOSTrafficTypeExcellentEffort;
if (tos >= 136) /* >= DSCP_AF41 */
qos_type = QOSTrafficTypeAudioVideo;
if (tos >= 184) /* >= DSCP_EF */
qos_type = QOSTrafficTypeVoice;
if (tos >= 224) /* >= DSCP_CS7 */
qos_type = QOSTrafficTypeControl;
#endif
if (!us)
return EINVAL;

#ifdef WIN32
err = QOSCreateHandle(&qos_version, &us->qos);
if (!err)
return GetLastError();

us->qos_id = 0;
if (-1 != us->fd) {
err = QOSAddSocketToFlow(us->qos, us->fd, NULL,
qos_type,
QOS_NON_ADAPTIVE_FLOW,
&us->qos_id);
if (!err)
return WSAGetLastError();
}

us->qos_id6 = 0;
if (-1 != us->fd6) {
err = QOSAddSocketToFlow(us->qos, us->fd6, NULL,
qos_type,
QOS_NON_ADAPTIVE_FLOW,
&us->qos_id6);
if (!err)
return WSAGetLastError();
}
#endif
err = udp_setsockopt(us, IPPROTO_IP, IP_TOS, &v, sizeof(v));
return err;
}
Expand Down