Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 1 | /* |
| 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: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 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. |
| 14 | */ |
| 15 | |
| 16 | #include <vnet/vnet.h> |
| 17 | #include <vnet/adj/adj_l2.h> |
| 18 | #include <vnet/ethernet/ethernet.h> |
| 19 | #include <vnet/ip/ip.h> |
| 20 | |
| 21 | /** |
| 22 | * @brief Trace data for a L2 Midchain |
| 23 | */ |
| 24 | typedef struct adj_l2_trace_t_ { |
| 25 | /** Adjacency index taken. */ |
| 26 | u32 adj_index; |
| 27 | } adj_l2_trace_t; |
| 28 | |
| 29 | static u8 * |
| 30 | format_adj_l2_trace (u8 * s, va_list * args) |
| 31 | { |
| 32 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 33 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 34 | adj_l2_trace_t * t = va_arg (*args, adj_l2_trace_t *); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 35 | |
| 36 | s = format (s, "adj-idx %d : %U", |
| 37 | t->adj_index, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 38 | format_ip_adjacency, t->adj_index, FORMAT_IP_ADJACENCY_NONE); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 39 | return s; |
| 40 | } |
| 41 | |
| 42 | typedef enum adj_l2_rewrite_next_t_ |
| 43 | { |
| 44 | ADJ_L2_REWRITE_NEXT_DROP, |
| 45 | } adj_l2_rewrite_next_t; |
| 46 | |
| 47 | always_inline uword |
| 48 | adj_l2_rewrite_inline (vlib_main_t * vm, |
| 49 | vlib_node_runtime_t * node, |
| 50 | vlib_frame_t * frame, |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 51 | int is_midchain, |
| 52 | int do_counters) |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 53 | { |
| 54 | u32 * from = vlib_frame_vector_args (frame); |
| 55 | u32 n_left_from, n_left_to_next, * to_next, next_index; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 56 | u32 thread_index = vlib_get_thread_index(); |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 57 | ethernet_main_t * em = ðernet_main; |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 58 | |
| 59 | n_left_from = frame->n_vectors; |
| 60 | next_index = node->cached_next_index; |
| 61 | |
| 62 | while (n_left_from > 0) |
| 63 | { |
| 64 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 65 | |
| 66 | while (n_left_from > 0 && n_left_to_next > 0) |
| 67 | { |
| 68 | ip_adjacency_t * adj0; |
| 69 | vlib_buffer_t * p0; |
| 70 | char *h0; |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 71 | u32 pi0, rw_len0, len0, adj_index0, next0 = 0; |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 72 | u32 tx_sw_if_index0; |
| 73 | |
| 74 | pi0 = to_next[0] = from[0]; |
| 75 | from += 1; |
| 76 | n_left_from -= 1; |
| 77 | to_next += 1; |
| 78 | n_left_to_next -= 1; |
| 79 | |
| 80 | p0 = vlib_get_buffer (vm, pi0); |
| 81 | h0 = vlib_buffer_get_current (p0); |
| 82 | |
| 83 | adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX]; |
| 84 | |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 85 | adj0 = adj_get (adj_index0); |
| 86 | |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 87 | /* Guess we are writing on ip4 header. */ |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 88 | vnet_rewrite_one_header (adj0[0], h0, |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 89 | //sizeof (gre_header_t) + |
| 90 | sizeof (ip4_header_t)); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 91 | |
| 92 | /* Update packet buffer attributes/set output interface. */ |
| 93 | rw_len0 = adj0[0].rewrite_header.data_bytes; |
| 94 | vnet_buffer(p0)->ip.save_rewrite_length = rw_len0; |
Filip Tehlar | 4868ff6 | 2017-03-09 16:48:39 +0100 | [diff] [blame] | 95 | vnet_buffer(p0)->sw_if_index[VLIB_TX] = adj0->rewrite_header.sw_if_index; |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 96 | len0 = vlib_buffer_length_in_chain (vm, p0); |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 97 | /* since we are coming out of the L2 world, where the vlib_buffer |
| 98 | * union is used for other things, make sure it is clean for |
| 99 | * MPLS from now on. |
| 100 | */ |
| 101 | vnet_buffer(p0)->mpls.first = 0; |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 102 | |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 103 | if (do_counters) |
| 104 | vlib_increment_combined_counter(&adjacency_counters, |
| 105 | thread_index, |
| 106 | adj_index0, |
| 107 | 0, len0); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 108 | |
| 109 | /* Check MTU of outgoing interface. */ |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 110 | if (PREDICT_TRUE(len0 <= adj0[0].rewrite_header.max_l3_packet_bytes)) |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 111 | { |
| 112 | /* Don't adjust the buffer for ttl issue; icmp-error node wants |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 113 | * to see the IP header */ |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 114 | p0->current_data -= rw_len0; |
| 115 | p0->current_length += rw_len0; |
| 116 | tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index; |
| 117 | |
| 118 | if (is_midchain) |
| 119 | { |
Neale Ranns | db14f5a | 2018-01-29 10:43:33 -0800 | [diff] [blame] | 120 | adj0->sub_type.midchain.fixup_func( |
| 121 | vm, adj0, p0, |
| 122 | adj0->sub_type.midchain.fixup_data); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0; |
| 126 | |
| 127 | /* |
| 128 | * Follow the feature ARC. this will result eventually in |
| 129 | * the midchain-tx node |
| 130 | */ |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 131 | if (PREDICT_FALSE (adj0->rewrite_header.flags & |
| 132 | VNET_REWRITE_HAS_FEATURES)) |
| 133 | vnet_feature_arc_start_w_cfg_index ( |
| 134 | em->output_feature_arc_index, |
| 135 | tx_sw_if_index0, |
| 136 | &next0, p0, |
| 137 | adj0->ia_cfg_index); |
| 138 | else |
| 139 | next0 = adj0[0].rewrite_header.next_index; |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 140 | } |
| 141 | else |
| 142 | { |
| 143 | /* can't fragment L2 */ |
| 144 | next0 = ADJ_L2_REWRITE_NEXT_DROP; |
| 145 | } |
| 146 | |
| 147 | if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED)) |
| 148 | { |
| 149 | adj_l2_trace_t *tr = vlib_add_trace (vm, node, |
| 150 | p0, sizeof (*tr)); |
| 151 | tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX]; |
| 152 | } |
| 153 | |
| 154 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 155 | to_next, n_left_to_next, |
| 156 | pi0, next0); |
| 157 | } |
| 158 | |
| 159 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 160 | } |
| 161 | |
| 162 | return frame->n_vectors; |
| 163 | } |
| 164 | |
Filip Tehlar | 0b2c15d | 2019-03-04 02:52:54 -0800 | [diff] [blame] | 165 | VLIB_NODE_FN (adj_l2_rewrite_node) (vlib_main_t * vm, |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 166 | vlib_node_runtime_t * node, |
| 167 | vlib_frame_t * frame) |
| 168 | { |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 169 | if (adj_are_counters_enabled ()) |
| 170 | return adj_l2_rewrite_inline (vm, node, frame, 0, 1); |
| 171 | else |
| 172 | return adj_l2_rewrite_inline (vm, node, frame, 0, 0); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Filip Tehlar | 0b2c15d | 2019-03-04 02:52:54 -0800 | [diff] [blame] | 175 | VLIB_NODE_FN (adj_l2_midchain_node) (vlib_main_t * vm, |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 176 | vlib_node_runtime_t * node, |
| 177 | vlib_frame_t * frame) |
| 178 | { |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 179 | if (adj_are_counters_enabled ()) |
| 180 | return adj_l2_rewrite_inline (vm, node, frame, 1, 1); |
| 181 | else |
| 182 | return adj_l2_rewrite_inline (vm, node, frame, 1, 0); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | VLIB_REGISTER_NODE (adj_l2_rewrite_node) = { |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 186 | .name = "adj-l2-rewrite", |
| 187 | .vector_size = sizeof (u32), |
| 188 | |
| 189 | .format_trace = format_adj_l2_trace, |
| 190 | |
| 191 | .n_next_nodes = 1, |
| 192 | .next_nodes = { |
| 193 | [ADJ_L2_REWRITE_NEXT_DROP] = "error-drop", |
| 194 | }, |
| 195 | }; |
| 196 | |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 197 | VLIB_REGISTER_NODE (adj_l2_midchain_node) = { |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 198 | .name = "adj-l2-midchain", |
| 199 | .vector_size = sizeof (u32), |
| 200 | |
| 201 | .format_trace = format_adj_l2_trace, |
| 202 | |
| 203 | .n_next_nodes = 1, |
| 204 | .next_nodes = { |
| 205 | [ADJ_L2_REWRITE_NEXT_DROP] = "error-drop", |
| 206 | }, |
| 207 | }; |