Typos. A bunch of typos I've been collecting.
[vpp.git] / src / vnet / adj / adj_delegate.c
1 /*
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
20 /*
21  * The per-type vector of virtual function tables
22  */
23 static adj_delegate_vft_t *ad_vfts;
24
25 /**
26  * The value of the last dynamically allocated delegate value
27  */
28 static adj_delegate_type_t ad_max_id = ADJ_DELEGATE_LAST;
29
30 static adj_delegate_t *
31 adj_delegate_find_i (const ip_adjacency_t *adj,
32                      adj_delegate_type_t type,
33                      u32 *index)
34 {
35     adj_delegate_t *delegate;
36     int ii;
37
38     ii = 0;
39     vec_foreach(delegate, adj->ia_delegates)
40     {
41         if (delegate->ad_type == type)
42         {
43             if (NULL != index)
44                 *index = ii;
45
46             return (delegate);
47         }
48         else
49         {
50             ii++;
51         }
52     }
53
54     return (NULL);
55 }
56
57 adj_delegate_t *
58 adj_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
64 void
65 adj_delegate_remove (adj_index_t ai,
66                      adj_delegate_type_t type)
67 {
68     ip_adjacency_t *adj;
69     adj_delegate_t *aed;
70     u32 index = ~0;
71
72     adj = adj_get(ai);
73     aed = adj_delegate_find_i(adj, type, &index);
74
75     ASSERT(NULL != aed);
76
77     vec_del1(adj->ia_delegates, index);
78 }
79
80 static int
81 adj_delegate_cmp_for_sort (void * v1,
82                            void * v2)
83 {
84     adj_delegate_t *aed1 = v1, *aed2 = v2;
85
86     return (aed1->ad_type - aed2->ad_type);
87 }
88
89 static void
90 adj_delegate_init (ip_adjacency_t *adj,
91                    adj_delegate_type_t adt,
92                    index_t adi)
93
94 {
95     adj_delegate_t aed = {
96         .ad_adj_index = adj_get_index(adj),
97         .ad_type = adt,
98         .ad_index = adi,
99     };
100
101     vec_add1(adj->ia_delegates, aed);
102     vec_sort_with_function(adj->ia_delegates,
103                            adj_delegate_cmp_for_sort);
104 }
105
106 int
107 adj_delegate_add (ip_adjacency_t *adj,
108                   adj_delegate_type_t adt,
109                   index_t adi)
110 {
111     adj_delegate_t *delegate;
112
113     delegate = adj_delegate_get(adj, adt);
114
115     if (NULL == delegate)
116     {
117         adj_delegate_init(adj, adt, adi);
118     }
119     else
120     {
121         return (-1);
122     }
123
124     return (0);
125 }
126
127 void
128 adj_delegate_adj_deleted (ip_adjacency_t *adj)
129 {
130     adj_delegate_t *aed;
131
132     vec_foreach(aed, adj->ia_delegates)
133     {
134         if (ad_vfts[aed->ad_type].adv_adj_deleted)
135         {
136             ad_vfts[aed->ad_type].adv_adj_deleted(aed);
137         }
138     }
139
140     vec_reset_length(adj->ia_delegates);
141 }
142
143 u8*
144 adj_delegate_format (u8* s, ip_adjacency_t *adj)
145 {
146     adj_delegate_t *aed;
147
148     vec_foreach(aed, adj->ia_delegates)
149     {
150         if (ad_vfts[aed->ad_type].adv_format)
151         {
152             s = format(s, "{");
153             s = ad_vfts[aed->ad_type].adv_format(aed, s);
154             s = format(s, "}");
155         }
156         else
157         {
158             s = format(s, "{unknown delegate}");
159         }
160     }
161
162     return (s);
163 }
164
165 /**
166  * adj_delegate_register_type
167  *
168  * Register the function table for a given type
169  */
170 void
171 adj_delegate_register_type (adj_delegate_type_t type,
172                             const adj_delegate_vft_t *vft)
173 {
174     /*
175      * assert that one only registration is made per-node type
176      */
177     if (vec_len(ad_vfts) > type)
178         ASSERT(NULL == ad_vfts[type].adv_adj_deleted);
179
180     vec_validate(ad_vfts, type);
181     ad_vfts[type] = *vft;
182 }
183
184 /**
185  * adj_delegate_register_new_type
186  *
187  * Register the function table for a new type
188  */
189 adj_delegate_type_t
190 adj_delegate_register_new_type (const adj_delegate_vft_t *vft)
191 {
192     adj_delegate_type_t type;
193
194     type = ++ad_max_id;
195
196     vec_validate(ad_vfts, type);
197     ad_vfts[type] = *vft;
198
199     return (type);
200 }