win95-initial-port-20010430
[openafs.git] / src / rx / rx_packet.h
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 #ifndef _RX_PACKET_
11 #define _RX_PACKET_
12 #ifndef UKERNEL
13 #if defined(AFS_NT40_ENV) || defined(AFS_DJGPP_ENV)
14 #include "rx_xmit_nt.h"
15 #endif
16 #ifndef AFS_NT40_ENV
17 #include <sys/uio.h>
18 #endif /* !AFS_NT40_ENV */
19 #endif /* !UKERNEL */
20 /* this file includes the macros and decls which depend on packet
21  * format, and related packet manipulation macros.  Note that code
22  * which runs at NETPRI should not sleep, or AIX will panic */
23 /* There are some assumptions that various code makes -- I'll try to 
24  * express them all here: 
25  * 1.  rx_ReceiveAckPacket assumes that it can get an entire ack
26  * contiguous in the first iovec.  As a result, the iovec buffers must
27  * be >= sizeof (struct rx_ackpacket)
28  * 2. All callers of rx_Pullup besides rx_ReceiveAckPacket try to pull
29  * up less data than rx_ReceiveAckPacket does.
30  * 3. rx_GetInt32 and rx_PutInt32 (and the slow versions of same) assume
31  * that the iovec buffers are all integral multiples of the word size,
32  * and that the offsets are as well.
33  */
34
35
36 #if defined(AFS_NT40_ENV) || defined(AFS_DJGPP_ENV)
37 #ifndef MIN
38 #define MIN(a,b)  ((a)<(b)?(a):(b))
39 #endif
40 #ifndef MAX
41 #define MAX(a,b)  ((a)>(b)?(a):(b))
42 #endif
43 #else /* AFS_NT40_ENV */
44 #if !defined(AFS_DARWIN_ENV) && !defined(AFS_USR_DARWIN_ENV) && !defined(AFS_FBSD_ENV) && !defined(AFS_USR_FBSD_ENV)
45 #include <sys/sysmacros.h>      /* MIN, MAX on Solaris */
46 #endif
47 #include <sys/param.h>          /* MIN, MAX elsewhere */
48 #endif /* AFS_NT40_ENV */
49
50 #define IPv6_HDR_SIZE           40      /* IPv6 Header */
51 #define IPv6_FRAG_HDR_SIZE       8      /* IPv6 Fragment Header */
52 #define UDP_HDR_SIZE             8      /* UDP Header */
53 #define RX_IP_SIZE              (IPv6_HDR_SIZE + IPv6_FRAG_HDR_SIZE)
54 #define RX_IPUDP_SIZE           (RX_IP_SIZE + UDP_HDR_SIZE)
55
56 /* REMOTE_PACKET_SIZE is currently the same as local.  This is because REMOTE
57  * is defined much too generally for my tastes, and includes the case of 
58  * multiple class C nets connected with a router within one campus or MAN. 
59  * I don't want to make local performance suffer just because of some
60  * out-dated protocol that used to be in use on the NSFANET that's
61  * practically unused anymore.  Any modern IP implementation will be
62  * using MTU discovery, and even old routers shouldn't frag packets
63  * when sending from one connected network directly to another.  Maybe
64  * the next release of RX will do MTU discovery. */
65
66 /* MTUXXX the various "MAX" params here must be rationalized.  From now on,
67  * the MAX packet size will be the maximum receive size, but the maximum send
68  * size will be larger than that. */
69
70 #ifdef notdef 
71 /*  some sample MTUs 
72            4352   what FDDI(RFC1188) uses... Larger? 
73            4096   VJ's recommendation for FDDI 
74           17914   what IBM 16MB TR  uses   
75            8166   IEEE 802.4 
76            4464   IEEE 802.5 MAX
77            2002   IEEE 802.5 Recommended 
78            1500   what Ethernet uses 
79            1492   what 802.3 uses ( 8 bytes for 802.2 SAP )
80            9180   Classical IP over ATM (RFC2225)
81 */
82
83 /* * * * these are the old defines
84 */
85  define RX_MAX_PACKET_SIZE      (RX_MAX_DL_MTU -RX_IPUDP_SIZE)
86
87  define RX_MAX_PACKET_DATA_SIZE (RX_MAX_PACKET_SIZE-RX_HEADER_SIZE)
88  ifdef AFS_HPUX_ENV
89 /* HPUX by default uses an 802.3 size, and it's not evident from SIOCGIFCONF */
90  define RX_LOCAL_PACKET_SIZE    (1492 - RX_IPUDP_SIZE)   
91  define RX_REMOTE_PACKET_SIZE   (1492 - RX_IPUDP_SIZE)   
92  else
93  define RX_LOCAL_PACKET_SIZE    RX_MAX_PACKET_SIZE  /* For hosts on same net */
94  define RX_REMOTE_PACKET_SIZE   RX_MAX_PACKET_SIZE  /* see note above */
95  endif
96 #endif /* notdef */
97
98 /* These are the new, streamlined ones.
99  */
100 #define RX_HEADER_SIZE          sizeof (struct rx_header) 
101
102 /* The minimum MTU for an IP network is 576 bytes including headers */
103 #define RX_MIN_PACKET_SIZE      (576 - RX_IPUDP_SIZE)
104 #define RX_PP_PACKET_SIZE       RX_MIN_PACKET_SIZE
105
106 #define OLD_MAX_PACKET_SIZE     (1500 - RX_IPUDP_SIZE)
107
108 /* if the other guy is not on the local net, use this size */
109 #define RX_REMOTE_PACKET_SIZE   (1500 - RX_IPUDP_SIZE)   
110
111 /* for now, never send more data than this */
112 #define RX_MAX_PACKET_SIZE      16384
113 #define RX_MAX_PACKET_DATA_SIZE (16384 - RX_HEADER_SIZE) 
114
115 /* Packet types, for rx_packet.type */
116 #define RX_PACKET_TYPE_DATA         1    /* A vanilla data packet */
117 #define RX_PACKET_TYPE_ACK          2    /* Acknowledge packet */
118 #define RX_PACKET_TYPE_BUSY         3    /* Busy: can't accept call immediately; try later */
119 #define RX_PACKET_TYPE_ABORT        4    /* Abort packet.  No response needed. */
120 #define RX_PACKET_TYPE_ACKALL       5    /* Acknowledges receipt of all packets */
121 #define RX_PACKET_TYPE_CHALLENGE    6    /* Challenge client's identity: request credentials */
122 #define RX_PACKET_TYPE_RESPONSE     7    /* Respond to challenge packet */
123 #define RX_PACKET_TYPE_DEBUG        8    /* Get debug information */
124
125 #define RX_PACKET_TYPE_PARAMS       9    /* exchange size params (showUmine) */
126 #define RX_PACKET_TYPE_VERSION     13   /* get AFS version */
127
128
129 #define RX_PACKET_TYPES     {"data", "ack", "busy", "abort", "ackall", "challenge", "response", "debug", "params", "unused", "unused", "unused", "version"}
130 #define RX_N_PACKET_TYPES           13      /* Must agree with above list;
131                                                counts 0
132                                                WARNING: if this number ever
133                                                grows past 13, rxdebug packets
134                                                will need to be modified */
135
136 /* Packet classes, for rx_AllocPacket */
137 #define RX_PACKET_CLASS_RECEIVE     0
138 #define RX_PACKET_CLASS_SEND        1
139 #define RX_PACKET_CLASS_SPECIAL     2
140 #define RX_PACKET_CLASS_RECV_CBUF   3
141 #define RX_PACKET_CLASS_SEND_CBUF   4
142
143 #define RX_N_PACKET_CLASSES         5       /* Must agree with above list */
144
145 /* Flags for rx_header flags field */
146 #define RX_CLIENT_INITIATED     1   /* Packet is sent/received from client side of call */
147 #define RX_REQUEST_ACK          2   /* Peer requests acknowledgement */
148 #define RX_LAST_PACKET          4   /* This is the last packet from this side of the call */
149 #define RX_MORE_PACKETS         8   /* There are more packets following this,
150                                      * i.e. the next sequence number seen by
151                                      * the receiver should be greater than
152                                      * this one, rather than a resend of an
153                                      * earlier sequence number */
154 #define RX_FREE_PACKET          16      /* Unallocated to a call */
155 #define RX_SLOW_START_OK        32  /* Set this flag in an ack packet to
156                                      * inform the sender that slow start is
157                                      * supported by the receiver. */
158 #define RX_JUMBO_PACKET         32  /* Set this flag in a data packet to
159                                      * indicate that more packets follow
160                                      * this packet in the datagram */
161
162 /* The following flags are preset per packet, i.e. they don't change
163  * on retransmission of the packet */
164 #define RX_PRESET_FLAGS         (RX_CLIENT_INITIATED | RX_LAST_PACKET)
165
166
167 /* The rx part of the header of a packet, in host form */
168 struct rx_header {
169     afs_uint32 epoch;   /* Start time of client process */
170     afs_uint32 cid;             /* Connection id (defined by client) */
171     afs_uint32 callNumber;      /* Current call number */
172     afs_uint32 seq;             /* Sequence number of this packet, within this call */
173     afs_uint32 serial;  /* Serial number of this packet: a new serial
174                          * number is stamped on each packet sent out */
175     u_char type;        /* RX packet type */
176     u_char flags;       /* Flags, defined below */
177     u_char userStatus;  /* User defined status information,
178                          * returned/set by macros
179                          * rx_Get/SetLocal/RemoteStatus */
180     u_char securityIndex; /* Which service-defined security method to use */
181     u_short serviceId;  /* service this packet is directed _to_ */
182     /* This spare is now used for packet header checkksum.  see
183      * rxi_ReceiveDataPacket and packet cksum macros above for details. */
184     u_short spare;
185 };
186
187 /* The abbreviated header for jumbo packets. Most fields in the
188  * jumbo packet headers are either the same as or can be quickly
189  * derived from their counterparts in the main packet header.
190  */
191 struct rx_jumboHeader {
192     u_char flags;      /* Flags, defined below */
193     u_char spare1;
194     u_short cksum;     /* packet header checksum */
195 };
196
197 /* For most Unixes, maximum elements in an iovec is 16 */
198 #define RX_MAXIOVECS 16            /* limit for ReadvProc/WritevProc */
199 #define RX_MAXWVECS RX_MAXIOVECS-1 /* need one iovec for packet header */
200
201 /*
202  * The values for the RX buffer sizes are calculated to ensure efficient
203  * use of network resources when sending AFS 3.5 jumbograms over Ethernet,
204  * 802.3, FDDI, and ATM networks running IPv4 or IPv6. Changing these
205  * values may affect interoperability with AFS 3.5 clients.
206  */
207
208 /*
209  * We always transmit jumbo grams so that each packet starts at the
210  * beginning of a packet buffer. Because of the requirement that all
211  * segments of a 3.4a jumbogram contain multiples of eight bytes, the
212  * receivers iovec has RX_HEADERSIZE bytes in the first element,
213  * RX_FIRSTBUFFERSIZE bytes in the second element, and RX_CBUFFERSIZE
214  * bytes in each successive entry.  All packets in a jumbogram
215  * except for the last must contain RX_JUMBOBUFFERSIZE bytes of data
216  * so the receiver can split the AFS 3.5 jumbograms back into packets
217  * without having to copy any of the data.
218  */
219 #define RX_JUMBOBUFFERSIZE 1412
220 #define RX_JUMBOHEADERSIZE 4
221 /*
222  * RX_FIRSTBUFFERSIZE must be larger than the largest ack packet, 
223  * the largest possible challenge or response packet. 
224  * Both Firstbuffersize and cbuffersize must be integral multiples of 8,
225  * so the security header and trailer stuff works for rxkad_crypt.  yuck.
226  */
227 #define RX_FIRSTBUFFERSIZE (RX_JUMBOBUFFERSIZE+RX_JUMBOHEADERSIZE)
228 /*
229  * The size of a continuation buffer is buffer is the same as the
230  * size of the first buffer, which must also the size of a jumbo packet
231  * buffer plus the size of a jumbo packet header. */
232 #define RX_CBUFFERSIZE (RX_JUMBOBUFFERSIZE+RX_JUMBOHEADERSIZE)
233 /*
234  * Add an extra four bytes of slop at the end of each buffer.
235  */
236 #define RX_EXTRABUFFERSIZE 4
237
238 struct rx_packet {
239     struct rx_queue queueItemHeader;   /* Packets are chained using the queue.h package */
240     struct clock retryTime;         /* When this packet should NEXT be re-transmitted */
241     struct clock timeSent;          /* When this packet was transmitted last */
242     afs_uint32 firstSerial;                 /* Original serial number of this packet */
243     struct clock firstSent;         /* When this packet was transmitted first */
244     struct rx_header header;        /* The internal packet header */
245     unsigned int niovecs;
246     struct iovec wirevec[RX_MAXWVECS+1];       /* the new form of the packet */
247     
248     u_char acked;       /* This packet has been *tentatively* acknowledged */
249     u_char backoff;                 /* for multiple re-sends */
250     u_short length;                 /* Data length */
251     /* NT port relies on the fact that the next two are physically adjacent.
252      * If that assumption changes change sendmsg and recvmsg in rx_xmit_nt.c .
253      * The jumbo datagram code also relies on the next two being
254      * physically adjacent.
255      * The Linux port uses this knowledge as well in osi_NetSend.
256      */
257     afs_uint32 wirehead[RX_HEADER_SIZE/sizeof(afs_int32)];
258     afs_uint32 localdata[RX_CBUFFERSIZE/sizeof(afs_int32)]; 
259     afs_uint32 extradata[RX_EXTRABUFFERSIZE/sizeof(afs_int32)];
260 };
261
262 /* Macro to convert continuation buffer pointers to packet pointers */
263 #define RX_CBUF_TO_PACKET(CP, PP) \
264     ((struct rx_packet *) \
265      ((char *)(CP) - ((char *)(&(PP)->localdata[0])-(char *)(PP))))
266
267 /* Macros callable by security modules, to set header/trailer lengths,
268  * set actual packet size, and find the beginning of the security
269  * header (or data) */
270 #define rx_SetSecurityHeaderSize(conn, length) ((conn)->securityHeaderSize = (length))
271 #define rx_SetSecurityMaxTrailerSize(conn, length) ((conn)->securityMaxTrailerSize = (length))
272 #define rx_GetSecurityHeaderSize(conn) ((conn)->securityHeaderSize)
273 #define rx_GetSecurityMaxTrailerSize(conn) ((conn)->securityMaxTrailerSize)
274
275 /* This is the address of the data portion of the packet.  Any encryption
276  * headers will be at this address, the actual data, for a data packet, will
277  * start at this address + the connection's security header size. */
278 #define rx_DataOf(packet)               ((char *) (packet)->wirevec[1].iov_base)
279 #define rx_GetDataSize(packet)          ((packet)->length)
280 #define rx_SetDataSize(packet, size)    ((packet)->length = (size))
281
282 /* These macros used in conjunction with reuse of packet header spare as a
283  * packet cksum for rxkad security module. */
284 #define rx_GetPacketCksum(packet)        ((packet)->header.spare)
285 #define rx_SetPacketCksum(packet, cksum) ((packet)->header.spare = (cksum))
286
287 #ifdef KERNEL
288 #define rxi_OverQuota(packetclass) (rx_nFreePackets - 1 < rx_packetQuota[packetclass])
289 #endif /* KERNEL */
290
291 /* this returns an afs_int32 from byte offset o in packet p.  offset must
292  * always be aligned properly for an afs_int32, I'm leaving this up to the
293  * caller. */
294 #define rx_GetInt32(p,off) (( (off) >= (p)->wirevec[1].iov_len) ? \
295    rx_SlowGetInt32((p), (off)) :  \
296   *((afs_int32 *)((char *)((p)->wirevec[1].iov_base) + (off))))
297
298 #define rx_PutInt32(p,off,b) { \
299        if ((off) >= (p)->wirevec[1].iov_len) \
300           rx_SlowPutInt32((p), (off), (b));   \
301        else *((afs_int32 *)((char *)((p)->wirevec[1].iov_base) + (off))) = b; }
302
303 #define rx_data(p, o, l) ((l=((struct rx_packet*)(p))->wirevec[(o+1)].iov_len),\
304   (((struct rx_packet*)(p))->wirevec[(o+1)].iov_base))
305
306
307 struct rx_packet *rx_AllocPacket();
308 struct rx_packet *rxi_ReceiveDebugPacket();
309 struct rx_packet *rxi_ReceiveVersionPacket();
310 struct rx_packet *rxi_SplitJumboPacket();
311
312 /* copy data into an RX packet */
313 #define rx_packetwrite(p, off, len, in)               \
314   ( (off) + (len) > (p)->wirevec[1].iov_len ?         \
315     rx_SlowWritePacket(p, off, len, (char*)(in)) :             \
316     ((bcopy((char *)(in), (char*)((p)->wirevec[1].iov_base)+(off), (len))),0))
317
318 /* copy data from an RX packet */
319 #define rx_packetread(p, off, len, out)               \
320   ( (off) + (len) > (p)->wirevec[1].iov_len ?         \
321     rx_SlowReadPacket(p, off, len, (char*)out) :             \
322     ((bcopy((char*)((p)->wirevec[1].iov_base)+(off), (char *)(out), len)),0))
323
324 #define rx_computelen(p,l) { register int i; \
325    for (l=0, i=1; i < p->niovecs; i++ ) l += p->wirevec[i].iov_len; }
326
327 /* return what the actual contiguous space is: should be min(length,size) */
328 /* The things that call this really want something like ...pullup MTUXXX  */
329 #define rx_Contiguous(p) \
330     MIN((unsigned) (p)->length, (unsigned) ((p)->wirevec[1].iov_len))
331
332 #ifndef TRUE
333 #define TRUE 1
334 #define FALSE 0
335 #endif
336
337 /* === packet-ized down to here, the following macros work temporarily */
338 /* Unfortunately, they know that the cbuf stuff isn't there. */
339
340 /* try to ensure that rx_DataOf will return a contiguous space at
341  * least size bytes long */
342 /* return what the actual contiguous space is: should be min(length,size) */
343 #define rx_Pullup(p,size) /* this idea here is that this will make a guarantee */
344
345
346 /* The offset of the actual user's data in the packet, skipping any
347  * security header */
348 /* DEPRECATED */
349 #define rx_UserDataOf(conn, packet)     (((char *) (packet)->wirevec[1].iov_base) + (conn)->securityHeaderSize)
350
351 /* Adjust an MTU for efficient use of RX buffers */
352 extern int rxi_AdjustIfMTU(int mtu);
353 /* Adjust a maximum MTU for efficient use of RX buffers */
354 extern int rxi_AdjustMaxMTU(int mtu, int peerMaxMTU);
355 /* Figure out how many datagram packets will fit in this mtu */
356 extern int rxi_AdjustDgramPackets(int frags, int mtu);
357
358 #endif /* _RX_PACKET_ */