blob: 87a83fb42399841c4acdd2cec54185a6e5c6d632 [file] [log] [blame]
Neale Ranns88fc83e2017-04-05 08:11:14 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <vnet/adj/adj_delegate.h>
17#include <vnet/adj/adj.h>
18#include <vnet/adj/adj_internal.h>
19
Ole Troan793c7fe2018-02-15 16:14:56 +010020/*
21 * The per-type vector of virtual function tables
22 */
23static adj_delegate_vft_t *ad_vfts;
24
Neale Rannsd79a43c2018-02-19 02:36:19 -080025/**
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -070026 * The value of the last dynamically allocated delegate value
Neale Rannsd79a43c2018-02-19 02:36:19 -080027 */
Neale Ranns4c3ba812019-03-26 07:02:58 +000028static adj_delegate_type_t ad_max_id = ADJ_DELEGATE_LAST;
Neale Rannsd79a43c2018-02-19 02:36:19 -080029
Neale Ranns88fc83e2017-04-05 08:11:14 -070030static adj_delegate_t *
31adj_delegate_find_i (const ip_adjacency_t *adj,
32 adj_delegate_type_t type,
33 u32 *index)
34{
Neale Ranns76447a72018-02-20 06:25:02 -080035 adj_delegate_t *delegate;
Neale Ranns88fc83e2017-04-05 08:11:14 -070036 int ii;
37
38 ii = 0;
39 vec_foreach(delegate, adj->ia_delegates)
40 {
Neale Ranns76447a72018-02-20 06:25:02 -080041 if (delegate->ad_type == type)
Neale Ranns88fc83e2017-04-05 08:11:14 -070042 {
43 if (NULL != index)
44 *index = ii;
45
Neale Ranns76447a72018-02-20 06:25:02 -080046 return (delegate);
Neale Ranns88fc83e2017-04-05 08:11:14 -070047 }
48 else
49 {
50 ii++;
51 }
52 }
53
54 return (NULL);
55}
56
57adj_delegate_t *
58adj_delegate_get (const ip_adjacency_t *adj,
59 adj_delegate_type_t type)
60{
61 return (adj_delegate_find_i(adj, type, NULL));
62}
63
64void
Neale Rannsd79a43c2018-02-19 02:36:19 -080065adj_delegate_remove (adj_index_t ai,
Neale Ranns88fc83e2017-04-05 08:11:14 -070066 adj_delegate_type_t type)
67{
Neale Rannsd79a43c2018-02-19 02:36:19 -080068 ip_adjacency_t *adj;
Neale Ranns88fc83e2017-04-05 08:11:14 -070069 adj_delegate_t *aed;
70 u32 index = ~0;
71
Neale Rannsd79a43c2018-02-19 02:36:19 -080072 adj = adj_get(ai);
Neale Ranns88fc83e2017-04-05 08:11:14 -070073 aed = adj_delegate_find_i(adj, type, &index);
74
75 ASSERT(NULL != aed);
76
77 vec_del1(adj->ia_delegates, index);
78}
79
80static int
81adj_delegate_cmp_for_sort (void * v1,
82 void * v2)
83{
Neale Ranns76447a72018-02-20 06:25:02 -080084 adj_delegate_t *aed1 = v1, *aed2 = v2;
Neale Ranns88fc83e2017-04-05 08:11:14 -070085
Neale Ranns76447a72018-02-20 06:25:02 -080086 return (aed1->ad_type - aed2->ad_type);
Neale Ranns88fc83e2017-04-05 08:11:14 -070087}
88
89static void
90adj_delegate_init (ip_adjacency_t *adj,
Neale Rannsd79a43c2018-02-19 02:36:19 -080091 adj_delegate_type_t adt,
Neale Ranns76447a72018-02-20 06:25:02 -080092 index_t adi)
Neale Ranns88fc83e2017-04-05 08:11:14 -070093
94{
Neale Ranns76447a72018-02-20 06:25:02 -080095 adj_delegate_t aed = {
96 .ad_adj_index = adj_get_index(adj),
97 .ad_type = adt,
98 .ad_index = adi,
99 };
Neale Ranns88fc83e2017-04-05 08:11:14 -0700100
Neale Rannsd79a43c2018-02-19 02:36:19 -0800101 vec_add1(adj->ia_delegates, aed);
Neale Ranns88fc83e2017-04-05 08:11:14 -0700102 vec_sort_with_function(adj->ia_delegates,
103 adj_delegate_cmp_for_sort);
104}
105
Neale Rannsd79a43c2018-02-19 02:36:19 -0800106int
107adj_delegate_add (ip_adjacency_t *adj,
108 adj_delegate_type_t adt,
Neale Ranns76447a72018-02-20 06:25:02 -0800109 index_t adi)
Neale Ranns88fc83e2017-04-05 08:11:14 -0700110{
111 adj_delegate_t *delegate;
112
113 delegate = adj_delegate_get(adj, adt);
114
115 if (NULL == delegate)
116 {
Neale Ranns76447a72018-02-20 06:25:02 -0800117 adj_delegate_init(adj, adt, adi);
Neale Rannsd79a43c2018-02-19 02:36:19 -0800118 }
119 else
120 {
121 return (-1);
Neale Ranns88fc83e2017-04-05 08:11:14 -0700122 }
123
Neale Rannsd79a43c2018-02-19 02:36:19 -0800124 return (0);
Neale Ranns88fc83e2017-04-05 08:11:14 -0700125}
126
Neale Rannsd79a43c2018-02-19 02:36:19 -0800127void
128adj_delegate_adj_deleted (ip_adjacency_t *adj)
Neale Ranns88fc83e2017-04-05 08:11:14 -0700129{
Neale Ranns76447a72018-02-20 06:25:02 -0800130 adj_delegate_t *aed;
Neale Rannsd79a43c2018-02-19 02:36:19 -0800131
Neale Ranns76447a72018-02-20 06:25:02 -0800132 vec_foreach(aed, adj->ia_delegates)
Neale Rannsd79a43c2018-02-19 02:36:19 -0800133 {
Neale Ranns76447a72018-02-20 06:25:02 -0800134 if (ad_vfts[aed->ad_type].adv_adj_deleted)
Neale Rannsd79a43c2018-02-19 02:36:19 -0800135 {
Neale Ranns76447a72018-02-20 06:25:02 -0800136 ad_vfts[aed->ad_type].adv_adj_deleted(aed);
Neale Rannsd79a43c2018-02-19 02:36:19 -0800137 }
Ole Troan793c7fe2018-02-15 16:14:56 +0100138 }
Neale Rannsd79a43c2018-02-19 02:36:19 -0800139
140 vec_reset_length(adj->ia_delegates);
Neale Ranns88fc83e2017-04-05 08:11:14 -0700141}
142
Neale Ranns77cfc012019-12-15 22:26:37 +0000143void
144adj_delegate_adj_created (ip_adjacency_t *adj)
145{
146 adj_delegate_vft_t *vft;
147
148 vec_foreach(vft, ad_vfts)
149 {
150 if (vft->adv_adj_created)
151 {
152 vft->adv_adj_created(adj_get_index(adj));
153 }
154 }
155}
156
Neale Rannsd79a43c2018-02-19 02:36:19 -0800157u8*
158adj_delegate_format (u8* s, ip_adjacency_t *adj)
Neale Ranns88fc83e2017-04-05 08:11:14 -0700159{
Neale Ranns76447a72018-02-20 06:25:02 -0800160 adj_delegate_t *aed;
Neale Ranns88fc83e2017-04-05 08:11:14 -0700161
Neale Rannsd79a43c2018-02-19 02:36:19 -0800162 vec_foreach(aed, adj->ia_delegates)
163 {
Neale Ranns76447a72018-02-20 06:25:02 -0800164 if (ad_vfts[aed->ad_type].adv_format)
Neale Rannsd79a43c2018-02-19 02:36:19 -0800165 {
Neale Ranns14053c92019-12-29 23:55:18 +0000166 s = format(s, "\n {");
Neale Ranns76447a72018-02-20 06:25:02 -0800167 s = ad_vfts[aed->ad_type].adv_format(aed, s);
Neale Rannsd79a43c2018-02-19 02:36:19 -0800168 s = format(s, "}");
169 }
170 else
171 {
Neale Ranns14053c92019-12-29 23:55:18 +0000172 s = format(s, "\n {unknown delegate}");
Neale Rannsd79a43c2018-02-19 02:36:19 -0800173 }
174 }
175
176 return (s);
Ole Troan793c7fe2018-02-15 16:14:56 +0100177}
Neale Ranns88fc83e2017-04-05 08:11:14 -0700178
Ole Troan793c7fe2018-02-15 16:14:56 +0100179/**
180 * adj_delegate_register_type
181 *
182 * Register the function table for a given type
183 */
Ole Troan793c7fe2018-02-15 16:14:56 +0100184void
185adj_delegate_register_type (adj_delegate_type_t type,
186 const adj_delegate_vft_t *vft)
187{
Neale Rannsd79a43c2018-02-19 02:36:19 -0800188 /*
189 * assert that one only registration is made per-node type
190 */
191 if (vec_len(ad_vfts) > type)
192 ASSERT(NULL == ad_vfts[type].adv_adj_deleted);
Ole Troan793c7fe2018-02-15 16:14:56 +0100193
Neale Rannsd79a43c2018-02-19 02:36:19 -0800194 vec_validate(ad_vfts, type);
195 ad_vfts[type] = *vft;
196}
197
198/**
199 * adj_delegate_register_new_type
200 *
201 * Register the function table for a new type
202 */
203adj_delegate_type_t
204adj_delegate_register_new_type (const adj_delegate_vft_t *vft)
205{
206 adj_delegate_type_t type;
207
208 type = ++ad_max_id;
209
210 vec_validate(ad_vfts, type);
211 ad_vfts[type] = *vft;
212
213 return (type);
Neale Ranns88fc83e2017-04-05 08:11:14 -0700214}