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