1 | /* Copyright (c) 2001, Stanford University
|
---|
2 | * All rights reserved.
|
---|
3 | *
|
---|
4 | * See the file LICENSE.txt for information on redistributing this software.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #ifndef CR_NET_H
|
---|
8 | #define CR_NET_H
|
---|
9 |
|
---|
10 | #ifdef WINDOWS
|
---|
11 | #define WIN32_LEAN_AND_MEAN
|
---|
12 | #pragma warning( push, 3 ) /* shut up about warnings in YOUR OWN HEADER FILES!!! */
|
---|
13 | #include <winsock.h>
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | #include <stdio.h>
|
---|
17 |
|
---|
18 | #ifndef WINDOWS
|
---|
19 | #include <sys/socket.h>
|
---|
20 | #ifndef DARWIN
|
---|
21 | #ifdef AF_INET6
|
---|
22 | /* getaddrinfo & co appeared with ipv6 */
|
---|
23 | #define ADDRINFO
|
---|
24 | #endif
|
---|
25 | #endif
|
---|
26 | #include <netinet/in.h>
|
---|
27 | #endif
|
---|
28 |
|
---|
29 | #ifdef SunOS
|
---|
30 | #include <sys/types.h>
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #include "cr_protocol.h"
|
---|
34 | #include "cr_threads.h"
|
---|
35 |
|
---|
36 | #include <iprt/types.h>
|
---|
37 |
|
---|
38 | #ifdef __cplusplus
|
---|
39 | extern "C" {
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #define DEFAULT_SERVER_PORT 7000
|
---|
43 |
|
---|
44 | /* If you change this, update DefaultMothershipPort in mothership.py */
|
---|
45 | #define DEFAULT_MOTHERSHIP_PORT 10000
|
---|
46 |
|
---|
47 | typedef struct CRConnection CRConnection;
|
---|
48 |
|
---|
49 | typedef enum {
|
---|
50 | CR_NO_CONNECTION,
|
---|
51 | CR_SDP,
|
---|
52 | CR_TCPIP,
|
---|
53 | CR_UDPTCPIP,
|
---|
54 | CR_FILE,
|
---|
55 | CR_GM,
|
---|
56 | CR_IB,
|
---|
57 | CR_TEAC,
|
---|
58 | CR_TCSCOMM,
|
---|
59 | CR_VBOXHGCM,
|
---|
60 | CR_DROP_PACKETS
|
---|
61 | } CRConnectionType;
|
---|
62 |
|
---|
63 | #if defined(WINDOWS)
|
---|
64 | typedef SOCKET CRSocket;
|
---|
65 | #else
|
---|
66 | typedef int CRSocket;
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | typedef void (*CRVoidFunc)( void );
|
---|
70 | typedef int (*CRNetReceiveFunc)( CRConnection *conn, CRMessage *msg, unsigned int len );
|
---|
71 | typedef int (*CRNetConnectFunc)( CRConnection *conn );
|
---|
72 | typedef void (*CRNetCloseFunc)( unsigned int sender_id );
|
---|
73 |
|
---|
74 | typedef struct __recvFuncList {
|
---|
75 | CRNetReceiveFunc recv;
|
---|
76 | struct __recvFuncList *next;
|
---|
77 | } CRNetReceiveFuncList;
|
---|
78 |
|
---|
79 | typedef struct __closeFuncList {
|
---|
80 | CRNetCloseFunc close;
|
---|
81 | struct __closeFuncList *next;
|
---|
82 | } CRNetCloseFuncList;
|
---|
83 |
|
---|
84 | typedef struct __messageListNode {
|
---|
85 | CRMessage *mesg; /* the actual message (header + payload) */
|
---|
86 | unsigned int len; /* length of message (header + payload) */
|
---|
87 | CRConnection *conn; /* some messages are assoc. with specific connections*/
|
---|
88 | struct __messageListNode *next; /* next in list */
|
---|
89 | } CRMessageListNode;
|
---|
90 |
|
---|
91 | typedef struct {
|
---|
92 | CRMessageListNode *head, *tail;
|
---|
93 | int numMessages;
|
---|
94 | CRmutex lock;
|
---|
95 | CRcondition nonEmpty;
|
---|
96 | } CRMessageList;
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Used to accumulate CR_MESSAGE_MULTI_BODY/TAIL chunks into one big buffer.
|
---|
101 | */
|
---|
102 | typedef struct CRMultiBuffer {
|
---|
103 | unsigned int len; /* current length (<= max) (with sizeof_buffer_header) */
|
---|
104 | unsigned int max; /* size in bytes of data buffer */
|
---|
105 | void *buf; /* data buffer */
|
---|
106 | } CRMultiBuffer;
|
---|
107 |
|
---|
108 | #ifdef VBOX_WITH_CRHGSMI
|
---|
109 | # ifdef IN_GUEST
|
---|
110 | typedef struct CRVBOXHGSMI_CLIENT {
|
---|
111 | struct VBOXUHGSMI *pHgsmi;
|
---|
112 | struct VBOXUHGSMI_BUFFER *pCmdBuffer;
|
---|
113 | struct VBOXUHGSMI_BUFFER *pHGBuffer;
|
---|
114 | void *pvHGBuffer;
|
---|
115 | struct CRBufferPool_t *bufpool;
|
---|
116 | } CRVBOXHGSMI_CLIENT, *PCRVBOXHGSMI_CLIENT;
|
---|
117 | #endif /* IN_GUEST */
|
---|
118 | #endif /* #ifdef VBOX_WITH_CRHGSMI */
|
---|
119 | /**
|
---|
120 | * Chromium network connection (bidirectional).
|
---|
121 | */
|
---|
122 | struct CRConnection {
|
---|
123 | int ignore;
|
---|
124 | CRConnectionType type;
|
---|
125 | unsigned int id; /* obtained from the mothership (if brokered) */
|
---|
126 |
|
---|
127 | /* List of messages that we've received on the network connection but
|
---|
128 | * nobody has yet consumed.
|
---|
129 | */
|
---|
130 | CRMessageList messageList;
|
---|
131 |
|
---|
132 | CRMultiBuffer multi;
|
---|
133 |
|
---|
134 | unsigned int mtu; /* max transmission unit size (in bytes) */
|
---|
135 | unsigned int buffer_size;
|
---|
136 | unsigned int krecv_buf_size;
|
---|
137 | int broker; /* is connection brokered through mothership? */
|
---|
138 | int threaded; /* is this a threaded connection? */
|
---|
139 | int endianness, swap;
|
---|
140 | int actual_network; /* is this a real network? */
|
---|
141 |
|
---|
142 | unsigned char *userbuf;
|
---|
143 | int userbuf_len;
|
---|
144 |
|
---|
145 | char *hostname;
|
---|
146 | int port;
|
---|
147 |
|
---|
148 | /* To allocate a data buffer of size conn->buffer_size bytes */
|
---|
149 | void *(*Alloc)( CRConnection *conn );
|
---|
150 | /* To indicate the client's done with a data buffer */
|
---|
151 | void (*Free)( CRConnection *conn, void *buf );
|
---|
152 | /* To send a data buffer. If bufp is non-null, it must have been obtained
|
---|
153 | * from Alloc() and it'll be freed when Send() returns.
|
---|
154 | */
|
---|
155 | void (*Send)( CRConnection *conn, void **buf, const void *start, unsigned int len );
|
---|
156 | /* To send a data buffer than can optionally be dropped on the floor */
|
---|
157 | void (*Barf)( CRConnection *conn, void **buf, const void *start, unsigned int len );
|
---|
158 | /* To send 'len' bytes from buffer at 'start', no funny business */
|
---|
159 | void (*SendExact)( CRConnection *conn, const void *start, unsigned int len );
|
---|
160 | /* To receive data. 'len' bytes will be placed into 'buf'. */
|
---|
161 | void (*Recv)( CRConnection *conn, void *buf, unsigned int len );
|
---|
162 | /* To receive one message on the connection */
|
---|
163 | void (*RecvMsg)( CRConnection *conn );
|
---|
164 | /* What's this??? */
|
---|
165 | void (*InstantReclaim)( CRConnection *conn, CRMessage *mess );
|
---|
166 | /* Called when a full CR_MESSAGE_MULTI_HEAD/TAIL message has been received */
|
---|
167 | void (*HandleNewMessage)( CRConnection *conn, CRMessage *mess, unsigned int len );
|
---|
168 | /* To accept a new connection from a client */
|
---|
169 | void (*Accept)( CRConnection *conn, const char *hostname, unsigned short port );
|
---|
170 | /* To connect to a server (return 0 if error, 1 if success) */
|
---|
171 | int (*Connect)( CRConnection *conn );
|
---|
172 | /* To disconnect from a server */
|
---|
173 | void (*Disconnect)( CRConnection *conn );
|
---|
174 |
|
---|
175 | unsigned int sizeof_buffer_header;
|
---|
176 |
|
---|
177 | /* logging */
|
---|
178 | int total_bytes_sent;
|
---|
179 | int total_bytes_recv;
|
---|
180 | int recv_count;
|
---|
181 | int opcodes_count;
|
---|
182 |
|
---|
183 | /* credits for flow control */
|
---|
184 | int send_credits;
|
---|
185 | int recv_credits;
|
---|
186 |
|
---|
187 | /* TCP/IP */
|
---|
188 | CRSocket tcp_socket;
|
---|
189 | int index;
|
---|
190 |
|
---|
191 | CRSocket sdp_socket;
|
---|
192 |
|
---|
193 | /* UDP/IP */
|
---|
194 | CRSocket udp_socket;
|
---|
195 | #ifndef ADDRINFO
|
---|
196 | struct sockaddr_in remoteaddr;
|
---|
197 | #else
|
---|
198 | struct sockaddr_storage remoteaddr;
|
---|
199 | #endif
|
---|
200 |
|
---|
201 | /* UDP/TCP/IP */
|
---|
202 | unsigned int seq;
|
---|
203 | unsigned int ack;
|
---|
204 | void *udp_packet;
|
---|
205 | int udp_packetlen;
|
---|
206 |
|
---|
207 | /* FILE Tracing */
|
---|
208 | enum { CR_FILE_WRITE, CR_FILE_READ } file_direction;
|
---|
209 | char *filename;
|
---|
210 | int fd;
|
---|
211 |
|
---|
212 | /* Myrinet GM */
|
---|
213 | unsigned int gm_node_id;
|
---|
214 | unsigned int gm_port_num;
|
---|
215 |
|
---|
216 | /* Mellanox IB */
|
---|
217 | unsigned int ib_node_id;
|
---|
218 | unsigned int ib_port_num;
|
---|
219 |
|
---|
220 | /* Quadrics Elan3 (teac) */
|
---|
221 | int teac_id;
|
---|
222 | int teac_rank;
|
---|
223 |
|
---|
224 | /* Quadrics Elan3 (tcscomm) */
|
---|
225 | int tcscomm_id;
|
---|
226 | int tcscomm_rank;
|
---|
227 |
|
---|
228 | /* VBox HGCM */
|
---|
229 | uint32_t u32ClientID;
|
---|
230 | uint8_t *pBuffer;
|
---|
231 | uint32_t cbBuffer;
|
---|
232 | uint8_t *pHostBuffer;
|
---|
233 | uint32_t cbHostBufferAllocated;
|
---|
234 | uint32_t cbHostBuffer;
|
---|
235 | #ifdef IN_GUEST
|
---|
236 | uint32_t u32InjectClientID;
|
---|
237 | # ifdef VBOX_WITH_CRHGSMI
|
---|
238 | # ifndef VBOX_CRHGSMI_WITH_D3DDEV
|
---|
239 | CRVBOXHGSMI_CLIENT HgsmiClient;
|
---|
240 | # endif
|
---|
241 | # endif
|
---|
242 | #else
|
---|
243 | # ifdef VBOX_WITH_CRHGSMI
|
---|
244 | struct _crclient *pClient; /* back reference, just for simplicity */
|
---|
245 | CRVBOXHGSMI_CMDDATA CmdData;
|
---|
246 | # endif
|
---|
247 | #endif
|
---|
248 | /* Used on host side to indicate that we are not allowed to store above pointers for later use
|
---|
249 | * in crVBoxHGCMReceiveMessage. As those messages are going to be processed after the corresponding
|
---|
250 | * HGCM call is finished and memory is freed. So we have to store a copy.
|
---|
251 | * This happens when message processing for client associated with this connection
|
---|
252 | * is blocked by another client, which has send us glBegin call and we're waiting to receive glEnd.
|
---|
253 | */
|
---|
254 | uint8_t allow_redir_ptr;
|
---|
255 |
|
---|
256 | uint32_t vMajor, vMinor; /*Protocol version*/
|
---|
257 | };
|
---|
258 |
|
---|
259 |
|
---|
260 | /*
|
---|
261 | * Network functions
|
---|
262 | */
|
---|
263 | extern DECLEXPORT(int) crGetHostname( char *buf, unsigned int len );
|
---|
264 |
|
---|
265 | extern DECLEXPORT(void) crNetInit( CRNetReceiveFunc recvFunc, CRNetCloseFunc closeFunc );
|
---|
266 | extern DECLEXPORT(void) crNetTearDown();
|
---|
267 |
|
---|
268 | extern DECLEXPORT(void) *crNetAlloc( CRConnection *conn );
|
---|
269 | extern DECLEXPORT(void) crNetFree( CRConnection *conn, void *buf );
|
---|
270 |
|
---|
271 | extern DECLEXPORT(void) crNetAccept( CRConnection *conn, const char *hostname, unsigned short port );
|
---|
272 | extern DECLEXPORT(int) crNetConnect( CRConnection *conn );
|
---|
273 | extern DECLEXPORT(void) crNetDisconnect( CRConnection *conn );
|
---|
274 | extern DECLEXPORT(void) crNetFreeConnection( CRConnection *conn );
|
---|
275 | extern DECLEXPORT(void) crCloseSocket( CRSocket sock );
|
---|
276 |
|
---|
277 | extern DECLEXPORT(void) crNetSend( CRConnection *conn, void **bufp, const void *start, unsigned int len );
|
---|
278 | extern DECLEXPORT(void) crNetBarf( CRConnection *conn, void **bufp, const void *start, unsigned int len );
|
---|
279 | extern DECLEXPORT(void) crNetSendExact( CRConnection *conn, const void *start, unsigned int len );
|
---|
280 | extern DECLEXPORT(void) crNetSingleRecv( CRConnection *conn, void *buf, unsigned int len );
|
---|
281 | extern DECLEXPORT(unsigned int) crNetGetMessage( CRConnection *conn, CRMessage **message );
|
---|
282 | extern DECLEXPORT(unsigned int) crNetPeekMessage( CRConnection *conn, CRMessage **message );
|
---|
283 | extern DECLEXPORT(int) crNetNumMessages(CRConnection *conn);
|
---|
284 | extern DECLEXPORT(void) crNetReadline( CRConnection *conn, void *buf );
|
---|
285 | extern DECLEXPORT(int) crNetRecv( void );
|
---|
286 | #define CR_WRITEBACK_WAIT() do { \
|
---|
287 | while (writeback) { \
|
---|
288 | crNetRecv(); \
|
---|
289 | } \
|
---|
290 | } while (0)
|
---|
291 | extern DECLEXPORT(void) crNetDefaultRecv( CRConnection *conn, CRMessage *msg, unsigned int len );
|
---|
292 | extern DECLEXPORT(void) crNetDispatchMessage( CRNetReceiveFuncList *rfl, CRConnection *conn, CRMessage *msg, unsigned int len );
|
---|
293 |
|
---|
294 | extern DECLEXPORT(CRConnection *) crNetConnectToServer( const char *server, unsigned short default_port, int mtu, int broker );
|
---|
295 | extern DECLEXPORT(CRConnection *) crNetAcceptClient( const char *protocol, const char *hostname, unsigned short port, unsigned int mtu, int broker );
|
---|
296 |
|
---|
297 |
|
---|
298 | extern DECLEXPORT(void) crInitMessageList(CRMessageList *list);
|
---|
299 | extern DECLEXPORT(void) crEnqueueMessage(CRMessageList *list, CRMessage *msg, unsigned int len, CRConnection *conn);
|
---|
300 | extern DECLEXPORT(void) crDequeueMessage(CRMessageList *list, CRMessage **msg, unsigned int *len, CRConnection **conn);
|
---|
301 |
|
---|
302 | extern DECLEXPORT(void) crNetRecvReadPixels( const CRMessageReadPixels *rp, unsigned int len );
|
---|
303 |
|
---|
304 |
|
---|
305 | /*
|
---|
306 | * Quadrics stuff
|
---|
307 | */
|
---|
308 | #define CR_QUADRICS_DEFAULT_LOW_CONTEXT 32
|
---|
309 | #define CR_QUADRICS_DEFAULT_HIGH_CONTEXT 35
|
---|
310 |
|
---|
311 | extern DECLEXPORT(void) crNetSetRank( int my_rank );
|
---|
312 | extern DECLEXPORT(void) crNetSetContextRange( int low_context, int high_context );
|
---|
313 | extern DECLEXPORT(void) crNetSetNodeRange( const char *low_node, const char *high_node );
|
---|
314 | extern DECLEXPORT(void) crNetSetKey( const unsigned char* key, const int keyLength );
|
---|
315 |
|
---|
316 |
|
---|
317 | /*
|
---|
318 | * Socket callback facility
|
---|
319 | */
|
---|
320 | #define CR_SOCKET_CREATE 1
|
---|
321 | #define CR_SOCKET_DESTROY 2
|
---|
322 | typedef void (*CRSocketCallbackProc)(int mode, int socket);
|
---|
323 | extern DECLEXPORT(void) crRegisterSocketCallback(int mode, CRSocketCallbackProc proc);
|
---|
324 |
|
---|
325 |
|
---|
326 | #ifdef __cplusplus
|
---|
327 | }
|
---|
328 | #endif
|
---|
329 |
|
---|
330 | #endif /* CR_NET_H */
|
---|