Changeset aadf01e in mainline for uspace/srv/net/structures/packet
- Timestamp:
- 2010-03-07T15:13:28Z (16 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 936835e
- Parents:
- aa85487
- Location:
- uspace/srv/net/structures/packet
- Files:
-
- 9 edited
-
packet.c (modified) (9 diffs)
-
packet.h (modified) (13 diffs)
-
packet_client.c (modified) (3 diffs)
-
packet_client.h (modified) (17 diffs)
-
packet_header.h (modified) (3 diffs)
-
packet_messages.h (modified) (1 diff)
-
packet_remote.c (modified) (4 diffs)
-
packet_server.c (modified) (11 diffs)
-
packet_server.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/structures/packet/packet.c
raa85487 raadf01e 59 59 * @param[in] packet_id The packet identifier. 60 60 */ 61 #define PACKET_MAP_PAGE( packet_id ) ((( packet_id ) - 1 ) / PACKET_MAP_SIZE)61 #define PACKET_MAP_PAGE(packet_id) (((packet_id) - 1) / PACKET_MAP_SIZE) 62 62 63 63 /** Returns the packet index in the corresponding packet map page. 64 64 * @param[in] packet_id The packet identifier. 65 65 */ 66 #define PACKET_MAP_INDEX( packet_id ) ((( packet_id ) - 1 ) % PACKET_MAP_SIZE)66 #define PACKET_MAP_INDEX(packet_id) (((packet_id) - 1) % PACKET_MAP_SIZE) 67 67 68 68 /** Type definition of the packet map page. 69 69 */ 70 typedef packet_t packet_map_t[ PACKET_MAP_SIZE];70 typedef packet_t packet_map_t[PACKET_MAP_SIZE]; 71 71 /** Type definition of the packet map page pointer. 72 72 */ … … 77 77 * @see generic_field.h 78 78 */ 79 GENERIC_FIELD_DECLARE( gpm, packet_map_t);79 GENERIC_FIELD_DECLARE(gpm, packet_map_t); 80 80 81 81 /** Releases the packet. … … 84 84 * @returns EINVAL if the packet is not valid. 85 85 */ 86 int packet_destroy( packet_t packet);86 int packet_destroy(packet_t packet); 87 87 88 88 /** Packet map global data. … … 91 91 /** Safety lock. 92 92 */ 93 fibril_rwlock_t lock;93 fibril_rwlock_t lock; 94 94 /** Packet map. 95 95 */ 96 gpm_t packet_map;96 gpm_t packet_map; 97 97 } pm_globals; 98 98 99 GENERIC_FIELD_IMPLEMENT( gpm, packet_map_t ); 100 101 int packet_destroy( packet_t packet ){ 102 if( ! packet_is_valid( packet )) return EINVAL; 103 return munmap( packet, packet->length ); 104 } 105 106 int pm_init( void ){ 99 GENERIC_FIELD_IMPLEMENT(gpm, packet_map_t); 100 101 int packet_destroy(packet_t packet){ 102 if(! packet_is_valid(packet)){ 103 return EINVAL; 104 } 105 return munmap(packet, packet->length); 106 } 107 108 int pm_init(void){ 107 109 ERROR_DECLARE; 108 110 109 fibril_rwlock_initialize( & pm_globals.lock);110 fibril_rwlock_write_lock( & pm_globals.lock);111 ERROR_PROPAGATE( gpm_initialize( & pm_globals.packet_map));112 fibril_rwlock_write_unlock( & pm_globals.lock);113 return EOK; 114 } 115 116 packet_t pm_find( packet_id_t packet_id){111 fibril_rwlock_initialize(&pm_globals.lock); 112 fibril_rwlock_write_lock(&pm_globals.lock); 113 ERROR_PROPAGATE(gpm_initialize(&pm_globals.packet_map)); 114 fibril_rwlock_write_unlock(&pm_globals.lock); 115 return EOK; 116 } 117 118 packet_t pm_find(packet_id_t packet_id){ 117 119 packet_map_ref map; 118 120 packet_t packet; 119 121 120 if( ! packet_id ) return NULL; 121 fibril_rwlock_read_lock( & pm_globals.lock ); 122 if( packet_id > PACKET_MAP_SIZE * gpm_count( & pm_globals.packet_map )){ 123 fibril_rwlock_read_unlock( & pm_globals.lock ); 124 return NULL; 125 } 126 map = gpm_get_index( & pm_globals.packet_map, PACKET_MAP_PAGE( packet_id )); 127 if( ! map ){ 128 fibril_rwlock_read_unlock( & pm_globals.lock ); 129 return NULL; 130 } 131 packet = ( * map )[ PACKET_MAP_INDEX( packet_id ) ]; 132 fibril_rwlock_read_unlock( & pm_globals.lock ); 122 if(! packet_id){ 123 return NULL; 124 } 125 fibril_rwlock_read_lock(&pm_globals.lock); 126 if(packet_id > PACKET_MAP_SIZE * gpm_count(&pm_globals.packet_map)){ 127 fibril_rwlock_read_unlock(&pm_globals.lock); 128 return NULL; 129 } 130 map = gpm_get_index(&pm_globals.packet_map, PACKET_MAP_PAGE(packet_id)); 131 if(! map){ 132 fibril_rwlock_read_unlock(&pm_globals.lock); 133 return NULL; 134 } 135 packet = (*map)[PACKET_MAP_INDEX(packet_id)]; 136 fibril_rwlock_read_unlock(&pm_globals.lock); 133 137 return packet; 134 138 } 135 139 136 int pm_add( packet_t packet){140 int pm_add(packet_t packet){ 137 141 ERROR_DECLARE; 138 142 139 143 packet_map_ref map; 140 144 141 if( ! packet_is_valid( packet )) return EINVAL; 142 fibril_rwlock_write_lock( & pm_globals.lock ); 143 if( PACKET_MAP_PAGE( packet->packet_id ) < gpm_count( & pm_globals.packet_map )){ 144 map = gpm_get_index( & pm_globals.packet_map, PACKET_MAP_PAGE( packet->packet_id )); 145 if(! packet_is_valid(packet)){ 146 return EINVAL; 147 } 148 fibril_rwlock_write_lock(&pm_globals.lock); 149 if(PACKET_MAP_PAGE(packet->packet_id) < gpm_count(&pm_globals.packet_map)){ 150 map = gpm_get_index(&pm_globals.packet_map, PACKET_MAP_PAGE(packet->packet_id)); 145 151 }else{ 146 152 do{ 147 map = ( packet_map_ref ) malloc( sizeof( packet_map_t));148 if( ! map){149 fibril_rwlock_write_unlock( & pm_globals.lock);153 map = (packet_map_ref) malloc(sizeof(packet_map_t)); 154 if(! map){ 155 fibril_rwlock_write_unlock(&pm_globals.lock); 150 156 return ENOMEM; 151 157 } 152 bzero( map, sizeof( packet_map_t));153 if(( ERROR_CODE = gpm_add( & pm_globals.packet_map, map )) < 0){154 fibril_rwlock_write_unlock( & pm_globals.lock);155 free( map);158 bzero(map, sizeof(packet_map_t)); 159 if((ERROR_CODE = gpm_add(&pm_globals.packet_map, map)) < 0){ 160 fibril_rwlock_write_unlock(&pm_globals.lock); 161 free(map); 156 162 return ERROR_CODE; 157 163 } 158 }while( PACKET_MAP_PAGE( packet->packet_id ) >= gpm_count( & pm_globals.packet_map));159 } 160 ( * map )[ PACKET_MAP_INDEX( packet->packet_id )] = packet;161 fibril_rwlock_write_unlock( & pm_globals.lock);162 return EOK; 163 } 164 165 void pm_destroy( void){164 }while(PACKET_MAP_PAGE(packet->packet_id) >= gpm_count(&pm_globals.packet_map)); 165 } 166 (*map)[PACKET_MAP_INDEX(packet->packet_id)] = packet; 167 fibril_rwlock_write_unlock(&pm_globals.lock); 168 return EOK; 169 } 170 171 void pm_destroy(void){ 166 172 int count; 167 173 int index; … … 169 175 packet_t packet; 170 176 171 fibril_rwlock_write_lock( & pm_globals.lock);172 count = gpm_count( & pm_globals.packet_map);173 while( count > 0){174 map = gpm_get_index( & pm_globals.packet_map, count - 1);175 for( index = PACKET_MAP_SIZE - 1; index >= 0; -- index){176 packet = ( * map )[ index];177 if( packet_is_valid( packet)){178 munmap( packet, packet->length);177 fibril_rwlock_write_lock(&pm_globals.lock); 178 count = gpm_count(&pm_globals.packet_map); 179 while(count > 0){ 180 map = gpm_get_index(&pm_globals.packet_map, count - 1); 181 for(index = PACKET_MAP_SIZE - 1; index >= 0; -- index){ 182 packet = (*map)[index]; 183 if(packet_is_valid(packet)){ 184 munmap(packet, packet->length); 179 185 } 180 186 } 181 187 } 182 gpm_destroy( & pm_globals.packet_map);188 gpm_destroy(&pm_globals.packet_map); 183 189 // leave locked 184 190 } 185 191 186 int pq_add( packet_t * first, packet_t packet, size_t order, size_t metric ){ 187 packet_t item; 188 189 if(( ! first ) || ( ! packet_is_valid( packet ))) return EINVAL; 190 pq_set_order( packet, order, metric ); 191 if( packet_is_valid( * first )){ 192 int pq_add(packet_t * first, packet_t packet, size_t order, size_t metric){ 193 packet_t item; 194 195 if((! first) || (! packet_is_valid(packet))){ 196 return EINVAL; 197 } 198 pq_set_order(packet, order, metric); 199 if(packet_is_valid(*first)){ 192 200 item = * first; 193 201 do{ 194 if( item->order < order){195 if( item->next){196 item = pm_find( item->next);202 if(item->order < order){ 203 if(item->next){ 204 item = pm_find(item->next); 197 205 }else{ 198 206 item->next = packet->packet_id; … … 204 212 packet->next = item->packet_id; 205 213 item->previous = packet->packet_id; 206 item = pm_find( packet->previous);207 if( item){214 item = pm_find(packet->previous); 215 if(item){ 208 216 item->next = packet->packet_id; 209 217 }else{ 210 * first = packet;218 *first = packet; 211 219 } 212 220 return EOK; 213 221 } 214 }while( packet_is_valid( item )); 215 } 216 * first = packet; 217 return EOK; 218 } 219 220 packet_t pq_find( packet_t packet, size_t order ){ 221 packet_t item; 222 223 if( ! packet_is_valid( packet )) return NULL; 224 if( packet->order == order ) return packet; 225 item = pm_find( packet->next ); 226 while( item && ( item != packet )){ 227 item = pm_find( item->next ); 228 if( item->order == order ){ 222 }while(packet_is_valid(item)); 223 } 224 *first = packet; 225 return EOK; 226 } 227 228 packet_t pq_find(packet_t packet, size_t order){ 229 packet_t item; 230 231 if(! packet_is_valid(packet)){ 232 return NULL; 233 } 234 if(packet->order == order){ 235 return packet; 236 } 237 item = pm_find(packet->next); 238 while(item && (item != packet)){ 239 item = pm_find(item->next); 240 if(item->order == order){ 229 241 return item; 230 242 } … … 233 245 } 234 246 235 int pq_insert_after( packet_t packet, packet_t new_packet ){ 236 packet_t item; 237 238 if( !( packet_is_valid( packet ) && packet_is_valid( new_packet ))) return EINVAL; 247 int pq_insert_after(packet_t packet, packet_t new_packet){ 248 packet_t item; 249 250 if(!(packet_is_valid(packet) && packet_is_valid(new_packet))){ 251 return EINVAL; 252 } 239 253 new_packet->previous = packet->packet_id; 240 254 new_packet->next = packet->next; 241 item = pm_find( packet->next ); 242 if( item ) item->previous = new_packet->packet_id; 255 item = pm_find(packet->next); 256 if(item){ 257 item->previous = new_packet->packet_id; 258 } 243 259 packet->next = new_packet->packet_id; 244 260 return EOK; 245 261 } 246 262 247 packet_t pq_detach( packet_t packet){263 packet_t pq_detach(packet_t packet){ 248 264 packet_t next; 249 265 packet_t previous; 250 266 251 if( ! packet_is_valid( packet )) return NULL; 252 next = pm_find( packet->next ); 253 if( next ){ 267 if(! packet_is_valid(packet)){ 268 return NULL; 269 } 270 next = pm_find(packet->next); 271 if(next){ 254 272 next->previous = packet->previous; 255 previous = pm_find( next->previous);256 if( previous){273 previous = pm_find(next->previous); 274 if(previous){ 257 275 previous->next = next->packet_id; 258 276 } … … 263 281 } 264 282 265 int pq_set_order( packet_t packet, size_t order, size_t metric ){ 266 if( ! packet_is_valid( packet )) return EINVAL; 283 int pq_set_order(packet_t packet, size_t order, size_t metric){ 284 if(! packet_is_valid(packet)){ 285 return EINVAL; 286 } 267 287 packet->order = order; 268 288 packet->metric = metric; … … 270 290 } 271 291 272 int pq_get_order( packet_t packet, size_t * order, size_t * metric ){ 273 if( ! packet_is_valid( packet )) return EINVAL; 274 if( order ) * order = packet->order; 275 if( metric ) * metric = packet->metric; 276 return EOK; 277 } 278 279 void pq_destroy( packet_t first, void ( * packet_release )( packet_t packet )){ 280 packet_t actual; 281 packet_t next; 292 int pq_get_order(packet_t packet, size_t * order, size_t * metric){ 293 if(! packet_is_valid(packet)){ 294 return EINVAL; 295 } 296 if(order){ 297 *order = packet->order; 298 } 299 if(metric){ 300 *metric = packet->metric; 301 } 302 return EOK; 303 } 304 305 void pq_destroy(packet_t first, void (*packet_release)(packet_t packet)){ 306 packet_t actual; 307 packet_t next; 282 308 283 309 actual = first; 284 while( packet_is_valid( actual)){285 next = pm_find( actual->next);310 while(packet_is_valid(actual)){ 311 next = pm_find(actual->next); 286 312 actual->next = 0; 287 313 actual->previous = 0; 288 if( packet_release ) packet_release( actual ); 314 if(packet_release){ 315 packet_release(actual); 316 } 289 317 actual = next; 290 318 } 291 319 } 292 320 293 packet_t pq_next( packet_t packet ){ 294 if( ! packet_is_valid( packet )) return NULL; 295 return pm_find( packet->next ); 296 } 297 298 packet_t pq_previous( packet_t packet ){ 299 if( ! packet_is_valid( packet )) return NULL; 300 return pm_find( packet->previous ); 321 packet_t pq_next(packet_t packet){ 322 if(! packet_is_valid(packet)){ 323 return NULL; 324 } 325 return pm_find(packet->next); 326 } 327 328 packet_t pq_previous(packet_t packet){ 329 if(! packet_is_valid(packet)){ 330 return NULL; 331 } 332 return pm_find(packet->previous); 301 333 } 302 334 -
uspace/srv/net/structures/packet/packet.h
raa85487 raadf01e 68 68 /** Reserved packet prefix length. 69 69 */ 70 size_t prefix;70 size_t prefix; 71 71 /** Maximal packet content length. 72 72 */ 73 size_t content;73 size_t content; 74 74 /** Reserved packet suffix length. 75 75 */ 76 size_t suffix;76 size_t suffix; 77 77 /** Maximal packet address length. 78 78 */ 79 size_t addr_len;79 size_t addr_len; 80 80 }; 81 81 … … 89 89 * @returns NULL if the mapping does not exist. 90 90 */ 91 packet_t pm_find( packet_id_t packet_id);91 packet_t pm_find(packet_id_t packet_id); 92 92 93 93 /** Adds the packet mapping. … … 98 98 * @returns ENOMEM if there is not enough memory left. 99 99 */ 100 int pm_add( packet_t packet);100 int pm_add(packet_t packet); 101 101 102 102 /** Initializes the packet map. … … 104 104 * @returns ENOMEM if there is not enough memory left. 105 105 */ 106 int pm_init( void);106 int pm_init(void); 107 107 108 108 /** Releases the packet map. 109 109 */ 110 void pm_destroy( void);110 void pm_destroy(void); 111 111 112 112 /** Add packet to the sorted queue. … … 121 121 * @returns EINVAL if the packet is not valid. 122 122 */ 123 int pq_add( packet_t * first, packet_t packet, size_t order, size_t metric);123 int pq_add(packet_t * first, packet_t packet, size_t order, size_t metric); 124 124 125 125 /** Finds the packet with the given order. … … 130 130 * @returns NULL if the packet is not found. 131 131 */ 132 packet_t pq_find( packet_t first, size_t order);132 packet_t pq_find(packet_t first, size_t order); 133 133 134 134 /** Inserts packet after the given one. … … 138 138 * @returns EINVAL if etiher of the packets is invalid. 139 139 */ 140 int pq_insert_after( packet_t packet, packet_t new_packet);140 int pq_insert_after(packet_t packet, packet_t new_packet); 141 141 142 142 /** Detach the packet from the queue. … … 146 146 * @returns NULL if the packet is not valid. 147 147 */ 148 packet_t pq_detach( packet_t packet);148 packet_t pq_detach(packet_t packet); 149 149 150 150 /** Sets the packet order and metric attributes. … … 155 155 * @returns EINVAL if the packet is invalid.. 156 156 */ 157 int pq_set_order( packet_t packet, size_t order, size_t metric);157 int pq_set_order(packet_t packet, size_t order, size_t metric); 158 158 159 159 /** Sets the packet order and metric attributes. … … 164 164 * @returns EINVAL if the packet is invalid.. 165 165 */ 166 int pq_get_order( packet_t packet, size_t * order, size_t * metric);166 int pq_get_order(packet_t packet, size_t * order, size_t * metric); 167 167 168 168 /** Releases the whole queue. … … 171 171 * @param[in] packet_release The releasing function called for each of the packets after its detachment. 172 172 */ 173 void pq_destroy( packet_t first, void ( * packet_release )( packet_t packet));173 void pq_destroy(packet_t first, void (*packet_release)(packet_t packet)); 174 174 175 175 /** Returns the next packet in the queue. … … 179 179 * @returns NULL if the packet is not valid. 180 180 */ 181 packet_t pq_next( packet_t packet);181 packet_t pq_next(packet_t packet); 182 182 183 183 /** Returns the previous packet in the queue. … … 187 187 * @returns NULL if the packet is not valid. 188 188 */ 189 packet_t pq_previous( packet_t packet);189 packet_t pq_previous(packet_t packet); 190 190 191 191 /*@}*/ -
uspace/srv/net/structures/packet/packet_client.c
raa85487 raadf01e 48 48 #include "packet_client.h" 49 49 50 int packet_copy_data( packet_t packet, const void * data, size_t length ){ 51 if( ! packet_is_valid( packet )) return EINVAL; 52 if( packet->data_start + length >= packet->length ) return ENOMEM; 53 memcpy(( void * ) packet + packet->data_start, data, length ); 54 if( packet->data_start + length > packet->data_end ){ 50 int packet_copy_data(packet_t packet, const void * data, size_t length){ 51 if(! packet_is_valid(packet)){ 52 return EINVAL; 53 } 54 if(packet->data_start + length >= packet->length){ 55 return ENOMEM; 56 } 57 memcpy((void *) packet + packet->data_start, data, length); 58 if(packet->data_start + length > packet->data_end){ 55 59 packet->data_end = packet->data_start + length; 56 60 } … … 58 62 } 59 63 60 void * packet_prefix( packet_t packet, size_t length ){ 61 if(( ! packet_is_valid( packet )) || ( packet->data_start - sizeof( struct packet ) - 2 * ( packet->dest_addr - packet->src_addr ) < length )) return NULL; 64 void * packet_prefix(packet_t packet, size_t length){ 65 if((! packet_is_valid(packet)) || (packet->data_start - sizeof(struct packet) - 2 * (packet->dest_addr - packet->src_addr) < length)){ 66 return NULL; 67 } 62 68 packet->data_start -= length; 63 return ( void *) packet + packet->data_start;69 return (void *) packet + packet->data_start; 64 70 } 65 71 66 void * packet_suffix( packet_t packet, size_t length ){ 67 if(( ! packet_is_valid( packet )) || ( packet->data_end + length >= packet->length )) return NULL; 72 void * packet_suffix(packet_t packet, size_t length){ 73 if((! packet_is_valid(packet)) || (packet->data_end + length >= packet->length)){ 74 return NULL; 75 } 68 76 packet->data_end += length; 69 return ( void *) packet + packet->data_end - length;77 return (void *) packet + packet->data_end - length; 70 78 } 71 79 72 int packet_trim( packet_t packet, size_t prefix, size_t suffix ){ 73 if( ! packet_is_valid( packet )) return EINVAL; 74 if( prefix + suffix > PACKET_DATA_LENGTH( packet )) return ENOMEM; 80 int packet_trim(packet_t packet, size_t prefix, size_t suffix){ 81 if(! packet_is_valid(packet)){ 82 return EINVAL; 83 } 84 if(prefix + suffix > PACKET_DATA_LENGTH(packet)){ 85 return ENOMEM; 86 } 75 87 packet->data_start += prefix; 76 88 packet->data_end -= suffix; … … 78 90 } 79 91 80 packet_id_t packet_get_id( const packet_t packet){81 return packet_is_valid( packet) ? packet->packet_id : 0;92 packet_id_t packet_get_id(const packet_t packet){ 93 return packet_is_valid(packet) ? packet->packet_id : 0; 82 94 } 83 95 84 int packet_get_addr( const packet_t packet, uint8_t ** src, uint8_t ** dest ){ 85 if( ! packet_is_valid( packet )) return EINVAL; 86 if( ! packet->addr_len ) return 0; 87 if( src ) * src = ( void * ) packet + packet->src_addr; 88 if( dest ) * dest = ( void * ) packet + packet->dest_addr; 96 int packet_get_addr(const packet_t packet, uint8_t ** src, uint8_t ** dest){ 97 if(! packet_is_valid(packet)){ 98 return EINVAL; 99 } 100 if(! packet->addr_len){ 101 return 0; 102 } 103 if(src){ 104 *src = (void *) packet + packet->src_addr; 105 } 106 if(dest){ 107 *dest = (void *) packet + packet->dest_addr; 108 } 89 109 return packet->addr_len; 90 110 } 91 111 92 size_t packet_get_data_length( const packet_t packet ){ 93 if( ! packet_is_valid( packet )) return 0; 94 return PACKET_DATA_LENGTH( packet ); 112 size_t packet_get_data_length(const packet_t packet){ 113 if(! packet_is_valid(packet)){ 114 return 0; 115 } 116 return PACKET_DATA_LENGTH(packet); 95 117 } 96 118 97 void * packet_get_data( const packet_t packet ){ 98 if( ! packet_is_valid( packet )) return NULL; 99 return ( void * ) packet + packet->data_start; 119 void * packet_get_data(const packet_t packet){ 120 if(! packet_is_valid(packet)){ 121 return NULL; 122 } 123 return (void *) packet + packet->data_start; 100 124 } 101 125 102 int packet_set_addr( packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len){103 size_t padding;104 size_t allocated;126 int packet_set_addr(packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len){ 127 size_t padding; 128 size_t allocated; 105 129 106 if( ! packet_is_valid( packet )) return EINVAL; 107 allocated = PACKET_MAX_ADDRESS_LENGTH( packet ); 108 if( allocated < addr_len ) return ENOMEM; 130 if(! packet_is_valid(packet)){ 131 return EINVAL; 132 } 133 allocated = PACKET_MAX_ADDRESS_LENGTH(packet); 134 if(allocated < addr_len){ 135 return ENOMEM; 136 } 109 137 padding = allocated - addr_len; 110 138 packet->addr_len = addr_len; 111 if( src ){ 112 memcpy(( void * ) packet + packet->src_addr, src, addr_len ); 113 if( padding ) bzero(( void * ) packet + packet->src_addr + addr_len, padding ); 139 if(src){ 140 memcpy((void *) packet + packet->src_addr, src, addr_len); 141 if(padding){ 142 bzero((void *) packet + packet->src_addr + addr_len, padding); 143 } 114 144 }else{ 115 bzero(( void * ) packet + packet->src_addr, allocated);145 bzero((void *) packet + packet->src_addr, allocated); 116 146 } 117 if( dest ){ 118 memcpy(( void * ) packet + packet->dest_addr, dest, addr_len ); 119 if( padding ) bzero(( void * ) packet + packet->dest_addr + addr_len, padding ); 147 if(dest){ 148 memcpy((void *) packet + packet->dest_addr, dest, addr_len); 149 if(padding){ 150 bzero((void *) packet + packet->dest_addr + addr_len, padding); 151 } 120 152 }else{ 121 bzero(( void * ) packet + packet->dest_addr, allocated);153 bzero((void *) packet + packet->dest_addr, allocated); 122 154 } 123 155 return EOK; 124 156 } 125 157 126 packet_t packet_get_copy( int phone, packet_t packet){127 packet_t copy;128 uint8_t * src;129 uint8_t * dest;130 size_t addrlen;158 packet_t packet_get_copy(int phone, packet_t packet){ 159 packet_t copy; 160 uint8_t * src; 161 uint8_t * dest; 162 size_t addrlen; 131 163 132 if( ! packet_is_valid( packet )) return NULL; 164 if(! packet_is_valid(packet)){ 165 return NULL; 166 } 133 167 // get a new packet 134 copy = packet_get_4( phone, PACKET_DATA_LENGTH( packet ), PACKET_MAX_ADDRESS_LENGTH( packet ), packet->max_prefix, PACKET_MIN_SUFFIX( packet )); 135 if( ! copy ) return NULL; 168 copy = packet_get_4(phone, PACKET_DATA_LENGTH(packet), PACKET_MAX_ADDRESS_LENGTH(packet), packet->max_prefix, PACKET_MIN_SUFFIX(packet)); 169 if(! copy){ 170 return NULL; 171 } 136 172 // get addresses 137 addrlen = packet_get_addr( packet, & src, & dest);173 addrlen = packet_get_addr(packet, &src, &dest); 138 174 // copy data 139 if(( packet_copy_data( copy, packet_get_data( packet ), PACKET_DATA_LENGTH( packet )) == EOK)175 if((packet_copy_data(copy, packet_get_data(packet), PACKET_DATA_LENGTH(packet)) == EOK) 140 176 // copy addresses if present 141 && (( addrlen <= 0 ) || ( packet_set_addr( copy, src, dest, addrlen ) == EOK))){177 && ((addrlen <= 0) || (packet_set_addr(copy, src, dest, addrlen) == EOK))){ 142 178 copy->order = packet->order; 143 179 copy->metric = packet->metric; 144 180 return copy; 145 181 }else{ 146 pq_release( phone, copy->packet_id);182 pq_release(phone, copy->packet_id); 147 183 return NULL; 148 184 } -
uspace/srv/net/structures/packet/packet_client.h
raa85487 raadf01e 59 59 * @returns NULL if there is not enough memory left. 60 60 */ 61 #define PACKET_PREFIX( packet, type ) ( type * ) packet_prefix(( packet ), sizeof( type))61 #define PACKET_PREFIX(packet, type) (type *) packet_prefix((packet), sizeof(type)) 62 62 63 63 /** Allocates the specified type right after the actual packet content and returns its pointer. … … 69 69 * @returns NULL if there is not enough memory left. 70 70 */ 71 #define PACKET_SUFFIX( packet, type ) ( type * ) packet_suffix(( packet ), sizeof( type))71 #define PACKET_SUFFIX(packet, type) (type *) packet_suffix((packet), sizeof(type)) 72 72 73 73 /** Trims the actual packet content by the specified prefix and suffix types. … … 80 80 * @returns ENOMEM if there is not enough memory left. 81 81 */ 82 #define PACKET_TRIM( packet, prefix, suffix ) packet_trim(( packet ), sizeof( prefix ), sizeof( suffix))82 #define PACKET_TRIM(packet, prefix, suffix) packet_trim((packet), sizeof(prefix), sizeof(suffix)) 83 83 84 84 /** Allocates the specified space right before the actual packet content and returns its pointer. … … 88 88 * @returns NULL if there is not enough memory left. 89 89 */ 90 void * packet_prefix( packet_t packet, size_t length);90 void * packet_prefix(packet_t packet, size_t length); 91 91 92 92 /** Allocates the specified space right after the actual packet content and returns its pointer. … … 96 96 * @returns NULL if there is not enough memory left. 97 97 */ 98 void * packet_suffix( packet_t packet, size_t length);98 void * packet_suffix(packet_t packet, size_t length); 99 99 100 100 /** Trims the actual packet content by the specified prefix and suffix lengths. … … 106 106 * @returns ENOMEM if there is not enough memory left. 107 107 */ 108 int packet_trim( packet_t packet, size_t prefix, size_t suffix);108 int packet_trim(packet_t packet, size_t prefix, size_t suffix); 109 109 110 110 /** Copies the specified data to the beginning of the actual packet content. … … 117 117 * @returns ENOMEM if there is not enough memory left. 118 118 */ 119 int packet_copy_data( packet_t packet, const void * data, size_t length);119 int packet_copy_data(packet_t packet, const void * data, size_t length); 120 120 121 121 /** Returns the packet identifier. … … 124 124 * @returns Zero (0) if the packet is not valid. 125 125 */ 126 packet_id_t packet_get_id( const packet_t packet);126 packet_id_t packet_get_id(const packet_t packet); 127 127 128 128 /** Returns the packet content length. … … 131 131 * @returns Zero (0) if the packet is not valid. 132 132 */ 133 size_t packet_get_data_length( const packet_t packet);133 size_t packet_get_data_length(const packet_t packet); 134 134 135 135 /** Returns the pointer to the beginning of the packet content. … … 138 138 * @returns NULL if the packet is not valid. 139 139 */ 140 void * packet_get_data( const packet_t packet);140 void * packet_get_data(const packet_t packet); 141 141 142 142 /** Returns the stored packet addresses and their length. … … 148 148 * @returns EINVAL if the packet is not valid. 149 149 */ 150 int packet_get_addr( const packet_t packet, uint8_t ** src, uint8_t ** dest);150 int packet_get_addr(const packet_t packet, uint8_t ** src, uint8_t ** dest); 151 151 152 152 /** Sets the packet addresses. … … 159 159 * @returns ENOMEM if there is not enough memory left. 160 160 */ 161 int packet_set_addr( packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len);161 int packet_set_addr(packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len); 162 162 163 163 /** Translates the packet identifier to the packet reference. … … 172 172 * @returns Other error codes as defined for the packet_return() function. 173 173 */ 174 int packet_translate( int phone, packet_ref packet, packet_id_t packet_id);174 int packet_translate(int phone, packet_ref packet, packet_id_t packet_id); 175 175 176 176 /** Obtains the packet of the given dimensions. … … 184 184 * @returns NULL on error. 185 185 */ 186 packet_t packet_get_4( int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix);186 packet_t packet_get_4(int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix); 187 187 188 188 /** Obtains the packet of the given content size. … … 193 193 * @returns NULL on error. 194 194 */ 195 packet_t packet_get_1( int phone, size_t content);195 packet_t packet_get_1(int phone, size_t content); 196 196 197 197 /** Releases the packet queue. … … 202 202 * @param[in] packet_id The packet identifier. 203 203 */ 204 void pq_release( int phone, packet_id_t packet_id);204 void pq_release(int phone, packet_id_t packet_id); 205 205 206 206 /** Returns the packet copy. … … 212 212 * @returns NULL on error. 213 213 */ 214 packet_t packet_get_copy( int phone, packet_t packet);214 packet_t packet_get_copy(int phone, packet_t packet); 215 215 216 216 /*@}*/ -
uspace/srv/net/structures/packet/packet_header.h
raa85487 raadf01e 43 43 * @param[in] header The packet header. 44 44 */ 45 #define PACKET_DATA_LENGTH( header ) (( header )->data_end - ( header )->data_start)45 #define PACKET_DATA_LENGTH(header) ((header)->data_end - (header)->data_start) 46 46 47 47 /** Returns the maximum packet address length. 48 48 * @param[in] header The packet header. 49 49 */ 50 #define PACKET_MAX_ADDRESS_LENGTH( header ) (( header )->dest_addr - ( header )->src_addr)50 #define PACKET_MAX_ADDRESS_LENGTH(header) ((header)->dest_addr - (header)->src_addr) 51 51 52 52 /** Returns the minimum packet suffix. 53 53 * @param[in] header The packet header. 54 54 */ 55 #define PACKET_MIN_SUFFIX( header ) (( header )->length - ( header )->data_start - ( header )->max_content)55 #define PACKET_MIN_SUFFIX(header) ((header)->length - (header)->data_start - (header)->max_content) 56 56 57 57 /** Packet integrity check magic value. … … 64 64 /** Packet identifier. 65 65 */ 66 packet_id_t packet_id;66 packet_id_t packet_id; 67 67 /** Packet queue sorting value. 68 68 * The packet queue is sorted the ascending order. 69 69 */ 70 size_t order;70 size_t order; 71 71 /** Packet metric. 72 72 */ 73 size_t metric;73 size_t metric; 74 74 /** Previous packet in the queue. 75 75 */ 76 packet_id_t previous;76 packet_id_t previous; 77 77 /** Next packet in the queue. 78 78 */ 79 packet_id_t next;79 packet_id_t next; 80 80 /** Total length of the packet. 81 81 * Contains the header, the addresses and the data of the packet. 82 82 * Corresponds to the mapped sharable memory block. 83 83 */ 84 size_t length;84 size_t length; 85 85 /** Stored source and destination addresses length. 86 86 */ 87 size_t addr_len;87 size_t addr_len; 88 88 /** Souce address offset in bytes from the beginning of the packet header. 89 89 */ 90 size_t src_addr;90 size_t src_addr; 91 91 /** Destination address offset in bytes from the beginning of the packet header. 92 92 */ 93 size_t dest_addr;93 size_t dest_addr; 94 94 /** Reserved data prefix length in bytes. 95 95 */ 96 size_t max_prefix;96 size_t max_prefix; 97 97 /** Reserved content length in bytes. 98 98 */ 99 size_t max_content;99 size_t max_content; 100 100 /** Actual data start offset in bytes from the beginning of the packet header. 101 101 */ 102 size_t data_start;102 size_t data_start; 103 103 /** Actual data end offset in bytes from the beginning of the packet header. 104 104 */ 105 size_t data_end;105 size_t data_end; 106 106 /** Integrity check magic value. 107 107 */ 108 int magic_value;108 int magic_value; 109 109 }; 110 110 … … 114 114 * @returns false otherwise. 115 115 */ 116 static inline int packet_is_valid( const packet_t packet){117 return packet && ( packet->magic_value == PACKET_MAGIC_VALUE);116 static inline int packet_is_valid(const packet_t packet){ 117 return packet && (packet->magic_value == PACKET_MAGIC_VALUE); 118 118 } 119 119 -
uspace/srv/net/structures/packet/packet_messages.h
raa85487 raadf01e 69 69 /** Returns the protocol service message parameter. 70 70 */ 71 #define ARP_GET_PROTO( call ) ( services_t ) IPC_GET_ARG2( * call)71 #define ARP_GET_PROTO(call) (services_t) IPC_GET_ARG2(*call) 72 72 73 73 /** Returns the packet identifier message parameter. 74 74 */ 75 #define IPC_GET_ID( call ) ( packet_id_t ) IPC_GET_ARG1( * call)75 #define IPC_GET_ID(call) (packet_id_t) IPC_GET_ARG1(*call) 76 76 77 77 /** Returns the maximal content length message parameter. 78 78 */ 79 #define IPC_GET_CONTENT( call ) ( size_t ) IPC_GET_ARG1( * call)79 #define IPC_GET_CONTENT(call) (size_t) IPC_GET_ARG1(*call) 80 80 81 81 /** Returns the maximal address length message parameter. 82 82 */ 83 #define IPC_GET_ADDR_LEN( call ) ( size_t ) IPC_GET_ARG2( * call)83 #define IPC_GET_ADDR_LEN(call) (size_t) IPC_GET_ARG2(*call) 84 84 85 85 /** Returns the maximal prefix length message parameter. 86 86 */ 87 #define IPC_GET_PREFIX( call ) ( size_t ) IPC_GET_ARG3( * call)87 #define IPC_GET_PREFIX(call) (size_t) IPC_GET_ARG3(*call) 88 88 89 89 /** Returns the maximal suffix length message parameter. 90 90 */ 91 #define IPC_GET_SUFFIX( call ) ( size_t ) IPC_GET_ARG4( * call)91 #define IPC_GET_SUFFIX(call) (size_t) IPC_GET_ARG4(*call) 92 92 93 93 #endif -
uspace/srv/net/structures/packet/packet_remote.c
raa85487 raadf01e 60 60 * @returns Other error codes as defined for the async_share_in_start() function. 61 61 */ 62 int packet_return( int phone, packet_ref packet, packet_id_t packet_id, size_t size);62 int packet_return(int phone, packet_ref packet, packet_id_t packet_id, size_t size); 63 63 64 int packet_translate( int phone, packet_ref packet, packet_id_t packet_id){64 int packet_translate(int phone, packet_ref packet, packet_id_t packet_id){ 65 65 ERROR_DECLARE; 66 66 67 ipcarg_t size;68 packet_t next;67 ipcarg_t size; 68 packet_t next; 69 69 70 if( ! packet ) return EINVAL; 71 * packet = pm_find( packet_id ); 72 if( !( * packet )){ 73 ERROR_PROPAGATE( async_req_1_1( phone, NET_PACKET_GET_SIZE, packet_id, & size )); 74 ERROR_PROPAGATE( packet_return( phone, packet, packet_id, size )); 70 if(! packet){ 71 return EINVAL; 75 72 } 76 if(( ** packet ).next ){ 77 return packet_translate( phone, & next, ( ** packet ).next ); 73 *packet = pm_find(packet_id); 74 if(!(*packet)){ 75 ERROR_PROPAGATE(async_req_1_1(phone, NET_PACKET_GET_SIZE, packet_id, &size)); 76 ERROR_PROPAGATE(packet_return(phone, packet, packet_id, size)); 77 } 78 if((** packet).next){ 79 return packet_translate(phone, &next, (** packet).next); 78 80 }else return EOK; 79 81 } 80 82 81 int packet_return( int phone, packet_ref packet, packet_id_t packet_id, size_t size){83 int packet_return(int phone, packet_ref packet, packet_id_t packet_id, size_t size){ 82 84 ERROR_DECLARE; 83 85 84 aid_t message;85 ipc_call_t answer;86 ipcarg_t result;86 aid_t message; 87 ipc_call_t answer; 88 ipcarg_t result; 87 89 88 message = async_send_1( phone, NET_PACKET_GET, packet_id, & answer);89 * packet = ( packet_t ) as_get_mappable_page( size);90 if( ERROR_OCCURRED( async_share_in_start_0_0( phone, * packet, size))91 || ERROR_OCCURRED( pm_add( * packet))){92 munmap( * packet, size);93 async_wait_for( message, NULL);90 message = async_send_1(phone, NET_PACKET_GET, packet_id, &answer); 91 *packet = (packet_t) as_get_mappable_page(size); 92 if(ERROR_OCCURRED(async_share_in_start_0_0(phone, * packet, size)) 93 || ERROR_OCCURRED(pm_add(*packet))){ 94 munmap(*packet, size); 95 async_wait_for(message, NULL); 94 96 return ERROR_CODE; 95 97 } 96 async_wait_for( message, & result);98 async_wait_for(message, &result); 97 99 return result; 98 100 } 99 101 100 packet_t packet_get_4( int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix){102 packet_t packet_get_4(int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix){ 101 103 ERROR_DECLARE; 102 104 … … 105 107 packet_t packet; 106 108 107 if( ERROR_OCCURRED( async_req_4_2( phone, NET_PACKET_CREATE_4, max_content, addr_len, max_prefix, max_suffix, & packet_id, & size))){109 if(ERROR_OCCURRED(async_req_4_2(phone, NET_PACKET_CREATE_4, max_content, addr_len, max_prefix, max_suffix, &packet_id, &size))){ 108 110 return NULL; 109 111 } 110 packet = pm_find( packet_id);111 if( ! packet){112 if( ERROR_OCCURRED( packet_return( phone, & packet, packet_id, size))){112 packet = pm_find(packet_id); 113 if(! packet){ 114 if(ERROR_OCCURRED(packet_return(phone, &packet, packet_id, size))){ 113 115 return NULL; 114 116 } … … 117 119 } 118 120 119 packet_t packet_get_1( int phone, size_t content){121 packet_t packet_get_1(int phone, size_t content){ 120 122 ERROR_DECLARE; 121 123 122 ipcarg_t packet_id;123 ipcarg_t size;124 packet_t packet;124 ipcarg_t packet_id; 125 ipcarg_t size; 126 packet_t packet; 125 127 126 if( ERROR_OCCURRED( async_req_1_2( phone, NET_PACKET_CREATE_1, content, & packet_id, & size))){128 if(ERROR_OCCURRED(async_req_1_2(phone, NET_PACKET_CREATE_1, content, &packet_id, &size))){ 127 129 return NULL; 128 130 } 129 packet = pm_find( packet_id);130 if( ! packet){131 if( ERROR_OCCURRED( packet_return( phone, & packet, packet_id, size))){131 packet = pm_find(packet_id); 132 if(! packet){ 133 if(ERROR_OCCURRED(packet_return(phone, &packet, packet_id, size))){ 132 134 return NULL; 133 135 } … … 136 138 } 137 139 138 void pq_release( int phone, packet_id_t packet_id){139 async_msg_1( phone, NET_PACKET_RELEASE, packet_id);140 void pq_release(int phone, packet_id_t packet_id){ 141 async_msg_1(phone, NET_PACKET_RELEASE, packet_id); 140 142 } 141 143 -
uspace/srv/net/structures/packet/packet_server.c
raa85487 raadf01e 76 76 /** Free packet queues. 77 77 */ 78 packet_t free[ FREE_QUEUES_COUNT];78 packet_t free[FREE_QUEUES_COUNT]; 79 79 /** Packet length upper bounds of the free packet queues. 80 80 * The maximal lengths of packets in each queue in the ascending order. 81 81 * The last queue is not limited. 82 82 */ 83 size_t sizes[ FREE_QUEUES_COUNT];83 size_t sizes[FREE_QUEUES_COUNT]; 84 84 /** Total packets allocated. 85 85 */ … … 89 89 .counter = 1, 90 90 .waiters = { 91 .prev = & ps_globals.lock.waiters,92 .next = & ps_globals.lock.waiters,91 .prev = &ps_globals.lock.waiters, 92 .next = &ps_globals.lock.waiters, 93 93 } 94 94 }, 95 .free = { NULL, NULL, NULL, NULL, NULL, NULL, NULL},96 .sizes = { PAGE_SIZE, PAGE_SIZE * 2, PAGE_SIZE * 4, PAGE_SIZE * 8, PAGE_SIZE * 16, PAGE_SIZE * 32, PAGE_SIZE * 64},95 .free = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}, 96 .sizes = {PAGE_SIZE, PAGE_SIZE * 2, PAGE_SIZE * 4, PAGE_SIZE * 8, PAGE_SIZE * 16, PAGE_SIZE * 32, PAGE_SIZE * 64}, 97 97 .count = 0 98 98 }; … … 113 113 * @returns NULL if there is not enough memory left. 114 114 */ 115 packet_t packet_get( size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix);115 packet_t packet_get(size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix); 116 116 117 117 /** Releases the packet queue. … … 120 120 * @returns ENOENT if there is no such packet. 121 121 */ 122 int packet_release_wrapper( packet_id_t packet_id);122 int packet_release_wrapper(packet_id_t packet_id); 123 123 124 124 /** Releases the packet and returns it to the appropriate free packet queue. … … 126 126 * @param[in] packet The packet to be released. 127 127 */ 128 void packet_release( packet_t packet);128 void packet_release(packet_t packet); 129 129 130 130 /** Creates a new packet of dimensions at least as given. … … 138 138 * @returns NULL if there is not enough memory left. 139 139 */ 140 packet_t packet_create( size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix);140 packet_t packet_create(size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix); 141 141 142 142 /** Clears and initializes the packet according to the given dimensions. … … 147 147 * @param[in] max_suffix The maximal suffix length in bytes. 148 148 */ 149 void packet_init( packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix);149 void packet_init(packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix); 150 150 151 151 /** Shares the packet memory block. … … 157 157 * @returns Other error codes as defined for the async_share_in_finalize() function. 158 158 */ 159 int packet_reply( const packet_t packet);159 int packet_reply(const packet_t packet); 160 160 161 161 /*@}*/ 162 162 163 int packet_translate( int phone, packet_ref packet, packet_id_t packet_id ){ 164 if( ! packet ) return EINVAL; 165 * packet = pm_find( packet_id ); 166 return ( * packet ) ? EOK : ENOENT; 167 } 168 169 packet_t packet_get_4( int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix ){ 170 return packet_get( addr_len, max_prefix, max_content, max_suffix ); 171 } 172 173 packet_t packet_get_1( int phone, size_t content ){ 174 return packet_get( DEFAULT_ADDR_LEN, DEFAULT_PREFIX, content, DEFAULT_SUFFIX ); 175 } 176 177 void pq_release( int phone, packet_id_t packet_id ){ 178 ( void ) packet_release_wrapper( packet_id ); 179 } 180 181 int packet_server_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ 163 int packet_translate(int phone, packet_ref packet, packet_id_t packet_id){ 164 if(! packet){ 165 return EINVAL; 166 } 167 *packet = pm_find(packet_id); 168 return (*packet) ? EOK : ENOENT; 169 } 170 171 packet_t packet_get_4(int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix){ 172 return packet_get(addr_len, max_prefix, max_content, max_suffix); 173 } 174 175 packet_t packet_get_1(int phone, size_t content){ 176 return packet_get(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, content, DEFAULT_SUFFIX); 177 } 178 179 void pq_release(int phone, packet_id_t packet_id){ 180 (void) packet_release_wrapper(packet_id); 181 } 182 183 int packet_server_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){ 182 184 packet_t packet; 183 185 184 * answer_count = 0;185 switch( IPC_GET_METHOD( * call)){186 *answer_count = 0; 187 switch(IPC_GET_METHOD(*call)){ 186 188 case IPC_M_PHONE_HUNGUP: 187 189 return EOK; 188 190 case NET_PACKET_CREATE_1: 189 packet = packet_get( DEFAULT_ADDR_LEN, DEFAULT_PREFIX, IPC_GET_CONTENT( call ), DEFAULT_SUFFIX ); 190 if( ! packet ) return ENOMEM; 191 * answer_count = 2; 192 IPC_SET_ARG1( * answer, packet->packet_id ); 193 IPC_SET_ARG2( * answer, packet->length ); 191 packet = packet_get(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, IPC_GET_CONTENT(call), DEFAULT_SUFFIX); 192 if(! packet){ 193 return ENOMEM; 194 } 195 *answer_count = 2; 196 IPC_SET_ARG1(*answer, packet->packet_id); 197 IPC_SET_ARG2(*answer, packet->length); 194 198 return EOK; 195 199 case NET_PACKET_CREATE_4: 196 packet = packet_get((( 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 )); 197 if( ! packet ) return ENOMEM; 198 * answer_count = 2; 199 IPC_SET_ARG1( * answer, packet->packet_id ); 200 IPC_SET_ARG2( * answer, packet->length ); 200 packet = packet_get(((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)); 201 if(! packet){ 202 return ENOMEM; 203 } 204 *answer_count = 2; 205 IPC_SET_ARG1(*answer, packet->packet_id); 206 IPC_SET_ARG2(*answer, packet->length); 201 207 return EOK; 202 208 case NET_PACKET_GET: 203 packet = pm_find( IPC_GET_ID( call )); 204 if( ! packet_is_valid( packet )) return ENOENT; 205 return packet_reply( packet ); 209 packet = pm_find(IPC_GET_ID(call)); 210 if(! packet_is_valid(packet)){ 211 return ENOENT; 212 } 213 return packet_reply(packet); 206 214 case NET_PACKET_GET_SIZE: 207 packet = pm_find( IPC_GET_ID( call )); 208 if( ! packet_is_valid( packet )) return ENOENT; 209 IPC_SET_ARG1( * answer, packet->length ); 210 * answer_count = 1; 215 packet = pm_find(IPC_GET_ID(call)); 216 if(! packet_is_valid(packet)){ 217 return ENOENT; 218 } 219 IPC_SET_ARG1(*answer, packet->length); 220 *answer_count = 1; 211 221 return EOK; 212 222 case NET_PACKET_RELEASE: 213 return packet_release_wrapper( IPC_GET_ID( call));223 return packet_release_wrapper(IPC_GET_ID(call)); 214 224 } 215 225 return ENOTSUP; 216 226 } 217 227 218 int packet_release_wrapper( packet_id_t packet_id ){ 219 packet_t packet; 220 221 packet = pm_find( packet_id ); 222 if( ! packet_is_valid( packet )) return ENOENT; 223 fibril_mutex_lock( & ps_globals.lock ); 224 pq_destroy( packet, packet_release ); 225 fibril_mutex_unlock( & ps_globals.lock ); 228 int packet_release_wrapper(packet_id_t packet_id){ 229 packet_t packet; 230 231 packet = pm_find(packet_id); 232 if(! packet_is_valid(packet)){ 233 return ENOENT; 234 } 235 fibril_mutex_lock(&ps_globals.lock); 236 pq_destroy(packet, packet_release); 237 fibril_mutex_unlock(&ps_globals.lock); 226 238 return EOK; 227 239 } 228 240 229 void packet_release( packet_t packet){241 void packet_release(packet_t packet){ 230 242 int index; 231 243 int result; 232 244 233 245 // remove debug dump 234 // printf( "packet %d released\n", packet->packet_id);235 for( index = 0; ( index < FREE_QUEUES_COUNT - 1 ) && ( packet->length > ps_globals.sizes[ index ] ); ++ index);236 result = pq_add( & ps_globals.free[ index ], packet, packet->length, packet->length);237 assert( result == EOK);238 } 239 240 packet_t packet_get( size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){246 // printf("packet %d released\n", packet->packet_id); 247 for(index = 0; (index < FREE_QUEUES_COUNT - 1) && (packet->length > ps_globals.sizes[index]); ++ index); 248 result = pq_add(&ps_globals.free[index], packet, packet->length, packet->length); 249 assert(result == EOK); 250 } 251 252 packet_t packet_get(size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){ 241 253 int index; 242 254 packet_t packet; 243 255 size_t length; 244 256 245 length = ALIGN_UP( sizeof( struct packet ) + 2 * addr_len + max_prefix + max_content + max_suffix, PAGE_SIZE);246 fibril_mutex_lock( & ps_globals.lock);247 for( index = 0; index < FREE_QUEUES_COUNT - 1; ++ index){248 if( length <= ps_globals.sizes[ index ]){249 packet = ps_globals.free[ index];250 while( packet_is_valid( packet ) && ( packet->length < length)){251 packet = pm_find( packet->next);252 } 253 if( packet_is_valid( packet)){254 if( packet == ps_globals.free[ index ]){255 ps_globals.free[ index ] = pq_detach( packet);257 length = ALIGN_UP(sizeof(struct packet) + 2 * addr_len + max_prefix + max_content + max_suffix, PAGE_SIZE); 258 fibril_mutex_lock(&ps_globals.lock); 259 for(index = 0; index < FREE_QUEUES_COUNT - 1; ++ index){ 260 if(length <= ps_globals.sizes[index]){ 261 packet = ps_globals.free[index]; 262 while(packet_is_valid(packet) && (packet->length < length)){ 263 packet = pm_find(packet->next); 264 } 265 if(packet_is_valid(packet)){ 266 if(packet == ps_globals.free[index]){ 267 ps_globals.free[index] = pq_detach(packet); 256 268 }else{ 257 pq_detach( packet);269 pq_detach(packet); 258 270 } 259 packet_init( packet, addr_len, max_prefix, max_content, max_suffix);260 fibril_mutex_unlock( & ps_globals.lock);271 packet_init(packet, addr_len, max_prefix, max_content, max_suffix); 272 fibril_mutex_unlock(&ps_globals.lock); 261 273 // remove debug dump 262 // printf( "packet %d got\n", packet->packet_id);274 // printf("packet %d got\n", packet->packet_id); 263 275 return packet; 264 276 } 265 277 } 266 278 } 267 packet = packet_create( length, addr_len, max_prefix, max_content, max_suffix);268 fibril_mutex_unlock( & ps_globals.lock);279 packet = packet_create(length, addr_len, max_prefix, max_content, max_suffix); 280 fibril_mutex_unlock(&ps_globals.lock); 269 281 // remove debug dump 270 // printf( "packet %d created\n", packet->packet_id);282 // printf("packet %d created\n", packet->packet_id); 271 283 return packet; 272 284 } 273 285 274 packet_t packet_create( size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){286 packet_t packet_create(size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){ 275 287 ERROR_DECLARE; 276 288 277 packet_t packet;289 packet_t packet; 278 290 279 291 // already locked 280 packet = ( packet_t ) mmap( NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 ); 281 if( packet == MAP_FAILED ) return NULL; 292 packet = (packet_t) mmap(NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0); 293 if(packet == MAP_FAILED){ 294 return NULL; 295 } 282 296 ++ ps_globals.count; 283 297 packet->packet_id = ps_globals.count; 284 298 packet->length = length; 285 packet_init( packet, addr_len, max_prefix, max_content, max_suffix);299 packet_init(packet, addr_len, max_prefix, max_content, max_suffix); 286 300 packet->magic_value = PACKET_MAGIC_VALUE; 287 if( ERROR_OCCURRED( pm_add( packet))){288 munmap( packet, packet->length);301 if(ERROR_OCCURRED(pm_add(packet))){ 302 munmap(packet, packet->length); 289 303 return NULL; 290 304 } … … 292 306 } 293 307 294 void packet_init( packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){308 void packet_init(packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){ 295 309 // clear the packet content 296 bzero((( void * ) packet ) + sizeof( struct packet ), packet->length - sizeof( struct packet));310 bzero(((void *) packet) + sizeof(struct packet), packet->length - sizeof(struct packet)); 297 311 // clear the packet header 298 312 packet->order = 0; … … 301 315 packet->next = 0; 302 316 packet->addr_len = 0; 303 packet->src_addr = sizeof( struct packet);317 packet->src_addr = sizeof(struct packet); 304 318 packet->dest_addr = packet->src_addr + addr_len; 305 319 packet->max_prefix = max_prefix; … … 309 323 } 310 324 311 int packet_reply( const packet_t packet ){ 312 ipc_callid_t callid; 313 size_t size; 314 315 if( ! packet_is_valid( packet )) return EINVAL; 316 if( async_share_in_receive( & callid, & size ) <= 0 ) return EINVAL; 317 if( size != packet->length ) return ENOMEM; 318 return async_share_in_finalize( callid, packet, PROTO_READ | PROTO_WRITE ); 325 int packet_reply(const packet_t packet){ 326 ipc_callid_t callid; 327 size_t size; 328 329 if(! packet_is_valid(packet)){ 330 return EINVAL; 331 } 332 if(async_share_in_receive(&callid, &size) <= 0) return EINVAL; 333 if(size != packet->length){ 334 return ENOMEM; 335 } 336 return async_share_in_finalize(callid, packet, PROTO_READ | PROTO_WRITE); 319 337 } 320 338 -
uspace/srv/net/structures/packet/packet_server.h
raa85487 raadf01e 56 56 * @returns Other error codes as defined for the packet_release_wrapper() function. 57 57 */ 58 int packet_server_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count);58 int packet_server_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count); 59 59 60 60 #endif
Note:
See TracChangeset
for help on using the changeset viewer.
