blob: 27c12c0d73dd8697ec719605ca5633debb0ac044 [file] [log] [blame]
David Garcia Quintas5c4543d2015-09-03 15:49:56 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
David Garcia Quintas5c4543d2015-09-03 15:49:56 -07004 * 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. Roth15195742016-10-07 09:02:28 -070034#ifndef GRPC_CORE_EXT_CLIENT_CHANNEL_LB_POLICY_FACTORY_H
35#define GRPC_CORE_EXT_CLIENT_CHANNEL_LB_POLICY_FACTORY_H
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070036
Mark D. Roth2137cd82016-09-14 09:04:00 -070037#include "src/core/ext/client_channel/client_channel_factory.h"
38#include "src/core/ext/client_channel/lb_policy.h"
David Garcia Quintas67c0d042016-03-25 01:37:53 -070039
David Garcia Quintas3c05db52016-03-28 16:57:23 -070040#include "src/core/lib/iomgr/exec_ctx.h"
Mark D. Rothfef585f2016-09-16 10:23:28 -070041#include "src/core/lib/iomgr/resolve_address.h"
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070042
Mark D. Rothbe5e3ca2016-12-12 09:58:20 -080043// Channel arg key for grpc_lb_addresses.
44#define GRPC_ARG_LB_ADDRESSES "grpc.lb_addresses"
45
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070046typedef struct grpc_lb_policy_factory grpc_lb_policy_factory;
47typedef struct grpc_lb_policy_factory_vtable grpc_lb_policy_factory_vtable;
48
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070049struct grpc_lb_policy_factory {
50 const grpc_lb_policy_factory_vtable *vtable;
51};
52
David Garcia Quintas331b9c02016-09-12 18:37:05 -070053/** A resolved address alongside any LB related information associated with it.
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -070054 * \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 Quintas331b9c02016-09-12 18:37:05 -070058typedef struct grpc_lb_address {
Mark D. Rothc5c38782016-09-16 08:51:01 -070059 grpc_resolved_address address;
60 bool is_balancer;
Mark D. Roth64f1f8d2016-09-16 09:00:09 -070061 char *balancer_name; /* For secure naming. */
David Garcia Quintas331b9c02016-09-12 18:37:05 -070062 void *user_data;
63} grpc_lb_address;
64
Mark D. Roth16883a32016-10-21 10:30:58 -070065typedef struct grpc_lb_user_data_vtable {
Mark D. Roth557c9902016-10-24 11:12:05 -070066 void *(*copy)(void *);
Craig Tiller87a7e1f2016-11-09 09:42:19 -080067 void (*destroy)(grpc_exec_ctx *exec_ctx, void *);
Mark D. Roth557c9902016-10-24 11:12:05 -070068 int (*cmp)(void *, void *);
Mark D. Roth16883a32016-10-21 10:30:58 -070069} grpc_lb_user_data_vtable;
70
Mark D. Rothc5c38782016-09-16 08:51:01 -070071typedef struct grpc_lb_addresses {
David Garcia Quintas331b9c02016-09-12 18:37:05 -070072 size_t num_addresses;
Mark D. Rothc5c38782016-09-16 08:51:01 -070073 grpc_lb_address *addresses;
Mark D. Roth16883a32016-10-21 10:30:58 -070074 const grpc_lb_user_data_vtable *user_data_vtable;
Mark D. Rothc5c38782016-09-16 08:51:01 -070075} grpc_lb_addresses;
76
77/** Returns a grpc_addresses struct with enough space for
Mark D. Roth16883a32016-10-21 10:30:58 -070078 \a num_addresses addresses. The \a user_data_vtable argument may be
79 NULL if no user data will be added. */
80grpc_lb_addresses *grpc_lb_addresses_create(
Mark D. Roth557c9902016-10-24 11:12:05 -070081 size_t num_addresses, const grpc_lb_user_data_vtable *user_data_vtable);
Mark D. Rothc5c38782016-09-16 08:51:01 -070082
Mark D. Rothe917f5d2016-10-25 07:48:28 -070083/** Creates a copy of \a addresses. */
Mark D. Roth16883a32016-10-21 10:30:58 -070084grpc_lb_addresses *grpc_lb_addresses_copy(const grpc_lb_addresses *addresses);
Mark D. Rothee5173f2016-09-16 10:14:18 -070085
Mark D. Rothc5c38782016-09-16 08:51:01 -070086/** 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. */
89void grpc_lb_addresses_set_address(grpc_lb_addresses *addresses, size_t index,
90 void *address, size_t address_len,
Mark D. Roth64f1f8d2016-09-16 09:00:09 -070091 bool is_balancer, char *balancer_name,
Mark D. Rothc5c38782016-09-16 08:51:01 -070092 void *user_data);
93
Mark D. Roth16883a32016-10-21 10:30:58 -070094/** Compares \a addresses1 and \a addresses2. */
95int grpc_lb_addresses_cmp(const grpc_lb_addresses *addresses1,
96 const grpc_lb_addresses *addresses2);
97
Mark D. Rothe917f5d2016-10-25 07:48:28 -070098/** Destroys \a addresses. */
Craig Tiller87a7e1f2016-11-09 09:42:19 -080099void grpc_lb_addresses_destroy(grpc_exec_ctx *exec_ctx,
100 grpc_lb_addresses *addresses);
Mark D. Rothc5c38782016-09-16 08:51:01 -0700101
Mark D. Roth36869962016-10-21 10:43:45 -0700102/** Returns a channel arg containing \a addresses. */
103grpc_arg grpc_lb_addresses_create_channel_arg(
104 const grpc_lb_addresses *addresses);
105
Mark D. Rothc5c38782016-09-16 08:51:01 -0700106/** Arguments passed to LB policies. */
David Garcia Quintasc7705c72015-09-09 17:21:11 -0700107typedef struct grpc_lb_policy_args {
David Garcia Quintas86fcfcc2016-03-31 23:22:28 -0700108 grpc_client_channel_factory *client_channel_factory;
Mark D. Roth98abfd32016-10-21 08:10:51 -0700109 grpc_channel_args *args;
Craig Tiller2400bf52017-02-09 16:25:19 -0800110 grpc_combiner *combiner;
David Garcia Quintasc7705c72015-09-09 17:21:11 -0700111} grpc_lb_policy_args;
112
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700113struct 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 Quintas67c0d042016-03-25 01:37:53 -0700118 grpc_lb_policy *(*create_lb_policy)(grpc_exec_ctx *exec_ctx,
119 grpc_lb_policy_factory *factory,
David Garcia Quintasc7705c72015-09-09 17:21:11 -0700120 grpc_lb_policy_args *args);
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700121
122 /** Name for the LB policy this factory implements */
123 const char *name;
124};
125
126void grpc_lb_policy_factory_ref(grpc_lb_policy_factory *factory);
127void grpc_lb_policy_factory_unref(grpc_lb_policy_factory *factory);
128
129/** Create a lb_policy instance. */
130grpc_lb_policy *grpc_lb_policy_factory_create_lb_policy(
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700131 grpc_exec_ctx *exec_ctx, grpc_lb_policy_factory *factory,
132 grpc_lb_policy_args *args);
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700133
Mark D. Roth15195742016-10-07 09:02:28 -0700134#endif /* GRPC_CORE_EXT_CLIENT_CHANNEL_LB_POLICY_FACTORY_H */