A Protocol Independent Hierarchical FIB (VPP-352)
[vpp.git] / vnet / vnet / fib / fib_entry_src_mpls.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/mpls/mpls_types.h>
17 #include <vnet/dpo/drop_dpo.h>
18
19 #include <vnet/fib/fib_table.h>
20 #include <vnet/fib/fib_entry.h>
21 #include <vnet/fib/fib_entry_src.h>
22 #include <vnet/fib/mpls_fib.h>
23
24 /**
25  * Source initialisation Function 
26  */
27 static void
28 fib_entry_src_mpls_init (fib_entry_src_t *src)
29 {
30     mpls_eos_bit_t eos;
31
32     src->fes_flags = FIB_ENTRY_FLAG_NONE;
33     src->mpls.fesm_label = MPLS_LABEL_INVALID;
34
35     FOR_EACH_MPLS_EOS_BIT(eos)
36     {
37         src->mpls.fesm_lfes[eos] = FIB_NODE_INDEX_INVALID;
38     }
39 }
40
41 /**
42  * Source deinitialisation Function 
43  */
44 static void
45 fib_entry_src_mpls_deinit (fib_entry_src_t *src)
46 {
47 }
48
49 static void
50 fib_entry_src_mpls_remove (fib_entry_src_t *src)
51 {
52     src->fes_pl = FIB_NODE_INDEX_INVALID;
53     src->mpls.fesm_label = MPLS_LABEL_INVALID;
54 }
55
56 static void
57 fib_entry_src_mpls_add (fib_entry_src_t *src,
58                         const fib_entry_t *entry,
59                         fib_entry_flag_t flags,
60                         fib_protocol_t proto,
61                         const dpo_id_t *dpo)
62 {
63     src->fes_pl =
64         fib_path_list_create_special(proto,
65                                      FIB_PATH_LIST_FLAG_DROP,
66                                      drop_dpo_get(fib_proto_to_dpo(proto)));
67 }
68
69 static void
70 fib_entry_src_mpls_fwd_update (fib_entry_src_t *src,
71                                const fib_entry_t *fib_entry,
72                                fib_source_t best_source)
73 {
74     dpo_id_t dpo = DPO_NULL;
75     mpls_eos_bit_t eos;
76
77     FOR_EACH_MPLS_EOS_BIT(eos)
78     {
79         fib_entry_contribute_forwarding(fib_entry_get_index(fib_entry),
80                                         (eos ?
81                                          FIB_FORW_CHAIN_TYPE_MPLS_EOS :
82                                          FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS),
83                                         &dpo);
84
85         fib_table_entry_special_dpo_update(src->mpls.fesm_lfes[eos],
86                                            FIB_SOURCE_SPECIAL,
87                                            FIB_ENTRY_FLAG_EXCLUSIVE,
88                                            &dpo);
89     }
90     dpo_reset(&dpo);
91 }
92
93 static void
94 fib_entry_src_mpls_set_data (fib_entry_src_t *src,
95                              const fib_entry_t *entry,
96                              const void *data)
97 {
98     dpo_proto_t payload_proto;
99     fib_node_index_t fei;
100     mpls_label_t label;
101     mpls_eos_bit_t eos;
102
103     /*
104      * post MPLS table alloc and the possible rea-alloc of fib entrys
105      * the entry pointer will no longer be valid. so save its index
106      */
107     payload_proto = entry->fe_prefix.fp_proto;
108     fei = fib_entry_get_index(entry);
109     label = *(mpls_label_t*)data;
110
111     if (MPLS_LABEL_INVALID == label)
112     {
113         /*
114          * removing the local label
115          */
116         FOR_EACH_MPLS_EOS_BIT(eos)
117         {
118             fib_table_entry_delete_index(src->mpls.fesm_lfes[eos],
119                                          FIB_SOURCE_SPECIAL);
120         }
121         fib_table_unlock(MPLS_FIB_DEFAULT_TABLE_ID, FIB_PROTOCOL_MPLS);
122         src->mpls.fesm_label = label;
123     }
124     else
125     {
126         fib_prefix_t prefix = {
127             .fp_proto = FIB_PROTOCOL_MPLS,
128             .fp_label = label,
129         };
130         fib_node_index_t fib_index;
131         dpo_id_t dpo = DPO_NULL;
132
133         /*
134          * adding a new local label. make sure the MPLS fib exists.
135          */
136         if (MPLS_LABEL_INVALID == src->mpls.fesm_label)
137         {
138             fib_index =
139                 fib_table_find_or_create_and_lock(FIB_PROTOCOL_MPLS,
140                                                   MPLS_FIB_DEFAULT_TABLE_ID);
141         }
142         else
143         {
144             fib_index = mpls_fib_index_from_table_id(MPLS_FIB_DEFAULT_TABLE_ID);
145         }
146
147         src->mpls.fesm_label = label;
148
149         FOR_EACH_MPLS_EOS_BIT(eos)
150         {
151             prefix.fp_eos = eos;
152             prefix.fp_payload_proto = fib_proto_to_dpo(payload_proto);
153
154             fib_entry_contribute_forwarding(fei,
155                                             (eos ?
156                                              FIB_FORW_CHAIN_TYPE_MPLS_EOS :
157                                              FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS),
158                                             &dpo);
159             src->mpls.fesm_lfes[eos] = 
160                 fib_table_entry_special_dpo_add(fib_index,
161                                                 &prefix,
162                                                 FIB_SOURCE_SPECIAL,
163                                                 FIB_ENTRY_FLAG_EXCLUSIVE,
164                                                 &dpo);
165             dpo_reset(&dpo);
166         }
167     }
168 }
169
170 static const void *
171 fib_entry_src_mpls_get_data (fib_entry_src_t *src,
172                              const fib_entry_t *entry)
173 {
174     return (&(src->mpls.fesm_label));
175 }
176
177 static u8*
178 fib_entry_src_mpls_format (fib_entry_src_t *src,
179                            u8* s)
180 {
181     return (format(s, "MPLS local-label:%d", src->mpls.fesm_label));
182 }
183
184 const static fib_entry_src_vft_t mpls_src_vft = {
185     .fesv_init = fib_entry_src_mpls_init,
186     .fesv_deinit = fib_entry_src_mpls_deinit,
187     .fesv_add = fib_entry_src_mpls_add,
188     .fesv_remove = fib_entry_src_mpls_remove,
189     .fesv_format = fib_entry_src_mpls_format,
190     .fesv_fwd_update = fib_entry_src_mpls_fwd_update,
191     .fesv_set_data = fib_entry_src_mpls_set_data,
192     .fesv_get_data = fib_entry_src_mpls_get_data,
193 };
194
195 void
196 fib_entry_src_mpls_register (void)
197 {
198     fib_entry_src_register(FIB_SOURCE_MPLS, &mpls_src_vft);    
199 }
200
201