FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
adj_delegate.c
Go to the documentation of this file.
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  */
24 
25 /**
26  * The value of the last dynamically allocated delegate value
27  */
29 
30 static adj_delegate_t *
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 
60 {
61  return (adj_delegate_find_i(adj, type, NULL));
62 }
63 
64 void
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 
78 }
79 
80 static int
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
92  index_t adi)
93 
94 {
95  adj_delegate_t aed = {
97  .ad_type = adt,
98  .ad_index = adi,
99  };
100 
101  vec_add1(adj->ia_delegates, aed);
104 }
105 
106 int
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
129 {
130  adj_delegate_t *aed;
131 
132  vec_foreach(aed, adj->ia_delegates)
133  {
134  if (ad_vfts[aed->ad_type].adv_adj_modified)
135  {
136  ad_vfts[aed->ad_type].adv_adj_modified(aed);
137  }
138  }
139 }
140 
141 void
143 {
144  adj_delegate_t *aed;
145 
146  vec_foreach(aed, adj->ia_delegates)
147  {
148  if (ad_vfts[aed->ad_type].adv_adj_deleted)
149  {
150  ad_vfts[aed->ad_type].adv_adj_deleted(aed);
151  }
152  }
153 
155 }
156 
157 void
159 {
160  adj_delegate_vft_t *vft;
161 
162  vec_foreach(vft, ad_vfts)
163  {
164  if (vft->adv_adj_created)
165  {
166  vft->adv_adj_created(adj_get_index(adj));
167  }
168  }
169 }
170 
171 u8*
173 {
174  adj_delegate_t *aed;
175 
176  vec_foreach(aed, adj->ia_delegates)
177  {
178  if (ad_vfts[aed->ad_type].adv_format)
179  {
180  s = format(s, "\n {");
181  s = ad_vfts[aed->ad_type].adv_format(aed, s);
182  s = format(s, "}");
183  }
184  else
185  {
186  s = format(s, "\n {unknown delegate}");
187  }
188  }
189 
190  return (s);
191 }
192 
193 /**
194  * adj_delegate_register_type
195  *
196  * Register the function table for a given type
197  */
198 void
200  const adj_delegate_vft_t *vft)
201 {
202  /*
203  * assert that one only registration is made per-node type
204  */
205  if (vec_len(ad_vfts) > type)
206  ASSERT(NULL == ad_vfts[type].adv_adj_deleted);
207 
209  ad_vfts[type] = *vft;
210 }
211 
212 /**
213  * adj_delegate_register_new_type
214  *
215  * Register the function table for a new type
216  */
219 {
221 
222  type = ++ad_max_id;
223 
225  ad_vfts[type] = *vft;
226 
227  return (type);
228 }
vec_reset_length
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
Definition: vec_bootstrap.h:194
ad_vfts
static adj_delegate_vft_t * ad_vfts
Definition: adj_delegate.c:23
adj_delegate_remove
void adj_delegate_remove(adj_index_t ai, adj_delegate_type_t type)
Remove a delegate from an adjacency.
Definition: adj_delegate.c:65
adj.h
adj_delegate_adj_created
void adj_delegate_adj_created(ip_adjacency_t *adj)
Definition: adj_delegate.c:158
adj_delegate_vft_t_
An ADJ delegate virtual function table.
Definition: adj_delegate.h:94
adj_delegate_t_
Adj delegate.
Definition: adj_delegate.h:50
adj_delegate_t_::ad_type
adj_delegate_type_t ad_type
The delagate type.
Definition: adj_delegate.h:60
adj_delegate_find_i
static adj_delegate_t * adj_delegate_find_i(const ip_adjacency_t *adj, adj_delegate_type_t type, u32 *index)
Definition: adj_delegate.c:31
ad_max_id
static adj_delegate_type_t ad_max_id
The value of the last dynamically allocated delegate value.
Definition: adj_delegate.c:28
adj_delegate_add
int adj_delegate_add(ip_adjacency_t *adj, adj_delegate_type_t adt, index_t adi)
Add a delegate to an adjacency.
Definition: adj_delegate.c:107
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
adj_get_index
static adj_index_t adj_get_index(const ip_adjacency_t *adj)
Get a pointer to an adjacency object from its index.
Definition: adj_internal.h:109
adj_delegate_type_t
enum adj_delegate_type_t_ adj_delegate_type_t
A Delagate is a means to implement the Delagation design pattern; the extension of an object's functi...
ip_adjacency_t_::ia_delegates
struct adj_delegate_t_ * ia_delegates
A sorted vector of delegates.
Definition: adj.h:325
adj_delegate_t_::ad_adj_index
adj_index_t ad_adj_index
The ADJ entry object to which the delagate is attached.
Definition: adj_delegate.h:55
index_t
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:43
ADJ_DELEGATE_LAST
#define ADJ_DELEGATE_LAST
Definition: adj_delegate.h:45
adj_delegate.h
adj_delegate_adj_modified
void adj_delegate_adj_modified(ip_adjacency_t *adj)
Definition: adj_delegate.c:128
vec_validate
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment)
Definition: vec.h:523
adj_delegate_register_new_type
adj_delegate_type_t adj_delegate_register_new_type(const adj_delegate_vft_t *vft)
adj_delegate_register_new_type
Definition: adj_delegate.c:218
adj_delegate_register_type
void adj_delegate_register_type(adj_delegate_type_t type, const adj_delegate_vft_t *vft)
adj_delegate_register_type
Definition: adj_delegate.c:199
ip_adjacency_t_
IP unicast adjacency.
Definition: adj.h:235
adj_delegate_init
static void adj_delegate_init(ip_adjacency_t *adj, adj_delegate_type_t adt, index_t adi)
Definition: adj_delegate.c:90
adj_delegate_get
adj_delegate_t * adj_delegate_get(const ip_adjacency_t *adj, adj_delegate_type_t type)
Get a delegate from an adjacency.
Definition: adj_delegate.c:58
index
u32 index
Definition: flow_types.api:221
format
description fragment has unexpected format
Definition: map.api:433
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
u32
unsigned int u32
Definition: types.h:88
adj_delegate_vft_t_::adv_adj_deleted
adj_delegate_adj_deleted_t adv_adj_deleted
Definition: adj_delegate.h:96
adj_internal.h
adj_delegate_format
u8 * adj_delegate_format(u8 *s, ip_adjacency_t *adj)
Definition: adj_delegate.c:172
vec_foreach
#define vec_foreach(var, vec)
Vector iterator.
Definition: vec_bootstrap.h:213
vec_sort_with_function
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:1097
adj_index_t
u32 adj_index_t
An index for adjacencies.
Definition: adj_types.h:30
u8
unsigned char u8
Definition: types.h:56
adj_delegate_vft_t_::adv_adj_modified
adj_delegate_adj_modified_t adv_adj_modified
Definition: adj_delegate.h:97
adj_get
static ip_adjacency_t * adj_get(adj_index_t adj_index)
Get a pointer to an adjacency object from its index.
Definition: adj.h:470
adj_delegate_vft_t_::adv_format
adj_delegate_format_t adv_format
Definition: adj_delegate.h:95
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
vec_del1
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:896
adj_delegate_cmp_for_sort
static int adj_delegate_cmp_for_sort(void *v1, void *v2)
Definition: adj_delegate.c:81
adj_delegate_adj_deleted
void adj_delegate_adj_deleted(ip_adjacency_t *adj)
Definition: adj_delegate.c:142
adj_delegate_vft_t_::adv_adj_created
adj_delegate_adj_created_t adv_adj_created
Definition: adj_delegate.h:98