FD.io VPP  v18.07.1-19-g511ce25
Vector Packet Processing
hw.cpp
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 #include "vom/hw.hpp"
17 #include "vom/hw_cmds.hpp"
18 #include "vom/logger.hpp"
19 
20 namespace VOM {
22  : m_enabled(true)
23  , m_connected(false)
24  , m_conn()
25 {
26 }
27 
29 {
30 }
31 
32 HW::cmd_q&
34 {
35  return (*this);
36 }
37 
38 /**
39  * Run the connect/dispatch thread.
40  */
41 void
42 HW::cmd_q::rx_run()
43 {
44  while (m_connected) {
45  m_conn.ctx().dispatch();
46  }
47 }
48 
49 void
51 {
52  std::shared_ptr<cmd> sp(c);
53 
54  m_queue.push_back(sp);
55 }
56 
57 void
58 HW::cmd_q::enqueue(std::shared_ptr<cmd> c)
59 {
60  m_queue.push_back(c);
61 }
62 
63 void
64 HW::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 
74 bool
76 {
77  if (m_connected)
78  return m_connected;
79 
80  if (0 == m_conn.connect()) {
81  m_connected = true;
82  m_rx_thread.reset(new std::thread(&HW::cmd_q::rx_run, this));
83  }
84  return (m_connected);
85 }
86 
87 void
89 {
90 
91  if (!m_connected)
92  return;
93 
94  m_connected = false;
95 
96  if (m_rx_thread && m_rx_thread->joinable()) {
97  m_rx_thread->join();
98  }
99 
100  m_conn.disconnect();
101 }
102 
103 void
105 {
106  m_enabled = true;
107 }
108 
109 void
111 {
112  m_enabled = false;
113 }
114 
115 rc_t
117 {
118  rc_t rc = rc_t::OK;
119 
120  /*
121  * The queue is enabled, Execute each command in the queue.
122  * If one execution fails, abort the rest
123  */
124  auto it = m_queue.begin();
125 
126  while (it != m_queue.end()) {
127  std::shared_ptr<cmd> c = *it;
128 
130 
131  if (m_enabled) {
132  /*
133  * before we issue the command we must move it to the pending
134  * store
135  * ince a async event can be recieved before the command
136  * completes
137  */
138  rc = c->issue(m_conn);
139 
140  if (rc_t::OK == rc) {
141  /*
142  * move to the next
143  */
144  } else {
145  /*
146  * barf out without issuing the rest
147  */
148  VOM_LOG(log_level_t::ERROR) << "Failed to execute: " << c->to_string();
149  break;
150  }
151  } else {
152  /*
153  * The HW is disabled, so set each command as succeeded
154  */
155  c->succeeded();
156  }
157 
158  ++it;
159  }
160 
161  /*
162  * erase all objects in the queue
163  */
164  m_queue.erase(m_queue.begin(), m_queue.end());
165 
166  return (rc);
167 }
168 
169 /*
170  * The single Command Queue
171  */
172 HW::cmd_q* HW::m_cmdQ;
173 HW::item<bool> HW::m_poll_state;
174 
175 /**
176  * Initialse the connection to VPP
177  */
178 void
180 {
181  m_cmdQ = f;
182 }
183 
184 /**
185  * Initialse the connection to VPP
186  */
187 void
189 {
190  m_cmdQ = new cmd_q();
191 }
192 
193 void
195 {
196  m_cmdQ->enqueue(cmd);
197 }
198 
199 void
200 HW::enqueue(std::shared_ptr<cmd> cmd)
201 {
202  m_cmdQ->enqueue(cmd);
203 }
204 
205 void
206 HW::enqueue(std::queue<cmd*>& cmds)
207 {
208  m_cmdQ->enqueue(cmds);
209 }
210 
211 bool
213 {
214  return m_cmdQ->connect();
215 }
216 
217 void
219 {
220  m_cmdQ->disconnect();
221 }
222 
223 void
224 HW::enable()
225 {
226  m_cmdQ->enable();
227 }
228 
229 void
230 HW::disable()
231 {
232  m_cmdQ->disable();
233 }
234 
235 rc_t
237 {
238  return (m_cmdQ->write());
239 }
240 
241 bool
243 {
244  std::shared_ptr<cmd> poll(new hw_cmds::poll(m_poll_state));
245 
246  HW::enqueue(poll);
247  HW::write();
248 
249  return (m_poll_state);
250 }
251 
252 template <>
253 std::string
255 {
256  std::ostringstream os;
257 
258  os << "hw-item:["
259  << "rc:" << item_rc.to_string() << " data:" << item_data << "]";
260  return (os.str());
261 }
262 
263 template <>
264 std::string
266 {
267  std::ostringstream os;
268 
269  os << "hw-item:["
270  << "rc:" << item_rc.to_string() << " data:" << item_data << "]";
271  return (os.str());
272 }
273 }
274 
275 /*
276  * fd.io coding-style-patch-verification: ON
277  *
278  * Local Variables:
279  * eval: (c-set-style "mozilla")
280  * End:
281  */
#define VOM_LOG(lvl)
Definition: logger.hpp:181
cmd_q()
Constructor.
Definition: hw.cpp:21
static rc_t write()
Write/Execute all commands hitherto enqueued.
Definition: hw.cpp:236
Error codes that VPP will return during a HW write.
Definition: types.hpp:84
static const log_level_t DEBUG
Definition: logger.hpp:32
A command poll the HW for liveness.
Definition: hw_cmds.hpp:30
pthread_t thread[MAX_CONNS]
Definition: main.c:125
std::string to_string() const
convert to string format for debug purposes
Definition: hw.hpp:160
virtual bool connect()
Blocking Connect to VPP - call once at bootup.
Definition: hw.cpp:75
static bool connect()
Blocking Connect to VPP.
Definition: hw.cpp:212
virtual rc_t write()
Write all the commands to HW.
Definition: hw.cpp:116
void enable()
Enable the passing of commands to VPP - undoes the disable.
Definition: hw.cpp:104
The pipe to VPP into which we write the commands.
Definition: hw.hpp:186
cmd_q & operator=(const cmd_q &f)
Copy assignement - only used in UT.
Definition: hw.cpp:33
virtual void disconnect()
Disconnect to VPP.
Definition: hw.cpp:88
svmdb_client_t * c
vapi_error_e dispatch(const Common_req *limit=nullptr, u32 time=5)
wait for responses from vpp and assign them to appropriate objects
Definition: vapi.hpp:250
static void init()
Initialise the HW.
Definition: hw.cpp:188
static const rc_t OK
The HW write was successfull.
Definition: types.hpp:104
static void enqueue(cmd *f)
Enqueue A command for execution.
Definition: hw.cpp:194
static void disconnect()
Disconnect to VPP.
Definition: hw.cpp:218
void disconnect()
Blocking disconnect.
Definition: connection.cpp:32
static const log_level_t ERROR
Definition: logger.hpp:29
vapi::Connection & ctx()
Retrun the VAPI context the commands will use.
Definition: connection.cpp:49
~cmd_q()
Destructor.
Definition: hw.cpp:28
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
A representation of a method call to VPP.
Definition: cmd.hpp:32
void disable()
Disable the passing of commands to VPP.
Definition: hw.cpp:110
int connect()
Blocking [re]connect call - always eventually succeeds, or the universe expires.
Definition: connection.cpp:38
virtual void enqueue(cmd *c)
Enqueue a command into the Q.
Definition: hw.cpp:50
static bool poll()
Blocking pool of the HW connection.
Definition: hw.cpp:242