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> |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 26 | #include <common/standard.h> |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 27 | #include <common/hathreads.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 28 | |
| 29 | #include <types/global.h> |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 30 | #include <types/listener.h> |
| 31 | #include <types/obj_type.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 32 | #include <types/peers.h> |
| 33 | |
| 34 | #include <proto/acl.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 35 | #include <proto/applet.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 36 | #include <proto/channel.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 37 | #include <proto/fd.h> |
Willy Tarreau | d1d48d4 | 2015-03-13 16:15:46 +0100 | [diff] [blame] | 38 | #include <proto/frontend.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 39 | #include <proto/log.h> |
| 40 | #include <proto/hdr_idx.h> |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 41 | #include <proto/mux_pt.h> |
Frédéric Lécaille | 1055e68 | 2018-04-26 14:35:21 +0200 | [diff] [blame] | 42 | #include <proto/peers.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 43 | #include <proto/proxy.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 44 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 45 | #include <proto/stream.h> |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 46 | #include <proto/signal.h> |
| 47 | #include <proto/stick_table.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 48 | #include <proto/stream_interface.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 49 | #include <proto/task.h> |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 50 | |
| 51 | |
| 52 | /*******************************/ |
| 53 | /* Current peer learning state */ |
| 54 | /*******************************/ |
| 55 | |
| 56 | /******************************/ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 57 | /* Current peers section resync state */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 58 | /******************************/ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 59 | #define PEERS_F_RESYNC_LOCAL 0x00000001 /* Learn from local finished or no more needed */ |
| 60 | #define PEERS_F_RESYNC_REMOTE 0x00000002 /* Learn from remote finished or no more needed */ |
| 61 | #define PEERS_F_RESYNC_ASSIGN 0x00000004 /* A peer was assigned to learn our lesson */ |
| 62 | #define PEERS_F_RESYNC_PROCESS 0x00000008 /* The assigned peer was requested for resync */ |
| 63 | #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] | 64 | to push data to new process */ |
| 65 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 66 | #define PEERS_RESYNC_STATEMASK (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE) |
| 67 | #define PEERS_RESYNC_FROMLOCAL 0x00000000 |
| 68 | #define PEERS_RESYNC_FROMREMOTE PEERS_F_RESYNC_LOCAL |
| 69 | #define PEERS_RESYNC_FINISHED (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE) |
| 70 | |
| 71 | /***********************************/ |
| 72 | /* Current shared table sync state */ |
| 73 | /***********************************/ |
| 74 | #define SHTABLE_F_TEACH_STAGE1 0x00000001 /* Teach state 1 complete */ |
| 75 | #define SHTABLE_F_TEACH_STAGE2 0x00000002 /* Teach state 2 complete */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 76 | |
| 77 | /******************************/ |
| 78 | /* Remote peer teaching state */ |
| 79 | /******************************/ |
| 80 | #define PEER_F_TEACH_PROCESS 0x00000001 /* Teach a lesson to current peer */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 81 | #define PEER_F_TEACH_FINISHED 0x00000008 /* Teach conclude, (wait for confirm) */ |
| 82 | #define PEER_F_TEACH_COMPLETE 0x00000010 /* All that we know already taught to current peer, used only for a local peer */ |
| 83 | #define PEER_F_LEARN_ASSIGN 0x00000100 /* Current peer was assigned for a lesson */ |
| 84 | #define PEER_F_LEARN_NOTUP2DATE 0x00000200 /* Learn from peer finished but peer is not up to date */ |
Frédéric Lécaille | 645635d | 2019-02-11 17:49:39 +0100 | [diff] [blame] | 85 | #define PEER_F_HEARTBEAT 0x40000000 /* Heartbeat message to send. */ |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 86 | #define PEER_F_DWNGRD 0x80000000 /* When this flag is enabled, we must downgrade the supported version announced during peer sessions. */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 87 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 88 | #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] | 89 | #define PEER_LEARN_RESET ~(PEER_F_LEARN_ASSIGN|PEER_F_LEARN_NOTUP2DATE) |
| 90 | |
Frédéric Lécaille | 645635d | 2019-02-11 17:49:39 +0100 | [diff] [blame] | 91 | #define PEER_HEARTBEAT_TIMEOUT 3000 /* 3 seconds */ |
| 92 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 93 | /*****************************/ |
| 94 | /* Sync message class */ |
| 95 | /*****************************/ |
| 96 | enum { |
| 97 | PEER_MSG_CLASS_CONTROL = 0, |
| 98 | PEER_MSG_CLASS_ERROR, |
| 99 | PEER_MSG_CLASS_STICKTABLE = 10, |
| 100 | PEER_MSG_CLASS_RESERVED = 255, |
| 101 | }; |
| 102 | |
| 103 | /*****************************/ |
| 104 | /* control message types */ |
| 105 | /*****************************/ |
| 106 | enum { |
| 107 | PEER_MSG_CTRL_RESYNCREQ = 0, |
| 108 | PEER_MSG_CTRL_RESYNCFINISHED, |
| 109 | PEER_MSG_CTRL_RESYNCPARTIAL, |
| 110 | PEER_MSG_CTRL_RESYNCCONFIRM, |
Frédéric Lécaille | 645635d | 2019-02-11 17:49:39 +0100 | [diff] [blame] | 111 | PEER_MSG_CTRL_HEARTBEAT, |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | /*****************************/ |
| 115 | /* error message types */ |
| 116 | /*****************************/ |
| 117 | enum { |
| 118 | PEER_MSG_ERR_PROTOCOL = 0, |
| 119 | PEER_MSG_ERR_SIZELIMIT, |
| 120 | }; |
| 121 | |
Frédéric Lécaille | d5fe14b | 2019-01-24 10:33:40 +0100 | [diff] [blame] | 122 | /* |
| 123 | * Parameters used by functions to build peer protocol messages. */ |
| 124 | struct peer_prep_params { |
| 125 | struct { |
Frédéric Lécaille | 7d0ceee | 2019-01-24 14:24:05 +0100 | [diff] [blame] | 126 | struct peer *peer; |
| 127 | } hello; |
| 128 | struct { |
| 129 | unsigned int st1; |
| 130 | } error_status; |
| 131 | struct { |
Frédéric Lécaille | d5fe14b | 2019-01-24 10:33:40 +0100 | [diff] [blame] | 132 | struct stksess *stksess; |
| 133 | struct shared_table *shared_table; |
| 134 | unsigned int updateid; |
| 135 | int use_identifier; |
| 136 | int use_timed; |
| 137 | } updt; |
| 138 | struct { |
| 139 | struct shared_table *shared_table; |
| 140 | } swtch; |
| 141 | struct { |
| 142 | struct shared_table *shared_table; |
| 143 | } ack; |
Frédéric Lécaille | 7d0ceee | 2019-01-24 14:24:05 +0100 | [diff] [blame] | 144 | struct { |
| 145 | unsigned char head[2]; |
| 146 | } control; |
| 147 | struct { |
| 148 | unsigned char head[2]; |
| 149 | } error; |
Frédéric Lécaille | d5fe14b | 2019-01-24 10:33:40 +0100 | [diff] [blame] | 150 | }; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 151 | |
| 152 | /*******************************/ |
| 153 | /* stick table sync mesg types */ |
| 154 | /* Note: ids >= 128 contains */ |
| 155 | /* id message cotains data */ |
| 156 | /*******************************/ |
Olivier Houchard | 3399226 | 2018-10-16 18:49:26 +0200 | [diff] [blame] | 157 | #define PEER_MSG_STKT_UPDATE 0x80 |
| 158 | #define PEER_MSG_STKT_INCUPDATE 0x81 |
| 159 | #define PEER_MSG_STKT_DEFINE 0x82 |
| 160 | #define PEER_MSG_STKT_SWITCH 0x83 |
| 161 | #define PEER_MSG_STKT_ACK 0x84 |
| 162 | #define PEER_MSG_STKT_UPDATE_TIMED 0x85 |
| 163 | #define PEER_MSG_STKT_INCUPDATE_TIMED 0x86 |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 164 | |
| 165 | /**********************************/ |
| 166 | /* Peer Session IO handler states */ |
| 167 | /**********************************/ |
| 168 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 169 | enum { |
| 170 | PEER_SESS_ST_ACCEPT = 0, /* Initial state for session create by an accept, must be zero! */ |
| 171 | PEER_SESS_ST_GETVERSION, /* Validate supported protocol version */ |
| 172 | PEER_SESS_ST_GETHOST, /* Validate host ID correspond to local host id */ |
| 173 | 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] | 174 | /* after this point, data were possibly exchanged */ |
| 175 | PEER_SESS_ST_SENDSUCCESS, /* Send ret code 200 (success) and wait for message */ |
| 176 | PEER_SESS_ST_CONNECT, /* Initial state for session create on a connect, push presentation into buffer */ |
| 177 | PEER_SESS_ST_GETSTATUS, /* Wait for the welcome message */ |
| 178 | PEER_SESS_ST_WAITMSG, /* Wait for data messages */ |
| 179 | PEER_SESS_ST_EXIT, /* Exit with status code */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 180 | PEER_SESS_ST_ERRPROTO, /* Send error proto message before exit */ |
| 181 | PEER_SESS_ST_ERRSIZE, /* Send error size message before exit */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 182 | PEER_SESS_ST_END, /* Killed session */ |
| 183 | }; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 184 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 185 | /***************************************************/ |
| 186 | /* Peer Session status code - part of the protocol */ |
| 187 | /***************************************************/ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 188 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 189 | #define PEER_SESS_SC_CONNECTCODE 100 /* connect in progress */ |
| 190 | #define PEER_SESS_SC_CONNECTEDCODE 110 /* tcp connect success */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 191 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 192 | #define PEER_SESS_SC_SUCCESSCODE 200 /* accept or connect successful */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 193 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 194 | #define PEER_SESS_SC_TRYAGAIN 300 /* try again later */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 195 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 196 | #define PEER_SESS_SC_ERRPROTO 501 /* error protocol */ |
| 197 | #define PEER_SESS_SC_ERRVERSION 502 /* unknown protocol version */ |
| 198 | #define PEER_SESS_SC_ERRHOST 503 /* bad host name */ |
| 199 | #define PEER_SESS_SC_ERRPEER 504 /* unknown peer */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 200 | |
| 201 | #define PEER_SESSION_PROTO_NAME "HAProxyS" |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 202 | #define PEER_MAJOR_VER 2 |
| 203 | #define PEER_MINOR_VER 1 |
| 204 | #define PEER_DWNGRD_MINOR_VER 0 |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 205 | |
Willy Tarreau | 6254a92 | 2019-01-29 17:45:23 +0100 | [diff] [blame] | 206 | static size_t proto_len = sizeof(PEER_SESSION_PROTO_NAME) - 1; |
Frédéric Lécaille | ed2b4a6 | 2017-07-13 09:07:09 +0200 | [diff] [blame] | 207 | struct peers *cfg_peers = NULL; |
Willy Tarreau | 81bc3b0 | 2016-10-31 17:37:39 +0100 | [diff] [blame] | 208 | static void peer_session_forceshutdown(struct appctx *appctx); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 209 | |
Emeric Brun | 18928af | 2017-03-29 16:32:53 +0200 | [diff] [blame] | 210 | /* This function encode an uint64 to 'dynamic' length format. |
| 211 | The encoded value is written at address *str, and the |
| 212 | caller must assure that size after *str is large enought. |
| 213 | At return, the *str is set at the next Byte after then |
| 214 | encoded integer. The function returns then length of the |
| 215 | encoded integer in Bytes */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 216 | int intencode(uint64_t i, char **str) { |
| 217 | int idx = 0; |
| 218 | unsigned char *msg; |
| 219 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 220 | msg = (unsigned char *)*str; |
| 221 | if (i < 240) { |
| 222 | msg[0] = (unsigned char)i; |
| 223 | *str = (char *)&msg[idx+1]; |
| 224 | return (idx+1); |
| 225 | } |
| 226 | |
| 227 | msg[idx] =(unsigned char)i | 240; |
| 228 | i = (i - 240) >> 4; |
| 229 | while (i >= 128) { |
| 230 | msg[++idx] = (unsigned char)i | 128; |
| 231 | i = (i - 128) >> 7; |
| 232 | } |
| 233 | msg[++idx] = (unsigned char)i; |
| 234 | *str = (char *)&msg[idx+1]; |
| 235 | return (idx+1); |
| 236 | } |
| 237 | |
| 238 | |
| 239 | /* This function returns the decoded integer or 0 |
| 240 | if decode failed |
| 241 | *str point on the beginning of the integer to decode |
| 242 | at the end of decoding *str point on the end of the |
| 243 | encoded integer or to null if end is reached */ |
Emeric Brun | 18928af | 2017-03-29 16:32:53 +0200 | [diff] [blame] | 244 | uint64_t intdecode(char **str, char *end) |
| 245 | { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 246 | unsigned char *msg; |
Emeric Brun | 18928af | 2017-03-29 16:32:53 +0200 | [diff] [blame] | 247 | uint64_t i; |
| 248 | int shift; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 249 | |
| 250 | if (!*str) |
| 251 | return 0; |
| 252 | |
| 253 | msg = (unsigned char *)*str; |
Emeric Brun | 18928af | 2017-03-29 16:32:53 +0200 | [diff] [blame] | 254 | if (msg >= (unsigned char *)end) |
| 255 | goto fail; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 256 | |
Emeric Brun | 18928af | 2017-03-29 16:32:53 +0200 | [diff] [blame] | 257 | i = *(msg++); |
| 258 | if (i >= 240) { |
| 259 | shift = 4; |
| 260 | do { |
| 261 | if (msg >= (unsigned char *)end) |
| 262 | goto fail; |
| 263 | i += (uint64_t)*msg << shift; |
| 264 | shift += 7; |
| 265 | } while (*(msg++) >= 128); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 266 | } |
Frédéric Lécaille | a8725ec | 2019-01-22 10:31:39 +0100 | [diff] [blame] | 267 | *str = (char *)msg; |
| 268 | return i; |
Emeric Brun | 18928af | 2017-03-29 16:32:53 +0200 | [diff] [blame] | 269 | |
Frédéric Lécaille | a8725ec | 2019-01-22 10:31:39 +0100 | [diff] [blame] | 270 | fail: |
| 271 | *str = NULL; |
| 272 | return 0; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 273 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 274 | |
Frédéric Lécaille | 7d0ceee | 2019-01-24 14:24:05 +0100 | [diff] [blame] | 275 | /* |
| 276 | * Build a "hello" peer protocol message. |
| 277 | * Return the number of written bytes written to build this messages if succeeded, |
| 278 | * 0 if not. |
| 279 | */ |
| 280 | static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params *p) |
| 281 | { |
| 282 | int min_ver, ret; |
| 283 | struct peer *peer; |
| 284 | |
| 285 | peer = p->hello.peer; |
| 286 | min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER; |
| 287 | /* Prepare headers */ |
| 288 | ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n", |
| 289 | PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), relative_pid); |
| 290 | if (ret >= size) |
| 291 | return 0; |
| 292 | |
| 293 | return ret; |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Build a "handshake succeeded" status message. |
| 298 | * Return the number of written bytes written to build this messages if succeeded, |
| 299 | * 0 if not. |
| 300 | */ |
| 301 | static int peer_prepare_status_successmsg(char *msg, size_t size, struct peer_prep_params *p) |
| 302 | { |
| 303 | int ret; |
| 304 | |
| 305 | ret = snprintf(msg, size, "%d\n", PEER_SESS_SC_SUCCESSCODE); |
| 306 | if (ret >= size) |
| 307 | return 0; |
| 308 | |
| 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Build an error status message. |
| 314 | * Return the number of written bytes written to build this messages if succeeded, |
| 315 | * 0 if not. |
| 316 | */ |
| 317 | static int peer_prepare_status_errormsg(char *msg, size_t size, struct peer_prep_params *p) |
| 318 | { |
| 319 | int ret; |
| 320 | unsigned int st1; |
| 321 | |
| 322 | st1 = p->error_status.st1; |
| 323 | ret = snprintf(msg, size, "%d\n", st1); |
| 324 | if (ret >= size) |
| 325 | return 0; |
| 326 | |
| 327 | return ret; |
| 328 | } |
| 329 | |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 330 | /* Set the stick-table UPDATE message type byte at <msg_type> address, |
| 331 | * depending on <use_identifier> and <use_timed> boolean parameters. |
| 332 | * Always successful. |
| 333 | */ |
| 334 | static inline void peer_set_update_msg_type(char *msg_type, int use_identifier, int use_timed) |
| 335 | { |
| 336 | if (use_timed) { |
| 337 | if (use_identifier) |
| 338 | *msg_type = PEER_MSG_STKT_UPDATE_TIMED; |
| 339 | else |
| 340 | *msg_type = PEER_MSG_STKT_INCUPDATE_TIMED; |
| 341 | } |
| 342 | else { |
| 343 | if (use_identifier) |
| 344 | *msg_type = PEER_MSG_STKT_UPDATE; |
| 345 | else |
| 346 | *msg_type = PEER_MSG_STKT_INCUPDATE; |
| 347 | } |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 348 | } |
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 | * This prepare the data update message on the stick session <ts>, <st> is the considered |
| 351 | * stick table. |
Joseph Herlant | 82b2f54 | 2018-11-15 12:19:14 -0800 | [diff] [blame] | 352 | * <msg> is a buffer of <size> to receive data message content |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 353 | * If function returns 0, the caller should consider we were unable to encode this message (TODO: |
| 354 | * check size) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 355 | */ |
Frédéric Lécaille | d5fe14b | 2019-01-24 10:33:40 +0100 | [diff] [blame] | 356 | static int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 357 | { |
| 358 | uint32_t netinteger; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 359 | unsigned short datalen; |
| 360 | char *cursor, *datamsg; |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 361 | unsigned int data_type; |
| 362 | void *data_ptr; |
Frédéric Lécaille | d5fe14b | 2019-01-24 10:33:40 +0100 | [diff] [blame] | 363 | struct stksess *ts; |
| 364 | struct shared_table *st; |
| 365 | unsigned int updateid; |
| 366 | int use_identifier; |
| 367 | int use_timed; |
| 368 | |
| 369 | ts = p->updt.stksess; |
| 370 | st = p->updt.shared_table; |
| 371 | updateid = p->updt.updateid; |
| 372 | use_identifier = p->updt.use_identifier; |
| 373 | use_timed = p->updt.use_timed; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 374 | |
| 375 | cursor = datamsg = msg + 1 + 5; |
| 376 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 377 | /* construct message */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 378 | |
| 379 | /* check if we need to send the update identifer */ |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 380 | if (!st->last_pushed || updateid < st->last_pushed || ((updateid - st->last_pushed) != 1)) { |
Emeric Brun | a6a0998 | 2015-09-22 15:34:19 +0200 | [diff] [blame] | 381 | use_identifier = 1; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 382 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 383 | |
| 384 | /* encode update identifier if needed */ |
| 385 | if (use_identifier) { |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 386 | netinteger = htonl(updateid); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 387 | memcpy(cursor, &netinteger, sizeof(netinteger)); |
| 388 | cursor += sizeof(netinteger); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 389 | } |
| 390 | |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 391 | if (use_timed) { |
| 392 | netinteger = htonl(tick_remain(now_ms, ts->expire)); |
| 393 | memcpy(cursor, &netinteger, sizeof(netinteger)); |
| 394 | cursor += sizeof(netinteger); |
| 395 | } |
| 396 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 397 | /* encode the key */ |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 398 | if (st->table->type == SMP_T_STR) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 399 | int stlen = strlen((char *)ts->key.key); |
| 400 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 401 | intencode(stlen, &cursor); |
| 402 | memcpy(cursor, ts->key.key, stlen); |
| 403 | cursor += stlen; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 404 | } |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 405 | else if (st->table->type == SMP_T_SINT) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 406 | netinteger = htonl(*((uint32_t *)ts->key.key)); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 407 | memcpy(cursor, &netinteger, sizeof(netinteger)); |
| 408 | cursor += sizeof(netinteger); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 409 | } |
| 410 | else { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 411 | memcpy(cursor, ts->key.key, st->table->key_size); |
| 412 | cursor += st->table->key_size; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 413 | } |
| 414 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 415 | HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 416 | /* encode values */ |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 417 | for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 418 | |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 419 | data_ptr = stktable_data_ptr(st->table, ts, data_type); |
| 420 | if (data_ptr) { |
| 421 | switch (stktable_data_types[data_type].std_type) { |
| 422 | case STD_T_SINT: { |
| 423 | int data; |
| 424 | |
| 425 | data = stktable_data_cast(data_ptr, std_t_sint); |
| 426 | intencode(data, &cursor); |
| 427 | break; |
| 428 | } |
| 429 | case STD_T_UINT: { |
| 430 | unsigned int data; |
| 431 | |
| 432 | data = stktable_data_cast(data_ptr, std_t_uint); |
| 433 | intencode(data, &cursor); |
| 434 | break; |
| 435 | } |
| 436 | case STD_T_ULL: { |
| 437 | unsigned long long data; |
| 438 | |
| 439 | data = stktable_data_cast(data_ptr, std_t_ull); |
| 440 | intencode(data, &cursor); |
| 441 | break; |
| 442 | } |
| 443 | case STD_T_FRQP: { |
| 444 | struct freq_ctr_period *frqp; |
| 445 | |
| 446 | frqp = &stktable_data_cast(data_ptr, std_t_frqp); |
| 447 | intencode((unsigned int)(now_ms - frqp->curr_tick), &cursor); |
| 448 | intencode(frqp->curr_ctr, &cursor); |
| 449 | intencode(frqp->prev_ctr, &cursor); |
| 450 | break; |
| 451 | } |
| 452 | } |
| 453 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 454 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 455 | HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 456 | |
| 457 | /* Compute datalen */ |
| 458 | datalen = (cursor - datamsg); |
| 459 | |
| 460 | /* prepare message header */ |
| 461 | msg[0] = PEER_MSG_CLASS_STICKTABLE; |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 462 | peer_set_update_msg_type(&msg[1], use_identifier, use_timed); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 463 | cursor = &msg[2]; |
| 464 | intencode(datalen, &cursor); |
| 465 | |
| 466 | /* move data after header */ |
| 467 | memmove(cursor, datamsg, datalen); |
| 468 | |
| 469 | /* return header size + data_len */ |
| 470 | return (cursor - msg) + datalen; |
| 471 | } |
| 472 | |
| 473 | /* |
| 474 | * This prepare the switch table message to targeted share table <st>. |
Joseph Herlant | 82b2f54 | 2018-11-15 12:19:14 -0800 | [diff] [blame] | 475 | * <msg> is a buffer of <size> to receive data message content |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 476 | * If function returns 0, the caller should consider we were unable to encode this message (TODO: |
| 477 | * check size) |
| 478 | */ |
Frédéric Lécaille | d5fe14b | 2019-01-24 10:33:40 +0100 | [diff] [blame] | 479 | static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_params *params) |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 480 | { |
| 481 | int len; |
| 482 | unsigned short datalen; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 483 | struct buffer *chunk; |
Frédéric Lécaille | 37a7254 | 2017-07-06 15:02:16 +0200 | [diff] [blame] | 484 | char *cursor, *datamsg, *chunkp, *chunkq; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 485 | uint64_t data = 0; |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 486 | unsigned int data_type; |
Frédéric Lécaille | d5fe14b | 2019-01-24 10:33:40 +0100 | [diff] [blame] | 487 | struct shared_table *st; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 488 | |
Frédéric Lécaille | d5fe14b | 2019-01-24 10:33:40 +0100 | [diff] [blame] | 489 | st = params->swtch.shared_table; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 490 | cursor = datamsg = msg + 2 + 5; |
| 491 | |
| 492 | /* Encode data */ |
| 493 | |
| 494 | /* encode local id */ |
| 495 | intencode(st->local_id, &cursor); |
| 496 | |
| 497 | /* encode table name */ |
| 498 | len = strlen(st->table->id); |
| 499 | intencode(len, &cursor); |
| 500 | memcpy(cursor, st->table->id, len); |
| 501 | cursor += len; |
| 502 | |
| 503 | /* encode table type */ |
| 504 | |
| 505 | intencode(st->table->type, &cursor); |
| 506 | |
| 507 | /* encode table key size */ |
| 508 | intencode(st->table->key_size, &cursor); |
| 509 | |
Frédéric Lécaille | 37a7254 | 2017-07-06 15:02:16 +0200 | [diff] [blame] | 510 | chunk = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 511 | chunkp = chunkq = chunk->area; |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 512 | /* encode available known data types in table */ |
| 513 | for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) { |
| 514 | if (st->table->data_ofs[data_type]) { |
| 515 | switch (stktable_data_types[data_type].std_type) { |
| 516 | case STD_T_SINT: |
| 517 | case STD_T_UINT: |
| 518 | case STD_T_ULL: |
Frédéric Lécaille | 37a7254 | 2017-07-06 15:02:16 +0200 | [diff] [blame] | 519 | data |= 1 << data_type; |
| 520 | break; |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 521 | case STD_T_FRQP: |
| 522 | data |= 1 << data_type; |
Frédéric Lécaille | 37a7254 | 2017-07-06 15:02:16 +0200 | [diff] [blame] | 523 | intencode(data_type, &chunkq); |
| 524 | intencode(st->table->data_arg[data_type].u, &chunkq); |
Emeric Brun | 9490095 | 2015-06-11 18:25:54 +0200 | [diff] [blame] | 525 | break; |
| 526 | } |
| 527 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 528 | } |
| 529 | intencode(data, &cursor); |
| 530 | |
Frédéric Lécaille | 37a7254 | 2017-07-06 15:02:16 +0200 | [diff] [blame] | 531 | /* Encode stick-table entries duration. */ |
| 532 | intencode(st->table->expire, &cursor); |
| 533 | |
| 534 | if (chunkq > chunkp) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 535 | chunk->data = chunkq - chunkp; |
| 536 | memcpy(cursor, chunk->area, chunk->data); |
| 537 | cursor += chunk->data; |
Frédéric Lécaille | 37a7254 | 2017-07-06 15:02:16 +0200 | [diff] [blame] | 538 | } |
| 539 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 540 | /* Compute datalen */ |
| 541 | datalen = (cursor - datamsg); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 542 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 543 | /* prepare message header */ |
| 544 | msg[0] = PEER_MSG_CLASS_STICKTABLE; |
| 545 | msg[1] = PEER_MSG_STKT_DEFINE; |
| 546 | cursor = &msg[2]; |
| 547 | intencode(datalen, &cursor); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 548 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 549 | /* move data after header */ |
| 550 | memmove(cursor, datamsg, datalen); |
| 551 | |
| 552 | /* return header size + data_len */ |
| 553 | return (cursor - msg) + datalen; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 554 | } |
| 555 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 556 | /* |
| 557 | * This prepare the acknowledge message on the stick session <ts>, <st> is the considered |
| 558 | * stick table. |
Joseph Herlant | 82b2f54 | 2018-11-15 12:19:14 -0800 | [diff] [blame] | 559 | * <msg> is a buffer of <size> to receive data message content |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 560 | * If function returns 0, the caller should consider we were unable to encode this message (TODO: |
| 561 | * check size) |
| 562 | */ |
Frédéric Lécaille | d5fe14b | 2019-01-24 10:33:40 +0100 | [diff] [blame] | 563 | static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p) |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 564 | { |
| 565 | unsigned short datalen; |
| 566 | char *cursor, *datamsg; |
| 567 | uint32_t netinteger; |
Frédéric Lécaille | d5fe14b | 2019-01-24 10:33:40 +0100 | [diff] [blame] | 568 | struct shared_table *st; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 569 | |
Emeric Brun | b058f1c | 2015-09-22 15:50:18 +0200 | [diff] [blame] | 570 | cursor = datamsg = msg + 2 + 5; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 571 | |
Frédéric Lécaille | d5fe14b | 2019-01-24 10:33:40 +0100 | [diff] [blame] | 572 | st = p->ack.shared_table; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 573 | intencode(st->remote_id, &cursor); |
| 574 | netinteger = htonl(st->last_get); |
| 575 | memcpy(cursor, &netinteger, sizeof(netinteger)); |
| 576 | cursor += sizeof(netinteger); |
| 577 | |
| 578 | /* Compute datalen */ |
| 579 | datalen = (cursor - datamsg); |
| 580 | |
| 581 | /* prepare message header */ |
| 582 | msg[0] = PEER_MSG_CLASS_STICKTABLE; |
Emeric Brun | e1ab808 | 2015-08-21 11:48:54 +0200 | [diff] [blame] | 583 | msg[1] = PEER_MSG_STKT_ACK; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 584 | cursor = &msg[2]; |
| 585 | intencode(datalen, &cursor); |
| 586 | |
| 587 | /* move data after header */ |
| 588 | memmove(cursor, datamsg, datalen); |
| 589 | |
| 590 | /* return header size + data_len */ |
| 591 | return (cursor - msg) + datalen; |
| 592 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 593 | |
| 594 | /* |
| 595 | * Callback to release a session with a peer |
| 596 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 597 | static void peer_session_release(struct appctx *appctx) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 598 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 599 | struct stream_interface *si = appctx->owner; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 600 | struct stream *s = si_strm(si); |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 601 | struct peer *peer = appctx->ctx.peers.ptr; |
| 602 | struct peers *peers = strm_fe(s)->parent; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 603 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 604 | /* appctx->ctx.peers.ptr is not a peer session */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 605 | if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 606 | return; |
| 607 | |
| 608 | /* peer session identified */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 609 | if (peer) { |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 610 | if (appctx->st0 == PEER_SESS_ST_WAITMSG) |
Olivier Houchard | ed87989 | 2019-03-08 18:53:43 +0100 | [diff] [blame] | 611 | _HA_ATOMIC_SUB(&connected_peers, 1); |
| 612 | _HA_ATOMIC_SUB(&active_peers, 1); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 613 | HA_SPIN_LOCK(PEER_LOCK, &peer->lock); |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 614 | if (peer->appctx == appctx) { |
Emeric Brun | b157d73 | 2015-08-21 12:00:30 +0200 | [diff] [blame] | 615 | /* Re-init current table pointers to force announcement on re-connect */ |
| 616 | peer->remote_table = peer->last_local_table = NULL; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 617 | peer->appctx = NULL; |
| 618 | if (peer->flags & PEER_F_LEARN_ASSIGN) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 619 | /* unassign current peer for learning */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 620 | peer->flags &= ~(PEER_F_LEARN_ASSIGN); |
| 621 | peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 622 | |
| 623 | /* reschedule a resync */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 624 | peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 625 | } |
| 626 | /* reset teaching and learning flags to 0 */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 627 | peer->flags &= PEER_TEACH_RESET; |
| 628 | peer->flags &= PEER_LEARN_RESET; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 629 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 630 | HA_SPIN_UNLOCK(PEER_LOCK, &peer->lock); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 631 | task_wakeup(peers->sync_task, TASK_WOKEN_MSG); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 635 | /* Retrieve the major and minor versions of peers protocol |
| 636 | * announced by a remote peer. <str> is a null-terminated |
| 637 | * string with the following format: "<maj_ver>.<min_ver>". |
| 638 | */ |
| 639 | static int peer_get_version(const char *str, |
| 640 | unsigned int *maj_ver, unsigned int *min_ver) |
| 641 | { |
| 642 | unsigned int majv, minv; |
| 643 | const char *pos, *saved; |
| 644 | const char *end; |
| 645 | |
| 646 | saved = pos = str; |
| 647 | end = str + strlen(str); |
| 648 | |
| 649 | majv = read_uint(&pos, end); |
| 650 | if (saved == pos || *pos++ != '.') |
| 651 | return -1; |
| 652 | |
| 653 | saved = pos; |
| 654 | minv = read_uint(&pos, end); |
| 655 | if (saved == pos || pos != end) |
| 656 | return -1; |
| 657 | |
| 658 | *maj_ver = majv; |
| 659 | *min_ver = minv; |
| 660 | |
| 661 | return 0; |
| 662 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 663 | |
| 664 | /* |
Frédéric Lécaille | ce02557 | 2019-01-21 13:38:06 +0100 | [diff] [blame] | 665 | * Parse a line terminated by an optional '\r' character, followed by a mandatory |
| 666 | * '\n' character. |
| 667 | * Returns 1 if succeeded or 0 if a '\n' character could not be found, and -1 if |
| 668 | * a line could not be read because the communication channel is closed. |
| 669 | */ |
| 670 | static inline int peer_getline(struct appctx *appctx) |
| 671 | { |
| 672 | int n; |
| 673 | struct stream_interface *si = appctx->owner; |
| 674 | |
| 675 | n = co_getline(si_oc(si), trash.area, trash.size); |
| 676 | if (!n) |
| 677 | return 0; |
| 678 | |
| 679 | if (n < 0 || trash.area[n - 1] != '\n') { |
| 680 | appctx->st0 = PEER_SESS_ST_END; |
| 681 | return -1; |
| 682 | } |
| 683 | |
| 684 | if (n > 1 && (trash.area[n - 2] == '\r')) |
| 685 | trash.area[n - 2] = 0; |
| 686 | else |
| 687 | trash.area[n - 1] = 0; |
| 688 | |
| 689 | co_skip(si_oc(si), n); |
| 690 | |
| 691 | return n; |
| 692 | } |
| 693 | |
| 694 | /* |
Frédéric Lécaille | ec44ea8 | 2019-01-22 15:54:53 +0100 | [diff] [blame] | 695 | * Send a message after having called <peer_prepare_msg> to build it. |
| 696 | * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value. |
| 697 | * Returns -1 if there was not enough room left to send the message, |
| 698 | * any other negative returned value must be considered as an error with an appcxt st0 |
| 699 | * returned value equal to PEER_SESS_ST_END. |
| 700 | */ |
Frédéric Lécaille | d5fe14b | 2019-01-24 10:33:40 +0100 | [diff] [blame] | 701 | static inline int peer_send_msg(struct appctx *appctx, |
| 702 | int (*peer_prepare_msg)(char *, size_t, struct peer_prep_params *), |
| 703 | struct peer_prep_params *params) |
Frédéric Lécaille | ec44ea8 | 2019-01-22 15:54:53 +0100 | [diff] [blame] | 704 | { |
| 705 | int ret, msglen; |
| 706 | struct stream_interface *si = appctx->owner; |
| 707 | |
Frédéric Lécaille | d5fe14b | 2019-01-24 10:33:40 +0100 | [diff] [blame] | 708 | msglen = peer_prepare_msg(trash.area, trash.size, params); |
Frédéric Lécaille | ec44ea8 | 2019-01-22 15:54:53 +0100 | [diff] [blame] | 709 | if (!msglen) { |
| 710 | /* internal error: message does not fit in trash */ |
| 711 | appctx->st0 = PEER_SESS_ST_END; |
| 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | /* message to buffer */ |
| 716 | ret = ci_putblk(si_ic(si), trash.area, msglen); |
| 717 | if (ret <= 0) { |
| 718 | if (ret == -1) { |
| 719 | /* No more write possible */ |
| 720 | si_rx_room_blk(si); |
| 721 | return -1; |
| 722 | } |
| 723 | appctx->st0 = PEER_SESS_ST_END; |
| 724 | } |
| 725 | |
| 726 | return ret; |
| 727 | } |
| 728 | |
| 729 | /* |
Frédéric Lécaille | 7d0ceee | 2019-01-24 14:24:05 +0100 | [diff] [blame] | 730 | * Send a hello message. |
| 731 | * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value. |
| 732 | * Returns -1 if there was not enough room left to send the message, |
| 733 | * any other negative returned value must be considered as an error with an appcxt st0 |
| 734 | * returned value equal to PEER_SESS_ST_END. |
| 735 | */ |
| 736 | static inline int peer_send_hellomsg(struct appctx *appctx, struct peer *peer) |
| 737 | { |
| 738 | struct peer_prep_params p = { |
| 739 | .hello.peer = peer, |
| 740 | }; |
| 741 | |
| 742 | return peer_send_msg(appctx, peer_prepare_hellomsg, &p); |
| 743 | } |
| 744 | |
| 745 | /* |
| 746 | * Send a success peer handshake status message. |
| 747 | * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value. |
| 748 | * Returns -1 if there was not enough room left to send the message, |
| 749 | * any other negative returned value must be considered as an error with an appcxt st0 |
| 750 | * returned value equal to PEER_SESS_ST_END. |
| 751 | */ |
| 752 | static inline int peer_send_status_successmsg(struct appctx *appctx) |
| 753 | { |
| 754 | return peer_send_msg(appctx, peer_prepare_status_successmsg, NULL); |
| 755 | } |
| 756 | |
| 757 | /* |
| 758 | * Send a peer handshake status error message. |
| 759 | * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value. |
| 760 | * Returns -1 if there was not enough room left to send the message, |
| 761 | * any other negative returned value must be considered as an error with an appcxt st0 |
| 762 | * returned value equal to PEER_SESS_ST_END. |
| 763 | */ |
| 764 | static inline int peer_send_status_errormsg(struct appctx *appctx) |
| 765 | { |
| 766 | struct peer_prep_params p = { |
| 767 | .error_status.st1 = appctx->st1, |
| 768 | }; |
| 769 | |
| 770 | return peer_send_msg(appctx, peer_prepare_status_errormsg, &p); |
| 771 | } |
| 772 | |
| 773 | /* |
Frédéric Lécaille | ec44ea8 | 2019-01-22 15:54:53 +0100 | [diff] [blame] | 774 | * Send a stick-table switch message. |
| 775 | * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value. |
| 776 | * Returns -1 if there was not enough room left to send the message, |
| 777 | * any other negative returned value must be considered as an error with an appcxt st0 |
| 778 | * returned value equal to PEER_SESS_ST_END. |
| 779 | */ |
| 780 | static inline int peer_send_switchmsg(struct shared_table *st, struct appctx *appctx) |
| 781 | { |
Frédéric Lécaille | d5fe14b | 2019-01-24 10:33:40 +0100 | [diff] [blame] | 782 | struct peer_prep_params p = { |
| 783 | .swtch.shared_table = st, |
| 784 | }; |
| 785 | |
| 786 | return peer_send_msg(appctx, peer_prepare_switchmsg, &p); |
Frédéric Lécaille | ec44ea8 | 2019-01-22 15:54:53 +0100 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | /* |
| 790 | * Send a stick-table update acknowledgement message. |
| 791 | * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value. |
| 792 | * Returns -1 if there was not enough room left to send the message, |
| 793 | * any other negative returned value must be considered as an error with an appcxt st0 |
| 794 | * returned value equal to PEER_SESS_ST_END. |
| 795 | */ |
| 796 | static inline int peer_send_ackmsg(struct shared_table *st, struct appctx *appctx) |
| 797 | { |
Frédéric Lécaille | d5fe14b | 2019-01-24 10:33:40 +0100 | [diff] [blame] | 798 | struct peer_prep_params p = { |
| 799 | .ack.shared_table = st, |
| 800 | }; |
| 801 | |
| 802 | return peer_send_msg(appctx, peer_prepare_ackmsg, &p); |
Frédéric Lécaille | ec44ea8 | 2019-01-22 15:54:53 +0100 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /* |
Frédéric Lécaille | 87f554c | 2019-01-22 17:26:50 +0100 | [diff] [blame] | 806 | * Send a stick-table update message. |
| 807 | * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value. |
| 808 | * Returns -1 if there was not enough room left to send the message, |
| 809 | * any other negative returned value must be considered as an error with an appcxt st0 |
| 810 | * returned value equal to PEER_SESS_ST_END. |
| 811 | */ |
| 812 | static inline int peer_send_updatemsg(struct shared_table *st, struct appctx *appctx, struct stksess *ts, |
| 813 | unsigned int updateid, int use_identifier, int use_timed) |
| 814 | { |
Frédéric Lécaille | d5fe14b | 2019-01-24 10:33:40 +0100 | [diff] [blame] | 815 | struct peer_prep_params p = { |
| 816 | .updt.stksess = ts, |
| 817 | .updt.shared_table = st, |
| 818 | .updt.updateid = updateid, |
| 819 | .updt.use_identifier = use_identifier, |
| 820 | .updt.use_timed = use_timed, |
| 821 | }; |
| 822 | |
| 823 | return peer_send_msg(appctx, peer_prepare_updatemsg, &p); |
Frédéric Lécaille | 87f554c | 2019-01-22 17:26:50 +0100 | [diff] [blame] | 824 | } |
| 825 | |
Frédéric Lécaille | 7d0ceee | 2019-01-24 14:24:05 +0100 | [diff] [blame] | 826 | /* |
| 827 | * Build a peer protocol control class message. |
| 828 | * Returns the number of written bytes used to build the message if succeeded, |
| 829 | * 0 if not. |
| 830 | */ |
| 831 | static int peer_prepare_control_msg(char *msg, size_t size, struct peer_prep_params *p) |
| 832 | { |
| 833 | if (size < sizeof p->control.head) |
| 834 | return 0; |
| 835 | |
| 836 | msg[0] = p->control.head[0]; |
| 837 | msg[1] = p->control.head[1]; |
| 838 | |
| 839 | return 2; |
| 840 | } |
Frédéric Lécaille | 6a8303d | 2019-01-22 22:25:17 +0100 | [diff] [blame] | 841 | |
| 842 | /* |
Frédéric Lécaille | 7d0ceee | 2019-01-24 14:24:05 +0100 | [diff] [blame] | 843 | * Send a stick-table synchronization request message. |
| 844 | * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value. |
| 845 | * Returns -1 if there was not enough room left to send the message, |
| 846 | * any other negative returned value must be considered as an error with an appctx st0 |
| 847 | * returned value equal to PEER_SESS_ST_END. |
| 848 | */ |
| 849 | static inline int peer_send_resync_reqmsg(struct appctx *appctx) |
| 850 | { |
| 851 | struct peer_prep_params p = { |
| 852 | .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCREQ, }, |
| 853 | }; |
| 854 | |
| 855 | return peer_send_msg(appctx, peer_prepare_control_msg, &p); |
| 856 | } |
| 857 | |
| 858 | /* |
| 859 | * Send a stick-table synchronization confirmation message. |
| 860 | * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value. |
| 861 | * Returns -1 if there was not enough room left to send the message, |
| 862 | * any other negative returned value must be considered as an error with an appctx st0 |
| 863 | * returned value equal to PEER_SESS_ST_END. |
| 864 | */ |
| 865 | static inline int peer_send_resync_confirmsg(struct appctx *appctx) |
| 866 | { |
| 867 | struct peer_prep_params p = { |
| 868 | .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_RESYNCCONFIRM, }, |
| 869 | }; |
| 870 | |
| 871 | return peer_send_msg(appctx, peer_prepare_control_msg, &p); |
| 872 | } |
| 873 | |
| 874 | /* |
| 875 | * Send a stick-table synchronization finished message. |
| 876 | * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value. |
| 877 | * Returns -1 if there was not enough room left to send the message, |
| 878 | * any other negative returned value must be considered as an error with an appctx st0 |
| 879 | * returned value equal to PEER_SESS_ST_END. |
| 880 | */ |
| 881 | static inline int peer_send_resync_finishedmsg(struct appctx *appctx, struct peer *peer) |
| 882 | { |
| 883 | struct peer_prep_params p = { |
| 884 | .control.head = { PEER_MSG_CLASS_CONTROL, }, |
| 885 | }; |
| 886 | |
| 887 | p.control.head[1] = (peer->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FINISHED ? |
| 888 | PEER_MSG_CTRL_RESYNCFINISHED : PEER_MSG_CTRL_RESYNCPARTIAL; |
| 889 | |
| 890 | return peer_send_msg(appctx, peer_prepare_control_msg, &p); |
| 891 | } |
| 892 | |
| 893 | /* |
Frédéric Lécaille | 645635d | 2019-02-11 17:49:39 +0100 | [diff] [blame] | 894 | * Send a heartbeat message. |
| 895 | * Return 0 if the message could not be built modifying the appctx st0 to PEER_SESS_ST_END value. |
| 896 | * Returns -1 if there was not enough room left to send the message, |
| 897 | * any other negative returned value must be considered as an error with an appctx st0 |
| 898 | * returned value equal to PEER_SESS_ST_END. |
| 899 | */ |
| 900 | static inline int peer_send_heartbeatmsg(struct appctx *appctx) |
| 901 | { |
| 902 | struct peer_prep_params p = { |
| 903 | .control.head = { PEER_MSG_CLASS_CONTROL, PEER_MSG_CTRL_HEARTBEAT, }, |
| 904 | }; |
| 905 | |
| 906 | return peer_send_msg(appctx, peer_prepare_control_msg, &p); |
| 907 | } |
| 908 | |
| 909 | /* |
Frédéric Lécaille | 7d0ceee | 2019-01-24 14:24:05 +0100 | [diff] [blame] | 910 | * Build a peer protocol error class message. |
| 911 | * Returns the number of written bytes used to build the message if succeeded, |
| 912 | * 0 if not. |
| 913 | */ |
| 914 | static int peer_prepare_error_msg(char *msg, size_t size, struct peer_prep_params *p) |
| 915 | { |
| 916 | if (size < sizeof p->error.head) |
| 917 | return 0; |
| 918 | |
| 919 | msg[0] = p->error.head[0]; |
| 920 | msg[1] = p->error.head[1]; |
| 921 | |
| 922 | return 2; |
| 923 | } |
| 924 | |
| 925 | /* |
| 926 | * Send a "size limit reached" error message. |
| 927 | * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value. |
| 928 | * Returns -1 if there was not enough room left to send the message, |
| 929 | * any other negative returned value must be considered as an error with an appctx st0 |
| 930 | * returned value equal to PEER_SESS_ST_END. |
| 931 | */ |
| 932 | static inline int peer_send_error_size_limitmsg(struct appctx *appctx) |
| 933 | { |
| 934 | struct peer_prep_params p = { |
| 935 | .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_SIZELIMIT, }, |
| 936 | }; |
| 937 | |
| 938 | return peer_send_msg(appctx, peer_prepare_error_msg, &p); |
| 939 | } |
| 940 | |
| 941 | /* |
| 942 | * Send a "peer protocol" error message. |
| 943 | * Return 0 if the message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value. |
| 944 | * Returns -1 if there was not enough room left to send the message, |
| 945 | * any other negative returned value must be considered as an error with an appctx st0 |
| 946 | * returned value equal to PEER_SESS_ST_END. |
| 947 | */ |
| 948 | static inline int peer_send_error_protomsg(struct appctx *appctx) |
| 949 | { |
| 950 | struct peer_prep_params p = { |
| 951 | .error.head = { PEER_MSG_CLASS_ERROR, PEER_MSG_ERR_PROTOCOL, }, |
| 952 | }; |
| 953 | |
| 954 | return peer_send_msg(appctx, peer_prepare_error_msg, &p); |
| 955 | } |
| 956 | |
| 957 | /* |
Frédéric Lécaille | 6a8303d | 2019-01-22 22:25:17 +0100 | [diff] [blame] | 958 | * Function used to lookup for recent stick-table updates associated with |
| 959 | * <st> shared stick-table when a lesson must be taught a peer (PEER_F_LEARN_ASSIGN flag set). |
| 960 | */ |
| 961 | static inline struct stksess *peer_teach_process_stksess_lookup(struct shared_table *st) |
| 962 | { |
| 963 | struct eb32_node *eb; |
| 964 | |
| 965 | eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1); |
| 966 | if (!eb) { |
| 967 | eb = eb32_first(&st->table->updates); |
| 968 | if (!eb || ((int)(eb->key - st->last_pushed) <= 0)) { |
| 969 | st->table->commitupdate = st->last_pushed = st->table->localupdate; |
| 970 | return NULL; |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | if ((int)(eb->key - st->table->localupdate) > 0) { |
| 975 | st->table->commitupdate = st->last_pushed = st->table->localupdate; |
| 976 | return NULL; |
| 977 | } |
| 978 | |
| 979 | return eb32_entry(eb, struct stksess, upd); |
| 980 | } |
| 981 | |
| 982 | /* |
| 983 | * Function used to lookup for recent stick-table updates associated with |
| 984 | * <st> shared stick-table during teach state 1 step. |
| 985 | */ |
| 986 | static inline struct stksess *peer_teach_stage1_stksess_lookup(struct shared_table *st) |
| 987 | { |
| 988 | struct eb32_node *eb; |
| 989 | |
| 990 | eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1); |
| 991 | if (!eb) { |
| 992 | st->flags |= SHTABLE_F_TEACH_STAGE1; |
| 993 | eb = eb32_first(&st->table->updates); |
| 994 | if (eb) |
| 995 | st->last_pushed = eb->key - 1; |
| 996 | return NULL; |
| 997 | } |
| 998 | |
| 999 | return eb32_entry(eb, struct stksess, upd); |
| 1000 | } |
| 1001 | |
| 1002 | /* |
| 1003 | * Function used to lookup for recent stick-table updates associated with |
| 1004 | * <st> shared stick-table during teach state 2 step. |
| 1005 | */ |
| 1006 | static inline struct stksess *peer_teach_stage2_stksess_lookup(struct shared_table *st) |
| 1007 | { |
| 1008 | struct eb32_node *eb; |
| 1009 | |
| 1010 | eb = eb32_lookup_ge(&st->table->updates, st->last_pushed+1); |
| 1011 | if (!eb || eb->key > st->teaching_origin) { |
| 1012 | st->flags |= SHTABLE_F_TEACH_STAGE2; |
| 1013 | return NULL; |
| 1014 | } |
| 1015 | |
| 1016 | return eb32_entry(eb, struct stksess, upd); |
| 1017 | } |
| 1018 | |
| 1019 | /* |
| 1020 | * Generic function to emit update messages for <st> stick-table when a lesson must |
| 1021 | * be taught to the peer <p>. |
| 1022 | * <locked> must be set to 1 if the shared table <st> is already locked when entering |
| 1023 | * this function, 0 if not. |
| 1024 | * |
| 1025 | * This function temporary unlock/lock <st> when it sends stick-table updates or |
| 1026 | * when decrementing its refcount in case of any error when it sends this updates. |
| 1027 | * |
| 1028 | * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value. |
| 1029 | * Returns -1 if there was not enough room left to send the message, |
| 1030 | * any other negative returned value must be considered as an error with an appcxt st0 |
| 1031 | * returned value equal to PEER_SESS_ST_END. |
| 1032 | * If it returns 0 or -1, this function leave <st> locked if already locked when entering this function |
| 1033 | * unlocked if not already locked when entering this function. |
| 1034 | */ |
| 1035 | static inline int peer_send_teachmsgs(struct appctx *appctx, struct peer *p, |
| 1036 | struct stksess *(*peer_stksess_lookup)(struct shared_table *), |
| 1037 | struct shared_table *st, int locked) |
| 1038 | { |
| 1039 | int ret, new_pushed, use_timed; |
| 1040 | |
| 1041 | ret = 1; |
| 1042 | use_timed = 0; |
| 1043 | if (st != p->last_local_table) { |
| 1044 | ret = peer_send_switchmsg(st, appctx); |
| 1045 | if (ret <= 0) |
| 1046 | return ret; |
| 1047 | |
| 1048 | p->last_local_table = st; |
| 1049 | } |
| 1050 | |
| 1051 | if (peer_stksess_lookup != peer_teach_process_stksess_lookup) |
| 1052 | use_timed = !(p->flags & PEER_F_DWNGRD); |
| 1053 | |
| 1054 | /* We force new pushed to 1 to force identifier in update message */ |
| 1055 | new_pushed = 1; |
| 1056 | |
| 1057 | if (!locked) |
| 1058 | HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock); |
| 1059 | |
| 1060 | while (1) { |
| 1061 | struct stksess *ts; |
| 1062 | unsigned updateid; |
| 1063 | |
| 1064 | /* push local updates */ |
| 1065 | ts = peer_stksess_lookup(st); |
| 1066 | if (!ts) |
| 1067 | break; |
| 1068 | |
| 1069 | updateid = ts->upd.key; |
| 1070 | ts->ref_cnt++; |
| 1071 | HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock); |
| 1072 | |
| 1073 | ret = peer_send_updatemsg(st, appctx, ts, updateid, new_pushed, use_timed); |
| 1074 | if (ret <= 0) { |
| 1075 | HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock); |
| 1076 | ts->ref_cnt--; |
| 1077 | if (!locked) |
| 1078 | HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock); |
| 1079 | return ret; |
| 1080 | } |
| 1081 | |
| 1082 | HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock); |
| 1083 | ts->ref_cnt--; |
| 1084 | st->last_pushed = updateid; |
| 1085 | |
| 1086 | if (peer_stksess_lookup == peer_teach_process_stksess_lookup && |
| 1087 | (int)(st->last_pushed - st->table->commitupdate) > 0) |
| 1088 | st->table->commitupdate = st->last_pushed; |
| 1089 | |
| 1090 | /* identifier may not needed in next update message */ |
| 1091 | new_pushed = 0; |
| 1092 | } |
| 1093 | |
| 1094 | out: |
| 1095 | if (!locked) |
| 1096 | HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock); |
| 1097 | return 1; |
| 1098 | } |
| 1099 | |
| 1100 | /* |
| 1101 | * Function to emit update messages for <st> stick-table when a lesson must |
| 1102 | * be taught to the peer <p> (PEER_F_LEARN_ASSIGN flag set). |
| 1103 | * |
| 1104 | * Note that <st> shared stick-table is locked when calling this function. |
| 1105 | * |
| 1106 | * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value. |
| 1107 | * Returns -1 if there was not enough room left to send the message, |
| 1108 | * any other negative returned value must be considered as an error with an appcxt st0 |
| 1109 | * returned value equal to PEER_SESS_ST_END. |
| 1110 | */ |
| 1111 | static inline int peer_send_teach_process_msgs(struct appctx *appctx, struct peer *p, |
| 1112 | struct shared_table *st) |
| 1113 | { |
| 1114 | return peer_send_teachmsgs(appctx, p, peer_teach_process_stksess_lookup, st, 1); |
| 1115 | } |
| 1116 | |
| 1117 | /* |
| 1118 | * Function to emit update messages for <st> stick-table when a lesson must |
| 1119 | * be taught to the peer <p> during teach state 1 step. |
| 1120 | * |
| 1121 | * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value. |
| 1122 | * Returns -1 if there was not enough room left to send the message, |
| 1123 | * any other negative returned value must be considered as an error with an appcxt st0 |
| 1124 | * returned value equal to PEER_SESS_ST_END. |
| 1125 | */ |
| 1126 | static inline int peer_send_teach_stage1_msgs(struct appctx *appctx, struct peer *p, |
| 1127 | struct shared_table *st) |
| 1128 | { |
| 1129 | return peer_send_teachmsgs(appctx, p, peer_teach_stage1_stksess_lookup, st, 0); |
| 1130 | } |
| 1131 | |
| 1132 | /* |
| 1133 | * Function to emit update messages for <st> stick-table when a lesson must |
| 1134 | * be taught to the peer <p> during teach state 1 step. |
| 1135 | * |
| 1136 | * Return 0 if any message could not be built modifying the appcxt st0 to PEER_SESS_ST_END value. |
| 1137 | * Returns -1 if there was not enough room left to send the message, |
| 1138 | * any other negative returned value must be considered as an error with an appcxt st0 |
| 1139 | * returned value equal to PEER_SESS_ST_END. |
| 1140 | */ |
| 1141 | static inline int peer_send_teach_stage2_msgs(struct appctx *appctx, struct peer *p, |
| 1142 | struct shared_table *st) |
| 1143 | { |
| 1144 | return peer_send_teachmsgs(appctx, p, peer_teach_stage2_stksess_lookup, st, 0); |
| 1145 | } |
| 1146 | |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1147 | |
| 1148 | /* |
| 1149 | * Function used to parse a stick-table update message after it has been received |
| 1150 | * by <p> peer with <msg_cur> as address of the pointer to the position in the |
| 1151 | * receipt buffer with <msg_end> being position of the end of the stick-table message. |
| 1152 | * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error |
| 1153 | * was encountered. |
| 1154 | * <exp> must be set if the stick-table entry expires. |
| 1155 | * <updt> must be set for PEER_MSG_STKT_UPDATE or PEER_MSG_STKT_UPDATE_TIMED stick-table |
| 1156 | * messages, in this case the stick-table udpate message is received with a stick-table |
| 1157 | * update ID. |
| 1158 | * <totl> is the length of the stick-table update message computed upon receipt. |
| 1159 | */ |
Frédéric Lécaille | 444243c | 2019-01-24 15:40:11 +0100 | [diff] [blame] | 1160 | static int peer_treat_updatemsg(struct appctx *appctx, struct peer *p, int updt, int exp, |
| 1161 | char **msg_cur, char *msg_end, int msg_len, int totl) |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1162 | { |
| 1163 | struct stream_interface *si = appctx->owner; |
| 1164 | struct shared_table *st = p->remote_table; |
| 1165 | struct stksess *ts, *newts; |
| 1166 | uint32_t update; |
| 1167 | int expire; |
| 1168 | unsigned int data_type; |
| 1169 | void *data_ptr; |
| 1170 | |
| 1171 | /* Here we have data message */ |
| 1172 | if (!st) |
| 1173 | goto ignore_msg; |
| 1174 | |
| 1175 | expire = MS_TO_TICKS(st->table->expire); |
| 1176 | |
| 1177 | if (updt) { |
Willy Tarreau | 1e82a14 | 2019-01-29 11:08:06 +0100 | [diff] [blame] | 1178 | if (msg_len < sizeof(update)) |
| 1179 | goto malformed_exit; |
| 1180 | |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1181 | memcpy(&update, *msg_cur, sizeof(update)); |
| 1182 | *msg_cur += sizeof(update); |
| 1183 | st->last_get = htonl(update); |
| 1184 | } |
| 1185 | else { |
| 1186 | st->last_get++; |
| 1187 | } |
| 1188 | |
| 1189 | if (exp) { |
| 1190 | size_t expire_sz = sizeof expire; |
| 1191 | |
Willy Tarreau | 1e82a14 | 2019-01-29 11:08:06 +0100 | [diff] [blame] | 1192 | if (*msg_cur + expire_sz > msg_end) |
| 1193 | goto malformed_exit; |
| 1194 | |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1195 | memcpy(&expire, *msg_cur, expire_sz); |
| 1196 | *msg_cur += expire_sz; |
| 1197 | expire = ntohl(expire); |
| 1198 | } |
| 1199 | |
| 1200 | newts = stksess_new(st->table, NULL); |
| 1201 | if (!newts) |
| 1202 | goto ignore_msg; |
| 1203 | |
| 1204 | if (st->table->type == SMP_T_STR) { |
| 1205 | unsigned int to_read, to_store; |
| 1206 | |
| 1207 | to_read = intdecode(msg_cur, msg_end); |
Willy Tarreau | 1e82a14 | 2019-01-29 11:08:06 +0100 | [diff] [blame] | 1208 | if (!*msg_cur) |
| 1209 | goto malformed_free_newts; |
| 1210 | |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1211 | to_store = MIN(to_read, st->table->key_size - 1); |
Willy Tarreau | 1e82a14 | 2019-01-29 11:08:06 +0100 | [diff] [blame] | 1212 | if (*msg_cur + to_store > msg_end) |
| 1213 | goto malformed_free_newts; |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1214 | |
| 1215 | memcpy(newts->key.key, *msg_cur, to_store); |
| 1216 | newts->key.key[to_store] = 0; |
| 1217 | *msg_cur += to_read; |
| 1218 | } |
| 1219 | else if (st->table->type == SMP_T_SINT) { |
| 1220 | unsigned int netinteger; |
| 1221 | |
Willy Tarreau | 1e82a14 | 2019-01-29 11:08:06 +0100 | [diff] [blame] | 1222 | if (*msg_cur + sizeof(netinteger) > msg_end) |
| 1223 | goto malformed_free_newts; |
| 1224 | |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1225 | memcpy(&netinteger, *msg_cur, sizeof(netinteger)); |
| 1226 | netinteger = ntohl(netinteger); |
| 1227 | memcpy(newts->key.key, &netinteger, sizeof(netinteger)); |
| 1228 | *msg_cur += sizeof(netinteger); |
| 1229 | } |
| 1230 | else { |
Willy Tarreau | 1e82a14 | 2019-01-29 11:08:06 +0100 | [diff] [blame] | 1231 | if (*msg_cur + st->table->key_size > msg_end) |
| 1232 | goto malformed_free_newts; |
| 1233 | |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1234 | memcpy(newts->key.key, *msg_cur, st->table->key_size); |
| 1235 | *msg_cur += st->table->key_size; |
| 1236 | } |
| 1237 | |
| 1238 | /* lookup for existing entry */ |
| 1239 | ts = stktable_set_entry(st->table, newts); |
| 1240 | if (ts != newts) { |
| 1241 | stksess_free(st->table, newts); |
| 1242 | newts = NULL; |
| 1243 | } |
| 1244 | |
| 1245 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
| 1246 | |
| 1247 | for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) { |
Willy Tarreau | 1e82a14 | 2019-01-29 11:08:06 +0100 | [diff] [blame] | 1248 | uint64_t decoded_int; |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1249 | |
| 1250 | if (!((1 << data_type) & st->remote_data)) |
| 1251 | continue; |
| 1252 | |
Willy Tarreau | 1e82a14 | 2019-01-29 11:08:06 +0100 | [diff] [blame] | 1253 | decoded_int = intdecode(msg_cur, msg_end); |
| 1254 | if (!*msg_cur) |
| 1255 | goto malformed_unlock; |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1256 | |
Willy Tarreau | 1e82a14 | 2019-01-29 11:08:06 +0100 | [diff] [blame] | 1257 | switch (stktable_data_types[data_type].std_type) { |
| 1258 | case STD_T_SINT: |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1259 | data_ptr = stktable_data_ptr(st->table, ts, data_type); |
| 1260 | if (data_ptr) |
Willy Tarreau | 1e82a14 | 2019-01-29 11:08:06 +0100 | [diff] [blame] | 1261 | stktable_data_cast(data_ptr, std_t_sint) = decoded_int; |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1262 | break; |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1263 | |
Willy Tarreau | 1e82a14 | 2019-01-29 11:08:06 +0100 | [diff] [blame] | 1264 | case STD_T_UINT: |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1265 | data_ptr = stktable_data_ptr(st->table, ts, data_type); |
| 1266 | if (data_ptr) |
Willy Tarreau | 1e82a14 | 2019-01-29 11:08:06 +0100 | [diff] [blame] | 1267 | stktable_data_cast(data_ptr, std_t_uint) = decoded_int; |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1268 | break; |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1269 | |
Willy Tarreau | 1e82a14 | 2019-01-29 11:08:06 +0100 | [diff] [blame] | 1270 | case STD_T_ULL: |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1271 | data_ptr = stktable_data_ptr(st->table, ts, data_type); |
| 1272 | if (data_ptr) |
Willy Tarreau | 1e82a14 | 2019-01-29 11:08:06 +0100 | [diff] [blame] | 1273 | stktable_data_cast(data_ptr, std_t_ull) = decoded_int; |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1274 | break; |
Willy Tarreau | 1e82a14 | 2019-01-29 11:08:06 +0100 | [diff] [blame] | 1275 | |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1276 | case STD_T_FRQP: { |
| 1277 | struct freq_ctr_period data; |
| 1278 | |
| 1279 | /* First bit is reserved for the freq_ctr_period lock |
| 1280 | Note: here we're still protected by the stksess lock |
| 1281 | so we don't need to update the update the freq_ctr_period |
| 1282 | using its internal lock */ |
| 1283 | |
Willy Tarreau | 1e82a14 | 2019-01-29 11:08:06 +0100 | [diff] [blame] | 1284 | data.curr_tick = tick_add(now_ms, -decoded_int) & ~0x1; |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1285 | data.curr_ctr = intdecode(msg_cur, msg_end); |
Willy Tarreau | 1e82a14 | 2019-01-29 11:08:06 +0100 | [diff] [blame] | 1286 | if (!*msg_cur) |
| 1287 | goto malformed_unlock; |
| 1288 | |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1289 | data.prev_ctr = intdecode(msg_cur, msg_end); |
Willy Tarreau | 1e82a14 | 2019-01-29 11:08:06 +0100 | [diff] [blame] | 1290 | if (!*msg_cur) |
| 1291 | goto malformed_unlock; |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1292 | |
| 1293 | data_ptr = stktable_data_ptr(st->table, ts, data_type); |
| 1294 | if (data_ptr) |
| 1295 | stktable_data_cast(data_ptr, std_t_frqp) = data; |
| 1296 | break; |
| 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | /* Force new expiration */ |
| 1301 | ts->expire = tick_add(now_ms, expire); |
| 1302 | |
| 1303 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
| 1304 | stktable_touch_remote(st->table, ts, 1); |
| 1305 | return 1; |
| 1306 | |
| 1307 | ignore_msg: |
| 1308 | /* skip consumed message */ |
| 1309 | co_skip(si_oc(si), totl); |
| 1310 | return 0; |
Willy Tarreau | 1e82a14 | 2019-01-29 11:08:06 +0100 | [diff] [blame] | 1311 | |
| 1312 | malformed_unlock: |
| 1313 | /* malformed message */ |
| 1314 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
| 1315 | stktable_touch_remote(st->table, ts, 1); |
| 1316 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1317 | return 0; |
| 1318 | |
| 1319 | malformed_free_newts: |
| 1320 | /* malformed message */ |
| 1321 | stksess_free(st->table, newts); |
| 1322 | malformed_exit: |
| 1323 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1324 | return 0; |
Frédéric Lécaille | 168a34b | 2019-01-23 11:16:57 +0100 | [diff] [blame] | 1325 | } |
| 1326 | |
Frédéric Lécaille | 87f554c | 2019-01-22 17:26:50 +0100 | [diff] [blame] | 1327 | /* |
Frédéric Lécaille | d27b094 | 2019-01-23 17:31:37 +0100 | [diff] [blame] | 1328 | * Function used to parse a stick-table update acknowledgement message after it |
| 1329 | * has been received by <p> peer with <msg_cur> as address of the pointer to the position in the |
| 1330 | * receipt buffer with <msg_end> being the position of the end of the stick-table message. |
| 1331 | * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error |
| 1332 | * was encountered. |
| 1333 | * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO. |
| 1334 | */ |
| 1335 | static inline int peer_treat_ackmsg(struct appctx *appctx, struct peer *p, |
| 1336 | char **msg_cur, char *msg_end) |
| 1337 | { |
| 1338 | /* ack message */ |
| 1339 | uint32_t table_id ; |
| 1340 | uint32_t update; |
| 1341 | struct shared_table *st; |
| 1342 | |
| 1343 | table_id = intdecode(msg_cur, msg_end); |
| 1344 | if (!*msg_cur || (*msg_cur + sizeof(update) > msg_end)) { |
| 1345 | /* malformed message */ |
| 1346 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1347 | return 0; |
| 1348 | } |
| 1349 | |
| 1350 | memcpy(&update, *msg_cur, sizeof(update)); |
| 1351 | update = ntohl(update); |
| 1352 | |
| 1353 | for (st = p->tables; st; st = st->next) { |
| 1354 | if (st->local_id == table_id) { |
| 1355 | st->update = update; |
| 1356 | break; |
| 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | return 1; |
| 1361 | } |
| 1362 | |
| 1363 | /* |
| 1364 | * Function used to parse a stick-table switch message after it has been received |
| 1365 | * by <p> peer with <msg_cur> as address of the pointer to the position in the |
| 1366 | * receipt buffer with <msg_end> being the position of the end of the stick-table message. |
| 1367 | * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error |
| 1368 | * was encountered. |
| 1369 | * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO. |
| 1370 | */ |
| 1371 | static inline int peer_treat_switchmsg(struct appctx *appctx, struct peer *p, |
| 1372 | char **msg_cur, char *msg_end) |
| 1373 | { |
| 1374 | struct shared_table *st; |
| 1375 | int table_id; |
| 1376 | |
| 1377 | table_id = intdecode(msg_cur, msg_end); |
| 1378 | if (!*msg_cur) { |
| 1379 | /* malformed message */ |
| 1380 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1381 | return 0; |
| 1382 | } |
| 1383 | |
| 1384 | p->remote_table = NULL; |
| 1385 | for (st = p->tables; st; st = st->next) { |
| 1386 | if (st->remote_id == table_id) { |
| 1387 | p->remote_table = st; |
| 1388 | break; |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | return 1; |
| 1393 | } |
| 1394 | |
| 1395 | /* |
| 1396 | * Function used to parse a stick-table definition message after it has been received |
| 1397 | * by <p> peer with <msg_cur> as address of the pointer to the position in the |
| 1398 | * receipt buffer with <msg_end> being the position of the end of the stick-table message. |
| 1399 | * Update <msg_curr> accordingly to the peer protocol specs if no peer protocol error |
| 1400 | * was encountered. |
| 1401 | * <totl> is the length of the stick-table update message computed upon receipt. |
| 1402 | * Return 1 if succeeded, 0 if not with the appctx state st0 set to PEER_SESS_ST_ERRPROTO. |
| 1403 | */ |
| 1404 | static inline int peer_treat_definemsg(struct appctx *appctx, struct peer *p, |
| 1405 | char **msg_cur, char *msg_end, int totl) |
| 1406 | { |
| 1407 | struct stream_interface *si = appctx->owner; |
| 1408 | int table_id_len; |
| 1409 | struct shared_table *st; |
| 1410 | int table_type; |
| 1411 | int table_keylen; |
| 1412 | int table_id; |
| 1413 | uint64_t table_data; |
| 1414 | |
| 1415 | table_id = intdecode(msg_cur, msg_end); |
Willy Tarreau | 6f731f3 | 2019-01-29 11:11:23 +0100 | [diff] [blame] | 1416 | if (!*msg_cur) |
| 1417 | goto malformed_exit; |
Frédéric Lécaille | d27b094 | 2019-01-23 17:31:37 +0100 | [diff] [blame] | 1418 | |
| 1419 | table_id_len = intdecode(msg_cur, msg_end); |
Willy Tarreau | 6f731f3 | 2019-01-29 11:11:23 +0100 | [diff] [blame] | 1420 | if (!*msg_cur) |
| 1421 | goto malformed_exit; |
Frédéric Lécaille | d27b094 | 2019-01-23 17:31:37 +0100 | [diff] [blame] | 1422 | |
| 1423 | p->remote_table = NULL; |
Willy Tarreau | 6f731f3 | 2019-01-29 11:11:23 +0100 | [diff] [blame] | 1424 | if (!table_id_len || (*msg_cur + table_id_len) >= msg_end) |
| 1425 | goto malformed_exit; |
Frédéric Lécaille | d27b094 | 2019-01-23 17:31:37 +0100 | [diff] [blame] | 1426 | |
| 1427 | for (st = p->tables; st; st = st->next) { |
| 1428 | /* Reset IDs */ |
| 1429 | if (st->remote_id == table_id) |
| 1430 | st->remote_id = 0; |
| 1431 | |
| 1432 | if (!p->remote_table && (table_id_len == strlen(st->table->id)) && |
| 1433 | (memcmp(st->table->id, *msg_cur, table_id_len) == 0)) |
| 1434 | p->remote_table = st; |
| 1435 | } |
| 1436 | |
| 1437 | if (!p->remote_table) |
| 1438 | goto ignore_msg; |
| 1439 | |
| 1440 | *msg_cur += table_id_len; |
Willy Tarreau | 6f731f3 | 2019-01-29 11:11:23 +0100 | [diff] [blame] | 1441 | if (*msg_cur >= msg_end) |
| 1442 | goto malformed_exit; |
Frédéric Lécaille | d27b094 | 2019-01-23 17:31:37 +0100 | [diff] [blame] | 1443 | |
| 1444 | table_type = intdecode(msg_cur, msg_end); |
Willy Tarreau | 6f731f3 | 2019-01-29 11:11:23 +0100 | [diff] [blame] | 1445 | if (!*msg_cur) |
| 1446 | goto malformed_exit; |
Frédéric Lécaille | d27b094 | 2019-01-23 17:31:37 +0100 | [diff] [blame] | 1447 | |
| 1448 | table_keylen = intdecode(msg_cur, msg_end); |
Willy Tarreau | 6f731f3 | 2019-01-29 11:11:23 +0100 | [diff] [blame] | 1449 | if (!*msg_cur) |
| 1450 | goto malformed_exit; |
Frédéric Lécaille | d27b094 | 2019-01-23 17:31:37 +0100 | [diff] [blame] | 1451 | |
| 1452 | table_data = intdecode(msg_cur, msg_end); |
Willy Tarreau | 6f731f3 | 2019-01-29 11:11:23 +0100 | [diff] [blame] | 1453 | if (!*msg_cur) |
| 1454 | goto malformed_exit; |
Frédéric Lécaille | d27b094 | 2019-01-23 17:31:37 +0100 | [diff] [blame] | 1455 | |
| 1456 | if (p->remote_table->table->type != table_type |
| 1457 | || p->remote_table->table->key_size != table_keylen) { |
| 1458 | p->remote_table = NULL; |
| 1459 | goto ignore_msg; |
| 1460 | } |
| 1461 | |
| 1462 | p->remote_table->remote_data = table_data; |
| 1463 | p->remote_table->remote_id = table_id; |
| 1464 | return 1; |
| 1465 | |
| 1466 | ignore_msg: |
| 1467 | co_skip(si_oc(si), totl); |
| 1468 | return 0; |
Willy Tarreau | 6f731f3 | 2019-01-29 11:11:23 +0100 | [diff] [blame] | 1469 | |
| 1470 | malformed_exit: |
| 1471 | /* malformed message */ |
| 1472 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1473 | return 0; |
Frédéric Lécaille | d27b094 | 2019-01-23 17:31:37 +0100 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | /* |
Frédéric Lécaille | 95203f2 | 2019-01-23 19:38:11 +0100 | [diff] [blame] | 1477 | * Receive a stick-table message. |
| 1478 | * Returns 1 if there was no error, if not, returns 0 if not enough data were available, |
| 1479 | * -1 if there was an error updating the appctx state st0 accordingly. |
| 1480 | */ |
| 1481 | static inline int peer_recv_msg(struct appctx *appctx, char *msg_head, size_t msg_head_sz, |
| 1482 | uint32_t *msg_len, int *totl) |
| 1483 | { |
| 1484 | int reql; |
| 1485 | struct stream_interface *si = appctx->owner; |
| 1486 | |
| 1487 | reql = co_getblk(si_oc(si), msg_head, 2 * sizeof(char), *totl); |
| 1488 | if (reql <= 0) /* closed or EOL not found */ |
| 1489 | goto incomplete; |
| 1490 | |
| 1491 | *totl += reql; |
| 1492 | |
| 1493 | if ((unsigned int)msg_head[1] < 128) |
| 1494 | return 1; |
| 1495 | |
| 1496 | /* Read and Decode message length */ |
| 1497 | reql = co_getblk(si_oc(si), &msg_head[2], sizeof(char), *totl); |
| 1498 | if (reql <= 0) /* closed */ |
| 1499 | goto incomplete; |
| 1500 | |
| 1501 | *totl += reql; |
| 1502 | |
| 1503 | if ((unsigned int)msg_head[2] < 240) { |
| 1504 | *msg_len = msg_head[2]; |
| 1505 | } |
| 1506 | else { |
| 1507 | int i; |
| 1508 | char *cur; |
| 1509 | char *end; |
| 1510 | |
| 1511 | for (i = 3 ; i < msg_head_sz ; i++) { |
| 1512 | reql = co_getblk(si_oc(si), &msg_head[i], sizeof(char), *totl); |
| 1513 | if (reql <= 0) /* closed */ |
| 1514 | goto incomplete; |
| 1515 | |
| 1516 | *totl += reql; |
| 1517 | |
| 1518 | if (!(msg_head[i] & 0x80)) |
| 1519 | break; |
| 1520 | } |
| 1521 | |
| 1522 | if (i == msg_head_sz) { |
| 1523 | /* malformed message */ |
| 1524 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1525 | return -1; |
| 1526 | } |
| 1527 | end = msg_head + msg_head_sz; |
| 1528 | cur = &msg_head[2]; |
| 1529 | *msg_len = intdecode(&cur, end); |
| 1530 | if (!cur) { |
| 1531 | /* malformed message */ |
| 1532 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1533 | return -1; |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | /* Read message content */ |
| 1538 | if (*msg_len) { |
| 1539 | if (*msg_len > trash.size) { |
| 1540 | /* Status code is not success, abort */ |
| 1541 | appctx->st0 = PEER_SESS_ST_ERRSIZE; |
| 1542 | return -1; |
| 1543 | } |
| 1544 | |
| 1545 | reql = co_getblk(si_oc(si), trash.area, *msg_len, *totl); |
| 1546 | if (reql <= 0) /* closed */ |
| 1547 | goto incomplete; |
| 1548 | *totl += reql; |
| 1549 | } |
| 1550 | |
| 1551 | return 1; |
| 1552 | |
| 1553 | incomplete: |
| 1554 | if (reql < 0) { |
| 1555 | /* there was an error */ |
| 1556 | appctx->st0 = PEER_SESS_ST_END; |
| 1557 | return -1; |
| 1558 | } |
| 1559 | |
| 1560 | return 0; |
| 1561 | } |
Frédéric Lécaille | 444243c | 2019-01-24 15:40:11 +0100 | [diff] [blame] | 1562 | |
| 1563 | /* |
| 1564 | * Treat the awaited message with <msg_head> as header.* |
| 1565 | * Return 1 if succeeded, 0 if not. |
| 1566 | */ |
| 1567 | static inline int peer_treat_awaited_msg(struct appctx *appctx, struct peer *peer, unsigned char *msg_head, |
| 1568 | char **msg_cur, char *msg_end, int msg_len, int totl) |
| 1569 | { |
| 1570 | struct stream_interface *si = appctx->owner; |
| 1571 | struct stream *s = si_strm(si); |
| 1572 | struct peers *peers = strm_fe(s)->parent; |
| 1573 | |
| 1574 | if (msg_head[0] == PEER_MSG_CLASS_CONTROL) { |
| 1575 | if (msg_head[1] == PEER_MSG_CTRL_RESYNCREQ) { |
| 1576 | struct shared_table *st; |
| 1577 | /* Reset message: remote need resync */ |
| 1578 | |
| 1579 | /* prepare tables fot a global push */ |
| 1580 | for (st = peer->tables; st; st = st->next) { |
| 1581 | st->teaching_origin = st->last_pushed = st->table->update; |
| 1582 | st->flags = 0; |
| 1583 | } |
| 1584 | |
| 1585 | /* reset teaching flags to 0 */ |
| 1586 | peer->flags &= PEER_TEACH_RESET; |
| 1587 | |
| 1588 | /* flag to start to teach lesson */ |
| 1589 | peer->flags |= PEER_F_TEACH_PROCESS; |
| 1590 | } |
| 1591 | else if (msg_head[1] == PEER_MSG_CTRL_RESYNCFINISHED) { |
| 1592 | if (peer->flags & PEER_F_LEARN_ASSIGN) { |
| 1593 | peer->flags &= ~PEER_F_LEARN_ASSIGN; |
| 1594 | peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS); |
| 1595 | peers->flags |= (PEERS_F_RESYNC_LOCAL|PEERS_F_RESYNC_REMOTE); |
| 1596 | } |
| 1597 | peer->confirm++; |
| 1598 | } |
| 1599 | else if (msg_head[1] == PEER_MSG_CTRL_RESYNCPARTIAL) { |
| 1600 | if (peer->flags & PEER_F_LEARN_ASSIGN) { |
| 1601 | peer->flags &= ~PEER_F_LEARN_ASSIGN; |
| 1602 | peers->flags &= ~(PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS); |
| 1603 | |
| 1604 | peer->flags |= PEER_F_LEARN_NOTUP2DATE; |
| 1605 | peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); |
| 1606 | task_wakeup(peers->sync_task, TASK_WOKEN_MSG); |
| 1607 | } |
| 1608 | peer->confirm++; |
| 1609 | } |
| 1610 | else if (msg_head[1] == PEER_MSG_CTRL_RESYNCCONFIRM) { |
| 1611 | struct shared_table *st; |
| 1612 | |
| 1613 | /* If stopping state */ |
| 1614 | if (stopping) { |
| 1615 | /* Close session, push resync no more needed */ |
| 1616 | peer->flags |= PEER_F_TEACH_COMPLETE; |
| 1617 | appctx->st0 = PEER_SESS_ST_END; |
| 1618 | return 0; |
| 1619 | } |
| 1620 | for (st = peer->tables; st; st = st->next) { |
| 1621 | st->update = st->last_pushed = st->teaching_origin; |
| 1622 | st->flags = 0; |
| 1623 | } |
| 1624 | |
| 1625 | /* reset teaching flags to 0 */ |
| 1626 | peer->flags &= PEER_TEACH_RESET; |
| 1627 | } |
Frédéric Lécaille | 645635d | 2019-02-11 17:49:39 +0100 | [diff] [blame] | 1628 | else if (msg_head[1] == PEER_MSG_CTRL_HEARTBEAT) { |
| 1629 | peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000)); |
| 1630 | } |
Frédéric Lécaille | 444243c | 2019-01-24 15:40:11 +0100 | [diff] [blame] | 1631 | } |
| 1632 | else if (msg_head[0] == PEER_MSG_CLASS_STICKTABLE) { |
| 1633 | if (msg_head[1] == PEER_MSG_STKT_DEFINE) { |
| 1634 | if (!peer_treat_definemsg(appctx, peer, msg_cur, msg_end, totl)) |
| 1635 | return 0; |
| 1636 | } |
| 1637 | else if (msg_head[1] == PEER_MSG_STKT_SWITCH) { |
| 1638 | if (!peer_treat_switchmsg(appctx, peer, msg_cur, msg_end)) |
| 1639 | return 0; |
| 1640 | } |
| 1641 | else if (msg_head[1] == PEER_MSG_STKT_UPDATE || |
| 1642 | msg_head[1] == PEER_MSG_STKT_INCUPDATE || |
| 1643 | msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || |
| 1644 | msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED) { |
| 1645 | int update, expire; |
| 1646 | |
| 1647 | update = msg_head[1] == PEER_MSG_STKT_UPDATE || msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED; |
| 1648 | expire = msg_head[1] == PEER_MSG_STKT_UPDATE_TIMED || msg_head[1] == PEER_MSG_STKT_INCUPDATE_TIMED; |
| 1649 | if (!peer_treat_updatemsg(appctx, peer, update, expire, |
| 1650 | msg_cur, msg_end, msg_len, totl)) |
| 1651 | return 0; |
| 1652 | |
| 1653 | } |
| 1654 | else if (msg_head[1] == PEER_MSG_STKT_ACK) { |
| 1655 | if (!peer_treat_ackmsg(appctx, peer, msg_cur, msg_end)) |
| 1656 | return 0; |
| 1657 | } |
| 1658 | } |
| 1659 | else if (msg_head[0] == PEER_MSG_CLASS_RESERVED) { |
| 1660 | appctx->st0 = PEER_SESS_ST_ERRPROTO; |
| 1661 | return 0; |
| 1662 | } |
| 1663 | |
| 1664 | return 1; |
| 1665 | } |
Frédéric Lécaille | 25e1d5e | 2019-01-24 17:33:48 +0100 | [diff] [blame] | 1666 | |
| 1667 | |
| 1668 | /* |
| 1669 | * Send any message to <peer> peer. |
| 1670 | * Returns 1 if succeeded, or -1 or 0 if failed. |
| 1671 | * -1 means an internal error occured, 0 is for a peer protocol error leading |
| 1672 | * to a peer state change (from the peer I/O handler point of view). |
| 1673 | */ |
| 1674 | static inline int peer_send_msgs(struct appctx *appctx, struct peer *peer) |
| 1675 | { |
| 1676 | int repl; |
| 1677 | struct stream_interface *si = appctx->owner; |
| 1678 | struct stream *s = si_strm(si); |
| 1679 | struct peers *peers = strm_fe(s)->parent; |
| 1680 | |
| 1681 | /* Need to request a resync */ |
| 1682 | if ((peer->flags & PEER_F_LEARN_ASSIGN) && |
| 1683 | (peers->flags & PEERS_F_RESYNC_ASSIGN) && |
| 1684 | !(peers->flags & PEERS_F_RESYNC_PROCESS)) { |
| 1685 | |
| 1686 | repl = peer_send_resync_reqmsg(appctx); |
| 1687 | if (repl <= 0) |
| 1688 | return repl; |
| 1689 | |
| 1690 | peers->flags |= PEERS_F_RESYNC_PROCESS; |
| 1691 | } |
| 1692 | |
| 1693 | /* Nothing to read, now we start to write */ |
| 1694 | if (peer->tables) { |
| 1695 | struct shared_table *st; |
| 1696 | struct shared_table *last_local_table; |
| 1697 | |
| 1698 | last_local_table = peer->last_local_table; |
| 1699 | if (!last_local_table) |
| 1700 | last_local_table = peer->tables; |
| 1701 | st = last_local_table->next; |
| 1702 | |
| 1703 | while (1) { |
| 1704 | if (!st) |
| 1705 | st = peer->tables; |
| 1706 | |
| 1707 | /* It remains some updates to ack */ |
| 1708 | if (st->last_get != st->last_acked) { |
| 1709 | repl = peer_send_ackmsg(st, appctx); |
| 1710 | if (repl <= 0) |
| 1711 | return repl; |
| 1712 | |
| 1713 | st->last_acked = st->last_get; |
| 1714 | } |
| 1715 | |
| 1716 | if (!(peer->flags & PEER_F_TEACH_PROCESS)) { |
| 1717 | HA_SPIN_LOCK(STK_TABLE_LOCK, &st->table->lock); |
| 1718 | if (!(peer->flags & PEER_F_LEARN_ASSIGN) && |
| 1719 | ((int)(st->last_pushed - st->table->localupdate) < 0)) { |
| 1720 | |
| 1721 | repl = peer_send_teach_process_msgs(appctx, peer, st); |
| 1722 | if (repl <= 0) { |
| 1723 | HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock); |
| 1724 | return repl; |
| 1725 | } |
| 1726 | } |
| 1727 | HA_SPIN_UNLOCK(STK_TABLE_LOCK, &st->table->lock); |
| 1728 | } |
| 1729 | else { |
| 1730 | if (!(st->flags & SHTABLE_F_TEACH_STAGE1)) { |
| 1731 | repl = peer_send_teach_stage1_msgs(appctx, peer, st); |
| 1732 | if (repl <= 0) |
| 1733 | return repl; |
| 1734 | } |
| 1735 | |
| 1736 | if (!(st->flags & SHTABLE_F_TEACH_STAGE2)) { |
| 1737 | repl = peer_send_teach_stage2_msgs(appctx, peer, st); |
| 1738 | if (repl <= 0) |
| 1739 | return repl; |
| 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | if (st == last_local_table) |
| 1744 | break; |
| 1745 | st = st->next; |
| 1746 | } |
| 1747 | } |
| 1748 | |
| 1749 | if ((peer->flags & PEER_F_TEACH_PROCESS) && !(peer->flags & PEER_F_TEACH_FINISHED)) { |
| 1750 | repl = peer_send_resync_finishedmsg(appctx, peer); |
| 1751 | if (repl <= 0) |
| 1752 | return repl; |
| 1753 | |
| 1754 | /* flag finished message sent */ |
| 1755 | peer->flags |= PEER_F_TEACH_FINISHED; |
| 1756 | } |
| 1757 | |
| 1758 | /* Confirm finished or partial messages */ |
| 1759 | while (peer->confirm) { |
| 1760 | repl = peer_send_resync_confirmsg(appctx); |
| 1761 | if (repl <= 0) |
| 1762 | return repl; |
| 1763 | |
| 1764 | peer->confirm--; |
| 1765 | } |
| 1766 | |
| 1767 | return 1; |
| 1768 | } |
| 1769 | |
Frédéric Lécaille | 95203f2 | 2019-01-23 19:38:11 +0100 | [diff] [blame] | 1770 | /* |
Frédéric Lécaille | 3f0fb9d | 2019-01-25 08:30:29 +0100 | [diff] [blame] | 1771 | * Read and parse a first line of a "hello" peer protocol message. |
| 1772 | * Returns 0 if could not read a line, -1 if there was a read error or |
| 1773 | * the line is malformed, 1 if succeeded. |
| 1774 | */ |
| 1775 | static inline int peer_getline_version(struct appctx *appctx, |
| 1776 | unsigned int *maj_ver, unsigned int *min_ver) |
| 1777 | { |
| 1778 | int reql; |
| 1779 | |
| 1780 | reql = peer_getline(appctx); |
| 1781 | if (!reql) |
| 1782 | return 0; |
| 1783 | |
| 1784 | if (reql < 0) |
| 1785 | return -1; |
| 1786 | |
| 1787 | /* test protocol */ |
| 1788 | if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.area, proto_len + 1) != 0) { |
| 1789 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 1790 | appctx->st1 = PEER_SESS_SC_ERRPROTO; |
| 1791 | return -1; |
| 1792 | } |
| 1793 | if (peer_get_version(trash.area + proto_len + 1, maj_ver, min_ver) == -1 || |
| 1794 | *maj_ver != PEER_MAJOR_VER || *min_ver > PEER_MINOR_VER) { |
| 1795 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 1796 | appctx->st1 = PEER_SESS_SC_ERRVERSION; |
| 1797 | return -1; |
| 1798 | } |
| 1799 | |
| 1800 | return 1; |
| 1801 | } |
| 1802 | |
| 1803 | /* |
| 1804 | * Read and parse a second line of a "hello" peer protocol message. |
| 1805 | * Returns 0 if could not read a line, -1 if there was a read error or |
| 1806 | * the line is malformed, 1 if succeeded. |
| 1807 | */ |
| 1808 | static inline int peer_getline_host(struct appctx *appctx) |
| 1809 | { |
| 1810 | int reql; |
| 1811 | |
| 1812 | reql = peer_getline(appctx); |
| 1813 | if (!reql) |
| 1814 | return 0; |
| 1815 | |
| 1816 | if (reql < 0) |
| 1817 | return -1; |
| 1818 | |
| 1819 | /* test hostname match */ |
| 1820 | if (strcmp(localpeer, trash.area) != 0) { |
| 1821 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 1822 | appctx->st1 = PEER_SESS_SC_ERRHOST; |
| 1823 | return -1; |
| 1824 | } |
| 1825 | |
| 1826 | return 1; |
| 1827 | } |
| 1828 | |
| 1829 | /* |
| 1830 | * Read and parse a last line of a "hello" peer protocol message. |
| 1831 | * Returns 0 if could not read a character, -1 if there was a read error or |
| 1832 | * the line is malformed, 1 if succeeded. |
| 1833 | * Set <curpeer> accordingly (the remote peer sending the "hello" message). |
| 1834 | */ |
| 1835 | static inline int peer_getline_last(struct appctx *appctx, struct peer **curpeer) |
| 1836 | { |
| 1837 | char *p; |
| 1838 | int reql; |
| 1839 | struct peer *peer; |
| 1840 | struct stream_interface *si = appctx->owner; |
| 1841 | struct stream *s = si_strm(si); |
| 1842 | struct peers *peers = strm_fe(s)->parent; |
| 1843 | |
| 1844 | reql = peer_getline(appctx); |
| 1845 | if (!reql) |
| 1846 | return 0; |
| 1847 | |
| 1848 | if (reql < 0) |
| 1849 | return -1; |
| 1850 | |
| 1851 | /* parse line "<peer name> <pid> <relative_pid>" */ |
| 1852 | p = strchr(trash.area, ' '); |
| 1853 | if (!p) { |
| 1854 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 1855 | appctx->st1 = PEER_SESS_SC_ERRPROTO; |
| 1856 | return -1; |
| 1857 | } |
| 1858 | *p = 0; |
| 1859 | |
| 1860 | /* lookup known peer */ |
| 1861 | for (peer = peers->remote; peer; peer = peer->next) { |
| 1862 | if (strcmp(peer->id, trash.area) == 0) |
| 1863 | break; |
| 1864 | } |
| 1865 | |
| 1866 | /* if unknown peer */ |
| 1867 | if (!peer) { |
| 1868 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 1869 | appctx->st1 = PEER_SESS_SC_ERRPEER; |
| 1870 | return -1; |
| 1871 | } |
| 1872 | *curpeer = peer; |
| 1873 | |
| 1874 | return 1; |
| 1875 | } |
| 1876 | |
| 1877 | /* |
Frédéric Lécaille | 4b2fd9b | 2019-01-25 08:58:41 +0100 | [diff] [blame] | 1878 | * Init <peer> peer after having accepted it at peer protocol level. |
| 1879 | */ |
| 1880 | static inline void init_accepted_peer(struct peer *peer, struct peers *peers) |
| 1881 | { |
| 1882 | struct shared_table *st; |
| 1883 | |
| 1884 | /* Register status code */ |
| 1885 | peer->statuscode = PEER_SESS_SC_SUCCESSCODE; |
| 1886 | |
| 1887 | /* Awake main task */ |
| 1888 | task_wakeup(peers->sync_task, TASK_WOKEN_MSG); |
| 1889 | |
| 1890 | /* Init confirm counter */ |
| 1891 | peer->confirm = 0; |
| 1892 | |
| 1893 | /* Init cursors */ |
| 1894 | for (st = peer->tables; st ; st = st->next) { |
| 1895 | st->last_get = st->last_acked = 0; |
| 1896 | st->teaching_origin = st->last_pushed = st->update; |
| 1897 | } |
| 1898 | |
| 1899 | /* reset teaching and learning flags to 0 */ |
| 1900 | peer->flags &= PEER_TEACH_RESET; |
| 1901 | peer->flags &= PEER_LEARN_RESET; |
| 1902 | |
| 1903 | /* if current peer is local */ |
| 1904 | if (peer->local) { |
| 1905 | /* if current host need resyncfrom local and no process assined */ |
| 1906 | if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL && |
| 1907 | !(peers->flags & PEERS_F_RESYNC_ASSIGN)) { |
| 1908 | /* assign local peer for a lesson, consider lesson already requested */ |
| 1909 | peer->flags |= PEER_F_LEARN_ASSIGN; |
| 1910 | peers->flags |= (PEERS_F_RESYNC_ASSIGN|PEERS_F_RESYNC_PROCESS); |
| 1911 | } |
| 1912 | |
| 1913 | } |
| 1914 | else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE && |
| 1915 | !(peers->flags & PEERS_F_RESYNC_ASSIGN)) { |
| 1916 | /* assign peer for a lesson */ |
| 1917 | peer->flags |= PEER_F_LEARN_ASSIGN; |
| 1918 | peers->flags |= PEERS_F_RESYNC_ASSIGN; |
| 1919 | } |
| 1920 | } |
| 1921 | |
| 1922 | /* |
| 1923 | * Init <peer> peer after having connected it at peer protocol level. |
| 1924 | */ |
| 1925 | static inline void init_connected_peer(struct peer *peer, struct peers *peers) |
| 1926 | { |
| 1927 | struct shared_table *st; |
| 1928 | |
| 1929 | /* Init cursors */ |
| 1930 | for (st = peer->tables; st ; st = st->next) { |
| 1931 | st->last_get = st->last_acked = 0; |
| 1932 | st->teaching_origin = st->last_pushed = st->update; |
| 1933 | } |
| 1934 | |
| 1935 | /* Init confirm counter */ |
| 1936 | peer->confirm = 0; |
| 1937 | |
| 1938 | /* reset teaching and learning flags to 0 */ |
| 1939 | peer->flags &= PEER_TEACH_RESET; |
| 1940 | peer->flags &= PEER_LEARN_RESET; |
| 1941 | |
| 1942 | /* If current peer is local */ |
| 1943 | if (peer->local) { |
| 1944 | /* flag to start to teach lesson */ |
| 1945 | peer->flags |= PEER_F_TEACH_PROCESS; |
| 1946 | } |
| 1947 | else if ((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE && |
| 1948 | !(peers->flags & PEERS_F_RESYNC_ASSIGN)) { |
| 1949 | /* If peer is remote and resync from remote is needed, |
| 1950 | and no peer currently assigned */ |
| 1951 | |
| 1952 | /* assign peer for a lesson */ |
| 1953 | peer->flags |= PEER_F_LEARN_ASSIGN; |
| 1954 | peers->flags |= PEERS_F_RESYNC_ASSIGN; |
| 1955 | } |
| 1956 | } |
| 1957 | |
| 1958 | /* |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1959 | * IO Handler to handle message exchance with a peer |
| 1960 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1961 | static void peer_io_handler(struct appctx *appctx) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1962 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1963 | struct stream_interface *si = appctx->owner; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1964 | struct stream *s = si_strm(si); |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1965 | struct peers *curpeers = strm_fe(s)->parent; |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 1966 | struct peer *curpeer = NULL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1967 | int reql = 0; |
| 1968 | int repl = 0; |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 1969 | unsigned int maj_ver, min_ver; |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 1970 | int prev_state; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1971 | |
Joseph Herlant | 82b2f54 | 2018-11-15 12:19:14 -0800 | [diff] [blame] | 1972 | /* Check if the input buffer is available. */ |
Frédéric Lécaille | 7d0ceee | 2019-01-24 14:24:05 +0100 | [diff] [blame] | 1973 | if (si_ic(si)->buf.size == 0) { |
| 1974 | si_rx_room_blk(si); |
| 1975 | goto out; |
| 1976 | } |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 1977 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1978 | while (1) { |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 1979 | prev_state = appctx->st0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1980 | switchstate: |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 1981 | maj_ver = min_ver = (unsigned int)-1; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1982 | switch(appctx->st0) { |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1983 | case PEER_SESS_ST_ACCEPT: |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 1984 | prev_state = appctx->st0; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1985 | appctx->ctx.peers.ptr = NULL; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1986 | appctx->st0 = PEER_SESS_ST_GETVERSION; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1987 | /* fall through */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1988 | case PEER_SESS_ST_GETVERSION: |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 1989 | prev_state = appctx->st0; |
Frédéric Lécaille | 3f0fb9d | 2019-01-25 08:30:29 +0100 | [diff] [blame] | 1990 | reql = peer_getline_version(appctx, &maj_ver, &min_ver); |
| 1991 | if (reql <= 0) { |
| 1992 | if (!reql) |
| 1993 | goto out; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1994 | goto switchstate; |
| 1995 | } |
| 1996 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1997 | appctx->st0 = PEER_SESS_ST_GETHOST; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 1998 | /* fall through */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 1999 | case PEER_SESS_ST_GETHOST: |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 2000 | prev_state = appctx->st0; |
Frédéric Lécaille | 3f0fb9d | 2019-01-25 08:30:29 +0100 | [diff] [blame] | 2001 | reql = peer_getline_host(appctx); |
| 2002 | if (reql <= 0) { |
| 2003 | if (!reql) |
| 2004 | goto out; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2005 | goto switchstate; |
| 2006 | } |
| 2007 | |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 2008 | appctx->st0 = PEER_SESS_ST_GETPEER; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2009 | /* fall through */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 2010 | case PEER_SESS_ST_GETPEER: { |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 2011 | prev_state = appctx->st0; |
Frédéric Lécaille | 3f0fb9d | 2019-01-25 08:30:29 +0100 | [diff] [blame] | 2012 | reql = peer_getline_last(appctx, &curpeer); |
| 2013 | if (reql <= 0) { |
| 2014 | if (!reql) |
| 2015 | goto out; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2016 | goto switchstate; |
| 2017 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2018 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2019 | HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock); |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 2020 | if (curpeer->appctx && curpeer->appctx != appctx) { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2021 | if (curpeer->local) { |
| 2022 | /* Local connection, reply a retry */ |
| 2023 | appctx->st0 = PEER_SESS_ST_EXIT; |
| 2024 | appctx->st1 = PEER_SESS_SC_TRYAGAIN; |
| 2025 | goto switchstate; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2026 | } |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 2027 | |
| 2028 | /* we're killing a connection, we must apply a random delay before |
| 2029 | * retrying otherwise the other end will do the same and we can loop |
| 2030 | * for a while. |
| 2031 | */ |
| 2032 | curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000)); |
Willy Tarreau | 81bc3b0 | 2016-10-31 17:37:39 +0100 | [diff] [blame] | 2033 | peer_session_forceshutdown(curpeer->appctx); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2034 | } |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 2035 | if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) { |
| 2036 | if (min_ver == PEER_DWNGRD_MINOR_VER) { |
| 2037 | curpeer->flags |= PEER_F_DWNGRD; |
| 2038 | } |
| 2039 | else { |
| 2040 | curpeer->flags &= ~PEER_F_DWNGRD; |
| 2041 | } |
| 2042 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2043 | curpeer->appctx = appctx; |
| 2044 | appctx->ctx.peers.ptr = curpeer; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 2045 | appctx->st0 = PEER_SESS_ST_SENDSUCCESS; |
Olivier Houchard | ed87989 | 2019-03-08 18:53:43 +0100 | [diff] [blame] | 2046 | _HA_ATOMIC_ADD(&active_peers, 1); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2047 | /* fall through */ |
| 2048 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 2049 | case PEER_SESS_ST_SENDSUCCESS: { |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 2050 | prev_state = appctx->st0; |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 2051 | if (!curpeer) { |
| 2052 | curpeer = appctx->ctx.peers.ptr; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2053 | HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock); |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 2054 | if (curpeer->appctx != appctx) { |
| 2055 | appctx->st0 = PEER_SESS_ST_END; |
| 2056 | goto switchstate; |
| 2057 | } |
| 2058 | } |
Frédéric Lécaille | 7d0ceee | 2019-01-24 14:24:05 +0100 | [diff] [blame] | 2059 | |
| 2060 | repl = peer_send_status_successmsg(appctx); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2061 | if (repl <= 0) { |
| 2062 | if (repl == -1) |
Frédéric Lécaille | 7d0ceee | 2019-01-24 14:24:05 +0100 | [diff] [blame] | 2063 | goto out; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2064 | goto switchstate; |
| 2065 | } |
| 2066 | |
Frédéric Lécaille | 4b2fd9b | 2019-01-25 08:58:41 +0100 | [diff] [blame] | 2067 | init_accepted_peer(curpeer, curpeers); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2068 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2069 | /* switch to waiting message state */ |
Olivier Houchard | ed87989 | 2019-03-08 18:53:43 +0100 | [diff] [blame] | 2070 | _HA_ATOMIC_ADD(&connected_peers, 1); |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 2071 | appctx->st0 = PEER_SESS_ST_WAITMSG; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2072 | goto switchstate; |
| 2073 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 2074 | case PEER_SESS_ST_CONNECT: { |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 2075 | prev_state = appctx->st0; |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 2076 | if (!curpeer) { |
| 2077 | curpeer = appctx->ctx.peers.ptr; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2078 | HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock); |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 2079 | if (curpeer->appctx != appctx) { |
| 2080 | appctx->st0 = PEER_SESS_ST_END; |
| 2081 | goto switchstate; |
| 2082 | } |
| 2083 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2084 | |
Frédéric Lécaille | 7d0ceee | 2019-01-24 14:24:05 +0100 | [diff] [blame] | 2085 | repl = peer_send_hellomsg(appctx, curpeer); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2086 | if (repl <= 0) { |
| 2087 | if (repl == -1) |
Frédéric Lécaille | 7d0ceee | 2019-01-24 14:24:05 +0100 | [diff] [blame] | 2088 | goto out; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2089 | goto switchstate; |
| 2090 | } |
| 2091 | |
| 2092 | /* switch to the waiting statuscode state */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 2093 | appctx->st0 = PEER_SESS_ST_GETSTATUS; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2094 | /* fall through */ |
| 2095 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 2096 | case PEER_SESS_ST_GETSTATUS: { |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 2097 | prev_state = appctx->st0; |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 2098 | if (!curpeer) { |
| 2099 | curpeer = appctx->ctx.peers.ptr; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2100 | HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock); |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 2101 | if (curpeer->appctx != appctx) { |
| 2102 | appctx->st0 = PEER_SESS_ST_END; |
| 2103 | goto switchstate; |
| 2104 | } |
| 2105 | } |
| 2106 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 2107 | if (si_ic(si)->flags & CF_WRITE_PARTIAL) |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2108 | curpeer->statuscode = PEER_SESS_SC_CONNECTEDCODE; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2109 | |
Frédéric Lécaille | ce02557 | 2019-01-21 13:38:06 +0100 | [diff] [blame] | 2110 | reql = peer_getline(appctx); |
| 2111 | if (!reql) |
| 2112 | goto out; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2113 | |
Frédéric Lécaille | ce02557 | 2019-01-21 13:38:06 +0100 | [diff] [blame] | 2114 | if (reql < 0) |
| 2115 | goto switchstate; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2116 | |
| 2117 | /* Register status code */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2118 | curpeer->statuscode = atoi(trash.area); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2119 | |
| 2120 | /* Awake main task */ |
Frédéric Lécaille | ed2b4a6 | 2017-07-13 09:07:09 +0200 | [diff] [blame] | 2121 | task_wakeup(curpeers->sync_task, TASK_WOKEN_MSG); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2122 | |
| 2123 | /* If status code is success */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2124 | if (curpeer->statuscode == PEER_SESS_SC_SUCCESSCODE) { |
Frédéric Lécaille | 4b2fd9b | 2019-01-25 08:58:41 +0100 | [diff] [blame] | 2125 | init_connected_peer(curpeer, curpeers); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2126 | } |
| 2127 | else { |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 2128 | if (curpeer->statuscode == PEER_SESS_SC_ERRVERSION) |
| 2129 | curpeer->flags |= PEER_F_DWNGRD; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2130 | /* Status code is not success, abort */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 2131 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2132 | goto switchstate; |
| 2133 | } |
Olivier Houchard | ed87989 | 2019-03-08 18:53:43 +0100 | [diff] [blame] | 2134 | _HA_ATOMIC_ADD(&connected_peers, 1); |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 2135 | appctx->st0 = PEER_SESS_ST_WAITMSG; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2136 | /* fall through */ |
| 2137 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 2138 | case PEER_SESS_ST_WAITMSG: { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2139 | uint32_t msg_len = 0; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2140 | char *msg_cur = trash.area; |
| 2141 | char *msg_end = trash.area; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2142 | unsigned char msg_head[7]; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2143 | int totl = 0; |
| 2144 | |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 2145 | prev_state = appctx->st0; |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 2146 | if (!curpeer) { |
| 2147 | curpeer = appctx->ctx.peers.ptr; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2148 | HA_SPIN_LOCK(PEER_LOCK, &curpeer->lock); |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 2149 | if (curpeer->appctx != appctx) { |
| 2150 | appctx->st0 = PEER_SESS_ST_END; |
| 2151 | goto switchstate; |
| 2152 | } |
| 2153 | } |
| 2154 | |
Frédéric Lécaille | 95203f2 | 2019-01-23 19:38:11 +0100 | [diff] [blame] | 2155 | reql = peer_recv_msg(appctx, (char *)msg_head, sizeof msg_head, &msg_len, &totl); |
| 2156 | if (reql <= 0) { |
| 2157 | if (reql == -1) |
| 2158 | goto switchstate; |
Frédéric Lécaille | be825e5 | 2019-01-24 18:28:44 +0100 | [diff] [blame] | 2159 | goto send_msgs; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2160 | } |
Willy Tarreau | 86a446e | 2013-11-25 23:02:37 +0100 | [diff] [blame] | 2161 | |
Frédéric Lécaille | 95203f2 | 2019-01-23 19:38:11 +0100 | [diff] [blame] | 2162 | msg_end += msg_len; |
Frédéric Lécaille | 444243c | 2019-01-24 15:40:11 +0100 | [diff] [blame] | 2163 | if (!peer_treat_awaited_msg(appctx, curpeer, msg_head, &msg_cur, msg_end, msg_len, totl)) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2164 | goto switchstate; |
Frédéric Lécaille | be825e5 | 2019-01-24 18:28:44 +0100 | [diff] [blame] | 2165 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2166 | /* skip consumed message */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2167 | co_skip(si_oc(si), totl); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2168 | /* loop on that state to peek next message */ |
Willy Tarreau | 72d6c16 | 2013-04-11 16:14:13 +0200 | [diff] [blame] | 2169 | goto switchstate; |
| 2170 | |
Frédéric Lécaille | be825e5 | 2019-01-24 18:28:44 +0100 | [diff] [blame] | 2171 | send_msgs: |
Frédéric Lécaille | 645635d | 2019-02-11 17:49:39 +0100 | [diff] [blame] | 2172 | if (curpeer->flags & PEER_F_HEARTBEAT) { |
| 2173 | curpeer->flags &= ~PEER_F_HEARTBEAT; |
| 2174 | repl = peer_send_heartbeatmsg(appctx); |
| 2175 | if (repl <= 0) { |
| 2176 | if (repl == -1) |
| 2177 | goto out; |
| 2178 | goto switchstate; |
| 2179 | } |
| 2180 | } |
Frédéric Lécaille | be825e5 | 2019-01-24 18:28:44 +0100 | [diff] [blame] | 2181 | /* we get here when a peer_recv_msg() returns 0 in reql */ |
Frédéric Lécaille | 25e1d5e | 2019-01-24 17:33:48 +0100 | [diff] [blame] | 2182 | repl = peer_send_msgs(appctx, curpeer); |
| 2183 | if (repl <= 0) { |
| 2184 | if (repl == -1) |
| 2185 | goto out; |
| 2186 | goto switchstate; |
Emeric Brun | 597b26e | 2016-08-12 11:23:31 +0200 | [diff] [blame] | 2187 | } |
| 2188 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2189 | /* noting more to do */ |
| 2190 | goto out; |
| 2191 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 2192 | case PEER_SESS_ST_EXIT: |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 2193 | if (prev_state == PEER_SESS_ST_WAITMSG) |
Olivier Houchard | ed87989 | 2019-03-08 18:53:43 +0100 | [diff] [blame] | 2194 | _HA_ATOMIC_SUB(&connected_peers, 1); |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 2195 | prev_state = appctx->st0; |
Frédéric Lécaille | 7d0ceee | 2019-01-24 14:24:05 +0100 | [diff] [blame] | 2196 | if (peer_send_status_errormsg(appctx) == -1) |
| 2197 | goto out; |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 2198 | appctx->st0 = PEER_SESS_ST_END; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2199 | goto switchstate; |
| 2200 | case PEER_SESS_ST_ERRSIZE: { |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 2201 | if (prev_state == PEER_SESS_ST_WAITMSG) |
Olivier Houchard | ed87989 | 2019-03-08 18:53:43 +0100 | [diff] [blame] | 2202 | _HA_ATOMIC_SUB(&connected_peers, 1); |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 2203 | prev_state = appctx->st0; |
Frédéric Lécaille | 7d0ceee | 2019-01-24 14:24:05 +0100 | [diff] [blame] | 2204 | if (peer_send_error_size_limitmsg(appctx) == -1) |
| 2205 | goto out; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2206 | appctx->st0 = PEER_SESS_ST_END; |
| 2207 | goto switchstate; |
| 2208 | } |
| 2209 | case PEER_SESS_ST_ERRPROTO: { |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 2210 | if (prev_state == PEER_SESS_ST_WAITMSG) |
Olivier Houchard | ed87989 | 2019-03-08 18:53:43 +0100 | [diff] [blame] | 2211 | _HA_ATOMIC_SUB(&connected_peers, 1); |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 2212 | prev_state = appctx->st0; |
Frédéric Lécaille | 7d0ceee | 2019-01-24 14:24:05 +0100 | [diff] [blame] | 2213 | if (peer_send_error_protomsg(appctx) == -1) |
| 2214 | goto out; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2215 | appctx->st0 = PEER_SESS_ST_END; |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 2216 | prev_state = appctx->st0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2217 | /* fall through */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2218 | } |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 2219 | case PEER_SESS_ST_END: { |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 2220 | if (prev_state == PEER_SESS_ST_WAITMSG) |
Olivier Houchard | ed87989 | 2019-03-08 18:53:43 +0100 | [diff] [blame] | 2221 | _HA_ATOMIC_SUB(&connected_peers, 1); |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 2222 | prev_state = appctx->st0; |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 2223 | if (curpeer) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2224 | HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock); |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 2225 | curpeer = NULL; |
| 2226 | } |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 2227 | si_shutw(si); |
| 2228 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 2229 | si_ic(si)->flags |= CF_READ_NULL; |
Willy Tarreau | 828824a | 2015-04-19 17:20:03 +0200 | [diff] [blame] | 2230 | goto out; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2231 | } |
| 2232 | } |
| 2233 | } |
| 2234 | out: |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 2235 | si_oc(si)->flags |= CF_READ_DONTWAIT; |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 2236 | |
| 2237 | if (curpeer) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2238 | HA_SPIN_UNLOCK(PEER_LOCK, &curpeer->lock); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2239 | return; |
| 2240 | } |
| 2241 | |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2242 | static struct applet peer_applet = { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 2243 | .obj_type = OBJ_TYPE_APPLET, |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 2244 | .name = "<PEER>", /* used for logging */ |
| 2245 | .fct = peer_io_handler, |
Aman Gupta | 9a13e84 | 2012-04-02 18:57:53 -0700 | [diff] [blame] | 2246 | .release = peer_session_release, |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 2247 | }; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2248 | |
| 2249 | /* |
| 2250 | * Use this function to force a close of a peer session |
| 2251 | */ |
Willy Tarreau | 81bc3b0 | 2016-10-31 17:37:39 +0100 | [diff] [blame] | 2252 | static void peer_session_forceshutdown(struct appctx *appctx) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2253 | { |
Frédéric Lécaille | 5df1190 | 2017-06-13 16:39:57 +0200 | [diff] [blame] | 2254 | /* Note that the peer sessions which have just been created |
| 2255 | * (->st0 == PEER_SESS_ST_CONNECT) must not |
| 2256 | * be shutdown, if not, the TCP session will never be closed |
| 2257 | * and stay in CLOSE_WAIT state after having been closed by |
| 2258 | * the remote side. |
| 2259 | */ |
| 2260 | if (!appctx || appctx->st0 == PEER_SESS_ST_CONNECT) |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2261 | return; |
| 2262 | |
Willy Tarreau | 81bc3b0 | 2016-10-31 17:37:39 +0100 | [diff] [blame] | 2263 | if (appctx->applet != &peer_applet) |
| 2264 | return; |
| 2265 | |
Willy Tarreau | 2d372c2 | 2018-11-05 17:12:27 +0100 | [diff] [blame] | 2266 | if (appctx->st0 == PEER_SESS_ST_WAITMSG) |
Olivier Houchard | ed87989 | 2019-03-08 18:53:43 +0100 | [diff] [blame] | 2267 | _HA_ATOMIC_SUB(&connected_peers, 1); |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 2268 | appctx->st0 = PEER_SESS_ST_END; |
Willy Tarreau | 78c0c50 | 2016-10-31 17:32:20 +0100 | [diff] [blame] | 2269 | appctx_wakeup(appctx); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2270 | } |
| 2271 | |
Willy Tarreau | 91d9628 | 2015-03-13 15:47:26 +0100 | [diff] [blame] | 2272 | /* Pre-configures a peers frontend to accept incoming connections */ |
| 2273 | void peers_setup_frontend(struct proxy *fe) |
| 2274 | { |
| 2275 | fe->last_change = now.tv_sec; |
Frédéric Lécaille | c06b5d4 | 2018-04-26 10:06:41 +0200 | [diff] [blame] | 2276 | fe->cap = PR_CAP_FE | PR_CAP_BE; |
Willy Tarreau | 91d9628 | 2015-03-13 15:47:26 +0100 | [diff] [blame] | 2277 | fe->maxconn = 0; |
| 2278 | fe->conn_retries = CONN_RETRIES; |
| 2279 | fe->timeout.client = MS_TO_TICKS(5000); |
Willy Tarreau | d1d48d4 | 2015-03-13 16:15:46 +0100 | [diff] [blame] | 2280 | fe->accept = frontend_accept; |
Willy Tarreau | f87ab94 | 2015-03-13 15:55:16 +0100 | [diff] [blame] | 2281 | fe->default_target = &peer_applet.obj_type; |
Willy Tarreau | 91d9628 | 2015-03-13 15:47:26 +0100 | [diff] [blame] | 2282 | fe->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC; |
Willy Tarreau | 0fca483 | 2015-05-01 19:12:05 +0200 | [diff] [blame] | 2283 | fe->bind_proc = 0; /* will be filled by users */ |
Willy Tarreau | 91d9628 | 2015-03-13 15:47:26 +0100 | [diff] [blame] | 2284 | } |
| 2285 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2286 | /* |
Willy Tarreau | bd55e31 | 2010-11-11 10:55:09 +0100 | [diff] [blame] | 2287 | * Create a new peer session in assigned state (connect will start automatically) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2288 | */ |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 2289 | static struct appctx *peer_session_create(struct peers *peers, struct peer *peer) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2290 | { |
Willy Tarreau | 04b9286 | 2017-09-15 11:01:04 +0200 | [diff] [blame] | 2291 | struct proxy *p = peers->peers_fe; /* attached frontend */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2292 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2293 | struct session *sess; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2294 | struct stream *s; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 2295 | struct connection *conn; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2296 | struct conn_stream *cs; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2297 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2298 | peer->reconnect = tick_add(now_ms, MS_TO_TICKS(5000)); |
Frédéric Lécaille | 645635d | 2019-02-11 17:49:39 +0100 | [diff] [blame] | 2299 | peer->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT)); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2300 | peer->statuscode = PEER_SESS_SC_CONNECTCODE; |
Willy Tarreau | d990baf | 2015-04-05 00:32:03 +0200 | [diff] [blame] | 2301 | s = NULL; |
| 2302 | |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 2303 | appctx = appctx_new(&peer_applet, tid_bit); |
Willy Tarreau | d990baf | 2015-04-05 00:32:03 +0200 | [diff] [blame] | 2304 | if (!appctx) |
| 2305 | goto out_close; |
| 2306 | |
| 2307 | appctx->st0 = PEER_SESS_ST_CONNECT; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2308 | appctx->ctx.peers.ptr = (void *)peer; |
Willy Tarreau | d990baf | 2015-04-05 00:32:03 +0200 | [diff] [blame] | 2309 | |
Willy Tarreau | 04b9286 | 2017-09-15 11:01:04 +0200 | [diff] [blame] | 2310 | sess = session_new(p, NULL, &appctx->obj_type); |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2311 | if (!sess) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2312 | ha_alert("out of memory in peer_session_create().\n"); |
Willy Tarreau | d990baf | 2015-04-05 00:32:03 +0200 | [diff] [blame] | 2313 | goto out_free_appctx; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2314 | } |
| 2315 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2316 | if ((s = stream_new(sess, &appctx->obj_type)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2317 | ha_alert("Failed to initialize stream in peer_session_create().\n"); |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2318 | goto out_free_sess; |
Willy Tarreau | 8baf906 | 2015-04-05 00:46:36 +0200 | [diff] [blame] | 2319 | } |
| 2320 | |
Willy Tarreau | 342bfb1 | 2015-04-05 01:35:34 +0200 | [diff] [blame] | 2321 | /* The tasks below are normally what is supposed to be done by |
| 2322 | * fe->accept(). |
| 2323 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2324 | s->flags = SF_ASSIGNED|SF_ADDR_SET; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2325 | |
Willy Tarreau | 6e2979c | 2015-04-27 13:21:15 +0200 | [diff] [blame] | 2326 | /* applet is waiting for data */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2327 | si_cant_get(&s->si[0]); |
Willy Tarreau | 6e2979c | 2015-04-27 13:21:15 +0200 | [diff] [blame] | 2328 | appctx_wakeup(appctx); |
| 2329 | |
Willy Tarreau | 3ed35ef | 2013-10-24 11:51:38 +0200 | [diff] [blame] | 2330 | /* initiate an outgoing connection */ |
Willy Tarreau | dbd0267 | 2017-12-06 17:39:53 +0100 | [diff] [blame] | 2331 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 3ed35ef | 2013-10-24 11:51:38 +0200 | [diff] [blame] | 2332 | si_set_state(&s->si[1], SI_ST_ASS); |
Willy Tarreau | 3ed35ef | 2013-10-24 11:51:38 +0200 | [diff] [blame] | 2333 | |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 2334 | /* automatically prepare the stream interface to connect to the |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2335 | * pre-initialized connection in si->conn. |
| 2336 | */ |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 2337 | if (unlikely((conn = conn_new()) == NULL)) |
Willy Tarreau | 8baf906 | 2015-04-05 00:46:36 +0200 | [diff] [blame] | 2338 | goto out_free_strm; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 2339 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2340 | if (unlikely((cs = cs_new(conn)) == NULL)) |
| 2341 | goto out_free_conn; |
| 2342 | |
Frédéric Lécaille | 1055e68 | 2018-04-26 14:35:21 +0200 | [diff] [blame] | 2343 | conn->target = s->target = peer_session_target(peer, s); |
Willy Tarreau | be37315 | 2018-09-06 11:45:30 +0200 | [diff] [blame] | 2344 | memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to)); |
| 2345 | |
Frédéric Lécaille | 1055e68 | 2018-04-26 14:35:21 +0200 | [diff] [blame] | 2346 | conn_prepare(conn, peer->proto, peer_xprt(peer)); |
Olivier Houchard | ef60ff3 | 2019-01-29 19:00:33 +0100 | [diff] [blame] | 2347 | if (conn_install_mux(conn, &mux_pt_ops, cs, s->be, NULL) < 0) |
| 2348 | goto out_free_cs; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2349 | si_attach_cs(&s->si[1], cs); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 2350 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2351 | s->do_log = NULL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2352 | s->uniq_id = 0; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2353 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2354 | s->res.flags |= CF_READ_DONTWAIT; |
Willy Tarreau | 696a291 | 2014-11-24 11:36:57 +0100 | [diff] [blame] | 2355 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2356 | peer->appctx = appctx; |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2357 | task_wakeup(s->task, TASK_WOKEN_INIT); |
Olivier Houchard | ed87989 | 2019-03-08 18:53:43 +0100 | [diff] [blame] | 2358 | _HA_ATOMIC_ADD(&active_peers, 1); |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 2359 | return appctx; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2360 | |
| 2361 | /* Error unrolling */ |
Olivier Houchard | ef60ff3 | 2019-01-29 19:00:33 +0100 | [diff] [blame] | 2362 | out_free_cs: |
| 2363 | cs_free(cs); |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2364 | out_free_conn: |
| 2365 | conn_free(conn); |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2366 | out_free_strm: |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2367 | LIST_DEL(&s->list); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2368 | pool_free(pool_head_stream, s); |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2369 | out_free_sess: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2370 | session_free(sess); |
Willy Tarreau | d990baf | 2015-04-05 00:32:03 +0200 | [diff] [blame] | 2371 | out_free_appctx: |
| 2372 | appctx_free(appctx); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2373 | out_close: |
Willy Tarreau | b21d08e | 2016-10-31 17:46:57 +0100 | [diff] [blame] | 2374 | return NULL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2375 | } |
| 2376 | |
| 2377 | /* |
Frédéric Lécaille | 645635d | 2019-02-11 17:49:39 +0100 | [diff] [blame] | 2378 | * Task processing function to manage re-connect, peer session |
| 2379 | * tasks wakeup on local update and heartbeat. |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2380 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2381 | static struct task *process_peer_sync(struct task * task, void *context, unsigned short state) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2382 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2383 | struct peers *peers = context; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2384 | struct peer *ps; |
| 2385 | struct shared_table *st; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2386 | |
| 2387 | task->expire = TICK_ETERNITY; |
| 2388 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2389 | if (!peers->peers_fe) { |
Willy Tarreau | 46dc1ca | 2015-05-01 18:32:13 +0200 | [diff] [blame] | 2390 | /* this one was never started, kill it */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2391 | signal_unregister_handler(peers->sighandler); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2392 | task_delete(peers->sync_task); |
| 2393 | task_free(peers->sync_task); |
Willy Tarreau | 37bb7be | 2015-09-21 15:24:58 +0200 | [diff] [blame] | 2394 | peers->sync_task = NULL; |
Willy Tarreau | 46dc1ca | 2015-05-01 18:32:13 +0200 | [diff] [blame] | 2395 | return NULL; |
| 2396 | } |
| 2397 | |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 2398 | /* Acquire lock for all peers of the section */ |
| 2399 | for (ps = peers->remote; ps; ps = ps->next) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2400 | HA_SPIN_LOCK(PEER_LOCK, &ps->lock); |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 2401 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2402 | if (!stopping) { |
| 2403 | /* Normal case (not soft stop)*/ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2404 | |
| 2405 | if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMLOCAL) && |
| 2406 | (!nb_oldpids || tick_is_expired(peers->resync_timeout, now_ms)) && |
| 2407 | !(peers->flags & PEERS_F_RESYNC_ASSIGN)) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2408 | /* Resync from local peer needed |
| 2409 | no peer was assigned for the lesson |
| 2410 | and no old local peer found |
| 2411 | or resync timeout expire */ |
| 2412 | |
| 2413 | /* flag no more resync from local, to try resync from remotes */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2414 | peers->flags |= PEERS_F_RESYNC_LOCAL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2415 | |
| 2416 | /* reschedule a resync */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2417 | peers->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2418 | } |
| 2419 | |
| 2420 | /* For each session */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2421 | for (ps = peers->remote; ps; ps = ps->next) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2422 | /* For each remote peers */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2423 | if (!ps->local) { |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 2424 | if (!ps->appctx) { |
| 2425 | /* no active peer connection */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2426 | if (ps->statuscode == 0 || |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 2427 | ((ps->statuscode == PEER_SESS_SC_CONNECTCODE || |
Willy Tarreau | b4e34da | 2015-05-20 10:39:04 +0200 | [diff] [blame] | 2428 | ps->statuscode == PEER_SESS_SC_SUCCESSCODE || |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 2429 | ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) && |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2430 | tick_is_expired(ps->reconnect, now_ms))) { |
| 2431 | /* connection never tried |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 2432 | * or previous peer connection established with success |
| 2433 | * or previous peer connection failed while connecting |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2434 | * and reconnection timer is expired */ |
| 2435 | |
| 2436 | /* retry a connect */ |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 2437 | ps->appctx = peer_session_create(peers, ps); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2438 | } |
Willy Tarreau | b4e34da | 2015-05-20 10:39:04 +0200 | [diff] [blame] | 2439 | else if (!tick_is_expired(ps->reconnect, now_ms)) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2440 | /* If previous session failed during connection |
| 2441 | * but reconnection timer is not expired */ |
| 2442 | |
| 2443 | /* reschedule task for reconnect */ |
| 2444 | task->expire = tick_first(task->expire, ps->reconnect); |
| 2445 | } |
| 2446 | /* else do nothing */ |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 2447 | } /* !ps->appctx */ |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 2448 | else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) { |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 2449 | /* current peer connection is active and established */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2450 | if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) && |
| 2451 | !(peers->flags & PEERS_F_RESYNC_ASSIGN) && |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2452 | !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) { |
| 2453 | /* Resync from a remote is needed |
| 2454 | * and no peer was assigned for lesson |
| 2455 | * and current peer may be up2date */ |
| 2456 | |
| 2457 | /* assign peer for the lesson */ |
| 2458 | ps->flags |= PEER_F_LEARN_ASSIGN; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2459 | peers->flags |= PEERS_F_RESYNC_ASSIGN; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2460 | |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 2461 | /* wake up peer handler to handle a request of resync */ |
Willy Tarreau | e5843b3 | 2015-04-27 18:40:14 +0200 | [diff] [blame] | 2462 | appctx_wakeup(ps->appctx); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2463 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2464 | else { |
Frédéric Lécaille | 645635d | 2019-02-11 17:49:39 +0100 | [diff] [blame] | 2465 | int update_to_push = 0; |
| 2466 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2467 | /* Awake session if there is data to push */ |
| 2468 | for (st = ps->tables; st ; st = st->next) { |
| 2469 | if ((int)(st->last_pushed - st->table->localupdate) < 0) { |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 2470 | /* wake up the peer handler to push local updates */ |
Frédéric Lécaille | 645635d | 2019-02-11 17:49:39 +0100 | [diff] [blame] | 2471 | update_to_push = 1; |
| 2472 | ps->flags &= ~PEER_F_HEARTBEAT; |
| 2473 | ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT)); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2474 | appctx_wakeup(ps->appctx); |
| 2475 | break; |
| 2476 | } |
| 2477 | } |
Frédéric Lécaille | 645635d | 2019-02-11 17:49:39 +0100 | [diff] [blame] | 2478 | if (!update_to_push && tick_is_expired(ps->heartbeat, now_ms)) { |
| 2479 | ps->heartbeat = tick_add(now_ms, MS_TO_TICKS(PEER_HEARTBEAT_TIMEOUT)); |
| 2480 | ps->flags |= PEER_F_HEARTBEAT; |
| 2481 | appctx_wakeup(ps->appctx); |
| 2482 | } |
| 2483 | task->expire = tick_first(task->expire, ps->heartbeat); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2484 | } |
| 2485 | /* else do nothing */ |
| 2486 | } /* SUCCESSCODE */ |
| 2487 | } /* !ps->peer->local */ |
| 2488 | } /* for */ |
| 2489 | |
| 2490 | /* Resync from remotes expired: consider resync is finished */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2491 | if (((peers->flags & PEERS_RESYNC_STATEMASK) == PEERS_RESYNC_FROMREMOTE) && |
| 2492 | !(peers->flags & PEERS_F_RESYNC_ASSIGN) && |
| 2493 | tick_is_expired(peers->resync_timeout, now_ms)) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2494 | /* Resync from remote peer needed |
| 2495 | * no peer was assigned for the lesson |
| 2496 | * and resync timeout expire */ |
| 2497 | |
| 2498 | /* flag no more resync from remote, consider resync is finished */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2499 | peers->flags |= PEERS_F_RESYNC_REMOTE; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2500 | } |
| 2501 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2502 | if ((peers->flags & PEERS_RESYNC_STATEMASK) != PEERS_RESYNC_FINISHED) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2503 | /* Resync not finished*/ |
Frédéric Lécaille | 5d6e5f8 | 2017-05-29 13:47:16 +0200 | [diff] [blame] | 2504 | /* reschedule task to resync timeout if not expired, to ended resync if needed */ |
| 2505 | if (!tick_is_expired(peers->resync_timeout, now_ms)) |
| 2506 | task->expire = tick_first(task->expire, peers->resync_timeout); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2507 | } |
| 2508 | } /* !stopping */ |
| 2509 | else { |
| 2510 | /* soft stop case */ |
Willy Tarreau | 086735a | 2018-11-05 15:09:47 +0100 | [diff] [blame] | 2511 | if (state & TASK_WOKEN_SIGNAL) { |
Joseph Herlant | 82b2f54 | 2018-11-15 12:19:14 -0800 | [diff] [blame] | 2512 | /* We've just received the signal */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2513 | if (!(peers->flags & PEERS_F_DONOTSTOP)) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2514 | /* add DO NOT STOP flag if not present */ |
Olivier Houchard | ed87989 | 2019-03-08 18:53:43 +0100 | [diff] [blame] | 2515 | _HA_ATOMIC_ADD(&jobs, 1); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2516 | peers->flags |= PEERS_F_DONOTSTOP; |
| 2517 | ps = peers->local; |
| 2518 | for (st = ps->tables; st ; st = st->next) |
| 2519 | st->table->syncing++; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2520 | } |
| 2521 | |
| 2522 | /* disconnect all connected peers */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2523 | for (ps = peers->remote; ps; ps = ps->next) { |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 2524 | /* we're killing a connection, we must apply a random delay before |
| 2525 | * retrying otherwise the other end will do the same and we can loop |
| 2526 | * for a while. |
| 2527 | */ |
| 2528 | ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000)); |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 2529 | if (ps->appctx) { |
Willy Tarreau | 81bc3b0 | 2016-10-31 17:37:39 +0100 | [diff] [blame] | 2530 | peer_session_forceshutdown(ps->appctx); |
Willy Tarreau | e5843b3 | 2015-04-27 18:40:14 +0200 | [diff] [blame] | 2531 | ps->appctx = NULL; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2532 | } |
| 2533 | } |
| 2534 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2535 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2536 | ps = peers->local; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2537 | if (ps->flags & PEER_F_TEACH_COMPLETE) { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2538 | if (peers->flags & PEERS_F_DONOTSTOP) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2539 | /* resync of new process was complete, current process can die now */ |
Olivier Houchard | ed87989 | 2019-03-08 18:53:43 +0100 | [diff] [blame] | 2540 | _HA_ATOMIC_SUB(&jobs, 1); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2541 | peers->flags &= ~PEERS_F_DONOTSTOP; |
| 2542 | for (st = ps->tables; st ; st = st->next) |
| 2543 | st->table->syncing--; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2544 | } |
| 2545 | } |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 2546 | else if (!ps->appctx) { |
| 2547 | /* If there's no active peer connection */ |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2548 | if (ps->statuscode == 0 || |
Willy Tarreau | e4d927a | 2013-12-01 12:47:35 +0100 | [diff] [blame] | 2549 | ps->statuscode == PEER_SESS_SC_SUCCESSCODE || |
| 2550 | ps->statuscode == PEER_SESS_SC_CONNECTEDCODE || |
| 2551 | ps->statuscode == PEER_SESS_SC_TRYAGAIN) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2552 | /* connection never tried |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 2553 | * or previous peer connection was successfully established |
| 2554 | * or previous tcp connect succeeded but init state incomplete |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2555 | * or during previous connect, peer replies a try again statuscode */ |
| 2556 | |
| 2557 | /* connect to the peer */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2558 | peer_session_create(peers, ps); |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2559 | } |
| 2560 | else { |
| 2561 | /* Other error cases */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2562 | if (peers->flags & PEERS_F_DONOTSTOP) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2563 | /* unable to resync new process, current process can die now */ |
Olivier Houchard | ed87989 | 2019-03-08 18:53:43 +0100 | [diff] [blame] | 2564 | _HA_ATOMIC_SUB(&jobs, 1); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2565 | peers->flags &= ~PEERS_F_DONOTSTOP; |
| 2566 | for (st = ps->tables; st ; st = st->next) |
| 2567 | st->table->syncing--; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2568 | } |
| 2569 | } |
| 2570 | } |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2571 | else if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE ) { |
Willy Tarreau | 9df94c2 | 2016-10-31 18:42:52 +0100 | [diff] [blame] | 2572 | /* current peer connection is active and established |
| 2573 | * wake up all peer handlers to push remaining local updates */ |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2574 | for (st = ps->tables; st ; st = st->next) { |
| 2575 | if ((int)(st->last_pushed - st->table->localupdate) < 0) { |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2576 | appctx_wakeup(ps->appctx); |
| 2577 | break; |
| 2578 | } |
| 2579 | } |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2580 | } |
| 2581 | } /* stopping */ |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 2582 | |
| 2583 | /* Release lock for all peers of the section */ |
| 2584 | for (ps = peers->remote; ps; ps = ps->next) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2585 | HA_SPIN_UNLOCK(PEER_LOCK, &ps->lock); |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 2586 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2587 | /* Wakeup for re-connect */ |
| 2588 | return task; |
| 2589 | } |
| 2590 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2591 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2592 | /* |
Willy Tarreau | d944344 | 2018-10-15 11:18:03 +0200 | [diff] [blame] | 2593 | * returns 0 in case of error. |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2594 | */ |
Willy Tarreau | d944344 | 2018-10-15 11:18:03 +0200 | [diff] [blame] | 2595 | int peers_init_sync(struct peers *peers) |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2596 | { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2597 | struct peer * curpeer; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2598 | |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2599 | for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) { |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2600 | peers->peers_fe->maxconn += 3; |
| 2601 | } |
| 2602 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 2603 | peers->sync_task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | d944344 | 2018-10-15 11:18:03 +0200 | [diff] [blame] | 2604 | if (!peers->sync_task) |
| 2605 | return 0; |
| 2606 | |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2607 | peers->sync_task->process = process_peer_sync; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2608 | peers->sync_task->context = (void *)peers; |
| 2609 | peers->sighandler = signal_register_task(0, peers->sync_task, 0); |
| 2610 | task_wakeup(peers->sync_task, TASK_WOKEN_INIT); |
Willy Tarreau | d944344 | 2018-10-15 11:18:03 +0200 | [diff] [blame] | 2611 | return 1; |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | |
| 2615 | |
| 2616 | /* |
| 2617 | * Function used to register a table for sync on a group of peers |
| 2618 | * |
| 2619 | */ |
| 2620 | void peers_register_table(struct peers *peers, struct stktable *table) |
| 2621 | { |
| 2622 | struct shared_table *st; |
| 2623 | struct peer * curpeer; |
| 2624 | int id = 0; |
| 2625 | |
| 2626 | for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 2627 | st = calloc(1,sizeof(*st)); |
Emeric Brun | b3971ab | 2015-05-12 18:49:09 +0200 | [diff] [blame] | 2628 | st->table = table; |
| 2629 | st->next = curpeer->tables; |
| 2630 | if (curpeer->tables) |
| 2631 | id = curpeer->tables->local_id; |
| 2632 | st->local_id = id + 1; |
| 2633 | |
| 2634 | curpeer->tables = st; |
| 2635 | } |
| 2636 | |
| 2637 | table->sync_task = peers->sync_task; |
Emeric Brun | 2b920a1 | 2010-09-23 18:30:22 +0200 | [diff] [blame] | 2638 | } |
| 2639 | |