xref: /illumos-gate/usr/src/uts/common/io/aggr/aggr_port.c (revision f48205be)
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 2006 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  * IEEE 802.3ad Link Aggregation - Link Aggregation MAC ports.
30  *
31  * Implements the functions needed to manage the MAC ports that are
32  * part of Link Aggregation groups.
33  */
34 
35 #include <sys/types.h>
36 #include <sys/sysmacros.h>
37 #include <sys/conf.h>
38 #include <sys/cmn_err.h>
39 #include <sys/list.h>
40 #include <sys/ksynch.h>
41 #include <sys/kmem.h>
42 #include <sys/stream.h>
43 #include <sys/modctl.h>
44 #include <sys/ddi.h>
45 #include <sys/sunddi.h>
46 #include <sys/atomic.h>
47 #include <sys/stat.h>
48 #include <sys/sdt.h>
49 #include <sys/dlpi.h>
50 
51 #include <sys/aggr.h>
52 #include <sys/aggr_impl.h>
53 
54 static kmem_cache_t *aggr_port_cache;
55 static void aggr_port_notify_cb(void *, mac_notify_type_t);
56 
57 /*ARGSUSED*/
58 static int
59 aggr_port_constructor(void *buf, void *arg, int kmflag)
60 {
61 	aggr_port_t *port = buf;
62 
63 	bzero(buf, sizeof (aggr_port_t));
64 	rw_init(&port->lp_lock, NULL, RW_DRIVER, NULL);
65 
66 	return (0);
67 }
68 
69 /*ARGSUSED*/
70 static void
71 aggr_port_destructor(void *buf, void *arg)
72 {
73 	aggr_port_t *port = buf;
74 
75 	rw_destroy(&port->lp_lock);
76 }
77 
78 void
79 aggr_port_init(void)
80 {
81 	aggr_port_cache = kmem_cache_create("aggr_port_cache",
82 	    sizeof (aggr_port_t), 0, aggr_port_constructor,
83 	    aggr_port_destructor, NULL, NULL, NULL, 0);
84 }
85 
86 void
87 aggr_port_fini(void)
88 {
89 	/*
90 	 * This function is called only after all groups have been
91 	 * freed. This ensures that there are no remaining allocated
92 	 * ports when this function is invoked.
93 	 */
94 	kmem_cache_destroy(aggr_port_cache);
95 }
96 
97 mac_resource_handle_t
98 aggr_port_resource_add(void *arg, mac_resource_t *mrp)
99 {
100 	aggr_port_t *port = (aggr_port_t *)arg;
101 	aggr_grp_t *grp = port->lp_grp;
102 
103 	return (mac_resource_add(grp->lg_mh, mrp));
104 }
105 
106 void
107 aggr_port_init_callbacks(aggr_port_t *port)
108 {
109 	/* add the port's receive callback */
110 	port->lp_mnh = mac_notify_add(port->lp_mh, aggr_port_notify_cb,
111 	    (void *)port);
112 
113 	/* set port's resource_add callback */
114 	mac_resource_set(port->lp_mh, aggr_port_resource_add, (void *)port);
115 }
116 
117 int
118 aggr_port_create(const char *name, aggr_port_t **pp)
119 {
120 	int err;
121 	mac_handle_t mh;
122 	aggr_port_t *port;
123 	uint_t i;
124 	const mac_info_t *mip;
125 	char driver[MAXNAMELEN];
126 	uint_t ddi_instance;
127 
128 	*pp = NULL;
129 
130 	if (ddi_parse(name, driver, &ddi_instance) != DDI_SUCCESS)
131 		return (EINVAL);
132 
133 	if ((err = mac_open(name, ddi_instance, &mh)) != 0)
134 		return (err);
135 
136 	mip = mac_info(mh);
137 	if (mip->mi_media != DL_ETHER || mip->mi_nativemedia != DL_ETHER) {
138 		mac_close(mh);
139 		return (EINVAL);
140 	}
141 
142 	if (!mac_active_set(mh)) {
143 		mac_close(mh);
144 		return (EBUSY);
145 	}
146 
147 	port = kmem_cache_alloc(aggr_port_cache, KM_SLEEP);
148 
149 	port->lp_refs = 1;
150 	port->lp_next = NULL;
151 	port->lp_mh = mh;
152 	port->lp_mip = mip;
153 	(void) strlcpy(port->lp_devname, name, sizeof (port->lp_devname));
154 	port->lp_closing = 0;
155 
156 	/* get the port's original MAC address */
157 	mac_unicst_get(port->lp_mh, port->lp_addr);
158 
159 	/* set port's transmit information */
160 	port->lp_txinfo = mac_tx_get(port->lp_mh);
161 
162 	/* initialize state */
163 	port->lp_state = AGGR_PORT_STATE_STANDBY;
164 	port->lp_link_state = LINK_STATE_UNKNOWN;
165 	port->lp_ifspeed = 0;
166 	port->lp_link_duplex = LINK_DUPLEX_UNKNOWN;
167 	port->lp_started = B_FALSE;
168 	port->lp_tx_enabled = B_FALSE;
169 	port->lp_promisc_on = B_FALSE;
170 
171 	/*
172 	 * Save the current statistics of the port. They will be used
173 	 * later by aggr_m_stats() when aggregating the stastics of
174 	 * the consistituent ports.
175 	 */
176 	for (i = 0; i < MAC_NSTAT; i++) {
177 		port->lp_stat[i] =
178 		    aggr_port_stat(port, i + MAC_STAT_MIN);
179 	}
180 	for (i = 0; i < ETHER_NSTAT; i++) {
181 		port->lp_ether_stat[i] =
182 		    aggr_port_stat(port, i + MACTYPE_STAT_MIN);
183 	}
184 
185 	/* LACP related state */
186 	port->lp_collector_enabled = B_FALSE;
187 
188 	*pp = port;
189 	return (0);
190 }
191 
192 void
193 aggr_port_delete(aggr_port_t *port)
194 {
195 	mac_resource_set(port->lp_mh, NULL, NULL);
196 	mac_notify_remove(port->lp_mh, port->lp_mnh);
197 	mac_active_clear(port->lp_mh);
198 
199 	/*
200 	 * Restore the port MAC address. Note it is called after the
201 	 * port's notification callback being removed. This prevent
202 	 * port's MAC_NOTE_UNICST notify callback function being called.
203 	 */
204 	(void) mac_unicst_set(port->lp_mh, port->lp_addr);
205 
206 	mac_close(port->lp_mh);
207 	AGGR_PORT_REFRELE(port);
208 }
209 
210 void
211 aggr_port_free(aggr_port_t *port)
212 {
213 	ASSERT(port->lp_refs == 0);
214 	if (port->lp_grp != NULL)
215 		AGGR_GRP_REFRELE(port->lp_grp);
216 	port->lp_grp = NULL;
217 	kmem_cache_free(aggr_port_cache, port);
218 }
219 
220 /*
221  * Invoked upon receiving a MAC_NOTE_LINK notification for
222  * one of the consistuent ports.
223  */
224 boolean_t
225 aggr_port_notify_link(aggr_grp_t *grp, aggr_port_t *port, boolean_t dolock)
226 {
227 	boolean_t do_attach = B_FALSE;
228 	boolean_t do_detach = B_FALSE;
229 	boolean_t link_state_changed = B_TRUE;
230 	uint64_t ifspeed;
231 	link_state_t link_state;
232 	link_duplex_t link_duplex;
233 
234 	if (dolock) {
235 		AGGR_LACP_LOCK(grp);
236 		rw_enter(&grp->lg_lock, RW_WRITER);
237 	} else {
238 		ASSERT(AGGR_LACP_LOCK_HELD(grp));
239 		ASSERT(RW_WRITE_HELD(&grp->lg_lock));
240 	}
241 
242 	rw_enter(&port->lp_lock, RW_WRITER);
243 
244 	/* link state change? */
245 	link_state = mac_link_get(port->lp_mh);
246 	if (port->lp_link_state != link_state) {
247 		if (link_state == LINK_STATE_UP)
248 			do_attach = (port->lp_link_state != LINK_STATE_UP);
249 		else
250 			do_detach = (port->lp_link_state == LINK_STATE_UP);
251 	}
252 	port->lp_link_state = link_state;
253 
254 	/* link duplex change? */
255 	link_duplex = aggr_port_stat(port, ETHER_STAT_LINK_DUPLEX);
256 	if (port->lp_link_duplex != link_duplex) {
257 		if (link_duplex == LINK_DUPLEX_FULL)
258 			do_attach |= (port->lp_link_duplex != LINK_DUPLEX_FULL);
259 		else
260 			do_detach |= (port->lp_link_duplex == LINK_DUPLEX_FULL);
261 	}
262 	port->lp_link_duplex = link_duplex;
263 
264 	/* link speed changes? */
265 	ifspeed = aggr_port_stat(port, MAC_STAT_IFSPEED);
266 	if (port->lp_ifspeed != ifspeed) {
267 		if (port->lp_state == AGGR_PORT_STATE_ATTACHED)
268 			do_detach |= (ifspeed != grp->lg_ifspeed);
269 		else
270 			do_attach |= (ifspeed == grp->lg_ifspeed);
271 	}
272 	port->lp_ifspeed = ifspeed;
273 
274 	if (do_attach) {
275 		/* attempt to attach the port to the aggregation */
276 		link_state_changed = aggr_grp_attach_port(grp, port);
277 	} else if (do_detach) {
278 		/* detach the port from the aggregation */
279 		link_state_changed = aggr_grp_detach_port(grp, port);
280 	}
281 
282 	rw_exit(&port->lp_lock);
283 
284 	if (dolock) {
285 		rw_exit(&grp->lg_lock);
286 		AGGR_LACP_UNLOCK(grp);
287 	}
288 
289 	return (link_state_changed);
290 }
291 
292 /*
293  * Invoked upon receiving a MAC_NOTE_UNICST for one of the constituent
294  * ports of a group.
295  */
296 static void
297 aggr_port_notify_unicst(aggr_grp_t *grp, aggr_port_t *port,
298     boolean_t *mac_addr_changedp, boolean_t *link_state_changedp)
299 {
300 	boolean_t mac_addr_changed = B_FALSE;
301 	boolean_t link_state_changed = B_FALSE;
302 	uint8_t mac_addr[ETHERADDRL];
303 
304 	ASSERT(mac_addr_changedp != NULL);
305 	ASSERT(link_state_changedp != NULL);
306 
307 	AGGR_LACP_LOCK(grp);
308 	rw_enter(&grp->lg_lock, RW_WRITER);
309 
310 	rw_enter(&port->lp_lock, RW_WRITER);
311 
312 	/*
313 	 * If it is called when setting the MAC address to the
314 	 * aggregation group MAC address, do nothing.
315 	 */
316 	mac_unicst_get(port->lp_mh, mac_addr);
317 	if (bcmp(mac_addr, grp->lg_addr, ETHERADDRL) == 0) {
318 		rw_exit(&port->lp_lock);
319 		goto done;
320 	}
321 
322 	/* save the new port MAC address */
323 	bcopy(mac_addr, port->lp_addr, ETHERADDRL);
324 
325 	aggr_grp_port_mac_changed(grp, port, &mac_addr_changed,
326 	    &link_state_changed);
327 
328 	rw_exit(&port->lp_lock);
329 
330 	if (grp->lg_closing)
331 		goto done;
332 
333 	/*
334 	 * If this port was used to determine the MAC address of
335 	 * the group, update the MAC address of the constituent
336 	 * ports.
337 	 */
338 	if (mac_addr_changed) {
339 		link_state_changed = link_state_changed ||
340 		    aggr_grp_update_ports_mac(grp);
341 	}
342 
343 done:
344 	*mac_addr_changedp = mac_addr_changed;
345 	*link_state_changedp = link_state_changed;
346 	rw_exit(&grp->lg_lock);
347 	AGGR_LACP_UNLOCK(grp);
348 }
349 
350 /*
351  * Notification callback invoked by the MAC service module for
352  * a particular MAC port.
353  */
354 static void
355 aggr_port_notify_cb(void *arg, mac_notify_type_t type)
356 {
357 	aggr_port_t *port = arg;
358 	aggr_grp_t *grp = port->lp_grp;
359 	boolean_t mac_addr_changed, link_state_changed;
360 
361 	/*
362 	 * Do nothing if the aggregation or the port is in the deletion
363 	 * process. Note that this is necessary to avoid deadlock.
364 	 */
365 	if ((grp->lg_closing) || (port->lp_closing))
366 		return;
367 
368 	AGGR_PORT_REFHOLD(port);
369 
370 	switch (type) {
371 	case MAC_NOTE_TX:
372 		mac_tx_update(grp->lg_mh);
373 		break;
374 	case MAC_NOTE_LINK:
375 		if (aggr_port_notify_link(grp, port, B_TRUE))
376 			mac_link_update(grp->lg_mh, grp->lg_link_state);
377 		break;
378 	case MAC_NOTE_UNICST:
379 		aggr_port_notify_unicst(grp, port, &mac_addr_changed,
380 		    &link_state_changed);
381 		if (mac_addr_changed)
382 			mac_unicst_update(grp->lg_mh, grp->lg_addr);
383 		if (link_state_changed)
384 			mac_link_update(grp->lg_mh, grp->lg_link_state);
385 		break;
386 	case MAC_NOTE_PROMISC:
387 		port->lp_txinfo = mac_tx_get(port->lp_mh);
388 		break;
389 	default:
390 		break;
391 	}
392 
393 	AGGR_PORT_REFRELE(port);
394 }
395 
396 int
397 aggr_port_start(aggr_port_t *port)
398 {
399 	int rc;
400 
401 	ASSERT(RW_WRITE_HELD(&port->lp_lock));
402 
403 	if (port->lp_started)
404 		return (0);
405 
406 	if ((rc = mac_start(port->lp_mh)) != 0)
407 		return (rc);
408 
409 	/* update the port state */
410 	port->lp_started = B_TRUE;
411 
412 	return (rc);
413 }
414 
415 void
416 aggr_port_stop(aggr_port_t *port)
417 {
418 	ASSERT(RW_WRITE_HELD(&port->lp_lock));
419 
420 	if (!port->lp_started)
421 		return;
422 
423 	aggr_grp_multicst_port(port, B_FALSE);
424 
425 	mac_stop(port->lp_mh);
426 
427 	/* update the port state */
428 	port->lp_started = B_FALSE;
429 }
430 
431 int
432 aggr_port_promisc(aggr_port_t *port, boolean_t on)
433 {
434 	int rc;
435 
436 	ASSERT(RW_WRITE_HELD(&port->lp_lock));
437 
438 	if (on == port->lp_promisc_on)
439 		/* already in desired promiscous mode */
440 		return (0);
441 
442 	rc = mac_promisc_set(port->lp_mh, on, MAC_DEVPROMISC);
443 
444 	if (rc == 0)
445 		port->lp_promisc_on = on;
446 
447 	return (rc);
448 }
449 
450 /*
451  * Set the MAC address of a port.
452  */
453 int
454 aggr_port_unicst(aggr_port_t *port, uint8_t *macaddr)
455 {
456 	int rc;
457 
458 	ASSERT(RW_WRITE_HELD(&port->lp_lock));
459 
460 	rc = mac_unicst_set(port->lp_mh, macaddr);
461 
462 	return (rc);
463 }
464 
465 /*
466  * Add or remove a multicast address to/from a port.
467  */
468 int
469 aggr_port_multicst(void *arg, boolean_t add, const uint8_t *addrp)
470 {
471 	aggr_port_t *port = arg;
472 
473 	return (add ? mac_multicst_add(port->lp_mh, addrp) :
474 	    mac_multicst_remove(port->lp_mh, addrp));
475 }
476 
477 uint64_t
478 aggr_port_stat(aggr_port_t *port, uint_t stat)
479 {
480 	return (mac_stat_get(port->lp_mh, stat));
481 }
482