1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright(c) 2019-2021 Xilinx, Inc.
4 * Copyright(c) 2019 Solarflare Communications Inc.
6 * This software was jointly developed between OKTET Labs (under contract
7 * for Solarflare) and Solarflare Communications, Inc.
10 #include <rte_service.h>
11 #include <rte_service_component.h>
14 #include "sfc_service.h"
15 #include "sfc_repr_proxy.h"
16 #include "sfc_repr_proxy_api.h"
21 #include "sfc_dp_rx.h"
24 * Amount of time to wait for the representor proxy routine (which is
25 * running on a service core) to handle a request sent via mbox.
27 #define SFC_REPR_PROXY_MBOX_POLL_TIMEOUT_MS 1000
30 * Amount of time to wait for the representor proxy routine (which is
31 * running on a service core) to terminate after service core is stopped.
33 #define SFC_REPR_PROXY_ROUTINE_TERMINATE_TIMEOUT_MS 10000
35 #define SFC_REPR_INVALID_ROUTE_PORT_ID (UINT16_MAX)
37 static struct sfc_repr_proxy *
38 sfc_repr_proxy_by_adapter(struct sfc_adapter *sa)
40 return &sa->repr_proxy;
43 static struct sfc_adapter *
44 sfc_get_adapter_by_pf_port_id(uint16_t pf_port_id)
46 struct rte_eth_dev *dev;
47 struct sfc_adapter *sa;
49 SFC_ASSERT(pf_port_id < RTE_MAX_ETHPORTS);
51 dev = &rte_eth_devices[pf_port_id];
52 sa = sfc_adapter_by_eth_dev(dev);
60 sfc_put_adapter(struct sfc_adapter *sa)
62 sfc_adapter_unlock(sa);
65 static struct sfc_repr_proxy_port *
66 sfc_repr_proxy_find_port(struct sfc_repr_proxy *rp, uint16_t repr_id)
68 struct sfc_repr_proxy_port *port;
70 TAILQ_FOREACH(port, &rp->ports, entries) {
71 if (port->repr_id == repr_id)
79 sfc_repr_proxy_mbox_send(struct sfc_repr_proxy_mbox *mbox,
80 struct sfc_repr_proxy_port *port,
81 enum sfc_repr_proxy_mbox_op op)
83 const unsigned int wait_ms = SFC_REPR_PROXY_MBOX_POLL_TIMEOUT_MS;
91 * Release ordering enforces marker set after data is populated.
92 * Paired with acquire ordering in sfc_repr_proxy_mbox_handle().
94 __atomic_store_n(&mbox->write_marker, true, __ATOMIC_RELEASE);
97 * Wait for the representor routine to process the request.
100 for (i = 0; i < wait_ms; i++) {
102 * Paired with release ordering in sfc_repr_proxy_mbox_handle()
103 * on acknowledge write.
105 if (__atomic_load_n(&mbox->ack, __ATOMIC_ACQUIRE))
113 "%s() failed to wait for representor proxy routine ack",
122 sfc_repr_proxy_mbox_handle(struct sfc_repr_proxy *rp)
124 struct sfc_repr_proxy_mbox *mbox = &rp->mbox;
127 * Paired with release ordering in sfc_repr_proxy_mbox_send()
130 if (!__atomic_load_n(&mbox->write_marker, __ATOMIC_ACQUIRE))
133 mbox->write_marker = false;
136 case SFC_REPR_PROXY_MBOX_ADD_PORT:
137 TAILQ_INSERT_TAIL(&rp->ports, mbox->port, entries);
139 case SFC_REPR_PROXY_MBOX_DEL_PORT:
140 TAILQ_REMOVE(&rp->ports, mbox->port, entries);
142 case SFC_REPR_PROXY_MBOX_START_PORT:
143 mbox->port->started = true;
145 case SFC_REPR_PROXY_MBOX_STOP_PORT:
146 mbox->port->started = false;
154 * Paired with acquire ordering in sfc_repr_proxy_mbox_send()
155 * on acknowledge read.
157 __atomic_store_n(&mbox->ack, true, __ATOMIC_RELEASE);
161 sfc_repr_proxy_handle_tx(struct sfc_repr_proxy_dp_txq *rp_txq,
162 struct sfc_repr_proxy_txq *repr_txq)
165 * With multiple representor proxy queues configured it is
166 * possible that not all of the corresponding representor
167 * queues were created. Skip the queues that do not exist.
169 if (repr_txq->ring == NULL)
172 if (rp_txq->available < RTE_DIM(rp_txq->tx_pkts)) {
174 rte_ring_sc_dequeue_burst(repr_txq->ring,
175 (void **)(&rp_txq->tx_pkts[rp_txq->available]),
176 RTE_DIM(rp_txq->tx_pkts) - rp_txq->available,
179 if (rp_txq->available == rp_txq->transmitted)
183 rp_txq->transmitted += rp_txq->pkt_burst(rp_txq->dp,
184 &rp_txq->tx_pkts[rp_txq->transmitted],
185 rp_txq->available - rp_txq->transmitted);
187 if (rp_txq->available == rp_txq->transmitted) {
188 rp_txq->available = 0;
189 rp_txq->transmitted = 0;
193 static struct sfc_repr_proxy_port *
194 sfc_repr_proxy_rx_route_mbuf(struct sfc_repr_proxy *rp, struct rte_mbuf *m)
196 struct sfc_repr_proxy_port *port;
197 efx_mport_id_t mport_id;
199 mport_id.id = *RTE_MBUF_DYNFIELD(m, sfc_dp_mport_offset,
200 typeof(&((efx_mport_id_t *)0)->id));
202 TAILQ_FOREACH(port, &rp->ports, entries) {
203 if (port->egress_mport.id == mport_id.id) {
204 m->port = port->rte_port_id;
205 m->ol_flags &= ~sfc_dp_mport_override;
214 * Returns true if a packet is encountered which should be forwarded to a
215 * port which is different from the one that is currently routed.
218 sfc_repr_proxy_rx_route(struct sfc_repr_proxy *rp,
219 struct sfc_repr_proxy_dp_rxq *rp_rxq)
223 for (i = rp_rxq->routed;
224 i < rp_rxq->available && !rp_rxq->stop_route;
225 i++, rp_rxq->routed++) {
226 struct sfc_repr_proxy_port *port;
227 struct rte_mbuf *m = rp_rxq->pkts[i];
229 port = sfc_repr_proxy_rx_route_mbuf(rp, m);
230 /* Cannot find destination representor */
232 /* Effectively drop the packet */
237 /* Currently routed packets are mapped to a different port */
238 if (port->repr_id != rp_rxq->route_port_id &&
239 rp_rxq->route_port_id != SFC_REPR_INVALID_ROUTE_PORT_ID)
242 rp_rxq->route_port_id = port->repr_id;
249 sfc_repr_proxy_rx_forward(struct sfc_repr_proxy *rp,
250 struct sfc_repr_proxy_dp_rxq *rp_rxq)
252 struct sfc_repr_proxy_port *port;
254 if (rp_rxq->route_port_id != SFC_REPR_INVALID_ROUTE_PORT_ID) {
255 port = sfc_repr_proxy_find_port(rp, rp_rxq->route_port_id);
257 if (port != NULL && port->started) {
259 rte_ring_sp_enqueue_burst(port->rxq[0].ring,
260 (void **)(&rp_rxq->pkts[rp_rxq->forwarded]),
261 rp_rxq->routed - rp_rxq->forwarded, NULL);
263 /* Drop all routed packets if the port is not started */
264 rp_rxq->forwarded = rp_rxq->routed;
268 if (rp_rxq->forwarded == rp_rxq->routed) {
269 rp_rxq->route_port_id = SFC_REPR_INVALID_ROUTE_PORT_ID;
270 rp_rxq->stop_route = false;
272 /* Stall packet routing if not all packets were forwarded */
273 rp_rxq->stop_route = true;
276 if (rp_rxq->available == rp_rxq->forwarded)
277 rp_rxq->available = rp_rxq->forwarded = rp_rxq->routed = 0;
281 sfc_repr_proxy_handle_rx(struct sfc_repr_proxy *rp,
282 struct sfc_repr_proxy_dp_rxq *rp_rxq)
286 if (rp_rxq->available < RTE_DIM(rp_rxq->pkts)) {
287 rp_rxq->available += rp_rxq->pkt_burst(rp_rxq->dp,
288 &rp_rxq->pkts[rp_rxq->available],
289 RTE_DIM(rp_rxq->pkts) - rp_rxq->available);
290 if (rp_rxq->available == rp_rxq->forwarded)
295 route_again = sfc_repr_proxy_rx_route(rp, rp_rxq);
296 sfc_repr_proxy_rx_forward(rp, rp_rxq);
297 } while (route_again && !rp_rxq->stop_route);
301 sfc_repr_proxy_routine(void *arg)
303 struct sfc_repr_proxy_port *port;
304 struct sfc_repr_proxy *rp = arg;
307 sfc_repr_proxy_mbox_handle(rp);
309 TAILQ_FOREACH(port, &rp->ports, entries) {
313 for (i = 0; i < rp->nb_txq; i++)
314 sfc_repr_proxy_handle_tx(&rp->dp_txq[i], &port->txq[i]);
317 for (i = 0; i < rp->nb_rxq; i++)
318 sfc_repr_proxy_handle_rx(rp, &rp->dp_rxq[i]);
323 static struct sfc_txq_info *
324 sfc_repr_proxy_txq_info_get(struct sfc_adapter *sa, unsigned int repr_queue_id)
326 struct sfc_adapter_shared *sas = sfc_sa2shared(sa);
327 struct sfc_repr_proxy_dp_txq *dp_txq;
329 SFC_ASSERT(repr_queue_id < sfc_repr_nb_txq(sas));
330 dp_txq = &sa->repr_proxy.dp_txq[repr_queue_id];
332 return &sas->txq_info[dp_txq->sw_index];
336 sfc_repr_proxy_txq_attach(struct sfc_adapter *sa)
338 struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
339 struct sfc_repr_proxy *rp = &sa->repr_proxy;
342 sfc_log_init(sa, "entry");
344 for (i = 0; i < sfc_repr_nb_txq(sas); i++) {
345 sfc_sw_index_t sw_index = sfc_repr_txq_sw_index(sas, i);
347 rp->dp_txq[i].sw_index = sw_index;
350 sfc_log_init(sa, "done");
356 sfc_repr_proxy_txq_detach(struct sfc_adapter *sa)
358 struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
359 struct sfc_repr_proxy *rp = &sa->repr_proxy;
362 sfc_log_init(sa, "entry");
364 for (i = 0; i < sfc_repr_nb_txq(sas); i++)
365 rp->dp_txq[i].sw_index = 0;
367 sfc_log_init(sa, "done");
371 sfc_repr_proxy_txq_init(struct sfc_adapter *sa)
373 struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
374 struct sfc_repr_proxy *rp = &sa->repr_proxy;
375 const struct rte_eth_txconf tx_conf = {
376 .tx_free_thresh = SFC_REPR_PROXY_TXQ_FREE_THRESH,
378 struct sfc_txq_info *txq_info;
383 sfc_log_init(sa, "entry");
385 if (!sfc_repr_available(sas)) {
386 sfc_log_init(sa, "representors not supported - skip");
390 for (init_i = 0; init_i < sfc_repr_nb_txq(sas); init_i++) {
391 struct sfc_repr_proxy_dp_txq *txq = &rp->dp_txq[init_i];
393 txq_info = &sfc_sa2shared(sa)->txq_info[txq->sw_index];
394 if (txq_info->state == SFC_TXQ_INITIALIZED) {
396 "representor proxy TxQ %u is already initialized - skip",
401 sfc_tx_qinit_info(sa, txq->sw_index);
403 rc = sfc_tx_qinit(sa, txq->sw_index,
404 SFC_REPR_PROXY_TX_DESC_COUNT, sa->socket_id,
408 sfc_err(sa, "failed to init representor proxy TxQ %u",
414 sfc_log_init(sa, "done");
419 for (i = 0; i < init_i; i++) {
420 struct sfc_repr_proxy_dp_txq *txq = &rp->dp_txq[i];
422 txq_info = &sfc_sa2shared(sa)->txq_info[txq->sw_index];
423 if (txq_info->state == SFC_TXQ_INITIALIZED)
424 sfc_tx_qfini(sa, txq->sw_index);
426 sfc_log_init(sa, "failed: %s", rte_strerror(rc));
432 sfc_repr_proxy_txq_fini(struct sfc_adapter *sa)
434 struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
435 struct sfc_repr_proxy *rp = &sa->repr_proxy;
436 struct sfc_txq_info *txq_info;
439 sfc_log_init(sa, "entry");
441 if (!sfc_repr_available(sas)) {
442 sfc_log_init(sa, "representors not supported - skip");
446 for (i = 0; i < sfc_repr_nb_txq(sas); i++) {
447 struct sfc_repr_proxy_dp_txq *txq = &rp->dp_txq[i];
449 txq_info = &sfc_sa2shared(sa)->txq_info[txq->sw_index];
450 if (txq_info->state != SFC_TXQ_INITIALIZED) {
452 "representor proxy TxQ %u is already finalized - skip",
457 sfc_tx_qfini(sa, txq->sw_index);
460 sfc_log_init(sa, "done");
464 sfc_repr_proxy_txq_start(struct sfc_adapter *sa)
466 struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
467 struct sfc_repr_proxy *rp = &sa->repr_proxy;
470 sfc_log_init(sa, "entry");
472 for (i = 0; i < sfc_repr_nb_txq(sas); i++) {
473 struct sfc_repr_proxy_dp_txq *txq = &rp->dp_txq[i];
475 txq->dp = sfc_repr_proxy_txq_info_get(sa, i)->dp;
476 txq->pkt_burst = sa->eth_dev->tx_pkt_burst;
478 txq->transmitted = 0;
481 sfc_log_init(sa, "done");
487 sfc_repr_proxy_txq_stop(struct sfc_adapter *sa)
489 sfc_log_init(sa, "entry");
490 sfc_log_init(sa, "done");
494 sfc_repr_proxy_rxq_attach(struct sfc_adapter *sa)
496 struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
497 struct sfc_repr_proxy *rp = &sa->repr_proxy;
500 sfc_log_init(sa, "entry");
502 for (i = 0; i < sfc_repr_nb_rxq(sas); i++) {
503 sfc_sw_index_t sw_index = sfc_repr_rxq_sw_index(sas, i);
505 rp->dp_rxq[i].sw_index = sw_index;
508 sfc_log_init(sa, "done");
514 sfc_repr_proxy_rxq_detach(struct sfc_adapter *sa)
516 struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
517 struct sfc_repr_proxy *rp = &sa->repr_proxy;
520 sfc_log_init(sa, "entry");
522 for (i = 0; i < sfc_repr_nb_rxq(sas); i++)
523 rp->dp_rxq[i].sw_index = 0;
525 sfc_log_init(sa, "done");
528 static struct sfc_rxq_info *
529 sfc_repr_proxy_rxq_info_get(struct sfc_adapter *sa, unsigned int repr_queue_id)
531 struct sfc_adapter_shared *sas = sfc_sa2shared(sa);
532 struct sfc_repr_proxy_dp_rxq *dp_rxq;
534 SFC_ASSERT(repr_queue_id < sfc_repr_nb_rxq(sas));
535 dp_rxq = &sa->repr_proxy.dp_rxq[repr_queue_id];
537 return &sas->rxq_info[dp_rxq->sw_index];
541 sfc_repr_proxy_rxq_init(struct sfc_adapter *sa,
542 struct sfc_repr_proxy_dp_rxq *rxq)
544 struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
545 uint16_t nb_rx_desc = SFC_REPR_PROXY_RX_DESC_COUNT;
546 struct sfc_rxq_info *rxq_info;
547 struct rte_eth_rxconf rxconf = {
548 .rx_free_thresh = SFC_REPR_PROXY_RXQ_REFILL_LEVEL,
553 sfc_log_init(sa, "entry");
555 rxq_info = &sas->rxq_info[rxq->sw_index];
556 if (rxq_info->state & SFC_RXQ_INITIALIZED) {
557 sfc_log_init(sa, "RxQ is already initialized - skip");
561 nb_rx_desc = RTE_MIN(nb_rx_desc, sa->rxq_max_entries);
562 nb_rx_desc = RTE_MAX(nb_rx_desc, sa->rxq_min_entries);
564 rc = sfc_rx_qinit_info(sa, rxq->sw_index, EFX_RXQ_FLAG_INGRESS_MPORT);
566 sfc_err(sa, "failed to init representor proxy RxQ info");
567 goto fail_repr_rxq_init_info;
570 rc = sfc_rx_qinit(sa, rxq->sw_index, nb_rx_desc, sa->socket_id, &rxconf,
573 sfc_err(sa, "failed to init representor proxy RxQ");
574 goto fail_repr_rxq_init;
577 sfc_log_init(sa, "done");
582 fail_repr_rxq_init_info:
583 sfc_log_init(sa, "failed: %s", rte_strerror(rc));
589 sfc_repr_proxy_rxq_fini(struct sfc_adapter *sa)
591 struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
592 struct sfc_repr_proxy *rp = &sa->repr_proxy;
593 struct sfc_rxq_info *rxq_info;
596 sfc_log_init(sa, "entry");
598 if (!sfc_repr_available(sas)) {
599 sfc_log_init(sa, "representors not supported - skip");
603 for (i = 0; i < sfc_repr_nb_rxq(sas); i++) {
604 struct sfc_repr_proxy_dp_rxq *rxq = &rp->dp_rxq[i];
606 rxq_info = &sas->rxq_info[rxq->sw_index];
607 if (rxq_info->state != SFC_RXQ_INITIALIZED) {
609 "representor RxQ %u is already finalized - skip",
614 sfc_rx_qfini(sa, rxq->sw_index);
617 sfc_log_init(sa, "done");
621 sfc_repr_proxy_rxq_stop(struct sfc_adapter *sa)
623 struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
626 sfc_log_init(sa, "entry");
628 for (i = 0; i < sfc_repr_nb_rxq(sas); i++)
629 sfc_rx_qstop(sa, sa->repr_proxy.dp_rxq[i].sw_index);
631 sfc_repr_proxy_rxq_fini(sa);
633 sfc_log_init(sa, "done");
637 sfc_repr_proxy_rxq_start(struct sfc_adapter *sa)
639 struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
640 struct sfc_repr_proxy *rp = &sa->repr_proxy;
644 sfc_log_init(sa, "entry");
646 if (!sfc_repr_available(sas)) {
647 sfc_log_init(sa, "representors not supported - skip");
651 for (i = 0; i < sfc_repr_nb_rxq(sas); i++) {
652 struct sfc_repr_proxy_dp_rxq *rxq = &rp->dp_rxq[i];
654 rc = sfc_repr_proxy_rxq_init(sa, rxq);
656 sfc_err(sa, "failed to init representor proxy RxQ %u",
661 rc = sfc_rx_qstart(sa, rxq->sw_index);
663 sfc_err(sa, "failed to start representor proxy RxQ %u",
668 rxq->dp = sfc_repr_proxy_rxq_info_get(sa, i)->dp;
669 rxq->pkt_burst = sa->eth_dev->rx_pkt_burst;
673 rxq->stop_route = false;
674 rxq->route_port_id = SFC_REPR_INVALID_ROUTE_PORT_ID;
677 sfc_log_init(sa, "done");
683 sfc_repr_proxy_rxq_stop(sa);
684 sfc_log_init(sa, "failed: %s", rte_strerror(rc));
689 sfc_repr_proxy_mae_rule_insert(struct sfc_adapter *sa,
690 struct sfc_repr_proxy_port *port)
692 struct sfc_repr_proxy *rp = &sa->repr_proxy;
693 efx_mport_sel_t mport_alias_selector;
694 efx_mport_sel_t mport_vf_selector;
695 struct sfc_mae_rule *mae_rule;
698 sfc_log_init(sa, "entry");
700 rc = efx_mae_mport_by_id(&port->egress_mport,
703 sfc_err(sa, "failed to get VF mport for repr %u",
708 rc = efx_mae_mport_by_id(&rp->mport_alias, &mport_alias_selector);
710 sfc_err(sa, "failed to get mport selector for repr %u",
715 rc = sfc_mae_rule_add_mport_match_deliver(sa, &mport_vf_selector,
716 &mport_alias_selector, -1,
719 sfc_err(sa, "failed to insert MAE rule for repr %u",
724 port->mae_rule = mae_rule;
726 sfc_log_init(sa, "done");
733 sfc_log_init(sa, "failed: %s", rte_strerror(rc));
738 sfc_repr_proxy_mae_rule_remove(struct sfc_adapter *sa,
739 struct sfc_repr_proxy_port *port)
741 struct sfc_mae_rule *mae_rule = port->mae_rule;
743 sfc_mae_rule_del(sa, mae_rule);
747 sfc_repr_proxy_mport_filter_insert(struct sfc_adapter *sa)
749 struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
750 struct sfc_repr_proxy *rp = &sa->repr_proxy;
751 struct sfc_rxq *rxq_ctrl;
752 struct sfc_repr_proxy_filter *filter = &rp->mport_filter;
753 efx_mport_sel_t mport_alias_selector;
754 static const efx_filter_match_flags_t flags[RTE_DIM(filter->specs)] = {
755 EFX_FILTER_MATCH_UNKNOWN_UCAST_DST,
756 EFX_FILTER_MATCH_UNKNOWN_MCAST_DST };
760 sfc_log_init(sa, "entry");
762 if (sfc_repr_nb_rxq(sas) == 1) {
763 rxq_ctrl = &sa->rxq_ctrl[rp->dp_rxq[0].sw_index];
765 sfc_err(sa, "multiple representor proxy RxQs not supported");
767 goto fail_multiple_queues;
770 rc = efx_mae_mport_by_id(&rp->mport_alias, &mport_alias_selector);
772 sfc_err(sa, "failed to get repr proxy mport by ID");
773 goto fail_get_selector;
776 memset(filter->specs, 0, sizeof(filter->specs));
777 for (i = 0; i < RTE_DIM(filter->specs); i++) {
778 filter->specs[i].efs_priority = EFX_FILTER_PRI_MANUAL;
779 filter->specs[i].efs_flags = EFX_FILTER_FLAG_RX;
780 filter->specs[i].efs_dmaq_id = rxq_ctrl->hw_index;
781 filter->specs[i].efs_match_flags = flags[i] |
782 EFX_FILTER_MATCH_MPORT;
783 filter->specs[i].efs_ingress_mport = mport_alias_selector.sel;
785 rc = efx_filter_insert(sa->nic, &filter->specs[i]);
787 sfc_err(sa, "failed to insert repr proxy filter");
792 sfc_log_init(sa, "done");
798 efx_filter_remove(sa->nic, &filter->specs[i]);
801 fail_multiple_queues:
802 sfc_log_init(sa, "failed: %s", rte_strerror(rc));
807 sfc_repr_proxy_mport_filter_remove(struct sfc_adapter *sa)
809 struct sfc_repr_proxy *rp = &sa->repr_proxy;
810 struct sfc_repr_proxy_filter *filter = &rp->mport_filter;
813 for (i = 0; i < RTE_DIM(filter->specs); i++)
814 efx_filter_remove(sa->nic, &filter->specs[i]);
818 sfc_repr_proxy_port_rule_insert(struct sfc_adapter *sa,
819 struct sfc_repr_proxy_port *port)
823 rc = sfc_repr_proxy_mae_rule_insert(sa, port);
825 goto fail_mae_rule_insert;
829 fail_mae_rule_insert:
834 sfc_repr_proxy_port_rule_remove(struct sfc_adapter *sa,
835 struct sfc_repr_proxy_port *port)
837 sfc_repr_proxy_mae_rule_remove(sa, port);
841 sfc_repr_proxy_ports_init(struct sfc_adapter *sa)
843 struct sfc_repr_proxy *rp = &sa->repr_proxy;
846 sfc_log_init(sa, "entry");
848 rc = efx_mcdi_mport_alloc_alias(sa->nic, &rp->mport_alias, NULL);
850 sfc_err(sa, "failed to alloc mport alias: %s",
852 goto fail_alloc_mport_alias;
855 TAILQ_INIT(&rp->ports);
857 sfc_log_init(sa, "done");
861 fail_alloc_mport_alias:
863 sfc_log_init(sa, "failed: %s", rte_strerror(rc));
868 sfc_repr_proxy_pre_detach(struct sfc_adapter *sa)
870 struct sfc_repr_proxy *rp = &sa->repr_proxy;
871 bool close_ports[RTE_MAX_ETHPORTS] = {0};
872 struct sfc_repr_proxy_port *port;
875 SFC_ASSERT(!sfc_adapter_is_locked(sa));
877 sfc_adapter_lock(sa);
879 if (sfc_repr_available(sfc_sa2shared(sa))) {
880 TAILQ_FOREACH(port, &rp->ports, entries)
881 close_ports[port->rte_port_id] = true;
883 sfc_log_init(sa, "representors not supported - skip");
886 sfc_adapter_unlock(sa);
888 for (i = 0; i < RTE_DIM(close_ports); i++) {
889 if (close_ports[i]) {
891 rte_eth_dev_close(i);
897 sfc_repr_proxy_ports_fini(struct sfc_adapter *sa)
899 struct sfc_repr_proxy *rp = &sa->repr_proxy;
901 efx_mae_mport_free(sa->nic, &rp->mport_alias);
905 sfc_repr_proxy_attach(struct sfc_adapter *sa)
907 struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
908 struct sfc_repr_proxy *rp = &sa->repr_proxy;
909 struct rte_service_spec service;
914 sfc_log_init(sa, "entry");
916 if (!sfc_repr_available(sas)) {
917 sfc_log_init(sa, "representors not supported - skip");
921 rc = sfc_repr_proxy_rxq_attach(sa);
923 goto fail_rxq_attach;
925 rc = sfc_repr_proxy_txq_attach(sa);
927 goto fail_txq_attach;
929 rc = sfc_repr_proxy_ports_init(sa);
931 goto fail_ports_init;
933 cid = sfc_get_service_lcore(sa->socket_id);
934 if (cid == RTE_MAX_LCORE && sa->socket_id != SOCKET_ID_ANY) {
935 /* Warn and try to allocate on any NUMA node */
937 "repr proxy: unable to get service lcore at socket %d",
940 cid = sfc_get_service_lcore(SOCKET_ID_ANY);
942 if (cid == RTE_MAX_LCORE) {
944 sfc_err(sa, "repr proxy: failed to get service lcore");
945 goto fail_get_service_lcore;
948 memset(&service, 0, sizeof(service));
949 snprintf(service.name, sizeof(service.name),
950 "net_sfc_%hu_repr_proxy", sfc_sa2shared(sa)->port_id);
951 service.socket_id = rte_lcore_to_socket_id(cid);
952 service.callback = sfc_repr_proxy_routine;
953 service.callback_userdata = rp;
955 rc = rte_service_component_register(&service, &sid);
958 sfc_err(sa, "repr proxy: failed to register service component");
962 rc = rte_service_map_lcore_set(sid, cid, 1);
965 sfc_err(sa, "repr proxy: failed to map lcore");
969 rp->service_core_id = cid;
970 rp->service_id = sid;
972 sfc_log_init(sa, "done");
977 rte_service_component_unregister(sid);
981 * No need to rollback service lcore get since
982 * it just makes socket_id based search and remembers it.
985 fail_get_service_lcore:
986 sfc_repr_proxy_ports_fini(sa);
989 sfc_repr_proxy_txq_detach(sa);
992 sfc_repr_proxy_rxq_detach(sa);
995 sfc_log_init(sa, "failed: %s", rte_strerror(rc));
1000 sfc_repr_proxy_detach(struct sfc_adapter *sa)
1002 struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
1003 struct sfc_repr_proxy *rp = &sa->repr_proxy;
1005 sfc_log_init(sa, "entry");
1007 if (!sfc_repr_available(sas)) {
1008 sfc_log_init(sa, "representors not supported - skip");
1012 rte_service_map_lcore_set(rp->service_id, rp->service_core_id, 0);
1013 rte_service_component_unregister(rp->service_id);
1014 sfc_repr_proxy_ports_fini(sa);
1015 sfc_repr_proxy_rxq_detach(sa);
1016 sfc_repr_proxy_txq_detach(sa);
1018 sfc_log_init(sa, "done");
1022 sfc_repr_proxy_do_start_port(struct sfc_adapter *sa,
1023 struct sfc_repr_proxy_port *port)
1025 struct sfc_repr_proxy *rp = &sa->repr_proxy;
1028 rc = sfc_repr_proxy_port_rule_insert(sa, port);
1030 goto fail_filter_insert;
1033 rc = sfc_repr_proxy_mbox_send(&rp->mbox, port,
1034 SFC_REPR_PROXY_MBOX_START_PORT);
1036 sfc_err(sa, "failed to start proxy port %u",
1038 goto fail_port_start;
1041 port->started = true;
1047 sfc_repr_proxy_port_rule_remove(sa, port);
1049 sfc_err(sa, "%s() failed %s", __func__, rte_strerror(rc));
1055 sfc_repr_proxy_do_stop_port(struct sfc_adapter *sa,
1056 struct sfc_repr_proxy_port *port)
1059 struct sfc_repr_proxy *rp = &sa->repr_proxy;
1063 rc = sfc_repr_proxy_mbox_send(&rp->mbox, port,
1064 SFC_REPR_PROXY_MBOX_STOP_PORT);
1066 sfc_err(sa, "failed to stop proxy port %u: %s",
1067 port->repr_id, rte_strerror(rc));
1071 port->started = false;
1074 sfc_repr_proxy_port_rule_remove(sa, port);
1080 sfc_repr_proxy_port_enabled(struct sfc_repr_proxy_port *port)
1082 return port->rte_port_id != RTE_MAX_ETHPORTS && port->enabled;
1086 sfc_repr_proxy_ports_disabled(struct sfc_repr_proxy *rp)
1088 struct sfc_repr_proxy_port *port;
1090 TAILQ_FOREACH(port, &rp->ports, entries) {
1091 if (sfc_repr_proxy_port_enabled(port))
1099 sfc_repr_proxy_start(struct sfc_adapter *sa)
1101 struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
1102 struct sfc_repr_proxy *rp = &sa->repr_proxy;
1103 struct sfc_repr_proxy_port *last_port = NULL;
1104 struct sfc_repr_proxy_port *port;
1107 sfc_log_init(sa, "entry");
1109 /* Representor proxy is not started when no representors are started */
1110 if (!sfc_repr_available(sas)) {
1111 sfc_log_init(sa, "representors not supported - skip");
1115 if (sfc_repr_proxy_ports_disabled(rp)) {
1116 sfc_log_init(sa, "no started representor ports - skip");
1120 rc = sfc_repr_proxy_rxq_start(sa);
1122 goto fail_rxq_start;
1124 rc = sfc_repr_proxy_txq_start(sa);
1126 goto fail_txq_start;
1128 rp->nb_txq = sfc_repr_nb_txq(sas);
1129 rp->nb_rxq = sfc_repr_nb_rxq(sas);
1131 /* Service core may be in "stopped" state, start it */
1132 rc = rte_service_lcore_start(rp->service_core_id);
1133 if (rc != 0 && rc != -EALREADY) {
1135 sfc_err(sa, "failed to start service core for %s: %s",
1136 rte_service_get_name(rp->service_id),
1138 goto fail_start_core;
1141 /* Run the service */
1142 rc = rte_service_component_runstate_set(rp->service_id, 1);
1145 sfc_err(sa, "failed to run %s component: %s",
1146 rte_service_get_name(rp->service_id),
1148 goto fail_component_runstate_set;
1150 rc = rte_service_runstate_set(rp->service_id, 1);
1153 sfc_err(sa, "failed to run %s: %s",
1154 rte_service_get_name(rp->service_id),
1156 goto fail_runstate_set;
1159 TAILQ_FOREACH(port, &rp->ports, entries) {
1160 if (sfc_repr_proxy_port_enabled(port)) {
1161 rc = sfc_repr_proxy_do_start_port(sa, port);
1169 rc = sfc_repr_proxy_mport_filter_insert(sa);
1171 goto fail_mport_filter_insert;
1175 sfc_log_init(sa, "done");
1179 fail_mport_filter_insert:
1181 if (last_port != NULL) {
1182 TAILQ_FOREACH(port, &rp->ports, entries) {
1183 if (sfc_repr_proxy_port_enabled(port)) {
1184 (void)sfc_repr_proxy_do_stop_port(sa, port);
1185 if (port == last_port)
1191 rte_service_runstate_set(rp->service_id, 0);
1194 rte_service_component_runstate_set(rp->service_id, 0);
1196 fail_component_runstate_set:
1197 /* Service lcore may be shared and we never stop it */
1200 sfc_repr_proxy_txq_stop(sa);
1203 sfc_repr_proxy_rxq_stop(sa);
1206 sfc_log_init(sa, "failed: %s", rte_strerror(rc));
1211 sfc_repr_proxy_stop(struct sfc_adapter *sa)
1213 struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
1214 struct sfc_repr_proxy *rp = &sa->repr_proxy;
1215 struct sfc_repr_proxy_port *port;
1216 const unsigned int wait_ms_total =
1217 SFC_REPR_PROXY_ROUTINE_TERMINATE_TIMEOUT_MS;
1221 sfc_log_init(sa, "entry");
1223 if (!sfc_repr_available(sas)) {
1224 sfc_log_init(sa, "representors not supported - skip");
1228 if (sfc_repr_proxy_ports_disabled(rp)) {
1229 sfc_log_init(sa, "no started representor ports - skip");
1233 TAILQ_FOREACH(port, &rp->ports, entries) {
1234 if (sfc_repr_proxy_port_enabled(port)) {
1235 rc = sfc_repr_proxy_do_stop_port(sa, port);
1238 "failed to stop representor proxy port %u: %s",
1239 port->repr_id, rte_strerror(rc));
1244 sfc_repr_proxy_mport_filter_remove(sa);
1246 rc = rte_service_runstate_set(rp->service_id, 0);
1248 sfc_err(sa, "failed to stop %s: %s",
1249 rte_service_get_name(rp->service_id),
1253 rc = rte_service_component_runstate_set(rp->service_id, 0);
1255 sfc_err(sa, "failed to stop %s component: %s",
1256 rte_service_get_name(rp->service_id),
1260 /* Service lcore may be shared and we never stop it */
1263 * Wait for the representor proxy routine to finish the last iteration.
1264 * Give up on timeout.
1266 for (i = 0; i < wait_ms_total; i++) {
1267 if (rte_service_may_be_active(rp->service_id) == 0)
1273 sfc_repr_proxy_rxq_stop(sa);
1274 sfc_repr_proxy_txq_stop(sa);
1276 rp->started = false;
1278 sfc_log_init(sa, "done");
1282 sfc_repr_proxy_add_port(uint16_t pf_port_id, uint16_t repr_id,
1283 uint16_t rte_port_id, const efx_mport_sel_t *mport_sel)
1285 struct sfc_repr_proxy_port *port;
1286 struct sfc_repr_proxy *rp;
1287 struct sfc_adapter *sa;
1290 sa = sfc_get_adapter_by_pf_port_id(pf_port_id);
1291 rp = sfc_repr_proxy_by_adapter(sa);
1293 sfc_log_init(sa, "entry");
1294 TAILQ_FOREACH(port, &rp->ports, entries) {
1295 if (port->rte_port_id == rte_port_id) {
1297 sfc_err(sa, "%s() failed: port exists", __func__);
1298 goto fail_port_exists;
1302 port = rte_zmalloc("sfc-repr-proxy-port", sizeof(*port),
1306 sfc_err(sa, "failed to alloc memory for proxy port");
1307 goto fail_alloc_port;
1310 rc = efx_mae_mport_id_by_selector(sa->nic, mport_sel,
1311 &port->egress_mport);
1314 "failed get MAE mport id by selector (repr_id %u): %s",
1315 repr_id, rte_strerror(rc));
1319 port->rte_port_id = rte_port_id;
1320 port->repr_id = repr_id;
1323 rc = sfc_repr_proxy_mbox_send(&rp->mbox, port,
1324 SFC_REPR_PROXY_MBOX_ADD_PORT);
1326 sfc_err(sa, "failed to add proxy port %u",
1331 TAILQ_INSERT_TAIL(&rp->ports, port, entries);
1334 sfc_log_init(sa, "done");
1335 sfc_put_adapter(sa);
1344 sfc_log_init(sa, "failed: %s", rte_strerror(rc));
1345 sfc_put_adapter(sa);
1351 sfc_repr_proxy_del_port(uint16_t pf_port_id, uint16_t repr_id)
1353 struct sfc_repr_proxy_port *port;
1354 struct sfc_repr_proxy *rp;
1355 struct sfc_adapter *sa;
1358 sa = sfc_get_adapter_by_pf_port_id(pf_port_id);
1359 rp = sfc_repr_proxy_by_adapter(sa);
1361 sfc_log_init(sa, "entry");
1363 port = sfc_repr_proxy_find_port(rp, repr_id);
1365 sfc_err(sa, "failed: no such port");
1371 rc = sfc_repr_proxy_mbox_send(&rp->mbox, port,
1372 SFC_REPR_PROXY_MBOX_DEL_PORT);
1374 sfc_err(sa, "failed to remove proxy port %u",
1376 goto fail_port_remove;
1379 TAILQ_REMOVE(&rp->ports, port, entries);
1384 sfc_log_init(sa, "done");
1386 sfc_put_adapter(sa);
1392 sfc_log_init(sa, "failed: %s", rte_strerror(rc));
1393 sfc_put_adapter(sa);
1399 sfc_repr_proxy_add_rxq(uint16_t pf_port_id, uint16_t repr_id,
1400 uint16_t queue_id, struct rte_ring *rx_ring,
1401 struct rte_mempool *mp)
1403 struct sfc_repr_proxy_port *port;
1404 struct sfc_repr_proxy_rxq *rxq;
1405 struct sfc_repr_proxy *rp;
1406 struct sfc_adapter *sa;
1408 sa = sfc_get_adapter_by_pf_port_id(pf_port_id);
1409 rp = sfc_repr_proxy_by_adapter(sa);
1411 sfc_log_init(sa, "entry");
1413 port = sfc_repr_proxy_find_port(rp, repr_id);
1415 sfc_err(sa, "%s() failed: no such port", __func__);
1419 rxq = &port->rxq[queue_id];
1420 if (rp->dp_rxq[queue_id].mp != NULL && rp->dp_rxq[queue_id].mp != mp) {
1421 sfc_err(sa, "multiple mempools per queue are not supported");
1422 sfc_put_adapter(sa);
1426 rxq->ring = rx_ring;
1428 rp->dp_rxq[queue_id].mp = mp;
1429 rp->dp_rxq[queue_id].ref_count++;
1431 sfc_log_init(sa, "done");
1432 sfc_put_adapter(sa);
1438 sfc_repr_proxy_del_rxq(uint16_t pf_port_id, uint16_t repr_id,
1441 struct sfc_repr_proxy_port *port;
1442 struct sfc_repr_proxy_rxq *rxq;
1443 struct sfc_repr_proxy *rp;
1444 struct sfc_adapter *sa;
1446 sa = sfc_get_adapter_by_pf_port_id(pf_port_id);
1447 rp = sfc_repr_proxy_by_adapter(sa);
1449 sfc_log_init(sa, "entry");
1451 port = sfc_repr_proxy_find_port(rp, repr_id);
1453 sfc_err(sa, "%s() failed: no such port", __func__);
1457 rxq = &port->rxq[queue_id];
1460 rxq->mb_pool = NULL;
1461 rp->dp_rxq[queue_id].ref_count--;
1462 if (rp->dp_rxq[queue_id].ref_count == 0)
1463 rp->dp_rxq[queue_id].mp = NULL;
1465 sfc_log_init(sa, "done");
1466 sfc_put_adapter(sa);
1470 sfc_repr_proxy_add_txq(uint16_t pf_port_id, uint16_t repr_id,
1471 uint16_t queue_id, struct rte_ring *tx_ring,
1472 efx_mport_id_t *egress_mport)
1474 struct sfc_repr_proxy_port *port;
1475 struct sfc_repr_proxy_txq *txq;
1476 struct sfc_repr_proxy *rp;
1477 struct sfc_adapter *sa;
1479 sa = sfc_get_adapter_by_pf_port_id(pf_port_id);
1480 rp = sfc_repr_proxy_by_adapter(sa);
1482 sfc_log_init(sa, "entry");
1484 port = sfc_repr_proxy_find_port(rp, repr_id);
1486 sfc_err(sa, "%s() failed: no such port", __func__);
1490 txq = &port->txq[queue_id];
1492 txq->ring = tx_ring;
1494 *egress_mport = port->egress_mport;
1496 sfc_log_init(sa, "done");
1497 sfc_put_adapter(sa);
1503 sfc_repr_proxy_del_txq(uint16_t pf_port_id, uint16_t repr_id,
1506 struct sfc_repr_proxy_port *port;
1507 struct sfc_repr_proxy_txq *txq;
1508 struct sfc_repr_proxy *rp;
1509 struct sfc_adapter *sa;
1511 sa = sfc_get_adapter_by_pf_port_id(pf_port_id);
1512 rp = sfc_repr_proxy_by_adapter(sa);
1514 sfc_log_init(sa, "entry");
1516 port = sfc_repr_proxy_find_port(rp, repr_id);
1518 sfc_err(sa, "%s() failed: no such port", __func__);
1522 txq = &port->txq[queue_id];
1526 sfc_log_init(sa, "done");
1527 sfc_put_adapter(sa);
1531 sfc_repr_proxy_start_repr(uint16_t pf_port_id, uint16_t repr_id)
1533 bool proxy_start_required = false;
1534 struct sfc_repr_proxy_port *port;
1535 struct sfc_repr_proxy *rp;
1536 struct sfc_adapter *sa;
1539 sa = sfc_get_adapter_by_pf_port_id(pf_port_id);
1540 rp = sfc_repr_proxy_by_adapter(sa);
1542 sfc_log_init(sa, "entry");
1544 port = sfc_repr_proxy_find_port(rp, repr_id);
1546 sfc_err(sa, "%s() failed: no such port", __func__);
1548 goto fail_not_found;
1551 if (port->enabled) {
1553 sfc_err(sa, "failed: repr %u proxy port already started",
1555 goto fail_already_started;
1558 if (sa->state == SFC_ETHDEV_STARTED) {
1559 if (sfc_repr_proxy_ports_disabled(rp)) {
1560 proxy_start_required = true;
1562 rc = sfc_repr_proxy_do_start_port(sa, port);
1565 "failed to start repr %u proxy port",
1572 port->enabled = true;
1574 if (proxy_start_required) {
1575 rc = sfc_repr_proxy_start(sa);
1577 sfc_err(sa, "failed to start proxy");
1578 goto fail_proxy_start;
1582 sfc_log_init(sa, "done");
1583 sfc_put_adapter(sa);
1588 port->enabled = false;
1591 fail_already_started:
1593 sfc_err(sa, "failed to start repr %u proxy port: %s", repr_id,
1595 sfc_put_adapter(sa);
1601 sfc_repr_proxy_stop_repr(uint16_t pf_port_id, uint16_t repr_id)
1603 struct sfc_repr_proxy_port *port;
1604 struct sfc_repr_proxy_port *p;
1605 struct sfc_repr_proxy *rp;
1606 struct sfc_adapter *sa;
1609 sa = sfc_get_adapter_by_pf_port_id(pf_port_id);
1610 rp = sfc_repr_proxy_by_adapter(sa);
1612 sfc_log_init(sa, "entry");
1614 port = sfc_repr_proxy_find_port(rp, repr_id);
1616 sfc_err(sa, "%s() failed: no such port", __func__);
1620 if (!port->enabled) {
1621 sfc_log_init(sa, "repr %u proxy port is not started - skip",
1623 sfc_put_adapter(sa);
1627 if (sa->state == SFC_ETHDEV_STARTED) {
1628 bool last_enabled = true;
1630 TAILQ_FOREACH(p, &rp->ports, entries) {
1634 if (sfc_repr_proxy_port_enabled(p)) {
1635 last_enabled = false;
1642 sfc_repr_proxy_stop(sa);
1644 rc = sfc_repr_proxy_do_stop_port(sa, port);
1648 "failed to stop representor proxy TxQ %u: %s",
1649 repr_id, rte_strerror(rc));
1650 sfc_put_adapter(sa);
1655 port->enabled = false;
1657 sfc_log_init(sa, "done");
1658 sfc_put_adapter(sa);