xref: /3.0.2-MP2/memcached/daemon/memcached.h (revision 28e825f7)
1/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2#ifndef MEMCACHED_H
3#define MEMCACHED_H
4
5/** \file
6 * The main memcached header holding commonly used data
7 * structures and function prototypes.
8 */
9#include <event.h>
10#include <platform/platform.h>
11#include <cbsasl/cbsasl.h>
12
13#include <memcached/openssl.h>
14
15#include <memcached/protocol_binary.h>
16#include <memcached/engine.h>
17#include <memcached/extension.h>
18
19#include "cache.h"
20
21/** Maximum length of a key. */
22#define KEY_MAX_LENGTH 250
23
24/** Size of an incr buf. */
25#define INCR_MAX_STORAGE_LEN 24
26
27#define DATA_BUFFER_SIZE 2048
28#define UDP_MAX_PAYLOAD_SIZE 1400
29#define MAX_SENDBUF_SIZE (256 * 1024 * 1024)
30
31/** Initial size of list of items being returned by "get". */
32#define ITEM_LIST_INITIAL 200
33
34/** Initial size of list of temprary auto allocates  */
35#define TEMP_ALLOC_LIST_INITIAL 20
36
37/** Initial size of the sendmsg() scatter/gather array. */
38#define IOV_LIST_INITIAL 400
39
40/** Initial number of sendmsg() argument structures to allocate. */
41#define MSG_LIST_INITIAL 10
42
43/** High water marks for buffer shrinking */
44#define READ_BUFFER_HIGHWAT 8192
45#define IOV_LIST_HIGHWAT 600
46#define MSG_LIST_HIGHWAT 100
47
48/* Slab sizing definitions. */
49#define POWER_SMALLEST 1
50#define POWER_LARGEST  200
51#define CHUNK_ALIGN_BYTES 8
52#define DONT_PREALLOC_SLABS
53#define MAX_NUMBER_OF_SLAB_CLASSES (POWER_LARGEST + 1)
54
55
56#define STAT_KEY_LEN 128
57#define STAT_VAL_LEN 128
58
59#define DEFAULT_REQS_PER_EVENT     20
60#define DEFAULT_REQS_PER_TAP_EVENT 50
61
62/** Append a simple stat with a stat name, value format and value */
63#define APPEND_STAT(name, fmt, val) \
64    append_stat(name, add_stats, c, fmt, val);
65
66/** Append an indexed stat with a stat name (with format), value format
67    and value */
68#define APPEND_NUM_FMT_STAT(name_fmt, num, name, fmt, val)          \
69    klen = snprintf(key_str, STAT_KEY_LEN, name_fmt, num, name);    \
70    vlen = snprintf(val_str, STAT_VAL_LEN, fmt, val);               \
71    add_stats(key_str, klen, val_str, vlen, c);
72
73/** Common APPEND_NUM_FMT_STAT format. */
74#define APPEND_NUM_STAT(num, name, fmt, val) \
75    APPEND_NUM_FMT_STAT("%d:%s", num, name, fmt, val)
76
77enum bin_substates {
78    bin_no_state,
79    bin_reading_set_header,
80    bin_reading_cas_header,
81    bin_read_set_value,
82    bin_reading_sasl_auth,
83    bin_reading_sasl_auth_data,
84    bin_reading_packet
85};
86
87/** Stats stored per slab (and per thread). */
88struct slab_stats {
89    uint64_t  cmd_set;
90    uint64_t  get_hits;
91    uint64_t  delete_hits;
92    uint64_t  cas_hits;
93    uint64_t  cas_badval;
94};
95
96/**
97 * Stats stored per-thread.
98 */
99struct thread_stats {
100    cb_mutex_t   mutex;
101    uint64_t          cmd_get;
102    uint64_t          get_misses;
103    uint64_t          delete_misses;
104    uint64_t          incr_misses;
105    uint64_t          decr_misses;
106    uint64_t          incr_hits;
107    uint64_t          decr_hits;
108    uint64_t          cas_misses;
109    uint64_t          bytes_read;
110    uint64_t          bytes_written;
111    uint64_t          cmd_flush;
112    uint64_t          conn_yields; /* # of yields for connections (-R option)*/
113    uint64_t          auth_cmds;
114    uint64_t          auth_errors;
115    struct slab_stats slab_stats[MAX_NUMBER_OF_SLAB_CLASSES];
116};
117
118/**
119 * Listening port.
120 */
121struct listening_port {
122    int port;
123    int curr_conns;
124    int maxconns;
125};
126
127/**
128 * Global stats.
129 */
130struct stats {
131    cb_mutex_t mutex;
132    unsigned int  daemon_conns; /* conns used by the server */
133    unsigned int  curr_conns;
134    unsigned int  total_conns;
135    unsigned int  conn_structs;
136    time_t        started;          /* when the process was started */
137    uint64_t      rejected_conns; /* number of times I reject a client */
138    struct listening_port *listening_ports;
139};
140
141#define MAX_VERBOSITY_LEVEL 2
142
143struct interface {
144    char *host;
145    struct {
146        const char *key;
147        const char *cert;
148    } ssl;
149    int maxconn;
150    int backlog;
151    in_port_t port;
152    bool ipv6;
153    bool ipv4;
154    bool tcp_nodelay;
155};
156
157/* When adding a setting, be sure to update process_stat_settings */
158/**
159 * Globally accessible settings as derived from the commandline.
160 */
161struct settings {
162    int maxconns;
163    struct interface *interfaces;
164    int num_interfaces;
165    int verbose;
166    int num_threads;        /* number of worker (without dispatcher) libevent threads to run */
167    char prefix_delimiter;  /* character that marks a key prefix (for stats) */
168    bool detail_enabled;    /* nonzero if we're collecting detailed stats */
169    bool allow_detailed;    /* detailed stats commands are allowed */
170
171    /* Maximum number of io events to process based on the priority of the
172       connection */
173    int reqs_per_event_high_priority;
174    int reqs_per_event_med_priority;
175    int reqs_per_event_low_priority;
176    int default_reqs_per_event;
177
178    bool sasl;              /* SASL on/off */
179    bool require_sasl;      /* require SASL auth */
180    int topkeys;            /* Number of top keys to track */
181    union {
182        ENGINE_HANDLE *v0;
183        ENGINE_HANDLE_V1 *v1;
184    } engine;
185    struct {
186        EXTENSION_DAEMON_DESCRIPTOR *daemons;
187        EXTENSION_LOGGER_DESCRIPTOR *logger;
188        EXTENSION_BINARY_PROTOCOL_DESCRIPTOR *binary;
189    } extensions;
190    size_t bio_drain_buffer_sz;
191    bool tcp_nodelay;
192    char *engine_module;
193    char *engine_config;
194    char *pid_file;
195    bool daemonize;
196    char *config;      /* The configuration specified by -C (json) */
197    char *admin;
198    bool disable_admin;
199    bool datatype;
200};
201
202struct engine_event_handler {
203    EVENT_CALLBACK cb;
204    const void *cb_data;
205    struct engine_event_handler *next;
206};
207
208extern struct stats stats;
209extern struct settings settings;
210
211enum thread_type {
212    GENERAL = 11,
213    TAP = 13,
214    DISPATCHER = 15
215};
216
217typedef struct {
218    cb_thread_t thread_id;      /* unique ID of this thread */
219    struct event_base *base;    /* libevent handle this thread uses */
220    struct event notify_event;  /* listen event for notify pipe */
221    SOCKET notify[2];           /* notification pipes */
222    struct conn_queue *new_conn_queue; /* queue of new connections to handle */
223    cb_mutex_t mutex;      /* Mutex to lock protect access to the pending_io */
224    bool is_locked;
225    struct conn *pending_io;    /* List of connection with pending async io ops */
226    int index;                  /* index of this thread in the threads array */
227    enum thread_type type;      /* Type of IO this thread processes */
228
229    rel_time_t last_checked;
230} LIBEVENT_THREAD;
231
232#define LOCK_THREAD(t)                          \
233    cb_mutex_enter(&t->mutex);                  \
234    cb_assert(t->is_locked == false);              \
235    t->is_locked = true;
236
237#define UNLOCK_THREAD(t)                         \
238    cb_assert(t->is_locked == true);                \
239    t->is_locked = false;                        \
240    cb_mutex_exit(&t->mutex);
241
242extern void notify_thread(LIBEVENT_THREAD *thread);
243extern void notify_dispatcher(void);
244extern bool create_notification_pipe(LIBEVENT_THREAD *me);
245
246typedef struct conn conn;
247typedef bool (*STATE_FUNC)(conn *);
248
249/**
250 * The structure representing a connection into memcached.
251 */
252struct conn {
253    SOCKET sfd;
254    int max_reqs_per_event; /** The maximum requests we can process in a worker
255                                thread timeslice */
256    int nevents;
257    bool admin;
258    cbsasl_conn_t *sasl_conn;
259    STATE_FUNC   state;
260    enum bin_substates substate;
261    bool   registered_in_libevent;
262    struct event event;
263    short  ev_flags;
264    short  which;   /** which events were just triggered */
265    char   *rbuf;   /** buffer to read commands into */
266    char   *rcurr;  /** but if we parsed some already, this is where we stopped */
267    uint32_t rsize;   /** total allocated size of rbuf */
268    uint32_t rbytes;  /** how much data, starting from rcur, do we have unparsed */
269
270    char   *wbuf;
271    char   *wcurr;
272    uint32_t wsize;
273    uint32_t wbytes;
274    /** which state to go into after finishing current write */
275    STATE_FUNC   write_and_go;
276    void   *write_and_free; /** free this memory after finishing writing */
277
278    char   *ritem;  /** when we read in an item's value, it goes here */
279    uint32_t rlbytes;
280
281    /* data for the nread state */
282
283    /**
284     * item is used to hold an item structure created after reading the command
285     * line of set/add/replace commands, but before we finished reading the actual
286     * data. The data is read into ITEM_data(item) to avoid extra copying.
287     */
288
289    void   *item;     /* for commands set/add/replace  */
290    ENGINE_STORE_OPERATION    store_op; /* which one is it: set/add/replace */
291
292
293    /* data for the swallow state */
294    uint32_t sbytes;    /* how many bytes to swallow */
295
296    /* data for the mwrite state */
297    struct iovec *iov;
298    int    iovsize;   /* number of elements allocated in iov[] */
299    int    iovused;   /* number of elements used in iov[] */
300
301    struct msghdr *msglist;
302    int    msgsize;   /* number of elements allocated in msglist[] */
303    int    msgused;   /* number of elements used in msglist[] */
304    int    msgcurr;   /* element in msglist[] being transmitted now */
305    int    msgbytes;  /* number of bytes in current msg */
306
307    item   **ilist;   /* list of items to write out */
308    int    isize;
309    item   **icurr;
310    int    ileft;
311
312    char   **temp_alloc_list;
313    int    temp_alloc_size;
314    char   **temp_alloc_curr;
315    int    temp_alloc_left;
316
317    struct sockaddr_storage request_addr; /* Who sent the most recent request */
318    socklen_t request_addr_size;
319    int    hdrsize;   /* number of headers' worth of space is allocated */
320
321    bool   noreply;   /* True if the reply should not be sent. */
322    /* current stats command */
323
324    uint8_t refcount; /* number of references to the object */
325    bool   supports_datatype;
326
327    struct {
328        char *buffer;
329        size_t size;
330        size_t offset;
331    } dynamic_buffer;
332
333    void *engine_storage;
334    hrtime_t start;
335
336    /* Binary protocol stuff */
337    /* This is where the binary header goes */
338    protocol_binary_request_header binary_header;
339    uint64_t cas; /* the cas to return */
340    short cmd; /* current command being processed */
341    int opaque;
342    int keylen;
343
344    int list_state; /* bitmask of list state data for this connection */
345    conn   *next;     /* Used for generating a list of conn structures */
346    LIBEVENT_THREAD *thread; /* Pointer to the thread object serving this connection */
347
348    ENGINE_ERROR_CODE aiostat;
349    bool ewouldblock;
350    TAP_ITERATOR tap_iterator;
351    in_port_t parent_port; /* Listening port that creates this connection instance */
352
353    int dcp;
354
355    /* Clean up this at one point.. */
356    struct {
357        struct {
358            char *buffer;
359            int buffsz;
360            int total;
361            int current;
362        } in, out;
363
364        bool enabled;
365        bool error;
366        SSL_CTX *ctx;
367        SSL *client;
368
369        bool connected;
370        BIO *application;
371        BIO *network;
372    } ssl;
373};
374
375/* States for the connection list_state */
376#define LIST_STATE_PROCESSING 1
377#define LIST_STATE_REQ_PENDING_IO 2
378#define LIST_STATE_REQ_PENDING_CLOSE 4
379
380/*
381 * Functions
382 */
383conn *conn_new(const SOCKET sfd, in_port_t parent_port,
384               STATE_FUNC init_state, int event_flags,
385               unsigned int read_buffer_size, struct event_base *base,
386               struct timeval *timeout);
387#ifndef WIN32
388extern int daemonize(int nochdir, int noclose);
389#endif
390
391#include "stats.h"
392#include "trace.h"
393#include "hash.h"
394#include <memcached/util.h>
395
396/*
397 * Functions to add / update the connection to libevent
398 */
399bool register_event(conn *c, struct timeval *timeout);
400bool unregister_event(conn *c);
401bool update_event(conn *c, const int new_flags);
402
403/*
404 * Functions such as the libevent-related calls that need to do cross-thread
405 * communication in multithreaded mode (rather than actually doing the work
406 * in the current thread) are called via "dispatch_" frontends, which are
407 * also #define-d to directly call the underlying code in singlethreaded mode.
408 */
409
410void thread_init(int nthreads, struct event_base *main_base,
411                 void (*dispatcher_callback)(evutil_socket_t, short, void *));
412void threads_shutdown(void);
413void threads_cleanup(void);
414
415int  dispatch_event_add(int thread, conn *c);
416void dispatch_conn_new(SOCKET sfd, int parent_port,
417                       STATE_FUNC init_state, int event_flags,
418                       int read_buffer_size);
419
420/* Lock wrappers for cache functions that are called from main loop. */
421void accept_new_conns(const bool do_accept);
422conn *conn_from_freelist(void);
423bool  conn_add_to_freelist(conn *c);
424int   is_listen_thread(void);
425
426void STATS_LOCK(void);
427void STATS_UNLOCK(void);
428void threadlocal_stats_clear(struct thread_stats *stats);
429void threadlocal_stats_reset(struct thread_stats *thread_stats);
430void threadlocal_stats_aggregate(struct thread_stats *thread_stats, struct thread_stats *stats);
431void slab_stats_aggregate(struct thread_stats *stats, struct slab_stats *out);
432
433/* Stat processing functions */
434void append_stat(const char *name, ADD_STAT add_stats, conn *c,
435                 const char *fmt, ...);
436
437void notify_io_complete(const void *cookie, ENGINE_ERROR_CODE status);
438void conn_set_state(conn *c, STATE_FUNC state);
439const char *state_text(STATE_FUNC state);
440void safe_close(SOCKET sfd);
441
442
443/* Number of times this connection is in the given pending list */
444int number_of_pending(conn *c, conn *pending);
445bool has_cycle(conn *c);
446bool list_contains(conn *h, conn *n);
447conn *list_remove(conn *h, conn *n);
448size_t list_to_array(conn **dest, size_t max_items, conn **l);
449void enlist_conn(conn *c, conn **list);
450void finalize_list(conn **list, size_t items);
451bool set_socket_nonblocking(SOCKET sfd);
452
453void conn_close(conn *c);
454bool load_extension(const char *soname, const char *config);
455
456int add_conn_to_pending_io_list(conn *c);
457
458extern void drop_privileges(void);
459
460/* connection state machine */
461bool conn_listening(conn *c);
462bool conn_new_cmd(conn *c);
463bool conn_waiting(conn *c);
464bool conn_read(conn *c);
465bool conn_parse_cmd(conn *c);
466bool conn_write(conn *c);
467bool conn_nread(conn *c);
468bool conn_swallow(conn *c);
469bool conn_pending_close(conn *c);
470bool conn_immediate_close(conn *c);
471bool conn_closing(conn *c);
472bool conn_mwrite(conn *c);
473bool conn_ship_log(conn *c);
474bool conn_setup_tap_stream(conn *c);
475bool conn_refresh_cbsasl(conn *c);
476bool conn_refresh_ssl_certs(conn *c);
477
478void log_socket_error(EXTENSION_LOG_LEVEL severity,
479                      const void* client_cookie,
480                      const char* prefix);
481#ifdef WIN32
482void log_errcode_error(EXTENSION_LOG_LEVEL severity,
483                          const void* client_cookie,
484                          const char* prefix, DWORD err);
485#else
486void log_errcode_error(EXTENSION_LOG_LEVEL severity,
487                          const void* client_cookie,
488                          const char* prefix, int err);
489#endif
490void log_system_error(EXTENSION_LOG_LEVEL severity,
491                      const void* cookie,
492                      const char* prefix);
493
494
495void read_config_file(const char *filename);
496void perform_callbacks(ENGINE_EVENT_TYPE type,
497                       const void *data,
498                       const void *c);
499
500const char* get_server_version(void);
501
502#endif
503