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 | 6f41788 | 2017-02-16 14:09:39 -0800 | [diff] [blame] | 65 | grpc_grpclb_server *server = gpr_zalloc(sizeof(grpc_grpclb_server)); |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 66 | GPR_ASSERT(dec_arg->num_servers > 0); |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 67 | if (dec_arg->decoding_idx == 0) { /* first iteration of second pass */ |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 68 | dec_arg->servers = |
| 69 | gpr_malloc(sizeof(grpc_grpclb_server *) * dec_arg->num_servers); |
| 70 | } |
David Garcia Quintas | 7f0793a | 2016-04-25 12:35:58 -0700 | [diff] [blame] | 71 | if (!pb_decode(stream, grpc_lb_v1_Server_fields, server)) { |
David Garcia Quintas | 18bc43b | 2016-08-22 15:05:32 -0700 | [diff] [blame] | 72 | gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(stream)); |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 73 | return false; |
| 74 | } |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 75 | dec_arg->servers[dec_arg->decoding_idx++] = server; |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | grpc_grpclb_request *grpc_grpclb_request_create(const char *lb_service_name) { |
| 82 | grpc_grpclb_request *req = gpr_malloc(sizeof(grpc_grpclb_request)); |
| 83 | |
| 84 | req->has_client_stats = 0; /* TODO(dgq): add support for stats once defined */ |
| 85 | req->has_initial_request = 1; |
| 86 | req->initial_request.has_name = 1; |
| 87 | strncpy(req->initial_request.name, lb_service_name, |
| 88 | GRPC_GRPCLB_SERVICE_NAME_MAX_LENGTH); |
| 89 | return req; |
| 90 | } |
| 91 | |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 92 | grpc_slice grpc_grpclb_request_encode(const grpc_grpclb_request *request) { |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 93 | size_t encoded_length; |
| 94 | pb_ostream_t sizestream; |
| 95 | pb_ostream_t outputstream; |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 96 | grpc_slice slice; |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 97 | memset(&sizestream, 0, sizeof(pb_ostream_t)); |
David Garcia Quintas | 7f0793a | 2016-04-25 12:35:58 -0700 | [diff] [blame] | 98 | pb_encode(&sizestream, grpc_lb_v1_LoadBalanceRequest_fields, request); |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 99 | encoded_length = sizestream.bytes_written; |
| 100 | |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 101 | slice = grpc_slice_malloc(encoded_length); |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 102 | outputstream = |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 103 | pb_ostream_from_buffer(GRPC_SLICE_START_PTR(slice), encoded_length); |
David Garcia Quintas | 7f0793a | 2016-04-25 12:35:58 -0700 | [diff] [blame] | 104 | GPR_ASSERT(pb_encode(&outputstream, grpc_lb_v1_LoadBalanceRequest_fields, |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 105 | request) != 0); |
| 106 | return slice; |
| 107 | } |
| 108 | |
| 109 | void grpc_grpclb_request_destroy(grpc_grpclb_request *request) { |
| 110 | gpr_free(request); |
| 111 | } |
| 112 | |
David Garcia Quintas | 4166cb0 | 2016-07-29 14:33:15 -0700 | [diff] [blame] | 113 | typedef grpc_lb_v1_LoadBalanceResponse grpc_grpclb_response; |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 114 | grpc_grpclb_initial_response *grpc_grpclb_initial_response_parse( |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 115 | grpc_slice encoded_grpc_grpclb_response) { |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 116 | pb_istream_t stream = |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 117 | pb_istream_from_buffer(GRPC_SLICE_START_PTR(encoded_grpc_grpclb_response), |
| 118 | GRPC_SLICE_LENGTH(encoded_grpc_grpclb_response)); |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 119 | grpc_grpclb_response res; |
| 120 | memset(&res, 0, sizeof(grpc_grpclb_response)); |
| 121 | if (!pb_decode(&stream, grpc_lb_v1_LoadBalanceResponse_fields, &res)) { |
David Garcia Quintas | 18bc43b | 2016-08-22 15:05:32 -0700 | [diff] [blame] | 122 | gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&stream)); |
David Garcia Quintas | bf2e73d | 2016-04-04 18:03:47 -0700 | [diff] [blame] | 123 | return NULL; |
| 124 | } |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 125 | grpc_grpclb_initial_response *initial_res = |
| 126 | gpr_malloc(sizeof(grpc_grpclb_initial_response)); |
| 127 | memcpy(initial_res, &res.initial_response, |
| 128 | sizeof(grpc_grpclb_initial_response)); |
| 129 | |
| 130 | return initial_res; |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | grpc_grpclb_serverlist *grpc_grpclb_response_parse_serverlist( |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 134 | grpc_slice encoded_grpc_grpclb_response) { |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 135 | bool status; |
| 136 | decode_serverlist_arg arg; |
| 137 | pb_istream_t stream = |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 138 | pb_istream_from_buffer(GRPC_SLICE_START_PTR(encoded_grpc_grpclb_response), |
| 139 | GRPC_SLICE_LENGTH(encoded_grpc_grpclb_response)); |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 140 | pb_istream_t stream_at_start = stream; |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 141 | grpc_grpclb_response res; |
| 142 | memset(&res, 0, sizeof(grpc_grpclb_response)); |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 143 | memset(&arg, 0, sizeof(decode_serverlist_arg)); |
| 144 | |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 145 | res.server_list.servers.funcs.decode = decode_serverlist; |
| 146 | res.server_list.servers.arg = &arg; |
| 147 | arg.first_pass = true; |
| 148 | status = pb_decode(&stream, grpc_lb_v1_LoadBalanceResponse_fields, &res); |
David Garcia Quintas | bf2e73d | 2016-04-04 18:03:47 -0700 | [diff] [blame] | 149 | if (!status) { |
David Garcia Quintas | 18bc43b | 2016-08-22 15:05:32 -0700 | [diff] [blame] | 150 | gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&stream)); |
David Garcia Quintas | bf2e73d | 2016-04-04 18:03:47 -0700 | [diff] [blame] | 151 | return NULL; |
| 152 | } |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 153 | |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 154 | arg.first_pass = false; |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 155 | status = |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 156 | pb_decode(&stream_at_start, grpc_lb_v1_LoadBalanceResponse_fields, &res); |
David Garcia Quintas | bf2e73d | 2016-04-04 18:03:47 -0700 | [diff] [blame] | 157 | if (!status) { |
David Garcia Quintas | 18bc43b | 2016-08-22 15:05:32 -0700 | [diff] [blame] | 158 | gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&stream)); |
David Garcia Quintas | bf2e73d | 2016-04-04 18:03:47 -0700 | [diff] [blame] | 159 | return NULL; |
| 160 | } |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 161 | |
Craig Tiller | 6f41788 | 2017-02-16 14:09:39 -0800 | [diff] [blame] | 162 | grpc_grpclb_serverlist *sl = gpr_zalloc(sizeof(grpc_grpclb_serverlist)); |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 163 | sl->num_servers = arg.num_servers; |
| 164 | sl->servers = arg.servers; |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 165 | if (res.server_list.has_expiration_interval) { |
| 166 | sl->expiration_interval = res.server_list.expiration_interval; |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 167 | } |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 168 | return sl; |
| 169 | } |
| 170 | |
| 171 | void grpc_grpclb_destroy_serverlist(grpc_grpclb_serverlist *serverlist) { |
David Garcia Quintas | bf2e73d | 2016-04-04 18:03:47 -0700 | [diff] [blame] | 172 | if (serverlist == NULL) { |
| 173 | return; |
| 174 | } |
| 175 | for (size_t i = 0; i < serverlist->num_servers; i++) { |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 176 | gpr_free(serverlist->servers[i]); |
| 177 | } |
| 178 | gpr_free(serverlist->servers); |
| 179 | gpr_free(serverlist); |
| 180 | } |
| 181 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 182 | grpc_grpclb_serverlist *grpc_grpclb_serverlist_copy( |
| 183 | const grpc_grpclb_serverlist *sl) { |
Craig Tiller | 6f41788 | 2017-02-16 14:09:39 -0800 | [diff] [blame] | 184 | grpc_grpclb_serverlist *copy = gpr_zalloc(sizeof(grpc_grpclb_serverlist)); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 185 | copy->num_servers = sl->num_servers; |
| 186 | memcpy(©->expiration_interval, &sl->expiration_interval, |
| 187 | sizeof(grpc_grpclb_duration)); |
David Garcia Quintas | 8782d1b | 2016-06-15 23:58:44 -0700 | [diff] [blame] | 188 | copy->servers = gpr_malloc(sizeof(grpc_grpclb_server *) * sl->num_servers); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 189 | for (size_t i = 0; i < sl->num_servers; i++) { |
| 190 | copy->servers[i] = gpr_malloc(sizeof(grpc_grpclb_server)); |
| 191 | memcpy(copy->servers[i], sl->servers[i], sizeof(grpc_grpclb_server)); |
| 192 | } |
| 193 | return copy; |
| 194 | } |
| 195 | |
| 196 | bool grpc_grpclb_serverlist_equals(const grpc_grpclb_serverlist *lhs, |
| 197 | const grpc_grpclb_serverlist *rhs) { |
| 198 | if ((lhs == NULL) || (rhs == NULL)) { |
| 199 | return false; |
| 200 | } |
| 201 | if (lhs->num_servers != rhs->num_servers) { |
| 202 | return false; |
| 203 | } |
| 204 | if (grpc_grpclb_duration_compare(&lhs->expiration_interval, |
| 205 | &rhs->expiration_interval) != 0) { |
| 206 | return false; |
| 207 | } |
| 208 | for (size_t i = 0; i < lhs->num_servers; i++) { |
| 209 | if (!grpc_grpclb_server_equals(lhs->servers[i], rhs->servers[i])) { |
| 210 | return false; |
| 211 | } |
| 212 | } |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | bool grpc_grpclb_server_equals(const grpc_grpclb_server *lhs, |
| 217 | const grpc_grpclb_server *rhs) { |
| 218 | return memcmp(lhs, rhs, sizeof(grpc_grpclb_server)) == 0; |
| 219 | } |
| 220 | |
| 221 | int grpc_grpclb_duration_compare(const grpc_grpclb_duration *lhs, |
| 222 | const grpc_grpclb_duration *rhs) { |
| 223 | GPR_ASSERT(lhs && rhs); |
| 224 | if (lhs->has_seconds && rhs->has_seconds) { |
David Garcia Quintas | 4166cb0 | 2016-07-29 14:33:15 -0700 | [diff] [blame] | 225 | if (lhs->seconds < rhs->seconds) return -1; |
| 226 | if (lhs->seconds > rhs->seconds) return 1; |
| 227 | } else if (lhs->has_seconds) { |
| 228 | return 1; |
| 229 | } else if (rhs->has_seconds) { |
| 230 | return -1; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 231 | } |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 232 | |
David Garcia Quintas | 4166cb0 | 2016-07-29 14:33:15 -0700 | [diff] [blame] | 233 | GPR_ASSERT(lhs->seconds == rhs->seconds); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 234 | if (lhs->has_nanos && rhs->has_nanos) { |
David Garcia Quintas | 4166cb0 | 2016-07-29 14:33:15 -0700 | [diff] [blame] | 235 | if (lhs->nanos < rhs->nanos) return -1; |
| 236 | if (lhs->nanos > rhs->nanos) return 1; |
| 237 | } else if (lhs->has_nanos) { |
| 238 | return 1; |
| 239 | } else if (rhs->has_nanos) { |
| 240 | return -1; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 241 | } |
David Garcia Quintas | 4166cb0 | 2016-07-29 14:33:15 -0700 | [diff] [blame] | 242 | |
| 243 | return 0; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 244 | } |
| 245 | |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 246 | void grpc_grpclb_initial_response_destroy( |
| 247 | grpc_grpclb_initial_response *response) { |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 248 | gpr_free(response); |
| 249 | } |