/* * 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, s
"""
  Neighbour Entries

  object abstractions for ARP and ND
"""

from ipaddress import ip_address
from vpp_object import VppObject
from vpp_papi import mac_pton, VppEnum
try:
    text_type = unicode
except NameError:
    text_type = str


def find_nbr(test, sw_if_index, nbr_addr, is_static=0, mac=None):
    ip_addr = ip_address(text_type(nbr_addr))
    e = VppEnum.vl_api_ip_neighbor_flags_t
    nbrs = test.vapi.ip_neighbor_dump(sw_if_index,
                                      is_ipv6=(6 == ip_addr.version))

    for n in nbrs:
        if ip_addr == n.neighbor.ip_address and \
           is_static == (n.neighbor.flags & e.IP_API_NEIGHBOR_FLAG_STATIC):
            if mac:
                if mac == str(n.neighbor.mac_address):
                    return True
            else:
                return True
    return False


class VppNeighbor(VppObject):
    """
    ARP Entry
    """

    def __init__(self, test, sw_if_index, mac_addr, nbr_addr,
                 is_static=False, is_no_fib_entry=False):
        self._test = test
        self.sw_if_index = sw_if_index
        self.mac_addr = mac_addr
        self.nbr_addr = nbr_addr

        e = VppEnum.vl_api_ip_neighbor_flags_t
        self.flags = e.IP_API_NEIGHBOR_FLAG_NONE
        if is_static:
            self.flags |= e.IP_API_NEIGHBOR_FLAG_STATIC
        if is_no_fib_entry:
            self.flags |= e.IP_API_NEIGHBOR_FLAG_NO_FIB_ENTRY

    def add_vpp_config(self):
        r = self._test.vapi.ip_neighbor_add_del(
            self.sw_if_index,
            self.mac_addr,
            self.nbr_addr,
            is_add=1,
            flags=self.flags)
        self.stats_index = r.stats_index
        self._test.registry.register(self, self._test.logger)

    def remove_vpp_config(self):
        self._test.vapi.ip_neighbor_add_del(
            self.sw_if_index,
            self.mac_addr,
            self.nbr_addr,
            is_add=0,
            flags=self.flags)

    def is_static(self):
        e = VppEnum.vl_api_ip_neighbor_flags_t
        return (self.flags & e.IP_API_NEIGHBOR_FLAG_STATIC)

    def query_vpp_config(self):
        return find_nbr(self._test,
                        self.sw_if_index,
                        self.nbr_addr,
                        self.is_static())

    def object_id(self):
        return ("%d:%s" % (self.sw_if_index, self.nbr_addr))

    def get_stats(self):
        c = self._test.statistics.get_counter("/net/adjacency")
        return c[0][self.stats_index]
segment_t; typedef struct { u8 need_byte_swap; u8 parsed_symbols; char *file_name; elf_first_header_t first_header; elf64_file_header_t file_header; elf_segment_t *segments; elf_section_t *sections; uword *section_by_name; uword *section_by_start_address; elf_symbol_table_t *symbol_tables; elf_relocation_table_t *relocation_tables; char *interpreter; elf64_dynamic_entry_t *dynamic_entries; u8 *dynamic_string_table; u32 dynamic_string_table_section_index; u32 dynamic_symbol_table_section_index; u32 dynamic_symbol_table_index; u32 dynamic_section_index; u16 *versym; u32 versym_section_index; elf_dynamic_version_need_union_t *verneed; u32 verneed_section_index; } elf_main_t; always_inline void elf_main_init (elf_main_t * em) { clib_memset (em, 0, sizeof (em[0])); } always_inline void elf_main_free (elf_main_t * em) { uword i; for (i = 0; i < vec_len (em->segments); i++) vec_free (em->segments[i].contents); vec_free (em->segments); for (i = 0; i < vec_len (em->sections); i++) vec_free (em->sections[i].contents); vec_free (em->sections); hash_free (em->section_by_name); for (i = 0; i < vec_len (em->symbol_tables); i++) elf_symbol_table_free (em->symbol_tables + i); for (i = 0; i < vec_len (em->relocation_tables); i++) elf_relocation_table_free (em->relocation_tables + i); vec_free (em->dynamic_entries); vec_free (em->interpreter); } always_inline void elf_get_segment_contents (elf_main_t * em, void *data, uword segment_index) { elf_segment_t *g = vec_elt_at_index (em->segments, segment_index); if (!g->contents) vec_add (g->contents, data + g->header.file_offset, g->header.memory_size); } always_inline void * elf_get_section_contents (elf_main_t * em, uword section_index, uword elt_size) { elf_section_t *s; void *result; s = vec_elt_at_index (em->sections, section_index); result = 0; if (vec_len (s->contents) > 0) { /* Make vector copy of contents with given element size. */ result = _vec_resize (result, vec_len (s->contents) / elt_size, vec_len (s->contents), /* header_bytes */ 0, /* align */ 0); clib_memcpy (result, s->contents, vec_len (s->contents)); } return result; } always_inline void elf_set_section_contents (elf_main_t * em, uword section_index, void *new_contents, uword n_content_bytes) { elf_section_t *s; s = vec_elt_at_index (em->sections, section_index); vec_free (s->contents); vec_add (s->contents, new_contents, n_content_bytes); } always_inline u8 * elf_section_name (elf_main_t * em, elf_section_t * s) { elf_section_t *es = vec_elt_at_index (em->sections, em-> file_header.section_header_string_table_index); return vec_elt_at_index (es->contents, s->header.name); } always_inline u8 elf_swap_u8 (elf_main_t * em, u8 x) { return x; } always_inline u16 elf_swap_u16 (elf_main_t * em, u16 x) { return em->need_byte_swap ? clib_byte_swap_u16 (x) : x; } always_inline u32 elf_swap_u32 (elf_main_t * em, u32 x) { return em->need_byte_swap ? clib_byte_swap_u32 (x) : x; } always_inline u64 elf_swap_u64 (elf_main_t * em, u64 x) { return em->need_byte_swap ? clib_byte_swap_u64 (x) : x; } #define FORMAT_ELF_MAIN_SYMBOLS (1 << 0) #define FORMAT_ELF_MAIN_RELOCATIONS (1 << 1) #define FORMAT_ELF_MAIN_DYNAMIC (1 << 2) format_function_t format_elf_main; format_function_t format_elf_symbol; clib_error_t *elf_read_file (elf_main_t * em, char *file_name); clib_error_t *elf_write_file (elf_main_t * em, char *file_name); clib_error_t *elf_delete_named_section (elf_main_t * em, char *section_name); clib_error_t *elf_parse (elf_main_t * em, void *data, uword data_bytes); void elf_parse_symbols (elf_main_t * em); clib_error_t *elf_get_section_by_name (elf_main_t * em, char *section_name, elf_section_t ** result); clib_error_t *elf_get_section_by_start_address (elf_main_t * em, uword start_address, elf_section_t ** result); void elf_create_section_with_contents (elf_main_t * em, char *section_name, elf64_section_header_t * header, void *contents, uword n_content_bytes); uword elf_delete_segment_with_type (elf_main_t * em, elf_segment_type_t segment_type); void elf_set_dynamic_entries (elf_main_t * em); #endif /* included_clib_elf_h */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */