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>
44 #include "rte_eth_bond.h"
45 #include "rte_eth_bond_private.h"
48 bond_ethdev_rx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
50 struct bond_dev_private *internals;
52 uint16_t num_rx_slave = 0;
53 uint16_t num_rx_total = 0;
57 /* Cast to structure, containing bonded device's port id and queue id */
58 struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)queue;
60 internals = bd_rx_q->dev_private;
62 switch (internals->mode) {
63 case BONDING_MODE_ROUND_ROBIN:
64 #ifdef RTE_MBUF_REFCNT
65 case BONDING_MODE_BROADCAST:
67 case BONDING_MODE_BALANCE:
68 for (i = 0; i < internals->active_slave_count && nb_pkts; i++) {
69 /* Offset of pointer to *bufs increases as packets are received
70 * from other slaves */
71 num_rx_slave = rte_eth_rx_burst(internals->active_slaves[i],
72 bd_rx_q->queue_id, bufs + num_rx_total, nb_pkts);
74 num_rx_total += num_rx_slave;
75 nb_pkts -= num_rx_slave;
79 case BONDING_MODE_ACTIVE_BACKUP:
80 num_rx_slave = rte_eth_rx_burst(internals->current_primary_port,
81 bd_rx_q->queue_id, bufs, nb_pkts);
83 num_rx_total = num_rx_slave;
90 bond_ethdev_tx_round_robin(void *queue, struct rte_mbuf **bufs,
93 struct bond_dev_private *dev_private;
94 struct bond_tx_queue *bd_tx_q;
96 struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS][nb_pkts];
97 uint16_t slave_nb_pkts[RTE_MAX_ETHPORTS] = { 0 };
99 uint8_t num_of_slaves;
100 uint8_t slaves[RTE_MAX_ETHPORTS];
102 uint16_t num_tx_total = 0;
104 static int slave_idx = 0;
107 bd_tx_q = (struct bond_tx_queue *)queue;
108 dev_private = bd_tx_q->dev_private;
110 /* Copy slave list to protect against slave up/down changes during tx
112 num_of_slaves = dev_private->active_slave_count;
113 memcpy(slaves, dev_private->active_slaves,
114 sizeof(dev_private->active_slaves[0]) * num_of_slaves);
116 if (num_of_slaves < 1)
119 /* Populate slaves mbuf with which packets are to be sent on it */
120 for (i = 0; i < nb_pkts; i++) {
121 cs_idx = (slave_idx + i) % num_of_slaves;
122 slave_bufs[cs_idx][(slave_nb_pkts[cs_idx])++] = bufs[i];
125 /* increment current slave index so the next call to tx burst starts on the
127 slave_idx = ++cs_idx;
129 /* Send packet burst on each slave device */
130 for (i = 0; i < num_of_slaves; i++)
131 if (slave_nb_pkts[i] > 0)
132 num_tx_total += rte_eth_tx_burst(slaves[i],
133 bd_tx_q->queue_id, slave_bufs[i], slave_nb_pkts[i]);
139 bond_ethdev_tx_active_backup(void *queue,
140 struct rte_mbuf **bufs, uint16_t nb_pkts)
142 struct bond_dev_private *internals;
143 struct bond_tx_queue *bd_tx_q;
145 bd_tx_q = (struct bond_tx_queue *)queue;
146 internals = bd_tx_q->dev_private;
148 if (internals->active_slave_count < 1)
151 return rte_eth_tx_burst(internals->current_primary_port, bd_tx_q->queue_id,
155 static inline uint16_t
156 ether_hash(struct ether_hdr *eth_hdr)
158 uint16_t *word_src_addr = (uint16_t *)eth_hdr->s_addr.addr_bytes;
159 uint16_t *word_dst_addr = (uint16_t *)eth_hdr->d_addr.addr_bytes;
161 return (word_src_addr[0] ^ word_dst_addr[0]) ^
162 (word_src_addr[1] ^ word_dst_addr[1]) ^
163 (word_src_addr[2] ^ word_dst_addr[2]);
166 static inline uint32_t
167 ipv4_hash(struct ipv4_hdr *ipv4_hdr)
169 return (ipv4_hdr->src_addr ^ ipv4_hdr->dst_addr);
172 static inline uint32_t
173 ipv6_hash(struct ipv6_hdr *ipv6_hdr)
175 uint32_t *word_src_addr = (uint32_t *)&(ipv6_hdr->src_addr[0]);
176 uint32_t *word_dst_addr = (uint32_t *)&(ipv6_hdr->dst_addr[0]);
178 return (word_src_addr[0] ^ word_dst_addr[0]) ^
179 (word_src_addr[1] ^ word_dst_addr[1]) ^
180 (word_src_addr[2] ^ word_dst_addr[2]) ^
181 (word_src_addr[3] ^ word_dst_addr[3]);
185 udp_hash(struct udp_hdr *hdr)
187 return hdr->src_port ^ hdr->dst_port;
190 static inline uint16_t
191 xmit_slave_hash(const struct rte_mbuf *buf, uint8_t slave_count, uint8_t policy)
193 struct ether_hdr *eth_hdr;
194 struct udp_hdr *udp_hdr;
195 size_t eth_offset = 0;
198 if (slave_count == 1)
202 case BALANCE_XMIT_POLICY_LAYER2:
203 eth_hdr = rte_pktmbuf_mtod(buf, struct ether_hdr *);
205 hash = ether_hash(eth_hdr);
207 return hash % slave_count;
209 case BALANCE_XMIT_POLICY_LAYER23:
210 eth_hdr = rte_pktmbuf_mtod(buf, struct ether_hdr *);
212 if (buf->ol_flags & PKT_RX_VLAN_PKT)
213 eth_offset = sizeof(struct ether_hdr) + sizeof(struct vlan_hdr);
215 eth_offset = sizeof(struct ether_hdr);
217 if (buf->ol_flags & PKT_RX_IPV4_HDR) {
218 struct ipv4_hdr *ipv4_hdr;
219 ipv4_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(buf,
220 unsigned char *) + eth_offset);
222 hash = ether_hash(eth_hdr) ^ ipv4_hash(ipv4_hdr);
225 struct ipv6_hdr *ipv6_hdr;
227 ipv6_hdr = (struct ipv6_hdr *)(rte_pktmbuf_mtod(buf,
228 unsigned char *) + eth_offset);
230 hash = ether_hash(eth_hdr) ^ ipv6_hash(ipv6_hdr);
234 case BALANCE_XMIT_POLICY_LAYER34:
235 if (buf->ol_flags & PKT_RX_VLAN_PKT)
236 eth_offset = sizeof(struct ether_hdr) + sizeof(struct vlan_hdr);
238 eth_offset = sizeof(struct ether_hdr);
240 if (buf->ol_flags & PKT_RX_IPV4_HDR) {
241 struct ipv4_hdr *ipv4_hdr = (struct ipv4_hdr *)
242 (rte_pktmbuf_mtod(buf, unsigned char *) + eth_offset);
244 if (ipv4_hdr->next_proto_id == IPPROTO_UDP) {
245 udp_hdr = (struct udp_hdr *)
246 (rte_pktmbuf_mtod(buf, unsigned char *) + eth_offset +
247 sizeof(struct ipv4_hdr));
248 hash = ipv4_hash(ipv4_hdr) ^ udp_hash(udp_hdr);
250 hash = ipv4_hash(ipv4_hdr);
253 struct ipv6_hdr *ipv6_hdr = (struct ipv6_hdr *)
254 (rte_pktmbuf_mtod(buf, unsigned char *) + eth_offset);
256 if (ipv6_hdr->proto == IPPROTO_UDP) {
257 udp_hdr = (struct udp_hdr *)
258 (rte_pktmbuf_mtod(buf, unsigned char *) + eth_offset +
259 sizeof(struct ipv6_hdr));
260 hash = ipv6_hash(ipv6_hdr) ^ udp_hash(udp_hdr);
262 hash = ipv6_hash(ipv6_hdr);
271 return hash % slave_count;
275 bond_ethdev_tx_balance(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
277 struct bond_dev_private *internals;
278 struct bond_tx_queue *bd_tx_q;
280 uint8_t num_of_slaves;
281 uint8_t slaves[RTE_MAX_ETHPORTS];
283 uint16_t num_tx_total = 0;
287 struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS][nb_pkts];
288 uint16_t slave_nb_pkts[RTE_MAX_ETHPORTS] = { 0 };
290 bd_tx_q = (struct bond_tx_queue *)queue;
291 internals = bd_tx_q->dev_private;
293 /* Copy slave list to protect against slave up/down changes during tx
295 num_of_slaves = internals->active_slave_count;
296 memcpy(slaves, internals->active_slaves,
297 sizeof(internals->active_slaves[0]) * num_of_slaves);
299 if (num_of_slaves < 1)
302 /* Populate slaves mbuf with the packets which are to be sent on it */
303 for (i = 0; i < nb_pkts; i++) {
304 /* Select output slave using hash based on xmit policy */
305 op_slave_id = xmit_slave_hash(bufs[i], num_of_slaves,
306 internals->balance_xmit_policy);
308 /* Populate slave mbuf arrays with mbufs for that slave */
309 slave_bufs[op_slave_id][slave_nb_pkts[op_slave_id]++] = bufs[i];
312 /* Send packet burst on each slave device */
313 for (i = 0; i < num_of_slaves; i++) {
314 if (slave_nb_pkts[i] > 0) {
315 num_tx_total += rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
316 slave_bufs[i], slave_nb_pkts[i]);
323 #ifdef RTE_MBUF_REFCNT
325 bond_ethdev_tx_burst_broadcast(void *queue, struct rte_mbuf **bufs,
328 struct bond_dev_private *internals;
329 struct bond_tx_queue *bd_tx_q;
331 uint8_t num_of_slaves;
332 uint8_t slaves[RTE_MAX_ETHPORTS];
334 uint16_t num_tx_total = 0;
338 bd_tx_q = (struct bond_tx_queue *)queue;
339 internals = bd_tx_q->dev_private;
341 /* Copy slave list to protect against slave up/down changes during tx
343 num_of_slaves = internals->active_slave_count;
344 memcpy(slaves, internals->active_slaves,
345 sizeof(internals->active_slaves[0]) * num_of_slaves);
347 if (num_of_slaves < 1)
350 /* Increment reference count on mbufs */
351 for (i = 0; i < nb_pkts; i++)
352 rte_mbuf_refcnt_update(bufs[i], num_of_slaves - 1);
354 /* Transmit burst on each active slave */
355 for (i = 0; i < num_of_slaves; i++)
356 num_tx_total += rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
364 link_properties_set(struct rte_eth_dev *bonded_eth_dev,
365 struct rte_eth_link *slave_dev_link)
367 struct rte_eth_link *bonded_dev_link = &bonded_eth_dev->data->dev_link;
368 struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
370 if (slave_dev_link->link_status &&
371 bonded_eth_dev->data->dev_started) {
372 bonded_dev_link->link_duplex = slave_dev_link->link_duplex;
373 bonded_dev_link->link_speed = slave_dev_link->link_speed;
375 internals->link_props_set = 1;
380 link_properties_reset(struct rte_eth_dev *bonded_eth_dev)
382 struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
384 memset(&(bonded_eth_dev->data->dev_link), 0,
385 sizeof(bonded_eth_dev->data->dev_link));
387 internals->link_props_set = 0;
391 link_properties_valid(struct rte_eth_link *bonded_dev_link,
392 struct rte_eth_link *slave_dev_link)
394 if (bonded_dev_link->link_duplex != slave_dev_link->link_duplex ||
395 bonded_dev_link->link_speed != slave_dev_link->link_speed)
402 mac_address_set(struct rte_eth_dev *eth_dev, struct ether_addr *new_mac_addr)
404 struct ether_addr *mac_addr;
406 mac_addr = eth_dev->data->mac_addrs;
408 if (eth_dev == NULL) {
409 RTE_LOG(ERR, PMD, "%s: NULL pointer eth_dev specified\n", __func__);
413 if (new_mac_addr == NULL) {
414 RTE_LOG(ERR, PMD, "%s: NULL pointer MAC specified\n", __func__);
418 /* if new MAC is different to current MAC then update */
419 if (memcmp(mac_addr, new_mac_addr, sizeof(*mac_addr)) != 0)
420 memcpy(mac_addr, new_mac_addr, sizeof(*mac_addr));
426 mac_address_slaves_update(struct rte_eth_dev *bonded_eth_dev)
428 struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
431 /* Update slave devices MAC addresses */
432 if (internals->slave_count < 1)
435 switch (internals->mode) {
436 case BONDING_MODE_ROUND_ROBIN:
437 case BONDING_MODE_BALANCE:
438 #ifdef RTE_MBUF_REFCNT
439 case BONDING_MODE_BROADCAST:
441 for (i = 0; i < internals->slave_count; i++) {
442 if (mac_address_set(&rte_eth_devices[internals->slaves[i]],
443 bonded_eth_dev->data->mac_addrs)) {
445 "%s: Failed to update port Id %d MAC address\n",
446 __func__, internals->slaves[i]);
451 case BONDING_MODE_ACTIVE_BACKUP:
453 for (i = 0; i < internals->slave_count; i++) {
454 if (internals->slaves[i] == internals->current_primary_port) {
455 if (mac_address_set(&rte_eth_devices[internals->primary_port],
456 bonded_eth_dev->data->mac_addrs)) {
458 "%s: Failed to update port Id %d MAC address\n",
459 __func__, internals->current_primary_port);
462 struct slave_conf *conf =
463 slave_config_get(internals, internals->slaves[i]);
465 if (mac_address_set(&rte_eth_devices[internals->slaves[i]],
468 "%s: Failed to update port Id %d MAC address\n",
469 __func__, internals->slaves[i]);
481 bond_ethdev_mode_set(struct rte_eth_dev *eth_dev, int mode)
483 struct bond_dev_private *internals;
485 internals = eth_dev->data->dev_private;
488 case BONDING_MODE_ROUND_ROBIN:
489 eth_dev->tx_pkt_burst = bond_ethdev_tx_round_robin;
491 case BONDING_MODE_ACTIVE_BACKUP:
492 eth_dev->tx_pkt_burst = bond_ethdev_tx_active_backup;
494 case BONDING_MODE_BALANCE:
495 eth_dev->tx_pkt_burst = bond_ethdev_tx_balance;
497 #ifdef RTE_MBUF_REFCNT
498 case BONDING_MODE_BROADCAST:
499 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_broadcast;
506 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst;
507 internals->mode = mode;
513 slave_configure(struct rte_eth_dev *bonded_eth_dev,
514 struct rte_eth_dev *slave_eth_dev)
516 struct bond_rx_queue *bd_rx_q;
517 struct bond_tx_queue *bd_tx_q;
522 rte_eth_dev_stop(slave_eth_dev->data->port_id);
524 /* Enable interrupts on slave device */
525 slave_eth_dev->data->dev_conf.intr_conf.lsc = 1;
527 if (rte_eth_dev_configure(slave_eth_dev->data->port_id,
528 bonded_eth_dev->data->nb_rx_queues,
529 bonded_eth_dev->data->nb_tx_queues,
530 &(slave_eth_dev->data->dev_conf)) != 0) {
531 RTE_LOG(ERR, PMD, "Cannot configure slave device: port=%u\n",
532 slave_eth_dev->data->port_id);
536 /* Setup Rx Queues */
537 for (q_id = 0; q_id < bonded_eth_dev->data->nb_rx_queues; q_id++) {
538 bd_rx_q = (struct bond_rx_queue *)bonded_eth_dev->data->rx_queues[q_id];
540 if (rte_eth_rx_queue_setup(slave_eth_dev->data->port_id, q_id,
542 rte_eth_dev_socket_id(slave_eth_dev->data->port_id),
543 &(bd_rx_q->rx_conf), bd_rx_q->mb_pool) != 0) {
544 RTE_LOG(ERR, PMD, "rte_eth_rx_queue_setup: port=%d queue_id %d\n",
545 slave_eth_dev->data->port_id, q_id);
550 /* Setup Tx Queues */
551 for (q_id = 0; q_id < bonded_eth_dev->data->nb_tx_queues; q_id++) {
552 bd_tx_q = (struct bond_tx_queue *)bonded_eth_dev->data->tx_queues[q_id];
554 if (rte_eth_tx_queue_setup(slave_eth_dev->data->port_id, q_id,
556 rte_eth_dev_socket_id(slave_eth_dev->data->port_id),
557 &bd_tx_q->tx_conf) != 0) {
558 RTE_LOG(ERR, PMD, "rte_eth_tx_queue_setup: port=%d queue_id %d\n",
559 slave_eth_dev->data->port_id, q_id);
565 if (rte_eth_dev_start(slave_eth_dev->data->port_id) != 0) {
566 RTE_LOG(ERR, PMD, "rte_eth_dev_start: port=%u\n",
567 slave_eth_dev->data->port_id);
575 slave_config_get(struct bond_dev_private *internals, uint8_t slave_port_id)
579 for (i = 0; i < internals->slave_count; i++) {
580 if (internals->presisted_slaves_conf[i].port_id == slave_port_id)
581 return &internals->presisted_slaves_conf[i];
587 slave_config_clear(struct bond_dev_private *internals,
588 struct rte_eth_dev *slave_eth_dev)
592 for (i = 0; i < internals->slave_count; i++) {
593 if (internals->presisted_slaves_conf[i].port_id ==
594 slave_eth_dev->data->port_id) {
596 memset(&internals->presisted_slaves_conf[i], 0,
597 sizeof(internals->presisted_slaves_conf[i]));
599 if (found && i < (internals->slave_count - 1)) {
600 memcpy(&internals->presisted_slaves_conf[i],
601 &internals->presisted_slaves_conf[i+1],
602 sizeof(internals->presisted_slaves_conf[i]));
608 slave_config_store(struct bond_dev_private *internals,
609 struct rte_eth_dev *slave_eth_dev)
611 struct slave_conf *presisted_slave_conf =
612 &internals->presisted_slaves_conf[internals->slave_count];
614 presisted_slave_conf->port_id = slave_eth_dev->data->port_id;
616 memcpy(&(presisted_slave_conf->mac_addr), slave_eth_dev->data->mac_addrs,
617 sizeof(struct ether_addr));
621 bond_ethdev_primary_set(struct bond_dev_private *internals,
622 uint8_t slave_port_id)
626 if (internals->active_slave_count < 1)
627 internals->current_primary_port = slave_port_id;
629 /* Search bonded device slave ports for new proposed primary port */
630 for (i = 0; i < internals->active_slave_count; i++) {
631 if (internals->active_slaves[i] == slave_port_id)
632 internals->current_primary_port = slave_port_id;
637 bond_ethdev_promiscuous_enable(struct rte_eth_dev *eth_dev);
640 bond_ethdev_start(struct rte_eth_dev *eth_dev)
642 struct bond_dev_private *internals;
645 /* slave eth dev will be started by bonded device */
646 if (valid_bonded_ethdev(eth_dev)) {
648 "%s: user tried to explicitly start a slave eth_dev (%d) of the bonded eth_dev\n",
649 __func__, eth_dev->data->port_id);
653 eth_dev->data->dev_link.link_status = 1;
654 eth_dev->data->dev_started = 1;
656 internals = eth_dev->data->dev_private;
658 if (internals->slave_count == 0) {
660 "%s: Cannot start port since there are no slave devices\n",
665 if (internals->user_defined_mac == 0) {
666 struct slave_conf *conf = slave_config_get(internals,
667 internals->primary_port);
669 if (mac_address_set(eth_dev, &(conf->mac_addr)) != 0) {
671 "bonded port (%d) failed to update mac address",
672 eth_dev->data->port_id);
677 /* Update all slave devices MACs*/
678 if (mac_address_slaves_update(eth_dev) != 0)
681 /* If bonded device is configure in promiscuous mode then re-apply config */
682 if (internals->promiscuous_en)
683 bond_ethdev_promiscuous_enable(eth_dev);
685 /* Reconfigure each slave device if starting bonded device */
686 for (i = 0; i < internals->slave_count; i++) {
687 if (slave_configure(eth_dev, &(rte_eth_devices[internals->slaves[i]]))
689 RTE_LOG(ERR, PMD, "bonded port "
690 "(%d) failed to reconfigure slave device (%d)\n)",
691 eth_dev->data->port_id, internals->slaves[i]);
696 if (internals->user_defined_primary_port)
697 bond_ethdev_primary_set(internals, internals->primary_port);
703 bond_ethdev_stop(struct rte_eth_dev *eth_dev)
705 struct bond_dev_private *internals = eth_dev->data->dev_private;
707 internals->active_slave_count = 0;
709 eth_dev->data->dev_link.link_status = 0;
710 eth_dev->data->dev_started = 0;
714 bond_ethdev_close(struct rte_eth_dev *dev __rte_unused)
718 /* forward declaration */
719 static int bond_ethdev_configure(struct rte_eth_dev *dev);
722 bond_ethdev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
724 dev_info->driver_name = driver_name;
725 dev_info->max_mac_addrs = 1;
727 dev_info->max_rx_pktlen = (uint32_t)2048;
729 dev_info->max_rx_queues = (uint16_t)128;
730 dev_info->max_tx_queues = (uint16_t)512;
732 dev_info->min_rx_bufsize = 0;
733 dev_info->pci_dev = dev->pci_dev;
737 bond_ethdev_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
738 uint16_t nb_rx_desc, unsigned int socket_id __rte_unused,
739 const struct rte_eth_rxconf *rx_conf, struct rte_mempool *mb_pool)
741 struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)
742 rte_zmalloc_socket(NULL, sizeof(struct bond_rx_queue),
743 0, dev->pci_dev->numa_node);
747 bd_rx_q->queue_id = rx_queue_id;
748 bd_rx_q->dev_private = dev->data->dev_private;
750 bd_rx_q->nb_rx_desc = nb_rx_desc;
752 memcpy(&(bd_rx_q->rx_conf), rx_conf, sizeof(struct rte_eth_rxconf));
753 bd_rx_q->mb_pool = mb_pool;
755 dev->data->rx_queues[rx_queue_id] = bd_rx_q;
761 bond_ethdev_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
762 uint16_t nb_tx_desc, unsigned int socket_id __rte_unused,
763 const struct rte_eth_txconf *tx_conf)
765 struct bond_tx_queue *bd_tx_q = (struct bond_tx_queue *)
766 rte_zmalloc_socket(NULL, sizeof(struct bond_tx_queue),
767 0, dev->pci_dev->numa_node);
772 bd_tx_q->queue_id = tx_queue_id;
773 bd_tx_q->dev_private = dev->data->dev_private;
775 bd_tx_q->nb_tx_desc = nb_tx_desc;
776 memcpy(&(bd_tx_q->tx_conf), tx_conf, sizeof(bd_tx_q->tx_conf));
778 dev->data->tx_queues[tx_queue_id] = bd_tx_q;
784 bond_ethdev_rx_queue_release(void *queue)
793 bond_ethdev_tx_queue_release(void *queue)
802 bond_ethdev_link_update(struct rte_eth_dev *bonded_eth_dev,
803 int wait_to_complete)
805 struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
807 if (!bonded_eth_dev->data->dev_started ||
808 internals->active_slave_count == 0) {
809 bonded_eth_dev->data->dev_link.link_status = 0;
812 struct rte_eth_dev *slave_eth_dev;
815 for (i = 0; i < internals->active_slave_count; i++) {
816 slave_eth_dev = &rte_eth_devices[internals->active_slaves[i]];
818 (*slave_eth_dev->dev_ops->link_update)(slave_eth_dev,
820 if (slave_eth_dev->data->dev_link.link_status == 1) {
826 bonded_eth_dev->data->dev_link.link_status = link_up;
833 bond_ethdev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
835 struct bond_dev_private *internals = dev->data->dev_private;
836 struct rte_eth_stats slave_stats;
840 /* clear bonded stats before populating from slaves */
841 memset(stats, 0, sizeof(*stats));
843 for (i = 0; i < internals->slave_count; i++) {
844 rte_eth_stats_get(internals->slaves[i], &slave_stats);
846 stats->ipackets += slave_stats.ipackets;
847 stats->opackets += slave_stats.opackets;
848 stats->ibytes += slave_stats.ibytes;
849 stats->obytes += slave_stats.obytes;
850 stats->ierrors += slave_stats.ierrors;
851 stats->oerrors += slave_stats.oerrors;
852 stats->imcasts += slave_stats.imcasts;
853 stats->rx_nombuf += slave_stats.rx_nombuf;
854 stats->fdirmatch += slave_stats.fdirmatch;
855 stats->fdirmiss += slave_stats.fdirmiss;
856 stats->tx_pause_xon += slave_stats.tx_pause_xon;
857 stats->rx_pause_xon += slave_stats.rx_pause_xon;
858 stats->tx_pause_xoff += slave_stats.tx_pause_xoff;
859 stats->rx_pause_xoff += slave_stats.rx_pause_xoff;
864 bond_ethdev_stats_reset(struct rte_eth_dev *dev)
866 struct bond_dev_private *internals = dev->data->dev_private;
869 for (i = 0; i < internals->slave_count; i++)
870 rte_eth_stats_reset(internals->slaves[i]);
874 bond_ethdev_promiscuous_enable(struct rte_eth_dev *eth_dev)
876 struct bond_dev_private *internals = eth_dev->data->dev_private;
879 internals->promiscuous_en = 1;
881 switch (internals->mode) {
882 /* Promiscuous mode is propagated to all slaves */
883 case BONDING_MODE_ROUND_ROBIN:
884 case BONDING_MODE_BALANCE:
885 #ifdef RTE_MBUF_REFCNT
886 case BONDING_MODE_BROADCAST:
888 for (i = 0; i < internals->slave_count; i++)
889 rte_eth_promiscuous_enable(internals->slaves[i]);
891 /* Promiscuous mode is propagated only to primary slave */
892 case BONDING_MODE_ACTIVE_BACKUP:
894 rte_eth_promiscuous_enable(internals->current_primary_port);
900 bond_ethdev_promiscuous_disable(struct rte_eth_dev *dev)
902 struct bond_dev_private *internals = dev->data->dev_private;
905 internals->promiscuous_en = 0;
907 switch (internals->mode) {
908 /* Promiscuous mode is propagated to all slaves */
909 case BONDING_MODE_ROUND_ROBIN:
910 case BONDING_MODE_BALANCE:
911 #ifdef RTE_MBUF_REFCNT
912 case BONDING_MODE_BROADCAST:
914 for (i = 0; i < internals->slave_count; i++)
915 rte_eth_promiscuous_disable(internals->slaves[i]);
917 /* Promiscuous mode is propagated only to primary slave */
918 case BONDING_MODE_ACTIVE_BACKUP:
920 rte_eth_promiscuous_disable(internals->current_primary_port);
925 bond_ethdev_lsc_event_callback(uint8_t port_id, enum rte_eth_event_type type,
928 struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
929 struct bond_dev_private *internals;
930 struct rte_eth_link link;
932 int i, valid_slave = 0, active_pos = -1;
933 uint8_t lsc_flag = 0;
935 if (type != RTE_ETH_EVENT_INTR_LSC || param == NULL)
938 bonded_eth_dev = &rte_eth_devices[*(uint8_t *)param];
939 slave_eth_dev = &rte_eth_devices[port_id];
941 if (valid_bonded_ethdev(bonded_eth_dev))
944 internals = bonded_eth_dev->data->dev_private;
946 /* If the device isn't started don't handle interrupts */
947 if (!bonded_eth_dev->data->dev_started)
950 /* verify that port_id is a valid slave of bonded port */
951 for (i = 0; i < internals->slave_count; i++) {
952 if (internals->slaves[i] == port_id) {
961 /* Search for port in active port list */
962 for (i = 0; i < internals->active_slave_count; i++) {
963 if (port_id == internals->active_slaves[i]) {
969 rte_eth_link_get_nowait(port_id, &link);
970 if (link.link_status) {
974 /* if no active slave ports then set this port to be primary port */
975 if (internals->active_slave_count < 1) {
976 /* If first active slave, then change link status */
977 bonded_eth_dev->data->dev_link.link_status = 1;
978 internals->current_primary_port = port_id;
981 /* Inherit eth dev link properties from first active slave */
982 link_properties_set(bonded_eth_dev,
983 &(slave_eth_dev->data->dev_link));
985 internals->active_slaves[internals->active_slave_count++] = port_id;
987 /* If user has defined the primary port then default to using it */
988 if (internals->user_defined_primary_port &&
989 internals->primary_port == port_id)
990 bond_ethdev_primary_set(internals, port_id);
995 /* Remove from active slave list */
996 for (i = active_pos; i < (internals->active_slave_count - 1); i++)
997 internals->active_slaves[i] = internals->active_slaves[i+1];
999 internals->active_slave_count--;
1001 /* No active slaves, change link status to down and reset other
1002 * link properties */
1003 if (internals->active_slave_count < 1) {
1005 bonded_eth_dev->data->dev_link.link_status = 0;
1007 link_properties_reset(bonded_eth_dev);
1010 /* Update primary id, take first active slave from list or if none
1011 * available set to -1 */
1012 if (port_id == internals->current_primary_port) {
1013 if (internals->active_slave_count > 0)
1014 bond_ethdev_primary_set(internals,
1015 internals->active_slaves[0]);
1017 internals->current_primary_port = internals->primary_port;
1022 _rte_eth_dev_callback_process(bonded_eth_dev, RTE_ETH_EVENT_INTR_LSC);
1025 struct eth_dev_ops default_dev_ops = {
1026 .dev_start = bond_ethdev_start,
1027 .dev_stop = bond_ethdev_stop,
1028 .dev_close = bond_ethdev_close,
1029 .dev_configure = bond_ethdev_configure,
1030 .dev_infos_get = bond_ethdev_info,
1031 .rx_queue_setup = bond_ethdev_rx_queue_setup,
1032 .tx_queue_setup = bond_ethdev_tx_queue_setup,
1033 .rx_queue_release = bond_ethdev_rx_queue_release,
1034 .tx_queue_release = bond_ethdev_tx_queue_release,
1035 .link_update = bond_ethdev_link_update,
1036 .stats_get = bond_ethdev_stats_get,
1037 .stats_reset = bond_ethdev_stats_reset,
1038 .promiscuous_enable = bond_ethdev_promiscuous_enable,
1039 .promiscuous_disable = bond_ethdev_promiscuous_disable
1043 bond_init(const char *name, const char *params)
1045 struct bond_dev_private *internals;
1046 struct rte_kvargs *kvlist;
1047 uint8_t bonding_mode, socket_id;
1048 int arg_count, port_id;
1050 RTE_LOG(INFO, EAL, "Initializing pmd_bond for %s\n", name);
1052 kvlist = rte_kvargs_parse(params, pmd_bond_init_valid_arguments);
1056 /* Parse link bonding mode */
1057 if (rte_kvargs_count(kvlist, PMD_BOND_MODE_KVARG) == 1) {
1058 if (rte_kvargs_process(kvlist, PMD_BOND_MODE_KVARG,
1059 &bond_ethdev_parse_slave_mode_kvarg, &bonding_mode) != 0) {
1060 RTE_LOG(ERR, EAL, "Invalid mode for bonded device %s\n", name);
1065 "Mode must be specified only once for bonded device %s\n",
1070 /* Parse socket id to create bonding device on */
1071 arg_count = rte_kvargs_count(kvlist, PMD_BOND_SOCKET_ID_KVARG);
1072 if (arg_count == 1) {
1073 if (rte_kvargs_process(kvlist, PMD_BOND_SOCKET_ID_KVARG,
1074 &bond_ethdev_parse_socket_id_kvarg, &socket_id) != 0) {
1076 "Invalid socket Id specified for bonded device %s\n",
1080 } else if (arg_count > 1) {
1082 "Socket Id can be specified only once for bonded device %s\n",
1086 socket_id = rte_socket_id();
1089 /* Create link bonding eth device */
1090 port_id = rte_eth_bond_create(name, bonding_mode, socket_id);
1093 "Failed to create socket %s in mode %u on socket %u.\n",
1094 name, bonding_mode, socket_id);
1097 internals = rte_eth_devices[port_id].data->dev_private;
1098 internals->kvlist = kvlist;
1101 "Create bonded device %s on port %d in mode %u on socket %u.\n",
1102 name, port_id, bonding_mode, socket_id);
1106 /* this part will resolve the slave portids after all the other pdev and vdev
1107 * have been allocated */
1109 bond_ethdev_configure(struct rte_eth_dev *dev)
1111 char *name = dev->data->name;
1112 struct bond_dev_private *internals = dev->data->dev_private;
1113 struct rte_kvargs *kvlist = internals->kvlist;
1114 int arg_count, port_id = dev - rte_eth_devices;
1117 * if no kvlist, it means that this bonded device has been created
1118 * through the bonding api.
1123 /* Parse MAC address for bonded device */
1124 arg_count = rte_kvargs_count(kvlist, PMD_BOND_MAC_ADDR_KVARG);
1125 if (arg_count == 1) {
1126 struct ether_addr bond_mac;
1128 if (rte_kvargs_process(kvlist, PMD_BOND_MAC_ADDR_KVARG,
1129 &bond_ethdev_parse_bond_mac_addr_kvarg, &bond_mac) < 0) {
1130 RTE_LOG(INFO, EAL, "Invalid mac address for bonded device %s\n",
1135 /* Set MAC address */
1136 if (rte_eth_bond_mac_address_set(port_id, &bond_mac) != 0) {
1138 "Failed to set mac address on bonded device %s\n",
1142 } else if (arg_count > 1) {
1144 "MAC address can be specified only once for bonded device %s\n",
1149 /* Parse/set balance mode transmit policy */
1150 arg_count = rte_kvargs_count(kvlist, PMD_BOND_XMIT_POLICY_KVARG);
1151 if (arg_count == 1) {
1152 uint8_t xmit_policy;
1154 if (rte_kvargs_process(kvlist, PMD_BOND_XMIT_POLICY_KVARG,
1155 &bond_ethdev_parse_balance_xmit_policy_kvarg, &xmit_policy) !=
1158 "Invalid xmit policy specified for bonded device %s\n",
1163 /* Set balance mode transmit policy*/
1164 if (rte_eth_bond_xmit_policy_set(port_id, xmit_policy) != 0) {
1166 "Failed to set balance xmit policy on bonded device %s\n",
1170 } else if (arg_count > 1) {
1172 "Transmit policy can be specified only once for bonded device %s\n",
1177 /* Parse/add slave ports to bonded device */
1178 if (rte_kvargs_count(kvlist, PMD_BOND_SLAVE_PORT_KVARG) > 0) {
1179 struct bond_ethdev_slave_ports slave_ports;
1182 memset(&slave_ports, 0, sizeof(slave_ports));
1184 if (rte_kvargs_process(kvlist, PMD_BOND_SLAVE_PORT_KVARG,
1185 &bond_ethdev_parse_slave_port_kvarg, &slave_ports) != 0) {
1187 "Failed to parse slave ports for bonded device %s\n",
1192 for (i = 0; i < slave_ports.slave_count; i++) {
1193 if (rte_eth_bond_slave_add(port_id, slave_ports.slaves[i]) != 0) {
1195 "Failed to add port %d as slave to bonded device %s\n",
1196 slave_ports.slaves[i], name);
1201 RTE_LOG(INFO, EAL, "No slaves specified for bonded device %s\n", name);
1205 /* Parse/set primary slave port id*/
1206 arg_count = rte_kvargs_count(kvlist, PMD_BOND_PRIMARY_SLAVE_KVARG);
1207 if (arg_count == 1) {
1208 uint8_t primary_slave_port_id;
1210 if (rte_kvargs_process(kvlist,
1211 PMD_BOND_PRIMARY_SLAVE_KVARG,
1212 &bond_ethdev_parse_primary_slave_port_id_kvarg,
1213 &primary_slave_port_id) < 0) {
1215 "Invalid primary slave port id specified for bonded device %s\n",
1220 /* Set balance mode transmit policy*/
1221 if (rte_eth_bond_primary_set(port_id, (uint8_t)primary_slave_port_id)
1224 "Failed to set primary slave port %d on bonded device %s\n",
1225 primary_slave_port_id, name);
1228 } else if (arg_count > 1) {
1230 "Primary slave can be specified only once for bonded device %s\n",
1238 static struct rte_driver bond_drv = {
1244 PMD_REGISTER_DRIVER(bond_drv);