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 #define so_is_disconn(so) ((so)->so_state & SS_ISDISCONNECTED)
75 extern int rxk_ListenerPid;
77 osi_StopListener(void)
82 * Have to drop global lock to safely do this.
83 * soclose() is currently protected by Giant,
84 * but pfind and psignal are MPSAFE.
86 int haveGlock = ISAFS_GLOCK();
89 soshutdown(rx_socket, SHUT_RDWR);
91 p = pfind(rxk_ListenerPid);
92 afs_warn("osi_StopListener: rxk_ListenerPid %lx\n", p);
98 /* Avoid destroying socket until osi_NetReceive has
99 * had a chance to clean up */
103 MUTEX_INIT(&s_mtx, "rx_shutdown_mutex", MUTEX_DEFAULT, 0);
106 while ((tries > 0) && (!so_is_disconn(rx_socket))) {
107 msleep(&osi_StopListener, &s_mtx, PSOCK | PCATCH,
108 "rx_shutdown_timedwait", 1 * hz);
111 if (so_is_disconn(rx_socket))
114 MUTEX_DESTROY(&s_mtx);
122 osi_NetSend(osi_socket asocket, struct sockaddr_in *addr, struct iovec *dvec,
123 int nvecs, afs_int32 alength, int istack)
125 register afs_int32 code;
127 struct iovec iov[RX_MAXIOVECS];
129 int haveGlock = ISAFS_GLOCK();
131 AFS_STATCNT(osi_NetSend);
132 if (nvecs > RX_MAXIOVECS)
133 osi_Panic("osi_NetSend: %d: Too many iovecs.\n", nvecs);
135 for (i = 0; i < nvecs; i++)
139 u.uio_iovcnt = nvecs;
141 u.uio_resid = alength;
142 u.uio_segflg = UIO_SYSSPACE;
143 u.uio_rw = UIO_WRITE;
146 addr->sin_len = sizeof(struct sockaddr_in);
154 sosend(asocket, (struct sockaddr *)addr, &u, NULL, NULL, 0,
159 Debugger("afs NetSend busted");
169 /* This code *almost* works :( */
170 static struct protosw parent_proto; /* udp proto switch */
171 static void rxk_input(struct mbuf *am, int iphlen);
172 static void rxk_fasttimo(void);
174 /* start intercepting basic calls */
177 register struct protosw *tpro, *last;
181 last = inetdomain.dom_protoswNPROTOSW;
182 for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
183 if (tpro->pr_protocol == IPPROTO_UDP) {
184 #if 0 /* not exported */
185 /* force UDP checksumming on for AFS */
189 memcpy(&parent_proto, tpro, sizeof(parent_proto));
190 tpro->pr_input = rxk_input;
191 tpro->pr_fasttimo = rxk_fasttimo;
193 * don't bother with pr_drain and pr_ctlinput
194 * until we have something to do
199 osi_Panic("inet:no udp");
204 rxk_input(struct mbuf *am, int iphlen)
207 register unsigned short *tsp;
210 register struct ip *ti;
211 struct udpiphdr *tvu;
215 struct sockaddr_in taddr;
218 int data_len, comp_sum;
223 /* make sure we have base ip and udp headers in first mbuf */
224 if (iphlen > sizeof(struct ip)) {
225 ip_stripoptions(am, NULL);
226 iphlen = sizeof(struct ip);
229 if (am->m_len < sizeof(struct udpiphdr)) {
230 am = m_pullup(am, sizeof(struct udpiphdr));
237 ti = mtod(am, struct ip *);
238 /* skip basic ip hdr */
239 tu = (struct udphdr *)(((char *)ti) + sizeof(struct ip));
241 /* now read the port out */
245 for (tsp = rxk_ports, i = 0; i < MAXRXPORTS; i++) {
246 if (*tsp++ == port) {
247 /* checksum the packet */
249 * Make mbuf data length reflect UDP length.
250 * If not enough data to reflect UDP length, drop.
252 tvu = (struct udpiphdr *)ti;
253 tlen = ntohs((u_short) tvu->ui_ulen);
254 if ((int)ti->ip_len != tlen) {
255 if (tlen > (int)ti->ip_len) {
260 m_adj(am, tlen - (int)ti->ip_len);
262 /* deliver packet to rx */
263 taddr.sin_family = AF_INET; /* compute source address */
264 taddr.sin_port = tu->uh_sport;
265 taddr.sin_addr.s_addr = ti->ip_src.s_addr;
266 taddr.sin_len = sizeof(taddr);
267 tvu = (struct udpiphdr *)ti; /* virtual udp structure, for cksum */
268 /* handle the checksum. Note that this code damages the actual ip
269 * header (replacing it with the virtual one, which is the same size),
270 * so we must ensure we get everything out we need, first */
271 if (tu->uh_sum != 0) {
272 /* if the checksum is there, always check it. It's crazy not
273 * to, unless you can really be sure that your
274 * underlying network (and interfaces and drivers and
275 * DMA hardware, etc!) is error-free. First, fill
276 * in entire virtual ip header. */
277 memset(tvu->ui_i.ih_x1, 0, 9);
278 tvu->ui_len = tvu->ui_ulen;
279 tlen = ntohs((unsigned short)(tvu->ui_ulen));
280 if (in_cksum(am, sizeof(struct ip) + tlen)) {
281 /* checksum, including cksum field, doesn't come out 0, so
282 * this packet is bad */
290 * 28 is IP (20) + UDP (8) header. ulen includes
291 * udp header, and we *don't* tell RX about udp
292 * header either. So, we remove those 8 as well.
294 data_len = ntohs(tu->uh_ulen);
296 if (!(*rxk_GetPacketProc) (&phandle, data_len)) {
297 if (rx_mb_to_packet(am, m_freem, 28, data_len, phandle)) {
298 /* XXX should just increment counter here.. */
299 printf("rx: truncated UDP packet\n");
300 rxi_FreePacket(phandle);
302 (*rxk_PacketArrivalProc) (phandle, &taddr,
303 rxk_portRocks[i], data_len);
312 /* if we get here, try to deliver packet to udp */
313 if (tproc = parent_proto.pr_input)
314 (*tproc) (am, iphlen);
321 * UDP fast timer to raise events for all but Solaris and NCR.
322 * Called about 5 times per second (at unknown priority?). Must go to
323 * splnet or obtain global lock before touching anything significant.
331 /* do rx fasttimo processing here */
332 rxevent_RaiseEvents(&temp);
333 if (tproc = parent_proto.pr_fasttimo)
337 /* rx_NetSend - send asize bytes at adata from asocket to host at addr.
339 * Now, why do we allocate a new buffer when we could theoretically use the one
340 * pointed to by adata? Because PRU_SEND returns after queueing the message,
341 * not after sending it. If the sender changes the data after queueing it,
342 * we'd see the already-queued data change. One attempt to fix this without
343 * adding a copy would be to have this function wait until the datagram is
344 * sent; however this doesn't work well. In particular, if a host is down, and
345 * an ARP fails to that host, this packet will be queued until the ARP request
346 * comes back, which could be hours later. We can't block in this routine that
347 * long, since it prevents RPC timeouts from happening.
349 /* XXX In the brave new world, steal the data bufs out of the rx_packet iovec,
350 * and just queue those. XXX
353 /* set lock on sockbuf sb; can't call sblock since we're at interrupt level
357 register struct sockbuf *sb;
359 AFS_STATCNT(trysblock);
360 if (sb->sb_flags & SB_LOCK) {
361 return -1; /* can't lock socket */
363 sb->sb_flags |= SB_LOCK;
367 /* We only have to do all the mbuf management ourselves if we can be called at
368 interrupt time. in RXK_LISTENER_ENV, we can just call sosend() */
370 osi_NetSend(osi_socket asocket, struct sockaddr_in *addr, struct iovec *dvec,
371 int nvec, afs_int32 asize, int istack)
373 register struct mbuf *tm, *um;
374 register afs_int32 code;
376 struct mbuf *top = 0;
377 register struct mbuf *m, **mp;
385 static int before = 0;
388 AFS_STATCNT(osi_NetSend);
389 /* Actually, the Ultrix way is as good as any for us, so we don't bother with
390 * special mbufs any more. Used to think we could get away with not copying
391 * the data to the interface, but there's no way to tell the caller not to
392 * reuse the buffers after sending, so we lost out on that trick anyway */
394 if (trysblock(&asocket->so_snd)) {
400 tdata = dvec[i].iov_base;
401 tl = dvec[i].iov_len;
405 MGETHDR(m, M_DONTWAIT, MT_DATA);
407 sbunlock(&asocket->so_snd);
413 m->m_pkthdr.rcvif = NULL;
415 MGET(m, M_DONTWAIT, MT_DATA);
417 /* can't get an mbuf, give up */
419 m_freem(top); /* free mbuf list we're building */
420 sbunlock(&asocket->so_snd);
425 * WARNING: the `4 * MLEN' is somewhat dubious. It is better than
426 * `NBPG', which may have no relation to `CLBYTES'. Also, `CLBYTES'
427 * may be so large that we never use clusters, resulting in far
428 * too many mbufs being used. It is often better to briefly use
429 * a cluster, even if we are only using a portion of it. Since
430 * we are on the xmit side, it shouldn't end up sitting on a queue
431 * for a potentially unbounded time (except perhaps if we are talking
434 if (asize >= 4 * MLEN) { /* try to get cluster mbuf */
435 /* different algorithms for getting cluster mbuf */
436 MCLGET(m, M_DONTWAIT);
437 if ((m->m_flags & M_EXT) == 0)
441 /* now compute usable size */
442 len = MIN(mlen, asize);
443 /* Should I look at MAPPED_MBUFS??? */
446 len = MIN(mlen, asize);
450 top->m_pkthdr.len += len;
451 tpa = mtod(m, caddr_t);
454 memcpy(tpa, tdata, rlen);
464 /* shouldn't come here! */
465 asize = 0; /* so we make progress toward completion */
468 tdata = dvec[i].iov_base;
469 tl = dvec[i].iov_len;
481 /* setup mbuf corresponding to destination address */
482 um = m_get(M_DONTWAIT, MT_SONAME);
485 m_freem(top); /* free mbuf chain */
486 sbunlock(&asocket->so_snd);
490 memcpy(mtod(um, caddr_t), addr, sizeof(*addr));
491 addr->sin_len = um->m_len = sizeof(*addr);
492 /* note that udp_usrreq frees funny mbuf. We hold onto data, but mbuf
493 * around it is gone. */
494 /* haveGlock = ISAFS_GLOCK();
498 /* SOCKET_LOCK(asocket); */
499 /* code = (*asocket->so_proto->pr_usrreq)(asocket, PRU_SEND, tm, um, 0); */
502 Debugger("afs NetSend before");
505 (*asocket->so_proto->pr_usrreqs->pru_send) (asocket, 0, tm,
508 /* SOCKET_UNLOCK(asocket); */
512 sbunlock(&asocket->so_snd);
517 Debugger("afs NetSend busted");