/* *------------------------------------------------------------------ * Copyright (c) 2017 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 vapi_hpp_included #define vapi_hpp_included #include #include #include #include #include #include #include #include #include #include #include #include #include #if VAPI_CPP_DEBUG_LEAKS #include #endif /** * @file * @brief C++ VPP API */ namespace vapi { class Connection; template class Request; template class Msg; template void vapi_swap_to_be (M *msg); template void vapi_swap_to_host (M *msg); template M *vapi_alloc (Connection &con, Args...); template vapi_msg_id_t vapi_get_msg_id_t (); template class Event_registration; class Unexpected_msg_id_exception : public std::exception { public: virtual const char *what () const throw () { return "unexpected message id"; } }; class Msg_not_available_exception : public std::exception { public: virtual const char *what () const throw () { return "message unavailable"; } }; typedef enum { /** response not ready yet */ RESPONSE_NOT_READY, /** response to request is ready */ RESPONSE_READY, /** no response to request (will never come) */ RESPONSE_NO_RESPONSE, } vapi_response_state_e; /** * Class representing common functionality of a request - response state * and context */ class Common_req { public: virtual ~Common_req (){}; Connection &get_connection () { return con; }; vapi_response_state_e get_response_state (void) const { return response_state; } private: Connection &con; Common_req (Connection &con) : con (con), context{0}, response_state{RESPONSE_NOT_READY} { } void set_response_state (vapi_response_state_e state) { response_state = state; } virtual std::tuple assign_response (vapi_msg_id_t id, void *shm_data) = 0; void set_context (u32 context) { this->context = context; } u32 get_context () { return context; } u32 context; vapi_response_state_e response_state; friend class Connection; template friend class Msg; template friend class Request; template friend class Dump; template friend class Event_registration; }; /** * Class representing a connection to VPP * * After creating a Connection object, call connect() to actually connect * to VPP. Use is_msg_available to discover whether a specific message is known * and supported by the VPP connected to. */ class Connection { public: Connection (void) : vapi_ctx{0}, event_count{0} { vapi_error_e rv = VAPI_OK; if (!vapi_ctx) { if (VAPI_OK != (rv = vapi_ctx_alloc (&vapi_ctx))) { throw std::bad_alloc (); } } events.reserve (vapi_get_message_count () + 1); } Connection (const Connection &) = delete; ~Connection (void) { vapi_ctx_free (vapi_ctx); #if VAPI_CPP_DEBUG_LEAKS for (auto x : shm_data_set) { printf ("Leaked shm_data@%p!\n", x); } #endif } /** * @brief check if message identified by it's message id is known by the * vpp to which the connection is open */ bool is_msg_available (vapi_msg_id_t type) { return vapi_is_msg_available (vapi_ctx, type); } /** * @brief connect to vpp * * @param name application name * @param chroot_prefix shared memory prefix * @param max_queued_request max number of outstanding requests queued * * @return VAPI_OK on success, other error code on error */ vapi_error_e connect (const char *name, const char *chroot_prefix, int max_outstanding_requests, int response_queue_size) { return vapi_connect (vapi_ctx, name, chroot_prefix, max_outstanding_requests, response_queue_size, VAPI_MODE_BLOCKING); } /** * @brief disconnect from vpp * * @return VAPI_OK on success, other error code on error */ vapi_error_e disconnect () { auto x = requests.size (); while (x > 0) { VAPI_DBG ("popping request @%p", requests.front ()); requests.pop_front (); --x; } return vapi_disconnect (vapi_ctx); }; /** * @brief get event file descriptor * * @note this file descriptor becomes readable when messages (from vpp) * are waiting in queue * * @param[out] fd pointer to result variable * * @return VAPI_OK on success, other error code on error */ vapi_error_e get_fd (int *fd) { return vapi_get_fd (vapi_ctx, fd); } /** * @brief wait for responses from vpp and assign them to appropriate objects * * @param limit stop dispatch after the limit object received it's response * * @return VAPI_OK on success, other error code on error */ vapi_error_e dispatch (const Common_req *limit = nullptr) { std::lock_guard lock (dispatch_mutex); vapi_error_e rv = VAPI_OK; bool loop_again = true; while (loop_again) { void *shm_data; size_t shm_data_size; rv = vapi_recv (vapi_ctx, &shm_data, &shm_data_size); if (VAPI_OK != rv) { return rv; } #if VAPI_CPP_DEBUG_LEAKS on_shm_data_alloc (shm_data); #endif std::lock_guard requests_lock (requests_mutex); std::lock_guard events_lock (events_mutex); vapi_msg_id_t id = vapi_lookup_vapi_msg_id_t ( vapi_ctx, be16toh (*static_cast (shm_data))); bool has_context = vapi_msg_is_with_context (id); bool break_dispatch = false; Common_req *matching_req = nullptr; if (has_context) { u32 context = *reinterpret_cast ( (static_cast (shm_data) + vapi_get_context_offset (id))); const auto x = requ
/*
 * Copyright (c) 2018 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 __GBP_LEARN_H__
#define __GBP_LEARN_H__

#include <plugins/gbp/gbp.h>

/**
 * The maximum learning rate per-hashed EP
 */
#define GBP_ENDPOINT_HASH_LEARN_RATE (1e-2)

typedef enum gbp_learn_mode_t_
{
  GBP_LEARN_MODE_L2,
  GBP_LEARN_MODE_L3,
} gbb_learn_mode_t;

/**
 * Grouping of global data for the GBP source EPG classification feature
 */
typedef struct gbp_learn_main_t_
{
  /**
   * Next nodes for L2 output features
   */
  u32 gl_l2_input_feat_next[32];

  /**
   * logger - VLIB log class
   */
  vlib_log_class_t gl_logger;

  /**
   * throttles for the DP leanring
   */
  throttle_t gl_l2_throttle;
  throttle_t gl_l3_throttle;
} gbp_learn_main_t;

extern gbp_learn_main_t gbp_learn_main;

extern void gbp_learn_enable (u32 sw_if_index, gbb_learn_mode_t mode);
extern void gbp_learn_disable (u32 sw_if_index, gbb_learn_mode_t mode);

extern void gbp_learn_set_inactive_threshold (u32 max_age);

#endif

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
ass representing event registration - incoming events (messages) from * vpp as a result of a subscription (typically a want_* simple request) */ template class Event_registration : public Common_req { public: Event_registration ( Connection &con, std::function &)> callback = nullptr) : Common_req{con}, result_set{con}, callback{callback} { if (!con.is_msg_available (M::get_msg_id ())) { throw Msg_not_available_exception (); } con.register_event (this); } Event_registration (const Event_registration &) = delete; virtual ~Event_registration () { con.unregister_event (this); } virtual std::tuple assign_response (vapi_msg_id_t id, void *shm_data) { result_set.assign_response (id, shm_data); if (nullptr != callback) { return std::make_pair (callback (*this), true); } return std::make_pair (VAPI_OK, true); } using resp_type = typename M::shm_data_type; Result_set &get_result_set (void) { return result_set; } private: Result_set result_set; std::function &)> callback; }; }; #endif /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */