FD.io VPP  v19.01.2-3-gf61a1a8
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 <vapi/vapi.hpp>
22 
23 #include "vom/cmd.hpp"
24 #include "vom/logger.hpp"
25 
26 namespace VOM {
27 /**
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.
35  *
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,
38  */
39 template <typename HWITEM, typename MSG>
40 class rpc_cmd : public cmd
41 {
42 public:
43  /**
44  * convenient typedef
45  */
46  typedef MSG msg_t;
47 
48  /**
49  * Constructor taking the HW item that will be updated by the command
50  */
51  rpc_cmd(HWITEM& item)
52  : cmd()
53  , m_hw_item(item)
54  , m_promise()
55  {
56  }
57 
58  /**
59  * Desructor
60  */
61  virtual ~rpc_cmd() {}
62 
63  /**
64  * return the HW item the command updates
65  */
66  HWITEM& item() { return m_hw_item; }
67 
68  /**
69  * return the const HW item the command updates
70  */
71  const HWITEM& item() const { return m_hw_item; }
72 
73  /**
74  * Fulfill the commands promise. Called from the RX thread
75  */
76  void fulfill(const HWITEM& d) { m_promise.set_value(d); }
77 
78  /**
79  * Wait on the commands promise. i.e. block on the completion
80  * of the command.
81  */
83  {
84  std::future_status status;
85  std::future<HWITEM> result;
86 
87  result = m_promise.get_future();
88  status = result.wait_for(std::chrono::seconds(5));
89 
90  if (status != std::future_status::ready) {
92  } else {
93  m_hw_item = result.get();
94  }
95 
96  return (m_hw_item.rc());
97  }
98 
99  /**
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
102  */
103  virtual void succeeded()
104  {
105  m_hw_item.set(rc_t::OK);
107  }
108 
109  /**
110  * call operator used as a callback by VAPI when the reply is available
111  */
112  virtual vapi_error_e operator()(MSG& reply)
113  {
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;
117 
118  /* set a temporary value in this callback thread */
119  hi.set(rc_t::from_vpp_retval(retval));
120  fulfill(hi);
121 
122  return (VAPI_OK);
123  }
124 
125  /**
126  * Retire/cancel a long running command
127  */
128  virtual void retire(connection& con) {}
129 
130 protected:
131  /**
132  * A reference to an object's HW::item that the command will update
133  */
134  HWITEM& m_hw_item;
135 
136  /**
137  * The promise that implements the synchronous issue
138  */
139  std::promise<HWITEM> m_promise;
140 };
141 };
142 
143 /*
144  * fd.io coding-style-patch-verification: ON
145  *
146  * Local Variables:
147  * eval: (c-set-style "mozilla")
148  * End:
149  */
150 
151 #endif
vmrglw vmrglh hi
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:33
#define VOM_LOG(lvl)
Definition: logger.hpp:181
Error codes that VPP will return during a HW write.
Definition: types.hpp:89
static const log_level_t DEBUG
Definition: logger.hpp:32
virtual void retire(connection &con)
Retire/cancel a long running command.
Definition: rpc_cmd.hpp:128
C++ VPP API.
rc_t wait()
Wait on the commands promise.
Definition: rpc_cmd.hpp:82
virtual ~rpc_cmd()
Desructor.
Definition: rpc_cmd.hpp:61
A representation of the connection to VPP.
Definition: connection.hpp:33
A base class for all RPC commands to VPP.
Definition: rpc_cmd.hpp:40
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:103
rpc_cmd(HWITEM &item)
Constructor taking the HW item that will be updated by the command.
Definition: rpc_cmd.hpp:51
void fulfill(const HWITEM &d)
Fulfill the commands promise.
Definition: rpc_cmd.hpp:76
const HWITEM & item() const
return the const HW item the command updates
Definition: rpc_cmd.hpp:71
static const rc_t OK
The HW write was successfull.
Definition: types.hpp:109
success
Definition: vapi_common.h:27
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
A representation of a method call to VPP.
Definition: cmd.hpp:32
virtual vapi_error_e operator()(MSG &reply)
call operator used as a callback by VAPI when the reply is available
Definition: rpc_cmd.hpp:112
std::promise< HWITEM > m_promise
The promise that implements the synchronous issue.
Definition: rpc_cmd.hpp:139
HWITEM & m_hw_item
A reference to an object&#39;s HW::item that the command will update.
Definition: rpc_cmd.hpp:134
static const rc_t TIMEOUT
HW write timedout - VPP did not respond within a timely manner.
Definition: types.hpp:119
HWITEM & item()
return the HW item the command updates
Definition: rpc_cmd.hpp:66
vapi_error_e
Definition: vapi_common.h:25
MSG msg_t
convenient typedef
Definition: rpc_cmd.hpp:46