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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/fib/fib_entry_delegate.h>
17 #include <vnet/fib/fib_entry.h>
18 #include <vnet/fib/fib_attached_export.h>
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,
25 fib_entry_delegate_t *delegate;
29 vec_foreach(delegate, fib_entry->fe_delegates)
31 if (delegate->fd_type == type)
47 fib_entry_delegate_t *
48 fib_entry_delegate_get (const fib_entry_t *fib_entry,
49 fib_entry_delegate_type_t type)
51 return (fib_entry_delegate_find_i(fib_entry, type, NULL));
55 fib_entry_delegate_remove (fib_entry_t *fib_entry,
56 fib_entry_delegate_type_t type)
58 fib_entry_delegate_t *fed;
61 fed = fib_entry_delegate_find_i(fib_entry, type, &index);
65 vec_del1(fib_entry->fe_delegates, index);
69 fib_entry_delegate_cmp_for_sort (void * v1,
72 fib_entry_delegate_t *delegate1 = v1, *delegate2 = v2;
74 return (delegate1->fd_type - delegate2->fd_type);
78 fib_entry_delegate_init (fib_entry_t *fib_entry,
79 fib_entry_delegate_type_t type)
82 fib_entry_delegate_t delegate = {
83 .fd_entry_index = fib_entry_get_index(fib_entry),
87 vec_add1(fib_entry->fe_delegates, delegate);
88 vec_sort_with_function(fib_entry->fe_delegates,
89 fib_entry_delegate_cmp_for_sort);
92 fib_entry_delegate_t *
93 fib_entry_delegate_find_or_add (fib_entry_t *fib_entry,
94 fib_entry_delegate_type_t fdt)
96 fib_entry_delegate_t *delegate;
98 delegate = fib_entry_delegate_get(fib_entry, fdt);
100 if (NULL == delegate)
102 fib_entry_delegate_init(fib_entry, fdt);
105 return (fib_entry_delegate_get(fib_entry, fdt));
108 fib_entry_delegate_type_t
109 fib_entry_chain_type_to_delegate_type (fib_forward_chain_type_t fct)
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:
127 case FIB_FORW_CHAIN_TYPE_NSH:
128 return (FIB_ENTRY_DELEGATE_CHAIN_NSH);
131 return (FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP4);
134 fib_forward_chain_type_t
135 fib_entry_delegate_type_to_chain_type (fib_entry_delegate_type_t fdt)
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:
158 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
162 * typedef for printing a delegate
164 typedef u8 * (*fib_entry_delegate_format_t)(const fib_entry_delegate_t *fed,
168 * Print a delegate that represents a forwarding chain
171 fib_entry_delegate_fmt_fwd_chain (const fib_entry_delegate_t *fed,
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);
183 * Print a delegate that represents cover tracking
186 fib_entry_delegate_fmt_covered (const fib_entry_delegate_t *fed,
189 s = format(s, "covered:[");
190 s = fib_node_children_format(fed->fd_list, s);
197 * Print a delegate that represents attached-import tracking
200 fib_entry_delegate_fmt_import (const fib_entry_delegate_t *fed,
203 s = format(s, "import:%U", fib_ae_import_format, fed->fd_index);
209 * Print a delegate that represents attached-export tracking
212 fib_entry_delegate_fmt_export (const fib_entry_delegate_t *fed,
215 s = format(s, "export:%U", fib_ae_export_format, fed->fd_index);
221 * Print a delegate that represents BFD tracking
224 fib_entry_delegate_fmt_bfd (const fib_entry_delegate_t *fed,
227 s = format(s, "BFD:%d", fed->fd_bfd_state);
233 * A delegate type to formatter map
235 static fib_entry_delegate_format_t fed_formatters[] =
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,
250 format_fib_entry_deletegate (u8 * s, va_list * args)
252 fib_entry_delegate_t *fed;
254 fed = va_arg (*args, fib_entry_delegate_t *);
256 return (fed_formatters[fed->fd_type](fed, s));