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