1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Ericsson AB
7 #ifdef DSW_SORT_DEQUEUED
14 #include <rte_atomic.h>
15 #include <rte_cycles.h>
16 #include <rte_memcpy.h>
17 #include <rte_random.h>
20 dsw_port_acquire_credits(struct dsw_evdev *dsw, struct dsw_port *port,
23 int32_t inflight_credits = port->inflight_credits;
24 int32_t missing_credits = credits - inflight_credits;
25 int32_t total_on_loan;
27 int32_t acquired_credits;
28 int32_t new_total_on_loan;
30 if (likely(missing_credits <= 0)) {
31 port->inflight_credits -= credits;
35 total_on_loan = rte_atomic32_read(&dsw->credits_on_loan);
36 available = dsw->max_inflight - total_on_loan;
37 acquired_credits = RTE_MAX(missing_credits, DSW_PORT_MIN_CREDITS);
39 if (available < acquired_credits)
42 /* This is a race, no locks are involved, and thus some other
43 * thread can allocate tokens in between the check and the
46 new_total_on_loan = rte_atomic32_add_return(&dsw->credits_on_loan,
49 if (unlikely(new_total_on_loan > dsw->max_inflight)) {
50 /* Some other port took the last credits */
51 rte_atomic32_sub(&dsw->credits_on_loan, acquired_credits);
55 DSW_LOG_DP_PORT(DEBUG, port->id, "Acquired %d tokens from pool.\n",
58 port->inflight_credits += acquired_credits;
59 port->inflight_credits -= credits;
65 dsw_port_return_credits(struct dsw_evdev *dsw, struct dsw_port *port,
68 port->inflight_credits += credits;
70 if (unlikely(port->inflight_credits > DSW_PORT_MAX_CREDITS)) {
71 int32_t leave_credits = DSW_PORT_MIN_CREDITS;
72 int32_t return_credits =
73 port->inflight_credits - leave_credits;
75 port->inflight_credits = leave_credits;
77 rte_atomic32_sub(&dsw->credits_on_loan, return_credits);
79 DSW_LOG_DP_PORT(DEBUG, port->id,
80 "Returned %d tokens to pool.\n",
86 dsw_port_load_record(struct dsw_port *port, unsigned int dequeued)
88 if (dequeued > 0 && port->busy_start == 0)
89 /* work period begins */
90 port->busy_start = rte_get_timer_cycles();
91 else if (dequeued == 0 && port->busy_start > 0) {
92 /* work period ends */
93 uint64_t work_period =
94 rte_get_timer_cycles() - port->busy_start;
95 port->busy_cycles += work_period;
101 dsw_port_load_close_period(struct dsw_port *port, uint64_t now)
103 uint64_t passed = now - port->measurement_start;
104 uint64_t busy_cycles = port->busy_cycles;
106 if (port->busy_start > 0) {
107 busy_cycles += (now - port->busy_start);
108 port->busy_start = now;
111 int16_t load = (DSW_MAX_LOAD * busy_cycles) / passed;
113 port->measurement_start = now;
114 port->busy_cycles = 0;
116 port->total_busy_cycles += busy_cycles;
122 dsw_port_load_update(struct dsw_port *port, uint64_t now)
128 old_load = rte_atomic16_read(&port->load);
130 period_load = dsw_port_load_close_period(port, now);
132 new_load = (period_load + old_load*DSW_OLD_LOAD_WEIGHT) /
133 (DSW_OLD_LOAD_WEIGHT+1);
135 rte_atomic16_set(&port->load, new_load);
139 dsw_port_consider_load_update(struct dsw_port *port, uint64_t now)
141 if (now < port->next_load_update)
144 port->next_load_update = now + port->load_update_interval;
146 dsw_port_load_update(port, now);
150 dsw_port_ctl_enqueue(struct dsw_port *port, struct dsw_ctl_msg *msg)
154 memcpy(&raw_msg, msg, sizeof(*msg));
156 /* there's always room on the ring */
157 while (rte_ring_enqueue(port->ctl_in_ring, raw_msg) != 0)
162 dsw_port_ctl_dequeue(struct dsw_port *port, struct dsw_ctl_msg *msg)
167 rc = rte_ring_dequeue(port->ctl_in_ring, &raw_msg);
170 memcpy(msg, &raw_msg, sizeof(*msg));
176 dsw_port_ctl_broadcast(struct dsw_evdev *dsw, struct dsw_port *source_port,
177 uint8_t type, uint8_t queue_id, uint16_t flow_hash)
180 struct dsw_ctl_msg msg = {
182 .originating_port_id = source_port->id,
183 .queue_id = queue_id,
184 .flow_hash = flow_hash
187 for (port_id = 0; port_id < dsw->num_ports; port_id++)
188 if (port_id != source_port->id)
189 dsw_port_ctl_enqueue(&dsw->ports[port_id], &msg);
193 dsw_port_is_flow_paused(struct dsw_port *port, uint8_t queue_id,
198 for (i = 0; i < port->paused_flows_len; i++) {
199 struct dsw_queue_flow *qf = &port->paused_flows[i];
200 if (qf->queue_id == queue_id &&
201 qf->flow_hash == flow_hash)
208 dsw_port_add_paused_flow(struct dsw_port *port, uint8_t queue_id,
209 uint16_t paused_flow_hash)
211 port->paused_flows[port->paused_flows_len] = (struct dsw_queue_flow) {
212 .queue_id = queue_id,
213 .flow_hash = paused_flow_hash
215 port->paused_flows_len++;
219 dsw_port_remove_paused_flow(struct dsw_port *port, uint8_t queue_id,
220 uint16_t paused_flow_hash)
224 for (i = 0; i < port->paused_flows_len; i++) {
225 struct dsw_queue_flow *qf = &port->paused_flows[i];
227 if (qf->queue_id == queue_id &&
228 qf->flow_hash == paused_flow_hash) {
229 uint16_t last_idx = port->paused_flows_len-1;
231 port->paused_flows[i] =
232 port->paused_flows[last_idx];
233 port->paused_flows_len--;
240 dsw_port_flush_out_buffers(struct dsw_evdev *dsw, struct dsw_port *source_port);
243 dsw_port_handle_pause_flow(struct dsw_evdev *dsw, struct dsw_port *port,
244 uint8_t originating_port_id, uint8_t queue_id,
245 uint16_t paused_flow_hash)
247 struct dsw_ctl_msg cfm = {
249 .originating_port_id = port->id,
250 .queue_id = queue_id,
251 .flow_hash = paused_flow_hash
254 DSW_LOG_DP_PORT(DEBUG, port->id, "Pausing queue_id %d flow_hash %d.\n",
255 queue_id, paused_flow_hash);
257 /* There might be already-scheduled events belonging to the
258 * paused flow in the output buffers.
260 dsw_port_flush_out_buffers(dsw, port);
262 dsw_port_add_paused_flow(port, queue_id, paused_flow_hash);
264 /* Make sure any stores to the original port's in_ring is seen
265 * before the ctl message.
269 dsw_port_ctl_enqueue(&dsw->ports[originating_port_id], &cfm);
273 dsw_find_lowest_load_port(uint8_t *port_ids, uint16_t num_port_ids,
274 uint8_t exclude_port_id, int16_t *port_loads,
275 uint8_t *target_port_id, int16_t *target_load)
277 int16_t candidate_port_id = -1;
278 int16_t candidate_load = DSW_MAX_LOAD;
281 for (i = 0; i < num_port_ids; i++) {
282 uint8_t port_id = port_ids[i];
283 if (port_id != exclude_port_id) {
284 int16_t load = port_loads[port_id];
285 if (candidate_port_id == -1 ||
286 load < candidate_load) {
287 candidate_port_id = port_id;
288 candidate_load = load;
292 *target_port_id = candidate_port_id;
293 *target_load = candidate_load;
296 struct dsw_queue_flow_burst {
297 struct dsw_queue_flow queue_flow;
302 dsw_cmp_burst(const void *v_burst_a, const void *v_burst_b)
304 const struct dsw_queue_flow_burst *burst_a = v_burst_a;
305 const struct dsw_queue_flow_burst *burst_b = v_burst_b;
307 int a_count = burst_a->count;
308 int b_count = burst_b->count;
310 return a_count - b_count;
313 #define DSW_QF_TO_INT(_qf) \
314 ((int)((((_qf)->queue_id)<<16)|((_qf)->flow_hash)))
317 dsw_cmp_qf(const void *v_qf_a, const void *v_qf_b)
319 const struct dsw_queue_flow *qf_a = v_qf_a;
320 const struct dsw_queue_flow *qf_b = v_qf_b;
322 return DSW_QF_TO_INT(qf_a) - DSW_QF_TO_INT(qf_b);
326 dsw_sort_qfs_to_bursts(struct dsw_queue_flow *qfs, uint16_t qfs_len,
327 struct dsw_queue_flow_burst *bursts)
330 struct dsw_queue_flow_burst *current_burst = NULL;
331 uint16_t num_bursts = 0;
333 /* We don't need the stable property, and the list is likely
334 * large enough for qsort() to outperform dsw_stable_sort(),
335 * so we use qsort() here.
337 qsort(qfs, qfs_len, sizeof(qfs[0]), dsw_cmp_qf);
339 /* arrange the (now-consecutive) events into bursts */
340 for (i = 0; i < qfs_len; i++) {
342 dsw_cmp_qf(&qfs[i], ¤t_burst->queue_flow) != 0) {
343 current_burst = &bursts[num_bursts];
344 current_burst->queue_flow = qfs[i];
345 current_burst->count = 0;
348 current_burst->count++;
351 qsort(bursts, num_bursts, sizeof(bursts[0]), dsw_cmp_burst);
357 dsw_retrieve_port_loads(struct dsw_evdev *dsw, int16_t *port_loads,
360 bool below_limit = false;
363 for (i = 0; i < dsw->num_ports; i++) {
364 int16_t load = rte_atomic16_read(&dsw->ports[i].load);
365 if (load < load_limit)
367 port_loads[i] = load;
373 dsw_select_migration_target(struct dsw_evdev *dsw,
374 struct dsw_port *source_port,
375 struct dsw_queue_flow_burst *bursts,
376 uint16_t num_bursts, int16_t *port_loads,
377 int16_t max_load, struct dsw_queue_flow *target_qf,
378 uint8_t *target_port_id)
380 uint16_t source_load = port_loads[source_port->id];
383 for (i = 0; i < num_bursts; i++) {
384 struct dsw_queue_flow *qf = &bursts[i].queue_flow;
386 if (dsw_port_is_flow_paused(source_port, qf->queue_id,
390 struct dsw_queue *queue = &dsw->queues[qf->queue_id];
393 dsw_find_lowest_load_port(queue->serving_ports,
394 queue->num_serving_ports,
395 source_port->id, port_loads,
396 target_port_id, &target_load);
398 if (target_load < source_load &&
399 target_load < max_load) {
405 DSW_LOG_DP_PORT(DEBUG, source_port->id, "For the %d flows considered, "
406 "no target port found with load less than %d.\n",
407 num_bursts, DSW_LOAD_TO_PERCENT(max_load));
413 dsw_schedule(struct dsw_evdev *dsw, uint8_t queue_id, uint16_t flow_hash)
415 struct dsw_queue *queue = &dsw->queues[queue_id];
418 if (queue->num_serving_ports > 1)
419 port_id = queue->flow_to_port_map[flow_hash];
421 /* A single-link queue, or atomic/ordered/parallel but
422 * with just a single serving port.
424 port_id = queue->serving_ports[0];
426 DSW_LOG_DP(DEBUG, "Event with queue_id %d flow_hash %d is scheduled "
427 "to port %d.\n", queue_id, flow_hash, port_id);
433 dsw_port_transmit_buffered(struct dsw_evdev *dsw, struct dsw_port *source_port,
434 uint8_t dest_port_id)
436 struct dsw_port *dest_port = &(dsw->ports[dest_port_id]);
437 uint16_t *buffer_len = &source_port->out_buffer_len[dest_port_id];
438 struct rte_event *buffer = source_port->out_buffer[dest_port_id];
439 uint16_t enqueued = 0;
441 if (*buffer_len == 0)
444 /* The rings are dimensioned to fit all in-flight events (even
445 * on a single ring), so looping will work.
449 rte_event_ring_enqueue_burst(dest_port->in_ring,
451 *buffer_len-enqueued,
453 } while (unlikely(enqueued != *buffer_len));
459 dsw_port_get_parallel_flow_id(struct dsw_port *port)
461 uint16_t flow_id = port->next_parallel_flow_id;
463 port->next_parallel_flow_id =
464 (port->next_parallel_flow_id + 1) % DSW_PARALLEL_FLOWS;
470 dsw_port_buffer_paused(struct dsw_port *port,
471 const struct rte_event *paused_event)
473 port->paused_events[port->paused_events_len] = *paused_event;
474 port->paused_events_len++;
478 dsw_port_buffer_non_paused(struct dsw_evdev *dsw, struct dsw_port *source_port,
479 uint8_t dest_port_id, const struct rte_event *event)
481 struct rte_event *buffer = source_port->out_buffer[dest_port_id];
482 uint16_t *buffer_len = &source_port->out_buffer_len[dest_port_id];
484 if (*buffer_len == DSW_MAX_PORT_OUT_BUFFER)
485 dsw_port_transmit_buffered(dsw, source_port, dest_port_id);
487 buffer[*buffer_len] = *event;
492 #define DSW_FLOW_ID_BITS (24)
494 dsw_flow_id_hash(uint32_t flow_id)
500 hash ^= ((flow_id >> offset) & DSW_MAX_FLOWS_MASK);
501 offset += DSW_MAX_FLOWS_BITS;
502 } while (offset < DSW_FLOW_ID_BITS);
508 dsw_port_buffer_parallel(struct dsw_evdev *dsw, struct dsw_port *source_port,
509 struct rte_event event)
511 uint8_t dest_port_id;
513 event.flow_id = dsw_port_get_parallel_flow_id(source_port);
515 dest_port_id = dsw_schedule(dsw, event.queue_id,
516 dsw_flow_id_hash(event.flow_id));
518 dsw_port_buffer_non_paused(dsw, source_port, dest_port_id, &event);
522 dsw_port_buffer_event(struct dsw_evdev *dsw, struct dsw_port *source_port,
523 const struct rte_event *event)
526 uint8_t dest_port_id;
528 if (unlikely(dsw->queues[event->queue_id].schedule_type ==
529 RTE_SCHED_TYPE_PARALLEL)) {
530 dsw_port_buffer_parallel(dsw, source_port, *event);
534 flow_hash = dsw_flow_id_hash(event->flow_id);
536 if (unlikely(dsw_port_is_flow_paused(source_port, event->queue_id,
538 dsw_port_buffer_paused(source_port, event);
542 dest_port_id = dsw_schedule(dsw, event->queue_id, flow_hash);
544 dsw_port_buffer_non_paused(dsw, source_port, dest_port_id, event);
548 dsw_port_flush_paused_events(struct dsw_evdev *dsw,
549 struct dsw_port *source_port,
550 uint8_t queue_id, uint16_t paused_flow_hash)
552 uint16_t paused_events_len = source_port->paused_events_len;
553 struct rte_event paused_events[paused_events_len];
554 uint8_t dest_port_id;
557 if (paused_events_len == 0)
560 if (dsw_port_is_flow_paused(source_port, queue_id, paused_flow_hash))
563 rte_memcpy(paused_events, source_port->paused_events,
564 paused_events_len * sizeof(struct rte_event));
566 source_port->paused_events_len = 0;
568 dest_port_id = dsw_schedule(dsw, queue_id, paused_flow_hash);
570 for (i = 0; i < paused_events_len; i++) {
571 struct rte_event *event = &paused_events[i];
574 flow_hash = dsw_flow_id_hash(event->flow_id);
576 if (event->queue_id == queue_id &&
577 flow_hash == paused_flow_hash)
578 dsw_port_buffer_non_paused(dsw, source_port,
579 dest_port_id, event);
581 dsw_port_buffer_paused(source_port, event);
586 dsw_port_migration_stats(struct dsw_port *port)
588 uint64_t migration_latency;
590 migration_latency = (rte_get_timer_cycles() - port->migration_start);
591 port->migration_latency += migration_latency;
596 dsw_port_end_migration(struct dsw_evdev *dsw, struct dsw_port *port)
598 uint8_t queue_id = port->migration_target_qf.queue_id;
599 uint16_t flow_hash = port->migration_target_qf.flow_hash;
601 port->migration_state = DSW_MIGRATION_STATE_IDLE;
602 port->seen_events_len = 0;
604 dsw_port_migration_stats(port);
606 if (dsw->queues[queue_id].schedule_type != RTE_SCHED_TYPE_PARALLEL) {
607 dsw_port_remove_paused_flow(port, queue_id, flow_hash);
608 dsw_port_flush_paused_events(dsw, port, queue_id, flow_hash);
611 DSW_LOG_DP_PORT(DEBUG, port->id, "Migration completed for queue_id "
612 "%d flow_hash %d.\n", queue_id, flow_hash);
616 dsw_port_consider_migration(struct dsw_evdev *dsw,
617 struct dsw_port *source_port,
620 bool any_port_below_limit;
621 struct dsw_queue_flow *seen_events = source_port->seen_events;
622 uint16_t seen_events_len = source_port->seen_events_len;
623 struct dsw_queue_flow_burst bursts[DSW_MAX_EVENTS_RECORDED];
625 int16_t source_port_load;
626 int16_t port_loads[dsw->num_ports];
628 if (now < source_port->next_migration)
631 if (dsw->num_ports == 1)
634 DSW_LOG_DP_PORT(DEBUG, source_port->id, "Considering migration.\n");
636 /* Randomize interval to avoid having all threads considering
637 * migration at the same in point in time, which might lead to
638 * all choosing the same target port.
640 source_port->next_migration = now +
641 source_port->migration_interval / 2 +
642 rte_rand() % source_port->migration_interval;
644 if (source_port->migration_state != DSW_MIGRATION_STATE_IDLE) {
645 DSW_LOG_DP_PORT(DEBUG, source_port->id,
646 "Migration already in progress.\n");
650 /* For simplicity, avoid migration in the unlikely case there
651 * is still events to consume in the in_buffer (from the last
654 if (source_port->in_buffer_len > 0) {
655 DSW_LOG_DP_PORT(DEBUG, source_port->id, "There are still "
656 "events in the input buffer.\n");
660 source_port_load = rte_atomic16_read(&source_port->load);
661 if (source_port_load < DSW_MIN_SOURCE_LOAD_FOR_MIGRATION) {
662 DSW_LOG_DP_PORT(DEBUG, source_port->id,
663 "Load %d is below threshold level %d.\n",
664 DSW_LOAD_TO_PERCENT(source_port_load),
665 DSW_LOAD_TO_PERCENT(DSW_MIN_SOURCE_LOAD_FOR_MIGRATION));
669 /* Avoid starting any expensive operations (sorting etc), in
670 * case of a scenario with all ports above the load limit.
672 any_port_below_limit =
673 dsw_retrieve_port_loads(dsw, port_loads,
674 DSW_MAX_TARGET_LOAD_FOR_MIGRATION);
675 if (!any_port_below_limit) {
676 DSW_LOG_DP_PORT(DEBUG, source_port->id,
677 "Candidate target ports are all too highly "
682 /* Sort flows into 'bursts' to allow attempting to migrating
683 * small (but still active) flows first - this it to avoid
684 * having large flows moving around the worker cores too much
685 * (to avoid cache misses, among other things). Of course, the
686 * number of recorded events (queue+flow ids) are limited, and
687 * provides only a snapshot, so only so many conclusions can
688 * be drawn from this data.
690 num_bursts = dsw_sort_qfs_to_bursts(seen_events, seen_events_len,
692 /* For non-big-little systems, there's no point in moving the
695 if (num_bursts < 2) {
696 DSW_LOG_DP_PORT(DEBUG, source_port->id, "Only a single flow "
697 "queue_id %d flow_hash %d has been seen.\n",
698 bursts[0].queue_flow.queue_id,
699 bursts[0].queue_flow.flow_hash);
703 /* The strategy is to first try to find a flow to move to a
704 * port with low load (below the migration-attempt
705 * threshold). If that fails, we try to find a port which is
706 * below the max threshold, and also less loaded than this
709 if (!dsw_select_migration_target(dsw, source_port, bursts, num_bursts,
711 DSW_MIN_SOURCE_LOAD_FOR_MIGRATION,
712 &source_port->migration_target_qf,
713 &source_port->migration_target_port_id)
715 !dsw_select_migration_target(dsw, source_port, bursts, num_bursts,
717 DSW_MAX_TARGET_LOAD_FOR_MIGRATION,
718 &source_port->migration_target_qf,
719 &source_port->migration_target_port_id))
722 DSW_LOG_DP_PORT(DEBUG, source_port->id, "Migrating queue_id %d "
723 "flow_hash %d from port %d to port %d.\n",
724 source_port->migration_target_qf.queue_id,
725 source_port->migration_target_qf.flow_hash,
726 source_port->id, source_port->migration_target_port_id);
728 /* We have a winner. */
730 source_port->migration_state = DSW_MIGRATION_STATE_PAUSING;
731 source_port->migration_start = rte_get_timer_cycles();
733 /* No need to go through the whole pause procedure for
734 * parallel queues, since atomic/ordered semantics need not to
738 if (dsw->queues[source_port->migration_target_qf.queue_id].schedule_type
739 == RTE_SCHED_TYPE_PARALLEL) {
740 uint8_t queue_id = source_port->migration_target_qf.queue_id;
741 uint16_t flow_hash = source_port->migration_target_qf.flow_hash;
742 uint8_t dest_port_id = source_port->migration_target_port_id;
744 /* Single byte-sized stores are always atomic. */
745 dsw->queues[queue_id].flow_to_port_map[flow_hash] =
749 dsw_port_end_migration(dsw, source_port);
754 /* There might be 'loopback' events already scheduled in the
757 dsw_port_flush_out_buffers(dsw, source_port);
759 dsw_port_add_paused_flow(source_port,
760 source_port->migration_target_qf.queue_id,
761 source_port->migration_target_qf.flow_hash);
763 dsw_port_ctl_broadcast(dsw, source_port, DSW_CTL_PAUS_REQ,
764 source_port->migration_target_qf.queue_id,
765 source_port->migration_target_qf.flow_hash);
766 source_port->cfm_cnt = 0;
770 dsw_port_flush_paused_events(struct dsw_evdev *dsw,
771 struct dsw_port *source_port,
772 uint8_t queue_id, uint16_t paused_flow_hash);
775 dsw_port_handle_unpause_flow(struct dsw_evdev *dsw, struct dsw_port *port,
776 uint8_t originating_port_id, uint8_t queue_id,
777 uint16_t paused_flow_hash)
779 struct dsw_ctl_msg cfm = {
781 .originating_port_id = port->id,
782 .queue_id = queue_id,
783 .flow_hash = paused_flow_hash
786 DSW_LOG_DP_PORT(DEBUG, port->id, "Un-pausing queue_id %d flow_hash %d.\n",
787 queue_id, paused_flow_hash);
789 dsw_port_remove_paused_flow(port, queue_id, paused_flow_hash);
793 dsw_port_ctl_enqueue(&dsw->ports[originating_port_id], &cfm);
795 dsw_port_flush_paused_events(dsw, port, queue_id, paused_flow_hash);
798 #define FORWARD_BURST_SIZE (32)
801 dsw_port_forward_migrated_flow(struct dsw_port *source_port,
802 struct rte_event_ring *dest_ring,
806 uint16_t events_left;
808 /* Control ring message should been seen before the ring count
809 * is read on the port's in_ring.
813 events_left = rte_event_ring_count(source_port->in_ring);
815 while (events_left > 0) {
816 uint16_t in_burst_size =
817 RTE_MIN(FORWARD_BURST_SIZE, events_left);
818 struct rte_event in_burst[in_burst_size];
822 in_len = rte_event_ring_dequeue_burst(source_port->in_ring,
824 in_burst_size, NULL);
825 /* No need to care about bursting forwarded events (to
826 * the destination port's in_ring), since migration
827 * doesn't happen very often, and also the majority of
828 * the dequeued events will likely *not* be forwarded.
830 for (i = 0; i < in_len; i++) {
831 struct rte_event *e = &in_burst[i];
832 if (e->queue_id == queue_id &&
833 dsw_flow_id_hash(e->flow_id) == flow_hash) {
834 while (rte_event_ring_enqueue_burst(dest_ring,
839 uint16_t last_idx = source_port->in_buffer_len;
840 source_port->in_buffer[last_idx] = *e;
841 source_port->in_buffer_len++;
845 events_left -= in_len;
850 dsw_port_move_migrating_flow(struct dsw_evdev *dsw,
851 struct dsw_port *source_port)
853 uint8_t queue_id = source_port->migration_target_qf.queue_id;
854 uint16_t flow_hash = source_port->migration_target_qf.flow_hash;
855 uint8_t dest_port_id = source_port->migration_target_port_id;
856 struct dsw_port *dest_port = &dsw->ports[dest_port_id];
858 dsw_port_flush_out_buffers(dsw, source_port);
862 dsw->queues[queue_id].flow_to_port_map[flow_hash] =
865 dsw_port_forward_migrated_flow(source_port, dest_port->in_ring,
866 queue_id, flow_hash);
868 /* Flow table update and migration destination port's enqueues
869 * must be seen before the control message.
873 dsw_port_ctl_broadcast(dsw, source_port, DSW_CTL_UNPAUS_REQ, queue_id,
875 source_port->cfm_cnt = 0;
876 source_port->migration_state = DSW_MIGRATION_STATE_UNPAUSING;
880 dsw_port_handle_confirm(struct dsw_evdev *dsw, struct dsw_port *port)
884 if (port->cfm_cnt == (dsw->num_ports-1)) {
885 switch (port->migration_state) {
886 case DSW_MIGRATION_STATE_PAUSING:
887 DSW_LOG_DP_PORT(DEBUG, port->id, "Going into forwarding "
888 "migration state.\n");
889 port->migration_state = DSW_MIGRATION_STATE_FORWARDING;
891 case DSW_MIGRATION_STATE_UNPAUSING:
892 dsw_port_end_migration(dsw, port);
902 dsw_port_ctl_process(struct dsw_evdev *dsw, struct dsw_port *port)
904 struct dsw_ctl_msg msg;
906 /* So any table loads happens before the ring dequeue, in the
907 * case of a 'paus' message.
911 if (dsw_port_ctl_dequeue(port, &msg) == 0) {
913 case DSW_CTL_PAUS_REQ:
914 dsw_port_handle_pause_flow(dsw, port,
915 msg.originating_port_id,
916 msg.queue_id, msg.flow_hash);
918 case DSW_CTL_UNPAUS_REQ:
919 dsw_port_handle_unpause_flow(dsw, port,
920 msg.originating_port_id,
925 dsw_port_handle_confirm(dsw, port);
932 dsw_port_note_op(struct dsw_port *port, uint16_t num_events)
934 /* To pull the control ring reasonbly often on busy ports,
935 * each dequeued/enqueued event is considered an 'op' too.
937 port->ops_since_bg_task += (num_events+1);
941 dsw_port_bg_process(struct dsw_evdev *dsw, struct dsw_port *port)
943 if (unlikely(port->migration_state == DSW_MIGRATION_STATE_FORWARDING &&
944 port->pending_releases == 0))
945 dsw_port_move_migrating_flow(dsw, port);
947 /* Polling the control ring is relatively inexpensive, and
948 * polling it often helps bringing down migration latency, so
949 * do this for every iteration.
951 dsw_port_ctl_process(dsw, port);
953 /* To avoid considering migration and flushing output buffers
954 * on every dequeue/enqueue call, the scheduler only performs
955 * such 'background' tasks every nth
956 * (i.e. DSW_MAX_PORT_OPS_PER_BG_TASK) operation.
958 if (unlikely(port->ops_since_bg_task >= DSW_MAX_PORT_OPS_PER_BG_TASK)) {
961 now = rte_get_timer_cycles();
965 /* Logic to avoid having events linger in the output
968 dsw_port_flush_out_buffers(dsw, port);
970 dsw_port_consider_load_update(port, now);
972 dsw_port_consider_migration(dsw, port, now);
974 port->ops_since_bg_task = 0;
979 dsw_port_flush_out_buffers(struct dsw_evdev *dsw, struct dsw_port *source_port)
981 uint16_t dest_port_id;
983 for (dest_port_id = 0; dest_port_id < dsw->num_ports; dest_port_id++)
984 dsw_port_transmit_buffered(dsw, source_port, dest_port_id);
988 dsw_event_enqueue(void *port, const struct rte_event *ev)
990 return dsw_event_enqueue_burst(port, ev, unlikely(ev == NULL) ? 0 : 1);
993 static __rte_always_inline uint16_t
994 dsw_event_enqueue_burst_generic(void *port, const struct rte_event events[],
995 uint16_t events_len, bool op_types_known,
996 uint16_t num_new, uint16_t num_release,
997 uint16_t num_non_release)
999 struct dsw_port *source_port = port;
1000 struct dsw_evdev *dsw = source_port->dsw;
1001 bool enough_credits;
1004 DSW_LOG_DP_PORT(DEBUG, source_port->id, "Attempting to enqueue %d "
1005 "events to port %d.\n", events_len, source_port->id);
1007 dsw_port_bg_process(dsw, source_port);
1009 /* XXX: For performance (=ring efficiency) reasons, the
1010 * scheduler relies on internal non-ring buffers instead of
1011 * immediately sending the event to the destination ring. For
1012 * a producer that doesn't intend to produce or consume any
1013 * more events, the scheduler provides a way to flush the
1014 * buffer, by means of doing an enqueue of zero events. In
1015 * addition, a port cannot be left "unattended" (e.g. unused)
1016 * for long periods of time, since that would stall
1017 * migration. Eventdev API extensions to provide a cleaner way
1018 * to archieve both of these functions should be
1021 if (unlikely(events_len == 0)) {
1022 dsw_port_note_op(source_port, DSW_MAX_PORT_OPS_PER_BG_TASK);
1026 if (unlikely(events_len > source_port->enqueue_depth))
1027 events_len = source_port->enqueue_depth;
1029 dsw_port_note_op(source_port, events_len);
1031 if (!op_types_known)
1032 for (i = 0; i < events_len; i++) {
1033 switch (events[i].op) {
1034 case RTE_EVENT_OP_RELEASE:
1037 case RTE_EVENT_OP_NEW:
1039 /* Falls through. */
1046 /* Technically, we could allow the non-new events up to the
1047 * first new event in the array into the system, but for
1048 * simplicity reasons, we deny the whole burst if the port is
1049 * above the water mark.
1051 if (unlikely(num_new > 0 && rte_atomic32_read(&dsw->credits_on_loan) >
1052 source_port->new_event_threshold))
1055 enough_credits = dsw_port_acquire_credits(dsw, source_port,
1057 if (unlikely(!enough_credits))
1060 source_port->pending_releases -= num_release;
1062 for (i = 0; i < events_len; i++) {
1063 const struct rte_event *event = &events[i];
1065 if (likely(num_release == 0 ||
1066 event->op != RTE_EVENT_OP_RELEASE))
1067 dsw_port_buffer_event(dsw, source_port, event);
1070 DSW_LOG_DP_PORT(DEBUG, source_port->id, "%d non-release events "
1071 "accepted.\n", num_non_release);
1073 return num_non_release;
1077 dsw_event_enqueue_burst(void *port, const struct rte_event events[],
1078 uint16_t events_len)
1080 return dsw_event_enqueue_burst_generic(port, events, events_len, false,
1085 dsw_event_enqueue_new_burst(void *port, const struct rte_event events[],
1086 uint16_t events_len)
1088 return dsw_event_enqueue_burst_generic(port, events, events_len, true,
1089 events_len, 0, events_len);
1093 dsw_event_enqueue_forward_burst(void *port, const struct rte_event events[],
1094 uint16_t events_len)
1096 return dsw_event_enqueue_burst_generic(port, events, events_len, true,
1101 dsw_event_dequeue(void *port, struct rte_event *events, uint64_t wait)
1103 return dsw_event_dequeue_burst(port, events, 1, wait);
1107 dsw_port_record_seen_events(struct dsw_port *port, struct rte_event *events,
1112 for (i = 0; i < num; i++) {
1113 uint16_t l_idx = port->seen_events_idx;
1114 struct dsw_queue_flow *qf = &port->seen_events[l_idx];
1115 struct rte_event *event = &events[i];
1116 qf->queue_id = event->queue_id;
1117 qf->flow_hash = dsw_flow_id_hash(event->flow_id);
1119 port->seen_events_idx = (l_idx+1) % DSW_MAX_EVENTS_RECORDED;
1122 if (unlikely(port->seen_events_len != DSW_MAX_EVENTS_RECORDED))
1123 port->seen_events_len =
1124 RTE_MIN(port->seen_events_len + num,
1125 DSW_MAX_EVENTS_RECORDED);
1128 #ifdef DSW_SORT_DEQUEUED
1130 #define DSW_EVENT_TO_INT(_event) \
1131 ((int)((((_event)->queue_id)<<16)|((_event)->flow_id)))
1134 dsw_cmp_event(const void *v_event_a, const void *v_event_b)
1136 const struct rte_event *event_a = v_event_a;
1137 const struct rte_event *event_b = v_event_b;
1139 return DSW_EVENT_TO_INT(event_a) - DSW_EVENT_TO_INT(event_b);
1144 dsw_port_dequeue_burst(struct dsw_port *port, struct rte_event *events,
1147 struct dsw_port *source_port = port;
1148 struct dsw_evdev *dsw = source_port->dsw;
1150 dsw_port_ctl_process(dsw, source_port);
1152 if (unlikely(port->in_buffer_len > 0)) {
1153 uint16_t dequeued = RTE_MIN(num, port->in_buffer_len);
1155 rte_memcpy(events, &port->in_buffer[port->in_buffer_start],
1156 dequeued * sizeof(struct rte_event));
1158 port->in_buffer_start += dequeued;
1159 port->in_buffer_len -= dequeued;
1161 if (port->in_buffer_len == 0)
1162 port->in_buffer_start = 0;
1167 return rte_event_ring_dequeue_burst(port->in_ring, events, num, NULL);
1171 dsw_event_dequeue_burst(void *port, struct rte_event *events, uint16_t num,
1172 uint64_t wait __rte_unused)
1174 struct dsw_port *source_port = port;
1175 struct dsw_evdev *dsw = source_port->dsw;
1178 source_port->pending_releases = 0;
1180 dsw_port_bg_process(dsw, source_port);
1182 if (unlikely(num > source_port->dequeue_depth))
1183 num = source_port->dequeue_depth;
1185 dequeued = dsw_port_dequeue_burst(source_port, events, num);
1187 source_port->pending_releases = dequeued;
1189 dsw_port_load_record(source_port, dequeued);
1191 dsw_port_note_op(source_port, dequeued);
1194 DSW_LOG_DP_PORT(DEBUG, source_port->id, "Dequeued %d events.\n",
1197 dsw_port_return_credits(dsw, source_port, dequeued);
1199 /* One potential optimization one might think of is to
1200 * add a migration state (prior to 'pausing'), and
1201 * only record seen events when the port is in this
1202 * state (and transit to 'pausing' when enough events
1203 * have been gathered). However, that schema doesn't
1204 * seem to improve performance.
1206 dsw_port_record_seen_events(port, events, dequeued);
1208 /* XXX: Assuming the port can't produce any more work,
1209 * consider flushing the output buffer, on dequeued ==
1213 #ifdef DSW_SORT_DEQUEUED
1214 dsw_stable_sort(events, dequeued, sizeof(events[0]), dsw_cmp_event);