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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* Load balancer module for Apache proxy */
19 #include "mod_proxy.h"
20 #include "scoreboard.h"
22 #include "apr_version.h"
26 static const char *balancer_mutex_type = "proxy-balancer-shm";
27 ap_slotmem_provider_t *storage = NULL;
29 module AP_MODULE_DECLARE_DATA proxy_balancer_module;
31 static int (*ap_proxy_retry_worker_fn)(const char *proxy_function,
32 proxy_worker *worker, server_rec *s) = NULL;
35 * Register our mutex type before the config is read so we
36 * can adjust the mutex settings using the Mutex directive.
38 static int balancer_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
44 rv = ap_mutex_register(pconf, balancer_mutex_type, NULL,
46 if (rv != APR_SUCCESS) {
54 extern void proxy_update_members(proxy_balancer **balancer, request_rec *r,
55 proxy_server_conf *conf);
58 static int proxy_balancer_canon(request_rec *r, char *url)
65 /* TODO: offset of BALANCER_PREFIX ?? */
66 if (strncasecmp(url, "balancer:", 9) == 0) {
73 ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, "canonicalising URL %s", url);
76 * We break the URL into host, port, path, search
78 err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
80 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01157)
81 "error parsing URL %s: %s",
83 return HTTP_BAD_REQUEST;
86 * now parse path/search args, according to rfc1738:
87 * process the path. With proxy-noncanon set (by
88 * mod_proxy) we use the raw, unparsed uri
90 if (apr_table_get(r->notes, "proxy-nocanon")) {
91 path = url; /* this is the raw path */
94 path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
99 return HTTP_BAD_REQUEST;
101 r->filename = apr_pstrcat(r->pool, "proxy:", BALANCER_PREFIX, host,
102 "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
104 r->path_info = apr_pstrcat(r->pool, "/", path, NULL);
109 static void init_balancer_members(apr_pool_t *p, server_rec *s,
110 proxy_balancer *balancer)
113 proxy_worker **workers;
115 workers = (proxy_worker **)balancer->workers->elts;
117 for (i = 0; i < balancer->workers->nelts; i++) {
118 int worker_is_initialized;
119 proxy_worker *worker = *workers;
120 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01158)
121 "Looking at %s -> %s initialized?", balancer->s->name, worker->s->name);
122 worker_is_initialized = PROXY_WORKER_IS_INITIALIZED(worker);
123 if (!worker_is_initialized) {
124 ap_proxy_initialize_worker(worker, s, p);
129 /* Set default number of attempts to the number of
132 if (!balancer->s->max_attempts_set && balancer->workers->nelts > 1) {
133 balancer->s->max_attempts = balancer->workers->nelts - 1;
134 balancer->s->max_attempts_set = 1;
138 /* Retrieve the parameter with the given name
139 * Something like 'JSESSIONID=12345...N'
141 static char *get_path_param(apr_pool_t *pool, char *url,
142 const char *name, int scolon_sep)
145 char *pathdelims = "?&";
150 for (path = strstr(url, name); path; path = strstr(path + 1, name)) {
151 path += strlen(name);
154 * Session path was found, get its value
159 path = apr_strtok(apr_pstrdup(pool, path), pathdelims, &q);
167 static char *get_cookie_param(request_rec *r, const char *name)
170 const char *start_cookie;
172 if ((cookies = apr_table_get(r->headers_in, "Cookie"))) {
173 for (start_cookie = ap_strstr_c(cookies, name); start_cookie;
174 start_cookie = ap_strstr_c(start_cookie + 1, name)) {
175 if (start_cookie == cookies ||
176 start_cookie[-1] == ';' ||
177 start_cookie[-1] == ',' ||
178 isspace(start_cookie[-1])) {
180 start_cookie += strlen(name);
181 while(*start_cookie && isspace(*start_cookie))
183 if (*start_cookie++ == '=' && *start_cookie) {
185 * Session cookie was found, get its value
187 char *end_cookie, *cookie;
188 cookie = apr_pstrdup(r->pool, start_cookie);
189 if ((end_cookie = strchr(cookie, ';')) != NULL)
191 if((end_cookie = strchr(cookie, ',')) != NULL)
201 /* Find the worker that has the 'route' defined
203 static proxy_worker *find_route_worker(proxy_balancer *balancer,
204 const char *route, request_rec *r)
207 int checking_standby;
210 proxy_worker **workers;
212 checking_standby = checked_standby = 0;
213 while (!checked_standby) {
214 workers = (proxy_worker **)balancer->workers->elts;
215 for (i = 0; i < balancer->workers->nelts; i++, workers++) {
216 proxy_worker *worker = *workers;
217 if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
219 if (*(worker->s->route) && strcmp(worker->s->route, route) == 0) {
220 if (PROXY_WORKER_IS_USABLE(worker)) {
224 * If the worker is in error state run
225 * retry on that worker. It will be marked as
226 * operational if the retry timeout is elapsed.
227 * The worker might still be unusable, but we try
230 ap_proxy_retry_worker_fn("BALANCER", worker, r->server);
231 if (PROXY_WORKER_IS_USABLE(worker)) {
235 * We have a worker that is unusable.
236 * It can be in error or disabled, but in case
237 * it has a redirection set use that redirection worker.
238 * This enables to safely remove the member from the
239 * balancer. Of course you will need some kind of
240 * session replication between those two remote.
242 if (*worker->s->redirect) {
243 proxy_worker *rworker = NULL;
244 rworker = find_route_worker(balancer, worker->s->redirect, r);
245 /* Check if the redirect worker is usable */
246 if (rworker && !PROXY_WORKER_IS_USABLE(rworker)) {
248 * If the worker is in error state run
249 * retry on that worker. It will be marked as
250 * operational if the retry timeout is elapsed.
251 * The worker might still be unusable, but we try
254 ap_proxy_retry_worker_fn("BALANCER", rworker, r->server);
256 if (rworker && PROXY_WORKER_IS_USABLE(rworker))
263 checked_standby = checking_standby++;
268 static proxy_worker *find_session_route(proxy_balancer *balancer,
271 const char **sticky_used,
274 proxy_worker *worker = NULL;
276 if (!*balancer->s->sticky)
278 /* Try to find the sticky route inside url */
279 *route = get_path_param(r->pool, *url, balancer->s->sticky_path, balancer->s->scolonsep);
281 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01159)
282 "Found value %s for stickysession %s",
283 *route, balancer->s->sticky_path);
284 *sticky_used = balancer->s->sticky_path;
287 *route = get_cookie_param(r, balancer->s->sticky);
289 *sticky_used = balancer->s->sticky;
290 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01160)
291 "Found value %s for stickysession %s",
292 *route, balancer->s->sticky);
296 * If we found a value for stickysession, find the first '.' (or whatever
297 * sticky_separator is set to) within. Everything after '.' (if present)
300 if ((*route) && (balancer->s->sticky_separator != 0) && ((*route = strchr(*route, balancer->s->sticky_separator)) != NULL ))
302 if ((*route) && (**route)) {
303 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01161) "Found route %s", *route);
304 /* We have a route in path or in cookie
305 * Find the worker that has this route defined.
307 worker = find_route_worker(balancer, *route, r);
308 if (worker && strcmp(*route, worker->s->route)) {
310 * Notice that the route of the worker chosen is different from
311 * the route supplied by the client.
313 apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
314 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01162)
315 "Route changed from %s to %s",
316 *route, worker->s->route);
324 static proxy_worker *find_best_worker(proxy_balancer *balancer,
327 proxy_worker *candidate = NULL;
330 if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
331 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01163)
332 "%s: Lock failed for find_best_worker()",
337 candidate = (*balancer->lbmethod->finder)(balancer, r);
340 candidate->s->elected++;
342 if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
343 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01164)
344 "%s: Unlock failed for find_best_worker()",
348 if (candidate == NULL) {
349 /* All the workers are in error state or disabled.
350 * If the balancer has a timeout sleep for a while
351 * and try again to find the worker. The chances are
352 * that some other thread will release a connection.
353 * By default the timeout is not set, and the server
354 * returns SERVER_BUSY.
356 if (balancer->s->timeout) {
357 /* XXX: This can perhaps be build using some
358 * smarter mechanism, like tread_cond.
359 * But since the statuses can came from
360 * different childs, use the provided algo.
362 apr_interval_time_t timeout = balancer->s->timeout;
363 apr_interval_time_t step, tval = 0;
364 /* Set the timeout to 0 so that we don't
365 * end in infinite loop
367 balancer->s->timeout = 0;
368 step = timeout / 100;
369 while (tval < timeout) {
372 if ((candidate = find_best_worker(balancer, r)))
376 /* restore the timeout */
377 balancer->s->timeout = timeout;
385 static int rewrite_url(request_rec *r, proxy_worker *worker,
388 const char *scheme = strstr(*url, "://");
389 const char *path = NULL;
392 path = ap_strchr_c(scheme + 3, '/');
394 /* we break the URL into host, port, uri */
396 return ap_proxyerror(r, HTTP_BAD_REQUEST, apr_pstrcat(r->pool,
397 "missing worker. URI cannot be parsed: ", *url,
401 *url = apr_pstrcat(r->pool, worker->s->name, path, NULL);
406 static void force_recovery(proxy_balancer *balancer, server_rec *s)
410 proxy_worker **worker;
412 worker = (proxy_worker **)balancer->workers->elts;
413 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
414 if (!((*worker)->s->status & PROXY_WORKER_IN_ERROR)) {
419 /* Try if we can recover */
420 ap_proxy_retry_worker_fn("BALANCER", *worker, s);
421 if (!((*worker)->s->status & PROXY_WORKER_IN_ERROR)) {
427 if (!ok && balancer->s->forcerecovery) {
428 /* If all workers are in error state force the recovery.
430 worker = (proxy_worker **)balancer->workers->elts;
431 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
432 ++(*worker)->s->retries;
433 (*worker)->s->status &= ~PROXY_WORKER_IN_ERROR;
434 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01165)
435 "%s: Forcing recovery for worker (%s)",
436 balancer->s->name, (*worker)->s->hostname);
441 static apr_status_t decrement_busy_count(void *worker_)
443 proxy_worker *worker = worker_;
445 if (worker->s->busy) {
452 static int proxy_balancer_pre_request(proxy_worker **worker,
453 proxy_balancer **balancer,
455 proxy_server_conf *conf, char **url)
458 proxy_worker *runtime;
460 const char *sticky = NULL;
464 /* Step 1: check if the url is for us
465 * The url we can handle starts with 'balancer://'
466 * If balancer is already provided skip the search
467 * for balancer, because this is failover attempt.
470 !(*balancer = ap_proxy_get_balancer(r->pool, conf, *url, 1)))
473 /* Step 2: Lock the LoadBalancer
474 * XXX: perhaps we need the process lock here
476 if ((rv = PROXY_THREAD_LOCK(*balancer)) != APR_SUCCESS) {
477 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01166)
478 "%s: Lock failed for pre_request", (*balancer)->s->name);
482 /* Step 3: force recovery */
483 force_recovery(*balancer, r->server);
485 /* Step 3.5: Update member list for the balancer */
486 /* TODO: Implement as provider! */
487 ap_proxy_sync_balancer(*balancer, r->server, conf);
489 /* Step 4: find the session route */
490 runtime = find_session_route(*balancer, r, &route, &sticky, url);
492 if ((*balancer)->lbmethod && (*balancer)->lbmethod->updatelbstatus) {
493 /* Call the LB implementation */
494 (*balancer)->lbmethod->updatelbstatus(*balancer, runtime, r->server);
496 else { /* Use the default one */
497 int i, total_factor = 0;
498 proxy_worker **workers;
499 /* We have a sticky load balancer
500 * Update the workers status
501 * so that even session routes get
504 workers = (proxy_worker **)(*balancer)->workers->elts;
505 for (i = 0; i < (*balancer)->workers->nelts; i++) {
506 /* Take into calculation only the workers that are
507 * not in error state or not disabled.
509 if (PROXY_WORKER_IS_USABLE(*workers)) {
510 (*workers)->s->lbstatus += (*workers)->s->lbfactor;
511 total_factor += (*workers)->s->lbfactor;
515 runtime->s->lbstatus -= total_factor;
517 runtime->s->elected++;
521 else if (route && (*balancer)->s->sticky_force) {
522 int i, member_of = 0;
523 proxy_worker **workers;
525 * We have a route provided that doesn't match the
526 * balancer name. See if the provider route is the
527 * member of the same balancer in which case return 503
529 workers = (proxy_worker **)(*balancer)->workers->elts;
530 for (i = 0; i < (*balancer)->workers->nelts; i++) {
531 if (*((*workers)->s->route) && strcmp((*workers)->s->route, route) == 0) {
538 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01167)
539 "%s: All workers are in error state for route (%s)",
540 (*balancer)->s->name, route);
541 if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
542 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01168)
543 "%s: Unlock failed for pre_request",
544 (*balancer)->s->name);
546 return HTTP_SERVICE_UNAVAILABLE;
550 if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
551 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01169)
552 "%s: Unlock failed for pre_request",
553 (*balancer)->s->name);
556 runtime = find_best_worker(*balancer, r);
558 if ((*balancer)->workers->nelts) {
559 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01170)
560 "%s: All workers are in error state",
561 (*balancer)->s->name);
563 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01171)
564 "%s: No workers in balancer",
565 (*balancer)->s->name);
568 return HTTP_SERVICE_UNAVAILABLE;
570 if (*(*balancer)->s->sticky && runtime) {
572 * This balancer has sticky sessions and the client either has not
573 * supplied any routing information or all workers for this route
574 * including possible redirect and hotstandby workers are in error
575 * state, but we have found another working worker for this
576 * balancer where we can send the request. Thus notice that we have
577 * changed the route to the backend.
579 apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
584 (*worker)->s->busy++;
585 apr_pool_cleanup_register(r->pool, *worker, decrement_busy_count,
586 apr_pool_cleanup_null);
588 /* Add balancer/worker info to env. */
589 apr_table_setn(r->subprocess_env,
590 "BALANCER_NAME", (*balancer)->s->name);
591 apr_table_setn(r->subprocess_env,
592 "BALANCER_WORKER_NAME", (*worker)->s->name);
593 apr_table_setn(r->subprocess_env,
594 "BALANCER_WORKER_ROUTE", (*worker)->s->route);
596 /* Rewrite the url from 'balancer://url'
597 * to the 'worker_scheme://worker_hostname[:worker_port]/url'
598 * This replaces the balancers fictional name with the
599 * real hostname of the elected worker.
601 access_status = rewrite_url(r, *worker, url);
602 /* Add the session route to request notes if present */
604 apr_table_setn(r->notes, "session-sticky", sticky);
605 apr_table_setn(r->notes, "session-route", route);
607 /* Add session info to env. */
608 apr_table_setn(r->subprocess_env,
609 "BALANCER_SESSION_STICKY", sticky);
610 apr_table_setn(r->subprocess_env,
611 "BALANCER_SESSION_ROUTE", route);
613 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01172)
614 "%s: worker (%s) rewritten to %s",
615 (*balancer)->s->name, (*worker)->s->name, *url);
617 return access_status;
620 static int proxy_balancer_post_request(proxy_worker *worker,
621 proxy_balancer *balancer,
623 proxy_server_conf *conf)
628 if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
629 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01173)
630 "%s: Lock failed for post_request",
632 return HTTP_INTERNAL_SERVER_ERROR;
635 if (!apr_is_empty_array(balancer->errstatuses)) {
637 for (i = 0; i < balancer->errstatuses->nelts; i++) {
638 int val = ((int *)balancer->errstatuses->elts)[i];
639 if (r->status == val) {
640 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01174)
641 "%s: Forcing worker (%s) into error state "
642 "due to status code %d matching 'failonstatus' "
643 "balancer parameter",
644 balancer->s->name, worker->s->name, val);
645 worker->s->status |= PROXY_WORKER_IN_ERROR;
646 worker->s->error_time = apr_time_now();
652 if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
653 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01175)
654 "%s: Unlock failed for post_request", balancer->s->name);
656 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01176)
657 "proxy_balancer_post_request for (%s)", balancer->s->name);
662 static void recalc_factors(proxy_balancer *balancer)
665 proxy_worker **workers;
668 /* Recalculate lbfactors */
669 workers = (proxy_worker **)balancer->workers->elts;
670 /* Special case if there is only one worker its
671 * load factor will always be 1
673 if (balancer->workers->nelts == 1) {
674 (*workers)->s->lbstatus = (*workers)->s->lbfactor = 1;
677 for (i = 0; i < balancer->workers->nelts; i++) {
678 /* Update the status entries */
679 workers[i]->s->lbstatus = workers[i]->s->lbfactor;
683 static apr_status_t lock_remove(void *data)
686 proxy_balancer *balancer;
687 server_rec *s = data;
688 void *sconf = s->module_config;
689 proxy_server_conf *conf = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
691 balancer = (proxy_balancer *)conf->balancers->elts;
692 for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
693 if (balancer->gmutex) {
694 apr_global_mutex_destroy(balancer->gmutex);
695 balancer->gmutex = NULL;
701 /* post_config hook: */
702 static int balancer_post_config(apr_pool_t *pconf, apr_pool_t *plog,
703 apr_pool_t *ptemp, server_rec *s)
706 proxy_server_conf *conf;
707 ap_slotmem_instance_t *new = NULL;
710 /* balancer_post_config() will be called twice during startup. So, don't
711 * set up the static data the 1st time through. */
712 if (ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG) {
716 if (!ap_proxy_retry_worker_fn) {
717 ap_proxy_retry_worker_fn =
718 APR_RETRIEVE_OPTIONAL_FN(ap_proxy_retry_worker);
719 if (!ap_proxy_retry_worker_fn) {
720 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(02230)
721 "mod_proxy must be loaded for mod_proxy_balancer");
729 storage = ap_lookup_provider(AP_SLOTMEM_PROVIDER_GROUP, "shm",
730 AP_SLOTMEM_PROVIDER_VERSION);
732 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01177)
733 "Failed to lookup provider 'shm' for '%s': is "
734 "mod_slotmem_shm loaded??",
735 AP_SLOTMEM_PROVIDER_GROUP);
739 tstamp = apr_time_now();
741 * Go thru each Vhost and create the shared mem slotmem for
742 * each balancer's workers
747 proxy_balancer *balancer;
748 ap_slotmem_type_t type;
749 void *sconf = s->module_config;
750 conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
752 * During create_proxy_config() we created a dummy id. Now that
753 * we have identifying info, we can create the real id
755 id = apr_psprintf(pconf, "%s.%s.%d.%s.%s.%u.%s",
756 (s->server_scheme ? s->server_scheme : "????"),
757 (s->server_hostname ? s->server_hostname : "???"),
759 (s->server_admin ? s->server_admin : "??"),
760 (s->defn_name ? s->defn_name : "?"),
762 (s->error_fname ? s->error_fname : DEFAULT_ERRORLOG));
763 conf->id = apr_psprintf(pconf, "p%x",
764 ap_proxy_hashfunc(id, PROXY_HASHFUNC_DEFAULT));
766 /* Shared memory already created for this proxy_server_conf.
771 if (conf->bal_persist) {
772 type = AP_SLOTMEM_TYPE_PREGRAB | AP_SLOTMEM_TYPE_PERSIST;
774 type = AP_SLOTMEM_TYPE_PREGRAB;
776 if (conf->balancers->nelts) {
777 conf->max_balancers = conf->balancers->nelts + conf->bgrowth;
778 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01178) "Doing balancers create: %d, %d (%d)",
779 (int)ALIGNED_PROXY_BALANCER_SHARED_SIZE,
780 (int)conf->balancers->nelts, conf->max_balancers);
782 rv = storage->create(&new, conf->id,
783 ALIGNED_PROXY_BALANCER_SHARED_SIZE,
784 conf->max_balancers, type, pconf);
785 if (rv != APR_SUCCESS) {
786 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01179) "balancer slotmem_create failed");
791 conf->storage = storage;
793 /* Initialize shared scoreboard data */
794 balancer = (proxy_balancer *)conf->balancers->elts;
795 for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
796 proxy_worker **workers;
797 proxy_worker *worker;
798 proxy_balancer_shared *bshm;
801 /* now that we have the right id, we need to redo the sname field */
802 ap_pstr2_alnum(pconf, balancer->s->name + sizeof(BALANCER_PREFIX) - 1,
804 sname = apr_pstrcat(pconf, conf->id, "_", sname, NULL);
805 PROXY_STRNCPY(balancer->s->sname, sname); /* We know this will succeed */
807 balancer->max_workers = balancer->workers->nelts + balancer->growth;
809 /* Create global mutex */
810 rv = ap_global_mutex_create(&(balancer->gmutex), NULL, balancer_mutex_type,
811 balancer->s->sname, s, pconf, 0);
812 if (rv != APR_SUCCESS || !balancer->gmutex) {
813 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01180)
814 "mutex creation of %s : %s failed", balancer_mutex_type,
816 return HTTP_INTERNAL_SERVER_ERROR;
819 apr_pool_cleanup_register(pconf, (void *)s, lock_remove,
820 apr_pool_cleanup_null);
822 /* setup shm for balancers */
823 if ((rv = storage->fgrab(conf->bslot, i)) != APR_SUCCESS) {
824 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01181) "balancer slotmem_grab failed");
828 if ((rv = storage->dptr(conf->bslot, i, (void *)&bshm)) != APR_SUCCESS) {
829 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01182) "balancer slotmem_dptr failed");
832 if ((rv = ap_proxy_share_balancer(balancer, bshm, i)) != APR_SUCCESS) {
833 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01183) "Cannot share balancer");
837 /* create slotmem slots for workers */
838 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01184) "Doing workers create: %s (%s), %d, %d [%u]",
839 balancer->s->name, balancer->s->sname,
840 (int)ALIGNED_PROXY_WORKER_SHARED_SIZE,
841 (int)balancer->max_workers, i);
843 rv = storage->create(&new, balancer->s->sname,
844 ALIGNED_PROXY_WORKER_SHARED_SIZE,
845 balancer->max_workers, type, pconf);
846 if (rv != APR_SUCCESS) {
847 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01185) "worker slotmem_create failed");
850 balancer->wslot = new;
851 balancer->storage = storage;
853 /* sync all timestamps */
854 balancer->wupdated = balancer->s->wupdated = tstamp;
856 /* now go thru each worker */
857 workers = (proxy_worker **)balancer->workers->elts;
858 for (j = 0; j < balancer->workers->nelts; j++, workers++) {
859 proxy_worker_shared *shm;
862 if ((rv = storage->fgrab(balancer->wslot, j)) != APR_SUCCESS) {
863 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01186) "worker slotmem_grab failed");
867 if ((rv = storage->dptr(balancer->wslot, j, (void *)&shm)) != APR_SUCCESS) {
868 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01187) "worker slotmem_dptr failed");
871 if ((rv = ap_proxy_share_worker(worker, shm, j)) != APR_SUCCESS) {
872 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01188) "Cannot share worker");
875 worker->s->updated = tstamp;
877 if (conf->bal_persist) {
878 /* We could have just read-in a persisted config. Force a sync. */
879 balancer->wupdated--;
880 ap_proxy_sync_balancer(balancer, s, conf);
889 static void create_radio(const char *name, unsigned int flag, request_rec *r)
891 ap_rvputs(r, "<td>On <input name='", name, "' id='", name, "' value='1' type=radio", NULL);
893 ap_rputs(" checked", r);
894 ap_rvputs(r, "> <br/> Off <input name='", name, "' id='", name, "' value='0' type=radio", NULL);
896 ap_rputs(" checked", r);
897 ap_rputs("></td>\n", r);
900 static void push2table(const char *input, apr_table_t *params,
901 const char *allowed[], apr_pool_t *p)
910 args = apr_pstrdup(p, input);
912 key = apr_strtok(args, "&", &tok);
914 val = strchr(key, '=');
921 ap_unescape_url(key);
922 ap_unescape_url(val);
923 if (allowed == NULL) { /* allow all */
924 apr_table_set(params, key, val);
927 const char **ok = allowed;
929 if (strcmp(*ok, key) == 0) {
930 apr_table_set(params, key, val);
936 key = apr_strtok(NULL, "&", &tok);
940 /* Manages the loadfactors and member status
941 * The balancer, worker and nonce are obtained from
942 * the request args (?b=...&w=...&nonce=....).
943 * All other params are pulled from any POST
946 * /.../<whatever>/balancer/worker/nonce
948 static int balancer_handler(request_rec *r)
951 proxy_server_conf *conf;
952 proxy_balancer *balancer, *bsel = NULL;
953 proxy_worker *worker, *wsel = NULL;
954 proxy_worker **workers = NULL;
962 /* is this for us? */
963 if (strcmp(r->handler, "balancer-manager")) {
968 | (AP_METHOD_BIT << M_GET)
969 | (AP_METHOD_BIT << M_POST);
970 if ((r->method_number != M_GET) && (r->method_number != M_POST)) {
974 sconf = r->server->module_config;
975 conf = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
976 params = apr_table_make(r->pool, 10);
978 balancer = (proxy_balancer *)conf->balancers->elts;
979 for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
980 if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
981 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01189)
982 "%s: Lock failed for balancer_handler",
985 ap_proxy_sync_balancer(balancer, r->server, conf);
986 if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
987 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01190)
988 "%s: Unlock failed for balancer_handler",
993 if (r->args && (r->method_number == M_GET)) {
994 const char *allowed[] = { "w", "b", "nonce", "xml", NULL };
995 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01191) "parsing r->args");
997 push2table(r->args, params, allowed, r->pool);
999 if (r->method_number == M_POST) {
1000 apr_bucket_brigade *ib;
1001 apr_size_t len = 1024;
1002 char *buf = apr_pcalloc(r->pool, len+1);
1004 ib = apr_brigade_create(r->connection->pool, r->connection->bucket_alloc);
1005 rv = ap_get_brigade(r->input_filters, ib, AP_MODE_READBYTES,
1006 APR_BLOCK_READ, len);
1007 if (rv != APR_SUCCESS) {
1008 return HTTP_INTERNAL_SERVER_ERROR;
1010 apr_brigade_flatten(ib, buf, &len);
1012 push2table(buf, params, NULL, r->pool);
1014 if ((name = apr_table_get(params, "b")))
1015 bsel = ap_proxy_get_balancer(r->pool, conf,
1016 apr_pstrcat(r->pool, BALANCER_PREFIX, name, NULL), 0);
1018 if ((name = apr_table_get(params, "w"))) {
1019 wsel = ap_proxy_get_worker(r->pool, bsel, conf, name);
1023 /* Check that the supplied nonce matches this server's nonce;
1024 * otherwise ignore all parameters, to prevent a CSRF attack. */
1028 (name = apr_table_get(params, "nonce")) == NULL ||
1029 strcmp(bsel->s->nonce, name) != 0
1033 apr_table_clear(params);
1037 /* First set the params */
1038 if (wsel && ok2change) {
1040 int was_usable = PROXY_WORKER_IS_USABLE(wsel);
1042 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01192) "settings worker params");
1044 if ((val = apr_table_get(params, "w_lf"))) {
1045 int ival = atoi(val);
1046 if (ival >= 1 && ival <= 100) {
1047 wsel->s->lbfactor = ival;
1049 recalc_factors(bsel);
1052 if ((val = apr_table_get(params, "w_wr"))) {
1053 if (strlen(val) && strlen(val) < sizeof(wsel->s->route))
1054 strcpy(wsel->s->route, val);
1056 *wsel->s->route = '\0';
1058 if ((val = apr_table_get(params, "w_rr"))) {
1059 if (strlen(val) && strlen(val) < sizeof(wsel->s->redirect))
1060 strcpy(wsel->s->redirect, val);
1062 *wsel->s->redirect = '\0';
1064 if ((val = apr_table_get(params, "w_status_I"))) {
1065 ap_proxy_set_wstatus('I', atoi(val), wsel);
1067 if ((val = apr_table_get(params, "w_status_N"))) {
1068 ap_proxy_set_wstatus('N', atoi(val), wsel);
1070 if ((val = apr_table_get(params, "w_status_D"))) {
1071 ap_proxy_set_wstatus('D', atoi(val), wsel);
1073 if ((val = apr_table_get(params, "w_status_H"))) {
1074 ap_proxy_set_wstatus('H', atoi(val), wsel);
1076 if ((val = apr_table_get(params, "w_ls"))) {
1077 int ival = atoi(val);
1078 if (ival >= 0 && ival <= 99) {
1079 wsel->s->lbset = ival;
1082 /* if enabling, we need to reset all lb params */
1083 if (bsel && !was_usable && PROXY_WORKER_IS_USABLE(wsel)) {
1084 bsel->s->need_reset = 1;
1089 if (bsel && ok2change) {
1092 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01193)
1093 "settings balancer params");
1094 if ((val = apr_table_get(params, "b_lbm"))) {
1095 if ((strlen(val) < (sizeof(bsel->s->lbpname)-1)) &&
1096 strcmp(val, bsel->s->lbpname)) {
1097 proxy_balancer_method *lbmethod;
1098 lbmethod = ap_lookup_provider(PROXY_LBMETHOD, val, "0");
1100 PROXY_STRNCPY(bsel->s->lbpname, val);
1101 bsel->lbmethod = lbmethod;
1102 bsel->s->wupdated = apr_time_now();
1103 bsel->s->need_reset = 1;
1107 if ((val = apr_table_get(params, "b_tmo"))) {
1109 if (ival >= 0 && ival <= 7200) { /* 2 hrs enuff? */
1110 bsel->s->timeout = apr_time_from_sec(ival);
1113 if ((val = apr_table_get(params, "b_max"))) {
1115 if (ival >= 0 && ival <= 99) {
1116 bsel->s->max_attempts = ival;
1119 if ((val = apr_table_get(params, "b_sforce"))) {
1121 bsel->s->sticky_force = (ival != 0);
1123 if ((val = apr_table_get(params, "b_ss")) && *val) {
1124 if (strlen(val) < (sizeof(bsel->s->sticky_path)-1)) {
1125 if (*val == '-' && *(val+1) == '\0')
1126 *bsel->s->sticky_path = *bsel->s->sticky = '\0';
1129 PROXY_STRNCPY(bsel->s->sticky_path, val);
1130 PROXY_STRNCPY(bsel->s->sticky, val);
1132 if ((path = strchr((char *)bsel->s->sticky, '|'))) {
1134 PROXY_STRNCPY(bsel->s->sticky_path, path);
1139 if ((val = apr_table_get(params, "b_wyes")) &&
1140 (*val == '1' && *(val+1) == '\0') &&
1141 (val = apr_table_get(params, "b_nwrkr"))) {
1143 proxy_worker *nworker;
1144 nworker = ap_proxy_get_worker(conf->pool, bsel, conf, val);
1145 if (!nworker && storage->num_free_slots(bsel->wslot)) {
1146 if ((rv = PROXY_GLOBAL_LOCK(bsel)) != APR_SUCCESS) {
1147 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01194)
1148 "%s: Lock failed for adding worker",
1151 ret = ap_proxy_define_worker(conf->pool, &nworker, bsel, conf, val, 0);
1154 proxy_worker_shared *shm;
1155 PROXY_COPY_CONF_PARAMS(nworker, conf);
1156 if ((rv = storage->grab(bsel->wslot, &index)) != APR_SUCCESS) {
1157 ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, APLOGNO(01195)
1158 "worker slotmem_grab failed");
1159 if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
1160 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01196)
1161 "%s: Unlock failed for adding worker",
1164 return HTTP_BAD_REQUEST;
1166 if ((rv = storage->dptr(bsel->wslot, index, (void *)&shm)) != APR_SUCCESS) {
1167 ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, APLOGNO(01197)
1168 "worker slotmem_dptr failed");
1169 if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
1170 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01198)
1171 "%s: Unlock failed for adding worker",
1174 return HTTP_BAD_REQUEST;
1176 if ((rv = ap_proxy_share_worker(nworker, shm, index)) != APR_SUCCESS) {
1177 ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, APLOGNO(01199)
1178 "Cannot share worker");
1179 if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
1180 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01200)
1181 "%s: Unlock failed for adding worker",
1184 return HTTP_BAD_REQUEST;
1186 if ((rv = ap_proxy_initialize_worker(nworker, r->server, conf->pool)) != APR_SUCCESS) {
1187 ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, APLOGNO(01201)
1188 "Cannot init worker");
1189 if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
1190 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01202)
1191 "%s: Unlock failed for adding worker",
1194 return HTTP_BAD_REQUEST;
1196 /* sync all timestamps */
1197 bsel->wupdated = bsel->s->wupdated = nworker->s->updated = apr_time_now();
1198 /* by default, all new workers are disabled */
1199 ap_proxy_set_wstatus('D', 1, nworker);
1201 if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
1202 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01203)
1203 "%s: Unlock failed for adding worker",
1212 action = ap_construct_url(r->pool, r->uri, r);
1213 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01204) "genning page");
1215 if (apr_table_get(params, "xml")) {
1216 char date[APR_RFC822_DATE_LEN];
1217 ap_set_content_type(r, "text/xml");
1218 ap_rputs("<?xml version='1.0' encoding='UTF-8' ?>\n", r);
1219 ap_rputs("<httpd:manager xmlns:httpd='http://httpd.apache.org'>\n", r);
1220 ap_rputs(" <httpd:balancers>\n", r);
1221 balancer = (proxy_balancer *)conf->balancers->elts;
1222 for (i = 0; i < conf->balancers->nelts; i++) {
1223 ap_rputs(" <httpd:balancer>\n", r);
1224 /* Start proxy_balancer */
1225 ap_rvputs(r, " <httpd:name>", balancer->s->name, "</httpd:name>\n", NULL);
1226 if (balancer->s->sticky) {
1227 ap_rvputs(r, " <httpd:stickysession>", balancer->s->sticky,
1228 "</httpd:stickysession>\n", NULL);
1230 " <httpd:nofailover>%s</httpd:nofailover>\n",
1231 (balancer->s->sticky_force ? "On" : "Off"));
1234 " <httpd:timeout>%" APR_TIME_T_FMT "</httpd:timeout>",
1235 apr_time_sec(balancer->s->timeout));
1236 if (balancer->s->max_attempts_set) {
1238 " <httpd:maxattempts>%d</httpd:maxattempts>\n",
1239 balancer->s->max_attempts);
1241 ap_rvputs(r, " <httpd:lbmethod>", balancer->lbmethod->name,
1242 "</httpd:lbmethod>\n", NULL);
1243 if (balancer->s->sticky) {
1245 " <httpd:scolonpathdelim>%s</httpd:scolonpathdelim>\n",
1246 (balancer->s->scolonsep ? "On" : "Off"));
1248 /* End proxy_balancer */
1249 ap_rputs(" <httpd:workers>\n", r);
1250 workers = (proxy_worker **)balancer->workers->elts;
1251 for (n = 0; n < balancer->workers->nelts; n++) {
1253 /* Start proxy_worker */
1254 ap_rputs(" <httpd:worker>\n", r);
1255 ap_rvputs(r, " <httpd:name>", worker->s->name,
1256 "</httpd:name>\n", NULL);
1257 ap_rvputs(r, " <httpd:scheme>", worker->s->scheme,
1258 "</httpd:scheme>\n", NULL);
1259 ap_rvputs(r, " <httpd:hostname>", worker->s->hostname,
1260 "</httpd:hostname>\n", NULL);
1261 ap_rprintf(r, " <httpd:loadfactor>%d</httpd:loadfactor>\n",
1262 worker->s->lbfactor);
1264 " <httpd:port>%d</httpd:port>\n",
1266 ap_rprintf(r, " <httpd:min>%d</httpd:min>\n",
1268 ap_rprintf(r, " <httpd:smax>%d</httpd:smax>\n",
1270 ap_rprintf(r, " <httpd:max>%d</httpd:max>\n",
1273 " <httpd:ttl>%" APR_TIME_T_FMT "</httpd:ttl>\n",
1274 apr_time_sec(worker->s->ttl));
1275 if (worker->s->timeout_set) {
1277 " <httpd:timeout>%" APR_TIME_T_FMT "</httpd:timeout>\n",
1278 apr_time_sec(worker->s->timeout));
1280 if (worker->s->acquire_set) {
1282 " <httpd:acquire>%" APR_TIME_T_FMT "</httpd:acquire>\n",
1283 apr_time_msec(worker->s->acquire));
1285 if (worker->s->recv_buffer_size_set) {
1287 " <httpd:recv_buffer_size>%" APR_SIZE_T_FMT "</httpd:recv_buffer_size>\n",
1288 worker->s->recv_buffer_size);
1290 if (worker->s->io_buffer_size_set) {
1292 " <httpd:io_buffer_size>%" APR_SIZE_T_FMT "</httpd:io_buffer_size>\n",
1293 worker->s->io_buffer_size);
1295 if (worker->s->keepalive_set) {
1297 " <httpd:keepalive>%s</httpd:keepalive>\n",
1298 (worker->s->keepalive ? "On" : "Off"));
1300 /* Begin proxy_worker_stat */
1301 ap_rputs(" <httpd:status>", r);
1302 if (worker->s->status & PROXY_WORKER_DISABLED)
1303 ap_rputs("Disabled", r);
1304 else if (worker->s->status & PROXY_WORKER_IN_ERROR)
1305 ap_rputs("Error", r);
1306 else if (worker->s->status & PROXY_WORKER_STOPPED)
1307 ap_rputs("Stopped", r);
1308 else if (worker->s->status & PROXY_WORKER_HOT_STANDBY)
1309 ap_rputs("Standby", r);
1310 else if (PROXY_WORKER_IS_USABLE(worker))
1312 else if (!PROXY_WORKER_IS_INITIALIZED(worker))
1313 ap_rputs("Uninitialized", r);
1314 ap_rputs("</httpd:status>\n", r);
1315 if ((worker->s->error_time > 0) && apr_rfc822_date(date, worker->s->error_time) == APR_SUCCESS) {
1316 ap_rvputs(r, " <httpd:error_time>", date,
1317 "</httpd:error_time>\n", NULL);
1320 " <httpd:retries>%d</httpd:retries>\n",
1321 worker->s->retries);
1323 " <httpd:lbstatus>%d</httpd:lbstatus>\n",
1324 worker->s->lbstatus);
1326 " <httpd:loadfactor>%d</httpd:loadfactor>\n",
1327 worker->s->lbfactor);
1329 " <httpd:transferred>%" APR_OFF_T_FMT "</httpd:transferred>\n",
1330 worker->s->transferred);
1332 " <httpd:read>%" APR_OFF_T_FMT "</httpd:read>\n",
1335 " <httpd:elected>%" APR_SIZE_T_FMT "</httpd:elected>\n",
1336 worker->s->elected);
1337 ap_rvputs(r, " <httpd:route>",
1338 ap_escape_html(r->pool, worker->s->route),
1339 "</httpd:route>\n", NULL);
1340 ap_rvputs(r, " <httpd:redirect>",
1341 ap_escape_html(r->pool, worker->s->redirect),
1342 "</httpd:redirect>\n", NULL);
1344 " <httpd:busy>%" APR_SIZE_T_FMT "</httpd:busy>\n",
1346 ap_rprintf(r, " <httpd:lbset>%d</httpd:lbset>\n",
1348 /* End proxy_worker_stat */
1349 if (!strcasecmp(worker->s->scheme, "ajp")) {
1350 ap_rputs(" <httpd:flushpackets>", r);
1351 switch (worker->s->flush_packets) {
1359 ap_rputs("Auto", r);
1362 ap_rputs("</httpd:flushpackets>\n", r);
1363 if (worker->s->flush_packets == flush_auto) {
1365 " <httpd:flushwait>%d</httpd:flushwait>\n",
1366 worker->s->flush_wait);
1368 if (worker->s->ping_timeout_set) {
1370 " <httpd:ping>%" APR_TIME_T_FMT "</httpd:ping>",
1371 apr_time_msec(worker->s->ping_timeout));
1374 if (worker->s->disablereuse_set) {
1376 " <httpd:disablereuse>%s</httpd:disablereuse>\n",
1377 (worker->s->disablereuse ? "On" : "Off"));
1379 if (worker->s->conn_timeout_set) {
1381 " <httpd:connectiontimeout>%" APR_TIME_T_FMT "</httpd:connectiontimeout>\n",
1382 apr_time_msec(worker->s->conn_timeout));
1384 if (worker->s->retry_set) {
1386 " <httpd:retry>%" APR_TIME_T_FMT "</httpd:retry>\n",
1387 apr_time_sec(worker->s->retry));
1389 ap_rputs(" </httpd:worker>\n", r);
1392 ap_rputs(" </httpd:workers>\n", r);
1393 ap_rputs(" </httpd:balancer>\n", r);
1396 ap_rputs(" </httpd:balancers>\n", r);
1397 ap_rputs("</httpd:manager>", r);
1400 ap_set_content_type(r, "text/html; charset=ISO-8859-1");
1401 ap_rputs(DOCTYPE_HTML_3_2
1402 "<html><head><title>Balancer Manager</title>\n", r);
1403 ap_rputs("<style type='text/css'>\n"
1405 " border-width: 1px;\n"
1406 " border-spacing: 3px;\n"
1407 " border-style: solid;\n"
1408 " border-color: gray;\n"
1409 " border-collapse: collapse;\n"
1410 " background-color: white;\n"
1411 " text-align: center;\n"
1414 " border-width: 1px;\n"
1416 " border-style: dotted;\n"
1417 " border-color: gray;\n"
1418 " background-color: white;\n"
1419 " text-align: center;\n"
1422 " border-width: 1px;\n"
1424 " border-style: dotted;\n"
1425 " border-color: gray;\n"
1426 " background-color: white;\n"
1427 " text-align: center;\n"
1429 "</style>\n</head>\n", r);
1430 ap_rputs("<body><h1>Load Balancer Manager for ", r);
1431 ap_rvputs(r, ap_get_server_name(r), "</h1>\n\n", NULL);
1432 ap_rvputs(r, "<dl><dt>Server Version: ",
1433 ap_get_server_description(), "</dt>\n", NULL);
1434 ap_rvputs(r, "<dt>Server Built: ",
1435 ap_get_server_built(), "\n</dt></dl>\n", NULL);
1436 balancer = (proxy_balancer *)conf->balancers->elts;
1437 for (i = 0; i < conf->balancers->nelts; i++) {
1439 ap_rputs("<hr />\n<h3>LoadBalancer Status for ", r);
1440 ap_rvputs(r, "<a href='", r->uri, "?b=",
1441 balancer->s->name + sizeof(BALANCER_PREFIX) - 1,
1442 "&nonce=", balancer->s->nonce,
1444 ap_rvputs(r, balancer->s->name, "</a> [",balancer->s->sname, "]</h3>\n", NULL);
1445 ap_rputs("\n\n<table><tr>"
1446 "<th>MaxMembers</th><th>StickySession</th><th>DisableFailover</th><th>Timeout</th><th>FailoverAttempts</th><th>Method</th>"
1447 "<th>Path</th><th>Active</th></tr>\n<tr>", r);
1448 /* the below is a safe cast, since the number of slots total will
1449 * never be more than max_workers, which is restricted to int */
1450 ap_rprintf(r, "<td>%d [%d Used]</td>\n", balancer->max_workers,
1451 balancer->max_workers - (int)storage->num_free_slots(balancer->wslot));
1452 if (*balancer->s->sticky) {
1453 if (strcmp(balancer->s->sticky, balancer->s->sticky_path)) {
1454 ap_rvputs(r, "<td>", balancer->s->sticky, " | ",
1455 balancer->s->sticky_path, NULL);
1458 ap_rvputs(r, "<td>", balancer->s->sticky, NULL);
1462 ap_rputs("<td> (None) ", r);
1464 ap_rprintf(r, "<td>%s</td>\n",
1465 balancer->s->sticky_force ? "On" : "Off");
1466 ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
1467 apr_time_sec(balancer->s->timeout));
1468 ap_rprintf(r, "<td>%d</td>\n", balancer->s->max_attempts);
1469 ap_rprintf(r, "<td>%s</td>\n",
1470 balancer->s->lbpname);
1471 ap_rputs("<td>", r);
1472 if (balancer->s->vhost && *(balancer->s->vhost)) {
1473 ap_rvputs(r, balancer->s->vhost, " -> ", NULL);
1475 ap_rvputs(r, balancer->s->vpath, "</td>\n", NULL);
1476 ap_rprintf(r, "<td>%s</td>\n",
1477 !balancer->s->inactive ? "Yes" : "No");
1478 ap_rputs("</table>\n<br />", r);
1479 ap_rputs("\n\n<table><tr>"
1480 "<th>Worker URL</th>"
1481 "<th>Route</th><th>RouteRedir</th>"
1482 "<th>Factor</th><th>Set</th><th>Status</th>"
1483 "<th>Elected</th><th>Busy</th><th>Load</th><th>To</th><th>From</th>"
1486 workers = (proxy_worker **)balancer->workers->elts;
1487 for (n = 0; n < balancer->workers->nelts; n++) {
1490 ap_rvputs(r, "<tr>\n<td><a href='", r->uri, "?b=",
1491 balancer->s->name + sizeof(BALANCER_PREFIX) - 1, "&w=",
1492 ap_escape_uri(r->pool, worker->s->name),
1493 "&nonce=", balancer->s->nonce,
1495 ap_rvputs(r, worker->s->name, "</a></td>", NULL);
1496 ap_rvputs(r, "<td>", ap_escape_html(r->pool, worker->s->route),
1498 ap_rvputs(r, "</td><td>",
1499 ap_escape_html(r->pool, worker->s->redirect), NULL);
1500 ap_rprintf(r, "</td><td>%d</td>", worker->s->lbfactor);
1501 ap_rprintf(r, "<td>%d</td><td>", worker->s->lbset);
1502 ap_rvputs(r, ap_proxy_parse_wstatus(r->pool, worker), NULL);
1503 ap_rputs("</td>", r);
1504 ap_rprintf(r, "<td>%" APR_SIZE_T_FMT "</td>", worker->s->elected);
1505 ap_rprintf(r, "<td>%" APR_SIZE_T_FMT "</td>", worker->s->busy);
1506 ap_rprintf(r, "<td>%d</td><td>", worker->s->lbstatus);
1507 ap_rputs(apr_strfsize(worker->s->transferred, fbuf), r);
1508 ap_rputs("</td><td>", r);
1509 ap_rputs(apr_strfsize(worker->s->read, fbuf), r);
1510 ap_rputs("</td></tr>\n", r);
1514 ap_rputs("</table>\n", r);
1517 ap_rputs("<hr />\n", r);
1519 ap_rputs("<h3>Edit worker settings for ", r);
1520 ap_rvputs(r, wsel->s->name, "</h3>\n", NULL);
1521 ap_rputs("<form method='POST' enctype='application/x-www-form-urlencoded' action='", r);
1522 ap_rvputs(r, action, "'>\n", NULL);
1523 ap_rputs("<dl>\n<table><tr><td>Load factor:</td><td><input name='w_lf' id='w_lf' type=text ", r);
1524 ap_rprintf(r, "value='%d'></td></tr>\n", wsel->s->lbfactor);
1525 ap_rputs("<tr><td>LB Set:</td><td><input name='w_ls' id='w_ls' type=text ", r);
1526 ap_rprintf(r, "value='%d'></td></tr>\n", wsel->s->lbset);
1527 ap_rputs("<tr><td>Route:</td><td><input name='w_wr' id='w_wr' type=text ", r);
1528 ap_rvputs(r, "value='", ap_escape_html(r->pool, wsel->s->route),
1530 ap_rputs("'></td></tr>\n", r);
1531 ap_rputs("<tr><td>Route Redirect:</td><td><input name='w_rr' id='w_rr' type=text ", r);
1532 ap_rvputs(r, "value='", ap_escape_html(r->pool, wsel->s->redirect),
1534 ap_rputs("'></td></tr>\n", r);
1535 ap_rputs("<tr><td>Status:</td>", r);
1536 ap_rputs("<td><table><tr><th>Ign</th><th>Drn</th><th>Dis</th><th>Stby</th></tr>\n<tr>", r);
1537 create_radio("w_status_I", (PROXY_WORKER_IGNORE_ERRORS & wsel->s->status), r);
1538 create_radio("w_status_N", (PROXY_WORKER_DRAIN & wsel->s->status), r);
1539 create_radio("w_status_D", (PROXY_WORKER_DISABLED & wsel->s->status), r);
1540 create_radio("w_status_H", (PROXY_WORKER_HOT_STANDBY & wsel->s->status), r);
1541 ap_rputs("</tr></table>\n", r);
1542 ap_rputs("<tr><td colspan=2><input type=submit value='Submit'></td></tr>\n", r);
1543 ap_rvputs(r, "</table>\n<input type=hidden name='w' id='w' ", NULL);
1544 ap_rvputs(r, "value='", ap_escape_uri(r->pool, wsel->s->name), "'>\n", NULL);
1545 ap_rvputs(r, "<input type=hidden name='b' id='b' ", NULL);
1546 ap_rvputs(r, "value='", bsel->s->name + sizeof(BALANCER_PREFIX) - 1,
1548 ap_rvputs(r, "<input type=hidden name='nonce' id='nonce' value='",
1549 bsel->s->nonce, "'>\n", NULL);
1550 ap_rvputs(r, "</form>\n", NULL);
1551 ap_rputs("<hr />\n", r);
1553 const apr_array_header_t *provs;
1554 const ap_list_provider_names_t *pname;
1556 ap_rputs("<h3>Edit balancer settings for ", r);
1557 ap_rvputs(r, bsel->s->name, "</h3>\n", NULL);
1558 ap_rputs("<form method='POST' enctype='application/x-www-form-urlencoded' action='", r);
1559 ap_rvputs(r, action, "'>\n", NULL);
1560 ap_rputs("<dl>\n<table>\n", r);
1561 provs = ap_list_provider_names(r->pool, PROXY_LBMETHOD, "0");
1563 ap_rputs("<tr><td>LBmethod:</td>", r);
1564 ap_rputs("<td>\n<select name='b_lbm' id='b_lbm'>", r);
1565 pname = (ap_list_provider_names_t *)provs->elts;
1566 for (i = 0; i < provs->nelts; i++, pname++) {
1567 ap_rvputs(r,"<option value='", pname->provider_name, "'", NULL);
1568 if (strcmp(pname->provider_name, bsel->s->lbpname) == 0)
1569 ap_rputs(" selected ", r);
1570 ap_rvputs(r, ">", pname->provider_name, "\n", NULL);
1572 ap_rputs("</select>\n</td></tr>\n", r);
1574 ap_rputs("<tr><td>Timeout:</td><td><input name='b_tmo' id='b_tmo' type=text ", r);
1575 ap_rprintf(r, "value='%" APR_TIME_T_FMT "'></td></tr>\n", apr_time_sec(bsel->s->timeout));
1576 ap_rputs("<tr><td>Failover Attempts:</td><td><input name='b_max' id='b_max' type=text ", r);
1577 ap_rprintf(r, "value='%d'></td></tr>\n", bsel->s->max_attempts);
1578 ap_rputs("<tr><td>Disable Failover:</td>", r);
1579 create_radio("b_sforce", bsel->s->sticky_force, r);
1580 ap_rputs("<tr><td>Sticky Session:</td><td><input name='b_ss' id='b_ss' size=64 type=text ", r);
1581 if (strcmp(bsel->s->sticky, bsel->s->sticky_path)) {
1582 ap_rvputs(r, "value ='", bsel->s->sticky, " | ",
1583 bsel->s->sticky_path, NULL);
1586 ap_rvputs(r, "value ='", bsel->s->sticky, NULL);
1588 ap_rputs("'> (Use '-' to delete)</td></tr>\n", r);
1589 if (storage->num_free_slots(bsel->wslot) != 0) {
1590 ap_rputs("<tr><td>Add New Worker:</td><td><input name='b_nwrkr' id='b_nwrkr' size=32 type=text>"
1591 " Are you sure? <input name='b_wyes' id='b_wyes' type=checkbox value='1'>"
1594 ap_rputs("<tr><td colspan=2><input type=submit value='Submit'></td></tr>\n", r);
1595 ap_rvputs(r, "</table>\n<input type=hidden name='b' id='b' ", NULL);
1596 ap_rvputs(r, "value='", bsel->s->name + sizeof(BALANCER_PREFIX) - 1,
1598 ap_rvputs(r, "<input type=hidden name='nonce' id='nonce' value='",
1599 bsel->s->nonce, "'>\n", NULL);
1600 ap_rvputs(r, "</form>\n", NULL);
1601 ap_rputs("<hr />\n", r);
1603 ap_rputs(ap_psignature("",r), r);
1604 ap_rputs("</body></html>\n", r);
1610 static void balancer_child_init(apr_pool_t *p, server_rec *s)
1613 proxy_balancer *balancer;
1615 void *sconf = s->module_config;
1616 proxy_server_conf *conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
1619 if (conf->balancers->nelts) {
1622 storage->attach(&(conf->bslot), conf->id, &size, &num, p);
1624 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01205) "slotmem_attach failed");
1625 exit(1); /* Ugly, but what else? */
1629 balancer = (proxy_balancer *)conf->balancers->elts;
1630 for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
1631 rv = ap_proxy_initialize_balancer(balancer, s, p);
1633 if (rv != APR_SUCCESS) {
1634 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, APLOGNO(01206)
1635 "Failed to init balancer %s in child",
1637 exit(1); /* Ugly, but what else? */
1639 init_balancer_members(conf->pool, s, balancer);
1646 static void ap_proxy_balancer_register_hook(apr_pool_t *p)
1648 /* Only the mpm_winnt has child init hook handler.
1649 * make sure that we are called after the mpm
1652 static const char *const aszPred[] = { "mpm_winnt.c", "mod_slotmem_shm.c", NULL};
1653 /* manager handler */
1654 ap_hook_post_config(balancer_post_config, NULL, NULL, APR_HOOK_MIDDLE);
1655 ap_hook_pre_config(balancer_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
1656 ap_hook_handler(balancer_handler, NULL, NULL, APR_HOOK_FIRST);
1657 ap_hook_child_init(balancer_child_init, aszPred, NULL, APR_HOOK_MIDDLE);
1658 proxy_hook_pre_request(proxy_balancer_pre_request, NULL, NULL, APR_HOOK_FIRST);
1659 proxy_hook_post_request(proxy_balancer_post_request, NULL, NULL, APR_HOOK_FIRST);
1660 proxy_hook_canon_handler(proxy_balancer_canon, NULL, NULL, APR_HOOK_FIRST);
1663 AP_DECLARE_MODULE(proxy_balancer) = {
1664 STANDARD20_MODULE_STUFF,
1665 NULL, /* create per-directory config structure */
1666 NULL, /* merge per-directory config structures */
1667 NULL, /* create per-server config structure */
1668 NULL, /* merge per-server config structures */
1669 NULL, /* command apr_table_t */
1670 ap_proxy_balancer_register_hook /* register hooks */