From 757585db71b78c4edf91ba011be419264de43d1a Mon Sep 17 00:00:00 2001 From: Damjan Marion Date: Thu, 20 Apr 2017 11:42:28 +0200 Subject: Move emacs stuff to extras/ Change-Id: Ibbb7d8500e9064215cf912bd00bdf72a748f8a27 Signed-off-by: Damjan Marion --- extras/emacs/README | 86 +++++++ extras/emacs/all-skel.el | 40 +++ extras/emacs/cli-cmd-skel.el | 32 +++ extras/emacs/config-skel.el | 28 +++ extras/emacs/dual-loop-skel.el | 302 +++++++++++++++++++++++ extras/emacs/elog-4-int-skel.el | 31 +++ extras/emacs/elog-4-int-track-skel.el | 34 +++ extras/emacs/elog-enum-skel.el | 35 +++ extras/emacs/elog-one-datum-skel.el | 28 +++ extras/emacs/fix-coding-style.el | 162 ++++++++++++ extras/emacs/make-plugin.sh | 4 + extras/emacs/periodic-skel.el | 86 +++++++ extras/emacs/pipe-skel.el | 132 ++++++++++ extras/emacs/plugin-all-apih-skel.el | 43 ++++ extras/emacs/plugin-am-skel.el | 60 +++++ extras/emacs/plugin-api-skel.el | 48 ++++ extras/emacs/plugin-h-skel.el | 66 +++++ extras/emacs/plugin-main-skel.el | 280 +++++++++++++++++++++ extras/emacs/plugin-msg-enum-skel.el | 55 +++++ extras/emacs/plugin-node-skel.el | 330 +++++++++++++++++++++++++ extras/emacs/plugin-test-skel.el | 212 ++++++++++++++++ extras/emacs/plugin.el | 32 +++ extras/emacs/tunnel-c-skel.el | 452 ++++++++++++++++++++++++++++++++++ extras/emacs/tunnel-decap-skel.el | 299 ++++++++++++++++++++++ extras/emacs/tunnel-encap-skel.el | 245 ++++++++++++++++++ extras/emacs/tunnel-h-skel.el | 128 ++++++++++ 26 files changed, 3250 insertions(+) create mode 100644 extras/emacs/README create mode 100644 extras/emacs/all-skel.el create mode 100644 extras/emacs/cli-cmd-skel.el create mode 100644 extras/emacs/config-skel.el create mode 100644 extras/emacs/dual-loop-skel.el create mode 100644 extras/emacs/elog-4-int-skel.el create mode 100644 extras/emacs/elog-4-int-track-skel.el create mode 100644 extras/emacs/elog-enum-skel.el create mode 100644 extras/emacs/elog-one-datum-skel.el create mode 100755 extras/emacs/fix-coding-style.el create mode 100755 extras/emacs/make-plugin.sh create mode 100644 extras/emacs/periodic-skel.el create mode 100644 extras/emacs/pipe-skel.el create mode 100644 extras/emacs/plugin-all-apih-skel.el create mode 100644 extras/emacs/plugin-am-skel.el create mode 100644 extras/emacs/plugin-api-skel.el create mode 100644 extras/emacs/plugin-h-skel.el create mode 100644 extras/emacs/plugin-main-skel.el create mode 100644 extras/emacs/plugin-msg-enum-skel.el create mode 100644 extras/emacs/plugin-node-skel.el create mode 100644 extras/emacs/plugin-test-skel.el create mode 100644 extras/emacs/plugin.el create mode 100644 extras/emacs/tunnel-c-skel.el create mode 100644 extras/emacs/tunnel-decap-skel.el create mode 100644 extras/emacs/tunnel-encap-skel.el create mode 100644 extras/emacs/tunnel-h-skel.el (limited to 'extras/emacs') diff --git a/extras/emacs/README b/extras/emacs/README new file mode 100644 index 00000000000..1f09a3fa4e4 --- /dev/null +++ b/extras/emacs/README @@ -0,0 +1,86 @@ +How to construct a complete plugin using the emacs skeletons + +0. Install open-vpp, including the development package. + +1. Load emacs skeletons + + M-x find-file all-skel.el + M-x eval-buffer + +2. Pick a single-word, lower-case name for your plugin. For example: macswap. +Hereafter, we'll refer to the selected name as . + +3. Generate the entire plugin: + + M-x make-plugin + Plugin-name: + +Or, generate each file individually: + +3. Create the required directories, e.g. under .../vpp + + $ mkdir -p -plugin/ + +4. Create -plugin/{configure.ac,Makefile.am} + + M-x find-file -plugin/configure.ac + M-x plugin-configure-skel + + M-x find-file -plugin/Makefile.am + M-x skel-plugin-makefile + +5. Create the api skeleton + M-x find-file -plugin//.api + M-x skel-plugin-api + +6. Create the api message enumeration header file + M-x find-file -plugin//_msg_enum.h + M-x skel-plugin-msg-enum + +7. Create the "all-api" header file + M-x find-file -plugin//_all_api_h.h + M-x skel-plugin-all-apih + +8. Create the main data structure definition header file + M-x find-file -plugin//.h + M-x skel-plugin-h + +9. Create the plugin main C file + M-x find-file -plugin//.c + M-x skel-plugin-main + +10. Create the vpp-api-test plugin main C file + M-x find-file -plugin//_test.c + M-x skel-plugin-test + +11. Create the data plane packet processing node + M-x find-file -plugin//node.c + M-x skel-plugin-node + +12. Process autotools input files + + $ cd -plugin + $ autoreconf -i -f + +13. Build the plugin skeleton + + $ mkdir build + $ cd build + $ ../configure + $ make + $ sudo make install + + + + + + + + + + + + + + + diff --git a/extras/emacs/all-skel.el b/extras/emacs/all-skel.el new file mode 100644 index 00000000000..f0cc9fe382c --- /dev/null +++ b/extras/emacs/all-skel.el @@ -0,0 +1,40 @@ +;;; Copyright (c) 2016 Cisco and/or its affiliates. +;;; Licensed under the Apache License, Version 2.0 (the "License"); +;;; you may not use this file except in compliance with the License. +;;; You may obtain a copy of the License at: +;;; +;;; http://www.apache.org/licenses/LICENSE-2.0 +;;; +;;; Unless required by applicable law or agreed to in writing, software +;;; distributed under the License is distributed on an "AS IS" BASIS, +;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +;;; See the License for the specific language governing permissions and +;;; limitations under the License. + +;; plugin all-in-1 program + +(load-file "./plugin.el") + +;; list of clib / vlib / vnet / vpp skeleton files + +(load-file "./cli-cmd-skel.el") +(load-file "./config-skel.el") +(load-file "./dual-loop-skel.el") +(load-file "./periodic-skel.el") +(load-file "./pipe-skel.el") +(load-file "./plugin-all-apih-skel.el") +(load-file "./plugin-am-skel.el") +(load-file "./plugin-api-skel.el") +(load-file "./plugin-h-skel.el") +(load-file "./plugin-main-skel.el") +(load-file "./plugin-msg-enum-skel.el") +(load-file "./plugin-node-skel.el") +(load-file "./plugin-test-skel.el") +(load-file "./tunnel-c-skel.el") +(load-file "./tunnel-decap-skel.el") +(load-file "./tunnel-encap-skel.el") +(load-file "./tunnel-h-skel.el") +(load-file "./elog-4-int-skel.el") +(load-file "./elog-4-int-track-skel.el") +(load-file "./elog-enum-skel.el") +(load-file "./elog-one-datum-skel.el") diff --git a/extras/emacs/cli-cmd-skel.el b/extras/emacs/cli-cmd-skel.el new file mode 100644 index 00000000000..cf8658c4233 --- /dev/null +++ b/extras/emacs/cli-cmd-skel.el @@ -0,0 +1,32 @@ +;;; cli-cmd-skel.el - cli command skeleton + +(require 'skeleton) + +(define-skeleton skel-cli-cmd +"Insert a CLI command " +nil +'(setq cmd-name (skeleton-read "Command Name: ")) +'(setq path (skeleton-read "Path: ")) + +" +static clib_error_t * +" cmd-name "_command_fn (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { + if (unformat (input, \"whatever %d\", &whatever)) + ; + else + return clib_error_return (0, \"unknown input `%U'\", + format_unformat_error, input); + } + return 0; +} + +VLIB_CLI_COMMAND (" cmd-name "_command, static) = { + .path = \"" path "\", + .short_help = \"" path "\", + .function = " cmd-name "_command_fn, +}; +") diff --git a/extras/emacs/config-skel.el b/extras/emacs/config-skel.el new file mode 100644 index 00000000000..dc2ec3805c2 --- /dev/null +++ b/extras/emacs/config-skel.el @@ -0,0 +1,28 @@ +;;; config-skel.el - config function command skeleton + +(require 'skeleton) + +(define-skeleton skel-config +"Insert a vlib config skeleton " +nil +'(setq cfg-name (skeleton-read "Config Class Name: ")) + +" +static clib_error_t * +" cfg-name "_config (vlib_main_t * vm, unformat_input_t * input) +{ + u32 whatever; + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { + if (unformat (input, \"whatever %d\", &whatever)) + ; + else + return clib_error_return (0, \"unknown input `%U'\", + format_unformat_error, input); + } + + return 0; +} + +VLIB_CONFIG_FUNCTION (" cfg-name "_config, \"" cfg-name "\"); +") diff --git a/extras/emacs/dual-loop-skel.el b/extras/emacs/dual-loop-skel.el new file mode 100644 index 00000000000..b23d65acaf3 --- /dev/null +++ b/extras/emacs/dual-loop-skel.el @@ -0,0 +1,302 @@ +;;; dual-loop-skel.el - Eliotic dual-loop node skeleton + +(require 'skeleton) + +(define-skeleton skel-dual-loop +"Insert a skeleton dual-loop graph node" +nil +'(setq node-name (skeleton-read "Node Name: ")) +'(setq uc-node-name (upcase node-name)) +" +#include +#include +#include + +#include +#include + +#include +#include +#include + +typedef struct { + /* convenience */ + vlib_main_t * vlib_main; + vnet_main_t * vnet_main; + ethernet_main_t * ethernet_main; +} " node-name "_main_t; + +" node-name "_main_t " node-name "_main; + +vlib_node_registration_t " node-name "_node; + +typedef struct { + u32 next_index; + u32 sw_if_index; +} " node-name "_trace_t; + +/* packet trace format function */ +static u8 * format_" node-name "_trace (u8 * s, va_list * args) +{ + CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); + CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); + " node-name "_trace_t * t = va_arg (*args, " node-name "_trace_t *); + + s = format (s, \"" uc-node-name ": sw_if_index %d, next index %d\", + t->sw_if_index, t->next_index); + return s; +} + +vlib_node_registration_t " node-name "_node; + +#define foreach_" node-name "_error \\ +_(SWAPPED, \"Mac swap packets processed\") + +typedef enum { +#define _(sym,str) " uc-node-name "_ERROR_##sym, + foreach_" node-name "_error +#undef _ + " uc-node-name "_N_ERROR, +} " node-name "_error_t; + +static char * " node-name "_error_strings[] = { +#define _(sym,string) string, + foreach_" node-name "_error +#undef _ +}; + +typedef enum { + " uc-node-name "_NEXT_INTERFACE_OUTPUT, + " uc-node-name "_N_NEXT, +} " node-name "_next_t; + +#define foreach_mac_address_offset \\ +_(0) \\ +_(1) \\ +_(2) \\ +_(3) \\ +_(4) \\ +_(5) + +static uword +" node-name "_node_fn (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + u32 n_left_from, * from, * to_next; + " node-name "_next_t next_index; + u32 pkts_swapped = 0; + + from = vlib_frame_vector_args (frame); + n_left_from = frame->n_vectors; + next_index = node->cached_next_index; + + while (n_left_from > 0) + { + u32 n_left_to_next; + + vlib_get_next_frame (vm, node, next_index, + to_next, n_left_to_next); + + while (n_left_from >= 4 && n_left_to_next >= 2) + { + u32 next0 = " uc-node-name "_NEXT_INTERFACE_OUTPUT; + u32 next1 = " uc-node-name "_NEXT_INTERFACE_OUTPUT; + u32 sw_if_index0, sw_if_index1; + u8 tmp0[6], tmp1[6]; + ethernet_header_t *en0, *en1; + u32 bi0, bi1; + vlib_buffer_t * b0, * b1; + + /* Prefetch next iteration. */ + { + vlib_buffer_t * p2, * p3; + + p2 = vlib_get_buffer (vm, from[2]); + p3 = vlib_get_buffer (vm, from[3]); + + vlib_prefetch_buffer_header (p2, LOAD); + vlib_prefetch_buffer_header (p3, LOAD); + + CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE); + CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE); + } + + /* speculatively enqueue b0 and b1 to the current next frame */ + to_next[0] = bi0 = from[0]; + to_next[1] = bi1 = from[1]; + from += 2; + to_next += 2; + n_left_from -= 2; + n_left_to_next -= 2; + + b0 = vlib_get_buffer (vm, bi0); + b1 = vlib_get_buffer (vm, bi1); + + /* $$$$$ Dual loop: process 2 x packets here $$$$$ */ + ASSERT (b0->current_data == 0); + ASSERT (b1->current_data == 0); + + en0 = vlib_buffer_get_current (b0); + en1 = vlib_buffer_get_current (b1); + + /* This is not the fastest way to swap src + dst mac addresses */ +#define _(a) tmp0[a] = en0->src_address[a]; + foreach_mac_address_offset; +#undef _ +#define _(a) en0->src_address[a] = en0->dst_address[a]; + foreach_mac_address_offset; +#undef _ +#define _(a) en0->dst_address[a] = tmp0[a]; + foreach_mac_address_offset; +#undef _ + +#define _(a) tmp1[a] = en1->src_address[a]; + foreach_mac_address_offset; +#undef _ +#define _(a) en1->src_address[a] = en1->dst_address[a]; + foreach_mac_address_offset; +#undef _ +#define _(a) en1->dst_address[a] = tmp1[a]; + foreach_mac_address_offset; +#undef _ + + sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX]; + sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX]; + + /* Send pkt back out the RX interface */ + vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0; + vnet_buffer(b1)->sw_if_index[VLIB_TX] = sw_if_index1; + + pkts_swapped += 2; + /* $$$$$ End of processing 2 x packets $$$$$ */ + + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE))) + { + if (b0->flags & VLIB_BUFFER_IS_TRACED) + { + " node-name "_trace_t *t = + vlib_add_trace (vm, node, b0, sizeof (*t)); + t->sw_if_index = sw_if_index0; + t->next_index = next0; + } + if (b1->flags & VLIB_BUFFER_IS_TRACED) + { + " node-name "_trace_t *t = + vlib_add_trace (vm, node, b1, sizeof (*t)); + t->sw_if_index = sw_if_index1; + t->next_index = next1; + } + } + + /* verify speculative enqueues, maybe switch current next frame */ + vlib_validate_buffer_enqueue_x2 (vm, node, next_index, + to_next, n_left_to_next, + bi0, bi1, next0, next1); + } + + while (n_left_from > 0 && n_left_to_next > 0) + { + u32 bi0; + vlib_buffer_t * b0; + u32 next0 = " uc-node-name "_NEXT_INTERFACE_OUTPUT; + u32 sw_if_index0; + u8 tmp0[6]; + ethernet_header_t *en0; + + /* speculatively enqueue b0 to the current next frame */ + bi0 = from[0]; + to_next[0] = bi0; + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + b0 = vlib_get_buffer (vm, bi0); + + /* $$$$$ Single loop: process 1 packet here $$$$$ */ + + /* + * Direct from the driver, we should be at offset 0 + * aka at &b0->data[0] + */ + ASSERT (b0->current_data == 0); + + en0 = vlib_buffer_get_current (b0); + + /* This is not the fastest way to swap src + dst mac addresses */ +#define _(a) tmp0[a] = en0->src_address[a]; + foreach_mac_address_offset; +#undef _ +#define _(a) en0->src_address[a] = en0->dst_address[a]; + foreach_mac_address_offset; +#undef _ +#define _(a) en0->dst_address[a] = tmp0[a]; + foreach_mac_address_offset; +#undef _ + + sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX]; + + /* Send pkt back out the RX interface */ + vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0; + + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) + && (b0->flags & VLIB_BUFFER_IS_TRACED))) + { + " node-name "_trace_t *t = + vlib_add_trace (vm, node, b0, sizeof (*t)); + t->sw_if_index = sw_if_index0; + t->next_index = next0; + } + + pkts_swapped += 1; + + /* $$$$$ Done processing 1 packet here $$$$$ */ + + /* verify speculative enqueue, maybe switch current next frame */ + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, + to_next, n_left_to_next, + bi0, next0); + } + + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + + vlib_node_increment_counter (vm, " node-name "_node.index, + " uc-node-name "_ERROR_SWAPPED, pkts_swapped); + return frame->n_vectors; +} + +VLIB_REGISTER_NODE (" node-name "_node) = { + .function = " node-name "_node_fn, + .name = \"" node-name "\", + .vector_size = sizeof (u32), + .format_trace = format_" node-name "_trace, + .type = VLIB_NODE_TYPE_INTERNAL, + + .n_errors = ARRAY_LEN(" node-name "_error_strings), + .error_strings = " node-name "_error_strings, + + .n_next_nodes = " uc-node-name "_N_NEXT, + + /* edit / add dispositions here */ + .next_nodes = { + [" uc-node-name "_NEXT_INTERFACE_OUTPUT] = \"interface-output\", + }, +}; + +clib_error_t *" node-name "_init (vlib_main_t *vm) +{ + " node-name "_main_t *msm = &" node-name "_main; + + /* $$$$$ Initialize " node-name "_main_t structure here. $$$$$ */ + msm->vlib_main = vm; + msm->vnet_main = vnet_get_main(); + msm->ethernet_main = ethernet_get_main(vm); + + return 0; +} + +VLIB_INIT_FUNCTION(" node-name "_init); +") + diff --git a/extras/emacs/elog-4-int-skel.el b/extras/emacs/elog-4-int-skel.el new file mode 100644 index 00000000000..fda412a7bb8 --- /dev/null +++ b/extras/emacs/elog-4-int-skel.el @@ -0,0 +1,31 @@ +;;; elog-4-int-skel.el - 4 integer elog skeleton + +(require 'skeleton) + +(define-skeleton skel-elog-4-int +"Insert a skeleton 4-integer event definition" +nil +'(setq function-name (skeleton-read "Function: ")) +'(setq label (skeleton-read "Label: ")) + +" + +/* $$$ May or may not be needed */ +#include +#include + +static inline void " function-name " (u32 *data) +{ + ELOG_TYPE_DECLARE(e) = + { + .format = \"" label ": first %d second %d third %d fourth %d\", + .format_args = \"i4i4i4i4\", + }; + struct { u32 data[4];} * ed; + ed = ELOG_DATA (&vlib_global_main.elog_main, e); + ed->data[0] = data[0]; + ed->data[1] = data[1]; + ed->data[2] = data[2]; + ed->data[3] = data[3]; +} +") diff --git a/extras/emacs/elog-4-int-track-skel.el b/extras/emacs/elog-4-int-track-skel.el new file mode 100644 index 00000000000..506cc3a2aa9 --- /dev/null +++ b/extras/emacs/elog-4-int-track-skel.el @@ -0,0 +1,34 @@ +;;; elog-4-int-skel.el - 4 integer elog skeleton + +(require 'skeleton) + +(define-skeleton skel-elog-4-int-track +"Insert a skeleton 4-integer-with-track event definition" +nil +'(setq function-name (skeleton-read "Function: ")) +'(setq track-label (skeleton-read "Track Label: ")) +'(setq label (skeleton-read "Label: ")) + +" + +/* $$$ May or may not be needed */ +#include +#include + +static inline void " function-name " (u32 *data) +{ + ELOG_TYPE_DECLARE(e) = + { + .format = \"" label ": first %d second %d third %d fourth %d\", + .format_args = \"i4i4i4i4\", + }; + struct { u32 data[4];} * ed; + ELOG_TRACK(" track-label "); + ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, " track-label "); + ed->data[0] = data[0]; + ed->data[1] = data[1]; + ed->data[2] = data[2]; + ed->data[3] = data[3]; +} + +") diff --git a/extras/emacs/elog-enum-skel.el b/extras/emacs/elog-enum-skel.el new file mode 100644 index 00000000000..836ce86a9d9 --- /dev/null +++ b/extras/emacs/elog-enum-skel.el @@ -0,0 +1,35 @@ +;;; elog-enum-skel.el - enum elog skeleton + +(require 'skeleton) + +(define-skeleton skel-elog-enum +"Insert a skeleton enum event definition" +nil +'(setq function-name (skeleton-read "Function: ")) +'(setq label (skeleton-read "Label: ")) + +" + +/* $$$ May or may not be needed */ +#include +#include + +static inline void " function-name " (u8 which) +{ + ELOG_TYPE_DECLARE (e) = + { + .format = \"" label ": %s\", + .format_args = \"t1\", + .n_enum_strings = 2, + .enum_strings = + { + \"string 1\", + \"string 2\", + }, + }; + struct { u8 which;} * ed; + ed = ELOG_DATA (&vlib_global_main.elog_main, e); + ed->which = which; +} + +") diff --git a/extras/emacs/elog-one-datum-skel.el b/extras/emacs/elog-one-datum-skel.el new file mode 100644 index 00000000000..11f8389619a --- /dev/null +++ b/extras/emacs/elog-one-datum-skel.el @@ -0,0 +1,28 @@ +;;; elog-one-datum-skel.el - single u32 datum elog skeleton + +(require 'skeleton) + +(define-skeleton skel-elog-one-datum +"Insert a skeleton single datum event definition" +nil +'(setq function-name (skeleton-read "Function: ")) +'(setq label (skeleton-read "Label: ")) + +" + +/* $$$ May or may not be needed */ +#include +#include + +static inline void " function-name " (u32 data) +{ + ELOG_TYPE_DECLARE (e) = + { + .format = \"" label ": %d\", + .format_args = \"i4\", + }; + + elog (&vlib_global_main.elog_main, &e, data); +} + +") diff --git a/extras/emacs/fix-coding-style.el b/extras/emacs/fix-coding-style.el new file mode 100755 index 00000000000..0bb63483ffb --- /dev/null +++ b/extras/emacs/fix-coding-style.el @@ -0,0 +1,162 @@ +#!/usr/bin/emacs --script + +;; Insert style boilerplate if it's not already there +;; +;; Breaking the string in half keeps emacs +;; from trying to interpret the local variable +;; settings e.g. when it reads the lisp source code + +(defun insert-style-boilerplate () (interactive) + (save-excursion + (goto-char (point-min)) + (if (eq nil (search-forward "coding-style-patch-verification" + (point-max) t)) + (let ((junk 0)) (goto-char (point-max)) + (insert " +/* + * fd.io coding-style-patch-verification: ON + * + * Local Var" "iables: + * eval: (c-set-style \"gnu\") + * End: + */"))))) + +;; (cons xxx ) means insert xxx at the head of +;; Build a sorted list of *INDENT-OFF* lines, by searching +;; backwards. The initial (setq indent-offset-list nil) +;; results in (cdr ) nil, which makes it a proper list + +(defun find-indent-offs () (interactive) + (save-excursion + (if (boundp 'indent-offset-list) + (makunbound 'indent-offset-list)) + (setq indent-offset-list nil) + (goto-char (point-max)) + (while (search-backward "*INDENT-OFF*" (point-min) t) + (move-beginning-of-line nil) + (setq indent-offset-list (cons (point) indent-offset-list)) + (previous-line)))) + +;; Insert indent-off ... indent-on brackets around +;; a certain xxx_foreach macro, etc. which "indent" +;; completely screws up. Doesn't handle nesting, of which there +;; are few examples (fortunately). + +(defun fix-initializer (what) (interactive) + (find-indent-offs) + (save-excursion + (goto-char (point-min)) + (while (search-forward-regexp what (point-max) t) + (move-beginning-of-line nil) + (previous-line) + (let ((index 0)(pointval 0)) + (while (and (< pointval (point))(elt indent-offset-list index)) + (setq pointval (elt indent-offset-list index)) + (setq index (1+ index))) + (if (not (eq pointval (point))) + (let ((junk 0)) + (next-line) + (open-line 1) + (c-indent-line-or-region) + (insert "/* *INDENT-OFF* */") + (search-forward "{") + (backward-char) + (forward-sexp) + (move-end-of-line nil) + (newline 1) + (c-indent-line-or-region) + (insert "/* *INDENT-ON* */") + (find-indent-offs)) + (search-forward "*INDENT-ON*")))))) + +(defun fix-pool-foreach () (interactive) + (fix-initializer "pool_foreach *(")) + +(defun fix-pool-foreach-index () (interactive) + (fix-initializer "pool_foreach_index *(")) + +(defun fix-hash-foreach () (interactive) + (fix-initializer "hash_foreach *(")) + +(defun fix-hash-foreach-pair () (interactive) + (fix-initializer "hash_foreach_pair *(")) + +(defun fix-hash-foreach-mem () (interactive) + (fix-initializer "hash_foreach_mem *(")) + +(defun fix-clib-fifo-foreach () (interactive) + (fix-initializer "clib_fifo_foreach *(")) + +(defun fix-clib-bitmap-foreach () (interactive) + (fix-initializer "clib_bitmap_foreach *(")) + +(defun fix-foreach-ip-interface-address () (interactive) + (fix-initializer "foreach_ip_interface_address *(")) + +(defun fix-vlib-register-thread () (interactive) + (fix-initializer "VLIB_REGISTER_THREAD *(")) + +(defun fix-vlib-cli-command () (interactive) + (fix-initializer "VLIB_CLI_COMMAND *(")) + +(defun fix-vlib-register-node () (interactive) + (fix-initializer "VLIB_REGISTER_NODE *(")) + +(defun fix-reply-macro2 () (interactive) + (fix-initializer "REPLY_MACRO2 *(")) + +(defun fix-vnet-device-class () (interactive) + (fix-initializer "VNET_DEVICE_CLASS *(")) + +(defun fix-vnet-hw-interface-class () (interactive) + (fix-initializer "VNET_HW_INTERFACE_CLASS *(")) + +(defun fix-clib-packed () (interactive) + (fix-initializer "CLIB_PACKED *(")) +(defun fix-vl-api-packed () (interactive) + (fix-initializer "VL_API_PACKED *(")) + +;; Driver routine which runs the set of functions +;; defined above, as well as the bottom boilerplate function + +(defun fd-io-styleify () (interactive) + (fix-pool-foreach) + (fix-pool-foreach-index) + (fix-hash-foreach) + (fix-hash-foreach-pair) + (fix-hash-foreach-mem) + (fix-foreach-ip-interface-address) + (fix-clib-fifo-foreach) + (fix-clib-bitmap-foreach) + (fix-vlib-register-thread) + (fix-vlib-cli-command) + (fix-vlib-register-node) + (fix-reply-macro2) + (fix-vnet-device-class) + (fix-vnet-hw-interface-class) + (fix-clib-packed) + (fix-vl-api-packed) + (insert-style-boilerplate) + (if (boundp 'indent-offset-list) + (makunbound 'indent-offset-list))) + +;; When run as a script, this sexp +;; walks the list of files supplied on the command line. +;; +;; (elt argv index) returns nil if you M-x eval-buffer +;; or M-x load-file the file, so we won't accidentally +;; evaluate (save-buffers-kill-emacs)... + +(let ((file-index 0)) + (if (elt argv file-index) + (while (elt argv file-index) + (find-file (elt argv file-index)) + (fd-io-styleify) + (message "Done %s..." (elt argv file-index)) + (setq file-index (1+ file-index)))) + (if (> file-index 0) + (let ((junk 0)) + (message "Save and quit...") + (save-buffers-kill-emacs t)))) + + diff --git a/extras/emacs/make-plugin.sh b/extras/emacs/make-plugin.sh new file mode 100755 index 00000000000..4985974cac5 --- /dev/null +++ b/extras/emacs/make-plugin.sh @@ -0,0 +1,4 @@ +#!/usr/bin/emacs --script +(load-file "./all-skel.el") +(make-plugin) +(save-some-buffers t) diff --git a/extras/emacs/periodic-skel.el b/extras/emacs/periodic-skel.el new file mode 100644 index 00000000000..a8f3ef6db82 --- /dev/null +++ b/extras/emacs/periodic-skel.el @@ -0,0 +1,86 @@ +;;; pipe-skel.el - pipelined graph node skeleton + +(require 'skeleton) + +(define-skeleton skel-periodic +"Insert a skeleton periodic process node" +nil +'(setq node-name (skeleton-read "Name: ")) +'(setq uc-node-name (upcase node-name)) +'(setq poll-period (skeleton-read "Poll period (f64 seconds, e.g. 10.0): ")) + +" +#define " uc-node-name "_POLL_PERIOD " poll-period " + +static uword +" node-name "_process (vlib_main_t * vm, + vlib_node_runtime_t * rt, + vlib_frame_t * f) +{ + f64 poll_time_remaining; + uword event_type, * event_data = 0; + + poll_time_remaining = " uc-node-name "_POLL_PERIOD; + while (1) { + int i; + + /* + * Sleep until next periodic call due, or until we receive event(s) + */ + poll_time_remaining = + vlib_process_wait_for_event_or_clock (vm, poll_time_remaining); + + event_type = vlib_process_get_events (vm, &event_data); + switch (event_type) { + case ~0: /* no events => timeout */ + break; + + /* + * $$$$ FIXME: add cases / handlers for each event type + */ + case EVENT1: + for (i = 0; i < vec_len (event_data); i++) + handle_event1 (mm, event_data[i]); + break; + + case EVENT2: + for (i = 0; i < vec_len (event_data); i++) + handle_event2 (vm, event_data[i]); + break; + + /* ... and so forth for each event type */ + + default: + /* This should never happen... */ + clib_warning (\"BUG: unhandled event type %d\", event_type); + break; + } + if (event_data) + _vec_len (event_data) = 0; + + /* Timer expired, call periodic function */ + if (vlib_process_suspend_time_is_zero (poll_time_remaining)) { + " node-name "_periodic (vm); + poll_time_remaining = " uc-node-name "_POLL_PERIOD; + } + } + + return 0; +} + +/* + * " node-name " periodic node declaration + */ +static VLIB_REGISTER_NODE (" node-name "_node) = { + .function = " node-name "_process, + .type = VLIB_NODE_TYPE_PROCESS, + .name = \"" node-name "-process\", +}; + +/* + * To signal an event: + * + * vlib_process_signal_event (vm, " node-name "_node.index, EVENTn, datum); + * + */ +") diff --git a/extras/emacs/pipe-skel.el b/extras/emacs/pipe-skel.el new file mode 100644 index 00000000000..911e0d07453 --- /dev/null +++ b/extras/emacs/pipe-skel.el @@ -0,0 +1,132 @@ +;;; pipe-skel.el - pipelined graph node skeleton + +(require 'skeleton) + +(define-skeleton skel-pipeline-node +"Insert a skeleton pipelined graph node" +nil +'(setq node-name (skeleton-read "Node Name: ")) +'(setq uc-node-name (upcase node-name)) +'(setq nstages (skeleton-read "Number of pipeline stages: ")) +" +#include +#include + +/* + * Dump these counters via the \"show error\" CLI command + * FIXME: Add packet counter / error strings as desired + */ + +#define foreach_" node-name "_error \\ +_(ERROR1, \"sample counter/ error string\") + +static char * " node-name "_error_strings[] = { +#define _(sym,string) string, + foreach_" node-name "_error +#undef _ +}; + +/* + * packet error / counter enumeration + * + * To count and drop a vlib_buffer_t *b: + * + * Set b->error = node->errors[" uc-node-name "_ERROR_xxx]; + * last_stage returns a disposition index bound to \"error-drop\" + * + * To manually increment the specific counter " uc-node-name "_ERROR1 + * + * vlib_node_t *n = vlib_get_node (vm, " node-name ".index); + * u32 node_counter_base_index = n->error_heap_index; + * vlib_error_main_t * em = &vm->error_main; + * em->counters[node_counter_base_index + " uc-node-name "_ERROR1] += 1; + * + */ + +typedef enum { +#define _(sym,str) " uc-node-name "_ERROR_##sym, + foreach_" node-name "_error +#undef _ + " uc-node-name "_N_ERROR, +} " node-name "_error_t; + +/* + * enumeration of per-packet dispositions + * FIXME: add dispositions as desired + */ + +typedef enum { \n" +" " uc-node-name "_NEXT_NORMAL,\n" +" " uc-node-name "_N_NEXT, +} " node-name "_next_t; + +#define NSTAGES " nstages " + +/* + * Use the generic buffer metadata + first line of packet data prefetch + * stage function from . This is usually a Good Idea. + */ +#define stage0 generic_stage0 + +/* + * FIXME: add stage functions. Here is the function prototype: + * + * static inline void stageN (vlib_main_t * vm, + * vlib_node_runtime_t * node, + * u32 buffer_index) + */ + +/* + * FIXME: the last pipeline stage returns the desired pkt next node index, + * from the " node-name "_next_t enum above + */ +static inline u32 last_stage (vlib_main_t *vm, vlib_node_runtime_t *node, + u32 bi) +{ + vlib_buffer_t *b = vlib_get_buffer (vm, bi); + + b->error = node->errors[EXAMPLE_ERROR_ERROR1]; + + return " uc-node-name "_NEXT_NORMAL; +} + +#include + +static uword " node-name "_node_fn (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + return dispatch_pipeline (vm, node, frame); +} + +static VLIB_REGISTER_NODE (example_node) = { + .function = " node-name "_node_fn, + .name = \"" node-name "-node\", + .vector_size = sizeof (u32), + .type = VLIB_NODE_TYPE_INTERNAL, + + .n_errors = ARRAY_LEN(" node-name "_error_strings), + .error_strings = " node-name "_error_strings, + + .n_next_nodes = " uc-node-name "_N_NEXT, + + /* edit / add dispositions here */ + .next_nodes = { + [" uc-node-name "_NEXT_NORMAL] = \"error-drop\", + }, +}; + +/* + * packet generator definition to push superframes of data into the + * new graph node. Cut and paste into , then + * \"exec \", \"pa enable test\" at the QVNET prompt... + * +packet-generator new { + name test + limit 100 + node " node-name "-node + size 374-374 + data { hex 0x02b46b96000100096978676265000500bf436973636f20494f5320536f6674776172652c2043333735304520536f66747761726520284333373530452d554e4956455253414c2d4d292c2056657273696f6e2031322e32283335295345352c2052454c4541534520534f4654574152452028666331290a436f707972696768742028632920313938362d3230303720627920436973636f2053797374656d732c20496e632e0a436f6d70696c6564205468752031392d4a756c2d30372031363a3137206279206e616368656e00060018636973636f2057532d4333373530452d3234544400020011000000010101cc0004000000000003001b54656e4769676162697445746865726e6574312f302f3100040008000000280008002400000c011200000000ffffffff010221ff000000000000001e7a50f000ff000000090004000a00060001000b0005010012000500001300050000160011000000010101cc000400000000001a00100000000100000000ffffffff } +} + */ +") diff --git a/extras/emacs/plugin-all-apih-skel.el b/extras/emacs/plugin-all-apih-skel.el new file mode 100644 index 00000000000..0f073f9cbfe --- /dev/null +++ b/extras/emacs/plugin-all-apih-skel.el @@ -0,0 +1,43 @@ +;;; plugin-all-apih-skel.el - vpp engine plug-in "all-apih.c" skeleton +;;; +;;; Copyright (c) 2016 Cisco and/or its affiliates. +;;; Licensed under the Apache License, Version 2.0 (the "License"); +;;; you may not use this file except in compliance with the License. +;;; You may obtain a copy of the License at: +;;; +;;; http://www.apache.org/licenses/LICENSE-2.0 +;;; +;;; Unless required by applicable law or agreed to in writing, software +;;; distributed under the License is distributed on an "AS IS" BASIS, +;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +;;; See the License for the specific language governing permissions and +;;; limitations under the License. + +(require 'skeleton) + +(define-skeleton skel-plugin-all-apih +"Insert a plug-in 'all_api_h.h' skeleton " +nil +'(if (not (boundp 'plugin-name)) + (setq plugin-name (read-string "Plugin name: "))) +'(setq PLUGIN-NAME (upcase plugin-name)) +" +/* + * " plugin-name "_all_api_h.h - skeleton vpp engine plug-in api #include file + * + * Copyright (c) + * Licensed under the Apache License, Version 2.0 (the \"License\"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an \"AS IS\" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* Include the generated file, see BUILT_SOURCES in Makefile.am */ +#include <" plugin-name "/" plugin-name ".api.h> +") diff --git a/extras/emacs/plugin-am-skel.el b/extras/emacs/plugin-am-skel.el new file mode 100644 index 00000000000..5a082b4c002 --- /dev/null +++ b/extras/emacs/plugin-am-skel.el @@ -0,0 +1,60 @@ +;;; plugin-am-skel.el - vpp engine plug-in foo.am skeleton +;;; +;;; Copyright (c) 2016 Cisco and/or its affiliates. +;;; Licensed under the Apache License, Version 2.0 (the "License"); +;;; you may not use this file except in compliance with the License. +;;; You may obtain a copy of the License at: +;;; +;;; http://www.apache.org/licenses/LICENSE-2.0 +;;; +;;; Unless required by applicable law or agreed to in writing, software +;;; distributed under the License is distributed on an "AS IS" BASIS, +;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +;;; See the License for the specific language governing permissions and +;;; limitations under the License. + +(require 'skeleton) + +(define-skeleton skel-plugin-makefile-am-fragment +"Insert a plug-in 'foo.am' skeleton " +nil +'(if (not (boundp 'plugin-name)) + (setq plugin-name (read-string "Plugin name: "))) +'(setq PLUGIN-NAME (upcase plugin-name)) +" +# Copyright (c) +# Licensed under the Apache License, Version 2.0 (the \"License\"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an \"AS IS\" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +vppapitestplugins_LTLIBRARIES += " plugin-name "_test_plugin.la +vppplugins_LTLIBRARIES += " plugin-name "_plugin.la + +" plugin-name "_plugin_la_SOURCES = \\ + " plugin-name "/node.c \\ + " plugin-name "/" plugin-name ".c \\ + " plugin-name "/" plugin-name ".h \\ + " plugin-name "/" plugin-name "_all_api_h.h \\ + " plugin-name "/" plugin-name "_msg_enum.h + +API_FILES += " plugin-name "/" plugin-name ".api + +nobase_apiinclude_HEADERS += \\ + " plugin-name "/" plugin-name "_all_api_h.h \\ + " plugin-name "/" plugin-name "_msg_enum.h \\ + " plugin-name "/" plugin-name ".api.h + +" plugin-name "_test_plugin_la_SOURCES = \\ + " plugin-name "/" plugin-name "_test.c \\ + " plugin-name "/" plugin-name ".api.h + +# vi:syntax=automake +") diff --git a/extras/emacs/plugin-api-skel.el b/extras/emacs/plugin-api-skel.el new file mode 100644 index 00000000000..74519e70d83 --- /dev/null +++ b/extras/emacs/plugin-api-skel.el @@ -0,0 +1,48 @@ +;;; plugin-api-skel.el - vpp engine plug-in "all-apih.c" skeleton +;;; +;;; Copyright (c) 2016 Cisco and/or its affiliates. +;;; Licensed under the Apache License, Version 2.0 (the "License"); +;;; you may not use this file except in compliance with the License. +;;; You may obtain a copy of the License at: +;;; +;;; http://www.apache.org/licenses/LICENSE-2.0 +;;; +;;; Unless required by applicable law or agreed to in writing, software +;;; distributed under the License is distributed on an "AS IS" BASIS, +;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +;;; See the License for the specific language governing permissions and +;;; limitations under the License. + +(require 'skeleton) + +(define-skeleton skel-plugin-api +"Insert a plug-in '.api' skeleton " +nil +'(if (not (boundp 'plugin-name)) + (setq plugin-name (read-string "Plugin name: "))) +'(setq PLUGIN-NAME (upcase plugin-name)) +" +/* Define a simple enable-disable binary API to control the feature */ + +define " plugin-name "_enable_disable { + /* Client identifier, set from api_main.my_client_index */ + u32 client_index; + + /* Arbitrary context, so client can match reply to request */ + u32 context; + + /* Enable / disable the feature */ + u8 enable_disable; + + /* Interface handle */ + u32 sw_if_index; +}; + +define " plugin-name "_enable_disable_reply { + /* From the request */ + u32 context; + + /* Return value, zero means all OK */ + i32 retval; +}; +") diff --git a/extras/emacs/plugin-h-skel.el b/extras/emacs/plugin-h-skel.el new file mode 100644 index 00000000000..8bf9b6fed23 --- /dev/null +++ b/extras/emacs/plugin-h-skel.el @@ -0,0 +1,66 @@ +;;; plugin-h-skel.el - vpp engine plug-in "main.c" skeleton +;;; +;;; Copyright (c) 2016 Cisco and/or its affiliates. +;;; Licensed under the Apache License, Version 2.0 (the "License"); +;;; you may not use this file except in compliance with the License. +;;; You may obtain a copy of the License at: +;;; +;;; http://www.apache.org/licenses/LICENSE-2.0 +;;; +;;; Unless required by applicable law or agreed to in writing, software +;;; distributed under the License is distributed on an "AS IS" BASIS, +;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +;;; See the License for the specific language governing permissions and +;;; limitations under the License. + +(require 'skeleton) + +(define-skeleton skel-plugin-h +"Insert a plug-in 'main.c' skeleton " +nil +'(if (not (boundp 'plugin-name)) + (setq plugin-name (read-string "Plugin name: "))) +'(setq PLUGIN-NAME (upcase plugin-name)) +" +/* + * " plugin-name ".h - skeleton vpp engine plug-in header file + * + * Copyright (c) + * Licensed under the Apache License, Version 2.0 (the \"License\"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an \"AS IS\" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __included_" plugin-name "_h__ +#define __included_" plugin-name "_h__ + +#include +#include +#include + +#include +#include + +typedef struct { + /* API message ID base */ + u16 msg_id_base; + + /* convenience */ + vlib_main_t * vlib_main; + vnet_main_t * vnet_main; + ethernet_main_t * ethernet_main; +} " plugin-name "_main_t; + +" plugin-name "_main_t " plugin-name "_main; + +vlib_node_registration_t " plugin-name "_node; + +#endif /* __included_" plugin-name "_h__ */ +") diff --git a/extras/emacs/plugin-main-skel.el b/extras/emacs/plugin-main-skel.el new file mode 100644 index 00000000000..46615c311c7 --- /dev/null +++ b/extras/emacs/plugin-main-skel.el @@ -0,0 +1,280 @@ +;;; plugin-main-skel.el - vpp engine plug-in "main.c" skeleton +;;; +;;; Copyright (c) 2016 Cisco and/or its affiliates. +;;; Licensed under the Apache License, Version 2.0 (the "License"); +;;; you may not use this file except in compliance with the License. +;;; You may obtain a copy of the License at: +;;; +;;; http://www.apache.org/licenses/LICENSE-2.0 +;;; +;;; Unless required by applicable law or agreed to in writing, software +;;; distributed under the License is distributed on an "AS IS" BASIS, +;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +;;; See the License for the specific language governing permissions and +;;; limitations under the License. + +(require 'skeleton) + +(define-skeleton skel-plugin-main +"Insert a plug-in 'main.c' skeleton " +nil +'(if (not (boundp 'plugin-name)) + (setq plugin-name (read-string "Plugin name: "))) +'(setq PLUGIN-NAME (upcase plugin-name)) +'(setq capital-oh-en "ON") +"/* + * " plugin-name ".c - skeleton vpp engine plug-in + * + * Copyright (c) + * Licensed under the Apache License, Version 2.0 (the \"License\"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an \"AS IS\" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include <" plugin-name "/" plugin-name ".h> + +#include +#include +#include +#include + +/* define message IDs */ +#include <" plugin-name "/" plugin-name "_msg_enum.h> + +/* define message structures */ +#define vl_typedefs +#include <" plugin-name "/" plugin-name "_all_api_h.h> +#undef vl_typedefs + +/* define generated endian-swappers */ +#define vl_endianfun +#include <" plugin-name "/" plugin-name "_all_api_h.h> +#undef vl_endianfun + +/* instantiate all the print functions we know about */ +#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) +#define vl_printfun +#include <" plugin-name "/" plugin-name "_all_api_h.h> +#undef vl_printfun + +/* Get the API version number */ +#define vl_api_version(n,v) static u32 api_version=(v); +#include <" plugin-name "/" plugin-name "_all_api_h.h> +#undef vl_api_version + +/* + * A handy macro to set up a message reply. + * Assumes that the following variables are available: + * mp - pointer to request message + * rmp - pointer to reply message type + * rv - return value + */ + +#define REPLY_MACRO(t) \\ +do { \\ + unix_shared_memory_queue_t * q = \\ + vl_api_client_index_to_input_queue (mp->client_index); \\ + if (!q) \\ + return; \\ + \\ + rmp = vl_msg_api_alloc (sizeof (*rmp)); \\ + rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \\ + rmp->context = mp->context; \\ + rmp->retval = ntohl(rv); \\ + \\ + vl_msg_api_send_shmem (q, (u8 *)&rmp); \\ +} while(0); + + +/* List of message types that this plugin understands */ + +#define foreach_" plugin-name "_plugin_api_msg \\ +_(" PLUGIN-NAME "_ENABLE_DISABLE, " plugin-name "_enable_disable) + +/* Action function shared between message handler and debug CLI */ + +int " plugin-name "_enable_disable (" plugin-name "_main_t * sm, u32 sw_if_index, + int enable_disable) +{ + vnet_sw_interface_t * sw; + int rv = 0; + + /* Utterly wrong? */ + if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces, + sw_if_index)) + return VNET_API_ERROR_INVALID_SW_IF_INDEX; + + /* Not a physical port? */ + sw = vnet_get_sw_interface (sm->vnet_main, sw_if_index); + if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE) + return VNET_API_ERROR_INVALID_SW_IF_INDEX; + + vnet_feature_enable_disable (\"device-input\", \"" plugin-name "\", + sw_if_index, enable_disable, 0, 0); + + return rv; +} + +static clib_error_t * +" plugin-name "_enable_disable_command_fn (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + " plugin-name "_main_t * sm = &" plugin-name "_main; + u32 sw_if_index = ~0; + int enable_disable = 1; + + int rv; + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (input, \"disable\")) + enable_disable = 0; + else if (unformat (input, \"%U\", unformat_vnet_sw_interface, + sm->vnet_main, &sw_if_index)) + ; + else + break; + } + + if (sw_if_index == ~0) + return clib_error_return (0, \"Please specify an interface...\"); + + rv = " plugin-name "_enable_disable (sm, sw_if_index, enable_disable); + + switch(rv) + { + case 0: + break; + + case VNET_API_ERROR_INVALID_SW_IF_INDEX: + return clib_error_return + (0, \"Invalid interface, only works on physical ports\"); + break; + + case VNET_API_ERROR_UNIMPLEMENTED: + return clib_error_return (0, \"Device driver doesn't support redirection\"); + break; + + default: + return clib_error_return (0, \"" plugin-name "_enable_disable returned %d\", + rv); + } + return 0; +} + +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (" plugin-name "_enable_disable_command, static) = +{ + .path = \"" plugin-name " enable-disable\", + .short_help = + \"" plugin-name " enable-disable [disable]\", + .function = " plugin-name "_enable_disable_command_fn, +}; +/* *INDENT-ON* */ + +/* API message handler */ +static void vl_api_" plugin-name "_enable_disable_t_handler +(vl_api_" plugin-name "_enable_disable_t * mp) +{ + vl_api_" plugin-name "_enable_disable_reply_t * rmp; + " plugin-name "_main_t * sm = &" plugin-name "_main; + int rv; + + rv = " plugin-name "_enable_disable (sm, ntohl(mp->sw_if_index), + (int) (mp->enable_disable)); + + REPLY_MACRO(VL_API_" PLUGIN-NAME "_ENABLE_DISABLE_REPLY); +} + +/* Set up the API message handling tables */ +static clib_error_t * +" plugin-name "_plugin_api_hookup (vlib_main_t *vm) +{ + " plugin-name "_main_t * sm = &" plugin-name "_main; +#define _(N,n) \\ + vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \\ + #n, \\ + vl_api_##n##_t_handler, \\ + vl_noop_handler, \\ + vl_api_##n##_t_endian, \\ + vl_api_##n##_t_print, \\ + sizeof(vl_api_##n##_t), 1); + foreach_" plugin-name "_plugin_api_msg; +#undef _ + + return 0; +} + +#define vl_msg_name_crc_list +#include <" plugin-name "/" plugin-name "_all_api_h.h> +#undef vl_msg_name_crc_list + +static void +setup_message_id_table (" plugin-name "_main_t * sm, api_main_t * am) +{ +#define _(id,n,crc) \ + vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base); + foreach_vl_msg_name_crc_" plugin-name" ; +#undef _ +} + +static clib_error_t * " plugin-name "_init (vlib_main_t * vm) +{ + " plugin-name "_main_t * sm = &" plugin-name "_main; + clib_error_t * error = 0; + u8 * name; + + name = format (0, \"" plugin-name "_%08x%c\", api_version, 0); + + /* Ask for a correctly-sized block of API message decode slots */ + sm->msg_id_base = vl_msg_api_get_msg_ids + ((char *) name, VL_MSG_FIRST_AVAILABLE); + + error = " plugin-name "_plugin_api_hookup (vm); + + /* Add our API messages to the global name_crc hash table */ + setup_message_id_table (sm, &api_main); + + vec_free(name); + + return error; +} + +VLIB_INIT_FUNCTION (" plugin-name "_init); + +/* *INDENT-OFF* */ +VNET_FEATURE_INIT (" plugin-name ", static) = +{ + .arc_name = \"device-input\", + .node_name = \"" plugin-name "\", + .runs_before = VNET_FEATURES (\"ethernet-input\"), +}; +/* *INDENT-ON */ + +/* *INDENT-OFF* */ +VLIB_PLUGIN_REGISTER () = +{ + .version = VPP_BUILD_VER, +}; +/* *INDENT-ON* */ + +/* + * fd.io coding-style-patch-verification: " capital-oh-en " + * + * Local Variables: + * eval: (c-set-style \"gnu\") + * End: + */ +") + diff --git a/extras/emacs/plugin-msg-enum-skel.el b/extras/emacs/plugin-msg-enum-skel.el new file mode 100644 index 00000000000..c44af051982 --- /dev/null +++ b/extras/emacs/plugin-msg-enum-skel.el @@ -0,0 +1,55 @@ +;;; plugin-msg-enum-skel.el - vpp engine plug-in message enum skeleton +;;; +;;; Copyright (c) 2016 Cisco and/or its affiliates. +;;; Licensed under the Apache License, Version 2.0 (the "License"); +;;; you may not use this file except in compliance with the License. +;;; You may obtain a copy of the License at: +;;; +;;; http://www.apache.org/licenses/LICENSE-2.0 +;;; +;;; Unless required by applicable law or agreed to in writing, software +;;; distributed under the License is distributed on an "AS IS" BASIS, +;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +;;; See the License for the specific language governing permissions and +;;; limitations under the License. + +(require 'skeleton) + +(define-skeleton skel-plugin-msg-enum +"Insert a plug-in message enumeration skeleton " +nil +'(if (not (boundp 'plugin-name)) + (setq plugin-name (read-string "Plugin name: "))) +'(setq PLUGIN-NAME (upcase plugin-name)) +" +/* + * " plugin-name "_msg_enum.h - skeleton vpp engine plug-in message enumeration + * + * Copyright (c) + * Licensed under the Apache License, Version 2.0 (the \"License\"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an \"AS IS\" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef included_" plugin-name "_msg_enum_h +#define included_" plugin-name "_msg_enum_h + +#include + +#define vl_msg_id(n,h) n, +typedef enum { +#include <" plugin-name "/" plugin-name "_all_api_h.h> + /* We'll want to know how many messages IDs we need... */ + VL_MSG_FIRST_AVAILABLE, +} vl_msg_id_t; +#undef vl_msg_id + +#endif /* included_" plugin-name "_msg_enum_h */ +") diff --git a/extras/emacs/plugin-node-skel.el b/extras/emacs/plugin-node-skel.el new file mode 100644 index 00000000000..167519aa266 --- /dev/null +++ b/extras/emacs/plugin-node-skel.el @@ -0,0 +1,330 @@ +;;; plugin-node-skel.el - vpp engine plug-in "node.c" skeleton +;;; +;;; Copyright (c) 2016 Cisco and/or its affiliates. +;;; Licensed under the Apache License, Version 2.0 (the "License"); +;;; you may not use this file except in compliance with the License. +;;; You may obtain a copy of the License at: +;;; +;;; http://www.apache.org/licenses/LICENSE-2.0 +;;; +;;; Unless required by applicable law or agreed to in writing, software +;;; distributed under the License is distributed on an "AS IS" BASIS, +;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +;;; See the License for the specific language governing permissions and +;;; limitations under the License. + +(require 'skeleton) + +(define-skeleton skel-plugin-node +"Insert a plug-in 'node.c' skeleton " +nil +'(if (not (boundp 'plugin-name)) + (setq plugin-name (read-string "Plugin name: "))) +'(setq PLUGIN-NAME (upcase plugin-name)) +'(setq capital-oh-en "ON") +"/* + * node.c - skeleton vpp engine plug-in dual-loop node skeleton + * + * Copyright (c) + * Licensed under the Apache License, Version 2.0 (the \"License\"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an \"AS IS\" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +#include <" plugin-name "/" plugin-name ".h> + +typedef struct +{ + u32 next_index; + u32 sw_if_index; + u8 new_src_mac[6]; + u8 new_dst_mac[6]; +} " plugin-name "_trace_t; + +static u8 * +format_mac_address (u8 * s, va_list * args) +{ + u8 *a = va_arg (*args, u8 *); + return format (s, \"%02x:%02x:%02x:%02x:%02x:%02x\", + a[0], a[1], a[2], a[3], a[4], a[5]); +} + +/* packet trace format function */ +static u8 * format_" plugin-name "_trace (u8 * s, va_list * args) +{ + CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); + CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); + " plugin-name "_trace_t * t = va_arg (*args, " plugin-name "_trace_t *); + + s = format (s, \"" PLUGIN-NAME ": sw_if_index %d, next index %d\\n\", + t->sw_if_index, t->next_index); + s = format (s, \" new src %U -> new dst %U\", + format_mac_address, t->new_src_mac, + format_mac_address, t->new_dst_mac); + return s; +} + +vlib_node_registration_t " plugin-name "_node; + +#define foreach_" plugin-name "_error \\ +_(SWAPPED, \"Mac swap packets processed\") + +typedef enum { +#define _(sym,str) " PLUGIN-NAME "_ERROR_##sym, + foreach_" plugin-name "_error +#undef _ + " PLUGIN-NAME "_N_ERROR, +} " plugin-name "_error_t; + +static char * " plugin-name "_error_strings[] = { +#define _(sym,string) string, + foreach_" plugin-name "_error +#undef _ +}; + +typedef enum +{ + " PLUGIN-NAME "_NEXT_INTERFACE_OUTPUT, + " PLUGIN-NAME "_N_NEXT, +} " plugin-name "_next_t; + +#define foreach_mac_address_offset \\ +_(0) \\ +_(1) \\ +_(2) \\ +_(3) \\ +_(4) \\ +_(5) + +static uword +" plugin-name "_node_fn (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + u32 n_left_from, * from, * to_next; + " plugin-name "_next_t next_index; + u32 pkts_swapped = 0; + + from = vlib_frame_vector_args (frame); + n_left_from = frame->n_vectors; + next_index = node->cached_next_index; + + while (n_left_from > 0) + { + u32 n_left_to_next; + + vlib_get_next_frame (vm, node, next_index, + to_next, n_left_to_next); + + while (n_left_from >= 4 && n_left_to_next >= 2) + { + u32 next0 = " PLUGIN-NAME "_NEXT_INTERFACE_OUTPUT; + u32 next1 = " PLUGIN-NAME "_NEXT_INTERFACE_OUTPUT; + u32 sw_if_index0, sw_if_index1; + u8 tmp0[6], tmp1[6]; + ethernet_header_t *en0, *en1; + u32 bi0, bi1; + vlib_buffer_t * b0, * b1; + + /* Prefetch next iteration. */ + { + vlib_buffer_t * p2, * p3; + + p2 = vlib_get_buffer (vm, from[2]); + p3 = vlib_get_buffer (vm, from[3]); + + vlib_prefetch_buffer_header (p2, LOAD); + vlib_prefetch_buffer_header (p3, LOAD); + + CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE); + CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE); + } + + /* speculatively enqueue b0 and b1 to the current next frame */ + to_next[0] = bi0 = from[0]; + to_next[1] = bi1 = from[1]; + from += 2; + to_next += 2; + n_left_from -= 2; + n_left_to_next -= 2; + + b0 = vlib_get_buffer (vm, bi0); + b1 = vlib_get_buffer (vm, bi1); + + ASSERT (b0->current_data == 0); + ASSERT (b1->current_data == 0); + + en0 = vlib_buffer_get_current (b0); + en1 = vlib_buffer_get_current (b1); + + /* This is not the fastest way to swap src + dst mac addresses */ +#define _(a) tmp0[a] = en0->src_address[a]; + foreach_mac_address_offset; +#undef _ +#define _(a) en0->src_address[a] = en0->dst_address[a]; + foreach_mac_address_offset; +#undef _ +#define _(a) en0->dst_address[a] = tmp0[a]; + foreach_mac_address_offset; +#undef _ + +#define _(a) tmp1[a] = en1->src_address[a]; + foreach_mac_address_offset; +#undef _ +#define _(a) en1->src_address[a] = en1->dst_address[a]; + foreach_mac_address_offset; +#undef _ +#define _(a) en1->dst_address[a] = tmp1[a]; + foreach_mac_address_offset; +#undef _ + + sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX]; + sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX]; + + /* Send pkt back out the RX interface */ + vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0; + vnet_buffer(b1)->sw_if_index[VLIB_TX] = sw_if_index1; + + pkts_swapped += 2; + + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE))) + { + if (b0->flags & VLIB_BUFFER_IS_TRACED) + { + " plugin-name "_trace_t *t = + vlib_add_trace (vm, node, b0, sizeof (*t)); + t->sw_if_index = sw_if_index0; + t->next_index = next0; + clib_memcpy (t->new_src_mac, en0->src_address, + sizeof (t->new_src_mac)); + clib_memcpy (t->new_dst_mac, en0->dst_address, + sizeof (t->new_dst_mac)); + } + if (b1->flags & VLIB_BUFFER_IS_TRACED) + { + " plugin-name "_trace_t *t = + vlib_add_trace (vm, node, b1, sizeof (*t)); + t->sw_if_index = sw_if_index1; + t->next_index = next1; + clib_memcpy (t->new_src_mac, en1->src_address, + sizeof (t->new_src_mac)); + clib_memcpy (t->new_dst_mac, en1->dst_address, + sizeof (t->new_dst_mac)); + } + } + + /* verify speculative enqueues, maybe switch current next frame */ + vlib_validate_buffer_enqueue_x2 (vm, node, next_index, + to_next, n_left_to_next, + bi0, bi1, next0, next1); + } + + while (n_left_from > 0 && n_left_to_next > 0) + { + u32 bi0; + vlib_buffer_t * b0; + u32 next0 = " PLUGIN-NAME "_NEXT_INTERFACE_OUTPUT; + u32 sw_if_index0; + u8 tmp0[6]; + ethernet_header_t *en0; + + /* speculatively enqueue b0 to the current next frame */ + bi0 = from[0]; + to_next[0] = bi0; + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + b0 = vlib_get_buffer (vm, bi0); + /* + * Direct from the driver, we should be at offset 0 + * aka at &b0->data[0] + */ + ASSERT (b0->current_data == 0); + + en0 = vlib_buffer_get_current (b0); + + /* This is not the fastest way to swap src + dst mac addresses */ +#define _(a) tmp0[a] = en0->src_address[a]; + foreach_mac_address_offset; +#undef _ +#define _(a) en0->src_address[a] = en0->dst_address[a]; + foreach_mac_address_offset; +#undef _ +#define _(a) en0->dst_address[a] = tmp0[a]; + foreach_mac_address_offset; +#undef _ + + sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX]; + + /* Send pkt back out the RX interface */ + vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0; + + if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) + && (b0->flags & VLIB_BUFFER_IS_TRACED))) { + " plugin-name "_trace_t *t = + vlib_add_trace (vm, node, b0, sizeof (*t)); + t->sw_if_index = sw_if_index0; + t->next_index = next0; + clib_memcpy (t->new_src_mac, en0->src_address, + sizeof (t->new_src_mac)); + clib_memcpy (t->new_dst_mac, en0->dst_address, + sizeof (t->new_dst_mac)); + } + + pkts_swapped += 1; + + /* verify speculative enqueue, maybe switch current next frame */ + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, + to_next, n_left_to_next, + bi0, next0); + } + + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + + vlib_node_increment_counter (vm, " plugin-name "_node.index, + " PLUGIN-NAME "_ERROR_SWAPPED, pkts_swapped); + return frame->n_vectors; +} + +/* *INDENT-OFF* */ +VLIB_REGISTER_NODE (" plugin-name "_node) = +{ + .function = " plugin-name "_node_fn, + .name = \"" plugin-name "\", + .vector_size = sizeof (u32), + .format_trace = format_" plugin-name "_trace, + .type = VLIB_NODE_TYPE_INTERNAL, + + .n_errors = ARRAY_LEN(" plugin-name "_error_strings), + .error_strings = " plugin-name "_error_strings, + + .n_next_nodes = " PLUGIN-NAME "_N_NEXT, + + /* edit / add dispositions here */ + .next_nodes = { + [" PLUGIN-NAME "_NEXT_INTERFACE_OUTPUT] = \"interface-output\", + }, +}; +/* *INDENT-ON* */ +/* + * fd.io coding-style-patch-verification: " capital-oh-en " + * + * Local Variables: + * eval: (c-set-style \"gnu\") + * End: + */ +") diff --git a/extras/emacs/plugin-test-skel.el b/extras/emacs/plugin-test-skel.el new file mode 100644 index 00000000000..837cbdaa1b1 --- /dev/null +++ b/extras/emacs/plugin-test-skel.el @@ -0,0 +1,212 @@ +;;; plugin-test-skel.el - vpp-api-test plug-in skeleton +;;; +;;; Copyright (c) 2016 Cisco and/or its affiliates. +;;; Licensed under the Apache License, Version 2.0 (the "License"); +;;; you may not use this file except in compliance with the License. +;;; You may obtain a copy of the License at: +;;; +;;; http://www.apache.org/licenses/LICENSE-2.0 +;;; +;;; Unless required by applicable law or agreed to in writing, software +;;; distributed under the License is distributed on an "AS IS" BASIS, +;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +;;; See the License for the specific language governing permissions and +;;; limitations under the License. + +(require 'skeleton) + +(define-skeleton skel-plugin-test +"Insert a plug-in vpp-api-test skeleton " +nil +'(if (not (boundp 'plugin-name)) + (setq plugin-name (read-string "Plugin name: "))) +'(setq PLUGIN-NAME (upcase plugin-name)) +'(setq capital-oh-en "ON") +"/* + * " plugin-name ".c - skeleton vpp-api-test plug-in + * + * Copyright (c) + * Licensed under the Apache License, Version 2.0 (the \"License\"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an \"AS IS\" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +#include + +uword unformat_sw_if_index (unformat_input_t * input, va_list * args); + +/* Declare message IDs */ +#include <" plugin-name "/" plugin-name "_msg_enum.h> + +/* define message structures */ +#define vl_typedefs +#include <" plugin-name "/" plugin-name "_all_api_h.h> +#undef vl_typedefs + +/* declare message handlers for each api */ + +#define vl_endianfun /* define message structures */ +#include <" plugin-name "/" plugin-name "_all_api_h.h> +#undef vl_endianfun + +/* instantiate all the print functions we know about */ +#define vl_print(handle, ...) +#define vl_printfun +#include <" plugin-name "/" plugin-name "_all_api_h.h> +#undef vl_printfun + +/* Get the API version number. */ +#define vl_api_version(n,v) static u32 api_version=(v); +#include <" plugin-name "/" plugin-name "_all_api_h.h> +#undef vl_api_version + + +typedef struct +{ + /* API message ID base */ + u16 msg_id_base; + vat_main_t *vat_main; +} " plugin-name "_test_main_t; + +" plugin-name "_test_main_t " plugin-name "_test_main; + +#define __plugin_msg_base " plugin-name"_test_main.msg_id_base +#include + +#define foreach_standard_reply_retval_handler \\ +_(" plugin-name "_enable_disable_reply) + +#define _(n) \\ + static void vl_api_##n##_t_handler \\ + (vl_api_##n##_t * mp) \\ + { \\ + vat_main_t * vam = " plugin-name "_test_main.vat_main; \\ + i32 retval = ntohl(mp->retval); \\ + if (vam->async_mode) { \\ + vam->async_errors += (retval < 0); \\ + } else { \\ + vam->retval = retval; \\ + vam->result_ready = 1; \\ + } \\ + } +foreach_standard_reply_retval_handler; +#undef _ + +/* + * Table of message reply handlers, must include boilerplate handlers + * we just generated + */ +#define foreach_vpe_api_reply_msg \\ +_(" PLUGIN-NAME "_ENABLE_DISABLE_REPLY, " plugin-name "_enable_disable_reply) + + +static int api_" plugin-name "_enable_disable (vat_main_t * vam) +{ + unformat_input_t * i = vam->input; + int enable_disable = 1; + u32 sw_if_index = ~0; + vl_api_" plugin-name "_enable_disable_t * mp; + int ret; + + /* Parse args required to build the message */ + while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) + { + if (unformat (i, \"%U\", unformat_sw_if_index, vam, &sw_if_index)) + ; + else if (unformat (i, \"sw_if_index %d\", &sw_if_index)) + ; + else if (unformat (i, \"disable\")) + enable_disable = 0; + else + break; + } + + if (sw_if_index == ~0) + { + errmsg (\"missing interface name / explicit sw_if_index number \\n\"); + return -99; + } + + /* Construct the API message */ + M(" PLUGIN-NAME "_ENABLE_DISABLE, mp); + mp->sw_if_index = ntohl (sw_if_index); + mp->enable_disable = enable_disable; + + /* send it... */ + S(mp); + + /* Wait for a reply... */ + W (ret); + return ret; +} + +/* + * List of messages that the api test plugin sends, + * and that the data plane plugin processes + */ +#define foreach_vpe_api_msg \\ +_(" plugin-name "_enable_disable, \" [disable]\") + +static void " plugin-name "_api_hookup (vat_main_t *vam) +{ + " plugin-name "_test_main_t * sm = &" plugin-name "_test_main; + /* Hook up handlers for replies from the data plane plug-in */ +#define _(N,n) \\ + vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \\ + #n, \\ + vl_api_##n##_t_handler, \\ + vl_noop_handler, \\ + vl_api_##n##_t_endian, \\ + vl_api_##n##_t_print, \\ + sizeof(vl_api_##n##_t), 1); + foreach_vpe_api_reply_msg; +#undef _ + + /* API messages we can send */ +#define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n); + foreach_vpe_api_msg; +#undef _ + + /* Help strings */ +#define _(n,h) hash_set_mem (vam->help_by_name, #n, h); + foreach_vpe_api_msg; +#undef _ +} + +clib_error_t * vat_plugin_register (vat_main_t *vam) +{ + " plugin-name "_test_main_t * sm = &" plugin-name "_test_main; + u8 * name; + + sm->vat_main = vam; + + /* Ask the vpp engine for the first assigned message-id */ + name = format (0, \"" plugin-name "_%08x%c\", api_version, 0); + sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name); + + if (sm->msg_id_base != (u16) ~0) + " plugin-name "_api_hookup (vam); + + vec_free(name); + + return 0; +} +/* + * fd.io coding-style-patch-verification: " capital-oh-en " + * + * Local Variables: + * eval: (c-set-style \"gnu\") + * End: + */ +") diff --git a/extras/emacs/plugin.el b/extras/emacs/plugin.el new file mode 100644 index 00000000000..4f1361a32f2 --- /dev/null +++ b/extras/emacs/plugin.el @@ -0,0 +1,32 @@ +(defun make-plugin () + "Create a plugin" + (interactive) + (save-excursion + (let (cd-args cmd-args start-dir) + (setq start-dir default-directory) + (makunbound 'plugin-name) + (makunbound 'PLUGIN-NAME) + (setq plugin-name (read-string "Plugin name: ")) + (setq PLUGIN-NAME (upcase plugin-name)) + (find-file (concat plugin-name ".am")) + (skel-plugin-makefile-am-fragment) + (setq cmd-args (concat "mkdir -p " plugin-name)) + (shell-command cmd-args) + (setq cd-args (concat start-dir "/" plugin-name)) + (setq default-directory cd-args) + (find-file (concat plugin-name ".api")) + (skel-plugin-api) + (find-file (concat plugin-name "_all_api_h.h")) + (skel-plugin-all-apih) + (find-file (concat plugin-name ".h")) + (skel-plugin-h) + (find-file (concat plugin-name ".c")) + (skel-plugin-main) + (find-file (concat plugin-name "_msg_enum.h")) + (skel-plugin-msg-enum) + (find-file "node.c") + (skel-plugin-node) + (find-file (concat plugin-name "_test.c")) + (skel-plugin-test) + (cd start-dir)))) + diff --git a/extras/emacs/tunnel-c-skel.el b/extras/emacs/tunnel-c-skel.el new file mode 100644 index 00000000000..a1b1757dab7 --- /dev/null +++ b/extras/emacs/tunnel-c-skel.el @@ -0,0 +1,452 @@ +;;; tunnel-c-skel.el - tunnel encap cli / api + +(require 'skeleton) + +(define-skeleton skel-tunnel-c +"Insert a tunnel cli/api implementation" +nil +'(setq encap_stack (skeleton-read "encap_stack (e.g ip4_udp_lisp): ")) +'(setq ENCAP_STACK (upcase encap_stack)) +'(setq encap-stack (replace-regexp-in-string "_" "-" encap_stack)) +" +#include + +" encap_stack "_main_t " encap_stack "_main; + +static u8 * format_decap_next (u8 * s, va_list * args) +{ + u32 next_index = va_arg (*args, u32); + + switch (next_index) + { + case " ENCAP_STACK "_INPUT_NEXT_DROP: + return format (s, \"drop\"); + case " ENCAP_STACK "_INPUT_NEXT_IP4_INPUT: + return format (s, \"ip4\"); + case " ENCAP_STACK "_INPUT_NEXT_IP6_INPUT: + return format (s, \"ip6\"); + case " ENCAP_STACK "_INPUT_NEXT_" ENCAP_STACK "_ENCAP: + return format (s, \"" encap-stack "\"); + default: + return format (s, \"unknown %d\", next_index); + } + return s; +} + +u8 * format_" encap_stack "_tunnel (u8 * s, va_list * args) +{ + " encap_stack "_tunnel_t * t = va_arg (*args, " encap_stack "_tunnel_t *); + " encap_stack "_main_t * ngm = &" encap_stack "_main; + + s = format (s, + \"[%d] %U (src) %U (dst) fibs: encap %d, decap %d\", + t - ngm->tunnels, + format_ip4_address, &t->src, + format_ip4_address, &t->dst, + t->encap_fib_index, + t->decap_fib_index); + + s = format (s, \" decap next %U\\n\", format_decap_next, t->decap_next_index); + /* FIXME: add protocol details */ + return s; +} + +static u8 * format_" encap_stack "_name (u8 * s, va_list * args) +{ + u32 dev_instance = va_arg (*args, u32); + return format (s, \"" encap_stack "_tunnel%d\", dev_instance); +} + +static uword dummy_interface_tx (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + clib_warning (\"you shouldn't be here, leaking buffers...\"); + return frame->n_vectors; +} + +VNET_DEVICE_CLASS (" encap_stack "_device_class,static) = { + .name = "" ENCAP_STACK "", + .format_device_name = format_" encap_stack "_name, + .format_tx_trace = format_" encap_stack "_encap_trace, + .tx_function = dummy_interface_tx, +}; + +static uword dummy_set_rewrite (vnet_main_t * vnm, + u32 sw_if_index, + u32 l3_type, + void * dst_address, + void * rewrite, + uword max_rewrite_bytes) +{ + return 0; +} + +u8 * format_" encap_stack "_header_with_length (u8 * s, va_list * args) +{ + " encap_stack "_header_t * h = va_arg (*args, " encap_stack "_header_t *); + u32 max_header_bytes = va_arg (*args, u32); + u32 header_bytes; + + header_bytes = sizeof (h[0]); + if (max_header_bytes != 0 && header_bytes > max_header_bytes) + return format (s, \"" encap-stack "header truncated\"); + + /* FIXME: pretty-print an " encap_stack " header */ + + return s; +} + +VNET_HW_INTERFACE_CLASS (" encap_stack "_hw_class) = { + .name = \"" ENCAP_STACK "\", + .format_header = format_" encap_stack "_header_with_length, + .set_rewrite = dummy_set_rewrite, +}; + +#define foreach_copy_field \ +_(src.as_u32) \ +_(dst.as_u32) \ +_(encap_fib_index) \ +_(decap_fib_index) \ +_(decap_next_index) \ +_(FIXME_ADD_ALL_COPY_FIELDS ) + +static int " encap_stack "_rewrite (" encap_stack "_tunnel_t * t) +{ + u8 *rw = 0; + ip4_header_t * ip0; + " encap_stack "_header_t * h0; + int len; + + len = sizeof (*h0); + + vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES); + + h0 = (ip4_udp_" encap_stack "_header_t *) rw; + + /* FIXME: build the actual header here... */ + + /* Fixed portion of the (outer) ip4 header */ + ip0 = &h0->ip4; + ip0->ip_version_and_header_length = 0x45; + ip0->ttl = 254; + ip0->protocol = IP_PROTOCOL_UDP; + + /* we'll fix up the ip4 header length and checksum after-the-fact */ + ip0->src_address.as_u32 = t->src.as_u32; + ip0->dst_address.as_u32 = t->dst.as_u32; + ip0->checksum = ip4_header_checksum (ip0); + + /* UDP header, randomize src port on something, maybe? */ + h0->udp.src_port = clib_host_to_net_u16 (4341); + h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_" encap_stack "); + + /* $$$ build a tunnel header here */ + + t->rewrite = rw; + return (0); +} + +int vnet_" encap_stack "_add_del_tunnel +(vnet_" encap_stack "_add_del_tunnel_args_t *a, u32 * hw_if_indexp) +{ + " encap_stack "_main_t * ngm = &" encap_stack "_main; + " encap_stack "_tunnel_t *t = 0; + vnet_main_t * vnm = ngm->vnet_main; + vnet_hw_interface_t * hi; + uword * p; + u32 hw_if_index = ~0; + int rv; + " encap_stack "_tunnel_key_t key, *key_copy; + hash_pair_t *hp; + + key.FIXME = clib_host_to_net_XXX(FIXME); + + p = hash_get_mem (ngm->" encap_stack "_tunnel_by_key, &key); + + if (a->is_add) + { + /* adding a tunnel: tunnel must not already exist */ + if (p) + return VNET_API_ERROR_INVALID_VALUE; + + pool_get_aligned (ngm->tunnels, t, CLIB_CACHE_LINE_BYTES); + memset (t, 0, sizeof (*t)); + + /* copy from arg structure */ +#define _(x) t->x = a->x; + foreach_copy_field; +#undef _ + + rv = " encap_stack "_rewrite (t); + + if (rv) + { + pool_put (ngm->tunnels, t); + return rv; + } + + /* $$$$ use a simple hash if you can ... */ + key_copy = clib_mem_alloc (sizeof (*key_copy)); + clib_memcpy (key_copy, &key, sizeof (*key_copy)); + + hash_set_mem (ngm->" encap_stack "_tunnel_by_key, key_copy, + t - ngm->tunnels); + + /* + * interface freelist / recycle shtik + * This simple implementation rapidly reuses freed tunnel interfaces. + * Consider whether to refcount, etc. etc. + */ + if (vec_len (ngm->free_" encap_stack "_tunnel_hw_if_indices) > 0) + { + hw_if_index = ngm->free_" encap_stack "_tunnel_hw_if_indices + [vec_len (ngm->free_" encap_stack "_tunnel_hw_if_indices)-1]; + _vec_len (ngm->free_" encap_stack "_tunnel_hw_if_indices) -= 1; + + hi = vnet_get_hw_interface (vnm, hw_if_index); + hi->dev_instance = t - ngm->tunnels; + hi->hw_instance = hi->dev_instance; + } + else + { + hw_if_index = vnet_register_interface + (vnm, " encap_stack "_device_class.index, t - ngm->tunnels, + " encap_stack "_hw_class.index, t - ngm->tunnels); + hi = vnet_get_hw_interface (vnm, hw_if_index); + hi->output_node_index = " encap_stack "_encap_node.index; + } + + t->hw_if_index = hw_if_index; + + vnet_sw_interface_set_flags (vnm, hi->sw_if_index, + VNET_SW_INTERFACE_FLAG_ADMIN_UP); + } + else + { + /* deleting a tunnel: tunnel must exist */ + if (!p) + return VNET_API_ERROR_NO_SUCH_ENTRY; + + t = pool_elt_at_index (ngm->tunnels, p[0]); + + vnet_sw_interface_set_flags (vnm, t->hw_if_index, 0 /* down */); + vec_add1 (ngm->free_" encap_stack "_tunnel_hw_if_indices, t->hw_if_index); + + hp = hash_get_pair (ngm->" encap_stack "_tunnel_by_key, &key); + key_copy = (void *)(hp->key); + hash_unset_mem (ngm->" encap_stack "_tunnel_by_key, &key); + clib_mem_free (key_copy); + + vec_free (t->rewrite); + pool_put (ngm->tunnels, t); + } + + if (hw_if_indexp) + *hw_if_indexp = hw_if_index; + + return 0; +} + +static u32 fib_index_from_fib_id (u32 fib_id) +{ + ip4_main_t * im = &ip4_main; + uword * p; + + p = hash_get (im->fib_index_by_table_id, fib_id); + if (!p) + return ~0; + + return p[0]; +} + +static uword unformat_decap_next (unformat_input_t * input, va_list * args) +{ + u32 * result = va_arg (*args, u32 *); + u32 tmp; + + if (unformat (input, \"drop\")) + *result = " ENCAP_STACK "_INPUT_NEXT_DROP; + else if (unformat (input, \"ip4\")) + *result = " ENCAP_STACK "_INPUT_NEXT_IP4_INPUT; + else if (unformat (input, \"ip6\")) + *result = " ENCAP_STACK "_INPUT_NEXT_IP6_INPUT; + else if (unformat (input, \"ethernet\")) + *result = " ENCAP_STACK "_INPUT_NEXT_IP6_INPUT; + else if (unformat (input, \"" encap-stack "\")) + *result = " ENCAP_STACK "_INPUT_NEXT_" ENCAP_STACK "_ENCAP; + else if (unformat (input, \"%d\", &tmp)) + *result = tmp; + else + return 0; + return 1; +} + +static clib_error_t * +" encap_stack "_add_del_tunnel_command_fn (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + unformat_input_t _line_input, * line_input = &_line_input; + clib_error_t *error = 0; + ip4_address_t src, dst; + u8 is_add = 1; + u8 src_set = 0; + u8 dst_set = 0; + u32 encap_fib_index = 0; + u32 decap_fib_index = 0; + u8 next_protocol = " ENCAP_STACK "_NEXT_PROTOCOL_IP4; + u32 decap_next_index = " ENCAP_STACK "_INPUT_NEXT_IP4_INPUT; + u8 flags = " ENCAP_STACK "_FLAGS_P; + u8 ver_res = 0; + u8 res = 0; + u32 iid = 0; + u8 iid_set = 0; + u32 tmp; + int rv; + vnet_" encap_stack "_add_del_tunnel_args_t _a, * a = &_a; + + /* Get a line of input. */ + if (! unformat_user (input, unformat_line_input, line_input)) + return 0; + + while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { + if (unformat (line_input, \"del\")) + is_add = 0; + else if (unformat (line_input, \"src %U\", + unformat_ip4_address, &src)) + src_set = 1; + else if (unformat (line_input, \"dst %U\", + unformat_ip4_address, &dst)) + dst_set = 1; + else if (unformat (line_input, \"encap-vrf-id %d\", &tmp)) + { + encap_fib_index = fib_index_from_fib_id (tmp); + if (encap_fib_index == ~0) + { + unformat_free (line_input); + return clib_error_return (0, \"nonexistent encap fib id %d\", tmp); + } + } + else if (unformat (line_input, \"decap-vrf-id %d\", &tmp)) + { + decap_fib_index = fib_index_from_fib_id (tmp); + if (decap_fib_index == ~0) + { + unformat_free (line_input); + return clib_error_return (0, \"nonexistent decap fib id %d\", tmp); + } + } + else if (unformat (line_input, \"decap-next %U\", unformat_decap_next, + &decap_next_index)) + ; + else if (unformat(line_input, \"next-ip4\")) + next_protocol = 1; + else if (unformat(line_input, \"next-ip6\")) + next_protocol = 2; + else if (unformat(line_input, \"next-ethernet\")) + next_protocol = 3; + else if (unformat(line_input, \"next-nsh\")) + next_protocol = 4; + /* + * $$$ allow the user to specify anything they want + * in the " ENCAP_STACK " header + */ + else + { + error = clib_error_return (0, \"parse error: '%U'\", + format_unformat_error, line_input); + unformat_free (line_input); + return error; + } + } + + unformat_free (line_input); + + if (src_set == 0) + return clib_error_return (0, \"tunnel src address not specified\"); + + if (dst_set == 0) + return clib_error_return (0, \"tunnel dst address not specified\"); + + memset (a, 0, sizeof (*a)); + + a->is_add = is_add; + +#define _(x) a->x = x; + foreach_copy_field; +#undef _ + + rv = vnet_" encap_stack "_add_del_tunnel (a, 0 /* hw_if_indexp */); + + switch(rv) + { + case 0: + break; + case VNET_API_ERROR_INVALID_VALUE: + return clib_error_return (0, \"tunnel already exists...\"); + + case VNET_API_ERROR_NO_SUCH_ENTRY: + return clib_error_return (0, \"tunnel does not exist...\"); + + default: + return clib_error_return + (0, \"vnet_" encap_stack "_add_del_tunnel returned %d\", rv); + } + + return 0; +} + +VLIB_CLI_COMMAND (create_" encap_stack "_tunnel_command, static) = { + .path = \"lisp gpe tunnel\", + .short_help = + \" tunnel src dst \\n\" + \" [encap-fib-id ] [decap-fib-id ]\\n\" + \" [decap-next [ip4|ip6|ethernet|nsh-encap|]][del]\\n\", + .function = " encap_stack "_add_del_tunnel_command_fn, +}; + +static clib_error_t * +show_" encap_stack "_tunnel_command_fn (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + " encap_stack "_main_t * ngm = &" encap_stack "_main; + " encap_stack "_tunnel_t * t; + + if (pool_elts (ngm->tunnels) == 0) + vlib_cli_output (vm, \"No lisp-gpe tunnels configured...\"); + + pool_foreach (t, ngm->tunnels, + ({ + vlib_cli_output (vm, \"%U\", format_" encap_stack "_tunnel); + })); + + return 0; +} + +VLIB_CLI_COMMAND (show_" encap_stack "_tunnel_command, static) = { + .path = \"show lisp gpe tunnel\", + .function = show_" encap_stack "_tunnel_command_fn, +}; + +clib_error_t *" encap_stack "_init (vlib_main_t *vm) +{ + " encap_stack "_main_t *ngm = &" encap_stack "_main; + + ngm->vnet_main = vnet_get_main(); + ngm->vlib_main = vm; + + ngm->" encap_stack "_tunnel_by_key + = hash_create_mem (0, sizeof(" encap_stack "_tunnel_key_t), sizeof (uword)); + + /* YMMV, register with the local netstack */ + udp_register_dst_port (vm, UDP_DST_PORT_" encap_stack ", + " encap_stack "_input_node.index, 1 /* is_ip4 */); + return 0; +} + +VLIB_INIT_FUNCTION(" encap_stack "_init); + +") + diff --git a/extras/emacs/tunnel-decap-skel.el b/extras/emacs/tunnel-decap-skel.el new file mode 100644 index 00000000000..380273b67a2 --- /dev/null +++ b/extras/emacs/tunnel-decap-skel.el @@ -0,0 +1,299 @@ +;;; tunnel-decap-skel.el - tunnel decapsulation skeleton + +(require 'skeleton) + +(define-skeleton skel-tunnel-decap +"Insert a tunnel decap implementation" +nil +'(setq encap_stack (skeleton-read "encap_stack (e.g ip4_udp_lisp): ")) +'(setq ENCAP_STACK (upcase encap_stack)) +'(setq encap-stack (replace-regexp-in-string "_" "-" encap_stack)) +'(setq ENCAP-STACK (upcase encap-stack)) +" +#include +#include +#include + +typedef struct { + u32 next_index; + u32 tunnel_index; + u32 error; + " encap_stack "_header_t h; +} " encap_stack "_rx_trace_t; + +static u8 * format_" encap_stack "_rx_trace (u8 * s, va_list * args) +{ + CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); + CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); + " encap_stack "_rx_trace_t * t = va_arg (*args, " encap_stack "_rx_trace_t *); + + if (t->tunnel_index != ~0) + { + s = format (s, \"" ENCAP-STACK ": tunnel %d next %d error %d\", + t->tunnel_index, t->next_index, t->error); + } + else + { + s = format (s, \"" ENCAP-STACK ": no tunnel next %d error %d\\n\", + t->next_index, t->error); + } + s = format (s, \"\\n %U\", format_" encap_stack "_header_with_length, &t->h, + (u32) sizeof (t->h) /* max size */); + return s; +} + +static uword +" encap_stack "_input (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * from_frame) +{ + u32 n_left_from, next_index, * from, * to_next; + " encap_stack "_main_t * ngm = &" encap_stack "_main; + u32 last_tunnel_index = ~0; + " encap_stack "_tunnel_key_t last_key; + u32 pkts_decapsulated = 0; + + memset (&last_key, 0xff, sizeof (last_key)); + + from = vlib_frame_vector_args (from_frame); + n_left_from = from_frame->n_vectors; + + next_index = node->cached_next_index; + + while (n_left_from > 0) + { + u32 n_left_to_next; + + vlib_get_next_frame (vm, node, next_index, + to_next, n_left_to_next); + +#if 0 /* $$$ dual loop when the single loop works */ + while (n_left_from >= 4 && n_left_to_next >= 2) + { + u32 bi0, bi1; + vlib_buffer_t * b0, * b1; + nsh_unicast_header_t * h0, * h1; + u32 label0, label1; + u32 next0, next1; + uword * p0, * p1; + + /* Prefetch next iteration. */ + { + vlib_buffer_t * p2, * p3; + + p2 = vlib_get_buffer (vm, from[2]); + p3 = vlib_get_buffer (vm, from[3]); + + vlib_prefetch_buffer_header (p2, LOAD); + vlib_prefetch_buffer_header (p3, LOAD); + + CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD); + CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD); + } + + bi0 = from[0]; + bi1 = from[1]; + to_next[0] = bi0; + to_next[1] = bi1; + from += 2; + to_next += 2; + n_left_to_next -= 2; + n_left_from -= 2; + + b0 = vlib_get_buffer (vm, bi0); + b1 = vlib_get_buffer (vm, bi1); + + h0 = vlib_buffer_get_current (b0); + h1 = vlib_buffer_get_current (b1); + + next0 = next1 = " ENCAP_STACK "_INPUT_NEXT_IP4_INPUT; + + label0 = clib_net_to_host_u32 (h0->label_exp_s_ttl); + label1 = clib_net_to_host_u32 (h1->label_exp_s_ttl); + + /* + * Translate label contents into a fib index. + * This is a decent sanity check, and guarantees + * a sane FIB for the downstream lookup + */ + label0 = vnet_nsh_uc_get_label (label0); + label1 = vnet_nsh_uc_get_label (label1); + + /* If 2xlabels match, and match the 1-wide cache, use it */ + if (label0 == label1 && rt->last_label == label0) + { + vnet_buffer(b0)->sw_if_index[VLIB_TX] = rt->last_fib_index; + vnet_buffer(b1)->sw_if_index[VLIB_TX] = rt->last_fib_index; + } + else + { + p0 = hash_get (rt->mm->fib_index_by_nsh_label, label0); + if (PREDICT_FALSE (p0 == 0)) + { + next0 = " ENCAP_STACK "_INPUT_NEXT_DROP; + b0->error = node->errors[NSH_ERROR_BAD_LABEL]; + } + else + vnet_buffer(b0)->sw_if_index[VLIB_TX] = p0[0]; + + p1 = hash_get (rt->mm->fib_index_by_nsh_label, label1); + if (PREDICT_FALSE (p1 == 0)) + { + next1 = " ENCAP_STACK "_INPUT_NEXT_DROP; + b1->error = node->errors[NSH_ERROR_BAD_LABEL]; + } + else + { + vnet_buffer(b1)->sw_if_index[VLIB_TX] = p1[0]; + rt->last_fib_index = p1[0]; + rt->last_label = label1; + } + } + + if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) + { + nsh_rx_trace_t *tr = vlib_add_trace (vm, node, + b0, sizeof (*tr)); + tr->label_exp_s_ttl = label0; + } + if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED)) + { + nsh_rx_trace_t *tr = vlib_add_trace (vm, node, + b1, sizeof (*tr)); + tr->label_exp_s_ttl = label1; + } + + vlib_buffer_advance (b0, sizeof (*h0)); + vlib_buffer_advance (b1, sizeof (*h1)); + + vlib_validate_buffer_enqueue_x2 (vm, node, next_index, + to_next, n_left_to_next, + bi0, bi1, next0, next1); + } +#endif + + while (n_left_from > 0 && n_left_to_next > 0) + { + u32 bi0; + vlib_buffer_t * b0; + u32 next0; + " encap_stack "_header_t * iuX0; + uword * p0; + u32 tunnel_index0; + " encap_stack "_tunnel_t * t0; + " encap_stack "_tunnel_key_t key0; + u32 error0; + + bi0 = from[0]; + to_next[0] = bi0; + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + b0 = vlib_get_buffer (vm, bi0); + + /* + * udp leaves current_data pointing at the tunnel header + * $$$$ FIXME + */ + vlib_buffer_advance + (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t))); + + iuX0 = vlib_buffer_get_current (b0); + + /* pop (ip, udp, lisp-gpe) */ + vlib_buffer_advance (b0, sizeof (*iuX0)); + + tunnel_index0 = ~0; + error0 = 0; + next0 = " ENCAP_STACK "_INPUT_NEXT_DROP; + + key0.src = iuX0->ip4.src_address.as_u32; + key0.iid = iuX0->lisp.iid; + + /* $$$ validate key comparison */ + if (PREDICT_FALSE ((key0.as_u64[0] != last_key.as_u64[0]))) + { + p0 = hash_get_mem (ngm->" encap_stack "_tunnel_by_key, &key0); + + if (p0 == 0) + { + error0 = " ENCAP_STACK "_ERROR_NO_SUCH_TUNNEL; + goto trace0; + } + + last_key.as_u64[0] = key0.as_u64[0]; + tunnel_index0 = last_tunnel_index = p0[0]; + } + else + tunnel_index0 = last_tunnel_index; + + t0 = pool_elt_at_index (ngm->tunnels, tunnel_index0); + + next0 = t0->decap_next_index; + + /* Required to make the l2 tag push / pop code work on l2 subifs */ + vnet_update_l2_len (b0); + + /* + * ip[46] lookup in the configured FIB + * " encap-stack ", here's the encap tunnel sw_if_index + */ + vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index; + pkts_decapsulated ++; + + trace0: + b0->error = error0 ? node->errors[error0] : 0; + + if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) + { + " encap_stack "_rx_trace_t *tr + = vlib_add_trace (vm, node, b0, sizeof (*tr)); + tr->next_index = next0; + tr->error = error0; + tr->tunnel_index = tunnel_index0; + tr->h = iuX0->lisp; + } + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, + to_next, n_left_to_next, + bi0, next0); + } + + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + vlib_node_increment_counter (vm, " encap_stack "_input_node.index, + " ENCAP_STACK "_ERROR_DECAPSULATED, + pkts_decapsulated); + return from_frame->n_vectors; +} + +static char * " encap_stack "_error_strings[] = { +#define " encap_stack "_error(n,s) s, +#include +#undef " encap_stack "_error +#undef _ +}; + +VLIB_REGISTER_NODE (" encap_stack "_input_node) = { + .function = \"" encap_stack "_input\", + .name = \"" encap-stack "-input\", + /* Takes a vector of packets. */ + .vector_size = sizeof (u32), + + .n_errors = " ENCAP_STACK "_N_ERROR, + .error_strings = " encap_stack "_error_strings, + + .n_next_nodes = " ENCAP_STACK "_INPUT_N_NEXT, + .next_nodes = { +#define _(s,n) [" ENCAP_STACK "_INPUT_NEXT_##s] = n, + foreach_" encap_stack "_input_next +#undef _ + }, + + .format_buffer = format_" encap_stack "_header_with_length, + .format_trace = format_" encap_stack "_rx_trace, + // $$$$ .unformat_buffer = unformat_" encap_stack "_header, +}; + +") diff --git a/extras/emacs/tunnel-encap-skel.el b/extras/emacs/tunnel-encap-skel.el new file mode 100644 index 00000000000..9c98a597d09 --- /dev/null +++ b/extras/emacs/tunnel-encap-skel.el @@ -0,0 +1,245 @@ +;;; tunnel-encap-skel.el - tunnel interface output skeleton + +(require 'skeleton) + +(define-skeleton skel-tunnel-encap +"Insert a tunnel encap implementation" +nil +'(setq encap_stack (skeleton-read "encap_stack (e.g ip4_udp_lisp): ")) +'(setq ENCAP_STACK (upcase encap_stack)) +'(setq encap-stack (replace-regexp-in-string "_" "-" encap_stack)) +'(setq ENCAP-STACK (upcase encap-stack)) +" +#include +#include +#include +#include +#include +#include + +/* Statistics (not really errors) */ +#define foreach_" encap_stack "_encap_error \\ +_(ENCAPSULATED, \"good packets encapsulated\") + +static char * " encap_stack "_encap_error_strings[] = { +#define _(sym,string) string, + foreach_" encap_stack "_encap_error +#undef _ +}; + +typedef enum { +#define _(sym,str) " ENCAP_STACK "_ENCAP_ERROR_##sym, + foreach_" encap_stack "_encap_error +#undef _ + " ENCAP_STACK "_ENCAP_N_ERROR, +} " encap_stack "_encap_error_t; + +typedef enum { + " ENCAP_STACK "_ENCAP_NEXT_IP4_LOOKUP, + " ENCAP_STACK "_ENCAP_NEXT_DROP, + " ENCAP_STACK "_ENCAP_N_NEXT, +} " encap_stack "_encap_next_t; + +typedef struct { + u32 tunnel_index; +} " encap_stack "_encap_trace_t; + +u8 * format_" encap_stack "_encap_trace (u8 * s, va_list * args) +{ + CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); + CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); + " encap_stack "_encap_trace_t * t + = va_arg (*args, " encap_stack "_encap_trace_t *); + + s = format (s, \"" ENCAP-STACK ": tunnel %d\", t->tunnel_index); + return s; +} + +/* $$$$ FIXME adjust to match the rewrite string */ +#define foreach_fixed_header_offset \\ +_(0) _(1) _(2) _(3) _(FIXME) + +static uword +" encap_stack "_encap (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * from_frame) +{ + u32 n_left_from, next_index, * from, * to_next; + " encap_stack "_main_t * ngm = &" encap_stack "_main; + vnet_main_t * vnm = ngm->vnet_main; + u32 pkts_encapsulated = 0; + u16 old_l0 = 0, old_l1 = 0; + + from = vlib_frame_vector_args (from_frame); + n_left_from = from_frame->n_vectors; + + next_index = node->cached_next_index; + + while (n_left_from > 0) + { + u32 n_left_to_next; + + vlib_get_next_frame (vm, node, next_index, + to_next, n_left_to_next); + +#if 0 /* $$$ dual loop when the single loop works */ + while (n_left_from >= 4 && n_left_to_next >= 2) + { + u32 bi0, bi1; + vlib_buffer_t * b0, * b1; + nsh_unicast_header_t * h0, * h1; + u32 label0, label1; + u32 next0, next1; + uword * p0, * p1; + + /* Prefetch next iteration. */ + { + vlib_buffer_t * p2, * p3; + + p2 = vlib_get_buffer (vm, from[2]); + p3 = vlib_get_buffer (vm, from[3]); + + vlib_prefetch_buffer_header (p2, LOAD); + vlib_prefetch_buffer_header (p3, LOAD); + + CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD); + CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD); + } + + bi0 = from[0]; + bi1 = from[1]; + to_next[0] = bi0; + to_next[1] = bi1; + from += 2; + to_next += 2; + n_left_to_next -= 2; + n_left_from -= 2; + + b0 = vlib_get_buffer (vm, bi0); + b1 = vlib_get_buffer (vm, bi1); + + h0 = vlib_buffer_get_current (b0); + h1 = vlib_buffer_get_current (b1); + + next0 = next1 = " ENCAP_STACK "_ENCAP_NEXT_IP4_LOOKUP; + + vlib_buffer_advance (b0, sizeof (*h0)); + vlib_buffer_advance (b1, sizeof (*h1)); + + vlib_validate_buffer_enqueue_x2 (vm, node, next_index, + to_next, n_left_to_next, + bi0, bi1, next0, next1); + } +#endif + + while (n_left_from > 0 && n_left_to_next > 0) + { + u32 bi0; + vlib_buffer_t * b0; + u32 next0 = " ENCAP_STACK "_ENCAP_NEXT_IP4_LOOKUP; + vnet_hw_interface_t * hi0; + ip4_header_t * ip0; + udp_header_t * udp0; + u64 * copy_src0, * copy_dst0; + u32 * copy_src_last0, * copy_dst_last0; + " encap_stack "_tunnel_t * t0; + u16 new_l0; + ip_csum_t sum0; + + bi0 = from[0]; + to_next[0] = bi0; + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + b0 = vlib_get_buffer (vm, bi0); + + /* 1-wide cache? */ + hi0 = vnet_get_sup_hw_interface + (vnm, vnet_buffer(b0)->sw_if_index[VLIB_TX]); + + t0 = pool_elt_at_index (ngm->tunnels, hi0->dev_instance); + + ASSERT(vec_len(t0->rewrite) >= 24); + + /* Apply the rewrite string. $$$$ vnet_rewrite? */ + vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite)); + + ip0 = vlib_buffer_get_current(b0); + /* Copy the fixed header */ + copy_dst0 = (u64 *) ip0; + copy_src0 = (u64 *) t0->rewrite; + + ASSERT (sizeof (ip4_udp_" encap_stack "_header_t) == FIXME); + + /* Copy first N octets 8-bytes at a time */ +#define _(offs) copy_dst0[offs] = copy_src0[offs]; + foreach_fixed_header_offset; +#undef _ +#if 0 /* needed if encap not a multiple of 8 bytes */ + /* Last 4 octets. Hopefully gcc will be our friend */ + copy_dst_last0 = (u32 *)(©_dst0[FIXME]); + copy_src_last0 = (u32 *)(©_src0[FIXME]); + copy_dst_last0[0] = copy_src_last0[0]; + +#endif + /* fix the ing outer-IP checksum */ + sum0 = ip0->checksum; + /* old_l0 always 0, see the rewrite setup */ + new_l0 = + clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)); + + sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t, + length /* changed member */); + ip0->checksum = ip_csum_fold (sum0); + ip0->length = new_l0; + + /* Fix UDP length */ + udp0 = (udp_header_t *)(ip0+1); + new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) + - sizeof (*ip0)); + + udp0->length = new_l0; + + /* Reset to look up tunnel partner in the configured FIB */ + vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index; + pkts_encapsulated ++; + + if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) + { + " encap_stack "_encap_trace_t *tr = + vlib_add_trace (vm, node, b0, sizeof (*tr)); + tr->tunnel_index = t0 - ngm->tunnels; + } + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, + to_next, n_left_to_next, + bi0, next0); + } + + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + vlib_node_increment_counter (vm, node->node_index, + " ENCAP_STACK "_ENCAP_ERROR_ENCAPSULATED, + pkts_encapsulated); + return from_frame->n_vectors; +} + +VLIB_REGISTER_NODE (" encap_stack "_encap_node) = { + .function = " encap_stack "_encap, + .name = \"" encap-stack "-encap\", + .vector_size = sizeof (u32), + .format_trace = format_" encap_stack "_encap_trace, + .type = VLIB_NODE_TYPE_INTERNAL, + + .n_errors = ARRAY_LEN(" encap_stack "_encap_error_strings), + .error_strings = " encap_stack "_encap_error_strings, + + .n_next_nodes = " ENCAP_STACK "_ENCAP_N_NEXT, + + .next_nodes = { + [" ENCAP_STACK "_ENCAP_NEXT_IP4_LOOKUP] = \"ip4-lookup\", + [" ENCAP_STACK "_ENCAP_NEXT_DROP] = \"error-drop\", + }, +}; +") diff --git a/extras/emacs/tunnel-h-skel.el b/extras/emacs/tunnel-h-skel.el new file mode 100644 index 00000000000..067cf1346ce --- /dev/null +++ b/extras/emacs/tunnel-h-skel.el @@ -0,0 +1,128 @@ +;;; tunnel-h-skel.el - tunnel encap header file skeleton + +(require 'skeleton) + +(define-skeleton skel-tunnel-h +"Insert a tunnel encap header file" +nil +'(setq encap_stack (skeleton-read "encap_stack (e.g ip4_udp_lisp): ")) +'(setq ENCAP_STACK (upcase encap_stack)) +'(setq encap-stack (replace-regexp-in-string "_" "-" encap_stack)) +" +#ifndef included_vnet_" encap_stack "_h +#define included_vnet_" encap_stack "_h + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Encap stack built in encap.c */ +typedef CLIB_PACKED (struct { + ip4_header_t ip4; /* 20 bytes */ + udp_header_t udp; /* 8 bytes */ + " encap_stack "_header_t lisp; /* 8 bytes */ +}) " encap_stack "_header_t; + +typedef CLIB_PACKED(struct { + /* + * Key fields: + * all fields in NET byte order + */ + union { + struct { + u32 FIXME_NET_BYTE_ORDER; + }; + u64 as_u64[1]; + }; +}) " encap_stack "_tunnel_key_t; + +typedef struct { + /* Rewrite string. $$$$ maybe: embed vnet_rewrite header */ + u8 * rewrite; + + /* decap next index */ + u32 decap_next_index; + + /* tunnel src and dst addresses */ + ip4_address_t src; + ip4_address_t dst; + + /* FIB indices */ + u32 encap_fib_index; /* tunnel partner lookup here */ + u32 decap_fib_index; /* inner IP lookup here */ + + /* vnet intfc hw/sw_if_index */ + u32 hw_if_index; + + /* encap header fields in HOST byte order */ + u32 FIXME; +} " encap_stack "_tunnel_t; + +#define foreach_" encap_stack "_input_next \\ +_(DROP, \"error-drop\") \\ +_(IP4_INPUT, \"ip4-input\") \\ +_(IP6_INPUT, \"ip6-input\") \\ +_(ETHERNET_INPUT, \"ethernet-input\") \\ +_(" ENCAP_STACK "_ENCAP, \"" encap-stack "-encap\") + +typedef enum { +#define _(s,n) " ENCAP_STACK "_INPUT_NEXT_##s, + foreach_" encap_stack "_input_next +#undef _ + " ENCAP_STACK "_INPUT_N_NEXT, +} " encap_stack "_input_next_t; + +typedef enum { +#define " encap_stack "_error(n,s) " ENCAP_STACK "_ERROR_##n, +#include +#undef " encap_stack "_error + " ENCAP_STACK "_N_ERROR, +} " encap_stack "_input_error_t; + +typedef struct { + /* vector of encap tunnel instances */ + " encap_stack "_tunnel_t *tunnels; + + /* lookup tunnel by key */ + uword * " encap_stack "_tunnel_by_key; + + /* Free vlib hw_if_indices */ + u32 * free_" encap_stack "_tunnel_hw_if_indices; + + /* convenience */ + vlib_main_t * vlib_main; + vnet_main_t * vnet_main; +} " encap_stack "_main_t; + +" encap_stack "_main_t " encap_stack "_main; + +vlib_node_registration_t " encap_stack "_input_node; +vlib_node_registration_t " encap_stack "_encap_node; + +u8 * format_" encap_stack "_encap_trace (u8 * s, va_list * args); +u8 * format_" encap_stack "_header_with_length (u8 * s, va_list * args); + +typedef struct { + u8 is_add; + ip4_address_t src, dst; + u32 encap_fib_index; + u32 decap_fib_index; + u32 decap_next_index; + /* encap fields in HOST byte order */ + u8 FIXME_HOST_BYTE_ORDER; +} vnet_" encap_stack "_add_del_tunnel_args_t; + +int vnet_" encap_stack "_add_del_tunnel +(vnet_" encap_stack "_add_del_tunnel_args_t *a, u32 * hw_if_indexp); + +u8 * format_" encap_stack "_header_with_length (u8 * s, va_list * args); + +#endif /* included_vnet_" encap_stack "_h */ + +") -- cgit 1.2.3-korg