Adj: VFTs for adjacency sub-blocks
[vpp.git] / src / vnet / adj / adj_delegate.h
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 #ifndef __ADJ_DELEGATE_T__
17 #define __ADJ_DELEGATE_T__
18
19 #include <vnet/adj/adj.h>
20
21 /**
22  * Delegate types
23  */
24 typedef enum adj_delegate_type_t_ {
25     /**
26      * BFD session state
27      */
28     ADJ_DELEGATE_BFD,
29 } adj_delegate_type_t;
30
31 #define FOR_EACH_ADJ_DELEGATE(_adj, _adt, _aed, _body)        \
32 {                                                             \
33     for (_adt = ADJ_DELEGATE_BFD;                             \
34          _adt <= ADJ_DELEGATE_BFD;                            \
35          _adt++)                                              \
36     {                                                         \
37         _aed = adj_delegate_get(_adj, _adt);                  \
38         if (NULL != _aed) {                                   \
39             _body;                                            \
40         }                                                     \
41     }                                                         \
42 }
43
44 /**
45  * Distillation of the BFD session states into a go/no-go for using
46  * the associated tracked adjacency
47  */
48 typedef enum adj_bfd_state_t_
49 {
50     ADJ_BFD_STATE_DOWN,
51     ADJ_BFD_STATE_UP,
52 } adj_bfd_state_t;
53
54 /**
55  * A Delagate is a means to implement the Delagation design pattern;
56  * the extension of an object's functionality through the composition of,
57  * and delgation to, other objects.
58  * These 'other' objects are delegates. Delagates are thus attached to
59  * ADJ objects to extend their functionality.
60  */
61 typedef struct adj_delegate_t_
62 {
63     /**
64      * The ADJ entry object to which the delagate is attached
65      */
66     adj_index_t ad_adj_index;
67
68     /**
69      * The delagate type
70      */
71     adj_delegate_type_t ad_type;
72
73     /**
74      * A union of data for the different delegate types
75      */
76     union
77     {
78         /**
79          * BFD delegate daa
80          */
81         struct {
82             /**
83              * BFD session state
84              */
85             adj_bfd_state_t ad_bfd_state;
86             /**
87              * BFD session index
88              */
89             u32 ad_bfd_index;
90         };
91     };
92 } adj_delegate_t;
93
94 /**
95  * An ADJ delegate virtual function table
96  */
97 typedef void (*adj_delegate_last_lock_gone_t)(ip_adjacency_t *adj, adj_delegate_t *aed);
98 typedef u8 * (*adj_delegate_format_t)(const adj_delegate_t *aed, u8 *s);
99 typedef struct adj_delegate_vft_t_ {
100   adj_delegate_format_t adv_format;
101   adj_delegate_last_lock_gone_t adv_last_lock;
102 } adj_delegate_vft_t;
103
104 extern void adj_delegate_remove(ip_adjacency_t *adj,
105                                 adj_delegate_type_t type);
106
107 extern adj_delegate_t *adj_delegate_find_or_add(ip_adjacency_t *adj,
108                                                 adj_delegate_type_t fdt);
109 extern adj_delegate_t *adj_delegate_get(const ip_adjacency_t *adj,
110                                         adj_delegate_type_t type);
111
112 extern u8 *format_adj_delegate(u8 * s, va_list * args);
113 extern void adj_delegate_register_type(adj_delegate_type_t type, const adj_delegate_vft_t *vft);
114
115 #endif