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