/* * Copyright (c) 2015 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. */ /* Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef included_clib_elf_h #define included_clib_elf_h #include #include #include #include #define foreach_elf_file_class \ _ (CLASS_NONE) _ (32BIT) _ (64BIT) #define foreach_elf_data_encoding \ _ (ENCODING_NONE) \ _ (TWOS_COMPLEMENT_LITTLE_ENDIAN) \ _ (TWOS_COMPLEMENT_BIG_ENDIAN) #define ELF_VERSION_NONE (0) #define ELF_VERSION_CURRENT (1) #define foreach_elf_abi \ _ (SYSV, 0) \ _ (HPUX, 1) \ _ (NETBSD, 2) \ _ (LINUX, 3) \ _ (SOLARIS, 6) \ _ (AIX, 7) \ _ (IRIX, 8) \ _ (FREEBSD, 9) \ _ (COMPAQ_TRU64, 10) \ _ (MODESTO, 11) \ _ (OPENBSD, 12) \ _ (ARM, 97) \ _ (STANDALONE, 255) /* Legal values for type (object file type). */ #define foreach_elf_file_type \ _ (NONE, 0) \ _ (RELOC, 1) \ _ (EXEC, 2) \ _ (SHARED, 3) \ _ (CORE, 4) \ _ (OS_SPECIFIC_LO, 0xfe00) \ _ (OS_SPECIFIC_HI, 0xfeff) \ _ (ARCH_SPECIFIC_LO, 0xff00) \ _ (ARCH_SPECIFIC_HI, 0xffff) /* Legal values for architecture. */ #define foreach_elf_architecture \ _ (NONE, 0) /* No machine */ \ _ (M32, 1) /* AT&T WE 32100 */ \ _ (SPARC, 2) /* SUN SPARC */ \ _ (386, 3) /* Intel 80386 */ \ _ (68K, 4) /* Motorola m68k family */ \ _ (88K, 5) /* Motorola m88k family */ \ _ (860, 7) /* Intel 80860 */ \ _ (MIPS, 8) /* MIPS R3000 big-endian */ \ _ (S370, 9) /* IBM System/370 */ \ _ (MIPS_RS3_LE, 10) /* MIPS R3000 little-endian */ \ _ (PARISC, 15) /* HPPA */ \ _ (VPP500, 17) /* Fujitsu VPP500 */ \ _ (SPARC32PLUS, 18) /* Sun's "v8plus" */ \ _ (960, 19) /* Intel 80960 */ \ _ (PPC, 20) /* PowerPC */ \ _ (PPC64, 21) /* PowerPC 64-bit */ \ _ (S390, 22) /* IBM S390 */ \ _ (V800, 36) /* NEC V800 series */ \ _ (FR20, 37) /* Fujitsu FR20 */ \ _ (RH32, 38) /* TRW RH-32 */ \ _ (RCE, 39) /* Motorola RCE */ \ _ (ARM, 40) /* ARM */ \ _ (FAKE_ALPHA, 41) /* Digital Alpha */ \ _ (SH, 42) /* Hitachi SH */ \ _ (SPARCV9, 43) /* SPARC v9 64-bit */ \ _ (TRICORE, 44) /* Siemens Tricore */ \ _ (ARC, 45) /* Argonaut RISC Core */ \ _ (H8_300, 46) /* Hitachi H8/300 */ \ _ (H8_300H, 47) /* Hitachi H8/300H */ \ _ (H8S, 48) /* Hitachi H8S */ \ _ (H8_500, 49) /* Hitachi H8/500 */ \ _ (IA_64, 50) /* Intel Merced */ \ _ (MIPS_X, 51) /* Stanford MIPS-X */ \ _ (COLDFIRE, 52) /* Motorola Coldfire */ \ _ (68HC12, 53) /* Motorola M68HC12 */ \ _ (MMA, 54) /* Fujitsu MMA Multimedia Accel. */ \ _ (PCP, 55) /* Siemens PCP */ \ _ (NCPU, 56) /* Sony nCPU embedded RISC */ \ _ (NDR1, 57) /* Denso NDR1 microprocessor */ \ _ (STARCORE, 58) /* Motorola Start*Core processor */ \ _ (ME16, 59) /* Toyota ME16 processor */ \ _ (ST100, 60) /* STMicroelectronic ST100 */ \ _ (TINYJ, 61) /* Advanced Logic Corp. Tinyj */ \ _ (X86_64, 62) /* AMD x86-64 architecture */ \ _ (PDSP, 63) /* Sony DSP Processor */ \ _ (FX66, 66) /* Siemens FX66 microcontroller */ \ _ (ST9PLUS, 67) /* STMicroelectronics ST9+ 8/16 mc */ \ _ (ST7, 68) /* STmicroelectronics ST7 8 bit mc */ \ _ (68HC16, 69) /* Motorola MC68HC16 */ \ _ (68HC11, 70) /* Motorola MC68HC11 */ \ _ (68HC08, 71) /* Motorola MC68HC08 */ \ _ (68HC05, 72) /* Motorola MC68HC05 */ \ _ (SVX, 73) /* Silicon Graphics SVx */ \ _ (ST19, 74) /* STMicroelectronics ST19 8 bit mc */ \ _ (VAX, 75) /* Digital VAX */ \ _ (CRIS, 76) /* Axis 32-bit embedded proc. */ \ _ (JAVELIN, 77) /* Infineon 32-bit embedded proc. */ \ _ (FIREPATH, 78) /* Element 14 64-bit DSP Processor */ \ _ (ZSP, 79) /* LSI Logic 16-bit DSP Processor */ \ _ (MMIX, 80) /* Knuth's 64-bit processor */ \ _ (HUANY, 81) /* Harvard machine-independent */ \ _ (PRISM, 82) /* SiTera Prism */ \ _ (AVR, 83) /* Atmel AVR 8-bit microcontroller */ \ _ (FR30, 84) /* Fujitsu FR30 */ \ _ (D10V, 85) /* Mitsubishi D10V */ \ _ (D30V, 86) /* Mitsubishi D30V */ \ _ (V850, 87) /* NEC v850 */ \ _ (M32R, 88) /* Mitsubishi M32R */ \ _ (MN10300, 89) /* Matsushita MN10300 */ \ _ (MN10200, 90) /* Matsushita MN10200 */ \ _ (PJ, 91) /* picoJava */ \ _ (OPENRISC, 92) /* OpenRISC 32-bit processor */ \ _ (ARC_A5, 93) /* ARC Cores Tangent-A5 */ \ _ (XTENSA, 94) /* Tensilica Xtensa Architecture */ \ _ (ALPHA, 0x9026) #define _(f) ELF_##f, typedef enum { foreach_elf_file_class ELF_N_FILE_CLASS, } elf_file_class_t; typedef enum { foreach_elf_data_encoding ELF_N_DATA_ENCODING, } elf_data_encoding_t; #undef _ #define _(f,i) ELF_##f = i, typedef enum { foreach_elf_abi } elf_abi_t; typedef enum { foreach_elf_file_type } elf_file_type_t; #undef _ typedef enum { #define _(f,i) ELF_ARCH_##f = i, foreach_elf_architecture #undef _ } elf_architecture_t; typedef struct { /* 0x7f ELF */ u8 magic[4]; elf_file_class_t file_class:8; elf_data_encoding_t data_encoding:8; u8 file_version_ident; elf_abi_t abi:8; u8 abi_version; u8 pad[7]; elf_file_type_t file_type:16; elf_architecture_t architecture:16; u32 file_version; } elf_first_header_t; /* 32/64 bit file header following basic file header. */ #define foreach_elf32_file_header \ _ (u32, entry_point) \ _ (u32, segment_header_file_offset) \ _ (u32, section_header_file_offset) \ _ (u32, flags) \ _ (u16, n_bytes_this_header) \ _ (u16, segment_header_size) \ _ (u16, segment_header_count) \ _ (u16, section_header_size) \ _ (u16, section_header_count) \ _ (u16, section_header_string_table_index) #define foreach_elf64_file_header \ _ (u64, entry_point) \ _ (u64, segment_header_file_offset) \ _ (u64, section_header_file_offset) \ _ (u32, flags) \ _ (u16, n_bytes_this_header) \ _ (u16, segment_header_size) \ _ (u16, segment_header_count) \ _ (u16, section_header_size) \ _ (u16, section_header_count) \ _ (u16, section_header_string_table_index) /* Section header. */ #define foreach_elf32_section_header \ _ (u32, name) \ _ (u32, type) \ _ (u32, flags) \ _ (u32, exec_address) \ _ (u32, file_offset) \ _ (u32, file_size) \ _ (u32, link) \ _ (u32, additional_info) \ _ (u32, align) \ _ (u32, entry_size) #define foreach_elf64_section_header \ _ (u32, name) \ _ (u32, type) \ _ (u64, flags) \ _ (u64, exec_address) \ _ (u64, file_offset) \ _ (u64, file_size) \ _ (u32, link) \ _ (u32, additional_info) \ _ (u64, align) \ _ (u64, entry_size) /* Program segment header. */ #define foreach_elf32_segment_header \ _ (u32, type) \ _ (u32, file_offset) \ _ (u32, virtual_address) \ _ (u32, physical_address) \ _ (u32, file_size) \ _ (u32, memory_size) \ _ (u32, flags) \ _ (u32, align) #define foreach_elf64_segment_header \ _ (u32, type) \ _ (u32, flags) \ _ (u64, file_offset) \ _ (u64, virtual_address) \ _ (u64, physical_address) \ _ (u64, file_size) \ _ (u64, memory_size) \ _ (u64, align) /* Symbol table. */ #define foreach_elf32_symbol_header \ _ (u32, name) \ _ (u32, value) \ _ (u32, size) \ /* binding upper 4 bits; type lower 4 bits */ \ _ (u8, binding_and_type) \ _ (u8, visibility) \ _ (u16, section_index) #define foreach_elf64_symbol_header \ _ (u32, name) \ _ (u8, binding_and_type) \ _ (u8, visibility) \ _ (u16, section_index) \ _ (u64, value) \ _ (u64, size) #define _(t,f) t f; typedef struct { foreach_elf32_file_header} elf32_file_header_t; typedef struct { foreach_elf64_file_header} elf64_file_header_t; typedef struct { foreach_elf32_section_header} elf32_section_header_t; typedef struct { foreach_elf64_section_header} elf64_section_header_t; typedef struct { foreach_elf32_segment_header} elf32_segment_header_t; typedef struct { foreach_elf64_segment_header} elf64_segment_header_t; typedef struct { foreach_elf32_symbol_header} elf32_symbol_t; typedef struct { foreach_elf64_symbol_header} elf64_symbol_t; #undef _ /* Special section names. */ #define foreach_elf_symbol_reserved_section_index \ _ (ABSOLUTE, 0xfff1) /* Associated symbol is absolute */ \ _ (COMMON, 0xfff2) /* Associated symbol is common */ \ _ (XINDEX, 0xffff) /* Index is in extra table. */ #define ELF_SYMBOL_SECTION_RESERVED_LO 0xff00 #define ELF_SYMBOL_SECTION_RESERVED_HI 0xffff #define ELF_SYMBOL_SECTION_ARCH_SPECIFIC_LO 0xff00 #define ELF_SYMBOL_SECTION_ARCH_SPECIFIC_HI 0xff1f #define ELF_SYMBOL_SECTION_OS_SPECIFIC_LO 0xff20 #define ELF_SYMBOL_SECTION_OS_SPECIFIC_HI 0xff3f /* Section types. */ #define foreach_elf_section_type \ _ (UNUSED, 0) \ _ (PROGRAM_DATA, 1) \ _ (SYMBOL_TABLE, 2) \ _ (STRING_TABLE, 3) \ _ (RELOCATION_ADD, 4) \ _ (SYMBOL_TABLE_HASH, 5) \ _ (DYNAMIC, 6) /* Dynamic linking information */ \ _ (NOTE, 7) /* Notes */ \ _ (NO_BITS, 8) /* Program space with no data (bss) */ \ _ (RELOCATION, 9) /* Relocation entries, no addends */ \ _ (DYNAMIC_SYMBOL_TABLE, 11) /* Dynamic linker symbol table */ \ _ (INIT_ARRAY, 14) /* Array of constructors */ \ _ (FINI_ARRAY, 15) /* Array of destructors */ \ _ (PREINIT_ARRAY, 16) /* Array of pre-constructors */ \ _ (GROUP, 17) /* Section group */ \ _ (SYMTAB_SHNDX, 18) /* Extended section indices */ \ _ (OS_SPECIFIC_LO, 0x60000000) /* Start OS-specific */ \ _ (GNU_LIBLIST, 0x6ffffff7) /* Prelink library list */ \ _ (CHECKSUM, 0x6ffffff8) /* Checksum for DSO content. */ \ _ (SUNW_MOVE, 0x6ffffffa) \ _ (SUNW_COMDAT, 0x6ffffffb) \ _ (SUNW_SYMINFO, 0x6ffffffc) \ _ (GNU_VERDEF, 0x6ffffffd) /* Version definition section. */ \ _ (GNU_VERNEED, 0x6ffffffe) /* Version needs section. */ \ _ (GNU_VERSYM, 0x6fffffff) /* Version symbol table. */ \ _ (ARCH_SPECIFIC_LO, 0x70000000) /* Start of processor-specific */ \ _ (ARCH_SPECIFIC_HI, 0x7fffffff) /* End of processor-specific */ \ _ (APP_SPECIFIC_LO, 0x80000000) /* Start of application-specific */ \ _ (APP_SPECIFIC_HI, 0x8fffffff) /* End of application-specific */ /* Section flags. */ #define foreach_elf_section_flag \ _ (WRITE, 0) \ _ (ALLOC, 1) \ _ (EXEC, 2) \ _ (MERGE, 3) \ _ (STRING_TABLE, 5) \ _ (INFO_LINK, 6) \ _ (PRESERVE_LINK_ORDER, 7) \ _ (OS_NON_CONFORMING, 8) \ _ (GROUP, 9) \ _ (TLS, 10) \ _ (OS_SPECIFIC_LO, 20) \ _ (OS_SPECIFIC_HI, 27) \ _ (ARCH_SPECIFIC_LO, 28) \ _ (ARCH_SPECIFIC_HI, 31) typedef enum { #define _(f,i) ELF_SECTION_##f = i, foreach_elf_section_type #undef _ ELF_SECTION_OS_SPECIFIC_HI = 0x6fffffff, } elf_section_type_t; typedef enum { #define _(f,i) ELF_SECTION_FLAG_BIT_##f = i, foreach_elf_section_flag #undef _ } elf_section_flag_bit_t; typedef enum { #define _(f,i) ELF_SECTION_FLAG_##f = 1 << ELF_SECTION_FLAG_BIT_##f, foreach_elf_section_flag #undef _ } elf_section_flag_t; /* Symbol bindings (upper 4 bits of binding_and_type). */ #define foreach_elf_symbol_binding \ _ (LOCAL, 0) /* Local symbol */ \ _ (GLOBAL, 1) /* Global symbol */ \ _ (WEAK, 2) /* Weak symbol */ \ _ (OS_SPECIFIC_LO, 10) /* Start of OS-specific */ \ _ (OS_SPECIFIC_HI, 12) /* End of OS-specific */ \ _ (ARCH_SPECIFIC_LO, 13) /* Start of processor-specific */ \ _ (ARCH_SPECIFIC_HI, 15) /* End of processor-specific */ /* Symbol types (low
;;; 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))
"
/*
 * " plugin-name ".c - skeleton vpp engine plug-in 
 *
 * Copyright (c) <current-year> <your-organization>
 * 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 <vnet/vnet.h>
#include <vnet/plugin/plugin.h>
#include <" plugin-name "/" plugin-name ".h>

#include <vlibapi/api.h>
#include <vlibmemory/api.h>
#include <vlibsocket/api.h>

/* 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)

/* 
 * This routine exists to convince the vlib plugin framework that
 * we haven't accidentally copied a random .dll into the plugin directory.
 *
 * Also collects global variable pointers passed from the vpp engine
 */

clib_error_t * 
vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
                      int from_early_init)
{
  " plugin-name "_main_t * sm = &" plugin-name "_main;
  clib_error_t * error = 0;

  sm->vlib_main = vm;
  sm->vnet_main = h->vnet_main;
  sm->ethernet_main = h->ethernet_main;

  return error;
}

/* 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;
  u32 node_index = enable_disable ? " plugin-name "_node.index : ~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;
  
  /* 
   * Redirect pkts from the driver to the macswap node.
   * Returns VNET_API_ERROR_UNIMPLEMENTED if the h/w driver
   * doesn't implement the API. 
   *
   * Node_index = ~0 => shut off redirection
   */
  rv = vnet_hw_interface_rx_redirect_to_node (sm->vnet_main, sw_if_index,
                                              node_index);
  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