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