FD.io VPP  v18.04-17-g3a0d853
Vector Packet Processing
rpc_cmd.hpp
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 
16 #ifndef __VOM_RPC_CMD_H__
17 #define __VOM_RPC_CMD_H__
18 
19 #include <future>
20 
21 #include "vom/cmd.hpp"
22 #include "vom/logger.hpp"
23 
24 namespace VOM {
25 /**
26  * A base class for all RPC commands to VPP.
27  * RPC commands are one of the sub-set of command types to VPP
28  * that modify/create state in VPP and thus return an error code.
29  * Commands are issued in one thread context, but read in another. The
30  * command has an associated std::promise that is met by the RX thread.
31  * this allows the sender, which waits on the promise's future, to
32  * experience a synchronous command.
33  *
34  * The command is templatised on the type of the HW::item to be set by
35  * the command, and the data returned in the promise,
36  */
37 template <typename HWITEM, typename DATA, typename MSG>
38 class rpc_cmd : public cmd
39 {
40 public:
41  /**
42  * convenient typedef
43  */
44  typedef MSG msg_t;
45 
46  /**
47  * Constructor taking the HW item that will be updated by the command
48  */
49  rpc_cmd(HWITEM& item)
50  : cmd()
51  , m_hw_item(item)
52  , m_promise()
53  {
54  }
55 
56  /**
57  * Desructor
58  */
59  virtual ~rpc_cmd() {}
60 
61  /**
62  * return the HW item the command updates
63  */
64  HWITEM& item() { return m_hw_item; }
65 
66  /**
67  * return the const HW item the command updates
68  */
69  const HWITEM& item() const { return m_hw_item; }
70 
71  /**
72  * Fulfill the commands promise. Called from the RX thread
73  */
74  void fulfill(const DATA& d)
75  {
76  m_promise.set_value(d);
77 
78  /*
79  * we reset the promise after setting the value to reuse it
80  * when we run the retire command from the same cmd object
81  */
82  // m_promise = std::promise<DATA>();
83  }
84 
85  /**
86  * Wait on the commands promise. i.e. block on the completion
87  * of the command.
88  */
90  {
91  std::future_status status;
92  std::future<DATA> result;
93 
94  result = m_promise.get_future();
95  status = result.wait_for(std::chrono::seconds(5));
96 
97  if (status != std::future_status::ready) {
98  return (DATA(rc_t::TIMEOUT));
99  }
100 
101  return (result.get());
102  }
103 
104  /**
105  * Called by the HW Command Q when it is disabled to indicate the
106  * command can be considered successful without issuing it to HW
107  */
108  virtual void succeeded()
109  {
110  m_hw_item.set(rc_t::OK);
112  }
113 
114  /**
115  * call operator used as a callback by VAPI when the reply is available
116  */
117  virtual vapi_error_e operator()(MSG& reply)
118  {
119  int retval = reply.get_response().get_payload().retval;
120  VOM_LOG(log_level_t::DEBUG) << to_string() << " " << retval;
122 
123  return (VAPI_OK);
124  }
125 
126  /**
127  * Retire/cancel a long running command
128  */
129  virtual void retire(connection& con) {}
130 
131 protected:
132  /**
133  * A reference to an object's HW::item that the command will update
134  */
135  HWITEM& m_hw_item;
136 
137  /**
138  * The promise that implements the synchronous issue
139  */
140  std::promise<DATA> m_promise;
141 };
142 };
143 
144 /*
145  * fd.io coding-style-patch-verification: ON
146  *
147  * Local Variables:
148  * eval: (c-set-style "mozilla")
149  * End:
150  */
151 
152 #endif
HWITEM & m_hw_item
A reference to an object&#39;s HW::item that the command will update.
Definition: rpc_cmd.hpp:135
virtual std::string to_string() const =0
convert to string format for debug purposes
static const rc_t & from_vpp_retval(int32_t rv)
Get the rc_t from the VPP API value.
Definition: types.cpp:36
#define VOM_LOG(lvl)
Definition: logger.hpp:181
std::promise< DATA > m_promise
The promise that implements the synchronous issue.
Definition: rpc_cmd.hpp:140
static const log_level_t DEBUG
Definition: logger.hpp:32
virtual void succeeded()
Called by the HW Command Q when it is disabled to indicate the command can be considered successful w...
Definition: rpc_cmd.hpp:108
virtual void retire(connection &con)
Retire/cancel a long running command.
Definition: rpc_cmd.hpp:129
const HWITEM & item() const
return the const HW item the command updates
Definition: rpc_cmd.hpp:69
A representation of the connection to VPP.
Definition: connection.hpp:33
rpc_cmd(HWITEM &item)
Constructor taking the HW item that will be updated by the command.
Definition: rpc_cmd.hpp:49
virtual vapi_error_e operator()(MSG &reply)
call operator used as a callback by VAPI when the reply is available
Definition: rpc_cmd.hpp:117
HWITEM & item()
return the HW item the command updates
Definition: rpc_cmd.hpp:64
virtual ~rpc_cmd()
Desructor.
Definition: rpc_cmd.hpp:59
A base class for all RPC commands to VPP.
Definition: rpc_cmd.hpp:38
static const rc_t OK
The HW write was successfull.
Definition: types.hpp:112
success
Definition: vapi_common.h:27
void fulfill(const DATA &d)
Fulfill the commands promise.
Definition: rpc_cmd.hpp:74
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
A representation of a method call to VPP.
Definition: cmd.hpp:32
MSG msg_t
convenient typedef
Definition: rpc_cmd.hpp:44
DATA wait()
Wait on the commands promise.
Definition: rpc_cmd.hpp:89
static const rc_t TIMEOUT
HW write timedout - VPP did not respond within a timely manner.
Definition: types.hpp:122
vapi_error_e
Definition: vapi_common.h:25