blob: f75b5626c04d12414098435f73932580f5c6171d [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
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
Neale Rannsad422ed2016-11-02 14:20:04 +000021#include <vnet/fib/fib_path_ext.h>
22#include <vnet/fib/fib_entry_src.h>
23#include <vnet/fib/fib_path.h>
24#include <vnet/fib/fib_path_list.h>
25#include <vnet/fib/fib_internal.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010026
27u8 *
28format_fib_path_ext (u8 * s, va_list * args)
29{
30 fib_path_ext_t *path_ext;
Neale Rannsad422ed2016-11-02 14:20:04 +000031 u32 ii;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010032
33 path_ext = va_arg (*args, fib_path_ext_t *);
34
Neale Rannsad422ed2016-11-02 14:20:04 +000035 s = format(s, "path:%d labels:",
36 path_ext->fpe_path_index);
37 for (ii = 0; ii < vec_len(path_ext->fpe_path.frp_label_stack); ii++)
38 {
39 s = format(s, "%U ",
40 format_mpls_unicast_label,
41 path_ext->fpe_path.frp_label_stack[ii]);
42 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +010043 return (s);
44}
45
46int
47fib_path_ext_cmp (fib_path_ext_t *path_ext,
48 const fib_route_path_t *rpath)
49{
50 return (fib_route_path_cmp(&path_ext->fpe_path, rpath));
51}
52
53static int
54fib_path_ext_match (fib_node_index_t pl_index,
55 fib_node_index_t path_index,
56 void *ctx)
57{
58 fib_path_ext_t *path_ext = ctx;
59
60 if (!fib_path_cmp_w_route_path(path_index,
61 &path_ext->fpe_path))
62 {
63 path_ext->fpe_path_index = path_index;
64 return (0);
65 }
66 // keep going
67 return (1);
68}
69
70void
71fib_path_ext_resolve (fib_path_ext_t *path_ext,
72 fib_node_index_t path_list_index)
73{
74 /*
75 * Find the path on the path list that this is an extension for
76 */
77 path_ext->fpe_path_index = FIB_NODE_INDEX_INVALID;
78 fib_path_list_walk(path_list_index,
79 fib_path_ext_match,
80 path_ext);
81}
82
83void
84fib_path_ext_init (fib_path_ext_t *path_ext,
85 fib_node_index_t path_list_index,
86 const fib_route_path_t *rpath)
87{
88 path_ext->fpe_path = *rpath;
89 path_ext->fpe_path_index = FIB_NODE_INDEX_INVALID;
90
91 fib_path_ext_resolve(path_ext, path_list_index);
92}
93
Neale Rannsad422ed2016-11-02 14:20:04 +000094/**
95 * @brief Return true if the label stack is implicit null
96 */
97static int
98fib_path_ext_is_imp_null (fib_path_ext_t *path_ext)
99{
100 return ((1 == vec_len(path_ext->fpe_label_stack)) &&
101 (MPLS_IETF_IMPLICIT_NULL_LABEL == path_ext->fpe_label_stack[0]));
102}
103
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100104load_balance_path_t *
105fib_path_ext_stack (fib_path_ext_t *path_ext,
Neale Rannsad422ed2016-11-02 14:20:04 +0000106 const fib_entry_t *entry,
107 fib_forward_chain_type_t child_fct,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100108 load_balance_path_t *nhs)
109{
Neale Rannsad422ed2016-11-02 14:20:04 +0000110 fib_forward_chain_type_t parent_fct;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100111 load_balance_path_t *nh;
112
113 if (!fib_path_is_resolved(path_ext->fpe_path_index))
114 return (nhs);
115
116 /*
117 * Since we are stacking this path-extension, it must have a valid out
118 * label. From the chain type request by the child, determine what
119 * chain type we will request from the parent.
120 */
Neale Rannsad422ed2016-11-02 14:20:04 +0000121 switch (child_fct)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100122 {
123 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
Neale Rannsad422ed2016-11-02 14:20:04 +0000124 {
125 /*
126 * The EOS chain is a tricky since, when the path has an imp NULL one cannot know
127 * the adjacency to link to without knowing what the packets payload protocol
128 * will be once the label is popped.
129 */
130 if (fib_path_ext_is_imp_null(path_ext))
131 {
132 parent_fct = fib_entry_chain_type_fixup(entry, child_fct);
133 }
134 else
135 {
136 /*
137 * we have a label to stack. packets will thus be labelled when
138 * they encounter the child, ergo, non-eos.
139 */
140 parent_fct = FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS;
141 }
Neale Ranns5899fde2016-10-12 13:51:05 +0100142 break;
Neale Rannsad422ed2016-11-02 14:20:04 +0000143 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100144 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
145 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
Neale Rannsad422ed2016-11-02 14:20:04 +0000146 if (fib_path_ext_is_imp_null(path_ext))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100147 {
148 /*
149 * implicit-null label for the eos or IP chain, need to pick up
150 * the IP adj
151 */
Neale Rannsad422ed2016-11-02 14:20:04 +0000152 parent_fct = child_fct;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100153 }
154 else
155 {
156 /*
157 * we have a label to stack. packets will thus be labelled when
Neale Rannsad422ed2016-11-02 14:20:04 +0000158 * they encounter the child, ergo, non-eos.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100159 */
Neale Rannsad422ed2016-11-02 14:20:04 +0000160 parent_fct = FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100161 }
162 break;
163 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
Neale Rannsad422ed2016-11-02 14:20:04 +0000164 parent_fct = child_fct;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100165 break;
166 default:
167 return (nhs);
168 break;
169 }
170
Neale Ranns948e00f2016-10-20 13:39:34 +0100171 dpo_id_t via_dpo = DPO_INVALID;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100172
173 /*
174 * The next object in the graph after the imposition of the label
175 * will be the DPO contributed by the path through which the packets
176 * are to be sent. We stack the MPLS Label DPO on this path DPO
177 */
178 fib_path_contribute_forwarding(path_ext->fpe_path_index,
Neale Rannsad422ed2016-11-02 14:20:04 +0000179 parent_fct,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100180 &via_dpo);
181
182 if (dpo_is_drop(&via_dpo) ||
183 load_balance_is_drop(&via_dpo))
184 {
185 /*
186 * don't stack a path extension on a drop. doing so will create
187 * a LB bucket entry on drop, and we will lose a percentage of traffic.
188 */
189 }
190 else
191 {
192 vec_add2(nhs, nh, 1);
193 nh->path_weight = fib_path_get_weight(path_ext->fpe_path_index);
194 nh->path_index = path_ext->fpe_path_index;
195 dpo_copy(&nh->path_dpo, &via_dpo);
196
197 /*
198 * The label is stackable for this chain type
199 * construct the mpls header that will be imposed in the data-path
200 */
Neale Rannsad422ed2016-11-02 14:20:04 +0000201 if (!fib_path_ext_is_imp_null(path_ext))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100202 {
Neale Rannsad422ed2016-11-02 14:20:04 +0000203 /*
204 * we use the parent protocol for the label so that
205 * we pickup the correct MPLS imposition nodes to do
206 * ip[46] processing.
207 */
208 dpo_proto_t chain_proto;
209 mpls_eos_bit_t eos;
210 index_t mldi;
211
212 eos = (child_fct == FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS ?
213 MPLS_NON_EOS :
214 MPLS_EOS);
215 chain_proto = fib_forw_chain_type_to_dpo_proto(child_fct);
216
217 mldi = mpls_label_dpo_create(path_ext->fpe_label_stack,
218 eos, 255, 0,
219 chain_proto,
220 &nh->path_dpo);
221
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100222 dpo_set(&nh->path_dpo,
223 DPO_MPLS_LABEL,
Neale Rannsad422ed2016-11-02 14:20:04 +0000224 chain_proto,
225 mldi);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100226 }
227 }
228 dpo_reset(&via_dpo);
229
230 return (nhs);
231}