/* * 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-2005 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. */ #include #include #include #include /* for clib_arch_is_big_endian */ always_inline void zero_pair (hash_t * h, hash_pair_t * p) { clib_memset (p, 0, hash_pair_bytes (h)); } always_inline void init_pair (hash_t * h, hash_pair_t * p) { clib_memset (p->value, ~0, hash_value_bytes (h)); } always_inline hash_pair_union_t * get_pair (void *v, uword i) { hash_t *h = hash_header (v); hash_pair_t *p; ASSERT (i < vec_len (v)); p = v; p += i << h->log2_pair_size; return (hash_pair_union_t *) p; } always_inline void set_is_user (void *v, uword i, uword is_user) { hash_t *h = hash_header (v); uword i0 = i / BITS (h->is_user[0]); uword i1 = (uword) 1 << (i % BITS (h->is_user[0])); if (is_user) h->is_user[i0] |= i1; else h->is_user[i0] &= ~i1; } static u8 *hash_format_pair_default (u8 * s, va_list * args); #if uword_bits == 64 static inline u64 zap64 (u64 x, word n) { #define _(n) (((u64) 1 << (u64) (8*(n))) - (u64) 1) static u64 masks_little_endian[] = { 0, _(1), _(2), _(3), _(4), _(5), _(6), _(7), }; static u64 masks_big_endian[] = { 0, ~_(7), ~_(6), ~_(5), ~_(4), ~_(3), ~_(2), ~_(1), }; #undef _ if (clib_arch_is_big_endian) return x & masks_big_endian[n]; else return x & masks_little_endian[n]; } /** * make address-sanitizer skip this: * clib_mem_unaligned + zap64 casts its input as u64, computes a mask * according to the input length, and returns the casted maked value. * Therefore all the 8 Bytes of the u64 are systematically read, which * rightfully causes address-sanitizer to raise an error on smaller inputs. * * However the invalid Bytes are discarded within zap64(), which is why * this can be silenced safely. * * The above is true *unless* the extra bytes cross a page boundary * into unmapped or no-access space, hence the boundary crossing check. */ static inline u64 hash_memory64 (void *p, word n_bytes, u64 state) { u64 *q = p; u64 a, b, c, n; int page_boundary_crossing; u64 start_addr, end_addr; union { u8 as_u8[8]; u64 as_u64; } tmp; /* * If the request crosses a 4k boundary, it's not OK to assume * that the zap64 game is safe. 4k is the minimum known page size. */ start_addr = (u64) p; end_addr = start_addr + n_bytes + 7; page_boundary_crossing = (start_addr >> 12) != (end_addr >> 12); a = b = 0x9e3779b97f4a7c13LL; c = state; n = n_bytes; while (n >= 3 * sizeof (u64)) { a += clib_mem_unaligned (q + 0, u64); b += clib_mem_unaligned (q + 1, u64); c += clib_mem_unaligned (q + 2, u64); hash_mix64 (a, b, c); n -= 3 * sizeof (u64); q += 3; } c += n_bytes; switch (n / sizeof (u64)) { case 2: a += clib_mem_unaligned (q + 0, u64); b += clib_mem_unaligned (q + 1, u64); if (n % sizeof (u64)) { if (PREDICT_TRUE (page_boundary_crossing == 0)) c += zap64 (CLIB_MEM_OVERFLOW (clib_mem_unaligned (q + 2, u64), q + 2, sizeof (u64)), n % sizeof (u64)) << 8; else { clib_memcpy_fast (tmp.as_u8, q + 2, n % sizeof (u64)); c += zap64 (tmp.as_u64, n % sizeof (u64)) << 8; } } break; case 1: a += clib_mem_unaligned (q + 0, u64); if (n % sizeof (u64)) { if (PREDICT_TRUE (page_boundary_crossing == 0)) b += zap64 (CLIB_MEM_OVERFLOW (clib_mem_unaligned (q + 1, u64), q + 1, sizeof (u64)), n % sizeof (u64)); else { clib_memcpy_fast (tmp.as_u8, q + 1, n % sizeof (u64)); b += zap64 (tmp.as_u64, n % sizeof (u64)); } } break; case 0: if (n % sizeof (u64)) { if (PREDICT_TRUE (page_boundary_crossing == 0)) a += zap64 (CLIB_MEM_OVERFLOW (clib_mem_unaligned (q + 0, u64), q + 0, sizeof (u64)), n % sizeof (u64)); else { clib_memcpy_fast (tmp.as_u8, q, n % sizeof (u64)); a += zap64 (tmp.as_u64, n % sizeof (u64)); } } break; } hash_mix64 (a, b, c); return c; } #else /* if uword_bits == 64 */ static inline u32 zap32 (u32 x, word n) { #define _(n) (((u32) 1 << (u32) (8*(n))) - (u32) 1) static u32 masks_little_endian[] = { 0, _(1), _(2), _(3), }; static u32 masks_big_endian[] = { 0, ~_(3), ~_(2), ~_(1), }; #undef _ if (clib_arch_is_big_endian) return x & masks_big_endian[n]; else return x & masks_little_endian[n]; } static inline u32 hash_memory32 (void *p, word n_bytes, u32 state) { u32 *q = p; u32 a, b, c, n; a = b = 0x9e3779b9; c = state; n = n_bytes; while (n >= 3 * sizeof (u32)) { a += clib_mem_unaligned (q + 0, u32); b += clib_mem_unaligned (q + 1, u32); c += clib_mem_unaligned (q + 2, u32); hash_mix32 (a, b, c); n -= 3 * sizeof (u32); q += 3; } c += n_bytes; switch (n / sizeof (u32)) { case 2: a += clib_mem_unaligned (q + 0, u32); b += clib_mem_unaligned (q + 1, u32); if (n % sizeof (u32)) c += zap32 (clib_mem_unaligned (q + 2, u32), n % sizeof (u32)) << 8; break; case 1: a += clib_mem_unaligned (q + 0, u32); if (n % sizeof (u32)) b += zap32 (clib_mem_unaligned (q + 1, u32), n % sizeof (u32)); break; case 0: if (n % sizeof (u32)) a += zap32 (clib_mem_unaligned (q + 0, u32), n % sizeof (u32)); break; } hash_mix32 (a, b, c); return c; } #endif __clib_export uword hash_memory (void *p, word n_bytes, uword state) { uword *q = p; #if uword_bits == 64 return hash_memory64 (q, n_bytes, state); #else return hash_memory32 (q, n_bytes, state); #endif } #if uword_bits == 64 always_inline uword hash_uword (uword x) { u64 a, b, c; a = b = 0x9e3779b97f4a7c13LL; c = 0; a += x; hash_mix64 (a, b, c); return c; } #else always_inline uword hash_uword (uword x) { u32 a, b, c; a = b = 0x9e3779b9; c = 0; a += x; hash_mix32 (a, b, c); return c; } #endif /* Call sum function. Hash code will be sum function value modulo the prime length of the hash table. */ always_inline uword key_sum (hash_t * h, uword key) { uword sum; switch (pointer_to_uword ((void *) h->key_sum)) { case KEY_FUNC_NONE: sum = hash_uword (key); break; case KEY_FUNC_POINTER_UWORD: sum = hash_uword (*uword_to_pointer (key, uword *)); break; case KEY_FUNC_POINTER_U32: sum = hash_uword (*uword_to_pointer (key, u32 *)); break; case KEY_FUNC_STRING: sum = string_key_sum (h, key); break; case KEY_FUNC_MEM: sum = mem_key_sum (h, key); break; default: sum = h->key_sum (h, key); break; } return sum; } always_inline uword key_equal1 (hash_t * h, uword key1, uword key2, uword e) { switch (pointer_to_uword ((void *) h->key_equal)) { case KEY_FUNC_NONE: break; case KEY_FUNC_POINTER_UWORD: e = *uword_to_pointer (key1, uword *) == *uword_to_pointer (key2, uword *); break; case KEY_FUNC_POINTER_U32: e = *uword_to_pointer (key1, u32 *) == *uword_to_pointer (key2, u32 *); break; case KEY_FUNC_STRING: e = string_key_equal (h, key1, key2); break; case KEY_FUNC_MEM: e = mem_key_equal (h, key1, key2); break; default: e = h->key_equal (h, key1, key2); break; } return e; } /* Compares two keys: returns 1 if equal, 0 if not. */ always_inline uword key_equal (hash_t * h, uword key1, uword key2) { uword e = key1 == key2; if (CLIB_DEBUG > 0 && key1 == key2) ASSERT (key_equal1 (h, key1, key2, e)); if (!e) e = key_equal1 (h, key1, key2, e); return e; } static hash_pair_union_t * get_indirect (void *v, hash_pair_indirect_t * pi, uword key) { hash_t *h = hash_header (v); hash_pair_t *p0, *p1; p0 = p1 = pi->pairs; if (h->log2_pair_size > 0) p1 = hash_forward (h, p0, indirect_pair_get_len (pi)); else p1 += vec_len (p0); while (p0 < p1)
/*
 * cdp.h - cdp protocol plug-in
 *
 * Copyright (c) 2011-2018 by 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.
 */
#ifndef __included_cdp_h__
#define __included_cdp_h__

#include <vlib/vlib.h>
#include <vlib/unix/unix.h>

#include <vnet/snap/snap.h>
#include <vnet/hdlc/hdlc.h>
#include <vnet/hdlc/packet.h>

#include <vppinfra/format.h>
#include <vppinfra/hash.h>

#include "cdp_protocol.h"

typedef enum
{
  CDP_PACKET_TEMPLATE_ETHERNET,
  CDP_PACKET_TEMPLATE_HDLC,
  CDP_PACKET_TEMPLATE_SRP,
  CDP_N_PACKET_TEMPLATES,
} cdp_packet_template_id_t;

typedef struct
{
  /* neighbor's vlib software interface index */
  u32 sw_if_index;

  /* Timers */
  f64 last_heard;
  f64 last_sent;

  /* Neighbor time-to-live (usually 180s) */
  u8 ttl_in_seconds;

  /* "no cdp run" or similar */
  u8 disabled;

  /* tx packet template id for this neighbor */
  u8 packet_template_index;

  /* Jenkins hash optimization: avoid tlv scan, send short keepalive msg */
  u8 last_packet_signature_valid;
  uword last_packet_signature;

  /* Info we actually keep about each neighbor */
  u8 *device_name;
  u8 *version;
  u8 *port_id;
  u8 *platform;

  /* last received packet, for the J-hash optimization */
  u8 *last_rx_pkt;
} cdp_neighbor_t;

#define foreach_neighbor_string_field           \
_(device_name)                                  \
_(version)                                      \
_(port_id)                                      \
_(platform)

typedef struct
{
  /* pool of cdp neighbors */
  cdp_neighbor_t *neighbors;

  /* plugin message id base */
  u16 msg_id_base;

  /* tx pcap debug enable */
  u8 tx_pcap_debug;

  /* rapidly find a neighbor by vlib software interface index */
  uword *neighbor_by_sw_if_index;

  /* Background process node index */
  u32 cdp_process_node_index;

  /* top-level state */
  int enabled;
  int cdp_protocol_registered;

  /* Packet templates for different encap types */
  vlib_packet_template_t packet_templates[CDP_N_PACKET_TEMPLATES];

  /* convenience variables */
  vlib_main_t *vlib_main;
  vnet_main_t *vnet_main;
} cdp_main_t;

extern cdp_main_t cdp_main;
extern vlib_node_registration_t cdp_process_node;

/* Packet counters */
#define foreach_cdp_error                                       \