merge from ronnie
[metze/ctdb/wip.git] / server / ctdb_tunables.c
1 /* 
2    ctdb tunables code
3
4    Copyright (C) Andrew Tridgell  2007
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "includes.h"
20 #include "../include/ctdb_private.h"
21
22 static const struct {
23         const char *name;
24         uint32_t default_v;
25         size_t offset;  
26 } tunable_map[] = {
27         { "MaxRedirectCount",     3,  offsetof(struct ctdb_tunable, max_redirect_count) },
28         { "SeqnumFrequency",      1,  offsetof(struct ctdb_tunable, seqnum_frequency) },
29         { "ControlTimeout",      60, offsetof(struct ctdb_tunable, control_timeout) },
30         { "TraverseTimeout",     20, offsetof(struct ctdb_tunable, traverse_timeout) },
31         { "KeepaliveInterval",    2,  offsetof(struct ctdb_tunable, keepalive_interval) },
32         { "KeepaliveLimit",       5,  offsetof(struct ctdb_tunable, keepalive_limit) },
33         { "MaxLACount",           7,  offsetof(struct ctdb_tunable, max_lacount) },
34         { "RecoverTimeout",       5,  offsetof(struct ctdb_tunable, recover_timeout) },
35         { "RecoverInterval",      1,  offsetof(struct ctdb_tunable, recover_interval) },
36         { "ElectionTimeout",      3,  offsetof(struct ctdb_tunable, election_timeout) },
37         { "TakeoverTimeout",      5,  offsetof(struct ctdb_tunable, takeover_timeout) },
38         { "MonitorInterval",     15,  offsetof(struct ctdb_tunable, monitor_interval) },
39         { "MonitorRetry",         5,  offsetof(struct ctdb_tunable, monitor_retry) },
40         { "TickleUpdateInterval",20,  offsetof(struct ctdb_tunable, tickle_update_interval) },
41         { "EventScriptTimeout",  20,  offsetof(struct ctdb_tunable, script_timeout) },
42         { "RecoveryGracePeriod", 60,  offsetof(struct ctdb_tunable, recovery_grace_period) },
43         { "RecoveryBanPeriod",  300,  offsetof(struct ctdb_tunable, recovery_ban_period) },
44         { "DatabaseHashSize", 10000,  offsetof(struct ctdb_tunable, database_hash_size) },
45         { "RerecoveryTimeout",   10,  offsetof(struct ctdb_tunable, rerecovery_timeout) },
46         { "EnableBans",           1,  offsetof(struct ctdb_tunable, enable_bans) },
47         { "DeterministicIPs",     1,  offsetof(struct ctdb_tunable, deterministic_public_ips) },
48 };
49
50 /*
51   set all tunables to defaults
52  */
53 void ctdb_tunables_set_defaults(struct ctdb_context *ctdb)
54 {
55         int i;
56         for (i=0;i<ARRAY_SIZE(tunable_map);i++) {
57                 *(uint32_t *)(tunable_map[i].offset + (uint8_t*)&ctdb->tunable) = tunable_map[i].default_v;
58         }
59 }
60
61
62 /*
63   get a tunable
64  */
65 int32_t ctdb_control_get_tunable(struct ctdb_context *ctdb, TDB_DATA indata, 
66                                  TDB_DATA *outdata)
67 {
68         struct ctdb_control_get_tunable *t = 
69                 (struct ctdb_control_get_tunable *)indata.dptr;
70         char *name;
71         uint32_t val;
72         int i;
73
74         if (indata.dsize < sizeof(*t) ||
75             t->length > indata.dsize - offsetof(struct ctdb_control_get_tunable, name)) {
76                 DEBUG(0,("Bad indata in ctdb_control_get_tunable\n"));
77                 return -1;
78         }
79
80         name = talloc_strndup(ctdb, (char*)t->name, t->length);
81         CTDB_NO_MEMORY(ctdb, name);
82
83         for (i=0;i<ARRAY_SIZE(tunable_map);i++) {
84                 if (strcasecmp(name, tunable_map[i].name) == 0) break;
85         }
86         talloc_free(name);
87         
88         if (i == ARRAY_SIZE(tunable_map)) {
89                 return -1;
90         }
91
92         val = *(uint32_t *)(tunable_map[i].offset + (uint8_t*)&ctdb->tunable);
93
94         outdata->dptr = (uint8_t *)talloc(outdata, uint32_t);
95         CTDB_NO_MEMORY(ctdb, outdata->dptr);
96
97         *(uint32_t *)outdata->dptr = val;
98         outdata->dsize = sizeof(uint32_t);
99
100         return 0;
101 }
102
103
104 /*
105   set a tunable
106  */
107 int32_t ctdb_control_set_tunable(struct ctdb_context *ctdb, TDB_DATA indata)
108 {
109         struct ctdb_control_set_tunable *t = 
110                 (struct ctdb_control_set_tunable *)indata.dptr;
111         char *name;
112         int i;
113
114         if (indata.dsize < sizeof(*t) ||
115             t->length > indata.dsize - offsetof(struct ctdb_control_set_tunable, name)) {
116                 DEBUG(0,("Bad indata in ctdb_control_set_tunable\n"));
117                 return -1;
118         }
119
120         name = talloc_strndup(ctdb, (char *)t->name, t->length);
121         CTDB_NO_MEMORY(ctdb, name);
122
123         for (i=0;i<ARRAY_SIZE(tunable_map);i++) {
124                 if (strcasecmp(name, tunable_map[i].name) == 0) break;
125         }
126
127         talloc_free(name);
128         
129         if (i == ARRAY_SIZE(tunable_map)) {
130                 return -1;
131         }
132         
133         *(uint32_t *)(tunable_map[i].offset + (uint8_t*)&ctdb->tunable) = t->value;
134
135         return 0;
136 }
137
138 /*
139   list tunables
140  */
141 int32_t ctdb_control_list_tunables(struct ctdb_context *ctdb, TDB_DATA *outdata)
142 {
143         char *list = NULL;
144         int i;
145         struct ctdb_control_list_tunable *t;
146
147         list = talloc_strdup(outdata, tunable_map[0].name);
148         CTDB_NO_MEMORY(ctdb, list);
149
150         for (i=1;i<ARRAY_SIZE(tunable_map);i++) {
151                 list = talloc_asprintf_append(list, ":%s", tunable_map[i].name);
152                 CTDB_NO_MEMORY(ctdb, list);             
153         }
154
155         outdata->dsize = offsetof(struct ctdb_control_list_tunable, data) + 
156                 strlen(list) + 1;
157         outdata->dptr = talloc_size(outdata, outdata->dsize);
158         CTDB_NO_MEMORY(ctdb, outdata->dptr);
159
160         t = (struct ctdb_control_list_tunable *)outdata->dptr;
161         t->length = strlen(list)+1;
162
163         memcpy(t->data, list, t->length);
164         talloc_free(list);
165
166         return 0;       
167 }