FD.io VPP  v21.01.1
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->flags & SEG_MANAGER_F_DETACHED);
71 }
72 
73 void
75 {
76  sm->flags |= SEG_MANAGER_F_DETACHED;
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  {
142  app_worker_t *app_wrk = app_worker_get (sm->app_wrk_index);
143  application_t *app = application_get (app_wrk->app_index);
144  seg_name = format (0, "%v segment%c", app->name, 0);
145  }
146 
147  fs->ssvm.ssvm_size = segment_size;
148  fs->ssvm.name = seg_name;
149  /* clib_mem_vm_map_shared consumes first page before requested_va */
150  fs->ssvm.requested_va = baseva + page_size;
151 
152  if ((rv = ssvm_server_init (&fs->ssvm, props->segment_type)))
153  {
154  clib_warning ("svm_master_init ('%v', %u) failed", seg_name,
155  segment_size);
156 
157  if (props->segment_type != SSVM_SEGMENT_PRIVATE)
158  clib_valloc_free (&smm->va_allocator, baseva);
159  pool_put (sm->segments, fs);
160  goto done;
161  }
162 
163  /*
164  * Initialize fifo segment
165  */
166  fs->n_slices = props->n_slices;
167  fifo_segment_init (fs);
168 
169  /*
170  * Save segment index before dropping lock, if any held
171  */
172  fs_index = fs - sm->segments;
173 
174  /*
175  * Set watermarks in segment
176  */
177  fs->h->high_watermark = sm->high_watermark;
178  fs->h->low_watermark = sm->low_watermark;
179  fs->h->pct_first_alloc = props->pct_first_alloc;
181 
182 done:
183 
184  if (vlib_num_workers ())
185  clib_rwlock_writer_unlock (&sm->segments_rwlock);
186 
187  return fs_index;
188 }
189 
190 /**
191  * Remove segment without lock
192  */
193 void
195 {
197 
198  if (ssvm_type (&fs->ssvm) != SSVM_SEGMENT_PRIVATE)
199  {
200  /* clib_mem_vm_map_shared consumes first page before requested_va */
203 
205  {
206  app_worker_t *app_wrk;
207  u64 segment_handle;
208  app_wrk = app_worker_get (sm->app_wrk_index);
209  segment_handle = segment_manager_segment_handle (sm, fs);
210  app_worker_del_segment_notify (app_wrk, segment_handle);
211  }
212  }
213 
214  ssvm_delete (&fs->ssvm);
215 
216  if (CLIB_DEBUG)
217  clib_memset (fs, 0xfb, sizeof (*fs));
218  pool_put (sm->segments, fs);
219 }
220 
221 static fifo_segment_t *
223  u32 segment_index)
224 {
225  if (pool_is_free_index (sm->segments, segment_index))
226  return 0;
227  return pool_elt_at_index (sm->segments, segment_index);
228 }
229 
230 /**
231  * Removes segment after acquiring writer lock
232  */
233 static inline void
235 {
236  fifo_segment_t *fs;
237  u8 is_prealloc;
238 
239  clib_rwlock_writer_lock (&sm->segments_rwlock);
240 
241  fs = segment_manager_get_segment_if_valid (sm, fs_index);
242  if (!fs)
243  goto done;
244 
246  if (is_prealloc && !segment_manager_app_detached (sm))
247  goto done;
248 
250 
251 done:
252  clib_rwlock_writer_unlock (&sm->segments_rwlock);
253 }
254 
255 void
257 {
258  sm_lock_and_del_segment_inline (sm, fs_index);
259 }
260 
261 /**
262  * Reads a segment from the segment manager's pool without lock
263  */
266 {
267  return pool_elt_at_index (sm->segments, segment_index);
268 }
269 
270 u64
272  fifo_segment_t * segment)
273 {
274  u32 segment_index = segment_manager_segment_index (sm, segment);
275  return (((u64) segment_manager_index (sm) << 32) | segment_index);
276 }
277 
278 u64
280  u32 segment_index)
281 {
282  return (((u64) segment_manager_index << 32) | segment_index);
283 }
284 
287 {
288  u32 sm_index, segment_index;
289  segment_manager_t *sm;
290 
291  segment_manager_parse_segment_handle (segment_handle, &sm_index,
292  &segment_index);
293  sm = segment_manager_get (sm_index);
294  if (!sm || pool_is_free_index (sm->segments, segment_index))
295  return 0;
296  return pool_elt_at_index (sm->segments, segment_index);
297 }
298 
299 /**
300  * Reads a segment from the segment manager's pool and acquires reader lock
301  *
302  * Caller must drop the reader's lock by calling
303  * @ref segment_manager_segment_reader_unlock once it finishes working with
304  * the segment.
305  */
308 {
309  clib_rwlock_reader_lock (&sm->segments_rwlock);
310  return pool_elt_at_index (sm->segments, segment_index);
311 }
312 
313 void
315 {
316  clib_rwlock_reader_lock (&sm->segments_rwlock);
317 }
318 
319 void
321 {
322  clib_rwlock_reader_unlock (&sm->segments_rwlock);
323 }
324 
325 void
327 {
328  clib_rwlock_writer_unlock (&sm->segments_rwlock);
329 }
330 
333 {
335  segment_manager_t *sm;
336 
337  pool_get_zero (smm->segment_managers, sm);
338  clib_rwlock_init (&sm->segments_rwlock);
339  return sm;
340 }
341 
342 int
344 {
346 
347  props = segment_manager_properties_get (sm);
348 
349  sm->max_fifo_size = props->max_fifo_size ?
350  props->max_fifo_size : sm_main.default_max_fifo_size;
351  sm->max_fifo_size = clib_max (sm->max_fifo_size, 4096);
352 
354  props->high_watermark,
355  props->low_watermark);
356  return 0;
357 }
358 
359 /**
360  * Initializes segment manager based on options provided.
361  * Returns error if ssvm segment(s) allocation fails.
362  */
363 int
365 {
367  uword first_seg_size;
368  fifo_segment_t *fs;
369  int fs_index, i;
370 
372  props = segment_manager_properties_get (sm);
373  first_seg_size = clib_max (props->segment_size,
374  sm_main.default_segment_size);
375 
376  if (props->prealloc_fifos)
377  {
378  u64 approx_total_size, max_seg_size = ((u64) 1 << 32) - (128 << 10);
379  u32 rx_rounded_data_size, tx_rounded_data_size;
380  u32 prealloc_fifo_pairs = props->prealloc_fifos;
381  u32 rx_fifo_size, tx_fifo_size, pair_size;
382  u32 approx_segment_count;
383 
384  /* Figure out how many segments should be preallocated */
385  rx_rounded_data_size = (1 << (max_log2 (props->rx_fifo_size)));
386  tx_rounded_data_size = (1 << (max_log2 (props->tx_fifo_size)));
387 
388  rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size;
389  tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size;
390  pair_size = rx_fifo_size + tx_fifo_size;
391 
392  approx_total_size = (u64) prealloc_fifo_pairs *pair_size;
393  if (first_seg_size > approx_total_size)
394  max_seg_size = first_seg_size;
395  approx_segment_count = (approx_total_size + (max_seg_size - 1))
396  / max_seg_size;
397 
398  /* Allocate the segments */
399  for (i = 0; i < approx_segment_count + 1; i++)
400  {
401  fs_index = segment_manager_add_segment (sm, max_seg_size);
402  if (fs_index < 0)
403  {
404  clib_warning ("Failed to preallocate segment %d", i);
405  return fs_index;
406  }
407 
408  fs = segment_manager_get_segment (sm, fs_index);
409  if (i == 0)
410  sm->event_queue = segment_manager_alloc_queue (fs, props);
411 
413  props->rx_fifo_size,
414  props->tx_fifo_size,
415  &prealloc_fifo_pairs);
417  if (prealloc_fifo_pairs == 0)
418  break;
419  }
420  return 0;
421  }
422 
423  fs_index = segment_manager_add_segment (sm, first_seg_size);
424  if (fs_index < 0)
425  {
426  clib_warning ("Failed to allocate segment");
427  return fs_index;
428  }
429 
430  fs = segment_manager_get_segment (sm, fs_index);
431  sm->event_queue = segment_manager_alloc_queue (fs, props);
432 
433  if (props->prealloc_fifo_hdrs)
434  {
435  u32 hdrs_per_slice;
436 
437  /* Do not preallocate on slice associated to main thread */
438  i = (vlib_num_workers ()? 1 : 0);
439  hdrs_per_slice = props->prealloc_fifo_hdrs / (fs->n_slices - i);
440 
441  for (; i < fs->n_slices; i++)
442  {
443  if (fifo_segment_prealloc_fifo_hdrs (fs, i, hdrs_per_slice))
444  return VNET_API_ERROR_SVM_SEGMENT_CREATE_FAIL;
445  }
446  }
447 
448  return 0;
449 }
450 
451 void
453 {
454  app_worker_t *app_wrk;
455 
456  app_wrk = app_worker_get_if_valid (sm->app_wrk_index);
457  if (!app_wrk)
458  return;
459 
461 }
462 
463 /**
464  * Cleanup segment manager.
465  */
466 void
468 {
470  fifo_segment_t *fifo_segment;
471 
475 
476  if (sm->flags & SEG_MANAGER_F_DETACHED_LISTENER)
478 
479  /* If we have empty preallocated segments that haven't been removed, remove
480  * them now. Apart from that, the first segment in the first segment manager
481  * is not removed when all fifos are removed. It can only be removed when
482  * the manager is explicitly deleted/detached by the app. */
483  clib_rwlock_writer_lock (&sm->segments_rwlock);
484 
485  /* *INDENT-OFF* */
486  pool_foreach (fifo_segment, sm->segments) {
487  segment_manager_del_segment (sm, fifo_segment);
488  }
489  /* *INDENT-ON* */
490 
491  clib_rwlock_writer_unlock (&sm->segments_rwlock);
492 
493  clib_rwlock_free (&sm->segments_rwlock);
494  if (CLIB_DEBUG)
495  clib_memset (sm, 0xfe, sizeof (*sm));
496  pool_put (smm->segment_managers, sm);
497 }
498 
499 static void
501 {
502  u32 sm_index = *(u32 *) arg;
503  segment_manager_t *sm;
504 
505  ASSERT (vlib_get_thread_index () == 0);
506 
507  if ((sm = segment_manager_get_if_valid (sm_index)))
509 }
510 
511 static void
513 {
515  {
516  u32 sm_index = segment_manager_index (sm);
518  sizeof (sm_index));
519  }
520  else
521  {
523  }
524 }
525 
526 void
528 {
529  ASSERT (vlib_get_thread_index () == 0);
530 
532  if (segment_manager_has_fifos (sm))
534  else
535  {
536  ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm));
538  }
539 }
540 
543 {
544  return pool_elt_at_index (sm_main.segment_managers, index);
545 }
546 
549 {
550  if (pool_is_free_index (sm_main.segment_managers, index))
551  return 0;
552  return pool_elt_at_index (sm_main.segment_managers, index);
553 }
554 
555 u32
557 {
558  return sm - sm_main.segment_managers;
559 }
560 
561 u8
563 {
564  fifo_segment_t *seg;
565  u8 first = 1;
566 
567  /* *INDENT-OFF* */
569  if (CLIB_DEBUG && !first && !fifo_segment_has_fifos (seg)
571  {
572  clib_warning ("segment %d has no fifos!",
574  first = 0;
575  }
576  if (fifo_segment_has_fifos (seg))
577  {
579  return 1;
580  }
581  }));
582  /* *INDENT-ON* */
583 
584  return 0;
585 }
586 
587 /**
588  * Initiate disconnects for all sessions 'owned' by a segment manager
589  */
590 void
592 {
593  session_handle_t *handles = 0, *handle;
594  fifo_segment_t *fs;
595  session_t *session;
596  int slice_index;
597  svm_fifo_t *f;
598 
599  ASSERT (pool_elts (sm->segments) != 0);
600 
601  /* Across all fifo segments used by the server */
602  /* *INDENT-OFF* */
604  for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
605  {
606  f = fifo_segment_get_slice_fifo_list (fs, slice_index);
607 
608  /*
609  * Remove any residual sessions from the session lookup table
610  * Don't bother deleting the individual fifos, we're going to
611  * throw away the fifo segment in a minute.
612  */
613  while (f)
614  {
615  session = session_get_if_valid (f->master_session_index,
616  f->master_thread_index);
617  if (session)
618  vec_add1 (handles, session_handle (session));
619  f = f->next;
620  }
621  }
622 
623  /* Instead of removing the segment, test when cleaning up disconnected
624  * sessions if the segment can be removed.
625  */
626  }));
627  /* *INDENT-ON* */
628 
629  vec_foreach (handle, handles)
630  {
631  session = session_get_from_handle (*handle);
632  session_close (session);
633  /* Avoid propagating notifications back to the app */
634  session->app_wrk_index = APP_INVALID_INDEX;
635  }
636 }
637 
638 int
640  u32 thread_index,
641  u32 rx_fifo_size, u32 tx_fifo_size,
642  svm_fifo_t ** rx_fifo, svm_fifo_t ** tx_fifo)
643 {
644  rx_fifo_size = clib_max (rx_fifo_size, sm_main.default_fifo_size);
645  *rx_fifo = fifo_segment_alloc_fifo_w_slice (fifo_segment, thread_index,
646  rx_fifo_size,
648 
649  tx_fifo_size = clib_max (tx_fifo_size, sm_main.default_fifo_size);
650  *tx_fifo = fifo_segment_alloc_fifo_w_slice (fifo_segment, thread_index,
651  tx_fifo_size,
653 
654  if (*rx_fifo == 0)
655  {
656  /* This would be very odd, but handle it... */
657  if (*tx_fifo != 0)
658  {
659  fifo_segment_free_fifo (fifo_segment, *tx_fifo);
660  *tx_fifo = 0;
661  }
662  return -1;
663  }
664  if (*tx_fifo == 0)
665  {
666  if (*rx_fifo != 0)
667  {
668  fifo_segment_free_fifo (fifo_segment, *rx_fifo);
669  *rx_fifo = 0;
670  }
671  return -1;
672  }
673 
674  return 0;
675 }
676 
677 int
679  u32 thread_index,
680  svm_fifo_t ** rx_fifo,
681  svm_fifo_t ** tx_fifo)
682 {
683  int alloc_fail = 1, rv = 0, new_fs_index;
684  uword free_bytes, max_free_bytes = 0;
686  fifo_segment_t *fs = 0, *cur;
687  u32 sm_index, fs_index;
688  u8 added_a_segment = 0;
689  u64 fs_handle;
690 
691  props = segment_manager_properties_get (sm);
692 
693  /*
694  * Find the first free segment to allocate the fifos in
695  */
696 
698 
699  /* *INDENT-OFF* */
700  pool_foreach (cur, sm->segments) {
701  free_bytes = fifo_segment_available_bytes (cur);
702  if (free_bytes > max_free_bytes)
703  {
704  max_free_bytes = free_bytes;
705  fs = cur;
706  }
707  }
708  /* *INDENT-ON* */
709 
710  if (fs)
711  {
712  alloc_fail = segment_manager_try_alloc_fifos (fs, thread_index,
713  props->rx_fifo_size,
714  props->tx_fifo_size,
715  rx_fifo, tx_fifo);
716  /* On success, keep lock until fifos are initialized */
717  if (!alloc_fail)
718  goto alloc_success;
719  }
720 
722 
723 alloc_check:
724 
725  if (!alloc_fail)
726  {
727 
728  alloc_success:
729 
730  ASSERT (rx_fifo && tx_fifo);
731  sm_index = segment_manager_index (sm);
732  fs_index = segment_manager_segment_index (sm, fs);
733  (*tx_fifo)->segment_manager = sm_index;
734  (*rx_fifo)->segment_manager = sm_index;
735  (*tx_fifo)->segment_index = fs_index;
736  (*rx_fifo)->segment_index = fs_index;
737 
738  if (added_a_segment)
739  {
740  app_worker_t *app_wrk;
741  fs_handle = segment_manager_segment_handle (sm, fs);
742  app_wrk = app_worker_get (sm->app_wrk_index);
743  rv = app_worker_add_segment_notify (app_wrk, fs_handle);
744  }
745  /* Drop the lock after app is notified */
747  return rv;
748  }
749 
750  /*
751  * Allocation failed, see if we can add a new segment
752  */
753  if (props->add_segment)
754  {
755  if (added_a_segment)
756  {
757  clib_warning ("Added a segment, still can't allocate a fifo");
759  return SESSION_E_SEG_NO_SPACE2;
760  }
761  if ((new_fs_index = segment_manager_add_segment (sm, 0)) < 0)
762  {
763  clib_warning ("Failed to add new segment");
764  return SESSION_E_SEG_CREATE;
765  }
766  fs = segment_manager_get_segment_w_lock (sm, new_fs_index);
767  alloc_fail = segment_manager_try_alloc_fifos (fs, thread_index,
768  props->rx_fifo_size,
769  props->tx_fifo_size,
770  rx_fifo, tx_fifo);
771  added_a_segment = 1;
772  goto alloc_check;
773  }
774  else
775  {
776  SESSION_DBG ("Can't add new seg and no space to allocate fifos!");
777  return SESSION_E_SEG_NO_SPACE;
778  }
779 }
780 
781 void
783 {
784  segment_manager_t *sm;
785  fifo_segment_t *fs;
786  u32 segment_index;
787 
788  if (!rx_fifo || !tx_fifo)
789  return;
790 
791  /* It's possible to have no segment manager if the session was removed
792  * as result of a detach. */
793  if (!(sm = segment_manager_get_if_valid (rx_fifo->segment_manager)))
794  return;
795 
796  segment_index = rx_fifo->segment_index;
797  fs = segment_manager_get_segment_w_lock (sm, segment_index);
798  fifo_segment_free_fifo (fs, rx_fifo);
799  fifo_segment_free_fifo (fs, tx_fifo);
800 
801  /*
802  * Try to remove svm segment if it has no fifos. This can be done only if
803  * the segment is not the first in the segment manager or if it is first
804  * and it is not protected. Moreover, if the segment is first and the app
805  * has detached from the segment manager, remove the segment manager.
806  */
807  if (!fifo_segment_has_fifos (fs))
808  {
810 
811  /* Remove segment if it holds no fifos or first but not protected */
812  if (segment_index != 0 || !sm->first_is_protected)
813  sm_lock_and_del_segment_inline (sm, segment_index);
814 
815  /* Remove segment manager if no sessions and detached from app */
817  && !segment_manager_has_fifos (sm))
819  }
820  else
822 }
823 
824 void
826 {
827  fifo_segment_t *fs;
828 
829  fs = segment_manager_get_segment_w_lock (sm, f->segment_index);
830  fifo_segment_detach_fifo (fs, f);
832 }
833 
834 void
836  session_t * s)
837 {
838  fifo_segment_t *fs;
839 
840  fs = segment_manager_get_segment_w_lock (sm, f->segment_index);
843 
844  f->master_session_index = s->session_index;
845  f->master_thread_index = s->thread_index;
846 }
847 
848 u32
850 {
851  u32 fifo_evt_size, notif_q_size, q_hdrs;
852  u32 msg_q_sz, fifo_evt_ring_sz, session_ntf_ring_sz;
853 
854  fifo_evt_size = 1 << max_log2 (sizeof (session_event_t));
855  notif_q_size = clib_max (16, q_len >> 4);
856 
857  msg_q_sz = q_len * sizeof (svm_msg_q_msg_t);
858  fifo_evt_ring_sz = q_len * fifo_evt_size;
859  session_ntf_ring_sz = notif_q_size * 256;
860  q_hdrs = sizeof (svm_queue_t) + sizeof (svm_msg_q_t);
861 
862  return (msg_q_sz + fifo_evt_ring_sz + session_ntf_ring_sz + q_hdrs);
863 }
864 
865 /**
866  * Allocates shm queue in the first segment
867  *
868  * Must be called with lock held
869  */
870 svm_msg_q_t *
872  segment_manager_props_t * props)
873 {
874  u32 fifo_evt_size, session_evt_size = 256, notif_q_size;
875  svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
876  svm_msg_q_t *q;
877  void *oldheap;
878 
879  fifo_evt_size = sizeof (session_event_t);
880  notif_q_size = clib_max (16, props->evt_q_size >> 4);
881  /* *INDENT-OFF* */
883  {props->evt_q_size, fifo_evt_size, 0},
884  {notif_q_size, session_evt_size, 0}
885  };
886  /* *INDENT-ON* */
887  cfg->consumer_pid = 0;
888  cfg->n_rings = 2;
889  cfg->q_nitems = props->evt_q_size;
890  cfg->ring_cfgs = rc;
891 
892  oldheap = ssvm_push_heap (segment->ssvm.sh);
893  q = svm_msg_q_alloc (cfg);
895  ssvm_pop_heap (oldheap);
896 
897  if (props->use_mq_eventfd)
898  {
900  clib_warning ("failed to alloc eventfd");
901  }
902  return q;
903 }
904 
905 svm_msg_q_t *
907 {
908  return sm->event_queue;
909 }
910 
911 /**
912  * Frees shm queue allocated in the first segment
913  */
914 void
916 {
917  fifo_segment_t *segment;
919  void *oldheap;
920 
921  ASSERT (!pool_is_free_index (sm->segments, 0));
922 
923  segment = segment_manager_get_segment_w_lock (sm, 0);
924  sh = segment->ssvm.sh;
925 
926  oldheap = ssvm_push_heap (sh);
927  svm_queue_free (q);
928  ssvm_pop_heap (oldheap);
930 }
931 
932 /*
933  * Init segment vm address allocator
934  */
935 void
937 {
939  clib_valloc_chunk_t _ip, *ip = &_ip;
940 
941  ip->baseva = a->baseva;
942  ip->size = a->size;
943 
944  clib_valloc_init (&sm->va_allocator, ip, 1 /* lock */ );
945 
946  sm->default_fifo_size = 1 << 12;
947  sm->default_segment_size = 1 << 20;
948  sm->default_app_mq_size = 128;
949  sm->default_max_fifo_size = 4 << 20;
950  sm->default_high_watermark = 80;
951  sm->default_low_watermark = 50;
952 }
953 
954 static clib_error_t *
956  vlib_cli_command_t * cmd)
957 {
959  u8 show_segments = 0, verbose = 0;
960  uword max_fifo_size;
961  segment_manager_t *sm;
962  fifo_segment_t *seg;
963  app_worker_t *app_wrk;
964  application_t *app;
965  u8 custom_logic;
966 
968  {
969  if (unformat (input, "segments"))
970  show_segments = 1;
971  else if (unformat (input, "verbose"))
972  verbose = 1;
973  else
974  return clib_error_return (0, "unknown input `%U'",
975  format_unformat_error, input);
976  }
977  vlib_cli_output (vm, "%d segment managers allocated",
978  pool_elts (smm->segment_managers));
979  if (verbose && pool_elts (smm->segment_managers))
980  {
981  vlib_cli_output (vm, "%-6s%=10s%=10s%=13s%=11s%=11s%=12s",
982  "Index", "AppIndex", "Segments", "MaxFifoSize",
983  "HighWater", "LowWater", "FifoTuning");
984 
985  /* *INDENT-OFF* */
986  pool_foreach (sm, smm->segment_managers) {
987  app_wrk = app_worker_get_if_valid (sm->app_wrk_index);
988  app = app_wrk ? application_get (app_wrk->app_index) : 0;
989  custom_logic = (app && (app->cb_fns.fifo_tuning_callback)) ? 1 : 0;
990  max_fifo_size = sm->max_fifo_size;
991 
992  vlib_cli_output (vm, "%-6d%=10d%=10d%=13U%=11d%=11d%=12s",
994  sm->app_wrk_index, pool_elts (sm->segments),
995  format_memory_size, max_fifo_size,
996  sm->high_watermark, sm->low_watermark,
997  custom_logic ? "custom" : "none");
998  }
999  /* *INDENT-ON* */
1000 
1001  vlib_cli_output (vm, "\n");
1002  }
1003  if (show_segments)
1004  {
1005  vlib_cli_output (vm, "%U", format_fifo_segment, 0, verbose);
1006 
1007  /* *INDENT-OFF* */
1008  pool_foreach (sm, smm->segment_managers) {
1010  vlib_cli_output (vm, "%U", format_fifo_segment, seg, verbose);
1011  }));
1012  }
1013  /* *INDENT-ON* */
1014 
1015  }
1016  return 0;
1017 }
1018 
1019 /* *INDENT-OFF* */
1020 VLIB_CLI_COMMAND (segment_manager_show_command, static) =
1021 {
1022  .path = "show segment-manager",
1023  .short_help = "show segment-manager [segments][verbose]",
1024  .function = segment_manager_show_fn,
1025 };
1026 /* *INDENT-ON* */
1027 
1028 void
1030 {
1032  app_worker_t *app_wrk;
1033  fifo_segment_t *fs;
1034  const u8 *app_name;
1035  int slice_index;
1036  u8 *s = 0, *str;
1037  svm_fifo_t *f;
1038 
1039  if (!sm)
1040  {
1041  if (verbose)
1042  vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
1043  "API Client", "SegManager");
1044  else
1045  vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
1046  return;
1047  }
1048 
1049  app_wrk = app_worker_get (sm->app_wrk_index);
1050  app_name = application_name_from_index (app_wrk->app_index);
1051 
1052  clib_rwlock_reader_lock (&sm->segments_rwlock);
1053 
1054  /* *INDENT-OFF* */
1055  pool_foreach (fs, sm->segments) {
1056  for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1057  {
1058  f = fifo_segment_get_slice_fifo_list (fs, slice_index);
1059  while (f)
1060  {
1061  u32 session_index, thread_index;
1062  session_t *session;
1063 
1064  session_index = f->master_session_index;
1065  thread_index = f->master_thread_index;
1066 
1067  session = session_get (session_index, thread_index);
1068  str = format (0, "%U", format_session, session, verbose);
1069 
1070  if (verbose)
1071  s = format (s, "%-40v%-20v%-15u%-10u", str, app_name,
1072  app_wrk->api_client_index, app_wrk->connects_seg_manager);
1073  else
1074  s = format (s, "%-40v%-20v", str, app_name);
1075 
1076  vlib_cli_output (vm, "%v", s);
1077  vec_reset_length (s);
1078  vec_free (str);
1079 
1080  f = f->next;
1081  }
1082  vec_free (s);
1083  }
1084  }
1085  /* *INDENT-ON* */
1086 
1087  clib_rwlock_reader_unlock (&sm->segments_rwlock);
1088 }
1089 
1090 void
1092  u8 high_watermark, u8 low_watermark)
1093 {
1094  ASSERT (high_watermark <= 100 && low_watermark <= 100 &&
1095  low_watermark <= high_watermark);
1096 
1097  sm->high_watermark = high_watermark;
1098  sm->low_watermark = low_watermark;
1099 }
1100 
1101 /*
1102  * fd.io coding-style-patch-verification: ON
1103  *
1104  * Local Variables:
1105  * eval: (c-set-style "gnu")
1106  * End:
1107  */
u32 segment_manager_index(segment_manager_t *sm)
static void clib_rwlock_reader_lock(clib_rwlock_t *p)
Definition: lock.h:169
u8 low_watermark
Memory pressure watermark low.
Definition: fifo_types.h:125
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)
static void sm_free_w_index_helper(void *arg)
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:85
u8 * format_session(u8 *s, va_list *args)
Format stream session as per the following format.
Definition: session_cli.c:101
a
Definition: bitmap.h:544
static void clib_rwlock_writer_lock(clib_rwlock_t *p)
Definition: lock.h:192
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:84
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:254
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:527
u32 session_index
Index in thread pool where session was allocated.
static void segment_manager_free_safe(segment_manager_t *sm)
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:751
int segment_manager_init_first(segment_manager_t *sm)
Initializes segment manager based on options provided.
static void clib_rwlock_free(clib_rwlock_t *p)
Definition: lock.h:159
static session_t * session_get_if_valid(u64 si, u32 thread_index)
Definition: session.h:314
fifo_segment_t * segment_manager_get_segment_w_handle(u64 segment_handle)
__clib_export 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
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:592
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.
vlib_main_t * vm
Definition: in2out_ed.c:1580
ssvm_shared_header_t * sh
Definition: ssvm.h:83
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)
static session_t * session_get(u32 si, u32 thread_index)
Definition: session.h:307
u8 default_low_watermark
default low watermark %
u32 connects_seg_manager
Segment manager used for outgoing connects issued by the app.
Definition: application.h:49
segment_manager_t * segment_managers
Pool of segment managers.
static_always_inline uword clib_mem_get_page_size(void)
Definition: mem.h:468
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:445
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
void segment_manager_cleanup_detached_listener(segment_manager_t *sm)
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)
int ssvm_server_init(ssvm_private_t *ssvm, ssvm_segment_type_t type)
Definition: ssvm.c:433
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)
description fragment has unexpected format
Definition: map.api:433
u8 * format_memory_size(u8 *s, va_list *va)
Definition: std-formats.c:209
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 ssvm_pop_heap(void *oldheap)
Definition: ssvm.h:151
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.
Definition: fifo_segment.c:919
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:546
static void clib_rwlock_init(clib_rwlock_t *p)
Definition: lock.h:152
static void clib_rwlock_reader_unlock(clib_rwlock_t *p)
Definition: lock.h:184
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:327
void segment_manager_del_segment(segment_manager_t *sm, fifo_segment_t *fs)
Remove segment without lock.
__clib_export uword clib_valloc_free(clib_valloc_main_t *vam, uword baseva)
Free virtual space.
Definition: valloc.c:228
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
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:301
#define APP_INVALID_INDEX
Definition: application.h:178
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
app_worker_t * app_worker_get_if_valid(u32 wrk_index)
void segment_manager_set_watermarks(segment_manager_t *sm, u8 high_watermark, u8 low_watermark)
void fifo_segment_detach_fifo(fifo_segment_t *fs, svm_fifo_t *f)
Definition: fifo_segment.c:839
uword baseva
base VA for this chunk
Definition: valloc.h:32
#define SESSION_DBG(_fmt, _args...)
static void clib_rwlock_writer_unlock(clib_rwlock_t *p)
Definition: lock.h:206
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:144
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:219
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 void sm_lock_and_del_segment_inline(segment_manager_t *sm, u32 fs_index)
Removes segment after acquiring writer lock.
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
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:298
static u8 vlib_thread_is_main_w_barrier(void)
Definition: threads.h:530
int segment_manager_alloc_session_fifos(segment_manager_t *sm, u32 thread_index, svm_fifo_t **rx_fifo, svm_fifo_t **tx_fifo)
application_t * application_get(u32 app_index)
Definition: application.c:426
int segment_manager_init(segment_manager_t *sm)
void svm_queue_free(svm_queue_t *q)
Definition: queue.c:91
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:158
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:1317
#define ASSERT(truth)
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:696
void app_worker_del_detached_sm(app_worker_t *app_wrk, u32 sm_index)
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:793
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)
session_cb_vft_t cb_fns
Callbacks: shoulder-taps for the server/client.
Definition: application.h:104
void segment_manager_lock_and_del_segment(segment_manager_t *sm, u32 fs_index)
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:321
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u8 * name
Definition: ssvm.h:87
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
void vlib_rpc_call_main_thread(void *callback, u8 *args, u32 arg_size)
Definition: threads.c:1945
u8 * name
Name registered by builtin apps.
Definition: application.h:113
static uword max_log2(uword x)
Definition: clib.h:209
void session_close(session_t *s)
Initialize session closing procedure.
Definition: session.c:1392
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.
u32 index
Definition: flow_types.api:221
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:885
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
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:377
int(* fifo_tuning_callback)(session_t *s, svm_fifo_t *f, session_ft_action_t act, u32 bytes)
Delegate fifo-tuning-logic to application.
#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)
__clib_export 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
void fifo_segment_attach_fifo(fifo_segment_t *fs, svm_fifo_t *f, u32 slice_index)
Definition: fifo_segment.c:862
static fifo_segment_t * segment_manager_get_segment_if_valid(segment_manager_t *sm, u32 segment_index)
u32 api_client_index
API index for the worker.
Definition: application.h:63
uword fifo_segment_available_bytes(fifo_segment_t *fs)
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:233
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:170
ssvm_segment_type_t ssvm_type(const ssvm_private_t *ssvm)
Definition: ssvm.c:451
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:127