tcp: Cache align ACK queue header.
[dragonfly.git] / lib / libstand / netif.c
blob2c20aa532747e4ee4d81ffd4a3d9a683203752ff
1 /* $NetBSD: netif.c,v 1.10 1997/09/06 13:57:14 drochner Exp $ */
3 /*
4 * Copyright (c) 1993 Adam Glass
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Adam Glass.
18 * 4. The name of the Author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY Adam Glass ``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.
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/mount.h>
37 #include <string.h>
39 #include <netinet/in.h>
40 #include <netinet/in_systm.h>
42 #include "stand.h"
43 #include "net.h"
44 #include "netif.h"
46 struct iodesc sockets[SOPEN_MAX];
47 #ifdef NETIF_DEBUG
48 int netif_debug = 0;
49 #endif
52 * netif_init:
54 * initialize the generic network interface layer
57 void
58 netif_init(void)
60 struct netif_driver *drv;
61 int d, i;
63 #ifdef NETIF_DEBUG
64 if (netif_debug)
65 printf("netif_init: called\n");
66 #endif
67 for (d = 0; netif_drivers[d]; d++) {
68 drv = netif_drivers[d];
69 for (i = 0; i < drv->netif_nifs; i++)
70 drv->netif_ifs[i].dif_used = 0;
74 int
75 netif_match(struct netif *nif, void *machdep_hint)
77 struct netif_driver *drv = nif->nif_driver;
79 #if 0
80 if (netif_debug)
81 printf("%s%d: netif_match (%d)\n", drv->netif_bname,
82 nif->nif_unit, nif->nif_sel);
83 #endif
84 return drv->netif_match(nif, machdep_hint);
87 struct netif *
88 netif_select(void *machdep_hint)
90 int d, u, s;
91 struct netif_driver *drv;
92 struct netif cur_if;
93 static struct netif best_if;
94 int best_val;
95 int val;
97 best_val = 0;
98 best_if.nif_driver = NULL;
100 for (d = 0; netif_drivers[d] != NULL; d++) {
101 cur_if.nif_driver = netif_drivers[d];
102 drv = cur_if.nif_driver;
104 for (u = 0; u < drv->netif_nifs; u++) {
105 cur_if.nif_unit = u;
107 #ifdef NETIF_DEBUG
108 if (netif_debug)
109 printf("\t%s%d:", drv->netif_bname,
110 cur_if.nif_unit);
111 #endif
113 for (s = 0; s < drv->netif_ifs[u].dif_nsel; s++) {
114 cur_if.nif_sel = s;
116 if (drv->netif_ifs[u].dif_used & (1 << s)) {
117 #ifdef NETIF_DEBUG
118 if (netif_debug)
119 printf(" [%d used]", s);
120 #endif
121 continue;
124 val = netif_match(&cur_if, machdep_hint);
125 #ifdef NETIF_DEBUG
126 if (netif_debug)
127 printf(" [%d -> %d]", s, val);
128 #endif
129 if (val > best_val) {
130 best_val = val;
131 best_if = cur_if;
134 #ifdef NETIF_DEBUG
135 if (netif_debug)
136 printf("\n");
137 #endif
141 if (best_if.nif_driver == NULL)
142 return NULL;
144 best_if.nif_driver->
145 netif_ifs[best_if.nif_unit].dif_used |= (1 << best_if.nif_sel);
147 #ifdef NETIF_DEBUG
148 if (netif_debug)
149 printf("netif_select: %s%d(%d) wins\n",
150 best_if.nif_driver->netif_bname,
151 best_if.nif_unit, best_if.nif_sel);
152 #endif
153 return &best_if;
157 netif_probe(struct netif *nif, void *machdep_hint)
159 struct netif_driver *drv = nif->nif_driver;
161 #ifdef NETIF_DEBUG
162 if (netif_debug)
163 printf("%s%d: netif_probe\n", drv->netif_bname, nif->nif_unit);
164 #endif
165 return drv->netif_probe(nif, machdep_hint);
168 void
169 netif_attach(struct netif *nif, struct iodesc *desc, void *machdep_hint)
171 struct netif_driver *drv = nif->nif_driver;
173 #ifdef NETIF_DEBUG
174 if (netif_debug)
175 printf("%s%d: netif_attach\n", drv->netif_bname, nif->nif_unit);
176 #endif
177 desc->io_netif = nif;
178 #ifdef PARANOID
179 if (drv->netif_init == NULL)
180 panic("%s%d: no netif_init support\n", drv->netif_bname,
181 nif->nif_unit);
182 #endif
183 drv->netif_init(desc, machdep_hint);
184 bzero(drv->netif_ifs[nif->nif_unit].dif_stats,
185 sizeof(struct netif_stats));
188 void
189 netif_detach(struct netif *nif)
191 struct netif_driver *drv = nif->nif_driver;
193 #ifdef NETIF_DEBUG
194 if (netif_debug)
195 printf("%s%d: netif_detach\n", drv->netif_bname, nif->nif_unit);
196 #endif
197 #ifdef PARANOID
198 if (drv->netif_end == NULL)
199 panic("%s%d: no netif_end support\n", drv->netif_bname,
200 nif->nif_unit);
201 #endif
202 drv->netif_end(nif);
205 ssize_t
206 netif_get(struct iodesc *desc, void *pkt, size_t len, time_t timo)
208 #ifdef NETIF_DEBUG
209 struct netif *nif = desc->io_netif;
210 #endif
211 struct netif_driver *drv = desc->io_netif->nif_driver;
212 ssize_t rv;
214 #ifdef NETIF_DEBUG
215 if (netif_debug)
216 printf("%s%d: netif_get\n", drv->netif_bname, nif->nif_unit);
217 #endif
218 #ifdef PARANOID
219 if (drv->netif_get == NULL)
220 panic("%s%d: no netif_get support\n", drv->netif_bname,
221 nif->nif_unit);
222 #endif
223 rv = drv->netif_get(desc, pkt, len, timo);
224 #ifdef NETIF_DEBUG
225 if (netif_debug)
226 printf("%s%d: netif_get returning %d\n", drv->netif_bname,
227 nif->nif_unit, (int)rv);
228 #endif
229 return rv;
232 ssize_t
233 netif_put(struct iodesc *desc, void *pkt, size_t len)
235 #ifdef NETIF_DEBUG
236 struct netif *nif = desc->io_netif;
237 #endif
238 struct netif_driver *drv = desc->io_netif->nif_driver;
239 ssize_t rv;
241 #ifdef NETIF_DEBUG
242 if (netif_debug)
243 printf("%s%d: netif_put\n", drv->netif_bname, nif->nif_unit);
244 #endif
245 #ifdef PARANOID
246 if (drv->netif_put == NULL)
247 panic("%s%d: no netif_put support\n", drv->netif_bname,
248 nif->nif_unit);
249 #endif
250 rv = drv->netif_put(desc, pkt, len);
251 #ifdef NETIF_DEBUG
252 if (netif_debug)
253 printf("%s%d: netif_put returning %d\n", drv->netif_bname,
254 nif->nif_unit, (int)rv);
255 #endif
256 return rv;
259 struct iodesc *
260 socktodesc(int sock)
262 if (sock >= SOPEN_MAX) {
263 errno = EBADF;
264 return (NULL);
266 return (&sockets[sock]);
270 netif_open(void *machdep_hint)
272 int fd;
273 struct iodesc *s;
274 struct netif *nif;
276 /* find a free socket */
277 for (fd = 0, s = sockets; fd < SOPEN_MAX; fd++, s++)
278 if (s->io_netif == NULL)
279 goto fnd;
280 errno = EMFILE;
281 return (-1);
283 fnd:
284 bzero(s, sizeof(*s));
285 netif_init();
286 nif = netif_select(machdep_hint);
287 if (!nif)
288 panic("netboot: no interfaces left untried");
289 if (netif_probe(nif, machdep_hint)) {
290 printf("netboot: couldn't probe %s%d\n",
291 nif->nif_driver->netif_bname, nif->nif_unit);
292 errno = EINVAL;
293 return(-1);
295 netif_attach(nif, s, machdep_hint);
297 return(fd);
301 netif_close(int sock)
303 if (sock >= SOPEN_MAX) {
304 errno = EBADF;
305 return(-1);
307 netif_detach(sockets[sock].io_netif);
308 sockets[sock].io_netif = NULL;
310 return(0);