mk: no more bare metal environment
[dpdk.git] / lib / librte_pmd_bond / rte_eth_bond_args.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_devargs.h>
35 #include <rte_kvargs.h>
36
37 #include <cmdline_parse.h>
38 #include <cmdline_parse_etheraddr.h>
39
40 #include "rte_eth_bond.h"
41 #include "rte_eth_bond_private.h"
42
43 const char *pmd_bond_init_valid_arguments[] = {
44         PMD_BOND_SLAVE_PORT_KVARG,
45         PMD_BOND_PRIMARY_SLAVE_KVARG,
46         PMD_BOND_MODE_KVARG,
47         PMD_BOND_XMIT_POLICY_KVARG,
48         PMD_BOND_SOCKET_ID_KVARG,
49         PMD_BOND_MAC_ADDR_KVARG,
50
51         NULL
52 };
53
54 static inline int
55 find_port_id_by_pci_addr(const struct rte_pci_addr *pci_addr)
56 {
57         struct rte_pci_addr *eth_pci_addr;
58         unsigned i;
59
60         for (i = 0; i < rte_eth_dev_count(); i++) {
61
62                 if (rte_eth_devices[i].pci_dev == NULL)
63                         continue;
64
65                 eth_pci_addr = &(rte_eth_devices[i].pci_dev->addr);
66
67                 if (pci_addr->bus == eth_pci_addr->bus &&
68                         pci_addr->devid == eth_pci_addr->devid &&
69                         pci_addr->domain == eth_pci_addr->domain &&
70                         pci_addr->function == eth_pci_addr->function)
71                         return i;
72         }
73         return -1;
74 }
75
76 static inline int
77 find_port_id_by_dev_name(const char *name)
78 {
79         unsigned i;
80
81         for (i = 0; i < rte_eth_dev_count(); i++) {
82                 if (rte_eth_devices[i].data == NULL)
83                         continue;
84
85                 if (strcmp(rte_eth_devices[i].data->name, name) == 0)
86                         return i;
87         }
88         return -1;
89 }
90
91 /**
92  * Parses a port identifier string to a port id by pci address, then by name,
93  * and finally port id.
94  */
95 static inline int
96 parse_port_id(const char *port_str)
97 {
98         struct rte_pci_addr dev_addr;
99         int port_id;
100
101         /* try parsing as pci address, physical devices */
102         if (eal_parse_pci_DomBDF(port_str, &dev_addr) == 0) {
103                 port_id = find_port_id_by_pci_addr(&dev_addr);
104                 if (port_id < 0)
105                         return -1;
106         } else {
107                 /* try parsing as device name, virtual devices */
108                 port_id = find_port_id_by_dev_name(port_str);
109                 if (port_id < 0) {
110                         char *end;
111                         errno = 0;
112
113                         /* try parsing as port id */
114                         port_id = strtol(port_str, &end, 10);
115                         if (*end != 0 || errno != 0)
116                                 return -1;
117                 }
118         }
119
120         if (port_id < 0 || port_id > RTE_MAX_ETHPORTS) {
121                 RTE_BOND_LOG(ERR, "Slave port specified (%s) outside expected range",
122                                 port_str);
123                 return -1;
124         }
125         return port_id;
126 }
127
128 int
129 bond_ethdev_parse_slave_port_kvarg(const char *key __rte_unused,
130                 const char *value, void *extra_args)
131 {
132         struct bond_ethdev_slave_ports *slave_ports;
133
134         if (value == NULL || extra_args == NULL)
135                 return -1;
136
137         slave_ports = extra_args;
138
139         if (strcmp(key, PMD_BOND_SLAVE_PORT_KVARG) == 0) {
140                 int port_id = parse_port_id(value);
141                 if (port_id < 0) {
142                         RTE_BOND_LOG(ERR, "Invalid slave port value (%s) specified", value);
143                         return -1;
144                 } else
145                         slave_ports->slaves[slave_ports->slave_count++] =
146                                         (uint8_t)port_id;
147         }
148         return 0;
149 }
150
151 int
152 bond_ethdev_parse_slave_mode_kvarg(const char *key __rte_unused,
153                 const char *value, void *extra_args)
154 {
155         uint8_t *mode;
156         char *endptr;
157
158         if (value == NULL || extra_args == NULL)
159                 return -1;
160
161         mode = extra_args;
162
163         errno = 0;
164         *mode = strtol(value, &endptr, 10);
165         if (*endptr != 0 || errno != 0)
166                 return -1;
167
168         /* validate mode value */
169         switch (*mode) {
170         case BONDING_MODE_ROUND_ROBIN:
171         case BONDING_MODE_ACTIVE_BACKUP:
172         case BONDING_MODE_BALANCE:
173 #ifdef RTE_MBUF_REFCNT
174         case BONDING_MODE_BROADCAST:
175 #endif
176                 return 0;
177         default:
178                 RTE_BOND_LOG(ERR, "Invalid slave mode value (%s) specified", value);
179                 return -1;
180         }
181 }
182
183 int
184 bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused,
185                 const char *value, void *extra_args)
186 {
187         int socket_id;
188         char *endptr;
189
190         if (value == NULL || extra_args == NULL)
191                 return -1;
192
193         errno = 0;
194         socket_id = (uint8_t)strtol(value, &endptr, 10);
195         if (*endptr != 0 || errno != 0)
196                 return -1;
197
198         /* validate mode value */
199         if (socket_id >= 0 && socket_id < number_of_sockets()) {
200                 *(uint8_t *)extra_args = (uint8_t)socket_id;
201                 return 0;
202         }
203         return -1;
204 }
205
206 int
207 bond_ethdev_parse_primary_slave_port_id_kvarg(const char *key __rte_unused,
208                 const char *value, void *extra_args)
209 {
210         int primary_slave_port_id;
211
212         if (value == NULL || extra_args == NULL)
213                 return -1;
214
215         primary_slave_port_id = parse_port_id(value);
216         if (primary_slave_port_id < 0)
217                 return -1;
218
219         *(uint8_t *)extra_args = (uint8_t)primary_slave_port_id;
220
221         return 0;
222 }
223
224 int
225 bond_ethdev_parse_balance_xmit_policy_kvarg(const char *key __rte_unused,
226                 const char *value, void *extra_args)
227 {
228         uint8_t *xmit_policy;
229
230         if (value == NULL || extra_args == NULL)
231                 return -1;
232
233         xmit_policy = extra_args;
234
235         if (strcmp(PMD_BOND_XMIT_POLICY_LAYER2_KVARG, value) == 0)
236                 *xmit_policy = BALANCE_XMIT_POLICY_LAYER2;
237         else if (strcmp(PMD_BOND_XMIT_POLICY_LAYER23_KVARG, value) == 0)
238                 *xmit_policy = BALANCE_XMIT_POLICY_LAYER23;
239         else if (strcmp(PMD_BOND_XMIT_POLICY_LAYER34_KVARG, value) == 0)
240                 *xmit_policy = BALANCE_XMIT_POLICY_LAYER34;
241         else
242                 return -1;
243
244         return 0;
245 }
246
247 int
248 bond_ethdev_parse_bond_mac_addr_kvarg(const char *key __rte_unused,
249                 const char *value, void *extra_args)
250 {
251         if (value == NULL || extra_args == NULL)
252                 return -1;
253
254         /* Parse MAC */
255         return cmdline_parse_etheraddr(NULL, value, extra_args);
256 }
257
258 int
259 bond_ethdev_parse_time_ms_kvarg(const char *key __rte_unused,
260                 const char *value, void *extra_args)
261 {
262         uint32_t time_ms;
263         char *endptr;
264
265         if (value == NULL || extra_args == NULL)
266                 return -1;
267
268         errno = 0;
269         time_ms = (uint32_t)strtol(value, &endptr, 10);
270         if (*endptr != 0 || errno != 0)
271                 return -1;
272
273         *(uint32_t *)extra_args = time_ms;
274
275         return 0;
276 }