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 **delegate1 = v1, **delegate2 = v2;
86 return ((*delegate1)->ad_type - (*delegate2)->ad_type);
90 adj_delegate_init (ip_adjacency_t *adj,
91 adj_delegate_type_t adt,
95 aed->ad_adj_index = adj_get_index(adj);
98 vec_add1(adj->ia_delegates, aed);
99 vec_sort_with_function(adj->ia_delegates,
100 adj_delegate_cmp_for_sort);
104 adj_delegate_add (ip_adjacency_t *adj,
105 adj_delegate_type_t adt,
108 adj_delegate_t *delegate;
110 delegate = adj_delegate_get(adj, adt);
112 if (NULL == delegate)
114 adj_delegate_init(adj, adt, ad);
125 adj_delegate_adj_deleted (ip_adjacency_t *adj)
127 adj_delegate_t **delegate;
129 vec_foreach(delegate, adj->ia_delegates)
131 if (ad_vfts[(*delegate)->ad_type].adv_adj_deleted)
133 ad_vfts[(*delegate)->ad_type].adv_adj_deleted(*delegate);
137 vec_reset_length(adj->ia_delegates);
141 adj_delegate_format (u8* s, ip_adjacency_t *adj)
143 adj_delegate_t **aed;
145 vec_foreach(aed, adj->ia_delegates)
147 if (ad_vfts[(*aed)->ad_type].adv_format)
150 s = ad_vfts[(*aed)->ad_type].adv_format(*aed, s);
155 s = format(s, "{unknown delegate}");
163 * adj_delegate_register_type
165 * Register the function table for a given type
168 adj_delegate_register_type (adj_delegate_type_t type,
169 const adj_delegate_vft_t *vft)
172 * assert that one only registration is made per-node type
174 if (vec_len(ad_vfts) > type)
175 ASSERT(NULL == ad_vfts[type].adv_adj_deleted);
177 vec_validate(ad_vfts, type);
178 ad_vfts[type] = *vft;
182 * adj_delegate_register_new_type
184 * Register the function table for a new type
187 adj_delegate_register_new_type (const adj_delegate_vft_t *vft)
189 adj_delegate_type_t type;
193 vec_validate(ad_vfts, type);
194 ad_vfts[type] = *vft;