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