bond: new link bonding library
[dpdk.git] / lib / librte_pmd_bond / rte_eth_bond_api.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <sys/queue.h>
35 #include <linux/binfmts.h>
36
37 #include <rte_mbuf.h>
38 #include <rte_cycles.h>
39 #include <rte_dev.h>
40 #include <rte_devargs.h>
41 #include <rte_ethdev.h>
42 #include <rte_ip.h>
43 #include <rte_kvargs.h>
44 #include <rte_malloc.h>
45 #include <rte_memcpy.h>
46 #include <rte_memory.h>
47 #include <rte_udp.h>
48
49 #include "rte_eth_bond.h"
50 #include "rte_eth_bond_private.h"
51
52 int
53 valid_bonded_ethdev(struct rte_eth_dev *eth_dev)
54 {
55         size_t len;
56
57         /* Check valid pointer */
58         if (eth_dev->driver->pci_drv.name == NULL || driver_name == NULL)
59                 return -1;
60
61         /* Check string lengths are equal */
62         len = strlen(driver_name);
63         if (strlen(eth_dev->driver->pci_drv.name) != len)
64                 return -1;
65
66         /* Compare strings */
67         return strncmp(eth_dev->driver->pci_drv.name, driver_name, len);
68 }
69
70 int
71 valid_port_id(uint8_t port_id)
72 {
73         /* Verify that port id is valid */
74         int ethdev_count = rte_eth_dev_count();
75         if (port_id >= ethdev_count) {
76                 RTE_LOG(ERR, PMD,
77                                 "%s: port Id %d is greater than rte_eth_dev_count %d\n",
78                                 __func__, port_id, ethdev_count);
79                 return -1;
80         }
81
82         return 0;
83 }
84
85 int
86 valid_bonded_port_id(uint8_t port_id)
87 {
88         /* Verify that port id's are valid */
89         if (valid_port_id(port_id))
90                 return -1;
91
92         /* Verify that bonded_port_id refers to a bonded port */
93         if (valid_bonded_ethdev(&rte_eth_devices[port_id])) {
94                 RTE_LOG(ERR, PMD,
95                                 "%s: Specified port Id %d is not a bonded eth_dev device\n",
96                                 __func__, port_id);
97                 return -1;
98         }
99
100         return 0;
101 }
102
103 int
104 valid_slave_port_id(uint8_t port_id)
105 {
106         /* Verify that port id's are valid */
107         if (valid_port_id(port_id))
108                 return -1;
109
110         /* Verify that port_id refers to a non bonded port */
111         if (!valid_bonded_ethdev(&rte_eth_devices[port_id]))
112                 return -1;
113
114         return 0;
115 }
116
117 uint8_t
118 number_of_sockets(void)
119 {
120         int sockets = 0;
121         int i;
122         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
123
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;
127         }
128
129         /* Number of sockets = maximum socket_id + 1 */
130         return ++sockets;
131 }
132
133 const char *driver_name = "Link Bonding PMD";
134
135 int
136 rte_eth_bond_create(const char *name, uint8_t mode, uint8_t socket_id)
137 {
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
146          */
147
148         if (name == NULL) {
149                 RTE_LOG(ERR, PMD, "Invalid name specified\n");
150                 goto err;
151         }
152
153         if (socket_id >= number_of_sockets()) {
154                 RTE_LOG(ERR, PMD,
155                                 "%s: invalid socket id specified to create bonded device on.\n",
156                                 __func__);
157                 goto err;
158         }
159
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");
163                 goto err;
164         }
165
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");
169                 goto err;
170         }
171
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");
175                 goto err;
176         }
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");
180                 goto err;
181         }
182
183         pci_drv->id_table = pci_id_table;
184
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;
189
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");
193                 goto err;
194         }
195
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");
200                 goto err;
201         }
202
203         pci_dev->numa_node = socket_id;
204         pci_drv->name = driver_name;
205
206         eth_drv->pci_drv = (struct rte_pci_driver)(*pci_drv);
207         eth_dev->driver = eth_drv;
208
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;
212
213         eth_dev->data->dev_link.link_status = 0;
214
215         eth_dev->data->mac_addrs = rte_zmalloc_socket(name, ETHER_ADDR_LEN, 0,
216                         socket_id);
217
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;
222
223         eth_dev->dev_ops = &default_dev_ops;
224         eth_dev->pci_dev = pci_dev;
225
226         if (bond_ethdev_mode_set(eth_dev, mode)) {
227                 RTE_LOG(ERR, PMD,
228                                 "%s: failed to set bonded device %d mode too %d\n",
229                                 __func__, eth_dev->data->port_id, mode);
230                 goto err;
231         }
232
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;
239
240         memset(internals->active_slaves, 0, sizeof(internals->active_slaves));
241         memset(internals->slaves, 0, sizeof(internals->slaves));
242
243         memset(internals->presisted_slaves_conf, 0,
244                         sizeof(internals->presisted_slaves_conf));
245
246         return eth_dev->data->port_id;
247
248 err:
249         if (pci_dev)
250                 rte_free(pci_dev);
251         if (pci_drv)
252                 rte_free(pci_drv);
253         if (pci_id_table)
254                 rte_free(pci_id_table);
255         if (eth_drv)
256                 rte_free(eth_drv);
257         if (internals)
258                 rte_free(internals);
259         return -1;
260 }
261
262 int
263 rte_eth_bond_slave_add(uint8_t bonded_port_id, uint8_t slave_port_id)
264 {
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;
269
270         int i, j;
271
272         /* Verify that port id's are valid bonded and slave ports */
273         if (valid_bonded_port_id(bonded_port_id) != 0)
274                 goto err_add;
275
276         if (valid_slave_port_id(slave_port_id) != 0)
277                 goto err_add;
278
279         /*
280          * Verify that new slave device is not already a slave of another bonded
281          * device */
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)
288                                         goto err_add;
289                         }
290                 }
291         }
292
293         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
294         internals = bonded_eth_dev->data->dev_private;
295
296         slave_eth_dev = &rte_eth_devices[slave_port_id];
297
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)
305                                 goto err_add;
306                 }
307         }
308
309         /* Add slave details to bonded device */
310         internals->slaves[internals->slave_count] = slave_port_id;
311
312         slave_config_store(internals, slave_eth_dev);
313
314         if (internals->slave_count < 1) {
315                 /* if MAC is not user defined then use MAC of first slave add to bonded
316                  * device */
317                 if (!internals->user_defined_mac)
318                         mac_address_set(bonded_eth_dev, slave_eth_dev->data->mac_addrs);
319
320                 /* Inherit eth dev link properties from first slave */
321                 link_properties_set(bonded_eth_dev, &(slave_eth_dev->data->dev_link));
322
323                 /* Make primary slave */
324                 internals->primary_port = slave_port_id;
325         } else {
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))) {
331                                 RTE_LOG(ERR, PMD,
332                                                 "%s: Slave port %d link speed/duplex not supported\n",
333                                                 __func__, slave_port_id);
334                                 goto err_add;
335                         }
336                 } else {
337                         link_properties_set(bonded_eth_dev,
338                                         &(slave_eth_dev->data->dev_link));
339                 }
340         }
341
342         internals->slave_count++;
343
344         /* Update all slave devices MACs*/
345         mac_address_slaves_update(bonded_eth_dev);
346
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",
350                                         slave_port_id);
351                         goto err_add;
352                 }
353         }
354
355         /* Register link status change callback with bonded device pointer as
356          * argument*/
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);
359
360         /* If bonded device is started then we can add the slave to our active
361          * slave array */
362         if (bonded_eth_dev->data->dev_started) {
363                 rte_eth_link_get_nowait(slave_port_id, &link_props);
364
365                  if (link_props.link_status == 1) {
366                         internals->active_slaves[internals->active_slave_count++] =
367                                         slave_port_id;
368                 }
369         }
370
371         return 0;
372
373 err_add:
374         RTE_LOG(ERR, PMD, "Failed to add port %d as slave\n", slave_port_id);
375         return -1;
376
377 }
378
379 int
380 rte_eth_bond_slave_remove(uint8_t bonded_port_id, uint8_t slave_port_id)
381 {
382         struct bond_dev_private *internals;
383         struct slave_conf *slave_conf;
384
385         int i;
386         int pos = -1;
387
388         /* Verify that port id's are valid bonded and slave ports */
389         if (valid_bonded_port_id(bonded_port_id) != 0)
390                 goto err_del;
391
392         if (valid_slave_port_id(slave_port_id) != 0)
393                 goto err_del;
394
395         internals = rte_eth_devices[bonded_port_id].data->dev_private;
396
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)
400                         pos = i;
401
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];
405         }
406
407         if (pos >= 0)
408                 internals->active_slave_count--;
409
410         pos = -1;
411         /* now remove from slave list */
412         for (i = 0; i < internals->slave_count; i++) {
413                 if (internals->slaves[i] == slave_port_id)
414                         pos = i;
415
416                 /* shift slaves up list */
417                 if (pos >= 0 && i < internals->slave_count)
418                         internals->slaves[i] = internals->slaves[i+1];
419         }
420
421         if (pos < 0)
422                 goto err_del;
423
424         /* Un-register link status change callback with bonded device pointer as
425          * argument*/
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);
429
430         /* Restore original MAC address of slave device */
431         slave_conf = slave_config_get(internals, slave_port_id);
432
433         mac_address_set(&rte_eth_devices[slave_port_id], &(slave_conf->mac_addr));
434
435         slave_config_clear(internals, &rte_eth_devices[slave_port_id]);
436
437         internals->slave_count--;
438
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];
446                 else
447                         internals->primary_port = 0;
448         }
449
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]);
453
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)));
460         }
461
462         return 0;
463
464 err_del:
465         RTE_LOG(ERR, PMD,
466                         "Cannot remove slave device (not present in bonded device)\n");
467         return -1;
468
469 }
470
471 int
472 rte_eth_bond_mode_set(uint8_t bonded_port_id, uint8_t mode)
473 {
474         if (valid_bonded_port_id(bonded_port_id) != 0)
475                 return -1;
476
477         return bond_ethdev_mode_set(&rte_eth_devices[bonded_port_id], mode);
478 }
479
480 int
481 rte_eth_bond_mode_get(uint8_t bonded_port_id)
482 {
483         struct bond_dev_private *internals;
484
485         if (valid_bonded_port_id(bonded_port_id) != 0)
486                 return -1;
487
488         internals = rte_eth_devices[bonded_port_id].data->dev_private;
489
490         return internals->mode;
491 }
492
493 int
494 rte_eth_bond_primary_set(uint8_t bonded_port_id, uint8_t slave_port_id)
495 {
496         struct bond_dev_private *internals;
497
498         if (valid_bonded_port_id(bonded_port_id) != 0)
499                 return -1;
500
501         if (valid_slave_port_id(slave_port_id) != 0)
502                 return -1;
503
504         internals =  rte_eth_devices[bonded_port_id].data->dev_private;
505
506         internals->user_defined_primary_port = 1;
507         internals->primary_port = slave_port_id;
508
509         bond_ethdev_primary_set(internals, slave_port_id);
510
511         return 0;
512 }
513
514 int
515 rte_eth_bond_primary_get(uint8_t bonded_port_id)
516 {
517         struct bond_dev_private *internals;
518
519         if (valid_bonded_port_id(bonded_port_id) != 0)
520                 return -1;
521
522         internals = rte_eth_devices[bonded_port_id].data->dev_private;
523
524         if (internals->slave_count < 1)
525                 return -1;
526
527         return internals->current_primary_port;
528 }
529 int
530 rte_eth_bond_slaves_get(uint8_t bonded_port_id, uint8_t slaves[], uint8_t len)
531 {
532         struct bond_dev_private *internals;
533
534         if (valid_bonded_port_id(bonded_port_id) != 0)
535                 return -1;
536
537         if (slaves == NULL)
538                 return -1;
539
540         internals = rte_eth_devices[bonded_port_id].data->dev_private;
541
542         if (internals->slave_count > len)
543                 return -1;
544
545         memcpy(slaves, internals->slaves, internals->slave_count);
546
547         return internals->slave_count;
548
549 }
550
551 int
552 rte_eth_bond_active_slaves_get(uint8_t bonded_port_id, uint8_t slaves[],
553                 uint8_t len)
554 {
555         struct bond_dev_private *internals;
556
557         if (valid_bonded_port_id(bonded_port_id) != 0)
558                 return -1;
559
560         if (slaves == NULL)
561                 return -1;
562
563         internals = rte_eth_devices[bonded_port_id].data->dev_private;
564
565         if (internals->active_slave_count > len)
566                 return -1;
567
568         memcpy(slaves, internals->active_slaves, internals->active_slave_count);
569
570         return internals->active_slave_count;
571 }
572
573 int
574 rte_eth_bond_mac_address_set(uint8_t bonded_port_id,
575                 struct ether_addr *mac_addr)
576 {
577         struct rte_eth_dev *bonded_eth_dev;
578         struct bond_dev_private *internals;
579
580         if (valid_bonded_port_id(bonded_port_id) != 0)
581                 return -1;
582
583         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
584         internals = bonded_eth_dev->data->dev_private;
585
586         /* Set MAC Address of Bonded Device */
587         if (mac_address_set(bonded_eth_dev, mac_addr))
588                 return -1;
589
590         internals->user_defined_mac = 1;
591
592         /* Update all slave devices MACs*/
593         if (internals->slave_count > 0)
594                 return mac_address_slaves_update(bonded_eth_dev);
595
596         return 0;
597 }
598
599 int
600 rte_eth_bond_mac_address_reset(uint8_t bonded_port_id)
601 {
602         struct rte_eth_dev *bonded_eth_dev;
603         struct bond_dev_private *internals;
604
605         if (valid_bonded_port_id(bonded_port_id) != 0)
606                 return -1;
607
608         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
609         internals = bonded_eth_dev->data->dev_private;
610
611         internals->user_defined_mac = 0;
612
613         if (internals->slave_count > 0) {
614                 struct slave_conf *conf;
615                 conf = slave_config_get(internals, internals->primary_port);
616
617                 /* Set MAC Address of Bonded Device */
618                 if (mac_address_set(bonded_eth_dev, &conf->mac_addr) != 0)
619                         return -1;
620
621                 /* Update all slave devices MAC addresses */
622                 return mac_address_slaves_update(bonded_eth_dev);
623         }
624         /* No need to update anything as no slaves present */
625         return 0;
626 }
627
628 int
629 rte_eth_bond_xmit_policy_set(uint8_t bonded_port_id, uint8_t policy)
630 {
631         struct bond_dev_private *internals;
632
633         if (valid_bonded_port_id(bonded_port_id) != 0)
634                 return -1;
635
636         internals = rte_eth_devices[bonded_port_id].data->dev_private;
637
638         switch (policy) {
639         case BALANCE_XMIT_POLICY_LAYER2:
640         case BALANCE_XMIT_POLICY_LAYER23:
641         case BALANCE_XMIT_POLICY_LAYER34:
642                 internals->balance_xmit_policy = policy;
643                 break;
644
645         default:
646                 return -1;
647         }
648         return 0;
649 }
650
651 int
652 rte_eth_bond_xmit_policy_get(uint8_t bonded_port_id)
653 {
654         struct bond_dev_private *internals;
655
656         if (valid_bonded_port_id(bonded_port_id) != 0)
657                 return -1;
658
659         internals = rte_eth_devices[bonded_port_id].data->dev_private;
660
661         return internals->balance_xmit_policy;
662 }