blob: 95915cb3807a642821e128a4f88be53d7b7a3af1 [file] [log] [blame]
Neale Ranns9e829a82018-12-17 05:50:32 -08001/*
2 * Copyright (c) 2018 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/mfib/mfib_entry_delegate.h>
17#include <vnet/mfib/mfib_entry.h>
18
19static mfib_entry_delegate_t *
20mfib_entry_delegate_find_i (const mfib_entry_t *mfib_entry,
21 mfib_entry_delegate_type_t type,
22 u32 *index)
23{
24 mfib_entry_delegate_t *delegate;
25 int ii;
26
27 ii = 0;
28 vec_foreach(delegate, mfib_entry->fe_delegates)
29 {
30 if (delegate->mfd_type == type)
31 {
32 if (NULL != index)
33 *index = ii;
34
35 return (delegate);
36 }
37 else
38 {
39 ii++;
40 }
41 }
42
43 return (NULL);
44}
45
46mfib_entry_delegate_t *
47mfib_entry_delegate_get (const mfib_entry_t *mfib_entry,
48 mfib_entry_delegate_type_t type)
49{
50 return (mfib_entry_delegate_find_i(mfib_entry, type, NULL));
51}
52
53void
54mfib_entry_delegate_remove (mfib_entry_t *mfib_entry,
55 mfib_entry_delegate_type_t type)
56{
57 mfib_entry_delegate_t *fed;
58 u32 index = ~0;
59
60 fed = mfib_entry_delegate_find_i(mfib_entry, type, &index);
61
62 ASSERT(NULL != fed);
63
64 vec_del1(mfib_entry->fe_delegates, index);
65}
66
67static int
68mfib_entry_delegate_cmp_for_sort (void * v1,
69 void * v2)
70{
71 mfib_entry_delegate_t *delegate1 = v1, *delegate2 = v2;
72
73 return (delegate1->mfd_type - delegate2->mfd_type);
74}
75
76static void
77mfib_entry_delegate_init (mfib_entry_t *mfib_entry,
78 mfib_entry_delegate_type_t type)
79
80{
81 mfib_entry_delegate_t delegate = {
82 .mfd_entry_index = mfib_entry_get_index(mfib_entry),
83 .mfd_type = type,
84 };
85
86 vec_add1(mfib_entry->fe_delegates, delegate);
87 vec_sort_with_function(mfib_entry->fe_delegates,
88 mfib_entry_delegate_cmp_for_sort);
89}
90
91mfib_entry_delegate_t *
92mfib_entry_delegate_find_or_add (mfib_entry_t *mfib_entry,
93 mfib_entry_delegate_type_t fdt)
94{
95 mfib_entry_delegate_t *delegate;
96
97 delegate = mfib_entry_delegate_get(mfib_entry, fdt);
98
99 if (NULL == delegate)
100 {
101 mfib_entry_delegate_init(mfib_entry, fdt);
102 }
103
104 return (mfib_entry_delegate_get(mfib_entry, fdt));
105}
106
107/**
108 * typedef for printing a delegate
109 */
110typedef u8 * (*mfib_entry_delegate_format_t)(const mfib_entry_delegate_t *fed,
111 u8 *s);
112
113/**
114 * Print a delegate that represents cover tracking
115 */
116static u8 *
117mfib_entry_delegate_fmt_covered (const mfib_entry_delegate_t *fed,
118 u8 *s)
119{
120 s = format(s, "covered:[");
121 s = fib_node_children_format(fed->mfd_list, s);
122 s = format(s, "]");
123
124 return (s);
125}
126
127/**
128 * A delegate type to formatter map
129 */
130static mfib_entry_delegate_format_t fed_formatters[] =
131{
132 [MFIB_ENTRY_DELEGATE_COVERED] = mfib_entry_delegate_fmt_covered,
133};
134
135u8 *
136format_mfib_entry_deletegate (u8 * s, va_list * args)
137{
138 mfib_entry_delegate_t *fed;
139
140 fed = va_arg (*args, mfib_entry_delegate_t *);
141
142 return (fed_formatters[fed->mfd_type](fed, s));
143}