1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef NET_SOCKET_DIFF_SERV_CODE_POINT_H_
6 #define NET_SOCKET_DIFF_SERV_CODE_POINT_H_
7
8 namespace net {
9
10 // Differentiated Services Code Point.
11 // See http://tools.ietf.org/html/rfc2474 for details.
12 enum DiffServCodePoint {
13 DSCP_NO_CHANGE = -1,
14 DSCP_FIRST = DSCP_NO_CHANGE,
15 DSCP_DEFAULT = 0, // Same as DSCP_CS0
16 DSCP_CS0 = 0, // The default
17 DSCP_CS1 = 8, // Bulk/background traffic
18 DSCP_AF11 = 10,
19 DSCP_AF12 = 12,
20 DSCP_AF13 = 14,
21 DSCP_CS2 = 16,
22 DSCP_AF21 = 18,
23 DSCP_AF22 = 20,
24 DSCP_AF23 = 22,
25 DSCP_CS3 = 24,
26 DSCP_AF31 = 26,
27 DSCP_AF32 = 28,
28 DSCP_AF33 = 30,
29 DSCP_CS4 = 32,
30 DSCP_AF41 = 34, // Video
31 DSCP_AF42 = 36, // Video
32 DSCP_AF43 = 38, // Video
33 DSCP_CS5 = 40, // Video
34 DSCP_EF = 46, // Voice
35 DSCP_CS6 = 48, // Voice
36 DSCP_CS7 = 56, // Control messages
37 DSCP_LAST = DSCP_CS7
38 };
39
40 // Explicit Congestion Notification
41 // See RFC3168 and RFC9330 for details.
42 enum EcnCodePoint {
43 ECN_NO_CHANGE = -1,
44 ECN_FIRST = ECN_NO_CHANGE,
45 ECN_DEFAULT = 0,
46 ECN_NOT_ECT = 0,
47 ECN_ECT1 = 1,
48 ECN_ECT0 = 2,
49 ECN_CE = 3,
50 ECN_LAST = ECN_CE,
51 };
52
53 struct DscpAndEcn {
54 DiffServCodePoint dscp;
55 EcnCodePoint ecn;
56 };
57
58 // Converts an 8-bit IP TOS field to its DSCP and ECN parts.
TosToDscpAndEcn(uint8_t tos)59 static inline DscpAndEcn TosToDscpAndEcn(uint8_t tos) {
60 // Bitmasks to find the DSCP and ECN pieces of the TOS byte.
61 constexpr uint8_t kEcnMask = 0b11;
62 constexpr uint8_t kDscpMask = ~kEcnMask;
63 return DscpAndEcn{static_cast<DiffServCodePoint>((tos & kDscpMask) >> 2),
64 static_cast<EcnCodePoint>(tos & kEcnMask)};
65 }
66
67 } // namespace net
68
69 #endif // NET_SOCKET_DIFF_SERV_CODE_POINT_H_
70