Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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 | #include <vlib/vlib.h> |
| 16 | #include <vnet/vnet.h> |
| 17 | #include <vnet/pg/pg.h> |
| 18 | #include <vppinfra/error.h> |
| 19 | #include <sample/sample.h> |
| 20 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 21 | typedef struct |
| 22 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 23 | u32 next_index; |
| 24 | u32 sw_if_index; |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 25 | u8 new_src_mac[6]; |
| 26 | u8 new_dst_mac[6]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 27 | } sample_trace_t; |
| 28 | |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 29 | static u8 * |
| 30 | format_mac_address (u8 * s, va_list * args) |
| 31 | { |
| 32 | u8 *a = va_arg (*args, u8 *); |
| 33 | return format (s, "%02x:%02x:%02x:%02x:%02x:%02x", |
| 34 | a[0], a[1], a[2], a[3], a[4], a[5]); |
| 35 | } |
| 36 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 37 | /* packet trace format function */ |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 38 | static u8 * |
| 39 | format_sample_trace (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 40 | { |
| 41 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 42 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 43 | sample_trace_t *t = va_arg (*args, sample_trace_t *); |
| 44 | |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 45 | s = format (s, "SAMPLE: sw_if_index %d, next index %d\n", |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 46 | t->sw_if_index, t->next_index); |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 47 | s = format (s, " new src %U -> new dst %U", |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 48 | format_mac_address, t->new_src_mac, |
| 49 | format_mac_address, t->new_dst_mac); |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 50 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | return s; |
| 52 | } |
| 53 | |
| 54 | vlib_node_registration_t sample_node; |
| 55 | |
| 56 | #define foreach_sample_error \ |
| 57 | _(SWAPPED, "Mac swap packets processed") |
| 58 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 59 | typedef enum |
| 60 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | #define _(sym,str) SAMPLE_ERROR_##sym, |
| 62 | foreach_sample_error |
| 63 | #undef _ |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 64 | SAMPLE_N_ERROR, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | } sample_error_t; |
| 66 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 67 | static char *sample_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | #define _(sym,string) string, |
| 69 | foreach_sample_error |
| 70 | #undef _ |
| 71 | }; |
| 72 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 73 | typedef enum |
| 74 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | SAMPLE_NEXT_INTERFACE_OUTPUT, |
| 76 | SAMPLE_N_NEXT, |
| 77 | } sample_next_t; |
| 78 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 79 | /* |
| 80 | * Simple dual/single loop version, default version which will compile |
| 81 | * everywhere. |
| 82 | * |
| 83 | * Node costs 30 clocks/pkt at a vector size of 51 |
| 84 | */ |
| 85 | #define VERSION_1 1 |
| 86 | |
| 87 | #ifdef VERSION_1 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | #define foreach_mac_address_offset \ |
| 89 | _(0) \ |
| 90 | _(1) \ |
| 91 | _(2) \ |
| 92 | _(3) \ |
| 93 | _(4) \ |
| 94 | _(5) |
| 95 | |
| 96 | static uword |
| 97 | sample_node_fn (vlib_main_t * vm, |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 98 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | { |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 100 | u32 n_left_from, *from, *to_next; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | sample_next_t next_index; |
| 102 | u32 pkts_swapped = 0; |
| 103 | |
| 104 | from = vlib_frame_vector_args (frame); |
| 105 | n_left_from = frame->n_vectors; |
| 106 | next_index = node->cached_next_index; |
| 107 | |
| 108 | while (n_left_from > 0) |
| 109 | { |
| 110 | u32 n_left_to_next; |
| 111 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 112 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | |
| 114 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 115 | { |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 116 | u32 next0 = SAMPLE_NEXT_INTERFACE_OUTPUT; |
| 117 | u32 next1 = SAMPLE_NEXT_INTERFACE_OUTPUT; |
| 118 | u32 sw_if_index0, sw_if_index1; |
| 119 | u8 tmp0[6], tmp1[6]; |
| 120 | ethernet_header_t *en0, *en1; |
| 121 | u32 bi0, bi1; |
| 122 | vlib_buffer_t *b0, *b1; |
| 123 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | /* Prefetch next iteration. */ |
| 125 | { |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 126 | vlib_buffer_t *p2, *p3; |
| 127 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | p2 = vlib_get_buffer (vm, from[2]); |
| 129 | p3 = vlib_get_buffer (vm, from[3]); |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 130 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 131 | vlib_prefetch_buffer_header (p2, LOAD); |
| 132 | vlib_prefetch_buffer_header (p3, LOAD); |
| 133 | |
| 134 | CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 135 | CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 136 | } |
| 137 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 138 | /* speculatively enqueue b0 and b1 to the current next frame */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | to_next[0] = bi0 = from[0]; |
| 140 | to_next[1] = bi1 = from[1]; |
| 141 | from += 2; |
| 142 | to_next += 2; |
| 143 | n_left_from -= 2; |
| 144 | n_left_to_next -= 2; |
| 145 | |
| 146 | b0 = vlib_get_buffer (vm, bi0); |
| 147 | b1 = vlib_get_buffer (vm, bi1); |
| 148 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 149 | ASSERT (b0->current_data == 0); |
| 150 | ASSERT (b1->current_data == 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 152 | en0 = vlib_buffer_get_current (b0); |
| 153 | en1 = vlib_buffer_get_current (b1); |
| 154 | |
| 155 | /* This is not the fastest way to swap src + dst mac addresses */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 156 | #define _(a) tmp0[a] = en0->src_address[a]; |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 157 | foreach_mac_address_offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | #undef _ |
| 159 | #define _(a) en0->src_address[a] = en0->dst_address[a]; |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 160 | foreach_mac_address_offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | #undef _ |
| 162 | #define _(a) en0->dst_address[a] = tmp0[a]; |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 163 | foreach_mac_address_offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | #undef _ |
| 165 | |
| 166 | #define _(a) tmp1[a] = en1->src_address[a]; |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 167 | foreach_mac_address_offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | #undef _ |
| 169 | #define _(a) en1->src_address[a] = en1->dst_address[a]; |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 170 | foreach_mac_address_offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | #undef _ |
| 172 | #define _(a) en1->dst_address[a] = tmp1[a]; |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 173 | foreach_mac_address_offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 174 | #undef _ |
| 175 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 176 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 177 | sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 179 | /* Send pkt back out the RX interface */ |
| 180 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0; |
| 181 | vnet_buffer (b1)->sw_if_index[VLIB_TX] = sw_if_index1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 183 | pkts_swapped += 2; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 184 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 185 | if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE))) |
| 186 | { |
| 187 | if (b0->flags & VLIB_BUFFER_IS_TRACED) |
| 188 | { |
| 189 | sample_trace_t *t = |
| 190 | vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 191 | t->sw_if_index = sw_if_index0; |
| 192 | t->next_index = next0; |
| 193 | clib_memcpy (t->new_src_mac, en0->src_address, |
| 194 | sizeof (t->new_src_mac)); |
| 195 | clib_memcpy (t->new_dst_mac, en0->dst_address, |
| 196 | sizeof (t->new_dst_mac)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 198 | } |
| 199 | if (b1->flags & VLIB_BUFFER_IS_TRACED) |
| 200 | { |
| 201 | sample_trace_t *t = |
| 202 | vlib_add_trace (vm, node, b1, sizeof (*t)); |
| 203 | t->sw_if_index = sw_if_index1; |
| 204 | t->next_index = next1; |
| 205 | clib_memcpy (t->new_src_mac, en1->src_address, |
| 206 | sizeof (t->new_src_mac)); |
| 207 | clib_memcpy (t->new_dst_mac, en1->dst_address, |
| 208 | sizeof (t->new_dst_mac)); |
| 209 | } |
| 210 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 211 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 212 | /* verify speculative enqueues, maybe switch current next frame */ |
| 213 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, |
| 214 | to_next, n_left_to_next, |
| 215 | bi0, bi1, next0, next1); |
| 216 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 217 | |
| 218 | while (n_left_from > 0 && n_left_to_next > 0) |
| 219 | { |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 220 | u32 bi0; |
| 221 | vlib_buffer_t *b0; |
| 222 | u32 next0 = SAMPLE_NEXT_INTERFACE_OUTPUT; |
| 223 | u32 sw_if_index0; |
| 224 | u8 tmp0[6]; |
| 225 | ethernet_header_t *en0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 226 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 227 | /* speculatively enqueue b0 to the current next frame */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | bi0 = from[0]; |
| 229 | to_next[0] = bi0; |
| 230 | from += 1; |
| 231 | to_next += 1; |
| 232 | n_left_from -= 1; |
| 233 | n_left_to_next -= 1; |
| 234 | |
| 235 | b0 = vlib_get_buffer (vm, bi0); |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 236 | /* |
| 237 | * Direct from the driver, we should be at offset 0 |
| 238 | * aka at &b0->data[0] |
| 239 | */ |
| 240 | ASSERT (b0->current_data == 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 242 | en0 = vlib_buffer_get_current (b0); |
| 243 | |
| 244 | /* This is not the fastest way to swap src + dst mac addresses */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 245 | #define _(a) tmp0[a] = en0->src_address[a]; |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 246 | foreach_mac_address_offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 247 | #undef _ |
| 248 | #define _(a) en0->src_address[a] = en0->dst_address[a]; |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 249 | foreach_mac_address_offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 250 | #undef _ |
| 251 | #define _(a) en0->dst_address[a] = tmp0[a]; |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 252 | foreach_mac_address_offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 253 | #undef _ |
| 254 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 255 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 256 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 257 | /* Send pkt back out the RX interface */ |
| 258 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 259 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 260 | if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) |
| 261 | && (b0->flags & VLIB_BUFFER_IS_TRACED))) |
| 262 | { |
| 263 | sample_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 264 | t->sw_if_index = sw_if_index0; |
| 265 | t->next_index = next0; |
| 266 | clib_memcpy (t->new_src_mac, en0->src_address, |
| 267 | sizeof (t->new_src_mac)); |
| 268 | clib_memcpy (t->new_dst_mac, en0->dst_address, |
| 269 | sizeof (t->new_dst_mac)); |
| 270 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 271 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 272 | pkts_swapped += 1; |
| 273 | |
| 274 | /* verify speculative enqueue, maybe switch current next frame */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 275 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 276 | to_next, n_left_to_next, |
| 277 | bi0, next0); |
| 278 | } |
| 279 | |
| 280 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 281 | } |
| 282 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 283 | vlib_node_increment_counter (vm, sample_node.index, |
| 284 | SAMPLE_ERROR_SWAPPED, pkts_swapped); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 285 | return frame->n_vectors; |
| 286 | } |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 287 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 288 | |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 289 | /* |
| 290 | * This version swaps mac addresses using an MMX vector shuffle |
| 291 | * Node costs about 17 clocks/pkt at a vector size of 26 |
| 292 | */ |
| 293 | #ifdef VERSION_2 |
| 294 | static uword |
| 295 | sample_node_fn (vlib_main_t * vm, |
| 296 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 297 | { |
| 298 | u32 n_left_from, *from, *to_next; |
| 299 | sample_next_t next_index; |
| 300 | u32 pkts_swapped = 0; |
| 301 | /* Vector shuffle mask to swap src, dst */ |
| 302 | u8x16 swapmac = { 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 12, 13, 14, 15 }; |
| 303 | |
| 304 | from = vlib_frame_vector_args (frame); |
| 305 | n_left_from = frame->n_vectors; |
| 306 | next_index = node->cached_next_index; |
| 307 | |
| 308 | while (n_left_from > 0) |
| 309 | { |
| 310 | u32 n_left_to_next; |
| 311 | |
| 312 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 313 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 314 | { |
| 315 | u32 next0 = SAMPLE_NEXT_INTERFACE_OUTPUT; |
| 316 | u32 next1 = SAMPLE_NEXT_INTERFACE_OUTPUT; |
| 317 | u32 sw_if_index0, sw_if_index1; |
| 318 | u8x16 src_dst0, src_dst1; |
| 319 | ethernet_header_t *en0, *en1; |
| 320 | u32 bi0, bi1; |
| 321 | vlib_buffer_t *b0, *b1; |
| 322 | |
| 323 | /* Prefetch next iteration. */ |
| 324 | { |
| 325 | vlib_buffer_t *p2, *p3; |
| 326 | |
| 327 | p2 = vlib_get_buffer (vm, from[2]); |
| 328 | p3 = vlib_get_buffer (vm, from[3]); |
| 329 | |
| 330 | vlib_prefetch_buffer_header (p2, LOAD); |
| 331 | vlib_prefetch_buffer_header (p3, LOAD); |
| 332 | |
| 333 | CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 334 | CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 335 | } |
| 336 | |
| 337 | /* speculatively enqueue b0 and b1 to the current next frame */ |
| 338 | to_next[0] = bi0 = from[0]; |
| 339 | to_next[1] = bi1 = from[1]; |
| 340 | from += 2; |
| 341 | to_next += 2; |
| 342 | n_left_from -= 2; |
| 343 | n_left_to_next -= 2; |
| 344 | |
| 345 | b0 = vlib_get_buffer (vm, bi0); |
| 346 | b1 = vlib_get_buffer (vm, bi1); |
| 347 | |
| 348 | ASSERT (b0->current_data == 0); |
| 349 | ASSERT (b1->current_data == 0); |
| 350 | |
| 351 | en0 = vlib_buffer_get_current (b0); |
| 352 | en1 = vlib_buffer_get_current (b1); |
| 353 | |
| 354 | src_dst0 = ((u8x16 *) en0)[0]; |
| 355 | src_dst1 = ((u8x16 *) en1)[0]; |
| 356 | src_dst0 = u8x16_shuffle (src_dst0, swapmac); |
| 357 | src_dst1 = u8x16_shuffle (src_dst1, swapmac); |
| 358 | ((u8x16 *) en0)[0] = src_dst0; |
| 359 | ((u8x16 *) en1)[0] = src_dst1; |
| 360 | |
| 361 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 362 | sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX]; |
| 363 | |
| 364 | /* Send pkt back out the RX interface */ |
| 365 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0; |
| 366 | vnet_buffer (b1)->sw_if_index[VLIB_TX] = sw_if_index1; |
| 367 | |
| 368 | pkts_swapped += 2; |
| 369 | |
| 370 | if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE))) |
| 371 | { |
| 372 | if (b0->flags & VLIB_BUFFER_IS_TRACED) |
| 373 | { |
| 374 | sample_trace_t *t = |
| 375 | vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 376 | t->sw_if_index = sw_if_index0; |
| 377 | t->next_index = next0; |
| 378 | clib_memcpy (t->new_src_mac, en0->src_address, |
| 379 | sizeof (t->new_src_mac)); |
| 380 | clib_memcpy (t->new_dst_mac, en0->dst_address, |
| 381 | sizeof (t->new_dst_mac)); |
| 382 | |
| 383 | } |
| 384 | if (b1->flags & VLIB_BUFFER_IS_TRACED) |
| 385 | { |
| 386 | sample_trace_t *t = |
| 387 | vlib_add_trace (vm, node, b1, sizeof (*t)); |
| 388 | t->sw_if_index = sw_if_index1; |
| 389 | t->next_index = next1; |
| 390 | clib_memcpy (t->new_src_mac, en1->src_address, |
| 391 | sizeof (t->new_src_mac)); |
| 392 | clib_memcpy (t->new_dst_mac, en1->dst_address, |
| 393 | sizeof (t->new_dst_mac)); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /* verify speculative enqueues, maybe switch current next frame */ |
| 398 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, |
| 399 | to_next, n_left_to_next, |
| 400 | bi0, bi1, next0, next1); |
| 401 | } |
| 402 | |
| 403 | while (n_left_from > 0 && n_left_to_next > 0) |
| 404 | { |
| 405 | u32 bi0; |
| 406 | vlib_buffer_t *b0; |
| 407 | u32 next0 = SAMPLE_NEXT_INTERFACE_OUTPUT; |
| 408 | u32 sw_if_index0; |
| 409 | u8x16 src_dst0; |
| 410 | ethernet_header_t *en0; |
| 411 | |
| 412 | /* speculatively enqueue b0 to the current next frame */ |
| 413 | bi0 = from[0]; |
| 414 | to_next[0] = bi0; |
| 415 | from += 1; |
| 416 | to_next += 1; |
| 417 | n_left_from -= 1; |
| 418 | n_left_to_next -= 1; |
| 419 | |
| 420 | b0 = vlib_get_buffer (vm, bi0); |
| 421 | /* |
| 422 | * Direct from the driver, we should be at offset 0 |
| 423 | * aka at &b0->data[0] |
| 424 | */ |
| 425 | ASSERT (b0->current_data == 0); |
| 426 | |
| 427 | en0 = vlib_buffer_get_current (b0); |
| 428 | src_dst0 = ((u8x16 *) en0)[0]; |
| 429 | src_dst0 = u8x16_shuffle (src_dst0, swapmac); |
| 430 | ((u8x16 *) en0)[0] = src_dst0; |
| 431 | |
| 432 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 433 | |
| 434 | /* Send pkt back out the RX interface */ |
| 435 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0; |
| 436 | |
| 437 | if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) |
| 438 | && (b0->flags & VLIB_BUFFER_IS_TRACED))) |
| 439 | { |
| 440 | sample_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 441 | t->sw_if_index = sw_if_index0; |
| 442 | t->next_index = next0; |
| 443 | clib_memcpy (t->new_src_mac, en0->src_address, |
| 444 | sizeof (t->new_src_mac)); |
| 445 | clib_memcpy (t->new_dst_mac, en0->dst_address, |
| 446 | sizeof (t->new_dst_mac)); |
| 447 | } |
| 448 | |
| 449 | pkts_swapped += 1; |
| 450 | |
| 451 | /* verify speculative enqueue, maybe switch current next frame */ |
| 452 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 453 | to_next, n_left_to_next, |
| 454 | bi0, next0); |
| 455 | } |
| 456 | |
| 457 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 458 | } |
| 459 | |
| 460 | vlib_node_increment_counter (vm, sample_node.index, |
| 461 | SAMPLE_ERROR_SWAPPED, pkts_swapped); |
| 462 | return frame->n_vectors; |
| 463 | } |
| 464 | #endif |
| 465 | |
| 466 | |
| 467 | /* |
| 468 | * This version computes all of the buffer pointers in |
| 469 | * one motion, uses a quad/single loop model, and |
| 470 | * traces the entire frame in one motion. |
| 471 | * |
| 472 | * Node costs about 16 clocks/pkt at a vector size of 26 |
| 473 | * |
| 474 | * Some compilation drama with u8x16_shuffle, so turned off by |
| 475 | * default. |
| 476 | */ |
| 477 | |
| 478 | #ifdef VERSION_3 |
| 479 | |
| 480 | #define u8x16_shuffle __builtin_shuffle |
| 481 | /* This would normally be a stack local, but since it's a constant... */ |
| 482 | static const u16 nexts[VLIB_FRAME_SIZE] = { 0 }; |
| 483 | |
| 484 | static uword |
| 485 | sample_node_fn (vlib_main_t * vm, |
| 486 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 487 | { |
| 488 | u32 n_left_from, *from; |
| 489 | u32 pkts_swapped = 0; |
| 490 | /* Vector shuffle mask to swap src, dst */ |
| 491 | u8x16 swapmac = { 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 12, 13, 14, 15 }; |
| 492 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b; |
| 493 | /* See comment below about sending all pkts to the same place... */ |
| 494 | u16 *next __attribute__ ((unused)); |
| 495 | |
| 496 | from = vlib_frame_vector_args (frame); |
| 497 | n_left_from = frame->n_vectors; |
| 498 | |
| 499 | vlib_get_buffers (vm, from, bufs, n_left_from); |
| 500 | b = bufs; |
| 501 | // next = nexts; |
| 502 | |
| 503 | /* |
| 504 | * We send all pkts to SAMPLE_NEXT_INTERFACE_OUTPUT, aka |
| 505 | * graph arc 0. So the usual setting of next[0...3] is commented |
| 506 | * out below |
| 507 | */ |
| 508 | |
| 509 | while (n_left_from >= 4) |
| 510 | { |
| 511 | u8x16 src_dst0, src_dst1, src_dst2, src_dst3; |
| 512 | /* Prefetch next iteration. */ |
| 513 | if (PREDICT_TRUE (n_left_from >= 8)) |
| 514 | { |
| 515 | vlib_prefetch_buffer_header (b[4], STORE); |
| 516 | vlib_prefetch_buffer_header (b[5], STORE); |
| 517 | vlib_prefetch_buffer_header (b[6], STORE); |
| 518 | vlib_prefetch_buffer_header (b[7], STORE); |
| 519 | CLIB_PREFETCH (&b[4]->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 520 | CLIB_PREFETCH (&b[5]->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 521 | CLIB_PREFETCH (&b[6]->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 522 | CLIB_PREFETCH (&b[7]->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 523 | } |
| 524 | |
| 525 | src_dst0 = ((u8x16 *) vlib_buffer_get_current (b[0]))[0]; |
| 526 | src_dst1 = ((u8x16 *) vlib_buffer_get_current (b[1]))[0]; |
| 527 | src_dst2 = ((u8x16 *) vlib_buffer_get_current (b[2]))[0]; |
| 528 | src_dst3 = ((u8x16 *) vlib_buffer_get_current (b[3]))[0]; |
| 529 | |
| 530 | src_dst0 = u8x16_shuffle (src_dst0, swapmac); |
| 531 | src_dst1 = u8x16_shuffle (src_dst1, swapmac); |
| 532 | src_dst2 = u8x16_shuffle (src_dst2, swapmac); |
| 533 | src_dst3 = u8x16_shuffle (src_dst3, swapmac); |
| 534 | |
| 535 | ((u8x16 *) vlib_buffer_get_current (b[0]))[0] = src_dst0; |
| 536 | ((u8x16 *) vlib_buffer_get_current (b[1]))[0] = src_dst1; |
| 537 | ((u8x16 *) vlib_buffer_get_current (b[2]))[0] = src_dst2; |
| 538 | ((u8x16 *) vlib_buffer_get_current (b[3]))[0] = src_dst3; |
| 539 | |
| 540 | vnet_buffer (b[0])->sw_if_index[VLIB_TX] = |
| 541 | vnet_buffer (b[0])->sw_if_index[VLIB_RX]; |
| 542 | vnet_buffer (b[1])->sw_if_index[VLIB_TX] = |
| 543 | vnet_buffer (b[1])->sw_if_index[VLIB_RX]; |
| 544 | vnet_buffer (b[2])->sw_if_index[VLIB_TX] = |
| 545 | vnet_buffer (b[2])->sw_if_index[VLIB_RX]; |
| 546 | vnet_buffer (b[3])->sw_if_index[VLIB_TX] = |
| 547 | vnet_buffer (b[3])->sw_if_index[VLIB_RX]; |
| 548 | |
| 549 | // next[0] = SAMPLE_NEXT_INTERFACE_OUTPUT; |
| 550 | // next[1] = SAMPLE_NEXT_INTERFACE_OUTPUT; |
| 551 | // next[2] = SAMPLE_NEXT_INTERFACE_OUTPUT; |
| 552 | // next[3] = SAMPLE_NEXT_INTERFACE_OUTPUT; |
| 553 | |
| 554 | b += 4; |
| 555 | // next += 4; |
| 556 | n_left_from -= 4; |
| 557 | pkts_swapped += 4; |
| 558 | } |
| 559 | |
| 560 | while (n_left_from > 0) |
| 561 | { |
| 562 | u8x16 src_dst0; |
| 563 | src_dst0 = ((u8x16 *) vlib_buffer_get_current (b[0]))[0]; |
| 564 | src_dst0 = u8x16_shuffle (src_dst0, swapmac); |
| 565 | ((u8x16 *) vlib_buffer_get_current (b[0]))[0] = src_dst0; |
| 566 | vnet_buffer (b[0])->sw_if_index[VLIB_TX] = |
| 567 | vnet_buffer (b[0])->sw_if_index[VLIB_RX]; |
| 568 | // next[0] = SAMPLE_NEXT_INTERFACE_OUTPUT; |
| 569 | |
| 570 | b += 1; |
| 571 | // next += 1; |
| 572 | n_left_from -= 1; |
| 573 | pkts_swapped += 1; |
| 574 | |
| 575 | } |
| 576 | vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts, |
| 577 | frame->n_vectors); |
| 578 | |
| 579 | vlib_node_increment_counter (vm, sample_node.index, |
| 580 | SAMPLE_ERROR_SWAPPED, pkts_swapped); |
| 581 | |
| 582 | if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE))) |
| 583 | { |
| 584 | int i; |
| 585 | b = bufs; |
| 586 | |
| 587 | for (i = 0; i < frame->n_vectors; i++) |
| 588 | { |
| 589 | if (b[0]->flags & VLIB_BUFFER_IS_TRACED) |
| 590 | { |
| 591 | ethernet_header_t *en; |
| 592 | sample_trace_t *t = |
| 593 | vlib_add_trace (vm, node, b[0], sizeof (*t)); |
| 594 | t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX]; |
| 595 | t->next_index = SAMPLE_NEXT_INTERFACE_OUTPUT; |
| 596 | en = vlib_buffer_get_current (b[0]); |
| 597 | clib_memcpy (t->new_src_mac, en->src_address, |
| 598 | sizeof (t->new_src_mac)); |
| 599 | clib_memcpy (t->new_dst_mac, en->dst_address, |
| 600 | sizeof (t->new_dst_mac)); |
| 601 | b++; |
| 602 | } |
| 603 | else |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | return frame->n_vectors; |
| 608 | } |
| 609 | #endif |
| 610 | |
| 611 | /* *INDENT-OFF* */ |
| 612 | VLIB_REGISTER_NODE (sample_node) = |
| 613 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 614 | .function = sample_node_fn, |
| 615 | .name = "sample", |
| 616 | .vector_size = sizeof (u32), |
| 617 | .format_trace = format_sample_trace, |
| 618 | .type = VLIB_NODE_TYPE_INTERNAL, |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 619 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 620 | .n_errors = ARRAY_LEN(sample_error_strings), |
| 621 | .error_strings = sample_error_strings, |
| 622 | |
| 623 | .n_next_nodes = SAMPLE_N_NEXT, |
| 624 | |
| 625 | /* edit / add dispositions here */ |
| 626 | .next_nodes = { |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 627 | [SAMPLE_NEXT_INTERFACE_OUTPUT] = "interface-output", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 628 | }, |
| 629 | }; |
Dave Barach | 9594b56 | 2018-07-25 16:56:38 -0400 | [diff] [blame^] | 630 | /* *INDENT-ON* */ |
| 631 | |
| 632 | VLIB_NODE_FUNCTION_MULTIARCH (sample_node, sample_node_fn); |
| 633 | |
| 634 | /* |
| 635 | * fd.io coding-style-patch-verification: ON |
| 636 | * |
| 637 | * Local Variables: |
| 638 | * eval: (c-set-style "gnu") |
| 639 | * End: |
| 640 | */ |