eal/bsd: fix fd leak
[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 <rte_mbuf.h>
35 #include <rte_malloc.h>
36 #include <rte_ethdev.h>
37 #include <rte_tcp.h>
38
39 #include "rte_eth_bond.h"
40 #include "rte_eth_bond_private.h"
41
42 int
43 valid_bonded_ethdev(struct rte_eth_dev *eth_dev)
44 {
45         size_t len;
46
47         /* Check valid pointer */
48         if (eth_dev->driver->pci_drv.name == NULL || driver_name == NULL)
49                 return -1;
50
51         /* Check string lengths are equal */
52         len = strlen(driver_name);
53         if (strlen(eth_dev->driver->pci_drv.name) != len)
54                 return -1;
55
56         /* Compare strings */
57         return strncmp(eth_dev->driver->pci_drv.name, driver_name, len);
58 }
59
60 int
61 valid_port_id(uint8_t port_id)
62 {
63         /* Verify that port id is valid */
64         int ethdev_count = rte_eth_dev_count();
65         if (port_id >= ethdev_count) {
66                 RTE_LOG(ERR, PMD,
67                                 "%s: port Id %d is greater than rte_eth_dev_count %d\n",
68                                 __func__, port_id, ethdev_count);
69                 return -1;
70         }
71
72         return 0;
73 }
74
75 int
76 valid_bonded_port_id(uint8_t port_id)
77 {
78         /* Verify that port id's are valid */
79         if (valid_port_id(port_id))
80                 return -1;
81
82         /* Verify that bonded_port_id refers to a bonded port */
83         if (valid_bonded_ethdev(&rte_eth_devices[port_id])) {
84                 RTE_LOG(ERR, PMD,
85                                 "%s: Specified port Id %d is not a bonded eth_dev device\n",
86                                 __func__, port_id);
87                 return -1;
88         }
89
90         return 0;
91 }
92
93 int
94 valid_slave_port_id(uint8_t port_id)
95 {
96         /* Verify that port id's are valid */
97         if (valid_port_id(port_id))
98                 return -1;
99
100         /* Verify that port_id refers to a non bonded port */
101         if (!valid_bonded_ethdev(&rte_eth_devices[port_id]))
102                 return -1;
103
104         return 0;
105 }
106
107 uint8_t
108 number_of_sockets(void)
109 {
110         int sockets = 0;
111         int i;
112         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
113
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;
117         }
118
119         /* Number of sockets = maximum socket_id + 1 */
120         return ++sockets;
121 }
122
123 const char *driver_name = "Link Bonding PMD";
124
125 int
126 rte_eth_bond_create(const char *name, uint8_t mode, uint8_t socket_id)
127 {
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
136          */
137
138         if (name == NULL) {
139                 RTE_LOG(ERR, PMD, "Invalid name specified\n");
140                 goto err;
141         }
142
143         if (socket_id >= number_of_sockets()) {
144                 RTE_LOG(ERR, PMD,
145                                 "%s: invalid socket id specified to create bonded device on.\n",
146                                 __func__);
147                 goto err;
148         }
149
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");
153                 goto err;
154         }
155
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");
159                 goto err;
160         }
161
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");
165                 goto err;
166         }
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");
170                 goto err;
171         }
172
173         pci_drv->id_table = pci_id_table;
174
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;
179
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");
183                 goto err;
184         }
185
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");
190                 goto err;
191         }
192
193         pci_dev->numa_node = socket_id;
194         pci_drv->name = driver_name;
195
196         eth_drv->pci_drv = (struct rte_pci_driver)(*pci_drv);
197         eth_dev->driver = eth_drv;
198
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;
202
203         eth_dev->data->dev_link.link_status = 0;
204
205         eth_dev->data->mac_addrs = rte_zmalloc_socket(name, ETHER_ADDR_LEN, 0,
206                         socket_id);
207
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;
212
213         eth_dev->dev_ops = &default_dev_ops;
214         eth_dev->pci_dev = pci_dev;
215
216         if (bond_ethdev_mode_set(eth_dev, mode)) {
217                 RTE_LOG(ERR, PMD,
218                                 "%s: failed to set bonded device %d mode too %d\n",
219                                 __func__, eth_dev->data->port_id, mode);
220                 goto err;
221         }
222
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;
229
230         memset(internals->active_slaves, 0, sizeof(internals->active_slaves));
231         memset(internals->slaves, 0, sizeof(internals->slaves));
232
233         memset(internals->presisted_slaves_conf, 0,
234                         sizeof(internals->presisted_slaves_conf));
235
236         return eth_dev->data->port_id;
237
238 err:
239         if (pci_dev)
240                 rte_free(pci_dev);
241         if (pci_drv)
242                 rte_free(pci_drv);
243         if (pci_id_table)
244                 rte_free(pci_id_table);
245         if (eth_drv)
246                 rte_free(eth_drv);
247         if (internals)
248                 rte_free(internals);
249         return -1;
250 }
251
252 int
253 rte_eth_bond_slave_add(uint8_t bonded_port_id, uint8_t slave_port_id)
254 {
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;
259
260         int i, j;
261
262         /* Verify that port id's are valid bonded and slave ports */
263         if (valid_bonded_port_id(bonded_port_id) != 0)
264                 goto err_add;
265
266         if (valid_slave_port_id(slave_port_id) != 0)
267                 goto err_add;
268
269         /*
270          * Verify that new slave device is not already a slave of another bonded
271          * device */
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)
278                                         goto err_add;
279                         }
280                 }
281         }
282
283         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
284         internals = bonded_eth_dev->data->dev_private;
285
286         slave_eth_dev = &rte_eth_devices[slave_port_id];
287
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)
295                                 goto err_add;
296                 }
297         }
298
299         /* Add slave details to bonded device */
300         internals->slaves[internals->slave_count] = slave_port_id;
301
302         slave_config_store(internals, slave_eth_dev);
303
304         if (internals->slave_count < 1) {
305                 /* if MAC is not user defined then use MAC of first slave add to bonded
306                  * device */
307                 if (!internals->user_defined_mac)
308                         mac_address_set(bonded_eth_dev, slave_eth_dev->data->mac_addrs);
309
310                 /* Inherit eth dev link properties from first slave */
311                 link_properties_set(bonded_eth_dev, &(slave_eth_dev->data->dev_link));
312
313                 /* Make primary slave */
314                 internals->primary_port = slave_port_id;
315         } else {
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))) {
321                                 RTE_LOG(ERR, PMD,
322                                                 "%s: Slave port %d link speed/duplex not supported\n",
323                                                 __func__, slave_port_id);
324                                 goto err_add;
325                         }
326                 } else {
327                         link_properties_set(bonded_eth_dev,
328                                         &(slave_eth_dev->data->dev_link));
329                 }
330         }
331
332         internals->slave_count++;
333
334         /* Update all slave devices MACs*/
335         mac_address_slaves_update(bonded_eth_dev);
336
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",
340                                         slave_port_id);
341                         goto err_add;
342                 }
343         }
344
345         /* Register link status change callback with bonded device pointer as
346          * argument*/
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);
349
350         /* If bonded device is started then we can add the slave to our active
351          * slave array */
352         if (bonded_eth_dev->data->dev_started) {
353                 rte_eth_link_get_nowait(slave_port_id, &link_props);
354
355                  if (link_props.link_status == 1) {
356                         internals->active_slaves[internals->active_slave_count++] =
357                                         slave_port_id;
358                 }
359         }
360
361         return 0;
362
363 err_add:
364         RTE_LOG(ERR, PMD, "Failed to add port %d as slave\n", slave_port_id);
365         return -1;
366
367 }
368
369 int
370 rte_eth_bond_slave_remove(uint8_t bonded_port_id, uint8_t slave_port_id)
371 {
372         struct bond_dev_private *internals;
373         struct slave_conf *slave_conf;
374
375         int i;
376         int pos = -1;
377
378         /* Verify that port id's are valid bonded and slave ports */
379         if (valid_bonded_port_id(bonded_port_id) != 0)
380                 goto err_del;
381
382         if (valid_slave_port_id(slave_port_id) != 0)
383                 goto err_del;
384
385         internals = rte_eth_devices[bonded_port_id].data->dev_private;
386
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)
390                         pos = i;
391
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];
395         }
396
397         if (pos >= 0)
398                 internals->active_slave_count--;
399
400         pos = -1;
401         /* now remove from slave list */
402         for (i = 0; i < internals->slave_count; i++) {
403                 if (internals->slaves[i] == slave_port_id)
404                         pos = i;
405
406                 /* shift slaves up list */
407                 if (pos >= 0 && i < internals->slave_count)
408                         internals->slaves[i] = internals->slaves[i+1];
409         }
410
411         if (pos < 0)
412                 goto err_del;
413
414         /* Un-register link status change callback with bonded device pointer as
415          * argument*/
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);
419
420         /* Restore original MAC address of slave device */
421         slave_conf = slave_config_get(internals, slave_port_id);
422
423         mac_address_set(&rte_eth_devices[slave_port_id], &(slave_conf->mac_addr));
424
425         slave_config_clear(internals, &rte_eth_devices[slave_port_id]);
426
427         internals->slave_count--;
428
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];
436                 else
437                         internals->primary_port = 0;
438         }
439
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]);
443
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)));
450         }
451
452         return 0;
453
454 err_del:
455         RTE_LOG(ERR, PMD,
456                         "Cannot remove slave device (not present in bonded device)\n");
457         return -1;
458
459 }
460
461 int
462 rte_eth_bond_mode_set(uint8_t bonded_port_id, uint8_t mode)
463 {
464         if (valid_bonded_port_id(bonded_port_id) != 0)
465                 return -1;
466
467         return bond_ethdev_mode_set(&rte_eth_devices[bonded_port_id], mode);
468 }
469
470 int
471 rte_eth_bond_mode_get(uint8_t bonded_port_id)
472 {
473         struct bond_dev_private *internals;
474
475         if (valid_bonded_port_id(bonded_port_id) != 0)
476                 return -1;
477
478         internals = rte_eth_devices[bonded_port_id].data->dev_private;
479
480         return internals->mode;
481 }
482
483 int
484 rte_eth_bond_primary_set(uint8_t bonded_port_id, uint8_t slave_port_id)
485 {
486         struct bond_dev_private *internals;
487
488         if (valid_bonded_port_id(bonded_port_id) != 0)
489                 return -1;
490
491         if (valid_slave_port_id(slave_port_id) != 0)
492                 return -1;
493
494         internals =  rte_eth_devices[bonded_port_id].data->dev_private;
495
496         internals->user_defined_primary_port = 1;
497         internals->primary_port = slave_port_id;
498
499         bond_ethdev_primary_set(internals, slave_port_id);
500
501         return 0;
502 }
503
504 int
505 rte_eth_bond_primary_get(uint8_t bonded_port_id)
506 {
507         struct bond_dev_private *internals;
508
509         if (valid_bonded_port_id(bonded_port_id) != 0)
510                 return -1;
511
512         internals = rte_eth_devices[bonded_port_id].data->dev_private;
513
514         if (internals->slave_count < 1)
515                 return -1;
516
517         return internals->current_primary_port;
518 }
519 int
520 rte_eth_bond_slaves_get(uint8_t bonded_port_id, uint8_t slaves[], uint8_t len)
521 {
522         struct bond_dev_private *internals;
523
524         if (valid_bonded_port_id(bonded_port_id) != 0)
525                 return -1;
526
527         if (slaves == NULL)
528                 return -1;
529
530         internals = rte_eth_devices[bonded_port_id].data->dev_private;
531
532         if (internals->slave_count > len)
533                 return -1;
534
535         memcpy(slaves, internals->slaves, internals->slave_count);
536
537         return internals->slave_count;
538
539 }
540
541 int
542 rte_eth_bond_active_slaves_get(uint8_t bonded_port_id, uint8_t slaves[],
543                 uint8_t len)
544 {
545         struct bond_dev_private *internals;
546
547         if (valid_bonded_port_id(bonded_port_id) != 0)
548                 return -1;
549
550         if (slaves == NULL)
551                 return -1;
552
553         internals = rte_eth_devices[bonded_port_id].data->dev_private;
554
555         if (internals->active_slave_count > len)
556                 return -1;
557
558         memcpy(slaves, internals->active_slaves, internals->active_slave_count);
559
560         return internals->active_slave_count;
561 }
562
563 int
564 rte_eth_bond_mac_address_set(uint8_t bonded_port_id,
565                 struct ether_addr *mac_addr)
566 {
567         struct rte_eth_dev *bonded_eth_dev;
568         struct bond_dev_private *internals;
569
570         if (valid_bonded_port_id(bonded_port_id) != 0)
571                 return -1;
572
573         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
574         internals = bonded_eth_dev->data->dev_private;
575
576         /* Set MAC Address of Bonded Device */
577         if (mac_address_set(bonded_eth_dev, mac_addr))
578                 return -1;
579
580         internals->user_defined_mac = 1;
581
582         /* Update all slave devices MACs*/
583         if (internals->slave_count > 0)
584                 return mac_address_slaves_update(bonded_eth_dev);
585
586         return 0;
587 }
588
589 int
590 rte_eth_bond_mac_address_reset(uint8_t bonded_port_id)
591 {
592         struct rte_eth_dev *bonded_eth_dev;
593         struct bond_dev_private *internals;
594
595         if (valid_bonded_port_id(bonded_port_id) != 0)
596                 return -1;
597
598         bonded_eth_dev = &rte_eth_devices[bonded_port_id];
599         internals = bonded_eth_dev->data->dev_private;
600
601         internals->user_defined_mac = 0;
602
603         if (internals->slave_count > 0) {
604                 struct slave_conf *conf;
605                 conf = slave_config_get(internals, internals->primary_port);
606
607                 /* Set MAC Address of Bonded Device */
608                 if (mac_address_set(bonded_eth_dev, &conf->mac_addr) != 0)
609                         return -1;
610
611                 /* Update all slave devices MAC addresses */
612                 return mac_address_slaves_update(bonded_eth_dev);
613         }
614         /* No need to update anything as no slaves present */
615         return 0;
616 }
617
618 int
619 rte_eth_bond_xmit_policy_set(uint8_t bonded_port_id, uint8_t policy)
620 {
621         struct bond_dev_private *internals;
622
623         if (valid_bonded_port_id(bonded_port_id) != 0)
624                 return -1;
625
626         internals = rte_eth_devices[bonded_port_id].data->dev_private;
627
628         switch (policy) {
629         case BALANCE_XMIT_POLICY_LAYER2:
630         case BALANCE_XMIT_POLICY_LAYER23:
631         case BALANCE_XMIT_POLICY_LAYER34:
632                 internals->balance_xmit_policy = policy;
633                 break;
634
635         default:
636                 return -1;
637         }
638         return 0;
639 }
640
641 int
642 rte_eth_bond_xmit_policy_get(uint8_t bonded_port_id)
643 {
644         struct bond_dev_private *internals;
645
646         if (valid_bonded_port_id(bonded_port_id) != 0)
647                 return -1;
648
649         internals = rte_eth_devices[bonded_port_id].data->dev_private;
650
651         return internals->balance_xmit_policy;
652 }