2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/adj/adj_delegate.h>
17 #include <vnet/adj/adj.h>
18 #include <vnet/adj/adj_internal.h>
21 * The per-type vector of virtual function tables
23 static adj_delegate_vft_t *ad_vfts;
26 * The value of the last dynamically allocated delegate value
28 static adj_delegate_type_t ad_max_id = ADJ_DELEGATE_LAST;
30 static adj_delegate_t *
31 adj_delegate_find_i (const ip_adjacency_t *adj,
32 adj_delegate_type_t type,
35 adj_delegate_t *delegate;
39 vec_foreach(delegate, adj->ia_delegates)
41 if (delegate->ad_type == type)
58 adj_delegate_get (const ip_adjacency_t *adj,
59 adj_delegate_type_t type)
61 return (adj_delegate_find_i(adj, type, NULL));
65 adj_delegate_remove (adj_index_t ai,
66 adj_delegate_type_t type)
73 aed = adj_delegate_find_i(adj, type, &index);
77 vec_del1(adj->ia_delegates, index);
81 adj_delegate_cmp_for_sort (void * v1,
84 adj_delegate_t *aed1 = v1, *aed2 = v2;
86 return (aed1->ad_type - aed2->ad_type);
90 adj_delegate_init (ip_adjacency_t *adj,
91 adj_delegate_type_t adt,
95 adj_delegate_t aed = {
96 .ad_adj_index = adj_get_index(adj),
101 vec_add1(adj->ia_delegates, aed);
102 vec_sort_with_function(adj->ia_delegates,
103 adj_delegate_cmp_for_sort);
107 adj_delegate_add (ip_adjacency_t *adj,
108 adj_delegate_type_t adt,
111 adj_delegate_t *delegate;
113 delegate = adj_delegate_get(adj, adt);
115 if (NULL == delegate)
117 adj_delegate_init(adj, adt, adi);
128 adj_delegate_adj_modified (ip_adjacency_t *adj)
132 vec_foreach(aed, adj->ia_delegates)
134 if (ad_vfts[aed->ad_type].adv_adj_modified)
136 ad_vfts[aed->ad_type].adv_adj_modified(aed);
142 adj_delegate_adj_deleted (ip_adjacency_t *adj)
146 vec_foreach(aed, adj->ia_delegates)
148 if (ad_vfts[aed->ad_type].adv_adj_deleted)
150 ad_vfts[aed->ad_type].adv_adj_deleted(aed);
154 vec_reset_length(adj->ia_delegates);
158 adj_delegate_adj_created (ip_adjacency_t *adj)
160 adj_delegate_vft_t *vft;
162 vec_foreach(vft, ad_vfts)
164 if (vft->adv_adj_created)
166 vft->adv_adj_created(adj_get_index(adj));
172 adj_delegate_format (u8* s, ip_adjacency_t *adj)
176 vec_foreach(aed, adj->ia_delegates)
178 if (ad_vfts[aed->ad_type].adv_format)
180 s = format(s, "\n {");
181 s = ad_vfts[aed->ad_type].adv_format(aed, s);
186 s = format(s, "\n {unknown delegate}");
194 * adj_delegate_register_type
196 * Register the function table for a given type
199 adj_delegate_register_type (adj_delegate_type_t type,
200 const adj_delegate_vft_t *vft)
203 * assert that one only registration is made per-node type
205 if (vec_len(ad_vfts) > type)
206 ASSERT(NULL == ad_vfts[type].adv_adj_deleted);
208 vec_validate(ad_vfts, type);
209 ad_vfts[type] = *vft;
213 * adj_delegate_register_new_type
215 * Register the function table for a new type
218 adj_delegate_register_new_type (const adj_delegate_vft_t *vft)
220 adj_delegate_type_t type;
224 vec_validate(ad_vfts, type);
225 ad_vfts[type] = *vft;