aboutsummaryrefslogtreecommitdiffstats
path: root/vnet/vnet/fib/fib_entry_cover.c
blob: 147c5daa4fd125617dbce3c792d2d5aa509dcb5a (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
/*
 * 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 <vnet/fib/fib_entry_cover.h>
#include <vnet/fib/fib_entry_src.h>
#include <vnet/fib/fib_node_list.h>

u32
fib_entry_cover_track (fib_entry_t* cover,
		       fib_node_index_t covered)
{
    fib_entry_delegate_t *fed;

    FIB_ENTRY_DBG(cover, "cover-track %d", covered);

    ASSERT(fib_entry_get_index(cover) != covered);

    fed = fib_entry_delegate_get(cover, FIB_ENTRY_DELEGATE_COVERED);

    if (NULL == fed)
    {
        fed = fib_entry_delegate_find_or_add(cover, FIB_ENTRY_DELEGATE_COVERED);
        fed->fd_list = fib_node_list_create();
    }

    return (fib_node_list_push_front(fed->fd_list,
                                     0, FIB_NODE_TYPE_ENTRY,
                                     covered));
}

void
fib_entry_cover_untrack (fib_entry_t* cover,
			 u32 tracked_index)
{
    fib_entry_delegate_t *fed;

    FIB_ENTRY_DBG(cover, "cover-untrack @ %d", tracked_index);

    fed = fib_entry_delegate_get(cover, FIB_ENTRY_DELEGATE_COVERED);

    if (NULL == fed)
        return;

    fib_node_list_remove(fed->fd_list, tracked_index);

    if (0 == fib_node_list_get_size(fed->fd_list))
    {
        fib_node_list_destroy(&fed->fd_list);
        fib_entry_delegate_remove(cover, FIB_ENTRY_DELEGATE_COVERED);        
    }
}

/**
 * Internal struct to hold user supplied paraneters for the cover walk
 */
typedef struct fib_enty_cover_walk_ctx_t_ {
    fib_entry_t *cover;
    fib_entry_covered_walk_t walk;
    void *ctx;
} fib_enty_cover_walk_ctx_t;

static int
fib_entry_cover_walk_node_ptr (fib_node_ptr_t *depend,
			       void *args)
{
    fib_enty_cover_walk_ctx_t *ctx = args;

    ctx->walk(ctx->cover, depend->fnp_index, ctx->ctx);

    /* continue */
    return (1);
}

void
fib_entry_cover_walk (fib_entry_t *cover,
		      fib_entry_covered_walk_t walk,
		      void *args)
{
    fib_entry_delegate_t *fed;

    fed = fib_entry_delegate_get(cover, FIB_ENTRY_DELEGATE_COVERED);

    if (NULL == fed)
        return;

    fib_enty_cover_walk_ctx_t ctx = {
        .cover = cover,
        .walk = walk,
        .ctx = args,
    };

    fib_node_list_walk(fed->fd_list,
                       fib_entry_cover_walk_node_ptr,
                       &ctx);
}

u32
fib_entry_cover_get_size (fib_entry_t *cover)
{
    fib_entry_delegate_t *fed;

    fed = fib_entry_delegate_get(cover, FIB_ENTRY_DELEGATE_COVERED);

    if (NULL == fed)
        return (0);

    return (fib_node_list_get_size(fed->fd_list));
}

typedef struct fib_entry_cover_list_format_ctx_t_ {
    u8 *s;
} fib_entry_cover_list_format_ctx_t;

static int
fib_entry_covered_list_format_one (fib_entry_t *cover,
				   fib_node_index_t covered,
				   void *args)
{
    fib_entry_cover_list_format_ctx_t * ctx = args;

    ctx->s = format(ctx->s, "%d, ", covered);

    /* continue */
    return (1);
}

u8*
fib_entry_cover_list_format (fib_entry_t *fib_entry,
			     u8 *s)
{
    fib_entry_cover_list_format_ctx_t ctx = {
	.s = s,
    };

    fib_entry_cover_walk(fib_entry, 
			 fib_entry_covered_list_format_one,
			 &ctx);

    return (ctx.s);
}

static int
fib_entry_cover_change_one (fib_entry_t *cover,
			    fib_node_index_t covered,
			    void *args)
{
    fib_node_index_t new_cover;

    /*
     * The 3 entries involved here are:
     *   cover - the least specific. It will cover both the others
     *  new_cover - the enty just inserted below the cover
     *  covered - the entry that was tracking the cover.
     *
     * The checks below are to determine if new_cover is a cover for covered.
     */
    new_cover = pointer_to_uword(args);

    if (FIB_NODE_INDEX_INVALID == new_cover)
    {
	/*
	 * nothing has been inserted, which implies the cover was removed.
	 * 'cover' is thus the new cover.
	 */
	fib_entry_cover_changed(covered);
    }
    else if (new_cover != covered)
    {
	fib_prefix_t pfx_covered, pfx_new_cover;

	fib_entry_get_prefix(covered, &pfx_covered);
	fib_entry_get_prefix(new_cover, &pfx_new_cover);

	if (fib_prefix_is_cover(&pfx_new_cover, &pfx_covered))
	{
	    fib_entry_cover_changed(covered);
	}
    }
    /* continue */
    return (1);
}

void
fib_entry_cover_change_notify (fib_node_index_t cover_index,
			       fib_node_index_t covered)
{
    fib_entry_t *cover;

    cover = fib_entry_get(cover_index);

    fib_entry_cover_walk(cover, 
			 fib_entry_cover_change_one,
			 uword_to_pointer(covered, void*));
}

static int
fib_entry_cover_update_one (fib_entry_t *cover,
			    fib_node_index_t covered,
			    void *args)
{
    fib_entry_cover_updated(covered);

    /* continue */
    return (1);
}

void
fib_entry_cover_update_notify (fib_entry_t *fib_entry)
{
    fib_entry_cover_walk(fib_entry, 
			 fib_entry_cover_update_one,
			 NULL);
}