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 | d7a37a7 | 2018-08-10 16:22:48 -0400 | [diff] [blame] | 55 | #ifndef CLIB_MARCH_VARIANT |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 56 | static u8 * |
| 57 | format_mac_address (u8 * s, va_list * args) |
| 58 | { |
| 59 | u8 *a = va_arg (*args, u8 *); |
| 60 | return format (s, \"%02x:%02x:%02x:%02x:%02x:%02x\", |
| 61 | a[0], a[1], a[2], a[3], a[4], a[5]); |
| 62 | } |
| 63 | |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 64 | /* packet trace format function */ |
| 65 | static u8 * format_" plugin-name "_trace (u8 * s, va_list * args) |
| 66 | { |
| 67 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 68 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 69 | " plugin-name "_trace_t * t = va_arg (*args, " plugin-name "_trace_t *); |
| 70 | |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 71 | 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] | 72 | t->sw_if_index, t->next_index); |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 73 | s = format (s, \" new src %U -> new dst %U\", |
| 74 | format_mac_address, t->new_src_mac, |
| 75 | format_mac_address, t->new_dst_mac); |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 76 | return s; |
| 77 | } |
Dave Barach | d7a37a7 | 2018-08-10 16:22:48 -0400 | [diff] [blame] | 78 | #endif /* CLIB_MARCH_VARIANT */ |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 79 | |
| 80 | vlib_node_registration_t " plugin-name "_node; |
| 81 | |
| 82 | #define foreach_" plugin-name "_error \\ |
| 83 | _(SWAPPED, \"Mac swap packets processed\") |
| 84 | |
| 85 | typedef enum { |
| 86 | #define _(sym,str) " PLUGIN-NAME "_ERROR_##sym, |
| 87 | foreach_" plugin-name "_error |
| 88 | #undef _ |
| 89 | " PLUGIN-NAME "_N_ERROR, |
| 90 | } " plugin-name "_error_t; |
| 91 | |
Dave Barach | d7a37a7 | 2018-08-10 16:22:48 -0400 | [diff] [blame] | 92 | #ifndef CLIB_MARCH_VARIANT |
| 93 | static char * " plugin-name "_error_strings[] = |
| 94 | { |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 95 | #define _(sym,string) string, |
| 96 | foreach_" plugin-name "_error |
| 97 | #undef _ |
| 98 | }; |
Dave Barach | d7a37a7 | 2018-08-10 16:22:48 -0400 | [diff] [blame] | 99 | #endif /* CLIB_MARCH_VARIANT */ |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 100 | |
Dave Barach | fed79e8 | 2017-02-10 11:57:46 -0500 | [diff] [blame] | 101 | typedef enum |
| 102 | { |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 103 | " PLUGIN-NAME "_NEXT_INTERFACE_OUTPUT, |
| 104 | " PLUGIN-NAME "_N_NEXT, |
| 105 | } " plugin-name "_next_t; |
| 106 | |
| 107 | #define foreach_mac_address_offset \\ |
| 108 | _(0) \\ |
| 109 | _(1) \\ |
| 110 | _(2) \\ |
| 111 | _(3) \\ |
| 112 | _(4) \\ |
| 113 | _(5) |
| 114 | |
Dave Barach | d7a37a7 | 2018-08-10 16:22:48 -0400 | [diff] [blame] | 115 | |
| 116 | VLIB_NODE_FN (" plugin-name "_node) (vlib_main_t * vm, |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 117 | vlib_node_runtime_t * node, |
| 118 | vlib_frame_t * frame) |
| 119 | { |
| 120 | u32 n_left_from, * from, * to_next; |
| 121 | " plugin-name "_next_t next_index; |
| 122 | u32 pkts_swapped = 0; |
| 123 | |
| 124 | from = vlib_frame_vector_args (frame); |
| 125 | n_left_from = frame->n_vectors; |
| 126 | next_index = node->cached_next_index; |
| 127 | |
| 128 | while (n_left_from > 0) |
| 129 | { |
| 130 | u32 n_left_to_next; |
| 131 | |
| 132 | vlib_get_next_frame (vm, node, next_index, |
| 133 | to_next, n_left_to_next); |
| 134 | |
| 135 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 136 | { |
| 137 | u32 next0 = " PLUGIN-NAME "_NEXT_INTERFACE_OUTPUT; |
| 138 | u32 next1 = " PLUGIN-NAME "_NEXT_INTERFACE_OUTPUT; |
| 139 | u32 sw_if_index0, sw_if_index1; |
| 140 | u8 tmp0[6], tmp1[6]; |
| 141 | ethernet_header_t *en0, *en1; |
| 142 | u32 bi0, bi1; |
| 143 | vlib_buffer_t * b0, * b1; |
| 144 | |
| 145 | /* Prefetch next iteration. */ |
| 146 | { |
| 147 | vlib_buffer_t * p2, * p3; |
| 148 | |
| 149 | p2 = vlib_get_buffer (vm, from[2]); |
| 150 | p3 = vlib_get_buffer (vm, from[3]); |
| 151 | |
| 152 | vlib_prefetch_buffer_header (p2, LOAD); |
| 153 | vlib_prefetch_buffer_header (p3, LOAD); |
| 154 | |
| 155 | CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 156 | CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 157 | } |
| 158 | |
| 159 | /* speculatively enqueue b0 and b1 to the current next frame */ |
| 160 | to_next[0] = bi0 = from[0]; |
| 161 | to_next[1] = bi1 = from[1]; |
| 162 | from += 2; |
| 163 | to_next += 2; |
| 164 | n_left_from -= 2; |
| 165 | n_left_to_next -= 2; |
| 166 | |
| 167 | b0 = vlib_get_buffer (vm, bi0); |
| 168 | b1 = vlib_get_buffer (vm, bi1); |
| 169 | |
| 170 | ASSERT (b0->current_data == 0); |
| 171 | ASSERT (b1->current_data == 0); |
| 172 | |
| 173 | en0 = vlib_buffer_get_current (b0); |
| 174 | en1 = vlib_buffer_get_current (b1); |
| 175 | |
| 176 | /* This is not the fastest way to swap src + dst mac addresses */ |
| 177 | #define _(a) tmp0[a] = en0->src_address[a]; |
| 178 | foreach_mac_address_offset; |
| 179 | #undef _ |
| 180 | #define _(a) en0->src_address[a] = en0->dst_address[a]; |
| 181 | foreach_mac_address_offset; |
| 182 | #undef _ |
| 183 | #define _(a) en0->dst_address[a] = tmp0[a]; |
| 184 | foreach_mac_address_offset; |
| 185 | #undef _ |
| 186 | |
| 187 | #define _(a) tmp1[a] = en1->src_address[a]; |
| 188 | foreach_mac_address_offset; |
| 189 | #undef _ |
| 190 | #define _(a) en1->src_address[a] = en1->dst_address[a]; |
| 191 | foreach_mac_address_offset; |
| 192 | #undef _ |
| 193 | #define _(a) en1->dst_address[a] = tmp1[a]; |
| 194 | foreach_mac_address_offset; |
| 195 | #undef _ |
| 196 | |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 197 | sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX]; |
| 198 | sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX]; |
| 199 | |
| 200 | /* Send pkt back out the RX interface */ |
| 201 | vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0; |
| 202 | vnet_buffer(b1)->sw_if_index[VLIB_TX] = sw_if_index1; |
| 203 | |
| 204 | pkts_swapped += 2; |
| 205 | |
| 206 | if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE))) |
| 207 | { |
| 208 | if (b0->flags & VLIB_BUFFER_IS_TRACED) |
| 209 | { |
| 210 | " plugin-name "_trace_t *t = |
| 211 | vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 212 | t->sw_if_index = sw_if_index0; |
| 213 | t->next_index = next0; |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 214 | clib_memcpy (t->new_src_mac, en0->src_address, |
| 215 | sizeof (t->new_src_mac)); |
| 216 | clib_memcpy (t->new_dst_mac, en0->dst_address, |
| 217 | sizeof (t->new_dst_mac)); |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 218 | } |
| 219 | if (b1->flags & VLIB_BUFFER_IS_TRACED) |
| 220 | { |
| 221 | " plugin-name "_trace_t *t = |
| 222 | vlib_add_trace (vm, node, b1, sizeof (*t)); |
| 223 | t->sw_if_index = sw_if_index1; |
| 224 | t->next_index = next1; |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 225 | clib_memcpy (t->new_src_mac, en1->src_address, |
| 226 | sizeof (t->new_src_mac)); |
| 227 | clib_memcpy (t->new_dst_mac, en1->dst_address, |
| 228 | sizeof (t->new_dst_mac)); |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | /* verify speculative enqueues, maybe switch current next frame */ |
| 233 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, |
| 234 | to_next, n_left_to_next, |
| 235 | bi0, bi1, next0, next1); |
| 236 | } |
| 237 | |
| 238 | while (n_left_from > 0 && n_left_to_next > 0) |
| 239 | { |
| 240 | u32 bi0; |
| 241 | vlib_buffer_t * b0; |
| 242 | u32 next0 = " PLUGIN-NAME "_NEXT_INTERFACE_OUTPUT; |
| 243 | u32 sw_if_index0; |
| 244 | u8 tmp0[6]; |
| 245 | ethernet_header_t *en0; |
| 246 | |
| 247 | /* speculatively enqueue b0 to the current next frame */ |
| 248 | bi0 = from[0]; |
| 249 | to_next[0] = bi0; |
| 250 | from += 1; |
| 251 | to_next += 1; |
| 252 | n_left_from -= 1; |
| 253 | n_left_to_next -= 1; |
| 254 | |
| 255 | b0 = vlib_get_buffer (vm, bi0); |
| 256 | /* |
| 257 | * Direct from the driver, we should be at offset 0 |
| 258 | * aka at &b0->data[0] |
| 259 | */ |
| 260 | ASSERT (b0->current_data == 0); |
| 261 | |
| 262 | en0 = vlib_buffer_get_current (b0); |
| 263 | |
| 264 | /* This is not the fastest way to swap src + dst mac addresses */ |
| 265 | #define _(a) tmp0[a] = en0->src_address[a]; |
| 266 | foreach_mac_address_offset; |
| 267 | #undef _ |
| 268 | #define _(a) en0->src_address[a] = en0->dst_address[a]; |
| 269 | foreach_mac_address_offset; |
| 270 | #undef _ |
| 271 | #define _(a) en0->dst_address[a] = tmp0[a]; |
| 272 | foreach_mac_address_offset; |
| 273 | #undef _ |
| 274 | |
| 275 | sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX]; |
| 276 | |
| 277 | /* Send pkt back out the RX interface */ |
| 278 | vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0; |
| 279 | |
| 280 | if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) |
| 281 | && (b0->flags & VLIB_BUFFER_IS_TRACED))) { |
| 282 | " plugin-name "_trace_t *t = |
| 283 | vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 284 | t->sw_if_index = sw_if_index0; |
| 285 | t->next_index = next0; |
Dave Barach | b7e2f3d | 2016-11-08 16:47:34 -0500 | [diff] [blame] | 286 | clib_memcpy (t->new_src_mac, en0->src_address, |
| 287 | sizeof (t->new_src_mac)); |
| 288 | clib_memcpy (t->new_dst_mac, en0->dst_address, |
| 289 | sizeof (t->new_dst_mac)); |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | pkts_swapped += 1; |
| 293 | |
| 294 | /* verify speculative enqueue, maybe switch current next frame */ |
| 295 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 296 | to_next, n_left_to_next, |
| 297 | bi0, next0); |
| 298 | } |
| 299 | |
| 300 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 301 | } |
| 302 | |
| 303 | vlib_node_increment_counter (vm, " plugin-name "_node.index, |
| 304 | " PLUGIN-NAME "_ERROR_SWAPPED, pkts_swapped); |
| 305 | return frame->n_vectors; |
| 306 | } |
| 307 | |
Dave Barach | fed79e8 | 2017-02-10 11:57:46 -0500 | [diff] [blame] | 308 | /* *INDENT-OFF* */ |
Dave Barach | d7a37a7 | 2018-08-10 16:22:48 -0400 | [diff] [blame] | 309 | #ifndef CLIB_MARCH_VARIANT |
Dave Barach | fed79e8 | 2017-02-10 11:57:46 -0500 | [diff] [blame] | 310 | VLIB_REGISTER_NODE (" plugin-name "_node) = |
| 311 | { |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 312 | .name = \"" plugin-name "\", |
| 313 | .vector_size = sizeof (u32), |
| 314 | .format_trace = format_" plugin-name "_trace, |
| 315 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 316 | |
| 317 | .n_errors = ARRAY_LEN(" plugin-name "_error_strings), |
| 318 | .error_strings = " plugin-name "_error_strings, |
| 319 | |
| 320 | .n_next_nodes = " PLUGIN-NAME "_N_NEXT, |
| 321 | |
| 322 | /* edit / add dispositions here */ |
| 323 | .next_nodes = { |
| 324 | [" PLUGIN-NAME "_NEXT_INTERFACE_OUTPUT] = \"interface-output\", |
| 325 | }, |
| 326 | }; |
Dave Barach | d7a37a7 | 2018-08-10 16:22:48 -0400 | [diff] [blame] | 327 | #endif /* CLIB_MARCH_VARIANT */ |
Dave Barach | fed79e8 | 2017-02-10 11:57:46 -0500 | [diff] [blame] | 328 | /* *INDENT-ON* */ |
| 329 | /* |
| 330 | * fd.io coding-style-patch-verification: " capital-oh-en " |
| 331 | * |
| 332 | * Local Variables: |
| 333 | * eval: (c-set-style \"gnu\") |
| 334 | * End: |
| 335 | */ |
Dave Barach | b852bfa | 2016-01-04 15:27:42 -0500 | [diff] [blame] | 336 | ") |