aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-plugin/libvapi-safe/src/vapi_safe.cc
blob: 723a0fa64780b340bd9d203a47f0df53339d545e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
 * Copyright (c) 2023 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

extern "C"
{
#include <stdio.h>
#include <stdlib.h>
#include <vapi/vapi_safe.h>
}

#include <thread>
#include <random>
#include <asio.hpp>
#include <iostream>

namespace
{
class NonCopyable
{
protected:
  NonCopyable () = default;
  ~NonCopyable () = default;

  NonCopyable (const NonCopyable &) = delete;
  NonCopyable &operator= (const NonCopyable &) = delete;
};

template <typename T> class Singleton : NonCopyable
{
public:
  static T &
  getInstance ()
  {
    static T instance;
    return instance;
  }

protected:
  Singleton () {}
  ~Singleton () {}
};

template <typename T> class ThreadLocalSingleton : NonCopyable
{
public:
  static T &
  getInstance ()
  {
    static thread_local T instance;
    return instance;
  }

protected:
  ThreadLocalSingleton () {}
  ~ThreadLocalSingleton () {}
};

class EventThread
{
public:
  EventThread (asio::io_service &io_service, bool detached = false)
      : internal_io_service_ (nullptr), io_service_ (std::ref (io_service)),
	work_guard_ (asio::make_work_guard (io_service_.get ())),
	thread_ (nullptr), detached_ (detached)
  {
    run ();
  }

  explicit EventThread (bool detached = false)
      : internal_io_service_ (std::make_unique<asio::io_service> ()),
	io_service_ (std::ref (*internal_io_service_)),
	work_guard_ (asio::make_work_guard (io_service_.get ())),
	thread_ (nullptr), detached_ (detached)
  {
    run ();
  }

  EventThread (const EventThread &) = delete;
  EventThread &operator= (const EventThread &) = delete;

  EventThread (EventThread &&other) noexcept
      : internal_io_service_ (std::move (other.internal_io_service_)),
	io_service_ (std::move (other.io_service_)),
	work_guard_ (std::move (other.work_guard_)),
	thread_ (std::move (other.thread_)),
	detached_ (other.detached_)
  {
  }

  ~EventThread () { stop (); }

  void
  run ()
  {
    if (stopped ())
      {
	io_service_.get ().stopped ();
      }

    thread_ =
      std::make_unique<std::thread> ([this] () { io_service_.get ().run (); });

    if (detached_)
      {
	thread_->detach ();
      }
  }

  std::thread::id
  getThreadId () const
  {
    if (thread_)
      {
	return thread_->get_id ();
      }
    else
      {
	throw std::runtime_error ("Event thread is not running.");
      }
  }

  template <typename Func>
  void
  add (Func &&f)
  {
    io_service_.get ().post (std::forward<Func> (f));
  }

  template <typename Func>
  void
  tryRunHandlerNow (Func &&f)
  {
    io_service_.get ().dispatch (std::forward<Func> (f));
  }

  template <typename Func>
  void
  addAndWaitForExecution (Func &&f) const
  {
    auto promise = std::promise<void> ();
    auto future = promise.get_future ();

    asio::dispatch (io_service_.get (),
		    [&promise, f = std::forward<Func> (f)] () {
		      f ();
		      promise.set_value ();
		    });

    future.wait ();
  }

  void
  stop ()
  {
    add ([this] () { work_guard_.reset (); });

    if (thread_ && thread_->joinable ())
      {
	thread_->join ();
      }

    thread_.reset ();
  }

  bool
  stopped () const
  {
    return io_service_.get ().stopped ();
  }

  asio::io_service &
  getIoService ()
  {
    return io_service_;
  }

private:
  std::unique_ptr<asio::io_service> internal_io_service_;
  std::reference_wrapper<asio::io_service> io_service_;
  asio::executor_work_guard<asio::io_context::executor_type> work_guard_;
  std::unique_ptr<std::thread> thread_;
  bool detached_;
};

class UUID : public Singleton<UUID>
{
  friend class Singleton<UUID>;
  static inline unsigned char hex_chars[16] = { '0', '1', '2', '3', '4', '5',
						'6', '7', '8', '9', 'a', 'b',
						'c', 'd', 'e', 'f' };

public:
  static inline constexpr unsigned int UUID_LEN = 64;

  ~UUID () = default;
  std::string
  generate ()
  {
    return generate_hex (UUID_LEN);
  }

  std::string
  generate_hex (const unsigned int len)
  {
    std::string ret (len, 0);

    for (auto &c : ret)
      {
	c = random_char ();
      }

    return ret;
  }

private:
  UUID () : rd_ (), gen_ (rd_ ()), dis_ (0, sizeof (hex_chars) - 1) {}

  unsigned char
  random_char ()
  {
    return hex_chars[dis_ (gen_)];
  }

private:
  std::random_device rd_;
  std::mt19937 gen_;
  std::uniform_int_distribution<> dis_;
};

} // namespace

DEFINE_VAPI_MSG_IDS_HICN_API_JSON
DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON
DEFINE_VAPI_MSG_IDS_IP_API_JSON
DEFINE_VAPI_MSG_IDS_UDP_API_JSON
DEFINE_VAPI_MSG_IDS_MEMIF_API_JSON

class VapiGlobalConnection : public Singleton<VapiGlobalConnection>
{
  friend class Singleton<VapiGlobalConnection>;

  static inline char kapp_name[] = "hicn_app";
  static inline char kapi_prefix[] = "";
  static inline int kresponse_queue_size = 32;
  static inline int kmax_outstanding_requests = 32;
  static inline uint32_t ktimeout_seconds = 1;

public:
  vapi_error_e
  vapiConnectSafe (vapi_ctx_t *vapi_ctx_ret)
  {
    if (isConnected ())
      {
	*vapi_ctx_ret = vapi_ctx_;
	return VAPI_OK;
      }

    std::unique_lock<std::mutex> lock (vapi_mtx_);

    auto rv = vapi_ctx_alloc (&vapi_ctx_);
    if (rv != VAPI_OK)
      {
	return rv;
      }

    rv = vapi_connect (vapi_ctx_, app_name_.c_str (), nullptr,
		       max_outstanding_requests_, response_queue_size_,
		       VAPI_MODE_BLOCKING, 1);
    connected_ = true;

    vapi_set_generic_event_cb (vapi_ctx_, &VapiGlobalConnection::genericCb,
			       nullptr);

    if (rv == VAPI_OK)
      {
	// startDispatcher ();
	*vapi_ctx_ret = vapi_ctx_;
      }

    return rv;
  }

  void
  vapiLock ()
  {
    vapi_mtx_.lock ();
  }

  void
  vapiUnLock ()
  {
    vapi_mtx_.unlock ();
  }

  bool
  isConnected ()
  {
    return connected_;
  }

  ~VapiGlobalConnection ()
  {
    if (!isConnected ())
      {
	return;
      }
    std::unique_lock<std::mutex> lock (vapi_mtx_);
    vapi_disconnect (vapi_ctx_);
    vapi_ctx_free (vapi_ctx_);
    try
      {
	timer_.cancel ();
      }
    catch (asio::system_error &e)
      {
	// quit anyway
      }
  }

private:
  VapiGlobalConnection (
    const std::string &app_name = std::string (kapp_name) + "_" +
				  UUID::getInstance ().generate_hex (5),
    const std::string &api_prefix = kapi_prefix,
    int max_outstanding_requests = kmax_outstanding_requests,
    int response_queue_size = kresponse_queue_size)
      : app_name_ (app_name), api_prefix_ (api_prefix),
	max_outstanding_requests_ (max_outstanding_requests),
	response_queue_size_ (response_queue_size), vapi_mtx_ (),
	vapi_ctx_ (nullptr), connected_ (false), thread_ (),
	timer_ (thread_.getIoService ())
  {
  }

  void
  timerHandler (const std::error_code &ec)
  {
    if (ec)
      {
	// Timer was canceled
	return;
      }

    if (!isConnected ())
      {
	return;
      }

    std::unique_lock<std::mutex> lock (vapi_mtx_);
    auto err = vapi_dispatch (vapi_ctx_);
    if (err != VAPI_OK)
      {
	return;
      }

    startDispatcher ();
  }

  void
  startDispatcher ()
  {
    timer_.expires_after (std::chrono::seconds (ktimeout_seconds));
    timer_.async_wait (std::bind (&VapiGlobalConnection::timerHandler, this,
				  std::placeholders::_1));
  }

  static vapi_error_e
  genericCb (vapi_ctx_t ctx, void *callback_ctx, vapi_msg_id_t id, void *msg)
  {
    std::cout << "Called" << std::endl;
    return VAPI_OK;
  }

private:
  std::string app_name_;
  std::string api_prefix_;
  int max_outstanding_requests_;
  int response_queue_size_;
  std::mutex vapi_mtx_;
  vapi_ctx_t vapi_ctx_;
  std::atomic_bool connected_;
  EventThread thread_;
  asio::steady_timer timer_;
};

vapi_error_e
vapi_connect_safe (vapi_ctx_t *vapi_ctx_ret, int async)
{
  return VapiGlobalConnection::getInstance ().vapiConnectSafe (vapi_ctx_ret);
}

vapi_error_e
vapi_disconnect_safe ()
{
  return VAPI_OK;
}

void
vapi_lock ()
{
  VapiGlobalConnection::getInstance ().vapiLock ();
}

void
vapi_unlock ()
{
  VapiGlobalConnection::getInstance ().vapiUnLock ();
}