Debian Codegraph @

Xpjproject/pjlib/src/pj/sock_qos_common.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include <pj/sock_qos.h>
19 #include <pj/assert.h>
20 #include <pj/errno.h>
21 #include <pj/log.h>
22 #include <pj/string.h>
23
24 #define THIS_FILE "sock_qos_common.c"
25 #define ALL_FLAGS (PJ_QOS_PARAM_HAS_DSCP | PJ_QOS_PARAM_HAS_SO_PRIO | \
26 PJ_QOS_PARAM_HAS_WMM)
27
28 /* "Standard" mapping between traffic type and QoS params */
29 static const pj_qos_params qos_map[] =
30 {
31 /* flags dscp prio wmm_prio */
32 {ALL_FLAGS, 0x00, 0, PJ_QOS_WMM_PRIO_BULK_EFFORT}, /* BE */
33 {ALL_FLAGS, 0x08, 2, PJ_QOS_WMM_PRIO_BULK}, /* BK */
34 {ALL_FLAGS, 0x20, 5, PJ_QOS_WMM_PRIO_VIDEO}, /* VI */
35 {ALL_FLAGS, 0x2E, 6, PJ_QOS_WMM_PRIO_VOICE}, /* VO */
36 {ALL_FLAGS, 0x38, 7, PJ_QOS_WMM_PRIO_VOICE}, /* CO */
37 {ALL_FLAGS, 0x28, 5, PJ_QOS_WMM_PRIO_VIDEO} /* SIG */
38 };
39
40
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)
44 {
45 PJ_ASSERT_RETURN(type<=PJ_QOS_TYPE_SIGNALLING && p_param, PJ_EINVAL);
46 pj_memcpy(p_param, &qos_map[type], sizeof(*p_param));
47 return PJ_SUCCESS;
48 }
49
50 /* Get the matching traffic type */
51 PJ_DEF(pj_status_t) pj_qos_get_type( const pj_qos_params *param,
52 pj_qos_type *p_type)
53 {
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;
57 unsigned i, count=0;
58
59 PJ_ASSERT_RETURN(param && p_type, PJ_EINVAL);
60
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;
65 }
66 ++count;
67 }
68
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;
73 }
74 ++count;
75 }
76
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;
81 }
82 ++count;
83 }
84
85 if (count)
86 *p_type = (pj_qos_type)((dscp_type + prio_type + wmm_type) / count);
87 else
88 *p_type = PJ_QOS_TYPE_BEST_EFFORT;
89
90 return PJ_SUCCESS;
91 }
92
93 /* Apply QoS */
94 PJ_DEF(pj_status_t) pj_sock_apply_qos( pj_sock_t sock,
95 pj_qos_type qos_type,
96 pj_qos_params *qos_params,
97 unsigned log_level,
98 const char *log_sender,
99 const char *sock_name)
100 {
101 pj_status_t qos_type_rc = PJ_SUCCESS,
102 qos_params_rc = PJ_SUCCESS;
103
104 if (!log_sender)
105 log_sender = THIS_FILE;
106 if (!sock_name)
107 sock_name = "socket";
108
109 if (qos_type != PJ_QOS_TYPE_BEST_EFFORT) {
110 qos_type_rc = pj_sock_set_qos_type(sock, qos_type);
111
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);
116 }
117 }
118
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;
127 }
128 } else if (qos_type_rc != PJ_SUCCESS)
129 return qos_type_rc;
130
131 return PJ_SUCCESS;
132 }
133
134
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,
138 unsigned log_level,
139 const char *log_sender,
140 const char *sock_name)
141 {
142 pj_qos_params qos_params_buf, *qos_params_copy = NULL;
143
144 if (qos_params) {
145 pj_memcpy(&qos_params_buf, qos_params, sizeof(*qos_params));
146 qos_params_copy = &qos_params_buf;
147 }
148
149 return pj_sock_apply_qos(sock, qos_type, qos_params_copy,
150 log_level, log_sender, sock_name);
151 }