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 delegeate value
28 static adj_delegate_type_t ad_max_id = ADJ_DELEGATE_BFD;
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_deleted (ip_adjacency_t *adj)
132 vec_foreach(aed, adj->ia_delegates)
134 if (ad_vfts[aed->ad_type].adv_adj_deleted)
136 ad_vfts[aed->ad_type].adv_adj_deleted(aed);
140 vec_reset_length(adj->ia_delegates);
144 adj_delegate_format (u8* s, ip_adjacency_t *adj)
148 vec_foreach(aed, adj->ia_delegates)
150 if (ad_vfts[aed->ad_type].adv_format)
153 s = ad_vfts[aed->ad_type].adv_format(aed, s);
158 s = format(s, "{unknown delegate}");
166 * adj_delegate_register_type
168 * Register the function table for a given type
171 adj_delegate_register_type (adj_delegate_type_t type,
172 const adj_delegate_vft_t *vft)
175 * assert that one only registration is made per-node type
177 if (vec_len(ad_vfts) > type)
178 ASSERT(NULL == ad_vfts[type].adv_adj_deleted);
180 vec_validate(ad_vfts, type);
181 ad_vfts[type] = *vft;
185 * adj_delegate_register_new_type
187 * Register the function table for a new type
190 adj_delegate_register_new_type (const adj_delegate_vft_t *vft)
192 adj_delegate_type_t type;
196 vec_validate(ad_vfts, type);
197 ad_vfts[type] = *vft;