2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 80 WCCP Support */
15 #include "comm/Connection.h"
16 #include "comm/Loops.h"
19 #include "SquidConfig.h"
22 #define WCCP_PORT 2048
23 #define WCCP_REVISION 0
24 #define WCCP_ACTIVE_CACHES 32
25 #define WCCP_HASH_SIZE 32
26 #define WCCP_BUCKETS 256
27 #define WCCP_CACHE_LEN 4
29 #define WCCP_HERE_I_AM 7
30 #define WCCP_I_SEE_YOU 8
31 #define WCCP_ASSIGN_BUCKET 9
33 struct wccp_here_i_am_t
{
37 char hash
[WCCP_HASH_SIZE
];
42 struct wccp_cache_entry_t
{
43 struct in_addr ip_addr
; // WCCP on-the-wire in 32-bit IPv4-only.
45 char hash
[WCCP_HASH_SIZE
];
49 struct wccp_i_see_you_t
{
56 struct wccp_cache_entry_t wccp_cache_entry
[WCCP_ACTIVE_CACHES
];
59 struct wccp_assign_bucket_t
{
65 static int theWccpConnection
= -1;
67 static struct wccp_here_i_am_t wccp_here_i_am
;
69 static struct wccp_i_see_you_t wccp_i_see_you
;
70 static int last_change
;
72 static int last_assign_buckets_change
;
73 static unsigned int number_caches
;
75 static Ip::Address local_ip
;
77 static PF wccpHandleUdp
;
78 static int wccpLowestIP(void);
79 static EVH wccpHereIam
;
80 static void wccpAssignBuckets(void);
83 * The functions used during startup:
86 * wccpConnectionShutdown
93 debugs(80, 5, "wccpInit: Called");
94 memset(&wccp_here_i_am
, '\0', sizeof(wccp_here_i_am
));
95 wccp_here_i_am
.type
= htonl(WCCP_HERE_I_AM
);
96 wccp_here_i_am
.version
= htonl(Config
.Wccp
.version
);
97 wccp_here_i_am
.revision
= htonl(WCCP_REVISION
);
100 last_assign_buckets_change
= 0;
103 if (!Config
.Wccp
.router
.isAnyAddr())
104 if (!eventFind(wccpHereIam
, nullptr))
105 eventAdd("wccpHereIam", wccpHereIam
, nullptr, 5.0, 1);
109 wccpConnectionOpen(void)
111 debugs(80, 5, "wccpConnectionOpen: Called");
113 if (Config
.Wccp
.router
.isAnyAddr()) {
114 debugs(80, 2, "WCCPv1 disabled.");
118 if ( !Config
.Wccp
.router
.setIPv4() ) {
119 debugs(80, DBG_CRITICAL
, "WCCPv1 Disabled. Router " << Config
.Wccp
.router
<< " is not an IPv4 address.");
123 if ( !Config
.Wccp
.address
.setIPv4() ) {
124 debugs(80, DBG_CRITICAL
, "WCCPv1 Disabled. Local address " << Config
.Wccp
.address
<< " is not an IPv4 address.");
128 Config
.Wccp
.address
.port(WCCP_PORT
);
129 Config
.Wccp
.router
.port(WCCP_PORT
);
131 theWccpConnection
= comm_open_listener(SOCK_DGRAM
,
137 if (theWccpConnection
< 0)
138 fatal("Cannot open WCCP Port");
140 Comm::SetSelect(theWccpConnection
, COMM_SELECT_READ
, wccpHandleUdp
, nullptr, 0);
142 debugs(80, DBG_IMPORTANT
, "Accepting WCCPv1 messages on " << Config
.Wccp
.address
<< ", FD " << theWccpConnection
<< ".");
144 // Sadly WCCP only does IPv4
146 struct sockaddr_in router
;
147 Config
.Wccp
.router
.getSockAddr(router
);
148 if (connect(theWccpConnection
, (struct sockaddr
*)&router
, sizeof(router
)))
149 fatal("Unable to connect WCCP out socket");
151 struct sockaddr_in local
;
152 memset(&local
, '\0', sizeof(local
));
153 socklen_t slen
= sizeof(local
);
154 if (getsockname(theWccpConnection
, (struct sockaddr
*)&local
, &slen
))
155 fatal("Unable to getsockname on WCCP out socket");
161 wccpConnectionClose(void)
163 if (theWccpConnection
> -1) {
164 debugs(80, DBG_IMPORTANT
, "FD " << theWccpConnection
<< " Closing WCCPv1 socket");
165 comm_close(theWccpConnection
);
166 theWccpConnection
= -1;
171 * Functions for handling the requests.
175 * Accept the UDP packet
178 wccpHandleUdp(int sock
, void *)
183 debugs(80, 6, "wccpHandleUdp: Called.");
185 Comm::SetSelect(sock
, COMM_SELECT_READ
, wccpHandleUdp
, nullptr, 0);
187 memset(&wccp_i_see_you
, '\0', sizeof(wccp_i_see_you
));
189 len
= comm_udp_recvfrom(sock
,
190 (void *) &wccp_i_see_you
,
191 sizeof(wccp_i_see_you
),
194 debugs(80, 3, "wccpHandleUdp: " << len
<< " bytes WCCP pkt from " << from
<<
196 (unsigned) ntohl(wccp_i_see_you
.type
) << ", version=" <<
197 (unsigned) ntohl(wccp_i_see_you
.version
) << ", change=" <<
198 (unsigned) ntohl(wccp_i_see_you
.change
) << ", id=" <<
199 (unsigned) ntohl(wccp_i_see_you
.id
) << ", number=" <<
200 (unsigned) ntohl(wccp_i_see_you
.number
));
205 if (from
!= Config
.Wccp
.router
)
208 if ((unsigned) ntohl(wccp_i_see_you
.version
) != (unsigned) Config
.Wccp
.version
)
211 if (ntohl(wccp_i_see_you
.type
) != WCCP_I_SEE_YOU
)
214 if (ntohl(wccp_i_see_you
.number
) > WCCP_ACTIVE_CACHES
) {
215 debugs(80, DBG_IMPORTANT
, "Ignoring WCCP_I_SEE_YOU from " <<
216 from
<< " with number of caches set to " <<
217 (int) ntohl(wccp_i_see_you
.number
));
222 last_id
= wccp_i_see_you
.id
;
224 if ((0 == last_change
) && (number_caches
== (unsigned) ntohl(wccp_i_see_you
.number
))) {
225 if (last_assign_buckets_change
== wccp_i_see_you
.change
) {
227 * After a WCCP_ASSIGN_BUCKET message, the router should
228 * update the change value. If not, maybe the route didn't
229 * receive our WCCP_ASSIGN_BUCKET message, so send it again.
231 * Don't update change here. Instead, fall through to
232 * the next block to call wccpAssignBuckets() again.
236 last_change
= wccp_i_see_you
.change
;
241 if (last_change
!= wccp_i_see_you
.change
) {
242 last_change
= wccp_i_see_you
.change
;
244 if (wccpLowestIP() && wccp_i_see_you
.number
) {
245 last_assign_buckets_change
= last_change
;
258 * We sanity checked wccp_i_see_you.number back in wccpHandleUdp()
261 for (loop
= 0; loop
< (unsigned) ntohl(wccp_i_see_you
.number
); ++loop
) {
262 assert(loop
< WCCP_ACTIVE_CACHES
);
264 if (local_ip
> wccp_i_see_you
.wccp_cache_entry
[loop
].ip_addr
)
267 if (local_ip
== wccp_i_see_you
.wccp_cache_entry
[loop
].ip_addr
)
277 debugs(80, 6, "wccpHereIam: Called");
279 wccp_here_i_am
.id
= last_id
;
280 double interval
= 10.0; // TODO: make this configurable, possibly negotiate with the router.
281 ssize_t sent
= comm_udp_send(theWccpConnection
, &wccp_here_i_am
, sizeof(wccp_here_i_am
), 0);
283 // if we failed to send the whole lot, try again at a shorter interval (20%)
284 if (sent
!= sizeof(wccp_here_i_am
)) {
286 debugs(80, 2, "ERROR: failed to send WCCP HERE_I_AM packet: " << xstrerr(xerrno
));
290 if (!eventFind(wccpHereIam
, nullptr))
291 eventAdd("wccpHereIam", wccpHereIam
, nullptr, interval
, 1);
295 wccpAssignBuckets(void)
298 struct wccp_assign_bucket_t
*wccp_assign_bucket
;
301 int buckets_per_cache
;
308 debugs(80, 6, "wccpAssignBuckets: Called");
309 number_caches
= ntohl(wccp_i_see_you
.number
);
311 assert(number_caches
> 0);
312 assert(number_caches
<= WCCP_ACTIVE_CACHES
);
314 wab_len
= sizeof(struct wccp_assign_bucket_t
);
316 cache_len
= WCCP_CACHE_LEN
* number_caches
;
318 buf
= (char *)xmalloc(wab_len
+
322 wccp_assign_bucket
= (struct wccp_assign_bucket_t
*) buf
;
324 caches
= (int *) (buf
+ wab_len
);
326 buckets
= buf
+ wab_len
+ cache_len
;
328 memset(wccp_assign_bucket
, '\0', sizeof(*wccp_assign_bucket
));
330 memset(buckets
, 0xFF, WCCP_BUCKETS
);
332 buckets_per_cache
= WCCP_BUCKETS
/ number_caches
;
334 for (loop
= 0; loop
< number_caches
; ++loop
) {
336 memcpy(&caches
[loop
],
337 &wccp_i_see_you
.wccp_cache_entry
[loop
].ip_addr
,
340 for (i
= 0; i
< buckets_per_cache
; ++i
) {
341 assert(bucket
< WCCP_BUCKETS
);
342 buckets
[bucket
] = loop
;
347 while (bucket
< WCCP_BUCKETS
) {
348 buckets
[bucket
] = number_caches
- 1;
352 wccp_assign_bucket
->type
= htonl(WCCP_ASSIGN_BUCKET
);
353 wccp_assign_bucket
->id
= wccp_i_see_you
.id
;
354 wccp_assign_bucket
->number
= wccp_i_see_you
.number
;
356 comm_udp_send(theWccpConnection
,
358 wab_len
+ WCCP_BUCKETS
+ cache_len
,
364 #endif /* USE_WCCP */