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>
39 #include "rte_eth_bond.h"
40 #include "rte_eth_bond_private.h"
43 valid_bonded_ethdev(struct rte_eth_dev *eth_dev)
47 /* Check valid pointer */
48 if (eth_dev->driver->pci_drv.name == NULL || driver_name == NULL)
51 /* Check string lengths are equal */
52 len = strlen(driver_name);
53 if (strlen(eth_dev->driver->pci_drv.name) != len)
57 return strncmp(eth_dev->driver->pci_drv.name, driver_name, len);
61 valid_port_id(uint8_t port_id)
63 /* Verify that port id is valid */
64 int ethdev_count = rte_eth_dev_count();
65 if (port_id >= ethdev_count) {
67 "%s: port Id %d is greater than rte_eth_dev_count %d\n",
68 __func__, port_id, ethdev_count);
76 valid_bonded_port_id(uint8_t port_id)
78 /* Verify that port id's are valid */
79 if (valid_port_id(port_id))
82 /* Verify that bonded_port_id refers to a bonded port */
83 if (valid_bonded_ethdev(&rte_eth_devices[port_id])) {
85 "%s: Specified port Id %d is not a bonded eth_dev device\n",
94 valid_slave_port_id(uint8_t port_id)
96 /* Verify that port id's are valid */
97 if (valid_port_id(port_id))
100 /* Verify that port_id refers to a non bonded port */
101 if (!valid_bonded_ethdev(&rte_eth_devices[port_id]))
108 number_of_sockets(void)
112 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
114 for (i = 0; ((i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL)); i++) {
115 if (sockets < ms[i].socket_id)
116 sockets = ms[i].socket_id;
119 /* Number of sockets = maximum socket_id + 1 */
123 const char *driver_name = "Link Bonding PMD";
126 rte_eth_bond_create(const char *name, uint8_t mode, uint8_t socket_id)
128 struct rte_pci_device *pci_dev = NULL;
129 struct bond_dev_private *internals = NULL;
130 struct rte_eth_dev *eth_dev = NULL;
131 struct eth_driver *eth_drv = NULL;
132 struct rte_pci_driver *pci_drv = NULL;
133 struct rte_pci_id *pci_id_table = NULL;
134 /* now do all data allocation - for eth_dev structure, dummy pci driver
135 * and internal (private) data
139 RTE_LOG(ERR, PMD, "Invalid name specified\n");
143 if (socket_id >= number_of_sockets()) {
145 "%s: invalid socket id specified to create bonded device on.\n",
150 pci_dev = rte_zmalloc_socket(name, sizeof(*pci_dev), 0, socket_id);
151 if (pci_dev == NULL) {
152 RTE_LOG(ERR, PMD, "Unable to malloc pci dev on socket\n");
156 eth_drv = rte_zmalloc_socket(name, sizeof(*eth_drv), 0, socket_id);
157 if (eth_drv == NULL) {
158 RTE_LOG(ERR, PMD, "Unable to malloc eth_drv on socket\n");
162 pci_drv = rte_zmalloc_socket(name, sizeof(*pci_drv), 0, socket_id);
163 if (pci_drv == NULL) {
164 RTE_LOG(ERR, PMD, "Unable to malloc pci_drv on socket\n");
167 pci_id_table = rte_zmalloc_socket(name, sizeof(*pci_id_table), 0, socket_id);
168 if (pci_drv == NULL) {
169 RTE_LOG(ERR, PMD, "Unable to malloc pci_id_table on socket\n");
173 pci_drv->id_table = pci_id_table;
175 pci_drv->id_table->device_id = PCI_ANY_ID;
176 pci_drv->id_table->subsystem_device_id = PCI_ANY_ID;
177 pci_drv->id_table->vendor_id = PCI_ANY_ID;
178 pci_drv->id_table->subsystem_vendor_id = PCI_ANY_ID;
180 internals = rte_zmalloc_socket(name, sizeof(*internals), 0, socket_id);
181 if (internals == NULL) {
182 RTE_LOG(ERR, PMD, "Unable to malloc internals on socket\n");
186 /* reserve an ethdev entry */
187 eth_dev = rte_eth_dev_allocate(name);
188 if (eth_dev == NULL) {
189 RTE_LOG(ERR, PMD, "Unable to allocate rte_eth_dev\n");
193 pci_dev->numa_node = socket_id;
194 pci_drv->name = driver_name;
196 eth_drv->pci_drv = (struct rte_pci_driver)(*pci_drv);
197 eth_dev->driver = eth_drv;
199 eth_dev->data->dev_private = internals;
200 eth_dev->data->nb_rx_queues = (uint16_t)1;
201 eth_dev->data->nb_tx_queues = (uint16_t)1;
203 eth_dev->data->dev_link.link_status = 0;
205 eth_dev->data->mac_addrs = rte_zmalloc_socket(name, ETHER_ADDR_LEN, 0,
208 eth_dev->data->dev_started = 0;
209 eth_dev->data->promiscuous = 0;
210 eth_dev->data->scattered_rx = 0;
211 eth_dev->data->all_multicast = 0;
213 eth_dev->dev_ops = &default_dev_ops;
214 eth_dev->pci_dev = pci_dev;
216 if (bond_ethdev_mode_set(eth_dev, mode)) {
218 "%s: failed to set bonded device %d mode too %d\n",
219 __func__, eth_dev->data->port_id, mode);
223 internals->current_primary_port = 0;
224 internals->balance_xmit_policy = BALANCE_XMIT_POLICY_LAYER2;
225 internals->user_defined_mac = 0;
226 internals->link_props_set = 0;
227 internals->slave_count = 0;
228 internals->active_slave_count = 0;
230 memset(internals->active_slaves, 0, sizeof(internals->active_slaves));
231 memset(internals->slaves, 0, sizeof(internals->slaves));
233 memset(internals->presisted_slaves_conf, 0,
234 sizeof(internals->presisted_slaves_conf));
236 return eth_dev->data->port_id;
244 rte_free(pci_id_table);
253 rte_eth_bond_slave_add(uint8_t bonded_port_id, uint8_t slave_port_id)
255 struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
256 struct bond_dev_private *internals;
257 struct bond_dev_private *temp_internals;
258 struct rte_eth_link link_props;
262 /* Verify that port id's are valid bonded and slave ports */
263 if (valid_bonded_port_id(bonded_port_id) != 0)
266 if (valid_slave_port_id(slave_port_id) != 0)
270 * Verify that new slave device is not already a slave of another bonded
272 for (i = rte_eth_dev_count()-1; i >= 0; i--) {
273 if (valid_bonded_ethdev(&rte_eth_devices[i]) == 0) {
274 temp_internals = rte_eth_devices[i].data->dev_private;
275 for (j = 0; j < temp_internals->slave_count; j++) {
276 /* Device already a slave of a bonded device */
277 if (temp_internals->slaves[j] == slave_port_id)
283 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
284 internals = bonded_eth_dev->data->dev_private;
286 slave_eth_dev = &rte_eth_devices[slave_port_id];
288 if (internals->slave_count > 0) {
289 /* Check that new slave device is the same type as the other slaves
290 * and not repetitive */
291 for (i = 0; i < internals->slave_count; i++) {
292 if (slave_eth_dev->pci_dev->driver->id_table->device_id !=
293 rte_eth_devices[internals->slaves[i]].pci_dev->driver->id_table->device_id ||
294 internals->slaves[i] == slave_port_id)
299 /* Add slave details to bonded device */
300 internals->slaves[internals->slave_count] = slave_port_id;
302 slave_config_store(internals, slave_eth_dev);
304 if (internals->slave_count < 1) {
305 /* if MAC is not user defined then use MAC of first slave add to bonded
307 if (!internals->user_defined_mac)
308 mac_address_set(bonded_eth_dev, slave_eth_dev->data->mac_addrs);
310 /* Inherit eth dev link properties from first slave */
311 link_properties_set(bonded_eth_dev, &(slave_eth_dev->data->dev_link));
313 /* Make primary slave */
314 internals->primary_port = slave_port_id;
316 /* Check slave link properties are supported if props are set,
317 * all slaves must be the same */
318 if (internals->link_props_set) {
319 if (link_properties_valid(&(bonded_eth_dev->data->dev_link),
320 &(slave_eth_dev->data->dev_link))) {
322 "%s: Slave port %d link speed/duplex not supported\n",
323 __func__, slave_port_id);
327 link_properties_set(bonded_eth_dev,
328 &(slave_eth_dev->data->dev_link));
332 internals->slave_count++;
334 /* Update all slave devices MACs*/
335 mac_address_slaves_update(bonded_eth_dev);
337 if (bonded_eth_dev->data->dev_started) {
338 if (slave_configure(bonded_eth_dev, slave_eth_dev) != 0) {
339 RTE_LOG(ERR, PMD, "rte_bond_slaves_configure: port=%d\n",
345 /* Register link status change callback with bonded device pointer as
347 rte_eth_dev_callback_register(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
348 bond_ethdev_lsc_event_callback, &bonded_eth_dev->data->port_id);
350 /* If bonded device is started then we can add the slave to our active
352 if (bonded_eth_dev->data->dev_started) {
353 rte_eth_link_get_nowait(slave_port_id, &link_props);
355 if (link_props.link_status == 1) {
356 internals->active_slaves[internals->active_slave_count++] =
364 RTE_LOG(ERR, PMD, "Failed to add port %d as slave\n", slave_port_id);
370 rte_eth_bond_slave_remove(uint8_t bonded_port_id, uint8_t slave_port_id)
372 struct bond_dev_private *internals;
373 struct slave_conf *slave_conf;
378 /* Verify that port id's are valid bonded and slave ports */
379 if (valid_bonded_port_id(bonded_port_id) != 0)
382 if (valid_slave_port_id(slave_port_id) != 0)
385 internals = rte_eth_devices[bonded_port_id].data->dev_private;
387 /* first remove from active slave list */
388 for (i = 0; i < internals->active_slave_count; i++) {
389 if (internals->active_slaves[i] == slave_port_id)
392 /* shift active slaves up active array list */
393 if (pos >= 0 && i < (internals->active_slave_count - 1))
394 internals->active_slaves[i] = internals->active_slaves[i+1];
398 internals->active_slave_count--;
401 /* now remove from slave list */
402 for (i = 0; i < internals->slave_count; i++) {
403 if (internals->slaves[i] == slave_port_id)
406 /* shift slaves up list */
407 if (pos >= 0 && i < internals->slave_count)
408 internals->slaves[i] = internals->slaves[i+1];
414 /* Un-register link status change callback with bonded device pointer as
416 rte_eth_dev_callback_unregister(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
417 bond_ethdev_lsc_event_callback,
418 &rte_eth_devices[bonded_port_id].data->port_id);
420 /* Restore original MAC address of slave device */
421 slave_conf = slave_config_get(internals, slave_port_id);
423 mac_address_set(&rte_eth_devices[slave_port_id], &(slave_conf->mac_addr));
425 slave_config_clear(internals, &rte_eth_devices[slave_port_id]);
427 internals->slave_count--;
429 /* first slave in the active list will be the primary by default,
430 * otherwise use first device in list */
431 if (internals->current_primary_port == slave_port_id) {
432 if (internals->active_slave_count > 0)
433 internals->current_primary_port = internals->active_slaves[0];
434 else if (internals->slave_count > 0)
435 internals->current_primary_port = internals->slaves[0];
437 internals->primary_port = 0;
440 if (internals->active_slave_count < 1) {
441 /* reset device link properties as no slaves are active */
442 link_properties_reset(&rte_eth_devices[bonded_port_id]);
444 /* if no slaves are any longer attached to bonded device and MAC is not
445 * user defined then clear MAC of bonded device as it will be reset
446 * when a new slave is added */
447 if (internals->slave_count < 1 && !internals->user_defined_mac)
448 memset(rte_eth_devices[bonded_port_id].data->mac_addrs, 0,
449 sizeof(*(rte_eth_devices[bonded_port_id].data->mac_addrs)));
456 "Cannot remove slave device (not present in bonded device)\n");
462 rte_eth_bond_mode_set(uint8_t bonded_port_id, uint8_t mode)
464 if (valid_bonded_port_id(bonded_port_id) != 0)
467 return bond_ethdev_mode_set(&rte_eth_devices[bonded_port_id], mode);
471 rte_eth_bond_mode_get(uint8_t bonded_port_id)
473 struct bond_dev_private *internals;
475 if (valid_bonded_port_id(bonded_port_id) != 0)
478 internals = rte_eth_devices[bonded_port_id].data->dev_private;
480 return internals->mode;
484 rte_eth_bond_primary_set(uint8_t bonded_port_id, uint8_t slave_port_id)
486 struct bond_dev_private *internals;
488 if (valid_bonded_port_id(bonded_port_id) != 0)
491 if (valid_slave_port_id(slave_port_id) != 0)
494 internals = rte_eth_devices[bonded_port_id].data->dev_private;
496 internals->user_defined_primary_port = 1;
497 internals->primary_port = slave_port_id;
499 bond_ethdev_primary_set(internals, slave_port_id);
505 rte_eth_bond_primary_get(uint8_t bonded_port_id)
507 struct bond_dev_private *internals;
509 if (valid_bonded_port_id(bonded_port_id) != 0)
512 internals = rte_eth_devices[bonded_port_id].data->dev_private;
514 if (internals->slave_count < 1)
517 return internals->current_primary_port;
520 rte_eth_bond_slaves_get(uint8_t bonded_port_id, uint8_t slaves[], uint8_t len)
522 struct bond_dev_private *internals;
524 if (valid_bonded_port_id(bonded_port_id) != 0)
530 internals = rte_eth_devices[bonded_port_id].data->dev_private;
532 if (internals->slave_count > len)
535 memcpy(slaves, internals->slaves, internals->slave_count);
537 return internals->slave_count;
542 rte_eth_bond_active_slaves_get(uint8_t bonded_port_id, uint8_t slaves[],
545 struct bond_dev_private *internals;
547 if (valid_bonded_port_id(bonded_port_id) != 0)
553 internals = rte_eth_devices[bonded_port_id].data->dev_private;
555 if (internals->active_slave_count > len)
558 memcpy(slaves, internals->active_slaves, internals->active_slave_count);
560 return internals->active_slave_count;
564 rte_eth_bond_mac_address_set(uint8_t bonded_port_id,
565 struct ether_addr *mac_addr)
567 struct rte_eth_dev *bonded_eth_dev;
568 struct bond_dev_private *internals;
570 if (valid_bonded_port_id(bonded_port_id) != 0)
573 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
574 internals = bonded_eth_dev->data->dev_private;
576 /* Set MAC Address of Bonded Device */
577 if (mac_address_set(bonded_eth_dev, mac_addr))
580 internals->user_defined_mac = 1;
582 /* Update all slave devices MACs*/
583 if (internals->slave_count > 0)
584 return mac_address_slaves_update(bonded_eth_dev);
590 rte_eth_bond_mac_address_reset(uint8_t bonded_port_id)
592 struct rte_eth_dev *bonded_eth_dev;
593 struct bond_dev_private *internals;
595 if (valid_bonded_port_id(bonded_port_id) != 0)
598 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
599 internals = bonded_eth_dev->data->dev_private;
601 internals->user_defined_mac = 0;
603 if (internals->slave_count > 0) {
604 struct slave_conf *conf;
605 conf = slave_config_get(internals, internals->primary_port);
607 /* Set MAC Address of Bonded Device */
608 if (mac_address_set(bonded_eth_dev, &conf->mac_addr) != 0)
611 /* Update all slave devices MAC addresses */
612 return mac_address_slaves_update(bonded_eth_dev);
614 /* No need to update anything as no slaves present */
619 rte_eth_bond_xmit_policy_set(uint8_t bonded_port_id, uint8_t policy)
621 struct bond_dev_private *internals;
623 if (valid_bonded_port_id(bonded_port_id) != 0)
626 internals = rte_eth_devices[bonded_port_id].data->dev_private;
629 case BALANCE_XMIT_POLICY_LAYER2:
630 case BALANCE_XMIT_POLICY_LAYER23:
631 case BALANCE_XMIT_POLICY_LAYER34:
632 internals->balance_xmit_policy = policy;
642 rte_eth_bond_xmit_policy_get(uint8_t bonded_port_id)
644 struct bond_dev_private *internals;
646 if (valid_bonded_port_id(bonded_port_id) != 0)
649 internals = rte_eth_devices[bonded_port_id].data->dev_private;
651 return internals->balance_xmit_policy;