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);
98 afs_warn("osi_StopListener: rxk_Listener not found (pid %u)\n",
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));
111 /* afs_osi_Sleep requires the 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);
119 /* in theory, we are now the only people doing anything with rx_socket */
127 osi_NetSend(osi_socket asocket, struct sockaddr_in *addr, struct iovec *dvec,
128 int nvecs, afs_int32 alength, int istack)
132 struct iovec iov[RX_MAXIOVECS];
134 int haveGlock = ISAFS_GLOCK();
136 AFS_STATCNT(osi_NetSend);
137 if (nvecs > RX_MAXIOVECS)
138 osi_Panic("osi_NetSend: %d: Too many iovecs.\n", nvecs);
140 for (i = 0; i < nvecs; i++)
144 u.uio_iovcnt = nvecs;
146 u.uio_resid = alength;
147 u.uio_segflg = UIO_SYSSPACE;
148 u.uio_rw = UIO_WRITE;
151 addr->sin_len = sizeof(struct sockaddr_in);
159 sosend(asocket, (struct sockaddr *)addr, &u, NULL, NULL, 0,
164 Debugger("afs NetSend busted");
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);
179 /* start intercepting basic calls */
182 struct protosw *tpro, *last;
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 */
194 memcpy(&parent_proto, tpro, sizeof(parent_proto));
195 tpro->pr_input = rxk_input;
196 tpro->pr_fasttimo = rxk_fasttimo;
198 * don't bother with pr_drain and pr_ctlinput
199 * until we have something to do
204 osi_Panic("inet:no udp");
209 rxk_input(struct mbuf *am, int iphlen)
216 struct udpiphdr *tvu;
220 struct sockaddr_in taddr;
223 int data_len, comp_sum;
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);
234 if (am->m_len < sizeof(struct udpiphdr)) {
235 am = m_pullup(am, sizeof(struct udpiphdr));
242 ti = mtod(am, struct ip *);
243 /* skip basic ip hdr */
244 tu = (struct udphdr *)(((char *)ti) + sizeof(struct ip));
246 /* now read the port out */
250 for (tsp = rxk_ports, i = 0; i < MAXRXPORTS; i++) {
251 if (*tsp++ == port) {
252 /* checksum the packet */
254 * Make mbuf data length reflect UDP length.
255 * If not enough data to reflect UDP length, drop.
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) {
265 m_adj(am, tlen - (int)ti->ip_len);
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 */
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.
299 data_len = ntohs(tu->uh_ulen);
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);
307 (*rxk_PacketArrivalProc) (phandle, &taddr,
308 rxk_portRocks[i], data_len);
317 /* if we get here, try to deliver packet to udp */
318 if (tproc = parent_proto.pr_input)
319 (*tproc) (am, iphlen);
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.
336 /* do rx fasttimo processing here */
337 rxevent_RaiseEvents(&temp);
338 if (tproc = parent_proto.pr_fasttimo)
342 /* rx_NetSend - send asize bytes at adata from asocket to host at addr.
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.
354 /* XXX In the brave new world, steal the data bufs out of the rx_packet iovec,
355 * and just queue those. XXX
358 /* set lock on sockbuf sb; can't call sblock since we're at interrupt level
364 AFS_STATCNT(trysblock);
365 if (sb->sb_flags & SB_LOCK) {
366 return -1; /* can't lock socket */
368 sb->sb_flags |= SB_LOCK;
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() */
375 osi_NetSend(osi_socket asocket, struct sockaddr_in *addr, struct iovec *dvec,
376 int nvec, afs_int32 asize, int istack)
378 struct mbuf *tm, *um;
381 struct mbuf *top = 0;
382 struct mbuf *m, **mp;
390 static int before = 0;
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 */
399 if (trysblock(&asocket->so_snd)) {
405 tdata = dvec[i].iov_base;
406 tl = dvec[i].iov_len;
410 MGETHDR(m, M_DONTWAIT, MT_DATA);
412 sbunlock(&asocket->so_snd);
418 m->m_pkthdr.rcvif = NULL;
420 MGET(m, M_DONTWAIT, MT_DATA);
422 /* can't get an mbuf, give up */
424 m_freem(top); /* free mbuf list we're building */
425 sbunlock(&asocket->so_snd);
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
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)
446 /* now compute usable size */
447 len = MIN(mlen, asize);
448 /* Should I look at MAPPED_MBUFS??? */
451 len = MIN(mlen, asize);
455 top->m_pkthdr.len += len;
456 tpa = mtod(m, caddr_t);
459 memcpy(tpa, tdata, rlen);
469 /* shouldn't come here! */
470 asize = 0; /* so we make progress toward completion */
473 tdata = dvec[i].iov_base;
474 tl = dvec[i].iov_len;
486 /* setup mbuf corresponding to destination address */
487 um = m_get(M_DONTWAIT, MT_SONAME);
490 m_freem(top); /* free mbuf chain */
491 sbunlock(&asocket->so_snd);
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();
503 /* SOCKET_LOCK(asocket); */
504 /* code = (*asocket->so_proto->pr_usrreq)(asocket, PRU_SEND, tm, um, 0); */
507 Debugger("afs NetSend before");
510 (*asocket->so_proto->pr_usrreqs->pru_send) (asocket, 0, tm,
513 /* SOCKET_UNLOCK(asocket); */
517 sbunlock(&asocket->so_snd);
522 Debugger("afs NetSend busted");