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