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 | 41a85c0 | 2022-08-09 01:24:41 +0000 | [diff] [blame] | 22 | #include <vnet/mpls/mpls.api_enum.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 23 | |
| 24 | typedef struct { |
| 25 | u32 next_index; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 26 | u32 label_net_byte_order; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 27 | } mpls_input_trace_t; |
| 28 | |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 29 | #define foreach_mpls_input_next \ |
| 30 | _(DROP, "error-drop") \ |
| 31 | _(LOOKUP, "mpls-lookup") |
| 32 | |
| 33 | typedef enum { |
| 34 | #define _(s,n) MPLS_INPUT_NEXT_##s, |
| 35 | foreach_mpls_input_next |
| 36 | #undef _ |
| 37 | MPLS_INPUT_N_NEXT, |
| 38 | } mpls_input_next_t; |
| 39 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 40 | static u8 * |
| 41 | format_mpls_input_trace (u8 * s, va_list * args) |
| 42 | { |
| 43 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 44 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 45 | mpls_input_trace_t * t = va_arg (*args, mpls_input_trace_t *); |
| 46 | char * next_name; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 47 | u32 label; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 48 | next_name = "BUG!"; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 49 | label = clib_net_to_host_u32(t->label_net_byte_order); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 50 | |
| 51 | #define _(a,b) if (t->next_index == MPLS_INPUT_NEXT_##a) next_name = b; |
| 52 | foreach_mpls_input_next; |
| 53 | #undef _ |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 54 | |
| 55 | s = format (s, "MPLS: next %s[%d] label %d ttl %d exp %d", |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 56 | next_name, t->next_index, |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 57 | vnet_mpls_uc_get_label(label), |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 58 | vnet_mpls_uc_get_ttl(label), |
| 59 | vnet_mpls_uc_get_exp(label)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 60 | |
| 61 | return s; |
| 62 | } |
| 63 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 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; |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 77 | mpls_main_t * mm = &mpls_main; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 78 | u32 thread_index = vlib_get_thread_index(); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 79 | vlib_simple_counter_main_t * cm; |
| 80 | vnet_main_t * vnm = vnet_get_main(); |
| 81 | |
| 82 | from = vlib_frame_vector_args (from_frame); |
| 83 | n_left_from = from_frame->n_vectors; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 84 | |
| 85 | next_index = node->cached_next_index; |
| 86 | |
| 87 | cm = vec_elt_at_index (vnm->interface_main.sw_if_counters, |
| 88 | VNET_INTERFACE_COUNTER_MPLS); |
| 89 | |
| 90 | while (n_left_from > 0) |
| 91 | { |
| 92 | u32 n_left_to_next; |
| 93 | |
| 94 | vlib_get_next_frame (vm, node, next_index, |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 95 | to_next, n_left_to_next); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 96 | |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 97 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 98 | { |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 99 | u32 bi0, next0, sw_if_index0; |
| 100 | u32 bi1, next1, sw_if_index1; |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 101 | vlib_buffer_t *b0, *b1; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 102 | char *h0, *h1; |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 103 | |
| 104 | /* Prefetch next iteration. */ |
| 105 | { |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 106 | vlib_buffer_t * p2, * p3; |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 107 | |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 108 | p2 = vlib_get_buffer (vm, from[2]); |
| 109 | p3 = vlib_get_buffer (vm, from[3]); |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 110 | |
Neale Ranns | bd4d645 | 2018-05-02 05:15:16 -0700 | [diff] [blame] | 111 | vlib_prefetch_buffer_header (p2, LOAD); |
| 112 | vlib_prefetch_buffer_header (p3, LOAD); |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 113 | |
Neale Ranns | bd4d645 | 2018-05-02 05:15:16 -0700 | [diff] [blame] | 114 | CLIB_PREFETCH (p2->data, sizeof (h0[0]), LOAD); |
| 115 | CLIB_PREFETCH (p3->data, sizeof (h1[0]), LOAD); |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 118 | bi0 = to_next[0] = from[0]; |
| 119 | bi1 = to_next[1] = from[1]; |
| 120 | |
| 121 | from += 2; |
| 122 | to_next += 2; |
| 123 | n_left_from -= 2; |
| 124 | n_left_to_next -= 2; |
| 125 | |
| 126 | b0 = vlib_get_buffer (vm, bi0); |
| 127 | b1 = vlib_get_buffer (vm, bi1); |
| 128 | |
| 129 | h0 = vlib_buffer_get_current (b0); |
| 130 | h1 = vlib_buffer_get_current (b1); |
| 131 | |
| 132 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 133 | sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX]; |
| 134 | |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 135 | /* TTL expired? */ |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 136 | if (PREDICT_FALSE(h0[3] == 0)) |
| 137 | { |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 138 | next0 = MPLS_INPUT_NEXT_DROP; |
| 139 | b0->error = node->errors[MPLS_ERROR_TTL_EXPIRED]; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 140 | } |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 141 | else |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 142 | { |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 143 | next0 = MPLS_INPUT_NEXT_LOOKUP; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 144 | vnet_feature_arc_start(mm->input_feature_arc_index, |
| 145 | sw_if_index0, &next0, b0); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 146 | vlib_increment_simple_counter (cm, thread_index, sw_if_index0, 1); |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 147 | } |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 148 | |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 149 | if (PREDICT_FALSE(h1[3] == 0)) |
| 150 | { |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 151 | next1 = MPLS_INPUT_NEXT_DROP; |
| 152 | b1->error = node->errors[MPLS_ERROR_TTL_EXPIRED]; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 153 | } |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 154 | else |
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 | next1 = MPLS_INPUT_NEXT_LOOKUP; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 157 | vnet_feature_arc_start(mm->input_feature_arc_index, |
| 158 | sw_if_index1, &next1, b1); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 159 | vlib_increment_simple_counter (cm, thread_index, sw_if_index1, 1); |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 160 | } |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 161 | |
| 162 | if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) |
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 | mpls_input_trace_t *tr = vlib_add_trace (vm, node, |
| 165 | b0, sizeof (*tr)); |
| 166 | tr->next_index = next0; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 167 | tr->label_net_byte_order = *((u32*)h0); |
| 168 | } |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 169 | if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED)) |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 170 | { |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 171 | mpls_input_trace_t *tr = vlib_add_trace (vm, node, |
| 172 | b1, sizeof (*tr)); |
| 173 | tr->next_index = next1; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 174 | tr->label_net_byte_order = *((u32*)h1); |
| 175 | } |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 176 | |
| 177 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, |
| 178 | to_next, n_left_to_next, |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 179 | bi0, bi1, |
| 180 | next0, next1); |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 183 | while (n_left_from > 0 && n_left_to_next > 0) |
| 184 | { |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 185 | u32 sw_if_index0, next0, bi0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 186 | vlib_buffer_t * b0; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 187 | char * h0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 188 | |
| 189 | bi0 = from[0]; |
| 190 | to_next[0] = bi0; |
| 191 | from += 1; |
| 192 | to_next += 1; |
| 193 | n_left_from -= 1; |
| 194 | n_left_to_next -= 1; |
| 195 | |
| 196 | b0 = vlib_get_buffer (vm, bi0); |
| 197 | h0 = vlib_buffer_get_current (b0); |
| 198 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 199 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 200 | /* TTL expired? */ |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 201 | if (PREDICT_FALSE(h0[3] == 0)) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 202 | { |
| 203 | next0 = MPLS_INPUT_NEXT_DROP; |
| 204 | b0->error = node->errors[MPLS_ERROR_TTL_EXPIRED]; |
| 205 | } |
| 206 | else |
| 207 | { |
Neale Ranns | b85e439 | 2017-03-16 16:12:57 -0400 | [diff] [blame] | 208 | next0 = MPLS_INPUT_NEXT_LOOKUP; |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 209 | 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] | 210 | vlib_increment_simple_counter (cm, thread_index, sw_if_index0, 1); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 213 | if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 214 | { |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 215 | mpls_input_trace_t *tr = vlib_add_trace (vm, node, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 216 | b0, sizeof (*tr)); |
| 217 | tr->next_index = next0; |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 218 | tr->label_net_byte_order = *(u32*)h0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 222 | to_next, n_left_to_next, |
| 223 | bi0, next0); |
| 224 | } |
| 225 | |
| 226 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 227 | } |
| 228 | vlib_node_increment_counter (vm, mpls_input_node.index, |
| 229 | MPLS_ERROR_PKTS_DECAP, from_frame->n_vectors); |
| 230 | return from_frame->n_vectors; |
| 231 | } |
| 232 | |
Filip Tehlar | 17fcd98 | 2019-03-05 04:32:11 -0800 | [diff] [blame] | 233 | VLIB_NODE_FN (mpls_input_node) (vlib_main_t * vm, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 234 | vlib_node_runtime_t * node, |
| 235 | vlib_frame_t * from_frame) |
| 236 | { |
| 237 | return mpls_input_inline (vm, node, from_frame); |
| 238 | } |
| 239 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 240 | VLIB_REGISTER_NODE (mpls_input_node) = { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 241 | .name = "mpls-input", |
| 242 | /* Takes a vector of packets. */ |
| 243 | .vector_size = sizeof (u32), |
| 244 | |
| 245 | .runtime_data_bytes = sizeof(mpls_input_runtime_t), |
| 246 | |
| 247 | .n_errors = MPLS_N_ERROR, |
Neale Ranns | 41a85c0 | 2022-08-09 01:24:41 +0000 | [diff] [blame] | 248 | .error_counters = mpls_error_counters, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 249 | |
| 250 | .n_next_nodes = MPLS_INPUT_N_NEXT, |
| 251 | .next_nodes = { |
| 252 | #define _(s,n) [MPLS_INPUT_NEXT_##s] = n, |
| 253 | foreach_mpls_input_next |
| 254 | #undef _ |
| 255 | }, |
| 256 | |
| 257 | .format_buffer = format_mpls_unicast_header_net_byte_order, |
| 258 | .format_trace = format_mpls_input_trace, |
| 259 | }; |
| 260 | |
Filip Tehlar | 17fcd98 | 2019-03-05 04:32:11 -0800 | [diff] [blame] | 261 | #ifndef CLIB_MARCH_VARIANT |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 262 | static void |
| 263 | mpls_setup_nodes (vlib_main_t * vm) |
| 264 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 265 | pg_node_t * pn; |
| 266 | |
| 267 | pn = pg_get_node (mpls_input_node.index); |
| 268 | pn->unformat_edit = unformat_pg_mpls_header; |
| 269 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 270 | ethernet_register_input_type (vm, ETHERNET_TYPE_MPLS, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 271 | mpls_input_node.index); |
| 272 | } |
| 273 | |
| 274 | static clib_error_t * mpls_input_init (vlib_main_t * vm) |
| 275 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 276 | mpls_setup_nodes (vm); |
| 277 | |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 278 | return 0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 279 | } |
| 280 | |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 281 | VLIB_INIT_FUNCTION (mpls_input_init) = |
| 282 | { |
| 283 | .runs_after = VLIB_INITS("mpls_init"), |
| 284 | }; |
Filip Tehlar | 17fcd98 | 2019-03-05 04:32:11 -0800 | [diff] [blame] | 285 | #endif /* CLIB_MARCH_VARIANT */ |