David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 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 | |
Mark D. Roth | 1519574 | 2016-10-07 09:02:28 -0700 | [diff] [blame] | 34 | #ifndef GRPC_CORE_EXT_CLIENT_CHANNEL_LB_POLICY_FACTORY_H |
| 35 | #define GRPC_CORE_EXT_CLIENT_CHANNEL_LB_POLICY_FACTORY_H |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 36 | |
Mark D. Roth | 2137cd8 | 2016-09-14 09:04:00 -0700 | [diff] [blame] | 37 | #include "src/core/ext/client_channel/client_channel_factory.h" |
| 38 | #include "src/core/ext/client_channel/lb_policy.h" |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 39 | |
David Garcia Quintas | 3c05db5 | 2016-03-28 16:57:23 -0700 | [diff] [blame] | 40 | #include "src/core/lib/iomgr/exec_ctx.h" |
Mark D. Roth | fef585f | 2016-09-16 10:23:28 -0700 | [diff] [blame] | 41 | #include "src/core/lib/iomgr/resolve_address.h" |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 42 | |
Mark D. Roth | be5e3ca | 2016-12-12 09:58:20 -0800 | [diff] [blame] | 43 | // Channel arg key for grpc_lb_addresses. |
| 44 | #define GRPC_ARG_LB_ADDRESSES "grpc.lb_addresses" |
| 45 | |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 46 | typedef struct grpc_lb_policy_factory grpc_lb_policy_factory; |
| 47 | typedef struct grpc_lb_policy_factory_vtable grpc_lb_policy_factory_vtable; |
| 48 | |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 49 | struct grpc_lb_policy_factory { |
| 50 | const grpc_lb_policy_factory_vtable *vtable; |
| 51 | }; |
| 52 | |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 53 | /** A resolved address alongside any LB related information associated with it. |
David Garcia Quintas | f47d6fb | 2016-09-14 12:59:17 -0700 | [diff] [blame] | 54 | * \a user_data, if not NULL, contains opaque data meant to be consumed by the |
| 55 | * gRPC LB policy. Note that no all LB policies support \a user_data as input. |
| 56 | * Those who don't will simply ignore it and will correspondingly return NULL in |
| 57 | * their namesake pick() output argument. */ |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 58 | typedef struct grpc_lb_address { |
Mark D. Roth | c5c3878 | 2016-09-16 08:51:01 -0700 | [diff] [blame] | 59 | grpc_resolved_address address; |
| 60 | bool is_balancer; |
Mark D. Roth | 64f1f8d | 2016-09-16 09:00:09 -0700 | [diff] [blame] | 61 | char *balancer_name; /* For secure naming. */ |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 62 | void *user_data; |
| 63 | } grpc_lb_address; |
| 64 | |
Mark D. Roth | 16883a3 | 2016-10-21 10:30:58 -0700 | [diff] [blame] | 65 | typedef struct grpc_lb_user_data_vtable { |
Mark D. Roth | 557c990 | 2016-10-24 11:12:05 -0700 | [diff] [blame] | 66 | void *(*copy)(void *); |
Craig Tiller | 87a7e1f | 2016-11-09 09:42:19 -0800 | [diff] [blame] | 67 | void (*destroy)(grpc_exec_ctx *exec_ctx, void *); |
Mark D. Roth | 557c990 | 2016-10-24 11:12:05 -0700 | [diff] [blame] | 68 | int (*cmp)(void *, void *); |
Mark D. Roth | 16883a3 | 2016-10-21 10:30:58 -0700 | [diff] [blame] | 69 | } grpc_lb_user_data_vtable; |
| 70 | |
Mark D. Roth | c5c3878 | 2016-09-16 08:51:01 -0700 | [diff] [blame] | 71 | typedef struct grpc_lb_addresses { |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 72 | size_t num_addresses; |
Mark D. Roth | c5c3878 | 2016-09-16 08:51:01 -0700 | [diff] [blame] | 73 | grpc_lb_address *addresses; |
Mark D. Roth | 16883a3 | 2016-10-21 10:30:58 -0700 | [diff] [blame] | 74 | const grpc_lb_user_data_vtable *user_data_vtable; |
Mark D. Roth | c5c3878 | 2016-09-16 08:51:01 -0700 | [diff] [blame] | 75 | } grpc_lb_addresses; |
| 76 | |
| 77 | /** Returns a grpc_addresses struct with enough space for |
Mark D. Roth | 16883a3 | 2016-10-21 10:30:58 -0700 | [diff] [blame] | 78 | \a num_addresses addresses. The \a user_data_vtable argument may be |
| 79 | NULL if no user data will be added. */ |
| 80 | grpc_lb_addresses *grpc_lb_addresses_create( |
Mark D. Roth | 557c990 | 2016-10-24 11:12:05 -0700 | [diff] [blame] | 81 | size_t num_addresses, const grpc_lb_user_data_vtable *user_data_vtable); |
Mark D. Roth | c5c3878 | 2016-09-16 08:51:01 -0700 | [diff] [blame] | 82 | |
Mark D. Roth | e917f5d | 2016-10-25 07:48:28 -0700 | [diff] [blame] | 83 | /** Creates a copy of \a addresses. */ |
Mark D. Roth | 16883a3 | 2016-10-21 10:30:58 -0700 | [diff] [blame] | 84 | grpc_lb_addresses *grpc_lb_addresses_copy(const grpc_lb_addresses *addresses); |
Mark D. Roth | ee5173f | 2016-09-16 10:14:18 -0700 | [diff] [blame] | 85 | |
Mark D. Roth | c5c3878 | 2016-09-16 08:51:01 -0700 | [diff] [blame] | 86 | /** Sets the value of the address at index \a index of \a addresses. |
| 87 | * \a address is a socket address of length \a address_len. |
| 88 | * Takes ownership of \a balancer_name. */ |
| 89 | void grpc_lb_addresses_set_address(grpc_lb_addresses *addresses, size_t index, |
| 90 | void *address, size_t address_len, |
Mark D. Roth | 64f1f8d | 2016-09-16 09:00:09 -0700 | [diff] [blame] | 91 | bool is_balancer, char *balancer_name, |
Mark D. Roth | c5c3878 | 2016-09-16 08:51:01 -0700 | [diff] [blame] | 92 | void *user_data); |
| 93 | |
Mark D. Roth | 16883a3 | 2016-10-21 10:30:58 -0700 | [diff] [blame] | 94 | /** Compares \a addresses1 and \a addresses2. */ |
| 95 | int grpc_lb_addresses_cmp(const grpc_lb_addresses *addresses1, |
| 96 | const grpc_lb_addresses *addresses2); |
| 97 | |
Mark D. Roth | e917f5d | 2016-10-25 07:48:28 -0700 | [diff] [blame] | 98 | /** Destroys \a addresses. */ |
Craig Tiller | 87a7e1f | 2016-11-09 09:42:19 -0800 | [diff] [blame] | 99 | void grpc_lb_addresses_destroy(grpc_exec_ctx *exec_ctx, |
| 100 | grpc_lb_addresses *addresses); |
Mark D. Roth | c5c3878 | 2016-09-16 08:51:01 -0700 | [diff] [blame] | 101 | |
Mark D. Roth | 3686996 | 2016-10-21 10:43:45 -0700 | [diff] [blame] | 102 | /** Returns a channel arg containing \a addresses. */ |
| 103 | grpc_arg grpc_lb_addresses_create_channel_arg( |
| 104 | const grpc_lb_addresses *addresses); |
| 105 | |
Mark D. Roth | c5c3878 | 2016-09-16 08:51:01 -0700 | [diff] [blame] | 106 | /** Arguments passed to LB policies. */ |
David Garcia Quintas | c7705c7 | 2015-09-09 17:21:11 -0700 | [diff] [blame] | 107 | typedef struct grpc_lb_policy_args { |
David Garcia Quintas | 86fcfcc | 2016-03-31 23:22:28 -0700 | [diff] [blame] | 108 | grpc_client_channel_factory *client_channel_factory; |
Mark D. Roth | 98abfd3 | 2016-10-21 08:10:51 -0700 | [diff] [blame] | 109 | grpc_channel_args *args; |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 110 | grpc_combiner *combiner; |
David Garcia Quintas | c7705c7 | 2015-09-09 17:21:11 -0700 | [diff] [blame] | 111 | } grpc_lb_policy_args; |
| 112 | |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 113 | struct grpc_lb_policy_factory_vtable { |
| 114 | void (*ref)(grpc_lb_policy_factory *factory); |
| 115 | void (*unref)(grpc_lb_policy_factory *factory); |
| 116 | |
| 117 | /** Implementation of grpc_lb_policy_factory_create_lb_policy */ |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 118 | grpc_lb_policy *(*create_lb_policy)(grpc_exec_ctx *exec_ctx, |
| 119 | grpc_lb_policy_factory *factory, |
David Garcia Quintas | c7705c7 | 2015-09-09 17:21:11 -0700 | [diff] [blame] | 120 | grpc_lb_policy_args *args); |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 121 | |
| 122 | /** Name for the LB policy this factory implements */ |
| 123 | const char *name; |
| 124 | }; |
| 125 | |
| 126 | void grpc_lb_policy_factory_ref(grpc_lb_policy_factory *factory); |
| 127 | void grpc_lb_policy_factory_unref(grpc_lb_policy_factory *factory); |
| 128 | |
| 129 | /** Create a lb_policy instance. */ |
| 130 | grpc_lb_policy *grpc_lb_policy_factory_create_lb_policy( |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 131 | grpc_exec_ctx *exec_ctx, grpc_lb_policy_factory *factory, |
| 132 | grpc_lb_policy_args *args); |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 133 | |
Mark D. Roth | 1519574 | 2016-10-07 09:02:28 -0700 | [diff] [blame] | 134 | #endif /* GRPC_CORE_EXT_CLIENT_CHANNEL_LB_POLICY_FACTORY_H */ |