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;
25 static adj_delegate_t *
26 adj_delegate_find_i (const ip_adjacency_t *adj,
27 adj_delegate_type_t type,
30 adj_delegate_t *delegate;
34 vec_foreach(delegate, adj->ia_delegates)
36 if (delegate->ad_type == type)
53 adj_delegate_get (const ip_adjacency_t *adj,
54 adj_delegate_type_t type)
56 return (adj_delegate_find_i(adj, type, NULL));
60 adj_delegate_remove (ip_adjacency_t *adj,
61 adj_delegate_type_t type)
66 aed = adj_delegate_find_i(adj, type, &index);
70 vec_del1(adj->ia_delegates, index);
74 adj_delegate_cmp_for_sort (void * v1,
77 adj_delegate_t *delegate1 = v1, *delegate2 = v2;
79 return (delegate1->ad_type - delegate2->ad_type);
83 adj_delegate_init (ip_adjacency_t *adj,
84 adj_delegate_type_t type)
87 adj_delegate_t delegate = {
88 .ad_adj_index = adj_get_index(adj),
92 vec_add1(adj->ia_delegates, delegate);
93 vec_sort_with_function(adj->ia_delegates,
94 adj_delegate_cmp_for_sort);
98 adj_delegate_find_or_add (ip_adjacency_t *adj,
99 adj_delegate_type_t adt)
101 adj_delegate_t *delegate;
103 delegate = adj_delegate_get(adj, adt);
105 if (NULL == delegate)
107 adj_delegate_init(adj, adt);
110 return (adj_delegate_get(adj, adt));
113 void adj_delegate_vft_lock_gone (ip_adjacency_t *adj)
115 adj_delegate_t *delegate;
116 vec_foreach(delegate, adj->ia_delegates) {
117 if (ad_vfts[delegate->ad_type].adv_last_lock)
118 ad_vfts[delegate->ad_type].adv_last_lock(adj, delegate);
123 format_adj_delegate (u8 * s, va_list * args)
127 aed = va_arg (*args, adj_delegate_t *);
128 if (ad_vfts[aed->ad_type].adv_format)
129 return ad_vfts[aed->ad_type].adv_format(aed, s);
130 return format(s, "unknown delegate");
134 * adj_delegate_register_type
136 * Register the function table for a given type
140 adj_delegate_register_type (adj_delegate_type_t type,
141 const adj_delegate_vft_t *vft)
144 * assert that one only registration is made per-node type
146 if (vec_len(ad_vfts) > type)
147 ASSERT(NULL == ad_vfts[type].adv_last_lock);
150 * Assert that we are getting each of the required functions
152 ASSERT(NULL != vft->adv_last_lock);
154 vec_validate(ad_vfts, type);
155 ad_vfts[type] = *vft;