Skip to content

Commit c95cfa2

Browse files
committed
svm: support for multi-segment enqueues
Type: feature Signed-off-by: Florin Coras <fcoras@cisco.com> Change-Id: I06c7022a6afbb146b23cbd3a430497ec9e8be73d
1 parent 6155902 commit c95cfa2

File tree

4 files changed

+142
-23
lines changed

4 files changed

+142
-23
lines changed

src/svm/svm_fifo.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,87 @@ svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len)
955955
clib_atomic_store_rel_n (&f->tail, tail);
956956
}
957957

958+
int
959+
svm_fifo_enqueue_segments (svm_fifo_t * f, const svm_fifo_seg_t segs[],
960+
u32 n_segs, u8 allow_partial)
961+
{
962+
u32 tail, head, free_count, len = 0, i;
963+
svm_fifo_chunk_t *old_tail_c;
964+
965+
f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
966+
967+
f_load_head_tail_prod (f, &head, &tail);
968+
969+
/* free space in fifo can only increase during enqueue: SPSC */
970+
free_count = f_free_count (f, head, tail);
971+
972+
if (PREDICT_FALSE (free_count == 0))
973+
return SVM_FIFO_EFULL;
974+
975+
for (i = 0; i < n_segs; i++)
976+
len += segs[i].len;
977+
978+
old_tail_c = f->tail_chunk;
979+
980+
if (!allow_partial)
981+
{
982+
if (PREDICT_FALSE (free_count < len))
983+
return SVM_FIFO_EFULL;
984+
985+
if (f_pos_gt (tail + len, f_chunk_end (f->end_chunk)))
986+
{
987+
if (PREDICT_FALSE (f_try_chunk_alloc (f, head, tail, len)))
988+
return SVM_FIFO_EGROW;
989+
}
990+
991+
for (i = 0; i < n_segs; i++)
992+
{
993+
svm_fifo_copy_to_chunk (f, f->tail_chunk, tail, segs[i].data,
994+
segs[i].len, &f->tail_chunk);
995+
tail += segs[i].len;
996+
}
997+
}
998+
else
999+
{
1000+
len = clib_min (free_count, len);
1001+
1002+
if (f_pos_gt (tail + len, f_chunk_end (f->end_chunk)))
1003+
{
1004+
if (PREDICT_FALSE (f_try_chunk_alloc (f, head, tail, len)))
1005+
{
1006+
len = f_chunk_end (f->end_chunk) - tail;
1007+
if (!len)
1008+
return SVM_FIFO_EGROW;
1009+
}
1010+
}
1011+
1012+
i = 0;
1013+
while (len)
1014+
{
1015+
u32 to_copy = clib_min (segs[i].len, len);
1016+
svm_fifo_copy_to_chunk (f, f->tail_chunk, tail, segs[i].data,
1017+
to_copy, &f->tail_chunk);
1018+
len -= to_copy;
1019+
tail += to_copy;
1020+
i++;
1021+
}
1022+
}
1023+
1024+
/* collect out-of-order segments */
1025+
if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
1026+
{
1027+
len += ooo_segment_try_collect (f, len, &tail);
1028+
/* Tail chunk might've changed even if nothing was collected */
1029+
f->tail_chunk = f_lookup_clear_enq_chunks (f, old_tail_c, tail);
1030+
f->ooo_enq = 0;
1031+
}
1032+
1033+
/* store-rel: producer owned index (paired with load-acq in consumer) */
1034+
clib_atomic_store_rel_n (&f->tail, tail);
1035+
1036+
return len;
1037+
}
1038+
9581039
always_inline svm_fifo_chunk_t *
9591040
f_unlink_chunks (svm_fifo_t * f, u32 end_pos, u8 maybe_ooo)
9601041
{

src/svm/svm_fifo.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,17 @@ int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len,
291291
* @param len number of bytes to add to tail
292292
*/
293293
void svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len);
294+
/**
295+
* Enqueue array of @ref svm_fifo_seg_t in order
296+
*
297+
* @param f fifo
298+
* @param segs array of segments to enqueue
299+
* @param n_segs number of segments
300+
* @param allow_partial if set partial enqueues are allowed
301+
* @return len if enqueue was successful, error otherwise
302+
*/
303+
int svm_fifo_enqueue_segments (svm_fifo_t * f, const svm_fifo_seg_t segs[],
304+
u32 n_segs, u8 allow_partial);
294305
/**
295306
* Overwrite fifo head with new data
296307
*

src/vnet/session/application_interface.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -615,35 +615,35 @@ app_send_dgram_raw (svm_fifo_t * f, app_session_transport_t * at,
615615
svm_msg_q_t * vpp_evt_q, u8 * data, u32 len, u8 evt_type,
616616
u8 do_evt, u8 noblock)
617617
{
618-
u32 max_enqueue, actual_write;
619618
session_dgram_hdr_t hdr;
620619
int rv;
621620

622-
max_enqueue = svm_fifo_max_enqueue_prod (f);
623-
if (max_enqueue < (sizeof (session_dgram_hdr_t) + len))
621+
if (svm_fifo_max_enqueue_prod (f) < (sizeof (session_dgram_hdr_t) + len))
624622
return 0;
625623

626-
max_enqueue -= sizeof (session_dgram_hdr_t);
627-
actual_write = clib_min (len, max_enqueue);
628-
hdr.data_length = actual_write;
624+
hdr.data_length = len;
629625
hdr.data_offset = 0;
630626
clib_memcpy_fast (&hdr.rmt_ip, &at->rmt_ip, sizeof (ip46_address_t));
631627
hdr.is_ip4 = at->is_ip4;
632628
hdr.rmt_port = at->rmt_port;
633629
clib_memcpy_fast (&hdr.lcl_ip, &at->lcl_ip, sizeof (ip46_address_t));
634630
hdr.lcl_port = at->lcl_port;
635-
rv = svm_fifo_enqueue (f, sizeof (hdr), (u8 *) & hdr);
636-
ASSERT (rv == sizeof (hdr));
637631

638-
rv = svm_fifo_enqueue (f, actual_write, data);
632+
/* *INDENT-OFF* */
633+
svm_fifo_seg_t segs[2] = {{ (u8 *) &hdr, sizeof (hdr) }, { data, len }};
634+
/* *INDENT-ON* */
635+
636+
rv = svm_fifo_enqueue_segments (f, segs, 2, 0 /* allow partial */ );
637+
if (PREDICT_FALSE (rv < 0))
638+
return 0;
639+
639640
if (do_evt)
640641
{
641-
if (rv > 0 && svm_fifo_set_event (f))
642+
if (svm_fifo_set_event (f))
642643
app_send_io_evt_to_vpp (vpp_evt_q, f->master_session_index, evt_type,
643644
noblock);
644645
}
645-
ASSERT (rv);
646-
return rv;
646+
return len;
647647
}
648648

649649
always_inline int

src/vnet/session/session.c

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -516,22 +516,49 @@ session_enqueue_dgram_connection (session_t * s,
516516
session_dgram_hdr_t * hdr,
517517
vlib_buffer_t * b, u8 proto, u8 queue_event)
518518
{
519-
int enqueued = 0, rv, in_order_off;
519+
int rv;
520520

521521
ASSERT (svm_fifo_max_enqueue_prod (s->rx_fifo)
522522
>= b->current_length + sizeof (*hdr));
523523

524-
svm_fifo_enqueue (s->rx_fifo, sizeof (session_dgram_hdr_t), (u8 *) hdr);
525-
enqueued = svm_fifo_enqueue (s->rx_fifo, b->current_length,
526-
vlib_buffer_get_current (b));
527-
if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && enqueued >= 0))
524+
if (PREDICT_TRUE (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)))
528525
{
529-
in_order_off = enqueued > b->current_length ? enqueued : 0;
530-
rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
531-
if (rv > 0)
532-
enqueued += rv;
526+
/* *INDENT-OFF* */
527+
svm_fifo_seg_t segs[2] = {
528+
{ (u8 *) hdr, sizeof (*hdr) },
529+
{ vlib_buffer_get_current (b), b->current_length }
530+
};
531+
/* *INDENT-ON* */
532+
533+
rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, 2,
534+
0 /* allow_partial */ );
533535
}
534-
if (queue_event)
536+
else
537+
{
538+
vlib_main_t *vm = vlib_get_main ();
539+
svm_fifo_seg_t *segs = 0, *seg;
540+
vlib_buffer_t *it = b;
541+
u32 n_segs = 1;
542+
543+
vec_add2 (segs, seg, 1);
544+
seg->data = (u8 *) hdr;
545+
seg->len = sizeof (*hdr);
546+
while (it)
547+
{
548+
vec_add2 (segs, seg, 1);
549+
seg->data = vlib_buffer_get_current (it);
550+
seg->len = it->current_length;
551+
n_segs++;
552+
if (!(it->flags & VLIB_BUFFER_NEXT_PRESENT))
553+
break;
554+
it = vlib_get_buffer (vm, it->next_buffer);
555+
}
556+
rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, n_segs,
557+
0 /* allow partial */ );
558+
vec_free (segs);
559+
}
560+
561+
if (queue_event && rv > 0)
535562
{
536563
/* Queue RX event on this fifo. Eventually these will need to be flushed
537564
* by calling stream_server_flush_enqueue_events () */
@@ -546,7 +573,7 @@ session_enqueue_dgram_connection (session_t * s,
546573

547574
session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
548575
}
549-
return enqueued;
576+
return rv > 0 ? rv : 0;
550577
}
551578

552579
int

0 commit comments

Comments
 (0)