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