varnish-cache/vmod/vmod_directors_round_robin.c
1
/*-
2
 * Copyright (c) 2013-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@FreeBSD.org>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 */
30
31
#include "config.h"
32
33
#include <stdlib.h>
34
#include <string.h>
35
36
#include "cache/cache.h"
37
38
#include "vcc_directors_if.h"
39
40
#include "vmod_directors.h"
41
42
struct vmod_directors_round_robin {
43
        unsigned                                magic;
44
#define VMOD_DIRECTORS_ROUND_ROBIN_MAGIC        0xa80970cf
45
        struct vdir                             *vd;
46
        unsigned                                nxt;
47
};
48
49
static VCL_BOOL v_matchproto_(vdi_healthy_f)
50 430
vmod_rr_healthy(VRT_CTX, VCL_BACKEND dir, VCL_TIME *changed)
51
{
52
        struct vmod_directors_round_robin *rr;
53
54 430
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
55 430
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
56 430
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
57 430
        return (vdir_any_healthy(ctx, rr->vd, changed));
58
}
59
60
static void v_matchproto_(vdi_list_f)
61 350
vmod_rr_list(VRT_CTX, VCL_BACKEND dir, struct vsb *vsb, int pflag, int jflag)
62
{
63
        struct vmod_directors_round_robin *rr;
64
65 350
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
66 350
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
67 350
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
68 350
        vdir_list(ctx, rr->vd, vsb, pflag, jflag, 0);
69 350
}
70
71
static VCL_BACKEND v_matchproto_(vdi_resolve_f)
72 430
vmod_rr_resolve(VRT_CTX, VCL_BACKEND dir)
73
{
74
        struct vmod_directors_round_robin *rr;
75
        unsigned u;
76 430
        VCL_BACKEND be = NULL;
77
        unsigned nxt;
78
79 430
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
80 430
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
81 430
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
82 430
        vdir_rdlock(rr->vd);
83 430
        nxt = rr->nxt;
84 450
        for (u = 0; u < rr->vd->n_backend; u++) {
85 450
                be = rr->vd->backend[nxt];
86 450
                nxt++;
87 450
                nxt %= rr->vd->n_backend;
88 450
                CHECK_OBJ_NOTNULL(be, DIRECTOR_MAGIC);
89 450
                if (VRT_Healthy(ctx, be, NULL))
90 430
                        break;
91 20
        }
92 430
        rr->nxt = nxt;
93 430
        vdir_unlock(rr->vd);
94 430
        if (u == rr->vd->n_backend)
95 0
                be = NULL;
96 430
        return (be);
97
}
98
99
static void v_matchproto_(vdi_destroy_f)
100 30
vmod_rr_destroy(VCL_BACKEND dir)
101
{
102
        struct vmod_directors_round_robin *rr;
103
104 30
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
105 30
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
106 30
        vdir_delete(&rr->vd);
107 30
        FREE_OBJ(rr);
108 30
}
109
110
static const struct vdi_methods vmod_rr_methods[1] = {{
111
        .magic =                VDI_METHODS_MAGIC,
112
        .type =                 "round-robin",
113
        .healthy =              vmod_rr_healthy,
114
        .resolve =              vmod_rr_resolve,
115
        .destroy =              vmod_rr_destroy,
116
        .list =                 vmod_rr_list
117
}};
118
119
VCL_VOID v_matchproto_()
120 160
vmod_round_robin__init(VRT_CTX,
121
    struct vmod_directors_round_robin **rrp, const char *vcl_name)
122
{
123
        struct vmod_directors_round_robin *rr;
124
125 160
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
126 160
        AN(rrp);
127 160
        AZ(*rrp);
128 160
        ALLOC_OBJ(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
129 160
        AN(rr);
130 160
        *rrp = rr;
131 160
        vdir_new(ctx, &rr->vd, vcl_name, vmod_rr_methods, rr);
132 160
}
133
134
VCL_VOID v_matchproto_()
135 30
vmod_round_robin__fini(struct vmod_directors_round_robin **rrp)
136
{
137
        struct vmod_directors_round_robin *rr;
138
139 30
        TAKE_OBJ_NOTNULL(rr, rrp, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
140 30
        VRT_DelDirector(&rr->vd->dir);
141 30
}
142
143
VCL_VOID v_matchproto_()
144 310
vmod_round_robin_add_backend(VRT_CTX,
145
    struct vmod_directors_round_robin *rr, VCL_BACKEND be)
146
{
147
148 310
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
149 310
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
150 310
        vdir_add_backend(ctx, rr->vd, be, 0.0);
151 310
}
152
153
VCL_VOID v_matchproto_()
154 60
vmod_round_robin_remove_backend(VRT_CTX,
155
    struct vmod_directors_round_robin *rr, VCL_BACKEND be)
156
{
157 60
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
158 60
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
159 60
        vdir_remove_backend(ctx, rr->vd, be, NULL);
160 60
}
161
162
VCL_BACKEND v_matchproto_()
163 450
vmod_round_robin_backend(VRT_CTX,
164
    struct vmod_directors_round_robin *rr)
165
{
166
167 450
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
168 450
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
169 450
        return (rr->vd->dir);
170
}