xref: /illumos-gate/usr/src/uts/common/io/mac/plugins/mac_ether.c (revision 968633ad8faee931821fd6b656eb0d96d4b186c0)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Ethernet MAC plugin for the Nemo mac module
28  */
29 
30 #include <sys/types.h>
31 #include <sys/modctl.h>
32 #include <sys/dlpi.h>
33 #include <sys/mac.h>
34 #include <sys/mac_ether.h>
35 #include <sys/dls.h>
36 #include <sys/ethernet.h>
37 #include <sys/byteorder.h>
38 #include <sys/strsun.h>
39 #include <inet/common.h>
40 
41 static uint8_t	ether_brdcst[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
42 
43 static mac_stat_info_t ether_stats[] = {
44 	/* RFC1643 stats */
45 	{ ETHER_STAT_ALIGN_ERRORS, "align_errors", KSTAT_DATA_UINT32,	0 },
46 	{ ETHER_STAT_FCS_ERRORS, "fcs_errors", KSTAT_DATA_UINT32,	0 },
47 	{ ETHER_STAT_FIRST_COLLISIONS, "first_collisions", KSTAT_DATA_UINT32,
48 	    0 },
49 	{ ETHER_STAT_MULTI_COLLISIONS, "multi_collisions", KSTAT_DATA_UINT32,
50 	    0 },
51 	{ ETHER_STAT_SQE_ERRORS, "sqe_errors", KSTAT_DATA_UINT32,	0},
52 	{ ETHER_STAT_DEFER_XMTS, "defer_xmts", KSTAT_DATA_UINT32,	0},
53 	{ ETHER_STAT_TX_LATE_COLLISIONS, "tx_late_collisions",
54 	    KSTAT_DATA_UINT32, 0 },
55 	{ ETHER_STAT_EX_COLLISIONS, "ex_collisions", KSTAT_DATA_UINT32,	0 },
56 	{ ETHER_STAT_MACXMT_ERRORS, "macxmt_errors", KSTAT_DATA_UINT32,	0 },
57 	{ ETHER_STAT_CARRIER_ERRORS, "carrier_errors", KSTAT_DATA_UINT32, 0 },
58 	{ ETHER_STAT_TOOLONG_ERRORS, "toolong_errors", KSTAT_DATA_UINT32, 0 },
59 	{ ETHER_STAT_MACRCV_ERRORS, "macrcv_errors", KSTAT_DATA_UINT32, 0 },
60 	{ ETHER_STAT_TOOSHORT_ERRORS, "runt_errors", KSTAT_DATA_UINT32,	0 },
61 	{ ETHER_STAT_JABBER_ERRORS, "jabber_errors", KSTAT_DATA_UINT32,	0 },
62 
63 	/* Statistics described in the ieee802.3(5) man page */
64 	{ ETHER_STAT_XCVR_ADDR, "xcvr_addr", KSTAT_DATA_UINT32,		0 },
65 	{ ETHER_STAT_XCVR_ID, "xcvr_id", KSTAT_DATA_UINT32, 		0 },
66 	{ ETHER_STAT_XCVR_INUSE, "xcvr_inuse", KSTAT_DATA_UINT32,	0 },
67 	{ ETHER_STAT_CAP_1000FDX, "cap_1000fdx", KSTAT_DATA_UINT32,	0 },
68 	{ ETHER_STAT_CAP_1000HDX, "cap_1000hdx", KSTAT_DATA_UINT32,	0 },
69 	{ ETHER_STAT_CAP_100T4, "cap_100T4", KSTAT_DATA_UINT32,		0 },
70 	{ ETHER_STAT_CAP_100FDX, "cap_100fdx", KSTAT_DATA_UINT32,	0 },
71 	{ ETHER_STAT_CAP_100HDX, "cap_100hdx", KSTAT_DATA_UINT32,	0 },
72 	{ ETHER_STAT_CAP_10FDX, "cap_10fdx", KSTAT_DATA_UINT32,		0 },
73 	{ ETHER_STAT_CAP_10HDX, "cap_10hdx", KSTAT_DATA_UINT32,		0 },
74 	{ ETHER_STAT_CAP_ASMPAUSE, "cap_asmpause", KSTAT_DATA_UINT32,	0 },
75 	{ ETHER_STAT_CAP_PAUSE, "cap_pause", KSTAT_DATA_UINT32,		0 },
76 	{ ETHER_STAT_CAP_AUTONEG, "cap_autoneg", KSTAT_DATA_UINT32,	0 },
77 	{ ETHER_STAT_CAP_REMFAULT, "cap_rem_fault", KSTAT_DATA_UINT32,	0 },
78 	{ ETHER_STAT_ADV_CAP_1000FDX, "adv_cap_1000fdx", KSTAT_DATA_UINT32, 0 },
79 	{ ETHER_STAT_ADV_CAP_1000HDX, "adv_cap_1000hdx", KSTAT_DATA_UINT32, 0 },
80 	{ ETHER_STAT_ADV_CAP_100T4, "adv_cap_100T4", KSTAT_DATA_UINT32,	0 },
81 	{ ETHER_STAT_ADV_CAP_100FDX, "adv_cap_100fdx", KSTAT_DATA_UINT32, 0},
82 	{ ETHER_STAT_ADV_CAP_100HDX, "adv_cap_100hdx", KSTAT_DATA_UINT32, 0},
83 	{ ETHER_STAT_ADV_CAP_10FDX, "adv_cap_10fdx", KSTAT_DATA_UINT32,	0 },
84 	{ ETHER_STAT_ADV_CAP_10HDX, "adv_cap_10hdx", KSTAT_DATA_UINT32,	0 },
85 	{ ETHER_STAT_ADV_CAP_ASMPAUSE, "adv_cap_asmpause", KSTAT_DATA_UINT32,
86 	    0 },
87 	{ ETHER_STAT_ADV_CAP_PAUSE, "adv_cap_pause", KSTAT_DATA_UINT32,	0 },
88 	{ ETHER_STAT_ADV_CAP_AUTONEG, "adv_cap_autoneg", KSTAT_DATA_UINT32, 0 },
89 	{ ETHER_STAT_ADV_REMFAULT, "adv_rem_fault", KSTAT_DATA_UINT32,	0 },
90 	{ ETHER_STAT_LP_CAP_1000FDX, "lp_cap_1000fdx", KSTAT_DATA_UINT32, 0 },
91 	{ ETHER_STAT_LP_CAP_1000HDX, "lp_cap_1000hdx", KSTAT_DATA_UINT32, 0 },
92 	{ ETHER_STAT_LP_CAP_100T4, "lp_cap_100T4", KSTAT_DATA_UINT32,	0 },
93 	{ ETHER_STAT_LP_CAP_100FDX, "lp_cap_100fdx", KSTAT_DATA_UINT32,	0 },
94 	{ ETHER_STAT_LP_CAP_100HDX, "lp_cap_100hdx", KSTAT_DATA_UINT32,	0 },
95 	{ ETHER_STAT_LP_CAP_10FDX, "lp_cap_10fdx", KSTAT_DATA_UINT32,	0 },
96 	{ ETHER_STAT_LP_CAP_10HDX, "lp_cap_10hdx", KSTAT_DATA_UINT32,	0 },
97 	{ ETHER_STAT_LP_CAP_ASMPAUSE, "lp_cap_asmpause", KSTAT_DATA_UINT32, 0 },
98 	{ ETHER_STAT_LP_CAP_PAUSE, "lp_cap_pause", KSTAT_DATA_UINT32,	0 },
99 	{ ETHER_STAT_LP_CAP_AUTONEG, "lp_cap_autoneg", KSTAT_DATA_UINT32, 0 },
100 	{ ETHER_STAT_LP_REMFAULT, "lp_rem_fault", KSTAT_DATA_UINT32,	0 },
101 	{ ETHER_STAT_LINK_ASMPAUSE, "link_asmpause", KSTAT_DATA_UINT32,	0 },
102 	{ ETHER_STAT_LINK_PAUSE, "link_pause", KSTAT_DATA_UINT32,	0 },
103 	{ ETHER_STAT_LINK_AUTONEG, "link_autoneg", KSTAT_DATA_UINT32,	0 },
104 	{ ETHER_STAT_LINK_DUPLEX, "link_duplex", KSTAT_DATA_UINT32,	0 }
105 };
106 
107 static struct modlmisc mac_ether_modlmisc = {
108 	&mod_miscops,
109 	"Ethernet MAC plugin"
110 };
111 
112 static struct modlinkage mac_ether_modlinkage = {
113 	MODREV_1,
114 	&mac_ether_modlmisc,
115 	NULL
116 };
117 
118 static mactype_ops_t mac_ether_type_ops;
119 
120 static mac_ndd_mapping_t  mac_ether_mapping[] = {
121 	{"adv_autoneg_cap",	MAC_PROP_AUTONEG, 0, 1, sizeof (uint8_t),
122 	    MAC_PROP_PERM_RW},
123 
124 	{"adv_1000fdx_cap",	MAC_PROP_EN_1000FDX_CAP, 0, 1,
125 	    sizeof (uint8_t), MAC_PROP_PERM_RW},
126 
127 	{"adv_1000hdx_cap",	MAC_PROP_EN_1000HDX_CAP, 0, 1,
128 	    sizeof (uint8_t), MAC_PROP_PERM_RW},
129 
130 	{"adv_100fdx_cap",	MAC_PROP_EN_100FDX_CAP, 0, 1,
131 	    sizeof (uint8_t), MAC_PROP_PERM_RW},
132 
133 	{"adv_100hdx_cap",	MAC_PROP_EN_100HDX_CAP, 0, 1,
134 	    sizeof (uint8_t), MAC_PROP_PERM_RW},
135 
136 	{"adv_10fdx_cap",	MAC_PROP_EN_10FDX_CAP, 0, 1,
137 	    sizeof (uint8_t), MAC_PROP_PERM_RW},
138 
139 	{"adv_10hdx_cap",	MAC_PROP_EN_10HDX_CAP, 0, 1,
140 	    sizeof (uint8_t), MAC_PROP_PERM_RW},
141 
142 	{"adv_100T4_cap",	MAC_PROP_EN_100T4_CAP, 0, 1,
143 	    sizeof (uint8_t), MAC_PROP_PERM_READ},
144 
145 	{"link_status",		MAC_STAT_LINK_UP, 0, 1,
146 	    sizeof (long), MAC_PROP_FLAGS_RK},
147 
148 	{"link_speed",		MAC_PROP_SPEED, 0, LONG_MAX,
149 	    sizeof (uint64_t), MAC_PROP_PERM_READ},
150 
151 	{"link_duplex",		MAC_PROP_DUPLEX, 0, 2,
152 	    sizeof (link_duplex_t), MAC_PROP_PERM_READ},
153 
154 	{"autoneg_cap",		ETHER_STAT_CAP_AUTONEG, 0, 1,
155 	    sizeof (long), MAC_PROP_FLAGS_RK},
156 
157 	{"pause_cap",		ETHER_STAT_CAP_PAUSE, 0, 1,
158 	    sizeof (long), MAC_PROP_FLAGS_RK},
159 
160 	{"asym_pause_cap",	ETHER_STAT_CAP_ASMPAUSE, 0, 1,
161 	    sizeof (long), MAC_PROP_FLAGS_RK},
162 
163 	{"1000fdx_cap",		ETHER_STAT_CAP_1000FDX, 0, 1,
164 	    sizeof (long), MAC_PROP_FLAGS_RK},
165 
166 	{"1000hdx_cap",		ETHER_STAT_CAP_1000HDX, 0, 1,
167 	    sizeof (long), MAC_PROP_FLAGS_RK},
168 
169 	{"100T4_cap",		ETHER_STAT_CAP_100T4, 0, 1,
170 	    sizeof (long), MAC_PROP_FLAGS_RK},
171 
172 	{"100fdx_cap",		ETHER_STAT_CAP_100FDX, 0, 1,
173 	    sizeof (long), MAC_PROP_FLAGS_RK},
174 
175 	{"100hdx_cap",		ETHER_STAT_CAP_100HDX, 0, 1,
176 	    sizeof (long), MAC_PROP_FLAGS_RK},
177 
178 	{"10fdx_cap",		ETHER_STAT_CAP_10FDX, 0, 1,
179 	    sizeof (long), MAC_PROP_FLAGS_RK},
180 
181 	{"10hdx_cap",		ETHER_STAT_CAP_10HDX, 0, 1,
182 	    sizeof (long), MAC_PROP_FLAGS_RK},
183 
184 	{"lp_autoneg_cap",	ETHER_STAT_LP_CAP_AUTONEG, 0, 1,
185 	    sizeof (long), MAC_PROP_FLAGS_RK},
186 
187 	{"lp_pause_cap",	ETHER_STAT_LP_CAP_PAUSE, 0, 1,
188 	    sizeof (long), MAC_PROP_FLAGS_RK},
189 
190 	{"lp_asym_pause_cap",	ETHER_STAT_LP_CAP_ASMPAUSE, 0, 1,
191 	    sizeof (long), MAC_PROP_FLAGS_RK},
192 
193 	{"lp_1000hdx_cap",	ETHER_STAT_LP_CAP_1000HDX, 0, 1,
194 	    sizeof (long), MAC_PROP_FLAGS_RK},
195 
196 	{"lp_1000fdx_cap",	ETHER_STAT_LP_CAP_1000FDX, 0, 1,
197 	    sizeof (long), MAC_PROP_FLAGS_RK},
198 
199 	{"lp_100T4_cap",	ETHER_STAT_LP_CAP_100T4, 0, 1,
200 	    sizeof (long), MAC_PROP_FLAGS_RK},
201 
202 	{"lp_100fdx_cap",	ETHER_STAT_LP_CAP_100FDX, 0, 1,
203 	    sizeof (long), MAC_PROP_FLAGS_RK},
204 
205 	{"lp_100hdx_cap",	ETHER_STAT_LP_CAP_100HDX, 0, 1,
206 	    sizeof (long), MAC_PROP_FLAGS_RK},
207 
208 	{"lp_10fdx_cap",	ETHER_STAT_LP_CAP_10FDX, 0, 1,
209 	    sizeof (long), MAC_PROP_FLAGS_RK},
210 
211 	{"lp_10hdx_cap",	ETHER_STAT_LP_CAP_10HDX, 0, 1,
212 	    sizeof (long), MAC_PROP_FLAGS_RK},
213 
214 	{"link_autoneg",	ETHER_STAT_LINK_AUTONEG, 0, 1,
215 	    sizeof (long), MAC_PROP_FLAGS_RK}
216 
217 };
218 
219 
220 int
221 _init(void)
222 {
223 	mactype_register_t *mtrp;
224 	int	err;
225 
226 	if ((mtrp = mactype_alloc(MACTYPE_VERSION)) == NULL)
227 		return (ENOTSUP);
228 	mtrp->mtr_ident = MAC_PLUGIN_IDENT_ETHER;
229 	mtrp->mtr_ops = &mac_ether_type_ops;
230 	mtrp->mtr_mactype = DL_ETHER;
231 	mtrp->mtr_nativetype = DL_ETHER;
232 	mtrp->mtr_addrlen = ETHERADDRL;
233 	mtrp->mtr_brdcst_addr = ether_brdcst;
234 	mtrp->mtr_stats = ether_stats;
235 	mtrp->mtr_statcount = A_CNT(ether_stats);
236 	mtrp->mtr_mapping = mac_ether_mapping;
237 	mtrp->mtr_mappingcount = A_CNT(mac_ether_mapping);
238 	if ((err = mactype_register(mtrp)) == 0) {
239 		if ((err = mod_install(&mac_ether_modlinkage)) != 0)
240 			(void) mactype_unregister(MAC_PLUGIN_IDENT_ETHER);
241 	}
242 	mactype_free(mtrp);
243 	return (err);
244 }
245 
246 int
247 _fini(void)
248 {
249 	int	err;
250 
251 	if ((err = mactype_unregister(MAC_PLUGIN_IDENT_ETHER)) != 0)
252 		return (err);
253 	return (mod_remove(&mac_ether_modlinkage));
254 }
255 
256 int
257 _info(struct modinfo *modinfop)
258 {
259 	return (mod_info(&mac_ether_modlinkage, modinfop));
260 }
261 
262 /*
263  * MAC Type plugin operations
264  */
265 
266 /* ARGSUSED */
267 int
268 mac_ether_unicst_verify(const void *addr, void *mac_pdata)
269 {
270 	/* If it's not a group address, then it's a valid unicast address. */
271 	return (((((uint8_t *)addr)[0] & 0x01) != 0) ? EINVAL : 0);
272 }
273 
274 /* ARGSUSED */
275 int
276 mac_ether_multicst_verify(const void *addr, void *mac_pdata)
277 {
278 	/* The address must be a group address. */
279 	if ((((uint8_t *)addr)[0] & 0x01) == 0)
280 		return (EINVAL);
281 	/* The address must not be the media broadcast address. */
282 	if (bcmp(addr, ether_brdcst, ETHERADDRL) == 0)
283 		return (EINVAL);
284 	return (0);
285 }
286 
287 /*
288  * Check the legality of an Ethernet SAP value. The following values are
289  * allowed, as specified by PSARC 2003/150:
290  *
291  * 0..ETHERMTU (1500)					802 semantics
292  * ETHERTYPE_802_MIN (1536)..ETHERTYPE_MAX (65535)	ethertype semantics
293  *
294  * Note that SAP values less than or equal to ETHERMTU (1500) represent LLC
295  * channels. (See PSARC 2003/150).  We strictly use SAP 0 to represent LLC
296  * channels.
297  */
298 /* ARGSUSED */
299 boolean_t
300 mac_ether_sap_verify(uint32_t sap, uint32_t *bind_sap, void *mac_pdata)
301 {
302 	if (sap >= ETHERTYPE_802_MIN && sap <= ETHERTYPE_MAX) {
303 		if (bind_sap != NULL)
304 			*bind_sap = sap;
305 		return (B_TRUE);
306 	}
307 
308 	if (sap <= ETHERMTU) {
309 		if (bind_sap != NULL)
310 			*bind_sap = DLS_SAP_LLC;
311 		return (B_TRUE);
312 	}
313 	return (B_FALSE);
314 }
315 
316 /* ARGSUSED */
317 mblk_t *
318 mac_ether_header(const void *saddr, const void *daddr, uint32_t sap,
319     void *mac_pdata, mblk_t *payload, size_t extra_len)
320 {
321 	struct ether_header	*ehp;
322 	mblk_t			*mp;
323 	uint32_t		bind_sap;
324 
325 	if (!mac_ether_sap_verify(sap, &bind_sap, NULL))
326 		return (NULL);
327 
328 	mp = allocb(sizeof (struct ether_header) + extra_len, BPRI_HI);
329 	if (mp == NULL)
330 		return (NULL);
331 
332 	ehp = (void *)mp->b_rptr;
333 	bcopy(daddr, &(ehp->ether_dhost), ETHERADDRL);
334 	bcopy(saddr, &(ehp->ether_shost), ETHERADDRL);
335 
336 	/*
337 	 * sap <= ETHERMTU indicates that LLC is being used.  If that's the
338 	 * case, then the ether_type needs to be set to the payload length.
339 	 */
340 	if ((bind_sap == DLS_SAP_LLC) && (payload != NULL))
341 		sap = msgdsize(payload);
342 	ehp->ether_type = htons(sap);
343 
344 	mp->b_wptr += sizeof (struct ether_header);
345 	return (mp);
346 }
347 
348 /* ARGSUSED */
349 int
350 mac_ether_header_info(mblk_t *mp, void *mac_pdata, mac_header_info_t *hdr_info)
351 {
352 	struct ether_header	*ehp;
353 	uint16_t		ether_type;
354 
355 	if (MBLKL(mp) < sizeof (struct ether_header))
356 		return (EINVAL);
357 
358 	ehp = (void *)mp->b_rptr;
359 	ether_type = ntohs(ehp->ether_type);
360 
361 	hdr_info->mhi_hdrsize = sizeof (struct ether_header);
362 	hdr_info->mhi_daddr = (const uint8_t *)&(ehp->ether_dhost);
363 	hdr_info->mhi_saddr = (const uint8_t *)&(ehp->ether_shost);
364 	hdr_info->mhi_origsap = ether_type;
365 	hdr_info->mhi_bindsap = (ether_type > ETHERMTU) ?
366 	    ether_type : DLS_SAP_LLC;
367 	hdr_info->mhi_pktsize = (hdr_info->mhi_bindsap == DLS_SAP_LLC) ?
368 	    hdr_info->mhi_hdrsize + ether_type : 0;
369 
370 	if (mac_ether_unicst_verify(hdr_info->mhi_daddr, NULL) == 0)
371 		hdr_info->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
372 	else if (mac_ether_multicst_verify(hdr_info->mhi_daddr, NULL) == 0)
373 		hdr_info->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
374 	else
375 		hdr_info->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
376 
377 	return (0);
378 }
379 
380 /*ARGSUSED3*/
381 void
382 mac_ether_link_details(char *buf, size_t sz, mac_handle_t mh, void *mac_pdata)
383 {
384 	link_duplex_t	duplex;
385 	uint64_t	speed;
386 
387 	duplex = mac_stat_get(mh, ETHER_STAT_LINK_DUPLEX);
388 	speed = mac_stat_get(mh, MAC_STAT_IFSPEED);
389 
390 	/* convert to Mbps */
391 	speed /= 1000000;
392 
393 	buf[0] = 0;
394 	(void) snprintf(buf, sz, "%u Mbps, %s duplex", (uint32_t)speed,
395 	    duplex == LINK_DUPLEX_FULL ? "full" :
396 	    duplex == LINK_DUPLEX_HALF ? "half" : "unknown");
397 }
398 
399 static mactype_ops_t mac_ether_type_ops = {
400 	MTOPS_LINK_DETAILS,
401 	mac_ether_unicst_verify,
402 	mac_ether_multicst_verify,
403 	mac_ether_sap_verify,
404 	mac_ether_header,
405 	mac_ether_header_info,
406 	NULL, 	/* pdata_verify */
407 	NULL,	/* header_cook */
408 	NULL,	/* header_uncook */
409 	mac_ether_link_details
410 };
411