2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #ifndef __VOM_RPC_CMD_H__
17 #define __VOM_RPC_CMD_H__
21 #include <vapi/vapi.hpp>
23 #include "vom/cmd.hpp"
24 #include "vom/logger.hpp"
28 * A base class for all RPC commands to VPP.
29 * RPC commands are one of the sub-set of command types to VPP
30 * that modify/create state in VPP and thus return an error code.
31 * Commands are issued in one thread context, but read in another. The
32 * command has an associated std::promise that is met by the RX thread.
33 * this allows the sender, which waits on the promise's future, to
34 * experience a synchronous command.
36 * The command is templatised on the type of the HW::item to be set by
37 * the command, and the data returned in the promise,
39 template <typename HWITEM, typename MSG>
40 class rpc_cmd : public cmd
49 * Constructor taking the HW item that will be updated by the command
64 * return the HW item the command updates
66 HWITEM& item() { return m_hw_item; }
69 * return the const HW item the command updates
71 const HWITEM& item() const { return m_hw_item; }
74 * Fulfill the commands promise. Called from the RX thread
76 void fulfill(const HWITEM& d) { m_promise.set_value(d); }
79 * Wait on the commands promise. i.e. block on the completion
84 std::future_status status;
85 std::future<HWITEM> result;
87 result = m_promise.get_future();
88 status = result.wait_for(std::chrono::seconds(5));
90 if (status != std::future_status::ready) {
91 m_hw_item.set(rc_t::TIMEOUT);
93 m_hw_item = result.get();
96 return (m_hw_item.rc());
100 * Called by the HW Command Q when it is disabled to indicate the
101 * command can be considered successful without issuing it to HW
103 virtual void succeeded()
105 m_hw_item.set(rc_t::OK);
106 VOM_LOG(log_level_t::DEBUG) << to_string();
110 * call operator used as a callback by VAPI when the reply is available
112 virtual vapi_error_e operator()(MSG& reply)
114 HWITEM hi = m_hw_item;
115 int retval = reply.get_response().get_payload().retval;
116 VOM_LOG(log_level_t::DEBUG) << to_string() << " " << retval;
118 /* set a temporary value in this callback thread */
119 hi.set(rc_t::from_vpp_retval(retval));
126 * Retire/cancel a long running command
128 virtual void retire(connection& con) {}
132 * A reference to an object's HW::item that the command will update
137 * The promise that implements the synchronous issue
139 std::promise<HWITEM> m_promise;
144 * fd.io coding-style-patch-verification: ON
147 * eval: (c-set-style "mozilla")