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 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 /*
27 * Copyright 2019, Joyent, Inc.
28 * Copyright 2022 Garrett D'Amore
29 * Copyright 2025 Oxide Computer Company
30 */
31
32 /*
33 * Softmac data-path switching:
34 *
35 * - Fast-path model
36 *
37 * When the softmac fast-path is used, a dedicated lower-stream
38 * will be opened over the legacy device for each IP/ARP (upper-)stream
39 * over the softMAC, and all DLPI messages (including control messages
40 * and data messages) will be exchanged between the upper-stream and
41 * the corresponding lower-stream directly. Therefore, the data
42 * demultiplexing, filtering and classification processing will be done
43 * by the lower-stream, and the GLDv3 DLS/MAC layer processing will be
44 * no longer needed.
45 *
46 * - Slow-path model
47 *
48 * Some GLDv3 features requires the GLDv3 DLS/MAC layer processing to
49 * not be bypassed to assure its function correctness. For example,
50 * softmac fast-path must be disabled to support GLDv3 VNIC functionality.
51 * In this case, a shared lower-stream will be opened over the legacy
52 * device, which is responsible for implementing the GLDv3 callbacks
53 * and passing RAW data messages between the legacy devices and the GLDv3
54 * framework.
55 *
56 * By default, the softmac fast-path mode will be used to assure the
57 * performance; MAC clients will be able to request to disable the softmac
58 * fast-path mode to support certain features, and if that succeeds,
59 * the system will fallback to the slow-path softmac data-path model.
60 *
61 *
62 * The details of the softmac data fast-path model is stated as below
63 *
64 * 1. When a stream is opened on a softMAC, the softmac module will takes
65 * over the DLPI processing on this stream;
66 *
67 * 2. For IP/ARP streams over a softMAC, softmac data fast-path will be
68 * used by default, unless fast-path is disabled by any MAC client
69 * explicitly. The softmac module first identifies an IP/ARP stream
70 * by seeing whether there is a SIOCSLIFNAME ioctl sent from upstream,
71 * if there is one, this stream is either an IP or an ARP stream
72 * and will use fast-path potentially;
73 *
74 * 3. When the softmac fast-path is used, an dedicated lower-stream will
75 * be setup for each IP/ARP stream (1-1 mapping). From that point on,
76 * all control and data messages will be exchanged between the IP/ARP
77 * upper-stream and the legacy device through this dedicated
78 * lower-stream. As a result, the DLS/MAC layer processing in GLDv3
79 * will be skipped, and this greatly improves the performance;
80 *
81 * 4. When the softmac data fast-path is disabled by a MAC client (e.g.,
82 * by a VNIC), all the IP/ARP upper streams will try to switch from
83 * the fast-path to the slow-path. The dedicated lower-stream will be
84 * destroyed, and all the control and data-messages will go through the
85 * existing GLDv3 code path and (in the end) the shared lower-stream;
86 *
87 * 5. On the other hand, when the last MAC client cancels its fast-path
88 * disable request, all the IP/ARP streams will try to switch back to
89 * the fast-path mode;
90 *
91 * Step 5 and 6 both rely on the data-path mode switching process
92 * described below:
93 *
94 * 1) To switch the softmac data-path mode (between fast-path and slow-path),
95 * softmac will first send a DL_NOTE_REPLUMB DL_NOTIFY_IND message
96 * upstream over each IP/ARP streams that needs data-path mode switching;
97 *
98 * 2) When IP receives this DL_NOTE_REPLUMB message, it will bring down
99 * all the IP interfaces on the corresponding ill (IP Lower level
100 * structure), and bring up those interfaces over again; this will in
101 * turn cause the ARP to "replumb" the interface.
102 *
103 * During the replumb process, both IP and ARP will send downstream the
104 * necessary DL_DISABMULTI_REQ and DL_UNBIND_REQ messages and cleanup
105 * the old state of the underlying softMAC, following with the necessary
106 * DL_BIND_REQ and DL_ENABMULTI_REQ messages to setup the new state.
107 * Between the cleanup and re-setup process, IP/ARP will also send down
108 * a DL_NOTE_REPLUMB_DONE DL_NOTIFY_CONF messages to the softMAC to
109 * indicate the *switching point*;
110 *
111 * 3) When softmac receives the DL_NOTE_REPLUMB_DONE message, it either
112 * creates or destroys the dedicated lower-stream (depending on which
113 * data-path mode the softMAC switches to), and change the softmac
114 * data-path mode. From then on, softmac will process all the succeeding
115 * control messages (including the DL_BIND_REQ and DL_ENABMULTI_REQ
116 * messages) and data messages based on new data-path mode.
117 */
118
119 #include <sys/types.h>
120 #include <sys/disp.h>
121 #include <sys/callb.h>
122 #include <sys/sysmacros.h>
123 #include <sys/file.h>
124 #include <sys/vlan.h>
125 #include <sys/dld.h>
126 #include <sys/sockio.h>
127 #include <sys/softmac_impl.h>
128 #include <net/if.h>
129
130 static kmutex_t softmac_taskq_lock;
131 static kthread_t *softmac_taskq_thread;
132 static kcondvar_t softmac_taskq_cv;
133 static list_t softmac_taskq_list; /* List of softmac_upper_t */
134 boolean_t softmac_taskq_quit;
135 boolean_t softmac_taskq_done;
136
137 static void softmac_taskq_dispatch();
138 static int softmac_fastpath_setup(softmac_upper_t *);
139 static mac_tx_cookie_t softmac_fastpath_wput_data(softmac_upper_t *, mblk_t *,
140 uintptr_t, uint16_t);
141 static void softmac_datapath_switch_done(softmac_upper_t *);
142
143 void
softmac_fp_init()144 softmac_fp_init()
145 {
146 mutex_init(&softmac_taskq_lock, NULL, MUTEX_DRIVER, NULL);
147 cv_init(&softmac_taskq_cv, NULL, CV_DRIVER, NULL);
148
149 softmac_taskq_quit = B_FALSE;
150 softmac_taskq_done = B_FALSE;
151 list_create(&softmac_taskq_list, sizeof (softmac_upper_t),
152 offsetof(softmac_upper_t, su_taskq_list_node));
153 softmac_taskq_thread = thread_create(NULL, 0, softmac_taskq_dispatch,
154 NULL, 0, &p0, TS_RUN, minclsyspri);
155 }
156
157 void
softmac_fp_fini()158 softmac_fp_fini()
159 {
160 /*
161 * Request the softmac_taskq thread to quit and wait for it to be done.
162 */
163 mutex_enter(&softmac_taskq_lock);
164 softmac_taskq_quit = B_TRUE;
165 cv_signal(&softmac_taskq_cv);
166 while (!softmac_taskq_done)
167 cv_wait(&softmac_taskq_cv, &softmac_taskq_lock);
168 mutex_exit(&softmac_taskq_lock);
169 thread_join(softmac_taskq_thread->t_did);
170 list_destroy(&softmac_taskq_list);
171
172 mutex_destroy(&softmac_taskq_lock);
173 cv_destroy(&softmac_taskq_cv);
174 }
175
176 static boolean_t
check_ip_above(queue_t * q)177 check_ip_above(queue_t *q)
178 {
179 queue_t *next_q;
180 boolean_t ret = B_TRUE;
181
182 claimstr(q);
183 next_q = q->q_next;
184 if (strcmp(next_q->q_qinfo->qi_minfo->mi_idname, "ip") != 0)
185 ret = B_FALSE;
186 releasestr(q);
187 return (ret);
188 }
189
190 /* ARGSUSED */
191 static int
softmac_capab_perim(softmac_upper_t * sup,void * data,uint_t flags)192 softmac_capab_perim(softmac_upper_t *sup, void *data, uint_t flags)
193 {
194 switch (flags) {
195 case DLD_ENABLE:
196 mutex_enter(&sup->su_mutex);
197 break;
198 case DLD_DISABLE:
199 mutex_exit(&sup->su_mutex);
200 break;
201 case DLD_QUERY:
202 return (MUTEX_HELD(&sup->su_mutex));
203 }
204 return (0);
205 }
206
207 static mac_tx_notify_handle_t
softmac_client_tx_notify(softmac_upper_t * sup,mac_tx_notify_t func,void * arg)208 softmac_client_tx_notify(softmac_upper_t *sup, mac_tx_notify_t func, void *arg)
209 {
210 ASSERT(MUTEX_HELD(&sup->su_mutex));
211
212 if (func != NULL) {
213 sup->su_tx_notify_func = func;
214 sup->su_tx_notify_arg = arg;
215 } else {
216 /*
217 * Wait for all tx_notify_func call to be done.
218 */
219 while (sup->su_tx_inprocess != 0)
220 cv_wait(&sup->su_cv, &sup->su_mutex);
221
222 sup->su_tx_notify_func = NULL;
223 sup->su_tx_notify_arg = NULL;
224 }
225 return ((mac_tx_notify_handle_t)sup);
226 }
227
228 static boolean_t
softmac_tx_is_flow_blocked(softmac_upper_t * sup,mac_tx_cookie_t cookie)229 softmac_tx_is_flow_blocked(softmac_upper_t *sup, mac_tx_cookie_t cookie)
230 {
231 ASSERT(cookie == (mac_tx_cookie_t)sup);
232 return (sup->su_tx_busy);
233 }
234
235 static int
softmac_capab_direct(softmac_upper_t * sup,void * data,uint_t flags)236 softmac_capab_direct(softmac_upper_t *sup, void *data, uint_t flags)
237 {
238 dld_capab_direct_t *direct = data;
239 softmac_lower_t *slp = sup->su_slp;
240
241 ASSERT(MUTEX_HELD(&sup->su_mutex));
242
243 ASSERT(sup->su_mode == SOFTMAC_FASTPATH);
244
245 switch (flags) {
246 case DLD_ENABLE:
247 if (sup->su_direct)
248 return (0);
249
250 sup->su_direct_rxinfo.slr_rx = (softmac_rx_t)direct->di_rx_cf;
251 sup->su_direct_rxinfo.slr_arg = direct->di_rx_ch;
252 slp->sl_rxinfo = &sup->su_direct_rxinfo;
253 direct->di_tx_df = (uintptr_t)softmac_fastpath_wput_data;
254 direct->di_tx_dh = sup;
255 direct->di_tx_fctl_df = (uintptr_t)softmac_tx_is_flow_blocked;
256 direct->di_tx_fctl_dh = sup;
257 direct->di_tx_cb_df = (uintptr_t)softmac_client_tx_notify;
258 direct->di_tx_cb_dh = sup;
259 sup->su_direct = B_TRUE;
260 return (0);
261
262 case DLD_DISABLE:
263 if (!sup->su_direct)
264 return (0);
265
266 slp->sl_rxinfo = &sup->su_rxinfo;
267 sup->su_direct = B_FALSE;
268 return (0);
269 }
270 return (ENOTSUP);
271 }
272
273 static int
softmac_dld_capab(softmac_upper_t * sup,uint_t type,void * data,uint_t flags)274 softmac_dld_capab(softmac_upper_t *sup, uint_t type, void *data, uint_t flags)
275 {
276 int err;
277
278 /*
279 * Don't enable direct callback capabilities unless the caller is
280 * the IP client. When a module is inserted in a stream (_I_INSERT)
281 * the stack initiates capability disable, but due to races, the
282 * module insertion may complete before the capability disable
283 * completes. So we limit the check to DLD_ENABLE case.
284 */
285 if ((flags == DLD_ENABLE && type != DLD_CAPAB_PERIM) &&
286 !check_ip_above(sup->su_rq)) {
287 return (ENOTSUP);
288 }
289
290 switch (type) {
291 case DLD_CAPAB_DIRECT:
292 err = softmac_capab_direct(sup, data, flags);
293 break;
294
295 case DLD_CAPAB_PERIM:
296 err = softmac_capab_perim(sup, data, flags);
297 break;
298
299 default:
300 err = ENOTSUP;
301 break;
302 }
303 return (err);
304 }
305
306 static void
softmac_capability_advertise(softmac_upper_t * sup,mblk_t * mp)307 softmac_capability_advertise(softmac_upper_t *sup, mblk_t *mp)
308 {
309 dl_capability_ack_t *dlap;
310 dl_capability_sub_t *dlsp;
311 t_uscalar_t subsize;
312 uint8_t *ptr;
313 queue_t *q = sup->su_wq;
314 mblk_t *mp1;
315 softmac_t *softmac = sup->su_softmac;
316 boolean_t dld_capable = B_FALSE;
317 boolean_t hcksum_capable = B_FALSE;
318 boolean_t zcopy_capable = B_FALSE;
319
320 ASSERT(sup->su_mode == SOFTMAC_FASTPATH);
321
322 /*
323 * Initially assume no capabilities.
324 */
325 subsize = 0;
326
327 /*
328 * Direct capability negotiation interface between IP and softmac
329 */
330 if (check_ip_above(sup->su_rq)) {
331 dld_capable = B_TRUE;
332 subsize += sizeof (dl_capability_sub_t) +
333 sizeof (dl_capab_dld_t);
334 }
335
336 /*
337 * Check if checksum offload is supported on this MAC.
338 */
339 if (softmac->smac_capab_flags & MAC_CAPAB_HCKSUM) {
340 hcksum_capable = B_TRUE;
341 subsize += sizeof (dl_capability_sub_t) +
342 sizeof (dl_capab_hcksum_t);
343 }
344
345 /*
346 * Check if zerocopy is supported on this interface.
347 */
348 if (!(softmac->smac_capab_flags & MAC_CAPAB_NO_ZCOPY)) {
349 zcopy_capable = B_TRUE;
350 subsize += sizeof (dl_capability_sub_t) +
351 sizeof (dl_capab_zerocopy_t);
352 }
353
354 /*
355 * If there are no capabilities to advertise or if we
356 * can't allocate a response, send a DL_ERROR_ACK.
357 */
358 if ((subsize == 0) || (mp1 = reallocb(mp,
359 sizeof (dl_capability_ack_t) + subsize, 0)) == NULL) {
360 dlerrorack(q, mp, DL_CAPABILITY_REQ, DL_NOTSUPPORTED, 0);
361 return;
362 }
363
364 mp = mp1;
365 DB_TYPE(mp) = M_PROTO;
366 mp->b_wptr = mp->b_rptr + sizeof (dl_capability_ack_t) + subsize;
367 bzero(mp->b_rptr, MBLKL(mp));
368 dlap = (dl_capability_ack_t *)mp->b_rptr;
369 dlap->dl_primitive = DL_CAPABILITY_ACK;
370 dlap->dl_sub_offset = sizeof (dl_capability_ack_t);
371 dlap->dl_sub_length = subsize;
372 ptr = (uint8_t *)&dlap[1];
373
374 /*
375 * IP polling interface.
376 */
377 if (dld_capable) {
378 dl_capab_dld_t dld;
379
380 dlsp = (dl_capability_sub_t *)ptr;
381 dlsp->dl_cap = DL_CAPAB_DLD;
382 dlsp->dl_length = sizeof (dl_capab_dld_t);
383 ptr += sizeof (dl_capability_sub_t);
384
385 bzero(&dld, sizeof (dl_capab_dld_t));
386 dld.dld_version = DLD_CURRENT_VERSION;
387 dld.dld_capab = (uintptr_t)softmac_dld_capab;
388 dld.dld_capab_handle = (uintptr_t)sup;
389
390 dlcapabsetqid(&(dld.dld_mid), sup->su_rq);
391 bcopy(&dld, ptr, sizeof (dl_capab_dld_t));
392 ptr += sizeof (dl_capab_dld_t);
393 }
394
395 /*
396 * TCP/IP checksum offload.
397 */
398 if (hcksum_capable) {
399 dl_capab_hcksum_t hcksum;
400
401 dlsp = (dl_capability_sub_t *)ptr;
402
403 dlsp->dl_cap = DL_CAPAB_HCKSUM;
404 dlsp->dl_length = sizeof (dl_capab_hcksum_t);
405 ptr += sizeof (dl_capability_sub_t);
406
407 bzero(&hcksum, sizeof (dl_capab_hcksum_t));
408 hcksum.hcksum_version = HCKSUM_VERSION_1;
409 hcksum.hcksum_txflags = softmac->smac_hcksum_txflags;
410 dlcapabsetqid(&(hcksum.hcksum_mid), sup->su_rq);
411 bcopy(&hcksum, ptr, sizeof (dl_capab_hcksum_t));
412 ptr += sizeof (dl_capab_hcksum_t);
413 }
414
415 /*
416 * Zero copy
417 */
418 if (zcopy_capable) {
419 dl_capab_zerocopy_t zcopy;
420
421 dlsp = (dl_capability_sub_t *)ptr;
422
423 dlsp->dl_cap = DL_CAPAB_ZEROCOPY;
424 dlsp->dl_length = sizeof (dl_capab_zerocopy_t);
425 ptr += sizeof (dl_capability_sub_t);
426
427 bzero(&zcopy, sizeof (dl_capab_zerocopy_t));
428 zcopy.zerocopy_version = ZEROCOPY_VERSION_1;
429 zcopy.zerocopy_flags = DL_CAPAB_VMSAFE_MEM;
430 dlcapabsetqid(&(zcopy.zerocopy_mid), sup->su_rq);
431 bcopy(&zcopy, ptr, sizeof (dl_capab_zerocopy_t));
432 ptr += sizeof (dl_capab_zerocopy_t);
433 }
434
435 ASSERT(ptr == mp->b_rptr + sizeof (dl_capability_ack_t) + subsize);
436 qreply(q, mp);
437 }
438
439 static void
softmac_capability_req(softmac_upper_t * sup,mblk_t * mp)440 softmac_capability_req(softmac_upper_t *sup, mblk_t *mp)
441 {
442 dl_capability_req_t *dlp = (dl_capability_req_t *)mp->b_rptr;
443 dl_capability_sub_t *sp;
444 size_t size, len;
445 offset_t off, end;
446 t_uscalar_t dl_err;
447 queue_t *q = sup->su_wq;
448
449 ASSERT(sup->su_mode == SOFTMAC_FASTPATH);
450 if (MBLKL(mp) < sizeof (dl_capability_req_t)) {
451 dl_err = DL_BADPRIM;
452 goto failed;
453 }
454
455 if (!sup->su_bound) {
456 dl_err = DL_OUTSTATE;
457 goto failed;
458 }
459
460 /*
461 * This request is overloaded. If there are no requested capabilities
462 * then we just want to acknowledge with all the capabilities we
463 * support. Otherwise we enable the set of capabilities requested.
464 */
465 if (dlp->dl_sub_length == 0) {
466 softmac_capability_advertise(sup, mp);
467 return;
468 }
469
470 if (!MBLKIN(mp, dlp->dl_sub_offset, dlp->dl_sub_length)) {
471 dl_err = DL_BADPRIM;
472 goto failed;
473 }
474
475 dlp->dl_primitive = DL_CAPABILITY_ACK;
476
477 off = dlp->dl_sub_offset;
478 len = dlp->dl_sub_length;
479
480 /*
481 * Walk the list of capabilities to be enabled.
482 */
483 for (end = off + len; off < end; ) {
484 sp = (dl_capability_sub_t *)(mp->b_rptr + off);
485 size = sizeof (dl_capability_sub_t) + sp->dl_length;
486
487 if (off + size > end ||
488 !IS_P2ALIGNED(off, sizeof (uint32_t))) {
489 dl_err = DL_BADPRIM;
490 goto failed;
491 }
492
493 switch (sp->dl_cap) {
494 /*
495 * TCP/IP checksum offload to hardware.
496 */
497 case DL_CAPAB_HCKSUM: {
498 dl_capab_hcksum_t *hcksump;
499 dl_capab_hcksum_t hcksum;
500
501 hcksump = (dl_capab_hcksum_t *)&sp[1];
502 /*
503 * Copy for alignment.
504 */
505 bcopy(hcksump, &hcksum, sizeof (dl_capab_hcksum_t));
506 dlcapabsetqid(&(hcksum.hcksum_mid), sup->su_rq);
507 bcopy(&hcksum, hcksump, sizeof (dl_capab_hcksum_t));
508 break;
509 }
510
511 default:
512 break;
513 }
514
515 off += size;
516 }
517 qreply(q, mp);
518 return;
519 failed:
520 dlerrorack(q, mp, DL_CAPABILITY_REQ, dl_err, 0);
521 }
522
523 static void
softmac_bind_req(softmac_upper_t * sup,mblk_t * mp)524 softmac_bind_req(softmac_upper_t *sup, mblk_t *mp)
525 {
526 softmac_lower_t *slp = sup->su_slp;
527 softmac_t *softmac = sup->su_softmac;
528 mblk_t *ackmp, *mp1;
529 int err;
530
531 if (MBLKL(mp) < DL_BIND_REQ_SIZE) {
532 freemsg(mp);
533 return;
534 }
535
536 /*
537 * Allocate ackmp incase the underlying driver does not ack timely.
538 */
539 if ((mp1 = allocb(sizeof (dl_error_ack_t), BPRI_HI)) == NULL) {
540 dlerrorack(sup->su_wq, mp, DL_BIND_REQ, DL_SYSERR, ENOMEM);
541 return;
542 }
543
544 err = softmac_output(slp, mp, DL_BIND_REQ, DL_BIND_ACK, &ackmp);
545 if (ackmp != NULL) {
546 freemsg(mp1);
547 } else {
548 /*
549 * The driver does not ack timely.
550 */
551 ASSERT(err == ENOMSG);
552 ackmp = mp1;
553 }
554 if (err != 0)
555 goto failed;
556
557 /*
558 * Enable capabilities the underlying driver claims to support.
559 */
560 if ((err = softmac_capab_enable(slp)) != 0)
561 goto failed;
562
563 /*
564 * Check whether this softmac is already marked as exclusively used,
565 * e.g., an aggregation is created over it. Fail the BIND_REQ if so.
566 */
567 mutex_enter(&softmac->smac_active_mutex);
568 if (softmac->smac_active) {
569 mutex_exit(&softmac->smac_active_mutex);
570 err = EBUSY;
571 goto failed;
572 }
573 softmac->smac_nactive++;
574 sup->su_active = B_TRUE;
575 mutex_exit(&softmac->smac_active_mutex);
576 sup->su_bound = B_TRUE;
577
578 qreply(sup->su_wq, ackmp);
579 return;
580 failed:
581 if (err != 0) {
582 dlerrorack(sup->su_wq, ackmp, DL_BIND_REQ, DL_SYSERR, err);
583 return;
584 }
585 }
586
587 static void
softmac_unbind_req(softmac_upper_t * sup,mblk_t * mp)588 softmac_unbind_req(softmac_upper_t *sup, mblk_t *mp)
589 {
590 softmac_lower_t *slp = sup->su_slp;
591 softmac_t *softmac = sup->su_softmac;
592 mblk_t *ackmp, *mp1;
593 int err;
594
595 if (MBLKL(mp) < DL_UNBIND_REQ_SIZE) {
596 freemsg(mp);
597 return;
598 }
599
600 if (!sup->su_bound) {
601 dlerrorack(sup->su_wq, mp, DL_UNBIND_REQ, DL_OUTSTATE, 0);
602 return;
603 }
604
605 /*
606 * Allocate ackmp incase the underlying driver does not ack timely.
607 */
608 if ((mp1 = allocb(sizeof (dl_error_ack_t), BPRI_HI)) == NULL) {
609 dlerrorack(sup->su_wq, mp, DL_UNBIND_REQ, DL_SYSERR, ENOMEM);
610 return;
611 }
612
613 err = softmac_output(slp, mp, DL_UNBIND_REQ, DL_OK_ACK, &ackmp);
614 if (ackmp != NULL) {
615 freemsg(mp1);
616 } else {
617 /*
618 * The driver does not ack timely.
619 */
620 ASSERT(err == ENOMSG);
621 ackmp = mp1;
622 }
623 if (err != 0) {
624 dlerrorack(sup->su_wq, ackmp, DL_UNBIND_REQ, DL_SYSERR, err);
625 return;
626 }
627
628 sup->su_bound = B_FALSE;
629
630 mutex_enter(&softmac->smac_active_mutex);
631 if (sup->su_active) {
632 ASSERT(!softmac->smac_active);
633 softmac->smac_nactive--;
634 sup->su_active = B_FALSE;
635 }
636 mutex_exit(&softmac->smac_active_mutex);
637
638 qreply(sup->su_wq, ackmp);
639 }
640
641 /*
642 * Process the non-data mblk.
643 */
644 static void
softmac_wput_single_nondata(softmac_upper_t * sup,mblk_t * mp)645 softmac_wput_single_nondata(softmac_upper_t *sup, mblk_t *mp)
646 {
647 softmac_t *softmac = sup->su_softmac;
648 softmac_lower_t *slp = sup->su_slp;
649 unsigned char dbtype;
650 t_uscalar_t prim;
651
652 dbtype = DB_TYPE(mp);
653 sup->su_is_arp = 0;
654 switch (dbtype) {
655 case M_CTL:
656 sup->su_is_arp = 1;
657 /* FALLTHROUGH */
658 case M_IOCTL: {
659 uint32_t expected_mode;
660
661 if (((struct iocblk *)(mp->b_rptr))->ioc_cmd != SIOCSLIFNAME)
662 break;
663
664 /*
665 * Nak the M_IOCTL based on the STREAMS specification.
666 */
667 if (dbtype == M_IOCTL)
668 miocnak(sup->su_wq, mp, 0, EINVAL);
669 else
670 freemsg(mp);
671
672 /*
673 * This stream is either IP or ARP. See whether
674 * we need to setup a dedicated-lower-stream for it.
675 */
676 mutex_enter(&softmac->smac_fp_mutex);
677
678 expected_mode = DATAPATH_MODE(softmac);
679 if (expected_mode == SOFTMAC_SLOWPATH)
680 sup->su_mode = SOFTMAC_SLOWPATH;
681 list_insert_head(&softmac->smac_sup_list, sup);
682 mutex_exit(&softmac->smac_fp_mutex);
683
684 /*
685 * Setup the fast-path dedicated lower stream if fast-path
686 * is expected. Note that no lock is held here, and if
687 * smac_expected_mode is changed from SOFTMAC_FASTPATH to
688 * SOFTMAC_SLOWPATH, the DL_NOTE_REPLUMB message used for
689 * data-path switching would already be queued and will
690 * be processed by softmac_wput_single_nondata() later.
691 */
692 if (expected_mode == SOFTMAC_FASTPATH)
693 (void) softmac_fastpath_setup(sup);
694 return;
695 }
696 case M_PROTO:
697 case M_PCPROTO:
698 if (MBLKL(mp) < sizeof (t_uscalar_t)) {
699 freemsg(mp);
700 return;
701 }
702 prim = ((union DL_primitives *)mp->b_rptr)->dl_primitive;
703 switch (prim) {
704 case DL_NOTIFY_IND:
705 if (MBLKL(mp) < sizeof (dl_notify_ind_t) ||
706 ((dl_notify_ind_t *)mp->b_rptr)->dl_notification !=
707 DL_NOTE_REPLUMB) {
708 freemsg(mp);
709 return;
710 }
711 /*
712 * This DL_NOTE_REPLUMB message is initiated
713 * and queued by the softmac itself, when the
714 * sup is trying to switching its datapath mode
715 * between SOFTMAC_SLOWPATH and SOFTMAC_FASTPATH.
716 * Send this message upstream.
717 */
718 qreply(sup->su_wq, mp);
719 return;
720 case DL_NOTIFY_CONF:
721 if (MBLKL(mp) < sizeof (dl_notify_conf_t) ||
722 ((dl_notify_conf_t *)mp->b_rptr)->dl_notification !=
723 DL_NOTE_REPLUMB_DONE) {
724 freemsg(mp);
725 return;
726 }
727 /*
728 * This is an indication from IP/ARP that the
729 * fastpath->slowpath switch is done.
730 */
731 freemsg(mp);
732 softmac_datapath_switch_done(sup);
733 return;
734 }
735 break;
736 }
737
738 /*
739 * No need to hold lock to check su_mode, since su_mode updating only
740 * operation is is serialized by softmac_wput_nondata_task().
741 */
742 if (sup->su_mode != SOFTMAC_FASTPATH) {
743 (void) dld_wput(sup->su_wq, mp);
744 return;
745 }
746
747 /*
748 * Fastpath non-data message processing. Most of non-data messages
749 * can be directly passed down to the dedicated-lower-stream, aside
750 * from the following M_PROTO/M_PCPROTO messages.
751 */
752 switch (dbtype) {
753 case M_PROTO:
754 case M_PCPROTO:
755 switch (prim) {
756 case DL_BIND_REQ:
757 softmac_bind_req(sup, mp);
758 break;
759 case DL_UNBIND_REQ:
760 softmac_unbind_req(sup, mp);
761 break;
762 case DL_CAPABILITY_REQ:
763 softmac_capability_req(sup, mp);
764 break;
765 default:
766 putnext(slp->sl_wq, mp);
767 break;
768 }
769 break;
770 default:
771 putnext(slp->sl_wq, mp);
772 break;
773 }
774 }
775
776 /*
777 * The worker thread which processes non-data messages. Note we only process
778 * one message at one time in order to be able to "flush" the queued message
779 * and serialize the processing.
780 */
781 static void
softmac_wput_nondata_task(void * arg)782 softmac_wput_nondata_task(void *arg)
783 {
784 softmac_upper_t *sup = arg;
785 mblk_t *mp;
786
787 mutex_enter(&sup->su_disp_mutex);
788
789 while (sup->su_pending_head != NULL) {
790 if (sup->su_closing)
791 break;
792
793 SOFTMAC_DQ_PENDING(sup, &mp);
794 mutex_exit(&sup->su_disp_mutex);
795 softmac_wput_single_nondata(sup, mp);
796 mutex_enter(&sup->su_disp_mutex);
797 }
798
799 /*
800 * If the stream is closing, flush all queued messages and inform
801 * the stream to be closed.
802 */
803 freemsgchain(sup->su_pending_head);
804 sup->su_pending_head = sup->su_pending_tail = NULL;
805 sup->su_dlpi_pending = B_FALSE;
806 cv_signal(&sup->su_disp_cv);
807 mutex_exit(&sup->su_disp_mutex);
808 }
809
810 /*
811 * Kernel thread to handle taskq dispatch failures in softmac_wput_nondata().
812 * This thread is started when the softmac module is first loaded.
813 */
814 static void
softmac_taskq_dispatch(void)815 softmac_taskq_dispatch(void)
816 {
817 callb_cpr_t cprinfo;
818 softmac_upper_t *sup;
819
820 CALLB_CPR_INIT(&cprinfo, &softmac_taskq_lock, callb_generic_cpr,
821 "softmac_taskq_dispatch");
822 mutex_enter(&softmac_taskq_lock);
823
824 while (!softmac_taskq_quit) {
825 sup = list_head(&softmac_taskq_list);
826 while (sup != NULL) {
827 list_remove(&softmac_taskq_list, sup);
828 sup->su_taskq_scheduled = B_FALSE;
829 mutex_exit(&softmac_taskq_lock);
830 VERIFY(taskq_dispatch(system_taskq,
831 softmac_wput_nondata_task, sup, TQ_SLEEP) !=
832 TASKQID_INVALID);
833 mutex_enter(&softmac_taskq_lock);
834 sup = list_head(&softmac_taskq_list);
835 }
836
837 CALLB_CPR_SAFE_BEGIN(&cprinfo);
838 cv_wait(&softmac_taskq_cv, &softmac_taskq_lock);
839 CALLB_CPR_SAFE_END(&cprinfo, &softmac_taskq_lock);
840 }
841
842 softmac_taskq_done = B_TRUE;
843 cv_signal(&softmac_taskq_cv);
844 CALLB_CPR_EXIT(&cprinfo);
845 thread_exit();
846 }
847
848 void
softmac_wput_nondata(softmac_upper_t * sup,mblk_t * mp)849 softmac_wput_nondata(softmac_upper_t *sup, mblk_t *mp)
850 {
851 /*
852 * The processing of the message might block. Enqueue the
853 * message for later processing.
854 */
855 mutex_enter(&sup->su_disp_mutex);
856
857 if (sup->su_closing) {
858 mutex_exit(&sup->su_disp_mutex);
859 freemsg(mp);
860 return;
861 }
862
863 SOFTMAC_EQ_PENDING(sup, mp);
864
865 if (sup->su_dlpi_pending) {
866 mutex_exit(&sup->su_disp_mutex);
867 return;
868 }
869 sup->su_dlpi_pending = B_TRUE;
870 mutex_exit(&sup->su_disp_mutex);
871
872 if (taskq_dispatch(system_taskq, softmac_wput_nondata_task,
873 sup, TQ_NOSLEEP) != TASKQID_INVALID) {
874 return;
875 }
876
877 mutex_enter(&softmac_taskq_lock);
878 if (!sup->su_taskq_scheduled) {
879 list_insert_tail(&softmac_taskq_list, sup);
880 cv_signal(&softmac_taskq_cv);
881 }
882 sup->su_taskq_scheduled = B_TRUE;
883 mutex_exit(&softmac_taskq_lock);
884 }
885
886 /*
887 * Setup the dedicated-lower-stream (fast-path) for the IP/ARP upperstream.
888 */
889 static int
softmac_fastpath_setup(softmac_upper_t * sup)890 softmac_fastpath_setup(softmac_upper_t *sup)
891 {
892 softmac_t *softmac = sup->su_softmac;
893 softmac_lower_t *slp;
894 int err;
895
896 err = softmac_lower_setup(softmac, sup, &slp);
897
898 mutex_enter(&sup->su_mutex);
899 /*
900 * Wait for all data messages to be processed so that we can change
901 * the su_mode.
902 */
903 while (sup->su_tx_inprocess != 0)
904 cv_wait(&sup->su_cv, &sup->su_mutex);
905
906 ASSERT(sup->su_mode != SOFTMAC_FASTPATH);
907 ASSERT(sup->su_slp == NULL);
908 if (err != 0) {
909 sup->su_mode = SOFTMAC_SLOWPATH;
910 } else {
911 sup->su_slp = slp;
912 sup->su_mode = SOFTMAC_FASTPATH;
913 }
914 mutex_exit(&sup->su_mutex);
915 return (err);
916 }
917
918 /*
919 * Tear down the dedicated-lower-stream (fast-path) for the IP/ARP upperstream.
920 */
921 static void
softmac_fastpath_tear(softmac_upper_t * sup)922 softmac_fastpath_tear(softmac_upper_t *sup)
923 {
924 mutex_enter(&sup->su_mutex);
925 /*
926 * Wait for all data messages in the dedicated-lower-stream
927 * to be processed.
928 */
929 while (sup->su_tx_inprocess != 0)
930 cv_wait(&sup->su_cv, &sup->su_mutex);
931
932 /*
933 * Note that this function is called either when the stream is closed,
934 * or the stream is unbound (fastpath-slowpath-switch). Therefore,
935 * No need to call the tx_notify callback.
936 */
937 sup->su_tx_notify_func = NULL;
938 sup->su_tx_notify_arg = NULL;
939 if (sup->su_tx_busy) {
940 ASSERT(sup->su_tx_flow_mp == NULL);
941 VERIFY((sup->su_tx_flow_mp = getq(sup->su_wq)) != NULL);
942 sup->su_tx_busy = B_FALSE;
943 }
944
945 sup->su_mode = SOFTMAC_SLOWPATH;
946
947 /*
948 * Destroy the dedicated-lower-stream. Note that slp is destroyed
949 * when lh is closed.
950 */
951 (void) ldi_close(sup->su_slp->sl_lh, FREAD|FWRITE, kcred);
952 sup->su_slp = NULL;
953 mutex_exit(&sup->su_mutex);
954 }
955
956 void
softmac_wput_data(softmac_upper_t * sup,mblk_t * mp)957 softmac_wput_data(softmac_upper_t *sup, mblk_t *mp)
958 {
959 /*
960 * No lock is required to access the su_mode field since the data
961 * traffic is quiesce by IP when the data-path mode is in the
962 * process of switching.
963 */
964 if (sup->su_mode != SOFTMAC_FASTPATH)
965 (void) dld_wput(sup->su_wq, mp);
966 else
967 (void) softmac_fastpath_wput_data(sup, mp, (uintptr_t)NULL, 0);
968 }
969
970 /*ARGSUSED*/
971 static mac_tx_cookie_t
softmac_fastpath_wput_data(softmac_upper_t * sup,mblk_t * mp,uintptr_t f_hint,uint16_t flag)972 softmac_fastpath_wput_data(softmac_upper_t *sup, mblk_t *mp, uintptr_t f_hint,
973 uint16_t flag)
974 {
975 queue_t *wq = sup->su_slp->sl_wq;
976
977 /*
978 * This function is called from IP, only the MAC_DROP_ON_NO_DESC
979 * flag can be specified.
980 */
981 ASSERT((flag & ~MAC_DROP_ON_NO_DESC) == 0);
982 ASSERT(mp->b_next == NULL);
983
984 /*
985 * Check wether the dedicated-lower-stream is able to handle more
986 * messages, and enable the flow-control if it is not.
987 *
988 * Note that in order not to introduce any packet reordering, we
989 * always send the message down to the dedicated-lower-stream:
990 *
991 * If the flow-control is already enabled, but we still get
992 * the messages from the upper-stream, it means that the upper
993 * stream does not respect STREAMS flow-control (e.g., TCP). Simply
994 * pass the message down to the lower-stream in that case.
995 */
996 if (SOFTMAC_CANPUTNEXT(wq)) {
997 putnext(wq, mp);
998 return ((mac_tx_cookie_t)NULL);
999 }
1000
1001 if (sup->su_tx_busy) {
1002 if ((flag & MAC_DROP_ON_NO_DESC) != 0)
1003 freemsg(mp);
1004 else
1005 putnext(wq, mp);
1006 return ((mac_tx_cookie_t)sup);
1007 }
1008
1009 mutex_enter(&sup->su_mutex);
1010 if (!sup->su_tx_busy) {
1011 /*
1012 * If DLD_CAPAB_DIRECT is enabled, the notify callback will be
1013 * called when the flow control can be disabled. Otherwise,
1014 * put the tx_flow_mp into the wq to make use of the old
1015 * streams flow control.
1016 */
1017 ASSERT(sup->su_tx_flow_mp != NULL);
1018 (void) putq(sup->su_wq, sup->su_tx_flow_mp);
1019 sup->su_tx_flow_mp = NULL;
1020 sup->su_tx_busy = B_TRUE;
1021 qenable(wq);
1022 }
1023 mutex_exit(&sup->su_mutex);
1024
1025 if ((flag & MAC_DROP_ON_NO_DESC) != 0)
1026 freemsg(mp);
1027 else
1028 putnext(wq, mp);
1029 return ((mac_tx_cookie_t)sup);
1030 }
1031
1032 boolean_t
softmac_active_set(void * arg)1033 softmac_active_set(void *arg)
1034 {
1035 softmac_t *softmac = arg;
1036
1037 mutex_enter(&softmac->smac_active_mutex);
1038 if (softmac->smac_nactive != 0) {
1039 mutex_exit(&softmac->smac_active_mutex);
1040 return (B_FALSE);
1041 }
1042 softmac->smac_active = B_TRUE;
1043 mutex_exit(&softmac->smac_active_mutex);
1044 return (B_TRUE);
1045 }
1046
1047 void
softmac_active_clear(void * arg)1048 softmac_active_clear(void *arg)
1049 {
1050 softmac_t *softmac = arg;
1051
1052 mutex_enter(&softmac->smac_active_mutex);
1053 ASSERT(softmac->smac_active && (softmac->smac_nactive == 0));
1054 softmac->smac_active = B_FALSE;
1055 mutex_exit(&softmac->smac_active_mutex);
1056 }
1057
1058 /*
1059 * Disable/reenable fastpath on given softmac. This request could come from a
1060 * MAC client or directly from administrators.
1061 */
1062 int
softmac_datapath_switch(softmac_t * softmac,boolean_t disable,boolean_t admin)1063 softmac_datapath_switch(softmac_t *softmac, boolean_t disable, boolean_t admin)
1064 {
1065 softmac_upper_t *sup;
1066 mblk_t *head = NULL, *tail = NULL, *mp;
1067 list_t reqlist;
1068 softmac_switch_req_t *req;
1069 uint32_t current_mode, expected_mode;
1070 int err = 0;
1071
1072 mutex_enter(&softmac->smac_fp_mutex);
1073
1074 current_mode = DATAPATH_MODE(softmac);
1075 if (admin) {
1076 if (softmac->smac_fastpath_admin_disabled == disable) {
1077 mutex_exit(&softmac->smac_fp_mutex);
1078 return (0);
1079 }
1080 softmac->smac_fastpath_admin_disabled = disable;
1081 } else if (disable) {
1082 softmac->smac_fp_disable_clients++;
1083 } else {
1084 ASSERT(softmac->smac_fp_disable_clients != 0);
1085 softmac->smac_fp_disable_clients--;
1086 }
1087
1088 expected_mode = DATAPATH_MODE(softmac);
1089 if (current_mode == expected_mode) {
1090 mutex_exit(&softmac->smac_fp_mutex);
1091 return (0);
1092 }
1093
1094 /*
1095 * The expected mode is different from whatever datapath mode
1096 * this softmac is expected from last request, enqueue the data-path
1097 * switch request.
1098 */
1099 list_create(&reqlist, sizeof (softmac_switch_req_t),
1100 offsetof(softmac_switch_req_t, ssq_req_list_node));
1101
1102 /*
1103 * Allocate all DL_NOTIFY_IND messages and request structures that
1104 * are required to switch each IP/ARP stream to the expected mode.
1105 */
1106 for (sup = list_head(&softmac->smac_sup_list); sup != NULL;
1107 sup = list_next(&softmac->smac_sup_list, sup)) {
1108 dl_notify_ind_t *dlip;
1109
1110 req = kmem_alloc(sizeof (softmac_switch_req_t), KM_NOSLEEP);
1111 if (req == NULL)
1112 break;
1113
1114 req->ssq_expected_mode = expected_mode;
1115 if (sup->su_is_arp) {
1116 list_insert_tail(&reqlist, req);
1117 continue;
1118 }
1119 /*
1120 * Allocate the DL_NOTE_REPLUMB message.
1121 */
1122 if ((mp = allocb(sizeof (dl_notify_ind_t), BPRI_LO)) == NULL) {
1123 kmem_free(req, sizeof (softmac_switch_req_t));
1124 break;
1125 }
1126
1127 list_insert_tail(&reqlist, req);
1128
1129 mp->b_wptr = mp->b_rptr + sizeof (dl_notify_ind_t);
1130 mp->b_datap->db_type = M_PROTO;
1131 bzero(mp->b_rptr, sizeof (dl_notify_ind_t));
1132 dlip = (dl_notify_ind_t *)mp->b_rptr;
1133 dlip->dl_primitive = DL_NOTIFY_IND;
1134 dlip->dl_notification = DL_NOTE_REPLUMB;
1135 if (head == NULL) {
1136 head = tail = mp;
1137 } else {
1138 tail->b_next = mp;
1139 tail = mp;
1140 }
1141 }
1142
1143 /*
1144 * Note that it is fine if the expected data-path mode is fast-path
1145 * and some of streams fails to switch. Only return failure if we
1146 * are expected to switch to the slow-path.
1147 */
1148 if (sup != NULL && expected_mode == SOFTMAC_SLOWPATH) {
1149 err = ENOMEM;
1150 goto fail;
1151 }
1152
1153 /*
1154 * Start switching for each IP/ARP stream. The switching operation
1155 * will eventually succeed and there is no need to wait for it
1156 * to finish.
1157 */
1158 for (sup = list_head(&softmac->smac_sup_list); sup != NULL;
1159 sup = list_next(&softmac->smac_sup_list, sup)) {
1160 if (!sup->su_is_arp) {
1161 mp = head->b_next;
1162 head->b_next = NULL;
1163 softmac_wput_nondata(sup, head);
1164 head = mp;
1165 }
1166 /*
1167 * Add the switch request to the requests list of the stream.
1168 */
1169 req = list_head(&reqlist);
1170 ASSERT(req != NULL);
1171 list_remove(&reqlist, req);
1172 list_insert_tail(&sup->su_req_list, req);
1173 }
1174
1175 mutex_exit(&softmac->smac_fp_mutex);
1176 ASSERT(list_is_empty(&reqlist));
1177 list_destroy(&reqlist);
1178 return (0);
1179 fail:
1180 if (admin) {
1181 softmac->smac_fastpath_admin_disabled = !disable;
1182 } else if (disable) {
1183 softmac->smac_fp_disable_clients--;
1184 } else {
1185 softmac->smac_fp_disable_clients++;
1186 }
1187
1188 mutex_exit(&softmac->smac_fp_mutex);
1189 while ((req = list_head(&reqlist)) != NULL) {
1190 list_remove(&reqlist, req);
1191 kmem_free(req, sizeof (softmac_switch_req_t));
1192 }
1193 freemsgchain(head);
1194 list_destroy(&reqlist);
1195 return (err);
1196 }
1197
1198 int
softmac_fastpath_disable(void * arg)1199 softmac_fastpath_disable(void *arg)
1200 {
1201 return (softmac_datapath_switch((softmac_t *)arg, B_TRUE, B_FALSE));
1202 }
1203
1204 void
softmac_fastpath_enable(void * arg)1205 softmac_fastpath_enable(void *arg)
1206 {
1207 VERIFY(softmac_datapath_switch((softmac_t *)arg, B_FALSE,
1208 B_FALSE) == 0);
1209 }
1210
1211 void
softmac_upperstream_close(softmac_upper_t * sup)1212 softmac_upperstream_close(softmac_upper_t *sup)
1213 {
1214 softmac_t *softmac = sup->su_softmac;
1215 softmac_switch_req_t *req;
1216
1217 mutex_enter(&softmac->smac_fp_mutex);
1218
1219 if (sup->su_mode == SOFTMAC_FASTPATH)
1220 softmac_fastpath_tear(sup);
1221
1222 if (sup->su_mode != SOFTMAC_UNKNOWN) {
1223 list_remove(&softmac->smac_sup_list, sup);
1224 sup->su_mode = SOFTMAC_UNKNOWN;
1225 }
1226
1227 /*
1228 * Cleanup all the switch requests queueed on this stream.
1229 */
1230 while ((req = list_head(&sup->su_req_list)) != NULL) {
1231 list_remove(&sup->su_req_list, req);
1232 kmem_free(req, sizeof (softmac_switch_req_t));
1233 }
1234 mutex_exit(&softmac->smac_fp_mutex);
1235 }
1236
1237 /*
1238 * Handle the DL_NOTE_REPLUMB_DONE indication from IP/ARP. Change the upper
1239 * stream from the fastpath mode to the slowpath mode.
1240 */
1241 static void
softmac_datapath_switch_done(softmac_upper_t * sup)1242 softmac_datapath_switch_done(softmac_upper_t *sup)
1243 {
1244 softmac_t *softmac = sup->su_softmac;
1245 softmac_switch_req_t *req;
1246 uint32_t expected_mode;
1247
1248 mutex_enter(&softmac->smac_fp_mutex);
1249 req = list_head(&sup->su_req_list);
1250 list_remove(&sup->su_req_list, req);
1251 expected_mode = req->ssq_expected_mode;
1252 kmem_free(req, sizeof (softmac_switch_req_t));
1253
1254 if (expected_mode == sup->su_mode) {
1255 mutex_exit(&softmac->smac_fp_mutex);
1256 return;
1257 }
1258
1259 ASSERT(!sup->su_bound);
1260 mutex_exit(&softmac->smac_fp_mutex);
1261
1262 /*
1263 * It is fine if the expected mode is fast-path and we fail
1264 * to enable fastpath on this stream.
1265 */
1266 if (expected_mode == SOFTMAC_SLOWPATH)
1267 softmac_fastpath_tear(sup);
1268 else
1269 (void) softmac_fastpath_setup(sup);
1270 }
1271