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:
126 case FIB_FORW_CHAIN_TYPE_NSH:
127 return (FIB_ENTRY_DELEGATE_CHAIN_NSH);
130 return (FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP4);
133 fib_forward_chain_type_t
134 fib_entry_delegate_type_to_chain_type (fib_entry_delegate_type_t fdt)
138 case FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP4:
139 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
140 case FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP6:
141 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
142 case FIB_ENTRY_DELEGATE_CHAIN_MPLS_EOS:
143 return (FIB_FORW_CHAIN_TYPE_MPLS_EOS);
144 case FIB_ENTRY_DELEGATE_CHAIN_MPLS_NON_EOS:
145 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
146 case FIB_ENTRY_DELEGATE_CHAIN_ETHERNET:
147 return (FIB_FORW_CHAIN_TYPE_ETHERNET);
148 case FIB_ENTRY_DELEGATE_CHAIN_NSH:
149 return (FIB_FORW_CHAIN_TYPE_NSH);
150 case FIB_ENTRY_DELEGATE_COVERED:
151 case FIB_ENTRY_DELEGATE_ATTACHED_IMPORT:
152 case FIB_ENTRY_DELEGATE_ATTACHED_EXPORT:
153 case FIB_ENTRY_DELEGATE_BFD:
157 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
161 * typedef for printing a delegate
163 typedef u8 * (*fib_entry_delegate_format_t)(const fib_entry_delegate_t *fed,
167 * Print a delegate that represents a forwarding chain
170 fib_entry_delegate_fmt_fwd_chain (const fib_entry_delegate_t *fed,
173 s = format(s, "%U-chain\n %U",
174 format_fib_forw_chain_type,
175 fib_entry_delegate_type_to_chain_type(fed->fd_type),
176 format_dpo_id, &fed->fd_dpo, 2);
182 * Print a delegate that represents cover tracking
185 fib_entry_delegate_fmt_covered (const fib_entry_delegate_t *fed,
188 s = format(s, "covered:[");
189 s = fib_node_children_format(fed->fd_list, s);
196 * Print a delegate that represents attached-import tracking
199 fib_entry_delegate_fmt_import (const fib_entry_delegate_t *fed,
202 s = format(s, "import:%U", fib_ae_import_format, fed->fd_index);
208 * Print a delegate that represents attached-export tracking
211 fib_entry_delegate_fmt_export (const fib_entry_delegate_t *fed,
214 s = format(s, "export:%U", fib_ae_export_format, fed->fd_index);
220 * Print a delegate that represents BFD tracking
223 fib_entry_delegate_fmt_bfd (const fib_entry_delegate_t *fed,
226 s = format(s, "BFD:%d", fed->fd_bfd_state);
232 * A delegate type to formatter map
234 static fib_entry_delegate_format_t fed_formatters[] =
236 [FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP4] = fib_entry_delegate_fmt_fwd_chain,
237 [FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP6] = fib_entry_delegate_fmt_fwd_chain,
238 [FIB_ENTRY_DELEGATE_CHAIN_MPLS_EOS] = fib_entry_delegate_fmt_fwd_chain,
239 [FIB_ENTRY_DELEGATE_CHAIN_MPLS_NON_EOS] = fib_entry_delegate_fmt_fwd_chain,
240 [FIB_ENTRY_DELEGATE_CHAIN_ETHERNET] = fib_entry_delegate_fmt_fwd_chain,
241 [FIB_ENTRY_DELEGATE_CHAIN_NSH] = fib_entry_delegate_fmt_fwd_chain,
242 [FIB_ENTRY_DELEGATE_COVERED] = fib_entry_delegate_fmt_covered,
243 [FIB_ENTRY_DELEGATE_ATTACHED_IMPORT] = fib_entry_delegate_fmt_import,
244 [FIB_ENTRY_DELEGATE_ATTACHED_EXPORT] = fib_entry_delegate_fmt_export,
245 [FIB_ENTRY_DELEGATE_BFD] = fib_entry_delegate_fmt_bfd,
249 format_fib_entry_deletegate (u8 * s, va_list * args)
251 fib_entry_delegate_t *fed;
253 fed = va_arg (*args, fib_entry_delegate_t *);
255 return (fed_formatters[fed->fd_type](fed, s));