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.
25 #include <vapi/vapi.hpp>
26 #include <vapi/vpe.api.vapi.hpp>
28 #include "vom/connection.hpp"
29 #include "vom/rpc_cmd.hpp"
36 * A HW::item is data that is either to be written to or read from
38 * The item is a pair of the data written/read and the result of that
58 , item_rc(rc_t::UNSET)
74 item(const T& data, rc_t rc)
83 bool operator==(const item<T>& i) const
85 return (item_data == i.item_data);
91 item& operator=(const item& other)
93 item_data = other.item_data;
94 item_rc = other.item_rc;
100 * Return the data read/written
102 T& data() { return (item_data); }
105 * Const reference to the data
107 const T& data() const { return (item_data); }
110 * Get the HW return code
112 rc_t rc() const { return (item_rc); }
115 * Set the HW return code - should only be called from the
116 * family of Command objects
118 void set(const rc_t& rc) { item_rc = rc; }
121 * Return true if the HW item is configred in HW
123 operator bool() const { return (rc_t::OK == item_rc); }
126 * update the item to the desired state.
127 * return true if a HW update is required
129 bool update(const item& desired)
131 bool need_hw_update = false;
134 * if the deisred set is unset (i.e. defaulted, we've
137 if (rc_t::UNSET == desired.rc()) {
141 * A HW update is needed if thestate is different
142 * or the state is not yet in HW
144 need_hw_update = (item_data != desired.data() || rc_t::OK != rc());
146 item_data = desired.data();
148 return (need_hw_update);
152 * convert to string format for debug purposes
154 std::string to_string() const
156 std::ostringstream os;
159 << "rc:" << item_rc.to_string() << " data:" << item_data.to_string()
172 * The result when the item was written
178 * The pipe to VPP into which we write the commands
193 * Copy assignement - only used in UT
195 cmd_q& operator=(const cmd_q& f);
198 * Enqueue a command into the Q.
200 virtual void enqueue(cmd* c);
202 * Enqueue a command into the Q.
204 virtual void enqueue(std::shared_ptr<cmd> c);
207 * Enqueue a set of commands
209 virtual void enqueue(std::queue<cmd*>& c);
212 * dequeue a command from the Q.
214 virtual void dequeue(cmd* c);
217 * dequeue a command from the Q.
219 virtual void dequeue(std::shared_ptr<cmd> c);
222 * Write all the commands to HW
224 virtual rc_t write();
227 * Blocking Connect to VPP - call once at bootup
232 * Disable the passing of commands to VPP. Whilst disabled all
233 * writes will be discarded. Use this during the reset phase.
238 * Enable the passing of commands to VPP - undoes the disable.
239 * The Q is enabled by default.
245 * A queue of enqueued commands, ready to be written
247 std::deque<std::shared_ptr<cmd>> m_queue;
250 * A map of issued, but uncompleted, commands.
251 * i.e. those that we are waiting, async stylee,
252 * for VPP to complete
254 std::map<cmd*, std::shared_ptr<cmd>> m_pending;
257 * VPP Q poll function
262 * The thread object running the poll/dispatch/connect thread
264 std::unique_ptr<std::thread> m_rx_thread;
267 * A flag indicating the client has disabled the cmd Q.
272 * A flag for the thread to poll to see if the queue is still alive
277 * The connection to VPP
283 * Initialise the HW connection to VPP - the UT version passing
286 static void init(cmd_q* f);
294 * Enqueue A command for execution
296 static void enqueue(cmd* f);
299 * Enqueue A command for execution
301 static void enqueue(std::shared_ptr<cmd> c);
304 * Enqueue A set of commands for execution
306 static void enqueue(std::queue<cmd*>& c);
309 * dequeue A command for execution
311 static void dequeue(cmd* f);
314 * dequeue A command for execution
316 static void dequeue(std::shared_ptr<cmd> c);
319 * Write/Execute all commands hitherto enqueued.
324 * Blocking Connect to VPP
326 static void connect();
329 * Blocking pool of the HW connection
335 * The command Q toward HW
337 static cmd_q* m_cmdQ;
340 * HW::item representing the connection state as determined by polling
342 static HW::item<bool> m_poll_state;
345 * Disable the passing of commands to VPP. Whilst disabled all writes
346 * will be discarded. Use this during the reset phase.
348 static void disable();
351 * Enable the passing of commands to VPP - undoes the disable.
352 * The Q is enabled by default.
354 static void enable();
357 * Only the OM can enable/disable HW
362 * A command pool the HW for liveness
364 class Poll : public rpc_cmd<HW::item<bool>, rc_t, vapi::Control_ping>
368 * Constructor taking the HW::item to update
370 Poll(HW::item<bool>& item);
373 * Issue the command to VPP/HW
375 rc_t issue(connection& con);
378 * convert to string format for debug purposes
380 std::string to_string() const;
383 * Comparison operator - only used for UT
385 bool operator==(const Poll& i) const;
390 * bool Specialisation for HW::item to_string
393 std::string HW::item<bool>::to_string() const;
396 * uint Specialisation for HW::item to_string
399 std::string HW::item<unsigned int>::to_string() const;
403 * fd.io coding-style-patch-verification: ON
406 * eval: (c-set-style "mozilla")