Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1 | /* |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2 | * Peer synchro management. |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <errno.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | |
| 19 | #include <sys/socket.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include <common/compat.h> |
| 24 | #include <common/config.h> |
| 25 | #include <common/time.h> |
| 26 | |
| 27 | #include <types/global.h> |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 28 | #include <types/listener.h> |
| 29 | #include <types/obj_type.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 30 | #include <types/peers.h> |
| 31 | |
| 32 | #include <proto/acl.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 33 | #include <proto/applet.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 34 | #include <proto/channel.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 35 | #include <proto/fd.h> |
Willy Tarreau | d1d48d4 | 2015-03-13 16:15:46 +0100 | [diff] [blame] | 36 | #include <proto/frontend.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 37 | #include <proto/log.h> |
| 38 | #include <proto/hdr_idx.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 39 | #include <proto/proto_tcp.h> |
| 40 | #include <proto/proto_http.h> |
| 41 | #include <proto/proxy.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 42 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 43 | #include <proto/stream.h> |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 44 | #include <proto/signal.h> |
| 45 | #include <proto/stick_table.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 46 | #include <proto/stream_interface.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 47 | #include <proto/task.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 48 | |
| 49 | |
| 50 | /*******************************/ |
| 51 | /* Current peer learning state */ |
| 52 | /*******************************/ |
| 53 | |
| 54 | /******************************/ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 55 | /* Current peers section resync state */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 56 | /******************************/ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 57 | #define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */ |
| 58 | #define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */ |
| 59 | #define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */ |
| 60 | #define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */ |
| 61 | #define PEERS_F_DONOTSTOP 0x00010000 /* Main table sync task block process during soft stop |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 62 | to push data to new process */ |
| 63 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 64 | #define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE) |
| 65 | #define PEERS_RESYNC_FROMLOCAL 0x00000000 |
| 66 | #define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL |
| 67 | #define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE) |
| 68 | |
| 69 | /***********************************/ |
| 70 | /* Current shared table sync state */ |
| 71 | /***********************************/ |
| 72 | #define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */ |
| 73 | #define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 74 | |
| 75 | /******************************/ |
| 76 | /* Remote peer teaching state */ |
| 77 | /******************************/ |
| 78 | #define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 79 | #define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */ |
| 80 | #define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */ |
| 81 | #define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */ |
| 82 | #define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */ |
| 83 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 84 | #define PEER_TEACH_RESET ~(PEER_F_TEACH_PROCESS|PEER_F_TEACH_FINISHED) /* PEER_F_TEACH_COMPLETE should never be reset */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 85 | #define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE) |
| 86 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 87 | /*****************************/ |
| 88 | /* Sync message class */ |
| 89 | /*****************************/ |
| 90 | enum { |
| 91 | PEER_MSG_CLASS_CONTROL = 0, |
| 92 | PEER_MSG_CLASS_ERROR, |
| 93 | PEER_MSG_CLASS_STICKTABLE = 10, |
| 94 | PEER_MSG_CLASS_RESERVED = 255, |
| 95 | }; |
| 96 | |
| 97 | /*****************************/ |
| 98 | /* control message types */ |
| 99 | /*****************************/ |
| 100 | enum { |
| 101 | PEER_MSG_CTRL_RESYNCREQ = 0, |
| 102 | PEER_MSG_CTRL_RESYNCFINISHED, |
| 103 | PEER_MSG_CTRL_RESYNCPARTIAL, |
| 104 | PEER_MSG_CTRL_RESYNCCONFIRM, |
| 105 | }; |
| 106 | |
| 107 | /*****************************/ |
| 108 | /* error message types */ |
| 109 | /*****************************/ |
| 110 | enum { |
| 111 | PEER_MSG_ERR_PROTOCOL = 0, |
| 112 | PEER_MSG_ERR_SIZELIMIT, |
| 113 | }; |
| 114 | |
| 115 | |
| 116 | /*******************************/ |
| 117 | /* stick table sync mesg types */ |
| 118 | /* Note: ids >= 128 contains */ |
| 119 | /* id message cotains data */ |
| 120 | /*******************************/ |
| 121 | enum { |
| 122 | PEER_MSG_STKT_UPDATE = 128, |
| 123 | PEER_MSG_STKT_INCUPDATE, |
| 124 | PEER_MSG_STKT_DEFINE, |
| 125 | PEER_MSG_STKT_SWITCH, |
| 126 | PEER_MSG_STKT_ACK, |
| 127 | }; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 128 | |
| 129 | /**********************************/ |
| 130 | /* Peer Session IO handler states */ |
| 131 | /**********************************/ |
| 132 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 133 | enum { |
| 134 | PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */ |
| 135 | PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */ |
| 136 | PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */ |
| 137 | PEER_SESS_ST_GETPEER, /* Validate peer ID correspond to a known remote peer id */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 138 | /* after this point, data were possibly exchanged */ |
| 139 | PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */ |
| 140 | PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */ |
| 141 | PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */ |
| 142 | PEER_SESS_ST_WAITMSG, /* Wait for data messages */ |
| 143 | PEER_SESS_ST_EXIT, /* Exit with status code */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 144 | PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */ |
| 145 | PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 146 | PEER_SESS_ST_END, /* Killed session */ |
| 147 | }; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 148 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 149 | /***************************************************/ |
| 150 | /* Peer Session status code - part of the protocol */ |
| 151 | /***************************************************/ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 152 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 153 | #define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */ |
| 154 | #define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 155 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 156 | #define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 157 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 158 | #define PEER_SESS_SC_TRYAGAIN 300 /* try again later */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 159 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 160 | #define PEER_SESS_SC_ERRPROTO 501 /* error protocol */ |
| 161 | #define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */ |
| 162 | #define PEER_SESS_SC_ERRHOST 503 /* bad host name */ |
| 163 | #define PEER_SESS_SC_ERRPEER 504 /* unknown peer */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 164 | |
| 165 | #define PEER_SESSION_PROTO_NAME "HAProxyS" |
| 166 | |
| 167 | struct peers *peers = NULL; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 168 | static void peer_session_forceshutdown(struct stream * stream); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 169 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 170 | int intencode(uint64_t i, char **str) { |
| 171 | int idx = 0; |
| 172 | unsigned char *msg; |
| 173 | |
| 174 | if (!*str) |
| 175 | return 0; |
| 176 | |
| 177 | msg = (unsigned char *)*str; |
| 178 | if (i < 240) { |
| 179 | msg[0] = (unsigned char)i; |
| 180 | *str = (char *)&msg[idx+1]; |
| 181 | return (idx+1); |
| 182 | } |
| 183 | |
| 184 | msg[idx] =(unsigned char)i | 240; |
| 185 | i = (i - 240) >> 4; |
| 186 | while (i >= 128) { |
| 187 | msg[++idx] = (unsigned char)i | 128; |
| 188 | i = (i - 128) >> 7; |
| 189 | } |
| 190 | msg[++idx] = (unsigned char)i; |
| 191 | *str = (char *)&msg[idx+1]; |
| 192 | return (idx+1); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | /* This function returns the decoded integer or 0 |
| 197 | if decode failed |
| 198 | *str point on the beginning of the integer to decode |
| 199 | at the end of decoding *str point on the end of the |
| 200 | encoded integer or to null if end is reached */ |
| 201 | uint64_t intdecode(char **str, char *end) { |
| 202 | uint64_t i; |
| 203 | int idx = 0; |
| 204 | unsigned char *msg; |
| 205 | |
| 206 | if (!*str) |
| 207 | return 0; |
| 208 | |
| 209 | msg = (unsigned char *)*str; |
| 210 | if (msg >= (unsigned char *)end) { |
| 211 | *str = NULL; |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | if (msg[idx] < 240) { |
| 216 | *str = (char *)&msg[idx+1]; |
| 217 | return msg[idx]; |
| 218 | } |
| 219 | i = msg[idx]; |
| 220 | do { |
| 221 | idx++; |
| 222 | if (msg >= (unsigned char *)end) { |
| 223 | *str = NULL; |
| 224 | return 0; |
| 225 | } |
| 226 | i += (uint64_t)msg[idx] << (4 + 7*(idx-1)); |
| 227 | } |
| 228 | while (msg[idx] > 128); |
| 229 | *str = (char *)&msg[idx+1]; |
| 230 | return i; |
| 231 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 232 | |
| 233 | /* |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 234 | * This prepare the data update message on the stick session <ts>, <st> is the considered |
| 235 | * stick table. |
| 236 | * <msg> is a buffer of <size> to recieve data message content |
| 237 | * If function returns 0, the caller should consider we were unable to encode this message (TODO: |
| 238 | * check size) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 239 | */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 240 | static int peer_prepare_updatemsg(struct stksess *ts, struct shared_table *st, char *msg, size_t size, int use_identifier) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 241 | { |
| 242 | uint32_t netinteger; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 243 | unsigned short datalen; |
| 244 | char *cursor, *datamsg; |
| 245 | |
| 246 | cursor = datamsg = msg + 1 + 5; |
| 247 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 248 | /* construct message */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 249 | |
| 250 | /* check if we need to send the update identifer */ |
| 251 | if (st->last_pushed && ts->upd.key > st->last_pushed && (ts->upd.key - st->last_pushed) == 1) { |
| 252 | use_identifier = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 253 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 254 | |
| 255 | /* encode update identifier if needed */ |
| 256 | if (use_identifier) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 257 | netinteger = htonl(ts->upd.key); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 258 | memcpy(cursor, &netinteger, sizeof(netinteger)); |
| 259 | cursor += sizeof(netinteger); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 262 | /* encode the key */ |
| 263 | if (st->table->type == STKTABLE_TYPE_STRING) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 264 | int stlen = strlen((char *)ts->key.key); |
| 265 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 266 | intencode(stlen, &cursor); |
| 267 | memcpy(cursor, ts->key.key, stlen); |
| 268 | cursor += stlen; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 269 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 270 | else if (st->table->type == STKTABLE_TYPE_INTEGER) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 271 | netinteger = htonl(*((uint32_t *)ts->key.key)); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 272 | memcpy(cursor, &netinteger, sizeof(netinteger)); |
| 273 | cursor += sizeof(netinteger); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 274 | } |
| 275 | else { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 276 | memcpy(cursor, ts->key.key, st->table->key_size); |
| 277 | cursor += st->table->key_size; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 280 | /* encode values */ |
| 281 | if (stktable_data_ptr(st->table, ts, STKTABLE_DT_SERVER_ID)) { |
| 282 | int srvid; |
| 283 | |
| 284 | srvid = stktable_data_cast(stktable_data_ptr(st->table, ts, STKTABLE_DT_SERVER_ID), server_id); |
| 285 | intencode(srvid, &cursor); |
| 286 | } |
| 287 | |
| 288 | /* Compute datalen */ |
| 289 | datalen = (cursor - datamsg); |
| 290 | |
| 291 | /* prepare message header */ |
| 292 | msg[0] = PEER_MSG_CLASS_STICKTABLE; |
| 293 | if (use_identifier) |
| 294 | msg[1] = PEER_MSG_STKT_UPDATE; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 295 | else |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 296 | msg[1] = PEER_MSG_STKT_INCUPDATE; |
| 297 | |
| 298 | cursor = &msg[2]; |
| 299 | intencode(datalen, &cursor); |
| 300 | |
| 301 | /* move data after header */ |
| 302 | memmove(cursor, datamsg, datalen); |
| 303 | |
| 304 | /* return header size + data_len */ |
| 305 | return (cursor - msg) + datalen; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * This prepare the switch table message to targeted share table <st>. |
| 310 | * <msg> is a buffer of <size> to recieve data message content |
| 311 | * If function returns 0, the caller should consider we were unable to encode this message (TODO: |
| 312 | * check size) |
| 313 | */ |
| 314 | static int peer_prepare_switchmsg(struct shared_table *st, char *msg, size_t size) |
| 315 | { |
| 316 | int len; |
| 317 | unsigned short datalen; |
| 318 | char *cursor, *datamsg; |
| 319 | uint64_t data = 0; |
| 320 | |
| 321 | cursor = datamsg = msg + 2 + 5; |
| 322 | |
| 323 | /* Encode data */ |
| 324 | |
| 325 | /* encode local id */ |
| 326 | intencode(st->local_id, &cursor); |
| 327 | |
| 328 | /* encode table name */ |
| 329 | len = strlen(st->table->id); |
| 330 | intencode(len, &cursor); |
| 331 | memcpy(cursor, st->table->id, len); |
| 332 | cursor += len; |
| 333 | |
| 334 | /* encode table type */ |
| 335 | |
| 336 | intencode(st->table->type, &cursor); |
| 337 | |
| 338 | /* encode table key size */ |
| 339 | intencode(st->table->key_size, &cursor); |
| 340 | |
| 341 | /* encode available data types in table */ |
| 342 | if (st->table->data_ofs[STKTABLE_DT_SERVER_ID]) { |
| 343 | data |= 1 << STKTABLE_DT_SERVER_ID; |
| 344 | } |
| 345 | intencode(data, &cursor); |
| 346 | |
| 347 | /* Compute datalen */ |
| 348 | datalen = (cursor - datamsg); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 349 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 350 | /* prepare message header */ |
| 351 | msg[0] = PEER_MSG_CLASS_STICKTABLE; |
| 352 | msg[1] = PEER_MSG_STKT_DEFINE; |
| 353 | cursor = &msg[2]; |
| 354 | intencode(datalen, &cursor); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 355 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 356 | /* move data after header */ |
| 357 | memmove(cursor, datamsg, datalen); |
| 358 | |
| 359 | /* return header size + data_len */ |
| 360 | return (cursor - msg) + datalen; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 361 | } |
| 362 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 363 | /* |
| 364 | * This prepare the acknowledge message on the stick session <ts>, <st> is the considered |
| 365 | * stick table. |
| 366 | * <msg> is a buffer of <size> to recieve data message content |
| 367 | * If function returns 0, the caller should consider we were unable to encode this message (TODO: |
| 368 | * check size) |
| 369 | */ |
| 370 | static int peer_prepare_ackmsg(struct shared_table *st, char *msg, size_t size) |
| 371 | { |
| 372 | unsigned short datalen; |
| 373 | char *cursor, *datamsg; |
| 374 | uint32_t netinteger; |
| 375 | |
| 376 | cursor = datamsg = trash.str + 2 + 5; |
| 377 | |
| 378 | intencode(st->remote_id, &cursor); |
| 379 | netinteger = htonl(st->last_get); |
| 380 | memcpy(cursor, &netinteger, sizeof(netinteger)); |
| 381 | cursor += sizeof(netinteger); |
| 382 | |
| 383 | /* Compute datalen */ |
| 384 | datalen = (cursor - datamsg); |
| 385 | |
| 386 | /* prepare message header */ |
| 387 | msg[0] = PEER_MSG_CLASS_STICKTABLE; |
| 388 | msg[1] = PEER_MSG_STKT_DEFINE; |
| 389 | cursor = &msg[2]; |
| 390 | intencode(datalen, &cursor); |
| 391 | |
| 392 | /* move data after header */ |
| 393 | memmove(cursor, datamsg, datalen); |
| 394 | |
| 395 | /* return header size + data_len */ |
| 396 | return (cursor - msg) + datalen; |
| 397 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 398 | |
| 399 | /* |
| 400 | * Callback to release a session with a peer |
| 401 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 402 | static void peer_session_release(struct appctx *appctx) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 403 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 404 | struct stream_interface *si = appctx->owner; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 405 | struct stream *s = si_strm(si); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 406 | struct peer *peer = (struct peer *)appctx->ctx.peers.ptr; |
| 407 | struct peers *peers = (struct peers *)strm_fe(s)->parent; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 408 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 409 | /* appctx->ctx.peers.ptr is not a peer session */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 410 | if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 411 | return; |
| 412 | |
| 413 | /* peer session identified */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 414 | if (peer) { |
| 415 | if (peer->stream == s) { |
| 416 | peer->stream = NULL; |
| 417 | peer->appctx = NULL; |
| 418 | if (peer->flags & PEER_F_LEARN_ASSIGN) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 419 | /* unassign current peer for learning */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 420 | peer->flags &= ~(PEER_F_LEARN_ASSIGN); |
| 421 | peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 422 | |
| 423 | /* reschedule a resync */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 424 | peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 425 | } |
| 426 | /* reset teaching and learning flags to 0 */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 427 | peer->flags &= PEER_TEACH_RESET; |
| 428 | peer->flags &= PEER_LEARN_RESET; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 429 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 430 | task_wakeup(peers->sync_task, TASK_WOKEN_MSG); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| 434 | |
| 435 | /* |
| 436 | * IO Handler to handle message exchance with a peer |
| 437 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 438 | static void peer_io_handler(struct appctx *appctx) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 439 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 440 | struct stream_interface *si = appctx->owner; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 441 | struct stream *s = si_strm(si); |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 442 | struct peers *curpeers = (struct peers *)strm_fe(s)->parent; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 443 | int reql = 0; |
| 444 | int repl = 0; |
| 445 | |
| 446 | while (1) { |
| 447 | switchstate: |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 448 | switch(appctx->st0) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 449 | case PEER_SESS_ST_ACCEPT: |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 450 | appctx->ctx.peers.ptr = NULL; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 451 | appctx->st0 = PEER_SESS_ST_GETVERSION; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 452 | /* fall through */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 453 | case PEER_SESS_ST_GETVERSION: |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 454 | reql = bo_getline(si_oc(si), trash.str, trash.size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 455 | if (reql <= 0) { /* closed or EOL not found */ |
| 456 | if (reql == 0) |
| 457 | goto out; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 458 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 459 | goto switchstate; |
| 460 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 461 | if (trash.str[reql-1] != '\n') { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 462 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 463 | goto switchstate; |
| 464 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 465 | else if (reql > 1 && (trash.str[reql-2] == '\r')) |
| 466 | trash.str[reql-2] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 467 | else |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 468 | trash.str[reql-1] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 469 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 470 | bo_skip(si_oc(si), reql); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 471 | |
| 472 | /* test version */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 473 | if (strcmp(PEER_SESSION_PROTO_NAME " 2.0", trash.str) != 0) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 474 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 475 | appctx->st1 = PEER_SESS_SC_ERRVERSION; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 476 | /* test protocol */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 477 | if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.str, strlen(PEER_SESSION_PROTO_NAME)+1) != 0) |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 478 | appctx->st1 = PEER_SESS_SC_ERRPROTO; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 479 | goto switchstate; |
| 480 | } |
| 481 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 482 | appctx->st0 = PEER_SESS_ST_GETHOST; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 483 | /* fall through */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 484 | case PEER_SESS_ST_GETHOST: |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 485 | reql = bo_getline(si_oc(si), trash.str, trash.size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 486 | if (reql <= 0) { /* closed or EOL not found */ |
| 487 | if (reql == 0) |
| 488 | goto out; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 489 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 490 | goto switchstate; |
| 491 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 492 | if (trash.str[reql-1] != '\n') { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 493 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 494 | goto switchstate; |
| 495 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 496 | else if (reql > 1 && (trash.str[reql-2] == '\r')) |
| 497 | trash.str[reql-2] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 498 | else |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 499 | trash.str[reql-1] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 500 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 501 | bo_skip(si_oc(si), reql); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 502 | |
| 503 | /* test hostname match */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 504 | if (strcmp(localpeer, trash.str) != 0) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 505 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 506 | appctx->st1 = PEER_SESS_SC_ERRHOST; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 507 | goto switchstate; |
| 508 | } |
| 509 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 510 | appctx->st0 = PEER_SESS_ST_GETPEER; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 511 | /* fall through */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 512 | case PEER_SESS_ST_GETPEER: { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 513 | struct peer *curpeer; |
| 514 | char *p; |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 515 | reql = bo_getline(si_oc(si), trash.str, trash.size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 516 | if (reql <= 0) { /* closed or EOL not found */ |
| 517 | if (reql == 0) |
| 518 | goto out; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 519 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 520 | goto switchstate; |
| 521 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 522 | if (trash.str[reql-1] != '\n') { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 523 | /* Incomplete line, we quit */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 524 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 525 | goto switchstate; |
| 526 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 527 | else if (reql > 1 && (trash.str[reql-2] == '\r')) |
| 528 | trash.str[reql-2] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 529 | else |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 530 | trash.str[reql-1] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 531 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 532 | bo_skip(si_oc(si), reql); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 533 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 534 | /* parse line "<peer name> <pid> <relative_pid>" */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 535 | p = strchr(trash.str, ' '); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 536 | if (!p) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 537 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 538 | appctx->st1 = PEER_SESS_SC_ERRPROTO; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 539 | goto switchstate; |
| 540 | } |
| 541 | *p = 0; |
| 542 | |
| 543 | /* lookup known peer */ |
| 544 | for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 545 | if (strcmp(curpeer->id, trash.str) == 0) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 546 | break; |
| 547 | } |
| 548 | |
| 549 | /* if unknown peer */ |
| 550 | if (!curpeer) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 551 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 552 | appctx->st1 = PEER_SESS_SC_ERRPEER; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 553 | goto switchstate; |
| 554 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 555 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 556 | if (curpeer->stream && curpeer->stream != s) { |
| 557 | if (curpeer->local) { |
| 558 | /* Local connection, reply a retry */ |
| 559 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 560 | appctx->st1 = PEER_SESS_SC_TRYAGAIN; |
| 561 | goto switchstate; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 562 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 563 | peer_session_forceshutdown(curpeer->stream); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 564 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 565 | curpeer->stream = s; |
| 566 | curpeer->appctx = appctx; |
| 567 | appctx->ctx.peers.ptr = curpeer; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 568 | appctx->st0 = PEER_SESS_ST_SENDSUCCESS; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 569 | /* fall through */ |
| 570 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 571 | case PEER_SESS_ST_SENDSUCCESS: { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 572 | struct peer *curpeer = (struct peer *)appctx->ctx.peers.ptr; |
| 573 | struct shared_table *st; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 574 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 575 | repl = snprintf(trash.str, trash.size, "%d\n", PEER_SESS_SC_SUCCESSCODE); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 576 | repl = bi_putblk(si_ic(si), trash.str, repl); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 577 | if (repl <= 0) { |
| 578 | if (repl == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 579 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 580 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 581 | goto switchstate; |
| 582 | } |
| 583 | |
| 584 | /* Register status code */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 585 | curpeer->statuscode = PEER_SESS_SC_SUCCESSCODE; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 586 | |
| 587 | /* Awake main task */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 588 | task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 589 | |
| 590 | /* Init confirm counter */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 591 | curpeer->confirm = 0; |
| 592 | |
| 593 | /* Init cursors */ |
| 594 | for (st = curpeer->tables; st ; st = st->next) { |
| 595 | st->last_get = st->last_acked = 0; |
| 596 | st->teaching_origin = st->last_pushed = st->update; |
| 597 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 598 | |
| 599 | /* reset teaching and learning flags to 0 */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 600 | curpeer->flags &= PEER_TEACH_RESET; |
| 601 | curpeer->flags &= PEER_LEARN_RESET; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 602 | |
| 603 | /* if current peer is local */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 604 | if (curpeer->local) { |
| 605 | /* if current host need resyncfrom local and no process assined */ |
| 606 | if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL && |
| 607 | !(peers->flags & PEERS_F_RESYNC_ASSIGN)) { |
| 608 | /* assign local peer for a lesson, consider lesson already requested */ |
| 609 | curpeer->flags |= PEER_F_LEARN_ASSIGN; |
| 610 | peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS); |
| 611 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 612 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 613 | } |
| 614 | else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE && |
| 615 | !(peers->flags & PEERS_F_RESYNC_ASSIGN)) { |
| 616 | /* assign peer for a lesson */ |
| 617 | curpeer->flags |= PEER_F_LEARN_ASSIGN; |
| 618 | peers->flags |= PEERS_F_RESYNC_ASSIGN; |
| 619 | } |
| 620 | |
| 621 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 622 | /* switch to waiting message state */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 623 | appctx->st0 = PEER_SESS_ST_WAITMSG; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 624 | goto switchstate; |
| 625 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 626 | case PEER_SESS_ST_CONNECT: { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 627 | struct peer *curpeer = (struct peer *)appctx->ctx.peers.ptr; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 628 | |
| 629 | /* Send headers */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 630 | repl = snprintf(trash.str, trash.size, |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 631 | PEER_SESSION_PROTO_NAME " 2.0\n%s\n%s %d %d\n", |
| 632 | curpeer->id, |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 633 | localpeer, |
Willy Tarreau | 7b77c9f | 2012-01-07 22:52:12 +0100 | [diff] [blame] | 634 | (int)getpid(), |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 635 | relative_pid); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 636 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 637 | if (repl >= trash.size) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 638 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 639 | goto switchstate; |
| 640 | } |
| 641 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 642 | repl = bi_putblk(si_ic(si), trash.str, repl); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 643 | if (repl <= 0) { |
| 644 | if (repl == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 645 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 646 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 647 | goto switchstate; |
| 648 | } |
| 649 | |
| 650 | /* switch to the waiting statuscode state */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 651 | appctx->st0 = PEER_SESS_ST_GETSTATUS; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 652 | /* fall through */ |
| 653 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 654 | case PEER_SESS_ST_GETSTATUS: { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 655 | struct peer *curpeer = (struct peer *)appctx->ctx.peers.ptr; |
| 656 | struct shared_table *st; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 657 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 658 | if (si_ic(si)->flags & CF_WRITE_PARTIAL) |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 659 | curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 660 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 661 | reql = bo_getline(si_oc(si), trash.str, trash.size); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 662 | if (reql <= 0) { /* closed or EOL not found */ |
| 663 | if (reql == 0) |
| 664 | goto out; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 665 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 666 | goto switchstate; |
| 667 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 668 | if (trash.str[reql-1] != '\n') { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 669 | /* Incomplete line, we quit */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 670 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 671 | goto switchstate; |
| 672 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 673 | else if (reql > 1 && (trash.str[reql-2] == '\r')) |
| 674 | trash.str[reql-2] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 675 | else |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 676 | trash.str[reql-1] = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 677 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 678 | bo_skip(si_oc(si), reql); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 679 | |
| 680 | /* Register status code */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 681 | curpeer->statuscode = atoi(trash.str); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 682 | |
| 683 | /* Awake main task */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 684 | task_wakeup(peers->sync_task, TASK_WOKEN_MSG); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 685 | |
| 686 | /* If status code is success */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 687 | if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 688 | /* Init cursors */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 689 | for (st = curpeer->tables; st ; st = st->next) { |
| 690 | st->last_get = st->last_acked = 0; |
| 691 | st->teaching_origin = st->last_pushed = st->update; |
| 692 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 693 | |
| 694 | /* Init confirm counter */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 695 | curpeer->confirm = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 696 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 697 | /* reset teaching and learning flags to 0 */ |
| 698 | curpeer->flags &= PEER_TEACH_RESET; |
| 699 | curpeer->flags &= PEER_LEARN_RESET; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 700 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 701 | /* If current peer is local */ |
| 702 | if (curpeer->local) { |
| 703 | /* flag to start to teach lesson */ |
| 704 | curpeer->flags |= PEER_F_TEACH_PROCESS; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 705 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 706 | } |
| 707 | else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE && |
| 708 | !(peers->flags & PEERS_F_RESYNC_ASSIGN)) { |
| 709 | /* If peer is remote and resync from remote is needed, |
| 710 | and no peer currently assigned */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 711 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 712 | /* assign peer for a lesson */ |
| 713 | curpeer->flags |= PEER_F_LEARN_ASSIGN; |
| 714 | peers->flags |= PEERS_F_RESYNC_ASSIGN; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | } |
| 718 | else { |
| 719 | /* Status code is not success, abort */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 720 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 721 | goto switchstate; |
| 722 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 723 | appctx->st0 = PEER_SESS_ST_WAITMSG; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 724 | /* fall through */ |
| 725 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 726 | case PEER_SESS_ST_WAITMSG: { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 727 | struct peer *curpeer = (struct peer *)appctx->ctx.peers.ptr; |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 728 | struct stksess *ts, *newts = NULL; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 729 | uint32_t msg_len = 0; |
| 730 | char *msg_cur = trash.str; |
| 731 | char *msg_end = trash.str; |
| 732 | unsigned char msg_head[7]; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 733 | int totl = 0; |
| 734 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 735 | reql = bo_getblk(si_oc(si), (char *)msg_head, 2*sizeof(unsigned char), totl); |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 736 | if (reql <= 0) /* closed or EOL not found */ |
| 737 | goto incomplete; |
| 738 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 739 | totl += reql; |
| 740 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 741 | if (msg_head[1] >= 128) { |
| 742 | /* Read and Decode message length */ |
| 743 | reql = bo_getblk(si_oc(si), (char *)&msg_head[2], sizeof(unsigned char), totl); |
| 744 | if (reql <= 0) /* closed */ |
| 745 | goto incomplete; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 746 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 747 | totl += reql; |
| 748 | |
| 749 | if (msg_head[2] < 240) { |
| 750 | msg_len = msg_head[2]; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 751 | } |
| 752 | else { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 753 | int i; |
| 754 | char *cur; |
| 755 | char *end; |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 756 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 757 | for (i = 3 ; i < sizeof(msg_head) ; i++) { |
| 758 | reql = bo_getblk(si_oc(si), (char *)&msg_head[i], sizeof(char), totl); |
| 759 | if (reql <= 0) /* closed */ |
| 760 | goto incomplete; |
| 761 | |
| 762 | totl += reql; |
| 763 | |
| 764 | if (!(msg_head[i] & 0x80)) |
| 765 | break; |
| 766 | } |
| 767 | |
| 768 | if (i == sizeof(msg_head)) { |
| 769 | /* malformed message */ |
| 770 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 771 | goto switchstate; |
| 772 | |
| 773 | } |
| 774 | end = (char *)msg_head + sizeof(msg_head); |
| 775 | cur = (char *)&msg_head[2]; |
| 776 | msg_len = intdecode(&cur, end); |
| 777 | if (!cur) { |
| 778 | /* malformed message */ |
| 779 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 780 | goto switchstate; |
| 781 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 782 | } |
| 783 | |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 784 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 785 | /* Read message content */ |
| 786 | if (msg_len) { |
| 787 | if (msg_len > trash.size) { |
| 788 | /* Status code is not success, abort */ |
| 789 | appctx->st0 = PEER_SESS_ST_ERRSIZE; |
| 790 | goto switchstate; |
| 791 | } |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 792 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 793 | reql = bo_getblk(si_oc(si), trash.str, msg_len, totl); |
| 794 | if (reql <= 0) /* closed */ |
| 795 | goto incomplete; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 796 | totl += reql; |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 797 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 798 | msg_end += msg_len; |
| 799 | } |
| 800 | } |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 801 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 802 | if (msg_head[0] == PEER_MSG_CLASS_CONTROL) { |
| 803 | if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) { |
| 804 | struct shared_table *st; |
| 805 | /* Reset message: remote need resync */ |
| 806 | |
| 807 | /* prepare tables fot a global push */ |
| 808 | for (st = curpeer->tables; st; st = st->next) { |
| 809 | st->teaching_origin = st->last_pushed = st->table->update; |
| 810 | st->flags = 0; |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 811 | } |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 812 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 813 | /* reset teaching flags to 0 */ |
| 814 | curpeer->flags &= PEER_TEACH_RESET; |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 815 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 816 | /* flag to start to teach lesson */ |
| 817 | curpeer->flags |= PEER_F_TEACH_PROCESS; |
| 818 | |
| 819 | |
| 820 | } |
| 821 | else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) { |
| 822 | |
| 823 | if (curpeer->flags & PEER_F_LEARN_ASSIGN) { |
| 824 | curpeer->flags &= ~PEER_F_LEARN_ASSIGN; |
| 825 | peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS); |
| 826 | peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE); |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 827 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 828 | curpeer->confirm++; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 829 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 830 | else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) { |
| 831 | |
| 832 | if (curpeer->flags & PEER_F_LEARN_ASSIGN) { |
| 833 | curpeer->flags &= ~PEER_F_LEARN_ASSIGN; |
| 834 | peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS); |
| 835 | |
| 836 | curpeer->flags |= PEER_F_LEARN_NOTUP2DATE; |
| 837 | peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); |
| 838 | task_wakeup(peers->sync_task, TASK_WOKEN_MSG); |
Cyril Bonté | 9a60ff9 | 2014-02-16 01:07:07 +0100 | [diff] [blame] | 839 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 840 | curpeer->confirm++; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 841 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 842 | else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) { |
| 843 | |
| 844 | /* If stopping state */ |
| 845 | if (stopping) { |
| 846 | /* Close session, push resync no more needed */ |
| 847 | curpeer->flags |= PEER_F_TEACH_COMPLETE; |
| 848 | appctx->st0 = PEER_SESS_ST_END; |
| 849 | goto switchstate; |
| 850 | } |
| 851 | |
| 852 | /* reset teaching flags to 0 */ |
| 853 | curpeer->flags &= PEER_TEACH_RESET; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 854 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 855 | } |
| 856 | else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) { |
| 857 | if (msg_head[1] == PEER_MSG_STKT_DEFINE) { |
| 858 | int table_id_len; |
| 859 | struct shared_table *st; |
| 860 | int table_type; |
| 861 | int table_keylen; |
| 862 | int table_id; |
| 863 | uint64_t table_data; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 864 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 865 | table_id = intdecode(&msg_cur, msg_end); |
| 866 | if (!msg_cur) { |
| 867 | /* malformed message */ |
| 868 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 869 | goto switchstate; |
| 870 | } |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 871 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 872 | table_id_len = intdecode(&msg_cur, msg_end); |
| 873 | if (!msg_cur) { |
| 874 | /* malformed message */ |
| 875 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 876 | goto switchstate; |
| 877 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 878 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 879 | curpeer->remote_table = NULL; |
| 880 | if (!table_id_len || (msg_cur + table_id_len) >= msg_end) { |
| 881 | /* malformed message */ |
| 882 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 883 | goto switchstate; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 884 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 885 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 886 | for (st = curpeer->tables; st; st = st->next) { |
| 887 | /* Reset IDs */ |
| 888 | if (st->remote_id == table_id) |
| 889 | st->remote_id = 0; |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 890 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 891 | if (!curpeer->remote_table |
| 892 | && (table_id_len == strlen(st->table->id)) |
| 893 | && (memcmp(st->table->id, msg_cur, table_id_len) == 0)) { |
| 894 | curpeer->remote_table = st; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 898 | if (!curpeer->remote_table) { |
| 899 | goto ignore_msg; |
| 900 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 901 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 902 | msg_cur += table_id_len; |
| 903 | if (msg_cur >= msg_end) { |
| 904 | /* malformed message */ |
| 905 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 906 | goto switchstate; |
| 907 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 908 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 909 | table_type = intdecode(&msg_cur, msg_end); |
| 910 | if (!msg_cur) { |
| 911 | /* malformed message */ |
| 912 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 913 | goto switchstate; |
| 914 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 915 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 916 | table_keylen = intdecode(&msg_cur, msg_end); |
| 917 | if (!msg_cur) { |
| 918 | /* malformed message */ |
| 919 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 920 | goto switchstate; |
| 921 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 922 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 923 | table_data = intdecode(&msg_cur, msg_end); |
| 924 | if (!msg_cur) { |
| 925 | /* malformed message */ |
| 926 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 927 | goto switchstate; |
| 928 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 929 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 930 | if (curpeer->remote_table->table->type != table_type |
| 931 | || curpeer->remote_table->table->key_size != table_keylen) { |
| 932 | curpeer->remote_table = NULL; |
| 933 | goto ignore_msg; |
| 934 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 935 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 936 | curpeer->remote_table->remote_data = table_data; |
| 937 | curpeer->remote_table->remote_id = table_id; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 938 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 939 | else if (msg_head[1] == PEER_MSG_STKT_SWITCH) { |
| 940 | struct shared_table *st; |
| 941 | int table_id; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 942 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 943 | table_id = intdecode(&msg_cur, msg_end); |
| 944 | if (!msg_cur) { |
| 945 | /* malformed message */ |
| 946 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 947 | goto switchstate; |
| 948 | } |
| 949 | curpeer->remote_table = NULL; |
| 950 | for (st = curpeer->tables; st; st = st->next) { |
| 951 | if (st->remote_id == table_id) { |
| 952 | curpeer->remote_table = st; |
| 953 | break; |
| 954 | } |
| 955 | } |
| 956 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 957 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 958 | else if (msg_head[1] == PEER_MSG_STKT_UPDATE |
| 959 | || msg_head[1] == PEER_MSG_STKT_INCUPDATE) { |
| 960 | struct shared_table *st = curpeer->remote_table; |
| 961 | uint32_t update; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 962 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 963 | /* Here we have data message */ |
| 964 | if (!st) |
| 965 | goto ignore_msg; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 966 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 967 | if (msg_head[1] == PEER_MSG_STKT_UPDATE) { |
| 968 | if (msg_len < sizeof(update)) { |
| 969 | /* malformed message */ |
| 970 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 971 | goto switchstate; |
| 972 | } |
| 973 | memcpy(&update, msg_cur, sizeof(update)); |
| 974 | msg_cur += sizeof(update); |
| 975 | st->last_get = htonl(update); |
| 976 | } |
| 977 | else { |
| 978 | st->last_get++; |
| 979 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 980 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 981 | newts = stksess_new(st->table, NULL); |
| 982 | if (!newts) |
| 983 | goto ignore_msg; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 984 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 985 | if (st->table->type == STKTABLE_TYPE_STRING) { |
| 986 | unsigned int to_read, to_store; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 987 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 988 | to_read = intdecode(&msg_cur, msg_end); |
| 989 | if (!msg_cur) { |
| 990 | /* malformed message */ |
| 991 | stksess_free(st->table, newts); |
| 992 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 993 | goto switchstate; |
| 994 | } |
| 995 | to_store = MIN(to_read, st->table->key_size - 1); |
| 996 | if (msg_cur + to_store > msg_end) { |
| 997 | /* malformed message */ |
| 998 | stksess_free(st->table, newts); |
| 999 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1000 | goto switchstate; |
| 1001 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1002 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1003 | memcpy(newts->key.key, msg_cur, to_store); |
| 1004 | newts->key.key[to_store] = 0; |
| 1005 | msg_cur += to_read; |
| 1006 | } |
| 1007 | else if (st->table->type == STKTABLE_TYPE_INTEGER) { |
| 1008 | unsigned int netinteger; |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 1009 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1010 | if (msg_cur + sizeof(netinteger) > msg_end) { |
| 1011 | /* malformed message */ |
| 1012 | stksess_free(st->table, newts); |
| 1013 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1014 | goto switchstate; |
| 1015 | } |
| 1016 | memcpy(&netinteger, msg_cur, sizeof(netinteger)); |
| 1017 | netinteger = ntohl(netinteger); |
| 1018 | memcpy(newts->key.key, &netinteger, sizeof(netinteger)); |
| 1019 | msg_cur += sizeof(netinteger); |
| 1020 | } |
| 1021 | else { |
| 1022 | if (msg_cur + st->table->key_size > msg_end) { |
| 1023 | /* malformed message */ |
| 1024 | stksess_free(st->table, newts); |
| 1025 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1026 | goto switchstate; |
| 1027 | } |
| 1028 | memcpy(newts->key.key, msg_cur, st->table->key_size); |
| 1029 | msg_cur += st->table->key_size; |
| 1030 | } |
| 1031 | |
| 1032 | /* lookup for existing entry */ |
| 1033 | ts = stktable_lookup(st->table, newts); |
| 1034 | if (ts) { |
| 1035 | /* the entry already exist, we can free ours */ |
| 1036 | stktable_touch(st->table, ts, 0); |
| 1037 | stksess_free(st->table, newts); |
| 1038 | newts = NULL; |
| 1039 | } |
| 1040 | else { |
| 1041 | struct eb32_node *eb; |
| 1042 | |
| 1043 | /* create new entry */ |
| 1044 | ts = stktable_store(st->table, newts, 0); |
| 1045 | newts = NULL; /* don't reuse it */ |
| 1046 | |
| 1047 | ts->upd.key= (++st->table->update)+(2^31); |
| 1048 | eb = eb32_insert(&st->table->updates, &ts->upd); |
| 1049 | if (eb != &ts->upd) { |
| 1050 | eb32_delete(eb); |
| 1051 | eb32_insert(&st->table->updates, &ts->upd); |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | if ((1 << STKTABLE_DT_SERVER_ID) & st->remote_data) { |
| 1056 | int srvid; |
| 1057 | |
| 1058 | srvid = intdecode(&msg_cur, msg_end); |
| 1059 | if (!msg_cur) { |
| 1060 | /* malformed message */ |
| 1061 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1062 | goto switchstate; |
| 1063 | } |
| 1064 | |
| 1065 | if (stktable_data_ptr(st->table, ts, STKTABLE_DT_SERVER_ID)) { |
| 1066 | stktable_data_cast(stktable_data_ptr(st->table, ts, STKTABLE_DT_SERVER_ID), server_id) = srvid; |
| 1067 | } |
| 1068 | } |
| 1069 | } |
| 1070 | else if (msg_head[1] == PEER_MSG_STKT_ACK) { |
| 1071 | /* ack message */ |
| 1072 | uint32_t table_id ; |
| 1073 | uint32_t update; |
| 1074 | struct shared_table *st; |
| 1075 | |
| 1076 | table_id = intdecode(&msg_cur, msg_end); |
| 1077 | if (!msg_cur || (msg_cur + sizeof(update) > msg_end)) { |
| 1078 | /* malformed message */ |
| 1079 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1080 | goto switchstate; |
| 1081 | } |
| 1082 | memcpy(&update, msg_cur, sizeof(update)); |
| 1083 | update = ntohl(update); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1084 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1085 | for (st = curpeer->tables; st; st = st->next) { |
| 1086 | if (st->local_id == table_id) { |
| 1087 | st->update = update; |
| 1088 | break; |
| 1089 | } |
| 1090 | } |
| 1091 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1092 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1093 | else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) { |
| 1094 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1095 | goto switchstate; |
| 1096 | } |
| 1097 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1098 | ignore_msg: |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1099 | /* skip consumed message */ |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1100 | bo_skip(si_oc(si), totl); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1101 | /* loop on that state to peek next message */ |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 1102 | goto switchstate; |
| 1103 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1104 | incomplete: |
Willy Tarreau | 9d9179b | 2013-04-11 16:56:44 +0200 | [diff] [blame] | 1105 | /* we get here when a bo_getblk() returns <= 0 in reql */ |
| 1106 | |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 1107 | if (reql < 0) { |
| 1108 | /* there was an error */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1109 | appctx->st0 = PEER_SESS_ST_END; |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 1110 | goto switchstate; |
| 1111 | } |
| 1112 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1113 | |
| 1114 | /* Confirm finished or partial messages */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1115 | while (curpeer->confirm) { |
| 1116 | unsigned char msg[2]; |
| 1117 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1118 | /* There is a confirm messages to send */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1119 | msg[0] = PEER_MSG_CLASS_CONTROL; |
| 1120 | msg[1] = PEER_MSG_CTRL_RESYNCCONFIRM; |
| 1121 | |
| 1122 | /* message to buffer */ |
| 1123 | repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg)); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1124 | if (repl <= 0) { |
| 1125 | /* no more write possible */ |
| 1126 | if (repl == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1127 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1128 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1129 | goto switchstate; |
| 1130 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1131 | curpeer->confirm--; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1132 | } |
| 1133 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1134 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1135 | /* Need to request a resync */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1136 | if ((curpeer->flags & PEER_F_LEARN_ASSIGN) && |
| 1137 | (peers->flags & PEERS_F_RESYNC_ASSIGN) && |
| 1138 | !(peers->flags & PEERS_F_RESYNC_PROCESS)) { |
| 1139 | unsigned char msg[2]; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1140 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1141 | /* Current peer was elected to request a resync */ |
| 1142 | msg[0] = PEER_MSG_CLASS_CONTROL; |
| 1143 | msg[1] = PEER_MSG_CTRL_RESYNCREQ; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1144 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1145 | /* message to buffer */ |
| 1146 | repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg)); |
| 1147 | if (repl <= 0) { |
| 1148 | /* no more write possible */ |
| 1149 | if (repl == -1) |
| 1150 | goto full; |
| 1151 | appctx->st0 = PEER_SESS_ST_END; |
| 1152 | goto switchstate; |
| 1153 | } |
| 1154 | peers->flags |= PEERS_F_RESYNC_PROCESS; |
| 1155 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1156 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1157 | /* Nothing to read, now we start to write */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1158 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1159 | if (curpeer->tables) { |
| 1160 | struct shared_table *st; |
| 1161 | struct shared_table *last_local_table; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1162 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1163 | last_local_table = curpeer->last_local_table; |
| 1164 | if (!last_local_table) |
| 1165 | last_local_table = curpeer->tables; |
| 1166 | st = last_local_table->next; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1167 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1168 | while (1) { |
| 1169 | if (!st) |
| 1170 | st = curpeer->tables; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1171 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1172 | /* It remains some updates to ack */ |
| 1173 | if (st->last_get != st->last_acked) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1174 | int msglen; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1175 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1176 | msglen = peer_prepare_ackmsg(st, trash.str, trash.size); |
| 1177 | if (!msglen) { |
| 1178 | /* internal error: message does not fit in trash */ |
| 1179 | appctx->st0 = PEER_SESS_ST_END; |
| 1180 | goto switchstate; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1181 | } |
| 1182 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1183 | /* message to buffer */ |
| 1184 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1185 | if (repl <= 0) { |
| 1186 | /* no more write possible */ |
| 1187 | if (repl == -1) { |
| 1188 | goto full; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1189 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1190 | appctx->st0 = PEER_SESS_ST_END; |
| 1191 | goto switchstate; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1192 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1193 | st->last_acked = st->last_get; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1194 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1195 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1196 | if (!(curpeer->flags & PEER_F_TEACH_PROCESS)) { |
| 1197 | if (!(curpeer->flags & PEER_F_LEARN_ASSIGN) && |
| 1198 | ((int)(st->last_pushed - st->table->localupdate) < 0)) { |
| 1199 | struct eb32_node *eb; |
| 1200 | int new_pushed; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1201 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1202 | if (st != curpeer->last_local_table) { |
| 1203 | int msglen; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1204 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1205 | msglen = peer_prepare_switchmsg(st, trash.str, trash.size); |
| 1206 | if (!msglen) { |
| 1207 | /* internal error: message does not fit in trash */ |
| 1208 | appctx->st0 = PEER_SESS_ST_END; |
| 1209 | goto switchstate; |
| 1210 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1211 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1212 | /* message to buffer */ |
| 1213 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1214 | if (repl <= 0) { |
| 1215 | /* no more write possible */ |
| 1216 | if (repl == -1) { |
| 1217 | goto full; |
| 1218 | } |
| 1219 | appctx->st0 = PEER_SESS_ST_END; |
| 1220 | goto switchstate; |
| 1221 | } |
| 1222 | curpeer->last_local_table = st; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1223 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1224 | |
| 1225 | /* We force new pushed to 1 to force identifier in update message */ |
| 1226 | new_pushed = 1; |
| 1227 | eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1); |
| 1228 | while (1) { |
| 1229 | uint32_t msglen; |
| 1230 | struct stksess *ts; |
| 1231 | |
| 1232 | /* push local updates */ |
| 1233 | if (!eb) { |
| 1234 | eb = eb32_first(&st->table->updates); |
| 1235 | if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) { |
Emeric Brun | aaf5860 | 2015-06-15 17:23:30 +0200 | [diff] [blame^] | 1236 | st->table->commitupdate = st->last_pushed = st->table->localupdate; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1237 | break; |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | if ((int)(eb->key - st->table->localupdate) > 0) { |
Emeric Brun | aaf5860 | 2015-06-15 17:23:30 +0200 | [diff] [blame^] | 1242 | st->table->commitupdate = st->last_pushed = st->table->localupdate; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1243 | break; |
| 1244 | } |
| 1245 | |
| 1246 | ts = eb32_entry(eb, struct stksess, upd); |
| 1247 | msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed); |
| 1248 | if (!msglen) { |
| 1249 | /* internal error: message does not fit in trash */ |
| 1250 | appctx->st0 = PEER_SESS_ST_END; |
| 1251 | goto switchstate; |
| 1252 | } |
| 1253 | |
| 1254 | /* message to buffer */ |
| 1255 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1256 | if (repl <= 0) { |
| 1257 | /* no more write possible */ |
| 1258 | if (repl == -1) { |
| 1259 | goto full; |
| 1260 | } |
| 1261 | appctx->st0 = PEER_SESS_ST_END; |
| 1262 | goto switchstate; |
| 1263 | } |
| 1264 | st->last_pushed = ts->upd.key; |
Emeric Brun | aaf5860 | 2015-06-15 17:23:30 +0200 | [diff] [blame^] | 1265 | if ((int)(st->last_pushed - st->table->commitupdate) > 0) |
| 1266 | st->table->commitupdate = st->last_pushed; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1267 | /* identifier may not needed in next update message */ |
| 1268 | new_pushed = 0; |
| 1269 | |
| 1270 | eb = eb32_next(eb); |
| 1271 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1272 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1273 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1274 | else { |
| 1275 | if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) { |
| 1276 | struct eb32_node *eb; |
| 1277 | int new_pushed; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1278 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1279 | if (st != curpeer->last_local_table) { |
| 1280 | int msglen; |
| 1281 | |
| 1282 | msglen = peer_prepare_switchmsg(st, trash.str, trash.size); |
| 1283 | if (!msglen) { |
| 1284 | /* internal error: message does not fit in trash */ |
| 1285 | appctx->st0 = PEER_SESS_ST_END; |
| 1286 | goto switchstate; |
| 1287 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1288 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1289 | /* message to buffer */ |
| 1290 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1291 | if (repl <= 0) { |
| 1292 | /* no more write possible */ |
| 1293 | if (repl == -1) { |
| 1294 | goto full; |
| 1295 | } |
| 1296 | appctx->st0 = PEER_SESS_ST_END; |
| 1297 | goto switchstate; |
| 1298 | } |
| 1299 | curpeer->last_local_table = st; |
| 1300 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1301 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1302 | /* We force new pushed to 1 to force identifier in update message */ |
| 1303 | new_pushed = 1; |
| 1304 | eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1); |
| 1305 | while (1) { |
| 1306 | uint32_t msglen; |
| 1307 | struct stksess *ts; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1308 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1309 | /* push local updates */ |
| 1310 | if (!eb) { |
| 1311 | st->flags |= SHTABLE_F_TEACH_STAGE1; |
| 1312 | eb = eb32_first(&st->table->updates); |
| 1313 | if (eb) |
| 1314 | st->last_pushed = eb->key - 1; |
| 1315 | break; |
| 1316 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1317 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1318 | ts = eb32_entry(eb, struct stksess, upd); |
| 1319 | msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed); |
| 1320 | if (!msglen) { |
| 1321 | /* internal error: message does not fit in trash */ |
| 1322 | appctx->st0 = PEER_SESS_ST_END; |
| 1323 | goto switchstate; |
| 1324 | } |
| 1325 | |
| 1326 | /* message to buffer */ |
| 1327 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1328 | if (repl <= 0) { |
| 1329 | /* no more write possible */ |
| 1330 | if (repl == -1) { |
| 1331 | goto full; |
| 1332 | } |
| 1333 | appctx->st0 = PEER_SESS_ST_END; |
| 1334 | goto switchstate; |
| 1335 | } |
| 1336 | st->last_pushed = ts->upd.key; |
| 1337 | /* identifier may not needed in next update message */ |
| 1338 | new_pushed = 0; |
| 1339 | |
| 1340 | eb = eb32_next(eb); |
| 1341 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1342 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1343 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1344 | if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) { |
| 1345 | struct eb32_node *eb; |
| 1346 | int new_pushed; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1347 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1348 | if (st != curpeer->last_local_table) { |
| 1349 | int msglen; |
| 1350 | |
| 1351 | msglen = peer_prepare_switchmsg(st, trash.str, trash.size); |
| 1352 | if (!msglen) { |
| 1353 | /* internal error: message does not fit in trash */ |
| 1354 | appctx->st0 = PEER_SESS_ST_END; |
| 1355 | goto switchstate; |
| 1356 | } |
| 1357 | |
| 1358 | /* message to buffer */ |
| 1359 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1360 | if (repl <= 0) { |
| 1361 | /* no more write possible */ |
| 1362 | if (repl == -1) { |
| 1363 | goto full; |
| 1364 | } |
| 1365 | appctx->st0 = PEER_SESS_ST_END; |
| 1366 | goto switchstate; |
| 1367 | } |
| 1368 | curpeer->last_local_table = st; |
| 1369 | } |
| 1370 | |
| 1371 | /* We force new pushed to 1 to force identifier in update message */ |
| 1372 | new_pushed = 1; |
| 1373 | eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1); |
| 1374 | while (1) { |
| 1375 | uint32_t msglen; |
| 1376 | struct stksess *ts; |
| 1377 | |
| 1378 | /* push local updates */ |
| 1379 | if (!eb || eb->key > st->teaching_origin) { |
| 1380 | st->flags |= SHTABLE_F_TEACH_STAGE2; |
| 1381 | eb = eb32_first(&st->table->updates); |
| 1382 | if (eb) |
| 1383 | st->last_pushed = eb->key - 1; |
| 1384 | break; |
| 1385 | } |
| 1386 | |
| 1387 | ts = eb32_entry(eb, struct stksess, upd); |
| 1388 | msglen = peer_prepare_updatemsg(ts, st, trash.str, trash.size, new_pushed); |
| 1389 | if (!msglen) { |
| 1390 | /* internal error: message does not fit in trash */ |
| 1391 | appctx->st0 = PEER_SESS_ST_END; |
| 1392 | goto switchstate; |
| 1393 | } |
| 1394 | |
| 1395 | /* message to buffer */ |
| 1396 | repl = bi_putblk(si_ic(si), trash.str, msglen); |
| 1397 | if (repl <= 0) { |
| 1398 | /* no more write possible */ |
| 1399 | if (repl == -1) { |
| 1400 | goto full; |
| 1401 | } |
| 1402 | appctx->st0 = PEER_SESS_ST_END; |
| 1403 | goto switchstate; |
| 1404 | } |
| 1405 | st->last_pushed = ts->upd.key; |
| 1406 | /* identifier may not needed in next update message */ |
| 1407 | new_pushed = 0; |
| 1408 | |
| 1409 | eb = eb32_next(eb); |
| 1410 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1411 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1412 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1413 | |
| 1414 | if (st == last_local_table) |
| 1415 | break; |
| 1416 | st = st->next; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1417 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | |
| 1421 | if ((curpeer->flags & PEER_F_TEACH_PROCESS) && !(curpeer->flags & PEER_F_TEACH_FINISHED)) { |
| 1422 | unsigned char msg[2]; |
| 1423 | |
| 1424 | /* Current peer was elected to request a resync */ |
| 1425 | msg[0] = PEER_MSG_CLASS_CONTROL; |
| 1426 | msg[1] = ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED) ? PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL; |
| 1427 | /* process final lesson message */ |
| 1428 | repl = bi_putblk(si_ic(si), (char *)msg, sizeof(msg)); |
| 1429 | if (repl <= 0) { |
| 1430 | /* no more write possible */ |
| 1431 | if (repl == -1) |
| 1432 | goto full; |
| 1433 | appctx->st0 = PEER_SESS_ST_END; |
| 1434 | goto switchstate; |
| 1435 | } |
| 1436 | /* flag finished message sent */ |
| 1437 | curpeer->flags |= PEER_F_TEACH_FINISHED; |
| 1438 | } |
| 1439 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1440 | /* noting more to do */ |
| 1441 | goto out; |
| 1442 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1443 | case PEER_SESS_ST_EXIT: |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1444 | repl = snprintf(trash.str, trash.size, "%d\n", appctx->st1); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1445 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1446 | if (bi_putblk(si_ic(si), trash.str, repl) == -1) |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1447 | goto full; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1448 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1449 | goto switchstate; |
| 1450 | case PEER_SESS_ST_ERRSIZE: { |
| 1451 | unsigned char msg[2]; |
| 1452 | |
| 1453 | msg[0] = PEER_MSG_CLASS_ERROR; |
| 1454 | msg[1] = PEER_MSG_ERR_SIZELIMIT; |
| 1455 | |
| 1456 | if (bi_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1) |
| 1457 | goto full; |
| 1458 | appctx->st0 = PEER_SESS_ST_END; |
| 1459 | goto switchstate; |
| 1460 | } |
| 1461 | case PEER_SESS_ST_ERRPROTO: { |
| 1462 | unsigned char msg[2]; |
| 1463 | |
| 1464 | msg[0] = PEER_MSG_CLASS_ERROR; |
| 1465 | msg[1] = PEER_MSG_ERR_PROTOCOL; |
| 1466 | |
| 1467 | if (bi_putblk(si_ic(si), (char *)msg, sizeof(msg)) == -1) |
| 1468 | goto full; |
| 1469 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1470 | /* fall through */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1471 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1472 | case PEER_SESS_ST_END: { |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1473 | si_shutw(si); |
| 1474 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1475 | si_ic(si)->flags |= CF_READ_NULL; |
Willy Tarreau | 828824a | 2015-04-19 17:20:03 +0200 | [diff] [blame] | 1476 | goto out; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | out: |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1481 | si_oc(si)->flags |= CF_READ_DONTWAIT; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1482 | return; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1483 | full: |
Willy Tarreau | fe12793 | 2015-04-21 19:23:39 +0200 | [diff] [blame] | 1484 | si_applet_cant_put(si); |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1485 | goto out; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1486 | } |
| 1487 | |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 1488 | static struct applet peer_applet = { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1489 | .obj_type = OBJ_TYPE_APPLET, |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 1490 | .name = "<PEER>", /* used for logging */ |
| 1491 | .fct = peer_io_handler, |
Aman Gupta | 9a13e84 | 2012-04-02 18:57:53 -0700 | [diff] [blame] | 1492 | .release = peer_session_release, |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 1493 | }; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1494 | |
| 1495 | /* |
| 1496 | * Use this function to force a close of a peer session |
| 1497 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1498 | static void peer_session_forceshutdown(struct stream * stream) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1499 | { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1500 | struct appctx *appctx = NULL; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1501 | struct peer *ps; |
| 1502 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1503 | int i; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1504 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1505 | for (i = 0; i <= 1; i++) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1506 | appctx = objt_appctx(stream->si[i].end); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1507 | if (!appctx) |
| 1508 | continue; |
| 1509 | if (appctx->applet != &peer_applet) |
| 1510 | continue; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1511 | break; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1512 | } |
| 1513 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1514 | if (!appctx) |
| 1515 | return; |
| 1516 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1517 | ps = (struct peer *)appctx->ctx.peers.ptr; |
Willy Tarreau | b4e34da | 2015-05-20 10:39:04 +0200 | [diff] [blame] | 1518 | /* we're killing a connection, we must apply a random delay before |
| 1519 | * retrying otherwise the other end will do the same and we can loop |
| 1520 | * for a while. |
| 1521 | */ |
| 1522 | if (ps) |
| 1523 | ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000)); |
| 1524 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1525 | /* call release to reinit resync states if needed */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1526 | peer_session_release(appctx); |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1527 | appctx->st0 = PEER_SESS_ST_END; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1528 | appctx->ctx.peers.ptr = NULL; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1529 | task_wakeup(stream->task, TASK_WOKEN_MSG); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1530 | } |
| 1531 | |
Willy Tarreau | 91d9628 | 2015-03-13 15:47:26 +0100 | [diff] [blame] | 1532 | /* Pre-configures a peers frontend to accept incoming connections */ |
| 1533 | void peers_setup_frontend(struct proxy *fe) |
| 1534 | { |
| 1535 | fe->last_change = now.tv_sec; |
| 1536 | fe->cap = PR_CAP_FE; |
| 1537 | fe->maxconn = 0; |
| 1538 | fe->conn_retries = CONN_RETRIES; |
| 1539 | fe->timeout.client = MS_TO_TICKS(5000); |
Willy Tarreau | d1d48d4 | 2015-03-13 16:15:46 +0100 | [diff] [blame] | 1540 | fe->accept = frontend_accept; |
Willy Tarreau | f87ab94 | 2015-03-13 15:55:16 +0100 | [diff] [blame] | 1541 | fe->default_target = &peer_applet.obj_type; |
Willy Tarreau | 91d9628 | 2015-03-13 15:47:26 +0100 | [diff] [blame] | 1542 | fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC; |
Willy Tarreau | 0fca483 | 2015-05-01 19:12:05 +0200 | [diff] [blame] | 1543 | fe->bind_proc = 0; /* will be filled by users */ |
Willy Tarreau | 91d9628 | 2015-03-13 15:47:26 +0100 | [diff] [blame] | 1544 | } |
| 1545 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1546 | /* |
Willy Tarreau | bd55e31 | 2010-11-11 10:55:09 +0100 | [diff] [blame] | 1547 | * Create a new peer session in assigned state (connect will start automatically) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1548 | */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1549 | static struct stream *peer_session_create(struct peers *peers, struct peer *peer) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1550 | { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1551 | struct listener *l = LIST_NEXT(&peers->peers_fe->conf.listeners, struct listener *, by_fe); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1552 | struct proxy *p = (struct proxy *)l->frontend; /* attached frontend */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1553 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1554 | struct session *sess; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1555 | struct stream *s; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1556 | struct task *t; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 1557 | struct connection *conn; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1558 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1559 | peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000)); |
| 1560 | peer->statuscode = PEER_SESS_SC_CONNECTCODE; |
Willy Tarreau | d990baf | 2015-04-05 00:32:03 +0200 | [diff] [blame] | 1561 | s = NULL; |
| 1562 | |
| 1563 | appctx = appctx_new(&peer_applet); |
| 1564 | if (!appctx) |
| 1565 | goto out_close; |
| 1566 | |
| 1567 | appctx->st0 = PEER_SESS_ST_CONNECT; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1568 | appctx->ctx.peers.ptr = (void *)peer; |
Willy Tarreau | d990baf | 2015-04-05 00:32:03 +0200 | [diff] [blame] | 1569 | |
Willy Tarreau | 4099a7c | 2015-04-05 00:39:55 +0200 | [diff] [blame] | 1570 | sess = session_new(p, l, &appctx->obj_type); |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1571 | if (!sess) { |
Godbach | 430f291 | 2013-06-20 13:28:38 +0800 | [diff] [blame] | 1572 | Alert("out of memory in peer_session_create().\n"); |
Willy Tarreau | d990baf | 2015-04-05 00:32:03 +0200 | [diff] [blame] | 1573 | goto out_free_appctx; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1574 | } |
| 1575 | |
Willy Tarreau | 8baf906 | 2015-04-05 00:46:36 +0200 | [diff] [blame] | 1576 | if ((t = task_new()) == NULL) { |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1577 | Alert("out of memory in peer_session_create().\n"); |
| 1578 | goto out_free_sess; |
| 1579 | } |
Willy Tarreau | 8baf906 | 2015-04-05 00:46:36 +0200 | [diff] [blame] | 1580 | t->nice = l->nice; |
| 1581 | |
Willy Tarreau | 73b65ac | 2015-04-08 18:26:29 +0200 | [diff] [blame] | 1582 | if ((s = stream_new(sess, t, &appctx->obj_type)) == NULL) { |
Willy Tarreau | 342bfb1 | 2015-04-05 01:35:34 +0200 | [diff] [blame] | 1583 | Alert("Failed to initialize stream in peer_session_create().\n"); |
Willy Tarreau | 8baf906 | 2015-04-05 00:46:36 +0200 | [diff] [blame] | 1584 | goto out_free_task; |
| 1585 | } |
| 1586 | |
Willy Tarreau | 342bfb1 | 2015-04-05 01:35:34 +0200 | [diff] [blame] | 1587 | /* The tasks below are normally what is supposed to be done by |
| 1588 | * fe->accept(). |
| 1589 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1590 | s->flags = SF_ASSIGNED|SF_ADDR_SET; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1591 | |
Willy Tarreau | 6e2979c | 2015-04-27 13:21:15 +0200 | [diff] [blame] | 1592 | /* applet is waiting for data */ |
| 1593 | si_applet_cant_get(&s->si[0]); |
| 1594 | appctx_wakeup(appctx); |
| 1595 | |
Willy Tarreau | 3ed35ef | 2013-10-24 11:51:38 +0200 | [diff] [blame] | 1596 | /* initiate an outgoing connection */ |
| 1597 | si_set_state(&s->si[1], SI_ST_ASS); |
Willy Tarreau | 3ed35ef | 2013-10-24 11:51:38 +0200 | [diff] [blame] | 1598 | |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 1599 | /* automatically prepare the stream interface to connect to the |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1600 | * pre-initialized connection in si->conn. |
| 1601 | */ |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 1602 | if (unlikely((conn = conn_new()) == NULL)) |
Willy Tarreau | 8baf906 | 2015-04-05 00:46:36 +0200 | [diff] [blame] | 1603 | goto out_free_strm; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 1604 | |
| 1605 | conn_prepare(conn, peer->proto, peer->xprt); |
| 1606 | si_attach_conn(&s->si[1], conn); |
| 1607 | |
| 1608 | conn->target = s->target = &s->be->obj_type; |
| 1609 | memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to)); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1610 | s->do_log = NULL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1611 | s->uniq_id = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1612 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1613 | s->res.flags |= CF_READ_DONTWAIT; |
Willy Tarreau | 696a291 | 2014-11-24 11:36:57 +0100 | [diff] [blame] | 1614 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1615 | l->nbconn++; /* warning! right now, it's up to the handler to decrease this */ |
| 1616 | p->feconn++;/* beconn will be increased later */ |
| 1617 | jobs++; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1618 | if (!(s->sess->listener->options & LI_O_UNLIMITED)) |
Willy Tarreau | 3c63fd8 | 2011-09-07 18:00:47 +0200 | [diff] [blame] | 1619 | actconn++; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1620 | totalconn++; |
| 1621 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1622 | peer->appctx = appctx; |
| 1623 | peer->stream = s; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1624 | return s; |
| 1625 | |
| 1626 | /* Error unrolling */ |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1627 | out_free_strm: |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1628 | LIST_DEL(&s->list); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1629 | pool_free2(pool2_stream, s); |
Willy Tarreau | 8baf906 | 2015-04-05 00:46:36 +0200 | [diff] [blame] | 1630 | out_free_task: |
| 1631 | task_free(t); |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 1632 | out_free_sess: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 1633 | session_free(sess); |
Willy Tarreau | d990baf | 2015-04-05 00:32:03 +0200 | [diff] [blame] | 1634 | out_free_appctx: |
| 1635 | appctx_free(appctx); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1636 | out_close: |
| 1637 | return s; |
| 1638 | } |
| 1639 | |
| 1640 | /* |
| 1641 | * Task processing function to manage re-connect and peer session |
| 1642 | * tasks wakeup on local update. |
| 1643 | */ |
Simon Horman | 9655377 | 2011-06-08 09:18:51 +0900 | [diff] [blame] | 1644 | static struct task *process_peer_sync(struct task * task) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1645 | { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1646 | struct peers *peers = (struct peers *)task->context; |
| 1647 | struct peer *ps; |
| 1648 | struct shared_table *st; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1649 | |
| 1650 | task->expire = TICK_ETERNITY; |
| 1651 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1652 | if (!peers->peers_fe) { |
Willy Tarreau | 46dc1ca | 2015-05-01 18:32:13 +0200 | [diff] [blame] | 1653 | /* this one was never started, kill it */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1654 | signal_unregister_handler(peers->sighandler); |
| 1655 | peers->sync_task = NULL; |
| 1656 | task_delete(peers->sync_task); |
| 1657 | task_free(peers->sync_task); |
Willy Tarreau | 46dc1ca | 2015-05-01 18:32:13 +0200 | [diff] [blame] | 1658 | return NULL; |
| 1659 | } |
| 1660 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1661 | if (!stopping) { |
| 1662 | /* Normal case (not soft stop)*/ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1663 | |
| 1664 | if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) && |
| 1665 | (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) && |
| 1666 | !(peers->flags & PEERS_F_RESYNC_ASSIGN)) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1667 | /* Resync from local peer needed |
| 1668 | no peer was assigned for the lesson |
| 1669 | and no old local peer found |
| 1670 | or resync timeout expire */ |
| 1671 | |
| 1672 | /* flag no more resync from local, to try resync from remotes */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1673 | peers->flags |= PEERS_F_RESYNC_LOCAL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1674 | |
| 1675 | /* reschedule a resync */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1676 | peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | /* For each session */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1680 | for (ps = peers->remote; ps; ps = ps->next) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1681 | /* For each remote peers */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1682 | if (!ps->local) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1683 | if (!ps->stream) { |
| 1684 | /* no active stream */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1685 | if (ps->statuscode == 0 || |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1686 | ((ps->statuscode == PEER_SESS_SC_CONNECTCODE || |
Willy Tarreau | b4e34da | 2015-05-20 10:39:04 +0200 | [diff] [blame] | 1687 | ps->statuscode == PEER_SESS_SC_SUCCESSCODE || |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1688 | ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) && |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1689 | tick_is_expired(ps->reconnect, now_ms))) { |
| 1690 | /* connection never tried |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1691 | * or previous stream established with success |
| 1692 | * or previous stream failed during connection |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1693 | * and reconnection timer is expired */ |
| 1694 | |
| 1695 | /* retry a connect */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1696 | ps->stream = peer_session_create(peers, ps); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1697 | } |
Willy Tarreau | b4e34da | 2015-05-20 10:39:04 +0200 | [diff] [blame] | 1698 | else if (!tick_is_expired(ps->reconnect, now_ms)) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1699 | /* If previous session failed during connection |
| 1700 | * but reconnection timer is not expired */ |
| 1701 | |
| 1702 | /* reschedule task for reconnect */ |
| 1703 | task->expire = tick_first(task->expire, ps->reconnect); |
| 1704 | } |
| 1705 | /* else do nothing */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1706 | } /* !ps->stream */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1707 | else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1708 | /* current stream is active and established */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1709 | if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) && |
| 1710 | !(peers->flags & PEERS_F_RESYNC_ASSIGN) && |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1711 | !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) { |
| 1712 | /* Resync from a remote is needed |
| 1713 | * and no peer was assigned for lesson |
| 1714 | * and current peer may be up2date */ |
| 1715 | |
| 1716 | /* assign peer for the lesson */ |
| 1717 | ps->flags |= PEER_F_LEARN_ASSIGN; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1718 | peers->flags |= PEERS_F_RESYNC_ASSIGN; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1719 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1720 | /* awake peer stream task to handle a request of resync */ |
Willy Tarreau | e5843b3 | 2015-04-27 18:40:14 +0200 | [diff] [blame] | 1721 | appctx_wakeup(ps->appctx); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1722 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1723 | else { |
| 1724 | /* Awake session if there is data to push */ |
| 1725 | for (st = ps->tables; st ; st = st->next) { |
| 1726 | if ((int)(st->last_pushed - st->table->localupdate) < 0) { |
| 1727 | /* awake peer stream task to push local updates */ |
| 1728 | appctx_wakeup(ps->appctx); |
| 1729 | break; |
| 1730 | } |
| 1731 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1732 | } |
| 1733 | /* else do nothing */ |
| 1734 | } /* SUCCESSCODE */ |
| 1735 | } /* !ps->peer->local */ |
| 1736 | } /* for */ |
| 1737 | |
| 1738 | /* Resync from remotes expired: consider resync is finished */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1739 | if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) && |
| 1740 | !(peers->flags & PEERS_F_RESYNC_ASSIGN) && |
| 1741 | tick_is_expired(peers->resync_timeout, now_ms)) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1742 | /* Resync from remote peer needed |
| 1743 | * no peer was assigned for the lesson |
| 1744 | * and resync timeout expire */ |
| 1745 | |
| 1746 | /* flag no more resync from remote, consider resync is finished */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1747 | peers->flags |= PEERS_F_RESYNC_REMOTE; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1748 | } |
| 1749 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1750 | if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1751 | /* Resync not finished*/ |
| 1752 | /* reschedule task to resync timeout, to ended resync if needed */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1753 | task->expire = tick_first(task->expire, peers->resync_timeout); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1754 | } |
| 1755 | } /* !stopping */ |
| 1756 | else { |
| 1757 | /* soft stop case */ |
| 1758 | if (task->state & TASK_WOKEN_SIGNAL) { |
| 1759 | /* We've just recieved the signal */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1760 | if (!(peers->flags & PEERS_F_DONOTSTOP)) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1761 | /* add DO NOT STOP flag if not present */ |
| 1762 | jobs++; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1763 | peers->flags |= PEERS_F_DONOTSTOP; |
| 1764 | ps = peers->local; |
| 1765 | for (st = ps->tables; st ; st = st->next) |
| 1766 | st->table->syncing++; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | /* disconnect all connected peers */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1770 | for (ps = peers->remote; ps; ps = ps->next) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1771 | if (ps->stream) { |
| 1772 | peer_session_forceshutdown(ps->stream); |
| 1773 | ps->stream = NULL; |
Willy Tarreau | e5843b3 | 2015-04-27 18:40:14 +0200 | [diff] [blame] | 1774 | ps->appctx = NULL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1775 | } |
| 1776 | } |
| 1777 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1778 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1779 | ps = peers->local; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1780 | if (ps->flags & PEER_F_TEACH_COMPLETE) { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1781 | if (peers->flags & PEERS_F_DONOTSTOP) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1782 | /* resync of new process was complete, current process can die now */ |
| 1783 | jobs--; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1784 | peers->flags &= ~PEERS_F_DONOTSTOP; |
| 1785 | for (st = ps->tables; st ; st = st->next) |
| 1786 | st->table->syncing--; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1787 | } |
| 1788 | } |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1789 | else if (!ps->stream) { |
| 1790 | /* If stream is not active */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1791 | if (ps->statuscode == 0 || |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1792 | ps->statuscode == PEER_SESS_SC_SUCCESSCODE || |
| 1793 | ps->statuscode == PEER_SESS_SC_CONNECTEDCODE || |
| 1794 | ps->statuscode == PEER_SESS_SC_TRYAGAIN) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1795 | /* connection never tried |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1796 | * or previous stream was successfully established |
| 1797 | * or previous stream tcp connect success but init state incomplete |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1798 | * or during previous connect, peer replies a try again statuscode */ |
| 1799 | |
| 1800 | /* connect to the peer */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1801 | peer_session_create(peers, ps); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1802 | } |
| 1803 | else { |
| 1804 | /* Other error cases */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1805 | if (peers->flags & PEERS_F_DONOTSTOP) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1806 | /* unable to resync new process, current process can die now */ |
| 1807 | jobs--; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1808 | peers->flags &= ~PEERS_F_DONOTSTOP; |
| 1809 | for (st = ps->tables; st ; st = st->next) |
| 1810 | st->table->syncing--; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1811 | } |
| 1812 | } |
| 1813 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1814 | else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1815 | /* current stream active and established |
| 1816 | awake stream to push remaining local updates */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1817 | for (st = ps->tables; st ; st = st->next) { |
| 1818 | if ((int)(st->last_pushed - st->table->localupdate) < 0) { |
| 1819 | /* awake peer stream task to push local updates */ |
| 1820 | appctx_wakeup(ps->appctx); |
| 1821 | break; |
| 1822 | } |
| 1823 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1824 | } |
| 1825 | } /* stopping */ |
| 1826 | /* Wakeup for re-connect */ |
| 1827 | return task; |
| 1828 | } |
| 1829 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1830 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1831 | /* |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1832 | * |
| 1833 | */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1834 | void peers_init_sync(struct peers *peers) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1835 | { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1836 | struct peer * curpeer; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1837 | struct listener *listener; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1838 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1839 | for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1840 | peers->peers_fe->maxconn += 3; |
| 1841 | } |
| 1842 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1843 | list_for_each_entry(listener, &peers->peers_fe->conf.listeners, by_fe) |
| 1844 | listener->maxconn = peers->peers_fe->maxconn; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 1845 | peers->sync_task = task_new(); |
| 1846 | peers->sync_task->process = process_peer_sync; |
| 1847 | peers->sync_task->expire = TICK_ETERNITY; |
| 1848 | peers->sync_task->context = (void *)peers; |
| 1849 | peers->sighandler = signal_register_task(0, peers->sync_task, 0); |
| 1850 | task_wakeup(peers->sync_task, TASK_WOKEN_INIT); |
| 1851 | } |
| 1852 | |
| 1853 | |
| 1854 | |
| 1855 | /* |
| 1856 | * Function used to register a table for sync on a group of peers |
| 1857 | * |
| 1858 | */ |
| 1859 | void peers_register_table(struct peers *peers, struct stktable *table) |
| 1860 | { |
| 1861 | struct shared_table *st; |
| 1862 | struct peer * curpeer; |
| 1863 | int id = 0; |
| 1864 | |
| 1865 | for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) { |
| 1866 | st = (struct shared_table *)calloc(1,sizeof(struct shared_table)); |
| 1867 | st->table = table; |
| 1868 | st->next = curpeer->tables; |
| 1869 | if (curpeer->tables) |
| 1870 | id = curpeer->tables->local_id; |
| 1871 | st->local_id = id + 1; |
| 1872 | |
| 1873 | curpeer->tables = st; |
| 1874 | } |
| 1875 | |
| 1876 | table->sync_task = peers->sync_task; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1877 | } |
| 1878 | |