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