15337421cSSepherosa Ziehau /*
25337421cSSepherosa Ziehau * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
35337421cSSepherosa Ziehau * Copyright (c) 2004 The DragonFly Project. All rights reserved.
45337421cSSepherosa Ziehau *
55337421cSSepherosa Ziehau * This code is derived from software contributed to The DragonFly Project
65337421cSSepherosa Ziehau * by Jeffrey M. Hsu.
75337421cSSepherosa Ziehau *
85337421cSSepherosa Ziehau * Redistribution and use in source and binary forms, with or without
95337421cSSepherosa Ziehau * modification, are permitted provided that the following conditions
105337421cSSepherosa Ziehau * are met:
115337421cSSepherosa Ziehau * 1. Redistributions of source code must retain the above copyright
125337421cSSepherosa Ziehau * notice, this list of conditions and the following disclaimer.
135337421cSSepherosa Ziehau * 2. Redistributions in binary form must reproduce the above copyright
145337421cSSepherosa Ziehau * notice, this list of conditions and the following disclaimer in the
155337421cSSepherosa Ziehau * documentation and/or other materials provided with the distribution.
165337421cSSepherosa Ziehau * 3. Neither the name of The DragonFly Project nor the names of its
175337421cSSepherosa Ziehau * contributors may be used to endorse or promote products derived
185337421cSSepherosa Ziehau * from this software without specific, prior written permission.
195337421cSSepherosa Ziehau *
205337421cSSepherosa Ziehau * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
215337421cSSepherosa Ziehau * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
225337421cSSepherosa Ziehau * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
235337421cSSepherosa Ziehau * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
245337421cSSepherosa Ziehau * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
255337421cSSepherosa Ziehau * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
265337421cSSepherosa Ziehau * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
275337421cSSepherosa Ziehau * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
285337421cSSepherosa Ziehau * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
295337421cSSepherosa Ziehau * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
305337421cSSepherosa Ziehau * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315337421cSSepherosa Ziehau * SUCH DAMAGE.
325337421cSSepherosa Ziehau */
335337421cSSepherosa Ziehau
345337421cSSepherosa Ziehau /*
355337421cSSepherosa Ziehau * Copyright (c) 1980, 1986, 1989, 1993
365337421cSSepherosa Ziehau * The Regents of the University of California. All rights reserved.
375337421cSSepherosa Ziehau *
385337421cSSepherosa Ziehau * Redistribution and use in source and binary forms, with or without
395337421cSSepherosa Ziehau * modification, are permitted provided that the following conditions
405337421cSSepherosa Ziehau * are met:
415337421cSSepherosa Ziehau * 1. Redistributions of source code must retain the above copyright
425337421cSSepherosa Ziehau * notice, this list of conditions and the following disclaimer.
435337421cSSepherosa Ziehau * 2. Redistributions in binary form must reproduce the above copyright
445337421cSSepherosa Ziehau * notice, this list of conditions and the following disclaimer in the
455337421cSSepherosa Ziehau * documentation and/or other materials provided with the distribution.
462c64e990Szrj * 3. Neither the name of the University nor the names of its contributors
475337421cSSepherosa Ziehau * may be used to endorse or promote products derived from this software
485337421cSSepherosa Ziehau * without specific prior written permission.
495337421cSSepherosa Ziehau *
505337421cSSepherosa Ziehau * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
515337421cSSepherosa Ziehau * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
525337421cSSepherosa Ziehau * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
535337421cSSepherosa Ziehau * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
545337421cSSepherosa Ziehau * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
555337421cSSepherosa Ziehau * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
565337421cSSepherosa Ziehau * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
575337421cSSepherosa Ziehau * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
585337421cSSepherosa Ziehau * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
595337421cSSepherosa Ziehau * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
605337421cSSepherosa Ziehau * SUCH DAMAGE.
615337421cSSepherosa Ziehau *
625337421cSSepherosa Ziehau * @(#)netisr.h 8.1 (Berkeley) 6/10/93
635337421cSSepherosa Ziehau * $FreeBSD: src/sys/net/netisr.h,v 1.21.2.5 2002/02/09 23:02:39 luigi Exp $
645337421cSSepherosa Ziehau */
655337421cSSepherosa Ziehau
665337421cSSepherosa Ziehau #ifndef _NET_NETISR2_H_
675337421cSSepherosa Ziehau #define _NET_NETISR2_H_
685337421cSSepherosa Ziehau
695337421cSSepherosa Ziehau #ifndef _KERNEL
705337421cSSepherosa Ziehau #error "kernel only header file"
715337421cSSepherosa Ziehau #endif
725337421cSSepherosa Ziehau
73b42f202aSSepherosa Ziehau #include <sys/systm.h>
7492b34312SSepherosa Ziehau #include <sys/msgport2.h>
755337421cSSepherosa Ziehau #include <sys/thread.h>
765337421cSSepherosa Ziehau #include <net/netisr.h>
775337421cSSepherosa Ziehau
786617c2d1SSepherosa Ziehau extern struct thread *netisr_threads[MAXCPU];
795337421cSSepherosa Ziehau
805337421cSSepherosa Ziehau /*
815337421cSSepherosa Ziehau * Return the message port for the general protocol message servicing
825337421cSSepherosa Ziehau * thread for a particular cpu.
835337421cSSepherosa Ziehau */
845337421cSSepherosa Ziehau static __inline lwkt_port_t
netisr_cpuport(int cpu)855337421cSSepherosa Ziehau netisr_cpuport(int cpu)
865337421cSSepherosa Ziehau {
875337421cSSepherosa Ziehau KKASSERT(cpu >= 0 && cpu < ncpus);
886617c2d1SSepherosa Ziehau return &netisr_threads[cpu]->td_msgport;
895337421cSSepherosa Ziehau }
905337421cSSepherosa Ziehau
915337421cSSepherosa Ziehau /*
925337421cSSepherosa Ziehau * Return the current cpu's network protocol thread.
935337421cSSepherosa Ziehau */
945337421cSSepherosa Ziehau static __inline lwkt_port_t
netisr_curport(void)955337421cSSepherosa Ziehau netisr_curport(void)
965337421cSSepherosa Ziehau {
975337421cSSepherosa Ziehau return netisr_cpuport(mycpuid);
985337421cSSepherosa Ziehau }
995337421cSSepherosa Ziehau
100b42f202aSSepherosa Ziehau /*
101a1fe5debSSepherosa Ziehau * Return the LSB of the hash.
102a1fe5debSSepherosa Ziehau */
103a1fe5debSSepherosa Ziehau static __inline uint32_t
netisr_hashlsb(uint32_t hash)104a1fe5debSSepherosa Ziehau netisr_hashlsb(uint32_t hash)
105a1fe5debSSepherosa Ziehau {
106a1fe5debSSepherosa Ziehau
107a1fe5debSSepherosa Ziehau return (hash & NETISR_CPUMASK);
108a1fe5debSSepherosa Ziehau }
109a1fe5debSSepherosa Ziehau
110a1fe5debSSepherosa Ziehau /*
111eaea30e3SSepherosa Ziehau * Return the cpu for the hash.
112eaea30e3SSepherosa Ziehau */
113eaea30e3SSepherosa Ziehau static __inline int
netisr_hashcpu(uint16_t hash)114eaea30e3SSepherosa Ziehau netisr_hashcpu(uint16_t hash)
115eaea30e3SSepherosa Ziehau {
116a1fe5debSSepherosa Ziehau
117a1fe5debSSepherosa Ziehau return (netisr_hashlsb(hash) % netisr_ncpus);
118eaea30e3SSepherosa Ziehau }
119eaea30e3SSepherosa Ziehau
120eaea30e3SSepherosa Ziehau /*
121b42f202aSSepherosa Ziehau * Return the message port for the general protocol message servicing
122b42f202aSSepherosa Ziehau * thread for the hash.
123b42f202aSSepherosa Ziehau */
124b42f202aSSepherosa Ziehau static __inline lwkt_port_t
netisr_hashport(uint16_t hash)125b42f202aSSepherosa Ziehau netisr_hashport(uint16_t hash)
126b42f202aSSepherosa Ziehau {
127eaea30e3SSepherosa Ziehau return netisr_cpuport(netisr_hashcpu(hash));
128b42f202aSSepherosa Ziehau }
129b42f202aSSepherosa Ziehau
1305204e13cSSepherosa Ziehau #define IN_NETISR(n) \
1315204e13cSSepherosa Ziehau (&curthread->td_msgport == netisr_cpuport((n)))
1325204e13cSSepherosa Ziehau #define IN_NETISR_NCPUS(n) \
1335204e13cSSepherosa Ziehau ((n) < netisr_ncpus && IN_NETISR((n)))
1345204e13cSSepherosa Ziehau #define ASSERT_NETISR0 \
1355204e13cSSepherosa Ziehau KASSERT(IN_NETISR(0), ("thread %p is not netisr0", curthread))
1365204e13cSSepherosa Ziehau #define ASSERT_NETISR_NCPUS(n) \
1375204e13cSSepherosa Ziehau KASSERT(IN_NETISR_NCPUS(n), \
138016fb59eSSepherosa Ziehau ("thread %p cpu%d is not within netisr_ncpus %d", \
1395204e13cSSepherosa Ziehau curthread, (n), netisr_ncpus))
1409763404fSSepherosa Ziehau
14192b34312SSepherosa Ziehau static __inline int
netisr_domsg_port(struct netmsg_base * nm,lwkt_port_t port)14292b34312SSepherosa Ziehau netisr_domsg_port(struct netmsg_base *nm, lwkt_port_t port)
14392b34312SSepherosa Ziehau {
14492b34312SSepherosa Ziehau
14592b34312SSepherosa Ziehau #ifdef INVARIANTS
1465204e13cSSepherosa Ziehau /*
1475204e13cSSepherosa Ziehau * Only netisr0, netisrN itself, or non-netisr threads
1485204e13cSSepherosa Ziehau * can perform synchronous message sending to netisrN.
1495204e13cSSepherosa Ziehau */
1505204e13cSSepherosa Ziehau KASSERT(curthread->td_type != TD_TYPE_NETISR ||
1515204e13cSSepherosa Ziehau IN_NETISR(0) || port == &curthread->td_msgport,
1525204e13cSSepherosa Ziehau ("can't domsg to netisr port %p from thread %p", port, curthread));
15392b34312SSepherosa Ziehau #endif
15492b34312SSepherosa Ziehau return (lwkt_domsg(port, &nm->lmsg, 0));
15592b34312SSepherosa Ziehau }
15692b34312SSepherosa Ziehau
15792b34312SSepherosa Ziehau static __inline int
netisr_domsg(struct netmsg_base * nm,int cpu)15892b34312SSepherosa Ziehau netisr_domsg(struct netmsg_base *nm, int cpu)
15992b34312SSepherosa Ziehau {
16092b34312SSepherosa Ziehau
16192b34312SSepherosa Ziehau return (netisr_domsg_port(nm, netisr_cpuport(cpu)));
16292b34312SSepherosa Ziehau }
16392b34312SSepherosa Ziehau
16443dbcc2aSSepherosa Ziehau static __inline int
netisr_domsg_global(struct netmsg_base * nm)16543dbcc2aSSepherosa Ziehau netisr_domsg_global(struct netmsg_base *nm)
16643dbcc2aSSepherosa Ziehau {
16743dbcc2aSSepherosa Ziehau
16843dbcc2aSSepherosa Ziehau /* Start from netisr0. */
16943dbcc2aSSepherosa Ziehau return (netisr_domsg(nm, 0));
17043dbcc2aSSepherosa Ziehau }
17143dbcc2aSSepherosa Ziehau
17292b34312SSepherosa Ziehau static __inline void
netisr_sendmsg(struct netmsg_base * nm,int cpu)173deae8bb6SSepherosa Ziehau netisr_sendmsg(struct netmsg_base *nm, int cpu)
174deae8bb6SSepherosa Ziehau {
175deae8bb6SSepherosa Ziehau
176deae8bb6SSepherosa Ziehau lwkt_sendmsg(netisr_cpuport(cpu), &nm->lmsg);
177deae8bb6SSepherosa Ziehau }
178deae8bb6SSepherosa Ziehau
179deae8bb6SSepherosa Ziehau static __inline void
netisr_sendmsg_oncpu(struct netmsg_base * nm)18092deb165SSepherosa Ziehau netisr_sendmsg_oncpu(struct netmsg_base *nm)
18192deb165SSepherosa Ziehau {
18292deb165SSepherosa Ziehau
18392deb165SSepherosa Ziehau lwkt_sendmsg_oncpu(netisr_cpuport(mycpuid), &nm->lmsg);
18492deb165SSepherosa Ziehau }
18592deb165SSepherosa Ziehau
18692deb165SSepherosa Ziehau static __inline void
netisr_replymsg(struct netmsg_base * nm,int error)18792b34312SSepherosa Ziehau netisr_replymsg(struct netmsg_base *nm, int error)
18892b34312SSepherosa Ziehau {
18992b34312SSepherosa Ziehau
19092b34312SSepherosa Ziehau lwkt_replymsg(&nm->lmsg, error);
19192b34312SSepherosa Ziehau }
19292b34312SSepherosa Ziehau
193*de844fedSSepherosa Ziehau static __inline void
netisr_dropmsg(struct netmsg_base * nm)194*de844fedSSepherosa Ziehau netisr_dropmsg(struct netmsg_base *nm)
195*de844fedSSepherosa Ziehau {
196*de844fedSSepherosa Ziehau
197*de844fedSSepherosa Ziehau lwkt_dropmsg(&nm->lmsg);
198*de844fedSSepherosa Ziehau }
199*de844fedSSepherosa Ziehau
200c3b4f1bfSSepherosa Ziehau /*
201c3b4f1bfSSepherosa Ziehau * To all netisrs, instead of netisr_ncpus.
202c3b4f1bfSSepherosa Ziehau */
20392b34312SSepherosa Ziehau static __inline void
netisr_forwardmsg_all(struct netmsg_base * nm,int next_cpu)204c3b4f1bfSSepherosa Ziehau netisr_forwardmsg_all(struct netmsg_base *nm, int next_cpu)
20592b34312SSepherosa Ziehau {
20692b34312SSepherosa Ziehau
20792b34312SSepherosa Ziehau KKASSERT(next_cpu > mycpuid && next_cpu <= ncpus);
20892b34312SSepherosa Ziehau if (next_cpu < ncpus)
20992b34312SSepherosa Ziehau lwkt_forwardmsg(netisr_cpuport(next_cpu), &nm->lmsg);
21092b34312SSepherosa Ziehau else
21192b34312SSepherosa Ziehau netisr_replymsg(nm, 0);
21292b34312SSepherosa Ziehau }
21392b34312SSepherosa Ziehau
214016fb59eSSepherosa Ziehau /*
215016fb59eSSepherosa Ziehau * To netisr_ncpus.
216016fb59eSSepherosa Ziehau */
217016fb59eSSepherosa Ziehau static __inline void
netisr_forwardmsg_error(struct netmsg_base * nm,int next_cpu,int error)21843dbcc2aSSepherosa Ziehau netisr_forwardmsg_error(struct netmsg_base *nm, int next_cpu,
21943dbcc2aSSepherosa Ziehau int error)
220016fb59eSSepherosa Ziehau {
221016fb59eSSepherosa Ziehau
222016fb59eSSepherosa Ziehau KKASSERT(next_cpu > mycpuid && next_cpu <= netisr_ncpus);
223016fb59eSSepherosa Ziehau if (next_cpu < netisr_ncpus)
224016fb59eSSepherosa Ziehau lwkt_forwardmsg(netisr_cpuport(next_cpu), &nm->lmsg);
225016fb59eSSepherosa Ziehau else
22643dbcc2aSSepherosa Ziehau netisr_replymsg(nm, error);
22743dbcc2aSSepherosa Ziehau }
22843dbcc2aSSepherosa Ziehau
22943dbcc2aSSepherosa Ziehau /*
23043dbcc2aSSepherosa Ziehau * To netisr_ncpus.
23143dbcc2aSSepherosa Ziehau */
23243dbcc2aSSepherosa Ziehau static __inline void
netisr_forwardmsg(struct netmsg_base * nm,int next_cpu)23343dbcc2aSSepherosa Ziehau netisr_forwardmsg(struct netmsg_base *nm, int next_cpu)
23443dbcc2aSSepherosa Ziehau {
23543dbcc2aSSepherosa Ziehau
23643dbcc2aSSepherosa Ziehau netisr_forwardmsg_error(nm, next_cpu, 0);
237016fb59eSSepherosa Ziehau }
238016fb59eSSepherosa Ziehau
2395337421cSSepherosa Ziehau #endif /* _NET_NETISR2_H_ */
240