2 * Copyright (C) 1996-2014 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"
18 #include "SquidConfig.h"
20 #define WCCP_PORT 2048
21 #define WCCP_REVISION 0
22 #define WCCP_ACTIVE_CACHES 32
23 #define WCCP_HASH_SIZE 32
24 #define WCCP_BUCKETS 256
25 #define WCCP_CACHE_LEN 4
27 #define WCCP_HERE_I_AM 7
28 #define WCCP_I_SEE_YOU 8
29 #define WCCP_ASSIGN_BUCKET 9
31 struct wccp_here_i_am_t
{
35 char hash
[WCCP_HASH_SIZE
];
40 struct wccp_cache_entry_t
{
41 struct in_addr ip_addr
; // WCCP on-the-wire in 32-bit IPv4-only.
43 char hash
[WCCP_HASH_SIZE
];
47 struct wccp_i_see_you_t
{
54 struct wccp_cache_entry_t wccp_cache_entry
[WCCP_ACTIVE_CACHES
];
57 struct wccp_assign_bucket_t
{
63 static int theWccpConnection
= -1;
65 static struct wccp_here_i_am_t wccp_here_i_am
;
67 static struct wccp_i_see_you_t wccp_i_see_you
;
68 static int last_change
;
70 static int last_assign_buckets_change
;
71 static unsigned int number_caches
;
73 static Ip::Address local_ip
;
75 static PF wccpHandleUdp
;
76 static int wccpLowestIP(void);
77 static EVH wccpHereIam
;
78 static void wccpAssignBuckets(void);
81 * The functions used during startup:
84 * wccpConnectionShutdown
91 debugs(80, 5, "wccpInit: Called");
92 memset(&wccp_here_i_am
, '\0', sizeof(wccp_here_i_am
));
93 wccp_here_i_am
.type
= htonl(WCCP_HERE_I_AM
);
94 wccp_here_i_am
.version
= htonl(Config
.Wccp
.version
);
95 wccp_here_i_am
.revision
= htonl(WCCP_REVISION
);
98 last_assign_buckets_change
= 0;
101 if (!Config
.Wccp
.router
.isAnyAddr())
102 if (!eventFind(wccpHereIam
, NULL
))
103 eventAdd("wccpHereIam", wccpHereIam
, NULL
, 5.0, 1);
107 wccpConnectionOpen(void)
109 debugs(80, 5, "wccpConnectionOpen: Called");
111 if (Config
.Wccp
.router
.isAnyAddr()) {
112 debugs(80, 2, "WCCPv1 disabled.");
116 if ( !Config
.Wccp
.router
.setIPv4() ) {
117 debugs(80, DBG_CRITICAL
, "WCCPv1 Disabled. Router " << Config
.Wccp
.router
<< " is not an IPv4 address.");
121 if ( !Config
.Wccp
.address
.setIPv4() ) {
122 debugs(80, DBG_CRITICAL
, "WCCPv1 Disabled. Local address " << Config
.Wccp
.address
<< " is not an IPv4 address.");
126 Config
.Wccp
.address
.port(WCCP_PORT
);
127 Config
.Wccp
.router
.port(WCCP_PORT
);
129 theWccpConnection
= comm_open_listener(SOCK_DGRAM
,
135 if (theWccpConnection
< 0)
136 fatal("Cannot open WCCP Port");
138 Comm::SetSelect(theWccpConnection
, COMM_SELECT_READ
, wccpHandleUdp
, NULL
, 0);
140 debugs(80, DBG_IMPORTANT
, "Accepting WCCPv1 messages on " << Config
.Wccp
.address
<< ", FD " << theWccpConnection
<< ".");
142 // Sadly WCCP only does IPv4
144 struct sockaddr_in router
;
145 Config
.Wccp
.router
.getSockAddr(router
);
146 if (connect(theWccpConnection
, (struct sockaddr
*)&router
, sizeof(router
)))
147 fatal("Unable to connect WCCP out socket");
149 struct sockaddr_in local
;
150 memset(&local
, '\0', sizeof(local
));
151 socklen_t slen
= sizeof(local
);
152 if (getsockname(theWccpConnection
, (struct sockaddr
*)&local
, &slen
))
153 fatal("Unable to getsockname on WCCP out socket");
159 wccpConnectionClose(void)
161 if (theWccpConnection
> -1) {
162 debugs(80, DBG_IMPORTANT
, "FD " << theWccpConnection
<< " Closing WCCPv1 socket");
163 comm_close(theWccpConnection
);
164 theWccpConnection
= -1;
169 * Functions for handling the requests.
173 * Accept the UDP packet
176 wccpHandleUdp(int sock
, void *not_used
)
181 debugs(80, 6, "wccpHandleUdp: Called.");
183 Comm::SetSelect(sock
, COMM_SELECT_READ
, wccpHandleUdp
, NULL
, 0);
185 memset(&wccp_i_see_you
, '\0', sizeof(wccp_i_see_you
));
187 len
= comm_udp_recvfrom(sock
,
188 (void *) &wccp_i_see_you
,
189 sizeof(wccp_i_see_you
),
192 debugs(80, 3, "wccpHandleUdp: " << len
<< " bytes WCCP pkt from " << from
<<
194 (unsigned) ntohl(wccp_i_see_you
.type
) << ", version=" <<
195 (unsigned) ntohl(wccp_i_see_you
.version
) << ", change=" <<
196 (unsigned) ntohl(wccp_i_see_you
.change
) << ", id=" <<
197 (unsigned) ntohl(wccp_i_see_you
.id
) << ", number=" <<
198 (unsigned) ntohl(wccp_i_see_you
.number
));
203 if (from
!= Config
.Wccp
.router
)
206 if ((unsigned) ntohl(wccp_i_see_you
.version
) != (unsigned) Config
.Wccp
.version
)
209 if (ntohl(wccp_i_see_you
.type
) != WCCP_I_SEE_YOU
)
212 if (ntohl(wccp_i_see_you
.number
) > WCCP_ACTIVE_CACHES
) {
213 debugs(80, DBG_IMPORTANT
, "Ignoring WCCP_I_SEE_YOU from " <<
214 from
<< " with number of caches set to " <<
215 (int) ntohl(wccp_i_see_you
.number
));
220 last_id
= wccp_i_see_you
.id
;
222 if ((0 == last_change
) && (number_caches
== (unsigned) ntohl(wccp_i_see_you
.number
))) {
223 if (last_assign_buckets_change
== wccp_i_see_you
.change
) {
225 * After a WCCP_ASSIGN_BUCKET message, the router should
226 * update the change value. If not, maybe the route didn't
227 * receive our WCCP_ASSIGN_BUCKET message, so send it again.
229 * Don't update change here. Instead, fall through to
230 * the next block to call wccpAssignBuckets() again.
234 last_change
= wccp_i_see_you
.change
;
239 if (last_change
!= wccp_i_see_you
.change
) {
240 last_change
= wccp_i_see_you
.change
;
242 if (wccpLowestIP() && wccp_i_see_you
.number
) {
243 last_assign_buckets_change
= last_change
;
256 * We sanity checked wccp_i_see_you.number back in wccpHandleUdp()
259 for (loop
= 0; loop
< (unsigned) ntohl(wccp_i_see_you
.number
); ++loop
) {
260 assert(loop
< WCCP_ACTIVE_CACHES
);
262 if (local_ip
> wccp_i_see_you
.wccp_cache_entry
[loop
].ip_addr
)
265 if (local_ip
== wccp_i_see_you
.wccp_cache_entry
[loop
].ip_addr
)
273 wccpHereIam(void *voidnotused
)
275 debugs(80, 6, "wccpHereIam: Called");
277 wccp_here_i_am
.id
= last_id
;
278 double interval
= 10.0; // TODO: make this configurable, possibly negotiate with the router.
280 ssize_t sent
= comm_udp_send(theWccpConnection
, &wccp_here_i_am
, sizeof(wccp_here_i_am
), 0);
282 // if we failed to send the whole lot, try again at a shorter interval (20%)
283 if (sent
!= sizeof(wccp_here_i_am
)) {
284 debugs(80, 2, "ERROR: failed to send WCCP HERE_I_AM packet: " << xstrerror());
288 if (!eventFind(wccpHereIam
, NULL
))
289 eventAdd("wccpHereIam", wccpHereIam
, NULL
, interval
, 1);
293 wccpAssignBuckets(void)
296 struct wccp_assign_bucket_t
*wccp_assign_bucket
;
299 int buckets_per_cache
;
306 debugs(80, 6, "wccpAssignBuckets: Called");
307 number_caches
= ntohl(wccp_i_see_you
.number
);
309 assert(number_caches
> 0);
310 assert(number_caches
<= WCCP_ACTIVE_CACHES
);
312 wab_len
= sizeof(struct wccp_assign_bucket_t
);
314 cache_len
= WCCP_CACHE_LEN
* number_caches
;
316 buf
= (char *)xmalloc(wab_len
+
320 wccp_assign_bucket
= (struct wccp_assign_bucket_t
*) buf
;
322 caches
= (int *) (buf
+ wab_len
);
324 buckets
= buf
+ wab_len
+ cache_len
;
326 memset(wccp_assign_bucket
, '\0', sizeof(*wccp_assign_bucket
));
328 memset(buckets
, 0xFF, WCCP_BUCKETS
);
330 buckets_per_cache
= WCCP_BUCKETS
/ number_caches
;
332 for (loop
= 0; loop
< number_caches
; ++loop
) {
334 memcpy(&caches
[loop
],
335 &wccp_i_see_you
.wccp_cache_entry
[loop
].ip_addr
,
338 for (i
= 0; i
< buckets_per_cache
; ++i
) {
339 assert(bucket
< WCCP_BUCKETS
);
340 buckets
[bucket
] = loop
;
345 while (bucket
< WCCP_BUCKETS
) {
346 buckets
[bucket
] = number_caches
- 1;
350 wccp_assign_bucket
->type
= htonl(WCCP_ASSIGN_BUCKET
);
351 wccp_assign_bucket
->id
= wccp_i_see_you
.id
;
352 wccp_assign_bucket
->number
= wccp_i_see_you
.number
;
354 comm_udp_send(theWccpConnection
,
356 wab_len
+ WCCP_BUCKETS
+ cache_len
,
362 #endif /* USE_WCCP */