FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
segment_manager.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019 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 
17 #include <vnet/session/session.h>
19 
20 typedef struct segment_manager_main_
21 {
22  segment_manager_t *segment_managers; /**< Pool of segment managers */
23  clib_valloc_main_t va_allocator; /**< Virtual address allocator */
24  u32 seg_name_counter; /**< Counter for segment names */
25 
26  /*
27  * Configuration
28  */
29  u32 default_fifo_size; /**< default rx/tx fifo size */
30  u32 default_segment_size; /**< default fifo segment size */
31  u32 default_app_mq_size; /**< default app msg q size */
33 
35 
36 #define segment_manager_foreach_segment_w_lock(VAR, SM, BODY) \
37 do { \
38  clib_rwlock_reader_lock (&(SM)->segments_rwlock); \
39  pool_foreach((VAR), ((SM)->segments), (BODY)); \
40  clib_rwlock_reader_unlock (&(SM)->segments_rwlock); \
41 } while (0)
42 
45 {
46  app_worker_t *app_wrk = app_worker_get (sm->app_wrk_index);
48 }
49 
52 {
53  props->add_segment_size = sm_main.default_segment_size;
54  props->rx_fifo_size = sm_main.default_fifo_size;
55  props->tx_fifo_size = sm_main.default_fifo_size;
56  props->evt_q_size = sm_main.default_app_mq_size;
57  return props;
58 }
59 
60 static u8
62 {
63  return (sm->app_wrk_index == SEGMENT_MANAGER_INVALID_APP_INDEX);
64 }
65 
66 void
68 {
69  sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
70 }
71 
74 {
75  return (seg - sm->segments);
76 }
77 
78 /**
79  * Adds segment to segment manager's pool
80  *
81  * If needed a writer's lock is acquired before allocating a new segment
82  * to avoid affecting any of the segments pool readers.
83  */
84 int
86 {
88  u32 rnd_margin = 128 << 10, fs_index = ~0, page_size;
89  uword baseva = (uword) ~ 0ULL, alloc_size;
91  fifo_segment_t *fs;
92  u8 *seg_name;
93  int rv;
94 
95  props = segment_manager_properties_get (sm);
96 
97  /* Not configured for addition of new segments and not first */
98  if (!props->add_segment && !segment_size)
99  {
100  clib_warning ("cannot allocate new segment");
101  return VNET_API_ERROR_INVALID_VALUE;
102  }
103 
104  /*
105  * Allocate fifo segment and grab lock if needed
106  */
107  if (vlib_num_workers ())
108  clib_rwlock_writer_lock (&sm->segments_rwlock);
109 
110  pool_get_zero (sm->segments, fs);
111 
112  /*
113  * Allocate ssvm segment
114  */
115  segment_size = segment_size ? segment_size : props->add_segment_size;
116  page_size = clib_mem_get_page_size ();
117  /* Protect against segment size u32 wrap */
118  segment_size = clib_max (segment_size + page_size - 1, segment_size);
119  segment_size = segment_size & ~(page_size - 1);
120 
121  if (props->segment_type != SSVM_SEGMENT_PRIVATE)
122  {
123  seg_name = format (0, "%d-%d%c", getpid (), smm->seg_name_counter++, 0);
124  alloc_size = (uword) segment_size + rnd_margin;
125  baseva = clib_valloc_alloc (&smm->va_allocator, alloc_size, 0);
126  if (!baseva)
127  {
128  clib_warning ("out of space for segments");
129  pool_put (sm->segments, fs);
130  goto done;
131  }
132  }
133  else
134  seg_name = format (0, "%s%c", "process-private", 0);
135 
136  fs->ssvm.ssvm_size = segment_size;
137  fs->ssvm.name = seg_name;
138  fs->ssvm.requested_va = baseva;
139 
140  if ((rv = ssvm_master_init (&fs->ssvm, props->segment_type)))
141  {
142  clib_warning ("svm_master_init ('%v', %u) failed", seg_name,
143  segment_size);
144 
145  if (props->segment_type != SSVM_SEGMENT_PRIVATE)
146  clib_valloc_free (&smm->va_allocator, baseva);
147  pool_put (sm->segments, fs);
148  goto done;
149  }
150 
151  /*
152  * Initialize fifo segment
153  */
154  fifo_segment_init (fs);
155 
156  /*
157  * Save segment index before dropping lock, if any held
158  */
159  fs_index = fs - sm->segments;
160 
161 done:
162 
163  if (vlib_num_workers ())
164  clib_rwlock_writer_unlock (&sm->segments_rwlock);
165 
166  return fs_index;
167 }
168 
169 /**
170  * Remove segment without lock
171  */
172 void
174 {
176 
177  if (ssvm_type (&fs->ssvm) != SSVM_SEGMENT_PRIVATE)
178  {
180 
181  if (sm->app_wrk_index != SEGMENT_MANAGER_INVALID_APP_INDEX)
182  {
183  app_worker_t *app_wrk;
184  u64 segment_handle;
185  app_wrk = app_worker_get (sm->app_wrk_index);
186  segment_handle = segment_manager_segment_handle (sm, fs);
187  app_worker_del_segment_notify (app_wrk, segment_handle);
188  }
189  }
190 
191  ssvm_delete (&fs->ssvm);
192 
193  if (CLIB_DEBUG)
194  clib_memset (fs, 0xfb, sizeof (*fs));
195  pool_put (sm->segments, fs);
196 }
197 
198 /**
199  * Removes segment after acquiring writer lock
200  */
201 static inline void
203 {
204  fifo_segment_t *fs;
205  u8 is_prealloc;
206 
207  clib_rwlock_writer_lock (&sm->segments_rwlock);
208  fs = segment_manager_get_segment (sm, fs_index);
210  if (is_prealloc && !segment_manager_app_detached (sm))
211  {
212  clib_rwlock_writer_unlock (&sm->segments_rwlock);
213  return;
214  }
215 
217  clib_rwlock_writer_unlock (&sm->segments_rwlock);
218 }
219 
220 /**
221  * Reads a segment from the segment manager's pool without lock
222  */
225 {
226  return pool_elt_at_index (sm->segments, segment_index);
227 }
228 
229 u64
231  fifo_segment_t * segment)
232 {
233  u32 segment_index = segment_manager_segment_index (sm, segment);
234  return (((u64) segment_manager_index (sm) << 32) | segment_index);
235 }
236 
237 static void
238 segment_manager_parse_segment_handle (u64 segment_handle, u32 * sm_index,
239  u32 * segment_index)
240 {
241  *sm_index = segment_handle >> 32;
242  *segment_index = segment_handle & 0xFFFFFFFF;
243 }
244 
245 u64
247  u32 segment_index)
248 {
249  return (((u64) segment_manager_index << 32) | segment_index);
250 }
251 
254 {
255  u32 sm_index, segment_index;
256  segment_manager_t *sm;
257 
258  segment_manager_parse_segment_handle (segment_handle, &sm_index,
259  &segment_index);
260  sm = segment_manager_get (sm_index);
261  if (!sm || pool_is_free_index (sm->segments, segment_index))
262  return 0;
263  return pool_elt_at_index (sm->segments, segment_index);
264 }
265 
266 /**
267  * Reads a segment from the segment manager's pool and acquires reader lock
268  *
269  * Caller must drop the reader's lock by calling
270  * @ref segment_manager_segment_reader_unlock once it finishes working with
271  * the segment.
272  */
275 {
276  clib_rwlock_reader_lock (&sm->segments_rwlock);
277  return pool_elt_at_index (sm->segments, segment_index);
278 }
279 
280 void
282 {
283  clib_rwlock_reader_unlock (&sm->segments_rwlock);
284 }
285 
286 void
288 {
289  clib_rwlock_writer_unlock (&sm->segments_rwlock);
290 }
291 
294 {
296  segment_manager_t *sm;
297 
298  pool_get_zero (smm->segment_managers, sm);
299  clib_rwlock_init (&sm->segments_rwlock);
300  return sm;
301 }
302 
303 /**
304  * Initializes segment manager based on options provided.
305  * Returns error if ssvm segment(s) allocation fails.
306  */
307 int
309  u32 prealloc_fifo_pairs)
310 {
311  u32 rx_fifo_size, tx_fifo_size, pair_size;
312  u32 rx_rounded_data_size, tx_rounded_data_size;
313  u64 approx_total_size, max_seg_size = ((u64) 1 << 32) - (128 << 10);
315  fifo_segment_t *segment;
316  u32 approx_segment_count;
317  int seg_index, i;
318 
319  props = segment_manager_properties_get (sm);
320  first_seg_size = clib_max (first_seg_size, sm_main.default_segment_size);
321 
322  if (prealloc_fifo_pairs)
323  {
324  /* Figure out how many segments should be preallocated */
325  rx_rounded_data_size = (1 << (max_log2 (props->rx_fifo_size)));
326  tx_rounded_data_size = (1 << (max_log2 (props->tx_fifo_size)));
327 
328  rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size;
329  tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size;
330  pair_size = rx_fifo_size + tx_fifo_size;
331 
332  approx_total_size = (u64) prealloc_fifo_pairs *pair_size;
333  if (first_seg_size > approx_total_size)
334  max_seg_size = first_seg_size;
335  approx_segment_count = (approx_total_size + (max_seg_size - 1))
336  / max_seg_size;
337 
338  /* Allocate the segments */
339  for (i = 0; i < approx_segment_count + 1; i++)
340  {
341  seg_index = segment_manager_add_segment (sm, max_seg_size);
342  if (seg_index < 0)
343  {
344  clib_warning ("Failed to preallocate segment %d", i);
345  return seg_index;
346  }
347 
348  segment = segment_manager_get_segment (sm, seg_index);
349  if (i == 0)
350  sm->event_queue = segment_manager_alloc_queue (segment, props);
351 
353  props->rx_fifo_size,
354  props->tx_fifo_size,
355  &prealloc_fifo_pairs);
357  if (prealloc_fifo_pairs == 0)
358  break;
359  }
360  }
361  else
362  {
363  seg_index = segment_manager_add_segment (sm, first_seg_size);
364  if (seg_index < 0)
365  {
366  clib_warning ("Failed to allocate segment");
367  return seg_index;
368  }
369  segment = segment_manager_get_segment (sm, seg_index);
370  sm->event_queue = segment_manager_alloc_queue (segment, props);
371  }
372 
373  return 0;
374 }
375 
376 /**
377  * Cleanup segment manager.
378  */
379 void
381 {
383  fifo_segment_t *fifo_segment;
384 
387 
388  /* If we have empty preallocated segments that haven't been removed, remove
389  * them now. Apart from that, the first segment in the first segment manager
390  * is not removed when all fifos are removed. It can only be removed when
391  * the manager is explicitly deleted/detached by the app. */
392  clib_rwlock_writer_lock (&sm->segments_rwlock);
393 
394  /* *INDENT-OFF* */
395  pool_foreach (fifo_segment, sm->segments, ({
396  segment_manager_del_segment (sm, fifo_segment);
397  }));
398  /* *INDENT-ON* */
399 
400  clib_rwlock_writer_unlock (&sm->segments_rwlock);
401 
402  clib_rwlock_free (&sm->segments_rwlock);
403  if (CLIB_DEBUG)
404  clib_memset (sm, 0xfe, sizeof (*sm));
405  pool_put (smm->segment_managers, sm);
406 }
407 
408 void
410 {
412  if (segment_manager_has_fifos (sm))
414  else
415  {
416  ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm));
418  }
419 }
420 
423 {
424  return pool_elt_at_index (sm_main.segment_managers, index);
425 }
426 
429 {
430  if (pool_is_free_index (sm_main.segment_managers, index))
431  return 0;
432  return pool_elt_at_index (sm_main.segment_managers, index);
433 }
434 
435 u32
437 {
438  return sm - sm_main.segment_managers;
439 }
440 
441 u8
443 {
444  fifo_segment_t *seg;
445  u8 first = 1;
446 
447  /* *INDENT-OFF* */
449  if (CLIB_DEBUG && !first && !fifo_segment_has_fifos (seg)
451  {
452  clib_warning ("segment %d has no fifos!",
454  first = 0;
455  }
456  if (fifo_segment_has_fifos (seg))
457  {
459  return 1;
460  }
461  }));
462  /* *INDENT-ON* */
463 
464  return 0;
465 }
466 
467 /**
468  * Initiate disconnects for all sessions 'owned' by a segment manager
469  */
470 void
472 {
473  fifo_segment_t *fifo_segment;
474  session_handle_t *handles = 0, *handle;
475  session_t *session;
476  svm_fifo_t *fifo;
477 
478  ASSERT (pool_elts (sm->segments) != 0);
479 
480  /* Across all fifo segments used by the server */
481  /* *INDENT-OFF* */
482  segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({
483  fifo = fifo_segment_get_fifo_list (fifo_segment);
484 
485  /*
486  * Remove any residual sessions from the session lookup table
487  * Don't bother deleting the individual fifos, we're going to
488  * throw away the fifo segment in a minute.
489  */
490  while (fifo)
491  {
492  session = session_get_if_valid (fifo->master_session_index,
493  fifo->master_thread_index);
494  if (session)
495  vec_add1 (handles, session_handle (session));
496  fifo = fifo->next;
497  }
498 
499  /* Instead of removing the segment, test when cleaning up disconnected
500  * sessions if the segment can be removed.
501  */
502  }));
503  /* *INDENT-ON* */
504 
505  vec_foreach (handle, handles)
506  {
507  session = session_get_from_handle (*handle);
508  session_close (session);
509  /* Avoid propagating notifications back to the app */
510  session->app_wrk_index = APP_INVALID_INDEX;
511  }
512 }
513 
514 int
516  u32 rx_fifo_size, u32 tx_fifo_size,
517  svm_fifo_t ** rx_fifo, svm_fifo_t ** tx_fifo)
518 {
519  rx_fifo_size = clib_max (rx_fifo_size, sm_main.default_fifo_size);
520  *rx_fifo = fifo_segment_alloc_fifo (fifo_segment, rx_fifo_size,
522 
523  tx_fifo_size = clib_max (tx_fifo_size, sm_main.default_fifo_size);
524  *tx_fifo = fifo_segment_alloc_fifo (fifo_segment, tx_fifo_size,
526 
527  if (*rx_fifo == 0)
528  {
529  /* This would be very odd, but handle it... */
530  if (*tx_fifo != 0)
531  {
532  fifo_segment_free_fifo (fifo_segment, *tx_fifo);
533  *tx_fifo = 0;
534  }
535  return -1;
536  }
537  if (*tx_fifo == 0)
538  {
539  if (*rx_fifo != 0)
540  {
541  fifo_segment_free_fifo (fifo_segment, *rx_fifo);
542  *rx_fifo = 0;
543  }
544  return -1;
545  }
546 
547  return 0;
548 }
549 
550 int
552  svm_fifo_t ** rx_fifo,
553  svm_fifo_t ** tx_fifo)
554 {
555  int alloc_fail = 1, rv = 0, new_fs_index;
557  fifo_segment_t *fs = 0;
558  u32 sm_index, fs_index;
559  u8 added_a_segment = 0;
560  u64 fs_handle;
561 
562  props = segment_manager_properties_get (sm);
563 
564  /*
565  * Find the first free segment to allocate the fifos in
566  */
567 
568  /* *INDENT-OFF* */
570  alloc_fail = segment_manager_try_alloc_fifos (fs,
571  props->rx_fifo_size,
572  props->tx_fifo_size,
573  rx_fifo, tx_fifo);
574  /* Exit with lock held, drop it after notifying app */
575  if (!alloc_fail)
576  goto alloc_success;
577  }));
578  /* *INDENT-ON* */
579 
580 alloc_check:
581 
582  if (!alloc_fail)
583  {
584 
585  alloc_success:
586 
587  ASSERT (rx_fifo && tx_fifo);
588  sm_index = segment_manager_index (sm);
589  fs_index = segment_manager_segment_index (sm, fs);
590  (*tx_fifo)->segment_manager = sm_index;
591  (*rx_fifo)->segment_manager = sm_index;
592  (*tx_fifo)->segment_index = fs_index;
593  (*rx_fifo)->segment_index = fs_index;
594 
595  if (added_a_segment)
596  {
597  app_worker_t *app_wrk;
598  fs_handle = segment_manager_segment_handle (sm, fs);
599  app_wrk = app_worker_get (sm->app_wrk_index);
600  rv = app_worker_add_segment_notify (app_wrk, fs_handle);
601  }
602  /* Drop the lock after app is notified */
604  return rv;
605  }
606 
607  /*
608  * Allocation failed, see if we can add a new segment
609  */
610  if (props->add_segment)
611  {
612  if (added_a_segment)
613  {
614  clib_warning ("Added a segment, still can't allocate a fifo");
616  return SESSION_ERROR_NEW_SEG_NO_SPACE;
617  }
618  if ((new_fs_index = segment_manager_add_segment (sm, 0)) < 0)
619  {
620  clib_warning ("Failed to add new segment");
621  return SESSION_ERROR_SEG_CREATE;
622  }
623  fs = segment_manager_get_segment_w_lock (sm, new_fs_index);
624  alloc_fail = segment_manager_try_alloc_fifos (fs, props->rx_fifo_size,
625  props->tx_fifo_size,
626  rx_fifo, tx_fifo);
627  added_a_segment = 1;
628  goto alloc_check;
629  }
630  else
631  {
632  clib_warning ("Can't add new seg and no space to allocate fifos!");
633  return SESSION_ERROR_NO_SPACE;
634  }
635 }
636 
637 void
639 {
640  segment_manager_t *sm;
641  fifo_segment_t *fs;
642  u32 segment_index;
643 
644  if (!rx_fifo || !tx_fifo)
645  return;
646 
647  /* It's possible to have no segment manager if the session was removed
648  * as result of a detach. */
649  if (!(sm = segment_manager_get_if_valid (rx_fifo->segment_manager)))
650  return;
651 
652  segment_index = rx_fifo->segment_index;
653  fs = segment_manager_get_segment_w_lock (sm, segment_index);
654  fifo_segment_free_fifo (fs, rx_fifo);
655  fifo_segment_free_fifo (fs, tx_fifo);
656 
657  /*
658  * Try to remove svm segment if it has no fifos. This can be done only if
659  * the segment is not the first in the segment manager or if it is first
660  * and it is not protected. Moreover, if the segment is first and the app
661  * has detached from the segment manager, remove the segment manager.
662  */
663  if (!fifo_segment_has_fifos (fs))
664  {
666 
667  /* Remove segment if it holds no fifos or first but not protected */
668  if (segment_index != 0 || !sm->first_is_protected)
669  segment_manager_lock_and_del_segment (sm, segment_index);
670 
671  /* Remove segment manager if no sessions and detached from app */
673  && !segment_manager_has_fifos (sm))
674  {
676  }
677  }
678  else
680 }
681 
682 int
684 {
685  fifo_segment_t *fs;
686  int rv;
687 
688  fs = segment_manager_get_segment_w_lock (sm, f->segment_index);
689  rv = fifo_segment_grow_fifo (fs, f, size);
691 
692  return rv;
693 }
694 
695 int
697 {
698  fifo_segment_t *fs;
699  int rv;
700 
701  fs = segment_manager_get_segment_w_lock (sm, f->segment_index);
704 
705  return rv;
706 }
707 
708 int
710  u8 is_producer)
711 {
712  int rv;
713 
714  rv = svm_fifo_reduce_size (f, size, is_producer);
715 
716  /* Nothing to collect at this point */
717  if (!is_producer)
718  return rv;
719 
720  if (f->flags & SVM_FIFO_F_COLLECT_CHUNKS)
722 
723  return rv;
724 }
725 
726 u32
728 {
729  u32 fifo_evt_size, notif_q_size, q_hdrs;
730  u32 msg_q_sz, fifo_evt_ring_sz, session_ntf_ring_sz;
731 
732  fifo_evt_size = 1 << max_log2 (sizeof (session_event_t));
733  notif_q_size = clib_max (16, q_len >> 4);
734 
735  msg_q_sz = q_len * sizeof (svm_msg_q_msg_t);
736  fifo_evt_ring_sz = q_len * fifo_evt_size;
737  session_ntf_ring_sz = notif_q_size * 256;
738  q_hdrs = sizeof (svm_queue_t) + sizeof (svm_msg_q_t);
739 
740  return (msg_q_sz + fifo_evt_ring_sz + session_ntf_ring_sz + q_hdrs);
741 }
742 
743 /**
744  * Allocates shm queue in the first segment
745  *
746  * Must be called with lock held
747  */
748 svm_msg_q_t *
750  segment_manager_props_t * props)
751 {
752  u32 fifo_evt_size, session_evt_size = 256, notif_q_size;
753  svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
754  svm_msg_q_t *q;
755  void *oldheap;
756 
757  fifo_evt_size = sizeof (session_event_t);
758  notif_q_size = clib_max (16, props->evt_q_size >> 4);
759  /* *INDENT-OFF* */
761  {props->evt_q_size, fifo_evt_size, 0},
762  {notif_q_size, session_evt_size, 0}
763  };
764  /* *INDENT-ON* */
765  cfg->consumer_pid = 0;
766  cfg->n_rings = 2;
767  cfg->q_nitems = props->evt_q_size;
768  cfg->ring_cfgs = rc;
769 
770  oldheap = ssvm_push_heap (segment->ssvm.sh);
771  q = svm_msg_q_alloc (cfg);
773  ssvm_pop_heap (oldheap);
774 
775  if (props->use_mq_eventfd)
776  {
778  clib_warning ("failed to alloc eventfd");
779  }
780  return q;
781 }
782 
783 svm_msg_q_t *
785 {
786  return sm->event_queue;
787 }
788 
789 /**
790  * Frees shm queue allocated in the first segment
791  */
792 void
794 {
795  fifo_segment_t *segment;
797  void *oldheap;
798 
799  ASSERT (!pool_is_free_index (sm->segments, 0));
800 
801  segment = segment_manager_get_segment_w_lock (sm, 0);
802  sh = segment->ssvm.sh;
803 
804  oldheap = ssvm_push_heap (sh);
805  svm_queue_free (q);
806  ssvm_pop_heap (oldheap);
808 }
809 
810 /*
811  * Init segment vm address allocator
812  */
813 void
815 {
817  clib_valloc_chunk_t _ip, *ip = &_ip;
818 
819  ip->baseva = a->baseva;
820  ip->size = a->size;
821 
822  clib_valloc_init (&sm->va_allocator, ip, 1 /* lock */ );
823 
824  sm->default_fifo_size = 1 << 12;
825  sm->default_segment_size = 1 << 20;
826  sm->default_app_mq_size = 128;
827 }
828 
829 static clib_error_t *
831  vlib_cli_command_t * cmd)
832 {
834  u8 show_segments = 0, verbose = 0;
835  segment_manager_t *sm;
836  fifo_segment_t *seg;
837 
839  {
840  if (unformat (input, "segments"))
841  show_segments = 1;
842  else if (unformat (input, "verbose"))
843  verbose = 1;
844  else
845  return clib_error_return (0, "unknown input `%U'",
846  format_unformat_error, input);
847  }
848  vlib_cli_output (vm, "%d segment managers allocated",
849  pool_elts (smm->segment_managers));
850  if (verbose && pool_elts (smm->segment_managers))
851  {
852  vlib_cli_output (vm, "%-10s%=15s%=12s", "Index", "App Index",
853  "Segments");
854 
855  /* *INDENT-OFF* */
856  pool_foreach (sm, smm->segment_managers, ({
857  vlib_cli_output (vm, "%-10d%=15d%=12d", segment_manager_index (sm),
858  sm->app_wrk_index, pool_elts (sm->segments));
859  }));
860  /* *INDENT-ON* */
861 
862  }
863  if (show_segments)
864  {
865  vlib_cli_output (vm, "%U", format_fifo_segment, 0, verbose);
866 
867  /* *INDENT-OFF* */
868  pool_foreach (sm, smm->segment_managers, ({
869  segment_manager_foreach_segment_w_lock (seg, sm, ({
870  vlib_cli_output (vm, "%U", format_fifo_segment, seg, verbose);
871  }));
872  }));
873  /* *INDENT-ON* */
874 
875  }
876  return 0;
877 }
878 
879 /* *INDENT-OFF* */
880 VLIB_CLI_COMMAND (segment_manager_show_command, static) =
881 {
882  .path = "show segment-manager",
883  .short_help = "show segment-manager [segments][verbose]",
884  .function = segment_manager_show_fn,
885 };
886 /* *INDENT-ON* */
887 
888 void
890 {
891  fifo_segment_t *fifo_segment;
893  app_worker_t *app_wrk;
894  const u8 *app_name;
895  u8 *s = 0;
896 
897  if (!sm)
898  {
899  if (verbose)
900  vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
901  "API Client", "SegManager");
902  else
903  vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
904  return;
905  }
906 
907  app_wrk = app_worker_get (sm->app_wrk_index);
908  app_name = application_name_from_index (app_wrk->app_index);
909 
910  clib_rwlock_reader_lock (&sm->segments_rwlock);
911 
912  /* *INDENT-OFF* */
913  pool_foreach (fifo_segment, sm->segments, ({
914  svm_fifo_t *fifo;
915  u8 *str;
916 
917  fifo = fifo_segment_get_fifo_list (fifo_segment);
918  while (fifo)
919  {
920  u32 session_index, thread_index;
921  session_t *session;
922 
923  session_index = fifo->master_session_index;
924  thread_index = fifo->master_thread_index;
925 
926  session = session_get (session_index, thread_index);
927  str = format (0, "%U", format_session, session, verbose);
928 
929  if (verbose)
930  s = format (s, "%-40s%-20s%-15u%-10u", str, app_name,
931  app_wrk->api_client_index, app_wrk->connects_seg_manager);
932  else
933  s = format (s, "%-40s%-20s", str, app_name);
934 
935  vlib_cli_output (vm, "%v", s);
936  vec_reset_length (s);
937  vec_free (str);
938 
939  fifo = fifo->next;
940  }
941  vec_free (s);
942  }));
943  /* *INDENT-ON* */
944 
945  clib_rwlock_reader_unlock (&sm->segments_rwlock);
946 }
947 
948 /*
949  * fd.io coding-style-patch-verification: ON
950  *
951  * Local Variables:
952  * eval: (c-set-style "gnu")
953  * End:
954  */
u32 segment_manager_index(segment_manager_t *sm)
static void clib_rwlock_reader_lock(clib_rwlock_t *p)
Definition: lock.h:150
u64 ssvm_size
Definition: ssvm.h:85
void segment_manager_segment_reader_unlock(segment_manager_t *sm)
u64 segment_manager_segment_handle(segment_manager_t *sm, fifo_segment_t *segment)
svm_msg_q_t * segment_manager_alloc_queue(fifo_segment_t *segment, segment_manager_props_t *props)
Allocates shm queue in the first segment.
uword requested_va
Definition: ssvm.h:88
a
Definition: bitmap.h:538
static void clib_rwlock_writer_lock(clib_rwlock_t *p)
Definition: lock.h:173
fifo_segment_t * segment_manager_get_segment(segment_manager_t *sm, u32 segment_index)
Reads a segment from the segment manager&#39;s pool without lock.
#define pool_get_zero(P, E)
Allocate an object E from a pool P and zero it.
Definition: pool.h:239
unsigned long u64
Definition: types.h:89
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
struct segment_manager_main_ segment_manager_main_t
static void clib_rwlock_free(clib_rwlock_t *p)
Definition: lock.h:140
static session_t * session_get_if_valid(u64 si, u32 thread_index)
Definition: session.h:302
fifo_segment_t * segment_manager_get_segment_w_handle(u64 segment_handle)
void segment_manager_format_sessions(segment_manager_t *sm, int verbose)
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
clib_valloc_main_t va_allocator
Virtual address allocator.
static segment_manager_main_t sm_main
int i
void segment_manager_free(segment_manager_t *sm)
Cleanup segment manager.
ssvm_shared_header_t * sh
Definition: ssvm.h:84
int app_worker_add_segment_notify(app_worker_t *app_wrk, u64 segment_handle)
Send an API message to the external app, to map new segment.
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
segment_manager_t * segment_managers
Pool of segment managers.
u32 segment_manager_evt_q_expected_size(u32 q_len)
unsigned char u8
Definition: types.h:56
static void segment_manager_parse_segment_handle(u64 segment_handle, u32 *sm_index, u32 *segment_index)
int segment_manager_init(segment_manager_t *sm, u32 first_seg_size, u32 prealloc_fifo_pairs)
Initializes segment manager based on options provided.
static u32 segment_manager_segment_index(segment_manager_t *sm, fifo_segment_t *seg)
void ssvm_delete(ssvm_private_t *ssvm)
Definition: ssvm.c:437
static session_handle_t session_handle(session_t *s)
struct _svm_fifo svm_fifo_t
int segment_manager_try_alloc_fifos(fifo_segment_t *fifo_segment, u32 rx_fifo_size, u32 tx_fifo_size, svm_fifo_t **rx_fifo, svm_fifo_t **tx_fifo)
#define fifo_segment_flags(_fs)
Definition: fifo_segment.h:72
u64 segment_manager_make_segment_handle(u32 segment_manager_index, u32 segment_index)
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
svm_msg_q_t * svm_msg_q_alloc(svm_msg_q_cfg_t *cfg)
Allocate message queue.
Definition: message_queue.c:40
int segment_manager_alloc_session_fifos(segment_manager_t *sm, svm_fifo_t **rx_fifo, svm_fifo_t **tx_fifo)
static u8 segment_manager_app_detached(segment_manager_t *sm)
void segment_manager_dealloc_fifos(svm_fifo_t *rx_fifo, svm_fifo_t *tx_fifo)
#define always_inline
Definition: clib.h:99
static void * ssvm_push_heap(ssvm_shared_header_t *sh)
Definition: ssvm.h:143
#define clib_error_return(e, args...)
Definition: error.h:99
u32 default_fifo_size
default rx/tx fifo size
unsigned int u32
Definition: types.h:88
static void segment_manager_lock_and_del_segment(segment_manager_t *sm, u32 fs_index)
Removes segment after acquiring writer lock.
int ssvm_master_init(ssvm_private_t *ssvm, ssvm_segment_type_t type)
Definition: ssvm.c:425
static void ssvm_pop_heap(void *oldheap)
Definition: ssvm.h:151
static heap_elt_t * first(heap_header_t *h)
Definition: heap.c:59
void fifo_segment_preallocate_fifo_pairs(fifo_segment_t *fs, u32 rx_fifo_size, u32 tx_fifo_size, u32 *n_fifo_pairs)
Pre-allocates fifo pairs in fifo segment.
Definition: fifo_segment.c:594
u32 seg_name_counter
Counter for segment names.
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
uword size
static void clib_rwlock_init(clib_rwlock_t *p)
Definition: lock.h:133
static void clib_rwlock_reader_unlock(clib_rwlock_t *p)
Definition: lock.h:165
void segment_manager_app_detach(segment_manager_t *sm)
u32 default_app_mq_size
default app msg q size
static session_t * session_get_from_handle(session_handle_t handle)
Definition: session.h:315
void segment_manager_del_segment(segment_manager_t *sm, fifo_segment_t *fs)
Remove segment without lock.
void segment_manager_dealloc_queue(segment_manager_t *sm, svm_queue_t *q)
Frees shm queue allocated in the first segment.
struct _unformat_input_t unformat_input_t
uword clib_mem_get_page_size(void)
Definition: mem.c:51
const u8 * application_name_from_index(u32 app_index)
Returns app name for app-index.
Definition: application.c:352
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
#define APP_INVALID_INDEX
Definition: application.h:167
void segment_manager_init_free(segment_manager_t *sm)
Initiate segment manager cleanup.
struct _segment_manager_props segment_manager_props_t
int fifo_segment_grow_fifo(fifo_segment_t *fs, svm_fifo_t *f, u32 chunk_size)
Grow fifo size by adding an additional chunk of memory.
Definition: fifo_segment.c:645
u8 segment_manager_has_fifos(segment_manager_t *sm)
uword size
size in bytes of this chunk
Definition: valloc.h:33
svm_fifo_t * fifo_segment_get_fifo_list(fifo_segment_t *fs)
Definition: fifo_segment.c:821
int segment_manager_add_segment(segment_manager_t *sm, u32 segment_size)
Adds segment to segment manager&#39;s pool.
int segment_manager_grow_fifo(segment_manager_t *sm, svm_fifo_t *f, u32 size)
Grows fifo owned by segment manager.
int segment_manager_collect_fifo_chunks(segment_manager_t *sm, svm_fifo_t *f)
Collect fifo chunks that are no longer used.
uword baseva
base VA for this chunk
Definition: valloc.h:32
#define SEGMENT_MANAGER_INVALID_APP_INDEX
static void clib_rwlock_writer_unlock(clib_rwlock_t *p)
Definition: lock.h:187
u32 n_rings
number of msg rings
Definition: message_queue.h:54
svm_msg_q_t * segment_manager_event_queue(segment_manager_t *sm)
ssvm_private_t ssvm
ssvm segment data
Definition: fifo_segment.h:52
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
vlib_main_t * vm
Definition: buffer.c:323
segment_manager_t * segment_manager_alloc(void)
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
static segment_manager_props_t * segment_manager_properties_get(segment_manager_t *sm)
#define clib_warning(format, args...)
Definition: error.h:59
int svm_fifo_reduce_size(svm_fifo_t *f, u32 len, u8 try_shrink)
Request to reduce fifo size by amount of bytes.
Definition: svm_fifo.c:807
#define segment_manager_foreach_segment_w_lock(VAR, SM, BODY)
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:283
void svm_queue_free(svm_queue_t *q)
Definition: queue.c:89
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:161
segment_manager_props_t * application_get_segment_manager_properties(u32 app_index)
Definition: application.c:1299
uword clib_valloc_alloc(clib_valloc_main_t *vam, uword size, int os_out_of_memory_on_failure)
Allocate virtual space.
Definition: valloc.c:151
#define ASSERT(truth)
segment_manager_props_t * segment_manager_props_init(segment_manager_props_t *props)
void fifo_segment_free_fifo(fifo_segment_t *fs, svm_fifo_t *f)
Free fifo allocated in fifo segment.
Definition: fifo_segment.c:435
fifo_segment_t * segment_manager_get_segment_w_lock(segment_manager_t *sm, u32 segment_index)
Reads a segment from the segment manager&#39;s pool and acquires reader lock.
void segment_manager_main_init(segment_manager_main_init_args_t *a)
svm_fifo_t * fifo_segment_alloc_fifo(fifo_segment_t *fs, u32 data_bytes, fifo_segment_ftype_t ftype)
Allocate fifo in fifo segment.
Definition: fifo_segment.c:380
svm_msg_q_ring_cfg_t * ring_cfgs
array of ring cfgs
Definition: message_queue.h:55
#define clib_max(x, y)
Definition: clib.h:295
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u8 * name
Definition: ssvm.h:87
int app_worker_del_segment_notify(app_worker_t *app_wrk, u64 segment_handle)
void segment_manager_segment_writer_unlock(segment_manager_t *sm)
vl_api_address_t ip
Definition: l2.api:489
app_worker_t * app_worker_get(u32 wrk_index)
u32 q_nitems
msg queue size (not rings)
Definition: message_queue.h:53
u64 session_handle_t
static uword max_log2(uword x)
Definition: clib.h:192
void session_close(session_t *s)
Initialize session closing procedure.
Definition: session.c:1223
u64 uword
Definition: types.h:112
void segment_manager_del_sessions(segment_manager_t *sm)
Initiate disconnects for all sessions &#39;owned&#39; by a segment manager.
uword clib_valloc_free(clib_valloc_main_t *vam, uword baseva)
Free virtual space.
Definition: valloc.c:228
struct _segment_manager segment_manager_t
u32 default_segment_size
default fifo segment size
segment_manager_t * segment_manager_get(u32 index)
struct _svm_queue svm_queue_t
void fifo_segment_update_free_bytes(fifo_segment_t *fs)
Update fifo segment free bytes estimate.
Definition: fifo_segment.c:797
u8 fifo_segment_has_fifos(fifo_segment_t *fs)
Definition: fifo_segment.c:815
u32 app_index
Index of owning app.
Definition: application.h:43
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
int fifo_segment_collect_fifo_chunks(fifo_segment_t *fs, svm_fifo_t *f)
Collect unused chunks for fifo.
Definition: fifo_segment.c:694
void clib_valloc_init(clib_valloc_main_t *vam, clib_valloc_chunk_t *template, int need_lock)
Initialize a virtual memory allocation arena.
Definition: valloc.c:129
static u32 vlib_num_workers()
Definition: threads.h:372
#define vec_foreach(var, vec)
Vector iterator.
u32 app_wrk_index
Index of the app worker that owns the session.
static clib_error_t * segment_manager_show_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
int consumer_pid
pid of msg consumer
Definition: message_queue.h:52
int svm_msg_q_alloc_producer_eventfd(svm_msg_q_t *mq)
Allocate event fd for queue consumer.
format_function_t format_fifo_segment
Definition: fifo_segment.h:212
int fifo_segment_init(fifo_segment_t *fs)
Initialize fifo segment shared header.
Definition: fifo_segment.c:41
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:772
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
segment_manager_t * segment_manager_get_if_valid(u32 index)
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171
int segment_manager_shrink_fifo(segment_manager_t *sm, svm_fifo_t *f, u32 size, u8 is_producer)
Request to shrink fifo owned by segment manager.
ssvm_segment_type_t ssvm_type(const ssvm_private_t *ssvm)
Definition: ssvm.c:443
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:128