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.
34 #include <sys/queue.h>
35 #include <linux/binfmts.h>
38 #include <rte_cycles.h>
40 #include <rte_devargs.h>
41 #include <rte_ethdev.h>
43 #include <rte_kvargs.h>
44 #include <rte_malloc.h>
45 #include <rte_memcpy.h>
46 #include <rte_memory.h>
49 #include "rte_eth_bond.h"
50 #include "rte_eth_bond_private.h"
53 valid_bonded_ethdev(struct rte_eth_dev *eth_dev)
57 /* Check valid pointer */
58 if (eth_dev->driver->pci_drv.name == NULL || driver_name == NULL)
61 /* Check string lengths are equal */
62 len = strlen(driver_name);
63 if (strlen(eth_dev->driver->pci_drv.name) != len)
67 return strncmp(eth_dev->driver->pci_drv.name, driver_name, len);
71 valid_port_id(uint8_t port_id)
73 /* Verify that port id is valid */
74 int ethdev_count = rte_eth_dev_count();
75 if (port_id >= ethdev_count) {
77 "%s: port Id %d is greater than rte_eth_dev_count %d\n",
78 __func__, port_id, ethdev_count);
86 valid_bonded_port_id(uint8_t port_id)
88 /* Verify that port id's are valid */
89 if (valid_port_id(port_id))
92 /* Verify that bonded_port_id refers to a bonded port */
93 if (valid_bonded_ethdev(&rte_eth_devices[port_id])) {
95 "%s: Specified port Id %d is not a bonded eth_dev device\n",
104 valid_slave_port_id(uint8_t port_id)
106 /* Verify that port id's are valid */
107 if (valid_port_id(port_id))
110 /* Verify that port_id refers to a non bonded port */
111 if (!valid_bonded_ethdev(&rte_eth_devices[port_id]))
118 number_of_sockets(void)
122 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
124 for (i = 0; ((i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL)); i++) {
125 if (sockets < ms[i].socket_id)
126 sockets = ms[i].socket_id;
129 /* Number of sockets = maximum socket_id + 1 */
133 const char *driver_name = "Link Bonding PMD";
136 rte_eth_bond_create(const char *name, uint8_t mode, uint8_t socket_id)
138 struct rte_pci_device *pci_dev = NULL;
139 struct bond_dev_private *internals = NULL;
140 struct rte_eth_dev *eth_dev = NULL;
141 struct eth_driver *eth_drv = NULL;
142 struct rte_pci_driver *pci_drv = NULL;
143 struct rte_pci_id *pci_id_table = NULL;
144 /* now do all data allocation - for eth_dev structure, dummy pci driver
145 * and internal (private) data
149 RTE_LOG(ERR, PMD, "Invalid name specified\n");
153 if (socket_id >= number_of_sockets()) {
155 "%s: invalid socket id specified to create bonded device on.\n",
160 pci_dev = rte_zmalloc_socket(name, sizeof(*pci_dev), 0, socket_id);
161 if (pci_dev == NULL) {
162 RTE_LOG(ERR, PMD, "Unable to malloc pci dev on socket\n");
166 eth_drv = rte_zmalloc_socket(name, sizeof(*eth_drv), 0, socket_id);
167 if (eth_drv == NULL) {
168 RTE_LOG(ERR, PMD, "Unable to malloc eth_drv on socket\n");
172 pci_drv = rte_zmalloc_socket(name, sizeof(*pci_drv), 0, socket_id);
173 if (pci_drv == NULL) {
174 RTE_LOG(ERR, PMD, "Unable to malloc pci_drv on socket\n");
177 pci_id_table = rte_zmalloc_socket(name, sizeof(*pci_id_table), 0, socket_id);
178 if (pci_drv == NULL) {
179 RTE_LOG(ERR, PMD, "Unable to malloc pci_id_table on socket\n");
183 pci_drv->id_table = pci_id_table;
185 pci_drv->id_table->device_id = PCI_ANY_ID;
186 pci_drv->id_table->subsystem_device_id = PCI_ANY_ID;
187 pci_drv->id_table->vendor_id = PCI_ANY_ID;
188 pci_drv->id_table->subsystem_vendor_id = PCI_ANY_ID;
190 internals = rte_zmalloc_socket(name, sizeof(*internals), 0, socket_id);
191 if (internals == NULL) {
192 RTE_LOG(ERR, PMD, "Unable to malloc internals on socket\n");
196 /* reserve an ethdev entry */
197 eth_dev = rte_eth_dev_allocate(name);
198 if (eth_dev == NULL) {
199 RTE_LOG(ERR, PMD, "Unable to allocate rte_eth_dev\n");
203 pci_dev->numa_node = socket_id;
204 pci_drv->name = driver_name;
206 eth_drv->pci_drv = (struct rte_pci_driver)(*pci_drv);
207 eth_dev->driver = eth_drv;
209 eth_dev->data->dev_private = internals;
210 eth_dev->data->nb_rx_queues = (uint16_t)1;
211 eth_dev->data->nb_tx_queues = (uint16_t)1;
213 eth_dev->data->dev_link.link_status = 0;
215 eth_dev->data->mac_addrs = rte_zmalloc_socket(name, ETHER_ADDR_LEN, 0,
218 eth_dev->data->dev_started = 0;
219 eth_dev->data->promiscuous = 0;
220 eth_dev->data->scattered_rx = 0;
221 eth_dev->data->all_multicast = 0;
223 eth_dev->dev_ops = &default_dev_ops;
224 eth_dev->pci_dev = pci_dev;
226 if (bond_ethdev_mode_set(eth_dev, mode)) {
228 "%s: failed to set bonded device %d mode too %d\n",
229 __func__, eth_dev->data->port_id, mode);
233 internals->current_primary_port = 0;
234 internals->balance_xmit_policy = BALANCE_XMIT_POLICY_LAYER2;
235 internals->user_defined_mac = 0;
236 internals->link_props_set = 0;
237 internals->slave_count = 0;
238 internals->active_slave_count = 0;
240 memset(internals->active_slaves, 0, sizeof(internals->active_slaves));
241 memset(internals->slaves, 0, sizeof(internals->slaves));
243 memset(internals->presisted_slaves_conf, 0,
244 sizeof(internals->presisted_slaves_conf));
246 return eth_dev->data->port_id;
254 rte_free(pci_id_table);
263 rte_eth_bond_slave_add(uint8_t bonded_port_id, uint8_t slave_port_id)
265 struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
266 struct bond_dev_private *internals;
267 struct bond_dev_private *temp_internals;
268 struct rte_eth_link link_props;
272 /* Verify that port id's are valid bonded and slave ports */
273 if (valid_bonded_port_id(bonded_port_id) != 0)
276 if (valid_slave_port_id(slave_port_id) != 0)
280 * Verify that new slave device is not already a slave of another bonded
282 for (i = rte_eth_dev_count()-1; i >= 0; i--) {
283 if (valid_bonded_ethdev(&rte_eth_devices[i]) == 0) {
284 temp_internals = rte_eth_devices[i].data->dev_private;
285 for (j = 0; j < temp_internals->slave_count; j++) {
286 /* Device already a slave of a bonded device */
287 if (temp_internals->slaves[j] == slave_port_id)
293 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
294 internals = bonded_eth_dev->data->dev_private;
296 slave_eth_dev = &rte_eth_devices[slave_port_id];
298 if (internals->slave_count > 0) {
299 /* Check that new slave device is the same type as the other slaves
300 * and not repetitive */
301 for (i = 0; i < internals->slave_count; i++) {
302 if (slave_eth_dev->pci_dev->driver->id_table->device_id !=
303 rte_eth_devices[internals->slaves[i]].pci_dev->driver->id_table->device_id ||
304 internals->slaves[i] == slave_port_id)
309 /* Add slave details to bonded device */
310 internals->slaves[internals->slave_count] = slave_port_id;
312 slave_config_store(internals, slave_eth_dev);
314 if (internals->slave_count < 1) {
315 /* if MAC is not user defined then use MAC of first slave add to bonded
317 if (!internals->user_defined_mac)
318 mac_address_set(bonded_eth_dev, slave_eth_dev->data->mac_addrs);
320 /* Inherit eth dev link properties from first slave */
321 link_properties_set(bonded_eth_dev, &(slave_eth_dev->data->dev_link));
323 /* Make primary slave */
324 internals->primary_port = slave_port_id;
326 /* Check slave link properties are supported if props are set,
327 * all slaves must be the same */
328 if (internals->link_props_set) {
329 if (link_properties_valid(&(bonded_eth_dev->data->dev_link),
330 &(slave_eth_dev->data->dev_link))) {
332 "%s: Slave port %d link speed/duplex not supported\n",
333 __func__, slave_port_id);
337 link_properties_set(bonded_eth_dev,
338 &(slave_eth_dev->data->dev_link));
342 internals->slave_count++;
344 /* Update all slave devices MACs*/
345 mac_address_slaves_update(bonded_eth_dev);
347 if (bonded_eth_dev->data->dev_started) {
348 if (slave_configure(bonded_eth_dev, slave_eth_dev) != 0) {
349 RTE_LOG(ERR, PMD, "rte_bond_slaves_configure: port=%d\n",
355 /* Register link status change callback with bonded device pointer as
357 rte_eth_dev_callback_register(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
358 bond_ethdev_lsc_event_callback, &bonded_eth_dev->data->port_id);
360 /* If bonded device is started then we can add the slave to our active
362 if (bonded_eth_dev->data->dev_started) {
363 rte_eth_link_get_nowait(slave_port_id, &link_props);
365 if (link_props.link_status == 1) {
366 internals->active_slaves[internals->active_slave_count++] =
374 RTE_LOG(ERR, PMD, "Failed to add port %d as slave\n", slave_port_id);
380 rte_eth_bond_slave_remove(uint8_t bonded_port_id, uint8_t slave_port_id)
382 struct bond_dev_private *internals;
383 struct slave_conf *slave_conf;
388 /* Verify that port id's are valid bonded and slave ports */
389 if (valid_bonded_port_id(bonded_port_id) != 0)
392 if (valid_slave_port_id(slave_port_id) != 0)
395 internals = rte_eth_devices[bonded_port_id].data->dev_private;
397 /* first remove from active slave list */
398 for (i = 0; i < internals->active_slave_count; i++) {
399 if (internals->active_slaves[i] == slave_port_id)
402 /* shift active slaves up active array list */
403 if (pos >= 0 && i < (internals->active_slave_count - 1))
404 internals->active_slaves[i] = internals->active_slaves[i+1];
408 internals->active_slave_count--;
411 /* now remove from slave list */
412 for (i = 0; i < internals->slave_count; i++) {
413 if (internals->slaves[i] == slave_port_id)
416 /* shift slaves up list */
417 if (pos >= 0 && i < internals->slave_count)
418 internals->slaves[i] = internals->slaves[i+1];
424 /* Un-register link status change callback with bonded device pointer as
426 rte_eth_dev_callback_unregister(slave_port_id, RTE_ETH_EVENT_INTR_LSC,
427 bond_ethdev_lsc_event_callback,
428 &rte_eth_devices[bonded_port_id].data->port_id);
430 /* Restore original MAC address of slave device */
431 slave_conf = slave_config_get(internals, slave_port_id);
433 mac_address_set(&rte_eth_devices[slave_port_id], &(slave_conf->mac_addr));
435 slave_config_clear(internals, &rte_eth_devices[slave_port_id]);
437 internals->slave_count--;
439 /* first slave in the active list will be the primary by default,
440 * otherwise use first device in list */
441 if (internals->current_primary_port == slave_port_id) {
442 if (internals->active_slave_count > 0)
443 internals->current_primary_port = internals->active_slaves[0];
444 else if (internals->slave_count > 0)
445 internals->current_primary_port = internals->slaves[0];
447 internals->primary_port = 0;
450 if (internals->active_slave_count < 1) {
451 /* reset device link properties as no slaves are active */
452 link_properties_reset(&rte_eth_devices[bonded_port_id]);
454 /* if no slaves are any longer attached to bonded device and MAC is not
455 * user defined then clear MAC of bonded device as it will be reset
456 * when a new slave is added */
457 if (internals->slave_count < 1 && !internals->user_defined_mac)
458 memset(rte_eth_devices[bonded_port_id].data->mac_addrs, 0,
459 sizeof(*(rte_eth_devices[bonded_port_id].data->mac_addrs)));
466 "Cannot remove slave device (not present in bonded device)\n");
472 rte_eth_bond_mode_set(uint8_t bonded_port_id, uint8_t mode)
474 if (valid_bonded_port_id(bonded_port_id) != 0)
477 return bond_ethdev_mode_set(&rte_eth_devices[bonded_port_id], mode);
481 rte_eth_bond_mode_get(uint8_t bonded_port_id)
483 struct bond_dev_private *internals;
485 if (valid_bonded_port_id(bonded_port_id) != 0)
488 internals = rte_eth_devices[bonded_port_id].data->dev_private;
490 return internals->mode;
494 rte_eth_bond_primary_set(uint8_t bonded_port_id, uint8_t slave_port_id)
496 struct bond_dev_private *internals;
498 if (valid_bonded_port_id(bonded_port_id) != 0)
501 if (valid_slave_port_id(slave_port_id) != 0)
504 internals = rte_eth_devices[bonded_port_id].data->dev_private;
506 internals->user_defined_primary_port = 1;
507 internals->primary_port = slave_port_id;
509 bond_ethdev_primary_set(internals, slave_port_id);
515 rte_eth_bond_primary_get(uint8_t bonded_port_id)
517 struct bond_dev_private *internals;
519 if (valid_bonded_port_id(bonded_port_id) != 0)
522 internals = rte_eth_devices[bonded_port_id].data->dev_private;
524 if (internals->slave_count < 1)
527 return internals->current_primary_port;
530 rte_eth_bond_slaves_get(uint8_t bonded_port_id, uint8_t slaves[], uint8_t len)
532 struct bond_dev_private *internals;
534 if (valid_bonded_port_id(bonded_port_id) != 0)
540 internals = rte_eth_devices[bonded_port_id].data->dev_private;
542 if (internals->slave_count > len)
545 memcpy(slaves, internals->slaves, internals->slave_count);
547 return internals->slave_count;
552 rte_eth_bond_active_slaves_get(uint8_t bonded_port_id, uint8_t slaves[],
555 struct bond_dev_private *internals;
557 if (valid_bonded_port_id(bonded_port_id) != 0)
563 internals = rte_eth_devices[bonded_port_id].data->dev_private;
565 if (internals->active_slave_count > len)
568 memcpy(slaves, internals->active_slaves, internals->active_slave_count);
570 return internals->active_slave_count;
574 rte_eth_bond_mac_address_set(uint8_t bonded_port_id,
575 struct ether_addr *mac_addr)
577 struct rte_eth_dev *bonded_eth_dev;
578 struct bond_dev_private *internals;
580 if (valid_bonded_port_id(bonded_port_id) != 0)
583 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
584 internals = bonded_eth_dev->data->dev_private;
586 /* Set MAC Address of Bonded Device */
587 if (mac_address_set(bonded_eth_dev, mac_addr))
590 internals->user_defined_mac = 1;
592 /* Update all slave devices MACs*/
593 if (internals->slave_count > 0)
594 return mac_address_slaves_update(bonded_eth_dev);
600 rte_eth_bond_mac_address_reset(uint8_t bonded_port_id)
602 struct rte_eth_dev *bonded_eth_dev;
603 struct bond_dev_private *internals;
605 if (valid_bonded_port_id(bonded_port_id) != 0)
608 bonded_eth_dev = &rte_eth_devices[bonded_port_id];
609 internals = bonded_eth_dev->data->dev_private;
611 internals->user_defined_mac = 0;
613 if (internals->slave_count > 0) {
614 struct slave_conf *conf;
615 conf = slave_config_get(internals, internals->primary_port);
617 /* Set MAC Address of Bonded Device */
618 if (mac_address_set(bonded_eth_dev, &conf->mac_addr) != 0)
621 /* Update all slave devices MAC addresses */
622 return mac_address_slaves_update(bonded_eth_dev);
624 /* No need to update anything as no slaves present */
629 rte_eth_bond_xmit_policy_set(uint8_t bonded_port_id, uint8_t policy)
631 struct bond_dev_private *internals;
633 if (valid_bonded_port_id(bonded_port_id) != 0)
636 internals = rte_eth_devices[bonded_port_id].data->dev_private;
639 case BALANCE_XMIT_POLICY_LAYER2:
640 case BALANCE_XMIT_POLICY_LAYER23:
641 case BALANCE_XMIT_POLICY_LAYER34:
642 internals->balance_xmit_policy = policy;
652 rte_eth_bond_xmit_policy_get(uint8_t bonded_port_id)
654 struct bond_dev_private *internals;
656 if (valid_bonded_port_id(bonded_port_id) != 0)
659 internals = rte_eth_devices[bonded_port_id].data->dev_private;
661 return internals->balance_xmit_policy;