xref: /dragonfly/sys/netproto/mpls/mpls_demux.c (revision 7b6b9c91)
19b42cabeSNuno Antunes /*
29b42cabeSNuno Antunes  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
39b42cabeSNuno Antunes  *
49b42cabeSNuno Antunes  * Redistribution and use in source and binary forms, with or without
59b42cabeSNuno Antunes  * modification, are permitted provided that the following conditions
69b42cabeSNuno Antunes  * are met:
79b42cabeSNuno Antunes  *
89b42cabeSNuno Antunes  * 1. Redistributions of source code must retain the above copyright
99b42cabeSNuno Antunes  *    notice, this list of conditions and the following disclaimer.
109b42cabeSNuno Antunes  * 2. Redistributions in binary form must reproduce the above copyright
119b42cabeSNuno Antunes  *    notice, this list of conditions and the following disclaimer in
129b42cabeSNuno Antunes  *    the documentation and/or other materials provided with the
139b42cabeSNuno Antunes  *    distribution.
149b42cabeSNuno Antunes  * 3. Neither the name of The DragonFly Project nor the names of its
159b42cabeSNuno Antunes  *    contributors may be used to endorse or promote products derived
169b42cabeSNuno Antunes  *    from this software without specific, prior written permission.
179b42cabeSNuno Antunes  *
189b42cabeSNuno Antunes  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
199b42cabeSNuno Antunes  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
209b42cabeSNuno Antunes  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
219b42cabeSNuno Antunes  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
229b42cabeSNuno Antunes  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
239b42cabeSNuno Antunes  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
249b42cabeSNuno Antunes  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
259b42cabeSNuno Antunes  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
269b42cabeSNuno Antunes  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
279b42cabeSNuno Antunes  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
289b42cabeSNuno Antunes  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b42cabeSNuno Antunes  * SUCH DAMAGE.
309b42cabeSNuno Antunes  *
31cb8d752cSNuno Antunes  * $DragonFly: src/sys/netproto/mpls/mpls_demux.c,v 1.2 2008/08/05 15:11:32 nant Exp $
329b42cabeSNuno Antunes  */
339b42cabeSNuno Antunes 
34*7b6b9c91SSepherosa Ziehau #include <sys/param.h>
359b42cabeSNuno Antunes #include <sys/mbuf.h>
369b42cabeSNuno Antunes 
379b42cabeSNuno Antunes #include <net/netisr.h>
389b42cabeSNuno Antunes #include <netinet/in_var.h>
399b42cabeSNuno Antunes 
409b42cabeSNuno Antunes #include <netproto/mpls/mpls.h>
419b42cabeSNuno Antunes #include <netproto/mpls/mpls_var.h>
429b42cabeSNuno Antunes 
439b42cabeSNuno Antunes static __inline int
MPLSP_MPORT_HASH(mpls_label_t label,u_short if_index)449b42cabeSNuno Antunes MPLSP_MPORT_HASH(mpls_label_t label, u_short if_index)
459b42cabeSNuno Antunes {
469b42cabeSNuno Antunes 	/* Use low order byte (demux up to 256 cpus) */
47*7b6b9c91SSepherosa Ziehau 	KASSERT(netisr_ncpus <= 256, ("need different hash function"));  /* XXX */
489b42cabeSNuno Antunes #if BYTE_ORDER == LITTLE_ENDIAN
499b42cabeSNuno Antunes 	label &= 0x00ff0000;
509b42cabeSNuno Antunes 	label = label >> 16;
519b42cabeSNuno Antunes #endif
52*7b6b9c91SSepherosa Ziehau         return ((label ^ if_index) % netisr_ncpus);
539b42cabeSNuno Antunes }
549b42cabeSNuno Antunes 
55c3c96e44SMatthew Dillon static void
mpls_lengthcheck(struct mbuf ** mp,int hoff)56c3c96e44SMatthew Dillon mpls_lengthcheck(struct mbuf **mp, int hoff)
579b42cabeSNuno Antunes {
589b42cabeSNuno Antunes 	struct mbuf *m = *mp;
59c3c96e44SMatthew Dillon 	int hlen = hoff + sizeof(struct mpls);
609b42cabeSNuno Antunes 
619b42cabeSNuno Antunes 	/* The packet must be at least the size of an MPLS header. */
62c3c96e44SMatthew Dillon 	if (m->m_pkthdr.len < hlen) {
639b42cabeSNuno Antunes 		mplsstat.mplss_tooshort++;
649b42cabeSNuno Antunes 		m_free(m);
65c3c96e44SMatthew Dillon 		*mp = NULL;
66c3c96e44SMatthew Dillon 		return;
679b42cabeSNuno Antunes 	}
689b42cabeSNuno Antunes 
699b42cabeSNuno Antunes 	/* The MPLS header must reside completely in the first mbuf. */
70c3c96e44SMatthew Dillon 	if (m->m_len < hlen) {
71c3c96e44SMatthew Dillon 		m = m_pullup(m, hlen);
729b42cabeSNuno Antunes 		if (m == NULL) {
739b42cabeSNuno Antunes 			mplsstat.mplss_toosmall++;
74c3c96e44SMatthew Dillon 			*mp = NULL;
75c3c96e44SMatthew Dillon 			return;
769b42cabeSNuno Antunes 		}
779b42cabeSNuno Antunes 	}
789b42cabeSNuno Antunes 	*mp = m;
799b42cabeSNuno Antunes }
809b42cabeSNuno Antunes 
81c3c96e44SMatthew Dillon void
mpls_hashfn(struct mbuf ** mp,int hoff)82ca86d83eSSepherosa Ziehau mpls_hashfn(struct mbuf **mp, int hoff)
839b42cabeSNuno Antunes {
849b42cabeSNuno Antunes 	struct mbuf *m = *mp;
859b42cabeSNuno Antunes 	struct mpls *mpls;
869b42cabeSNuno Antunes 	mpls_label_t label;
879b42cabeSNuno Antunes 	struct ifnet *ifp;
889b42cabeSNuno Antunes 
89c3c96e44SMatthew Dillon 	mpls_lengthcheck(mp, hoff);
90c3c96e44SMatthew Dillon 	if ((m = *mp) == NULL)
91c3c96e44SMatthew Dillon 		return;
929b42cabeSNuno Antunes 
93c3c96e44SMatthew Dillon 	mpls = mtodoff(m, struct mpls *, hoff);
949b42cabeSNuno Antunes 
959b42cabeSNuno Antunes 	label = MPLS_LABEL(ntohl(mpls->mpls_shim));
969b42cabeSNuno Antunes 	ifp = m->m_pkthdr.rcvif;
977558541bSSepherosa Ziehau 	m_sethash(m, MPLSP_MPORT_HASH(label, ifp->if_index));
989b42cabeSNuno Antunes }
99