/* Ido Barnea Cisco Systems, Inc. */ /* Copyright (c) 2015-2016 Cisco Systems, Inc. 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. */ /* Important classes in this file: CFlowStatUserIdInfo - Information about one packet group id CFlowStatUserIdMap - Mapping between packet group id (chosen by user) and hardware counter id CFlowStatHwIdMap - Mapping between hardware id and packet group id CFlowStatRuleMgr - API to users of the file General idea of operation: For each stream needing flow statistics, the user provides packet group id (pg_id). Few streams can have the same pg_id. We maintain reference count. When doing start_stream, for the first stream in pg_id, hw_id is associated with the pg_id, and relevant hardware rules are inserted (on supported hardware). When stopping all streams with the pg_id, the hw_id <--> pg_id mapping is removed, hw_id is returned to the free hw_id pool, and hardware rules are removed. Counters for the pg_id are kept. If starting streams again, new hw_id will be assigned, and counters will continue from where they stopped. Only When deleting all streams using certain pg_id, infromation about this pg_id will be freed. For each stream we keep state in the m_rx_check.m_hw_id field. Since we keep reference count for certain structs, we want to protect from illegal operations, like starting stream while it is already starting, stopping when it is stopped... State machine is: stream_init: HW_ID_INIT stream_add: HW_ID_FREE stream_start: legal hw_id (range is 0..MAX_FLOW_STATS) stream_stop: HW_ID_FREE stream_del: HW_ID_INIT */ #include #include #include #include #include #include "internal_api/trex_platform_api.h" #include "trex_stateless.h" #include "trex_stateless_messaging.h" #include "trex_stateless_rx_core.h" #include "trex_stream.h" #include "flow_stat_parser.h" #include "flow_stat.h" #define FLOW_STAT_ADD_ALL_PORTS 255 static const uint16_t HW_ID_INIT = UINT16_MAX; static const uint16_t HW_ID_FREE = UINT16_MAX - 1; static const uint8_t PAYLOAD_RULE_PROTO = 255; const uint32_t FLOW_STAT_PAYLOAD_IP_ID = IP_ID_RESERVE_BASE + MAX_FLOW_STATS; inline std::string methodName(const std::string& prettyFunction) { size_t colons = prettyFunction.find("::"); size_t begin = prettyFunction.substr(0,colons).rfind(" ") + 1; size_t end = prettyFunction.rfind("(") - begin; return prettyFunction.substr(begin,end) + "()"; } #define __METHOD_NAME__ methodName(__PRETTY_FUNCTION__) #ifdef __DEBUG_FUNC_ENTRY__ #define FUNC_ENTRY (std::cout << __METHOD_NAME__ << std::endl); #ifdef __STREAM_DUMP__ #define stream_dump(stream) stream->Dump(stderr) #else #define stream_dump(stream) #endif #else #define FUNC_ENTRY #endif /************** class CFlowStatUserIdInfo ***************/ CFlowStatUserIdInfo::CFlowStatUserIdInfo(uint16_t l3_proto, uint8_t l4_proto, uint8_t ipv6_next_h) { memset(m_rx_cntr, 0, sizeof(m_rx_cntr)); memset(m_rx_cntr_base, 0, sizeof(m_rx_cntr)); memset(m_tx_cntr, 0, sizeof(m_tx_cntr)); memset(m_tx_cntr_base, 0, sizeof(m_tx_cntr)); m_hw_id = UINT16_MAX; m_l3_proto = l3_proto; m_l4_proto = l4_proto; m_ipv6_next_h = ipv6_next_h; m_ref_count = 1; m_trans_ref_count = 0; m_was_sent = false; for (int i = 0; i < TREX_MAX_PORTS; i++) { m_rx_changed[i] = false; m_tx_changed[i] = false; } m_rfc2544_support = false; } std::ostream& operator<<(std::ostream& os, const CFlowStatUserIdInfo& cf) { os << "hw_id:" << cf.m_hw_id << " l3 proto:" << (uint16_t) cf.m_l3_proto << " ref(" << (uint16_t) cf.m_ref_count << "," << (uint16_t) cf.m_trans_ref_count << ")"; os << " rx count ("; os << cf.m_rx_cntr[0]; for (int i = 1; i < TREX_MAX_PORTS; i++) { os << "," << cf.m_rx_cntr[i]; } os << ")"; os << " rx count base("; os << cf.m_rx_cntr_base[0]; for (int i = 1; i < TREX_MAX_PORTS; i++) { os << "," << cf.m_rx_cntr_base[i]; } os << ")"; os << " tx count ("; os << cf.m_tx_cntr[0]; for (int i = 1; i < TREX_MAX_PORTS; i++) { os << "," << cf.m_tx_cntr[i]; } os << ")"; os << " tx count base("; os << cf.m_tx_cntr_base[0]; for (int i = 1; i < TREX_MAX_PORTS; i++) { os << "," << cf.m_tx_cntr_base[i]; } os << ")"; return os; } void CFlowStatUserIdInfo::add_stream(uint8_t proto) { #ifdef __DEBUG_FUNC_ENTRY__ std::cout << __METHOD_NAME__ << " proto:" << (uint16_t)proto << std::endl; #endif if (proto != m_l4_proto) throw TrexFStatEx("Can't use same pg_id for streams with different l4 protocol", TrexException::T_FLOW_STAT_PG_ID_DIFF_L4); m_ref_count++; } void CFlowStatUserIdInfo::reset_hw_id() { FUNC_ENTRY; m_hw_id = UINT16_MAX; // we are not attached to hw. Save packet count of session. // Next session will start counting from 0. for (int i = 0; i < TREX_MAX_PORTS; i++) { m_rx_cntr_base[i] += m_rx_cntr[i]; memset(&m_rx_cntr[i], 0, sizeof(m_rx_cntr[0])); m_tx_cntr_base[i] += m_tx_cntr[i]; memset(&m_tx_cntr[i], 0, sizeof(m_tx_cntr[0])); } } /************** class CFlowStatUserIdInfoPayload ***************/ void CFlowStatUserIdInfoPayload::add_stream(uint8_t proto) { throw TrexFStatEx("For payload rules: Can't have two streams with same pg_id, or same stream on more than one port" , TrexException::T_FLOW_STAT_DUP_PG_ID); } void CFlowStatUserIdInfoPayload::reset_hw_id() { CFlowStatUserIdInfo::reset_hw_id(); m_seq_err_base += m_rfc2544_info.m_seq_err; m_out_of_order_base += m_rfc2544_info.m_out_of_order; m_dup_base += m_rfc2544_info.m_dup; m_seq_err_ev_big_base += m_rfc2544_info.m_seq_err_ev_big; m_seq_err_ev_low_base += m_rfc2544_info.m_seq_err_ev_low; m_rfc2544_info.m_seq_err = 0; m_rfc2544_info.m_out_of_order = 0; m_rfc2544_info.m_dup = 0; m_rfc2544_info.m_seq_err_ev_big = 0; m_rfc2544_info.m_seq_err_ev_low = 0; } /************** class CFlowStatUserIdMap ***************/ CFlowStatUserIdMap::CFlowStatUserIdMap() { } std::ostream& operator<<(std::ostream& os, const CFlowStatUserIdMap& cf) { std::map::const_iterator it; for (it = cf.m_map.begin(); it != cf.m_map.end(); it++) { CFlowStatUserIdInfo *user_id_info = it->second; uint32_t user_id = it->first; os << "Flow stat user id info:\n"; os << " " << user_id << ":" << *user_id_info << std::endl; } return os; } uint16_t CFlowStatUserIdMap::get_hw_id(uint32_t user_id) { CFlowStatUserIdInfo *cf = find_user_id(user_id); if (cf == NULL) { return HW_ID_FREE; } else { return cf->get_hw_id(); } } CFlowStatUserIdInfo * CFlowStatUserIdMap::find_user_id(uint32_t user_id) { flow_stat_user_id_map_it_t it = m_map.find(user_id); if (it == m_map.end()) { return NULL; } else { return it->second; } } CFlowStatUserIdInfo * CFlowStatUserIdMap::add_user_id(uint32_t user_id, uint16_t l3_proto, uint8_t l4_proto, uint8_t ipv6_next_h) { #ifdef __DEBUG_FUNC_ENTRY__ std::cout << __METHOD_NAME__ << " user id:" << user_id << " proto:" << (uint16_t)proto << std::endl; #endif CFlowStatUserIdInfo *new_id; if (l4_proto == PAYLOAD_RULE_PROTO) { new_id = new CFlowStatUserIdInfoPayload(l3_proto, l4_proto, ipv6_next_h); } else { new_id = new CFlowStatUserIdInfo(l3_proto, l4_proto, ipv6_next_h); } if (new_id != NULL) { std::pair ret; ret = m_map.insert(std::pair(user_id, new_id)); if (ret.second == false) { delete new_id; throw TrexFStatEx("packet group id " + std::to_string(user_id) + " already exists" , TrexException::T_FLOW_STAT_ALREADY_EXIST); } return new_id; } else { throw TrexFStatEx("Failed allocating memory for new statistic counter" , TrexException::T_FLOW_STAT_ALLOC_FAIL); } } void CFlowStatUserIdMap::add_stream(uint32_t user_id, uint16_t l3_proto, uint8_t l4_proto, uint8_t ipv6_next_h) { #ifdef __DEBUG_FUNC_ENTRY__ std::cout << __METHOD_NAME__ << " user id:" << user_id << " l3 proto:" << (uint16_t)l3_proto << " l4 proto:" << (uint16_t)l4_proto << " IPv6 next header:" << (uint16_t)ipv6_next_h << std::endl; #endif CFlowStatUserIdInfo *c_user_id; c_user_id = find_user_id(user_id); if (! c_user_id) { // throws exception on error c_user_id = add_user_id(user_id, l3_proto, l4_proto, ipv6_next_h); } else { c_user_id->add_stream(l4_proto); } } int CFlowStatUserIdMap::del_stream(uint32_t user_id) { #ifdef __DEBUG_FUNC_ENTRY__ std::cout << __METHOD_NAME__ << " user id:" << user_id << std::endl; #endif CFlowStatUserIdInfo *c_user_id; c_user_id = find_user_id(user_id); if (! c_user_id) { throw TrexFStatEx("Trying to delete stream which does not exist" , TrexException::T_FLOW_STAT_DEL_NON_EXIST); } if (c_user_id->del_stream() == 0) { // ref count of this entry became 0. can release this entry. m_map.erase(user_id); delete c_user_id; } return 0; } int CFlowStatUserIdMap::start_stream(uint32_t user_id, uint16_t hw_id) { #ifdef __DEBUG_FUNC_ENTRY__ std::cout << __METHOD_NAME__ << " user id:" << user_id << " hw_id:" << hw_id << std::endl; #endif CFlowStatUserIdInfo *c_user_id; c_user_id = find_user_id(user_id); if (! c_user_id) { throw TrexFStatEx("Internal error: Trying to associate non exist group id " + std::to_string(user_id) + " to hardware id " + std::to_string(hw_id) , TrexException::T_FLOW_STAT_ASSOC_NON_EXIST_ID); } if (c_user_id->is_hw_id()) { throw TrexFStatEx("Internal error: Trying to associate hw id " + std::to_string(hw_id) + " to user_id " + std::to_string(user_id) + ", but it is already associated to " + std::to_string(c_user_id->get_hw_id()) , TrexException::T_FLOW_STAT_ASSOC_OCC_ID); } c_user_id->set_hw_id(hw_id); c_user_id->add_started_stream(); return 0; } int CFlowStatUserIdMap::start_stream(uint32_t user_id) { #ifdef __DEBUG_FUNC_ENTRY__ std::cout << __METHOD_NAME__ << " user id:" << user_id << std::endl; #endif CFlowStatUserIdInfo *c_user_id; c_user_id = find_user_id(user_id); if (! c_user_id) { throw TrexFStatEx("Trying to start stream with non exist packet group id " + std::to_string(user_id) , TrexException::T_FLOW_STAT_NON_EXIST_ID); } c_user_id->add_started_stream(); return 0; } // return: negative number in case of error. // Number of started streams attached to used_id otherwise. int CFlowStatUserIdMap::stop_stream(uint32_t user_id) { #ifdef __DEBUG_FUNC_ENTRY__ std::cout << __METHOD_NAME__ << " user id:" << user_id << std::endl; #endif CFlowStatUserIdInfo *c_user_id; c_user_id = find_user_id(user_id); if (! c_user_id) { throw TrexFStatEx("Trying to stop stream with non exist packet group id" + std::to_string(user_id) , TrexException::T_FLOW_STAT_NON_EXIST_ID); } return c_user_id->stop_started_stream(); } bool CFlowStatUserIdMap::is_started(uint32_t user_id) { CFlowStatUserIdInfo *c_user_id; c_user_id = find_user_id(user_id); if (! c_user_id) { return false; } return c_user_id->is_started(); } uint16_t CFlowStatUserIdMap::unmap(uint32_t user_id) { #ifdef __DEBUG_FUNC_ENTRY__ std::cout << __METHOD_NAME__ << " user id:" << user_id << std::endl; #endif CFlowStatUserIdInfo *c_user_id; c_user_id = find_user_id(user_id); if (! c_user_id) { return UINT16_MAX; } uint16_t old_hw_id = c_user_id->get_hw_id(); c_user_id->reset_hw_id(); return old_hw_id; } /************** class CFlowStatHwIdMap ***************/ CFlowStatHwIdMap::CFlowStatHwIdMap() { m_map = NULL; // must call create in order to work with the class m_num_free = 0; // to make coverity happy, init this here too. } CFlow
/*
 * ------------------------------------------------------------------
 * sr_api.c - ipv6 segment routing api
 *
 * 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.
 * ------------------------------------------------------------------
 */

#include <vnet/vnet.h>
#include <vnet/srmpls/sr_mpls.h>
#include <vlibmemory/api.h>

#include <vnet/interface.h>
#include <vnet/api_errno.h>
#include <vnet/feature/feature.h>
#include <vnet/ip/ip_types_api.h>

#include <vnet/format_fns.h>
#include <vnet/srmpls/sr_mpls.api_enum.h>
#include <vnet/srmpls/sr_mpls.api_types.h>


#define vl_api_version(n, v) static u32 api_version = v;
#include <vnet/srmpls/sr_mpls.api.h>
#undef vl_api_version

#define vl_endianfun
#include <vnet/srmpls/sr_mpls.api.h>
#undef vl_endianfun

#define vl_calcsizefun
#include <vnet/srmpls/sr_mpls.api.h>
#undef vl_calcsizefun

#define vl_printfun
#include <vnet/srmpls/sr_mpls.api.h>
#undef vl_printfun

#define vl_msg_name_crc_list
#include <vnet/srmpls/sr_mpls.api.h>
#undef vl_msg_name_crc_list

#define REPLY_MSG_ID_BASE msg_id_base
#include <vlibapi/api_helper_macros.h>

#define foreach_vpe_api_msg                             \
_(SR_MPLS_POLICY_DEL, sr_mpls_policy_del)                         \
_(SR_MPLS_STEERING_ADD_DEL, sr_mpls_steering_add_del)             \
_(SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR, sr_mpls_policy_assign_endpoint_color)

static u16 msg_id_base;

static void
vl_api_sr_mpls_policy_add_t_handler (vl_api_sr_mpls_policy_add_t * mp)
{
  vl_api_sr_mpls_policy_add_reply_t *rmp;

  mpls_label_t *segments = 0, *seg;
  mpls_label_t this_address = 0;

  int i;
  for (i = 0; i < mp->n_segments; i++)
    {
      vec_add2 (segments, seg, 1);
      this_address = ntohl (mp->segments[i]);
      clib_memcpy (seg, &this_address, sizeof (this_address));
    }

  int rv = 0;
  rv = sr_mpls_policy_add (ntohl (mp->bsid),
			   segments, mp->is_spray, ntohl (mp->weight));
  vec_free (segments);

  REPLY_MACRO (VL_API_SR_MPLS_POLICY_ADD_REPLY);
}

static void
vl_api_sr_mpls_policy_mod_t_handler (vl_api_sr_mpls_policy_mod_t * mp)
{
  vl_api_sr_mpls_policy_mod_reply_t *rmp;

  mpls_label_t *segments = 0, *seg;
  mpls_label_t this_address = 0;

  int i;
  for (i = 0; i < mp->n_segments; i++)
    {
      vec_add2 (segments, seg, 1);
      this_address = ntohl (mp->segments[i]);
      clib_memcpy (seg, &this_address, sizeof (this_address));
    }

  int rv = 0;
  rv = sr_mpls_policy_mod (ntohl (mp->bsid),
			   ntohl (mp->operation), segments,
			   ntohl (mp->sl_index), ntohl (mp->weight));
  vec_free (segments);

  REPLY_MACRO (VL_API_SR_MPLS_POLICY_MOD_REPLY);
}

static void
vl_api_sr_mpls_policy_del_t_handler (vl_api_sr_mpls_policy_del_t * mp)
{
  vl_api_sr_mpls_policy_del_reply_t *rmp;
  int rv = 0;
  rv = sr_mpls_policy_del (ntohl (mp->bsid));

  REPLY_MACRO (VL_API_SR_MPLS_POLICY_DEL_REPLY);
}

static void vl_api_sr_mpls_steering_add_del_t_handler
  (vl_api_sr_mpls_steering_add_del_t * mp)
{
  vl_api_sr_mpls_steering_add_del_reply_t *rmp;
  fib_prefix_t prefix;
  ip46_address_t next_hop;
  clib_memset (&prefix, 0, sizeof (ip46_address_t));

  ip_prefix_decode (&mp->prefix, &prefix);
  ip_address_decode (&mp->next_hop, &next_hop);

  int rv = 0;
  if (mp->is_del)
    rv = sr_mpls_steering_policy_del (&prefix.fp_addr,
				      prefix.fp_len,
				      ip46_address_is_ip4 (&prefix.fp_addr) ?
				      SR_STEER_IPV4 : SR_STEER_IPV6,
				      ntohl (mp->table_id),
				      ntohl (mp->color));
  else
    rv = sr_mpls_steering_policy_add (ntohl (mp->bsid),
				      ntohl (mp->table_id),
				      &prefix.fp_addr,
				      prefix.fp_len,
				      ip46_address_is_ip4 (&prefix.fp_addr) ?
				      SR_STEER_IPV4 : SR_STEER_IPV6,
				      &next_hop,
				      ip46_address_is_ip4 (&next_hop) ?
				      SR_STEER_IPV4 : SR_STEER_IPV6,
				      ntohl (mp->color), mp->co_bits,
				      ntohl (mp->vpn_label));

  REPLY_MACRO (VL_API_SR_MPLS_STEERING_ADD_DEL_REPLY);
}

static void vl_api_sr_mpls_policy_assign_endpoint_color_t_handler
  (vl_api_sr_mpls_policy_assign_endpoint_color_t * mp)
{
  vl_api_sr_mpls_policy_assign_endpoint_color_reply_t *rmp;
  int rv = 0;

  ip46_address_t endpoint;
  clib_memset (&endpoint, 0, sizeof (ip46_address_t));
  ip_address_decode (&mp->endpoint, &endpoint);

  rv = sr_mpls_policy_assign_endpoint_color (ntohl (mp->bsid),
					     &endpoint,
					     ip46_address_is_ip4 (&endpoint) ?
					     SR_STEER_IPV4 : SR_STEER_IPV6,
					     ntohl (mp->color));

  REPLY_MACRO (VL_API_SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR_REPLY);
}

static void
setup_message_id_table (api_main_t * am)
{
#define _(id, n, crc)                                                         \
  vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + REPLY_MSG_ID_BASE);
  foreach_vl_msg_name_crc_sr_mpls;
#undef _
}

static clib_error_t *
sr_mpls_api_hookup (vlib_main_t * vm)
{
  api_main_t *am = vlibapi_get_main ();

  u8 *name = format (0, "sr_mpls_%08x%c", api_version, 0);
  REPLY_MSG_ID_BASE =
    vl_msg_api_get_msg_ids ((char *) name, VL_MSG_SR_MPLS_LAST);
  vec_free (name);

#define _(N, n)                                                               \
  vl_msg_api_config (&(vl_msg_api_msg_config_t){                              \
    .id = REPLY_MSG_ID_BASE + VL_API_##N,                                     \
    .name = #n,                                                               \
    .handler = vl_api_##n##_t_handler,                                        \
    .endian = vl_api_##n##_t_endian,                                          \
    .format_fn = vl_api_##n##_t_format,                                       \
    .size = sizeof (vl_api_##n##_t),                                          \
    .traced = 1,                                                              \
    .tojson = vl_api_##n##_t_tojson,                                          \
    .fromjson = vl_api_##n##_t_fromjson,                                      \
    .calc_size = vl_api_##n##_t_calc_size,                                    \
  });
  foreach_vpe_api_msg;
#undef _

  /*
   * Manually register the sr policy add msg, so we trace enough bytes
   * to capture a typical segment list
   */
  vl_msg_api_config (&(vl_msg_api_msg_config_t){
    .id = REPLY_MSG_ID_BASE + VL_API_SR_MPLS_POLICY_ADD,
    .name = &q