]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ip/Qos.cci
SourceFormat Enforcement
[thirdparty/squid.git] / src / ip / Qos.cci
1 /*
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* Inline QOS functions */
10 #include "comm/Connection.h"
11 #include "Debug.h"
12
13 int
14 Ip::Qos::setSockTos(const int fd, tos_t tos, int type)
15 {
16 // Bug 3731: FreeBSD produces 'invalid option'
17 // unless we pass it a 32-bit variable storing 8-bits of data.
18 // NP: it is documented as 'int' for all systems, even those like Linux which accept 8-bit char
19 // so we convert to a int before setting.
20 int bTos = tos;
21
22 if (type == AF_INET) {
23 #if defined(IP_TOS)
24 const int x = setsockopt(fd, IPPROTO_IP, IP_TOS, &bTos, sizeof(bTos));
25 if (x < 0)
26 debugs(50, 2, "Ip::Qos::setSockTos: setsockopt(IP_TOS) on " << fd << ": " << xstrerror());
27 return x;
28 #else
29 debugs(50, DBG_IMPORTANT, "WARNING: setsockopt(IP_TOS) not supported on this platform");
30 return -1;
31 #endif
32 } else { // type == AF_INET6
33 #if defined(IPV6_TCLASS)
34 const int x = setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &bTos, sizeof(bTos));
35 if (x < 0)
36 debugs(50, 2, "Ip::Qos::setSockTos: setsockopt(IPV6_TCLASS) on " << fd << ": " << xstrerror());
37 return x;
38 #else
39 debugs(50, DBG_IMPORTANT, "WARNING: setsockopt(IPV6_TCLASS) not supported on this platform");
40 return -1;
41 #endif
42 }
43
44 /* CANNOT REACH HERE */
45 }
46
47 int
48 Ip::Qos::setSockTos(const Comm::ConnectionPointer &conn, tos_t tos)
49 {
50 const int x = Ip::Qos::setSockTos(conn->fd, tos, conn->remote.isIPv4() ? AF_INET : AF_INET6);
51 if (x >= 0)
52 conn->tos = tos;
53
54 return x;
55 }
56
57 int
58 Ip::Qos::setSockNfmark(const int fd, nfmark_t mark)
59 {
60 #if SO_MARK && USE_LIBCAP
61 const int x = setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(nfmark_t));
62 if (x < 0)
63 debugs(50, 2, "setSockNfmark: setsockopt(SO_MARK) on " << fd << ": " << xstrerror());
64 return x;
65 #elif USE_LIBCAP
66 debugs(50, DBG_IMPORTANT, "WARNING: setsockopt(SO_MARK) not supported on this platform");
67 return -1;
68 #else
69 debugs(50, DBG_IMPORTANT, "WARNING: Netfilter marking disabled (netfilter marking requires build with LIBCAP)");
70 return -1;
71 #endif
72 }
73
74 int
75 Ip::Qos::setSockNfmark(const Comm::ConnectionPointer &conn, nfmark_t mark)
76 {
77 const int x = Ip::Qos::setSockNfmark(conn->fd, mark);
78 if (x >= 0)
79 conn->nfmark = mark;
80 return x;
81 }
82
83 bool
84 Ip::Qos::Config::isHitTosActive() const
85 {
86 return (tosLocalHit || tosSiblingHit || tosParentHit || tosMiss || preserveMissTos);
87 }
88
89 bool
90 Ip::Qos::Config::isHitNfmarkActive() const
91 {
92 return (markLocalHit || markSiblingHit || markParentHit || markMiss || preserveMissMark);
93 }
94
95 bool
96 Ip::Qos::Config::isAclNfmarkActive() const
97 {
98 acl_nfmark * nfmarkAcls [] = { nfmarkToServer, nfmarkToClient };
99
100 for (int i=0; i<2; ++i) {
101 while (nfmarkAcls[i]) {
102 acl_nfmark *l = nfmarkAcls[i];
103 if (l->nfmark > 0)
104 return true;
105 nfmarkAcls[i] = l->next;
106 }
107 }
108
109 return false;
110 }
111
112 bool
113 Ip::Qos::Config::isAclTosActive() const
114 {
115 acl_tos * tosAcls [] = { tosToServer, tosToClient };
116
117 for (int i=0; i<2; ++i) {
118 while (tosAcls[i]) {
119 acl_tos *l = tosAcls[i];
120 if (l->tos > 0)
121 return true;
122 tosAcls[i] = l->next;
123 }
124 }
125
126 return false;
127 }
128