blob: 4bf37df0a7eefe032a9b5f14e382d1dc119f4683 [file] [log] [blame]
Neale Rannsad422ed2016-11-02 14:20:04 +00001/*
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>
Neale Ranns88fc83e2017-04-05 08:11:14 -070018#include <vnet/fib/fib_attached_export.h>
Neale Rannsad422ed2016-11-02 14:20:04 +000019
20static fib_entry_delegate_t *
21fib_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
47fib_entry_delegate_t *
48fib_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
54void
55fib_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
68static int
69fib_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
77static void
78fib_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
92fib_entry_delegate_t *
93fib_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
108fib_entry_delegate_type_t
109fib_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);
Neale Ranns32e1c012016-11-22 17:07:28 +0000123 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
124 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
Neale Rannsd792d9c2017-10-21 10:53:20 -0700125 case FIB_FORW_CHAIN_TYPE_BIER:
Neale Ranns32e1c012016-11-22 17:07:28 +0000126 break;
Florin Corasce1b4c72017-01-26 14:25:34 -0800127 case FIB_FORW_CHAIN_TYPE_NSH:
128 return (FIB_ENTRY_DELEGATE_CHAIN_NSH);
Neale Rannsad422ed2016-11-02 14:20:04 +0000129 }
130 ASSERT(0);
131 return (FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP4);
132}
133
134fib_forward_chain_type_t
135fib_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);
Florin Corasce1b4c72017-01-26 14:25:34 -0800149 case FIB_ENTRY_DELEGATE_CHAIN_NSH:
150 return (FIB_FORW_CHAIN_TYPE_NSH);
Neale Rannsad422ed2016-11-02 14:20:04 +0000151 case FIB_ENTRY_DELEGATE_COVERED:
152 case FIB_ENTRY_DELEGATE_ATTACHED_IMPORT:
153 case FIB_ENTRY_DELEGATE_ATTACHED_EXPORT:
Neale Ranns88fc83e2017-04-05 08:11:14 -0700154 case FIB_ENTRY_DELEGATE_BFD:
Neale Rannsad422ed2016-11-02 14:20:04 +0000155 break;
156 }
157 ASSERT(0);
158 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
159}
Neale Ranns88fc83e2017-04-05 08:11:14 -0700160
161/**
162 * typedef for printing a delegate
163 */
164typedef 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 */
170static u8 *
171fib_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 */
185static u8 *
186fib_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 */
199static u8 *
200fib_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 */
211static u8 *
212fib_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 */
223static u8 *
224fib_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 */
235static 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
249u8 *
250format_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}