Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2016, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Craig Tiller | 14e0d98 | 2016-03-28 17:11:26 -0700 | [diff] [blame] | 34 | #include "src/core/ext/lb_policy/grpclb/load_balancer_api.h" |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 35 | #include "third_party/nanopb/pb_decode.h" |
| 36 | #include "third_party/nanopb/pb_encode.h" |
| 37 | |
| 38 | #include <grpc/support/alloc.h> |
| 39 | |
| 40 | typedef struct decode_serverlist_arg { |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 41 | /* The first pass counts the number of servers in the server list. The second |
| 42 | * one allocates and decodes. */ |
| 43 | bool first_pass; |
| 44 | /* The decoding callback is invoked once per server in serverlist. Remember |
| 45 | * which index of the serverlist are we currently decoding */ |
| 46 | size_t decoding_idx; |
| 47 | /* Populated after the first pass. Number of server in the input serverlist */ |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 48 | size_t num_servers; |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 49 | /* The decoded serverlist */ |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 50 | grpc_grpclb_server **servers; |
| 51 | } decode_serverlist_arg; |
| 52 | |
| 53 | /* invoked once for every Server in ServerList */ |
| 54 | static bool decode_serverlist(pb_istream_t *stream, const pb_field_t *field, |
| 55 | void **arg) { |
| 56 | decode_serverlist_arg *dec_arg = *arg; |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 57 | if (dec_arg->first_pass) { /* count how many server do we have */ |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 58 | grpc_grpclb_server server; |
David Garcia Quintas | 7f0793a | 2016-04-25 12:35:58 -0700 | [diff] [blame] | 59 | if (!pb_decode(stream, grpc_lb_v1_Server_fields, &server)) { |
David Garcia Quintas | 18bc43b | 2016-08-22 15:05:32 -0700 | [diff] [blame] | 60 | gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(stream)); |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 61 | return false; |
| 62 | } |
| 63 | dec_arg->num_servers++; |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 64 | } else { /* second pass. Actually decode. */ |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 65 | grpc_grpclb_server *server = gpr_malloc(sizeof(grpc_grpclb_server)); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 66 | memset(server, 0, sizeof(grpc_grpclb_server)); |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 67 | GPR_ASSERT(dec_arg->num_servers > 0); |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 68 | if (dec_arg->decoding_idx == 0) { /* first iteration of second pass */ |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 69 | dec_arg->servers = |
| 70 | gpr_malloc(sizeof(grpc_grpclb_server *) * dec_arg->num_servers); |
| 71 | } |
David Garcia Quintas | 7f0793a | 2016-04-25 12:35:58 -0700 | [diff] [blame] | 72 | if (!pb_decode(stream, grpc_lb_v1_Server_fields, server)) { |
David Garcia Quintas | 18bc43b | 2016-08-22 15:05:32 -0700 | [diff] [blame] | 73 | gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(stream)); |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 74 | return false; |
| 75 | } |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 76 | dec_arg->servers[dec_arg->decoding_idx++] = server; |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | grpc_grpclb_request *grpc_grpclb_request_create(const char *lb_service_name) { |
| 83 | grpc_grpclb_request *req = gpr_malloc(sizeof(grpc_grpclb_request)); |
| 84 | |
| 85 | req->has_client_stats = 0; /* TODO(dgq): add support for stats once defined */ |
| 86 | req->has_initial_request = 1; |
| 87 | req->initial_request.has_name = 1; |
| 88 | strncpy(req->initial_request.name, lb_service_name, |
| 89 | GRPC_GRPCLB_SERVICE_NAME_MAX_LENGTH); |
| 90 | return req; |
| 91 | } |
| 92 | |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 93 | grpc_slice grpc_grpclb_request_encode(const grpc_grpclb_request *request) { |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 94 | size_t encoded_length; |
| 95 | pb_ostream_t sizestream; |
| 96 | pb_ostream_t outputstream; |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 97 | grpc_slice slice; |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 98 | memset(&sizestream, 0, sizeof(pb_ostream_t)); |
David Garcia Quintas | 7f0793a | 2016-04-25 12:35:58 -0700 | [diff] [blame] | 99 | pb_encode(&sizestream, grpc_lb_v1_LoadBalanceRequest_fields, request); |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 100 | encoded_length = sizestream.bytes_written; |
| 101 | |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 102 | slice = grpc_slice_malloc(encoded_length); |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 103 | outputstream = |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 104 | pb_ostream_from_buffer(GRPC_SLICE_START_PTR(slice), encoded_length); |
David Garcia Quintas | 7f0793a | 2016-04-25 12:35:58 -0700 | [diff] [blame] | 105 | GPR_ASSERT(pb_encode(&outputstream, grpc_lb_v1_LoadBalanceRequest_fields, |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 106 | request) != 0); |
| 107 | return slice; |
| 108 | } |
| 109 | |
| 110 | void grpc_grpclb_request_destroy(grpc_grpclb_request *request) { |
| 111 | gpr_free(request); |
| 112 | } |
| 113 | |
David Garcia Quintas | 4166cb0 | 2016-07-29 14:33:15 -0700 | [diff] [blame] | 114 | typedef grpc_lb_v1_LoadBalanceResponse grpc_grpclb_response; |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 115 | grpc_grpclb_initial_response *grpc_grpclb_initial_response_parse( |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 116 | grpc_slice encoded_grpc_grpclb_response) { |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 117 | pb_istream_t stream = |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 118 | pb_istream_from_buffer(GRPC_SLICE_START_PTR(encoded_grpc_grpclb_response), |
| 119 | GRPC_SLICE_LENGTH(encoded_grpc_grpclb_response)); |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 120 | grpc_grpclb_response res; |
| 121 | memset(&res, 0, sizeof(grpc_grpclb_response)); |
| 122 | if (!pb_decode(&stream, grpc_lb_v1_LoadBalanceResponse_fields, &res)) { |
David Garcia Quintas | 18bc43b | 2016-08-22 15:05:32 -0700 | [diff] [blame] | 123 | gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&stream)); |
David Garcia Quintas | bf2e73d | 2016-04-04 18:03:47 -0700 | [diff] [blame] | 124 | return NULL; |
| 125 | } |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 126 | grpc_grpclb_initial_response *initial_res = |
| 127 | gpr_malloc(sizeof(grpc_grpclb_initial_response)); |
| 128 | memcpy(initial_res, &res.initial_response, |
| 129 | sizeof(grpc_grpclb_initial_response)); |
| 130 | |
| 131 | return initial_res; |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | grpc_grpclb_serverlist *grpc_grpclb_response_parse_serverlist( |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 135 | grpc_slice encoded_grpc_grpclb_response) { |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 136 | bool status; |
| 137 | decode_serverlist_arg arg; |
| 138 | pb_istream_t stream = |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 139 | pb_istream_from_buffer(GRPC_SLICE_START_PTR(encoded_grpc_grpclb_response), |
| 140 | GRPC_SLICE_LENGTH(encoded_grpc_grpclb_response)); |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 141 | pb_istream_t stream_at_start = stream; |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 142 | grpc_grpclb_response res; |
| 143 | memset(&res, 0, sizeof(grpc_grpclb_response)); |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 144 | memset(&arg, 0, sizeof(decode_serverlist_arg)); |
| 145 | |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 146 | res.server_list.servers.funcs.decode = decode_serverlist; |
| 147 | res.server_list.servers.arg = &arg; |
| 148 | arg.first_pass = true; |
| 149 | status = pb_decode(&stream, grpc_lb_v1_LoadBalanceResponse_fields, &res); |
David Garcia Quintas | bf2e73d | 2016-04-04 18:03:47 -0700 | [diff] [blame] | 150 | if (!status) { |
David Garcia Quintas | 18bc43b | 2016-08-22 15:05:32 -0700 | [diff] [blame] | 151 | gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&stream)); |
David Garcia Quintas | bf2e73d | 2016-04-04 18:03:47 -0700 | [diff] [blame] | 152 | return NULL; |
| 153 | } |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 154 | |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 155 | arg.first_pass = false; |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 156 | status = |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 157 | pb_decode(&stream_at_start, grpc_lb_v1_LoadBalanceResponse_fields, &res); |
David Garcia Quintas | bf2e73d | 2016-04-04 18:03:47 -0700 | [diff] [blame] | 158 | if (!status) { |
David Garcia Quintas | 18bc43b | 2016-08-22 15:05:32 -0700 | [diff] [blame] | 159 | gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&stream)); |
David Garcia Quintas | bf2e73d | 2016-04-04 18:03:47 -0700 | [diff] [blame] | 160 | return NULL; |
| 161 | } |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 162 | |
David Garcia Quintas | bf2e73d | 2016-04-04 18:03:47 -0700 | [diff] [blame] | 163 | grpc_grpclb_serverlist *sl = gpr_malloc(sizeof(grpc_grpclb_serverlist)); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 164 | memset(sl, 0, sizeof(*sl)); |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 165 | sl->num_servers = arg.num_servers; |
| 166 | sl->servers = arg.servers; |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 167 | if (res.server_list.has_expiration_interval) { |
| 168 | sl->expiration_interval = res.server_list.expiration_interval; |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 169 | } |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 170 | return sl; |
| 171 | } |
| 172 | |
| 173 | void grpc_grpclb_destroy_serverlist(grpc_grpclb_serverlist *serverlist) { |
David Garcia Quintas | bf2e73d | 2016-04-04 18:03:47 -0700 | [diff] [blame] | 174 | if (serverlist == NULL) { |
| 175 | return; |
| 176 | } |
| 177 | for (size_t i = 0; i < serverlist->num_servers; i++) { |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 178 | gpr_free(serverlist->servers[i]); |
| 179 | } |
| 180 | gpr_free(serverlist->servers); |
| 181 | gpr_free(serverlist); |
| 182 | } |
| 183 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 184 | grpc_grpclb_serverlist *grpc_grpclb_serverlist_copy( |
| 185 | const grpc_grpclb_serverlist *sl) { |
| 186 | grpc_grpclb_serverlist *copy = gpr_malloc(sizeof(grpc_grpclb_serverlist)); |
| 187 | memset(copy, 0, sizeof(grpc_grpclb_serverlist)); |
| 188 | copy->num_servers = sl->num_servers; |
| 189 | memcpy(©->expiration_interval, &sl->expiration_interval, |
| 190 | sizeof(grpc_grpclb_duration)); |
David Garcia Quintas | 8782d1b | 2016-06-15 23:58:44 -0700 | [diff] [blame] | 191 | copy->servers = gpr_malloc(sizeof(grpc_grpclb_server *) * sl->num_servers); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 192 | for (size_t i = 0; i < sl->num_servers; i++) { |
| 193 | copy->servers[i] = gpr_malloc(sizeof(grpc_grpclb_server)); |
| 194 | memcpy(copy->servers[i], sl->servers[i], sizeof(grpc_grpclb_server)); |
| 195 | } |
| 196 | return copy; |
| 197 | } |
| 198 | |
| 199 | bool grpc_grpclb_serverlist_equals(const grpc_grpclb_serverlist *lhs, |
| 200 | const grpc_grpclb_serverlist *rhs) { |
| 201 | if ((lhs == NULL) || (rhs == NULL)) { |
| 202 | return false; |
| 203 | } |
| 204 | if (lhs->num_servers != rhs->num_servers) { |
| 205 | return false; |
| 206 | } |
| 207 | if (grpc_grpclb_duration_compare(&lhs->expiration_interval, |
| 208 | &rhs->expiration_interval) != 0) { |
| 209 | return false; |
| 210 | } |
| 211 | for (size_t i = 0; i < lhs->num_servers; i++) { |
| 212 | if (!grpc_grpclb_server_equals(lhs->servers[i], rhs->servers[i])) { |
| 213 | return false; |
| 214 | } |
| 215 | } |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | bool grpc_grpclb_server_equals(const grpc_grpclb_server *lhs, |
| 220 | const grpc_grpclb_server *rhs) { |
| 221 | return memcmp(lhs, rhs, sizeof(grpc_grpclb_server)) == 0; |
| 222 | } |
| 223 | |
| 224 | int grpc_grpclb_duration_compare(const grpc_grpclb_duration *lhs, |
| 225 | const grpc_grpclb_duration *rhs) { |
| 226 | GPR_ASSERT(lhs && rhs); |
| 227 | if (lhs->has_seconds && rhs->has_seconds) { |
David Garcia Quintas | 4166cb0 | 2016-07-29 14:33:15 -0700 | [diff] [blame] | 228 | if (lhs->seconds < rhs->seconds) return -1; |
| 229 | if (lhs->seconds > rhs->seconds) return 1; |
| 230 | } else if (lhs->has_seconds) { |
| 231 | return 1; |
| 232 | } else if (rhs->has_seconds) { |
| 233 | return -1; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 234 | } |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 235 | |
David Garcia Quintas | 4166cb0 | 2016-07-29 14:33:15 -0700 | [diff] [blame] | 236 | GPR_ASSERT(lhs->seconds == rhs->seconds); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 237 | if (lhs->has_nanos && rhs->has_nanos) { |
David Garcia Quintas | 4166cb0 | 2016-07-29 14:33:15 -0700 | [diff] [blame] | 238 | if (lhs->nanos < rhs->nanos) return -1; |
| 239 | if (lhs->nanos > rhs->nanos) return 1; |
| 240 | } else if (lhs->has_nanos) { |
| 241 | return 1; |
| 242 | } else if (rhs->has_nanos) { |
| 243 | return -1; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 244 | } |
David Garcia Quintas | 4166cb0 | 2016-07-29 14:33:15 -0700 | [diff] [blame] | 245 | |
| 246 | return 0; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 247 | } |
| 248 | |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 249 | void grpc_grpclb_initial_response_destroy( |
| 250 | grpc_grpclb_initial_response *response) { |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 251 | gpr_free(response); |
| 252 | } |