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