VOM: mroutes
[vpp.git] / src / vnet / fib / fib_entry_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/fib/fib_entry_delegate.h>
17 #include <vnet/fib/fib_entry.h>
18 #include <vnet/fib/fib_attached_export.h>
19
20 static fib_entry_delegate_t *
21 fib_entry_delegate_find_i (const fib_entry_t *fib_entry,
22                            fib_entry_delegate_type_t type,
23                            u32 *index)
24 {
25     fib_entry_delegate_t *delegate;
26     int ii;
27
28     ii = 0;
29     vec_foreach(delegate, fib_entry->fe_delegates)
30     {
31         if (delegate->fd_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
47 fib_entry_delegate_t *
48 fib_entry_delegate_get (const fib_entry_t *fib_entry,
49                         fib_entry_delegate_type_t type)
50 {
51     return (fib_entry_delegate_find_i(fib_entry, type, NULL));
52 }
53
54 void
55 fib_entry_delegate_remove (fib_entry_t *fib_entry,
56                            fib_entry_delegate_type_t type)
57 {
58     fib_entry_delegate_t *fed;
59     u32 index = ~0;
60
61     fed = fib_entry_delegate_find_i(fib_entry, type, &index);
62
63     ASSERT(NULL != fed);
64
65     vec_del1(fib_entry->fe_delegates, index);
66 }
67
68 static int
69 fib_entry_delegate_cmp_for_sort (void * v1,
70                                  void * v2)
71 {
72     fib_entry_delegate_t *delegate1 = v1, *delegate2 = v2;
73
74     return (delegate1->fd_type - delegate2->fd_type);
75 }
76
77 static void
78 fib_entry_delegate_init (fib_entry_t *fib_entry,
79                          fib_entry_delegate_type_t type)
80
81 {
82     fib_entry_delegate_t delegate = {
83         .fd_entry_index = fib_entry_get_index(fib_entry),
84         .fd_type = type,
85     };
86
87     vec_add1(fib_entry->fe_delegates, delegate);
88     vec_sort_with_function(fib_entry->fe_delegates,
89                            fib_entry_delegate_cmp_for_sort);
90 }
91
92 fib_entry_delegate_t *
93 fib_entry_delegate_find_or_add (fib_entry_t *fib_entry,
94                                 fib_entry_delegate_type_t fdt)
95 {
96     fib_entry_delegate_t *delegate;
97
98     delegate = fib_entry_delegate_get(fib_entry, fdt);
99
100     if (NULL == delegate)
101     {
102         fib_entry_delegate_init(fib_entry, fdt);
103     }
104
105     return (fib_entry_delegate_get(fib_entry, fdt));
106 }
107
108 fib_entry_delegate_type_t
109 fib_entry_chain_type_to_delegate_type (fib_forward_chain_type_t fct)
110 {
111     switch (fct)
112     {
113     case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
114         return (FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP4);
115     case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
116         return (FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP6);
117     case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
118         return (FIB_ENTRY_DELEGATE_CHAIN_MPLS_EOS);
119     case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
120         return (FIB_ENTRY_DELEGATE_CHAIN_MPLS_NON_EOS);
121     case FIB_FORW_CHAIN_TYPE_ETHERNET:
122         return (FIB_ENTRY_DELEGATE_CHAIN_ETHERNET);
123     case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
124     case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
125     case FIB_FORW_CHAIN_TYPE_BIER:
126         break;
127     case FIB_FORW_CHAIN_TYPE_NSH:
128         return (FIB_ENTRY_DELEGATE_CHAIN_NSH);
129     }
130     ASSERT(0);
131     return (FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP4);
132 }
133
134 fib_forward_chain_type_t
135 fib_entry_delegate_type_to_chain_type (fib_entry_delegate_type_t fdt)
136 {
137     switch (fdt)
138     {
139     case FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP4:
140         return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
141     case FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP6:
142         return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
143     case FIB_ENTRY_DELEGATE_CHAIN_MPLS_EOS:
144         return (FIB_FORW_CHAIN_TYPE_MPLS_EOS);
145     case FIB_ENTRY_DELEGATE_CHAIN_MPLS_NON_EOS:
146         return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
147     case FIB_ENTRY_DELEGATE_CHAIN_ETHERNET:
148         return (FIB_FORW_CHAIN_TYPE_ETHERNET);
149     case FIB_ENTRY_DELEGATE_CHAIN_NSH:
150         return (FIB_FORW_CHAIN_TYPE_NSH);
151     case FIB_ENTRY_DELEGATE_COVERED:
152     case FIB_ENTRY_DELEGATE_ATTACHED_IMPORT:
153     case FIB_ENTRY_DELEGATE_ATTACHED_EXPORT:
154     case FIB_ENTRY_DELEGATE_BFD:
155         break;
156     }
157     ASSERT(0);
158     return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
159 }
160
161 /**
162  * typedef for printing a delegate
163  */
164 typedef u8 * (*fib_entry_delegate_format_t)(const fib_entry_delegate_t *fed,
165                                             u8 *s);
166
167 /**
168  * Print a delegate that represents a forwarding chain
169  */
170 static u8 *
171 fib_entry_delegate_fmt_fwd_chain (const fib_entry_delegate_t *fed,
172                                   u8 *s)
173 {
174     s = format(s, "%U-chain\n  %U",
175                format_fib_forw_chain_type,
176                fib_entry_delegate_type_to_chain_type(fed->fd_type),
177                format_dpo_id, &fed->fd_dpo, 2);
178
179     return (s);
180 }
181
182 /**
183  * Print a delegate that represents cover tracking
184  */
185 static u8 *
186 fib_entry_delegate_fmt_covered (const fib_entry_delegate_t *fed,
187                                   u8 *s)
188 {
189     s = format(s, "covered:[");
190     s = fib_node_children_format(fed->fd_list, s);
191     s = format(s, "]");
192
193     return (s);
194 }
195
196 /**
197  * Print a delegate that represents attached-import tracking
198  */
199 static u8 *
200 fib_entry_delegate_fmt_import (const fib_entry_delegate_t *fed,
201                                u8 *s)
202 {
203     s = format(s, "import:%U", fib_ae_import_format, fed->fd_index);
204
205     return (s);
206 }
207
208 /**
209  * Print a delegate that represents attached-export tracking
210  */
211 static u8 *
212 fib_entry_delegate_fmt_export (const fib_entry_delegate_t *fed,
213                                u8 *s)
214 {
215     s = format(s, "export:%U", fib_ae_export_format, fed->fd_index);
216
217     return (s);
218 }
219
220 /**
221  * Print a delegate that represents BFD tracking
222  */
223 static u8 *
224 fib_entry_delegate_fmt_bfd (const fib_entry_delegate_t *fed,
225                                u8 *s)
226 {
227     s = format(s, "BFD:%d", fed->fd_bfd_state);
228
229     return (s);
230 }
231
232 /**
233  * A delegate type to formatter map
234  */
235 static fib_entry_delegate_format_t fed_formatters[] =
236 {
237     [FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP4] = fib_entry_delegate_fmt_fwd_chain,
238     [FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP6] = fib_entry_delegate_fmt_fwd_chain,
239     [FIB_ENTRY_DELEGATE_CHAIN_MPLS_EOS] = fib_entry_delegate_fmt_fwd_chain,
240     [FIB_ENTRY_DELEGATE_CHAIN_MPLS_NON_EOS] = fib_entry_delegate_fmt_fwd_chain,
241     [FIB_ENTRY_DELEGATE_CHAIN_ETHERNET] = fib_entry_delegate_fmt_fwd_chain,
242     [FIB_ENTRY_DELEGATE_CHAIN_NSH] = fib_entry_delegate_fmt_fwd_chain,
243     [FIB_ENTRY_DELEGATE_COVERED] = fib_entry_delegate_fmt_covered,
244     [FIB_ENTRY_DELEGATE_ATTACHED_IMPORT] = fib_entry_delegate_fmt_import,
245     [FIB_ENTRY_DELEGATE_ATTACHED_EXPORT] = fib_entry_delegate_fmt_export,
246     [FIB_ENTRY_DELEGATE_BFD] = fib_entry_delegate_fmt_bfd,
247 };
248
249 u8 *
250 format_fib_entry_deletegate (u8 * s, va_list * args)
251 {
252     fib_entry_delegate_t *fed;
253
254     fed = va_arg (*args, fib_entry_delegate_t *);
255
256     return (fed_formatters[fed->fd_type](fed, s));
257 }