summaryrefslogtreecommitdiffstats
path: root/src/vnet/fib/fib_entry_src_interpose.c
blob: c362328f473de09f29ec991b81edd87246788195 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vlib/vlib.h>
#include <vnet/ip/format.h>
#include <vnet/ip/lookup.h>
#include <vnet/adj/adj.h>
#include <vnet/dpo/drop_dpo.h>

#include "fib_entry_src.h"
#include "fib_entry_src_rr.h"
#include "fib_entry_cover.h"
#include "fib_entry.h"
#include "fib_table.h"

/*
 * Source initialisation Function
 */
static void
fib_entry_src_interpose_init (fib_entry_src_t *src)
{
    src->u.interpose.fesi_cover = FIB_NODE_INDEX_INVALID;
    src->u.interpose.fesi_sibling = FIB_NODE_INDEX_INVALID;
}

/*
 * Source deinitialisation Function
 */
static void
fib_entry_src_interpose_deinit (fib_entry_src_t *src)
{
    ASSERT(src->u.interpose.fesi_cover == FIB_NODE_INDEX_INVALID);

    src->u.interpose.fesi_cover = FIB_NODE_INDEX_INVALID;
    src->u.interpose.fesi_sibling = FIB_NODE_INDEX_INVALID;

    dpo_reset(&src->u.interpose.fesi_dpo);
}

static fib_entry_src_t *
fib_entry_src_rr_get_next_best (const fib_entry_src_t *src,
                                const fib_entry_t *fib_entry)
{
    fib_entry_src_t *next_src, *best_src = NULL;
    fib_source_t source;

    FOR_EACH_SRC_ADDED(fib_entry, next_src, source,
    ({
        /*
         * skip to the next best source after this one
         */
        if (source <= src->fes_src)
        {
            continue;
        }
        else
        {
            best_src = next_src;
            break;
        }
    }));

    return (best_src);
}

/*
 * Source activation. Called when the source is the new best source on the entry
 */
static int
fib_entry_src_interpose_activate (fib_entry_src_t *src,
                                  const fib_entry_t *fib_entry)
{
    fib_entry_src_t *best_src;
    fib_node_index_t old_pl;
    fib_entry_t *cover;

    old_pl = src->fes_pl;
    src->fes_pl = FIB_NODE_INDEX_INVALID;

    /*
     * The goal here is to find a path-list that will contribute forwarding
     * for the entry.
     * First check this entry for other sources that have a path-list
     */
    best_src = fib_entry_src_rr_get_next_best(src, fib_entry);

    if (NULL != best_src)
    {
        const fib_entry_src_vft_t *vft;

        best_src->fes_flags |= FIB_ENTRY_SRC_FLAG_CONTRIBUTING;
        vft = fib_entry_src_get_vft(best_src);
        /*
         * there is another source for this entry. activate it so it
         * can provide forwarding
         */
        if (NULL != vft->fesv_activate)
        {
            if (vft->fesv_activate(best_src, fib_entry))
            {
                /*
                 * next best source activated ok, use its path list
                 */
                src->fes_pl = best_src->fes_pl;
            }
        }
        else
        {
            /*
             * next best source does not require activation, use its path list
             */
            src->fes_pl = best_src->fes_pl;
        }
    }
    else
    {
        /*
         * find the covering prefix. become a dependent thereof.
         * for IP there should always be a cover, though it may be the default route.
         * For MPLS there is never a cover.
         */
        if (FIB_PROTOCOL_MPLS == fib_entry->fe_prefix.fp_proto)
        {
            src->fes_pl = fib_path_list_create_special(DPO_PROTO_MPLS,
                                                       FIB_PATH_LIST_FLAG_DROP,
                                                       NULL);
        }
        else
        {
            src->u.interpose.fesi_cover =
                fib_table_get_less_specific(fib_entry->fe_fib_index,
                                            &fib_entry->fe_prefix);

            ASSERT(FIB_NODE_INDEX_INVALID != src->u.interpose.fesi_cover);

            cover = fib_entry_get(src->u.interpose.fesi_cover);

            src->u.interpose.fesi_sibling =
                fib_entry_cover_track(cover, fib_entry_get_index(fib_entry));

            /*
             * if the cover is attached then install an attached-host path
             * (like an adj-fib). Otherwise inherit the forwarding from the cover
             */
            if (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_i(cover))
            {
                fib_entry_src_rr_resolve_via_connected(src, fib_entry, cover);
            }
            else
            {
                fib_entry_src_rr_use_covers_pl(src, fib_entry, cover);
            }
        }
    }

    fib_path_list_unlock(old_pl);
    fib_path_list_lock(src->fes_pl);

    /*
     * return go for install
     */
    return (!0);
}

/**
 * Source Deactivate.
 * Called when the source is no longer best source on the entry
 */
static void
fib_entry_src_interpose_deactivate (fib_entry_src_t *src,
                                    const fib_entry_t *fib_entry)
{
    fib_entry_t *cover;

    if (FIB_NODE_INDEX_INVALID != src->u.interpose.fesi_cover)
    {
        /*
         * remove the depednecy on the covering entry, if that's
         * what was contributing the path-list
         */
        cover = fib_entry_get(src->u.interpose.fesi_cover);
        fib_entry_cover_untrack(cover, src->u.interpose.fesi_sibling);
        src->u.interpose.fesi_cover = FIB_NODE_INDEX_INVALID;
    }
    else
    {
        fib_entry_src_t *best_src;

        best_src = fib_entry_src_rr_get_next_best(src, fib_entry);

        if (best_src)
        {
            best_src->fes_flags &= ~FIB_ENTRY_SRC_FLAG_CONTRIBUTING;
            /*
             * there is another source for this entry. activate it so it
             * can provide forwarding
             */
            FIB_ENTRY_SRC_VFT_INVOKE(best_src, fesv_deactivate,
                                     (best_src, fib_entry));
        }
    }

    fib_path_list_unlock(src->fes_pl);
    src->fes_pl = FIB_NODE_INDEX_INVALID;
    src->fes_entry_flags &= ~FIB_ENTRY_FLAGS_RR_INHERITED;
}

static int
fib_entry_src_interpose_reactivate (fib_entry_src_t *src,
                                    const fib_entry_t *fib_entry)
{
    fib_entry_src_interpose_deactivate(src, fib_entry);
    return (fib_entry_src_interpose_activate(src, fib_entry));
}

static fib_entry_src_cover_res_t
fib_entry_src_interpose_cover_change (fib_entry_src_t *src,
                                      const fib_entry_t *fib_entry)
{
    fib_entry_src_cover_res_t res = {
       .install = !0,
       .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
    };

    if (FIB_NODE_INDEX_INVALID == src->u.interpose.fesi_cover)
    {
       /*
        * the source may be added, but it is not active
        * if it is not tracking the cover.
        */
       return (res);
    }

    /*
     * this function is called when this entry's cover has a more specific
     * entry inserted benaeth it. That does not necessarily mean that this
     * entry is covered by the new prefix. check that
     */
    if (src->u.interpose.fesi_cover !=
        fib_table_get_less_specific(fib_entry->fe_fib_index,
                                    &fib_entry->fe_prefix))
    {
       fib_entry_src_interpose_deactivate(src, fib_entry);
       fib_entry_src_interpose_activate(src, fib_entry);

       /*
        * dependent children need to re-resolve to the new forwarding info
        */
       res.bw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE;
    }
    return (res);
}

static void
fib_entry_src_interpose_add (fib_entry_src_t *src,
                             const fib_entry_t *entry,
                             fib_entry_flag_t flags,
                             dpo_proto_t proto,
                             const dpo_id_t *dpo)
{
    dpo_copy(&src->u.interpose.fesi_dpo, dpo);
}

static void
fib_entry_src_interpose_remove (fib_entry_src_t *src)
{
    dpo_reset(&src->u.interpose.fesi_dpo);
}

static void
fib_entry_src_interpose_set_data (fib_entry_src_t *src,
                                  const fib_entry_t *fib_entry,
                                  const void *data)
{
    const dpo_id_t *dpo = data;

    dpo_copy(&src->u.interpose.fesi_dpo, dpo);
}

/**
 * Contribute forwarding to interpose in the chain
 */
const dpo_id_t* fib_entry_src_interpose_contribute(const fib_entry_src_t *src,
                                                   const fib_entry_t *fib_entry)
{
    return (&src->u.interpose.fesi_dpo);
}

static void
fib_entry_src_interpose_copy (const fib_entry_src_t *orig_src,
                              const fib_entry_t *fib_entry,
                              fib_entry_src_t *copy_src)
{
    copy_src->u.interpose.fesi_cover = orig_src->u.interpose.fesi_cover;

    if (FIB_NODE_INDEX_INVALID != copy_src->u.interpose.fesi_cover)
    {
        fib_entry_t *cover;

        cover = fib_entry_get(orig_src->u.interpose.fesi_cover);
        copy_src->u.interpose.fesi_sibling =
            fib_entry_cover_track(cover, fib_entry_get_index(fib_entry));
    }

    dpo_copy(&copy_src->u.interpose.fesi_dpo,
             &orig_src->u.interpose.fesi_dpo);
}

static void
fib_entry_src_interpose_flag_change (fib_entry_src_t *src,
                                     const fib_entry_t *fib_entry,
                                     fib_entry_flag_t new_flags)
{
    if (!(new_flags & FIB_ENTRY_FLAG_INTERPOSE))
    {
        /*
         * stop tracking the source contributing forwarding
         * and reset the interposer DPO
         */
        fib_entry_src_interpose_deactivate(src, fib_entry);
        fib_entry_src_interpose_deinit(src);
    }
}

static u8*
fib_entry_src_interpose_format (fib_entry_src_t *src,
                                u8* s)
{
    s = format(s, " cover:%d interpose:\n%U%U",
               src->u.interpose.fesi_cover,
               format_white_space, 6,
               format_dpo_id, &src->u.interpose.fesi_dpo, 8);

    return (s);
}

const static fib_entry_src_vft_t interpose_src_vft = {
    .fesv_init = fib_entry_src_interpose_init,
    .fesv_deinit = fib_entry_src_interpose_deinit,
    .fesv_activate = fib_entry_src_interpose_activate,
    .fesv_reactivate = fib_entry_src_interpose_reactivate,
    .fesv_deactivate = fib_entry_src_interpose_deactivate,
    .fesv_cover_change = fib_entry_src_interpose_cover_change,
    .fesv_cover_update = fib_entry_src_rr_cover_update,
    .fesv_format = fib_entry_src_interpose_format,
    .fesv_add = fib_entry_src_interpose_add,
    .fesv_remove = fib_entry_src_interpose_remove,
    .fesv_contribute_interpose = fib_entry_src_interpose_contribute,
    .fesv_set_data = fib_entry_src_interpose_set_data,
    .fesv_copy = fib_entry_src_interpose_copy,
    .fesv_flags_change = fib_entry_src_interpose_flag_change,
};

void
fib_entry_src_interpose_register (void)
{
    fib_entry_src_register(FIB_SOURCE_INTERPOSE, &interpose_src_vft);
}