Changes in uspace/lib/packet/generic/packet_server.c [28a3e74:a649e73f] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/packet/generic/packet_server.c
r28a3e74 ra649e73f 36 36 37 37 #include <packet_server.h> 38 #include <packet_local.h> 38 39 39 40 #include <align.h> … … 41 42 #include <async.h> 42 43 #include <errno.h> 44 #include <err.h> 43 45 #include <fibril_synch.h> 44 46 #include <unistd.h> 45 47 #include <sys/mman.h> 48 49 #include <ipc/ipc.h> 46 50 #include <ipc/packet.h> 47 51 #include <ipc/net.h> 52 48 53 #include <net/packet.h> 49 54 #include <net/packet_header.h> … … 65 70 fibril_mutex_t lock; 66 71 /** Free packet queues. */ 67 packet_t *free[FREE_QUEUES_COUNT];72 packet_t free[FREE_QUEUES_COUNT]; 68 73 69 74 /** … … 99 104 }; 100 105 106 int packet_translate_local(int phone, packet_ref packet, packet_id_t packet_id) 107 { 108 if (!packet) 109 return EINVAL; 110 111 *packet = pm_find(packet_id); 112 return (*packet) ? EOK : ENOENT; 113 } 114 101 115 /** Clears and initializes the packet according to the given dimensions. 102 116 * … … 109 123 */ 110 124 static void 111 packet_init(packet_t *packet, size_t addr_len, size_t max_prefix,125 packet_init(packet_t packet, size_t addr_len, size_t max_prefix, 112 126 size_t max_content, size_t max_suffix) 113 127 { 114 / * Clear the packet content */115 bzero(((void *) packet) + sizeof( packet_t),116 packet->length - sizeof( packet_t));117 118 / * Clear the packet header */128 // clear the packet content 129 bzero(((void *) packet) + sizeof(struct packet), 130 packet->length - sizeof(struct packet)); 131 132 // clear the packet header 119 133 packet->order = 0; 120 134 packet->metric = 0; … … 122 136 packet->next = 0; 123 137 packet->addr_len = 0; 124 packet->src_addr = sizeof( packet_t);138 packet->src_addr = sizeof(struct packet); 125 139 packet->dest_addr = packet->src_addr + addr_len; 126 140 packet->max_prefix = max_prefix; … … 131 145 132 146 /** Creates a new packet of dimensions at least as given. 147 * 148 * Should be used only when the global data are locked. 133 149 * 134 150 * @param[in] length The total length of the packet, including the header, … … 139 155 * @param[in] max_content The maximal content length in bytes. 140 156 * @param[in] max_suffix The maximal suffix length in bytes. 141 * @return The packet of dimensions at least as given.142 * @return NULL if there is not enough memory left.143 */ 144 static packet_t *157 * @returns The packet of dimensions at least as given. 158 * @returns NULL if there is not enough memory left. 159 */ 160 static packet_t 145 161 packet_create(size_t length, size_t addr_len, size_t max_prefix, 146 162 size_t max_content, size_t max_suffix) 147 163 { 148 packet_t *packet; 149 int rc; 150 151 assert(fibril_mutex_is_locked(&ps_globals.lock)); 152 153 /* Already locked */ 154 packet = (packet_t *) mmap(NULL, length, PROTO_READ | PROTO_WRITE, 164 ERROR_DECLARE; 165 166 packet_t packet; 167 168 // already locked 169 packet = (packet_t) mmap(NULL, length, PROTO_READ | PROTO_WRITE, 155 170 MAP_SHARED | MAP_ANONYMOUS, 0, 0); 156 171 if (packet == MAP_FAILED) … … 162 177 packet_init(packet, addr_len, max_prefix, max_content, max_suffix); 163 178 packet->magic_value = PACKET_MAGIC_VALUE; 164 rc = pm_add(packet); 165 if (rc != EOK) { 179 if (ERROR_OCCURRED(pm_add(packet))) { 166 180 munmap(packet, packet->length); 167 181 return NULL; … … 185 199 * @return NULL if there is not enough memory left. 186 200 */ 187 static packet_t *201 static packet_t 188 202 packet_get_local(size_t addr_len, size_t max_prefix, size_t max_content, 189 203 size_t max_suffix) 190 204 { 191 size_t length = ALIGN_UP(sizeof( packet_t) + 2 * addr_len +205 size_t length = ALIGN_UP(sizeof(struct packet) + 2 * addr_len + 192 206 max_prefix + max_content + max_suffix, PAGE_SIZE); 193 207 194 208 fibril_mutex_lock(&ps_globals.lock); 195 209 196 packet_t *packet;210 packet_t packet; 197 211 unsigned int index; 198 212 199 213 for (index = 0; index < FREE_QUEUES_COUNT; index++) { 200 if ((length > ps_globals.sizes[index]) && 201 (index < FREE_QUEUES_COUNT - 1)) 214 if (length > ps_globals.sizes[index]) 202 215 continue; 203 216 … … 228 241 } 229 242 243 packet_t packet_get_4_local(int phone, size_t max_content, size_t addr_len, 244 size_t max_prefix, size_t max_suffix) 245 { 246 return packet_get_local(addr_len, max_prefix, max_content, max_suffix); 247 } 248 249 packet_t packet_get_1_local(int phone, size_t content) 250 { 251 return packet_get_local(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, content, 252 DEFAULT_SUFFIX); 253 } 254 230 255 /** Release the packet and returns it to the appropriate free packet queue. 231 256 * 257 * Should be used only when the global data are locked. 258 * 232 259 * @param[in] packet The packet to be released. 233 260 * 234 261 */ 235 static void packet_release(packet_t *packet)262 static void packet_release(packet_t packet) 236 263 { 237 264 int index; 238 265 int result; 239 240 assert(fibril_mutex_is_locked(&ps_globals.lock));241 266 242 267 for (index = 0; (index < FREE_QUEUES_COUNT - 1) && … … 253 278 * 254 279 * @param[in] packet_id The first packet identifier. 255 * @return EOK on success.256 * @return ENOENT if there is no such packet.280 * @returns EOK on success. 281 * @returns ENOENT if there is no such packet. 257 282 */ 258 283 static int packet_release_wrapper(packet_id_t packet_id) 259 284 { 260 packet_t *packet;285 packet_t packet; 261 286 262 287 packet = pm_find(packet_id); … … 271 296 } 272 297 298 void pq_release_local(int phone, packet_id_t packet_id) 299 { 300 (void) packet_release_wrapper(packet_id); 301 } 302 273 303 /** Shares the packet memory block. 274 304 * @param[in] packet The packet to be shared. 275 * @return EOK on success.276 * @return EINVAL if the packet is not valid.277 * @return EINVAL if the calling module does not accept the memory.278 * @return ENOMEM if the desired and actual sizes differ.279 * @return Other error codes as defined for the305 * @returns EOK on success. 306 * @returns EINVAL if the packet is not valid. 307 * @returns EINVAL if the calling module does not accept the memory. 308 * @returns ENOMEM if the desired and actual sizes differ. 309 * @returns Other error codes as defined for the 280 310 * async_share_in_finalize() function. 281 311 */ 282 static int packet_reply( packet_t *packet)312 static int packet_reply(const packet_t packet) 283 313 { 284 314 ipc_callid_t callid; … … 289 319 290 320 if (!async_share_in_receive(&callid, &size)) { 291 async_answer_0(callid, EINVAL);321 ipc_answer_0(callid, EINVAL); 292 322 return EINVAL; 293 323 } 294 324 295 325 if (size != packet->length) { 296 async_answer_0(callid, ENOMEM);326 ipc_answer_0(callid, ENOMEM); 297 327 return ENOMEM; 298 328 } … … 309 339 * @param[out] answer_count The last parameter for the actual answer in the 310 340 * answer parameter. 311 * @return EOK on success.312 * @return ENOMEM if there is not enough memory left.313 * @return ENOENT if there is no such packet as in the packet341 * @returns EOK on success. 342 * @returns ENOMEM if there is not enough memory left. 343 * @returns ENOENT if there is no such packet as in the packet 314 344 * message parameter. 315 * @return ENOTSUP if the message is not known.316 * @return Other error codes as defined for the345 * @returns ENOTSUP if the message is not known. 346 * @returns Other error codes as defined for the 317 347 * packet_release_wrapper() function. 318 348 */ 319 349 int 320 350 packet_server_message(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer, 321 size_t *answer_count)322 { 323 packet_t *packet;351 int *answer_count) 352 { 353 packet_t packet; 324 354 325 355 *answer_count = 0; 326 switch (IPC_GET_ IMETHOD(*call)) {356 switch (IPC_GET_METHOD(*call)) { 327 357 case IPC_M_PHONE_HUNGUP: 328 358 return EOK; … … 330 360 case NET_PACKET_CREATE_1: 331 361 packet = packet_get_local(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, 332 IPC_GET_CONTENT( *call), DEFAULT_SUFFIX);362 IPC_GET_CONTENT(call), DEFAULT_SUFFIX); 333 363 if (!packet) 334 364 return ENOMEM; 335 365 *answer_count = 2; 336 IPC_SET_ARG1(*answer, ( sysarg_t) packet->packet_id);337 IPC_SET_ARG2(*answer, ( sysarg_t) packet->length);366 IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id); 367 IPC_SET_ARG2(*answer, (ipcarg_t) packet->length); 338 368 return EOK; 339 369 340 370 case NET_PACKET_CREATE_4: 341 371 packet = packet_get_local( 342 ((DEFAULT_ADDR_LEN < IPC_GET_ADDR_LEN( *call)) ?343 IPC_GET_ADDR_LEN( *call) : DEFAULT_ADDR_LEN),344 DEFAULT_PREFIX + IPC_GET_PREFIX( *call),345 IPC_GET_CONTENT( *call),346 DEFAULT_SUFFIX + IPC_GET_SUFFIX( *call));372 ((DEFAULT_ADDR_LEN < IPC_GET_ADDR_LEN(call)) ? 373 IPC_GET_ADDR_LEN(call) : DEFAULT_ADDR_LEN), 374 DEFAULT_PREFIX + IPC_GET_PREFIX(call), 375 IPC_GET_CONTENT(call), 376 DEFAULT_SUFFIX + IPC_GET_SUFFIX(call)); 347 377 if (!packet) 348 378 return ENOMEM; 349 379 *answer_count = 2; 350 IPC_SET_ARG1(*answer, ( sysarg_t) packet->packet_id);351 IPC_SET_ARG2(*answer, ( sysarg_t) packet->length);380 IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id); 381 IPC_SET_ARG2(*answer, (ipcarg_t) packet->length); 352 382 return EOK; 353 383 354 384 case NET_PACKET_GET: 355 packet = pm_find(IPC_GET_ID( *call));385 packet = pm_find(IPC_GET_ID(call)); 356 386 if (!packet_is_valid(packet)) 357 387 return ENOENT; … … 359 389 360 390 case NET_PACKET_GET_SIZE: 361 packet = pm_find(IPC_GET_ID( *call));391 packet = pm_find(IPC_GET_ID(call)); 362 392 if (!packet_is_valid(packet)) 363 393 return ENOENT; 364 IPC_SET_ARG1(*answer, ( sysarg_t) packet->length);394 IPC_SET_ARG1(*answer, (ipcarg_t) packet->length); 365 395 *answer_count = 1; 366 396 return EOK; 367 397 368 398 case NET_PACKET_RELEASE: 369 return packet_release_wrapper(IPC_GET_ID( *call));399 return packet_release_wrapper(IPC_GET_ID(call)); 370 400 } 371 401
Note:
See TracChangeset
for help on using the changeset viewer.