~n-muench/ubuntu/oneiric/open-vm-tools/open-vm-tools.fix-836277

« back to all changes in this revision

Viewing changes to modules/linux/vsock/linux/vsockPacket.h

  • Committer: Bazaar Package Importer
  • Author(s): Devid Antonio Filoni
  • Date: 2008-08-15 21:21:40 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080815212140-05fhxj8wroosysmj
Tags: 2008.08.08-109361-1ubuntu1
* Merge from Debian unstable (LP: #258393), remaining Ubuntu change:
  - add ubuntu_toolchain_FTBFS.dpatch patch, fix FTBFS
* Update ubuntu_toolchain_FTBFS.dpatch patch for the new version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*********************************************************
 
2
 * Copyright (C) 2007 VMware, Inc. All rights reserved.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License as published by the
 
6
 * Free Software Foundation version 2 and no later version.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
10
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
11
 * for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License along
 
14
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
15
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
16
 *
 
17
 *********************************************************/
 
18
 
 
19
/*
 
20
 * vsockPacket.h --
 
21
 *
 
22
 *    Packet constants, types and functions.
 
23
 */
 
24
 
 
25
#if defined(_WIN32) || defined(VMKERNEL)
 
26
# include "vsockOSInt.h"
 
27
#else
 
28
# define VSockOS_ClearMemory(_dst, _sz)   memset(_dst, 0, _sz)
 
29
# define VSockOS_Memcpy(_dst, _src, _sz)  memcpy(_dst, _src, _sz)
 
30
#endif
 
31
 
 
32
 
 
33
#ifndef _VSOCK_PACKET_H_
 
34
#define _VSOCK_PACKET_H_
 
35
 
 
36
 
 
37
/* If the packet format changes in a release then this should change too. */
 
38
#define VSOCK_PACKET_VERSION 1
 
39
 
 
40
/* The resource ID on which control packets are sent. */
 
41
#define VSOCK_PACKET_RID 1
 
42
 
 
43
/* Assert that the given packet is valid. */
 
44
#define VSOCK_PACKET_ASSERT(_p)                 \
 
45
   ASSERT((_p));                                \
 
46
   ASSERT((_p)->type < VSOCK_PACKET_TYPE_MAX);  \
 
47
   ASSERT(0 == (_p)->_reserved1);               \
 
48
   ASSERT(0 == (_p)->_reserved2)
 
49
 
 
50
 
 
51
typedef enum VSockPacketType {
 
52
   VSOCK_PACKET_TYPE_INVALID = 0,   // Invalid type.
 
53
   VSOCK_PACKET_TYPE_REQUEST,       // Connection request.
 
54
   VSOCK_PACKET_TYPE_NEGOTIATE,     // Connection negotiate.
 
55
   VSOCK_PACKET_TYPE_OFFER,         // Connection offer queue pair.
 
56
   VSOCK_PACKET_TYPE_ATTACH,        // Connection attach.
 
57
   VSOCK_PACKET_TYPE_WROTE,         // Wrote data to queue pair.
 
58
   VSOCK_PACKET_TYPE_READ,          // Read data from queue pair.
 
59
   VSOCK_PACKET_TYPE_RST,           // Reset.
 
60
   VSOCK_PACKET_TYPE_SHUTDOWN,      // Shutdown the connection.
 
61
   VSOCK_PACKET_TYPE_WAITING_WRITE, // Notify peer we are waiting to write.
 
62
   VSOCK_PACKET_TYPE_WAITING_READ,  // Notify peer we are waiting to read.
 
63
   VSOCK_PACKET_TYPE_MAX            // Last message.
 
64
} VSockPacketType;
 
65
 
 
66
typedef struct VSockWaitingInfo {
 
67
   uint64 generation; // Generation of the queue.
 
68
   uint64 offset;     // Offset within the queue.
 
69
} VSockWaitingInfo;
 
70
 
 
71
/*
 
72
 * Control packet type for STREAM sockets.  DGRAMs have no control packets
 
73
 * nor special packet header for data packets, they are just raw VMCI DGRAM
 
74
 * messages.  For STREAMs, control packets are sent over the control channel
 
75
 * while data is written and read directly from queue pairs with no packet
 
76
 * format.
 
77
 */
 
78
typedef struct VSockPacket {
 
79
   VMCIDatagram dg;          // Datagram header.
 
80
   uint8 version;            // Version.
 
81
   uint8 type;               // Type of message.
 
82
   uint16 _reserved1;        // Reserved.
 
83
   uint32 srcPort;           // Source port.
 
84
   uint32 dstPort;           // Destination port.
 
85
   uint32 _reserved2;        // Reserved.
 
86
   union {
 
87
      uint64 size;           // Size of queue pair for request/negotiation.
 
88
      uint64 mode;           // Mode of shutdown for shutdown.
 
89
      VMCIHandle handle;     // Queue pair handle once size negotiated.
 
90
      VSockWaitingInfo wait; // Information provided for wait notifications.
 
91
   } u;
 
92
} VSockPacket;
 
93
 
 
94
 
 
95
MY_ASSERTS(VSockPacketAsserts,
 
96
   ASSERT_ON_COMPILE(sizeof (VSockPacket) == 56);
 
97
)
 
98
 
 
99
 
 
100
/*
 
101
 *-----------------------------------------------------------------------------
 
102
 *
 
103
 * VSockPacket_Init --
 
104
 *
 
105
 *      Initialize the given packet.  The packet version is set and the fields
 
106
 *      are filled out.  Reserved fields are cleared.
 
107
 *
 
108
 * Results:
 
109
 *      None.
 
110
 *
 
111
 * Side effects:
 
112
 *      None.
 
113
 *
 
114
 *-----------------------------------------------------------------------------
 
115
 */
 
116
 
 
117
static INLINE void
 
118
VSockPacket_Init(VSockPacket *pkt,        // OUT
 
119
                 struct sockaddr_vm *src, // IN
 
120
                 struct sockaddr_vm *dst, // IN
 
121
                 uint8 type,              // IN
 
122
                 uint64 size,             // IN
 
123
                 uint64 mode,             // IN
 
124
                 VSockWaitingInfo *wait,  // IN
 
125
                 VMCIHandle handle)       // IN
 
126
{
 
127
   ASSERT(pkt);
 
128
   VSOCK_ADDR_NOFAMILY_ASSERT(src);
 
129
   VSOCK_ADDR_NOFAMILY_ASSERT(dst);
 
130
 
 
131
   pkt->dg.src = VMCI_MAKE_HANDLE(src->svm_cid, VSOCK_PACKET_RID);
 
132
   pkt->dg.dst = VMCI_MAKE_HANDLE(dst->svm_cid, VSOCK_PACKET_RID);
 
133
   pkt->dg.payloadSize = sizeof *pkt - sizeof pkt->dg;
 
134
   pkt->version = VSOCK_PACKET_VERSION;
 
135
   pkt->type = type;
 
136
   pkt->srcPort = src->svm_port;
 
137
   pkt->dstPort = dst->svm_port;
 
138
   VSockOS_ClearMemory(&pkt->_reserved1, sizeof pkt->_reserved1);
 
139
   VSockOS_ClearMemory(&pkt->_reserved2, sizeof pkt->_reserved2);
 
140
 
 
141
   switch (pkt->type) {
 
142
   case VSOCK_PACKET_TYPE_INVALID:
 
143
      pkt->u.size = 0;
 
144
      break;
 
145
 
 
146
   case VSOCK_PACKET_TYPE_REQUEST:
 
147
   case VSOCK_PACKET_TYPE_NEGOTIATE:
 
148
      pkt->u.size = size;
 
149
      break;
 
150
 
 
151
   case VSOCK_PACKET_TYPE_OFFER:
 
152
   case VSOCK_PACKET_TYPE_ATTACH:
 
153
      pkt->u.handle = handle;
 
154
      break;
 
155
 
 
156
   case VSOCK_PACKET_TYPE_WROTE:
 
157
   case VSOCK_PACKET_TYPE_READ:
 
158
   case VSOCK_PACKET_TYPE_RST:
 
159
      pkt->u.size = 0;
 
160
      break;
 
161
 
 
162
   case VSOCK_PACKET_TYPE_SHUTDOWN:
 
163
      pkt->u.mode = mode;
 
164
      break;
 
165
 
 
166
   case VSOCK_PACKET_TYPE_WAITING_READ:
 
167
   case VSOCK_PACKET_TYPE_WAITING_WRITE:
 
168
      ASSERT(wait);
 
169
      VSockOS_Memcpy(&pkt->u.wait, wait, sizeof pkt->u.wait);
 
170
      break;
 
171
   }
 
172
 
 
173
   VSOCK_PACKET_ASSERT(pkt);
 
174
}
 
175
 
 
176
 
 
177
/*
 
178
 *-----------------------------------------------------------------------------
 
179
 *
 
180
 * VSockPacket_Validate --
 
181
 *
 
182
 *      Validate the given packet.
 
183
 *
 
184
 * Results:
 
185
 *      0 on success, EFAULT if the address is invalid, EINVAL if the packet
 
186
 *      fields are invalid.
 
187
 *
 
188
 * Side effects:
 
189
 *      None.
 
190
 *
 
191
 *-----------------------------------------------------------------------------
 
192
 */
 
193
 
 
194
static INLINE int32
 
195
VSockPacket_Validate(VSockPacket *pkt)
 
196
{
 
197
   int32 err = EINVAL;
 
198
 
 
199
   if (NULL == pkt) {
 
200
      err = EFAULT;
 
201
      goto exit;
 
202
   }
 
203
 
 
204
   if (VMCI_HANDLE_INVALID(pkt->dg.src)) {
 
205
      goto exit;
 
206
   }
 
207
 
 
208
   if (VMCI_HANDLE_INVALID(pkt->dg.dst)) {
 
209
      goto exit;
 
210
   }
 
211
 
 
212
   if (VMCI_INVALID_ID == pkt->dstPort || VMCI_INVALID_ID == pkt->srcPort) {
 
213
      goto exit;
 
214
   }
 
215
 
 
216
   if (VSOCK_PACKET_VERSION != pkt->version) {
 
217
      goto exit;
 
218
   }
 
219
 
 
220
   if (0 != pkt->_reserved1 || 0 != pkt->_reserved2) {
 
221
      goto exit;
 
222
   }
 
223
 
 
224
   switch (pkt->type) {
 
225
   case VSOCK_PACKET_TYPE_INVALID:
 
226
      if (0 != pkt->u.size) {
 
227
         goto exit;
 
228
      }
 
229
      break;
 
230
 
 
231
   case VSOCK_PACKET_TYPE_REQUEST:
 
232
   case VSOCK_PACKET_TYPE_NEGOTIATE:
 
233
      if (0 == pkt->u.size) {
 
234
         goto exit;
 
235
      }
 
236
      break;
 
237
 
 
238
   case VSOCK_PACKET_TYPE_OFFER:
 
239
   case VSOCK_PACKET_TYPE_ATTACH:
 
240
      if (VMCI_HANDLE_INVALID(pkt->u.handle)) {
 
241
         goto exit;
 
242
      }
 
243
      break;
 
244
 
 
245
   case VSOCK_PACKET_TYPE_WROTE:
 
246
   case VSOCK_PACKET_TYPE_READ:
 
247
   case VSOCK_PACKET_TYPE_RST:
 
248
      if (0 != pkt->u.size) {
 
249
         goto exit;
 
250
      }
 
251
      break;
 
252
   }
 
253
 
 
254
   err = 0;
 
255
   
 
256
exit:
 
257
   return sockerr2err(err);
 
258
}
 
259
 
 
260
 
 
261
/*
 
262
 *-----------------------------------------------------------------------------
 
263
 *
 
264
 * VSockPacket_GetAddresses --
 
265
 *
 
266
 *      Get the local and remote addresses from the given packet.
 
267
 *
 
268
 * Results:
 
269
 *      None
 
270
 *
 
271
 * Side effects:
 
272
 *      None.
 
273
 *
 
274
 *-----------------------------------------------------------------------------
 
275
 */
 
276
 
 
277
static INLINE void
 
278
VSockPacket_GetAddresses(VSockPacket *pkt,           // IN
 
279
                         struct sockaddr_vm *local,  // OUT
 
280
                         struct sockaddr_vm *remote) // OUT
 
281
{
 
282
   VSOCK_PACKET_ASSERT(pkt);
 
283
   VSockAddr_Init(local, VMCI_HANDLE_TO_CONTEXT_ID(pkt->dg.dst),
 
284
                  pkt->dstPort);
 
285
   VSockAddr_Init(remote, VMCI_HANDLE_TO_CONTEXT_ID(pkt->dg.src),
 
286
                  pkt->srcPort);
 
287
}
 
288
 
 
289
 
 
290
#endif // _VSOCK_PACKET_H_
 
291