FD.io VPP  v18.01.2-1-g9b554f3
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 static adj_delegate_t *
23  u32 *index)
24 {
25  adj_delegate_t *delegate;
26  int ii;
27 
28  ii = 0;
29  vec_foreach(delegate, adj->ia_delegates)
30  {
31  if (delegate->ad_type == type)
32  {
33  if (NULL != index)
34  *index = ii;
35 
36  return (delegate);
37  }
38  else
39  {
40  ii++;
41  }
42  }
43 
44  return (NULL);
45 }
46 
50 {
51  return (adj_delegate_find_i(adj, type, NULL));
52 }
53 
54 void
57 {
58  adj_delegate_t *aed;
59  u32 index = ~0;
60 
61  aed = adj_delegate_find_i(adj, type, &index);
62 
63  ASSERT(NULL != aed);
64 
65  vec_del1(adj->ia_delegates, index);
66 }
67 
68 static int
70  void * v2)
71 {
72  adj_delegate_t *delegate1 = v1, *delegate2 = v2;
73 
74  return (delegate1->ad_type - delegate2->ad_type);
75 }
76 
77 static void
80 
81 {
82  adj_delegate_t delegate = {
84  .ad_type = type,
85  };
86 
87  vec_add1(adj->ia_delegates, delegate);
90 }
91 
95 {
96  adj_delegate_t *delegate;
97 
98  delegate = adj_delegate_get(adj, adt);
99 
100  if (NULL == delegate)
101  {
102  adj_delegate_init(adj, adt);
103  }
104 
105  return (adj_delegate_get(adj, adt));
106 }
107 
108 /**
109  * typedef for printing a delegate
110  */
111 typedef u8 * (*adj_delegate_format_t)(const adj_delegate_t *aed,
112  u8 *s);
113 
114 /**
115  * Print a delegate that represents BFD tracking
116  */
117 static u8 *
119  u8 *s)
120 {
121  s = format(s, "BFD:[state:%d index:%d]",
122  aed->ad_bfd_state,
123  aed->ad_bfd_index);
124 
125  return (s);
126 }
127 
128 /**
129  * A delegate type to formatter map
130  */
132 {
134 };
135 
136 u8 *
137 format_adj_deletegate (u8 * s, va_list * args)
138 {
139  adj_delegate_t *aed;
140 
141  aed = va_arg (*args, adj_delegate_t *);
142 
143  return (aed_formatters[aed->ad_type](aed, s));
144 }
adj_delegate_type_t ad_type
The delagate type.
Definition: adj_delegate.h:71
static int adj_delegate_cmp_for_sort(void *v1, void *v2)
Definition: adj_delegate.c:69
static adj_delegate_t * adj_delegate_find_i(const ip_adjacency_t *adj, adj_delegate_type_t type, u32 *index)
Definition: adj_delegate.c:21
#define NULL
Definition: clib.h:55
IP unicast adjacency.
Definition: adj.h:174
adj_bfd_state_t ad_bfd_state
BFD session state.
Definition: adj_delegate.h:85
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:518
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
static adj_delegate_format_t aed_formatters[]
A delegate type to formatter map.
Definition: adj_delegate.c:131
u32 ad_bfd_index
BFD session index.
Definition: adj_delegate.h:89
static u8 * adj_delegate_fmt_bfd(const adj_delegate_t *aed, u8 *s)
Print a delegate that represents BFD tracking.
Definition: adj_delegate.c:118
A Delagate is a means to implement the Delagation design pattern; the extension of an object&#39;s functi...
Definition: adj_delegate.h:61
enum adj_delegate_type_t_ adj_delegate_type_t
Delegate types.
static adj_index_t adj_get_index(ip_adjacency_t *adj)
Get a pointer to an adjacency object from its index.
Definition: adj_internal.h:86
adj_index_t ad_adj_index
The ADJ entry object to which the delagate is attached.
Definition: adj_delegate.h:66
adj_delegate_t * adj_delegate_find_or_add(ip_adjacency_t *adj, adj_delegate_type_t adt)
Definition: adj_delegate.c:93
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:801
u8 * format_adj_deletegate(u8 *s, va_list *args)
Definition: adj_delegate.c:137
u8 *(* adj_delegate_format_t)(const adj_delegate_t *aed, u8 *s)
typedef for printing a delegate
Definition: adj_delegate.c:111
void adj_delegate_remove(ip_adjacency_t *adj, adj_delegate_type_t type)
Definition: adj_delegate.c:55
BFD session state.
Definition: adj_delegate.h:28
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
adj_delegate_t * adj_delegate_get(const ip_adjacency_t *adj, adj_delegate_type_t type)
Definition: adj_delegate.c:48
static void adj_delegate_init(ip_adjacency_t *adj, adj_delegate_type_t type)
Definition: adj_delegate.c:78
unsigned char u8
Definition: types.h:56
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:956
#define vec_foreach(var, vec)
Vector iterator.
struct adj_delegate_t_ * ia_delegates
more control plane members that do not fit on the first cacheline
Definition: adj.h:271