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 case BONDING_MODE_BROADCAST:
65 case BONDING_MODE_BALANCE:
66 for (i = 0; i < internals->active_slave_count && nb_pkts; i++) {
67 /* Offset of pointer to *bufs increases as packets are received
68 * from other slaves */
69 num_rx_slave = rte_eth_rx_burst(internals->active_slaves[i],
70 bd_rx_q->queue_id, bufs + num_rx_total, nb_pkts);
72 num_rx_total += num_rx_slave;
73 nb_pkts -= num_rx_slave;
77 case BONDING_MODE_ACTIVE_BACKUP:
78 num_rx_slave = rte_eth_rx_burst(internals->current_primary_port,
79 bd_rx_q->queue_id, bufs, nb_pkts);
81 num_rx_total = num_rx_slave;
88 bond_ethdev_tx_round_robin(void *queue, struct rte_mbuf **bufs,
91 struct bond_dev_private *dev_private;
92 struct bond_tx_queue *bd_tx_q;
94 struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS][nb_pkts];
95 uint16_t slave_nb_pkts[RTE_MAX_ETHPORTS] = { 0 };
97 uint8_t num_of_slaves;
98 uint8_t slaves[RTE_MAX_ETHPORTS];
100 uint16_t num_tx_total = 0;
102 static int slave_idx = 0;
105 bd_tx_q = (struct bond_tx_queue *)queue;
106 dev_private = bd_tx_q->dev_private;
108 /* Copy slave list to protect against slave up/down changes during tx
110 num_of_slaves = dev_private->active_slave_count;
111 memcpy(slaves, dev_private->active_slaves,
112 sizeof(dev_private->active_slaves[0]) * num_of_slaves);
114 if (num_of_slaves < 1)
117 /* Populate slaves mbuf with which packets are to be sent on it */
118 for (i = 0; i < nb_pkts; i++) {
119 cs_idx = (slave_idx + i) % num_of_slaves;
120 slave_bufs[cs_idx][(slave_nb_pkts[cs_idx])++] = bufs[i];
123 /* increment current slave index so the next call to tx burst starts on the
125 slave_idx = ++cs_idx;
127 /* Send packet burst on each slave device */
128 for (i = 0; i < num_of_slaves; i++)
129 if (slave_nb_pkts[i] > 0)
130 num_tx_total += rte_eth_tx_burst(slaves[i],
131 bd_tx_q->queue_id, slave_bufs[i], slave_nb_pkts[i]);
137 bond_ethdev_tx_active_backup(void *queue,
138 struct rte_mbuf **bufs, uint16_t nb_pkts)
140 struct bond_dev_private *internals;
141 struct bond_tx_queue *bd_tx_q;
143 bd_tx_q = (struct bond_tx_queue *)queue;
144 internals = bd_tx_q->dev_private;
146 if (internals->active_slave_count < 1)
149 return rte_eth_tx_burst(internals->current_primary_port, bd_tx_q->queue_id,
153 static inline uint16_t
154 ether_hash(struct ether_hdr *eth_hdr)
156 uint16_t *word_src_addr = (uint16_t *)eth_hdr->s_addr.addr_bytes;
157 uint16_t *word_dst_addr = (uint16_t *)eth_hdr->d_addr.addr_bytes;
159 return (word_src_addr[0] ^ word_dst_addr[0]) ^
160 (word_src_addr[1] ^ word_dst_addr[1]) ^
161 (word_src_addr[2] ^ word_dst_addr[2]);
164 static inline uint32_t
165 ipv4_hash(struct ipv4_hdr *ipv4_hdr)
167 return (ipv4_hdr->src_addr ^ ipv4_hdr->dst_addr);
170 static inline uint32_t
171 ipv6_hash(struct ipv6_hdr *ipv6_hdr)
173 uint32_t *word_src_addr = (uint32_t *)&(ipv6_hdr->src_addr[0]);
174 uint32_t *word_dst_addr = (uint32_t *)&(ipv6_hdr->dst_addr[0]);
176 return (word_src_addr[0] ^ word_dst_addr[0]) ^
177 (word_src_addr[1] ^ word_dst_addr[1]) ^
178 (word_src_addr[2] ^ word_dst_addr[2]) ^
179 (word_src_addr[3] ^ word_dst_addr[3]);
183 udp_hash(struct udp_hdr *hdr)
185 return hdr->src_port ^ hdr->dst_port;
188 static inline uint16_t
189 xmit_slave_hash(const struct rte_mbuf *buf, uint8_t slave_count, uint8_t policy)
191 struct ether_hdr *eth_hdr;
192 struct udp_hdr *udp_hdr;
193 size_t eth_offset = 0;
196 if (slave_count == 1)
200 case BALANCE_XMIT_POLICY_LAYER2:
201 eth_hdr = rte_pktmbuf_mtod(buf, struct ether_hdr *);
203 hash = ether_hash(eth_hdr);
205 return hash % slave_count;
207 case BALANCE_XMIT_POLICY_LAYER23:
208 eth_hdr = rte_pktmbuf_mtod(buf, struct ether_hdr *);
210 if (buf->ol_flags & PKT_RX_VLAN_PKT)
211 eth_offset = sizeof(struct ether_hdr) + sizeof(struct vlan_hdr);
213 eth_offset = sizeof(struct ether_hdr);
215 if (buf->ol_flags & PKT_RX_IPV4_HDR) {
216 struct ipv4_hdr *ipv4_hdr;
217 ipv4_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(buf,
218 unsigned char *) + eth_offset);
220 hash = ether_hash(eth_hdr) ^ ipv4_hash(ipv4_hdr);
223 struct ipv6_hdr *ipv6_hdr;
225 ipv6_hdr = (struct ipv6_hdr *)(rte_pktmbuf_mtod(buf,
226 unsigned char *) + eth_offset);
228 hash = ether_hash(eth_hdr) ^ ipv6_hash(ipv6_hdr);
232 case BALANCE_XMIT_POLICY_LAYER34:
233 if (buf->ol_flags & PKT_RX_VLAN_PKT)
234 eth_offset = sizeof(struct ether_hdr) + sizeof(struct vlan_hdr);
236 eth_offset = sizeof(struct ether_hdr);
238 if (buf->ol_flags & PKT_RX_IPV4_HDR) {
239 struct ipv4_hdr *ipv4_hdr = (struct ipv4_hdr *)
240 (rte_pktmbuf_mtod(buf, unsigned char *) + eth_offset);
242 if (ipv4_hdr->next_proto_id == IPPROTO_UDP) {
243 udp_hdr = (struct udp_hdr *)
244 (rte_pktmbuf_mtod(buf, unsigned char *) + eth_offset +
245 sizeof(struct ipv4_hdr));
246 hash = ipv4_hash(ipv4_hdr) ^ udp_hash(udp_hdr);
248 hash = ipv4_hash(ipv4_hdr);
251 struct ipv6_hdr *ipv6_hdr = (struct ipv6_hdr *)
252 (rte_pktmbuf_mtod(buf, unsigned char *) + eth_offset);
254 if (ipv6_hdr->proto == IPPROTO_UDP) {
255 udp_hdr = (struct udp_hdr *)
256 (rte_pktmbuf_mtod(buf, unsigned char *) + eth_offset +
257 sizeof(struct ipv6_hdr));
258 hash = ipv6_hash(ipv6_hdr) ^ udp_hash(udp_hdr);
260 hash = ipv6_hash(ipv6_hdr);
269 return hash % slave_count;
273 bond_ethdev_tx_balance(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
275 struct bond_dev_private *internals;
276 struct bond_tx_queue *bd_tx_q;
278 uint8_t num_of_slaves;
279 uint8_t slaves[RTE_MAX_ETHPORTS];
281 uint16_t num_tx_total = 0;
285 struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS][nb_pkts];
286 uint16_t slave_nb_pkts[RTE_MAX_ETHPORTS] = { 0 };
288 bd_tx_q = (struct bond_tx_queue *)queue;
289 internals = bd_tx_q->dev_private;
291 /* Copy slave list to protect against slave up/down changes during tx
293 num_of_slaves = internals->active_slave_count;
294 memcpy(slaves, internals->active_slaves,
295 sizeof(internals->active_slaves[0]) * num_of_slaves);
297 if (num_of_slaves < 1)
300 /* Populate slaves mbuf with the packets which are to be sent on it */
301 for (i = 0; i < nb_pkts; i++) {
302 /* Select output slave using hash based on xmit policy */
303 op_slave_id = xmit_slave_hash(bufs[i], num_of_slaves,
304 internals->balance_xmit_policy);
306 /* Populate slave mbuf arrays with mbufs for that slave */
307 slave_bufs[op_slave_id][slave_nb_pkts[op_slave_id]++] = bufs[i];
310 /* Send packet burst on each slave device */
311 for (i = 0; i < num_of_slaves; i++) {
312 if (slave_nb_pkts[i] > 0) {
313 num_tx_total += rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
314 slave_bufs[i], slave_nb_pkts[i]);
322 bond_ethdev_tx_burst_broadcast(void *queue, struct rte_mbuf **bufs,
325 struct bond_dev_private *internals;
326 struct bond_tx_queue *bd_tx_q;
328 uint8_t num_of_slaves;
329 uint8_t slaves[RTE_MAX_ETHPORTS];
331 uint16_t num_tx_total = 0;
335 bd_tx_q = (struct bond_tx_queue *)queue;
336 internals = bd_tx_q->dev_private;
338 /* Copy slave list to protect against slave up/down changes during tx
340 num_of_slaves = internals->active_slave_count;
341 memcpy(slaves, internals->active_slaves,
342 sizeof(internals->active_slaves[0]) * num_of_slaves);
344 if (num_of_slaves < 1)
347 /* Increment reference count on mbufs */
348 for (i = 0; i < nb_pkts; i++)
349 rte_mbuf_refcnt_update(bufs[i], num_of_slaves - 1);
351 /* Transmit burst on each active slave */
352 for (i = 0; i < num_of_slaves; i++)
353 num_tx_total += rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
360 link_properties_set(struct rte_eth_dev *bonded_eth_dev,
361 struct rte_eth_link *slave_dev_link)
363 struct rte_eth_link *bonded_dev_link = &bonded_eth_dev->data->dev_link;
364 struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
366 if (slave_dev_link->link_status &&
367 bonded_eth_dev->data->dev_started) {
368 bonded_dev_link->link_duplex = slave_dev_link->link_duplex;
369 bonded_dev_link->link_speed = slave_dev_link->link_speed;
371 internals->link_props_set = 1;
376 link_properties_reset(struct rte_eth_dev *bonded_eth_dev)
378 struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
380 memset(&(bonded_eth_dev->data->dev_link), 0,
381 sizeof(bonded_eth_dev->data->dev_link));
383 internals->link_props_set = 0;
387 link_properties_valid(struct rte_eth_link *bonded_dev_link,
388 struct rte_eth_link *slave_dev_link)
390 if (bonded_dev_link->link_duplex != slave_dev_link->link_duplex ||
391 bonded_dev_link->link_speed != slave_dev_link->link_speed)
398 mac_address_set(struct rte_eth_dev *eth_dev, struct ether_addr *new_mac_addr)
400 struct ether_addr *mac_addr;
402 mac_addr = eth_dev->data->mac_addrs;
404 if (eth_dev == NULL) {
405 RTE_LOG(ERR, PMD, "%s: NULL pointer eth_dev specified\n", __func__);
409 if (new_mac_addr == NULL) {
410 RTE_LOG(ERR, PMD, "%s: NULL pointer MAC specified\n", __func__);
414 /* if new MAC is different to current MAC then update */
415 if (memcmp(mac_addr, new_mac_addr, sizeof(*mac_addr)) != 0)
416 memcpy(mac_addr, new_mac_addr, sizeof(*mac_addr));
422 mac_address_slaves_update(struct rte_eth_dev *bonded_eth_dev)
424 struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
427 /* Update slave devices MAC addresses */
428 if (internals->slave_count < 1)
431 switch (internals->mode) {
432 case BONDING_MODE_ROUND_ROBIN:
433 case BONDING_MODE_BALANCE:
434 case BONDING_MODE_BROADCAST:
435 for (i = 0; i < internals->slave_count; i++) {
436 if (mac_address_set(&rte_eth_devices[internals->slaves[i]],
437 bonded_eth_dev->data->mac_addrs)) {
439 "%s: Failed to update port Id %d MAC address\n",
440 __func__, internals->slaves[i]);
445 case BONDING_MODE_ACTIVE_BACKUP:
447 for (i = 0; i < internals->slave_count; i++) {
448 if (internals->slaves[i] == internals->current_primary_port) {
449 if (mac_address_set(&rte_eth_devices[internals->primary_port],
450 bonded_eth_dev->data->mac_addrs)) {
452 "%s: Failed to update port Id %d MAC address\n",
453 __func__, internals->current_primary_port);
456 struct slave_conf *conf =
457 slave_config_get(internals, internals->slaves[i]);
459 if (mac_address_set(&rte_eth_devices[internals->slaves[i]],
462 "%s: Failed to update port Id %d MAC address\n",
463 __func__, internals->slaves[i]);
475 bond_ethdev_mode_set(struct rte_eth_dev *eth_dev, int mode)
477 struct bond_dev_private *internals;
479 internals = eth_dev->data->dev_private;
482 case BONDING_MODE_ROUND_ROBIN:
483 eth_dev->tx_pkt_burst = bond_ethdev_tx_round_robin;
485 case BONDING_MODE_ACTIVE_BACKUP:
486 eth_dev->tx_pkt_burst = bond_ethdev_tx_active_backup;
488 case BONDING_MODE_BALANCE:
489 eth_dev->tx_pkt_burst = bond_ethdev_tx_balance;
491 case BONDING_MODE_BROADCAST:
492 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_broadcast;
498 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst;
499 internals->mode = mode;
505 slave_configure(struct rte_eth_dev *bonded_eth_dev,
506 struct rte_eth_dev *slave_eth_dev)
508 struct bond_rx_queue *bd_rx_q;
509 struct bond_tx_queue *bd_tx_q;
514 rte_eth_dev_stop(slave_eth_dev->data->port_id);
516 /* Enable interrupts on slave device */
517 slave_eth_dev->data->dev_conf.intr_conf.lsc = 1;
519 if (rte_eth_dev_configure(slave_eth_dev->data->port_id,
520 bonded_eth_dev->data->nb_rx_queues,
521 bonded_eth_dev->data->nb_tx_queues,
522 &(slave_eth_dev->data->dev_conf)) != 0) {
523 RTE_LOG(ERR, PMD, "Cannot configure slave device: port=%u\n",
524 slave_eth_dev->data->port_id);
528 /* Setup Rx Queues */
529 for (q_id = 0; q_id < bonded_eth_dev->data->nb_rx_queues; q_id++) {
530 bd_rx_q = (struct bond_rx_queue *)bonded_eth_dev->data->rx_queues[q_id];
532 if (rte_eth_rx_queue_setup(slave_eth_dev->data->port_id, q_id,
534 rte_eth_dev_socket_id(slave_eth_dev->data->port_id),
535 &(bd_rx_q->rx_conf), bd_rx_q->mb_pool) != 0) {
536 RTE_LOG(ERR, PMD, "rte_eth_rx_queue_setup: port=%d queue_id %d\n",
537 slave_eth_dev->data->port_id, q_id);
542 /* Setup Tx Queues */
543 for (q_id = 0; q_id < bonded_eth_dev->data->nb_tx_queues; q_id++) {
544 bd_tx_q = (struct bond_tx_queue *)bonded_eth_dev->data->tx_queues[q_id];
546 if (rte_eth_tx_queue_setup(slave_eth_dev->data->port_id, q_id,
548 rte_eth_dev_socket_id(slave_eth_dev->data->port_id),
549 &bd_tx_q->tx_conf) != 0) {
550 RTE_LOG(ERR, PMD, "rte_eth_tx_queue_setup: port=%d queue_id %d\n",
551 slave_eth_dev->data->port_id, q_id);
557 if (rte_eth_dev_start(slave_eth_dev->data->port_id) != 0) {
558 RTE_LOG(ERR, PMD, "rte_eth_dev_start: port=%u\n",
559 slave_eth_dev->data->port_id);
567 slave_config_get(struct bond_dev_private *internals, uint8_t slave_port_id)
571 for (i = 0; i < internals->slave_count; i++) {
572 if (internals->presisted_slaves_conf[i].port_id == slave_port_id)
573 return &internals->presisted_slaves_conf[i];
579 slave_config_clear(struct bond_dev_private *internals,
580 struct rte_eth_dev *slave_eth_dev)
584 for (i = 0; i < internals->slave_count; i++) {
585 if (internals->presisted_slaves_conf[i].port_id ==
586 slave_eth_dev->data->port_id) {
588 memset(&internals->presisted_slaves_conf[i], 0,
589 sizeof(internals->presisted_slaves_conf[i]));
591 if (found && i < (internals->slave_count - 1)) {
592 memcpy(&internals->presisted_slaves_conf[i],
593 &internals->presisted_slaves_conf[i+1],
594 sizeof(internals->presisted_slaves_conf[i]));
600 slave_config_store(struct bond_dev_private *internals,
601 struct rte_eth_dev *slave_eth_dev)
603 struct slave_conf *presisted_slave_conf =
604 &internals->presisted_slaves_conf[internals->slave_count];
606 presisted_slave_conf->port_id = slave_eth_dev->data->port_id;
608 memcpy(&(presisted_slave_conf->mac_addr), slave_eth_dev->data->mac_addrs,
609 sizeof(struct ether_addr));
613 bond_ethdev_primary_set(struct bond_dev_private *internals,
614 uint8_t slave_port_id)
618 if (internals->active_slave_count < 1)
619 internals->current_primary_port = slave_port_id;
621 /* Search bonded device slave ports for new proposed primary port */
622 for (i = 0; i < internals->active_slave_count; i++) {
623 if (internals->active_slaves[i] == slave_port_id)
624 internals->current_primary_port = slave_port_id;
629 bond_ethdev_promiscuous_enable(struct rte_eth_dev *eth_dev);
632 bond_ethdev_start(struct rte_eth_dev *eth_dev)
634 struct bond_dev_private *internals;
637 /* slave eth dev will be started by bonded device */
638 if (valid_bonded_ethdev(eth_dev)) {
640 "%s: user tried to explicitly start a slave eth_dev (%d) of the bonded eth_dev\n",
641 __func__, eth_dev->data->port_id);
645 eth_dev->data->dev_link.link_status = 1;
646 eth_dev->data->dev_started = 1;
648 internals = eth_dev->data->dev_private;
650 if (internals->slave_count == 0) {
652 "%s: Cannot start port since there are no slave devices\n",
657 if (internals->user_defined_mac == 0) {
658 struct slave_conf *conf = slave_config_get(internals,
659 internals->primary_port);
661 if (mac_address_set(eth_dev, &(conf->mac_addr)) != 0) {
663 "bonded port (%d) failed to update mac address",
664 eth_dev->data->port_id);
669 /* Update all slave devices MACs*/
670 if (mac_address_slaves_update(eth_dev) != 0)
673 /* If bonded device is configure in promiscuous mode then re-apply config */
674 if (internals->promiscuous_en)
675 bond_ethdev_promiscuous_enable(eth_dev);
677 /* Reconfigure each slave device if starting bonded device */
678 for (i = 0; i < internals->slave_count; i++) {
679 if (slave_configure(eth_dev, &(rte_eth_devices[internals->slaves[i]]))
681 RTE_LOG(ERR, PMD, "bonded port "
682 "(%d) failed to reconfigure slave device (%d)\n)",
683 eth_dev->data->port_id, internals->slaves[i]);
688 if (internals->user_defined_primary_port)
689 bond_ethdev_primary_set(internals, internals->primary_port);
695 bond_ethdev_stop(struct rte_eth_dev *eth_dev)
697 struct bond_dev_private *internals = eth_dev->data->dev_private;
699 internals->active_slave_count = 0;
701 eth_dev->data->dev_link.link_status = 0;
702 eth_dev->data->dev_started = 0;
706 bond_ethdev_close(struct rte_eth_dev *dev __rte_unused)
710 /* forward declaration */
711 static int bond_ethdev_configure(struct rte_eth_dev *dev);
714 bond_ethdev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
716 dev_info->driver_name = driver_name;
717 dev_info->max_mac_addrs = 1;
719 dev_info->max_rx_pktlen = (uint32_t)2048;
721 dev_info->max_rx_queues = (uint16_t)128;
722 dev_info->max_tx_queues = (uint16_t)512;
724 dev_info->min_rx_bufsize = 0;
725 dev_info->pci_dev = dev->pci_dev;
729 bond_ethdev_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
730 uint16_t nb_rx_desc, unsigned int socket_id __rte_unused,
731 const struct rte_eth_rxconf *rx_conf, struct rte_mempool *mb_pool)
733 struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)
734 rte_zmalloc_socket(NULL, sizeof(struct bond_rx_queue),
735 0, dev->pci_dev->numa_node);
739 bd_rx_q->queue_id = rx_queue_id;
740 bd_rx_q->dev_private = dev->data->dev_private;
742 bd_rx_q->nb_rx_desc = nb_rx_desc;
744 memcpy(&(bd_rx_q->rx_conf), rx_conf, sizeof(struct rte_eth_rxconf));
745 bd_rx_q->mb_pool = mb_pool;
747 dev->data->rx_queues[rx_queue_id] = bd_rx_q;
753 bond_ethdev_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
754 uint16_t nb_tx_desc, unsigned int socket_id __rte_unused,
755 const struct rte_eth_txconf *tx_conf)
757 struct bond_tx_queue *bd_tx_q = (struct bond_tx_queue *)
758 rte_zmalloc_socket(NULL, sizeof(struct bond_tx_queue),
759 0, dev->pci_dev->numa_node);
764 bd_tx_q->queue_id = tx_queue_id;
765 bd_tx_q->dev_private = dev->data->dev_private;
767 bd_tx_q->nb_tx_desc = nb_tx_desc;
768 memcpy(&(bd_tx_q->tx_conf), tx_conf, sizeof(bd_tx_q->tx_conf));
770 dev->data->tx_queues[tx_queue_id] = bd_tx_q;
776 bond_ethdev_rx_queue_release(void *queue)
785 bond_ethdev_tx_queue_release(void *queue)
794 bond_ethdev_link_update(struct rte_eth_dev *bonded_eth_dev,
795 int wait_to_complete)
797 struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
799 if (!bonded_eth_dev->data->dev_started ||
800 internals->active_slave_count == 0) {
801 bonded_eth_dev->data->dev_link.link_status = 0;
804 struct rte_eth_dev *slave_eth_dev;
807 for (i = 0; i < internals->active_slave_count; i++) {
808 slave_eth_dev = &rte_eth_devices[internals->active_slaves[i]];
810 (*slave_eth_dev->dev_ops->link_update)(slave_eth_dev,
812 if (slave_eth_dev->data->dev_link.link_status == 1) {
818 bonded_eth_dev->data->dev_link.link_status = link_up;
825 bond_ethdev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
827 struct bond_dev_private *internals = dev->data->dev_private;
828 struct rte_eth_stats slave_stats;
832 /* clear bonded stats before populating from slaves */
833 memset(stats, 0, sizeof(*stats));
835 for (i = 0; i < internals->slave_count; i++) {
836 rte_eth_stats_get(internals->slaves[i], &slave_stats);
838 stats->ipackets += slave_stats.ipackets;
839 stats->opackets += slave_stats.opackets;
840 stats->ibytes += slave_stats.ibytes;
841 stats->obytes += slave_stats.obytes;
842 stats->ierrors += slave_stats.ierrors;
843 stats->oerrors += slave_stats.oerrors;
844 stats->imcasts += slave_stats.imcasts;
845 stats->rx_nombuf += slave_stats.rx_nombuf;
846 stats->fdirmatch += slave_stats.fdirmatch;
847 stats->fdirmiss += slave_stats.fdirmiss;
848 stats->tx_pause_xon += slave_stats.tx_pause_xon;
849 stats->rx_pause_xon += slave_stats.rx_pause_xon;
850 stats->tx_pause_xoff += slave_stats.tx_pause_xoff;
851 stats->rx_pause_xoff += slave_stats.rx_pause_xoff;
856 bond_ethdev_stats_reset(struct rte_eth_dev *dev)
858 struct bond_dev_private *internals = dev->data->dev_private;
861 for (i = 0; i < internals->slave_count; i++)
862 rte_eth_stats_reset(internals->slaves[i]);
866 bond_ethdev_promiscuous_enable(struct rte_eth_dev *eth_dev)
868 struct bond_dev_private *internals = eth_dev->data->dev_private;
871 internals->promiscuous_en = 1;
873 switch (internals->mode) {
874 /* Promiscuous mode is propagated to all slaves */
875 case BONDING_MODE_ROUND_ROBIN:
876 case BONDING_MODE_BALANCE:
877 case BONDING_MODE_BROADCAST:
878 for (i = 0; i < internals->slave_count; i++)
879 rte_eth_promiscuous_enable(internals->slaves[i]);
881 /* Promiscuous mode is propagated only to primary slave */
882 case BONDING_MODE_ACTIVE_BACKUP:
884 rte_eth_promiscuous_enable(internals->current_primary_port);
890 bond_ethdev_promiscuous_disable(struct rte_eth_dev *dev)
892 struct bond_dev_private *internals = dev->data->dev_private;
895 internals->promiscuous_en = 0;
897 switch (internals->mode) {
898 /* Promiscuous mode is propagated to all slaves */
899 case BONDING_MODE_ROUND_ROBIN:
900 case BONDING_MODE_BALANCE:
901 case BONDING_MODE_BROADCAST:
902 for (i = 0; i < internals->slave_count; i++)
903 rte_eth_promiscuous_disable(internals->slaves[i]);
905 /* Promiscuous mode is propagated only to primary slave */
906 case BONDING_MODE_ACTIVE_BACKUP:
908 rte_eth_promiscuous_disable(internals->current_primary_port);
913 bond_ethdev_lsc_event_callback(uint8_t port_id, enum rte_eth_event_type type,
916 struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
917 struct bond_dev_private *internals;
918 struct rte_eth_link link;
920 int i, valid_slave = 0, active_pos = -1;
922 if (type != RTE_ETH_EVENT_INTR_LSC || param == NULL)
925 bonded_eth_dev = &rte_eth_devices[*(uint8_t *)param];
926 slave_eth_dev = &rte_eth_devices[port_id];
928 if (valid_bonded_ethdev(bonded_eth_dev))
931 internals = bonded_eth_dev->data->dev_private;
933 /* If the device isn't started don't handle interrupts */
934 if (!bonded_eth_dev->data->dev_started)
937 /* verify that port_id is a valid slave of bonded port */
938 for (i = 0; i < internals->slave_count; i++) {
939 if (internals->slaves[i] == port_id) {
948 /* Search for port in active port list */
949 for (i = 0; i < internals->active_slave_count; i++) {
950 if (port_id == internals->active_slaves[i]) {
956 rte_eth_link_get_nowait(port_id, &link);
957 if (link.link_status) {
961 /* if no active slave ports then set this port to be primary port */
962 if (internals->active_slave_count < 1) {
963 /* If first active slave, then change link status */
964 bonded_eth_dev->data->dev_link.link_status = 1;
965 internals->current_primary_port = port_id;
967 /* Inherit eth dev link properties from first active slave */
968 link_properties_set(bonded_eth_dev,
969 &(slave_eth_dev->data->dev_link));
971 internals->active_slaves[internals->active_slave_count++] = port_id;
973 /* If user has defined the primary port then default to using it */
974 if (internals->user_defined_primary_port &&
975 internals->primary_port == port_id)
976 bond_ethdev_primary_set(internals, port_id);
981 /* Remove from active slave list */
982 for (i = active_pos; i < (internals->active_slave_count - 1); i++)
983 internals->active_slaves[i] = internals->active_slaves[i+1];
985 internals->active_slave_count--;
987 /* No active slaves, change link status to down and reset other
989 if (internals->active_slave_count < 1) {
990 bonded_eth_dev->data->dev_link.link_status = 0;
992 link_properties_reset(bonded_eth_dev);
995 /* Update primary id, take first active slave from list or if none
996 * available set to -1 */
997 if (port_id == internals->current_primary_port) {
998 if (internals->active_slave_count > 0)
999 bond_ethdev_primary_set(internals,
1000 internals->active_slaves[0]);
1002 internals->current_primary_port = internals->primary_port;
1007 struct eth_dev_ops default_dev_ops = {
1008 .dev_start = bond_ethdev_start,
1009 .dev_stop = bond_ethdev_stop,
1010 .dev_close = bond_ethdev_close,
1011 .dev_configure = bond_ethdev_configure,
1012 .dev_infos_get = bond_ethdev_info,
1013 .rx_queue_setup = bond_ethdev_rx_queue_setup,
1014 .tx_queue_setup = bond_ethdev_tx_queue_setup,
1015 .rx_queue_release = bond_ethdev_rx_queue_release,
1016 .tx_queue_release = bond_ethdev_tx_queue_release,
1017 .link_update = bond_ethdev_link_update,
1018 .stats_get = bond_ethdev_stats_get,
1019 .stats_reset = bond_ethdev_stats_reset,
1020 .promiscuous_enable = bond_ethdev_promiscuous_enable,
1021 .promiscuous_disable = bond_ethdev_promiscuous_disable
1025 bond_init(const char *name, const char *params)
1027 struct bond_dev_private *internals;
1028 struct rte_kvargs *kvlist;
1029 uint8_t bonding_mode, socket_id;
1030 int arg_count, port_id;
1032 RTE_LOG(INFO, EAL, "Initializing pmd_bond for %s\n", name);
1034 kvlist = rte_kvargs_parse(params, pmd_bond_init_valid_arguments);
1038 /* Parse link bonding mode */
1039 if (rte_kvargs_count(kvlist, PMD_BOND_MODE_KVARG) == 1) {
1040 if (rte_kvargs_process(kvlist, PMD_BOND_MODE_KVARG,
1041 &bond_ethdev_parse_slave_mode_kvarg, &bonding_mode) != 0) {
1042 RTE_LOG(ERR, EAL, "Invalid mode for bonded device %s\n", name);
1047 "Mode must be specified only once for bonded device %s\n",
1052 /* Parse socket id to create bonding device on */
1053 arg_count = rte_kvargs_count(kvlist, PMD_BOND_SOCKET_ID_KVARG);
1054 if (arg_count == 1) {
1055 if (rte_kvargs_process(kvlist, PMD_BOND_SOCKET_ID_KVARG,
1056 &bond_ethdev_parse_socket_id_kvarg, &socket_id) != 0) {
1058 "Invalid socket Id specified for bonded device %s\n",
1062 } else if (arg_count > 1) {
1064 "Socket Id can be specified only once for bonded device %s\n",
1068 socket_id = rte_socket_id();
1071 /* Create link bonding eth device */
1072 port_id = rte_eth_bond_create(name, bonding_mode, socket_id);
1075 "Failed to create socket %s in mode %u on socket %u.\n",
1076 name, bonding_mode, socket_id);
1079 internals = rte_eth_devices[port_id].data->dev_private;
1080 internals->kvlist = kvlist;
1083 "Create bonded device %s on port %d in mode %u on socket %u.\n",
1084 name, port_id, bonding_mode, socket_id);
1088 /* this part will resolve the slave portids after all the other pdev and vdev
1089 * have been allocated */
1091 bond_ethdev_configure(struct rte_eth_dev *dev)
1093 char *name = dev->data->name;
1094 struct bond_dev_private *internals = dev->data->dev_private;
1095 struct rte_kvargs *kvlist = internals->kvlist;
1096 int arg_count, port_id = dev - rte_eth_devices;
1099 * if no kvlist, it means that this bonded device has been created
1100 * through the bonding api.
1105 /* Parse MAC address for bonded device */
1106 arg_count = rte_kvargs_count(kvlist, PMD_BOND_MAC_ADDR_KVARG);
1107 if (arg_count == 1) {
1108 struct ether_addr bond_mac;
1110 if (rte_kvargs_process(kvlist, PMD_BOND_MAC_ADDR_KVARG,
1111 &bond_ethdev_parse_bond_mac_addr_kvarg, &bond_mac) < 0) {
1112 RTE_LOG(INFO, EAL, "Invalid mac address for bonded device %s\n",
1117 /* Set MAC address */
1118 if (rte_eth_bond_mac_address_set(port_id, &bond_mac) != 0) {
1120 "Failed to set mac address on bonded device %s\n",
1124 } else if (arg_count > 1) {
1126 "MAC address can be specified only once for bonded device %s\n",
1131 /* Parse/set balance mode transmit policy */
1132 arg_count = rte_kvargs_count(kvlist, PMD_BOND_XMIT_POLICY_KVARG);
1133 if (arg_count == 1) {
1134 uint8_t xmit_policy;
1136 if (rte_kvargs_process(kvlist, PMD_BOND_XMIT_POLICY_KVARG,
1137 &bond_ethdev_parse_balance_xmit_policy_kvarg, &xmit_policy) !=
1140 "Invalid xmit policy specified for bonded device %s\n",
1145 /* Set balance mode transmit policy*/
1146 if (rte_eth_bond_xmit_policy_set(port_id, xmit_policy) != 0) {
1148 "Failed to set balance xmit policy on bonded device %s\n",
1152 } else if (arg_count > 1) {
1154 "Transmit policy can be specified only once for bonded device %s\n",
1159 /* Parse/add slave ports to bonded device */
1160 if (rte_kvargs_count(kvlist, PMD_BOND_SLAVE_PORT_KVARG) > 0) {
1161 struct bond_ethdev_slave_ports slave_ports;
1164 memset(&slave_ports, 0, sizeof(slave_ports));
1166 if (rte_kvargs_process(kvlist, PMD_BOND_SLAVE_PORT_KVARG,
1167 &bond_ethdev_parse_slave_port_kvarg, &slave_ports) != 0) {
1169 "Failed to parse slave ports for bonded device %s\n",
1174 for (i = 0; i < slave_ports.slave_count; i++) {
1175 if (rte_eth_bond_slave_add(port_id, slave_ports.slaves[i]) != 0) {
1177 "Failed to add port %d as slave to bonded device %s\n",
1178 slave_ports.slaves[i], name);
1183 RTE_LOG(INFO, EAL, "No slaves specified for bonded device %s\n", name);
1187 /* Parse/set primary slave port id*/
1188 arg_count = rte_kvargs_count(kvlist, PMD_BOND_PRIMARY_SLAVE_KVARG);
1189 if (arg_count == 1) {
1190 uint8_t primary_slave_port_id;
1192 if (rte_kvargs_process(kvlist,
1193 PMD_BOND_PRIMARY_SLAVE_KVARG,
1194 &bond_ethdev_parse_primary_slave_port_id_kvarg,
1195 &primary_slave_port_id) < 0) {
1197 "Invalid primary slave port id specified for bonded device %s\n",
1202 /* Set balance mode transmit policy*/
1203 if (rte_eth_bond_primary_set(port_id, (uint8_t)primary_slave_port_id)
1206 "Failed to set primary slave port %d on bonded device %s\n",
1207 primary_slave_port_id, name);
1210 } else if (arg_count > 1) {
1212 "Primary slave can be specified only once for bonded device %s\n",
1220 static struct rte_driver bond_drv = {
1226 PMD_REGISTER_DRIVER(bond_drv);