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