]> granicus.if.org Git - apache/blob - modules/proxy/mod_proxy_balancer.c
No functional change; simplify the CVE-2007-6420 fix slightly, as
[apache] / modules / proxy / mod_proxy_balancer.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /* Load balancer module for Apache proxy */
18
19 #include "mod_proxy.h"
20 #include "scoreboard.h"
21 #include "ap_mpm.h"
22 #include "apr_version.h"
23 #include "apr_hooks.h"
24 #include "apr_uuid.h"
25
26 module AP_MODULE_DECLARE_DATA proxy_balancer_module;
27
28 static char balancer_nonce[APR_UUID_FORMATTED_LENGTH + 1];
29
30 static int proxy_balancer_canon(request_rec *r, char *url)
31 {
32     char *host, *path;
33     char *search = NULL;
34     const char *err;
35     apr_port_t port = 0;
36
37     if (strncasecmp(url, "balancer:", 9) == 0) {
38         url += 9;
39     }
40     else {
41         return DECLINED;
42     }
43
44     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
45              "proxy: BALANCER: canonicalising URL %s", url);
46
47     /* do syntatic check.
48      * We break the URL into host, port, path, search
49      */
50     err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
51     if (err) {
52         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
53                       "error parsing URL %s: %s",
54                       url, err);
55         return HTTP_BAD_REQUEST;
56     }
57     /*
58      * now parse path/search args, according to rfc1738:
59      * process the path. With proxy-noncanon set (by
60      * mod_proxy) we use the raw, unparsed uri
61      */
62     if (apr_table_get(r->notes, "proxy-nocanon")) {
63         path = url;   /* this is the raw path */
64     }
65     else {
66         path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
67                                  r->proxyreq);
68         search = r->args;
69     }
70     if (path == NULL)
71         return HTTP_BAD_REQUEST;
72
73     r->filename = apr_pstrcat(r->pool, "proxy:balancer://", host,
74             "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
75
76     r->path_info = apr_pstrcat(r->pool, "/", path, NULL);
77
78     return OK;
79 }
80
81 static int init_balancer_members(proxy_server_conf *conf, server_rec *s,
82                                  proxy_balancer *balancer)
83 {
84     int i;
85     proxy_worker *workers;
86
87     workers = (proxy_worker *)balancer->workers->elts;
88
89     for (i = 0; i < balancer->workers->nelts; i++) {
90         int worker_is_initialized;
91         worker_is_initialized = PROXY_WORKER_IS_INITIALIZED(workers);
92         if (!worker_is_initialized) {
93             proxy_worker_stat *slot;
94             /*
95              * If the worker is not initialized check whether its scoreboard
96              * slot is already initialized.
97              */
98             slot = (proxy_worker_stat *) ap_get_scoreboard_lb(workers->id);
99             if (slot) {
100                 worker_is_initialized = slot->status & PROXY_WORKER_INITIALIZED;
101             }
102             else {
103                 worker_is_initialized = 0;
104             }
105         }
106         ap_proxy_initialize_worker_share(conf, workers, s);
107         ap_proxy_initialize_worker(workers, s);
108         if (!worker_is_initialized) {
109             /* Set to the original configuration */
110             workers->s->lbstatus = workers->s->lbfactor =
111             (workers->lbfactor ? workers->lbfactor : 1);
112             workers->s->lbset = workers->lbset;
113         }
114         ++workers;
115     }
116
117     /* Set default number of attempts to the number of
118      * workers.
119      */
120     if (!balancer->max_attempts_set && balancer->workers->nelts > 1) {
121         balancer->max_attempts = balancer->workers->nelts - 1;
122         balancer->max_attempts_set = 1;
123     }
124     return 0;
125 }
126
127 /* Retrieve the parameter with the given name
128  * Something like 'JSESSIONID=12345...N'
129  */
130 static char *get_path_param(apr_pool_t *pool, char *url,
131                             const char *name)
132 {
133     char *path = NULL;
134
135     for (path = strstr(url, name); path; path = strstr(path + 1, name)) {
136         path += strlen(name);
137         if (*path == '=') {
138             /*
139              * Session path was found, get it's value
140              */
141             ++path;
142             if (strlen(path)) {
143                 char *q;
144                 path = apr_strtok(apr_pstrdup(pool, path), "?&", &q);
145                 return path;
146             }
147         }
148     }
149     return NULL;
150 }
151
152 static char *get_cookie_param(request_rec *r, const char *name)
153 {
154     const char *cookies;
155     const char *start_cookie;
156
157     if ((cookies = apr_table_get(r->headers_in, "Cookie"))) {
158         for (start_cookie = ap_strstr_c(cookies, name); start_cookie;
159              start_cookie = ap_strstr_c(start_cookie + 1, name)) {
160             if (start_cookie == cookies ||
161                 start_cookie[-1] == ';' ||
162                 start_cookie[-1] == ',' ||
163                 isspace(start_cookie[-1])) {
164
165                 start_cookie += strlen(name);
166                 while(*start_cookie && isspace(*start_cookie))
167                     ++start_cookie;
168                 if (*start_cookie == '=' && start_cookie[1]) {
169                     /*
170                      * Session cookie was found, get it's value
171                      */
172                     char *end_cookie, *cookie;
173                     ++start_cookie;
174                     cookie = apr_pstrdup(r->pool, start_cookie);
175                     if ((end_cookie = strchr(cookie, ';')) != NULL)
176                         *end_cookie = '\0';
177                     if((end_cookie = strchr(cookie, ',')) != NULL)
178                         *end_cookie = '\0';
179                     return cookie;
180                 }
181             }
182         }
183     }
184     return NULL;
185 }
186
187 /* Find the worker that has the 'route' defined
188  */
189 static proxy_worker *find_route_worker(proxy_balancer *balancer,
190                                        const char *route, request_rec *r)
191 {
192     int i;
193     int checking_standby;
194     int checked_standby;
195     
196     proxy_worker *worker;
197
198     checking_standby = checked_standby = 0;
199     while (!checked_standby) {
200         worker = (proxy_worker *)balancer->workers->elts;
201         for (i = 0; i < balancer->workers->nelts; i++, worker++) {
202             if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
203                 continue;
204             if (*(worker->s->route) && strcmp(worker->s->route, route) == 0) {
205                 if (worker && PROXY_WORKER_IS_USABLE(worker)) {
206                     return worker;
207                 } else {
208                     /*
209                      * If the worker is in error state run
210                      * retry on that worker. It will be marked as
211                      * operational if the retry timeout is elapsed.
212                      * The worker might still be unusable, but we try
213                      * anyway.
214                      */
215                     ap_proxy_retry_worker("BALANCER", worker, r->server);
216                     if (PROXY_WORKER_IS_USABLE(worker)) {
217                             return worker;
218                     } else {
219                         /*
220                          * We have a worker that is unusable.
221                          * It can be in error or disabled, but in case
222                          * it has a redirection set use that redirection worker.
223                          * This enables to safely remove the member from the
224                          * balancer. Of course you will need some kind of
225                          * session replication between those two remote.
226                          */
227                         if (*worker->s->redirect) {
228                             proxy_worker *rworker = NULL;
229                             rworker = find_route_worker(balancer, worker->s->redirect, r);
230                             /* Check if the redirect worker is usable */
231                             if (rworker && !PROXY_WORKER_IS_USABLE(rworker)) {
232                                 /*
233                                  * If the worker is in error state run
234                                  * retry on that worker. It will be marked as
235                                  * operational if the retry timeout is elapsed.
236                                  * The worker might still be unusable, but we try
237                                  * anyway.
238                                  */
239                                 ap_proxy_retry_worker("BALANCER", rworker, r->server);
240                             }
241                             if (rworker && PROXY_WORKER_IS_USABLE(rworker))
242                                 return rworker;
243                         }
244                     }
245                 }
246             }
247         }
248         checked_standby = checking_standby++;
249     }
250     return NULL;
251 }
252
253 static proxy_worker *find_session_route(proxy_balancer *balancer,
254                                         request_rec *r,
255                                         char **route,
256                                         const char **sticky_used,
257                                         char **url)
258 {
259     proxy_worker *worker = NULL;
260
261     if (!balancer->sticky)
262         return NULL;
263     /* Try to find the sticky route inside url */
264     *route = get_path_param(r->pool, *url, balancer->sticky_path);
265     if (*route) {
266         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
267                      "proxy: BALANCER: Found value %s for "
268                      "stickysession %s", *route, balancer->sticky_path);
269         *sticky_used =  balancer->sticky_path;
270     }
271     else {
272         *route = get_cookie_param(r, balancer->sticky);
273         if (*route) {
274             *sticky_used =  balancer->sticky;
275             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
276                          "proxy: BALANCER: Found value %s for "
277                          "stickysession %s", *route, balancer->sticky);
278         }
279     }
280     /*
281      * If we found a value for sticksession, find the first '.' within.
282      * Everything after '.' (if present) is our route.
283      */
284     if ((*route) && ((*route = strchr(*route, '.')) != NULL ))
285         (*route)++;
286     if ((*route) && (**route)) {
287         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
288                                   "proxy: BALANCER: Found route %s", *route);
289         /* We have a route in path or in cookie
290          * Find the worker that has this route defined.
291          */
292         worker = find_route_worker(balancer, *route, r);
293         if (worker && strcmp(*route, worker->s->route)) {
294             /*
295              * Notice that the route of the worker chosen is different from
296              * the route supplied by the client.
297              */
298             apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
299             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
300                          "proxy: BALANCER: Route changed from %s to %s",
301                          *route, worker->s->route);
302         }
303         return worker;
304     }
305     else
306         return NULL;
307 }
308
309 static proxy_worker *find_best_worker(proxy_balancer *balancer,
310                                       request_rec *r)
311 {
312     proxy_worker *candidate = NULL;
313     apr_status_t rv;
314
315     if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
316         ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
317         "proxy: BALANCER: (%s). Lock failed for find_best_worker()", balancer->name);
318         return NULL;
319     }
320
321     candidate = (*balancer->lbmethod->finder)(balancer, r);
322
323     if (candidate)
324         candidate->s->elected++;
325
326 /*
327         PROXY_THREAD_UNLOCK(balancer);
328         return NULL;
329 */
330
331     if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
332         ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
333         "proxy: BALANCER: (%s). Unlock failed for find_best_worker()", balancer->name);
334     }
335
336     if (candidate == NULL) {
337         /* All the workers are in error state or disabled.
338          * If the balancer has a timeout sleep for a while
339          * and try again to find the worker. The chances are
340          * that some other thread will release a connection.
341          * By default the timeout is not set, and the server
342          * returns SERVER_BUSY.
343          */
344 #if APR_HAS_THREADS
345         if (balancer->timeout) {
346             /* XXX: This can perhaps be build using some
347              * smarter mechanism, like tread_cond.
348              * But since the statuses can came from
349              * different childs, use the provided algo.
350              */
351             apr_interval_time_t timeout = balancer->timeout;
352             apr_interval_time_t step, tval = 0;
353             /* Set the timeout to 0 so that we don't
354              * end in infinite loop
355              */
356             balancer->timeout = 0;
357             step = timeout / 100;
358             while (tval < timeout) {
359                 apr_sleep(step);
360                 /* Try again */
361                 if ((candidate = find_best_worker(balancer, r)))
362                     break;
363                 tval += step;
364             }
365             /* restore the timeout */
366             balancer->timeout = timeout;
367         }
368 #endif
369     }
370     return candidate;
371 }
372
373 static int rewrite_url(request_rec *r, proxy_worker *worker,
374                         char **url)
375 {
376     const char *scheme = strstr(*url, "://");
377     const char *path = NULL;
378
379     if (scheme)
380         path = ap_strchr_c(scheme + 3, '/');
381
382     /* we break the URL into host, port, uri */
383     if (!worker) {
384         return ap_proxyerror(r, HTTP_BAD_REQUEST, apr_pstrcat(r->pool,
385                              "missing worker. URI cannot be parsed: ", *url,
386                              NULL));
387     }
388
389     *url = apr_pstrcat(r->pool, worker->name, path, NULL);
390
391     return OK;
392 }
393
394 static void force_recovery(proxy_balancer *balancer, server_rec *s)
395 {
396     int i;
397     int ok = 0;
398     proxy_worker *worker;
399
400     worker = (proxy_worker *)balancer->workers->elts;
401     for (i = 0; i < balancer->workers->nelts; i++, worker++) {
402         if (!(worker->s->status & PROXY_WORKER_IN_ERROR)) {
403             ok = 1;
404             break;    
405         }
406     }
407     if (!ok) {
408         /* If all workers are in error state force the recovery.
409          */
410         worker = (proxy_worker *)balancer->workers->elts;
411         for (i = 0; i < balancer->workers->nelts; i++, worker++) {
412             ++worker->s->retries;
413             worker->s->status &= ~PROXY_WORKER_IN_ERROR;
414             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
415                          "proxy: BALANCER: (%s). Forcing recovery for worker (%s)",
416                          balancer->name, worker->hostname);
417         }
418     }
419 }
420
421 static int proxy_balancer_pre_request(proxy_worker **worker,
422                                       proxy_balancer **balancer,
423                                       request_rec *r,
424                                       proxy_server_conf *conf, char **url)
425 {
426     int access_status;
427     proxy_worker *runtime;
428     char *route = NULL;
429     const char *sticky = NULL;
430     apr_status_t rv;
431
432     *worker = NULL;
433     /* Step 1: check if the url is for us
434      * The url we can handle starts with 'balancer://'
435      * If balancer is already provided skip the search
436      * for balancer, because this is failover attempt.
437      */
438     if (!*balancer &&
439         !(*balancer = ap_proxy_get_balancer(r->pool, conf, *url)))
440         return DECLINED;
441
442     /* Step 2: Lock the LoadBalancer
443      * XXX: perhaps we need the process lock here
444      */
445     if ((rv = PROXY_THREAD_LOCK(*balancer)) != APR_SUCCESS) {
446         ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
447                      "proxy: BALANCER: (%s). Lock failed for pre_request",
448                      (*balancer)->name);
449         return DECLINED;
450     }
451
452     /* Step 3: force recovery */
453     force_recovery(*balancer, r->server);
454
455     /* Step 4: find the session route */
456     runtime = find_session_route(*balancer, r, &route, &sticky, url);
457     if (runtime) {
458         int i, total_factor = 0;
459         proxy_worker *workers;
460         /* We have a sticky load balancer
461          * Update the workers status
462          * so that even session routes get
463          * into account.
464          */
465         workers = (proxy_worker *)(*balancer)->workers->elts;
466         for (i = 0; i < (*balancer)->workers->nelts; i++) {
467             /* Take into calculation only the workers that are
468              * not in error state or not disabled.
469              *
470              * TODO: Abstract the below, since this is dependent
471              *       on the LB implementation
472              */
473             if (PROXY_WORKER_IS_USABLE(workers)) {
474                 workers->s->lbstatus += workers->s->lbfactor;
475                 total_factor += workers->s->lbfactor;
476             }
477             workers++;
478         }
479         runtime->s->lbstatus -= total_factor;
480         runtime->s->elected++;
481
482         *worker = runtime;
483     }
484     else if (route && (*balancer)->sticky_force) {
485         int i, member_of = 0;
486         proxy_worker *workers;
487         /*
488          * We have a route provided that doesn't match the
489          * balancer name. See if the provider route is the
490          * member of the same balancer in which case return 503
491          */
492         workers = (proxy_worker *)(*balancer)->workers->elts;
493         for (i = 0; i < (*balancer)->workers->nelts; i++) {
494             if (*(workers->s->route) && strcmp(workers->s->route, route) == 0) {
495                 member_of = 1;
496                 break;
497             }
498             workers++;
499         }
500         if (member_of) {
501             ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
502                          "proxy: BALANCER: (%s). All workers are in error state for route (%s)",
503                          (*balancer)->name, route);
504             if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
505                 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
506                              "proxy: BALANCER: (%s). Unlock failed for pre_request",
507                              (*balancer)->name);
508             }
509             return HTTP_SERVICE_UNAVAILABLE;
510         }
511     }
512
513     if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
514         ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
515                      "proxy: BALANCER: (%s). Unlock failed for pre_request",
516                      (*balancer)->name);
517     }
518     if (!*worker) {
519         runtime = find_best_worker(*balancer, r);
520         if (!runtime) {
521             ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
522                          "proxy: BALANCER: (%s). All workers are in error state",
523                          (*balancer)->name);
524
525             return HTTP_SERVICE_UNAVAILABLE;
526         }
527         if ((*balancer)->sticky && runtime) {
528             /*
529              * This balancer has sticky sessions and the client either has not
530              * supplied any routing information or all workers for this route
531              * including possible redirect and hotstandby workers are in error
532              * state, but we have found another working worker for this
533              * balancer where we can send the request. Thus notice that we have
534              * changed the route to the backend.
535              */
536             apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
537         }
538         *worker = runtime;
539     }
540
541     /* Add balancer/worker info to env. */
542     apr_table_setn(r->subprocess_env,
543                    "BALANCER_NAME", (*balancer)->name);
544     apr_table_setn(r->subprocess_env,
545                    "BALANCER_WORKER_NAME", (*worker)->name);
546     apr_table_setn(r->subprocess_env,
547                    "BALANCER_WORKER_ROUTE", (*worker)->s->route);
548
549     /* Rewrite the url from 'balancer://url'
550      * to the 'worker_scheme://worker_hostname[:worker_port]/url'
551      * This replaces the balancers fictional name with the
552      * real hostname of the elected worker.
553      */
554     access_status = rewrite_url(r, *worker, url);
555     /* Add the session route to request notes if present */
556     if (route) {
557         apr_table_setn(r->notes, "session-sticky", sticky);
558         apr_table_setn(r->notes, "session-route", route);
559
560         /* Add session info to env. */
561         apr_table_setn(r->subprocess_env,
562                        "BALANCER_SESSION_STICKY", sticky);
563         apr_table_setn(r->subprocess_env,
564                        "BALANCER_SESSION_ROUTE", route);
565     }
566     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
567                  "proxy: BALANCER (%s) worker (%s) rewritten to %s",
568                  (*balancer)->name, (*worker)->name, *url);
569
570     return access_status;
571 }
572
573 static int proxy_balancer_post_request(proxy_worker *worker,
574                                        proxy_balancer *balancer,
575                                        request_rec *r,
576                                        proxy_server_conf *conf)
577 {
578
579 #if 0
580     apr_status_t rv;
581
582     if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
583         ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
584             "proxy: BALANCER: (%s). Lock failed for post_request",
585             balancer->name);
586         return HTTP_INTERNAL_SERVER_ERROR;
587     }
588     /* TODO: placeholder for post_request actions
589      */
590
591     if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
592         ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
593             "proxy: BALANCER: (%s). Unlock failed for post_request",
594             balancer->name);
595     }
596     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
597                  "proxy_balancer_post_request for (%s)", balancer->name);
598
599 #endif
600
601     return OK;
602 }
603
604 static void recalc_factors(proxy_balancer *balancer)
605 {
606     int i;
607     proxy_worker *workers;
608
609
610     /* Recalculate lbfactors */
611     workers = (proxy_worker *)balancer->workers->elts;
612     /* Special case if there is only one worker it's
613      * load factor will always be 1
614      */
615     if (balancer->workers->nelts == 1) {
616         workers->s->lbstatus = workers->s->lbfactor = 1;
617         return;
618     }
619     for (i = 0; i < balancer->workers->nelts; i++) {
620         /* Update the status entries */
621         workers[i].s->lbstatus = workers[i].s->lbfactor;
622     }
623 }
624
625 /* post_config hook: */
626 static int balancer_init(apr_pool_t *p, apr_pool_t *plog,
627                          apr_pool_t *ptemp, server_rec *s)
628 {
629     void *data;
630     const char *userdata_key = "mod_proxy_balancer_init";
631     apr_uuid_t uuid;
632
633     /* balancer_init() will be called twice during startup.  So, only
634      * set up the static data the second time through. */
635     apr_pool_userdata_get(&data, userdata_key, s->process->pool);
636     if (!data) {
637         apr_pool_userdata_set((const void *)1, userdata_key,
638                                apr_pool_cleanup_null, s->process->pool);
639         return OK;
640     }
641
642     /* Retrieve a UUID and store the nonce for the lifetime of
643      * the process. */
644     apr_uuid_get(&uuid);
645     apr_uuid_format(balancer_nonce, &uuid);
646
647     return OK;
648 }
649
650 /* Manages the loadfactors and member status
651  */
652 static int balancer_handler(request_rec *r)
653 {
654     void *sconf = r->server->module_config;
655     proxy_server_conf *conf = (proxy_server_conf *)
656         ap_get_module_config(sconf, &proxy_module);
657     proxy_balancer *balancer, *bsel = NULL;
658     proxy_worker *worker, *wsel = NULL;
659     apr_table_t *params = apr_table_make(r->pool, 10);
660     int access_status;
661     int i, n;
662     const char *name;
663
664     /* is this for us? */
665     if (strcmp(r->handler, "balancer-manager"))
666         return DECLINED;
667     r->allowed = (AP_METHOD_BIT << M_GET);
668     if (r->method_number != M_GET)
669         return DECLINED;
670
671     if (r->args) {
672         char *args = apr_pstrdup(r->pool, r->args);
673         char *tok, *val;
674         while (args && *args) {
675             if ((val = ap_strchr(args, '='))) {
676                 *val++ = '\0';
677                 if ((tok = ap_strchr(val, '&')))
678                     *tok++ = '\0';
679                 /*
680                  * Special case: workers are allowed path information
681                  */
682                 if ((access_status = ap_unescape_url(val)) != OK)
683                     if (strcmp(args, "w") || (access_status !=  HTTP_NOT_FOUND))
684                         return access_status;
685                 apr_table_setn(params, args, val);
686                 args = tok;
687             }
688             else
689                 return HTTP_BAD_REQUEST;
690         }
691     }
692     
693     /* Check that the supplied nonce matches this server's nonce;
694      * otherwise ignore all parameters, to prevent a CSRF attack. */
695     if ((name = apr_table_get(params, "nonce")) == NULL 
696         || strcmp(balancer_nonce, name) != 0) {
697         apr_table_clear(params);
698     }
699
700     if ((name = apr_table_get(params, "b")))
701         bsel = ap_proxy_get_balancer(r->pool, conf,
702             apr_pstrcat(r->pool, "balancer://", name, NULL));
703     if ((name = apr_table_get(params, "w"))) {
704         proxy_worker *ws;
705
706         ws = ap_proxy_get_worker(r->pool, conf, name);
707         if (bsel && ws) {
708             worker = (proxy_worker *)bsel->workers->elts;
709             for (n = 0; n < bsel->workers->nelts; n++) {
710                 if (strcasecmp(worker->name, ws->name) == 0) {
711                     wsel = worker;
712                     break;
713                 }
714                 ++worker;
715             }
716         }
717     }
718     /* First set the params */
719     /*
720      * Note that it is not possible set the proxy_balancer because it is not
721      * in shared memory.
722      */
723     if (wsel) {
724         const char *val;
725         if ((val = apr_table_get(params, "lf"))) {
726             int ival = atoi(val);
727             if (ival >= 1 && ival <= 100) {
728                 wsel->s->lbfactor = ival;
729                 if (bsel)
730                     recalc_factors(bsel);
731             }
732         }
733         if ((val = apr_table_get(params, "wr"))) {
734             if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
735                 strcpy(wsel->s->route, val);
736             else
737                 *wsel->s->route = '\0';
738         }
739         if ((val = apr_table_get(params, "rr"))) {
740             if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
741                 strcpy(wsel->s->redirect, val);
742             else
743                 *wsel->s->redirect = '\0';
744         }
745         if ((val = apr_table_get(params, "dw"))) {
746             if (!strcasecmp(val, "Disable"))
747                 wsel->s->status |= PROXY_WORKER_DISABLED;
748             else if (!strcasecmp(val, "Enable"))
749                 wsel->s->status &= ~PROXY_WORKER_DISABLED;
750         }
751         if ((val = apr_table_get(params, "ls"))) {
752             int ival = atoi(val);
753             if (ival >= 0 && ival <= 99) {
754                 wsel->s->lbset = ival;
755              }
756         }
757
758     }
759     if (apr_table_get(params, "xml")) {
760         ap_set_content_type(r, "text/xml");
761         ap_rputs("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n", r);
762         ap_rputs("<httpd:manager xmlns:httpd=\"http://httpd.apache.org\">\n", r);
763         ap_rputs("  <httpd:balancers>\n", r);
764         balancer = (proxy_balancer *)conf->balancers->elts;
765         for (i = 0; i < conf->balancers->nelts; i++) {
766             ap_rputs("    <httpd:balancer>\n", r);
767             ap_rvputs(r, "      <httpd:name>", balancer->name, "</httpd:name>\n", NULL);
768             ap_rputs("      <httpd:workers>\n", r);
769             worker = (proxy_worker *)balancer->workers->elts;
770             for (n = 0; n < balancer->workers->nelts; n++) {
771                 ap_rputs("        <httpd:worker>\n", r);
772                 ap_rvputs(r, "          <httpd:scheme>", worker->scheme,
773                           "</httpd:scheme>\n", NULL);
774                 ap_rvputs(r, "          <httpd:hostname>", worker->hostname,
775                           "</httpd:hostname>\n", NULL);
776                ap_rprintf(r, "          <httpd:loadfactor>%d</httpd:loadfactor>\n",
777                           worker->s->lbfactor);
778                 ap_rputs("        </httpd:worker>\n", r);
779                 ++worker;
780             }
781             ap_rputs("      </httpd:workers>\n", r);
782             ap_rputs("    </httpd:balancer>\n", r);
783             ++balancer;
784         }
785         ap_rputs("  </httpd:balancers>\n", r);
786         ap_rputs("</httpd:manager>", r);
787     }
788     else {
789         ap_set_content_type(r, "text/html; charset=ISO-8859-1");
790         ap_rputs(DOCTYPE_HTML_3_2
791                  "<html><head><title>Balancer Manager</title></head>\n", r);
792         ap_rputs("<body><h1>Load Balancer Manager for ", r);
793         ap_rvputs(r, ap_get_server_name(r), "</h1>\n\n", NULL);
794         ap_rvputs(r, "<dl><dt>Server Version: ",
795                   ap_get_server_description(), "</dt>\n", NULL);
796         ap_rvputs(r, "<dt>Server Built: ",
797                   ap_get_server_built(), "\n</dt></dl>\n", NULL);
798         balancer = (proxy_balancer *)conf->balancers->elts;
799         for (i = 0; i < conf->balancers->nelts; i++) {
800
801             ap_rputs("<hr />\n<h3>LoadBalancer Status for ", r);
802             ap_rvputs(r, balancer->name, "</h3>\n\n", NULL);
803             ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
804                 "<th>StickySession</th><th>Timeout</th><th>FailoverAttempts</th><th>Method</th>"
805                 "</tr>\n<tr>", r);
806             if (balancer->sticky) {
807                 if (strcmp(balancer->sticky, balancer->sticky_path)) {
808                     ap_rvputs(r, "<td>", balancer->sticky, " | ",
809                               balancer->sticky_path, NULL);
810                 }
811                 else {
812                     ap_rvputs(r, "<td>", balancer->sticky, NULL);
813                 }
814             }
815             else {
816                 ap_rputs("<td> - ", r);
817             }
818             ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
819                 apr_time_sec(balancer->timeout));
820             ap_rprintf(r, "<td>%d</td>\n", balancer->max_attempts);
821             ap_rprintf(r, "<td>%s</td>\n",
822                        balancer->lbmethod->name);
823             ap_rputs("</table>\n<br />", r);
824             ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
825                 "<th>Worker URL</th>"
826                 "<th>Route</th><th>RouteRedir</th>"
827                 "<th>Factor</th><th>Set</th><th>Status</th>"
828                 "<th>Elected</th><th>To</th><th>From</th>"
829                 "</tr>\n", r);
830
831             worker = (proxy_worker *)balancer->workers->elts;
832             for (n = 0; n < balancer->workers->nelts; n++) {
833                 char fbuf[50];
834                 ap_rvputs(r, "<tr>\n<td><a href=\"", r->uri, "?b=",
835                           balancer->name + sizeof("balancer://") - 1, "&w=",
836                           ap_escape_uri(r->pool, worker->name),
837                           "&nonce=", balancer_nonce, 
838                           "\">", NULL);
839                 ap_rvputs(r, worker->name, "</a></td>", NULL);
840                 ap_rvputs(r, "<td>", ap_escape_html(r->pool, worker->s->route),
841                           NULL);
842                 ap_rvputs(r, "</td><td>",
843                           ap_escape_html(r->pool, worker->s->redirect), NULL);
844                 ap_rprintf(r, "</td><td>%d</td>", worker->s->lbfactor);
845                 ap_rprintf(r, "<td>%d</td><td>", worker->s->lbset);
846                 if (worker->s->status & PROXY_WORKER_DISABLED)
847                    ap_rputs("Dis ", r);
848                 if (worker->s->status & PROXY_WORKER_IN_ERROR)
849                    ap_rputs("Err ", r);
850                 if (worker->s->status & PROXY_WORKER_STOPPED)
851                    ap_rputs("Stop ", r);
852                 if (worker->s->status & PROXY_WORKER_HOT_STANDBY)
853                    ap_rputs("Stby ", r);
854                 if (PROXY_WORKER_IS_USABLE(worker))
855                     ap_rputs("Ok", r);
856                 if (!PROXY_WORKER_IS_INITIALIZED(worker))
857                     ap_rputs("-", r);
858                 ap_rputs("</td>", r);
859                 ap_rprintf(r, "<td>%" APR_SIZE_T_FMT "</td><td>", worker->s->elected);
860                 ap_rputs(apr_strfsize(worker->s->transferred, fbuf), r);
861                 ap_rputs("</td><td>", r);
862                 ap_rputs(apr_strfsize(worker->s->read, fbuf), r);
863                 ap_rputs("</td></tr>\n", r);
864
865                 ++worker;
866             }
867             ap_rputs("</table>\n", r);
868             ++balancer;
869         }
870         ap_rputs("<hr />\n", r);
871         if (wsel && bsel) {
872             ap_rputs("<h3>Edit worker settings for ", r);
873             ap_rvputs(r, wsel->name, "</h3>\n", NULL);
874             ap_rvputs(r, "<form method=\"GET\" action=\"", NULL);
875             ap_rvputs(r, r->uri, "\">\n<dl>", NULL);
876             ap_rputs("<table><tr><td>Load factor:</td><td><input name=\"lf\" type=text ", r);
877             ap_rprintf(r, "value=\"%d\"></td></tr>\n", wsel->s->lbfactor);
878             ap_rputs("<tr><td>LB Set:</td><td><input name=\"ls\" type=text ", r);
879             ap_rprintf(r, "value=\"%d\"></td></tr>\n", wsel->s->lbset);
880             ap_rputs("<tr><td>Route:</td><td><input name=\"wr\" type=text ", r);
881             ap_rvputs(r, "value=\"", ap_escape_html(r->pool, wsel->s->route),
882                       NULL);
883             ap_rputs("\"></td></tr>\n", r);
884             ap_rputs("<tr><td>Route Redirect:</td><td><input name=\"rr\" type=text ", r);
885             ap_rvputs(r, "value=\"", ap_escape_html(r->pool, wsel->s->redirect),
886                       NULL);
887             ap_rputs("\"></td></tr>\n", r);
888             ap_rputs("<tr><td>Status:</td><td>Disabled: <input name=\"dw\" value=\"Disable\" type=radio", r);
889             if (wsel->s->status & PROXY_WORKER_DISABLED)
890                 ap_rputs(" checked", r);
891             ap_rputs("> | Enabled: <input name=\"dw\" value=\"Enable\" type=radio", r);
892             if (!(wsel->s->status & PROXY_WORKER_DISABLED))
893                 ap_rputs(" checked", r);
894             ap_rputs("></td></tr>\n", r);
895             ap_rputs("<tr><td colspan=2><input type=submit value=\"Submit\"></td></tr>\n", r);
896             ap_rvputs(r, "</table>\n<input type=hidden name=\"w\" ",  NULL);
897             ap_rvputs(r, "value=\"", ap_escape_uri(r->pool, wsel->name), "\">\n", NULL);
898             ap_rvputs(r, "<input type=hidden name=\"b\" ", NULL);
899             ap_rvputs(r, "value=\"", bsel->name + sizeof("balancer://") - 1,
900                       "\">\n</form>\n", NULL);
901             ap_rvputs(r, "<input type=hidden name=\"nonce\" value=\"", 
902                       balancer_nonce, "\">\n", NULL);
903             ap_rputs("<hr />\n", r);
904         }
905         ap_rputs(ap_psignature("",r), r);
906         ap_rputs("</body></html>\n", r);
907     }
908     return OK;
909 }
910
911 static void child_init(apr_pool_t *p, server_rec *s)
912 {
913     while (s) {
914         void *sconf = s->module_config;
915         proxy_server_conf *conf;
916         proxy_balancer *balancer;
917         int i;
918         conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
919
920         /* Initialize shared scoreboard data */
921         balancer = (proxy_balancer *)conf->balancers->elts;
922         for (i = 0; i < conf->balancers->nelts; i++) {
923             init_balancer_members(conf, s, balancer);
924             balancer++;
925         }
926         s = s->next;
927     }
928
929 }
930
931 /*
932  * The idea behind the find_best_byrequests scheduler is the following:
933  *
934  * lbfactor is "how much we expect this worker to work", or "the worker's
935  * normalized work quota".
936  *
937  * lbstatus is "how urgent this worker has to work to fulfill its quota
938  * of work".
939  *
940  * We distribute each worker's work quota to the worker, and then look
941  * which of them needs to work most urgently (biggest lbstatus).  This
942  * worker is then selected for work, and its lbstatus reduced by the
943  * total work quota we distributed to all workers.  Thus the sum of all
944  * lbstatus does not change.(*)
945  *
946  * If some workers are disabled, the others will
947  * still be scheduled correctly.
948  *
949  * If a balancer is configured as follows:
950  *
951  * worker     a    b    c    d
952  * lbfactor  25   25   25   25
953  *
954  * And b gets disabled, the following schedule is produced:
955  *
956  *    a c d a c d a c d ...
957  *
958  * Note that the above lbfactor setting is the *exact* same as:
959  *
960  * worker     a    b    c    d
961  * lbfactor   1    1    1    1
962  *
963  * Asymmetric configurations work as one would expect. For
964  * example:
965  *
966  * worker     a    b    c    d
967  * lbfactor   1    1    1    2
968  *
969  * would have a, b and c all handling about the same
970  * amount of load with d handling twice what a or b
971  * or c handles individually. So we could see:
972  *
973  *   b a d c d a c d b d ...
974  *
975  */
976
977 static proxy_worker *find_best_byrequests(proxy_balancer *balancer,
978                                 request_rec *r)
979 {
980     int i;
981     int total_factor = 0;
982     proxy_worker *worker;
983     proxy_worker *mycandidate = NULL;
984     int cur_lbset = 0;
985     int max_lbset = 0;
986     int checking_standby;
987     int checked_standby;
988     
989     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
990                  "proxy: Entering byrequests for BALANCER (%s)",
991                  balancer->name);
992
993     /* First try to see if we have available candidate */
994     do {
995         checking_standby = checked_standby = 0;
996         while (!mycandidate && !checked_standby) {
997             worker = (proxy_worker *)balancer->workers->elts;
998             for (i = 0; i < balancer->workers->nelts; i++, worker++) {
999                 if (!checking_standby) {    /* first time through */
1000                     if (worker->s->lbset > max_lbset)
1001                         max_lbset = worker->s->lbset;
1002                 }
1003                 if (worker->s->lbset > cur_lbset)
1004                     continue;
1005                 if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
1006                     continue;
1007                 /* If the worker is in error state run
1008                  * retry on that worker. It will be marked as
1009                  * operational if the retry timeout is elapsed.
1010                  * The worker might still be unusable, but we try
1011                  * anyway.
1012                  */
1013                 if (!PROXY_WORKER_IS_USABLE(worker))
1014                     ap_proxy_retry_worker("BALANCER", worker, r->server);
1015                 /* Take into calculation only the workers that are
1016                  * not in error state or not disabled.
1017                  */
1018                 if (PROXY_WORKER_IS_USABLE(worker)) {
1019                     worker->s->lbstatus += worker->s->lbfactor;
1020                     total_factor += worker->s->lbfactor;
1021                     if (!mycandidate || worker->s->lbstatus > mycandidate->s->lbstatus)
1022                         mycandidate = worker;
1023                 }
1024             }
1025             checked_standby = checking_standby++;
1026         }
1027         cur_lbset++;
1028     } while (cur_lbset <= max_lbset && !mycandidate);
1029
1030     if (mycandidate) {
1031         mycandidate->s->lbstatus -= total_factor;
1032     }
1033
1034     return mycandidate;
1035 }
1036
1037 /*
1038  * The idea behind the find_best_bytraffic scheduler is the following:
1039  *
1040  * We know the amount of traffic (bytes in and out) handled by each
1041  * worker. We normalize that traffic by each workers' weight. So assuming
1042  * a setup as below:
1043  *
1044  * worker     a    b    c
1045  * lbfactor   1    1    3
1046  *
1047  * the scheduler will allow worker c to handle 3 times the
1048  * traffic of a and b. If each request/response results in the
1049  * same amount of traffic, then c would be accessed 3 times as
1050  * often as a or b. If, for example, a handled a request that
1051  * resulted in a large i/o bytecount, then b and c would be
1052  * chosen more often, to even things out.
1053  */
1054 static proxy_worker *find_best_bytraffic(proxy_balancer *balancer,
1055                                          request_rec *r)
1056 {
1057     int i;
1058     apr_off_t mytraffic = 0;
1059     apr_off_t curmin = 0;
1060     proxy_worker *worker;
1061     proxy_worker *mycandidate = NULL;
1062     int cur_lbset = 0;
1063     int max_lbset = 0;
1064     int checking_standby;
1065     int checked_standby;
1066
1067     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1068                  "proxy: Entering bytraffic for BALANCER (%s)",
1069                  balancer->name);
1070
1071     /* First try to see if we have available candidate */
1072     do {
1073         checking_standby = checked_standby = 0;
1074         while (!mycandidate && !checked_standby) {
1075             worker = (proxy_worker *)balancer->workers->elts;
1076             for (i = 0; i < balancer->workers->nelts; i++, worker++) {
1077                 if (!checking_standby) {    /* first time through */
1078                     if (worker->s->lbset > max_lbset)
1079                         max_lbset = worker->s->lbset;
1080                 }
1081                 if (worker->s->lbset > cur_lbset)
1082                     continue;
1083                 if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
1084                     continue;
1085                 /* If the worker is in error state run
1086                  * retry on that worker. It will be marked as
1087                  * operational if the retry timeout is elapsed.
1088                  * The worker might still be unusable, but we try
1089                  * anyway.
1090                  */
1091                 if (!PROXY_WORKER_IS_USABLE(worker))
1092                     ap_proxy_retry_worker("BALANCER", worker, r->server);
1093                 /* Take into calculation only the workers that are
1094                  * not in error state or not disabled.
1095                  */
1096                 if (PROXY_WORKER_IS_USABLE(worker)) {
1097                     mytraffic = (worker->s->transferred/worker->s->lbfactor) +
1098                                 (worker->s->read/worker->s->lbfactor);
1099                     if (!mycandidate || mytraffic < curmin) {
1100                         mycandidate = worker;
1101                         curmin = mytraffic;
1102                     }
1103                 }
1104             }
1105             checked_standby = checking_standby++;
1106         }
1107         cur_lbset++;
1108     } while (cur_lbset <= max_lbset && !mycandidate);
1109
1110     return mycandidate;
1111 }
1112
1113 /*
1114  * How to add additional lbmethods:
1115  *   1. Create func which determines "best" candidate worker
1116  *      (eg: find_best_bytraffic, above)
1117  *   2. Register it as a provider.
1118  */
1119 static const proxy_balancer_method byrequests =
1120 {
1121     "byrequests",
1122     &find_best_byrequests,
1123     NULL
1124 };
1125
1126 static const proxy_balancer_method bytraffic =
1127 {
1128     "bytraffic",
1129     &find_best_bytraffic,
1130     NULL
1131 };
1132
1133 static void ap_proxy_balancer_register_hook(apr_pool_t *p)
1134 {
1135     /* Only the mpm_winnt has child init hook handler.
1136      * make sure that we are called after the mpm
1137      * initializes and after the mod_proxy
1138      */
1139     static const char *const aszPred[] = { "mpm_winnt.c", "mod_proxy.c", NULL};
1140      /* manager handler */
1141     ap_hook_post_config(balancer_init, NULL, NULL, APR_HOOK_MIDDLE);
1142     ap_hook_handler(balancer_handler, NULL, NULL, APR_HOOK_FIRST);
1143     ap_hook_child_init(child_init, aszPred, NULL, APR_HOOK_MIDDLE);
1144     proxy_hook_pre_request(proxy_balancer_pre_request, NULL, NULL, APR_HOOK_FIRST);
1145     proxy_hook_post_request(proxy_balancer_post_request, NULL, NULL, APR_HOOK_FIRST);
1146     proxy_hook_canon_handler(proxy_balancer_canon, NULL, NULL, APR_HOOK_FIRST);
1147     ap_register_provider(p, PROXY_LBMETHOD, "bytraffic", "0", &bytraffic);
1148     ap_register_provider(p, PROXY_LBMETHOD, "byrequests", "0", &byrequests);
1149 }
1150
1151 module AP_MODULE_DECLARE_DATA proxy_balancer_module = {
1152     STANDARD20_MODULE_STUFF,
1153     NULL,       /* create per-directory config structure */
1154     NULL,       /* merge per-directory config structures */
1155     NULL,       /* create per-server config structure */
1156     NULL,       /* merge per-server config structures */
1157     NULL,       /* command apr_table_t */
1158     ap_proxy_balancer_register_hook /* register hooks */
1159 };