osx: restore atomic stats to knet
[openafs.git] / src / rx / DARWIN / rx_knet.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <afsconfig.h>
11 #include "afs/param.h"
12
13
14 #include "rx/rx_kcommon.h"
15 #include "rx/rx_atomic.h"
16 #include "rx/rx_stats.h"
17
18 #ifdef AFS_DARWIN80_ENV
19 #define soclose sock_close
20 #endif
21
22 #ifdef RXK_UPCALL_ENV
23 void
24 rx_upcall(socket_t so, void *arg, __unused int waitflag)
25 {
26     mbuf_t m;
27     int error = 0;
28     int i, flags = 0;
29     struct msghdr msg;
30     struct sockaddr_storage ss;
31     struct sockaddr *sa = NULL;
32     struct sockaddr_in from;
33     struct rx_packet *p;
34     afs_int32 rlen;
35     afs_int32 tlen;
36     afs_int32 savelen;          /* was using rlen but had aliasing problems */
37     size_t nbytes, resid, noffset;
38
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.  */
45     tlen = rlen - tlen;
46     if (tlen > 0) {
47         tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
48         if (tlen > 0) {
49             tlen = rlen - tlen;
50         } else
51             tlen = rlen;
52     } else
53         tlen = rlen;
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;
59
60     resid = nbytes = tlen + sizeof(afs_int32);
61
62     memset(&msg, 0, sizeof(struct msghdr));
63     msg.msg_name = &ss;
64     msg.msg_namelen = sizeof(struct sockaddr_storage);
65     sa =(struct sockaddr *) &ss;
66
67     do {
68         m = NULL;
69         error = sock_receivembuf(so, &msg, &m, MSG_DONTWAIT, &nbytes);
70         if (!error) {
71             size_t sz, offset = 0;
72             noffset = 0;
73             resid = nbytes;
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);
77                 if (error)
78                     break;
79                 resid-=sz;
80                 offset+=sz;
81                 noffset += sz;
82             }
83         }
84     } while (0);
85
86     mbuf_freem(m);
87
88     /* restore the vec to its correct state */
89     p->wirevec[p->niovecs - 1].iov_len = savelen;
90
91     if (error == EWOULDBLOCK && noffset > 0)
92         error = 0;
93
94     if (!error) {
95         int host, port;
96
97         nbytes -= resid;
98
99         if (sa->sa_family == AF_INET)
100             from = *(struct sockaddr_in *)sa;
101
102         p->length = nbytes - RX_HEADER_SIZE;;
103         if ((nbytes > tlen) || (p->length & 0x8000)) {  /* Bogus packet */
104             if (nbytes <= 0) {
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);
110                 }
111                 dpf(("B: bogus packet from [%x,%d] nb=%d",
112                      from.sin_addr.s_addr, from.sin_port, nbytes));
113             }
114             return;
115         } else {
116             /* Extract packet header. */
117             rxi_DecodePacketHeader(p);
118
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]);
124                 }
125             }
126
127 #ifdef RX_TRIMDATABUFS
128             /* Free any empty packet buffers at the end of this packet */
129             rxi_TrimDataBufs(p, 1);
130 #endif
131             /* receive pcket */
132             p = rxi_ReceivePacket(p, so, host, port, 0, 0);
133         }
134     }
135     /* free packet? */
136     if (p)
137         rxi_FreePacket(p);
138
139     return;
140 }
141
142 /* in listener env, the listener shutdown does this. we have no listener */
143 void
144 osi_StopNetIfPoller(void)
145 {
146     soclose(rx_socket);
147     if (afs_termState == AFSOP_STOP_NETIF) {
148         afs_termState = AFSOP_STOP_COMPLETE;
149         osi_rxWakeup(&afs_termState);
150     }
151 }
152 #elif defined(RXK_LISTENER_ENV)
153 int
154 osi_NetReceive(osi_socket so, struct sockaddr_in *addr, struct iovec *dvec,
155                int nvecs, int *alength)
156 {
157 #ifdef AFS_DARWIN80_ENV
158     socket_t asocket = (socket_t)so;
159     struct msghdr msg;
160     struct sockaddr_storage ss;
161     int rlen;
162     mbuf_t m;
163 #else
164     struct socket *asocket = (struct socket *)so;
165     struct uio u;
166 #endif
167     int i;
168     struct iovec iov[RX_MAXIOVECS];
169     struct sockaddr *sa = NULL;
170     int code;
171     size_t resid;
172
173     int haveGlock = ISAFS_GLOCK();
174     /*AFS_STATCNT(osi_NetReceive); */
175
176     if (nvecs > RX_MAXIOVECS)
177         osi_Panic("osi_NetReceive: %d: Too many iovecs.\n", nvecs);
178
179     for (i = 0; i < nvecs; i++)
180         iov[i] = dvec[i];
181
182     if ((afs_termState == AFSOP_STOP_RXK_LISTENER) ||
183         (afs_termState == AFSOP_STOP_COMPLETE))
184         return -1;
185
186     if (haveGlock)
187         AFS_GUNLOCK();
188 #if defined(KERNEL_FUNNEL)
189     thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
190 #endif
191 #ifdef AFS_DARWIN80_ENV
192     resid = *alength;
193     memset(&msg, 0, sizeof(struct msghdr));
194     msg.msg_name = &ss;
195     msg.msg_namelen = sizeof(struct sockaddr_storage);
196     sa =(struct sockaddr *) &ss;
197     code = sock_receivembuf(asocket, &msg, &m, 0, alength);
198     if (!code) {
199         size_t offset=0,sz;
200         resid = *alength;
201         for (i=0;i<nvecs && resid;i++) {
202             sz=MIN(resid, iov[i].iov_len);
203             code = mbuf_copydata(m, offset, sz, iov[i].iov_base);
204             if (code)
205                 break;
206             resid-=sz;
207             offset+=sz;
208         }
209     }
210     mbuf_freem(m);
211 #else
212
213     u.uio_iov = &iov[0];
214     u.uio_iovcnt = nvecs;
215     u.uio_offset = 0;
216     u.uio_resid = *alength;
217     u.uio_segflg = UIO_SYSSPACE;
218     u.uio_rw = UIO_READ;
219     u.uio_procp = NULL;
220     code = soreceive(asocket, &sa, &u, NULL, NULL, NULL);
221     resid = u.uio_resid;
222 #endif
223
224 #if defined(KERNEL_FUNNEL)
225     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
226 #endif
227     if (haveGlock)
228         AFS_GLOCK();
229
230     if (code)
231         return code;
232     *alength -= resid;
233     if (sa) {
234         if (sa->sa_family == AF_INET) {
235             if (addr)
236                 *addr = *(struct sockaddr_in *)sa;
237         } else
238             printf("Unknown socket family %d in NetReceive\n", sa->sa_family);
239 #ifndef AFS_DARWIN80_ENV
240         FREE(sa, M_SONAME);
241 #endif
242     }
243     return code;
244 }
245
246 extern int rxk_ListenerPid;
247 void
248 osi_StopListener(void)
249 {
250     struct proc *p;
251
252 #if defined(KERNEL_FUNNEL)
253     thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
254 #endif
255     soclose(rx_socket);
256 #if defined(KERNEL_FUNNEL)
257     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
258 #endif
259 #ifndef AFS_DARWIN80_ENV
260     p = pfind(rxk_ListenerPid);
261     if (p)
262         psignal(p, SIGUSR1);
263 #endif
264 }
265 #else
266 #error need upcall or listener
267 #endif
268
269 int
270 osi_NetSend(osi_socket so, struct sockaddr_in *addr, struct iovec *dvec,
271             int nvecs, afs_int32 alength, int istack)
272 {
273 #ifdef AFS_DARWIN80_ENV
274     socket_t asocket = (socket_t)so;
275     struct msghdr msg;
276     size_t slen;
277 #else
278     struct socket *asocket = (struct socket *)so;
279     struct uio u;
280 #endif
281     afs_int32 code;
282     int i;
283     struct iovec iov[RX_MAXIOVECS];
284     int haveGlock = ISAFS_GLOCK();
285
286     AFS_STATCNT(osi_NetSend);
287     if (nvecs > RX_MAXIOVECS)
288         osi_Panic("osi_NetSend: %d: Too many iovecs.\n", nvecs);
289
290     for (i = 0; i < nvecs; i++)
291         iov[i] = dvec[i];
292
293     addr->sin_len = sizeof(struct sockaddr_in);
294
295     if ((afs_termState == AFSOP_STOP_RXK_LISTENER) ||
296         (afs_termState == AFSOP_STOP_COMPLETE))
297         return -1;
298
299     if (haveGlock)
300         AFS_GUNLOCK();
301
302 #if defined(KERNEL_FUNNEL)
303     thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
304 #endif
305 #ifdef AFS_DARWIN80_ENV
306     memset(&msg, 0, sizeof(struct msghdr));
307     msg.msg_name = addr;
308     msg.msg_namelen = ((struct sockaddr *)addr)->sa_len;
309     msg.msg_iov = &iov[0];
310     msg.msg_iovlen = nvecs;
311     code = sock_send(asocket, &msg, 0, &slen);
312 #else
313     u.uio_iov = &iov[0];
314     u.uio_iovcnt = nvecs;
315     u.uio_offset = 0;
316     u.uio_resid = alength;
317     u.uio_segflg = UIO_SYSSPACE;
318     u.uio_rw = UIO_WRITE;
319     u.uio_procp = NULL;
320     code = sosend(asocket, (struct sockaddr *)addr, &u, NULL, NULL, 0);
321 #endif
322
323 #if defined(KERNEL_FUNNEL)
324     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
325 #endif
326     if (haveGlock)
327         AFS_GLOCK();
328     return code;
329 }