summaryrefslogtreecommitdiffstats
path: root/src/vpp-api/vom/interface_cmds.cpp
blob: 4f6286f7befca6ae85ac8a79f193d78e8d6d63d5 (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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
 * Copyright (c) 2017 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.
 */

#include "vom/interface_cmds.hpp"
#include "vom/cmd.hpp"

DEFINE_VAPI_MSG_IDS_VPE_API_JSON;
DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON;
DEFINE_VAPI_MSG_IDS_AF_PACKET_API_JSON;
DEFINE_VAPI_MSG_IDS_TAP_API_JSON;
DEFINE_VAPI_MSG_IDS_STATS_API_JSON;

namespace VOM {
namespace interface_cmds {
loopback_create_cmd::loopback_create_cmd(HW::item<handle_t>& item,
                                         const std::string& name)
  : create_cmd(item, name)
{
}

rc_t
loopback_create_cmd::issue(connection& con)
{
  msg_t req(con.ctx(), std::ref(*this));

  VAPI_CALL(req.execute());

  m_hw_item = wait();

  if (m_hw_item.rc() == rc_t::OK) {
    insert_interface();
  }

  return rc_t::OK;
}
std::string
loopback_create_cmd::to_string() const
{
  std::ostringstream s;
  s << "loopback-itf-create: " << m_hw_item.to_string() << " name:" << m_name;

  return (s.str());
}

af_packet_create_cmd::af_packet_create_cmd(HW::item<handle_t>& item,
                                           const std::string& name)
  : create_cmd(item, name)
{
}

rc_t
af_packet_create_cmd::issue(connection& con)
{
  msg_t req(con.ctx(), std::ref(*this));

  auto& payload = req.get_request().get_payload();

  payload.use_random_hw_addr = 1;
  memset(payload.host_if_name, 0, sizeof(payload.host_if_name));
  memcpy(payload.host_if_name, m_name.c_str(),
         std::min(m_name.length(), sizeof(payload.host_if_name)));

  VAPI_CALL(req.execute());

  m_hw_item = wait();

  if (m_hw_item.rc() == rc_t::OK) {
    insert_interface();
  }

  return rc_t::OK;
}
std::string
af_packet_create_cmd::to_string() const
{
  std::ostringstream s;
  s << "af-packet-itf-create: " << m_hw_item.to_string() << " name:" << m_name;

  return (s.str());
}

tap_create_cmd::tap_create_cmd(HW::item<handle_t>& item,
                               const std::string& name)
  : create_cmd(item, name)
{
}

rc_t
tap_create_cmd::issue(connection& con)
{
  msg_t req(con.ctx(), std::ref(*this));

  auto& payload = req.get_request().get_payload();

  memset(payload.tap_name, 0, sizeof(payload.tap_name));
  memcpy(payload.tap_name, m_name.c_str(),
         std::min(m_name.length(), sizeof(payload.tap_name)));
  payload.use_random_mac = 1;

  VAPI_CALL(req.execute());

  m_hw_item = wait();

  if (m_hw_item.rc() == rc_t::OK) {
    insert_interface();
  }

  return rc_t::OK;
}

std::string
tap_create_cmd::to_string() const
{
  std::ostringstream s;
  s << "tap-intf-create: " << m_hw_item.to_string() << " name:" << m_name;

  return (s.str());
}

loopback_delete_cmd::loopback_delete_cmd(HW::item<handle_t>& item)
  : delete_cmd(item)
{
}

rc_t
loopback_delete_cmd::issue(connection& con)
{
  msg_t req(con.ctx(), std::ref(*this));

  auto& payload = req.get_request().get_payload();
  payload.sw_if_index = m_hw_item.data().value();

  VAPI_CALL(req.execute());

  wait();
  m_hw_item.set(rc_t::NOOP);

  remove_interface();
  return rc_t::OK;
}

std::string
loopback_delete_cmd::to_string() const
{
  std::ostringstream s;
  s << "loopback-itf-delete: " << m_hw_item.to_string();

  return (s.str());
}

af_packet_delete_cmd::af_packet_delete_cmd(HW::item<handle_t>& item,
                                           const std::string& name)
  : delete_cmd(item, name)
{
}

rc_t
af_packet_delete_cmd::issue(connection& con)
{
  msg_t req(con.ctx(), std::ref(*this));

  auto& payload = req.get_request().get_payload();
  memset(payload.host_if_name, 0, sizeof(payload.host_if_name));
  memcpy(payload.host_if_name, m_name.c_str(),
         std::min(m_name.length(), sizeof(payload.host_if_name)));

  VAPI_CALL(req.execute());

  wait();
  m_hw_item.set(rc_t::NOOP);

  remove_interface();
  return rc_t::OK;
}
std::string
af_packet_delete_cmd::to_string() const
{
  std::ostringstream s;
  s << "af_packet-itf-delete: " << m_hw_item.to_string();

  return (s.str());
}

tap_delete_cmd::tap_delete_cmd(HW::item<handle_t>& item)
  : delete_cmd(item)
{
}

rc_t
tap_delete_cmd::issue(connection& con)
{
  // finally... call VPP

  remove_interface();
  return rc_t::OK;
}
std::string
tap_delete_cmd::to_string() const
{
  std::ostringstream s;
  s << "tap-itf-delete: " << m_hw_item.to_string();

  return (s.str());
}

state_change_cmd::state_change_cmd(HW::item<interface::admin_state_t>& state,
                                   const HW::item<handle_t>& hdl)
  : rpc_cmd(state)
  , m_hdl(hdl)
{
}

bool
state_change_cmd::operator==(const state_change_cmd& other) const
{
  return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item));
}

rc_t
state_change_cmd::issue(connection& con)
{
  msg_t req(con.ctx(), std::ref(*this));

  auto& payload = req.get_request().get_payload();
  payload.sw_if_index = m_hdl.data().value();
  payload.admin_up_down = m_hw_item.data().value();

  VAPI_CALL(req.execute());

  m_hw_item.set(wait());

  return rc_t::OK;
}

std::string
state_change_cmd::to_string() const
{
  std::ostringstream s;
  s << "itf-state-change: " << m_hw_item.to_string()
    << " hdl:" << m_hdl.to_string();
  return (s.str());
}

set_table_cmd::set_table_cmd(HW::item<route::table_id_t>& table,
                             const l3_proto_t& proto,
                             const HW::item<handle_t>& hdl)
  : rpc_cmd(table)
  , m_hdl(hdl)
  , m_proto(proto)
{
}

bool
set_table_cmd::operator==(const set_table_cmd& other) const
{
  return ((m_hdl == other.m_hdl) && (m_proto == other.m_proto) &&
          (m_hw_item == other.m_hw_item));
}

rc_t
set_table_cmd::issue(connection& con)
{
  msg_t req(con.ctx(), std::ref(*this));

  auto& payload = req.get_request().get_payload();
  payload.sw_if_index = m_hdl.data().value();
  payload.is_ipv6 = m_proto.is_ipv6();
  payload.vrf_id = m_hw_item.data();

  VAPI_CALL(req.execute());

  m_hw_item.set(wait());

  return (rc_t::OK);
}

std::string
set_table_cmd::to_string() const
{
  std::ostringstream s;
  s << "itf-set-table: " << m_hw_item.to_string()
    << " proto:" << m_proto.to_string() << " hdl:" << m_hdl.to_string();
  return (s.str());
}

set_mac_cmd::set_mac_cmd(HW::item<l2_address_t>& mac,
                         const HW::item<handle_t>& hdl)
  : rpc_cmd(mac)
  , m_hdl(hdl)
{
}

bool
set_mac_cmd::operator==(const set_mac_cmd& other) const
{
  return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item));
}

rc_t
set_mac_cmd::issue(connection& con)
{
  msg_t req(con.ctx(), std::ref(*this));

  auto& payload = req.get_request().get_payload();
  payload.sw_if_index = m_hdl.data().value();
  m_hw_item.data().to_mac().to_bytes(payload.mac_address,
                                     sizeof(payload.mac_address));

  VAPI_CALL(req.execute());

  m_hw_item.set(wait());

  return (rc_t::OK);
}

std::string
set_mac_cmd::to_string() const
{
  std::ostringstream s;
  s << "itf-set-mac: " << m_hw_item.to_string() << " hdl:" << m_hdl.to_string();
  return (s.str());
}

events_cmd::events_cmd(interface::event_listener& el)
  : event_cmd(el.status())
  , m_listener(el)
{
}

bool
events_cmd::operator==(const events_cmd& other) const
{
  return (true);
}

rc_t
events_cmd::issue(connection& con)
{
  /*
 * First set the call back to handle the interface events
 */
  m_reg.reset(new reg_t(con.ctx(), std::ref(*(static_cast<event_cmd*>(this)))));

  /*
 * then send the request to enable them
 */
  msg_t req(con.ctx(), std::ref(*(static_cast<rpc_cmd*>(this))));

  auto& payload = req.get_request().get_payload();
  payload.enable_disable = 1;
  payload.pid = getpid();

  VAPI_CALL(req.execute());

  wait();

  return (rc_t::INPROGRESS);
}

void
events_cmd::retire(connection& con)
{
  /*
 * disable interface events.
 */
  msg_t req(con.ctx(), std::ref(*(static_cast<rpc_cmd*>(this))));

  auto& payload = req.get_request().get_payload();
  payload.enable_disable = 0;
  payload.pid = getpid();

  VAPI_CALL(req.execute());

  wait();
}

void
events_cmd::notify()
{
  m_listener.handle_interface_event(this);
}

std::string
events_cmd::to_string() const
{
  return ("itf-events");
}

/**
 * Interface statistics
 */
stats_cmd::stats_cmd(interface::stat_listener& el,
                     const std::vector<handle_t>& interfaces)
  : event_cmd(el.status())
  , m_listener(el)
  , m_swifindex(interfaces)
{
}

bool
stats_cmd::operator==(const stats_cmd& other) const
{
  return (true);
}

rc_t
stats_cmd::issue(connection& con)
{
  /*
 * First set the clal back to handle the interface stats
 */
  m_reg.reset(new reg_t(con.ctx(), std::ref(*(static_cast<event_cmd*>(this)))));
  // m_reg->execute();

  /*
 * then send the request to enable them
 */
  msg_t req(con.ctx(), m_swifindex.size(),
            std::ref(*(static_cast<rpc_cmd*>(this))));

  auto& payload = req.get_request().get_payload();
  payload.enable_disable = 1;
  payload.pid = getpid();
  payload.num = m_swifindex.size();

  auto it = m_swifindex.cbegin();
  uint32_t ii = 0;
  while (it != m_swifindex.cend()) {
    payload.sw_ifs[ii] = it->value();
    ++it;
    ++ii;
  }

  VAPI_CALL(req.execute());

  wait();

  return (rc_t::INPROGRESS);
}

void
stats_cmd::retire(connection& con)
{
}

void
stats_cmd::notify()
{
  m_listener.handle_interface_stat(this);
}

std::string
stats_cmd::to_string() const
{
  return ("itf-stats");
}

dump_cmd::dump_cmd()
{
}

bool
dump_cmd::operator==(const dump_cmd& other) const
{
  return (true);
}

rc_t
dump_cmd::issue(connection& con)
{
  m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));

  auto& payload = m_dump->get_request().get_payload();
  payload.name_filter_valid = 0;

  VAPI_CALL(m_dump->execute());

  wait();

  return rc_t::OK;
}

std::string
dump_cmd::to_string() const
{
  return ("itf-dump");
}

set_tag::set_tag(HW::item<handle_t>& item, const std::string& name)
  : rpc_cmd(item)
  , m_name(name)
{
}

rc_t
set_tag::issue(connection& con)
{
  msg_t req(con.ctx(), std::ref(*this));

  auto& payload = req.get_request().get_payload();
  payload.is_add = 1;
  payload.sw_if_index = m_hw_item.data().value();
  memcpy(payload.tag, m_name.c_str(), m_name.length());

  VAPI_CALL(req.execute());

  wait();

  return rc_t::OK;
}
std::string
set_tag::to_string() const
{
  std::ostringstream s;
  s << "itf-set-tag: " << m_hw_item.to_string() << " name:" << m_name;

  return (s.str());
}

bool
set_tag::operator==(const set_tag& o) const
{
  return ((m_name == o.m_name) && (m_hw_item.data() == o.m_hw_item.data()));
}
}; // namespace interface_cmds
}; // namespace VOM

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "mozilla")
 * End:
 */
= a->backing_mmap_size; } ASSERT (map_size <= rp->virtual_size - (MMAP_PAGESIZE + SVM_PVT_MHEAP_SIZE)); if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED) { clib_unix_warning ("mmap"); close (fd); return -3; } close (fd); CLIB_MEM_UNPOISON (rp->data_base, map_size); rp->backing_file = (char *) format (0, "%s%c", a->backing_file, 0); rp->flags |= SVM_FLAGS_FILE; } if (a->flags & SVM_FLAGS_MHEAP) { rp->data_heap = create_mspace_with_base (rp->data_base, map_size, 1 /* locked */ ); mspace_disable_expand (rp->data_heap); rp->flags |= SVM_FLAGS_MHEAP; } return 0; } static int svm_data_region_map (svm_map_region_args_t * a, svm_region_t * rp) { int fd; u8 junk = 0; uword map_size; struct stat statb; map_size = rp->virtual_size - (MMAP_PAGESIZE + (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE)); if (a->flags & SVM_FLAGS_FILE) { fd = open (a->backing_file, O_RDWR, 0777); if (fd < 0) { clib_unix_warning ("open"); return -1; } if (fstat (fd, &statb) < 0) { clib_unix_warning ("fstat"); close (fd); return -2; } if (statb.st_mode & S_IFREG) { if (statb.st_size == 0) { if (lseek (fd, map_size, SEEK_SET) == (off_t) - 1) { clib_unix_warning ("seek region size"); close (fd); return -3; } if (write (fd, &junk, 1) != 1) { clib_unix_warning ("set region size"); close (fd); return -3; } } else { map_size = rnd_pagesize (statb.st_size); } } else { map_size = a->backing_mmap_size; } ASSERT (map_size <= rp->virtual_size - (MMAP_PAGESIZE + (a->pvt_heap_size ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE))); if (mmap (rp->data_base, map_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED) { clib_unix_warning ("mmap"); close (fd); return -3; } close (fd); CLIB_MEM_UNPOISON (rp->data_base, map_size); } return 0; } u8 * shm_name_from_svm_map_region_args (svm_map_region_args_t * a) { u8 *shm_name; int root_path_offset = 0; int name_offset = 0; if (a->root_path) { /* Tolerate present or absent slashes */ if (a->root_path[0] == '/') root_path_offset++; if (a->name[0] == '/') name_offset = 1; shm_name = format (0, "/%s-%s%c", &a->root_path[root_path_offset], &a->name[name_offset], 0); } else shm_name = format (0, "%s%c", a->name, 0); return (shm_name); } void svm_region_init_mapped_region (svm_map_region_args_t * a, svm_region_t * rp) { pthread_mutexattr_t attr; pthread_condattr_t cattr; int nbits, words, bit; int overhead_space; void *oldheap; uword data_base; ASSERT (rp); int rv; clib_memset (rp, 0, sizeof (*rp)); if (pthread_mutexattr_init (&attr)) clib_unix_warning ("mutexattr_init"); if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED)) clib_unix_warning ("mutexattr_setpshared"); if (pthread_mutex_init (&rp->mutex, &attr)) clib_unix_warning ("mutex_init"); if (pthread_mutexattr_destroy (&attr)) clib_unix_warning ("mutexattr_destroy"); if (pthread_condattr_init (&cattr)) clib_unix_warning ("condattr_init"); if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED)) clib_unix_warning ("condattr_setpshared"); if (pthread_cond_init (&rp->condvar, &cattr)) clib_unix_warning ("cond_init"); if (pthread_condattr_destroy (&cattr)) clib_unix_warning ("condattr_destroy"); region_lock (rp, 1); rp->virtual_base = a->baseva; rp->virtual_size = a->size; rp->region_heap = create_mspace_with_base (uword_to_pointer (a->baseva + MMAP_PAGESIZE, void *), (a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE, 1 /* locked */ ); mspace_disable_expand (rp->region_heap); oldheap = svm_push_pvt_heap (rp); rp->region_name = (char *) format (0, "%s%c", a->name, 0); vec_add1 (rp->client_pids, getpid ()); nbits = rp->virtual_size / MMAP_PAGESIZE; ASSERT (nbits > 0); rp->bitmap_size = nbits; words = (nbits + BITS (uword) - 1) / BITS (uword); vec_validate (rp->bitmap, words - 1); overhead_space = MMAP_PAGESIZE /* header */ + ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE); bit = 0; data_base = (uword) rp->virtual_base; if (a->flags & SVM_FLAGS_NODATA) rp->flags |= SVM_FLAGS_NEED_DATA_INIT; do { clib_bitmap_set_no_check (rp->bitmap, bit, 1); bit++; overhead_space -= MMAP_PAGESIZE; data_base += MMAP_PAGESIZE; } while (overhead_space > 0); rp->data_base = (void *) data_base; /* * Note: although the POSIX spec guarantees that only one * process enters this block, we have to play games * to hold off clients until e.g. the mutex is ready */ rp->version = SVM_VERSION; /* setup the data portion of the region */ rv = svm_data_region_create (a, rp); if (rv) { clib_warning ("data_region_create: %d", rv); } region_unlock (rp); svm_pop_heap (oldheap); } /* * svm_map_region */ void * svm_map_region (svm_map_region_args_t * a) { int svm_fd; svm_region_t *rp; int deadman = 0; u8 junk = 0; void *oldheap; int rv; int pid_holding_region_lock; u8 *shm_name; int dead_region_recovery = 0; int time_left; struct stat stat; struct timespec ts, tsrem; ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size); ASSERT (a->name); shm_name = shm_name_from_svm_map_region_args (a); if (CLIB_DEBUG > 1) clib_warning ("[%d] map region %s: shm_open (%s)", getpid (), a->name, shm_name); svm_fd = shm_open ((char *) shm_name, O_RDWR | O_CREAT | O_EXCL, 0777); if (svm_fd >= 0) { if (fchmod (svm_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) < 0) clib_unix_warning ("segment chmod"); /* This turns out to fail harmlessly if the client starts first */ if (fchown (svm_fd, a->uid, a->gid) < 0) clib_unix_warning ("segment chown [ok if client starts first]"); vec_free (shm_name); if (lseek (svm_fd, a->size, SEEK_SET) == (off_t) - 1) { clib_warning ("seek region size"); close (svm_fd); return (0); } if (write (svm_fd, &junk, 1) != 1) { clib_warning ("set region size"); close (svm_fd); return (0); } rp = mmap (uword_to_pointer (a->baseva, void *), a->size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, svm_fd, 0); if (rp == (svm_region_t *) MAP_FAILED) { clib_unix_warning ("mmap create"); close (svm_fd); return (0); } close (svm_fd); CLIB_MEM_UNPOISON (rp, a->size); svm_region_init_mapped_region (a, rp); return ((void *) rp); } else { svm_fd = shm_open ((char *) shm_name, O_RDWR, 0777); vec_free (shm_name); if (svm_fd < 0) { perror ("svm_region_map(mmap open)"); return (0); } /* Reset ownership in case the client started first */ if (fchown (svm_fd, a->uid, a->gid) < 0) clib_unix_warning ("segment chown [ok if client starts first]"); time_left = 20; while (1) { if (0 != fstat (svm_fd, &stat)) { clib_warning ("fstat failed: %d", errno); close (svm_fd); return (0); } if (stat.st_size > 0) { break; } if (0 == time_left) { clib_warning ("waiting for resize of shm file timed out"); close (svm_fd); return (0); } ts.tv_sec = 0; ts.tv_nsec = 100000000; while (nanosleep (&ts, &tsrem) < 0) ts = tsrem; time_left--; } rp = mmap (0, MMAP_PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, svm_fd, 0); if (rp == (svm_region_t *) MAP_FAILED) { close (svm_fd); clib_warning ("mmap"); return (0); } CLIB_MEM_UNPOISON (rp, MMAP_PAGESIZE); /* * We lost the footrace to create this region; make sure * the winner has crossed the finish line. */ while (rp->version == 0 && deadman++ < 5) { sleep (1); } /* * <bleep>-ed? */ if (rp->version == 0) { clib_warning ("rp->version %d not %d", rp->version, SVM_VERSION); close (svm_fd); munmap (rp, a->size); return (0); } /* Remap now that the region has been placed */ a->baseva = rp->virtual_base; a->size = rp->virtual_size; munmap (rp, MMAP_PAGESIZE); rp = (void *) mmap (uword_to_pointer (a->baseva, void *), a->size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, svm_fd, 0); if ((uword) rp == (uword) MAP_FAILED) { clib_unix_warning ("mmap"); close (svm_fd); return (0); } close (svm_fd); CLIB_MEM_UNPOISON (rp, a->size); if ((uword) rp != rp->virtual_base) { clib_warning ("mmap botch"); } /* * Try to fix the region mutex if it is held by * a dead process */ pid_holding_region_lock = rp->mutex_owner_pid; if (pid_holding_region_lock && kill (pid_holding_region_lock, 0) < 0) { clib_warning ("region %s mutex held by dead pid %d, tag %d, force unlock", rp->region_name, pid_holding_region_lock, rp->mutex_owner_tag); /* owner pid is nonexistent */ rp->mutex.__data.__owner = 0; rp->mutex.__data.__lock = 0; dead_region_recovery = 1; } if (dead_region_recovery) clib_warning ("recovery: attempt to re-lock region"); region_lock (rp, 2); oldheap = svm_push_pvt_heap (rp); vec_add1 (rp->client_pids, getpid ()); if (dead_region_recovery) clib_warning ("recovery: attempt svm_data_region_map"); rv = svm_data_region_map (a, rp); if (rv) { clib_warning ("data_region_map: %d", rv); } if (dead_region_recovery) clib_warning ("unlock and continue"); region_unlock (rp); svm_pop_heap (oldheap); return ((void *) rp); } return 0; /* NOTREACHED *///NOSONAR } static void svm_mutex_cleanup (void) { int i; for (i = 0; i < nheld; i++) { pthread_mutex_unlock (mutexes_held[i]); //NOSONAR } } static int svm_region_init_internal (svm_map_region_args_t * a) { svm_region_t *rp; u64 ticks = clib_cpu_time_now (); uword randomize_baseva; /* guard against klutz calls */ if (root_rp) return -1; root_rp_refcount++; atexit (svm_mutex_cleanup); /* Randomize the shared-VM base at init time */ if (MMAP_PAGESIZE <= (4 << 10)) randomize_baseva = (ticks & 15) * MMAP_PAGESIZE; else randomize_baseva = (ticks & 3) * MMAP_PAGESIZE; a->baseva += randomize_baseva; rp = svm_map_region (a); if (!rp) return -1; region_lock (rp, 3); /* Set up the main region data structures */ if (rp->flags & SVM_FLAGS_NEED_DATA_INIT) { svm_main_region_t *mp = 0; void *oldheap; rp->flags &= ~(SVM_FLAGS_NEED_DATA_INIT); oldheap = svm_push_pvt_heap (rp); vec_validate (mp, 0); mp->name_hash = hash_create_string (0, sizeof (uword)); mp->root_path = a->root_path ? format (0, "%s%c", a->root_path, 0) : 0; mp->uid = a->uid; mp->gid = a->gid; rp->data_base = mp; svm_pop_heap (oldheap); } region_unlock (rp); root_rp = rp; return 0; } void svm_region_init (void) { svm_map_region_args_t _a, *a = &_a; clib_memset (a, 0, sizeof (*a)); a->root_path = 0; a->name = SVM_GLOBAL_REGION_NAME; a->baseva = svm_get_global_region_base_va (); a->size = SVM_GLOBAL_REGION_SIZE; a->flags = SVM_FLAGS_NODATA; a->uid = 0; a->gid = 0; svm_region_init_internal (a); } int svm_region_init_chroot (const char *root_path) { svm_map_region_args_t _a, *a = &_a; clib_memset (a, 0, sizeof (*a)); a->root_path = root_path; a->name = SVM_GLOBAL_REGION_NAME; a->baseva = svm_get_global_region_base_va (); a->size = SVM_GLOBAL_REGION_SIZE; a->flags = SVM_FLAGS_NODATA; a->uid = 0; a->gid = 0; return svm_region_init_internal (a); } void svm_region_init_chroot_uid_gid (const char *root_path, int uid, int gid) { svm_map_region_args_t _a, *a = &_a; clib_memset (a, 0, sizeof (*a)); a->root_path = root_path; a->name = SVM_GLOBAL_REGION_NAME; a->baseva = svm_get_global_region_base_va (); a->size = SVM_GLOBAL_REGION_SIZE; a->flags = SVM_FLAGS_NODATA; a->uid = uid; a->gid = gid; svm_region_init_internal (a); } void svm_region_init_args (svm_map_region_args_t * a) { svm_region_init_internal (a); } void * svm_region_find_or_create (svm_map_region_args_t * a) { svm_main_region_t *mp; svm_region_t *rp; uword need_nbits; int index, i; void *oldheap; uword *p; u8 *name; svm_subregion_t *subp; ASSERT (root_rp); a->size += MMAP_PAGESIZE + ((a->pvt_heap_size != 0) ? a->pvt_heap_size : SVM_PVT_MHEAP_SIZE); a->size = rnd_pagesize (a->size); region_lock (root_rp, 4); oldheap = svm_push_pvt_heap (root_rp); mp = root_rp->data_base; ASSERT (mp); /* Map the named region from the correct chroot environment */ if (a->root_path == NULL) a->root_path = (char *) mp->root_path; /* * See if this region is already known. If it is, we're * almost done... */ p = hash_get_mem (mp->name_hash, a->name); if (p) { rp = svm_map_region (a); region_unlock (root_rp); svm_pop_heap (oldheap); return rp; } /* Create the region. */ ASSERT ((a->size & ~(MMAP_PAGESIZE - 1)) == a->size); need_nbits = a->size / MMAP_PAGESIZE; index = 1; /* $$$ fixme, figure out how many bit to really skip */ /* * Scan the virtual space allocation bitmap, looking for a large * enough chunk */ do { if (clib_bitmap_get_no_check (root_rp->bitmap, index) == 0) { for (i = 0; i < (need_nbits - 1); i++) { if (clib_bitmap_get_no_check (root_rp->bitmap, index + i) == 1) { index = index + i; goto next; } } break; } index++; next:; } while (index < root_rp->bitmap_size); /* Completely out of VM? */ if (index >= root_rp->bitmap_size) { clib_warning ("region %s: not enough VM to allocate 0x%llx (%lld)", root_rp->region_name, a->size, a->size); svm_pop_heap (oldheap); region_unlock (root_rp); return 0; } /* * Mark virtual space allocated */ #if CLIB_DEBUG > 1 clib_warning ("set %d bits at index %d", need_nbits, index); #endif for (i = 0; i < need_nbits; i++) { clib_bitmap_set_no_check (root_rp->bitmap, index + i, 1); } /* Place this region where it goes... */ a->baseva = root_rp->virtual_base + index * MMAP_PAGESIZE; rp = svm_map_region (a); pool_get (mp->subregions, subp); name = format (0, "%s%c", a->name, 0); subp->subregion_name = name; hash_set_mem (mp->name_hash, name, subp - mp->subregions); svm_pop_heap (oldheap); region_unlock (root_rp); return (rp); } void svm_region_unlink (svm_region_t * rp) { svm_map_region_args_t _a, *a = &_a; svm_main_region_t *mp; u8 *shm_name; ASSERT (root_rp); ASSERT (rp); ASSERT (vec_c_string_is_terminated (rp->region_name)); mp = root_rp->data_base; ASSERT (mp); a->root_path = (char *) mp->root_path; a->name = rp->region_name; shm_name = shm_name_from_svm_map_region_args (a); if (CLIB_DEBUG > 1) clib_warning ("[%d] shm_unlink (%s)", getpid (), shm_name); shm_unlink ((const char *) shm_name); vec_free (shm_name); } /* * svm_region_unmap * * Let go of the indicated region. If the calling process * is the last customer, throw it away completely. * The root region mutex guarantees atomicity with respect to * a new region client showing up at the wrong moment. */ void svm_region_unmap_internal (void *rp_arg, u8 is_client) { int i, mypid = getpid (); int nclients_left; void *oldheap; uword virtual_base, virtual_size; svm_region_t *rp = rp_arg; char *name; /* * If we take a signal while holding one or more shared-memory * mutexes, we may end up back here from an otherwise * benign exit handler. Bail out to avoid a recursive * mutex screw-up. */ if (nheld) return; ASSERT (rp); ASSERT (root_rp); if (CLIB_DEBUG > 1) clib_warning ("[%d] unmap region %s", getpid (), rp->region_name); region_lock (root_rp, 5); region_lock (rp, 6); oldheap = svm_push_pvt_heap (rp); /* nb vec_delete() in the loop */ /* Remove the caller from the list of mappers */ CLIB_MEM_UNPOISON (rp->client_pids, vec_bytes (rp->client_pids)); for (i = 0; i < vec_len (rp->client_pids); i++) { if (rp->client_pids[i] == mypid) { vec_delete (rp->client_pids, 1, i); goto found; } } clib_warning ("pid %d AWOL", mypid); found: svm_pop_heap (oldheap); nclients_left = vec_len (rp->client_pids); virtual_base = rp->virtual_base; virtual_size = rp->virtual_size; if (nclients_left == 0) { int index, nbits, i; svm_main_region_t *mp; uword *p; svm_subregion_t *subp; /* Kill the region, last guy on his way out */ oldheap = svm_push_pvt_heap (root_rp); name = vec_dup (rp->region_name); virtual_base = rp->virtual_base; virtual_size = rp->virtual_size; /* Figure out which bits to clear in the root region bitmap */ index = (virtual_base - root_rp->virtual_base) / MMAP_PAGESIZE; nbits = (virtual_size + MMAP_PAGESIZE - 1) / MMAP_PAGESIZE; #if CLIB_DEBUG > 1 clib_warning ("clear %d bits at index %d", nbits, index); #endif /* Give back the allocated VM */ for (i = 0; i < nbits; i++) { clib_bitmap_set_no_check (root_rp->bitmap, index + i, 0); } mp = root_rp->data_base; p = hash_get_mem (mp->name_hash, name); /* Better never happen ... */ if (p == NULL) { region_unlock (rp); region_unlock (root_rp); svm_pop_heap (oldheap); clib_warning ("Region name '%s' not found?", name); return; } /* Remove from the root region subregion pool */ subp = mp->subregions + p[0]; pool_put (mp->subregions, subp); hash_unset_mem (mp->name_hash, name); vec_free (name); region_unlock (rp); /* If a client asks for the cleanup, don't unlink the backing * file since we can't tell if it has been recreated. */ if (!is_client) svm_region_unlink (rp); munmap ((void *) virtual_base, virtual_size); region_unlock (root_rp); svm_pop_heap (oldheap); return; } region_unlock (rp); region_unlock (root_rp); munmap ((void *) virtual_base, virtual_size); } void svm_region_unmap (void *rp_arg) { svm_region_unmap_internal (rp_arg, 0 /* is_client */ ); } void svm_region_unmap_client (void *rp_arg) { svm_region_unmap_internal (rp_arg, 1 /* is_client */ ); } /* * svm_region_exit */ static void svm_region_exit_internal (u8 is_client) { void *oldheap; int i, mypid = getpid (); uword virtual_base, virtual_size; /* It felt so nice we did it twice... */ if (root_rp == 0) return; if (--root_rp_refcount > 0) return; /* * If we take a signal while holding one or more shared-memory * mutexes, we may end up back here from an otherwise * benign exit handler. Bail out to avoid a recursive * mutex screw-up. */ if (nheld) return; region_lock (root_rp, 7); oldheap = svm_push_pvt_heap (root_rp); virtual_base = root_rp->virtual_base; virtual_size = root_rp->virtual_size; CLIB_MEM_UNPOISON (root_rp->client_pids, vec_bytes (root_rp->client_pids)); for (i = 0; i < vec_len (root_rp->client_pids); i++) { if (root_rp->client_pids[i] == mypid) { vec_delete (root_rp->client_pids, 1, i); goto found; } } clib_warning ("pid %d AWOL", mypid); found: if (!is_client && vec_len (root_rp->client_pids) == 0) svm_region_unlink (root_rp); region_unlock (root_rp); svm_pop_heap (oldheap); root_rp = 0; munmap ((void *) virtual_base, virtual_size); } void svm_region_exit (void) { svm_region_exit_internal (0 /* is_client */ ); } void svm_region_exit_client (void) { svm_region_exit_internal (1 /* is_client */ ); } void svm_client_scan_this_region_nolock (svm_region_t * rp) { int j; int mypid = getpid (); void *oldheap; for (j = 0; j < vec_len (rp->client_pids); j++) { if (mypid == rp->client_pids[j]) continue; if (rp->client_pids[j] && (kill (rp->client_pids[j], 0) < 0)) { clib_warning ("%s: cleanup ghost pid %d", rp->region_name, rp->client_pids[j]); /* nb: client vec in rp->region_heap */ oldheap = svm_push_pvt_heap (rp); vec_delete (rp->client_pids, 1, j); j--; svm_pop_heap (oldheap); } } } /* * Scan svm regions for dead clients */ void svm_client_scan (const char *root_path) { int i, j; svm_main_region_t *mp; svm_map_region_args_t *a = 0; svm_region_t *root_rp; svm_region_t *rp; svm_subregion_t *subp; u8 *name = 0; u8 **svm_names = 0; void *oldheap; int mypid = getpid (); vec_validate (a, 0); svm_region_init_chroot (root_path); root_rp = svm_get_root_rp (); pthread_mutex_lock (&root_rp->mutex); mp = root_rp->data_base; for (j = 0; j < vec_len (root_rp->client_pids); j++) { if (mypid == root_rp->client_pids[j]) continue; if (root_rp->client_pids[j] && (kill (root_rp->client_pids[j], 0) < 0)) { clib_warning ("%s: cleanup ghost pid %d", root_rp->region_name, root_rp->client_pids[j]); /* nb: client vec in root_rp->region_heap */ oldheap = svm_push_pvt_heap (root_rp); vec_delete (root_rp->client_pids, 1, j); j--; svm_pop_heap (oldheap); } } /* * Snapshoot names, can't hold root rp mutex across * find_or_create. */ /* *INDENT-OFF* */ pool_foreach (subp, mp->subregions, ({ name = vec_dup (subp->subregion_name); vec_add1(svm_names, name); })); /* *INDENT-ON* */ pthread_mutex_unlock (&root_rp->mutex); for (i = 0; i < vec_len (svm_names); i++) { vec_validate (a, 0); a->root_path = root_path; a->name = (char *) svm_names[i]; rp = svm_region_find_or_create (a); if (rp) { pthread_mutex_lock (&rp->mutex); svm_client_scan_this_region_nolock (rp); pthread_mutex_unlock (&rp->mutex); svm_region_unmap (rp); vec_free (svm_names[i]); } vec_free (a); } vec_free (svm_names); svm_region_exit (); vec_free (a); } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */