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_packet.h"
16 #include "rx/rx_atomic.h"
17 #include "rx/rx_stats.h"
19 #ifdef AFS_DARWIN80_ENV
20 #define soclose sock_close
25 rx_upcall(socket_t so, void *arg, __unused int waitflag)
31 struct sockaddr_storage ss;
32 struct sockaddr *sa = NULL;
33 struct sockaddr_in from;
37 afs_int32 savelen; /* was using rlen but had aliasing problems */
38 size_t nbytes, resid, noffset;
40 p = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
41 rx_computelen(p, tlen);
42 rx_SetDataSize(p, tlen); /* this is the size of the user data area */
43 tlen += RX_HEADER_SIZE; /* now this is the size of the entire packet */
44 rlen = rx_maxJumboRecvSize; /* this is what I am advertising. Only check
45 * it once in order to avoid races. */
48 tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
55 /* add some padding to the last iovec, it's just to make sure that the
56 * read doesn't return more data than we expect, and is done to get around
57 * our problems caused by the lack of a length field in the rx header. */
58 savelen = p->wirevec[p->niovecs - 1].iov_len;
59 p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
61 resid = nbytes = tlen + sizeof(afs_int32);
63 memset(&msg, 0, sizeof(struct msghdr));
65 msg.msg_namelen = sizeof(struct sockaddr_storage);
66 sa =(struct sockaddr *) &ss;
70 error = sock_receivembuf(so, &msg, &m, MSG_DONTWAIT, &nbytes);
72 size_t sz, offset = 0;
75 for (i=0;i<p->niovecs && resid;i++) {
76 sz=MIN(resid, p->wirevec[i].iov_len);
77 error = mbuf_copydata(m, offset, sz, p->wirevec[i].iov_base);
89 /* restore the vec to its correct state */
90 p->wirevec[p->niovecs - 1].iov_len = savelen;
92 if (error == EWOULDBLOCK && noffset > 0)
100 if (sa->sa_family == AF_INET)
101 from = *(struct sockaddr_in *)sa;
103 p->length = nbytes - RX_HEADER_SIZE;;
104 if ((nbytes > tlen) || (p->length & 0x8000)) { /* Bogus packet */
106 if (rx_stats_active) {
107 MUTEX_ENTER(&rx_stats_mutex);
108 rx_atomic_inc(&rx_stats.bogusPacketOnRead);
109 rx_stats.bogusHost = from.sin_addr.s_addr;
110 MUTEX_EXIT(&rx_stats_mutex);
112 dpf(("B: bogus packet from [%x,%d] nb=%d",
113 from.sin_addr.s_addr, from.sin_port, nbytes));
117 /* Extract packet header. */
118 rxi_DecodePacketHeader(p);
120 host = from.sin_addr.s_addr;
121 port = from.sin_port;
122 if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
123 if (rx_stats_active) {
124 rx_atomic_inc(&rx_stats.packetsRead[p->header.type - 1]);
128 #ifdef RX_TRIMDATABUFS
129 /* Free any empty packet buffers at the end of this packet */
130 rxi_TrimDataBufs(p, 1);
133 p = rxi_ReceivePacket(p, so, host, port, 0, 0);
143 /* in listener env, the listener shutdown does this. we have no listener */
145 osi_StopNetIfPoller(void)
149 if (afs_termState == AFSOP_STOP_NETIF) {
150 afs_termState = AFSOP_STOP_COMPLETE;
151 osi_rxWakeup(&afs_termState);
154 #elif defined(RXK_LISTENER_ENV)
156 osi_NetReceive(osi_socket so, struct sockaddr_in *addr, struct iovec *dvec,
157 int nvecs, int *alength)
159 #ifdef AFS_DARWIN80_ENV
160 socket_t asocket = (socket_t)so;
162 struct sockaddr_storage ss;
166 struct socket *asocket = (struct socket *)so;
170 struct iovec iov[RX_MAXIOVECS];
171 struct sockaddr *sa = NULL;
175 int haveGlock = ISAFS_GLOCK();
176 /*AFS_STATCNT(osi_NetReceive); */
178 if (nvecs > RX_MAXIOVECS)
179 osi_Panic("osi_NetReceive: %d: Too many iovecs.\n", nvecs);
181 for (i = 0; i < nvecs; i++)
184 if ((afs_termState == AFSOP_STOP_RXK_LISTENER) ||
185 (afs_termState == AFSOP_STOP_COMPLETE))
190 #if defined(KERNEL_FUNNEL)
191 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
193 #ifdef AFS_DARWIN80_ENV
195 memset(&msg, 0, sizeof(struct msghdr));
197 msg.msg_namelen = sizeof(struct sockaddr_storage);
198 sa =(struct sockaddr *) &ss;
199 code = sock_receivembuf(asocket, &msg, &m, 0, alength);
203 for (i=0;i<nvecs && resid;i++) {
204 sz=MIN(resid, iov[i].iov_len);
205 code = mbuf_copydata(m, offset, sz, iov[i].iov_base);
216 u.uio_iovcnt = nvecs;
218 u.uio_resid = *alength;
219 u.uio_segflg = UIO_SYSSPACE;
222 code = soreceive(asocket, &sa, &u, NULL, NULL, NULL);
226 #if defined(KERNEL_FUNNEL)
227 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
236 if (sa->sa_family == AF_INET) {
238 *addr = *(struct sockaddr_in *)sa;
240 printf("Unknown socket family %d in NetReceive\n", sa->sa_family);
241 #ifndef AFS_DARWIN80_ENV
248 extern int rxk_ListenerPid;
250 osi_StopListener(void)
254 #if defined(KERNEL_FUNNEL)
255 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
258 #if defined(KERNEL_FUNNEL)
259 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
261 #ifndef AFS_DARWIN80_ENV
262 p = pfind(rxk_ListenerPid);
268 #error need upcall or listener
272 osi_NetSend(osi_socket so, struct sockaddr_in *addr, struct iovec *dvec,
273 int nvecs, afs_int32 alength, int istack)
275 #ifdef AFS_DARWIN80_ENV
276 socket_t asocket = (socket_t)so;
280 struct socket *asocket = (struct socket *)so;
285 struct iovec iov[RX_MAXIOVECS];
286 int haveGlock = ISAFS_GLOCK();
288 AFS_STATCNT(osi_NetSend);
289 if (nvecs > RX_MAXIOVECS)
290 osi_Panic("osi_NetSend: %d: Too many iovecs.\n", nvecs);
292 for (i = 0; i < nvecs; i++)
295 addr->sin_len = sizeof(struct sockaddr_in);
297 if ((afs_termState == AFSOP_STOP_RXK_LISTENER) ||
298 (afs_termState == AFSOP_STOP_COMPLETE))
304 #if defined(KERNEL_FUNNEL)
305 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
307 #ifdef AFS_DARWIN80_ENV
308 memset(&msg, 0, sizeof(struct msghdr));
310 msg.msg_namelen = ((struct sockaddr *)addr)->sa_len;
311 msg.msg_iov = &iov[0];
312 msg.msg_iovlen = nvecs;
313 code = sock_send(asocket, &msg, 0, &slen);
316 u.uio_iovcnt = nvecs;
318 u.uio_resid = alength;
319 u.uio_segflg = UIO_SYSSPACE;
320 u.uio_rw = UIO_WRITE;
322 code = sosend(asocket, (struct sockaddr *)addr, &u, NULL, NULL, 0);
325 #if defined(KERNEL_FUNNEL)
326 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);