A Protocol Independent Hierarchical FIB (VPP-352)
[vpp.git] / vnet / vnet / fib / fib_path_ext.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.h>
17 #include <vnet/dpo/mpls_label_dpo.h>
18 #include <vnet/dpo/load_balance.h>
19 #include <vnet/dpo/drop_dpo.h>
20
21 #include "fib_path_ext.h"
22 #include "fib_path.h"
23 #include "fib_path_list.h"
24 #include "fib_internal.h"
25
26 u8 *
27 format_fib_path_ext (u8 * s, va_list * args)
28 {
29     fib_path_ext_t *path_ext;
30
31     path_ext = va_arg (*args, fib_path_ext_t *);
32
33     s = format(s, "path:%d label:%U",
34                path_ext->fpe_path_index,
35                format_mpls_unicast_label,
36                path_ext->fpe_path.frp_label);
37
38     return (s);
39 }
40
41 int
42 fib_path_ext_cmp (fib_path_ext_t *path_ext,
43                   const fib_route_path_t *rpath)
44 {
45     return (fib_route_path_cmp(&path_ext->fpe_path, rpath));
46 }
47
48 static int
49 fib_path_ext_match (fib_node_index_t pl_index,
50                     fib_node_index_t path_index,
51                     void *ctx)
52 {
53     fib_path_ext_t *path_ext = ctx;
54
55     if (!fib_path_cmp_w_route_path(path_index,
56                                    &path_ext->fpe_path))
57     {
58         path_ext->fpe_path_index = path_index;
59         return (0);
60     }
61     // keep going
62     return (1);
63 }
64
65 void
66 fib_path_ext_resolve (fib_path_ext_t *path_ext,
67                       fib_node_index_t path_list_index)
68 {
69     /*
70      * Find the path on the path list that this is an extension for
71      */
72     path_ext->fpe_path_index = FIB_NODE_INDEX_INVALID;
73     fib_path_list_walk(path_list_index,
74                        fib_path_ext_match,
75                        path_ext);
76 }
77
78 void
79 fib_path_ext_init (fib_path_ext_t *path_ext,
80                    fib_node_index_t path_list_index,
81                    const fib_route_path_t *rpath)
82 {
83     path_ext->fpe_path = *rpath;
84     path_ext->fpe_path_index = FIB_NODE_INDEX_INVALID;
85
86     fib_path_ext_resolve(path_ext, path_list_index);
87 }
88
89 load_balance_path_t *
90 fib_path_ext_stack (fib_path_ext_t *path_ext,
91                     fib_forward_chain_type_t parent_fct,
92                     load_balance_path_t *nhs)
93 {
94     fib_forward_chain_type_t child_fct;
95     load_balance_path_t *nh;
96
97     if (!fib_path_is_resolved(path_ext->fpe_path_index))
98         return (nhs);
99
100     /*
101      * Since we are stacking this path-extension, it must have a valid out
102      * label. From the chain type request by the child, determine what
103      * chain type we will request from the parent.
104      */
105     switch (parent_fct)
106     {
107     case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
108         ASSERT(0);
109     case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
110     case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
111         if (MPLS_IETF_IMPLICIT_NULL_LABEL == path_ext->fpe_label)
112         {
113             /*
114              * implicit-null label for the eos or IP chain, need to pick up
115              * the IP adj
116              */
117             child_fct = parent_fct;
118         }
119         else
120         {
121             /*
122              * we have a label to stack. packets will thus be labelled when
123              * they encounter th child, ergo, non-eos.
124              */
125             child_fct = FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS;
126         }
127         break;
128     case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
129         child_fct = parent_fct;
130         break;
131     default:
132         return (nhs);
133         break;
134     }
135
136     dpo_id_t via_dpo = DPO_NULL;
137
138     /*
139      * The next object in the graph after the imposition of the label
140      * will be the DPO contributed by the path through which the packets
141      * are to be sent. We stack the MPLS Label DPO on this path DPO
142      */
143     fib_path_contribute_forwarding(path_ext->fpe_path_index,
144                                    child_fct,
145                                    &via_dpo);
146
147     if (dpo_is_drop(&via_dpo) ||
148         load_balance_is_drop(&via_dpo))
149     {
150         /*
151          * don't stack a path extension on a drop. doing so will create
152          * a LB bucket entry on drop, and we will lose a percentage of traffic.
153          */
154     }
155     else
156     {
157         vec_add2(nhs, nh, 1);
158         nh->path_weight = fib_path_get_weight(path_ext->fpe_path_index);
159         nh->path_index = path_ext->fpe_path_index;
160         dpo_copy(&nh->path_dpo, &via_dpo);
161
162         /*
163          * The label is stackable for this chain type
164          * construct the mpls header that will be imposed in the data-path
165          */
166         if (MPLS_IETF_IMPLICIT_NULL_LABEL != path_ext->fpe_label)
167         {
168             dpo_set(&nh->path_dpo,
169                     DPO_MPLS_LABEL,
170                     DPO_PROTO_MPLS,
171                     mpls_label_dpo_create(path_ext->fpe_label,
172                                           (parent_fct == FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS ?
173                                            MPLS_NON_EOS :
174                                            MPLS_EOS),
175                                           255, 0,
176                                           &nh->path_dpo));
177         }
178     }
179     dpo_reset(&via_dpo);
180
181     return (nhs);
182 }