Changes in / [cc8d91a:dfda6a1] in mainline
- Location:
- uspace/lib
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/net/include/protocol_map.h
rcc8d91a rdfda6a1 45 45 #include <ipc/services.h> 46 46 47 e xtern eth_type_t protocol_map(services_t, services_t);48 externservices_t protocol_unmap(services_t, int);49 e xtern eth_type_t lsap_map(eth_lsap_t);50 e xtern eth_lsap_t lsap_unmap(eth_type_t);51 externhw_type_t hardware_map(services_t);47 eth_type_t protocol_map(services_t, services_t); 48 services_t protocol_unmap(services_t, int); 49 eth_type_t lsap_map(eth_lsap_t); 50 eth_lsap_t lsap_unmap(eth_type_t); 51 hw_type_t hardware_map(services_t); 52 52 53 53 #endif -
uspace/lib/packet/generic/packet_server.c
rcc8d91a rdfda6a1 27 27 */ 28 28 29 /** @addtogroup libpacket29 /** @addtogroup packet 30 30 * @{ 31 31 */ 32 32 33 33 /** @file 34 * Packet server implementation. 35 */ 36 37 #include <packet_server.h> 38 #include <packet_local.h> 34 * Packet server implementation. 35 */ 39 36 40 37 #include <align.h> … … 54 51 #include <net/packet_header.h> 55 52 53 #include <packet_server.h> 54 #include <packet_local.h> 55 56 56 #define FREE_QUEUES_COUNT 7 57 57 58 /** The default address length reserved for new packets. */ 58 /** The default address length reserved for new packets. 59 */ 59 60 #define DEFAULT_ADDR_LEN 32 60 61 61 /** The default prefix reserved for new packets. */ 62 /** The default prefix reserved for new packets. 63 */ 62 64 #define DEFAULT_PREFIX 64 63 65 64 /** The default suffix reserved for new packets. */ 66 /** The default suffix reserved for new packets. 67 */ 65 68 #define DEFAULT_SUFFIX 64 66 69 67 /** Packet server global data. */ 68 static struct { 69 /** Safety lock. */ 70 /** Packet server global data. 71 */ 72 static struct{ 73 /** Safety lock. 74 */ 70 75 fibril_mutex_t lock; 71 /** Free packet queues. */ 76 /** Free packet queues. 77 */ 72 78 packet_t free[FREE_QUEUES_COUNT]; 73 74 /** 75 * Packet length upper bounds of the free packet queues. The maximal 76 * lengths of packets in each queue in the ascending order. The last 77 * queue is not limited. 79 /** Packet length upper bounds of the free packet queues. 80 * The maximal lengths of packets in each queue in the ascending order. 81 * The last queue is not limited. 78 82 */ 79 83 size_t sizes[FREE_QUEUES_COUNT]; 80 81 /** Total packets allocated.*/84 /** Total packets allocated. 85 */ 82 86 unsigned int count; 83 87 } ps_globals = { 84 88 .lock = FIBRIL_MUTEX_INITIALIZER(ps_globals.lock), 85 .free = { 86 NULL, 87 NULL, 88 NULL, 89 NULL, 90 NULL, 91 NULL, 92 NULL 93 }, 94 .sizes = { 95 PAGE_SIZE, 96 PAGE_SIZE * 2, 97 PAGE_SIZE * 4, 98 PAGE_SIZE * 8, 99 PAGE_SIZE * 16, 100 PAGE_SIZE * 32, 101 PAGE_SIZE * 64 102 }, 89 .free = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}, 90 .sizes = {PAGE_SIZE, PAGE_SIZE * 2, PAGE_SIZE * 4, PAGE_SIZE * 8, PAGE_SIZE * 16, PAGE_SIZE * 32, PAGE_SIZE * 64}, 103 91 .count = 0 104 92 }; … … 114 102 115 103 /** Clears and initializes the packet according to the given dimensions. 116 * 117 * @param[in] packet The packet to be initialized. 118 * @param[in] addr_len The source and destination addresses maximal length in 119 * bytes. 120 * @param[in] max_prefix The maximal prefix length in bytes. 121 * @param[in] max_content The maximal content length in bytes. 122 * @param[in] max_suffix The maximal suffix length in bytes. 123 */ 124 static void 125 packet_init(packet_t packet, size_t addr_len, size_t max_prefix, 126 size_t max_content, size_t max_suffix) 127 { 104 * @param[in] packet The packet to be initialized. 105 * @param[in] addr_len The source and destination addresses maximal length in bytes. 106 * @param[in] max_prefix The maximal prefix length in bytes. 107 * @param[in] max_content The maximal content length in bytes. 108 * @param[in] max_suffix The maximal suffix length in bytes. 109 */ 110 static void packet_init(packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){ 128 111 // clear the packet content 129 bzero(((void *) packet) + sizeof(struct packet), 130 packet->length - sizeof(struct packet)); 131 112 bzero(((void *) packet) + sizeof(struct packet), packet->length - sizeof(struct packet)); 132 113 // clear the packet header 133 114 packet->order = 0; … … 144 125 } 145 126 146 /** Creates a new packet of dimensions at least as given. 147 * 148 * Should be used only when the global data are locked. 149 * 150 * @param[in] length The total length of the packet, including the header, 151 * the addresses and the data of the packet. 152 * @param[in] addr_len The source and destination addresses maximal length in 153 * bytes. 154 * @param[in] max_prefix The maximal prefix length in bytes. 155 * @param[in] max_content The maximal content length in bytes. 156 * @param[in] max_suffix The maximal suffix length in bytes. 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 161 packet_create(size_t length, size_t addr_len, size_t max_prefix, 162 size_t max_content, size_t max_suffix) 163 { 127 /** Creates a new packet of dimensions at least as given. 128 * Should be used only when the global data are locked. 129 * @param[in] length The total length of the packet, including the header, the addresses and the data of the packet. 130 * @param[in] addr_len The source and destination addresses maximal length in bytes. 131 * @param[in] max_prefix The maximal prefix length in bytes. 132 * @param[in] max_content The maximal content length in bytes. 133 * @param[in] max_suffix The maximal suffix length in bytes. 134 * @returns The packet of dimensions at least as given. 135 * @returns NULL if there is not enough memory left. 136 */ 137 static packet_t packet_create(size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){ 164 138 ERROR_DECLARE; 165 139 … … 167 141 168 142 // already locked 169 packet = (packet_t) mmap(NULL, length, PROTO_READ | PROTO_WRITE, 170 MAP_SHARED | MAP_ANONYMOUS, 0, 0); 171 if (packet == MAP_FAILED) 143 packet = (packet_t) mmap(NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0); 144 if(packet == MAP_FAILED){ 172 145 return NULL; 173 174 ps_globals.count++;146 } 147 ++ ps_globals.count; 175 148 packet->packet_id = ps_globals.count; 176 149 packet->length = length; 177 150 packet_init(packet, addr_len, max_prefix, max_content, max_suffix); 178 151 packet->magic_value = PACKET_MAGIC_VALUE; 179 if (ERROR_OCCURRED(pm_add(packet))){152 if(ERROR_OCCURRED(pm_add(packet))){ 180 153 munmap(packet, packet->length); 181 154 return NULL; 182 155 } 183 184 156 return packet; 185 157 } … … 191 163 * Lock the global data during its processing. 192 164 * 193 * @param[in] addr_len The source and destination addresses maximal length in194 * 195 * @param[in] max_prefix The maximal prefix length in bytes.165 * @param[in] addr_len The source and destination addresses 166 * maximal length in bytes. 167 * @param[in] max_prefix The maximal prefix length in bytes. 196 168 * @param[in] max_content The maximal content length in bytes. 197 * @param[in] max_suffix The maximal suffix length in bytes. 198 * @return The packet of dimensions at least as given. 199 * @return NULL if there is not enough memory left. 200 */ 201 static packet_t 202 packet_get_local(size_t addr_len, size_t max_prefix, size_t max_content, 203 size_t max_suffix) 204 { 205 size_t length = ALIGN_UP(sizeof(struct packet) + 2 * addr_len + 206 max_prefix + max_content + max_suffix, PAGE_SIZE); 169 * @param[in] max_suffix The maximal suffix length in bytes. 170 * 171 * @return The packet of dimensions at least as given. 172 * @return NULL if there is not enough memory left. 173 * 174 */ 175 static packet_t packet_get_local(size_t addr_len, size_t max_prefix, 176 size_t max_content, size_t max_suffix) 177 { 178 size_t length = ALIGN_UP(sizeof(struct packet) + 2 * addr_len + max_prefix 179 + max_content + max_suffix, PAGE_SIZE); 207 180 208 181 fibril_mutex_lock(&ps_globals.lock); … … 211 184 unsigned int index; 212 185 213 for (index = 0; index < FREE_QUEUES_COUNT; index++) { 214 if (length > ps_globals.sizes[index]) 215 continue; 216 217 packet = ps_globals.free[index]; 218 while (packet_is_valid(packet) && (packet->length < length)) 219 packet = pm_find(packet->next); 220 221 if (packet_is_valid(packet)) { 222 if (packet == ps_globals.free[index]) 223 ps_globals.free[index] = pq_detach(packet); 224 else 225 pq_detach(packet); 186 for (index = 0; index < FREE_QUEUES_COUNT - 1; index++) { 187 if (length <= ps_globals.sizes[index]) { 188 packet = ps_globals.free[index]; 226 189 227 packet_init(packet, addr_len, max_prefix, max_content, 228 max_suffix); 229 fibril_mutex_unlock(&ps_globals.lock); 190 while (packet_is_valid(packet) && (packet->length < length)) 191 packet = pm_find(packet->next); 230 192 231 return packet; 193 if (packet_is_valid(packet)) { 194 if (packet == ps_globals.free[index]) 195 ps_globals.free[index] = pq_detach(packet); 196 else 197 pq_detach(packet); 198 199 packet_init(packet, addr_len, max_prefix, max_content, 200 max_suffix); 201 fibril_mutex_unlock(&ps_globals.lock); 202 203 return packet; 204 } 232 205 } 233 206 } … … 255 228 /** Release the packet and returns it to the appropriate free packet queue. 256 229 * 257 * Should be used only when the global data are locked. 258 * 259 * @param[in] packet The packet to be released. 260 * 261 */ 262 static void packet_release(packet_t packet) 263 { 230 * Should be used only when the global data are locked. 231 * 232 * @param[in] packet The packet to be released. 233 * 234 */ 235 static void packet_release(packet_t packet){ 264 236 int index; 265 237 int result; 266 238 267 for (index = 0; (index < FREE_QUEUES_COUNT - 1) && 268 (packet->length > ps_globals.sizes[index]); index++) { 269 ; 270 } 271 272 result = pq_add(&ps_globals.free[index], packet, packet->length, 273 packet->length); 239 // remove debug dump 240 // printf("packet %d released\n", packet->packet_id); 241 for(index = 0; (index < FREE_QUEUES_COUNT - 1) && (packet->length > ps_globals.sizes[index]); ++ index); 242 result = pq_add(&ps_globals.free[index], packet, packet->length, packet->length); 274 243 assert(result == EOK); 275 244 } 276 245 277 246 /** Releases the packet queue. 278 * 279 * @param[in] packet_id The first packet identifier. 280 * @returns EOK on success. 281 * @returns ENOENT if there is no such packet. 282 */ 283 static int packet_release_wrapper(packet_id_t packet_id) 284 { 247 * @param[in] packet_id The first packet identifier. 248 * @returns EOK on success. 249 * @returns ENOENT if there is no such packet. 250 */ 251 static int packet_release_wrapper(packet_id_t packet_id){ 285 252 packet_t packet; 286 253 287 254 packet = pm_find(packet_id); 288 if (!packet_is_valid(packet))255 if(! packet_is_valid(packet)){ 289 256 return ENOENT; 290 257 } 291 258 fibril_mutex_lock(&ps_globals.lock); 292 259 pq_destroy(packet, packet_release); 293 260 fibril_mutex_unlock(&ps_globals.lock); 294 295 261 return EOK; 296 262 } … … 302 268 303 269 /** Shares the packet memory block. 304 * @param[in] packet The packet to be shared. 305 * @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 310 * async_share_in_finalize() function. 311 */ 312 static int packet_reply(const packet_t packet) 313 { 270 * @param[in] packet The packet to be shared. 271 * @returns EOK on success. 272 * @returns EINVAL if the packet is not valid. 273 * @returns EINVAL if the calling module does not accept the memory. 274 * @returns ENOMEM if the desired and actual sizes differ. 275 * @returns Other error codes as defined for the async_share_in_finalize() function. 276 */ 277 static int packet_reply(const packet_t packet){ 314 278 ipc_callid_t callid; 315 279 size_t size; 316 280 317 if (!packet_is_valid(packet))281 if(! packet_is_valid(packet)){ 318 282 return EINVAL; 319 320 if (!async_share_in_receive(&callid, &size)) { 321 ipc_answer_0(callid, EINVAL); 322 return EINVAL; 323 } 324 325 if (size != packet->length) { 326 ipc_answer_0(callid, ENOMEM); 283 } 284 if(async_share_in_receive(&callid, &size) <= 0) return EINVAL; 285 if(size != packet->length){ 327 286 return ENOMEM; 328 287 } 329 330 return async_share_in_finalize(callid, packet, 331 PROTO_READ | PROTO_WRITE); 332 } 333 334 /** Processes the packet server message. 335 * 336 * @param[in] callid The message identifier. 337 * @param[in] call The message parameters. 338 * @param[out] answer The message answer parameters. 339 * @param[out] answer_count The last parameter for the actual answer in the 340 * answer parameter. 341 * @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 344 * message parameter. 345 * @returns ENOTSUP if the message is not known. 346 * @returns Other error codes as defined for the 347 * packet_release_wrapper() function. 348 */ 349 int 350 packet_server_message(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer, 351 int *answer_count) 352 { 288 return async_share_in_finalize(callid, packet, PROTO_READ | PROTO_WRITE); 289 } 290 291 int packet_server_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){ 353 292 packet_t packet; 354 293 355 294 *answer_count = 0; 356 switch (IPC_GET_METHOD(*call)) { 357 case IPC_M_PHONE_HUNGUP: 358 return EOK; 359 360 case NET_PACKET_CREATE_1: 361 packet = packet_get_local(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, 362 IPC_GET_CONTENT(call), DEFAULT_SUFFIX); 363 if (!packet) 364 return ENOMEM; 365 *answer_count = 2; 366 IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id); 367 IPC_SET_ARG2(*answer, (ipcarg_t) packet->length); 368 return EOK; 369 370 case NET_PACKET_CREATE_4: 371 packet = packet_get_local( 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)); 377 if (!packet) 378 return ENOMEM; 379 *answer_count = 2; 380 IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id); 381 IPC_SET_ARG2(*answer, (ipcarg_t) packet->length); 382 return EOK; 383 384 case NET_PACKET_GET: 385 packet = pm_find(IPC_GET_ID(call)); 386 if (!packet_is_valid(packet)) 387 return ENOENT; 388 return packet_reply(packet); 389 390 case NET_PACKET_GET_SIZE: 391 packet = pm_find(IPC_GET_ID(call)); 392 if (!packet_is_valid(packet)) 393 return ENOENT; 394 IPC_SET_ARG1(*answer, (ipcarg_t) packet->length); 395 *answer_count = 1; 396 return EOK; 397 398 case NET_PACKET_RELEASE: 399 return packet_release_wrapper(IPC_GET_ID(call)); 400 } 401 295 switch(IPC_GET_METHOD(*call)){ 296 case IPC_M_PHONE_HUNGUP: 297 return EOK; 298 case NET_PACKET_CREATE_1: 299 packet = packet_get_local(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, IPC_GET_CONTENT(call), DEFAULT_SUFFIX); 300 if(! packet){ 301 return ENOMEM; 302 } 303 *answer_count = 2; 304 IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id); 305 IPC_SET_ARG2(*answer, (ipcarg_t) packet->length); 306 return EOK; 307 case NET_PACKET_CREATE_4: 308 packet = packet_get_local(((DEFAULT_ADDR_LEN < IPC_GET_ADDR_LEN(call)) ? IPC_GET_ADDR_LEN(call) : DEFAULT_ADDR_LEN), DEFAULT_PREFIX + IPC_GET_PREFIX(call), IPC_GET_CONTENT(call), DEFAULT_SUFFIX + IPC_GET_SUFFIX(call)); 309 if(! packet){ 310 return ENOMEM; 311 } 312 *answer_count = 2; 313 IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id); 314 IPC_SET_ARG2(*answer, (ipcarg_t) packet->length); 315 return EOK; 316 case NET_PACKET_GET: 317 packet = pm_find(IPC_GET_ID(call)); 318 if(! packet_is_valid(packet)){ 319 return ENOENT; 320 } 321 return packet_reply(packet); 322 case NET_PACKET_GET_SIZE: 323 packet = pm_find(IPC_GET_ID(call)); 324 if(! packet_is_valid(packet)){ 325 return ENOENT; 326 } 327 IPC_SET_ARG1(*answer, (ipcarg_t) packet->length); 328 *answer_count = 1; 329 return EOK; 330 case NET_PACKET_RELEASE: 331 return packet_release_wrapper(IPC_GET_ID(call)); 332 } 402 333 return ENOTSUP; 403 334 } -
uspace/lib/packet/include/packet_local.h
rcc8d91a rdfda6a1 27 27 */ 28 28 29 /** @addtogroup libpacket30 * @{29 /** @addtogroup packet 30 * @{ 31 31 */ 32 32 … … 34 34 */ 35 35 36 #ifndef LIBPACKET_PACKET_LOCAL_H_37 #define LIBPACKET_PACKET_LOCAL_H_36 #ifndef __NET_PACKET_LOCAL_H__ 37 #define __NET_PACKET_LOCAL_H__ 38 38 39 39 #include <net/packet.h> -
uspace/lib/packet/include/packet_server.h
rcc8d91a rdfda6a1 27 27 */ 28 28 29 /** @addtogroup libpacket30 * @{29 /** @addtogroup packet 30 * @{ 31 31 */ 32 32 33 33 /** @file 34 * Packet server. 35 * The hosting module has to be compiled with both the packet.c and the 36 * packet_server.c source files. To function correctly, initialization of the 37 * packet map by the pm_init() function has to happen at the first place. Then 38 * the packet messages have to be processed by the packet_server_message() 39 * function. The packet map should be released by the pm_destroy() function 40 * during the module termination. 41 * @see IS_NET_PACKET_MESSAGE() 34 * Packet server. 35 * The hosting module has to be compiled with both the packet.c and the packet_server.c source files. 36 * To function correctly, initialization of the packet map by the pm_init() function has to happen at the first place. 37 * Then the packet messages have to be processed by the packet_server_message() function. 38 * The packet map should be released by the pm_destroy() function during the module termination. 39 * @see IS_NET_PACKET_MESSAGE() 42 40 */ 43 41 44 #ifndef LIBPACKET_PACKET_SERVER_H_45 #define LIBPACKET_PACKET_SERVER_H_42 #ifndef __NET_PACKET_SERVER_H__ 43 #define __NET_PACKET_SERVER_H__ 46 44 47 45 #include <ipc/ipc.h> 48 46 49 extern int packet_server_message(ipc_callid_t, ipc_call_t *, ipc_call_t *, 50 int *); 47 /** Processes the packet server message. 48 * @param[in] callid The message identifier. 49 * @param[in] call The message parameters. 50 * @param[out] answer The message answer parameters. 51 * @param[out] answer_count The last parameter for the actual answer in the answer parameter. 52 * @returns EOK on success. 53 * @returns ENOMEM if there is not enough memory left. 54 * @returns ENOENT if there is no such packet as in the packet message parameter.. 55 * @returns ENOTSUP if the message is not known. 56 * @returns Other error codes as defined for the packet_release_wrapper() function. 57 */ 58 extern int packet_server_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count); 51 59 52 60 #endif
Note:
See TracChangeset
for help on using the changeset viewer.