blob: 8f64a75c092d60409e3c1628af643d3f061efb3b [file] [log] [blame]
Neale Ranns812ed392017-10-16 04:20:13 -07001/*
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#include "vom/hw.hpp"
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070017#include "vom/hw_cmds.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070018#include "vom/logger.hpp"
19
Neale Ranns812ed392017-10-16 04:20:13 -070020namespace VOM {
21HW::cmd_q::cmd_q()
22 : m_enabled(true)
23 , m_connected(false)
24 , m_conn()
25{
26}
27
28HW::cmd_q::~cmd_q()
29{
Neale Ranns812ed392017-10-16 04:20:13 -070030}
31
32HW::cmd_q&
33HW::cmd_q::operator=(const HW::cmd_q& f)
34{
35 return (*this);
36}
37
38/**
39 * Run the connect/dispatch thread.
40 */
41void
42HW::cmd_q::rx_run()
43{
44 while (m_connected) {
45 m_conn.ctx().dispatch();
46 }
47}
48
49void
50HW::cmd_q::enqueue(cmd* c)
51{
52 std::shared_ptr<cmd> sp(c);
53
54 m_queue.push_back(sp);
55}
56
57void
58HW::cmd_q::enqueue(std::shared_ptr<cmd> c)
59{
60 m_queue.push_back(c);
61}
62
63void
64HW::cmd_q::enqueue(std::queue<cmd*>& cmds)
65{
66 while (cmds.size()) {
67 std::shared_ptr<cmd> sp(cmds.front());
68
69 m_queue.push_back(sp);
70 cmds.pop();
71 }
72}
73
Mohsin Kazmi18e955e2018-01-08 12:58:32 +010074bool
Neale Ranns812ed392017-10-16 04:20:13 -070075HW::cmd_q::connect()
76{
Mohsin Kazmi18e955e2018-01-08 12:58:32 +010077 int rv;
78
79 if (m_connected)
80 return m_connected;
81
82 rv = m_conn.connect();
83
84 m_connected = true;
85 m_rx_thread.reset(new std::thread(&HW::cmd_q::rx_run, this));
86 return (rv == 0);
87}
88
89void
90HW::cmd_q::disconnect()
91{
92
93 if (!m_connected)
94 return;
Neale Ranns812ed392017-10-16 04:20:13 -070095
96 m_connected = false;
97
98 if (m_rx_thread && m_rx_thread->joinable()) {
99 m_rx_thread->join();
100 }
101
Mohsin Kazmi18e955e2018-01-08 12:58:32 +0100102 m_conn.disconnect();
Neale Ranns812ed392017-10-16 04:20:13 -0700103}
104
105void
106HW::cmd_q::enable()
107{
108 m_enabled = true;
109}
110
111void
112HW::cmd_q::disable()
113{
114 m_enabled = false;
115}
116
117rc_t
118HW::cmd_q::write()
119{
120 rc_t rc = rc_t::OK;
121
122 /*
Neale Ranns8ac4ce82017-11-17 05:08:55 -0800123 * The queue is enabled, Execute each command in the queue.
124 * If one execution fails, abort the rest
125 */
Neale Ranns812ed392017-10-16 04:20:13 -0700126 auto it = m_queue.begin();
127
128 while (it != m_queue.end()) {
129 std::shared_ptr<cmd> c = *it;
130
131 VOM_LOG(log_level_t::DEBUG) << *c;
132
133 if (m_enabled) {
134 /*
Neale Ranns8ac4ce82017-11-17 05:08:55 -0800135 * before we issue the command we must move it to the pending
136 * store
137 * ince a async event can be recieved before the command
138 * completes
139 */
Neale Ranns812ed392017-10-16 04:20:13 -0700140 rc = c->issue(m_conn);
141
Neale Rannsa2ee0292017-11-28 22:29:13 -0800142 if (rc_t::OK == rc) {
Neale Ranns812ed392017-10-16 04:20:13 -0700143 /*
Neale Rannsa2ee0292017-11-28 22:29:13 -0800144 * move to the next
Neale Ranns8ac4ce82017-11-17 05:08:55 -0800145 */
Neale Ranns812ed392017-10-16 04:20:13 -0700146 } else {
147 /*
Neale Rannsa2ee0292017-11-28 22:29:13 -0800148 * barf out without issuing the rest
Neale Ranns8ac4ce82017-11-17 05:08:55 -0800149 */
Neale Rannsa2ee0292017-11-28 22:29:13 -0800150 VOM_LOG(log_level_t::ERROR) << "Failed to execute: " << c->to_string();
151 break;
Neale Ranns812ed392017-10-16 04:20:13 -0700152 }
153 } else {
154 /*
Neale Ranns8ac4ce82017-11-17 05:08:55 -0800155 * The HW is disabled, so set each command as succeeded
156 */
Neale Ranns812ed392017-10-16 04:20:13 -0700157 c->succeeded();
158 }
159
160 ++it;
161 }
162
163 /*
Neale Ranns8ac4ce82017-11-17 05:08:55 -0800164 * erase all objects in the queue
165 */
Neale Ranns812ed392017-10-16 04:20:13 -0700166 m_queue.erase(m_queue.begin(), m_queue.end());
167
168 return (rc);
169}
170
171/*
172 * The single Command Queue
173 */
174HW::cmd_q* HW::m_cmdQ;
175HW::item<bool> HW::m_poll_state;
176
177/**
178 * Initialse the connection to VPP
179 */
180void
181HW::init(HW::cmd_q* f)
182{
183 m_cmdQ = f;
184}
185
186/**
187 * Initialse the connection to VPP
188 */
189void
190HW::init()
191{
192 m_cmdQ = new cmd_q();
193}
194
195void
196HW::enqueue(cmd* cmd)
197{
198 m_cmdQ->enqueue(cmd);
199}
200
201void
202HW::enqueue(std::shared_ptr<cmd> cmd)
203{
204 m_cmdQ->enqueue(cmd);
205}
206
207void
208HW::enqueue(std::queue<cmd*>& cmds)
209{
210 m_cmdQ->enqueue(cmds);
211}
212
Mohsin Kazmi18e955e2018-01-08 12:58:32 +0100213bool
Neale Ranns812ed392017-10-16 04:20:13 -0700214HW::connect()
215{
Mohsin Kazmi18e955e2018-01-08 12:58:32 +0100216 return m_cmdQ->connect();
217}
218
219void
220HW::disconnect()
221{
222 m_cmdQ->disconnect();
Neale Ranns812ed392017-10-16 04:20:13 -0700223}
224
225void
226HW::enable()
227{
228 m_cmdQ->enable();
229}
230
231void
232HW::disable()
233{
234 m_cmdQ->disable();
235}
236
237rc_t
238HW::write()
239{
240 return (m_cmdQ->write());
241}
242
243bool
244HW::poll()
245{
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700246 std::shared_ptr<cmd> poll(new hw_cmds::poll(m_poll_state));
Neale Ranns812ed392017-10-16 04:20:13 -0700247
248 HW::enqueue(poll);
249 HW::write();
250
251 return (m_poll_state);
252}
253
254template <>
255std::string
256HW::item<bool>::to_string() const
257{
258 std::ostringstream os;
259
260 os << "hw-item:["
261 << "rc:" << item_rc.to_string() << " data:" << item_data << "]";
262 return (os.str());
263}
264
265template <>
266std::string
267HW::item<unsigned int>::to_string() const
268{
269 std::ostringstream os;
270
271 os << "hw-item:["
272 << "rc:" << item_rc.to_string() << " data:" << item_data << "]";
273 return (os.str());
274}
Neale Ranns812ed392017-10-16 04:20:13 -0700275}
276
277/*
278 * fd.io coding-style-patch-verification: ON
279 *
280 * Local Variables:
281 * eval: (c-set-style "mozilla")
282 * End:
283 */