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 "rx/rx_kcommon.h"
15 #include "rx/rx_atomic.h"
16 #include "rx/rx_stats.h"
18 #ifdef AFS_DARWIN80_ENV
19 #define soclose sock_close
24 rx_upcall(socket_t so, void *arg, __unused int waitflag)
30 struct sockaddr_storage ss;
31 struct sockaddr *sa = NULL;
32 struct sockaddr_in from;
36 afs_int32 savelen; /* was using rlen but had aliasing problems */
37 size_t nbytes, resid, noffset;
39 p = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
40 rx_computelen(p, tlen);
41 rx_SetDataSize(p, tlen); /* this is the size of the user data area */
42 tlen += RX_HEADER_SIZE; /* now this is the size of the entire packet */
43 rlen = rx_maxJumboRecvSize; /* this is what I am advertising. Only check
44 * it once in order to avoid races. */
47 tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
54 /* add some padding to the last iovec, it's just to make sure that the
55 * read doesn't return more data than we expect, and is done to get around
56 * our problems caused by the lack of a length field in the rx header. */
57 savelen = p->wirevec[p->niovecs - 1].iov_len;
58 p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
60 resid = nbytes = tlen + sizeof(afs_int32);
62 memset(&msg, 0, sizeof(struct msghdr));
64 msg.msg_namelen = sizeof(struct sockaddr_storage);
65 sa =(struct sockaddr *) &ss;
69 error = sock_receivembuf(so, &msg, &m, MSG_DONTWAIT, &nbytes);
71 size_t sz, offset = 0;
74 for (i=0;i<p->niovecs && resid;i++) {
75 sz=MIN(resid, p->wirevec[i].iov_len);
76 error = mbuf_copydata(m, offset, sz, p->wirevec[i].iov_base);
88 /* restore the vec to its correct state */
89 p->wirevec[p->niovecs - 1].iov_len = savelen;
91 if (error == EWOULDBLOCK && noffset > 0)
99 if (sa->sa_family == AF_INET)
100 from = *(struct sockaddr_in *)sa;
102 p->length = nbytes - RX_HEADER_SIZE;;
103 if ((nbytes > tlen) || (p->length & 0x8000)) { /* Bogus packet */
105 if (rx_stats_active) {
106 MUTEX_ENTER(&rx_stats_mutex);
107 rx_atomic_inc(&rx_stats.bogusPacketOnRead);
108 rx_stats.bogusHost = from.sin_addr.s_addr;
109 MUTEX_EXIT(&rx_stats_mutex);
111 dpf(("B: bogus packet from [%x,%d] nb=%d",
112 from.sin_addr.s_addr, from.sin_port, nbytes));
116 /* Extract packet header. */
117 rxi_DecodePacketHeader(p);
119 host = from.sin_addr.s_addr;
120 port = from.sin_port;
121 if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
122 if (rx_stats_active) {
123 rx_atomic_inc(&rx_stats.packetsRead[p->header.type - 1]);
127 #ifdef RX_TRIMDATABUFS
128 /* Free any empty packet buffers at the end of this packet */
129 rxi_TrimDataBufs(p, 1);
132 p = rxi_ReceivePacket(p, so, host, port, 0, 0);
142 /* in listener env, the listener shutdown does this. we have no listener */
144 osi_StopNetIfPoller(void)
148 if (afs_termState == AFSOP_STOP_NETIF) {
149 afs_termState = AFSOP_STOP_COMPLETE;
150 osi_rxWakeup(&afs_termState);
153 #elif defined(RXK_LISTENER_ENV)
155 osi_NetReceive(osi_socket so, struct sockaddr_in *addr, struct iovec *dvec,
156 int nvecs, int *alength)
158 #ifdef AFS_DARWIN80_ENV
159 socket_t asocket = (socket_t)so;
161 struct sockaddr_storage ss;
165 struct socket *asocket = (struct socket *)so;
169 struct iovec iov[RX_MAXIOVECS];
170 struct sockaddr *sa = NULL;
174 int haveGlock = ISAFS_GLOCK();
175 /*AFS_STATCNT(osi_NetReceive); */
177 if (nvecs > RX_MAXIOVECS)
178 osi_Panic("osi_NetReceive: %d: Too many iovecs.\n", nvecs);
180 for (i = 0; i < nvecs; i++)
183 if ((afs_termState == AFSOP_STOP_RXK_LISTENER) ||
184 (afs_termState == AFSOP_STOP_COMPLETE))
189 #if defined(KERNEL_FUNNEL)
190 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
192 #ifdef AFS_DARWIN80_ENV
194 memset(&msg, 0, sizeof(struct msghdr));
196 msg.msg_namelen = sizeof(struct sockaddr_storage);
197 sa =(struct sockaddr *) &ss;
198 code = sock_receivembuf(asocket, &msg, &m, 0, alength);
202 for (i=0;i<nvecs && resid;i++) {
203 sz=MIN(resid, iov[i].iov_len);
204 code = mbuf_copydata(m, offset, sz, iov[i].iov_base);
215 u.uio_iovcnt = nvecs;
217 u.uio_resid = *alength;
218 u.uio_segflg = UIO_SYSSPACE;
221 code = soreceive(asocket, &sa, &u, NULL, NULL, NULL);
225 #if defined(KERNEL_FUNNEL)
226 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
235 if (sa->sa_family == AF_INET) {
237 *addr = *(struct sockaddr_in *)sa;
239 printf("Unknown socket family %d in NetReceive\n", sa->sa_family);
240 #ifndef AFS_DARWIN80_ENV
247 extern int rxk_ListenerPid;
249 osi_StopListener(void)
253 #if defined(KERNEL_FUNNEL)
254 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
257 #if defined(KERNEL_FUNNEL)
258 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
260 #ifndef AFS_DARWIN80_ENV
261 p = pfind(rxk_ListenerPid);
267 #error need upcall or listener
271 osi_NetSend(osi_socket so, struct sockaddr_in *addr, struct iovec *dvec,
272 int nvecs, afs_int32 alength, int istack)
274 #ifdef AFS_DARWIN80_ENV
275 socket_t asocket = (socket_t)so;
279 struct socket *asocket = (struct socket *)so;
284 struct iovec iov[RX_MAXIOVECS];
285 int haveGlock = ISAFS_GLOCK();
287 AFS_STATCNT(osi_NetSend);
288 if (nvecs > RX_MAXIOVECS)
289 osi_Panic("osi_NetSend: %d: Too many iovecs.\n", nvecs);
291 for (i = 0; i < nvecs; i++)
294 addr->sin_len = sizeof(struct sockaddr_in);
296 if ((afs_termState == AFSOP_STOP_RXK_LISTENER) ||
297 (afs_termState == AFSOP_STOP_COMPLETE))
303 #if defined(KERNEL_FUNNEL)
304 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
306 #ifdef AFS_DARWIN80_ENV
307 memset(&msg, 0, sizeof(struct msghdr));
309 msg.msg_namelen = ((struct sockaddr *)addr)->sa_len;
310 msg.msg_iov = &iov[0];
311 msg.msg_iovlen = nvecs;
312 code = sock_send(asocket, &msg, 0, &slen);
315 u.uio_iovcnt = nvecs;
317 u.uio_resid = alength;
318 u.uio_segflg = UIO_SYSSPACE;
319 u.uio_rw = UIO_WRITE;
321 code = sosend(asocket, (struct sockaddr *)addr, &u, NULL, NULL, 0);
324 #if defined(KERNEL_FUNNEL)
325 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);