Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1 | /* |
Neale Ranns | 1357f3b | 2016-10-16 12:01:42 -0700 | [diff] [blame] | 2 | * node.c: MPLS input |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 3 | * |
| 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 Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 21 | #include <vnet/feature/feature.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 22 | |
| 23 | typedef struct { |
| 24 | u32 next_index; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 25 | u32 label_net_byte_order; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 26 | } mpls_input_trace_t; |
| 27 | |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 28 | #define foreach_mpls_input_next \ |
| 29 | _(DROP, "error-drop") \ |
| 30 | _(LOOKUP, "mpls-lookup") |
| 31 | |
| 32 | typedef 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 Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 39 | static u8 * |
| 40 | format_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 Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 46 | u32 label; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 47 | next_name = "BUG!"; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 48 | label = clib_net_to_host_u32(t->label_net_byte_order); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 49 | |
| 50 | #define _(a,b) if (t->next_index == MPLS_INPUT_NEXT_##a) next_name = b; |
| 51 | foreach_mpls_input_next; |
| 52 | #undef _ |
| 53 | |
| 54 | s = format (s, "MPLS: next %s[%d] label %d ttl %d", |
| 55 | next_name, t->next_index, |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 56 | vnet_mpls_uc_get_label(label), |
| 57 | vnet_mpls_uc_get_ttl(label)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 58 | |
| 59 | return s; |
| 60 | } |
| 61 | |
| 62 | vlib_node_registration_t mpls_input_node; |
| 63 | |
| 64 | typedef struct { |
| 65 | u32 last_label; |
| 66 | u32 last_inner_fib_index; |
| 67 | u32 last_outer_fib_index; |
| 68 | mpls_main_t * mpls_main; |
| 69 | } mpls_input_runtime_t; |
| 70 | |
| 71 | static inline uword |
| 72 | mpls_input_inline (vlib_main_t * vm, |
| 73 | vlib_node_runtime_t * node, |
| 74 | vlib_frame_t * from_frame) |
| 75 | { |
| 76 | u32 n_left_from, next_index, * from, * to_next; |
| 77 | mpls_input_runtime_t * rt; |
| 78 | mpls_main_t * mm; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 79 | u32 thread_index = vlib_get_thread_index(); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 80 | vlib_simple_counter_main_t * cm; |
| 81 | vnet_main_t * vnm = vnet_get_main(); |
| 82 | |
| 83 | from = vlib_frame_vector_args (from_frame); |
| 84 | n_left_from = from_frame->n_vectors; |
| 85 | rt = vlib_node_get_runtime_data (vm, mpls_input_node.index); |
| 86 | mm = rt->mpls_main; |
| 87 | /* |
| 88 | * Force an initial lookup every time, in case the control-plane |
| 89 | * changed the label->FIB mapping. |
| 90 | */ |
| 91 | rt->last_label = ~0; |
| 92 | |
| 93 | next_index = node->cached_next_index; |
| 94 | |
| 95 | cm = vec_elt_at_index (vnm->interface_main.sw_if_counters, |
| 96 | VNET_INTERFACE_COUNTER_MPLS); |
| 97 | |
| 98 | while (n_left_from > 0) |
| 99 | { |
| 100 | u32 n_left_to_next; |
| 101 | |
| 102 | vlib_get_next_frame (vm, node, next_index, |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 103 | to_next, n_left_to_next); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 104 | |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 105 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 106 | { |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 107 | u32 bi0, next0, sw_if_index0; |
| 108 | u32 bi1, next1, sw_if_index1; |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 109 | vlib_buffer_t *b0, *b1; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 110 | char *h0, *h1; |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 111 | |
| 112 | /* Prefetch next iteration. */ |
| 113 | { |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 114 | vlib_buffer_t * p2, * p3; |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 115 | |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 116 | p2 = vlib_get_buffer (vm, from[2]); |
| 117 | p3 = vlib_get_buffer (vm, from[3]); |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 118 | |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 119 | vlib_prefetch_buffer_header (p2, STORE); |
| 120 | vlib_prefetch_buffer_header (p3, STORE); |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 121 | |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 122 | CLIB_PREFETCH (p2->data, sizeof (h0[0]), STORE); |
| 123 | CLIB_PREFETCH (p3->data, sizeof (h1[0]), STORE); |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 126 | bi0 = to_next[0] = from[0]; |
| 127 | bi1 = to_next[1] = from[1]; |
| 128 | |
| 129 | from += 2; |
| 130 | to_next += 2; |
| 131 | n_left_from -= 2; |
| 132 | n_left_to_next -= 2; |
| 133 | |
| 134 | b0 = vlib_get_buffer (vm, bi0); |
| 135 | b1 = vlib_get_buffer (vm, bi1); |
| 136 | |
| 137 | h0 = vlib_buffer_get_current (b0); |
| 138 | h1 = vlib_buffer_get_current (b1); |
| 139 | |
| 140 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 141 | sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX]; |
| 142 | |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 143 | /* TTL expired? */ |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 144 | if (PREDICT_FALSE(h0[3] == 0)) |
| 145 | { |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 146 | next0 = MPLS_INPUT_NEXT_DROP; |
| 147 | b0->error = node->errors[MPLS_ERROR_TTL_EXPIRED]; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 148 | } |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 149 | else |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 150 | { |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 151 | next0 = MPLS_INPUT_NEXT_LOOKUP; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 152 | vnet_feature_arc_start(mm->input_feature_arc_index, |
| 153 | sw_if_index0, &next0, b0); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 154 | vlib_increment_simple_counter (cm, thread_index, sw_if_index0, 1); |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 155 | } |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 156 | |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 157 | if (PREDICT_FALSE(h1[3] == 0)) |
| 158 | { |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 159 | next1 = MPLS_INPUT_NEXT_DROP; |
| 160 | b1->error = node->errors[MPLS_ERROR_TTL_EXPIRED]; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 161 | } |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 162 | else |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 163 | { |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 164 | next1 = MPLS_INPUT_NEXT_LOOKUP; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 165 | vnet_feature_arc_start(mm->input_feature_arc_index, |
| 166 | sw_if_index1, &next1, b1); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 167 | vlib_increment_simple_counter (cm, thread_index, sw_if_index1, 1); |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 168 | } |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 169 | |
| 170 | if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 171 | { |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 172 | mpls_input_trace_t *tr = vlib_add_trace (vm, node, |
| 173 | b0, sizeof (*tr)); |
| 174 | tr->next_index = next0; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 175 | tr->label_net_byte_order = *((u32*)h0); |
| 176 | } |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 177 | if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED)) |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 178 | { |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 179 | mpls_input_trace_t *tr = vlib_add_trace (vm, node, |
| 180 | b1, sizeof (*tr)); |
| 181 | tr->next_index = next1; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 182 | tr->label_net_byte_order = *((u32*)h1); |
| 183 | } |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 184 | |
| 185 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, |
| 186 | to_next, n_left_to_next, |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 187 | bi0, bi1, |
| 188 | next0, next1); |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 191 | while (n_left_from > 0 && n_left_to_next > 0) |
| 192 | { |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 193 | u32 sw_if_index0, next0, bi0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 194 | vlib_buffer_t * b0; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 195 | char * h0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 196 | |
| 197 | bi0 = from[0]; |
| 198 | to_next[0] = bi0; |
| 199 | from += 1; |
| 200 | to_next += 1; |
| 201 | n_left_from -= 1; |
| 202 | n_left_to_next -= 1; |
| 203 | |
| 204 | b0 = vlib_get_buffer (vm, bi0); |
| 205 | h0 = vlib_buffer_get_current (b0); |
| 206 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 207 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 208 | /* TTL expired? */ |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 209 | if (PREDICT_FALSE(h0[3] == 0)) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 210 | { |
| 211 | next0 = MPLS_INPUT_NEXT_DROP; |
| 212 | b0->error = node->errors[MPLS_ERROR_TTL_EXPIRED]; |
| 213 | } |
| 214 | else |
| 215 | { |
Neale Ranns | b85e439 | 2017-03-16 16:12:57 -0400 | [diff] [blame] | 216 | next0 = MPLS_INPUT_NEXT_LOOKUP; |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 217 | vnet_feature_arc_start(mm->input_feature_arc_index, sw_if_index0, &next0, b0); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 218 | vlib_increment_simple_counter (cm, thread_index, sw_if_index0, 1); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 222 | { |
| 223 | mpls_input_trace_t *tr = vlib_add_trace (vm, node, |
| 224 | b0, sizeof (*tr)); |
| 225 | tr->next_index = next0; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 226 | tr->label_net_byte_order = *(u32*)h0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 230 | to_next, n_left_to_next, |
| 231 | bi0, next0); |
| 232 | } |
| 233 | |
| 234 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 235 | } |
| 236 | vlib_node_increment_counter (vm, mpls_input_node.index, |
| 237 | MPLS_ERROR_PKTS_DECAP, from_frame->n_vectors); |
| 238 | return from_frame->n_vectors; |
| 239 | } |
| 240 | |
| 241 | static uword |
| 242 | mpls_input (vlib_main_t * vm, |
| 243 | vlib_node_runtime_t * node, |
| 244 | vlib_frame_t * from_frame) |
| 245 | { |
| 246 | return mpls_input_inline (vm, node, from_frame); |
| 247 | } |
| 248 | |
| 249 | static char * mpls_error_strings[] = { |
| 250 | #define mpls_error(n,s) s, |
| 251 | #include "error.def" |
| 252 | #undef mpls_error |
| 253 | }; |
| 254 | |
| 255 | VLIB_REGISTER_NODE (mpls_input_node) = { |
| 256 | .function = mpls_input, |
| 257 | .name = "mpls-input", |
| 258 | /* Takes a vector of packets. */ |
| 259 | .vector_size = sizeof (u32), |
| 260 | |
| 261 | .runtime_data_bytes = sizeof(mpls_input_runtime_t), |
| 262 | |
| 263 | .n_errors = MPLS_N_ERROR, |
| 264 | .error_strings = mpls_error_strings, |
| 265 | |
| 266 | .n_next_nodes = MPLS_INPUT_N_NEXT, |
| 267 | .next_nodes = { |
| 268 | #define _(s,n) [MPLS_INPUT_NEXT_##s] = n, |
| 269 | foreach_mpls_input_next |
| 270 | #undef _ |
| 271 | }, |
| 272 | |
| 273 | .format_buffer = format_mpls_unicast_header_net_byte_order, |
| 274 | .format_trace = format_mpls_input_trace, |
| 275 | }; |
| 276 | |
| 277 | VLIB_NODE_FUNCTION_MULTIARCH (mpls_input_node, mpls_input) |
| 278 | |
| 279 | static void |
| 280 | mpls_setup_nodes (vlib_main_t * vm) |
| 281 | { |
| 282 | mpls_input_runtime_t * rt; |
| 283 | pg_node_t * pn; |
| 284 | |
| 285 | pn = pg_get_node (mpls_input_node.index); |
| 286 | pn->unformat_edit = unformat_pg_mpls_header; |
| 287 | |
| 288 | rt = vlib_node_get_runtime_data (vm, mpls_input_node.index); |
| 289 | rt->last_label = (u32) ~0; |
| 290 | rt->last_inner_fib_index = 0; |
| 291 | rt->last_outer_fib_index = 0; |
| 292 | rt->mpls_main = &mpls_main; |
| 293 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 294 | ethernet_register_input_type (vm, ETHERNET_TYPE_MPLS, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 295 | mpls_input_node.index); |
| 296 | } |
| 297 | |
| 298 | static clib_error_t * mpls_input_init (vlib_main_t * vm) |
| 299 | { |
| 300 | clib_error_t * error; |
| 301 | |
| 302 | error = vlib_call_init_function (vm, mpls_init); |
| 303 | if (error) |
| 304 | clib_error_report (error); |
| 305 | |
| 306 | mpls_setup_nodes (vm); |
| 307 | |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 308 | return 0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | VLIB_INIT_FUNCTION (mpls_input_init); |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 312 | |
| 313 | static clib_error_t * mpls_input_worker_init (vlib_main_t * vm) |
| 314 | { |
| 315 | mpls_input_runtime_t * rt; |
| 316 | rt = vlib_node_get_runtime_data (vm, mpls_input_node.index); |
| 317 | rt->last_label = (u32) ~0; |
| 318 | rt->last_inner_fib_index = 0; |
| 319 | rt->last_outer_fib_index = 0; |
| 320 | rt->mpls_main = &mpls_main; |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | VLIB_WORKER_INIT_FUNCTION (mpls_input_worker_init); |