FD.io VPP  v20.05.1-6-gf53edbc3b
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 */
32  u32 default_max_fifo_size; /**< default max fifo size */
33  u8 default_high_watermark; /**< default high watermark % */
34  u8 default_low_watermark; /**< default low watermark % */
36 
38 
39 #define segment_manager_foreach_segment_w_lock(VAR, SM, BODY) \
40 do { \
41  clib_rwlock_reader_lock (&(SM)->segments_rwlock); \
42  pool_foreach((VAR), ((SM)->segments), (BODY)); \
43  clib_rwlock_reader_unlock (&(SM)->segments_rwlock); \
44 } while (0)
45 
48 {
49  app_worker_t *app_wrk = app_worker_get (sm->app_wrk_index);
51 }
52 
55 {
56  props->add_segment_size = sm_main.default_segment_size;
57  props->rx_fifo_size = sm_main.default_fifo_size;
58  props->tx_fifo_size = sm_main.default_fifo_size;
59  props->evt_q_size = sm_main.default_app_mq_size;
60  props->max_fifo_size = sm_main.default_max_fifo_size;
61  props->high_watermark = sm_main.default_high_watermark;
62  props->low_watermark = sm_main.default_low_watermark;
63  props->n_slices = vlib_num_workers () + 1;
64  return props;
65 }
66 
67 u8
69 {
70  return (sm->app_wrk_index == SEGMENT_MANAGER_INVALID_APP_INDEX);
71 }
72 
73 void
75 {
76  sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
77 }
78 
81 {
82  return (seg - sm->segments);
83 }
84 
85 /**
86  * Adds segment to segment manager's pool
87  *
88  * If needed a writer's lock is acquired before allocating a new segment
89  * to avoid affecting any of the segments pool readers.
90  */
91 int
93 {
94  uword baseva = (uword) ~ 0ULL, alloc_size, page_size;
95  u32 rnd_margin = 128 << 10, fs_index = ~0;
98  fifo_segment_t *fs;
99  u8 *seg_name;
100  int rv;
101 
102  props = segment_manager_properties_get (sm);
103 
104  /* Not configured for addition of new segments and not first */
105  if (!props->add_segment && !segment_size)
106  {
107  clib_warning ("cannot allocate new segment");
108  return VNET_API_ERROR_INVALID_VALUE;
109  }
110 
111  /*
112  * Allocate fifo segment and grab lock if needed
113  */
114  if (vlib_num_workers ())
115  clib_rwlock_writer_lock (&sm->segments_rwlock);
116 
117  pool_get_zero (sm->segments, fs);
118 
119  /*
120  * Allocate ssvm segment
121  */
122  segment_size = segment_size ? segment_size : props->add_segment_size;
123  page_size = clib_mem_get_page_size ();
124  /* Protect against segment size u32 wrap */
125  segment_size = clib_max (segment_size + page_size - 1, segment_size);
126  segment_size = segment_size & ~(page_size - 1);
127 
128  if (props->segment_type != SSVM_SEGMENT_PRIVATE)
129  {
130  seg_name = format (0, "%d-%d%c", getpid (), smm->seg_name_counter++, 0);
131  alloc_size = (uword) segment_size + rnd_margin;
132  baseva = clib_valloc_alloc (&smm->va_allocator, alloc_size, 0);
133  if (!baseva)
134  {
135  clib_warning ("out of space for segments");
136  pool_put (sm->segments, fs);
137  goto done;
138  }
139  }
140  else
141  seg_name = format (0, "%s%c", "process-private", 0);
142 
143  fs->ssvm.ssvm_size = segment_size;
144  fs->ssvm.name = seg_name;
145  fs->ssvm.requested_va = baseva;
146 
147  if ((rv = ssvm_master_init (&fs->ssvm, props->segment_type)))
148  {
149  clib_warning ("svm_master_init ('%v', %u) failed", seg_name,
150  segment_size);
151 
152  if (props->segment_type != SSVM_SEGMENT_PRIVATE)
153  clib_valloc_free (&smm->va_allocator, baseva);
154  pool_put (sm->segments, fs);
155  goto done;
156  }
157 
158  /*
159  * Initialize fifo segment
160  */
161  fs->n_slices = props->n_slices;
162  fifo_segment_init (fs);
163 
164  /*
165  * Save segment index before dropping lock, if any held
166  */
167  fs_index = fs - sm->segments;
168 
169  /*
170  * Set watermarks in segment
171  */
172  fs->h->high_watermark = sm->high_watermark;
173  fs->h->low_watermark = sm->low_watermark;
174  fs->h->pct_first_alloc = props->pct_first_alloc;
176 
177 done:
178 
179  if (vlib_num_workers ())
180  clib_rwlock_writer_unlock (&sm->segments_rwlock);
181 
182  return fs_index;
183 }
184 
185 /**
186  * Remove segment without lock
187  */
188 void
190 {
192 
193  if (ssvm_type (&fs->ssvm) != SSVM_SEGMENT_PRIVATE)
194  {
196 
197  if (sm->app_wrk_index != SEGMENT_MANAGER_INVALID_APP_INDEX)
198  {
199  app_worker_t *app_wrk;
200  u64 segment_handle;
201  app_wrk = app_worker_get (sm->app_wrk_index);
202  segment_handle = segment_manager_segment_handle (sm, fs);
203  app_worker_del_segment_notify (app_wrk, segment_handle);
204  }
205  }
206 
207  ssvm_delete (&fs->ssvm);
208 
209  if (CLIB_DEBUG)
210  clib_memset (fs, 0xfb, sizeof (*fs));
211  pool_put (sm->segments, fs);
212 }
213 
214 static fifo_segment_t *
216  u32 segment_index)
217 {
218  if (pool_is_free_index (sm->segments, segment_index))
219  return 0;
220  return pool_elt_at_index (sm->segments, segment_index);
221 }
222 
223 /**
224  * Removes segment after acquiring writer lock
225  */
226 static inline void
228 {
229  fifo_segment_t *fs;
230  u8 is_prealloc;
231 
232  clib_rwlock_writer_lock (&sm->segments_rwlock);
233 
234  fs = segment_manager_get_segment_if_valid (sm, fs_index);
235  if (!fs)
236  goto done;
237 
239  if (is_prealloc && !segment_manager_app_detached (sm))
240  goto done;
241 
243 
244 done:
245  clib_rwlock_writer_unlock (&sm->segments_rwlock);
246 }
247 
248 /**
249  * Reads a segment from the segment manager's pool without lock
250  */
253 {
254  return pool_elt_at_index (sm->segments, segment_index);
255 }
256 
257 u64
259  fifo_segment_t * segment)
260 {
261  u32 segment_index = segment_manager_segment_index (sm, segment);
262  return (((u64) segment_manager_index (sm) << 32) | segment_index);
263 }
264 
265 u64
267  u32 segment_index)
268 {
269  return (((u64) segment_manager_index << 32) | segment_index);
270 }
271 
274 {
275  u32 sm_index, segment_index;
276  segment_manager_t *sm;
277 
278  segment_manager_parse_segment_handle (segment_handle, &sm_index,
279  &segment_index);
280  sm = segment_manager_get (sm_index);
281  if (!sm || pool_is_free_index (sm->segments, segment_index))
282  return 0;
283  return pool_elt_at_index (sm->segments, segment_index);
284 }
285 
286 /**
287  * Reads a segment from the segment manager's pool and acquires reader lock
288  *
289  * Caller must drop the reader's lock by calling
290  * @ref segment_manager_segment_reader_unlock once it finishes working with
291  * the segment.
292  */
295 {
296  clib_rwlock_reader_lock (&sm->segments_rwlock);
297  return pool_elt_at_index (sm->segments, segment_index);
298 }
299 
300 void
302 {
303  clib_rwlock_reader_lock (&sm->segments_rwlock);
304 }
305 
306 void
308 {
309  clib_rwlock_reader_unlock (&sm->segments_rwlock);
310 }
311 
312 void
314 {
315  clib_rwlock_writer_unlock (&sm->segments_rwlock);
316 }
317 
320 {
322  segment_manager_t *sm;
323 
324  pool_get_zero (smm->segment_managers, sm);
325  clib_rwlock_init (&sm->segments_rwlock);
326  return sm;
327 }
328 
329 /**
330  * Initializes segment manager based on options provided.
331  * Returns error if ssvm segment(s) allocation fails.
332  */
333 int
335 {
337  uword first_seg_size;
338  fifo_segment_t *fs;
339  int fs_index, i;
340 
341  props = segment_manager_properties_get (sm);
342  first_seg_size = clib_max (props->segment_size,
343  sm_main.default_segment_size);
344 
345  sm->max_fifo_size = props->max_fifo_size ?
346  props->max_fifo_size : sm_main.default_max_fifo_size;
347  sm->max_fifo_size = clib_max (sm->max_fifo_size, 4096);
348 
350  props->high_watermark,
351  props->low_watermark);
352 
353  if (props->prealloc_fifos)
354  {
355  u64 approx_total_size, max_seg_size = ((u64) 1 << 32) - (128 << 10);
356  u32 rx_rounded_data_size, tx_rounded_data_size;
357  u32 prealloc_fifo_pairs = props->prealloc_fifos;
358  u32 rx_fifo_size, tx_fifo_size, pair_size;
359  u32 approx_segment_count;
360 
361  /* Figure out how many segments should be preallocated */
362  rx_rounded_data_size = (1 << (max_log2 (props->rx_fifo_size)));
363  tx_rounded_data_size = (1 << (max_log2 (props->tx_fifo_size)));
364 
365  rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size;
366  tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size;
367  pair_size = rx_fifo_size + tx_fifo_size;
368 
369  approx_total_size = (u64) prealloc_fifo_pairs *pair_size;
370  if (first_seg_size > approx_total_size)
371  max_seg_size = first_seg_size;
372  approx_segment_count = (approx_total_size + (max_seg_size - 1))
373  / max_seg_size;
374 
375  /* Allocate the segments */
376  for (i = 0; i < approx_segment_count + 1; i++)
377  {
378  fs_index = segment_manager_add_segment (sm, max_seg_size);
379  if (fs_index < 0)
380  {
381  clib_warning ("Failed to preallocate segment %d", i);
382  return fs_index;
383  }
384 
385  fs = segment_manager_get_segment (sm, fs_index);
386  if (i == 0)
387  sm->event_queue = segment_manager_alloc_queue (fs, props);
388 
390  props->rx_fifo_size,
391  props->tx_fifo_size,
392  &prealloc_fifo_pairs);
394  if (prealloc_fifo_pairs == 0)
395  break;
396  }
397  return 0;
398  }
399 
400  fs_index = segment_manager_add_segment (sm, first_seg_size);
401  if (fs_index < 0)
402  {
403  clib_warning ("Failed to allocate segment");
404  return fs_index;
405  }
406 
407  fs = segment_manager_get_segment (sm, fs_index);
408  sm->event_queue = segment_manager_alloc_queue (fs, props);
409 
410  if (props->prealloc_fifo_hdrs)
411  {
412  u32 hdrs_per_slice;
413 
414  /* Do not preallocate on slice associated to main thread */
415  i = (vlib_num_workers ()? 1 : 0);
416  hdrs_per_slice = props->prealloc_fifo_hdrs / (fs->n_slices - i);
417 
418  for (; i < fs->n_slices; i++)
419  {
420  if (fifo_segment_prealloc_fifo_hdrs (fs, i, hdrs_per_slice))
421  return VNET_API_ERROR_SVM_SEGMENT_CREATE_FAIL;
422  }
423  }
424 
425  return 0;
426 }
427 
428 /**
429  * Cleanup segment manager.
430  */
431 void
433 {
435  fifo_segment_t *fifo_segment;
436 
439 
440  /* If we have empty preallocated segments that haven't been removed, remove
441  * them now. Apart from that, the first segment in the first segment manager
442  * is not removed when all fifos are removed. It can only be removed when
443  * the manager is explicitly deleted/detached by the app. */
444  clib_rwlock_writer_lock (&sm->segments_rwlock);
445 
446  /* *INDENT-OFF* */
447  pool_foreach (fifo_segment, sm->segments, ({
448  segment_manager_del_segment (sm, fifo_segment);
449  }));
450  /* *INDENT-ON* */
451 
452  clib_rwlock_writer_unlock (&sm->segments_rwlock);
453 
454  clib_rwlock_free (&sm->segments_rwlock);
455  if (CLIB_DEBUG)
456  clib_memset (sm, 0xfe, sizeof (*sm));
457  pool_put (smm->segment_managers, sm);
458 }
459 
460 void
462 {
464  if (segment_manager_has_fifos (sm))
466  else
467  {
468  ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm));
470  }
471 }
472 
475 {
476  return pool_elt_at_index (sm_main.segment_managers, index);
477 }
478 
481 {
482  if (pool_is_free_index (sm_main.segment_managers, index))
483  return 0;
484  return pool_elt_at_index (sm_main.segment_managers, index);
485 }
486 
487 u32
489 {
490  return sm - sm_main.segment_managers;
491 }
492 
493 u8
495 {
496  fifo_segment_t *seg;
497  u8 first = 1;
498 
499  /* *INDENT-OFF* */
501  if (CLIB_DEBUG && !first && !fifo_segment_has_fifos (seg)
503  {
504  clib_warning ("segment %d has no fifos!",
506  first = 0;
507  }
508  if (fifo_segment_has_fifos (seg))
509  {
511  return 1;
512  }
513  }));
514  /* *INDENT-ON* */
515 
516  return 0;
517 }
518 
519 /**
520  * Initiate disconnects for all sessions 'owned' by a segment manager
521  */
522 void
524 {
525  session_handle_t *handles = 0, *handle;
526  fifo_segment_t *fs;
527  session_t *session;
528  int slice_index;
529  svm_fifo_t *f;
530 
531  ASSERT (pool_elts (sm->segments) != 0);
532 
533  /* Across all fifo segments used by the server */
534  /* *INDENT-OFF* */
536  for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
537  {
538  f = fifo_segment_get_slice_fifo_list (fs, slice_index);
539 
540  /*
541  * Remove any residual sessions from the session lookup table
542  * Don't bother deleting the individual fifos, we're going to
543  * throw away the fifo segment in a minute.
544  */
545  while (f)
546  {
547  session = session_get_if_valid (f->master_session_index,
548  f->master_thread_index);
549  if (session)
550  vec_add1 (handles, session_handle (session));
551  f = f->next;
552  }
553  }
554 
555  /* Instead of removing the segment, test when cleaning up disconnected
556  * sessions if the segment can be removed.
557  */
558  }));
559  /* *INDENT-ON* */
560 
561  vec_foreach (handle, handles)
562  {
563  session = session_get_from_handle (*handle);
564  session_close (session);
565  /* Avoid propagating notifications back to the app */
566  session->app_wrk_index = APP_INVALID_INDEX;
567  }
568 }
569 
570 int
572  u32 thread_index,
573  u32 rx_fifo_size, u32 tx_fifo_size,
574  svm_fifo_t ** rx_fifo, svm_fifo_t ** tx_fifo)
575 {
576  rx_fifo_size = clib_max (rx_fifo_size, sm_main.default_fifo_size);
577  *rx_fifo = fifo_segment_alloc_fifo_w_slice (fifo_segment, thread_index,
578  rx_fifo_size,
580 
581  tx_fifo_size = clib_max (tx_fifo_size, sm_main.default_fifo_size);
582  *tx_fifo = fifo_segment_alloc_fifo_w_slice (fifo_segment, thread_index,
583  tx_fifo_size,
585 
586  if (*rx_fifo == 0)
587  {
588  /* This would be very odd, but handle it... */
589  if (*tx_fifo != 0)
590  {
591  fifo_segment_free_fifo (fifo_segment, *tx_fifo);
592  *tx_fifo = 0;
593  }
594  return -1;
595  }
596  if (*tx_fifo == 0)
597  {
598  if (*rx_fifo != 0)
599  {
600  fifo_segment_free_fifo (fifo_segment, *rx_fifo);
601  *rx_fifo = 0;
602  }
603  return -1;
604  }
605 
606  return 0;
607 }
608 
609 int
611  u32 thread_index,
612  svm_fifo_t ** rx_fifo,
613  svm_fifo_t ** tx_fifo)
614 {
615  int alloc_fail = 1, rv = 0, new_fs_index;
616  uword free_bytes, max_free_bytes = 0;
618  fifo_segment_t *fs = 0, *cur;
619  u32 sm_index, fs_index;
620  u8 added_a_segment = 0;
621  u64 fs_handle;
622 
623  props = segment_manager_properties_get (sm);
624 
625  /*
626  * Find the first free segment to allocate the fifos in
627  */
628 
630 
631  /* *INDENT-OFF* */
632  pool_foreach (cur, sm->segments, ({
633  free_bytes = fifo_segment_available_bytes (cur);
634  if (free_bytes > max_free_bytes)
635  {
636  max_free_bytes = free_bytes;
637  fs = cur;
638  }
639  }));
640  /* *INDENT-ON* */
641 
642  if (fs)
643  {
644  alloc_fail = segment_manager_try_alloc_fifos (fs, thread_index,
645  props->rx_fifo_size,
646  props->tx_fifo_size,
647  rx_fifo, tx_fifo);
648  /* On success, keep lock until fifos are initialized */
649  if (!alloc_fail)
650  goto alloc_success;
651  }
652 
654 
655 alloc_check:
656 
657  if (!alloc_fail)
658  {
659 
660  alloc_success:
661 
662  ASSERT (rx_fifo && tx_fifo);
663  sm_index = segment_manager_index (sm);
664  fs_index = segment_manager_segment_index (sm, fs);
665  (*tx_fifo)->segment_manager = sm_index;
666  (*rx_fifo)->segment_manager = sm_index;
667  (*tx_fifo)->segment_index = fs_index;
668  (*rx_fifo)->segment_index = fs_index;
669 
670  if (added_a_segment)
671  {
672  app_worker_t *app_wrk;
673  fs_handle = segment_manager_segment_handle (sm, fs);
674  app_wrk = app_worker_get (sm->app_wrk_index);
675  rv = app_worker_add_segment_notify (app_wrk, fs_handle);
676  }
677  /* Drop the lock after app is notified */
679  return rv;
680  }
681 
682  /*
683  * Allocation failed, see if we can add a new segment
684  */
685  if (props->add_segment)
686  {
687  if (added_a_segment)
688  {
689  clib_warning ("Added a segment, still can't allocate a fifo");
691  return SESSION_E_SEG_NO_SPACE2;
692  }
693  if ((new_fs_index = segment_manager_add_segment (sm, 0)) < 0)
694  {
695  clib_warning ("Failed to add new segment");
696  return SESSION_E_SEG_CREATE;
697  }
698  fs = segment_manager_get_segment_w_lock (sm, new_fs_index);
699  alloc_fail = segment_manager_try_alloc_fifos (fs, thread_index,
700  props->rx_fifo_size,
701  props->tx_fifo_size,
702  rx_fifo, tx_fifo);
703  added_a_segment = 1;
704  goto alloc_check;
705  }
706  else
707  {
708  clib_warning ("Can't add new seg and no space to allocate fifos!");
709  return SESSION_E_SEG_NO_SPACE;
710  }
711 }
712 
713 void
715 {
716  segment_manager_t *sm;
717  fifo_segment_t *fs;
718  u32 segment_index;
719 
720  if (!rx_fifo || !tx_fifo)
721  return;
722 
723  /* It's possible to have no segment manager if the session was removed
724  * as result of a detach. */
725  if (!(sm = segment_manager_get_if_valid (rx_fifo->segment_manager)))
726  return;
727 
728  segment_index = rx_fifo->segment_index;
729  fs = segment_manager_get_segment_w_lock (sm, segment_index);
730  fifo_segment_free_fifo (fs, rx_fifo);
731  fifo_segment_free_fifo (fs, tx_fifo);
732 
733  /*
734  * Try to remove svm segment if it has no fifos. This can be done only if
735  * the segment is not the first in the segment manager or if it is first
736  * and it is not protected. Moreover, if the segment is first and the app
737  * has detached from the segment manager, remove the segment manager.
738  */
739  if (!fifo_segment_has_fifos (fs))
740  {
742 
743  /* Remove segment if it holds no fifos or first but not protected */
744  if (segment_index != 0 || !sm->first_is_protected)
745  segment_manager_lock_and_del_segment (sm, segment_index);
746 
747  /* Remove segment manager if no sessions and detached from app */
749  && !segment_manager_has_fifos (sm))
750  {
752  }
753  }
754  else
756 }
757 
758 void
760 {
761  fifo_segment_t *fs;
762 
763  fs = segment_manager_get_segment_w_lock (sm, f->segment_index);
764  fifo_segment_detach_fifo (fs, f);
766 }
767 
768 void
770  session_t * s)
771 {
772  fifo_segment_t *fs;
773 
774  fs = segment_manager_get_segment_w_lock (sm, f->segment_index);
777 
778  f->master_session_index = s->session_index;
779  f->master_thread_index = s->thread_index;
780 }
781 
782 u32
784 {
785  u32 fifo_evt_size, notif_q_size, q_hdrs;
786  u32 msg_q_sz, fifo_evt_ring_sz, session_ntf_ring_sz;
787 
788  fifo_evt_size = 1 << max_log2 (sizeof (session_event_t));
789  notif_q_size = clib_max (16, q_len >> 4);
790 
791  msg_q_sz = q_len * sizeof (svm_msg_q_msg_t);
792  fifo_evt_ring_sz = q_len * fifo_evt_size;
793  session_ntf_ring_sz = notif_q_size * 256;
794  q_hdrs = sizeof (svm_queue_t) + sizeof (svm_msg_q_t);
795 
796  return (msg_q_sz + fifo_evt_ring_sz + session_ntf_ring_sz + q_hdrs);
797 }
798 
799 /**
800  * Allocates shm queue in the first segment
801  *
802  * Must be called with lock held
803  */
804 svm_msg_q_t *
806  segment_manager_props_t * props)
807 {
808  u32 fifo_evt_size, session_evt_size = 256, notif_q_size;
809  svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
810  svm_msg_q_t *q;
811  void *oldheap;
812 
813  fifo_evt_size = sizeof (session_event_t);
814  notif_q_size = clib_max (16, props->evt_q_size >> 4);
815  /* *INDENT-OFF* */
817  {props->evt_q_size, fifo_evt_size, 0},
818  {notif_q_size, session_evt_size, 0}
819  };
820  /* *INDENT-ON* */
821  cfg->consumer_pid = 0;
822  cfg->n_rings = 2;
823  cfg->q_nitems = props->evt_q_size;
824  cfg->ring_cfgs = rc;
825 
826  oldheap = ssvm_push_heap (segment->ssvm.sh);
827  q = svm_msg_q_alloc (cfg);
829  ssvm_pop_heap (oldheap);
830 
831  if (props->use_mq_eventfd)
832  {
834  clib_warning ("failed to alloc eventfd");
835  }
836  return q;
837 }
838 
839 svm_msg_q_t *
841 {
842  return sm->event_queue;
843 }
844 
845 /**
846  * Frees shm queue allocated in the first segment
847  */
848 void
850 {
851  fifo_segment_t *segment;
853  void *oldheap;
854 
855  ASSERT (!pool_is_free_index (sm->segments, 0));
856 
857  segment = segment_manager_get_segment_w_lock (sm, 0);
858  sh = segment->ssvm.sh;
859 
860  oldheap = ssvm_push_heap (sh);
861  svm_queue_free (q);
862  ssvm_pop_heap (oldheap);
864 }
865 
866 /*
867  * Init segment vm address allocator
868  */
869 void
871 {
873  clib_valloc_chunk_t _ip, *ip = &_ip;
874 
875  ip->baseva = a->baseva;
876  ip->size = a->size;
877 
878  clib_valloc_init (&sm->va_allocator, ip, 1 /* lock */ );
879 
880  sm->default_fifo_size = 1 << 12;
881  sm->default_segment_size = 1 << 20;
882  sm->default_app_mq_size = 128;
883  sm->default_max_fifo_size = 4 << 20;
884  sm->default_high_watermark = 80;
885  sm->default_low_watermark = 50;
886 }
887 
888 static clib_error_t *
890  vlib_cli_command_t * cmd)
891 {
893  u8 show_segments = 0, verbose = 0;
894  uword max_fifo_size;
895  segment_manager_t *sm;
896  fifo_segment_t *seg;
897  app_worker_t *app_wrk;
898  application_t *app;
899  u8 custom_logic;
900 
902  {
903  if (unformat (input, "segments"))
904  show_segments = 1;
905  else if (unformat (input, "verbose"))
906  verbose = 1;
907  else
908  return clib_error_return (0, "unknown input `%U'",
909  format_unformat_error, input);
910  }
911  vlib_cli_output (vm, "%d segment managers allocated",
912  pool_elts (smm->segment_managers));
913  if (verbose && pool_elts (smm->segment_managers))
914  {
915  vlib_cli_output (vm, "%-6s%=10s%=10s%=13s%=11s%=11s%=12s",
916  "Index", "AppIndex", "Segments", "MaxFifoSize",
917  "HighWater", "LowWater", "FifoTuning");
918 
919  /* *INDENT-OFF* */
920  pool_foreach (sm, smm->segment_managers, ({
921  app_wrk = app_worker_get_if_valid (sm->app_wrk_index);
922  app = app_wrk ? application_get (app_wrk->app_index) : 0;
923  custom_logic = (app && (app->cb_fns.fifo_tuning_callback)) ? 1 : 0;
924  max_fifo_size = sm->max_fifo_size;
925 
926  vlib_cli_output (vm, "%-6d%=10d%=10d%=13U%=11d%=11d%=12s",
927  segment_manager_index (sm),
928  sm->app_wrk_index, pool_elts (sm->segments),
929  format_memory_size, max_fifo_size,
930  sm->high_watermark, sm->low_watermark,
931  custom_logic ? "custom" : "none");
932  }));
933  /* *INDENT-ON* */
934 
935  }
936  if (show_segments)
937  {
938  vlib_cli_output (vm, "%U", format_fifo_segment, 0, verbose);
939 
940  /* *INDENT-OFF* */
941  pool_foreach (sm, smm->segment_managers, ({
942  segment_manager_foreach_segment_w_lock (seg, sm, ({
943  vlib_cli_output (vm, "%U", format_fifo_segment, seg, verbose);
944  }));
945  }));
946  /* *INDENT-ON* */
947 
948  }
949  return 0;
950 }
951 
952 /* *INDENT-OFF* */
954 {
955  .path = "show segment-manager",
956  .short_help = "show segment-manager [segments][verbose]",
957  .function = segment_manager_show_fn,
958 };
959 /* *INDENT-ON* */
960 
961 void
963 {
965  app_worker_t *app_wrk;
966  fifo_segment_t *fs;
967  const u8 *app_name;
968  int slice_index;
969  u8 *s = 0, *str;
970  svm_fifo_t *f;
971 
972  if (!sm)
973  {
974  if (verbose)
975  vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
976  "API Client", "SegManager");
977  else
978  vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
979  return;
980  }
981 
982  app_wrk = app_worker_get (sm->app_wrk_index);
983  app_name = application_name_from_index (app_wrk->app_index);
984 
985  clib_rwlock_reader_lock (&sm->segments_rwlock);
986 
987  /* *INDENT-OFF* */
988  pool_foreach (fs, sm->segments, ({
989  for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
990  {
991  f = fifo_segment_get_slice_fifo_list (fs, slice_index);
992  while (f)
993  {
994  u32 session_index, thread_index;
995  session_t *session;
996 
997  session_index = f->master_session_index;
998  thread_index = f->master_thread_index;
999 
1000  session = session_get (session_index, thread_index);
1001  str = format (0, "%U", format_session, session, verbose);
1002 
1003  if (verbose)
1004  s = format (s, "%-40s%-20s%-15u%-10u", str, app_name,
1005  app_wrk->api_client_index, app_wrk->connects_seg_manager);
1006  else
1007  s = format (s, "%-40s%-20s", str, app_name);
1008 
1009  vlib_cli_output (vm, "%v", s);
1010  vec_reset_length (s);
1011  vec_free (str);
1012 
1013  f = f->next;
1014  }
1015  vec_free (s);
1016  }
1017  }));
1018  /* *INDENT-ON* */
1019 
1020  clib_rwlock_reader_unlock (&sm->segments_rwlock);
1021 }
1022 
1023 void
1025  u8 high_watermark, u8 low_watermark)
1026 {
1027  ASSERT (high_watermark <= 100 && low_watermark <= 100 &&
1028  low_watermark <= high_watermark);
1029 
1030  sm->high_watermark = high_watermark;
1031  sm->low_watermark = low_watermark;
1032 }
1033 
1034 /*
1035  * fd.io coding-style-patch-verification: ON
1036  *
1037  * Local Variables:
1038  * eval: (c-set-style "gnu")
1039  * End:
1040  */
u32 segment_manager_index(segment_manager_t *sm)
static void clib_rwlock_reader_lock(clib_rwlock_t *p)
Definition: lock.h:150
u8 low_watermark
Memory pressure watermark low.
Definition: fifo_types.h:125
static vlib_cli_command_t segment_manager_show_command
(constructor) VLIB_CLI_COMMAND (segment_manager_show_command)
fifo_segment_header_t * h
fifo segment data
Definition: fifo_segment.h:69
void segment_manager_segment_reader_unlock(segment_manager_t *sm)
u32 default_max_fifo_size
default max fifo size
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:86
a
Definition: bitmap.h:538
static void clib_rwlock_writer_lock(clib_rwlock_t *p)
Definition: lock.h:173
int segment_manager_add_segment(segment_manager_t *sm, uword segment_size)
Adds segment to segment manager&#39;s pool.
uword ssvm_size
Definition: ssvm.h:85
svm_fifo_t * fifo_segment_get_slice_fifo_list(fifo_segment_t *fs, u32 slice_index)
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:255
u32 session_index
Index in thread pool where session was allocated.
unsigned long u64
Definition: types.h:89
u8 pct_first_alloc
Pct of fifo size to alloc.
Definition: fifo_types.h:126
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
struct segment_manager_main_ segment_manager_main_t
svm_fifo_t * fifo_segment_alloc_fifo_w_slice(fifo_segment_t *fs, u32 slice_index, u32 data_bytes, fifo_segment_ftype_t ftype)
Allocate fifo in fifo segment.
Definition: fifo_segment.c:841
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:308
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:590
clib_valloc_main_t va_allocator
Virtual address allocator.
static segment_manager_main_t sm_main
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.
static void segment_manager_parse_segment_handle(u64 segment_handle, u32 *sm_index, u32 *segment_index)
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
u8 default_low_watermark
default low watermark %
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 u32 segment_manager_segment_index(segment_manager_t *sm, fifo_segment_t *seg)
void ssvm_delete(ssvm_private_t *ssvm)
Definition: ssvm.c:427
static session_handle_t session_handle(session_t *s)
void segment_manager_segment_reader_lock(segment_manager_t *sm)
#define fifo_segment_flags(_fs)
Definition: fifo_segment.h:89
void segment_manager_attach_fifo(segment_manager_t *sm, svm_fifo_t *f, session_t *s)
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:513
svm_msg_q_t * svm_msg_q_alloc(svm_msg_q_cfg_t *cfg)
Allocate message queue.
Definition: message_queue.c:41
void segment_manager_dealloc_fifos(svm_fifo_t *rx_fifo, svm_fifo_t *tx_fifo)
static void * ssvm_push_heap(ssvm_shared_header_t *sh)
Definition: ssvm.h:144
#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:415
static void ssvm_pop_heap(void *oldheap)
Definition: ssvm.h:152
u8 high_watermark
Memory pressure watermark high.
Definition: fifo_types.h:124
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.
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:534
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:321
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:360
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:302
#define APP_INVALID_INDEX
Definition: application.h:172
void segment_manager_init_free(segment_manager_t *sm)
Initiate segment manager cleanup.
struct _segment_manager_props segment_manager_props_t
u8 segment_manager_has_fifos(segment_manager_t *sm)
#define always_inline
Definition: ipsec.h:28
uword size
size in bytes of this chunk
Definition: valloc.h:33
void segment_manager_set_watermarks(segment_manager_t *sm, u8 high_watermark, u8 low_watermark)
vlib_main_t * vm
Definition: in2out_ed.c:1599
void fifo_segment_detach_fifo(fifo_segment_t *fs, svm_fifo_t *f)
Definition: fifo_segment.c:935
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:68
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
segment_manager_t * segment_manager_alloc(void)
void segment_manager_detach_fifo(segment_manager_t *sm, svm_fifo_t *f)
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
static segment_manager_props_t * segment_manager_properties_get(segment_manager_t *sm)
#define clib_warning(format, args...)
Definition: error.h:59
u8 segment_manager_app_detached(segment_manager_t *sm)
#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:299
int segment_manager_alloc_session_fifos(segment_manager_t *sm, u32 thread_index, svm_fifo_t **rx_fifo, svm_fifo_t **tx_fifo)
int segment_manager_init(segment_manager_t *sm)
Initializes segment manager based on options provided.
void svm_queue_free(svm_queue_t *q)
Definition: queue.c:89
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:152
u8 n_slices
number of fifo segment slices
Definition: fifo_segment.h:70
segment_manager_props_t * application_get_segment_manager_properties(u32 app_index)
Definition: application.c:1318
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)
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:689
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:889
u8 default_high_watermark
default high watermark %
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_msg_q_ring_cfg_t * ring_cfgs
array of ring cfgs
Definition: message_queue.h:55
#define clib_max(x, y)
Definition: clib.h:312
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u8 * name
Definition: ssvm.h:88
u8 thread_index
Index of the thread that allocated the session.
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:501
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:206
void session_close(session_t *s)
Initialize session closing procedure.
Definition: session.c:1360
u8 flags
Segment flags.
Definition: fifo_types.h:122
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)
int fifo_segment_prealloc_fifo_hdrs(fifo_segment_t *fs, u32 slice_index, u32 batch_size)
Try to preallocate fifo headers.
Definition: fifo_segment.c:981
struct _svm_queue svm_queue_t
void fifo_segment_update_free_bytes(fifo_segment_t *fs)
Update fifo segment free bytes estimate.
u8 fifo_segment_has_fifos(fifo_segment_t *fs)
u32 app_index
Index of owning app.
Definition: application.h:43
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
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
int segment_manager_try_alloc_fifos(fifo_segment_t *fifo_segment, u32 thread_index, u32 rx_fifo_size, u32 tx_fifo_size, svm_fifo_t **rx_fifo, svm_fifo_t **tx_fifo)
static u32 vlib_num_workers()
Definition: threads.h:376
#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)
void fifo_segment_attach_fifo(fifo_segment_t *fs, svm_fifo_t *f, u32 slice_index)
Definition: fifo_segment.c:958
static fifo_segment_t * segment_manager_get_segment_if_valid(segment_manager_t *sm, u32 segment_index)
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:281
int fifo_segment_init(fifo_segment_t *fs)
Initialize fifo segment shared header.
Definition: fifo_segment.c:143
struct _svm_fifo svm_fifo_t
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
ssvm_segment_type_t ssvm_type(const ssvm_private_t *ssvm)
Definition: ssvm.c:433
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:128