blob: 37fa1aead12fb076fbd95482ede2a6172a34c617 [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
Neale Ranns1357f3b2016-10-16 12:01:42 -07002 * node.c: MPLS input
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003 *
4 * Copyright (c) 2012-2014 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <vlib/vlib.h>
19#include <vnet/pg/pg.h>
20#include <vnet/mpls/mpls.h>
Damjan Marion22311502016-10-28 20:30:15 +020021#include <vnet/feature/feature.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010022
23typedef struct {
24 u32 next_index;
Neale Ranns696e88d2017-03-16 07:34:55 -040025 u32 label_net_byte_order;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010026} mpls_input_trace_t;
27
Neale Ranns696e88d2017-03-16 07:34:55 -040028#define foreach_mpls_input_next \
29_(DROP, "error-drop") \
30_(LOOKUP, "mpls-lookup")
31
32typedef enum {
33#define _(s,n) MPLS_INPUT_NEXT_##s,
34 foreach_mpls_input_next
35#undef _
36 MPLS_INPUT_N_NEXT,
37} mpls_input_next_t;
38
Neale Ranns0bfe5d82016-08-25 15:29:12 +010039static u8 *
40format_mpls_input_trace (u8 * s, va_list * args)
41{
42 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
43 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
44 mpls_input_trace_t * t = va_arg (*args, mpls_input_trace_t *);
45 char * next_name;
Neale Ranns696e88d2017-03-16 07:34:55 -040046 u32 label;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010047 next_name = "BUG!";
Neale Ranns696e88d2017-03-16 07:34:55 -040048 label = clib_net_to_host_u32(t->label_net_byte_order);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010049
50#define _(a,b) if (t->next_index == MPLS_INPUT_NEXT_##a) next_name = b;
51 foreach_mpls_input_next;
52#undef _
Dave Barachf8d50682019-05-14 18:01:44 -040053
54 s = format (s, "MPLS: next %s[%d] label %d ttl %d exp %d",
Neale Ranns0bfe5d82016-08-25 15:29:12 +010055 next_name, t->next_index,
Neale Ranns696e88d2017-03-16 07:34:55 -040056 vnet_mpls_uc_get_label(label),
Neale Ranns31ed7442018-02-23 05:29:09 -080057 vnet_mpls_uc_get_ttl(label),
58 vnet_mpls_uc_get_exp(label));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010059
60 return s;
61}
62
Neale Ranns0bfe5d82016-08-25 15:29:12 +010063typedef struct {
64 u32 last_label;
65 u32 last_inner_fib_index;
66 u32 last_outer_fib_index;
67 mpls_main_t * mpls_main;
68} mpls_input_runtime_t;
69
70static inline uword
71mpls_input_inline (vlib_main_t * vm,
72 vlib_node_runtime_t * node,
73 vlib_frame_t * from_frame)
74{
75 u32 n_left_from, next_index, * from, * to_next;
Neale Ranns31ed7442018-02-23 05:29:09 -080076 mpls_main_t * mm = &mpls_main;
Damjan Marion586afd72017-04-05 19:18:20 +020077 u32 thread_index = vlib_get_thread_index();
Neale Ranns0bfe5d82016-08-25 15:29:12 +010078 vlib_simple_counter_main_t * cm;
79 vnet_main_t * vnm = vnet_get_main();
80
81 from = vlib_frame_vector_args (from_frame);
82 n_left_from = from_frame->n_vectors;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010083
84 next_index = node->cached_next_index;
85
86 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
87 VNET_INTERFACE_COUNTER_MPLS);
88
89 while (n_left_from > 0)
90 {
91 u32 n_left_to_next;
92
93 vlib_get_next_frame (vm, node, next_index,
Neale Ranns696e88d2017-03-16 07:34:55 -040094 to_next, n_left_to_next);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010095
Neale Ranns9ca18c62016-12-10 21:08:09 +000096 while (n_left_from >= 4 && n_left_to_next >= 2)
97 {
Neale Ranns696e88d2017-03-16 07:34:55 -040098 u32 bi0, next0, sw_if_index0;
99 u32 bi1, next1, sw_if_index1;
Neale Ranns9ca18c62016-12-10 21:08:09 +0000100 vlib_buffer_t *b0, *b1;
Neale Ranns696e88d2017-03-16 07:34:55 -0400101 char *h0, *h1;
Neale Ranns9ca18c62016-12-10 21:08:09 +0000102
103 /* Prefetch next iteration. */
104 {
Neale Ranns696e88d2017-03-16 07:34:55 -0400105 vlib_buffer_t * p2, * p3;
Neale Ranns9ca18c62016-12-10 21:08:09 +0000106
Neale Ranns696e88d2017-03-16 07:34:55 -0400107 p2 = vlib_get_buffer (vm, from[2]);
108 p3 = vlib_get_buffer (vm, from[3]);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000109
Neale Rannsbd4d6452018-05-02 05:15:16 -0700110 vlib_prefetch_buffer_header (p2, LOAD);
111 vlib_prefetch_buffer_header (p3, LOAD);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000112
Neale Rannsbd4d6452018-05-02 05:15:16 -0700113 CLIB_PREFETCH (p2->data, sizeof (h0[0]), LOAD);
114 CLIB_PREFETCH (p3->data, sizeof (h1[0]), LOAD);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000115 }
116
Neale Ranns9ca18c62016-12-10 21:08:09 +0000117 bi0 = to_next[0] = from[0];
118 bi1 = to_next[1] = from[1];
119
120 from += 2;
121 to_next += 2;
122 n_left_from -= 2;
123 n_left_to_next -= 2;
124
125 b0 = vlib_get_buffer (vm, bi0);
126 b1 = vlib_get_buffer (vm, bi1);
127
128 h0 = vlib_buffer_get_current (b0);
129 h1 = vlib_buffer_get_current (b1);
130
131 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
132 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
133
Neale Ranns9ca18c62016-12-10 21:08:09 +0000134 /* TTL expired? */
Neale Ranns696e88d2017-03-16 07:34:55 -0400135 if (PREDICT_FALSE(h0[3] == 0))
136 {
Neale Ranns9ca18c62016-12-10 21:08:09 +0000137 next0 = MPLS_INPUT_NEXT_DROP;
138 b0->error = node->errors[MPLS_ERROR_TTL_EXPIRED];
Neale Ranns696e88d2017-03-16 07:34:55 -0400139 }
Neale Ranns9ca18c62016-12-10 21:08:09 +0000140 else
Neale Ranns696e88d2017-03-16 07:34:55 -0400141 {
Neale Ranns9ca18c62016-12-10 21:08:09 +0000142 next0 = MPLS_INPUT_NEXT_LOOKUP;
Neale Ranns696e88d2017-03-16 07:34:55 -0400143 vnet_feature_arc_start(mm->input_feature_arc_index,
144 sw_if_index0, &next0, b0);
Damjan Marion586afd72017-04-05 19:18:20 +0200145 vlib_increment_simple_counter (cm, thread_index, sw_if_index0, 1);
Neale Ranns696e88d2017-03-16 07:34:55 -0400146 }
Neale Ranns9ca18c62016-12-10 21:08:09 +0000147
Neale Ranns696e88d2017-03-16 07:34:55 -0400148 if (PREDICT_FALSE(h1[3] == 0))
149 {
Neale Ranns9ca18c62016-12-10 21:08:09 +0000150 next1 = MPLS_INPUT_NEXT_DROP;
151 b1->error = node->errors[MPLS_ERROR_TTL_EXPIRED];
Neale Ranns696e88d2017-03-16 07:34:55 -0400152 }
Neale Ranns9ca18c62016-12-10 21:08:09 +0000153 else
Neale Ranns696e88d2017-03-16 07:34:55 -0400154 {
Neale Ranns9ca18c62016-12-10 21:08:09 +0000155 next1 = MPLS_INPUT_NEXT_LOOKUP;
Neale Ranns696e88d2017-03-16 07:34:55 -0400156 vnet_feature_arc_start(mm->input_feature_arc_index,
157 sw_if_index1, &next1, b1);
Damjan Marion586afd72017-04-05 19:18:20 +0200158 vlib_increment_simple_counter (cm, thread_index, sw_if_index1, 1);
Neale Ranns696e88d2017-03-16 07:34:55 -0400159 }
Neale Ranns9ca18c62016-12-10 21:08:09 +0000160
161 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
Neale Ranns696e88d2017-03-16 07:34:55 -0400162 {
Neale Ranns9ca18c62016-12-10 21:08:09 +0000163 mpls_input_trace_t *tr = vlib_add_trace (vm, node,
164 b0, sizeof (*tr));
165 tr->next_index = next0;
Neale Ranns696e88d2017-03-16 07:34:55 -0400166 tr->label_net_byte_order = *((u32*)h0);
167 }
Neale Ranns9ca18c62016-12-10 21:08:09 +0000168 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
Neale Ranns696e88d2017-03-16 07:34:55 -0400169 {
Neale Ranns9ca18c62016-12-10 21:08:09 +0000170 mpls_input_trace_t *tr = vlib_add_trace (vm, node,
171 b1, sizeof (*tr));
172 tr->next_index = next1;
Neale Ranns696e88d2017-03-16 07:34:55 -0400173 tr->label_net_byte_order = *((u32*)h1);
174 }
Neale Ranns9ca18c62016-12-10 21:08:09 +0000175
176 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
177 to_next, n_left_to_next,
Neale Ranns696e88d2017-03-16 07:34:55 -0400178 bi0, bi1,
179 next0, next1);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000180 }
181
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100182 while (n_left_from > 0 && n_left_to_next > 0)
183 {
Neale Ranns696e88d2017-03-16 07:34:55 -0400184 u32 sw_if_index0, next0, bi0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100185 vlib_buffer_t * b0;
Neale Ranns696e88d2017-03-16 07:34:55 -0400186 char * h0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100187
188 bi0 = from[0];
189 to_next[0] = bi0;
190 from += 1;
191 to_next += 1;
192 n_left_from -= 1;
193 n_left_to_next -= 1;
194
195 b0 = vlib_get_buffer (vm, bi0);
196 h0 = vlib_buffer_get_current (b0);
197 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
198
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100199 /* TTL expired? */
Neale Ranns696e88d2017-03-16 07:34:55 -0400200 if (PREDICT_FALSE(h0[3] == 0))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100201 {
202 next0 = MPLS_INPUT_NEXT_DROP;
203 b0->error = node->errors[MPLS_ERROR_TTL_EXPIRED];
204 }
205 else
206 {
Neale Rannsb85e4392017-03-16 16:12:57 -0400207 next0 = MPLS_INPUT_NEXT_LOOKUP;
Damjan Marion8b3191e2016-11-09 19:54:20 +0100208 vnet_feature_arc_start(mm->input_feature_arc_index, sw_if_index0, &next0, b0);
Damjan Marion586afd72017-04-05 19:18:20 +0200209 vlib_increment_simple_counter (cm, thread_index, sw_if_index0, 1);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100210 }
211
Dave Barachf8d50682019-05-14 18:01:44 -0400212 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100213 {
Dave Barachf8d50682019-05-14 18:01:44 -0400214 mpls_input_trace_t *tr = vlib_add_trace (vm, node,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100215 b0, sizeof (*tr));
216 tr->next_index = next0;
Neale Ranns696e88d2017-03-16 07:34:55 -0400217 tr->label_net_byte_order = *(u32*)h0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100218 }
219
220 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
221 to_next, n_left_to_next,
222 bi0, next0);
223 }
224
225 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
226 }
227 vlib_node_increment_counter (vm, mpls_input_node.index,
228 MPLS_ERROR_PKTS_DECAP, from_frame->n_vectors);
229 return from_frame->n_vectors;
230}
231
Filip Tehlar17fcd982019-03-05 04:32:11 -0800232VLIB_NODE_FN (mpls_input_node) (vlib_main_t * vm,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100233 vlib_node_runtime_t * node,
234 vlib_frame_t * from_frame)
235{
236 return mpls_input_inline (vm, node, from_frame);
237}
238
239static char * mpls_error_strings[] = {
240#define mpls_error(n,s) s,
241#include "error.def"
242#undef mpls_error
243};
244
245VLIB_REGISTER_NODE (mpls_input_node) = {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100246 .name = "mpls-input",
247 /* Takes a vector of packets. */
248 .vector_size = sizeof (u32),
249
250 .runtime_data_bytes = sizeof(mpls_input_runtime_t),
251
252 .n_errors = MPLS_N_ERROR,
253 .error_strings = mpls_error_strings,
254
255 .n_next_nodes = MPLS_INPUT_N_NEXT,
256 .next_nodes = {
257#define _(s,n) [MPLS_INPUT_NEXT_##s] = n,
258 foreach_mpls_input_next
259#undef _
260 },
261
262 .format_buffer = format_mpls_unicast_header_net_byte_order,
263 .format_trace = format_mpls_input_trace,
264};
265
Filip Tehlar17fcd982019-03-05 04:32:11 -0800266#ifndef CLIB_MARCH_VARIANT
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100267static void
268mpls_setup_nodes (vlib_main_t * vm)
269{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100270 pg_node_t * pn;
271
272 pn = pg_get_node (mpls_input_node.index);
273 pn->unformat_edit = unformat_pg_mpls_header;
274
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800275 ethernet_register_input_type (vm, ETHERNET_TYPE_MPLS,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100276 mpls_input_node.index);
277}
278
279static clib_error_t * mpls_input_init (vlib_main_t * vm)
280{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100281 mpls_setup_nodes (vm);
282
Damjan Marion8b3191e2016-11-09 19:54:20 +0100283 return 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100284}
285
Dave Barachf8d50682019-05-14 18:01:44 -0400286/* *INDENT-OFF* */
287VLIB_INIT_FUNCTION (mpls_input_init) =
288{
289 .runs_after = VLIB_INITS("mpls_init"),
290};
291/* *INDENT-ON* */
Filip Tehlar17fcd982019-03-05 04:32:11 -0800292#endif /* CLIB_MARCH_VARIANT */