2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
11 #include "afs/param.h"
14 #include <sys/malloc.h>
15 #include "rx/rx_kcommon.h"
17 #ifdef RXK_LISTENER_ENV
19 osi_NetReceive(osi_socket asocket, struct sockaddr_in *addr,
20 struct iovec *dvec, int nvecs, int *alength)
24 struct iovec iov[RX_MAXIOVECS];
25 struct sockaddr *sa = NULL;
28 int haveGlock = ISAFS_GLOCK();
29 /*AFS_STATCNT(osi_NetReceive); */
31 if (nvecs > RX_MAXIOVECS)
32 osi_Panic("osi_NetReceive: %d: Too many iovecs.\n", nvecs);
34 for (i = 0; i < nvecs; i++)
40 u.uio_resid = *alength;
41 u.uio_segflg = UIO_SYSSPACE;
47 code = soreceive(asocket, &sa, &u, NULL, NULL, NULL);
54 Debugger("afs NetReceive busted");
61 *alength -= u.uio_resid;
63 if (sa->sa_family == AF_INET) {
65 *addr = *(struct sockaddr_in *)sa;
67 printf("Unknown socket family %d in NetReceive\n", sa->sa_family);
73 extern int rxk_ListenerPid;
75 osi_StopListener(void)
77 struct sockaddr_in taddr;
84 * Have to drop global lock to safely do this.
85 * soclose() is currently protected by Giant,
86 * but pfind and psignal are MPSAFE.
88 int haveGlock = ISAFS_GLOCK();
91 soshutdown(rx_socket, SHUT_RDWR);
92 p = pfind(rxk_ListenerPid);
94 afs_warn("osi_StopListener: rxk_ListenerPid %u\n", rxk_ListenerPid);
95 #if (__FreeBSD_version >= 90004)
96 kern_psignal(p, SIGUSR1);
102 afs_warn("osi_StopListener: rxk_Listener not found (pid %u)\n",
105 /* Avoid destroying socket until osi_NetReceive has
106 * had a chance to clean up. Otherwise we can't restart. */
107 bzero(&taddr, sizeof(taddr));
108 taddr.sin_len = sizeof(struct sockaddr_in);
109 taddr.sin_family = AF_INET;
110 taddr.sin_port = rx_port;
111 taddr.sin_addr.s_addr = htonl(0x7f000001); /* no place like localhost */
112 bzero(&dvec, sizeof(dvec));
115 /* afs_osi_Sleep requires the GLOCK */
117 while(rxk_ListenerPid) {
118 afs_warn("waiting for rxk_ListenerPid to die\n");
119 osi_NetSend(rx_socket, &taddr, &dvec, 1, 1, 0);
120 afs_osi_Sleep(&rxk_ListenerPid);
123 /* in theory, we are now the only people doing anything with rx_socket */
131 osi_NetSend(osi_socket asocket, struct sockaddr_in *addr, struct iovec *dvec,
132 int nvecs, afs_int32 alength, int istack)
136 struct iovec iov[RX_MAXIOVECS];
138 int haveGlock = ISAFS_GLOCK();
140 AFS_STATCNT(osi_NetSend);
141 if (nvecs > RX_MAXIOVECS)
142 osi_Panic("osi_NetSend: %d: Too many iovecs.\n", nvecs);
144 for (i = 0; i < nvecs; i++)
148 u.uio_iovcnt = nvecs;
150 u.uio_resid = alength;
151 u.uio_segflg = UIO_SYSSPACE;
152 u.uio_rw = UIO_WRITE;
155 addr->sin_len = sizeof(struct sockaddr_in);
163 sosend(asocket, (struct sockaddr *)addr, &u, NULL, NULL, 0,
168 Debugger("afs NetSend busted");
178 /* This code *almost* works :( */
179 static struct protosw parent_proto; /* udp proto switch */
180 static void rxk_input(struct mbuf *am, int iphlen);
181 static void rxk_fasttimo(void);
183 /* start intercepting basic calls */
186 struct protosw *tpro, *last;
190 last = inetdomain.dom_protoswNPROTOSW;
191 for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
192 if (tpro->pr_protocol == IPPROTO_UDP) {
193 #if 0 /* not exported */
194 /* force UDP checksumming on for AFS */
198 memcpy(&parent_proto, tpro, sizeof(parent_proto));
199 tpro->pr_input = rxk_input;
200 tpro->pr_fasttimo = rxk_fasttimo;
202 * don't bother with pr_drain and pr_ctlinput
203 * until we have something to do
208 osi_Panic("inet:no udp");
213 rxk_input(struct mbuf *am, int iphlen)
220 struct udpiphdr *tvu;
224 struct sockaddr_in taddr;
227 int data_len, comp_sum;
232 /* make sure we have base ip and udp headers in first mbuf */
233 if (iphlen > sizeof(struct ip)) {
234 ip_stripoptions(am, NULL);
235 iphlen = sizeof(struct ip);
238 if (am->m_len < sizeof(struct udpiphdr)) {
239 am = m_pullup(am, sizeof(struct udpiphdr));
246 ti = mtod(am, struct ip *);
247 /* skip basic ip hdr */
248 tu = (struct udphdr *)(((char *)ti) + sizeof(struct ip));
250 /* now read the port out */
254 for (tsp = rxk_ports, i = 0; i < MAXRXPORTS; i++) {
255 if (*tsp++ == port) {
256 /* checksum the packet */
258 * Make mbuf data length reflect UDP length.
259 * If not enough data to reflect UDP length, drop.
261 tvu = (struct udpiphdr *)ti;
262 tlen = ntohs((u_short) tvu->ui_ulen);
263 if ((int)ti->ip_len != tlen) {
264 if (tlen > (int)ti->ip_len) {
269 m_adj(am, tlen - (int)ti->ip_len);
271 /* deliver packet to rx */
272 taddr.sin_family = AF_INET; /* compute source address */
273 taddr.sin_port = tu->uh_sport;
274 taddr.sin_addr.s_addr = ti->ip_src.s_addr;
275 taddr.sin_len = sizeof(taddr);
276 tvu = (struct udpiphdr *)ti; /* virtual udp structure, for cksum */
277 /* handle the checksum. Note that this code damages the actual ip
278 * header (replacing it with the virtual one, which is the same size),
279 * so we must ensure we get everything out we need, first */
280 if (tu->uh_sum != 0) {
281 /* if the checksum is there, always check it. It's crazy not
282 * to, unless you can really be sure that your
283 * underlying network (and interfaces and drivers and
284 * DMA hardware, etc!) is error-free. First, fill
285 * in entire virtual ip header. */
286 memset(tvu->ui_i.ih_x1, 0, 9);
287 tvu->ui_len = tvu->ui_ulen;
288 tlen = ntohs((unsigned short)(tvu->ui_ulen));
289 if (in_cksum(am, sizeof(struct ip) + tlen)) {
290 /* checksum, including cksum field, doesn't come out 0, so
291 * this packet is bad */
299 * 28 is IP (20) + UDP (8) header. ulen includes
300 * udp header, and we *don't* tell RX about udp
301 * header either. So, we remove those 8 as well.
303 data_len = ntohs(tu->uh_ulen);
305 if (!(*rxk_GetPacketProc) (&phandle, data_len)) {
306 if (rx_mb_to_packet(am, m_freem, 28, data_len, phandle)) {
307 /* XXX should just increment counter here.. */
308 printf("rx: truncated UDP packet\n");
309 rxi_FreePacket(phandle);
311 (*rxk_PacketArrivalProc) (phandle, &taddr,
312 rxk_portRocks[i], data_len);
321 /* if we get here, try to deliver packet to udp */
322 if (tproc = parent_proto.pr_input)
323 (*tproc) (am, iphlen);
330 * UDP fast timer to raise events for all but Solaris and NCR.
331 * Called about 5 times per second (at unknown priority?). Must go to
332 * splnet or obtain global lock before touching anything significant.
340 /* do rx fasttimo processing here */
341 rxevent_RaiseEvents(&temp);
342 if (tproc = parent_proto.pr_fasttimo)
346 /* rx_NetSend - send asize bytes at adata from asocket to host at addr.
348 * Now, why do we allocate a new buffer when we could theoretically use the one
349 * pointed to by adata? Because PRU_SEND returns after queueing the message,
350 * not after sending it. If the sender changes the data after queueing it,
351 * we'd see the already-queued data change. One attempt to fix this without
352 * adding a copy would be to have this function wait until the datagram is
353 * sent; however this doesn't work well. In particular, if a host is down, and
354 * an ARP fails to that host, this packet will be queued until the ARP request
355 * comes back, which could be hours later. We can't block in this routine that
356 * long, since it prevents RPC timeouts from happening.
358 /* XXX In the brave new world, steal the data bufs out of the rx_packet iovec,
359 * and just queue those. XXX
362 /* set lock on sockbuf sb; can't call sblock since we're at interrupt level
368 AFS_STATCNT(trysblock);
369 if (sb->sb_flags & SB_LOCK) {
370 return -1; /* can't lock socket */
372 sb->sb_flags |= SB_LOCK;
376 /* We only have to do all the mbuf management ourselves if we can be called at
377 interrupt time. in RXK_LISTENER_ENV, we can just call sosend() */
379 osi_NetSend(osi_socket asocket, struct sockaddr_in *addr, struct iovec *dvec,
380 int nvec, afs_int32 asize, int istack)
382 struct mbuf *tm, *um;
385 struct mbuf *top = 0;
386 struct mbuf *m, **mp;
394 static int before = 0;
397 AFS_STATCNT(osi_NetSend);
398 /* Actually, the Ultrix way is as good as any for us, so we don't bother with
399 * special mbufs any more. Used to think we could get away with not copying
400 * the data to the interface, but there's no way to tell the caller not to
401 * reuse the buffers after sending, so we lost out on that trick anyway */
403 if (trysblock(&asocket->so_snd)) {
409 tdata = dvec[i].iov_base;
410 tl = dvec[i].iov_len;
414 MGETHDR(m, M_DONTWAIT, MT_DATA);
416 sbunlock(&asocket->so_snd);
422 m->m_pkthdr.rcvif = NULL;
424 MGET(m, M_DONTWAIT, MT_DATA);
426 /* can't get an mbuf, give up */
428 m_freem(top); /* free mbuf list we're building */
429 sbunlock(&asocket->so_snd);
434 * WARNING: the `4 * MLEN' is somewhat dubious. It is better than
435 * `NBPG', which may have no relation to `CLBYTES'. Also, `CLBYTES'
436 * may be so large that we never use clusters, resulting in far
437 * too many mbufs being used. It is often better to briefly use
438 * a cluster, even if we are only using a portion of it. Since
439 * we are on the xmit side, it shouldn't end up sitting on a queue
440 * for a potentially unbounded time (except perhaps if we are talking
443 if (asize >= 4 * MLEN) { /* try to get cluster mbuf */
444 /* different algorithms for getting cluster mbuf */
445 MCLGET(m, M_DONTWAIT);
446 if ((m->m_flags & M_EXT) == 0)
450 /* now compute usable size */
451 len = MIN(mlen, asize);
452 /* Should I look at MAPPED_MBUFS??? */
455 len = MIN(mlen, asize);
459 top->m_pkthdr.len += len;
460 tpa = mtod(m, caddr_t);
463 memcpy(tpa, tdata, rlen);
473 /* shouldn't come here! */
474 asize = 0; /* so we make progress toward completion */
477 tdata = dvec[i].iov_base;
478 tl = dvec[i].iov_len;
490 /* setup mbuf corresponding to destination address */
491 um = m_get(M_DONTWAIT, MT_SONAME);
494 m_freem(top); /* free mbuf chain */
495 sbunlock(&asocket->so_snd);
499 memcpy(mtod(um, caddr_t), addr, sizeof(*addr));
500 addr->sin_len = um->m_len = sizeof(*addr);
501 /* note that udp_usrreq frees funny mbuf. We hold onto data, but mbuf
502 * around it is gone. */
503 /* haveGlock = ISAFS_GLOCK();
507 /* SOCKET_LOCK(asocket); */
508 /* code = (*asocket->so_proto->pr_usrreq)(asocket, PRU_SEND, tm, um, 0); */
511 Debugger("afs NetSend before");
514 (*asocket->so_proto->pr_usrreqs->pru_send) (asocket, 0, tm,
517 /* SOCKET_UNLOCK(asocket); */
521 sbunlock(&asocket->so_snd);
526 Debugger("afs NetSend busted");