File: message.c

package info (click to toggle)
open-vm-tools 1%3A8.4.2-261024-1
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze
  • size: 20,368 kB
  • ctags: 30,043
  • sloc: ansic: 164,785; sh: 10,713; cpp: 6,525; makefile: 3,386
file content (298 lines) | stat: -rw-r--r-- 8,254 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*********************************************************
 * Copyright (C) 1999 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation version 2 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 *********************************************************/

/*
 * message.c --
 *
 *    Second layer of the internal communication channel between guest
 *    applications and vmware
 *
 *    Build a generic messaging system between guest applications and vmware.
 *
 *    The protocol is not completely symmetrical, because:
 *     . basic requests can only be sent by guest applications (when vmware
 *       wants to post a message to a guest application, the message will be
 *       really fetched only when the guest application will poll for new
 *       available messages)
 *     . several guest applications can talk to vmware, while the contrary is
 *       not true
 *
 *    Operations that are not atomic (in terms of number of backdoor calls)
 *    can be aborted by vmware if a checkpoint/restore occurs in the middle of
 *    such an operation. This layer takes care of retrying those operations.
 */

#ifdef __cplusplus
extern "C" {
#endif

#if defined(__KERNEL__) || defined(_KERNEL) || defined(KERNEL)
#   include "kernelStubs.h"
#else
#   include <stdio.h>
#   include <stdlib.h>
#   include "debug.h"
#endif

#include "backdoor_def.h"
#include "guest_msg_def.h"
#include "backdoor.h"
#include "message.h"

static MessageOpenProcType externalOpenProc = NULL;
static MessageGetReadEventProcType externalGetReadEventProc = NULL;
static MessageSendProcType externalSendProc = NULL;
static MessageReceiveProcType externalReceiveProc = NULL;
static MessageCloseProcType externalCloseProc = NULL;

/*
 * Currently, the default implementation is to use the backdoor. Soon,
 * this will not be the default, as we will explicitly set it when we
 * decide to use the backdoor.
 */
EXTERN Message_Channel *MessageBackdoor_Open(uint32 proto);

EXTERN Bool MessageBackdoor_GetReadEvent(Message_Channel *chan,
                                         int64 *event);

EXTERN Bool MessageBackdoor_Send(Message_Channel *chan,
                                 const unsigned char *buf,
                                 size_t bufSize);

EXTERN Bool MessageBackdoor_Receive(Message_Channel *chan,
                                    unsigned char **buf,
                                    size_t *bufSize);

EXTERN Bool MessageBackdoor_Close(Message_Channel *chan);



/*
 *-----------------------------------------------------------------------------
 *
 * Message_SetTransport --
 *
 *    This tells the message layer to use an alternate transport
 *    for messages. By default, we use the backdoor, so this function
 *    overrides that default at runtime and switches everything over to
 *    an alternate transport.
 *
 * Result:
 *    None
 *
 * Side-effects:
 *    None
 *
 *-----------------------------------------------------------------------------
 */

void 
Message_SetTransport(MessageOpenProcType openProc,                   // IN
                     MessageGetReadEventProcType getReadEeventProc,  // IN
                     MessageSendProcType sendProc,                   // IN
                     MessageReceiveProcType receiveProc,             // IN
                     MessageCloseProcType closeProc)                 // IN
{
   externalOpenProc = openProc;
   externalGetReadEventProc = getReadEeventProc;
   externalSendProc = sendProc;
   externalReceiveProc = receiveProc;
   externalCloseProc = closeProc;
}


/*
 *-----------------------------------------------------------------------------
 *
 * Message_Open --
 *
 *    Open a communication channel
 *
 * Result:
 *    An allocated Message_Channel on success
 *    NULL on failure
 *
 * Side-effects:
 *    None
 *
 *-----------------------------------------------------------------------------
 */

Message_Channel *
Message_Open(uint32 proto) // IN
{
   /*
    * If there is an alterate backdoor implementation, then call that.
    */
   if (NULL != externalOpenProc) {
      return((*externalOpenProc)(proto));
   }

   /*
    * Otherwise, we default to the backdoor.
    */
   return(MessageBackdoor_Open(proto));
}


/*
 *-----------------------------------------------------------------------------
 *
 * Message_GetReadEvent --
 *
 *    This allows higher levels of the IPC stack to use an event to detect
 *    when a message has arrived. This allows an asynchronous, event-based-model 
 *    rather than continually calling Message_Receive in a busy loop. This may 
 *    only be supported by some transports. The backdoor does not, so the IPC
 *    code will still have to poll in those cases.
 *
 * Result:
 *    Bool - whether this feature is supported by this transport.
 *
 * Side-effects:
 *    None
 *
 *-----------------------------------------------------------------------------
 */

Bool 
Message_GetReadEvent(Message_Channel *chan,  // IN
                     int64 *event)           // OUT
{
   /*
    * If there is an alterate backdoor implementation, then call that.
    */
   if (NULL != externalGetReadEventProc) {
      return((*externalGetReadEventProc)(chan, event));
   }

   /*
    * Otherwise, we default to the backdoor.
    */
   return(MessageBackdoor_GetReadEvent(chan, event));
}


/*
 *-----------------------------------------------------------------------------
 *
 * Message_Send --
 *
 *    Send a message over a communication channel
 *
 * Result:
 *    TRUE on success
 *    FALSE on failure (the message is discarded by vmware)
 *
 * Side-effects:
 *    None
 *
 *-----------------------------------------------------------------------------
 */

Bool
Message_Send(Message_Channel *chan,    // IN/OUT
             const unsigned char *buf, // IN
             size_t bufSize)           // IN
{
   /*
    * If there is an alterate backdoor implementation, then call that.
    */
   if (NULL != externalSendProc) {
      return((*externalSendProc)(chan, buf, bufSize));
   }

   /*
    * Otherwise, we default to the backdoor.
    */
   return(MessageBackdoor_Send(chan, buf, bufSize));
}


/*
 *-----------------------------------------------------------------------------
 *
 * Message_Receive --
 *
 *    If vmware has posted a message for this channel, retrieve it
 *
 * Result:
 *    TRUE on success (bufSize is 0 if there is no message)
 *    FALSE on failure
 *
 * Side-effects:
 *    None
 *
 *-----------------------------------------------------------------------------
 */

Bool
Message_Receive(Message_Channel *chan, // IN/OUT
                unsigned char **buf,   // OUT
                size_t *bufSize)       // OUT
{
   /*
    * If there is an alterate backdoor implementation, then call that.
    */
   if (NULL != externalReceiveProc) {
      return((*externalReceiveProc)(chan, buf, bufSize));
   }

   /*
    * Otherwise, we default to the backdoor.
    */
   return(MessageBackdoor_Receive(chan, buf, bufSize));
}


/*
 *-----------------------------------------------------------------------------
 *
 * Message_Close --
 *
 *    Close a communication channel
 *
 * Result:
 *    TRUE on success, the channel is destroyed
 *    FALSE on failure
 *
 * Side-effects:
 *    None
 *
 *-----------------------------------------------------------------------------
 */

Bool
Message_Close(Message_Channel *chan) // IN/OUT
{
   /*
    * If there is an alterate backdoor implementation, then call that.
    */
   if (NULL != externalCloseProc) {
      return((*externalCloseProc)(chan));
   }

   /*
    * Otherwise, we default to the backdoor.
    */
   return(MessageBackdoor_Close(chan));
}

#ifdef __cplusplus
}
#endif