IP Multicast FIB (mfib)
[vpp.git] / src / vnet / fib / fib_entry_delegate.c
1 /*
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
19 static fib_entry_delegate_t *
20 fib_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
46 fib_entry_delegate_t *
47 fib_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
53 void
54 fib_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
67 static int
68 fib_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
76 static void
77 fib_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
91 fib_entry_delegate_t *
92 fib_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
107 fib_entry_delegate_type_t
108 fib_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     case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
123     case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
124         break;
125     }
126     ASSERT(0);
127     return (FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP4);
128 }
129
130 fib_forward_chain_type_t
131 fib_entry_delegate_type_to_chain_type (fib_entry_delegate_type_t fdt)
132 {
133     switch (fdt)
134     {
135     case FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP4:
136         return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
137     case FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP6:
138         return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
139     case FIB_ENTRY_DELEGATE_CHAIN_MPLS_EOS:
140         return (FIB_FORW_CHAIN_TYPE_MPLS_EOS);
141     case FIB_ENTRY_DELEGATE_CHAIN_MPLS_NON_EOS:
142         return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
143     case FIB_ENTRY_DELEGATE_CHAIN_ETHERNET:
144         return (FIB_FORW_CHAIN_TYPE_ETHERNET);
145     case FIB_ENTRY_DELEGATE_COVERED:
146     case FIB_ENTRY_DELEGATE_ATTACHED_IMPORT:
147     case FIB_ENTRY_DELEGATE_ATTACHED_EXPORT:
148         break;
149     }
150     ASSERT(0);
151     return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
152 }