xref: /titanic_51/usr/src/uts/common/ipp/dscpmk/dscpmk.c (revision 1a5e258f5471356ca102c7176637cdce45bac147)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
231d8c4025Svi117747  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
291d8c4025Svi117747 #include <sys/pattr.h>
307c478bd9Sstevel@tonic-gate #include <netinet/in.h>
317c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
327c478bd9Sstevel@tonic-gate #include <inet/common.h>
337c478bd9Sstevel@tonic-gate #include <inet/ip.h>
347c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
357c478bd9Sstevel@tonic-gate #include <ipp/dscpmk/dscpmk_impl.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /* Module to mark the ToS/DS field for a given packet */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /* Debug level */
407c478bd9Sstevel@tonic-gate int dscpmk_debug = 0;
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * Given a packet, this routine marks the ToS or DSCP for IPv4 and IPv6 resp.
447c478bd9Sstevel@tonic-gate  * using the configured dscp_map.
457c478bd9Sstevel@tonic-gate  * Note that this module does not change the ECN bits.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate int
487c478bd9Sstevel@tonic-gate dscpmk_process(mblk_t **mpp, dscpmk_data_t *dscpmk_data, ip_proc_t proc)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	ipha_t *ipha;
517c478bd9Sstevel@tonic-gate 	ip6_t *ip6_hdr;
527c478bd9Sstevel@tonic-gate 	boolean_t is_v4;
537c478bd9Sstevel@tonic-gate 	uint8_t dscp, new_dscp;
547c478bd9Sstevel@tonic-gate 	mblk_t *mp;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	ASSERT((mpp != NULL) && (*mpp != NULL));
577c478bd9Sstevel@tonic-gate 	mp = *mpp;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	/*
607c478bd9Sstevel@tonic-gate 	 * The action module will receive an M_DATA or an M_CTL followed
617c478bd9Sstevel@tonic-gate 	 * by an M_DATA. In the latter case skip the M_CTL.
627c478bd9Sstevel@tonic-gate 	 */
637c478bd9Sstevel@tonic-gate 	if (mp->b_datap->db_type != M_DATA) {
647c478bd9Sstevel@tonic-gate 		if ((mp->b_cont != NULL) &&
657c478bd9Sstevel@tonic-gate 		    (mp->b_cont->b_datap->db_type == M_DATA)) {
667c478bd9Sstevel@tonic-gate 			mp = mp->b_cont;
677c478bd9Sstevel@tonic-gate 		} else {
687c478bd9Sstevel@tonic-gate 			dscpmk0dbg(("dscpmk_process: no data\n"));
69*1a5e258fSJosef 'Jeff' Sipek 			atomic_inc_64(&dscpmk_data->epackets);
707c478bd9Sstevel@tonic-gate 			return (EINVAL);
717c478bd9Sstevel@tonic-gate 		}
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	/* Pull-up needed? */
757c478bd9Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) < IP_SIMPLE_HDR_LENGTH) {
767c478bd9Sstevel@tonic-gate 		if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
777c478bd9Sstevel@tonic-gate 			dscpmk0dbg(("dscpmk_process: pullup failed\n"));
78*1a5e258fSJosef 'Jeff' Sipek 			atomic_inc_64(&dscpmk_data->epackets);
797c478bd9Sstevel@tonic-gate 			return (EINVAL);
807c478bd9Sstevel@tonic-gate 		}
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	/* Update global stats */
85*1a5e258fSJosef 'Jeff' Sipek 	atomic_inc_64(&dscpmk_data->npackets);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	/*
887c478bd9Sstevel@tonic-gate 	 * This should only be called for outgoing packets. For inbound packets
897c478bd9Sstevel@tonic-gate 	 * proceed with the next action.
907c478bd9Sstevel@tonic-gate 	 */
917c478bd9Sstevel@tonic-gate 	if ((proc == IPP_LOCAL_IN) || (proc == IPP_FWD_IN)) {
927c478bd9Sstevel@tonic-gate 		dscpmk2dbg(("dscpmk_process: cannot mark incoming packets\n"));
93*1a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&dscpmk_data->ipackets);
947c478bd9Sstevel@tonic-gate 		return (0);
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/* Figure out the ToS or the Traffic Class from the message */
987c478bd9Sstevel@tonic-gate 	if (IPH_HDR_VERSION(ipha) == IPV4_VERSION) {
997c478bd9Sstevel@tonic-gate 		dscp = ipha->ipha_type_of_service;
1007c478bd9Sstevel@tonic-gate 		is_v4 = B_TRUE;
1017c478bd9Sstevel@tonic-gate 	} else {
1027c478bd9Sstevel@tonic-gate 		ip6_hdr = (ip6_t *)mp->b_rptr;
1037c478bd9Sstevel@tonic-gate 		dscp = __IPV6_TCLASS_FROM_FLOW(ip6_hdr->ip6_vcf);
1047c478bd9Sstevel@tonic-gate 		is_v4 = B_FALSE;
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	/*
1087c478bd9Sstevel@tonic-gate 	 * Select the new dscp from the dscp_map after ignoring the
1097c478bd9Sstevel@tonic-gate 	 * ECN/CU from dscp (hence dscp >> 2). new_dscp will be the
1107c478bd9Sstevel@tonic-gate 	 * 6-bit DSCP value.
1117c478bd9Sstevel@tonic-gate 	 */
1127c478bd9Sstevel@tonic-gate 	new_dscp = dscpmk_data->dscp_map[dscp >> 2];
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	/* Update stats for this new_dscp */
115*1a5e258fSJosef 'Jeff' Sipek 	atomic_inc_64(&dscpmk_data->dscp_stats[new_dscp].npackets);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	/*
1187c478bd9Sstevel@tonic-gate 	 * if new_dscp is same as the original, update stats and
1197c478bd9Sstevel@tonic-gate 	 * return.
1207c478bd9Sstevel@tonic-gate 	 */
1217c478bd9Sstevel@tonic-gate 	if (new_dscp == (dscp >> 2)) {
122*1a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&dscpmk_data->unchanged);
1237c478bd9Sstevel@tonic-gate 		return (0);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	/* Get back the ECN/CU value from the original dscp */
1277c478bd9Sstevel@tonic-gate 	new_dscp = (new_dscp << 2) | (dscp & 0x3);
1287c478bd9Sstevel@tonic-gate 
129*1a5e258fSJosef 'Jeff' Sipek 	atomic_inc_64(&dscpmk_data->changed);
1307c478bd9Sstevel@tonic-gate 	/*
1317c478bd9Sstevel@tonic-gate 	 * IPv4 : ToS structure -- RFC 791
1327c478bd9Sstevel@tonic-gate 	 *
1337c478bd9Sstevel@tonic-gate 	 *	  0   1   2   3   4   5   6   7
1347c478bd9Sstevel@tonic-gate 	 *	+---+---+---+---+---+---+---+---+
1357c478bd9Sstevel@tonic-gate 	 *	| IP Precd  | D	| T | R	| 0 | 0	|
1367c478bd9Sstevel@tonic-gate 	 *	|	    |	|   |	|   |	|
1377c478bd9Sstevel@tonic-gate 	 *	+---+---+---+---+---+---+---+---+
1387c478bd9Sstevel@tonic-gate 	 *
1397c478bd9Sstevel@tonic-gate 	 * For Backward Compatability the diff serv DSCP will be mapped
1407c478bd9Sstevel@tonic-gate 	 * to the 3-bits Precedence field. DTR is not supported. Thus,
1417c478bd9Sstevel@tonic-gate 	 * the following Class Seletor CodePoints are reserved from this
1427c478bd9Sstevel@tonic-gate 	 * purpose : xxx000; where x is 0 or 1 (note the last 2 bits are
1437c478bd9Sstevel@tonic-gate 	 * 00) -- see RFC 2474.
1447c478bd9Sstevel@tonic-gate 	 */
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	if (is_v4) {
1477c478bd9Sstevel@tonic-gate 		ipha->ipha_type_of_service = new_dscp;
1481d8c4025Svi117747 		/*
1491d8c4025Svi117747 		 * If the hardware supports checksumming, we don't need
1501d8c4025Svi117747 		 * to do anything.
1511d8c4025Svi117747 		 */
1521d8c4025Svi117747 		if (!(mp->b_datap->db_struioun.cksum.flags &
1531d8c4025Svi117747 		    HCK_IPV4_HDRCKSUM)) {
1547c478bd9Sstevel@tonic-gate 			ipha->ipha_hdr_checksum = 0;
1557c478bd9Sstevel@tonic-gate 			ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
1567c478bd9Sstevel@tonic-gate 		}
1571d8c4025Svi117747 	} else {
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	/*
1607c478bd9Sstevel@tonic-gate 	 * IPv6 : DSCP field structure is as given -- RFC 2474
1617c478bd9Sstevel@tonic-gate 	 *
1627c478bd9Sstevel@tonic-gate 	 *	  0   1   2   3   4   5   6   7
1637c478bd9Sstevel@tonic-gate 	 *	+---+---+---+---+---+---+---+---+
1647c478bd9Sstevel@tonic-gate 	 *	|	DSCP		| CU	|
1657c478bd9Sstevel@tonic-gate 	 *	|			|	|
1667c478bd9Sstevel@tonic-gate 	 *	+---+---+---+---+---+---+---+---+
1677c478bd9Sstevel@tonic-gate 	 *
1687c478bd9Sstevel@tonic-gate 	 * CU -- Currently Unused
1691d8c4025Svi117747 	 *
1707c478bd9Sstevel@tonic-gate 	 * the 32 bit vcf consists of version (4 bits), Traffic class (8 bits)
1717c478bd9Sstevel@tonic-gate 	 * and flow id (20 bits). Need to take care of Big/Little-Endianess.
1727c478bd9Sstevel@tonic-gate 	 */
1737c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN
1747c478bd9Sstevel@tonic-gate 		ip6_hdr->ip6_vcf = (ip6_hdr->ip6_vcf & TCLASS_MASK) |
1757c478bd9Sstevel@tonic-gate 		    (new_dscp << 20);
1767c478bd9Sstevel@tonic-gate #else
1777c478bd9Sstevel@tonic-gate 		ip6_hdr->ip6_vcf = (ip6_hdr->ip6_vcf & TCLASS_MASK) |
1787c478bd9Sstevel@tonic-gate 		    ((new_dscp >> 4) | ((new_dscp << 12) & 0xF000));
1797c478bd9Sstevel@tonic-gate #endif
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	return (0);
1837c478bd9Sstevel@tonic-gate }
184