dux-min-direct-20040808
[openafs.git] / src / rx / DUX / 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 RCSID
14     ("$Header$");
15
16 #ifdef AFS_DUX40_ENV
17 #include "rx/rx_kcommon.h"
18
19
20 static struct protosw parent_proto;     /* udp proto switch */
21 static void rxk_input(struct mbuf *am, int iphlen);
22 static void rxk_fasttimo(void);
23
24 /* start intercepting basic calls */
25 rxk_init()
26 {
27     register struct protosw *tpro, *last;
28     if (rxk_initDone)
29         return 0;
30
31     last = inetdomain.dom_protoswNPROTOSW;
32     for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
33         if (tpro->pr_protocol == IPPROTO_UDP) {
34             /* force UDP checksumming on for AFS    */
35             extern int udpcksum;
36             udpcksum = 1;
37             memcpy(&parent_proto, tpro, sizeof(parent_proto));
38             tpro->pr_input = rxk_input;
39             tpro->pr_fasttimo = rxk_fasttimo;
40             /*
41              * don't bother with pr_drain and pr_ctlinput
42              * until we have something to do
43              */
44             rxk_initDone = 1;
45             return 0;
46         }
47     osi_Panic("inet:no udp");
48 }
49
50
51 static void
52 rxk_input(struct mbuf *am, int iphlen)
53 {
54     void (*tproc) ();
55     register unsigned short *tsp;
56     int hdr;
57     struct udphdr *tu;
58     register struct ip *ti;
59     struct udpiphdr *tvu;
60     register int i;
61     char *phandle;
62     afs_int32 code;
63     struct sockaddr_in taddr;
64     int tlen;
65     short port;
66     int data_len, comp_sum;
67
68     SPLVAR;
69     NETPRI;
70
71     /* make sure we have base ip and udp headers in first mbuf */
72     if (iphlen > sizeof(struct ip)) {
73         ip_stripoptions(am, NULL, NULL);
74         iphlen = sizeof(struct ip);
75     }
76
77     if (am->m_len < sizeof(struct udpiphdr)) {
78         am = m_pullup(am, sizeof(struct udpiphdr));
79         if (!am) {
80             USERPRI;
81             return;
82         }
83     }
84
85     ti = mtod(am, struct ip *);
86     /* skip basic ip hdr */
87     tu = (struct udphdr *)(((char *)ti) + sizeof(struct ip));
88
89     /* now read the port out */
90     port = tu->uh_dport;
91
92     if (port) {
93         for (tsp = rxk_ports, i = 0; i < MAXRXPORTS; i++) {
94             if (*tsp++ == port) {
95                 /* checksum the packet */
96                 /*
97                  * Make mbuf data length reflect UDP length.
98                  * If not enough data to reflect UDP length, drop.
99                  */
100                 tvu = (struct udpiphdr *)ti;
101                 tlen = ntohs((u_short) tvu->ui_ulen);
102                 if ((int)ti->ip_len != tlen) {
103                     if (tlen > (int)ti->ip_len) {
104                         m_free(am);
105                         USERPRI;
106                         return;
107                     }
108                     m_adj(am, tlen - (int)ti->ip_len);
109                 }
110                 /* deliver packet to rx */
111                 taddr.sin_family = AF_INET;     /* compute source address */
112                 taddr.sin_port = tu->uh_sport;
113                 taddr.sin_addr.s_addr = ti->ip_src.s_addr;
114                 taddr.sin_len = sizeof(taddr);
115                 tvu = (struct udpiphdr *)ti;    /* virtual udp structure, for cksum */
116                 /* handle the checksum.  Note that this code damages the actual ip
117                  * header (replacing it with the virtual one, which is the same size),
118                  * so we must ensure we get everything out we need, first */
119                 if (tu->uh_sum != 0) {
120                     /* if the checksum is there, always check it. It's crazy not
121                      * to, unless you can really be sure that your
122                      * underlying network (and interfaces and drivers and
123                      * DMA hardware, etc!) is error-free. First, fill
124                      * in entire virtual ip header. */
125                     tvu->ui_i.fill[0] = 0;
126                     tvu->ui_i.fill[1] = 0;
127                     tvu->ui_x1 = 0;
128                     tvu->ui_len = tvu->ui_ulen;
129                     tlen = ntohs((unsigned short)(tvu->ui_ulen));
130                     if (in_cksum(am, sizeof(struct ip) + tlen)) {
131                         /* checksum, including cksum field, doesn't come out 0, so
132                          * this packet is bad */
133                         m_freem(am);
134                         USERPRI;
135                         return;
136                     }
137                 }
138
139                 /*
140                  * 28 is IP (20) + UDP (8) header.  ulen includes
141                  * udp header, and we *don't* tell RX about udp
142                  * header either.  So, we remove those 8 as well.
143                  */
144                 data_len = ntohs(tu->uh_ulen);
145                 data_len -= 8;
146                 AFS_RXGLOCK();
147                 if (!(*rxk_GetPacketProc) (&phandle, data_len)) {
148                     if (rx_mb_to_packet(am, m_freem, 28, data_len, phandle)) {
149                         /* XXX should just increment counter here.. */
150                         printf("rx: truncated UDP packet\n");
151                         rxi_FreePacket(phandle);
152                     } else
153                         (*rxk_PacketArrivalProc) (phandle, &taddr,
154                                                   rxk_portRocks[i], data_len);
155                 } else
156                     m_freem(am);
157                 AFS_RXGUNLOCK();
158                 USERPRI;
159                 return;
160             }
161         }
162     }
163
164     /* if we get here, try to deliver packet to udp */
165     if (tproc = parent_proto.pr_input)
166         (*tproc) (am, iphlen);
167     USERPRI;
168     return;
169 }
170
171
172 /* 
173  * UDP fast timer to raise events for all but Solaris and NCR. 
174  * Called about 5 times per second (at unknown priority?).  Must go to
175  * splnet or obtain global lock before touching anything significant.
176  */
177 static void
178 rxk_fasttimo(void)
179 {
180     void (*tproc) ();
181     struct clock temp;
182
183     /* do rx fasttimo processing here */
184     rxevent_RaiseEvents(&temp);
185     if (tproc = parent_proto.pr_fasttimo)
186         (*tproc) ();
187 }
188
189
190 /* rx_NetSend - send asize bytes at adata from asocket to host at addr.
191  *
192  * Now, why do we allocate a new buffer when we could theoretically use the one
193  * pointed to by adata?  Because PRU_SEND returns after queueing the message,
194  * not after sending it.  If the sender changes the data after queueing it,
195  * we'd see the already-queued data change.  One attempt to fix this without
196  * adding a copy would be to have this function wait until the datagram is
197  * sent; however this doesn't work well.  In particular, if a host is down, and
198  * an ARP fails to that host, this packet will be queued until the ARP request
199  * comes back, which could be hours later.  We can't block in this routine that
200  * long, since it prevents RPC timeouts from happening.
201  */
202 /* XXX In the brave new world, steal the data bufs out of the rx_packet iovec,
203  * and just queue those.  XXX
204  */
205
206 /* set lock on sockbuf sb; can't call sblock since we're at interrupt level
207  * sometimes */
208 static
209 trysblock(sb)
210      register struct sockbuf *sb;
211 {
212     AFS_STATCNT(trysblock);
213     if (sb->sb_flags & SB_LOCK) {
214         return -1;              /* can't lock socket */
215     }
216     sb->sb_flags |= SB_LOCK;
217     return 0;
218 }
219
220 int
221 osi_NetSend(osi_socket asocket, struct sockaddr_in *addr, struct iovec *dvec,
222             int nvec, afs_int32 asize, int istack)
223 {
224     register struct mbuf *tm, *um;
225     register afs_int32 code;
226     int s;
227     struct mbuf *top = 0;
228     register struct mbuf *m, **mp;
229     int len;
230     char *tdata;
231     caddr_t tpa;
232     int i, tl, rlen;
233     int mlen;
234     int haveGlock;
235
236     AFS_STATCNT(osi_NetSend);
237
238 /* Actually, the Ultrix way is as good as any for us, so we don't bother with
239  * special mbufs any more.  Used to think we could get away with not copying
240  * the data to the interface, but there's no way to tell the caller not to
241  * reuse the buffers after sending, so we lost out on that trick anyway */
242
243     s = splnet();
244     mp = &top;
245     i = 0;
246     tdata = dvec[i].iov_base;
247     tl = dvec[i].iov_len;
248     while (1) {
249         mlen = MLEN;
250         if (top == 0) {
251             MGETHDR(m, M_DONTWAIT, MT_DATA);
252             if (!m) {
253                 splx(s);
254                 return 1;
255             }
256             mlen = MHLEN;
257             m->m_pkthdr.len = 0;
258             m->m_pkthdr.rcvif = NULL;
259         } else
260             MGET(m, M_DONTWAIT, MT_DATA);
261         if (!m) {
262             /* can't get an mbuf, give up */
263             if (top)
264                 m_freem(top);   /* free mbuf list we're building */
265             splx(s);
266             return 1;
267         }
268         /*
269          * WARNING: the `4 * MLEN' is somewhat dubious.  It is better than
270          * `NBPG', which may have no relation to `CLBYTES'.  Also, `CLBYTES'
271          * may be so large that we never use clusters, resulting in far
272          * too many mbufs being used.  It is often better to briefly use
273          * a cluster, even if we are only using a portion of it.  Since
274          * we are on the xmit side, it shouldn't end up sitting on a queue
275          * for a potentially unbounded time (except perhaps if we are talking
276          * to ourself).
277          */
278         if (asize >= 4 * MLEN) {        /* try to get cluster mbuf */
279             register struct mbuf *p;
280
281             /* different algorithms for getting cluster mbuf */
282             MCLGET(m, M_DONTWAIT);
283             if ((m->m_flags & M_EXT) == 0)
284                 goto nopages;
285             mlen = MCLBYTES;
286
287             /* now compute usable size */
288             len = MIN(mlen, asize);
289 /* Should I look at MAPPED_MBUFS??? */
290         } else {
291           nopages:
292             len = MIN(mlen, asize);
293         }
294         m->m_len = 0;
295         *mp = m;                /* XXXX */
296         top->m_pkthdr.len += len;
297         tpa = mtod(m, caddr_t);
298         while (len) {
299             rlen = MIN(len, tl);
300             memcpy(tpa, tdata, rlen);
301             asize -= rlen;
302             len -= rlen;
303             tpa += rlen;
304             m->m_len += rlen;
305             tdata += rlen;
306             tl -= rlen;
307             if (tl <= 0) {
308                 i++;
309                 if (i > nvec) {
310                     /* shouldn't come here! */
311                     asize = 0;  /* so we make progress toward completion */
312                     break;
313                 }
314                 tdata = dvec[i].iov_base;
315                 tl = dvec[i].iov_len;
316             }
317         }
318         *mp = m;
319         mp = &m->m_next;
320         if (asize <= 0)
321             break;
322     }
323     tm = top;
324
325     tm->m_act = NULL;
326
327     /* setup mbuf corresponding to destination address */
328     um = m_get(M_DONTWAIT, MT_SONAME);
329     if (!um) {
330         if (top)
331             m_freem(top);       /* free mbuf chain */
332         /* if this were vfs40, we'd do sbunlock(asocket, &asocket->so_snd), but
333          * we don't do the locking at all for vfs40 systems */
334         splx(s);
335         return 1;
336     }
337     memcpy(mtod(um, caddr_t), addr, sizeof(*addr));
338     um->m_len = sizeof(*addr);
339     /* note that udp_usrreq frees funny mbuf.  We hold onto data, but mbuf
340      * around it is gone.  we free address ourselves.  */
341     haveGlock = ISAFS_GLOCK();
342     if (haveGlock) {
343         AFS_GUNLOCK();
344     }
345     SOCKET_LOCK(asocket);
346     code = (*asocket->so_proto->pr_usrreq) (asocket, PRU_SEND, tm, um, 0);
347     SOCKET_UNLOCK(asocket);
348     if (haveGlock) {
349         AFS_GLOCK();
350     }
351     splx(s);
352     m_free(um);
353
354     return code;
355 }
356
357 #endif /* AFS_DUX40_ENV */