blob: a0d45f970b3bb44bc3cc736ea2cab220e9a4161b [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>
18
19static fib_entry_delegate_t *
20fib_entry_delegate_find_i (const fib_entry_t *fib_entry,
21 fib_entry_delegate_type_t type,
22 u32 *index)
23{
24 fib_entry_delegate_t *delegate;
25 int ii;
26
27 ii = 0;
28 vec_foreach(delegate, fib_entry->fe_delegates)
29 {
30 if (delegate->fd_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
46fib_entry_delegate_t *
47fib_entry_delegate_get (const fib_entry_t *fib_entry,
48 fib_entry_delegate_type_t type)
49{
50 return (fib_entry_delegate_find_i(fib_entry, type, NULL));
51}
52
53void
54fib_entry_delegate_remove (fib_entry_t *fib_entry,
55 fib_entry_delegate_type_t type)
56{
57 fib_entry_delegate_t *fed;
58 u32 index = ~0;
59
60 fed = fib_entry_delegate_find_i(fib_entry, type, &index);
61
62 ASSERT(NULL != fed);
63
64 vec_del1(fib_entry->fe_delegates, index);
65}
66
67static int
68fib_entry_delegate_cmp_for_sort (void * v1,
69 void * v2)
70{
71 fib_entry_delegate_t *delegate1 = v1, *delegate2 = v2;
72
73 return (delegate1->fd_type - delegate2->fd_type);
74}
75
76static void
77fib_entry_delegate_init (fib_entry_t *fib_entry,
78 fib_entry_delegate_type_t type)
79
80{
81 fib_entry_delegate_t delegate = {
82 .fd_entry_index = fib_entry_get_index(fib_entry),
83 .fd_type = type,
84 };
85
86 vec_add1(fib_entry->fe_delegates, delegate);
87 vec_sort_with_function(fib_entry->fe_delegates,
88 fib_entry_delegate_cmp_for_sort);
89}
90
91fib_entry_delegate_t *
92fib_entry_delegate_find_or_add (fib_entry_t *fib_entry,
93 fib_entry_delegate_type_t fdt)
94{
95 fib_entry_delegate_t *delegate;
96
97 delegate = fib_entry_delegate_get(fib_entry, fdt);
98
99 if (NULL == delegate)
100 {
101 fib_entry_delegate_init(fib_entry, fdt);
102 }
103
104 return (fib_entry_delegate_get(fib_entry, fdt));
105}
106
107fib_entry_delegate_type_t
108fib_entry_chain_type_to_delegate_type (fib_forward_chain_type_t fct)
109{
110 switch (fct)
111 {
112 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
113 return (FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP4);
114 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
115 return (FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP6);
116 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
117 return (FIB_ENTRY_DELEGATE_CHAIN_MPLS_EOS);
118 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
119 return (FIB_ENTRY_DELEGATE_CHAIN_MPLS_NON_EOS);
120 case FIB_FORW_CHAIN_TYPE_ETHERNET:
121 return (FIB_ENTRY_DELEGATE_CHAIN_ETHERNET);
122 }
123 ASSERT(0);
124 return (FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP4);
125}
126
127fib_forward_chain_type_t
128fib_entry_delegate_type_to_chain_type (fib_entry_delegate_type_t fdt)
129{
130 switch (fdt)
131 {
132 case FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP4:
133 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
134 case FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP6:
135 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
136 case FIB_ENTRY_DELEGATE_CHAIN_MPLS_EOS:
137 return (FIB_FORW_CHAIN_TYPE_MPLS_EOS);
138 case FIB_ENTRY_DELEGATE_CHAIN_MPLS_NON_EOS:
139 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
140 case FIB_ENTRY_DELEGATE_CHAIN_ETHERNET:
141 return (FIB_FORW_CHAIN_TYPE_ETHERNET);
142 case FIB_ENTRY_DELEGATE_COVERED:
143 case FIB_ENTRY_DELEGATE_ATTACHED_IMPORT:
144 case FIB_ENTRY_DELEGATE_ATTACHED_EXPORT:
145 break;
146 }
147 ASSERT(0);
148 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
149}