session/svm: apis for fifo shrinking

Change-Id: Ie519683bb90aae6fb95f2a09e251cded1890ed41
Signed-off-by: Florin Coras <fcoras@cisco.com>
diff --git a/src/vnet/session/segment_manager.c b/src/vnet/session/segment_manager.c
index 9a8af3b..6213cd5 100644
--- a/src/vnet/session/segment_manager.c
+++ b/src/vnet/session/segment_manager.c
@@ -682,6 +682,37 @@
   return rv;
 }
 
+int
+segment_manager_collect_fifo_chunks (segment_manager_t * sm, svm_fifo_t * f)
+{
+  fifo_segment_t *fs;
+  int rv;
+
+  fs = segment_manager_get_segment_w_lock (sm, f->segment_index);
+  rv = fifo_segment_collect_fifo_chunks (fs, f);
+  segment_manager_segment_reader_unlock (sm);
+
+  return rv;
+}
+
+int
+segment_manager_shrink_fifo (segment_manager_t * sm, svm_fifo_t * f, u32 size,
+			     u8 is_producer)
+{
+  int rv;
+
+  rv = svm_fifo_reduce_size (f, size, is_producer);
+
+  /* Nothing to collect at this point */
+  if (!is_producer)
+    return rv;
+
+  if (f->flags & SVM_FIFO_F_COLLECT_CHUNKS)
+    segment_manager_collect_fifo_chunks (sm, f);
+
+  return rv;
+}
+
 u32
 segment_manager_evt_q_expected_size (u32 q_len)
 {
diff --git a/src/vnet/session/segment_manager.h b/src/vnet/session/segment_manager.h
index cbf8e35..8358c50 100644
--- a/src/vnet/session/segment_manager.h
+++ b/src/vnet/session/segment_manager.h
@@ -125,6 +125,40 @@
  */
 int segment_manager_grow_fifo (segment_manager_t * sm, svm_fifo_t * f,
 			       u32 size);
+
+/**
+ * Request to shrink fifo owned by segment manager
+ *
+ * If this is not called by the producer, no attempt is made to reduce the
+ * size until the producer tries to enqueue more data. To collect the chunks
+ * that are to be removed call @ref segment_manager_collect_fifo_chunks
+ *
+ * Size reduction does not affect fifo chunk boundaries. Therefore chunks are
+ * not split and the amount of bytes to be removed can be equal to or less
+ * than what was requested.
+ *
+ * @param sm		segment manager that owns the fifo
+ * @param f		fifo to be shrunk
+ * @param size		amount of bytes to remove from fifo
+ * @param is_producer	flag that indicates is caller is the producer for the
+ * 			fifo.
+ * @return		actual number of bytes to be removed
+ */
+int segment_manager_shrink_fifo (segment_manager_t * sm, svm_fifo_t * f,
+				 u32 size, u8 is_producer);
+
+/**
+ * Collect fifo chunks that are no longer used
+ *
+ * This should not be called unless SVM_FIFO_F_COLLECT_CHUNKS is set for
+ * the fifo. The chunks are returned to the fifo segment freelist.
+ *
+ * @param sm		segment manager that owns the fifo
+ * @param f		fifo whose chunks are to be collected
+ * @return		0 on success, error otherwise
+ */
+int segment_manager_collect_fifo_chunks (segment_manager_t * sm,
+					 svm_fifo_t * f);
 u8 segment_manager_has_fifos (segment_manager_t * sm);
 
 svm_msg_q_t *segment_manager_alloc_queue (fifo_segment_t * fs,