1
/* $Id: sock_qos_common.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
#include <pj/sock_qos.h>
20
#include <pj/assert.h>
23
#include <pj/string.h>
25
#define THIS_FILE "sock_qos_common.c"
26
#define ALL_FLAGS (PJ_QOS_PARAM_HAS_DSCP | PJ_QOS_PARAM_HAS_SO_PRIO | \
29
/* "Standard" mapping between traffic type and QoS params */
30
static const pj_qos_params qos_map[] =
32
/* flags dscp prio wmm_prio */
33
{ALL_FLAGS, 0x00, 0, PJ_QOS_WMM_PRIO_BULK_EFFORT}, /* BE */
34
{ALL_FLAGS, 0x08, 2, PJ_QOS_WMM_PRIO_BULK}, /* BK */
35
{ALL_FLAGS, 0x28, 5, PJ_QOS_WMM_PRIO_VIDEO}, /* VI */
36
{ALL_FLAGS, 0x30, 6, PJ_QOS_WMM_PRIO_VOICE}, /* VO */
37
{ALL_FLAGS, 0x38, 7, PJ_QOS_WMM_PRIO_VOICE} /* CO */
41
/* Retrieve the mapping for the specified type */
42
PJ_DEF(pj_status_t) pj_qos_get_params(pj_qos_type type,
43
pj_qos_params *p_param)
45
PJ_ASSERT_RETURN(type<=PJ_QOS_TYPE_CONTROL && p_param, PJ_EINVAL);
46
pj_memcpy(p_param, &qos_map[type], sizeof(*p_param));
50
/* Get the matching traffic type */
51
PJ_DEF(pj_status_t) pj_qos_get_type( const pj_qos_params *param,
54
unsigned dscp_type = PJ_QOS_TYPE_BEST_EFFORT,
55
prio_type = PJ_QOS_TYPE_BEST_EFFORT,
56
wmm_type = PJ_QOS_TYPE_BEST_EFFORT;
59
PJ_ASSERT_RETURN(param && p_type, PJ_EINVAL);
61
if (param->flags & PJ_QOS_PARAM_HAS_DSCP) {
62
for (i=0; i<=PJ_QOS_TYPE_CONTROL; ++i) {
63
if (param->dscp_val >= qos_map[i].dscp_val)
64
dscp_type = (pj_qos_type)i;
69
if (param->flags & PJ_QOS_PARAM_HAS_SO_PRIO) {
70
for (i=0; i<=PJ_QOS_TYPE_CONTROL; ++i) {
71
if (param->so_prio >= qos_map[i].so_prio)
72
prio_type = (pj_qos_type)i;
77
if (param->flags & PJ_QOS_PARAM_HAS_WMM) {
78
for (i=0; i<=PJ_QOS_TYPE_CONTROL; ++i) {
79
if (param->wmm_prio >= qos_map[i].wmm_prio)
80
wmm_type = (pj_qos_type)i;
86
*p_type = (pj_qos_type)((dscp_type + prio_type + wmm_type) / count);
88
*p_type = PJ_QOS_TYPE_BEST_EFFORT;
94
PJ_DEF(pj_status_t) pj_sock_apply_qos( pj_sock_t sock,
96
pj_qos_params *qos_params,
98
const char *log_sender,
99
const char *sock_name)
101
pj_status_t qos_type_rc = PJ_SUCCESS,
102
qos_params_rc = PJ_SUCCESS;
105
log_sender = THIS_FILE;
107
sock_name = "socket";
109
if (qos_type != PJ_QOS_TYPE_BEST_EFFORT) {
110
qos_type_rc = pj_sock_set_qos_type(sock, qos_type);
112
if (qos_type_rc != PJ_SUCCESS) {
113
pj_perror(log_level, log_sender, qos_type_rc,
114
"Error setting QoS type %d to %s",
115
qos_type, sock_name);
119
if (qos_params && qos_params->flags) {
120
qos_params_rc = pj_sock_set_qos_params(sock, qos_params);
121
if (qos_params_rc != PJ_SUCCESS) {
122
pj_perror(log_level, log_sender, qos_params_rc,
123
"Error setting QoS params (flags=%d) to %s",
124
qos_params->flags, sock_name);
125
if (qos_type_rc != PJ_SUCCESS)
126
return qos_params_rc;
128
} else if (qos_type_rc != PJ_SUCCESS)
135
PJ_DEF(pj_status_t) pj_sock_apply_qos2( pj_sock_t sock,
136
pj_qos_type qos_type,
137
const pj_qos_params *qos_params,
139
const char *log_sender,
140
const char *sock_name)
142
pj_qos_params qos_params_buf, *qos_params_copy = NULL;
145
pj_memcpy(&qos_params_buf, qos_params, sizeof(*qos_params));
146
qos_params_copy = &qos_params_buf;
149
return pj_sock_apply_qos(sock, qos_type, qos_params_copy,
150
log_level, log_sender, sock_name);