Dave Barach | 8d0f2f0 | 2018-03-12 09:31:36 -0400 | [diff] [blame] | 1 | ;;; Copyright (c) 2016 Cisco and/or its affiliates. |
| 2 | ;;; Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | ;;; you may not use this file except in compliance with the License. |
| 4 | ;;; You may obtain a copy of the License at: |
| 5 | ;;; |
| 6 | ;;; http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | ;;; |
| 8 | ;;; Unless required by applicable law or agreed to in writing, software |
| 9 | ;;; distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | ;;; See the License for the specific language governing permissions and |
| 12 | ;;; limitations under the License. |
| 13 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 14 | ;;; dual-loop-skel.el - Eliotic dual-loop node skeleton |
| 15 | |
| 16 | (require 'skeleton) |
| 17 | |
Keith Burns (alagalah) | ca46d8c | 2016-03-18 07:22:15 -0700 | [diff] [blame] | 18 | (define-skeleton skel-dual-loop |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 19 | "Insert a skeleton dual-loop graph node" |
| 20 | nil |
| 21 | '(setq node-name (skeleton-read "Node Name: ")) |
| 22 | '(setq uc-node-name (upcase node-name)) |
| 23 | " |
| 24 | #include <vlib/vlib.h> |
| 25 | #include <vnet/vnet.h> |
| 26 | #include <vnet/pg/pg.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 27 | |
| 28 | #include <vnet/ip/ip.h> |
| 29 | #include <vnet/ethernet/ethernet.h> |
| 30 | |
Dave Wallace | 526b5e8 | 2016-03-14 00:10:24 -0400 | [diff] [blame] | 31 | #include <vppinfra/hash.h> |
| 32 | #include <vppinfra/error.h> |
| 33 | #include <vppinfra/elog.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 34 | |
| 35 | typedef struct { |
| 36 | /* convenience */ |
| 37 | vlib_main_t * vlib_main; |
| 38 | vnet_main_t * vnet_main; |
| 39 | ethernet_main_t * ethernet_main; |
| 40 | } " node-name "_main_t; |
| 41 | |
| 42 | " node-name "_main_t " node-name "_main; |
| 43 | |
| 44 | vlib_node_registration_t " node-name "_node; |
| 45 | |
| 46 | typedef struct { |
| 47 | u32 next_index; |
| 48 | u32 sw_if_index; |
| 49 | } " node-name "_trace_t; |
| 50 | |
| 51 | /* packet trace format function */ |
| 52 | static u8 * format_" node-name "_trace (u8 * s, va_list * args) |
| 53 | { |
| 54 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 55 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 56 | " node-name "_trace_t * t = va_arg (*args, " node-name "_trace_t *); |
| 57 | |
| 58 | s = format (s, \"" uc-node-name ": sw_if_index %d, next index %d\", |
| 59 | t->sw_if_index, t->next_index); |
| 60 | return s; |
| 61 | } |
| 62 | |
| 63 | vlib_node_registration_t " node-name "_node; |
| 64 | |
| 65 | #define foreach_" node-name "_error \\ |
| 66 | _(SWAPPED, \"Mac swap packets processed\") |
| 67 | |
| 68 | typedef enum { |
| 69 | #define _(sym,str) " uc-node-name "_ERROR_##sym, |
| 70 | foreach_" node-name "_error |
| 71 | #undef _ |
| 72 | " uc-node-name "_N_ERROR, |
| 73 | } " node-name "_error_t; |
| 74 | |
| 75 | static char * " node-name "_error_strings[] = { |
| 76 | #define _(sym,string) string, |
| 77 | foreach_" node-name "_error |
| 78 | #undef _ |
| 79 | }; |
| 80 | |
| 81 | typedef enum { |
| 82 | " uc-node-name "_NEXT_INTERFACE_OUTPUT, |
| 83 | " uc-node-name "_N_NEXT, |
| 84 | } " node-name "_next_t; |
| 85 | |
| 86 | #define foreach_mac_address_offset \\ |
| 87 | _(0) \\ |
| 88 | _(1) \\ |
| 89 | _(2) \\ |
| 90 | _(3) \\ |
| 91 | _(4) \\ |
| 92 | _(5) |
| 93 | |
| 94 | static uword |
| 95 | " node-name "_node_fn (vlib_main_t * vm, |
| 96 | vlib_node_runtime_t * node, |
| 97 | vlib_frame_t * frame) |
| 98 | { |
| 99 | u32 n_left_from, * from, * to_next; |
| 100 | " node-name "_next_t next_index; |
| 101 | u32 pkts_swapped = 0; |
| 102 | |
| 103 | from = vlib_frame_vector_args (frame); |
| 104 | n_left_from = frame->n_vectors; |
| 105 | next_index = node->cached_next_index; |
| 106 | |
| 107 | while (n_left_from > 0) |
| 108 | { |
| 109 | u32 n_left_to_next; |
| 110 | |
| 111 | vlib_get_next_frame (vm, node, next_index, |
| 112 | to_next, n_left_to_next); |
| 113 | |
| 114 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 115 | { |
| 116 | u32 next0 = " uc-node-name "_NEXT_INTERFACE_OUTPUT; |
| 117 | u32 next1 = " uc-node-name "_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 | |
| 124 | /* Prefetch next iteration. */ |
| 125 | { |
| 126 | vlib_buffer_t * p2, * p3; |
| 127 | |
| 128 | p2 = vlib_get_buffer (vm, from[2]); |
| 129 | p3 = vlib_get_buffer (vm, from[3]); |
| 130 | |
| 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 | |
| 138 | /* speculatively enqueue b0 and b1 to the current next frame */ |
| 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 | |
| 149 | /* $$$$$ Dual loop: process 2 x packets here $$$$$ */ |
| 150 | ASSERT (b0->current_data == 0); |
| 151 | ASSERT (b1->current_data == 0); |
| 152 | |
| 153 | en0 = vlib_buffer_get_current (b0); |
| 154 | en1 = vlib_buffer_get_current (b1); |
| 155 | |
| 156 | /* This is not the fastest way to swap src + dst mac addresses */ |
| 157 | #define _(a) tmp0[a] = en0->src_address[a]; |
| 158 | foreach_mac_address_offset; |
| 159 | #undef _ |
| 160 | #define _(a) en0->src_address[a] = en0->dst_address[a]; |
| 161 | foreach_mac_address_offset; |
| 162 | #undef _ |
| 163 | #define _(a) en0->dst_address[a] = tmp0[a]; |
| 164 | foreach_mac_address_offset; |
| 165 | #undef _ |
| 166 | |
| 167 | #define _(a) tmp1[a] = en1->src_address[a]; |
| 168 | foreach_mac_address_offset; |
| 169 | #undef _ |
| 170 | #define _(a) en1->src_address[a] = en1->dst_address[a]; |
| 171 | foreach_mac_address_offset; |
| 172 | #undef _ |
| 173 | #define _(a) en1->dst_address[a] = tmp1[a]; |
| 174 | foreach_mac_address_offset; |
| 175 | #undef _ |
| 176 | |
| 177 | sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX]; |
| 178 | sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX]; |
| 179 | |
| 180 | /* Send pkt back out the RX interface */ |
| 181 | vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0; |
| 182 | vnet_buffer(b1)->sw_if_index[VLIB_TX] = sw_if_index1; |
| 183 | |
| 184 | pkts_swapped += 2; |
| 185 | /* $$$$$ End of processing 2 x packets $$$$$ */ |
| 186 | |
| 187 | if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE))) |
| 188 | { |
| 189 | if (b0->flags & VLIB_BUFFER_IS_TRACED) |
| 190 | { |
| 191 | " node-name "_trace_t *t = |
| 192 | vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 193 | t->sw_if_index = sw_if_index0; |
| 194 | t->next_index = next0; |
| 195 | } |
| 196 | if (b1->flags & VLIB_BUFFER_IS_TRACED) |
| 197 | { |
| 198 | " node-name "_trace_t *t = |
| 199 | vlib_add_trace (vm, node, b1, sizeof (*t)); |
| 200 | t->sw_if_index = sw_if_index1; |
| 201 | t->next_index = next1; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /* verify speculative enqueues, maybe switch current next frame */ |
| 206 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, |
| 207 | to_next, n_left_to_next, |
| 208 | bi0, bi1, next0, next1); |
| 209 | } |
| 210 | |
| 211 | while (n_left_from > 0 && n_left_to_next > 0) |
| 212 | { |
| 213 | u32 bi0; |
| 214 | vlib_buffer_t * b0; |
| 215 | u32 next0 = " uc-node-name "_NEXT_INTERFACE_OUTPUT; |
| 216 | u32 sw_if_index0; |
| 217 | u8 tmp0[6]; |
| 218 | ethernet_header_t *en0; |
| 219 | |
| 220 | /* speculatively enqueue b0 to the current next frame */ |
| 221 | bi0 = from[0]; |
| 222 | to_next[0] = bi0; |
| 223 | from += 1; |
| 224 | to_next += 1; |
| 225 | n_left_from -= 1; |
| 226 | n_left_to_next -= 1; |
| 227 | |
| 228 | b0 = vlib_get_buffer (vm, bi0); |
| 229 | |
| 230 | /* $$$$$ Single loop: process 1 packet here $$$$$ */ |
| 231 | |
| 232 | /* |
| 233 | * Direct from the driver, we should be at offset 0 |
| 234 | * aka at &b0->data[0] |
| 235 | */ |
| 236 | ASSERT (b0->current_data == 0); |
| 237 | |
| 238 | en0 = vlib_buffer_get_current (b0); |
| 239 | |
| 240 | /* This is not the fastest way to swap src + dst mac addresses */ |
| 241 | #define _(a) tmp0[a] = en0->src_address[a]; |
| 242 | foreach_mac_address_offset; |
| 243 | #undef _ |
| 244 | #define _(a) en0->src_address[a] = en0->dst_address[a]; |
| 245 | foreach_mac_address_offset; |
| 246 | #undef _ |
| 247 | #define _(a) en0->dst_address[a] = tmp0[a]; |
| 248 | foreach_mac_address_offset; |
| 249 | #undef _ |
| 250 | |
| 251 | sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX]; |
| 252 | |
| 253 | /* Send pkt back out the RX interface */ |
| 254 | vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0; |
| 255 | |
| 256 | if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) |
| 257 | && (b0->flags & VLIB_BUFFER_IS_TRACED))) |
| 258 | { |
| 259 | " node-name "_trace_t *t = |
| 260 | vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 261 | t->sw_if_index = sw_if_index0; |
| 262 | t->next_index = next0; |
| 263 | } |
| 264 | |
| 265 | pkts_swapped += 1; |
| 266 | |
| 267 | /* $$$$$ Done processing 1 packet here $$$$$ */ |
| 268 | |
| 269 | /* verify speculative enqueue, maybe switch current next frame */ |
| 270 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 271 | to_next, n_left_to_next, |
| 272 | bi0, next0); |
| 273 | } |
| 274 | |
| 275 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 276 | } |
| 277 | |
| 278 | vlib_node_increment_counter (vm, " node-name "_node.index, |
| 279 | " uc-node-name "_ERROR_SWAPPED, pkts_swapped); |
| 280 | return frame->n_vectors; |
| 281 | } |
| 282 | |
| 283 | VLIB_REGISTER_NODE (" node-name "_node) = { |
| 284 | .function = " node-name "_node_fn, |
| 285 | .name = \"" node-name "\", |
| 286 | .vector_size = sizeof (u32), |
| 287 | .format_trace = format_" node-name "_trace, |
| 288 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 289 | |
| 290 | .n_errors = ARRAY_LEN(" node-name "_error_strings), |
| 291 | .error_strings = " node-name "_error_strings, |
| 292 | |
| 293 | .n_next_nodes = " uc-node-name "_N_NEXT, |
| 294 | |
| 295 | /* edit / add dispositions here */ |
| 296 | .next_nodes = { |
| 297 | [" uc-node-name "_NEXT_INTERFACE_OUTPUT] = \"interface-output\", |
| 298 | }, |
| 299 | }; |
| 300 | |
Dave Wallace | 334806c | 2016-03-15 01:28:58 -0400 | [diff] [blame] | 301 | clib_error_t *" node-name "_init (vlib_main_t *vm) |
| 302 | { |
| 303 | " node-name "_main_t *msm = &" node-name "_main; |
| 304 | |
| 305 | /* $$$$$ Initialize " node-name "_main_t structure here. $$$$$ */ |
| 306 | msm->vlib_main = vm; |
| 307 | msm->vnet_main = vnet_get_main(); |
| 308 | msm->ethernet_main = ethernet_get_main(vm); |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | VLIB_INIT_FUNCTION(" node-name "_init); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 314 | ") |
| 315 | |