xref: /illumos-gate/usr/src/uts/common/ipp/dlcosmk/dlcosmk.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 #include <sys/types.h>
29 #include <sys/stream.h>
30 #include <sys/dlpi.h>
31 #include <sys/strsun.h>
32 #include <netinet/in.h>
33 #include <netinet/ip6.h>
34 #include <inet/common.h>
35 #include <inet/ip.h>
36 #include <inet/ip6.h>
37 #include <inet/ip_if.h>
38 #include <ipp/dlcosmk/dlcosmk_impl.h>
39 
40 /* Module to mark the 802.1d user priority field for a given packet */
41 
42 /* Debug level */
43 int dlcosmk_debug = 0;
44 
45 /*
46  * Given a packet, this module marks the mblk with the appropriate b_band or
47  * dl_max value so that the VLAN driver marks the outgoing frame with the
48  * configured 802.1D user_priority value. For non-VLAN devices or for inbound
49  * packets, this module does not do anything (i.e. the packet is processed by
50  * the next action in the list, if present).
51  * This module does not free any mblks or packets in case or errors.
52  */
53 
54 int
55 dlcosmk_process(mblk_t **mpp, dlcosmk_data_t *dlcosmk_data, uint32_t ill_index,
56     ip_proc_t proc)
57 {
58 	ill_t *ill = NULL;
59 	mblk_t *mp;
60 
61 	ASSERT((mpp != NULL) && (*mpp != NULL));
62 	mp = *mpp;
63 
64 	/*
65 	 * The action module will receive an M_DATA or an M_CTL followed
66 	 * by an M_DATA. In the latter case skip the M_CTL.
67 	 */
68 	if (mp->b_datap->db_type != M_DATA) {
69 		if ((mp->b_cont == NULL) ||
70 		    (mp->b_cont->b_datap->db_type != M_DATA)) {
71 			atomic_add_64(&dlcosmk_data->epackets, 1);
72 			dlcosmk0dbg(("dlcosmk_process: no data\n"));
73 			return (EINVAL);
74 		}
75 	}
76 
77 	/* Update global stats */
78 	atomic_add_64(&dlcosmk_data->npackets, 1);
79 
80 	/*
81 	 * This should only be called for outgoing packets. For inbound, just
82 	 * send it along.
83 	 */
84 	if ((proc == IPP_LOCAL_IN) || (proc == IPP_FWD_IN)) {
85 		dlcosmk2dbg(("dlcosmk_process:cannot mark incoming packets\n"));
86 		atomic_add_64(&dlcosmk_data->ipackets, 1);
87 		return (0);
88 	}
89 
90 	if ((ill_index == 0) ||
91 	    ((ill = ill_lookup_on_ifindex_global_instance(ill_index, B_FALSE,
92 		NULL, NULL, NULL, NULL)) == NULL)) {
93 		dlcosmk2dbg(("dlcosmk_process:invalid ill index %u\n",
94 		    ill_index));
95 		atomic_add_64(&dlcosmk_data->ipackets, 1);
96 		return (0);
97 	}
98 
99 	/*
100 	 * Check if the interface supports CoS marking. If not send it to the
101 	 * next action in the chain
102 	 */
103 	if (!(ill->ill_flags & ILLF_COS_ENABLED)) {
104 		dlcosmk2dbg(("dlcosmk_process:ill %u does not support CoS\n",
105 		    ill_index));
106 		atomic_add_64(&dlcosmk_data->ipackets, 1);
107 		ill_refrele(ill);
108 		return (0);
109 	}
110 	ill_refrele(ill);
111 
112 
113 	/*
114 	 * Mark the b_band for fastpath messages or dl_priority.dl_max for
115 	 * DL_UNITDATA_REQ messages. For, others just pass it along.
116 	 */
117 	switch (DB_TYPE(mp)) {
118 		case M_PROTO:
119 		case M_PCPROTO:
120 			{ 	/* DL_UNITDATA */
121 				dl_unitdata_req_t *dlur;
122 				dlur = (dl_unitdata_req_t *)mp->b_rptr;
123 
124 				/* DL_UNITDATA message?? */
125 				if (dlur->dl_primitive == DL_UNITDATA_REQ) {
126 					dlur->dl_priority.dl_max =
127 					    dlcosmk_data->dl_max;
128 				} else {
129 					atomic_add_64(&dlcosmk_data->ipackets,
130 					    1);
131 				}
132 				break;
133 			}
134 		case M_DATA:
135 			/* fastpath message */
136 			mp->b_band = dlcosmk_data->b_band;
137 			break;
138 		default:
139 			atomic_add_64(&dlcosmk_data->ipackets, 1);
140 			break;
141 	}
142 
143 	return (0);
144 }
145