sock_qos_common.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #define THIS_FILE "sock_qos_common.c"
  24. #define ALL_FLAGS (PJ_QOS_PARAM_HAS_DSCP | PJ_QOS_PARAM_HAS_SO_PRIO | \
  25. PJ_QOS_PARAM_HAS_WMM)
  26. /* "Standard" mapping between traffic type and QoS params */
  27. static const pj_qos_params qos_map[] =
  28. {
  29. /* flags dscp prio wmm_prio */
  30. {ALL_FLAGS, 0x00, 0, PJ_QOS_WMM_PRIO_BULK_EFFORT}, /* BE */
  31. {ALL_FLAGS, 0x08, 2, PJ_QOS_WMM_PRIO_BULK}, /* BK */
  32. {ALL_FLAGS, 0x20, 5, PJ_QOS_WMM_PRIO_VIDEO}, /* VI */
  33. {ALL_FLAGS, 0x2E, 6, PJ_QOS_WMM_PRIO_VOICE}, /* VO */
  34. {ALL_FLAGS, 0x38, 7, PJ_QOS_WMM_PRIO_VOICE}, /* CO */
  35. {ALL_FLAGS, 0x28, 5, PJ_QOS_WMM_PRIO_VIDEO} /* SIG */
  36. };
  37. /* Retrieve the mapping for the specified type */
  38. PJ_DEF(pj_status_t) pj_qos_get_params(pj_qos_type type,
  39. pj_qos_params *p_param)
  40. {
  41. PJ_ASSERT_RETURN(type<=PJ_QOS_TYPE_SIGNALLING && p_param, PJ_EINVAL);
  42. pj_memcpy(p_param, &qos_map[type], sizeof(*p_param));
  43. return PJ_SUCCESS;
  44. }
  45. /* Get the matching traffic type */
  46. PJ_DEF(pj_status_t) pj_qos_get_type( const pj_qos_params *param,
  47. pj_qos_type *p_type)
  48. {
  49. unsigned dscp_type = PJ_QOS_TYPE_BEST_EFFORT,
  50. prio_type = PJ_QOS_TYPE_BEST_EFFORT,
  51. wmm_type = PJ_QOS_TYPE_BEST_EFFORT;
  52. unsigned i, count=0;
  53. PJ_ASSERT_RETURN(param && p_type, PJ_EINVAL);
  54. if (param->flags & PJ_QOS_PARAM_HAS_DSCP) {
  55. for (i=0; i<=PJ_QOS_TYPE_CONTROL; ++i) {
  56. if (param->dscp_val >= qos_map[i].dscp_val)
  57. dscp_type = (pj_qos_type)i;
  58. }
  59. ++count;
  60. }
  61. if (param->flags & PJ_QOS_PARAM_HAS_SO_PRIO) {
  62. for (i=0; i<=PJ_QOS_TYPE_CONTROL; ++i) {
  63. if (param->so_prio >= qos_map[i].so_prio)
  64. prio_type = (pj_qos_type)i;
  65. }
  66. ++count;
  67. }
  68. if (param->flags & PJ_QOS_PARAM_HAS_WMM) {
  69. for (i=0; i<=PJ_QOS_TYPE_CONTROL; ++i) {
  70. if (param->wmm_prio >= qos_map[i].wmm_prio)
  71. wmm_type = (pj_qos_type)i;
  72. }
  73. ++count;
  74. }
  75. if (count)
  76. *p_type = (pj_qos_type)((dscp_type + prio_type + wmm_type) / count);
  77. else
  78. *p_type = PJ_QOS_TYPE_BEST_EFFORT;
  79. return PJ_SUCCESS;
  80. }
  81. /* Apply QoS */
  82. PJ_DEF(pj_status_t) pj_sock_apply_qos( pj_sock_t sock,
  83. pj_qos_type qos_type,
  84. pj_qos_params *qos_params,
  85. unsigned log_level,
  86. const char *log_sender,
  87. const char *sock_name)
  88. {
  89. pj_status_t qos_type_rc = PJ_SUCCESS,
  90. qos_params_rc = PJ_SUCCESS;
  91. if (!log_sender)
  92. log_sender = THIS_FILE;
  93. if (!sock_name)
  94. sock_name = "socket";
  95. if (qos_type != PJ_QOS_TYPE_BEST_EFFORT) {
  96. qos_type_rc = pj_sock_set_qos_type(sock, qos_type);
  97. if (qos_type_rc != PJ_SUCCESS) {
  98. pj_perror(log_level, log_sender, qos_type_rc,
  99. "Error setting QoS type %d to %s",
  100. qos_type, sock_name);
  101. }
  102. }
  103. if (qos_params && qos_params->flags) {
  104. qos_params_rc = pj_sock_set_qos_params(sock, qos_params);
  105. if (qos_params_rc != PJ_SUCCESS) {
  106. pj_perror(log_level, log_sender, qos_params_rc,
  107. "Error setting QoS params (flags=%d) to %s",
  108. qos_params->flags, sock_name);
  109. if (qos_type_rc != PJ_SUCCESS)
  110. return qos_params_rc;
  111. }
  112. } else if (qos_type_rc != PJ_SUCCESS)
  113. return qos_type_rc;
  114. return PJ_SUCCESS;
  115. }
  116. PJ_DEF(pj_status_t) pj_sock_apply_qos2( pj_sock_t sock,
  117. pj_qos_type qos_type,
  118. const pj_qos_params *qos_params,
  119. unsigned log_level,
  120. const char *log_sender,
  121. const char *sock_name)
  122. {
  123. pj_qos_params qos_params_buf, *qos_params_copy = NULL;
  124. if (qos_params) {
  125. pj_memcpy(&qos_params_buf, qos_params, sizeof(*qos_params));
  126. qos_params_copy = &qos_params_buf;
  127. }
  128. return pj_sock_apply_qos(sock, qos_type, qos_params_copy,
  129. log_level, log_sender, sock_name);
  130. }