sock_qos.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  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.
  9. *
  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.
  14. *
  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
  18. */
  19. #ifndef __PJ_SOCK_QOS_H__
  20. #define __PJ_SOCK_QOS_H__
  21. /**
  22. * @file sock_qos.h
  23. * @brief Socket QoS API
  24. */
  25. #include <pj/sock.h>
  26. PJ_BEGIN_DECL
  27. /**
  28. * @defgroup socket_qos Socket Quality of Service (QoS) API: TOS, DSCP, WMM, IEEE 802.1p
  29. * @ingroup PJ_SOCK
  30. * @{
  31. \section intro QoS Technologies
  32. QoS settings are available for both Layer 2 and 3 of TCP/IP protocols:
  33. \subsection intro_ieee8021p Layer 2: IEEE 802.1p for Ethernet
  34. IEEE 802.1p tagging will mark frames sent by a host for prioritized
  35. delivery using a 3-bit Priority field in the virtual local area network
  36. (VLAN) header of the Ethernet frame. The VLAN header is placed inside
  37. the Ethernet header, between the Source Address field and either the
  38. Length field (for an IEEE 802.3 frame) or the EtherType field (for an
  39. Ethernet II frame).
  40. \subsection intro_wmm Layer 2: WMM
  41. At the Network Interface layer for IEEE 802.11 wireless, the Wi-Fi
  42. Alliance certification for Wi-Fi Multimedia (WMM) defines four access
  43. categories for prioritizing network traffic. These access categories
  44. are (in order of highest to lowest priority) voice, video, best-effort,
  45. and background. Host support for WMM prioritization requires that both
  46. wireless network adapters and their drivers support WMM. Wireless
  47. access points (APs) must have WMM enabled.
  48. \subsection intro_dscp Layer 3: DSCP
  49. At the Internet layer, you can use Differentiated Services/Diffserv and
  50. set the value of the Differentiated Services Code Point (DSCP) in the
  51. IP header. As defined in RFC 2474, the DSCP value is the high-order 6 bits
  52. of the IP version 4 (IPv4) TOS field and the IP version 6 (IPv6) Traffic
  53. Class field.
  54. \subsection intro_other Layer 3: Other
  55. Other mechanisms exist (such as RSVP, IntServ) but this will not be
  56. implemented.
  57. \section availability QoS Availability
  58. \subsection linux Linux
  59. DSCP is available via IP TOS option.
  60. Ethernet 802.1p tagging is done by setting setsockopt(SO_PRIORITY) option
  61. of the socket, then with the set_egress_map option of the vconfig utility
  62. to convert this to set vlan-qos field of the packet.
  63. WMM is not known to be available.
  64. \subsection windows Windows and Windows Mobile
  65. (It's a mess!)
  66. DSCP is settable with setsockopt() on Windows 2000 or older, but Windows
  67. would silently ignore this call on WinXP or later, unless administrator
  68. modifies the registry. On Windows 2000, Windows XP, and Windows Server
  69. 2003, GQoS (Generic QoS) API is the standard API, but this API may not be
  70. supported in the future. On Vista and Windows 7, the is a new QoS2 API,
  71. also known as Quality Windows Audio-Video Experience (qWAVE).
  72. IEEE 802.1p tagging is available via Traffic Control (TC) API, available
  73. on Windows XP SP2, but this needs administrator access. For Vista and
  74. later, it's in qWAVE.
  75. WMM is available for mobile platforms on Windows Mobile 6 platform and
  76. Windows Embedded CE 6, via setsockopt(IP_DSCP_TRAFFIC_TYPE). qWAVE
  77. supports this as well.
  78. \subsection symbian Symbian S60 3rd Ed
  79. Both DSCP and WMM is supported via RSocket::SetOpt() with will set both
  80. Layer 2 and Layer 3 QoS settings accordingly. Internally, PJLIB sets the
  81. DSCP field of the socket, and based on certain DSCP values mapping,
  82. Symbian will set the WMM tag accordingly.
  83. \section api PJLIB's QoS API Abstraction
  84. Based on the above, the following API is implemented.
  85. Declare the following "standard" traffic types.
  86. \code
  87. typedef enum pj_qos_type
  88. {
  89. PJ_QOS_TYPE_BEST_EFFORT,
  90. PJ_QOS_TYPE_BACKGROUND,
  91. PJ_QOS_TYPE_VIDEO,
  92. PJ_QOS_TYPE_VOICE,
  93. PJ_QOS_TYPE_CONTROL,
  94. PJ_QOS_TYPE_SIGNALLING
  95. } pj_qos_type;
  96. \endcode
  97. The traffic classes above will determine how the Layer 2 and 3 QoS
  98. settings will be used. The standard mapping between the classes above
  99. to the corresponding Layer 2 and 3 settings are as follows:
  100. \code
  101. =================================================================
  102. PJLIB Traffic Type IP DSCP WMM 802.1p
  103. -----------------------------------------------------------------
  104. BEST_EFFORT 0x00 BE (Bulk Effort) 0
  105. BACKGROUND 0x08 BK (Bulk) 2
  106. VIDEO 0x28 VI (Video) 5
  107. VOICE 0x30 VO (Voice) 6
  108. CONTROL 0x38 VO (Voice) 7
  109. SIGNALLING 0x28 VI (Video) 5
  110. =================================================================
  111. \endcode
  112. There are two sets of API provided to manipulate the QoS parameters.
  113. \subsection portable_api Portable API
  114. The first set of API is:
  115. \code
  116. // Set QoS parameters
  117. PJ_DECL(pj_status_t) pj_sock_set_qos_type(pj_sock_t sock,
  118. pj_qos_type val);
  119. // Get QoS parameters
  120. PJ_DECL(pj_status_t) pj_sock_get_qos_type(pj_sock_t sock,
  121. pj_qos_type *p_val);
  122. \endcode
  123. The API will set the traffic type according to the DSCP class, for both
  124. Layer 2 and Layer 3 QoS settings, where it's available. If any of the
  125. layer QoS setting is not settable, the API will silently ignore it.
  126. If both layers are not setable, the API will return error.
  127. The API above is the recommended use of QoS, since it is the most
  128. portable across all platforms.
  129. \subsection detail_api Fine Grained Control API
  130. The second set of API is intended for application that wants to fine
  131. tune the QoS parameters.
  132. The Layer 2 and 3 QoS parameters are stored in pj_qos_params structure:
  133. \code
  134. typedef enum pj_qos_flag
  135. {
  136. PJ_QOS_PARAM_HAS_DSCP = 1,
  137. PJ_QOS_PARAM_HAS_SO_PRIO = 2,
  138. PJ_QOS_PARAM_HAS_WMM = 4
  139. } pj_qos_flag;
  140. typedef enum pj_qos_wmm_prio
  141. {
  142. PJ_QOS_WMM_PRIO_BULK_EFFORT,
  143. PJ_QOS_WMM_PRIO_BULK,
  144. PJ_QOS_WMM_PRIO_VIDEO,
  145. PJ_QOS_WMM_PRIO_VOICE
  146. } pj_qos_wmm_prio;
  147. typedef struct pj_qos_params
  148. {
  149. pj_uint8_t flags; // Determines which values to
  150. // set, bitmask of pj_qos_flag
  151. pj_uint8_t dscp_val; // The 6 bits DSCP value to set
  152. pj_uint8_t so_prio; // SO_PRIORITY value
  153. pj_qos_wmm_prio wmm_prio; // WMM priority value
  154. } pj_qos_params;
  155. \endcode
  156. The second set of API with more fine-grained control over the parameters
  157. are:
  158. \code
  159. // Retrieve QoS params for the specified traffic type
  160. PJ_DECL(pj_status_t) pj_qos_get_params(pj_qos_type type,
  161. pj_qos_params *p);
  162. // Set QoS parameters to the socket
  163. PJ_DECL(pj_status_t) pj_sock_set_qos_params(pj_sock_t sock,
  164. const pj_qos_params *p);
  165. // Get QoS parameters from the socket
  166. PJ_DECL(pj_status_t) pj_sock_get_qos_params(pj_sock_t sock,
  167. pj_qos_params *p);
  168. \endcode
  169. Important:
  170. The pj_sock_set/get_qos_params() APIs are not portable, and it's probably
  171. only going to be implemented on Linux. Application should always try to
  172. use pj_sock_set_qos_type() instead.
  173. */
  174. /**
  175. * High level traffic classification.
  176. */
  177. typedef enum pj_qos_type
  178. {
  179. PJ_QOS_TYPE_BEST_EFFORT, /**< Best effort traffic (default value).
  180. Any QoS function calls with specifying
  181. this value are effectively no-op */
  182. PJ_QOS_TYPE_BACKGROUND, /**< Background traffic. */
  183. PJ_QOS_TYPE_VIDEO, /**< Video traffic. */
  184. PJ_QOS_TYPE_VOICE, /**< Voice traffic. */
  185. PJ_QOS_TYPE_CONTROL, /**< Control traffic. */
  186. PJ_QOS_TYPE_SIGNALLING /**< Signalling traffic. */
  187. } pj_qos_type;
  188. /**
  189. * Bitmask flag to indicate which QoS layer setting is set in the
  190. * \a flags field of the #pj_qos_params structure.
  191. */
  192. typedef enum pj_qos_flag
  193. {
  194. PJ_QOS_PARAM_HAS_DSCP = 1, /**< DSCP field is set. */
  195. PJ_QOS_PARAM_HAS_SO_PRIO = 2, /**< Socket SO_PRIORITY */
  196. PJ_QOS_PARAM_HAS_WMM = 4 /**< WMM field is set. */
  197. } pj_qos_flag;
  198. /**
  199. * Standard WMM priorities.
  200. */
  201. typedef enum pj_qos_wmm_prio
  202. {
  203. PJ_QOS_WMM_PRIO_BULK_EFFORT, /**< Bulk effort priority */
  204. PJ_QOS_WMM_PRIO_BULK, /**< Bulk priority. */
  205. PJ_QOS_WMM_PRIO_VIDEO, /**< Video priority */
  206. PJ_QOS_WMM_PRIO_VOICE /**< Voice priority */
  207. } pj_qos_wmm_prio;
  208. /**
  209. * QoS parameters to be set or retrieved to/from the socket.
  210. */
  211. typedef struct pj_qos_params
  212. {
  213. pj_uint8_t flags; /**< Determines which values to
  214. set, bitmask of pj_qos_flag */
  215. pj_uint8_t dscp_val; /**< The 6 bits DSCP value to set */
  216. pj_uint8_t so_prio; /**< SO_PRIORITY value */
  217. pj_qos_wmm_prio wmm_prio; /**< WMM priority value */
  218. } pj_qos_params;
  219. /**
  220. * This is the high level and portable API to enable QoS on the specified
  221. * socket, by setting the traffic type to the specified parameter.
  222. *
  223. * @param sock The socket.
  224. * @param type Traffic type to be set.
  225. *
  226. * @return PJ_SUCCESS if at least Layer 2 or Layer 3 setting is
  227. * successfully set. If both Layer 2 and Layer 3 settings
  228. * can't be set, this function will return error.
  229. */
  230. PJ_DECL(pj_status_t) pj_sock_set_qos_type(pj_sock_t sock,
  231. pj_qos_type type);
  232. /**
  233. * This is the high level and portable API to get the traffic type that has
  234. * been set on the socket. On occasions where the Layer 2 or Layer 3 settings
  235. * were modified by using low level API, this function may return approximation
  236. * of the closest QoS type that matches the settings.
  237. *
  238. * @param sock The socket.
  239. * @param p_type Pointer to receive the traffic type of the socket.
  240. *
  241. * @return PJ_SUCCESS if traffic type for the socket can be obtained
  242. * or approximated..
  243. */
  244. PJ_DECL(pj_status_t) pj_sock_get_qos_type(pj_sock_t sock,
  245. pj_qos_type *p_type);
  246. /**
  247. * This is a convenience function to apply QoS to the socket, and print error
  248. * logging if the operations failed. Both QoS traffic type and the low level
  249. * QoS parameters can be applied with this function.
  250. *
  251. * @param sock The socket handle.
  252. * @param qos_type QoS traffic type. The QoS traffic type will be applied
  253. * only if the value is not PJ_QOS_TYPE_BEST_EFFORT,
  254. * @param qos_params Optional low-level QoS parameters. This will be
  255. * applied only if this argument is not NULL and the
  256. * flags inside the structure is non-zero. Upon return,
  257. * the flags will indicate which parameters have been
  258. * applied successfully.
  259. * @param log_level This function will print to log at this level upon
  260. * encountering errors.
  261. * @param log_sender Optional sender name in the log.
  262. * @param sock_name Optional name to help identify the socket in the log.
  263. *
  264. * @return PJ_SUCCESS if at least Layer 2 or Layer 3 setting is
  265. * successfully set. If both Layer 2 and Layer 3 settings
  266. * can't be set, this function will return error.
  267. *
  268. * @see pj_sock_apply_qos2()
  269. */
  270. PJ_DECL(pj_status_t) pj_sock_apply_qos(pj_sock_t sock,
  271. pj_qos_type qos_type,
  272. pj_qos_params *qos_params,
  273. unsigned log_level,
  274. const char *log_sender,
  275. const char *sock_name);
  276. /**
  277. * Variant of #pj_sock_apply_qos() where the \a qos_params parameter is
  278. * const.
  279. *
  280. * @see pj_sock_apply_qos()
  281. */
  282. PJ_DECL(pj_status_t) pj_sock_apply_qos2(pj_sock_t sock,
  283. pj_qos_type qos_type,
  284. const pj_qos_params *qos_params,
  285. unsigned log_level,
  286. const char *log_sender,
  287. const char *sock_name);
  288. /**
  289. * Retrieve the standard mapping of QoS params for the specified traffic
  290. * type.
  291. *
  292. * @param type The traffic type from which the QoS parameters
  293. * are to be retrieved.
  294. * @param p_param Pointer to receive the QoS parameters.
  295. *
  296. * @return PJ_SUCCESS on success or the appropriate error code.
  297. */
  298. PJ_DECL(pj_status_t) pj_qos_get_params(pj_qos_type type,
  299. pj_qos_params *p_param);
  300. /**
  301. * Retrieve the traffic type that matches the specified QoS parameters.
  302. * If no exact matching is found, this function will return an
  303. * approximation of the closest matching traffic type for the specified
  304. * QoS parameters.
  305. *
  306. * @param param Structure containing QoS parameters to map into
  307. * "standard" traffic types.
  308. * @param p_type Pointer to receive the traffic type.
  309. *
  310. * @return PJ_SUCCESS on success or the appropriate error code.
  311. */
  312. PJ_DECL(pj_status_t) pj_qos_get_type(const pj_qos_params *param,
  313. pj_qos_type *p_type);
  314. /**
  315. * This is a low level API to set QoS parameters to the socket.
  316. *
  317. * @param sock The socket.
  318. * @param param Structure containing QoS parameters to be applied
  319. * to the socket. Upon return, the \a flags field
  320. * of this structure will be set with bitmask value
  321. * indicating which QoS settings have successfully
  322. * been applied to the socket.
  323. *
  324. * @return PJ_SUCCESS if at least one field setting has been
  325. * successfully set. If no setting can't be set,
  326. * this function will return error.
  327. */
  328. PJ_DECL(pj_status_t) pj_sock_set_qos_params(pj_sock_t sock,
  329. pj_qos_params *param);
  330. /**
  331. * This is a low level API to get QoS parameters from the socket.
  332. *
  333. * @param sock The socket.
  334. * @param p_param Pointer to receive the parameters. Upon returning
  335. * successfully, the \a flags field of this structure
  336. * will be initialized with the appropriate bitmask
  337. * to indicate which fields have been successfully
  338. * retrieved.
  339. *
  340. * @return PJ_SUCCESS on success or the appropriate error code.
  341. */
  342. PJ_DECL(pj_status_t) pj_sock_get_qos_params(pj_sock_t sock,
  343. pj_qos_params *p_param);
  344. /**
  345. * @}
  346. */
  347. PJ_END_DECL
  348. #endif /* __PJ_SOCK_QOS_H__ */