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 pci_drv->drv_flags = RTE_PCI_DRV_INTR_LSC;
182 internals = rte_zmalloc_socket(name, sizeof(*internals), 0, socket_id);
183 if (internals == NULL) {
184 RTE_LOG(ERR, PMD, "Unable to malloc internals on socket\n");
188 /* reserve an ethdev entry */
189 eth_dev = rte_eth_dev_allocate(name);
190 if (eth_dev == NULL) {
191 RTE_LOG(ERR, PMD, "Unable to allocate rte_eth_dev\n");
195 pci_dev->numa_node = socket_id;
196 pci_drv->name = driver_name;
198 eth_drv->pci_drv = (struct rte_pci_driver)(*pci_drv);
199 eth_dev->driver = eth_drv;
201 eth_dev->data->dev_private = internals;
202 eth_dev->data->nb_rx_queues = (uint16_t)1;
203 eth_dev->data->nb_tx_queues = (uint16_t)1;
205 TAILQ_INIT(&(eth_dev->callbacks));
207 eth_dev->data->dev_link.link_status = 0;
209 eth_dev->data->mac_addrs = rte_zmalloc_socket(name, ETHER_ADDR_LEN, 0,
212 eth_dev->data->dev_started = 0;
213 eth_dev->data->promiscuous = 0;
214 eth_dev->data->scattered_rx = 0;
215 eth_dev->data->all_multicast = 0;
217 eth_dev->dev_ops = &default_dev_ops;
218 eth_dev->pci_dev = pci_dev;
220 if (bond_ethdev_mode_set(eth_dev, mode)) {
222 "%s: failed to set bonded device %d mode too %d\n",
223 __func__, eth_dev->data->port_id, mode);
227 internals->current_primary_port = 0;
228 internals->balance_xmit_policy = BALANCE_XMIT_POLICY_LAYER2;
229 internals->user_defined_mac = 0;
230 internals->link_props_set = 0;
231 internals->slave_count = 0;
232 internals->active_slave_count = 0;
234 memset(internals->active_slaves, 0, sizeof(internals->active_slaves));
235 memset(internals->slaves, 0, sizeof(internals->slaves));
237 memset(internals->presisted_slaves_conf, 0,
238 sizeof(internals->presisted_slaves_conf));
240 return eth_dev->data->port_id;
248 rte_free(pci_id_table);
257 rte_eth_bond_slave_add(uint8_t bonded_port_id, uint8_t slave_port_id)
259 struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
260 struct bond_dev_private *internals;
261 struct bond_dev_private *temp_internals;
262 struct rte_eth_link link_props;
266 /* Verify that port id's are valid bonded and slave ports */
267 if (valid_bonded_port_id(bonded_port_id) != 0)
270 if (valid_slave_port_id(slave_port_id) != 0)
274 * Verify that new slave device is not already a slave of another bonded
276 for (i = rte_eth_dev_count()-1; i >= 0; i--) {
277 if (valid_bonded_ethdev(&rte_eth_devices[i]) == 0) {
278 temp_internals = rte_eth_devices[i].data->dev_private;
279 for (j = 0; j < temp_internals->slave_count; j++) {
280 /* Device already a slave of a bonded device */
281 if (temp_internals->slaves[j] == slave_port_id)
287 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
288 internals = bonded_eth_dev->data->dev_private;
290 slave_eth_dev = &rte_eth_devices[slave_port_id];
292 if (internals->slave_count > 0) {
293 /* Check that new slave device is the same type as the other slaves
294 * and not repetitive */
295 for (i = 0; i < internals->slave_count; i++) {
296 if (slave_eth_dev->pci_dev->driver->id_table->device_id !=
297 rte_eth_devices[internals->slaves[i]].pci_dev->driver->id_table->device_id ||
298 internals->slaves[i] == slave_port_id)
303 /* Add slave details to bonded device */
304 internals->slaves[internals->slave_count] = slave_port_id;
306 slave_config_store(internals, slave_eth_dev);
308 if (internals->slave_count < 1) {
309 /* if MAC is not user defined then use MAC of first slave add to bonded
311 if (!internals->user_defined_mac)
312 mac_address_set(bonded_eth_dev, slave_eth_dev->data->mac_addrs);
314 /* Inherit eth dev link properties from first slave */
315 link_properties_set(bonded_eth_dev, &(slave_eth_dev->data->dev_link));
317 /* Make primary slave */
318 internals->primary_port = slave_port_id;
320 /* Check slave link properties are supported if props are set,
321 * all slaves must be the same */
322 if (internals->link_props_set) {
323 if (link_properties_valid(&(bonded_eth_dev->data->dev_link),
324 &(slave_eth_dev->data->dev_link))) {
326 "%s: Slave port %d link speed/duplex not supported\n",
327 __func__, slave_port_id);
331 link_properties_set(bonded_eth_dev,
332 &(slave_eth_dev->data->dev_link));
336 internals->slave_count++;
338 /* Update all slave devices MACs*/
339 mac_address_slaves_update(bonded_eth_dev);
341 if (bonded_eth_dev->data->dev_started) {
342 if (slave_configure(bonded_eth_dev, slave_eth_dev) != 0) {
343 RTE_LOG(ERR, PMD, "rte_bond_slaves_configure: port=%d\n",
349 /* Register link status change callback with bonded device pointer as
351 rte_eth_dev_callback_register(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
352 bond_ethdev_lsc_event_callback, &bonded_eth_dev->data->port_id);
354 /* If bonded device is started then we can add the slave to our active
356 if (bonded_eth_dev->data->dev_started) {
357 rte_eth_link_get_nowait(slave_port_id, &link_props);
359 if (link_props.link_status == 1) {
360 internals->active_slaves[internals->active_slave_count++] =
368 RTE_LOG(ERR, PMD, "Failed to add port %d as slave\n", slave_port_id);
374 rte_eth_bond_slave_remove(uint8_t bonded_port_id, uint8_t slave_port_id)
376 struct bond_dev_private *internals;
377 struct slave_conf *slave_conf;
382 /* Verify that port id's are valid bonded and slave ports */
383 if (valid_bonded_port_id(bonded_port_id) != 0)
386 if (valid_slave_port_id(slave_port_id) != 0)
389 internals = rte_eth_devices[bonded_port_id].data->dev_private;
391 /* first remove from active slave list */
392 for (i = 0; i < internals->active_slave_count; i++) {
393 if (internals->active_slaves[i] == slave_port_id)
396 /* shift active slaves up active array list */
397 if (pos >= 0 && i < (internals->active_slave_count - 1))
398 internals->active_slaves[i] = internals->active_slaves[i+1];
402 internals->active_slave_count--;
405 /* now remove from slave list */
406 for (i = 0; i < internals->slave_count; i++) {
407 if (internals->slaves[i] == slave_port_id)
410 /* shift slaves up list */
411 if (pos >= 0 && i < internals->slave_count)
412 internals->slaves[i] = internals->slaves[i+1];
418 /* Un-register link status change callback with bonded device pointer as
420 rte_eth_dev_callback_unregister(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
421 bond_ethdev_lsc_event_callback,
422 &rte_eth_devices[bonded_port_id].data->port_id);
424 /* Restore original MAC address of slave device */
425 slave_conf = slave_config_get(internals, slave_port_id);
427 mac_address_set(&rte_eth_devices[slave_port_id], &(slave_conf->mac_addr));
429 slave_config_clear(internals, &rte_eth_devices[slave_port_id]);
431 internals->slave_count--;
433 /* first slave in the active list will be the primary by default,
434 * otherwise use first device in list */
435 if (internals->current_primary_port == slave_port_id) {
436 if (internals->active_slave_count > 0)
437 internals->current_primary_port = internals->active_slaves[0];
438 else if (internals->slave_count > 0)
439 internals->current_primary_port = internals->slaves[0];
441 internals->primary_port = 0;
444 if (internals->active_slave_count < 1) {
445 /* reset device link properties as no slaves are active */
446 link_properties_reset(&rte_eth_devices[bonded_port_id]);
448 /* if no slaves are any longer attached to bonded device and MAC is not
449 * user defined then clear MAC of bonded device as it will be reset
450 * when a new slave is added */
451 if (internals->slave_count < 1 && !internals->user_defined_mac)
452 memset(rte_eth_devices[bonded_port_id].data->mac_addrs, 0,
453 sizeof(*(rte_eth_devices[bonded_port_id].data->mac_addrs)));
460 "Cannot remove slave device (not present in bonded device)\n");
466 rte_eth_bond_mode_set(uint8_t bonded_port_id, uint8_t mode)
468 if (valid_bonded_port_id(bonded_port_id) != 0)
471 return bond_ethdev_mode_set(&rte_eth_devices[bonded_port_id], mode);
475 rte_eth_bond_mode_get(uint8_t bonded_port_id)
477 struct bond_dev_private *internals;
479 if (valid_bonded_port_id(bonded_port_id) != 0)
482 internals = rte_eth_devices[bonded_port_id].data->dev_private;
484 return internals->mode;
488 rte_eth_bond_primary_set(uint8_t bonded_port_id, uint8_t slave_port_id)
490 struct bond_dev_private *internals;
492 if (valid_bonded_port_id(bonded_port_id) != 0)
495 if (valid_slave_port_id(slave_port_id) != 0)
498 internals = rte_eth_devices[bonded_port_id].data->dev_private;
500 internals->user_defined_primary_port = 1;
501 internals->primary_port = slave_port_id;
503 bond_ethdev_primary_set(internals, slave_port_id);
509 rte_eth_bond_primary_get(uint8_t bonded_port_id)
511 struct bond_dev_private *internals;
513 if (valid_bonded_port_id(bonded_port_id) != 0)
516 internals = rte_eth_devices[bonded_port_id].data->dev_private;
518 if (internals->slave_count < 1)
521 return internals->current_primary_port;
524 rte_eth_bond_slaves_get(uint8_t bonded_port_id, uint8_t slaves[], uint8_t len)
526 struct bond_dev_private *internals;
528 if (valid_bonded_port_id(bonded_port_id) != 0)
534 internals = rte_eth_devices[bonded_port_id].data->dev_private;
536 if (internals->slave_count > len)
539 memcpy(slaves, internals->slaves, internals->slave_count);
541 return internals->slave_count;
546 rte_eth_bond_active_slaves_get(uint8_t bonded_port_id, uint8_t slaves[],
549 struct bond_dev_private *internals;
551 if (valid_bonded_port_id(bonded_port_id) != 0)
557 internals = rte_eth_devices[bonded_port_id].data->dev_private;
559 if (internals->active_slave_count > len)
562 memcpy(slaves, internals->active_slaves, internals->active_slave_count);
564 return internals->active_slave_count;
568 rte_eth_bond_mac_address_set(uint8_t bonded_port_id,
569 struct ether_addr *mac_addr)
571 struct rte_eth_dev *bonded_eth_dev;
572 struct bond_dev_private *internals;
574 if (valid_bonded_port_id(bonded_port_id) != 0)
577 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
578 internals = bonded_eth_dev->data->dev_private;
580 /* Set MAC Address of Bonded Device */
581 if (mac_address_set(bonded_eth_dev, mac_addr))
584 internals->user_defined_mac = 1;
586 /* Update all slave devices MACs*/
587 if (internals->slave_count > 0)
588 return mac_address_slaves_update(bonded_eth_dev);
594 rte_eth_bond_mac_address_reset(uint8_t bonded_port_id)
596 struct rte_eth_dev *bonded_eth_dev;
597 struct bond_dev_private *internals;
599 if (valid_bonded_port_id(bonded_port_id) != 0)
602 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
603 internals = bonded_eth_dev->data->dev_private;
605 internals->user_defined_mac = 0;
607 if (internals->slave_count > 0) {
608 struct slave_conf *conf;
609 conf = slave_config_get(internals, internals->primary_port);
611 /* Set MAC Address of Bonded Device */
612 if (mac_address_set(bonded_eth_dev, &conf->mac_addr) != 0)
615 /* Update all slave devices MAC addresses */
616 return mac_address_slaves_update(bonded_eth_dev);
618 /* No need to update anything as no slaves present */
623 rte_eth_bond_xmit_policy_set(uint8_t bonded_port_id, uint8_t policy)
625 struct bond_dev_private *internals;
627 if (valid_bonded_port_id(bonded_port_id) != 0)
630 internals = rte_eth_devices[bonded_port_id].data->dev_private;
633 case BALANCE_XMIT_POLICY_LAYER2:
634 case BALANCE_XMIT_POLICY_LAYER23:
635 case BALANCE_XMIT_POLICY_LAYER34:
636 internals->balance_xmit_policy = policy;
646 rte_eth_bond_xmit_policy_get(uint8_t bonded_port_id)
648 struct bond_dev_private *internals;
650 if (valid_bonded_port_id(bonded_port_id) != 0)
653 internals = rte_eth_devices[bonded_port_id].data->dev_private;
655 return internals->balance_xmit_policy;