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
57 : item_rc(rc_t::UNSET)
72 item(const T& data, rc_t rc)
81 bool operator==(const item<T>& i) const
83 return (item_data == i.item_data);
89 item& operator=(const item& other)
91 item_data = other.item_data;
92 item_rc = other.item_rc;
98 * Return the data read/written
100 T& data() { return (item_data); }
103 * Const reference to the data
105 const T& data() const { return (item_data); }
108 * Get the HW return code
110 rc_t rc() const { return (item_rc); }
113 * Set the HW return code - should only be called from the
114 * family of Command objects
116 void set(const rc_t& rc) { item_rc = rc; }
119 * Return true if the HW item is configred in HW
121 operator bool() const { return (rc_t::OK == item_rc); }
124 * update the item to the desired state.
125 * return true if a HW update is required
127 bool update(const item& desired)
129 bool need_hw_update = false;
132 * if the deisred set is unset (i.e. defaulted, we've
135 if (rc_t::UNSET == desired.rc()) {
139 * A HW update is needed if thestate is different
140 * or the state is not yet in HW
142 need_hw_update = (item_data != desired.data() || rc_t::OK != rc());
144 item_data = desired.data();
146 return (need_hw_update);
150 * convert to string format for debug purposes
152 std::string to_string() const
154 std::ostringstream os;
157 << "rc:" << item_rc.to_string() << " data:" << item_data.to_string()
170 * The result when the item was written
176 * The pipe to VPP into which we write the commands
191 * Copy assignement - only used in UT
193 cmd_q& operator=(const cmd_q& f);
196 * Enqueue a command into the Q.
198 virtual void enqueue(cmd* c);
200 * Enqueue a command into the Q.
202 virtual void enqueue(std::shared_ptr<cmd> c);
205 * Enqueue a set of commands
207 virtual void enqueue(std::queue<cmd*>& c);
210 * dequeue a command from the Q.
212 virtual void dequeue(cmd* c);
215 * dequeue a command from the Q.
217 virtual void dequeue(std::shared_ptr<cmd> c);
220 * Write all the commands to HW
222 virtual rc_t write();
225 * Blocking Connect to VPP - call once at bootup
230 * Disable the passing of commands to VPP. Whilst disabled all
231 * writes will be discarded. Use this during the reset phase.
236 * Enable the passing of commands to VPP - undoes the disable.
237 * The Q is enabled by default.
243 * A queue of enqueued commands, ready to be written
245 std::deque<std::shared_ptr<cmd>> m_queue;
248 * A map of issued, but uncompleted, commands.
249 * i.e. those that we are waiting, async stylee,
250 * for VPP to complete
252 std::map<cmd*, std::shared_ptr<cmd>> m_pending;
255 * VPP Q poll function
260 * The thread object running the poll/dispatch/connect thread
262 std::unique_ptr<std::thread> m_rx_thread;
265 * A flag indicating the client has disabled the cmd Q.
270 * A flag for the thread to poll to see if the queue is still alive
275 * The connection to VPP
281 * Initialise the HW connection to VPP - the UT version passing
284 static void init(cmd_q* f);
292 * Enqueue A command for execution
294 static void enqueue(cmd* f);
297 * Enqueue A command for execution
299 static void enqueue(std::shared_ptr<cmd> c);
302 * Enqueue A set of commands for execution
304 static void enqueue(std::queue<cmd*>& c);
307 * dequeue A command for execution
309 static void dequeue(cmd* f);
312 * dequeue A command for execution
314 static void dequeue(std::shared_ptr<cmd> c);
317 * Write/Execute all commands hitherto enqueued.
322 * Blocking Connect to VPP
324 static void connect();
327 * Blocking pool of the HW connection
333 * The command Q toward HW
335 static cmd_q* m_cmdQ;
338 * HW::item representing the connection state as determined by polling
340 static HW::item<bool> m_poll_state;
343 * Disable the passing of commands to VPP. Whilst disabled all writes
344 * will be discarded. Use this during the reset phase.
346 static void disable();
349 * Enable the passing of commands to VPP - undoes the disable.
350 * The Q is enabled by default.
352 static void enable();
355 * Only the OM can enable/disable HW
360 * A command pool the HW for liveness
362 class Poll : public rpc_cmd<HW::item<bool>, rc_t, vapi::Control_ping>
366 * Constructor taking the HW::item to update
368 Poll(HW::item<bool>& item);
371 * Issue the command to VPP/HW
373 rc_t issue(connection& con);
376 * convert to string format for debug purposes
378 std::string to_string() const;
381 * Comparison operator - only used for UT
383 bool operator==(const Poll& i) const;
388 * bool Specialisation for HW::item to_string
391 std::string HW::item<bool>::to_string() const;
394 * uint Specialisation for HW::item to_string
397 std::string HW::item<unsigned int>::to_string() const;
401 * fd.io coding-style-patch-verification: ON
404 * eval: (c-set-style "mozilla")