FBSD: StopListener glocking fixup
[openafs.git] / src / rx / FBSD / rx_knet.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include "afs/param.h"
12
13
14 #include <sys/malloc.h>
15 #include "rx/rx_kcommon.h"
16
17 #ifdef RXK_LISTENER_ENV
18 int
19 osi_NetReceive(osi_socket asocket, struct sockaddr_in *addr,
20                struct iovec *dvec, int nvecs, int *alength)
21 {
22     struct uio u;
23     int i;
24     struct iovec iov[RX_MAXIOVECS];
25     struct sockaddr *sa = NULL;
26     int code;
27
28     int haveGlock = ISAFS_GLOCK();
29     /*AFS_STATCNT(osi_NetReceive); */
30
31     if (nvecs > RX_MAXIOVECS)
32         osi_Panic("osi_NetReceive: %d: Too many iovecs.\n", nvecs);
33
34     for (i = 0; i < nvecs; i++)
35         iov[i] = dvec[i];
36
37     u.uio_iov = &iov[0];
38     u.uio_iovcnt = nvecs;
39     u.uio_offset = 0;
40     u.uio_resid = *alength;
41     u.uio_segflg = UIO_SYSSPACE;
42     u.uio_rw = UIO_READ;
43     u.uio_td = NULL;
44
45     if (haveGlock)
46         AFS_GUNLOCK();
47     code = soreceive(asocket, &sa, &u, NULL, NULL, NULL);
48     if (haveGlock)
49         AFS_GLOCK();
50
51     if (code) {
52 #if KNET_DEBUG
53         if (code == EINVAL)
54             Debugger("afs NetReceive busted");
55         else
56             printf("y");
57 #else
58         return code;
59 #endif
60     }
61     *alength -= u.uio_resid;
62     if (sa) {
63         if (sa->sa_family == AF_INET) {
64             if (addr)
65                 *addr = *(struct sockaddr_in *)sa;
66         } else
67             printf("Unknown socket family %d in NetReceive\n", sa->sa_family);
68         FREE(sa, M_SONAME);
69     }
70     return code;
71 }
72
73 extern int rxk_ListenerPid;
74 void
75 osi_StopListener(void)
76 {
77     struct sockaddr_in taddr;
78     struct iovec dvec;
79     struct proc *p;
80     char c;
81     c = '\0';
82
83     /*
84      * Have to drop global lock to safely do this.
85      * soclose() is currently protected by Giant,
86      * but pfind and psignal are MPSAFE.
87      */
88     int haveGlock = ISAFS_GLOCK();
89     if (haveGlock)
90         AFS_GUNLOCK();
91     soshutdown(rx_socket, SHUT_RDWR);
92     p = pfind(rxk_ListenerPid);
93     if (p) {
94         afs_warn("osi_StopListener: rxk_ListenerPid %u\n", rxk_ListenerPid);
95         psignal(p, SIGUSR1);
96         PROC_UNLOCK(p);
97     } else
98         afs_warn("osi_StopListener: rxk_Listener not found (pid %u)\n",
99             rxk_ListenerPid);
100
101     /* Avoid destroying socket until osi_NetReceive has
102     * had a chance to clean up.  Otherwise we can't restart. */
103     bzero(&taddr, sizeof(taddr));
104     taddr.sin_len = sizeof(struct sockaddr_in);
105     taddr.sin_family = AF_INET;
106     taddr.sin_port = rx_port;
107     taddr.sin_addr.s_addr = htonl(0x7f000001);  /* no place like localhost */
108     bzero(&dvec, sizeof(dvec));
109     dvec.iov_base = &c;
110     dvec.iov_len = 1;
111     /* afs_osi_Sleep requires the GLOCK */
112     AFS_GLOCK();
113     while(rxk_ListenerPid) {
114         afs_warn("waiting for rxk_ListenerPid to die\n");
115         osi_NetSend(rx_socket, &taddr, &dvec, 1, 1, 0);
116         afs_osi_Sleep(&rxk_ListenerPid);
117     }
118     AFS_GUNLOCK();
119     /* in theory, we are now the only people doing anything with rx_socket */
120     soclose(rx_socket);
121
122     if (haveGlock)
123         AFS_GLOCK();
124 }
125
126 int
127 osi_NetSend(osi_socket asocket, struct sockaddr_in *addr, struct iovec *dvec,
128             int nvecs, afs_int32 alength, int istack)
129 {
130     afs_int32 code;
131     int i;
132     struct iovec iov[RX_MAXIOVECS];
133     struct uio u;
134     int haveGlock = ISAFS_GLOCK();
135
136     AFS_STATCNT(osi_NetSend);
137     if (nvecs > RX_MAXIOVECS)
138         osi_Panic("osi_NetSend: %d: Too many iovecs.\n", nvecs);
139
140     for (i = 0; i < nvecs; i++)
141         iov[i] = dvec[i];
142
143     u.uio_iov = &iov[0];
144     u.uio_iovcnt = nvecs;
145     u.uio_offset = 0;
146     u.uio_resid = alength;
147     u.uio_segflg = UIO_SYSSPACE;
148     u.uio_rw = UIO_WRITE;
149     u.uio_td = NULL;
150
151     addr->sin_len = sizeof(struct sockaddr_in);
152
153     if (haveGlock)
154         AFS_GUNLOCK();
155 #if KNET_DEBUG
156     printf("+");
157 #endif
158     code =
159         sosend(asocket, (struct sockaddr *)addr, &u, NULL, NULL, 0,
160                curthread);
161 #if KNET_DEBUG
162     if (code) {
163         if (code == EINVAL)
164             Debugger("afs NetSend busted");
165         else
166             printf("z");
167     }
168 #endif
169     if (haveGlock)
170         AFS_GLOCK();
171     return code;
172 }
173 #else
174 /* This code *almost* works :( */
175 static struct protosw parent_proto;     /* udp proto switch */
176 static void rxk_input(struct mbuf *am, int iphlen);
177 static void rxk_fasttimo(void);
178
179 /* start intercepting basic calls */
180 rxk_init()
181 {
182     struct protosw *tpro, *last;
183     if (rxk_initDone)
184         return 0;
185
186     last = inetdomain.dom_protoswNPROTOSW;
187     for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
188         if (tpro->pr_protocol == IPPROTO_UDP) {
189 #if 0                           /* not exported */
190             /* force UDP checksumming on for AFS    */
191             extern int udpcksum;
192             udpcksum = 1;
193 #endif
194             memcpy(&parent_proto, tpro, sizeof(parent_proto));
195             tpro->pr_input = rxk_input;
196             tpro->pr_fasttimo = rxk_fasttimo;
197             /*
198              * don't bother with pr_drain and pr_ctlinput
199              * until we have something to do
200              */
201             rxk_initDone = 1;
202             return 0;
203         }
204     osi_Panic("inet:no udp");
205 }
206
207
208 static void
209 rxk_input(struct mbuf *am, int iphlen)
210 {
211     void (*tproc) ();
212     unsigned short *tsp;
213     int hdr;
214     struct udphdr *tu;
215     struct ip *ti;
216     struct udpiphdr *tvu;
217     int i;
218     char *phandle;
219     afs_int32 code;
220     struct sockaddr_in taddr;
221     int tlen;
222     short port;
223     int data_len, comp_sum;
224
225     SPLVAR;
226     NETPRI;
227
228     /* make sure we have base ip and udp headers in first mbuf */
229     if (iphlen > sizeof(struct ip)) {
230         ip_stripoptions(am, NULL);
231         iphlen = sizeof(struct ip);
232     }
233
234     if (am->m_len < sizeof(struct udpiphdr)) {
235         am = m_pullup(am, sizeof(struct udpiphdr));
236         if (!am) {
237             USERPRI;
238             return;
239         }
240     }
241
242     ti = mtod(am, struct ip *);
243     /* skip basic ip hdr */
244     tu = (struct udphdr *)(((char *)ti) + sizeof(struct ip));
245
246     /* now read the port out */
247     port = tu->uh_dport;
248
249     if (port) {
250         for (tsp = rxk_ports, i = 0; i < MAXRXPORTS; i++) {
251             if (*tsp++ == port) {
252                 /* checksum the packet */
253                 /*
254                  * Make mbuf data length reflect UDP length.
255                  * If not enough data to reflect UDP length, drop.
256                  */
257                 tvu = (struct udpiphdr *)ti;
258                 tlen = ntohs((u_short) tvu->ui_ulen);
259                 if ((int)ti->ip_len != tlen) {
260                     if (tlen > (int)ti->ip_len) {
261                         m_free(am);
262                         USERPRI;
263                         return;
264                     }
265                     m_adj(am, tlen - (int)ti->ip_len);
266                 }
267                 /* deliver packet to rx */
268                 taddr.sin_family = AF_INET;     /* compute source address */
269                 taddr.sin_port = tu->uh_sport;
270                 taddr.sin_addr.s_addr = ti->ip_src.s_addr;
271                 taddr.sin_len = sizeof(taddr);
272                 tvu = (struct udpiphdr *)ti;    /* virtual udp structure, for cksum */
273                 /* handle the checksum.  Note that this code damages the actual ip
274                  * header (replacing it with the virtual one, which is the same size),
275                  * so we must ensure we get everything out we need, first */
276                 if (tu->uh_sum != 0) {
277                     /* if the checksum is there, always check it. It's crazy not
278                      * to, unless you can really be sure that your
279                      * underlying network (and interfaces and drivers and
280                      * DMA hardware, etc!) is error-free. First, fill
281                      * in entire virtual ip header. */
282                     memset(tvu->ui_i.ih_x1, 0, 9);
283                     tvu->ui_len = tvu->ui_ulen;
284                     tlen = ntohs((unsigned short)(tvu->ui_ulen));
285                     if (in_cksum(am, sizeof(struct ip) + tlen)) {
286                         /* checksum, including cksum field, doesn't come out 0, so
287                          * this packet is bad */
288                         m_freem(am);
289                         USERPRI;
290                         return;
291                     }
292                 }
293
294                 /*
295                  * 28 is IP (20) + UDP (8) header.  ulen includes
296                  * udp header, and we *don't* tell RX about udp
297                  * header either.  So, we remove those 8 as well.
298                  */
299                 data_len = ntohs(tu->uh_ulen);
300                 data_len -= 8;
301                 if (!(*rxk_GetPacketProc) (&phandle, data_len)) {
302                     if (rx_mb_to_packet(am, m_freem, 28, data_len, phandle)) {
303                         /* XXX should just increment counter here.. */
304                         printf("rx: truncated UDP packet\n");
305                         rxi_FreePacket(phandle);
306                     } else
307                         (*rxk_PacketArrivalProc) (phandle, &taddr,
308                                                   rxk_portRocks[i], data_len);
309                 } else
310                     m_freem(am);
311                 USERPRI;
312                 return;
313             }
314         }
315     }
316
317     /* if we get here, try to deliver packet to udp */
318     if (tproc = parent_proto.pr_input)
319         (*tproc) (am, iphlen);
320     USERPRI;
321     return;
322 }
323
324
325 /* 
326  * UDP fast timer to raise events for all but Solaris and NCR. 
327  * Called about 5 times per second (at unknown priority?).  Must go to
328  * splnet or obtain global lock before touching anything significant.
329  */
330 static void
331 rxk_fasttimo(void)
332 {
333     void (*tproc) ();
334     struct clock temp;
335
336     /* do rx fasttimo processing here */
337     rxevent_RaiseEvents(&temp);
338     if (tproc = parent_proto.pr_fasttimo)
339         (*tproc) ();
340 }
341
342 /* rx_NetSend - send asize bytes at adata from asocket to host at addr.
343  *
344  * Now, why do we allocate a new buffer when we could theoretically use the one
345  * pointed to by adata?  Because PRU_SEND returns after queueing the message,
346  * not after sending it.  If the sender changes the data after queueing it,
347  * we'd see the already-queued data change.  One attempt to fix this without
348  * adding a copy would be to have this function wait until the datagram is
349  * sent; however this doesn't work well.  In particular, if a host is down, and
350  * an ARP fails to that host, this packet will be queued until the ARP request
351  * comes back, which could be hours later.  We can't block in this routine that
352  * long, since it prevents RPC timeouts from happening.
353  */
354 /* XXX In the brave new world, steal the data bufs out of the rx_packet iovec,
355  * and just queue those.  XXX
356  */
357
358 /* set lock on sockbuf sb; can't call sblock since we're at interrupt level
359  * sometimes */
360 static
361 trysblock(sb)
362      struct sockbuf *sb;
363 {
364     AFS_STATCNT(trysblock);
365     if (sb->sb_flags & SB_LOCK) {
366         return -1;              /* can't lock socket */
367     }
368     sb->sb_flags |= SB_LOCK;
369     return 0;
370 }
371
372 /* We only have to do all the mbuf management ourselves if we can be called at
373    interrupt time. in RXK_LISTENER_ENV, we can just call sosend() */
374 int
375 osi_NetSend(osi_socket asocket, struct sockaddr_in *addr, struct iovec *dvec,
376             int nvec, afs_int32 asize, int istack)
377 {
378     struct mbuf *tm, *um;
379     afs_int32 code;
380     int s;
381     struct mbuf *top = 0;
382     struct mbuf *m, **mp;
383     int len;
384     char *tdata;
385     caddr_t tpa;
386     int i, tl, rlen;
387     int mlen;
388     int haveGlock;
389 #if KNET_DEBUG
390     static int before = 0;
391 #endif
392
393     AFS_STATCNT(osi_NetSend);
394 /* Actually, the Ultrix way is as good as any for us, so we don't bother with
395  * special mbufs any more.  Used to think we could get away with not copying
396  * the data to the interface, but there's no way to tell the caller not to
397  * reuse the buffers after sending, so we lost out on that trick anyway */
398     s = splnet();
399     if (trysblock(&asocket->so_snd)) {
400         splx(s);
401         return 1;
402     }
403     mp = &top;
404     i = 0;
405     tdata = dvec[i].iov_base;
406     tl = dvec[i].iov_len;
407     while (1) {
408         mlen = MLEN;
409         if (top == 0) {
410             MGETHDR(m, M_DONTWAIT, MT_DATA);
411             if (!m) {
412                 sbunlock(&asocket->so_snd);
413                 splx(s);
414                 return 1;
415             }
416             mlen = MHLEN;
417             m->m_pkthdr.len = 0;
418             m->m_pkthdr.rcvif = NULL;
419         } else
420             MGET(m, M_DONTWAIT, MT_DATA);
421         if (!m) {
422             /* can't get an mbuf, give up */
423             if (top)
424                 m_freem(top);   /* free mbuf list we're building */
425             sbunlock(&asocket->so_snd);
426             splx(s);
427             return 1;
428         }
429         /*
430          * WARNING: the `4 * MLEN' is somewhat dubious.  It is better than
431          * `NBPG', which may have no relation to `CLBYTES'.  Also, `CLBYTES'
432          * may be so large that we never use clusters, resulting in far
433          * too many mbufs being used.  It is often better to briefly use
434          * a cluster, even if we are only using a portion of it.  Since
435          * we are on the xmit side, it shouldn't end up sitting on a queue
436          * for a potentially unbounded time (except perhaps if we are talking
437          * to ourself).
438          */
439         if (asize >= 4 * MLEN) {        /* try to get cluster mbuf */
440             /* different algorithms for getting cluster mbuf */
441             MCLGET(m, M_DONTWAIT);
442             if ((m->m_flags & M_EXT) == 0)
443                 goto nopages;
444             mlen = MCLBYTES;
445
446             /* now compute usable size */
447             len = MIN(mlen, asize);
448 /* Should I look at MAPPED_MBUFS??? */
449         } else {
450           nopages:
451             len = MIN(mlen, asize);
452         }
453         m->m_len = 0;
454         *mp = m;                /* XXXX */
455         top->m_pkthdr.len += len;
456         tpa = mtod(m, caddr_t);
457         while (len) {
458             rlen = MIN(len, tl);
459             memcpy(tpa, tdata, rlen);
460             asize -= rlen;
461             len -= rlen;
462             tpa += rlen;
463             m->m_len += rlen;
464             tdata += rlen;
465             tl -= rlen;
466             if (tl <= 0) {
467                 i++;
468                 if (i > nvec) {
469                     /* shouldn't come here! */
470                     asize = 0;  /* so we make progress toward completion */
471                     break;
472                 }
473                 tdata = dvec[i].iov_base;
474                 tl = dvec[i].iov_len;
475             }
476         }
477         *mp = m;
478         mp = &m->m_next;
479         if (asize <= 0)
480             break;
481     }
482     tm = top;
483
484     tm->m_act = NULL;
485
486     /* setup mbuf corresponding to destination address */
487     um = m_get(M_DONTWAIT, MT_SONAME);
488     if (!um) {
489         if (top)
490             m_freem(top);       /* free mbuf chain */
491         sbunlock(&asocket->so_snd);
492         splx(s);
493         return 1;
494     }
495     memcpy(mtod(um, caddr_t), addr, sizeof(*addr));
496     addr->sin_len = um->m_len = sizeof(*addr);
497     /* note that udp_usrreq frees funny mbuf.  We hold onto data, but mbuf
498      * around it is gone. */
499     /*    haveGlock = ISAFS_GLOCK();
500      * if (haveGlock) {
501      * AFS_GUNLOCK();
502      * }  */
503     /* SOCKET_LOCK(asocket); */
504     /* code = (*asocket->so_proto->pr_usrreq)(asocket, PRU_SEND, tm, um, 0); */
505 #if KNET_DEBUG
506     if (before)
507         Debugger("afs NetSend before");
508 #endif
509     code =
510         (*asocket->so_proto->pr_usrreqs->pru_send) (asocket, 0, tm,
511                                                     (struct sockaddr *)
512                                                     addr, um, &proc0);
513     /* SOCKET_UNLOCK(asocket); */
514     /* if (haveGlock) {
515      * AFS_GLOCK();
516      * } */
517     sbunlock(&asocket->so_snd);
518     splx(s);
519 #if KNET_DEBUG
520     if (code) {
521         if (code == EINVAL)
522             Debugger("afs NetSend busted");
523         else
524             printf("z");
525     }
526 #endif
527     return code;
528 }
529 #endif