]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ip/Qos.cci
SourceFormat Enforcement
[thirdparty/squid.git] / src / ip / Qos.cci
CommitLineData
bbc27441 1/*
ef57eb7b 2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
bbc27441
AJ
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
425de4c8 9/* Inline QOS functions */
b5523edc 10#include "comm/Connection.h"
582c2af2 11#include "Debug.h"
425de4c8
AJ
12
13int
c6f168c1 14Ip::Qos::setSockTos(const int fd, tos_t tos, int type)
425de4c8 15{
792e0eb2
AJ
16 // Bug 3731: FreeBSD produces 'invalid option'
17 // unless we pass it a 32-bit variable storing 8-bits of data.
b4878a4f
AJ
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;
eb42df69 21
4b77ea6b
AR
22 debugs(50, 3, "for FD " << fd << " to " << bTos);
23
c6f168c1 24 if (type == AF_INET) {
eb42df69 25#if defined(IP_TOS)
c6f168c1 26 const int x = setsockopt(fd, IPPROTO_IP, IP_TOS, &bTos, sizeof(bTos));
eb42df69 27 if (x < 0)
c6f168c1 28 debugs(50, 2, "Ip::Qos::setSockTos: setsockopt(IP_TOS) on " << fd << ": " << xstrerror());
eb42df69 29 return x;
425de4c8 30#else
eb42df69
AJ
31 debugs(50, DBG_IMPORTANT, "WARNING: setsockopt(IP_TOS) not supported on this platform");
32 return -1;
425de4c8 33#endif
c6f168c1 34 } else { // type == AF_INET6
eb42df69 35#if defined(IPV6_TCLASS)
c6f168c1 36 const int x = setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &bTos, sizeof(bTos));
eb42df69 37 if (x < 0)
c6f168c1 38 debugs(50, 2, "Ip::Qos::setSockTos: setsockopt(IPV6_TCLASS) on " << fd << ": " << xstrerror());
eb42df69
AJ
39 return x;
40#else
41 debugs(50, DBG_IMPORTANT, "WARNING: setsockopt(IPV6_TCLASS) not supported on this platform");
42 return -1;
43#endif
44 }
45
46 /* CANNOT REACH HERE */
425de4c8
AJ
47}
48
49int
c6f168c1
CT
50Ip::Qos::setSockTos(const Comm::ConnectionPointer &conn, tos_t tos)
51{
52 const int x = Ip::Qos::setSockTos(conn->fd, tos, conn->remote.isIPv4() ? AF_INET : AF_INET6);
4b77ea6b 53 conn->tos = (x >= 0) ? tos : 0;
c6f168c1
CT
54 return x;
55}
56
57int
58Ip::Qos::setSockNfmark(const int fd, nfmark_t mark)
425de4c8 59{
11e8cfe3 60#if SO_MARK && USE_LIBCAP
4b77ea6b 61 debugs(50, 3, "for FD " << fd << " to " << mark);
c6f168c1 62 const int x = setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(nfmark_t));
425de4c8 63 if (x < 0)
c6f168c1 64 debugs(50, 2, "setSockNfmark: setsockopt(SO_MARK) on " << fd << ": " << xstrerror());
425de4c8 65 return x;
11e8cfe3 66#elif USE_LIBCAP
425de4c8
AJ
67 debugs(50, DBG_IMPORTANT, "WARNING: setsockopt(SO_MARK) not supported on this platform");
68 return -1;
11e8cfe3
AB
69#else
70 debugs(50, DBG_IMPORTANT, "WARNING: Netfilter marking disabled (netfilter marking requires build with LIBCAP)");
71 return -1;
425de4c8
AJ
72#endif
73}
74
c6f168c1
CT
75int
76Ip::Qos::setSockNfmark(const Comm::ConnectionPointer &conn, nfmark_t mark)
77{
78 const int x = Ip::Qos::setSockNfmark(conn->fd, mark);
4b77ea6b 79 conn->nfmark = (x >= 0) ? mark : 0;
c6f168c1
CT
80 return x;
81}
82
425de4c8
AJ
83bool
84Ip::Qos::Config::isHitTosActive() const
85{
86 return (tosLocalHit || tosSiblingHit || tosParentHit || tosMiss || preserveMissTos);
87}
88
89bool
90Ip::Qos::Config::isHitNfmarkActive() const
91{
92 return (markLocalHit || markSiblingHit || markParentHit || markMiss || preserveMissMark);
93}
94
95bool
96Ip::Qos::Config::isAclNfmarkActive() const
97{
98 acl_nfmark * nfmarkAcls [] = { nfmarkToServer, nfmarkToClient };
ab745b44 99
f412b2d6 100 for (int i=0; i<2; ++i) {
425de4c8
AJ
101 while (nfmarkAcls[i]) {
102 acl_nfmark *l = nfmarkAcls[i];
103 if (l->nfmark > 0)
ab745b44 104 return true;
425de4c8
AJ
105 nfmarkAcls[i] = l->next;
106 }
107 }
108
109 return false;
110}
111
112bool
113Ip::Qos::Config::isAclTosActive() const
114{
115 acl_tos * tosAcls [] = { tosToServer, tosToClient };
116
f412b2d6 117 for (int i=0; i<2; ++i) {
425de4c8
AJ
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}
f53969cc 128