1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 #ifndef tsmemcache_h
25 #define tsmemcache_h
26 
27 #include "I_EventSystem.h"
28 #include "I_Net.h"
29 #include "I_Cache.h"
30 #include "tscore/I_Version.h"
31 
32 #include "ts/ts.h" // plugin header
33 #include "protocol_binary.h"
34 #include "tscore/ink_memory.h"
35 #include "tscore/ink_hrtime.h"
36 #include "tscore/CryptoHash.h"
37 
38 #define TSMEMCACHE_VERSION "1.0.0"
39 #define TSMEMCACHE_MAX_CMD_SIZE (128 * 1024 * 1024) // silly large
40 #define TSMEMCACHE_MAX_KEY_LEN 250
41 #define TSMEMCACHE_TMP_CMD_BUFFER_SIZE 320
42 #define TSMEMCACHE_HEADER_MAGIC 0x8765ACDC
43 #define TSMEMCACHE_RETRY_WRITE_INTERVAL HRTIME_MSECONDS(20)
44 
45 #define TSMEMCACHE_WRITE_SYNC 0 // not yet
46 
47 #define TSMEMCACHE_EVENT_GOT_ITEM 100000
48 #define TSMEMCACHE_EVENT_GOT_KEY 100001
49 #define TSMEMCACHE_STREAM_DONE 100002
50 #define TSMEMCACHE_TUNNEL_DONE 100003
51 
52 #define CHECK_RET(_e, _r) \
53   do {                    \
54     int ret = (_e);       \
55     if (ret != _r)        \
56       return _r;          \
57   } while (0)
58 #define WRITE(_s) write(_s "", sizeof(_s "") - 1)
59 #define STRLEN(_s) (sizeof(_s "") - 1)
60 
61 struct MCCacheHeader {
62   uint32_t magic;
63   uint32_t flags;
64   uint32_t nkey : 8;
65   uint32_t reserved : 24;
66   uint32_t exptime; // seconds offset from settime
67   uint64_t settime;
68   uint64_t cas;
69   uint64_t nbytes;
70   char *
keyMCCacheHeader71   key()
72   {
73     return ((char *)this) + sizeof(MCCacheHeader);
74   }
75   int
lenMCCacheHeader76   len()
77   {
78     return sizeof(MCCacheHeader) + nkey;
79   }
80 };
81 
82 struct MCAccept : public Continuation {
83 #ifndef HAVE_TLS
84   ProxyAllocator *theMCThreadAllocator;
85 #endif
86   int accept_port = 0;
87   int main_event(int event, void *netvc);
88 
MCAcceptMCAccept89   MCAccept()
90     :
91 #ifndef HAVE_TLS
92       theMCThreadAllocator(NULL)
93 #endif
94 
95   {
96     SET_HANDLER(&MCAccept::main_event);
97   }
98 };
99 
100 #define TS_PUSH_HANDLER(_h)                    \
101   do {                                         \
102     handler_stack[ihandler_stack++] = handler; \
103     SET_HANDLER(_h);                           \
104   } while (0)
105 
106 #define TS_POP_HANDLER handler = handler_stack[--ihandler_stack]
107 #define TS_POP_CALL(_event, _data) handleEvent((TS_POP_HANDLER, _event), _data)
108 #define TS_SET_CALL(_h, _event, _data) handleEvent((SET_HANDLER(_h), _event), _data)
109 #define ASCII_RESPONSE(_s) ascii_response((_s "\r\n"), sizeof(_s "\r\n") - 1)
110 #define ASCII_ERROR() ascii_response(("ERROR\r\n"), sizeof("ERROR\r\n") - 1)
111 #define ASCII_CLIENT_ERROR(_s) ascii_response(("CLIENT_ERROR: " _s "\r\n"), sizeof("CLIENT_ERROR: " _s "\r\n") - 1)
112 #define ASCII_SERVER_ERROR(_s) ascii_response(("SERVER_ERROR: " _s "\r\n"), sizeof("SERVER_ERROR: " _s "\r\n") - 1)
113 #define STRCMP(_s, _const_string) strncmp(_s, _const_string "", sizeof(_const_string) - 1)
114 
115 struct MC : Continuation {
116   Action *pending_action;
117   int ihandler_stack;
118   int swallow_bytes;
119   int64_t exptime;
120   ContinuationHandler handler_stack[2];
121   VConnection *nvc;
122   MIOBuffer *rbuf, *wbuf, *cbuf;
123   VIO *rvio, *wvio;
124   IOBufferReader *reader, *writer, *creader;
125   CacheVConnection *crvc, *cwvc;
126   VIO *crvio, *cwvio;
127   CacheKey cache_key;
128   MCCacheHeader *rcache_header, *wcache_header;
129   MCCacheHeader header;
130   char tmp_cache_header_key[256];
131   protocol_binary_request_header binary_header;
132   union {
133     protocol_binary_response_get get;
134   } res;
135   char *key, *tbuf;
136   int read_offset;
137   int end_of_cmd; // -1 means that it is already consumed
138   int ngets;
139   char tmp_cmd_buffer[TSMEMCACHE_TMP_CMD_BUFFER_SIZE];
140   union {
141     struct {
142       unsigned int noreply : 1;
143       unsigned int return_cas : 1;
144       unsigned int set_add : 1;
145       unsigned int set_cas : 1;
146       unsigned int set_append : 1;
147       unsigned int set_prepend : 1;
148       unsigned int set_replace : 1;
149       unsigned int set_incr : 1;
150       unsigned int set_decr : 1;
151     } f;
152     unsigned int ff;
153   };
154   uint64_t nbytes;
155   uint64_t delta;
156 
157   static int32_t verbosity;
158   static ink_hrtime last_flush;
159   static int64_t next_cas;
160 
161   int write_to_client(int64_t ntowrite = -1);
162   int write_then_read_from_client(int64_t ntowrite = -1);
163   int stream_then_read_from_client(int64_t ntowrite);
164   int write_then_close(int64_t ntowrite = -1);
165   int read_from_client();
166   int get_item();
167   int set_item();
168   int delete_item();
169   int read_from_client_event(int event, void *data);
170   int swallow_then_read_event(int event, void *data);
171   int swallow_cmd_then_read_from_client_event(int event, void *data);
172   int read_binary_from_client_event(int event, void *data);
173   int read_ascii_from_client_event(int event, void *data);
174   int binary_get_event(int event, void *data);
175   int cache_read_event(int event, void *data);
176   int write_then_close_event(int event, void *data);
177   int stream_event(int event, void *data); // cache <=> client
178   int tunnel_event(int event, void *data); // cache <=> cache
179 
180   char *get_ascii_input(int n, int *end);
181   int get_ascii_key(char *s, char *e);
182   int ascii_response(const char *s, int len);
183   int ascii_get(char *s, char *e);
184   int ascii_gets();
185   int ascii_set(char *s, char *e);
186   int ascii_delete(char *s, char *e);
187   int ascii_incr_decr(char *s, char *e);
188   int ascii_get_event(int event, void *data);
189   int ascii_set_event(int event, void *data);
190   int ascii_delete_event(int event, void *data);
191   int ascii_incr_decr_event(int event, void *data);
192 
193   int write_binary_error(protocol_binary_response_status err, int swallow);
194   void add_binary_header(uint16_t err, uint8_t hdr_len, uint16_t key_len, uint32_t body_len);
195   int write_binary_response(const void *d, int hlen, int keylen, int dlen);
196   int protocol_error();
197   int bin_read_key();
198 
199   void new_connection(NetVConnection *netvc, EThread *thread);
200   int unexpected_event();
201   int die();
202 };
203 
204 int init_tsmemcache(int port = 11211);
205 
206 // INLINE FUNCTIONS
207 
208 static inline char *
xutoa(uint32_t i,char * e)209 xutoa(uint32_t i, char *e)
210 {
211   do {
212     *--e = (char)(i % 10 + 48);
213   } while ((i /= 10) > 0);
214   return e;
215 }
216 
217 static inline char *
xutoa(uint64_t i,char * e)218 xutoa(uint64_t i, char *e)
219 {
220   do {
221     *--e = (char)(i % 10 + 48);
222   } while ((i /= 10) > 0);
223   return e;
224 }
225 
226 static inline uint64_t
xatoull(char * s,char * e)227 xatoull(char *s, char *e)
228 {
229   uint64_t n = 0;
230   if (isdigit(*s)) {
231     n = *s - '0';
232     s++;
233     if (s >= e) {
234       return n;
235     }
236   }
237   while (isdigit(*s)) {
238     n *= 10;
239     n += *s - '0';
240     s++;
241   }
242   return n;
243 }
244 
245 #endif
246