Rev 3901 | Rev 3914 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 3901 | Rev 3912 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 1 | /* |
1 | /* |
| 2 | * Copyright (c) 2008 Lukas Mejdrech |
2 | * Copyright (c) 2009 Lukas Mejdrech |
| 3 | * All rights reserved. |
3 | * All rights reserved. |
| 4 | * |
4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
7 | * are met: |
| Line 24... | Line 24... | ||
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
27 | */ |
| 28 | 28 | ||
| 29 | /** @addtogroup net |
29 | /** @addtogroup packet |
| 30 | * @{ |
30 | * @{ |
| 31 | */ |
31 | */ |
| 32 | 32 | ||
| 33 | /** @file |
33 | /** @file |
| - | 34 | * Packet server implementation. |
|
| 34 | */ |
35 | */ |
| 35 | 36 | ||
| 36 | #include <async.h> |
37 | #include <async.h> |
| 37 | #include <align.h> |
38 | #include <align.h> |
| 38 | #include <errno.h> |
39 | #include <errno.h> |
| Line 48... | Line 49... | ||
| 48 | 49 | ||
| 49 | #include "packet.h" |
50 | #include "packet.h" |
| 50 | #include "packet_header.h" |
51 | #include "packet_header.h" |
| 51 | #include "packet_server.h" |
52 | #include "packet_server.h" |
| 52 | 53 | ||
| - | 54 | /** Returns the packet identifier message parameter. |
|
| - | 55 | */ |
|
| 53 | #define IPC_GET_ID( call ) ( packet_id_t ) IPC_GET_ARG1( * call ) |
56 | #define IPC_GET_ID( call ) ( packet_id_t ) IPC_GET_ARG1( * call ) |
| - | 57 | ||
| - | 58 | /** Returns the owner message parameter. |
|
| - | 59 | */ |
|
| 54 | #define IPC_GET_OWNER( call ) ( services_t ) IPC_GET_ARG1( * call ) |
60 | #define IPC_GET_OWNER( call ) ( services_t ) IPC_GET_ARG1( * call ) |
| - | 61 | ||
| - | 62 | /** Returns the maximal content length message parameter. |
|
| - | 63 | */ |
|
| 55 | #define IPC_GET_CONTENT( call ) ( size_t ) IPC_GET_ARG2( * call ) |
64 | #define IPC_GET_CONTENT( call ) ( size_t ) IPC_GET_ARG2( * call ) |
| - | 65 | ||
| - | 66 | /** Returns the maximal address length message parameter. |
|
| - | 67 | */ |
|
| 56 | #define IPC_GET_ADDR_LEN( call ) ( size_t ) IPC_GET_ARG3( * call ) |
68 | #define IPC_GET_ADDR_LEN( call ) ( size_t ) IPC_GET_ARG3( * call ) |
| - | 69 | ||
| - | 70 | /** Returns the maximal prefix length message parameter. |
|
| - | 71 | */ |
|
| 57 | #define IPC_GET_PREFIX( call ) ( size_t ) IPC_GET_ARG4( * call ) |
72 | #define IPC_GET_PREFIX( call ) ( size_t ) IPC_GET_ARG4( * call ) |
| - | 73 | ||
| - | 74 | /** Returns the maximal suffix length message parameter. |
|
| - | 75 | */ |
|
| 58 | #define IPC_GET_SUFIX( call ) ( size_t ) IPC_GET_ARG5( * call ) |
76 | #define IPC_GET_SUFIX( call ) ( size_t ) IPC_GET_ARG5( * call ) |
| 59 | 77 | ||
| 60 | #define FREE_QUEUES_COUNT 7 |
78 | #define FREE_QUEUES_COUNT 7 |
| 61 | 79 | ||
| - | 80 | /** Packet server global data. |
|
| - | 81 | */ |
|
| 62 | static struct{ |
82 | static struct{ |
| - | 83 | /** Free packet queues. |
|
| - | 84 | */ |
|
| 63 | packet_t free[ FREE_QUEUES_COUNT ]; |
85 | packet_t free[ FREE_QUEUES_COUNT ]; |
| - | 86 | /** Packet length upper bounds of the free packet queues. |
|
| - | 87 | * The maximal lengths of packets in each queue in the ascending order. |
|
| - | 88 | * The last queue is not limited. |
|
| - | 89 | */ |
|
| 64 | int sizes[ FREE_QUEUES_COUNT ]; |
90 | int sizes[ FREE_QUEUES_COUNT ]; |
| - | 91 | /** Total packets allocated. |
|
| - | 92 | */ |
|
| 65 | unsigned int count; |
93 | unsigned int count; |
| 66 | } ps_globals = { |
94 | } ps_globals = { |
| 67 | { NULL, NULL, NULL, NULL, NULL, NULL, NULL }, |
95 | { NULL, NULL, NULL, NULL, NULL, NULL, NULL }, |
| 68 | { PAGE_SIZE, PAGE_SIZE * 2, PAGE_SIZE * 4, PAGE_SIZE * 8, PAGE_SIZE * 16, PAGE_SIZE * 32, PAGE_SIZE * 64 }, |
96 | { PAGE_SIZE, PAGE_SIZE * 2, PAGE_SIZE * 4, PAGE_SIZE * 8, PAGE_SIZE * 16, PAGE_SIZE * 32, PAGE_SIZE * 64 }, |
| 69 | 0 |
97 | 0 |
| 70 | }; |
98 | }; |
| 71 | 99 | ||
| - | 100 | /** Releases the packet and returns it to the appropriate free packet queue. |
|
| - | 101 | * @param packet The packet to be released. Input parameter. |
|
| - | 102 | */ |
|
| 72 | void packet_release( packet_t packet ); |
103 | void packet_release( packet_t packet ); |
| - | 104 | ||
| - | 105 | /** Returns the packet of dimensions at least as given. |
|
| - | 106 | * Tries to reuse free packets first. |
|
| - | 107 | * Creates a new packet aligned to the memory page size if none available. |
|
| - | 108 | * @param owner The new owner of the packet. Input parameter. |
|
| - | 109 | * @param addr_len The source and destination addresses maximal length in bytes. Input parameter. |
|
| - | 110 | * @param max_prefix The maximal prefix length in bytes. Input parameter. |
|
| - | 111 | * @param max_content The maximal content length in bytes. Input parameter. |
|
| - | 112 | * @param max_suffix The maximal suffix length in bytes. Input parameter. |
|
| - | 113 | * @returns The packet of dimensions at least as given. |
|
| - | 114 | * @returns NULL if there is not enough memory left. |
|
| - | 115 | */ |
|
| 73 | packet_t packet_get( services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_sufix ); |
116 | packet_t packet_get( services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ); |
| - | 117 | ||
| - | 118 | /** Creates a new packet of dimensions at least as given. |
|
| - | 119 | * @param length The total length of the packet, including the header, the addresses and the data of the packet. Input parameter. |
|
| - | 120 | * @param owner The new owner of the packet. Input parameter. |
|
| - | 121 | * @param addr_len The source and destination addresses maximal length in bytes. Input parameter. |
|
| - | 122 | * @param max_prefix The maximal prefix length in bytes. Input parameter. |
|
| - | 123 | * @param max_content The maximal content length in bytes. Input parameter. |
|
| - | 124 | * @param max_suffix The maximal suffix length in bytes. Input parameter. |
|
| - | 125 | * @returns The packet of dimensions at least as given. |
|
| - | 126 | * @returns NULL if there is not enough memory left. |
|
| - | 127 | */ |
|
| 74 | packet_t packet_create( size_t length, services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_sufix ); |
128 | packet_t packet_create( size_t length, services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ); |
| - | 129 | ||
| - | 130 | /** Initializes the packet according to the given dimensions. |
|
| - | 131 | * @param packet The packet to be initialized. Input parameter. |
|
| - | 132 | * @param owner The new owner of the packet. Input parameter. |
|
| - | 133 | * @param addr_len The source and destination addresses maximal length in bytes. Input parameter. |
|
| - | 134 | * @param max_prefix The maximal prefix length in bytes. Input parameter. |
|
| - | 135 | * @param max_content The maximal content length in bytes. Input parameter. |
|
| - | 136 | * @param max_suffix The maximal suffix length in bytes. Input parameter. |
|
| - | 137 | */ |
|
| 75 | void packet_init( packet_t packet, services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_sufix ); |
138 | void packet_init( packet_t packet, services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ); |
| - | 139 | ||
| - | 140 | /** Shares the packet memory block. |
|
| - | 141 | * @param packet The packet to be shared. |
|
| - | 142 | * @returns EOK on success. |
|
| - | 143 | * @returns EINVAL if the packet is not valid. |
|
| - | 144 | * @returns EINVAL if the calling module does not accept the memory. |
|
| - | 145 | * @returns ENOMEM if the desired and actual sizes differ. |
|
| - | 146 | * @returns Other error codes as defined for the ipc_share_in_finalize() function. |
|
| - | 147 | */ |
|
| 76 | int packet_reply( const packet_t packet ); |
148 | int packet_reply( const packet_t packet ); |
| 77 | 149 | ||
| 78 | int packet_server_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
150 | int packet_server_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
| 79 | packet_t packet; |
151 | packet_t packet; |
| 80 | 152 | ||
| Line 120... | Line 192... | ||
| 120 | 192 | ||
| 121 | for( index = 0; ( index < FREE_QUEUES_COUNT - 1 ) && ( packet->length > ps_globals.sizes[ index ] ); ++ index ); |
193 | for( index = 0; ( index < FREE_QUEUES_COUNT - 1 ) && ( packet->length > ps_globals.sizes[ index ] ); ++ index ); |
| 122 | ps_globals.free[ index ] = pq_add( ps_globals.free[ index ], packet, packet->length, packet->length ); |
194 | ps_globals.free[ index ] = pq_add( ps_globals.free[ index ], packet, packet->length, packet->length ); |
| 123 | } |
195 | } |
| 124 | 196 | ||
| 125 | packet_t packet_get( services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_sufix ){ |
197 | packet_t packet_get( services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){ |
| 126 | int index; |
198 | int index; |
| 127 | packet_t packet; |
199 | packet_t packet; |
| 128 | size_t length; |
200 | size_t length; |
| 129 | 201 | ||
| 130 | length = ALIGN_UP( sizeof( struct packet ) + 2 * addr_len + max_prefix + max_content + max_sufix, PAGE_SIZE ); |
202 | length = ALIGN_UP( sizeof( struct packet ) + 2 * addr_len + max_prefix + max_content + max_suffix, PAGE_SIZE ); |
| 131 | for( index = 0; index < FREE_QUEUES_COUNT - 1; ++ index ){ |
203 | for( index = 0; index < FREE_QUEUES_COUNT - 1; ++ index ){ |
| 132 | if( length <= ps_globals.sizes[ index ] ){ |
204 | if( length <= ps_globals.sizes[ index ] ){ |
| 133 | packet = ps_globals.free[ index ]; |
205 | packet = ps_globals.free[ index ]; |
| 134 | while( packet_is_valid( packet ) && ( packet->length < length )){ |
206 | while( packet_is_valid( packet ) && ( packet->length < length )){ |
| 135 | packet = pm_find( packet->next ); |
207 | packet = pm_find( packet->next ); |
| 136 | } |
208 | } |
| 137 | if( packet ){ |
209 | if( packet ){ |
| 138 | packet_init( packet, owner, addr_len, max_prefix, max_content, max_sufix ); |
210 | packet_init( packet, owner, addr_len, max_prefix, max_content, max_suffix ); |
| 139 | return packet; |
211 | return packet; |
| 140 | } |
212 | } |
| 141 | } |
213 | } |
| 142 | } |
214 | } |
| 143 | return packet_create( length, owner, addr_len, max_prefix, max_content, max_sufix ); |
215 | return packet_create( length, owner, addr_len, max_prefix, max_content, max_suffix ); |
| 144 | } |
216 | } |
| 145 | 217 | ||
| 146 | packet_t packet_create( size_t length, services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_sufix ){ |
218 | packet_t packet_create( size_t length, services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){ |
| 147 | ERROR_DECLARE; |
219 | ERROR_DECLARE; |
| 148 | 220 | ||
| 149 | packet_t packet; |
221 | packet_t packet; |
| 150 | 222 | ||
| 151 | packet = ( packet_t ) mmap( NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 ); |
223 | packet = ( packet_t ) mmap( NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 ); |
| 152 | if( packet == MAP_FAILED ) return NULL; |
224 | if( packet == MAP_FAILED ) return NULL; |
| 153 | ++ ps_globals.count; |
225 | ++ ps_globals.count; |
| 154 | packet->packet_id = ps_globals.count; |
226 | packet->packet_id = ps_globals.count; |
| 155 | packet->mode = PM_ONEWAY; |
227 | packet->mode = PM_ONE_WAY; |
| 156 | packet->length = length; |
228 | packet->length = length; |
| 157 | packet_init( packet, owner, addr_len, max_prefix, max_content, max_sufix ); |
229 | packet_init( packet, owner, addr_len, max_prefix, max_content, max_suffix ); |
| 158 | packet->magic_value = PACKET_MAGIC_VALUE; |
230 | packet->magic_value = PACKET_MAGIC_VALUE; |
| 159 | if( ERROR_OCCURED( pm_add( packet ))){ |
231 | if( ERROR_OCCURRED( pm_add( packet ))){ |
| 160 | munmap( packet, packet->length ); |
232 | munmap( packet, packet->length ); |
| 161 | return NULL; |
233 | return NULL; |
| 162 | } |
234 | } |
| 163 | packet_release( packet ); |
235 | packet_release( packet ); |
| 164 | return packet; |
236 | return packet; |
| 165 | } |
237 | } |
| 166 | 238 | ||
| 167 | void packet_init( packet_t packet, services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_sufix ){ |
239 | void packet_init( packet_t packet, services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){ |
| 168 | packet->owner = owner; |
240 | packet->owner = owner; |
| 169 | packet->order = 0; |
241 | packet->order = 0; |
| 170 | packet->metric = 0; |
242 | packet->metric = 0; |
| 171 | packet->previous = 0; |
243 | packet->previous = 0; |
| 172 | packet->next = 0; |
244 | packet->next = 0; |