/* * 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. */ /* * node_funcs.h: processing nodes global functions/inlines * * Copyright (c) 2008 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. */ /** \file vlib node functions */ #ifndef included_vlib_node_funcs_h #define included_vlib_node_funcs_h #include #include /** \brief Get vlib node by index. @warning This function will ASSERT if @c i is out of range. @param vm vlib_main_t pointer, varies by thread @param i node index. @return pointer to the requested vlib_node_t. */ always_inline vlib_node_t * vlib_get_node (vlib_main_t * vm, u32 i) { return vec_elt (vm->node_main.nodes, i); } /** \brief Get vlib node by graph arc (next) index. @param vm vlib_main_t pointer, varies by thread @param node_index index of original node @param next_index graph arc index @return pointer to the vlib_node_t at the end of the indicated arc */ always_inline vlib_node_t * vlib_get_next_node (vlib_main_t * vm, u32 node_index, u32 next_index) { vlib_node_main_t *nm = &vm->node_main; vlib_node_t *n; n = vec_elt (nm->nodes, node_index); ASSERT (next_index < vec_len (n->next_nodes)); return vlib_get_node (vm, n->next_nodes[next_index]); } /** \brief Get node runtime by node index. @param vm vlib_main_t pointer, varies by thread @param node_index index of node @return pointer to the indicated vlib_node_runtime_t */ always_inline vlib_node_runtime_t * vlib_node_get_runtime (vlib_main_t * vm, u32 node_index) { vlib_node_main_t *nm = &vm->node_main; vlib_node_t *n = vec_elt (nm->nodes, node_index); vlib_process_t *p; if (n->type != VLIB_NODE_TYPE_PROCESS) return vec_elt_at_index (nm->nodes_by_type[n->type], n->runtime_index); else { p = vec_elt (nm->processes, n->runtime_index); return &p->node_runtime; } } /** \brief Get node runtime private data by node index. @param vm vlib_main_t pointer, varies by thread @param node_index index of the node @return pointer to the indicated vlib_node_runtime_t private data */ always_inline void * vlib_node_get_runtime_data (vlib_main_t * vm, u32 node_index) { vlib_node_runtime_t *r = vlib_node_get_runtime (vm, node_index); return r->runtime_data; } /** \brief Set node runtime private data. @param vm vlib_main_t pointer, varies by thread @param node_index index of the node @param runtime_data arbitrary runtime private data @param n_runtime_data_bytes size of runtime private data */ always_inline void vlib_node_set_runtime_data (vlib_main_t * vm, u32 node_index, void *runtime_data, u32 n_runtime_data_bytes) { vlib_node_t *n = vlib_get_node (vm, node_index); vlib_node_runtime_t *r = vlib_node_get_runtime (vm, node_index); n->runtime_data_bytes = n_runtime_data_bytes; vec_free (n->runtime_data); vec_add (n->runtime_data, runtime_data, n_runtime_data_bytes); ASSERT (vec_len (n->runtime_data) <= sizeof (vlib_node_runtime_t) - STRUCT_OFFSET_OF (vlib_node_runtime_t, runtime_data)); if (vec_len (n->runtime_data) > 0) clib_memcpy (r->runtime_data, n->runtime_data, vec_len (n->runtime_data)); } /** \brief Set node dispatch state. @param vm vlib_main_t pointer, varies by thread @param node_index index of the node @param new_state new state for node, see vlib_node_state_t */ always_inline void vlib_node_set_state (vlib_main_t * vm, u32 node_index, vlib_node_state_t new_state) { vlib_node_main_t *nm = &vm->node_main; vlib_node_t *n; vlib_node_runtime_t *r; n = vec_elt (nm->nodes, node_index); if (n->type == VLIB_NODE_TYPE_PROCESS) { vlib_process_t *p = vec_elt (nm->processes, n->runtime_index); r = &p->node_runtime; /* When disabling make sure flags are cleared. */ p->flags &= ~(VLIB_PROCESS_RESUME_PENDING | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT); } else r = vec_elt_at_index (nm->nodes_by_type[n->type], n->runtime_index); ASSERT (new_state < VLIB_N_NODE_STATE); if (n->type == VLIB_NODE_TYPE_INPUT) { ASSERT (nm->input_node_counts_by_state[n->state] > 0); nm->input_node_counts_by_state[n->state] -= 1; nm->input_node_counts_by_state[new_state] += 1; } n->state = new_state; r->state = new_state; } /** \brief Get node dispatch state. @param vm vlib_main_t pointer, varies by thread @param node_index index of the node @return state for node, see vlib_node_state_t */ always_inline vlib_node_state_t vlib_node_get_state (vlib_main_t * vm, u32 node_index) { vlib_node_main_t *nm = &vm->node_main; vlib_node_t *n; n = vec_elt (nm->nodes, node_index); return n->state; } always_inline void vlib_node_set_interrupt_pending (vlib_main_t * vm, u32 node_index) { vlib_node_main_t *nm = &vm->node_main; vlib_node_t *n = vec_elt (nm->nodes, node_index); ASSERT (n->type == VLIB_NODE_TYPE_INPUT); clib_spinlock_lock_if_init (&nm->pending_interrupt_lock); vec_add1 (nm->pending_interrupt_node_runtime_indices, n->runtime_index); clib_spinlock_unlock_if_init (&nm->pending_interrupt_lock); } always_inline vlib_process_t * vlib_get_process_from_node (vlib_main_t * vm, vlib_node_t * node) { vlib_node_main_t *nm = &vm->node_main; ASSERT (node->type == VLIB_NODE_TYPE_PROCESS); return vec_elt (nm->processes, node->runtime_index); } /* Fetches frame with given handle. */ always_inline vlib_frame_t * vlib_get_frame_no_check (vlib_main_t * vm, uword frame_index) { vlib_frame_t *f; f = vm->heap_aligned_base + (frame_index * VLIB_FRAME_ALIGN); return f; } always_inline u32 vlib_frame_index_no_check (vlib_main_t * vm, vlib_frame_t * f) { uword i; ASSERT (((uword) f & (VLIB_FRAME_ALIGN - 1)) == 0); i = ((u8 *) f - (u8 *) vm->heap_aligned_base); ASSERT ((i / VLIB_FRAME_ALIGN) <= 0xFFFFFFFFULL); return i / VLIB_FRAME_ALIGN; } always_inline vlib_frame_t * vlib_get_frame (vlib_main_t * vm, uword frame_index) { vlib_frame_t *f = vlib_get_frame_no_check (vm, frame_index); ASSERT (f->flags & VLIB_FRAME_IS_ALLOCATED); return f; } always_inline u32 vlib_frame_index (vlib_main_t * vm, vlib_frame_t * f) { uword i = vlib_frame_index_no_check (vm, f); ASSERT (vlib_get_frame (vm, i) == f); return i; } /* Byte alignment for vector arguments. */ #define VLIB_FRAME_VECTOR_ALIGN (1 << 4) always_inline u32 vlib_frame_vector_byte_offset (u32 scalar_size) { return round_pow2 (sizeof (vlib_frame_t) + scalar_size, VLIB_FRAME_VECTOR_ALIGN); } /** \brief Get pointer to frame vector data. @param f vlib_frame_t pointer @return pointer to first vector element in frame */ always_inline void * vlib_frame_vector_args (vlib_frame_t * f) { return (void *) f + vlib_frame_vector_byte_offset (f->scalar_size); } /** \brief Get pointer to frame scalar data. @warning This is almost certainly not the function you wish to call. See @ref vlib_frame_vector_args instead. @param f vlib_frame_t pointer @return arbitrary node scalar data @sa vlib_frame_vector_args */ always_inline void * vlib_frame_args (vlib_frame_t * f) { return vlib_frame_vector_args (f) - f->scalar_size; } always_inline vlib_next_frame_t * vlib_node_runtime_get_next_frame (vlib_main_t * vm, vlib_node_runtime_t * n, u32 next_index) { vlib_node_main_t *nm = &vm->node_main; vlib_next_frame_t *nf; ASSERT (next_index < n->n_next_nodes); nf = vec_elt_at_index (nm->next_frames, n->next_frame_index + next_index); if (CLIB_DEBUG > 0) { vlib_node_t *node, *next; node = vec_elt (nm->nodes, n->node_index); next = vec_elt (nm->nodes, node->next_nodes[next_index]); ASSERT (nf->node_runtime_index == next->runtime_in
/*
 * l2_bvi.h : layer 2 Bridged Virtual Interface
 *
 * Copyright (c) 2013 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_l2bvi_h
#define included_l2bvi_h

#include <vlib/vlib.h>
#include <vnet/ethernet/ethernet.h>
#include <vppinfra/sparse_vec.h>

#include <vnet/l2/l2_input.h>

#define TO_BVI_ERR_OK        0
#define TO_BVI_ERR_BAD_MAC   1
#define TO_BVI_ERR_ETHERTYPE 2

static_always_inline u32
l2_to_bvi_dmac_check (vnet_hw_interface_t * hi, u8 * dmac,
		      ethernet_interface_t * ei, u8 have_sec_dmac)
{
  ethernet_interface_address_t *sec_addr;

  if (ethernet_mac_address_equal (dmac, hi->hw_address))
    return TO_BVI_ERR_OK;

  if (have_sec_dmac)
    {
      vec_foreach (sec_addr, ei->secondary_addrs)
      {
	if (ethernet_mac_address_equal (dmac, sec_addr->mac.bytes))
	  return TO_BVI_ERR_OK;
      }
    }

  return TO_BVI_ERR_BAD_MAC;
}

/**
 * Send a packet from L2 processing to L3 via the BVI interface.
 * Set next0 to the proper L3 input node.
 * Return an error if the packet isn't what we expect.
 */

static_always_inline u32
l2_to_bvi (vlib_main_t * vlib_main,
	   vnet_main_t * vnet_main,
	   vlib_buffer_t * b0,
	   u32 bvi_sw_if_index, next_by_ethertype_t * l3_next, u16 * next0)
{
  ethernet_main_t *em = &ethernet_main;

  /* Perform L3 my-mac filter */
  ethernet_header_t *e0 = vlib_buffer_get_current (b0);
  if (!ethernet_address_cast (e0->dst_address))
    {
      vnet_hw_interface_t *hi =
	vnet_get_sup_hw_interface (vnet_main, bvi_sw_if_index);
      ethernet_interface_t *ei = ethernet_get_interface (em, hi->hw_if_index);
      u32 rv;

      if (PREDICT_FALSE (ei && (vec_len (ei->secondary_addrs) > 0)))
	rv = l2_to_bvi_dmac_check (hi, e0->dst_address, ei,
				   1 /* have_sec_dmac */ );
      else
	rv = l2_to_bvi_dmac_check (hi, e0->dst_address, ei,
				   0 /* have_sec_dmac */ );

      if (rv != TO_BVI_ERR_OK)
	return rv;
    }

  /* Save L2 header position which may be changed due to packet replication */
  vnet_buffer (b0)->l2_hdr_offset = b0->current_data;

  /* Strip L2 header */
  u8 l2_len = vnet_buffer (b0)->l2.l2_len;
  vlib_buffer_advance (b0, l2_len);

  u8 *l3h = vlib_buffer_get_current (b0);
  u16 ethertype = clib_net_to_host_u16 (*(u16 *) (l3h - 2));

  /* Set the input interface to be the BVI interface */
  vnet_buffer (b0)->sw_if_index[VLIB_RX] = bvi_sw_if_index;
  vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;

  /* Go to appropriate L3 input node */
  if (ethertype == ETHERNET_TYPE_IP4)
    {
      *next0 = l3_next->input_next_ip4;
    }
  else if (ethertype == ETHERNET_TYPE_IP6)
    {
      *next0 = l3_next->input_next_ip6;
    }
  else
    {
      /* uncommon ethertype, check table */
      u32 i0 = sparse_vec_index (l3_next->input_next_by_type, ethertype);
      *next0 = vec_elt (l3_next->input_next_by_type, i0);

      if (i0 == SPARSE_VEC_INVALID_INDEX)
	{
	  return TO_BVI_ERR_ETHERTYPE;
	}
    }

  /* increment BVI RX interface stat */
  vlib_increment_combined_counter
    (vnet_main->interface_main.combined_sw_if_counters
     + VNET_INTERFACE_COUNTER_RX,
     vlib_main->thread_index, bvi_sw_if_index,
     1, vlib_buffer_length_in_chain (vlib_main, b0));
  return TO_BVI_ERR_OK;
}

void
l2bvi_register_input_type (vlib_main_t * vm,
			   ethernet_type_t type, u32 node_index);

extern int l2_bvi_create (u32 instance, const mac_address_t * mac,
			  u32 * sw_if_index);
extern int l2_bvi_delete (u32 sw_if_index);

#endif

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
har *next_name, uword slot); /* As above but adds to end of node's next vector. */ always_inline uword vlib_node_add_named_next (vlib_main_t * vm, uword node, char *name) { return vlib_node_add_named_next_with_slot (vm, node, name, ~0); } /** * Get list of nodes */ void vlib_node_get_nodes (vlib_main_t * vm, u32 max_threads, int include_stats, int barrier_sync, vlib_node_t **** node_dupsp, vlib_main_t *** stat_vmsp); /* Query node given name. */ vlib_node_t *vlib_get_node_by_name (vlib_main_t * vm, u8 * name); /* Rename a node. */ void vlib_node_rename (vlib_main_t * vm, u32 node_index, char *fmt, ...); /* Register new packet processing node. Nodes can be registered dynamically via this call or statically via the VLIB_REGISTER_NODE macro. */ u32 vlib_register_node (vlib_main_t * vm, vlib_node_registration_t * r); /* Register all static nodes registered via VLIB_REGISTER_NODE. */ void vlib_register_all_static_nodes (vlib_main_t * vm); /* Start a process. */ void vlib_start_process (vlib_main_t * vm, uword process_index); /* Sync up runtime and main node stats. */ void vlib_node_sync_stats (vlib_main_t * vm, vlib_node_t * n); /* Node graph initialization function. */ clib_error_t *vlib_node_main_init (vlib_main_t * vm); format_function_t format_vlib_node_graph; format_function_t format_vlib_node_name; format_function_t format_vlib_next_node_name; format_function_t format_vlib_node_and_next; format_function_t format_vlib_cpu_time; format_function_t format_vlib_time; /* Parse node name -> node index. */ unformat_function_t unformat_vlib_node; always_inline void vlib_node_increment_counter (vlib_main_t * vm, u32 node_index, u32 counter_index, u64 increment) { vlib_node_t *n = vlib_get_node (vm, node_index); vlib_error_main_t *em = &vm->error_main; u32 node_counter_base_index = n->error_heap_index; em->counters[node_counter_base_index + counter_index] += increment; } #endif /* included_vlib_node_funcs_h */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */