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 42
vmod_random_healthy(VRT_CTX, VCL_BACKEND dir, VCL_TIME *changed)
52
{
53
        struct vmod_directors_random *rr;
54
55 42
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
56 42
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
57 42
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC);
58 42
        return (vdir_any_healthy(ctx, rr->vd, changed));
59
}
60
61
static void v_matchproto_(vdi_list_f)
62 357
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 357
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
68 357
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
69 357
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC);
70 357
        vdir_list(ctx, rr->vd, vsb, pflag, jflag, 1);
71 357
}
72
73
static VCL_BACKEND v_matchproto_(vdi_resolve_f)
74 756
vmod_random_resolve(VRT_CTX, VCL_BACKEND dir)
75
{
76
        struct vmod_directors_random *rr;
77
        VCL_BACKEND be;
78
        double r;
79
80 756
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
81 756
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
82 756
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC);
83 756
        r = scalbn(VRND_RandomTestable(), -31);
84 756
        assert(r >= 0 && r < 1.0);
85 756
        be = vdir_pick_be(ctx, rr->vd, r);
86 756
        return (be);
87
}
88
89
static void v_matchproto_(vdi_destroy_f)
90 21
vmod_random_destroy(VCL_BACKEND dir)
91
{
92
        struct vmod_directors_random *rr;
93
94 21
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC);
95 21
        vdir_delete(&rr->vd);
96 21
        FREE_OBJ(rr);
97 21
}
98
99
static const struct vdi_methods vmod_random_methods[1] = {{
100
        .magic =                VDI_METHODS_MAGIC,
101
        .type =                 "random",
102
        .healthy =              vmod_random_healthy,
103
        .resolve =              vmod_random_resolve,
104
        .destroy =              vmod_random_destroy,
105
        .list =                 vmod_random_list
106
}};
107
108
109
VCL_VOID v_matchproto_()
110 210
vmod_random__init(VRT_CTX, struct vmod_directors_random **rrp,
111
    const char *vcl_name)
112
{
113
        struct vmod_directors_random *rr;
114
115 210
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
116 210
        AN(rrp);
117 210
        AZ(*rrp);
118 210
        ALLOC_OBJ(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
119 210
        AN(rr);
120 210
        *rrp = rr;
121 210
        vdir_new(ctx, &rr->vd, vcl_name, vmod_random_methods, rr);
122 210
}
123
124
VCL_VOID v_matchproto_()
125 21
vmod_random__fini(struct vmod_directors_random **rrp)
126
{
127
        struct vmod_directors_random *rr;
128
129 21
        TAKE_OBJ_NOTNULL(rr, rrp, VMOD_DIRECTORS_RANDOM_MAGIC);
130 21
        VRT_DelDirector(&rr->vd->dir);
131 21
}
132
133
VCL_VOID v_matchproto_()
134 609
vmod_random_add_backend(VRT_CTX,
135
    struct vmod_directors_random *rr, VCL_BACKEND be, double w)
136
{
137
138 609
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
139 609
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
140 609
        vdir_add_backend(ctx, rr->vd, be, w);
141 609
}
142
143 21
VCL_VOID vmod_random_remove_backend(VRT_CTX,
144
    struct vmod_directors_random *rr, VCL_BACKEND be) v_matchproto_()
145
{
146 21
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
147 21
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
148 21
        vdir_remove_backend(ctx, rr->vd, be, NULL);
149 21
}
150
151
VCL_BACKEND v_matchproto_()
152 798
vmod_random_backend(VRT_CTX, struct vmod_directors_random *rr)
153
{
154 798
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
155 798
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
156 798
        return (rr->vd->dir);
157
}