summaryrefslogtreecommitdiff
path: root/usr/src/lib/libdladm/common/libdlether.c
blob: 4855dc234d62b835601355658eea3cae1cae24fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <libdladm_impl.h>
#include <libdllink.h>
#include <libdlstat.h>
#include <libdlether.h>

/*
 * Ethernet administration library.
 */

/*
 * kstat names for extracting attributes.
 */
typedef struct ether_spdx_s {
	dladm_ether_spdx_t eth_spdx;
	char *eth_spdx_stat_name;
} ether_spdx_t;

static ether_spdx_t cap_spdx[] = {
	{{1000, LINK_DUPLEX_FULL}, "cap_1000fdx"},
	{{1000, LINK_DUPLEX_HALF}, "cap_1000hdx"},
	{{100, LINK_DUPLEX_FULL}, "cap_100fdx"},
	{{100, LINK_DUPLEX_HALF}, "cap_100hdx"},
	{{10, LINK_DUPLEX_FULL}, "cap_10fdx"},
	{{10, LINK_DUPLEX_HALF}, "cap_10hdx"},
	{{0, LINK_DUPLEX_UNKNOWN}, NULL}
};

static ether_spdx_t adv_cap_spdx[] = {
	{{1000, LINK_DUPLEX_FULL}, "adv_cap_1000fdx"},
	{{1000, LINK_DUPLEX_HALF}, "adv_cap_1000hdx"},
	{{100, LINK_DUPLEX_FULL}, "adv_cap_100fdx"},
	{{100, LINK_DUPLEX_HALF}, "adv_cap_100hdx"},
	{{10, LINK_DUPLEX_FULL}, "adv_cap_10fdx"},
	{{10, LINK_DUPLEX_HALF}, "adv_cap_10hdx"},
	{{0, LINK_DUPLEX_UNKNOWN}, NULL}
};

static ether_spdx_t lp_cap_spdx[] = {
	{{1000, LINK_DUPLEX_FULL}, "lp_cap_1000fdx"},
	{{1000, LINK_DUPLEX_HALF}, "lp_cap_1000hdx"},
	{{100, LINK_DUPLEX_FULL}, "lp_cap_100fdx"},
	{{100, LINK_DUPLEX_HALF}, "lp_cap_100hdx"},
	{{10, LINK_DUPLEX_FULL}, "lp_cap_10fdx"},
	{{10, LINK_DUPLEX_HALF}, "lp_cap_10hdx"},
	{{0, LINK_DUPLEX_UNKNOWN}, NULL}
};

typedef struct attr_kstat_s {
	char *autoneg_stat;
	char *pause_stat;
	char *asmpause_stat;
	char *fault_stat;
	ether_spdx_t *spdx_stat;
} attr_kstat_t;

static attr_kstat_t attrstat[] =  {
	{"link_autoneg",	/* current */
	    "link_pause",	"link_asmpause",	NULL,
	    NULL},

	{"cap_autoneg",		/* capable */
	    "cap_pause",	"cap_asmpause",		"cap_rem_fault",
	    cap_spdx},

	{"adv_cap_autoneg",	/* advertised */
	    "adv_cap_pause",	"adv_cap_asmpause",	"adv_rem_fault",
	    adv_cap_spdx},

	{"lp_cap_autoneg",	/* peer advertised */
	    "lp_cap_pause",	"lp_cap_asmpause",	"lp_rem_fault",
	    lp_cap_spdx}
};

/*
 * Get the speed-duplex stats specified in the ether_spdx_t table passed in
 * by querying the appropriate kstat for each entry in the table.
 */
static dladm_status_t
i_dladm_get_spdx(dladm_handle_t handle, datalink_id_t linkid,
    dladm_ether_attr_t *eattr, ether_spdx_t *spdx_stat)
{
	int		i, nspdx = 0;
	uint32_t	speed;
	dladm_status_t	status;
	void		*ptr;

	eattr->le_spdx = NULL;
	for (i = 0; spdx_stat[i].eth_spdx_stat_name != NULL; i++) {
		if ((status = dladm_get_single_mac_stat(handle, linkid,
		    spdx_stat[i].eth_spdx_stat_name,
		    KSTAT_DATA_UINT32, &speed)) != DLADM_STATUS_OK) {

			if (status == DLADM_STATUS_NOTFOUND) {
				/*
				 * Missing statistic.
				 * Skip this one and try the rest.
				 */
				continue;
			} else {
				free(eattr->le_spdx);
				eattr->le_num_spdx = 0;
				return (status);
			}
		}
		if (speed == 0)
			continue;
		nspdx++;
		ptr = realloc(eattr->le_spdx,
		    nspdx * sizeof (dladm_ether_spdx_t));
		if (ptr != NULL) {
			eattr->le_spdx = ptr;
		} else {
			free(eattr->le_spdx);
			eattr->le_num_spdx = 0;
			return (DLADM_STATUS_NOMEM);
		}
		eattr->le_spdx[nspdx - 1] = spdx_stat[i].eth_spdx;
	}
	eattr->le_num_spdx = nspdx;
	return (DLADM_STATUS_OK);
}

/*
 * Returns "yes" or "no" based on the autonegotion capabilities
 * for the parameter type indicated by ptype. The permissible
 * values for ptype are CURRENT, CAPABLE, ADV, PEERADV.
 */
char *
dladm_ether_autoneg2str(char *buf, size_t buflen, dladm_ether_info_t *eattr,
    int ptype)
{
	boolean_t autoneg = eattr->lei_attr[ptype].le_autoneg;

	(void) strlcpy(buf, (autoneg ? "yes" : "no"), buflen);
	return (buf);
}

/*
 * Returns {"bi", "tx", "none"} based on the flow-control capabilities
 * for the parameter type indicated by ptype. The permissible
 * values for ptype are CURRENT, CAPABLE, ADV, PEERADV.
 */
char *
dladm_ether_pause2str(char *buf, size_t buflen, dladm_ether_info_t *eattr,
    int ptype)
{
	boolean_t pause = eattr->lei_attr[ptype].le_pause;
	boolean_t asmpause = eattr->lei_attr[ptype].le_asmpause;

	if (pause)
		(void) strlcpy(buf, "bi", buflen);
	else if (asmpause)
		(void) strlcpy(buf, "tx", buflen);
	else
		(void) strlcpy(buf, "none", buflen);
	return (buf);
}

/*
 * For a given param type, parse the list of speed-duplex pairs in
 * the dladm_ether_info_t and return a  comma-separated string formatted
 * as <speed><speed-unit-char>-<duplex-chars> where <speed> is the value of
 * speed, in units specifid by the <speed-unit-char> which is one
 * of 'M' (Mbits/sec) or 'G' (Gigabits/sec).  The permissible values of
 * <duplex-chars> are 'u' (indicating duplex is "unknown") or one/both of
 * 'f', 'h' (indicating full-duplex and half-duplex respectively)
 */
extern char *
dladm_ether_spdx2str(char *buf, size_t buflen, dladm_ether_info_t *eattr,
    int ptype)
{
	int		i, j;
	boolean_t	is_full, is_half;
	int		speed;
	char		speed_unit;
	char		tmpbuf[DLADM_STRSIZE];
	dladm_ether_spdx_t *spdx;
	uint32_t	nspdx;

	spdx = eattr->lei_attr[ptype].le_spdx;
	nspdx = eattr->lei_attr[ptype].le_num_spdx;
	for (i = 0; i < nspdx; i++) {

		speed = spdx[i].lesd_speed;

		/*
		 * if we have already covered this speed for
		 * the <other>-duplex case before this, skip it
		 */
		for (j = 0; j < i; j++) {
			if (speed == spdx[j].lesd_speed)
				break;
		}
		if (j < i)
			continue;

		if (speed >= 1000) {
			speed = speed/1000;
			speed_unit = 'G';
		} else {
			speed_unit = 'M';
		}
		(void) snprintf(tmpbuf, DLADM_STRSIZE, "%d%c",
		    speed, speed_unit);
		if (i > 0)
			(void) strncat(buf, ",", buflen);
		(void) strncat(buf, tmpbuf, buflen);

		is_full = is_half = B_FALSE;
		/*
		 * Find all the supported duplex values for this speed.
		 */
		for (j = 0; j < nspdx; j++) {
			if (spdx[j].lesd_speed != spdx[i].lesd_speed)
				continue;
			if (spdx[j].lesd_duplex == LINK_DUPLEX_FULL)
				is_full = B_TRUE;
			if (spdx[j].lesd_duplex == LINK_DUPLEX_HALF)
				is_half = B_TRUE;
		}
		if (is_full && is_half)
			(void) strncat(buf, "-fh", buflen);
		else if (is_full)
			(void) strncat(buf, "-f", buflen);
		else if (is_half)
			(void) strncat(buf, "-h", buflen);
	}
	return (buf);
}

/*
 * Extract Ethernet attributes of the link specified by linkid.
 * Information for the CURRENT, CAPABLE, ADV and PEERADV parameter
 * types is extracted into the lei_attr[] entries in the dladm_ether_info_t.
 * On succesful return, the memory allocated in this function should be
 * freed by calling dladm_ether_info_done().
 */
extern dladm_status_t
dladm_ether_info(dladm_handle_t handle, datalink_id_t linkid,
    dladm_ether_info_t *eattr)
{
	uint32_t	autoneg, pause, asmpause, fault;
	uint64_t	sp64;
	dladm_status_t	status;
	int		i;
	link_duplex_t	link_duplex;

	bzero(eattr, sizeof (*eattr));
	status = dladm_datalink_id2info(handle, linkid, NULL, NULL, NULL,
	    eattr->lei_linkname, sizeof (eattr->lei_linkname));
	if (status != DLADM_STATUS_OK)
		goto bail;

	/* get current values of speed, duplex, state of link */
	eattr->lei_attr[CURRENT].le_num_spdx = 1;
	eattr->lei_attr[CURRENT].le_spdx = malloc(sizeof (dladm_ether_spdx_t));
	if (eattr->lei_attr[CURRENT].le_spdx == NULL) {
		status = DLADM_STATUS_NOMEM;
		goto bail;
	}

	if ((status = dladm_get_single_mac_stat(handle, linkid, "ifspeed",
	    KSTAT_DATA_UINT64, &sp64)) != DLADM_STATUS_OK)
		goto bail;

	if ((status = dladm_get_single_mac_stat(handle, linkid, "link_duplex",
	    KSTAT_DATA_UINT32, &link_duplex)) != DLADM_STATUS_OK)
		goto bail;

	eattr->lei_attr[CURRENT].le_spdx->lesd_speed = (int)(sp64/1000000ull);
	eattr->lei_attr[CURRENT].le_spdx->lesd_duplex = link_duplex;

	status = dladm_get_state(handle, linkid, &eattr->lei_state);
	if (status != DLADM_STATUS_OK)
		goto bail;

	/* get the auto, pause, asmpause, fault values */
	for (i = CURRENT; i <= PEERADV; i++)  {

		status = dladm_get_single_mac_stat(handle, linkid,
		    attrstat[i].autoneg_stat, KSTAT_DATA_UINT32, &autoneg);
		if (status != DLADM_STATUS_OK)
			goto bail;

		status = dladm_get_single_mac_stat(handle, linkid,
		    attrstat[i].pause_stat, KSTAT_DATA_UINT32, &pause);
		if (status != DLADM_STATUS_OK)
			goto bail;

		status = dladm_get_single_mac_stat(handle, linkid,
		    attrstat[i].asmpause_stat, KSTAT_DATA_UINT32, &asmpause);
		if (status != DLADM_STATUS_OK)
			goto bail;

		eattr->lei_attr[i].le_autoneg = (autoneg != 0);
		eattr->lei_attr[i].le_pause = (pause != 0);
		eattr->lei_attr[i].le_asmpause = (asmpause != 0);

		if (i == CURRENT)
			continue;
		status = dladm_get_single_mac_stat(handle, linkid,
		    attrstat[i].fault_stat, KSTAT_DATA_UINT32, &fault);
		if (status != DLADM_STATUS_OK)
			goto bail;
		eattr->lei_attr[i].le_fault = (pause != 0);

		/* get all the supported speed/duplex values */
		status = i_dladm_get_spdx(handle, linkid, &eattr->lei_attr[i],
		    attrstat[i].spdx_stat);
		if (status != DLADM_STATUS_OK)
			goto bail;
	}
	eattr->lei_attr[CURRENT].le_fault =
	    eattr->lei_attr[ADV].le_fault || eattr->lei_attr[PEERADV].le_fault;
bail:
	if (status != DLADM_STATUS_OK)
		dladm_ether_info_done(eattr);
	return (status);
}

extern void
dladm_ether_info_done(dladm_ether_info_t *eattr)
{
	int i;

	for (i = CURRENT; i <= PEERADV; i++)
		free(eattr->lei_attr[i].le_spdx);
}