varnish-cache/vmod/vmod_directors_random.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
35
#include "cache/cache.h"
36
37
#include "vbm.h"
38
#include "vrnd.h"
39
40
#include "vmod_directors.h"
41
42
#include "vcc_directors_if.h"
43
44
struct vmod_directors_random {
45
        unsigned                                magic;
46
#define VMOD_DIRECTORS_RANDOM_MAGIC             0x4732d092
47
        struct vdir                             *vd;
48
};
49
50
static VCL_BOOL v_matchproto_(vdi_healthy)
51 20
vmod_random_healthy(VRT_CTX, VCL_BACKEND dir, VCL_TIME *changed)
52
{
53
        struct vmod_directors_random *rr;
54
55 20
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
56 20
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
57 20
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC);
58 20
        return (vdir_any_healthy(ctx, rr->vd, changed));
59
}
60
61
static void v_matchproto_(vdi_list_f)
62 160
vmod_random_list(VRT_CTX, VCL_BACKEND dir, struct vsb *vsb, int pflag,
63
    int jflag)
64
{
65
        struct vmod_directors_random *rr;
66
67 160
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
68 160
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
69 160
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC);
70 160
        vdir_list(ctx, rr->vd, vsb, pflag, jflag, 1);
71 160
}
72
73
static VCL_BACKEND v_matchproto_(vdi_resolve_f)
74 360
vmod_random_resolve(VRT_CTX, VCL_BACKEND dir)
75
{
76
        struct vmod_directors_random *rr;
77
        VCL_BACKEND be;
78
        double r;
79
80 360
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
81 360
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
82 360
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC);
83 360
        r = scalbn(VRND_RandomTestable(), -31);
84 360
        assert(r >= 0 && r < 1.0);
85 360
        be = vdir_pick_be(ctx, rr->vd, r);
86 360
        return (be);
87
}
88
89
static void v_matchproto_(vdi_destroy_f)
90 10
vmod_random_destroy(VCL_BACKEND dir)
91
{
92
        struct vmod_directors_random *rr;
93
94 10
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
95 10
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC);
96 10
        vdir_delete(&rr->vd);
97 10
        FREE_OBJ(rr);
98 10
}
99
100
static const struct vdi_methods vmod_random_methods[1] = {{
101
        .magic =                VDI_METHODS_MAGIC,
102
        .type =                 "random",
103
        .healthy =              vmod_random_healthy,
104
        .resolve =              vmod_random_resolve,
105
        .destroy =              vmod_random_destroy,
106
        .list =                 vmod_random_list
107
}};
108
109
110
VCL_VOID v_matchproto_()
111 90
vmod_random__init(VRT_CTX, struct vmod_directors_random **rrp,
112
    const char *vcl_name)
113
{
114
        struct vmod_directors_random *rr;
115
116 90
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
117 90
        AN(rrp);
118 90
        AZ(*rrp);
119 90
        ALLOC_OBJ(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
120 90
        AN(rr);
121 90
        *rrp = rr;
122 90
        vdir_new(ctx, &rr->vd, vcl_name, vmod_random_methods, rr);
123 90
}
124
125
VCL_VOID v_matchproto_()
126 10
vmod_random__fini(struct vmod_directors_random **rrp)
127
{
128
        struct vmod_directors_random *rr;
129
130 10
        TAKE_OBJ_NOTNULL(rr, rrp, VMOD_DIRECTORS_RANDOM_MAGIC);
131 10
        VRT_DelDirector(&rr->vd->dir);
132 10
}
133
134
VCL_VOID v_matchproto_()
135 260
vmod_random_add_backend(VRT_CTX,
136
    struct vmod_directors_random *rr, VCL_BACKEND be, double w)
137
{
138
139 260
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
140 260
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
141 260
        vdir_add_backend(ctx, rr->vd, be, w);
142 260
}
143
144 10
VCL_VOID vmod_random_remove_backend(VRT_CTX,
145
    struct vmod_directors_random *rr, VCL_BACKEND be) v_matchproto_()
146
{
147 10
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
148 10
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
149 10
        vdir_remove_backend(ctx, rr->vd, be, NULL);
150 10
}
151
152
VCL_BACKEND v_matchproto_()
153 380
vmod_random_backend(VRT_CTX, struct vmod_directors_random *rr)
154
{
155 380
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
156 380
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
157 380
        return (rr->vd->dir);
158
}