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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/mfib/mfib_itf.h>
17 #include <vnet/mfib/mfib_entry.h>
18 #include <vnet/dpo/replicate_dpo.h>
19 #include <vnet/mfib/ip4_mfib.h>
20 #include <vnet/mfib/ip6_mfib.h>
21 #include <vnet/mfib/mfib_signal.h>
22 #include <vnet/fib/ip4_fib.h>
23 #include <vnet/fib/ip6_fib.h>
25 #include <vnet/ip/ip4.h>
26 #include <vnet/vnet.h>
28 typedef struct mfib_forward_lookup_trace_t_ {
31 } mfib_forward_lookup_trace_t;
34 format_mfib_forward_lookup_trace (u8 * s, va_list * args)
36 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
37 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
38 mfib_forward_lookup_trace_t * t = va_arg (*args, mfib_forward_lookup_trace_t *);
40 s = format (s, "fib %d entry %d", t->fib_index, t->entry_index);
44 /* Common trace function for all ip4-forward next nodes. */
46 mfib_forward_lookup_trace (vlib_main_t * vm,
47 vlib_node_runtime_t * node,
51 ip4_main_t * im = &ip4_main;
53 n_left = frame->n_vectors;
54 from = vlib_frame_vector_args (frame);
58 mfib_forward_lookup_trace_t * t0, * t1;
59 vlib_buffer_t * b0, * b1;
62 /* Prefetch next iteration. */
63 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
64 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
69 b0 = vlib_get_buffer (vm, bi0);
70 b1 = vlib_get_buffer (vm, bi1);
72 if (b0->flags & VLIB_BUFFER_IS_TRACED)
74 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
75 t0->entry_index = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
76 t0->fib_index = vec_elt (im->mfib_index_by_sw_if_index,
77 vnet_buffer(b1)->sw_if_index[VLIB_RX]);
79 if (b1->flags & VLIB_BUFFER_IS_TRACED)
81 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
82 t1->entry_index = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
83 t1->fib_index = vec_elt (im->mfib_index_by_sw_if_index,
84 vnet_buffer(b1)->sw_if_index[VLIB_RX]);
92 mfib_forward_lookup_trace_t * t0;
98 b0 = vlib_get_buffer (vm, bi0);
100 if (b0->flags & VLIB_BUFFER_IS_TRACED)
102 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
103 t0->entry_index = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
104 t0->fib_index = vec_elt (im->mfib_index_by_sw_if_index,
105 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
112 typedef enum mfib_forward_lookup_next_t_ {
113 MFIB_FORWARD_LOOKUP_NEXT_RPF,
114 MFIB_FORWARD_LOOKUP_N_NEXT,
115 } mfib_forward_lookup_next_t;
118 mfib_forward_lookup (vlib_main_t * vm,
119 vlib_node_runtime_t * node,
120 vlib_frame_t * frame,
123 u32 n_left_from, n_left_to_next, * from, * to_next;
125 from = vlib_frame_vector_args (frame);
126 n_left_from = frame->n_vectors;
128 while (n_left_from > 0)
130 vlib_get_next_frame (vm, node, MFIB_FORWARD_LOOKUP_NEXT_RPF,
131 to_next, n_left_to_next);
133 while (n_left_from > 0 && n_left_to_next > 0)
135 fib_node_index_t mfei0;
147 p0 = vlib_get_buffer (vm, pi0);
153 fib_index0 = vec_elt (ip4_main.mfib_index_by_sw_if_index,
154 vnet_buffer(p0)->sw_if_index[VLIB_RX]);
155 ip0 = vlib_buffer_get_current (p0);
156 mfei0 = ip4_mfib_table_lookup(ip4_mfib_get(fib_index0),
165 fib_index0 = vec_elt (ip6_main.mfib_index_by_sw_if_index,
166 vnet_buffer(p0)->sw_if_index[VLIB_RX]);
167 ip0 = vlib_buffer_get_current (p0);
168 mfei0 = ip6_mfib_table_fwd_lookup(ip6_mfib_get(fib_index0),
173 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = mfei0;
176 vlib_put_next_frame(vm, node,
177 MFIB_FORWARD_LOOKUP_NEXT_RPF,
181 if (node->flags & VLIB_NODE_FLAG_TRACE)
182 mfib_forward_lookup_trace(vm, node, frame);
184 return frame->n_vectors;
188 ip4_mfib_forward_lookup (vlib_main_t * vm,
189 vlib_node_runtime_t * node,
190 vlib_frame_t * frame)
192 return (mfib_forward_lookup (vm, node, frame, 1));
195 VLIB_REGISTER_NODE (ip4_mfib_forward_lookup_node, static) = {
196 .function = ip4_mfib_forward_lookup,
197 .name = "ip4-mfib-forward-lookup",
198 .vector_size = sizeof (u32),
200 .format_trace = format_mfib_forward_lookup_trace,
202 .n_next_nodes = MFIB_FORWARD_LOOKUP_N_NEXT,
204 [MFIB_FORWARD_LOOKUP_NEXT_RPF] = "ip4-mfib-forward-rpf",
208 VLIB_NODE_FUNCTION_MULTIARCH (ip4_mfib_forward_lookup_node,
209 ip4_mfib_forward_lookup)
212 ip6_mfib_forward_lookup (vlib_main_t * vm,
213 vlib_node_runtime_t * node,
214 vlib_frame_t * frame)
216 return (mfib_forward_lookup (vm, node, frame, 0));
219 VLIB_REGISTER_NODE (ip6_mfib_forward_lookup_node, static) = {
220 .function = ip6_mfib_forward_lookup,
221 .name = "ip6-mfib-forward-lookup",
222 .vector_size = sizeof (u32),
224 .format_trace = format_mfib_forward_lookup_trace,
226 .n_next_nodes = MFIB_FORWARD_LOOKUP_N_NEXT,
228 [MFIB_FORWARD_LOOKUP_NEXT_RPF] = "ip6-mfib-forward-rpf",
232 VLIB_NODE_FUNCTION_MULTIARCH (ip6_mfib_forward_lookup_node,
233 ip6_mfib_forward_lookup)
236 typedef struct mfib_forward_rpf_trace_t_ {
239 mfib_itf_flags_t itf_flags;
240 } mfib_forward_rpf_trace_t;
242 typedef enum mfib_forward_rpf_next_t_ {
243 MFIB_FORWARD_RPF_NEXT_DROP,
244 MFIB_FORWARD_RPF_N_NEXT,
245 } mfib_forward_rpf_next_t;
248 format_mfib_forward_rpf_trace (u8 * s, va_list * args)
250 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
251 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
252 mfib_forward_rpf_trace_t * t = va_arg (*args, mfib_forward_rpf_trace_t *);
254 s = format (s, "entry %d", t->entry_index);
255 s = format (s, " itf %d", t->sw_if_index);
256 s = format (s, " flags %U", format_mfib_itf_flags, t->itf_flags);
262 mfib_forward_connected_check (vlib_buffer_t * b0,
267 * Lookup the source of the IP packet in the
268 * FIB. return true if the entry is attached.
277 ip0 = vlib_buffer_get_current(b0);
279 lbi0 = ip4_fib_forwarding_lookup(
280 ip4_fib_table_get_index_for_sw_if_index(
283 lb0 = load_balance_get(lbi0);
285 return (FIB_ENTRY_FLAG_ATTACHED &
286 lb0->lb_fib_entry_flags);
296 mfib_forward_itf_signal (vlib_main_t *vm,
297 const mfib_entry_t *mfe,
301 mfib_itf_flags_t old_flags;
303 old_flags = clib_atomic_fetch_or(&mfi->mfi_flags,
304 MFIB_ITF_FLAG_SIGNAL_PRESENT);
306 if (!(old_flags & MFIB_ITF_FLAG_SIGNAL_PRESENT))
309 * we were the lucky ones to set the signal present flag
311 if (!(old_flags & MFIB_ITF_FLAG_DONT_PRESERVE))
314 * preserve a copy of the packet for the control
316 * Only allow one preserved packet at at time, since
317 * when the signal present flag is cleared so is the
320 mfib_signal_push(mfe, mfi, b0);
325 * The control plane just wants the signal, not the packet as well
327 mfib_signal_push(mfe, mfi, NULL);
332 * there is already a signal present on this interface that the
333 * control plane has not yet acknowledged
338 mfib_forward_rpf (vlib_main_t * vm,
339 vlib_node_runtime_t * node,
340 vlib_frame_t * frame,
343 u32 n_left_from, n_left_to_next, * from, * to_next;
344 mfib_forward_rpf_next_t next;
345 vlib_node_runtime_t *error_node;
348 error_node = vlib_node_get_runtime (vm, ip4_input_node.index);
350 error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
351 from = vlib_frame_vector_args (frame);
352 n_left_from = frame->n_vectors;
353 next = MFIB_FORWARD_RPF_NEXT_DROP;
355 while (n_left_from > 0)
357 vlib_get_next_frame (vm, node, next,
358 to_next, n_left_to_next);
360 while (n_left_from > 0 && n_left_to_next > 0)
362 fib_node_index_t mfei0;
363 const mfib_entry_t *mfe0;
367 mfib_itf_flags_t iflags0;
368 mfib_entry_flags_t eflags0;
378 error0 = IP4_ERROR_NONE;
379 b0 = vlib_get_buffer (vm, pi0);
380 mfei0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
381 mfe0 = mfib_entry_get(mfei0);
382 mfi0 = mfib_entry_get_itf(mfe0,
383 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
386 * throughout this function we are 'PREDICT' optimising
387 * for the case of throughput traffic that is not replicated
388 * to the host stack nor sets local flags
392 * If the mfib entry has a configured RPF-ID check that
393 * in preference to an interface based RPF
395 if (MFIB_RPF_ID_NONE != mfe0->mfe_rpf_id)
397 iflags0 = (mfe0->mfe_rpf_id == vnet_buffer(b0)->ip.rpf_id ?
398 MFIB_ITF_FLAG_ACCEPT :
403 if (PREDICT_TRUE(NULL != mfi0))
405 iflags0 = mfi0->mfi_flags;
409 iflags0 = MFIB_ITF_FLAG_NONE;
412 eflags0 = mfe0->mfe_flags;
414 if (PREDICT_FALSE(eflags0 & MFIB_ENTRY_FLAG_CONNECTED))
417 * lookup the source in the unicast FIB - check it
418 * matches a connected.
420 if (mfib_forward_connected_check(
422 vnet_buffer(b0)->sw_if_index[VLIB_RX],
425 mfib_forward_itf_signal(vm, mfe0, mfi0, b0);
428 if (PREDICT_FALSE((eflags0 & MFIB_ENTRY_FLAG_SIGNAL) ^
429 (iflags0 & MFIB_ITF_FLAG_NEGATE_SIGNAL)))
432 * Entry signal XOR interface negate-signal
436 mfib_forward_itf_signal(vm, mfe0, mfi0, b0);
440 if (PREDICT_TRUE((iflags0 & MFIB_ITF_FLAG_ACCEPT) ||
441 (eflags0 & MFIB_ENTRY_FLAG_ACCEPT_ALL_ITF)))
444 * This interface is accepting packets for the matching entry
446 next0 = mfe0->mfe_rep.dpoi_next_node;
448 vnet_buffer(b0)->ip.adj_index[VLIB_TX] =
449 mfe0->mfe_rep.dpoi_index;
453 next0 = MFIB_FORWARD_RPF_NEXT_DROP;
454 error0 = IP4_ERROR_RPF_FAILURE;
457 b0->error = error0 ? error_node->errors[error0] : 0;
459 if (b0->flags & VLIB_BUFFER_IS_TRACED)
461 mfib_forward_rpf_trace_t *t0;
463 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
464 t0->entry_index = mfei0;
465 t0->itf_flags = iflags0;
468 t0->sw_if_index = ~0;
472 t0->sw_if_index = mfi0->mfi_sw_if_index;
475 vlib_validate_buffer_enqueue_x1 (vm, node, next,
476 to_next, n_left_to_next,
480 vlib_put_next_frame(vm, node, next, n_left_to_next);
483 return frame->n_vectors;
487 ip4_mfib_forward_rpf (vlib_main_t * vm,
488 vlib_node_runtime_t * node,
489 vlib_frame_t * frame)
491 return (mfib_forward_rpf(vm, node, frame, 1));
495 VLIB_REGISTER_NODE (ip4_mfib_forward_rpf_node, static) = {
496 .function = ip4_mfib_forward_rpf,
497 .name = "ip4-mfib-forward-rpf",
498 .vector_size = sizeof (u32),
500 .format_trace = format_mfib_forward_rpf_trace,
502 .n_next_nodes = MFIB_FORWARD_RPF_N_NEXT,
504 [MFIB_FORWARD_RPF_NEXT_DROP] = "ip4-drop",
508 VLIB_NODE_FUNCTION_MULTIARCH (ip4_mfib_forward_rpf_node,
509 ip4_mfib_forward_rpf)
512 ip6_mfib_forward_rpf (vlib_main_t * vm,
513 vlib_node_runtime_t * node,
514 vlib_frame_t * frame)
516 return (mfib_forward_rpf(vm, node, frame, 1));
520 VLIB_REGISTER_NODE (ip6_mfib_forward_rpf_node, static) = {
521 .function = ip6_mfib_forward_rpf,
522 .name = "ip6-mfib-forward-rpf",
523 .vector_size = sizeof (u32),
525 .format_trace = format_mfib_forward_rpf_trace,
527 .n_next_nodes = MFIB_FORWARD_RPF_N_NEXT,
529 [MFIB_FORWARD_RPF_NEXT_DROP] = "ip6-drop",
533 VLIB_NODE_FUNCTION_MULTIARCH (ip6_mfib_forward_rpf_node,
534 ip6_mfib_forward_rpf)