rx-user-socket-buffer-sizes-20090110
[openafs.git] / src / rx / rx_user.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 /* rx_user.c contains routines specific to the user space UNIX implementation of rx */
11
12 #include <afsconfig.h>
13 #include <afs/param.h>
14
15 RCSID
16     ("$Header$");
17
18 # include <sys/types.h>
19 # include <errno.h>
20 # include <signal.h>
21 # include <string.h>
22 #ifdef AFS_NT40_ENV
23 # include <WINNT/syscfg.h>
24 #else
25 # include <sys/socket.h>
26 # include <sys/file.h>
27 # include <netdb.h>
28 # include <sys/stat.h>
29 # include <netinet/in.h>
30 # include <sys/time.h>
31 # include <net/if.h>
32 # include <sys/ioctl.h>
33 #endif
34 # include <fcntl.h>
35 #if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV) 
36 # include <sys/syscall.h>
37 #endif
38 #include <afs/afs_args.h>
39 #include <afs/afsutil.h>
40
41 #ifndef IPPORT_USERRESERVED
42 /* If in.h doesn't define this, define it anyway.  Unfortunately, defining
43    this doesn't put the code into the kernel to restrict kernel assigned
44    port numbers to numbers below IPPORT_USERRESERVED...  */
45 #define IPPORT_USERRESERVED 5000
46 # endif
47
48 #ifndef AFS_NT40_ENV
49 # include <sys/time.h>
50 #endif
51 # include "rx_internal.h"
52 # include "rx.h"
53 # include "rx_globals.h"
54
55 #ifdef AFS_PTHREAD_ENV
56 #include <assert.h>
57
58 /*
59  * The rx_if_init_mutex mutex protects the following global variables:
60  * Inited
61  */
62
63 pthread_mutex_t rx_if_init_mutex;
64 #define LOCK_IF_INIT assert(pthread_mutex_lock(&rx_if_init_mutex)==0)
65 #define UNLOCK_IF_INIT assert(pthread_mutex_unlock(&rx_if_init_mutex)==0)
66
67 /*
68  * The rx_if_mutex mutex protects the following global variables:
69  * myNetFlags
70  * myNetMTUs
71  * myNetMasks
72  */
73
74 pthread_mutex_t rx_if_mutex;
75 #define LOCK_IF assert(pthread_mutex_lock(&rx_if_mutex)==0)
76 #define UNLOCK_IF assert(pthread_mutex_unlock(&rx_if_mutex)==0)
77 #else
78 #define LOCK_IF_INIT
79 #define UNLOCK_IF_INIT
80 #define LOCK_IF
81 #define UNLOCK_IF
82 #endif /* AFS_PTHREAD_ENV */
83
84
85 /*
86  * Make a socket for receiving/sending IP packets.  Set it into non-blocking
87  * and large buffering modes.  If port isn't specified, the kernel will pick
88  * one.  Returns the socket (>= 0) on success.  Returns OSI_NULLSOCKET on
89  * failure. Port must be in network byte order. 
90  */
91 osi_socket
92 rxi_GetHostUDPSocket(u_int ahost, u_short port)
93 {
94     int binds, code = 0;
95     osi_socket socketFd = OSI_NULLSOCKET;
96     struct sockaddr_in taddr;
97     char *name = "rxi_GetUDPSocket: ";
98 #ifdef AFS_LINUX22_ENV
99 #if defined(ADAPT_PMTU)
100     int pmtu=IP_PMTUDISC_WANT;
101     int recverr=1;
102 #else
103     int pmtu=IP_PMTUDISC_DONT;
104 #endif
105 #endif
106 #if defined(HAVE_LINUX_ERRQUEUE_H) && defined(ADAPT_PMTU)
107 #include <linux/types.h>
108 #include <linux/errqueue.h>
109 #ifndef IP_MTU
110 #define IP_MTU 14
111 #endif
112 #endif
113
114 #if !defined(AFS_NT40_ENV) 
115     if (ntohs(port) >= IPPORT_RESERVED && ntohs(port) < IPPORT_USERRESERVED) {
116 /*      (osi_Msg "%s*WARNING* port number %d is not a reserved port number.  Use port numbers above %d\n", name, port, IPPORT_USERRESERVED);
117 */ ;
118     }
119     if (ntohs(port) > 0 && ntohs(port) < IPPORT_RESERVED && geteuid() != 0) {
120         (osi_Msg
121          "%sport number %d is a reserved port number which may only be used by root.  Use port numbers above %d\n",
122          name, ntohs(port), IPPORT_USERRESERVED);
123         goto error;
124     }
125 #endif
126     socketFd = socket(AF_INET, SOCK_DGRAM, 0);
127
128     if (socketFd < 0) {
129         perror("socket");
130         goto error;
131     }
132
133     taddr.sin_addr.s_addr = ahost;
134     taddr.sin_family = AF_INET;
135     taddr.sin_port = (u_short) port;
136 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
137     taddr.sin_len = sizeof(struct sockaddr_in);
138 #endif
139 #define MAX_RX_BINDS 10
140     for (binds = 0; binds < MAX_RX_BINDS; binds++) {
141         if (binds)
142             rxi_Delay(10);
143         code = bind(socketFd, (struct sockaddr *)&taddr, sizeof(taddr));
144         if (!code)
145             break;
146     }
147     if (code) {
148         perror("bind");
149         (osi_Msg "%sbind failed\n", name);
150         goto error;
151     }
152 #if !defined(AFS_NT40_ENV) 
153     /*
154      * Set close-on-exec on rx socket 
155      */
156     fcntl(socketFd, F_SETFD, 1);
157 #endif
158
159     /* Use one of three different ways of getting a socket buffer expanded to
160      * a reasonable size.
161      */
162     {
163         int greedy = 0;
164         int len1, len2;
165
166         len1 = 32766;
167         len2 = rx_UdpBufSize;
168
169         /* find the size closest to rx_UdpBufSize that will be accepted */
170         while (!greedy && len2 > len1) {
171             greedy =
172                 (setsockopt
173                   (socketFd, SOL_SOCKET, SO_RCVBUF, (char *)&len2,
174                    sizeof(len2)) >= 0);
175             if (!greedy)
176                 len2 /= 2;
177         }
178
179         /* but do not let it get smaller than 32K */ 
180         if (len2 < len1)
181             len2 = len1;
182
183         if (len1 < len2)
184             len1 = len2;
185
186
187         greedy =
188             (setsockopt
189              (socketFd, SOL_SOCKET, SO_SNDBUF, (char *)&len1,
190               sizeof(len1)) >= 0)
191             &&
192             (setsockopt
193              (socketFd, SOL_SOCKET, SO_RCVBUF, (char *)&len2,
194               sizeof(len2)) >= 0);
195         if (!greedy)
196             (osi_Msg "%s*WARNING* Unable to increase buffering on socket\n",
197              name);
198         MUTEX_ENTER(&rx_stats_mutex);
199         rx_stats.socketGreedy = greedy;
200         MUTEX_EXIT(&rx_stats_mutex);
201     }
202
203 #ifdef AFS_LINUX22_ENV
204     setsockopt(socketFd, SOL_IP, IP_MTU_DISCOVER, &pmtu, sizeof(pmtu));
205 #if defined(ADAPT_PMTU)
206     setsockopt(socketFd, SOL_IP, IP_RECVERR, &recverr, sizeof(recverr));
207 #endif
208 #endif
209     if (rxi_Listen(socketFd) < 0) {
210         goto error;
211     }
212
213     return socketFd;
214
215   error:
216 #ifdef AFS_NT40_ENV
217     if (socketFd >= 0)
218         closesocket(socketFd);
219 #else
220     if (socketFd >= 0)
221         close(socketFd);
222 #endif
223
224     return OSI_NULLSOCKET;
225 }
226
227 osi_socket
228 rxi_GetUDPSocket(u_short port)
229 {
230     return rxi_GetHostUDPSocket(htonl(INADDR_ANY), port);
231 }
232
233 void
234 osi_Panic(char *msg, ...)
235 {
236     va_list ap;
237     va_start(ap, msg);
238     (osi_Msg "Fatal Rx error: ");
239     (osi_VMsg msg, ap);
240     va_end(ap);
241     fflush(stderr);
242     fflush(stdout);
243     afs_abort();
244 }
245
246 /*
247  * osi_AssertFailU() -- used by the osi_Assert() macro.
248  */
249
250 void
251 osi_AssertFailU(const char *expr, const char *file, int line)
252 {
253     osi_Panic("assertion failed: %s, file: %s, line: %d\n", expr,
254               file, line);
255 }
256
257 #if defined(AFS_AIX32_ENV) && !defined(KERNEL)
258 #ifndef osi_Alloc
259 static const char memZero;
260 void *
261 osi_Alloc(afs_int32 x)
262 {
263     /* 
264      * 0-length allocs may return NULL ptr from malloc, so we special-case
265      * things so that NULL returned iff an error occurred 
266      */
267     if (x == 0)
268         return (void *)&memZero;
269     return(malloc(x));
270 }
271
272 void
273 osi_Free(void *x, afs_int32 size)
274 {
275     if (x == &memZero)
276         return;
277     free(x);
278 }
279 #endif
280 #endif /* defined(AFS_AIX32_ENV) && !defined(KERNEL) */
281
282 #define ADDRSPERSITE    16
283
284
285 static afs_uint32 rxi_NetAddrs[ADDRSPERSITE];   /* host order */
286 static int myNetMTUs[ADDRSPERSITE];
287 static int myNetMasks[ADDRSPERSITE];
288 static int myNetFlags[ADDRSPERSITE];
289 static u_int rxi_numNetAddrs;
290 static int Inited = 0;
291
292 #if defined(AFS_NT40_ENV) 
293 int
294 rxi_getaddr(void)
295 {
296     /* The IP address list can change so we must query for it */
297     rx_GetIFInfo();
298
299     /* we don't want to use the loopback adapter which is first */
300     /* this is a bad bad hack */
301     if (rxi_numNetAddrs > 1)
302         return htonl(rxi_NetAddrs[1]);  
303     else if (rxi_numNetAddrs > 0)
304         return htonl(rxi_NetAddrs[0]);
305     else
306         return 0;
307 }
308
309 /* 
310 ** return number of addresses 
311 ** and the addresses themselves in the buffer
312 ** maxSize - max number of interfaces to return.
313 */
314 int
315 rx_getAllAddr(afs_uint32 * buffer, int maxSize)
316 {
317     int count = 0, offset = 0;
318
319     /* The IP address list can change so we must query for it */
320     rx_GetIFInfo();
321
322 #ifdef AFS_DJGPP_ENV
323     /* we don't want to use the loopback adapter which is first */
324     /* this is a bad bad hack.
325      * and doesn't hold true on Windows.
326      */
327     if ( rxi_numNetAddrs > 1 )
328         offset = 1;
329 #endif /* AFS_DJGPP_ENV */
330
331     for (count = 0; offset < rxi_numNetAddrs && maxSize > 0;
332          count++, offset++, maxSize--)
333         buffer[count] = htonl(rxi_NetAddrs[offset]);
334
335     return count;
336 }
337
338 /* this function returns the total number of interface addresses
339  * the buffer has to be passed in by the caller. It also returns
340  * the matching interface mask and mtu.  All values are returned
341  * in network byte order.
342  */
343 int
344 rx_getAllAddrMaskMtu(afs_uint32 addrBuffer[], afs_uint32 maskBuffer[],
345                      afs_uint32 mtuBuffer[], int maxSize)
346 {
347     int count = 0, offset = 0;
348
349     /* The IP address list can change so we must query for it */
350     rx_GetIFInfo();
351
352 #ifdef AFS_DJGPP_ENV
353     /* we don't want to use the loopback adapter which is first */
354     /* this is a bad bad hack.
355      * and doesn't hold true on Windows.
356      */
357     if ( rxi_numNetAddrs > 1 )
358         offset = 1;
359 #endif /* AFS_DJGPP_ENV */
360
361     for (count = 0; 
362          offset < rxi_numNetAddrs && maxSize > 0;
363          count++, offset++, maxSize--) {
364         addrBuffer[count] = htonl(rxi_NetAddrs[offset]);
365         maskBuffer[count] = htonl(myNetMasks[offset]);
366         mtuBuffer[count]  = htonl(myNetMTUs[offset]);
367     }
368     return count;
369 }
370 #endif
371
372 #ifdef AFS_NT40_ENV
373 extern int rxinit_status;
374 void 
375 rxi_InitMorePackets(void) {
376     int npackets, ncbufs;
377
378     ncbufs = (rx_maxJumboRecvSize - RX_FIRSTBUFFERSIZE);
379     if (ncbufs > 0) {
380         ncbufs = ncbufs / RX_CBUFFERSIZE;
381         npackets = rx_initSendWindow - 1;
382         rxi_MorePackets(npackets * (ncbufs + 1));
383     }
384 }
385 void
386 rx_GetIFInfo(void)
387 {
388     u_int maxsize;
389     u_int rxsize;
390     afs_uint32 i;
391
392     LOCK_IF_INIT;
393     if (Inited) {
394         if (Inited < 2 && rxinit_status == 0) {
395             /* We couldn't initialize more packets earlier.
396              * Do it now. */
397             rxi_InitMorePackets();
398             Inited = 2;
399         }
400         UNLOCK_IF_INIT;
401         return;
402     }
403     Inited = 1;
404     UNLOCK_IF_INIT;
405
406     LOCK_IF;
407     rxi_numNetAddrs = ADDRSPERSITE;
408     (void)syscfg_GetIFInfo(&rxi_numNetAddrs, rxi_NetAddrs,
409                            myNetMasks, myNetMTUs, myNetFlags);
410
411     for (i = 0; i < rxi_numNetAddrs; i++) {
412         rxsize = rxi_AdjustIfMTU(myNetMTUs[i] - RX_IPUDP_SIZE);
413         maxsize =
414             rxi_nRecvFrags * rxsize + (rxi_nRecvFrags - 1) * UDP_HDR_SIZE;
415         maxsize = rxi_AdjustMaxMTU(rxsize, maxsize);
416         if (rx_maxReceiveSize < maxsize) {
417             rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxsize);
418             rx_maxReceiveSize =
419                 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
420         }
421
422     }
423     UNLOCK_IF;
424
425     /*
426      * If rxinit_status is still set, rx_InitHost() has yet to be called
427      * and we therefore do not have any mutex locks initialized.  As a
428      * result we cannot call rxi_MorePackets() without crashing.
429      */
430     if (rxinit_status)
431         return;
432
433     rxi_InitMorePackets();
434 }
435 #endif
436
437 static afs_uint32
438 fudge_netmask(afs_uint32 addr)
439 {
440     afs_uint32 msk;
441
442     if (IN_CLASSA(addr))
443         msk = IN_CLASSA_NET;
444     else if (IN_CLASSB(addr))
445         msk = IN_CLASSB_NET;
446     else if (IN_CLASSC(addr))
447         msk = IN_CLASSC_NET;
448     else
449         msk = 0;
450
451     return msk;
452 }
453
454
455
456 #if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV) 
457 int
458 rxi_syscall(a3, a4, a5)
459      afs_uint32 a3, a4;
460      void *a5;
461 {
462     afs_uint32 rcode;
463     void (*old) ();
464
465     old = (void (*)())signal(SIGSYS, SIG_IGN);
466
467 #if defined(AFS_SGI_ENV)
468     rcode = afs_syscall(a3, a4, a5);
469 #else
470     rcode = syscall(AFS_SYSCALL, 28 /* AFSCALL_CALL */ , a3, a4, a5);
471 #endif /* AFS_SGI_ENV */
472
473     signal(SIGSYS, old);
474
475     return rcode;
476 }
477 #endif /* AFS_AIX_ENV */
478
479 #ifndef AFS_NT40_ENV
480 void
481 rx_GetIFInfo(void)
482 {
483     int s;
484     int i, j, len, res;
485     struct ifconf ifc;
486     struct ifreq ifs[ADDRSPERSITE];
487     struct ifreq *ifr;
488 #ifdef  AFS_AIX41_ENV
489     char buf[BUFSIZ], *cp, *cplim;
490 #endif
491     struct sockaddr_in *a;
492
493     LOCK_IF_INIT;
494     if (Inited) {
495         UNLOCK_IF_INIT;
496         return;
497     }
498     Inited = 1;
499     UNLOCK_IF_INIT;
500     LOCK_IF;
501     rxi_numNetAddrs = 0;
502     memset(rxi_NetAddrs, 0, sizeof(rxi_NetAddrs));
503     memset(myNetFlags, 0, sizeof(myNetFlags));
504     memset(myNetMTUs, 0, sizeof(myNetMTUs));
505     memset(myNetMasks, 0, sizeof(myNetMasks));
506     UNLOCK_IF;
507     s = socket(AF_INET, SOCK_DGRAM, 0);
508     if (s < 0)
509         return;
510
511 #ifdef  AFS_AIX41_ENV
512     ifc.ifc_len = sizeof(buf);
513     ifc.ifc_buf = buf;
514     ifr = ifc.ifc_req;
515 #else
516     ifc.ifc_len = sizeof(ifs);
517     ifc.ifc_buf = (caddr_t) & ifs[0];
518     memset(&ifs[0], 0, sizeof(ifs));
519 #endif
520     res = ioctl(s, SIOCGIFCONF, &ifc);
521     if (res < 0) {
522         /* fputs(stderr, "ioctl error IFCONF\n"); */
523         close(s);
524         return;
525     }
526
527     LOCK_IF;
528 #ifdef  AFS_AIX41_ENV
529 #define size(p) MAX((p).sa_len, sizeof(p))
530     cplim = buf + ifc.ifc_len;  /*skip over if's with big ifr_addr's */
531     for (cp = buf; cp < cplim;
532          cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))) {
533         if (rxi_numNetAddrs >= ADDRSPERSITE)
534             break;
535
536         ifr = (struct ifreq *)cp;
537 #else
538     len = ifc.ifc_len / sizeof(struct ifreq);
539     if (len > ADDRSPERSITE)
540         len = ADDRSPERSITE;
541
542     for (i = 0; i < len; ++i) {
543         ifr = &ifs[i];
544         res = ioctl(s, SIOCGIFADDR, ifr);
545 #endif
546         if (res < 0) {
547             /* fputs(stderr, "ioctl error IFADDR\n");
548              * perror(ifr->ifr_name);   */
549             continue;
550         }
551         a = (struct sockaddr_in *)&ifr->ifr_addr;
552         if (a->sin_family != AF_INET)
553             continue;
554         rxi_NetAddrs[rxi_numNetAddrs] = ntohl(a->sin_addr.s_addr);
555         if (rxi_NetAddrs[rxi_numNetAddrs] == 0x7f000001) {
556             /* we don't really care about "localhost" */
557             continue;
558         }
559         for (j = 0; j < rxi_numNetAddrs; j++) {
560             if (rxi_NetAddrs[j] == rxi_NetAddrs[rxi_numNetAddrs])
561                 break;
562         }
563         if (j < rxi_numNetAddrs)
564             continue;
565
566         /* fprintf(stderr, "if %s addr=%x\n", ifr->ifr_name,
567          * rxi_NetAddrs[rxi_numNetAddrs]); */
568
569 #ifdef SIOCGIFFLAGS
570         res = ioctl(s, SIOCGIFFLAGS, ifr);
571         if (res == 0) {
572             myNetFlags[rxi_numNetAddrs] = ifr->ifr_flags;
573 #ifdef IFF_LOOPBACK
574             /* Handle aliased loopbacks as well. */
575             if (ifr->ifr_flags & IFF_LOOPBACK)
576                 continue;
577 #endif
578             /* fprintf(stderr, "if %s flags=%x\n", 
579              * ifr->ifr_name, ifr->ifr_flags); */
580         } else {                /*
581                                  * fputs(stderr, "ioctl error IFFLAGS\n");
582                                  * perror(ifr->ifr_name); */
583         }
584 #endif /* SIOCGIFFLAGS */
585
586 #if !defined(AFS_AIX_ENV)  && !defined(AFS_LINUX20_ENV)
587         /* this won't run on an AIX system w/o a cache manager */
588         rxi_syscallp = rxi_syscall;
589 #endif
590
591         /* If I refer to kernel extensions that aren't loaded on AIX, the 
592          * program refuses to load and run, so I simply can't include the 
593          * following code.  Fortunately, AIX is the one operating system in
594          * which the subsequent ioctl works reliably. */
595         if (rxi_syscallp) {
596             if ((*rxi_syscallp) (20 /*AFSOP_GETMTU */ ,
597                                  htonl(rxi_NetAddrs[rxi_numNetAddrs]),
598                                  &(myNetMTUs[rxi_numNetAddrs]))) {
599                 /* fputs(stderr, "syscall error GETMTU\n");
600                  * perror(ifr->ifr_name); */
601                 myNetMTUs[rxi_numNetAddrs] = 0;
602             }
603             if ((*rxi_syscallp) (42 /*AFSOP_GETMASK */ ,
604                                  htonl(rxi_NetAddrs[rxi_numNetAddrs]),
605                                  &(myNetMasks[rxi_numNetAddrs]))) {
606                 /* fputs(stderr, "syscall error GETMASK\n");
607                  * perror(ifr->ifr_name); */
608                 myNetMasks[rxi_numNetAddrs] = 0;
609             } else
610                 myNetMasks[rxi_numNetAddrs] =
611                     ntohl(myNetMasks[rxi_numNetAddrs]);
612             /* fprintf(stderr, "if %s mask=0x%x\n", 
613              * ifr->ifr_name, myNetMasks[rxi_numNetAddrs]); */
614         }
615
616         if (myNetMTUs[rxi_numNetAddrs] == 0) {
617             myNetMTUs[rxi_numNetAddrs] = OLD_MAX_PACKET_SIZE + RX_IPUDP_SIZE;
618 #ifdef SIOCGIFMTU
619             res = ioctl(s, SIOCGIFMTU, ifr);
620             if ((res == 0) && (ifr->ifr_metric > 128)) {        /* sanity check */
621                 myNetMTUs[rxi_numNetAddrs] = ifr->ifr_metric;
622                 /* fprintf(stderr, "if %s mtu=%d\n", 
623                  * ifr->ifr_name, ifr->ifr_metric); */
624             } else {
625                 /* fputs(stderr, "ioctl error IFMTU\n");
626                  * perror(ifr->ifr_name); */
627             }
628 #endif
629         }
630
631         if (myNetMasks[rxi_numNetAddrs] == 0) {
632             myNetMasks[rxi_numNetAddrs] =
633                 fudge_netmask(rxi_NetAddrs[rxi_numNetAddrs]);
634 #ifdef SIOCGIFNETMASK
635             res = ioctl(s, SIOCGIFNETMASK, ifr);
636             if ((res == 0)) {
637                 a = (struct sockaddr_in *)&ifr->ifr_addr;
638                 myNetMasks[rxi_numNetAddrs] = ntohl(a->sin_addr.s_addr);
639                 /* fprintf(stderr, "if %s subnetmask=0x%x\n", 
640                  * ifr->ifr_name, myNetMasks[rxi_numNetAddrs]); */
641             } else {
642                 /* fputs(stderr, "ioctl error IFMASK\n");
643                  * perror(ifr->ifr_name); */
644             }
645 #endif
646         }
647
648         if (rxi_NetAddrs[rxi_numNetAddrs] != 0x7f000001) {      /* ignore lo0 */
649             int maxsize;
650             maxsize =
651                 rxi_nRecvFrags * (myNetMTUs[rxi_numNetAddrs] - RX_IP_SIZE);
652             maxsize -= UDP_HDR_SIZE;    /* only the first frag has a UDP hdr */
653             if (rx_maxReceiveSize < maxsize)
654                 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxsize);
655             ++rxi_numNetAddrs;
656         }
657     }
658     UNLOCK_IF;
659     close(s);
660
661     /* have to allocate at least enough to allow a single packet to reach its
662      * maximum size, so ReadPacket will work.  Allocate enough for a couple
663      * of packets to do so, for good measure */
664     {
665         int npackets, ncbufs;
666
667         rx_maxJumboRecvSize =
668             RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
669             (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
670         rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
671         ncbufs = (rx_maxJumboRecvSize - RX_FIRSTBUFFERSIZE);
672         if (ncbufs > 0) {
673             ncbufs = ncbufs / RX_CBUFFERSIZE;
674             npackets = rx_initSendWindow - 1;
675             rxi_MorePackets(npackets * (ncbufs + 1));
676         }
677     }
678 }
679 #endif /* AFS_NT40_ENV */
680
681 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
682  * to get interesting information.
683  * Curiously enough, the rx_peerHashTable_lock currently protects the
684  * Inited variable (and hence rx_GetIFInfo). When the fs suite uses
685  * pthreads, this issue will need to be revisited.
686  */
687
688 void
689 rxi_InitPeerParams(struct rx_peer *pp)
690 {
691     afs_uint32 ppaddr;
692     u_short rxmtu;
693     int ix;
694 #if defined(ADAPT_PMTU) && defined(IP_MTU)
695     int sock;
696     struct sockaddr_in addr;
697 #endif
698
699     LOCK_IF_INIT;
700     if (!Inited) {
701         UNLOCK_IF_INIT;
702         /*
703          * there's a race here since more than one thread could call
704          * rx_GetIFInfo.  The race stops in rx_GetIFInfo.
705          */
706         rx_GetIFInfo();
707     } else {
708         UNLOCK_IF_INIT;
709     }
710
711 #ifdef ADAPT_MTU
712     /* try to second-guess IP, and identify which link is most likely to
713      * be used for traffic to/from this host. */
714     ppaddr = ntohl(pp->host);
715     
716     pp->ifMTU = 0;
717     pp->timeout.sec = 2;
718     pp->rateFlag = 2;         /* start timing after two full packets */
719     /* I don't initialize these, because I presume they are bzero'd... 
720      * pp->burstSize pp->burst pp->burstWait.sec pp->burstWait.usec
721      * pp->timeout.usec */
722     
723     LOCK_IF;
724     for (ix = 0; ix < rxi_numNetAddrs; ++ix) {
725         if ((rxi_NetAddrs[ix] & myNetMasks[ix]) == (ppaddr & myNetMasks[ix])) {
726 #ifdef IFF_POINTOPOINT
727             if (myNetFlags[ix] & IFF_POINTOPOINT)
728                 pp->timeout.sec = 4;
729 #endif /* IFF_POINTOPOINT */
730             rxmtu = myNetMTUs[ix] - RX_IPUDP_SIZE;
731             if (rxmtu < RX_MIN_PACKET_SIZE)
732                 rxmtu = RX_MIN_PACKET_SIZE;
733             if (pp->ifMTU < rxmtu)
734                 pp->ifMTU = MIN(rx_MyMaxSendSize, rxmtu);
735         }
736     }
737     UNLOCK_IF;
738     if (!pp->ifMTU) {         /* not local */
739         pp->timeout.sec = 3;
740         pp->ifMTU = MIN(rx_MyMaxSendSize, RX_REMOTE_PACKET_SIZE);
741     }
742 #else /* ADAPT_MTU */
743     pp->rateFlag = 2;           /* start timing after two full packets */
744     pp->timeout.sec = 2;
745     pp->ifMTU = MIN(rx_MyMaxSendSize, OLD_MAX_PACKET_SIZE);
746 #endif /* ADAPT_MTU */
747 #if defined(ADAPT_PMTU) && defined(IP_MTU)
748     sock=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
749     if (sock >= 0) {
750       addr.sin_family = AF_INET;
751       addr.sin_addr.s_addr = pp->host;
752       addr.sin_port = pp->port;
753       if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
754         int mtu=0;
755         socklen_t s = sizeof(mtu);
756         if (getsockopt(sock, SOL_IP, IP_MTU, &mtu, &s)== 0) {
757           pp->ifMTU = MIN(mtu - RX_IPUDP_SIZE, pp->ifMTU);
758         }
759       }
760       close(sock);
761     }
762 #endif
763     pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
764     pp->maxMTU = OLD_MAX_PACKET_SIZE;   /* for compatibility with old guys */
765     pp->natMTU = MIN((int)pp->ifMTU, OLD_MAX_PACKET_SIZE);
766     pp->maxDgramPackets =
767         MIN(rxi_nDgramPackets,
768             rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
769     pp->ifDgramPackets =
770         MIN(rxi_nDgramPackets,
771             rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
772     pp->maxDgramPackets = 1;
773     /* Initialize slow start parameters */
774     pp->MTU = MIN(pp->natMTU, pp->maxMTU);
775     pp->cwind = 1;
776     pp->nDgramPackets = 1;
777     pp->congestSeq = 0;
778 }
779
780 /* Don't expose jumobgram internals. */
781 void
782 rx_SetNoJumbo(void)
783 {
784     rx_maxReceiveSize = OLD_MAX_PACKET_SIZE;
785     rxi_nSendFrags = rxi_nRecvFrags = 1;
786 }
787
788 /* Override max MTU.  If rx_SetNoJumbo is called, it must be 
789    called before calling rx_SetMaxMTU since SetNoJumbo clobbers rx_maxReceiveSize */
790 void
791 rx_SetMaxMTU(int mtu)
792 {
793     rx_MyMaxSendSize = rx_maxReceiveSizeUser = rx_maxReceiveSize = mtu;
794 }
795
796 #if defined(HAVE_LINUX_ERRQUEUE_H) && defined(ADAPT_PMTU)
797 int
798 rxi_HandleSocketError(int socket)
799 {
800     struct msghdr msg;
801     struct cmsghdr *cmsg;
802     struct sock_extended_err *err;
803     struct sockaddr_in addr;
804     struct sockaddr *offender;
805     char controlmsgbuf[256];
806     int ret=0;
807     int code;
808
809     msg.msg_name = &addr;
810     msg.msg_namelen = sizeof(addr);
811     msg.msg_iov = NULL;
812     msg.msg_iovlen = 0;
813     msg.msg_control = controlmsgbuf;
814     msg.msg_controllen = 256;
815     msg.msg_flags = 0;
816     code = recvmsg(socket, &msg, MSG_ERRQUEUE|MSG_DONTWAIT|MSG_TRUNC);
817
818     if (code < 0 || !(msg.msg_flags & MSG_ERRQUEUE))
819         goto out;
820
821     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
822        if ((char *)cmsg - controlmsgbuf > msg.msg_controllen - CMSG_SPACE(0) ||
823            (char *)cmsg - controlmsgbuf > msg.msg_controllen - CMSG_SPACE(cmsg->cmsg_len) ||
824            cmsg->cmsg_len == 0) {
825            cmsg = 0;
826            break;
827         }
828         if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR)
829             break;
830     }
831     if (!cmsg)
832         goto out;
833     ret=1;
834     err =(struct sock_extended_err *) CMSG_DATA(cmsg);
835     
836     if (err->ee_errno == EMSGSIZE && err->ee_info >= 68) {
837         rxi_SetPeerMtu(addr.sin_addr.s_addr, addr.sin_port,
838                        err->ee_info - RX_IPUDP_SIZE);
839     }
840     /* other DEST_UNREACH's and TIME_EXCEEDED should be dealt with too */
841     
842 out:
843     return ret;
844 }
845 #endif