File: transport.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 (451 lines) | stat: -rw-r--r-- 11,079 bytes parent folder | download | duplicates (2)
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*********************************************************
 * Copyright (C) 2009 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
 *
 *********************************************************/

/*
 * transport.c --
 *
 * This file handles the transport mechanisms available for HGFS.
 * This acts as a glue between the HGFS filesystem driver and the
 * actual transport channels (backdoor, tcp, vsock, ...).
 *
 * The sends happen in the process context, where as a kernel thread
 * handles the asynchronous replies. A queue of pending replies is
 * maintained and is protected by a spinlock. The channel opens and close
 * is protected by a mutex.
 */

/* Must come before any kernel header file. */
#include "driver-config.h"

#include <linux/errno.h>
#include "compat_kthread.h"
#include "compat_list.h"
#include "compat_mutex.h"
#include "compat_sched.h"
#include "compat_spinlock.h"
#include "compat_version.h"

/* Must be included after semaphore.h. */
#include <linux/timer.h>
/* Must be included after sched.h. */
#include <linux/smp_lock.h>

#include "bdhandler.h"
#include "hgfsDevLinux.h"
#include "hgfsProto.h"
#include "module.h"
#include "request.h"
#include "tcp.h"
#include "transport.h"
#include "vm_assert.h"

COMPAT_KTHREAD_DECLARE_STOP_INFO();
static HgfsTransportChannel *hgfsChannel;     /* Current active channel. */
static compat_mutex_t hgfsChannelLock;        /* Lock to protect hgfsChannel. */
static struct list_head hgfsRepPending;       /* Reply pending queue. */
static spinlock_t hgfsRepQueueLock;           /* Reply pending queue lock. */

#define HgfsRequestId(req) ((HgfsRequest *)req)->id


/*
 * Private function implementations.
 */

/*
 *----------------------------------------------------------------------
 *
 * HgfsTransportSetupNewChannel --
 *
 *     Find a new workable channel.
 *
 * Results:
 *     TRUE on success, otherwise FALSE.
 *
 * Side effects:
 *     None
 *
 *----------------------------------------------------------------------
 */

static Bool
HgfsTransportSetupNewChannel(void)
{
   hgfsChannel = HgfsGetVSocketChannel();
   if (hgfsChannel != NULL) {
      if (hgfsChannel->ops.open(hgfsChannel)) {
         return TRUE;
      }
   }

   hgfsChannel = HgfsGetTcpChannel();
   if (hgfsChannel != NULL) {
      if (hgfsChannel->ops.open(hgfsChannel)) {
         return TRUE;
      }
   }

   hgfsChannel = HgfsGetBdChannel();
   if (hgfsChannel != NULL) {
      if (hgfsChannel->ops.open(hgfsChannel)) {
         return TRUE;
      }
   }

   hgfsChannel = NULL;
   return FALSE;
}


/*
 *----------------------------------------------------------------------
 *
 * HgfsTransportStopCurrentChannel --
 *
 *     Teardown current channel and stop current receive thread.
 *
 * Results:
 *     None
 *
 * Side effects:
 *     None
 *
 *----------------------------------------------------------------------
 */

static void
HgfsTransportStopCurrentChannel(void)
{
   if (hgfsChannel) {
      hgfsChannel->ops.exit(hgfsChannel);
      hgfsChannel = NULL;
   }
}


/*
 *----------------------------------------------------------------------
 *
 * HgfsTransportChannelFailover --
 *
 *     Called when current channel doesn't work. Find a new channel
 *     for transport.
 *
 * Results:
 *     TRUE on success, otherwise FALSE;
 *
 * Side effects:
 *     Teardown current opened channel and the receive thread, set up
 *     new channel and new receive thread.
 *
 *----------------------------------------------------------------------
 */

static Bool
HgfsTransportChannelFailover(void) {
   Bool ret = FALSE;
   HgfsTransportStopCurrentChannel();
   ret = HgfsTransportSetupNewChannel();
   LOG(8, ("VMware hgfs: %s result: %s.\n", __func__, ret ? "TRUE" : "FALSE"));
   return ret;
}


/*
 *----------------------------------------------------------------------
 *
 * HgfsTransporAddPendingRequest --
 *
 *     Adds a request to the hgfsRepPending queue.
 *
 * Results:
 *     None
 *
 * Side effects:
 *     None
 *
 *----------------------------------------------------------------------
 */

static void
HgfsTransportAddPendingRequest(HgfsReq *req)   // IN: Request to add
{
   ASSERT(req);

   spin_lock(&hgfsRepQueueLock);
   list_add_tail(&req->list, &hgfsRepPending);
   spin_unlock(&hgfsRepQueueLock);
}


/*
 *----------------------------------------------------------------------
 *
 * HgfsTransportRemovePendingRequest --
 *
 *     Dequeues the request from the hgfsRepPending queue.
 *
 * Results:
 *     None
 *
 * Side effects:
 *     None
 *
 *----------------------------------------------------------------------
 */

static void
HgfsTransportRemovePendingRequest(HgfsReq *req)   // IN: Request to dequeue
{
   ASSERT(req);

   spin_lock(&hgfsRepQueueLock);
   if (!list_empty(&req->list)) {
      list_del_init(&req->list);
   }
   spin_unlock(&hgfsRepQueueLock);
}


/*
 * Public function implementations.
 */

/*
 *----------------------------------------------------------------------
 *
 * HgfsTransportProcessPacket --
 *
 *     Helper function to process received packets, called by the channel
 *     handler thread.
 *
 * Results:
 *     None
 *
 * Side effects:
 *     None
 *
 *----------------------------------------------------------------------
 */

void
HgfsTransportProcessPacket(char *receivedPacket,    //IN: received packet
                           size_t receivedSize)     //IN: packet size
{
   struct list_head *cur, *next;
   HgfsHandle id;
   Bool found = FALSE;

   /* Got the reply. */

   ASSERT(receivedPacket != NULL && receivedSize > 0);
   id = HgfsRequestId(receivedPacket);
   LOG(8, ("VMware hgfs: %s entered.\n", __func__));
   LOG(6, (KERN_DEBUG "VMware hgfs: %s: req id: %d\n", __func__, id));
   /*
    * Search through hgfsRepPending queue for the matching id and wake up
    * the associated waiting process. Delete the req from the queue.
    */
   spin_lock(&hgfsRepQueueLock);
   list_for_each_safe(cur, next, &hgfsRepPending) {
      HgfsReq *req;
      req = list_entry(cur, HgfsReq, list);
      if (req->id == id) {
         ASSERT(req->state == HGFS_REQ_STATE_SUBMITTED);
         HgfsCompleteReq(req, receivedPacket, receivedSize);
         found = TRUE;
         break;
      }
   }
   spin_unlock(&hgfsRepQueueLock);

   if (!found) {
      LOG(4, ("VMware hgfs: %s: No matching id, dropping reply\n",
              __func__));
   }
   LOG(8, ("VMware hgfs: %s exited.\n", __func__));
}


/*
 *----------------------------------------------------------------------
 *
 * HgfsTransportBeforeExitingRecvThread --
 *
 *     The cleanup work to do before the recv thread exits, including
 *     completing pending requests with error.
 *
 * Results:
 *     None
 *
 * Side effects:
 *     None
 *
 *----------------------------------------------------------------------
 */

void
HgfsTransportBeforeExitingRecvThread(void)
{
   struct list_head *cur, *next;

   /* Walk through hgfsRepPending queue and reply them with error. */
   spin_lock(&hgfsRepQueueLock);
   list_for_each_safe(cur, next, &hgfsRepPending) {
      HgfsReq *req;
      HgfsReply reply;

      /* XXX: Make the request senders be aware of this error. */
      reply.status = -EIO;
      req = list_entry(cur, HgfsReq, list);
      LOG(6, ("VMware hgfs: %s: injecting error reply to req id: %d\n",
              __func__, req->id));
      HgfsCompleteReq(req, (char *)&reply, sizeof reply);
   }
   spin_unlock(&hgfsRepQueueLock);
}


/*
 *----------------------------------------------------------------------
 *
 * HgfsTransportSendRequest --
 *
 *     Sends the request via channel communication.
 *
 * Results:
 *     Zero on success, non-zero error on failure.
 *
 * Side effects:
 *     None
 *
 *----------------------------------------------------------------------
 */

int
HgfsTransportSendRequest(HgfsReq *req)   // IN: Request to send
{
   int ret;
   ASSERT(req);
   ASSERT(req->state == HGFS_REQ_STATE_UNSENT);
   ASSERT(req->payloadSize <= HGFS_PACKET_MAX);

   compat_mutex_lock(&hgfsChannelLock);

   /* Try opening the channel. */
   if (!hgfsChannel && !HgfsTransportSetupNewChannel()) {
      compat_mutex_unlock(&hgfsChannelLock);
      return -EPROTO;
   }

   ASSERT(hgfsChannel->ops.send);

   HgfsTransportAddPendingRequest(req);

   while ((ret = hgfsChannel->ops.send(hgfsChannel, req)) != 0) {
      LOG(4, (KERN_DEBUG "VMware hgfs: %s: send failed. Return %d\n",
              __func__, ret));
      if (ret == -EINTR) {
         /* Don't retry when we are interrupted by some signal. */
         goto out;
      }
      if (!hgfsChannel->ops.open(hgfsChannel) && !HgfsTransportChannelFailover()) {
         /* Can't establish a working channel, just report error. */
         ret = -EIO;
         goto out;
      }
   }

   ASSERT(req->state == HGFS_REQ_STATE_COMPLETED ||
          req->state == HGFS_REQ_STATE_SUBMITTED);

out:
   compat_mutex_unlock(&hgfsChannelLock);

   if (ret == 0) { /* Send succeeded. */
      if (wait_event_interruptible(req->queue,
                                   req->state == HGFS_REQ_STATE_COMPLETED)) {
         ret = -EINTR; /* Interrupted by some signal. */
      }
   } /* else send failed. */

   if (ret < 0) {
      HgfsTransportRemovePendingRequest(req);
   }

   return ret;
}


/*
 *----------------------------------------------------------------------
 *
 * HgfsTransportInit --
 *
 *     Initialize the transport.
 *
 *     Starts the reply thread, for handling incoming packets on the
 *     connected socket.
 *
 * Results:
 *     None
 *
 * Side effects:
 *     None
 *
 *----------------------------------------------------------------------
 */

void
HgfsTransportInit(void)
{
   INIT_LIST_HEAD(&hgfsRepPending);
   spin_lock_init(&hgfsRepQueueLock);
   compat_mutex_init(&hgfsChannelLock);

   hgfsChannel = NULL;
}


/*
 *----------------------------------------------------------------------
 *
 * HgfsTransportExit --
 *
 *     Teardown the transport.
 *
 * Results:
 *     None
 *
 * Side effects:
 *     Cleans up everything, frees queues, closes channel.
 *
 *----------------------------------------------------------------------
 */

void
HgfsTransportExit(void)
{
   LOG(8, ("VMware hgfs: %s entered.\n", __func__));
   compat_mutex_lock(&hgfsChannelLock);
   HgfsTransportStopCurrentChannel();
   compat_mutex_unlock(&hgfsChannelLock);

   ASSERT(list_empty(&hgfsRepPending));
   LOG(8, ("VMware hgfs: %s exited.\n", __func__));
}