4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <rte_malloc.h>
36 #include <rte_ethdev.h>
40 #include <rte_devargs.h>
41 #include <rte_kvargs.h>
43 #include <rte_alarm.h>
44 #include <rte_cycles.h>
46 #include "rte_eth_bond.h"
47 #include "rte_eth_bond_private.h"
48 #include "rte_eth_bond_8023ad_private.h"
50 #define REORDER_PERIOD_MS 10
51 /* Table for statistics in mode 5 TLB */
52 static uint64_t tlb_last_obytets[RTE_MAX_ETHPORTS];
55 bond_ethdev_rx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
57 struct bond_dev_private *internals;
59 uint16_t num_rx_slave = 0;
60 uint16_t num_rx_total = 0;
64 /* Cast to structure, containing bonded device's port id and queue id */
65 struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)queue;
67 internals = bd_rx_q->dev_private;
70 for (i = 0; i < internals->active_slave_count && nb_pkts; i++) {
71 /* Offset of pointer to *bufs increases as packets are received
72 * from other slaves */
73 num_rx_slave = rte_eth_rx_burst(internals->active_slaves[i],
74 bd_rx_q->queue_id, bufs + num_rx_total, nb_pkts);
76 num_rx_total += num_rx_slave;
77 nb_pkts -= num_rx_slave;
85 bond_ethdev_rx_burst_active_backup(void *queue, struct rte_mbuf **bufs,
88 struct bond_dev_private *internals;
90 /* Cast to structure, containing bonded device's port id and queue id */
91 struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)queue;
93 internals = bd_rx_q->dev_private;
95 return rte_eth_rx_burst(internals->current_primary_port,
96 bd_rx_q->queue_id, bufs, nb_pkts);
100 bond_ethdev_rx_burst_8023ad(void *queue, struct rte_mbuf **bufs,
103 /* Cast to structure, containing bonded device's port id and queue id */
104 struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)queue;
105 struct bond_dev_private *internals = bd_rx_q->dev_private;
106 struct ether_addr bond_mac;
108 struct ether_hdr *hdr;
110 const uint16_t ether_type_slow_be = rte_be_to_cpu_16(ETHER_TYPE_SLOW);
111 uint16_t num_rx_total = 0; /* Total number of received packets */
112 uint8_t slaves[RTE_MAX_ETHPORTS];
115 uint8_t collecting; /* current slave collecting status */
116 const uint8_t promisc = internals->promiscuous_en;
119 rte_eth_macaddr_get(internals->port_id, &bond_mac);
120 /* Copy slave list to protect against slave up/down changes during tx
122 slave_count = internals->active_slave_count;
123 memcpy(slaves, internals->active_slaves,
124 sizeof(internals->active_slaves[0]) * slave_count);
126 for (i = 0; i < slave_count && num_rx_total < nb_pkts; i++) {
128 collecting = ACTOR_STATE(&mode_8023ad_ports[slaves[i]], COLLECTING);
130 /* Read packets from this slave */
131 num_rx_total += rte_eth_rx_burst(slaves[i], bd_rx_q->queue_id,
132 &bufs[num_rx_total], nb_pkts - num_rx_total);
134 for (k = j; k < 2 && k < num_rx_total; k++)
135 rte_prefetch0(rte_pktmbuf_mtod(bufs[k], void *));
137 /* Handle slow protocol packets. */
138 while (j < num_rx_total) {
139 if (j + 3 < num_rx_total)
140 rte_prefetch0(rte_pktmbuf_mtod(bufs[j + 3], void *));
142 hdr = rte_pktmbuf_mtod(bufs[j], struct ether_hdr *);
143 /* Remove packet from array if it is slow packet or slave is not
144 * in collecting state or bondign interface is not in promiscus
145 * mode and packet address does not match. */
146 if (unlikely(hdr->ether_type == ether_type_slow_be ||
147 !collecting || (!promisc &&
148 !is_same_ether_addr(&bond_mac, &hdr->d_addr)))) {
150 if (hdr->ether_type == ether_type_slow_be) {
151 bond_mode_8023ad_handle_slow_pkt(internals, slaves[i],
154 rte_pktmbuf_free(bufs[j]);
156 /* Packet is managed by mode 4 or dropped, shift the array */
158 if (j < num_rx_total) {
159 memmove(&bufs[j], &bufs[j + 1], sizeof(bufs[0]) *
171 bond_ethdev_tx_burst_round_robin(void *queue, struct rte_mbuf **bufs,
174 struct bond_dev_private *internals;
175 struct bond_tx_queue *bd_tx_q;
177 struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS][nb_pkts];
178 uint16_t slave_nb_pkts[RTE_MAX_ETHPORTS] = { 0 };
180 uint8_t num_of_slaves;
181 uint8_t slaves[RTE_MAX_ETHPORTS];
183 uint16_t num_tx_total = 0, num_tx_slave;
185 static int slave_idx = 0;
186 int i, cslave_idx = 0, tx_fail_total = 0;
188 bd_tx_q = (struct bond_tx_queue *)queue;
189 internals = bd_tx_q->dev_private;
191 /* Copy slave list to protect against slave up/down changes during tx
193 num_of_slaves = internals->active_slave_count;
194 memcpy(slaves, internals->active_slaves,
195 sizeof(internals->active_slaves[0]) * num_of_slaves);
197 if (num_of_slaves < 1)
200 /* Populate slaves mbuf with which packets are to be sent on it */
201 for (i = 0; i < nb_pkts; i++) {
202 cslave_idx = (slave_idx + i) % num_of_slaves;
203 slave_bufs[cslave_idx][(slave_nb_pkts[cslave_idx])++] = bufs[i];
206 /* increment current slave index so the next call to tx burst starts on the
208 slave_idx = ++cslave_idx;
210 /* Send packet burst on each slave device */
211 for (i = 0; i < num_of_slaves; i++) {
212 if (slave_nb_pkts[i] > 0) {
213 num_tx_slave = rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
214 slave_bufs[i], slave_nb_pkts[i]);
216 /* if tx burst fails move packets to end of bufs */
217 if (unlikely(num_tx_slave < slave_nb_pkts[i])) {
218 int tx_fail_slave = slave_nb_pkts[i] - num_tx_slave;
220 tx_fail_total += tx_fail_slave;
222 memcpy(&bufs[nb_pkts - tx_fail_total],
223 &slave_bufs[i][num_tx_slave],
224 tx_fail_slave * sizeof(bufs[0]));
226 num_tx_total += num_tx_slave;
234 bond_ethdev_tx_burst_active_backup(void *queue,
235 struct rte_mbuf **bufs, uint16_t nb_pkts)
237 struct bond_dev_private *internals;
238 struct bond_tx_queue *bd_tx_q;
240 bd_tx_q = (struct bond_tx_queue *)queue;
241 internals = bd_tx_q->dev_private;
243 if (internals->active_slave_count < 1)
246 return rte_eth_tx_burst(internals->current_primary_port, bd_tx_q->queue_id,
250 static inline uint16_t
251 ether_hash(struct ether_hdr *eth_hdr)
253 uint16_t *word_src_addr = (uint16_t *)eth_hdr->s_addr.addr_bytes;
254 uint16_t *word_dst_addr = (uint16_t *)eth_hdr->d_addr.addr_bytes;
256 return (word_src_addr[0] ^ word_dst_addr[0]) ^
257 (word_src_addr[1] ^ word_dst_addr[1]) ^
258 (word_src_addr[2] ^ word_dst_addr[2]);
261 static inline uint32_t
262 ipv4_hash(struct ipv4_hdr *ipv4_hdr)
264 return (ipv4_hdr->src_addr ^ ipv4_hdr->dst_addr);
267 static inline uint32_t
268 ipv6_hash(struct ipv6_hdr *ipv6_hdr)
270 uint32_t *word_src_addr = (uint32_t *)&(ipv6_hdr->src_addr[0]);
271 uint32_t *word_dst_addr = (uint32_t *)&(ipv6_hdr->dst_addr[0]);
273 return (word_src_addr[0] ^ word_dst_addr[0]) ^
274 (word_src_addr[1] ^ word_dst_addr[1]) ^
275 (word_src_addr[2] ^ word_dst_addr[2]) ^
276 (word_src_addr[3] ^ word_dst_addr[3]);
280 udp_hash(struct udp_hdr *hdr)
282 return hdr->src_port ^ hdr->dst_port;
285 static inline uint16_t
286 xmit_slave_hash(const struct rte_mbuf *buf, uint8_t slave_count, uint8_t policy)
288 struct ether_hdr *eth_hdr;
289 struct udp_hdr *udp_hdr;
290 size_t eth_offset = 0;
293 if (slave_count == 1)
297 case BALANCE_XMIT_POLICY_LAYER2:
298 eth_hdr = rte_pktmbuf_mtod(buf, struct ether_hdr *);
300 hash = ether_hash(eth_hdr);
302 return hash % slave_count;
304 case BALANCE_XMIT_POLICY_LAYER23:
305 eth_hdr = rte_pktmbuf_mtod(buf, struct ether_hdr *);
307 if (buf->ol_flags & PKT_RX_VLAN_PKT)
308 eth_offset = sizeof(struct ether_hdr) + sizeof(struct vlan_hdr);
310 eth_offset = sizeof(struct ether_hdr);
312 if (buf->ol_flags & PKT_RX_IPV4_HDR) {
313 struct ipv4_hdr *ipv4_hdr;
314 ipv4_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(buf,
315 unsigned char *) + eth_offset);
317 hash = ether_hash(eth_hdr) ^ ipv4_hash(ipv4_hdr);
320 struct ipv6_hdr *ipv6_hdr;
322 ipv6_hdr = (struct ipv6_hdr *)(rte_pktmbuf_mtod(buf,
323 unsigned char *) + eth_offset);
325 hash = ether_hash(eth_hdr) ^ ipv6_hash(ipv6_hdr);
329 case BALANCE_XMIT_POLICY_LAYER34:
330 if (buf->ol_flags & PKT_RX_VLAN_PKT)
331 eth_offset = sizeof(struct ether_hdr) + sizeof(struct vlan_hdr);
333 eth_offset = sizeof(struct ether_hdr);
335 if (buf->ol_flags & PKT_RX_IPV4_HDR) {
336 struct ipv4_hdr *ipv4_hdr = (struct ipv4_hdr *)
337 (rte_pktmbuf_mtod(buf, unsigned char *) + eth_offset);
339 if (ipv4_hdr->next_proto_id == IPPROTO_UDP) {
340 udp_hdr = (struct udp_hdr *)
341 (rte_pktmbuf_mtod(buf, unsigned char *) + eth_offset +
342 sizeof(struct ipv4_hdr));
343 hash = ipv4_hash(ipv4_hdr) ^ udp_hash(udp_hdr);
345 hash = ipv4_hash(ipv4_hdr);
348 struct ipv6_hdr *ipv6_hdr = (struct ipv6_hdr *)
349 (rte_pktmbuf_mtod(buf, unsigned char *) + eth_offset);
351 if (ipv6_hdr->proto == IPPROTO_UDP) {
352 udp_hdr = (struct udp_hdr *)
353 (rte_pktmbuf_mtod(buf, unsigned char *) + eth_offset +
354 sizeof(struct ipv6_hdr));
355 hash = ipv6_hash(ipv6_hdr) ^ udp_hash(udp_hdr);
357 hash = ipv6_hash(ipv6_hdr);
366 return hash % slave_count;
370 uint64_t bwg_left_int;
371 uint64_t bwg_left_remainder;
376 bandwidth_cmp(const void *a, const void *b)
378 const struct bwg_slave *bwg_a = a;
379 const struct bwg_slave *bwg_b = b;
380 int64_t diff = (int64_t)bwg_b->bwg_left_int - (int64_t)bwg_a->bwg_left_int;
381 int64_t diff2 = (int64_t)bwg_b->bwg_left_remainder -
382 (int64_t)bwg_a->bwg_left_remainder;
396 bandwidth_left(int port_id, uint64_t load, uint8_t update_idx,
397 struct bwg_slave *bwg_slave)
399 struct rte_eth_link link_status;
401 rte_eth_link_get(port_id, &link_status);
402 uint64_t link_bwg = link_status.link_speed * 1000000ULL / 8;
405 link_bwg = (link_bwg * (update_idx+1) * REORDER_PERIOD_MS);
406 bwg_slave->bwg_left_int = (link_bwg - 1000*load) / link_bwg;
407 bwg_slave->bwg_left_remainder = (link_bwg - 1000*load) % link_bwg;
411 bond_ethdev_update_tlb_slave_cb(void *arg)
413 struct bond_dev_private *internals = arg;
414 struct rte_eth_stats slave_stats;
415 struct bwg_slave bwg_array[RTE_MAX_ETHPORTS];
419 uint8_t update_stats = 0;
422 internals->slave_update_idx++;
425 if (internals->slave_update_idx >= REORDER_PERIOD_MS)
428 for (i = 0; i < internals->active_slave_count; i++) {
429 slave_id = internals->active_slaves[i];
430 rte_eth_stats_get(slave_id, &slave_stats);
431 tx_bytes = slave_stats.obytes - tlb_last_obytets[slave_id];
432 bandwidth_left(slave_id, tx_bytes,
433 internals->slave_update_idx, &bwg_array[i]);
434 bwg_array[i].slave = slave_id;
437 tlb_last_obytets[slave_id] = slave_stats.obytes;
440 if (update_stats == 1)
441 internals->slave_update_idx = 0;
444 qsort(bwg_array, slave_count, sizeof(bwg_array[0]), bandwidth_cmp);
445 for (i = 0; i < slave_count; i++)
446 internals->active_slaves[i] = bwg_array[i].slave;
448 rte_eal_alarm_set(REORDER_PERIOD_MS * 1000, bond_ethdev_update_tlb_slave_cb,
449 (struct bond_dev_private *)internals);
453 bond_ethdev_tx_burst_tlb(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
455 struct bond_tx_queue *bd_tx_q = (struct bond_tx_queue *)queue;
456 struct bond_dev_private *internals = bd_tx_q->dev_private;
458 struct rte_eth_dev *primary_port =
459 &rte_eth_devices[internals->primary_port];
460 uint16_t num_tx_total = 0;
463 uint8_t num_of_slaves = internals->active_slave_count;
464 uint8_t slaves[RTE_MAX_ETHPORTS];
466 struct ether_hdr *ether_hdr;
467 struct ether_addr primary_slave_addr;
468 struct ether_addr active_slave_addr;
470 if (num_of_slaves < 1)
473 memcpy(slaves, internals->active_slaves,
474 sizeof(internals->active_slaves[0]) * num_of_slaves);
477 ether_addr_copy(primary_port->data->mac_addrs, &primary_slave_addr);
480 for (i = 0; i < 3; i++)
481 rte_prefetch0(rte_pktmbuf_mtod(bufs[i], void*));
484 for (i = 0; i < num_of_slaves; i++) {
485 ether_addr_copy(&internals->slaves[slaves[i]].persisted_mac_addr,
488 for (j = num_tx_total; j < nb_pkts; j++) {
490 rte_prefetch0(rte_pktmbuf_mtod(bufs[j+3], void*));
492 ether_hdr = rte_pktmbuf_mtod(bufs[j], struct ether_hdr *);
493 if (is_same_ether_addr(ðer_hdr->s_addr, &primary_slave_addr))
494 ether_addr_copy(&active_slave_addr, ðer_hdr->s_addr);
497 num_tx_total += rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
498 bufs + num_tx_total, nb_pkts - num_tx_total);
500 if (num_tx_total == nb_pkts)
508 bond_ethdev_tx_burst_balance(void *queue, struct rte_mbuf **bufs,
511 struct bond_dev_private *internals;
512 struct bond_tx_queue *bd_tx_q;
514 uint8_t num_of_slaves;
515 uint8_t slaves[RTE_MAX_ETHPORTS];
517 uint16_t num_tx_total = 0, num_tx_slave = 0, tx_fail_total = 0;
521 struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS][nb_pkts];
522 uint16_t slave_nb_pkts[RTE_MAX_ETHPORTS] = { 0 };
524 bd_tx_q = (struct bond_tx_queue *)queue;
525 internals = bd_tx_q->dev_private;
527 /* Copy slave list to protect against slave up/down changes during tx
529 num_of_slaves = internals->active_slave_count;
530 memcpy(slaves, internals->active_slaves,
531 sizeof(internals->active_slaves[0]) * num_of_slaves);
533 if (num_of_slaves < 1)
536 /* Populate slaves mbuf with the packets which are to be sent on it */
537 for (i = 0; i < nb_pkts; i++) {
538 /* Select output slave using hash based on xmit policy */
539 op_slave_id = xmit_slave_hash(bufs[i], num_of_slaves,
540 internals->balance_xmit_policy);
542 /* Populate slave mbuf arrays with mbufs for that slave */
543 slave_bufs[op_slave_id][slave_nb_pkts[op_slave_id]++] = bufs[i];
546 /* Send packet burst on each slave device */
547 for (i = 0; i < num_of_slaves; i++) {
548 if (slave_nb_pkts[i] > 0) {
549 num_tx_slave = rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
550 slave_bufs[i], slave_nb_pkts[i]);
552 /* if tx burst fails move packets to end of bufs */
553 if (unlikely(num_tx_slave < slave_nb_pkts[i])) {
554 int slave_tx_fail_count = slave_nb_pkts[i] - num_tx_slave;
556 tx_fail_total += slave_tx_fail_count;
557 memcpy(&bufs[nb_pkts - tx_fail_total],
558 &slave_bufs[i][num_tx_slave],
559 slave_tx_fail_count * sizeof(bufs[0]));
562 num_tx_total += num_tx_slave;
570 bond_ethdev_tx_burst_8023ad(void *queue, struct rte_mbuf **bufs,
573 struct bond_dev_private *internals;
574 struct bond_tx_queue *bd_tx_q;
576 uint8_t num_of_slaves;
577 uint8_t slaves[RTE_MAX_ETHPORTS];
578 /* possitions in slaves, not ID */
579 uint8_t distributing_offsets[RTE_MAX_ETHPORTS];
580 uint8_t distributing_count;
582 uint16_t num_tx_slave, num_tx_total = 0, num_tx_fail_total = 0;
583 uint16_t i, j, op_slave_idx;
584 const uint16_t buffs_size = nb_pkts + BOND_MODE_8023AX_SLAVE_TX_PKTS + 1;
586 /* Allocate additional packets in case 8023AD mode. */
587 struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS][buffs_size];
588 void *slow_pkts[BOND_MODE_8023AX_SLAVE_TX_PKTS] = { NULL };
590 /* Total amount of packets in slave_bufs */
591 uint16_t slave_nb_pkts[RTE_MAX_ETHPORTS] = { 0 };
592 /* Slow packets placed in each slave */
593 uint8_t slave_slow_nb_pkts[RTE_MAX_ETHPORTS] = { 0 };
595 bd_tx_q = (struct bond_tx_queue *)queue;
596 internals = bd_tx_q->dev_private;
598 /* Copy slave list to protect against slave up/down changes during tx
600 num_of_slaves = internals->active_slave_count;
601 if (num_of_slaves < 1)
604 memcpy(slaves, internals->active_slaves, sizeof(slaves[0]) * num_of_slaves);
606 distributing_count = 0;
607 for (i = 0; i < num_of_slaves; i++) {
608 struct port *port = &mode_8023ad_ports[slaves[i]];
610 slave_slow_nb_pkts[i] = rte_ring_dequeue_burst(port->tx_ring,
611 slow_pkts, BOND_MODE_8023AX_SLAVE_TX_PKTS);
612 slave_nb_pkts[i] = slave_slow_nb_pkts[i];
614 for (j = 0; j < slave_slow_nb_pkts[i]; j++)
615 slave_bufs[i][j] = slow_pkts[j];
617 if (ACTOR_STATE(port, DISTRIBUTING))
618 distributing_offsets[distributing_count++] = i;
621 if (likely(distributing_count > 0)) {
622 /* Populate slaves mbuf with the packets which are to be sent on it */
623 for (i = 0; i < nb_pkts; i++) {
624 /* Select output slave using hash based on xmit policy */
625 op_slave_idx = xmit_slave_hash(bufs[i], distributing_count,
626 internals->balance_xmit_policy);
628 /* Populate slave mbuf arrays with mbufs for that slave. Use only
629 * slaves that are currently distributing. */
630 uint8_t slave_offset = distributing_offsets[op_slave_idx];
631 slave_bufs[slave_offset][slave_nb_pkts[slave_offset]] = bufs[i];
632 slave_nb_pkts[slave_offset]++;
636 /* Send packet burst on each slave device */
637 for (i = 0; i < num_of_slaves; i++) {
638 if (slave_nb_pkts[i] == 0)
641 num_tx_slave = rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
642 slave_bufs[i], slave_nb_pkts[i]);
644 /* If tx burst fails drop slow packets */
645 for ( ; num_tx_slave < slave_slow_nb_pkts[i]; num_tx_slave++)
646 rte_pktmbuf_free(slave_bufs[i][num_tx_slave]);
648 num_tx_total += num_tx_slave - slave_slow_nb_pkts[i];
649 num_tx_fail_total += slave_nb_pkts[i] - num_tx_slave;
651 /* If tx burst fails move packets to end of bufs */
652 if (unlikely(num_tx_slave < slave_nb_pkts[i])) {
653 uint16_t j = nb_pkts - num_tx_fail_total;
654 for ( ; num_tx_slave < slave_nb_pkts[i]; j++, num_tx_slave++)
655 bufs[j] = slave_bufs[i][num_tx_slave];
662 #ifdef RTE_MBUF_REFCNT
664 bond_ethdev_tx_burst_broadcast(void *queue, struct rte_mbuf **bufs,
667 struct bond_dev_private *internals;
668 struct bond_tx_queue *bd_tx_q;
670 uint8_t tx_failed_flag = 0, num_of_slaves;
671 uint8_t slaves[RTE_MAX_ETHPORTS];
673 uint16_t max_nb_of_tx_pkts = 0;
675 int slave_tx_total[RTE_MAX_ETHPORTS];
676 int i, most_successful_tx_slave = -1;
678 bd_tx_q = (struct bond_tx_queue *)queue;
679 internals = bd_tx_q->dev_private;
681 /* Copy slave list to protect against slave up/down changes during tx
683 num_of_slaves = internals->active_slave_count;
684 memcpy(slaves, internals->active_slaves,
685 sizeof(internals->active_slaves[0]) * num_of_slaves);
687 if (num_of_slaves < 1)
690 /* Increment reference count on mbufs */
691 for (i = 0; i < nb_pkts; i++)
692 rte_mbuf_refcnt_update(bufs[i], num_of_slaves - 1);
694 /* Transmit burst on each active slave */
695 for (i = 0; i < num_of_slaves; i++) {
696 slave_tx_total[i] = rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
699 if (unlikely(slave_tx_total[i] < nb_pkts))
702 /* record the value and slave index for the slave which transmits the
703 * maximum number of packets */
704 if (slave_tx_total[i] > max_nb_of_tx_pkts) {
705 max_nb_of_tx_pkts = slave_tx_total[i];
706 most_successful_tx_slave = i;
710 /* if slaves fail to transmit packets from burst, the calling application
711 * is not expected to know about multiple references to packets so we must
712 * handle failures of all packets except those of the most successful slave
714 if (unlikely(tx_failed_flag))
715 for (i = 0; i < num_of_slaves; i++)
716 if (i != most_successful_tx_slave)
717 while (slave_tx_total[i] < nb_pkts)
718 rte_pktmbuf_free(bufs[slave_tx_total[i]++]);
720 return max_nb_of_tx_pkts;
725 link_properties_set(struct rte_eth_dev *bonded_eth_dev,
726 struct rte_eth_link *slave_dev_link)
728 struct rte_eth_link *bonded_dev_link = &bonded_eth_dev->data->dev_link;
729 struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
731 if (slave_dev_link->link_status &&
732 bonded_eth_dev->data->dev_started) {
733 bonded_dev_link->link_duplex = slave_dev_link->link_duplex;
734 bonded_dev_link->link_speed = slave_dev_link->link_speed;
736 internals->link_props_set = 1;
741 link_properties_reset(struct rte_eth_dev *bonded_eth_dev)
743 struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
745 memset(&(bonded_eth_dev->data->dev_link), 0,
746 sizeof(bonded_eth_dev->data->dev_link));
748 internals->link_props_set = 0;
752 link_properties_valid(struct rte_eth_link *bonded_dev_link,
753 struct rte_eth_link *slave_dev_link)
755 if (bonded_dev_link->link_duplex != slave_dev_link->link_duplex ||
756 bonded_dev_link->link_speed != slave_dev_link->link_speed)
763 mac_address_get(struct rte_eth_dev *eth_dev, struct ether_addr *dst_mac_addr)
765 struct ether_addr *mac_addr;
767 if (eth_dev == NULL) {
768 RTE_LOG(ERR, PMD, "%s: NULL pointer eth_dev specified\n", __func__);
772 if (dst_mac_addr == NULL) {
773 RTE_LOG(ERR, PMD, "%s: NULL pointer MAC specified\n", __func__);
777 mac_addr = eth_dev->data->mac_addrs;
779 ether_addr_copy(mac_addr, dst_mac_addr);
784 mac_address_set(struct rte_eth_dev *eth_dev, struct ether_addr *new_mac_addr)
786 struct ether_addr *mac_addr;
788 if (eth_dev == NULL) {
789 RTE_BOND_LOG(ERR, "NULL pointer eth_dev specified");
793 if (new_mac_addr == NULL) {
794 RTE_BOND_LOG(ERR, "NULL pointer MAC specified");
798 mac_addr = eth_dev->data->mac_addrs;
800 /* If new MAC is different to current MAC then update */
801 if (memcmp(mac_addr, new_mac_addr, sizeof(*mac_addr)) != 0)
802 memcpy(mac_addr, new_mac_addr, sizeof(*mac_addr));
808 mac_address_slaves_update(struct rte_eth_dev *bonded_eth_dev)
810 struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
813 /* Update slave devices MAC addresses */
814 if (internals->slave_count < 1)
817 switch (internals->mode) {
818 case BONDING_MODE_ROUND_ROBIN:
819 case BONDING_MODE_BALANCE:
820 #ifdef RTE_MBUF_REFCNT
821 case BONDING_MODE_BROADCAST:
823 for (i = 0; i < internals->slave_count; i++) {
824 if (mac_address_set(&rte_eth_devices[internals->slaves[i].port_id],
825 bonded_eth_dev->data->mac_addrs)) {
826 RTE_BOND_LOG(ERR, "Failed to update port Id %d MAC address",
827 internals->slaves[i].port_id);
832 case BONDING_MODE_8023AD:
833 bond_mode_8023ad_mac_address_update(bonded_eth_dev);
835 case BONDING_MODE_ACTIVE_BACKUP:
836 case BONDING_MODE_ADAPTIVE_TRANSMIT_LOAD_BALANCING:
838 for (i = 0; i < internals->slave_count; i++) {
839 if (internals->slaves[i].port_id ==
840 internals->current_primary_port) {
841 if (mac_address_set(&rte_eth_devices[internals->primary_port],
842 bonded_eth_dev->data->mac_addrs)) {
843 RTE_BOND_LOG(ERR, "Failed to update port Id %d MAC address",
844 internals->current_primary_port);
849 &rte_eth_devices[internals->slaves[i].port_id],
850 &internals->slaves[i].persisted_mac_addr)) {
851 RTE_BOND_LOG(ERR, "Failed to update port Id %d MAC address",
852 internals->slaves[i].port_id);
863 bond_ethdev_mode_set(struct rte_eth_dev *eth_dev, int mode)
865 struct bond_dev_private *internals;
867 internals = eth_dev->data->dev_private;
870 case BONDING_MODE_ROUND_ROBIN:
871 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_round_robin;
872 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst;
874 case BONDING_MODE_ACTIVE_BACKUP:
875 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_active_backup;
876 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst_active_backup;
878 case BONDING_MODE_BALANCE:
879 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_balance;
880 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst;
882 #ifdef RTE_MBUF_REFCNT
883 case BONDING_MODE_BROADCAST:
884 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_broadcast;
885 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst;
888 case BONDING_MODE_8023AD:
889 if (bond_mode_8023ad_enable(eth_dev) != 0)
892 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst_8023ad;
893 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_8023ad;
894 RTE_BOND_LOG(WARNING,
895 "Using mode 4, it is necessary to do TX burst and RX burst "
896 "at least every 100ms.");
898 case BONDING_MODE_ADAPTIVE_TRANSMIT_LOAD_BALANCING:
899 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_tlb;
900 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst_active_backup;
906 internals->mode = mode;
912 slave_configure(struct rte_eth_dev *bonded_eth_dev,
913 struct rte_eth_dev *slave_eth_dev)
915 struct bond_rx_queue *bd_rx_q;
916 struct bond_tx_queue *bd_tx_q;
921 rte_eth_dev_stop(slave_eth_dev->data->port_id);
923 /* Enable interrupts on slave device if supported */
924 if (slave_eth_dev->driver->pci_drv.drv_flags & RTE_PCI_DRV_INTR_LSC)
925 slave_eth_dev->data->dev_conf.intr_conf.lsc = 1;
927 /* Configure device */
928 errval = rte_eth_dev_configure(slave_eth_dev->data->port_id,
929 bonded_eth_dev->data->nb_rx_queues,
930 bonded_eth_dev->data->nb_tx_queues,
931 &(slave_eth_dev->data->dev_conf));
933 RTE_BOND_LOG(ERR, "Cannot configure slave device: port %u , err (%d)",
934 slave_eth_dev->data->port_id, errval);
938 /* Setup Rx Queues */
939 for (q_id = 0; q_id < bonded_eth_dev->data->nb_rx_queues; q_id++) {
940 bd_rx_q = (struct bond_rx_queue *)bonded_eth_dev->data->rx_queues[q_id];
942 errval = rte_eth_rx_queue_setup(slave_eth_dev->data->port_id, q_id,
944 rte_eth_dev_socket_id(slave_eth_dev->data->port_id),
945 &(bd_rx_q->rx_conf), bd_rx_q->mb_pool);
948 "rte_eth_rx_queue_setup: port=%d queue_id %d, err (%d)",
949 slave_eth_dev->data->port_id, q_id, errval);
954 /* Setup Tx Queues */
955 for (q_id = 0; q_id < bonded_eth_dev->data->nb_tx_queues; q_id++) {
956 bd_tx_q = (struct bond_tx_queue *)bonded_eth_dev->data->tx_queues[q_id];
958 errval = rte_eth_tx_queue_setup(slave_eth_dev->data->port_id, q_id,
960 rte_eth_dev_socket_id(slave_eth_dev->data->port_id),
964 "rte_eth_tx_queue_setup: port=%d queue_id %d, err (%d)",
965 slave_eth_dev->data->port_id, q_id, errval);
971 errval = rte_eth_dev_start(slave_eth_dev->data->port_id);
973 RTE_BOND_LOG(ERR, "rte_eth_dev_start: port=%u, err (%d)",
974 slave_eth_dev->data->port_id, errval);
982 slave_remove(struct bond_dev_private *internals,
983 struct rte_eth_dev *slave_eth_dev)
987 for (i = 0; i < internals->slave_count; i++)
988 if (internals->slaves[i].port_id ==
989 slave_eth_dev->data->port_id)
992 if (i < (internals->slave_count - 1))
993 memmove(&internals->slaves[i], &internals->slaves[i + 1],
994 sizeof(internals->slaves[0]) *
995 (internals->slave_count - i - 1));
997 internals->slave_count--;
1001 bond_ethdev_slave_link_status_change_monitor(void *cb_arg);
1004 slave_add(struct bond_dev_private *internals,
1005 struct rte_eth_dev *slave_eth_dev)
1007 struct bond_slave_details *slave_details =
1008 &internals->slaves[internals->slave_count];
1010 slave_details->port_id = slave_eth_dev->data->port_id;
1011 slave_details->last_link_status = 0;
1013 /* If slave device doesn't support interrupts then we need to enabled
1014 * polling to monitor link status */
1015 if (!(slave_eth_dev->pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)) {
1016 slave_details->link_status_poll_enabled = 1;
1018 if (!internals->link_status_polling_enabled) {
1019 internals->link_status_polling_enabled = 1;
1021 rte_eal_alarm_set(internals->link_status_polling_interval_ms * 1000,
1022 bond_ethdev_slave_link_status_change_monitor,
1023 (void *)&rte_eth_devices[internals->port_id]);
1027 slave_details->link_status_wait_to_complete = 0;
1028 /* clean tlb_last_obytes when adding port for bonding device */
1029 memcpy(&(slave_details->persisted_mac_addr), slave_eth_dev->data->mac_addrs,
1030 sizeof(struct ether_addr));
1034 bond_ethdev_primary_set(struct bond_dev_private *internals,
1035 uint8_t slave_port_id)
1039 if (internals->active_slave_count < 1)
1040 internals->current_primary_port = slave_port_id;
1042 /* Search bonded device slave ports for new proposed primary port */
1043 for (i = 0; i < internals->active_slave_count; i++) {
1044 if (internals->active_slaves[i] == slave_port_id)
1045 internals->current_primary_port = slave_port_id;
1050 bond_ethdev_promiscuous_enable(struct rte_eth_dev *eth_dev);
1053 bond_ethdev_start(struct rte_eth_dev *eth_dev)
1055 struct bond_dev_private *internals;
1058 /* slave eth dev will be started by bonded device */
1059 if (valid_bonded_ethdev(eth_dev)) {
1060 RTE_BOND_LOG(ERR, "User tried to explicitly start a slave eth_dev (%d)",
1061 eth_dev->data->port_id);
1065 eth_dev->data->dev_link.link_status = 0;
1066 eth_dev->data->dev_started = 1;
1068 internals = eth_dev->data->dev_private;
1070 if (internals->slave_count == 0) {
1071 RTE_BOND_LOG(ERR, "Cannot start port since there are no slave devices");
1075 if (internals->user_defined_mac == 0) {
1076 struct ether_addr *new_mac_addr = NULL;
1078 for (i = 0; i < internals->slave_count; i++)
1079 if (internals->slaves[i].port_id == internals->primary_port)
1080 new_mac_addr = &internals->slaves[i].persisted_mac_addr;
1082 if (new_mac_addr == NULL)
1085 if (mac_address_set(eth_dev, new_mac_addr) != 0) {
1086 RTE_BOND_LOG(ERR, "bonded port (%d) failed to update MAC address",
1087 eth_dev->data->port_id);
1092 /* Update all slave devices MACs*/
1093 if (mac_address_slaves_update(eth_dev) != 0)
1096 /* If bonded device is configure in promiscuous mode then re-apply config */
1097 if (internals->promiscuous_en)
1098 bond_ethdev_promiscuous_enable(eth_dev);
1100 /* Reconfigure each slave device if starting bonded device */
1101 for (i = 0; i < internals->slave_count; i++) {
1102 if (slave_configure(eth_dev,
1103 &(rte_eth_devices[internals->slaves[i].port_id])) != 0) {
1105 "bonded port (%d) failed to reconfigure slave device (%d)",
1106 eth_dev->data->port_id, internals->slaves[i].port_id);
1111 if (internals->user_defined_primary_port)
1112 bond_ethdev_primary_set(internals, internals->primary_port);
1114 if (internals->mode == BONDING_MODE_8023AD)
1115 bond_mode_8023ad_start(eth_dev);
1117 if (internals->mode == BONDING_MODE_ADAPTIVE_TRANSMIT_LOAD_BALANCING)
1118 bond_ethdev_update_tlb_slave_cb(internals);
1124 bond_ethdev_stop(struct rte_eth_dev *eth_dev)
1126 struct bond_dev_private *internals = eth_dev->data->dev_private;
1129 if (internals->mode == BONDING_MODE_8023AD) {
1133 bond_mode_8023ad_stop(eth_dev);
1135 /* Discard all messages to/from mode 4 state machines */
1136 for (i = 0; i < internals->slave_count; i++) {
1137 port = &mode_8023ad_ports[internals->slaves[i].port_id];
1139 RTE_VERIFY(port->rx_ring != NULL);
1140 while (rte_ring_dequeue(port->rx_ring, &pkt) != -ENOENT)
1141 rte_pktmbuf_free(pkt);
1143 RTE_VERIFY(port->tx_ring != NULL);
1144 while (rte_ring_dequeue(port->tx_ring, &pkt) != -ENOENT)
1145 rte_pktmbuf_free(pkt);
1149 if (internals->mode == BONDING_MODE_ADAPTIVE_TRANSMIT_LOAD_BALANCING) {
1150 rte_eal_alarm_cancel(bond_ethdev_update_tlb_slave_cb, internals);
1153 internals->active_slave_count = 0;
1154 internals->link_status_polling_enabled = 0;
1156 eth_dev->data->dev_link.link_status = 0;
1157 eth_dev->data->dev_started = 0;
1161 bond_ethdev_close(struct rte_eth_dev *dev __rte_unused)
1165 /* forward declaration */
1166 static int bond_ethdev_configure(struct rte_eth_dev *dev);
1169 bond_ethdev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1171 struct bond_dev_private *internals = dev->data->dev_private;
1173 dev_info->driver_name = driver_name;
1174 dev_info->max_mac_addrs = 1;
1176 dev_info->max_rx_pktlen = (uint32_t)2048;
1178 dev_info->max_rx_queues = (uint16_t)128;
1179 dev_info->max_tx_queues = (uint16_t)512;
1181 dev_info->min_rx_bufsize = 0;
1182 dev_info->pci_dev = dev->pci_dev;
1184 dev_info->rx_offload_capa = internals->rx_offload_capa;
1185 dev_info->tx_offload_capa = internals->tx_offload_capa;
1189 bond_ethdev_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
1190 uint16_t nb_rx_desc, unsigned int socket_id __rte_unused,
1191 const struct rte_eth_rxconf *rx_conf, struct rte_mempool *mb_pool)
1193 struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)
1194 rte_zmalloc_socket(NULL, sizeof(struct bond_rx_queue),
1195 0, dev->pci_dev->numa_node);
1196 if (bd_rx_q == NULL)
1199 bd_rx_q->queue_id = rx_queue_id;
1200 bd_rx_q->dev_private = dev->data->dev_private;
1202 bd_rx_q->nb_rx_desc = nb_rx_desc;
1204 memcpy(&(bd_rx_q->rx_conf), rx_conf, sizeof(struct rte_eth_rxconf));
1205 bd_rx_q->mb_pool = mb_pool;
1207 dev->data->rx_queues[rx_queue_id] = bd_rx_q;
1213 bond_ethdev_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
1214 uint16_t nb_tx_desc, unsigned int socket_id __rte_unused,
1215 const struct rte_eth_txconf *tx_conf)
1217 struct bond_tx_queue *bd_tx_q = (struct bond_tx_queue *)
1218 rte_zmalloc_socket(NULL, sizeof(struct bond_tx_queue),
1219 0, dev->pci_dev->numa_node);
1221 if (bd_tx_q == NULL)
1224 bd_tx_q->queue_id = tx_queue_id;
1225 bd_tx_q->dev_private = dev->data->dev_private;
1227 bd_tx_q->nb_tx_desc = nb_tx_desc;
1228 memcpy(&(bd_tx_q->tx_conf), tx_conf, sizeof(bd_tx_q->tx_conf));
1230 dev->data->tx_queues[tx_queue_id] = bd_tx_q;
1236 bond_ethdev_rx_queue_release(void *queue)
1245 bond_ethdev_tx_queue_release(void *queue)
1254 bond_ethdev_slave_link_status_change_monitor(void *cb_arg)
1256 struct rte_eth_dev *bonded_ethdev, *slave_ethdev;
1257 struct bond_dev_private *internals;
1259 /* Default value for polling slave found is true as we don't want to
1260 * disable the polling thread if we cannot get the lock */
1261 int i, polling_slave_found = 1;
1266 bonded_ethdev = (struct rte_eth_dev *)cb_arg;
1267 internals = (struct bond_dev_private *)bonded_ethdev->data->dev_private;
1269 if (!bonded_ethdev->data->dev_started ||
1270 !internals->link_status_polling_enabled)
1273 /* If device is currently being configured then don't check slaves link
1274 * status, wait until next period */
1275 if (rte_spinlock_trylock(&internals->lock)) {
1276 if (internals->slave_count > 0)
1277 polling_slave_found = 0;
1279 for (i = 0; i < internals->slave_count; i++) {
1280 if (!internals->slaves[i].link_status_poll_enabled)
1283 slave_ethdev = &rte_eth_devices[internals->slaves[i].port_id];
1284 polling_slave_found = 1;
1286 /* Update slave link status */
1287 (*slave_ethdev->dev_ops->link_update)(slave_ethdev,
1288 internals->slaves[i].link_status_wait_to_complete);
1290 /* if link status has changed since last checked then call lsc
1292 if (slave_ethdev->data->dev_link.link_status !=
1293 internals->slaves[i].last_link_status) {
1294 internals->slaves[i].last_link_status =
1295 slave_ethdev->data->dev_link.link_status;
1297 bond_ethdev_lsc_event_callback(internals->slaves[i].port_id,
1298 RTE_ETH_EVENT_INTR_LSC,
1299 &bonded_ethdev->data->port_id);
1302 rte_spinlock_unlock(&internals->lock);
1305 if (polling_slave_found)
1306 /* Set alarm to continue monitoring link status of slave ethdev's */
1307 rte_eal_alarm_set(internals->link_status_polling_interval_ms * 1000,
1308 bond_ethdev_slave_link_status_change_monitor, cb_arg);
1312 bond_ethdev_link_update(struct rte_eth_dev *bonded_eth_dev,
1313 int wait_to_complete)
1315 struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
1317 if (!bonded_eth_dev->data->dev_started ||
1318 internals->active_slave_count == 0) {
1319 bonded_eth_dev->data->dev_link.link_status = 0;
1322 struct rte_eth_dev *slave_eth_dev;
1325 for (i = 0; i < internals->active_slave_count; i++) {
1326 slave_eth_dev = &rte_eth_devices[internals->active_slaves[i]];
1328 (*slave_eth_dev->dev_ops->link_update)(slave_eth_dev,
1330 if (slave_eth_dev->data->dev_link.link_status == 1) {
1336 bonded_eth_dev->data->dev_link.link_status = link_up;
1343 bond_ethdev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1345 struct bond_dev_private *internals = dev->data->dev_private;
1346 struct rte_eth_stats slave_stats;
1350 /* clear bonded stats before populating from slaves */
1351 memset(stats, 0, sizeof(*stats));
1353 for (i = 0; i < internals->slave_count; i++) {
1354 rte_eth_stats_get(internals->slaves[i].port_id, &slave_stats);
1356 stats->ipackets += slave_stats.ipackets;
1357 stats->opackets += slave_stats.opackets;
1358 stats->ibytes += slave_stats.ibytes;
1359 stats->obytes += slave_stats.obytes;
1360 stats->ierrors += slave_stats.ierrors;
1361 stats->oerrors += slave_stats.oerrors;
1362 stats->imcasts += slave_stats.imcasts;
1363 stats->rx_nombuf += slave_stats.rx_nombuf;
1364 stats->fdirmatch += slave_stats.fdirmatch;
1365 stats->fdirmiss += slave_stats.fdirmiss;
1366 stats->tx_pause_xon += slave_stats.tx_pause_xon;
1367 stats->rx_pause_xon += slave_stats.rx_pause_xon;
1368 stats->tx_pause_xoff += slave_stats.tx_pause_xoff;
1369 stats->rx_pause_xoff += slave_stats.rx_pause_xoff;
1374 bond_ethdev_stats_reset(struct rte_eth_dev *dev)
1376 struct bond_dev_private *internals = dev->data->dev_private;
1379 for (i = 0; i < internals->slave_count; i++)
1380 rte_eth_stats_reset(internals->slaves[i].port_id);
1384 bond_ethdev_promiscuous_enable(struct rte_eth_dev *eth_dev)
1386 struct bond_dev_private *internals = eth_dev->data->dev_private;
1389 internals->promiscuous_en = 1;
1391 switch (internals->mode) {
1392 /* Promiscuous mode is propagated to all slaves */
1393 case BONDING_MODE_ROUND_ROBIN:
1394 case BONDING_MODE_BALANCE:
1395 #ifdef RTE_MBUF_REFCNT
1396 case BONDING_MODE_BROADCAST:
1398 for (i = 0; i < internals->slave_count; i++)
1399 rte_eth_promiscuous_enable(internals->slaves[i].port_id);
1401 /* In mode4 promiscus mode is managed when slave is added/removed */
1402 case BONDING_MODE_8023AD:
1404 /* Promiscuous mode is propagated only to primary slave */
1405 case BONDING_MODE_ACTIVE_BACKUP:
1406 case BONDING_MODE_ADAPTIVE_TRANSMIT_LOAD_BALANCING:
1408 rte_eth_promiscuous_enable(internals->current_primary_port);
1413 bond_ethdev_promiscuous_disable(struct rte_eth_dev *dev)
1415 struct bond_dev_private *internals = dev->data->dev_private;
1418 internals->promiscuous_en = 0;
1420 switch (internals->mode) {
1421 /* Promiscuous mode is propagated to all slaves */
1422 case BONDING_MODE_ROUND_ROBIN:
1423 case BONDING_MODE_BALANCE:
1424 #ifdef RTE_MBUF_REFCNT
1425 case BONDING_MODE_BROADCAST:
1427 for (i = 0; i < internals->slave_count; i++)
1428 rte_eth_promiscuous_disable(internals->slaves[i].port_id);
1430 /* In mode4 promiscus mode is set managed when slave is added/removed */
1431 case BONDING_MODE_8023AD:
1433 /* Promiscuous mode is propagated only to primary slave */
1434 case BONDING_MODE_ACTIVE_BACKUP:
1435 case BONDING_MODE_ADAPTIVE_TRANSMIT_LOAD_BALANCING:
1437 rte_eth_promiscuous_disable(internals->current_primary_port);
1442 bond_ethdev_delayed_lsc_propagation(void *arg)
1447 _rte_eth_dev_callback_process((struct rte_eth_dev *)arg,
1448 RTE_ETH_EVENT_INTR_LSC);
1452 bond_ethdev_lsc_event_callback(uint8_t port_id, enum rte_eth_event_type type,
1455 struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
1456 struct bond_dev_private *internals;
1457 struct rte_eth_link link;
1459 int i, valid_slave = 0;
1461 uint8_t lsc_flag = 0;
1463 if (type != RTE_ETH_EVENT_INTR_LSC || param == NULL)
1466 bonded_eth_dev = &rte_eth_devices[*(uint8_t *)param];
1467 slave_eth_dev = &rte_eth_devices[port_id];
1469 if (valid_bonded_ethdev(bonded_eth_dev))
1472 internals = bonded_eth_dev->data->dev_private;
1474 /* If the device isn't started don't handle interrupts */
1475 if (!bonded_eth_dev->data->dev_started)
1478 /* verify that port_id is a valid slave of bonded port */
1479 for (i = 0; i < internals->slave_count; i++) {
1480 if (internals->slaves[i].port_id == port_id) {
1489 /* Search for port in active port list */
1490 active_pos = find_slave_by_id(internals->active_slaves,
1491 internals->active_slave_count, port_id);
1493 rte_eth_link_get_nowait(port_id, &link);
1494 if (link.link_status) {
1495 if (active_pos < internals->active_slave_count)
1498 /* if no active slave ports then set this port to be primary port */
1499 if (internals->active_slave_count < 1) {
1500 /* If first active slave, then change link status */
1501 bonded_eth_dev->data->dev_link.link_status = 1;
1502 internals->current_primary_port = port_id;
1505 mac_address_slaves_update(bonded_eth_dev);
1507 /* Inherit eth dev link properties from first active slave */
1508 link_properties_set(bonded_eth_dev,
1509 &(slave_eth_dev->data->dev_link));
1512 activate_slave(bonded_eth_dev, port_id);
1514 /* If user has defined the primary port then default to using it */
1515 if (internals->user_defined_primary_port &&
1516 internals->primary_port == port_id)
1517 bond_ethdev_primary_set(internals, port_id);
1519 if (active_pos == internals->active_slave_count)
1522 /* Remove from active slave list */
1523 deactivate_slave(bonded_eth_dev, port_id);
1525 /* No active slaves, change link status to down and reset other
1526 * link properties */
1527 if (internals->active_slave_count < 1) {
1529 bonded_eth_dev->data->dev_link.link_status = 0;
1531 link_properties_reset(bonded_eth_dev);
1534 /* Update primary id, take first active slave from list or if none
1535 * available set to -1 */
1536 if (port_id == internals->current_primary_port) {
1537 if (internals->active_slave_count > 0)
1538 bond_ethdev_primary_set(internals,
1539 internals->active_slaves[0]);
1541 internals->current_primary_port = internals->primary_port;
1546 /* Cancel any possible outstanding interrupts if delays are enabled */
1547 if (internals->link_up_delay_ms > 0 ||
1548 internals->link_down_delay_ms > 0)
1549 rte_eal_alarm_cancel(bond_ethdev_delayed_lsc_propagation,
1552 if (bonded_eth_dev->data->dev_link.link_status) {
1553 if (internals->link_up_delay_ms > 0)
1554 rte_eal_alarm_set(internals->link_up_delay_ms * 1000,
1555 bond_ethdev_delayed_lsc_propagation,
1556 (void *)bonded_eth_dev);
1558 _rte_eth_dev_callback_process(bonded_eth_dev,
1559 RTE_ETH_EVENT_INTR_LSC);
1562 if (internals->link_down_delay_ms > 0)
1563 rte_eal_alarm_set(internals->link_down_delay_ms * 1000,
1564 bond_ethdev_delayed_lsc_propagation,
1565 (void *)bonded_eth_dev);
1567 _rte_eth_dev_callback_process(bonded_eth_dev,
1568 RTE_ETH_EVENT_INTR_LSC);
1573 struct eth_dev_ops default_dev_ops = {
1574 .dev_start = bond_ethdev_start,
1575 .dev_stop = bond_ethdev_stop,
1576 .dev_close = bond_ethdev_close,
1577 .dev_configure = bond_ethdev_configure,
1578 .dev_infos_get = bond_ethdev_info,
1579 .rx_queue_setup = bond_ethdev_rx_queue_setup,
1580 .tx_queue_setup = bond_ethdev_tx_queue_setup,
1581 .rx_queue_release = bond_ethdev_rx_queue_release,
1582 .tx_queue_release = bond_ethdev_tx_queue_release,
1583 .link_update = bond_ethdev_link_update,
1584 .stats_get = bond_ethdev_stats_get,
1585 .stats_reset = bond_ethdev_stats_reset,
1586 .promiscuous_enable = bond_ethdev_promiscuous_enable,
1587 .promiscuous_disable = bond_ethdev_promiscuous_disable
1591 bond_init(const char *name, const char *params)
1593 struct bond_dev_private *internals;
1594 struct rte_kvargs *kvlist;
1595 uint8_t bonding_mode, socket_id;
1596 int arg_count, port_id;
1598 RTE_LOG(INFO, EAL, "Initializing pmd_bond for %s\n", name);
1600 kvlist = rte_kvargs_parse(params, pmd_bond_init_valid_arguments);
1604 /* Parse link bonding mode */
1605 if (rte_kvargs_count(kvlist, PMD_BOND_MODE_KVARG) == 1) {
1606 if (rte_kvargs_process(kvlist, PMD_BOND_MODE_KVARG,
1607 &bond_ethdev_parse_slave_mode_kvarg, &bonding_mode) != 0) {
1608 RTE_LOG(ERR, EAL, "Invalid mode for bonded device %s\n", name);
1613 "Mode must be specified only once for bonded device %s\n",
1618 /* Parse socket id to create bonding device on */
1619 arg_count = rte_kvargs_count(kvlist, PMD_BOND_SOCKET_ID_KVARG);
1620 if (arg_count == 1) {
1621 if (rte_kvargs_process(kvlist, PMD_BOND_SOCKET_ID_KVARG,
1622 &bond_ethdev_parse_socket_id_kvarg, &socket_id) != 0) {
1624 "Invalid socket Id specified for bonded device %s\n",
1628 } else if (arg_count > 1) {
1630 "Socket Id can be specified only once for bonded device %s\n",
1634 socket_id = rte_socket_id();
1637 /* Create link bonding eth device */
1638 port_id = rte_eth_bond_create(name, bonding_mode, socket_id);
1641 "Failed to create socket %s in mode %u on socket %u.\n",
1642 name, bonding_mode, socket_id);
1645 internals = rte_eth_devices[port_id].data->dev_private;
1646 internals->kvlist = kvlist;
1649 "Create bonded device %s on port %d in mode %u on socket %u.\n",
1650 name, port_id, bonding_mode, socket_id);
1654 /* this part will resolve the slave portids after all the other pdev and vdev
1655 * have been allocated */
1657 bond_ethdev_configure(struct rte_eth_dev *dev)
1659 char *name = dev->data->name;
1660 struct bond_dev_private *internals = dev->data->dev_private;
1661 struct rte_kvargs *kvlist = internals->kvlist;
1662 int arg_count, port_id = dev - rte_eth_devices;
1665 * if no kvlist, it means that this bonded device has been created
1666 * through the bonding api.
1671 /* Parse MAC address for bonded device */
1672 arg_count = rte_kvargs_count(kvlist, PMD_BOND_MAC_ADDR_KVARG);
1673 if (arg_count == 1) {
1674 struct ether_addr bond_mac;
1676 if (rte_kvargs_process(kvlist, PMD_BOND_MAC_ADDR_KVARG,
1677 &bond_ethdev_parse_bond_mac_addr_kvarg, &bond_mac) < 0) {
1678 RTE_LOG(INFO, EAL, "Invalid mac address for bonded device %s\n",
1683 /* Set MAC address */
1684 if (rte_eth_bond_mac_address_set(port_id, &bond_mac) != 0) {
1686 "Failed to set mac address on bonded device %s\n",
1690 } else if (arg_count > 1) {
1692 "MAC address can be specified only once for bonded device %s\n",
1697 /* Parse/set balance mode transmit policy */
1698 arg_count = rte_kvargs_count(kvlist, PMD_BOND_XMIT_POLICY_KVARG);
1699 if (arg_count == 1) {
1700 uint8_t xmit_policy;
1702 if (rte_kvargs_process(kvlist, PMD_BOND_XMIT_POLICY_KVARG,
1703 &bond_ethdev_parse_balance_xmit_policy_kvarg, &xmit_policy) !=
1706 "Invalid xmit policy specified for bonded device %s\n",
1711 /* Set balance mode transmit policy*/
1712 if (rte_eth_bond_xmit_policy_set(port_id, xmit_policy) != 0) {
1714 "Failed to set balance xmit policy on bonded device %s\n",
1718 } else if (arg_count > 1) {
1720 "Transmit policy can be specified only once for bonded device"
1725 /* Parse/add slave ports to bonded device */
1726 if (rte_kvargs_count(kvlist, PMD_BOND_SLAVE_PORT_KVARG) > 0) {
1727 struct bond_ethdev_slave_ports slave_ports;
1730 memset(&slave_ports, 0, sizeof(slave_ports));
1732 if (rte_kvargs_process(kvlist, PMD_BOND_SLAVE_PORT_KVARG,
1733 &bond_ethdev_parse_slave_port_kvarg, &slave_ports) != 0) {
1735 "Failed to parse slave ports for bonded device %s\n",
1740 for (i = 0; i < slave_ports.slave_count; i++) {
1741 if (rte_eth_bond_slave_add(port_id, slave_ports.slaves[i]) != 0) {
1743 "Failed to add port %d as slave to bonded device %s\n",
1744 slave_ports.slaves[i], name);
1749 RTE_LOG(INFO, EAL, "No slaves specified for bonded device %s\n", name);
1753 /* Parse/set primary slave port id*/
1754 arg_count = rte_kvargs_count(kvlist, PMD_BOND_PRIMARY_SLAVE_KVARG);
1755 if (arg_count == 1) {
1756 uint8_t primary_slave_port_id;
1758 if (rte_kvargs_process(kvlist,
1759 PMD_BOND_PRIMARY_SLAVE_KVARG,
1760 &bond_ethdev_parse_primary_slave_port_id_kvarg,
1761 &primary_slave_port_id) < 0) {
1763 "Invalid primary slave port id specified for bonded device"
1768 /* Set balance mode transmit policy*/
1769 if (rte_eth_bond_primary_set(port_id, (uint8_t)primary_slave_port_id)
1772 "Failed to set primary slave port %d on bonded device %s\n",
1773 primary_slave_port_id, name);
1776 } else if (arg_count > 1) {
1778 "Primary slave can be specified only once for bonded device"
1783 /* Parse link status monitor polling interval */
1784 arg_count = rte_kvargs_count(kvlist, PMD_BOND_LSC_POLL_PERIOD_KVARG);
1785 if (arg_count == 1) {
1786 uint32_t lsc_poll_interval_ms;
1788 if (rte_kvargs_process(kvlist,
1789 PMD_BOND_LSC_POLL_PERIOD_KVARG,
1790 &bond_ethdev_parse_time_ms_kvarg,
1791 &lsc_poll_interval_ms) < 0) {
1793 "Invalid lsc polling interval value specified for bonded"
1794 " device %s\n", name);
1798 if (rte_eth_bond_link_monitoring_set(port_id, lsc_poll_interval_ms)
1801 "Failed to set lsc monitor polling interval (%u ms) on"
1802 " bonded device %s\n", lsc_poll_interval_ms, name);
1805 } else if (arg_count > 1) {
1807 "LSC polling interval can be specified only once for bonded"
1808 " device %s\n", name);
1812 /* Parse link up interrupt propagation delay */
1813 arg_count = rte_kvargs_count(kvlist, PMD_BOND_LINK_UP_PROP_DELAY_KVARG);
1814 if (arg_count == 1) {
1815 uint32_t link_up_delay_ms;
1817 if (rte_kvargs_process(kvlist,
1818 PMD_BOND_LINK_UP_PROP_DELAY_KVARG,
1819 &bond_ethdev_parse_time_ms_kvarg,
1820 &link_up_delay_ms) < 0) {
1822 "Invalid link up propagation delay value specified for"
1823 " bonded device %s\n", name);
1827 /* Set balance mode transmit policy*/
1828 if (rte_eth_bond_link_up_prop_delay_set(port_id, link_up_delay_ms)
1831 "Failed to set link up propagation delay (%u ms) on bonded"
1832 " device %s\n", link_up_delay_ms, name);
1835 } else if (arg_count > 1) {
1837 "Link up propagation delay can be specified only once for"
1838 " bonded device %s\n", name);
1842 /* Parse link down interrupt propagation delay */
1843 arg_count = rte_kvargs_count(kvlist, PMD_BOND_LINK_DOWN_PROP_DELAY_KVARG);
1844 if (arg_count == 1) {
1845 uint32_t link_down_delay_ms;
1847 if (rte_kvargs_process(kvlist,
1848 PMD_BOND_LINK_DOWN_PROP_DELAY_KVARG,
1849 &bond_ethdev_parse_time_ms_kvarg,
1850 &link_down_delay_ms) < 0) {
1852 "Invalid link down propagation delay value specified for"
1853 " bonded device %s\n", name);
1857 /* Set balance mode transmit policy*/
1858 if (rte_eth_bond_link_down_prop_delay_set(port_id, link_down_delay_ms)
1861 "Failed to set link down propagation delay (%u ms) on"
1862 " bonded device %s\n", link_down_delay_ms, name);
1865 } else if (arg_count > 1) {
1867 "Link down propagation delay can be specified only once for"
1868 " bonded device %s\n", name);
1875 static struct rte_driver bond_drv = {
1881 PMD_REGISTER_DRIVER(bond_drv);