Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * mpls_output.c: MPLS Adj rewrite |
| 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> |
Neale Ranns | 1357f3b | 2016-10-16 12:01:42 -0700 | [diff] [blame] | 19 | #include <vnet/ip/ip.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 20 | #include <vnet/mpls/mpls.h> |
Rajesh Goel | d6f1c9c | 2019-10-06 13:17:36 +0530 | [diff] [blame] | 21 | #include <vnet/ip/ip_frag.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 22 | |
| 23 | typedef struct { |
| 24 | /* Adjacency taken. */ |
| 25 | u32 adj_index; |
| 26 | u32 flow_hash; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 27 | } mpls_output_trace_t; |
| 28 | |
Rajesh Goel | d6f1c9c | 2019-10-06 13:17:36 +0530 | [diff] [blame] | 29 | typedef enum { |
| 30 | MPLS_OUTPUT_MODE, |
| 31 | MPLS_OUTPUT_MIDCHAIN_MODE |
| 32 | }mpls_output_mode_t; |
| 33 | |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 34 | #define foreach_mpls_output_next \ |
Rajesh Goel | d6f1c9c | 2019-10-06 13:17:36 +0530 | [diff] [blame] | 35 | _(DROP, "error-drop") \ |
Ole Troan | eb284a1 | 2019-10-09 13:33:19 +0200 | [diff] [blame] | 36 | _(FRAG, "mpls-frag") |
Neale Ranns | 696e88d | 2017-03-16 07:34:55 -0400 | [diff] [blame] | 37 | |
| 38 | typedef enum { |
| 39 | #define _(s,n) MPLS_OUTPUT_NEXT_##s, |
| 40 | foreach_mpls_output_next |
| 41 | #undef _ |
| 42 | MPLS_OUTPUT_N_NEXT, |
| 43 | } mpls_output_next_t; |
| 44 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 45 | static u8 * |
| 46 | format_mpls_output_trace (u8 * s, va_list * args) |
| 47 | { |
| 48 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 49 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 50 | mpls_output_trace_t * t = va_arg (*args, mpls_output_trace_t *); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 51 | |
| 52 | s = format (s, "adj-idx %d : %U flow hash: 0x%08x", |
| 53 | t->adj_index, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 54 | format_ip_adjacency, t->adj_index, FORMAT_IP_ADJACENCY_NONE, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 55 | t->flow_hash); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 56 | return s; |
| 57 | } |
| 58 | |
| 59 | static inline uword |
| 60 | mpls_output_inline (vlib_main_t * vm, |
| 61 | vlib_node_runtime_t * node, |
John Lo | aeb06f4 | 2016-10-15 17:45:35 -0400 | [diff] [blame] | 62 | vlib_frame_t * from_frame, |
Rajesh Goel | d6f1c9c | 2019-10-06 13:17:36 +0530 | [diff] [blame] | 63 | mpls_output_mode_t mode) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 64 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 65 | u32 n_left_from, next_index, * from, * to_next, thread_index; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 66 | vlib_node_runtime_t * error_node; |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 67 | u32 n_left_to_next; |
Neale Ranns | b069a69 | 2017-03-15 12:34:25 -0400 | [diff] [blame] | 68 | mpls_main_t *mm; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 69 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 70 | thread_index = vlib_get_thread_index(); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 71 | error_node = vlib_node_get_runtime (vm, mpls_output_node.index); |
| 72 | from = vlib_frame_vector_args (from_frame); |
| 73 | n_left_from = from_frame->n_vectors; |
| 74 | next_index = node->cached_next_index; |
Neale Ranns | b069a69 | 2017-03-15 12:34:25 -0400 | [diff] [blame] | 75 | mm = &mpls_main; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 76 | |
| 77 | while (n_left_from > 0) |
| 78 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 79 | vlib_get_next_frame (vm, node, next_index, |
| 80 | to_next, n_left_to_next); |
| 81 | |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 82 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 83 | { |
| 84 | ip_adjacency_t * adj0; |
| 85 | mpls_unicast_header_t *hdr0; |
| 86 | vlib_buffer_t * p0; |
Benoît Ganne | c257e07 | 2019-06-17 14:42:47 +0200 | [diff] [blame] | 87 | u32 pi0, adj_index0, next0, error0; |
| 88 | word rw_len0; |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 89 | |
| 90 | ip_adjacency_t * adj1; |
| 91 | mpls_unicast_header_t *hdr1; |
| 92 | vlib_buffer_t * p1; |
Benoît Ganne | c257e07 | 2019-06-17 14:42:47 +0200 | [diff] [blame] | 93 | u32 pi1, adj_index1, next1, error1; |
| 94 | word rw_len1; |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 95 | |
| 96 | /* Prefetch next iteration. */ |
| 97 | { |
| 98 | vlib_buffer_t * p2, * p3; |
| 99 | |
| 100 | p2 = vlib_get_buffer (vm, from[2]); |
| 101 | p3 = vlib_get_buffer (vm, from[3]); |
| 102 | |
| 103 | vlib_prefetch_buffer_header (p2, STORE); |
| 104 | vlib_prefetch_buffer_header (p3, STORE); |
| 105 | |
| 106 | CLIB_PREFETCH (p2->data, sizeof (hdr0[0]), STORE); |
| 107 | CLIB_PREFETCH (p3->data, sizeof (hdr1[0]), STORE); |
| 108 | } |
| 109 | |
| 110 | pi0 = to_next[0] = from[0]; |
| 111 | pi1 = to_next[1] = from[1]; |
| 112 | |
| 113 | from += 2; |
| 114 | n_left_from -= 2; |
| 115 | to_next += 2; |
| 116 | n_left_to_next -= 2; |
| 117 | |
| 118 | p0 = vlib_get_buffer (vm, pi0); |
| 119 | p1 = vlib_get_buffer (vm, pi1); |
| 120 | |
| 121 | adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX]; |
| 122 | adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX]; |
| 123 | |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 124 | adj0 = adj_get(adj_index0); |
| 125 | adj1 = adj_get(adj_index1); |
| 126 | hdr0 = vlib_buffer_get_current (p0); |
| 127 | hdr1 = vlib_buffer_get_current (p1); |
| 128 | |
| 129 | /* Guess we are only writing on simple Ethernet header. */ |
| 130 | vnet_rewrite_two_headers (adj0[0], adj1[0], hdr0, hdr1, |
| 131 | sizeof (ethernet_header_t)); |
| 132 | |
| 133 | /* Update packet buffer attributes/set output interface. */ |
| 134 | rw_len0 = adj0[0].rewrite_header.data_bytes; |
| 135 | rw_len1 = adj1[0].rewrite_header.data_bytes; |
Neale Ranns | 039cbfe | 2018-02-27 03:45:38 -0800 | [diff] [blame] | 136 | vnet_buffer (p0)->mpls.save_rewrite_length = rw_len0; |
| 137 | vnet_buffer (p1)->mpls.save_rewrite_length = rw_len1; |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 138 | |
Neale Ranns | 044183f | 2017-01-24 01:34:25 -0800 | [diff] [blame] | 139 | /* Bump the adj counters for packet and bytes */ |
| 140 | vlib_increment_combined_counter |
| 141 | (&adjacency_counters, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 142 | thread_index, |
Neale Ranns | 044183f | 2017-01-24 01:34:25 -0800 | [diff] [blame] | 143 | adj_index0, |
| 144 | 1, |
| 145 | vlib_buffer_length_in_chain (vm, p0) + rw_len0); |
| 146 | vlib_increment_combined_counter |
| 147 | (&adjacency_counters, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 148 | thread_index, |
Neale Ranns | 044183f | 2017-01-24 01:34:25 -0800 | [diff] [blame] | 149 | adj_index1, |
| 150 | 1, |
| 151 | vlib_buffer_length_in_chain (vm, p1) + rw_len1); |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 152 | |
| 153 | /* Check MTU of outgoing interface. */ |
| 154 | if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <= |
| 155 | adj0[0].rewrite_header.max_l3_packet_bytes)) |
| 156 | { |
Zhiyong Yang | 734d430 | 2019-05-29 22:38:19 -0400 | [diff] [blame] | 157 | vlib_buffer_advance(p0, -rw_len0); |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 158 | |
| 159 | vnet_buffer (p0)->sw_if_index[VLIB_TX] = |
| 160 | adj0[0].rewrite_header.sw_if_index; |
| 161 | next0 = adj0[0].rewrite_header.next_index; |
| 162 | error0 = IP4_ERROR_NONE; |
| 163 | |
Neale Ranns | b069a69 | 2017-03-15 12:34:25 -0400 | [diff] [blame] | 164 | if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES)) |
| 165 | vnet_feature_arc_start (mm->output_feature_arc_index, |
| 166 | adj0[0].rewrite_header.sw_if_index, |
| 167 | &next0, p0); |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 168 | } |
| 169 | else |
| 170 | { |
Rajesh Goel | d6f1c9c | 2019-10-06 13:17:36 +0530 | [diff] [blame] | 171 | error0 = IP4_ERROR_MTU_EXCEEDED; |
Ole Troan | eb284a1 | 2019-10-09 13:33:19 +0200 | [diff] [blame] | 172 | next0 = MPLS_OUTPUT_NEXT_FRAG; |
Rajesh Goel | d6f1c9c | 2019-10-06 13:17:36 +0530 | [diff] [blame] | 173 | vlib_node_increment_counter (vm, mpls_output_node.index, |
| 174 | MPLS_ERROR_PKTS_NEED_FRAG, |
| 175 | 1); |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 176 | } |
| 177 | if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p1) <= |
| 178 | adj1[0].rewrite_header.max_l3_packet_bytes)) |
| 179 | { |
Zhiyong Yang | 734d430 | 2019-05-29 22:38:19 -0400 | [diff] [blame] | 180 | vlib_buffer_advance(p1, -rw_len1); |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 181 | |
| 182 | vnet_buffer (p1)->sw_if_index[VLIB_TX] = |
| 183 | adj1[0].rewrite_header.sw_if_index; |
| 184 | next1 = adj1[0].rewrite_header.next_index; |
| 185 | error1 = IP4_ERROR_NONE; |
| 186 | |
Neale Ranns | b069a69 | 2017-03-15 12:34:25 -0400 | [diff] [blame] | 187 | if (PREDICT_FALSE(adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES)) |
| 188 | vnet_feature_arc_start (mm->output_feature_arc_index, |
| 189 | adj1[0].rewrite_header.sw_if_index, |
| 190 | &next1, p1); |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 191 | } |
| 192 | else |
| 193 | { |
Rajesh Goel | d6f1c9c | 2019-10-06 13:17:36 +0530 | [diff] [blame] | 194 | error1 = IP4_ERROR_MTU_EXCEEDED; |
Ole Troan | eb284a1 | 2019-10-09 13:33:19 +0200 | [diff] [blame] | 195 | next1 = MPLS_OUTPUT_NEXT_FRAG; |
Rajesh Goel | d6f1c9c | 2019-10-06 13:17:36 +0530 | [diff] [blame] | 196 | vlib_node_increment_counter (vm, mpls_output_node.index, |
| 197 | MPLS_ERROR_PKTS_NEED_FRAG, |
| 198 | 1); |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 199 | } |
Rajesh Goel | d6f1c9c | 2019-10-06 13:17:36 +0530 | [diff] [blame] | 200 | if (mode == MPLS_OUTPUT_MIDCHAIN_MODE) |
Neale Ranns | b069a69 | 2017-03-15 12:34:25 -0400 | [diff] [blame] | 201 | { |
Neale Ranns | db14f5a | 2018-01-29 10:43:33 -0800 | [diff] [blame] | 202 | adj0->sub_type.midchain.fixup_func |
| 203 | (vm, adj0, p0, |
| 204 | adj0->sub_type.midchain.fixup_data); |
| 205 | adj1->sub_type.midchain.fixup_func |
| 206 | (vm, adj1, p1, |
| 207 | adj1->sub_type.midchain.fixup_data); |
Neale Ranns | b069a69 | 2017-03-15 12:34:25 -0400 | [diff] [blame] | 208 | } |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 209 | |
| 210 | p0->error = error_node->errors[error0]; |
| 211 | p1->error = error_node->errors[error1]; |
| 212 | |
| 213 | if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED)) |
| 214 | { |
| 215 | mpls_output_trace_t *tr = vlib_add_trace (vm, node, |
| 216 | p0, sizeof (*tr)); |
| 217 | tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX]; |
| 218 | tr->flow_hash = vnet_buffer(p0)->ip.flow_hash; |
| 219 | } |
| 220 | if (PREDICT_FALSE(p1->flags & VLIB_BUFFER_IS_TRACED)) |
| 221 | { |
| 222 | mpls_output_trace_t *tr = vlib_add_trace (vm, node, |
| 223 | p1, sizeof (*tr)); |
| 224 | tr->adj_index = vnet_buffer(p1)->ip.adj_index[VLIB_TX]; |
| 225 | tr->flow_hash = vnet_buffer(p1)->ip.flow_hash; |
| 226 | } |
| 227 | |
| 228 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, |
| 229 | to_next, n_left_to_next, |
| 230 | pi0, pi1, next0, next1); |
| 231 | } |
| 232 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 233 | while (n_left_from > 0 && n_left_to_next > 0) |
| 234 | { |
| 235 | ip_adjacency_t * adj0; |
Rajesh Goel | d6f1c9c | 2019-10-06 13:17:36 +0530 | [diff] [blame] | 236 | mpls_unicast_header_t *hdr0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 237 | vlib_buffer_t * p0; |
Benoît Ganne | c257e07 | 2019-06-17 14:42:47 +0200 | [diff] [blame] | 238 | u32 pi0, adj_index0, next0, error0; |
| 239 | word rw_len0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 240 | |
| 241 | pi0 = to_next[0] = from[0]; |
| 242 | |
| 243 | p0 = vlib_get_buffer (vm, pi0); |
| 244 | |
| 245 | adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX]; |
| 246 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 247 | adj0 = adj_get(adj_index0); |
Rajesh Goel | d6f1c9c | 2019-10-06 13:17:36 +0530 | [diff] [blame] | 248 | hdr0 = vlib_buffer_get_current (p0); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 249 | |
| 250 | /* Guess we are only writing on simple Ethernet header. */ |
Dave Barach | 9cbfb4c | 2020-04-08 10:19:29 -0400 | [diff] [blame] | 251 | vnet_rewrite_one_header (adj0[0], hdr0, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 252 | sizeof (ethernet_header_t)); |
Dave Barach | 9cbfb4c | 2020-04-08 10:19:29 -0400 | [diff] [blame] | 253 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 254 | /* Update packet buffer attributes/set output interface. */ |
| 255 | rw_len0 = adj0[0].rewrite_header.data_bytes; |
Neale Ranns | 039cbfe | 2018-02-27 03:45:38 -0800 | [diff] [blame] | 256 | vnet_buffer (p0)->mpls.save_rewrite_length = rw_len0; |
| 257 | |
Neale Ranns | 044183f | 2017-01-24 01:34:25 -0800 | [diff] [blame] | 258 | vlib_increment_combined_counter |
| 259 | (&adjacency_counters, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 260 | thread_index, |
Neale Ranns | 044183f | 2017-01-24 01:34:25 -0800 | [diff] [blame] | 261 | adj_index0, |
| 262 | 1, |
| 263 | vlib_buffer_length_in_chain (vm, p0) + rw_len0); |
Zhiyong Yang | 734d430 | 2019-05-29 22:38:19 -0400 | [diff] [blame] | 264 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 265 | /* Check MTU of outgoing interface. */ |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 266 | if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <= |
| 267 | adj0[0].rewrite_header.max_l3_packet_bytes)) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 268 | { |
Zhiyong Yang | 734d430 | 2019-05-29 22:38:19 -0400 | [diff] [blame] | 269 | vlib_buffer_advance(p0, -rw_len0); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 270 | |
| 271 | vnet_buffer (p0)->sw_if_index[VLIB_TX] = |
| 272 | adj0[0].rewrite_header.sw_if_index; |
| 273 | next0 = adj0[0].rewrite_header.next_index; |
Neale Ranns | 9ca18c6 | 2016-12-10 21:08:09 +0000 | [diff] [blame] | 274 | error0 = IP4_ERROR_NONE; |
John Lo | aeb06f4 | 2016-10-15 17:45:35 -0400 | [diff] [blame] | 275 | |
Neale Ranns | b069a69 | 2017-03-15 12:34:25 -0400 | [diff] [blame] | 276 | if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES)) |
| 277 | vnet_feature_arc_start (mm->output_feature_arc_index, |
| 278 | adj0[0].rewrite_header.sw_if_index, |
| 279 | &next0, p0); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 280 | } |
| 281 | else |
| 282 | { |
Rajesh Goel | d6f1c9c | 2019-10-06 13:17:36 +0530 | [diff] [blame] | 283 | error0 = IP4_ERROR_MTU_EXCEEDED; |
Ole Troan | eb284a1 | 2019-10-09 13:33:19 +0200 | [diff] [blame] | 284 | next0 = MPLS_OUTPUT_NEXT_FRAG; |
Rajesh Goel | d6f1c9c | 2019-10-06 13:17:36 +0530 | [diff] [blame] | 285 | vlib_node_increment_counter (vm, mpls_output_node.index, |
| 286 | MPLS_ERROR_PKTS_NEED_FRAG, |
| 287 | 1); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 288 | } |
Rajesh Goel | d6f1c9c | 2019-10-06 13:17:36 +0530 | [diff] [blame] | 289 | if (mode == MPLS_OUTPUT_MIDCHAIN_MODE) |
Neale Ranns | b069a69 | 2017-03-15 12:34:25 -0400 | [diff] [blame] | 290 | { |
Neale Ranns | db14f5a | 2018-01-29 10:43:33 -0800 | [diff] [blame] | 291 | adj0->sub_type.midchain.fixup_func |
| 292 | (vm, adj0, p0, |
| 293 | adj0->sub_type.midchain.fixup_data); |
Neale Ranns | b069a69 | 2017-03-15 12:34:25 -0400 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | p0->error = error_node->errors[error0]; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 297 | |
| 298 | from += 1; |
| 299 | n_left_from -= 1; |
| 300 | to_next += 1; |
| 301 | n_left_to_next -= 1; |
Dave Barach | 9cbfb4c | 2020-04-08 10:19:29 -0400 | [diff] [blame] | 302 | |
| 303 | if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED)) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 304 | { |
Dave Barach | 9cbfb4c | 2020-04-08 10:19:29 -0400 | [diff] [blame] | 305 | mpls_output_trace_t *tr = vlib_add_trace (vm, node, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 306 | p0, sizeof (*tr)); |
| 307 | tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX]; |
| 308 | tr->flow_hash = vnet_buffer(p0)->ip.flow_hash; |
| 309 | } |
| 310 | |
| 311 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 312 | to_next, n_left_to_next, |
| 313 | pi0, next0); |
| 314 | } |
| 315 | |
| 316 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 317 | } |
| 318 | vlib_node_increment_counter (vm, mpls_output_node.index, |
| 319 | MPLS_ERROR_PKTS_ENCAP, |
| 320 | from_frame->n_vectors); |
| 321 | |
| 322 | return from_frame->n_vectors; |
| 323 | } |
| 324 | |
| 325 | static char * mpls_error_strings[] = { |
| 326 | #define mpls_error(n,s) s, |
| 327 | #include "error.def" |
| 328 | #undef mpls_error |
| 329 | }; |
| 330 | |
Filip Tehlar | 17fcd98 | 2019-03-05 04:32:11 -0800 | [diff] [blame] | 331 | VLIB_NODE_FN (mpls_output_node) (vlib_main_t * vm, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 332 | vlib_node_runtime_t * node, |
| 333 | vlib_frame_t * from_frame) |
| 334 | { |
Rajesh Goel | d6f1c9c | 2019-10-06 13:17:36 +0530 | [diff] [blame] | 335 | return (mpls_output_inline(vm, node, from_frame, MPLS_OUTPUT_MODE)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | VLIB_REGISTER_NODE (mpls_output_node) = { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 339 | .name = "mpls-output", |
| 340 | /* Takes a vector of packets. */ |
| 341 | .vector_size = sizeof (u32), |
| 342 | .n_errors = MPLS_N_ERROR, |
| 343 | .error_strings = mpls_error_strings, |
| 344 | |
| 345 | .n_next_nodes = MPLS_OUTPUT_N_NEXT, |
| 346 | .next_nodes = { |
Ole Troan | eb284a1 | 2019-10-09 13:33:19 +0200 | [diff] [blame] | 347 | [MPLS_OUTPUT_NEXT_DROP] = "mpls-drop", |
| 348 | [MPLS_OUTPUT_NEXT_FRAG] = "mpls-frag", |
| 349 | }, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 350 | .format_trace = format_mpls_output_trace, |
| 351 | }; |
| 352 | |
Filip Tehlar | 17fcd98 | 2019-03-05 04:32:11 -0800 | [diff] [blame] | 353 | VLIB_NODE_FN (mpls_midchain_node) (vlib_main_t * vm, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 354 | vlib_node_runtime_t * node, |
| 355 | vlib_frame_t * from_frame) |
| 356 | { |
Rajesh Goel | d6f1c9c | 2019-10-06 13:17:36 +0530 | [diff] [blame] | 357 | return (mpls_output_inline(vm, node, from_frame, MPLS_OUTPUT_MIDCHAIN_MODE)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | VLIB_REGISTER_NODE (mpls_midchain_node) = { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 361 | .name = "mpls-midchain", |
| 362 | .vector_size = sizeof (u32), |
| 363 | |
Ole Troan | eb284a1 | 2019-10-09 13:33:19 +0200 | [diff] [blame] | 364 | .n_errors = MPLS_N_ERROR, |
| 365 | .error_strings = mpls_error_strings, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 366 | |
| 367 | .sibling_of = "mpls-output", |
Ole Troan | eb284a1 | 2019-10-09 13:33:19 +0200 | [diff] [blame] | 368 | .format_trace = format_mpls_output_trace, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 369 | }; |
| 370 | |
Ole Troan | eb284a1 | 2019-10-09 13:33:19 +0200 | [diff] [blame] | 371 | static char *mpls_frag_error_strings[] = { |
| 372 | #define _(sym,string) string, |
| 373 | foreach_ip_frag_error |
| 374 | #undef _ |
| 375 | }; |
| 376 | |
| 377 | typedef struct mpls_frag_trace_t_ |
| 378 | { |
| 379 | u16 pkt_size; |
| 380 | u16 mtu; |
| 381 | } mpls_frag_trace_t; |
| 382 | |
| 383 | typedef enum |
| 384 | { |
| 385 | MPLS_FRAG_NEXT_REWRITE, |
| 386 | MPLS_FRAG_NEXT_REWRITE_MIDCHAIN, |
| 387 | MPLS_FRAG_NEXT_ICMP_ERROR, |
| 388 | MPLS_FRAG_NEXT_DROP, |
| 389 | MPLS_FRAG_N_NEXT, |
| 390 | } mpls_frag_next_t; |
| 391 | |
| 392 | static uword |
| 393 | mpls_frag (vlib_main_t * vm, |
| 394 | vlib_node_runtime_t * node, |
| 395 | vlib_frame_t * frame) |
| 396 | { |
| 397 | u32 n_left_from, next_index, * from, * to_next, n_left_to_next, *frags; |
| 398 | vlib_node_runtime_t * error_node; |
| 399 | |
| 400 | error_node = vlib_node_get_runtime (vm, mpls_output_node.index); |
| 401 | from = vlib_frame_vector_args (frame); |
| 402 | n_left_from = frame->n_vectors; |
| 403 | next_index = node->cached_next_index; |
| 404 | frags = NULL; |
| 405 | |
| 406 | while (n_left_from > 0) |
| 407 | { |
| 408 | vlib_get_next_frame (vm, node, next_index, |
| 409 | to_next, n_left_to_next); |
| 410 | |
| 411 | while (n_left_from > 0 && n_left_to_next > 0) |
| 412 | { |
| 413 | ip_adjacency_t * adj0; |
| 414 | vlib_buffer_t * p0; |
| 415 | mpls_frag_next_t next0; |
| 416 | u32 pi0, adj_index0; |
| 417 | ip_frag_error_t error0 = IP_FRAG_ERROR_NONE; |
| 418 | i16 encap_size; |
| 419 | u8 is_ip4; |
| 420 | |
| 421 | pi0 = to_next[0] = from[0]; |
| 422 | p0 = vlib_get_buffer (vm, pi0); |
| 423 | from += 1; |
| 424 | n_left_from -= 1; |
| 425 | is_ip4 = vnet_buffer (p0)->mpls.pyld_proto == DPO_PROTO_IP4; |
| 426 | |
| 427 | adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX]; |
| 428 | adj0 = adj_get(adj_index0); |
| 429 | |
| 430 | /* the size of the MPLS stack */ |
| 431 | encap_size = vnet_buffer(p0)->l3_hdr_offset - p0->current_data; |
Dave Barach | 9cbfb4c | 2020-04-08 10:19:29 -0400 | [diff] [blame] | 432 | |
Ole Troan | eb284a1 | 2019-10-09 13:33:19 +0200 | [diff] [blame] | 433 | /* IP fragmentation */ |
| 434 | if (is_ip4) |
| 435 | error0 = ip4_frag_do_fragment (vm, pi0, |
| 436 | adj0->rewrite_header.max_l3_packet_bytes, |
| 437 | encap_size, &frags); |
| 438 | else |
| 439 | error0 = ip6_frag_do_fragment (vm, pi0, |
| 440 | adj0->rewrite_header.max_l3_packet_bytes, |
| 441 | encap_size, &frags); |
| 442 | |
| 443 | if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED)) |
| 444 | { |
| 445 | mpls_frag_trace_t *tr = |
| 446 | vlib_add_trace (vm, node, p0, sizeof (*tr)); |
| 447 | tr->mtu = adj0->rewrite_header.max_l3_packet_bytes; |
| 448 | tr->pkt_size = vlib_buffer_length_in_chain(vm, p0); |
| 449 | } |
| 450 | |
| 451 | if (PREDICT_TRUE(error0 == IP_FRAG_ERROR_NONE)) |
| 452 | { |
| 453 | /* Free original buffer chain */ |
| 454 | vlib_buffer_free_one (vm, pi0); /* Free original packet */ |
| 455 | next0 = (IP_LOOKUP_NEXT_MIDCHAIN == adj0->lookup_next_index ? |
| 456 | MPLS_FRAG_NEXT_REWRITE_MIDCHAIN : |
| 457 | MPLS_FRAG_NEXT_REWRITE); |
| 458 | } |
| 459 | else if (is_ip4 && error0 == IP_FRAG_ERROR_DONT_FRAGMENT_SET) |
| 460 | { |
| 461 | icmp4_error_set_vnet_buffer ( |
| 462 | p0, ICMP4_destination_unreachable, |
| 463 | ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set, |
| 464 | vnet_buffer (p0)->ip_frag.mtu); |
| 465 | next0 = MPLS_FRAG_NEXT_ICMP_ERROR; |
| 466 | } |
| 467 | else |
| 468 | { |
Dave Barach | 9cbfb4c | 2020-04-08 10:19:29 -0400 | [diff] [blame] | 469 | vlib_error_count (vm, mpls_output_node.index, error0, 1); |
Ole Troan | eb284a1 | 2019-10-09 13:33:19 +0200 | [diff] [blame] | 470 | vec_add1 (frags, pi0); /* Get rid of the original buffer */ |
| 471 | next0 = MPLS_FRAG_NEXT_DROP; |
| 472 | } |
| 473 | |
| 474 | /* Send fragments that were added in the frame */ |
| 475 | u32 *frag_from, frag_left; |
| 476 | |
| 477 | frag_from = frags; |
| 478 | frag_left = vec_len (frags); |
| 479 | |
| 480 | while (frag_left > 0) |
| 481 | { |
| 482 | while (frag_left > 0 && n_left_to_next > 0) |
| 483 | { |
| 484 | u32 i; |
| 485 | i = to_next[0] = frag_from[0]; |
| 486 | frag_from += 1; |
| 487 | frag_left -= 1; |
| 488 | to_next += 1; |
| 489 | n_left_to_next -= 1; |
| 490 | |
| 491 | p0 = vlib_get_buffer (vm, i); |
| 492 | p0->error = error_node->errors[error0]; |
| 493 | |
| 494 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 495 | to_next, n_left_to_next, i, |
| 496 | next0); |
| 497 | } |
| 498 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 499 | vlib_get_next_frame (vm, node, next_index, to_next, |
| 500 | n_left_to_next); |
| 501 | } |
| 502 | vec_reset_length (frags); |
| 503 | } |
| 504 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 505 | } |
| 506 | vec_free (frags); |
| 507 | |
| 508 | return frame->n_vectors; |
| 509 | } |
| 510 | |
| 511 | static u8 * |
| 512 | format_mpls_frag_trace (u8 * s, va_list * args) |
| 513 | { |
| 514 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 515 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 516 | mpls_frag_trace_t *t = va_arg (*args, mpls_frag_trace_t *); |
| 517 | |
| 518 | s = format (s, "mtu:%d pkt-size:%d", t->mtu, t->pkt_size); |
| 519 | return s; |
| 520 | } |
| 521 | |
| 522 | VLIB_REGISTER_NODE (mpls_frag_node) = { |
| 523 | .function = mpls_frag, |
| 524 | .name = "mpls-frag", |
| 525 | .vector_size = sizeof (u32), |
| 526 | .format_trace = format_mpls_frag_trace, |
| 527 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 528 | |
| 529 | .n_errors = IP_FRAG_N_ERROR, |
| 530 | .error_strings = mpls_frag_error_strings, |
| 531 | |
| 532 | .n_next_nodes = MPLS_FRAG_N_NEXT, |
| 533 | .next_nodes = { |
| 534 | [MPLS_FRAG_NEXT_REWRITE] = "mpls-output", |
| 535 | [MPLS_FRAG_NEXT_REWRITE_MIDCHAIN] = "mpls-midchain", |
| 536 | [MPLS_FRAG_NEXT_ICMP_ERROR] = "ip4-icmp-error", |
| 537 | [MPLS_FRAG_NEXT_DROP] = "mpls-drop" |
| 538 | }, |
| 539 | }; |
| 540 | |
| 541 | /* |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 542 | * @brief Next index values from the MPLS incomplete adj node |
| 543 | */ |
| 544 | #define foreach_mpls_adj_incomplete_next \ |
| 545 | _(DROP, "error-drop") \ |
| 546 | _(IP4, "ip4-arp") \ |
| 547 | _(IP6, "ip6-discover-neighbor") |
| 548 | |
| 549 | typedef enum { |
| 550 | #define _(s,n) MPLS_ADJ_INCOMPLETE_NEXT_##s, |
| 551 | foreach_mpls_adj_incomplete_next |
| 552 | #undef _ |
| 553 | MPLS_ADJ_INCOMPLETE_N_NEXT, |
| 554 | } mpls_adj_incomplete_next_t; |
| 555 | |
| 556 | /** |
| 557 | * @brief A struct to hold tracing information for the MPLS label imposition |
| 558 | * node. |
| 559 | */ |
| 560 | typedef struct mpls_adj_incomplete_trace_t_ |
| 561 | { |
| 562 | u32 next; |
| 563 | } mpls_adj_incomplete_trace_t; |
| 564 | |
| 565 | |
| 566 | /** |
| 567 | * @brief Graph node for incomplete MPLS adjacency. |
| 568 | * This node will push traffic to either the v4-arp or v6-nd node |
| 569 | * based on the next-hop proto of the adj. |
| 570 | * We pay a cost for this 'routing' node, but an incomplete adj is the |
| 571 | * exception case. |
| 572 | */ |
Filip Tehlar | 17fcd98 | 2019-03-05 04:32:11 -0800 | [diff] [blame] | 573 | VLIB_NODE_FN (mpls_adj_incomplete_node) (vlib_main_t * vm, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 574 | vlib_node_runtime_t * node, |
| 575 | vlib_frame_t * from_frame) |
| 576 | { |
| 577 | u32 n_left_from, next_index, * from, * to_next; |
| 578 | |
| 579 | from = vlib_frame_vector_args (from_frame); |
| 580 | n_left_from = from_frame->n_vectors; |
| 581 | next_index = node->cached_next_index; |
| 582 | |
| 583 | while (n_left_from > 0) |
| 584 | { |
| 585 | u32 n_left_to_next; |
| 586 | |
| 587 | vlib_get_next_frame (vm, node, next_index, |
| 588 | to_next, n_left_to_next); |
| 589 | |
| 590 | while (n_left_from > 0 && n_left_to_next > 0) |
| 591 | { |
| 592 | u32 pi0, next0, adj_index0; |
| 593 | ip_adjacency_t * adj0; |
| 594 | vlib_buffer_t * p0; |
| 595 | |
| 596 | pi0 = to_next[0] = from[0]; |
| 597 | p0 = vlib_get_buffer (vm, pi0); |
| 598 | from += 1; |
| 599 | n_left_from -= 1; |
| 600 | to_next += 1; |
| 601 | n_left_to_next -= 1; |
| 602 | |
| 603 | adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX]; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 604 | |
| 605 | adj0 = adj_get(adj_index0); |
| 606 | |
| 607 | if (PREDICT_TRUE(FIB_PROTOCOL_IP4 == adj0->ia_nh_proto)) |
| 608 | { |
| 609 | next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP4; |
| 610 | } |
| 611 | else |
| 612 | { |
| 613 | next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP6; |
Dave Barach | 9cbfb4c | 2020-04-08 10:19:29 -0400 | [diff] [blame] | 614 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 615 | |
Dave Barach | 9cbfb4c | 2020-04-08 10:19:29 -0400 | [diff] [blame] | 616 | if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED)) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 617 | { |
| 618 | mpls_adj_incomplete_trace_t *tr = |
| 619 | vlib_add_trace (vm, node, p0, sizeof (*tr)); |
| 620 | tr->next = next0; |
| 621 | } |
| 622 | |
| 623 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 624 | to_next, n_left_to_next, |
| 625 | pi0, next0); |
| 626 | } |
| 627 | |
| 628 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 629 | } |
| 630 | |
| 631 | return from_frame->n_vectors; |
| 632 | } |
| 633 | |
| 634 | static u8 * |
| 635 | format_mpls_adj_incomplete_trace (u8 * s, va_list * args) |
| 636 | { |
| 637 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 638 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 639 | mpls_adj_incomplete_trace_t * t; |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 640 | u32 indent; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 641 | |
| 642 | t = va_arg (*args, mpls_adj_incomplete_trace_t *); |
| 643 | indent = format_get_indent (s); |
| 644 | |
| 645 | s = format (s, "%Unext:%d", |
| 646 | format_white_space, indent, |
| 647 | t->next); |
| 648 | return (s); |
| 649 | } |
| 650 | |
| 651 | VLIB_REGISTER_NODE (mpls_adj_incomplete_node) = { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 652 | .name = "mpls-adj-incomplete", |
| 653 | .format_trace = format_mpls_adj_incomplete_trace, |
| 654 | /* Takes a vector of packets. */ |
| 655 | .vector_size = sizeof (u32), |
| 656 | .n_errors = MPLS_N_ERROR, |
| 657 | .error_strings = mpls_error_strings, |
| 658 | |
| 659 | .n_next_nodes = MPLS_ADJ_INCOMPLETE_N_NEXT, |
| 660 | .next_nodes = { |
| 661 | #define _(s,n) [MPLS_ADJ_INCOMPLETE_NEXT_##s] = n, |
| 662 | foreach_mpls_adj_incomplete_next |
| 663 | #undef _ |
| 664 | }, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 665 | }; |