11 #include <config_static.h>
13 #include <memcached/protocol_binary.h>
14 #include <memcached/engine.h>
15 #include <memcached/extension.h>
20 #include "sasl_defs.h"
23 #define KEY_MAX_LENGTH 250
26 #define INCR_MAX_STORAGE_LEN 24
28 #define DATA_BUFFER_SIZE 2048
29 #define UDP_READ_BUFFER_SIZE 65536
30 #define UDP_MAX_PAYLOAD_SIZE 1400
31 #define UDP_HEADER_SIZE 8
32 #define MAX_SENDBUF_SIZE (256 * 1024 * 1024)
35 #define SUFFIX_SIZE 24
38 #define ITEM_LIST_INITIAL 200
41 #define SUFFIX_LIST_INITIAL 20
44 #define IOV_LIST_INITIAL 400
47 #define MSG_LIST_INITIAL 10
50 #define READ_BUFFER_HIGHWAT 8192
51 #define ITEM_LIST_HIGHWAT 400
52 #define IOV_LIST_HIGHWAT 600
53 #define MSG_LIST_HIGHWAT 100
56 #define MIN_BIN_PKT_LENGTH 16
57 #define BIN_PKT_HDR_WORDS (MIN_BIN_PKT_LENGTH/sizeof(uint32_t))
60 #define POWER_SMALLEST 1
61 #define POWER_LARGEST 200
62 #define CHUNK_ALIGN_BYTES 8
63 #define DONT_PREALLOC_SLABS
64 #define MAX_NUMBER_OF_SLAB_CLASSES (POWER_LARGEST + 1)
67 #define STAT_KEY_LEN 128
68 #define STAT_VAL_LEN 128
70 #define DEFAULT_REQS_PER_EVENT 20
71 #define DEFAULT_REQS_PER_TAP_EVENT 50
74 #define APPEND_STAT(name, fmt, val) \
75 append_stat(name, add_stats, c, fmt, val);
79 #define APPEND_NUM_FMT_STAT(name_fmt, num, name, fmt, val) \
80 klen = snprintf(key_str, STAT_KEY_LEN, name_fmt, num, name); \
81 vlen = snprintf(val_str, STAT_VAL_LEN, fmt, val); \
82 add_stats(key_str, klen, val_str, vlen, c);
85 #define APPEND_NUM_STAT(num, name, fmt, val) \
86 APPEND_NUM_FMT_STAT("%d:%s", num, name, fmt, val)
90 bin_reading_set_header,
91 bin_reading_cas_header,
95 bin_reading_del_header,
96 bin_reading_incr_header,
97 bin_read_flush_exptime,
98 bin_reading_sasl_auth,
99 bin_reading_sasl_auth_data,
109 enum network_transport {
115 #define IS_UDP(x) (x == udp_transport)
121 uint64_t delete_hits;
130 pthread_mutex_t mutex;
133 uint64_t delete_misses;
134 uint64_t incr_misses;
135 uint64_t decr_misses;
140 uint64_t bytes_written;
142 uint64_t conn_yields;
144 uint64_t auth_errors;
145 struct slab_stats slab_stats[MAX_NUMBER_OF_SLAB_CLASSES];
161 pthread_mutex_t mutex;
162 unsigned int daemon_conns;
163 unsigned int curr_conns;
164 unsigned int total_conns;
165 unsigned int conn_structs;
167 uint64_t rejected_conns;
170 #define MAX_VERBOSITY_LEVEL 2
183 rel_time_t oldest_live;
190 int num_threads_per_udp;
191 char prefix_delimiter;
196 int reqs_per_tap_event;
199 enum protocol binding_protocol;
201 size_t item_size_max;
234 struct event notify_event;
238 pthread_mutex_t mutex;
240 struct conn *pending_io;
242 enum thread_type
type;
244 rel_time_t last_checked;
245 struct conn *pending_close;
248 #define LOCK_THREAD(t) \
249 if (pthread_mutex_lock(&t->mutex) != 0) { \
252 assert(t->is_locked == false); \
255 #define UNLOCK_THREAD(t) \
256 assert(t->is_locked == true); \
257 t->is_locked = false; \
258 if (pthread_mutex_unlock(&t->mutex) != 0) { \
263 extern void notify_dispatcher(
void);
269 typedef bool (*STATE_FUNC)(
conn *);
279 enum bin_substates substate;
281 bool registered_in_libevent;
298 void *write_and_free;
312 ENGINE_STORE_OPERATION store_op;
339 enum protocol protocol;
340 enum network_transport transport;
344 struct sockaddr_storage request_addr;
345 socklen_t request_addr_size;
346 unsigned char *hdrbuf;
360 void *engine_storage;
378 ENGINE_ERROR_CODE aiostat;
385 #define LIST_STATE_PROCESSING 1
386 #define LIST_STATE_REQ_PENDING_IO 2
387 #define LIST_STATE_REQ_PENDING_CLOSE 4
392 conn *conn_new(
const SOCKET sfd, STATE_FUNC init_state,
const int event_flags,
393 const int read_buffer_size,
enum network_transport transport,
396 extern int daemonize(
int nochdir,
int noclose);
402 #include <memcached/util.h>
407 bool register_event(
conn *c,
struct timeval *timeout);
408 bool unregister_event(
conn *c);
409 bool update_event(
conn *c,
const int new_flags);
418 void thread_init(
int nthreads,
struct event_base *main_base,
419 void (*dispatcher_callback)(
int,
short,
void *));
420 void threads_shutdown(
void);
422 int dispatch_event_add(
int thread,
conn *c);
423 void dispatch_conn_new(SOCKET sfd, STATE_FUNC init_state,
int event_flags,
424 int read_buffer_size,
enum network_transport transport);
427 void accept_new_conns(
const bool do_accept);
428 conn *conn_from_freelist(
void);
429 bool conn_add_to_freelist(
conn *c);
430 int is_listen_thread(
void);
432 void STATS_LOCK(
void);
433 void STATS_UNLOCK(
void);
441 const char *
fmt, ...);
443 void notify_io_complete(
const void *cookie, ENGINE_ERROR_CODE status);
444 void conn_set_state(
conn *c, STATE_FUNC state);
445 const char *state_text(STATE_FUNC state);
446 void safe_close(SOCKET sfd);
450 int number_of_pending(
conn *c,
conn *pending);
451 bool has_cycle(
conn *c);
454 size_t list_to_array(
conn **dest,
size_t max_items,
conn **l);
455 void enlist_conn(
conn *c,
conn **list);
456 void finalize_list(
conn **list,
size_t items);
457 bool set_socket_nonblocking(SOCKET sfd);
459 void conn_close(
conn *c);
462 #if HAVE_DROP_PRIVILEGES
463 extern void drop_privileges(
void);
465 #define drop_privileges()
469 bool conn_listening(
conn *c);
470 bool conn_new_cmd(
conn *c);
471 bool conn_waiting(
conn *c);
472 bool conn_read(
conn *c);
473 bool conn_parse_cmd(
conn *c);
474 bool conn_write(
conn *c);
475 bool conn_nread(
conn *c);
476 bool conn_swallow(
conn *c);
477 bool conn_pending_close(
conn *c);
478 bool conn_immediate_close(
conn *c);
479 bool conn_closing(
conn *c);
480 bool conn_mwrite(
conn *c);
481 bool conn_ship_log(
conn *c);
482 bool conn_add_tap_client(
conn *c);
483 bool conn_setup_tap_stream(
conn *c);
486 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
487 #define __builtin_expect(x, expected_value) (x)
490 #define likely(x) __builtin_expect((x),1)
491 #define unlikely(x) __builtin_expect((x),0)