Reduce ifnet.if_serializer contention on output path:
[dragonfly.git] / sys / netproto / ns / ns_output.c
1 /*
2  * Copyright (c) 1984, 1985, 1986, 1987, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)ns_output.c 8.1 (Berkeley) 6/10/93
34  * $FreeBSD: src/sys/netns/ns_output.c,v 1.7 1999/08/28 00:49:51 peter Exp $
35  * $DragonFly: src/sys/netproto/ns/ns_output.c,v 1.9 2008/05/14 11:59:24 sephe Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/errno.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44
45 #include <net/if.h>
46 #include <net/route.h>
47
48 #include "ns.h"
49 #include "ns_if.h"
50 #include "idp.h"
51 #include "idp_var.h"
52
53 int ns_hold_output = 0;
54 int ns_copy_output = 0;
55 int ns_output_cnt = 0;
56 struct mbuf *ns_lastout;
57
58 int
59 ns_output(struct mbuf *m0, struct route *ro, int flags)
60 {
61         struct idp *idp = mtod(m0, struct idp *);
62         struct ifnet *ifp = 0;
63         int error = 0;
64         struct route idproute;
65         struct sockaddr_ns *dst;
66
67         if (ns_hold_output) {
68                 if (ns_lastout) {
69                         m_free(ns_lastout);
70                 }
71                 ns_lastout = m_copy(m0, 0, (int)M_COPYALL);
72         }
73         /*
74          * Route packet.
75          */
76         if (ro == 0) {
77                 ro = &idproute;
78                 bzero((caddr_t)ro, sizeof (*ro));
79         }
80         dst = (struct sockaddr_ns *)&ro->ro_dst;
81         if (ro->ro_rt == 0) {
82                 dst->sns_family = AF_NS;
83                 dst->sns_len = sizeof (*dst);
84                 dst->sns_addr = idp->idp_dna;
85                 dst->sns_addr.x_port = 0;
86                 /*
87                  * If routing to interface only,
88                  * short circuit routing lookup.
89                  */
90                 if (flags & NS_ROUTETOIF) {
91                         struct ns_ifaddr *ia = ns_iaonnetof(&idp->idp_dna);
92
93                         if (ia == 0) {
94                                 error = ENETUNREACH;
95                                 goto bad;
96                         }
97                         ifp = ia->ia_ifp;
98                         goto gotif;
99                 }
100                 rtalloc(ro);
101         } else if ((ro->ro_rt->rt_flags & RTF_UP) == 0) {
102                 /*
103                  * The old route has gone away; try for a new one.
104                  */
105                 rtfree(ro->ro_rt);
106                 ro->ro_rt = NULL;
107                 rtalloc(ro);
108         }
109         if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) {
110                 error = ENETUNREACH;
111                 goto bad;
112         }
113         ro->ro_rt->rt_use++;
114         if (ro->ro_rt->rt_flags & (RTF_GATEWAY|RTF_HOST))
115                 dst = (struct sockaddr_ns *)ro->ro_rt->rt_gateway;
116 gotif:
117
118         /*
119          * Look for multicast addresses and
120          * and verify user is allowed to send
121          * such a packet.
122          */
123         if (dst->sns_addr.x_host.c_host[0]&1) {
124                 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
125                         error = EADDRNOTAVAIL;
126                         goto bad;
127                 }
128                 if ((flags & NS_ALLOWBROADCAST) == 0) {
129                         error = EACCES;
130                         goto bad;
131                 }
132         }
133
134         if (htons(idp->idp_len) <= ifp->if_mtu) {
135                 ns_output_cnt++;
136                 if (ns_copy_output) {
137                         ns_watch_output(m0, ifp);
138                 }
139                 error = ifp->if_output(ifp, m0, (struct sockaddr *)dst,
140                                        ro->ro_rt);
141                 goto done;
142         } else error = EMSGSIZE;
143
144
145 bad:
146         if (ns_copy_output) {
147                 ns_watch_output(m0, ifp);
148         }
149         m_freem(m0);
150 done:
151         if (ro == &idproute && (flags & NS_ROUTETOIF) == 0 && ro->ro_rt) {
152                 RTFREE(ro->ro_rt);
153                 ro->ro_rt = 0;
154         }
155         return (error);
156 }