1 /*
2  * twemproxy - A fast and lightweight proxy for memcached protocol.
3  * Copyright (C) 2011 Twitter, Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef _NC_CONF_H_
19 #define _NC_CONF_H_
20 
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sys/un.h>
24 #include <yaml.h>
25 
26 #include <nc_core.h>
27 #include <hashkit/nc_hashkit.h>
28 
29 #define CONF_OK             (void *) NULL
30 #define CONF_ERROR          (void *) "has an invalid value"
31 
32 #define CONF_ROOT_DEPTH     1
33 #define CONF_MAX_DEPTH      CONF_ROOT_DEPTH + 1
34 
35 #define CONF_DEFAULT_ARGS       3
36 #define CONF_DEFAULT_POOL       8
37 #define CONF_DEFAULT_SERVERS    8
38 
39 #define CONF_UNSET_NUM  -1
40 #define CONF_UNSET_PTR  NULL
41 #define CONF_UNSET_HASH (hash_type_t) -1
42 #define CONF_UNSET_DIST (dist_type_t) -1
43 
44 #define CONF_DEFAULT_HASH                    HASH_FNV1A_64
45 #define CONF_DEFAULT_DIST                    DIST_KETAMA
46 #define CONF_DEFAULT_TIMEOUT                 -1
47 #define CONF_DEFAULT_LISTEN_BACKLOG          512
48 #define CONF_DEFAULT_CLIENT_CONNECTIONS      0
49 #define CONF_DEFAULT_REDIS                   false
50 #define CONF_DEFAULT_REDIS_DB                0
51 #define CONF_DEFAULT_PRECONNECT              false
52 #define CONF_DEFAULT_AUTO_EJECT_HOSTS        false
53 #define CONF_DEFAULT_SERVER_RETRY_TIMEOUT    30 * 1000      /* in msec */
54 #define CONF_DEFAULT_SERVER_FAILURE_LIMIT    2
55 #define CONF_DEFAULT_SERVER_CONNECTIONS      1
56 #define CONF_DEFAULT_KETAMA_PORT             11211
57 #define CONF_DEFAULT_TCPKEEPALIVE            false
58 
59 struct conf_listen {
60     struct string   pname;   /* listen: as "hostname:port" */
61     struct string   name;    /* hostname:port */
62     int             port;    /* port */
63     mode_t          perm;    /* socket permissions */
64     struct sockinfo info;    /* listen socket info */
65     unsigned        valid:1; /* valid? */
66 };
67 
68 struct conf_server {
69     struct string   pname;      /* server: as "hostname:port:weight" */
70     struct string   name;       /* hostname:port or [name] */
71     struct string   addrstr;    /* hostname */
72     int             port;       /* port */
73     int             weight;     /* weight */
74     struct sockinfo info;       /* connect socket info */
75     unsigned        valid:1;    /* valid? */
76 };
77 
78 struct conf_pool {
79     struct string      name;                  /* pool name (root node) */
80     struct conf_listen listen;                /* listen: */
81     hash_type_t        hash;                  /* hash: */
82     struct string      hash_tag;              /* hash_tag: */
83     dist_type_t        distribution;          /* distribution: */
84     int                timeout;               /* timeout: */
85     int                backlog;               /* backlog: */
86     int                client_connections;    /* client_connections: */
87     int                tcpkeepalive;          /* tcpkeepalive: */
88     int                redis;                 /* redis: */
89     struct string      redis_auth;            /* redis_auth: redis auth password (matches requirepass on redis) */
90     int                redis_db;              /* redis_db: redis db */
91     int                preconnect;            /* preconnect: */
92     int                auto_eject_hosts;      /* auto_eject_hosts: */
93     int                server_connections;    /* server_connections: */
94     int                server_retry_timeout;  /* server_retry_timeout: in msec */
95     int                server_failure_limit;  /* server_failure_limit: */
96     struct array       server;                /* servers: conf_server[] */
97     unsigned           valid:1;               /* valid? */
98 };
99 
100 struct conf {
101     const char    *fname;           /* file name (ref in argv[]) */
102     FILE          *fh;              /* file handle */
103     struct array  arg;              /* string[] (parsed {key, value} pairs) */
104     struct array  pool;             /* conf_pool[] (parsed pools) */
105     uint32_t      depth;            /* parsed tree depth */
106     yaml_parser_t parser;           /* yaml parser */
107     yaml_event_t  event;            /* yaml event */
108     yaml_token_t  token;            /* yaml token */
109     unsigned      seq:1;            /* sequence? */
110     unsigned      valid_parser:1;   /* valid parser? */
111     unsigned      valid_event:1;    /* valid event? */
112     unsigned      valid_token:1;    /* valid token? */
113     unsigned      sound:1;          /* sound? */
114     unsigned      parsed:1;         /* parsed? */
115     unsigned      valid:1;          /* valid? */
116 };
117 
118 struct command {
119     struct string name;
120     const char    *(*set)(struct conf *cf, const struct command *cmd, void *data);
121     int           offset;
122 };
123 
124 #define null_command { null_string, NULL, 0 }
125 
126 const char *conf_set_string(struct conf *cf, const struct command *cmd, void *conf);
127 const char *conf_set_listen(struct conf *cf, const struct command *cmd, void *conf);
128 const char *conf_add_server(struct conf *cf, const struct command *cmd, void *conf);
129 const char *conf_set_num(struct conf *cf, const struct command *cmd, void *conf);
130 const char *conf_set_bool(struct conf *cf, const struct command *cmd, void *conf);
131 const char *conf_set_hash(struct conf *cf, const struct command *cmd, void *conf);
132 const char *conf_set_distribution(struct conf *cf, const struct command *cmd, void *conf);
133 const char *conf_set_hashtag(struct conf *cf, const struct command *cmd, void *conf);
134 
135 rstatus_t conf_server_each_transform(void *elem, void *data);
136 rstatus_t conf_pool_each_transform(void *elem, void *data);
137 
138 struct conf *conf_create(const char *filename);
139 void conf_destroy(struct conf *cf);
140 
141 #endif
142