Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +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 <vlib/vlib.h> |
| 17 | #include <vnet/vnet.h> |
| 18 | #include <vppinfra/error.h> |
| 19 | |
| 20 | #include <vnet/span/span.h> |
| 21 | |
| 22 | #include <vppinfra/error.h> |
| 23 | #include <vppinfra/elog.h> |
| 24 | |
| 25 | vlib_node_registration_t span_node; |
| 26 | |
| 27 | /* packet trace format function */ |
| 28 | u8 * |
| 29 | format_span_trace (u8 * s, va_list * args) |
| 30 | { |
| 31 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 32 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 33 | span_trace_t *t = va_arg (*args, span_trace_t *); |
| 34 | |
| 35 | vnet_main_t *vnm = &vnet_main; |
| 36 | s = format (s, "SPAN: mirrored %U -> %U", |
| 37 | format_vnet_sw_if_index_name, vnm, t->src_sw_if_index, |
| 38 | format_vnet_sw_if_index_name, vnm, t->mirror_sw_if_index); |
| 39 | |
| 40 | return s; |
| 41 | } |
| 42 | |
| 43 | #define foreach_span_error \ |
| 44 | _(HITS, "SPAN incomming packets processed") |
| 45 | |
| 46 | typedef enum |
| 47 | { |
| 48 | #define _(sym,str) SPAN_ERROR_##sym, |
| 49 | foreach_span_error |
| 50 | #undef _ |
| 51 | SPAN_N_ERROR, |
| 52 | } span_error_t; |
| 53 | |
| 54 | static char *span_error_strings[] = { |
| 55 | #define _(sym,string) string, |
| 56 | foreach_span_error |
| 57 | #undef _ |
| 58 | }; |
| 59 | |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 60 | static_always_inline void |
| 61 | span_mirror (vlib_main_t * vm, span_interface_t * si0, vlib_buffer_t * b0, |
| 62 | vlib_frame_t ** mirror_frames, int is_rx) |
| 63 | { |
| 64 | vlib_buffer_t *c0; |
| 65 | vnet_main_t *vnm = &vnet_main; |
| 66 | u32 *to_mirror_next = 0; |
| 67 | u32 i; |
| 68 | |
| 69 | if (is_rx != 0 && si0->num_rx_mirror_ports == 0) |
| 70 | return; |
| 71 | |
| 72 | if (is_rx == 0 && si0->num_tx_mirror_ports == 0) |
| 73 | return; |
| 74 | |
| 75 | /* Don't do it again */ |
| 76 | if (PREDICT_FALSE (b0->flags & VNET_BUFFER_SPAN_CLONE)) |
| 77 | return; |
| 78 | |
| 79 | /* *INDENT-OFF* */ |
| 80 | clib_bitmap_foreach (i, is_rx ? si0->rx_mirror_ports : si0->tx_mirror_ports, ( |
| 81 | { |
| 82 | if (mirror_frames[i] == 0) |
| 83 | mirror_frames[i] = vnet_get_frame_to_sw_interface (vnm, i); |
| 84 | to_mirror_next = vlib_frame_vector_args (mirror_frames[i]); |
| 85 | to_mirror_next += mirror_frames[i]->n_vectors; |
Dave Barach | 26cd8c1 | 2017-02-23 17:11:26 -0500 | [diff] [blame] | 86 | /* This can fail */ |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 87 | c0 = vlib_buffer_copy (vm, b0); |
Dave Barach | 26cd8c1 | 2017-02-23 17:11:26 -0500 | [diff] [blame] | 88 | if (PREDICT_TRUE(c0 != 0)) |
| 89 | { |
| 90 | vnet_buffer (c0)->sw_if_index[VLIB_TX] = i; |
| 91 | c0->flags |= VNET_BUFFER_SPAN_CLONE; |
| 92 | to_mirror_next[0] = vlib_get_buffer_index (vm, c0); |
| 93 | mirror_frames[i]->n_vectors++; |
| 94 | } |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 95 | })); |
| 96 | /* *INDENT-ON* */ |
| 97 | } |
| 98 | |
| 99 | static_always_inline uword |
| 100 | span_node_inline_fn (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 101 | vlib_frame_t * frame, int is_rx) |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 102 | { |
| 103 | span_main_t *sm = &span_main; |
| 104 | vnet_main_t *vnm = &vnet_main; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 105 | u32 n_left_from, *from, *to_next; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 106 | u32 n_span_packets = 0; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 107 | u32 next_index; |
| 108 | u32 sw_if_index; |
| 109 | static __thread vlib_frame_t **mirror_frames = 0; |
| 110 | vlib_rx_or_tx_t rxtx = is_rx ? VLIB_RX : VLIB_TX; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 111 | |
| 112 | from = vlib_frame_vector_args (frame); |
| 113 | n_left_from = frame->n_vectors; |
| 114 | next_index = node->cached_next_index; |
| 115 | |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 116 | vec_validate_aligned (mirror_frames, sm->max_sw_if_index, |
| 117 | CLIB_CACHE_LINE_BYTES); |
| 118 | |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 119 | while (n_left_from > 0) |
| 120 | { |
| 121 | u32 n_left_to_next; |
| 122 | |
| 123 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 124 | |
| 125 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 126 | { |
| 127 | u32 bi0; |
| 128 | u32 bi1; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 129 | vlib_buffer_t *b0; |
| 130 | vlib_buffer_t *b1; |
| 131 | span_interface_t *si0, *si1; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 132 | u32 sw_if_index0; |
Pavel Kotucek | e2e95ce | 2016-11-29 11:03:37 +0100 | [diff] [blame] | 133 | u32 next0 = 0; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 134 | u32 sw_if_index1; |
Pavel Kotucek | e2e95ce | 2016-11-29 11:03:37 +0100 | [diff] [blame] | 135 | u32 next1 = 0; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 136 | |
| 137 | /* speculatively enqueue b0, b1 to the current next frame */ |
| 138 | to_next[0] = bi0 = from[0]; |
| 139 | to_next[1] = bi1 = from[1]; |
| 140 | to_next += 2; |
| 141 | n_left_to_next -= 2; |
| 142 | from += 2; |
| 143 | n_left_from -= 2; |
| 144 | |
| 145 | b0 = vlib_get_buffer (vm, bi0); |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 146 | b1 = vlib_get_buffer (vm, bi1); |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 147 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[rxtx]; |
| 148 | sw_if_index1 = vnet_buffer (b1)->sw_if_index[rxtx]; |
| 149 | si0 = vec_elt_at_index (sm->interfaces, sw_if_index0); |
| 150 | si1 = vec_elt_at_index (sm->interfaces, sw_if_index1); |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 151 | |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 152 | span_mirror (vm, si0, b0, mirror_frames, is_rx); |
| 153 | span_mirror (vm, si1, b1, mirror_frames, is_rx); |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 154 | |
| 155 | vnet_feature_next (sw_if_index0, &next0, b0); |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 156 | vnet_feature_next (sw_if_index1, &next1, b1); |
| 157 | |
| 158 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 159 | { |
| 160 | span_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 161 | t->src_sw_if_index = sw_if_index0; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 162 | //t->mirror_sw_if_index = si0->mirror_sw_if_index; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 166 | { |
| 167 | span_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t)); |
| 168 | t->src_sw_if_index = sw_if_index1; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 169 | //t->mirror_sw_if_index = si1->mirror_sw_if_index; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 170 | } |
| 171 | /* verify speculative enqueue, maybe switch current next frame */ |
| 172 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, |
| 173 | to_next, n_left_to_next, |
| 174 | bi0, bi1, next0, next1); |
| 175 | } |
| 176 | while (n_left_from > 0 && n_left_to_next > 0) |
| 177 | { |
| 178 | u32 bi0; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 179 | vlib_buffer_t *b0; |
| 180 | span_interface_t *si0; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 181 | u32 sw_if_index0; |
Pavel Kotucek | e2e95ce | 2016-11-29 11:03:37 +0100 | [diff] [blame] | 182 | u32 next0 = 0; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 183 | |
| 184 | /* speculatively enqueue b0 to the current next frame */ |
| 185 | to_next[0] = bi0 = from[0]; |
| 186 | to_next += 1; |
| 187 | n_left_to_next -= 1; |
| 188 | from += 1; |
| 189 | n_left_from -= 1; |
| 190 | |
| 191 | b0 = vlib_get_buffer (vm, bi0); |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 192 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[rxtx]; |
| 193 | si0 = vec_elt_at_index (sm->interfaces, sw_if_index0); |
| 194 | span_mirror (vm, si0, b0, mirror_frames, is_rx); |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 195 | |
| 196 | vnet_feature_next (sw_if_index0, &next0, b0); |
| 197 | |
| 198 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 199 | { |
| 200 | span_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 201 | t->src_sw_if_index = sw_if_index0; |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 202 | } |
| 203 | /* verify speculative enqueue, maybe switch current next frame */ |
| 204 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 205 | n_left_to_next, bi0, next0); |
| 206 | } |
| 207 | |
| 208 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 209 | } |
| 210 | |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 211 | |
| 212 | for (sw_if_index = 0; sw_if_index < vec_len (mirror_frames); sw_if_index++) |
| 213 | { |
| 214 | if (mirror_frames[sw_if_index] == 0) |
| 215 | continue; |
| 216 | |
| 217 | vnet_put_frame_to_sw_interface (vnm, sw_if_index, |
| 218 | mirror_frames[sw_if_index]); |
| 219 | mirror_frames[sw_if_index] = 0; |
| 220 | } |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 221 | vlib_node_increment_counter (vm, span_node.index, SPAN_ERROR_HITS, |
| 222 | n_span_packets); |
| 223 | |
| 224 | return frame->n_vectors; |
| 225 | } |
| 226 | |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 227 | static uword |
| 228 | span_input_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 229 | vlib_frame_t * frame) |
| 230 | { |
| 231 | return span_node_inline_fn (vm, node, frame, 1); |
| 232 | } |
| 233 | |
| 234 | static uword |
| 235 | span_output_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 236 | vlib_frame_t * frame) |
| 237 | { |
| 238 | return span_node_inline_fn (vm, node, frame, 0); |
| 239 | } |
| 240 | |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 241 | /* *INDENT-OFF* */ |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 242 | VLIB_REGISTER_NODE (span_input_node) = { |
| 243 | .function = span_input_node_fn, |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 244 | .name = "span-input", |
| 245 | .vector_size = sizeof (u32), |
| 246 | .format_trace = format_span_trace, |
| 247 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 248 | |
| 249 | .n_errors = ARRAY_LEN(span_error_strings), |
| 250 | .error_strings = span_error_strings, |
| 251 | |
| 252 | .n_next_nodes = 0, |
| 253 | |
| 254 | /* edit / add dispositions here */ |
| 255 | .next_nodes = { |
| 256 | [0] = "error-drop", |
| 257 | }, |
| 258 | }; |
| 259 | |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 260 | VLIB_NODE_FUNCTION_MULTIARCH (span_input_node, span_input_node_fn) |
| 261 | |
| 262 | VLIB_REGISTER_NODE (span_output_node) = { |
| 263 | .function = span_output_node_fn, |
| 264 | .name = "span-output", |
| 265 | .vector_size = sizeof (u32), |
| 266 | .format_trace = format_span_trace, |
| 267 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 268 | |
| 269 | .n_errors = ARRAY_LEN(span_error_strings), |
| 270 | .error_strings = span_error_strings, |
| 271 | |
| 272 | .n_next_nodes = 0, |
| 273 | |
| 274 | /* edit / add dispositions here */ |
| 275 | .next_nodes = { |
| 276 | [0] = "error-drop", |
| 277 | }, |
| 278 | }; |
| 279 | |
| 280 | VLIB_NODE_FUNCTION_MULTIARCH (span_output_node, span_output_node_fn) |
| 281 | |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 282 | /* *INDENT-ON* */ |
| 283 | |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 284 | /* |
| 285 | * fd.io coding-style-patch-verification: ON |
| 286 | * |
| 287 | * Local Variables: |
| 288 | * eval: (c-set-style "gnu") |
| 289 | * End: |
| 290 | */ |