Subversion Repositories HelenOS

Rev

Rev 3912 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3901 mejdrech 1
/*
2
 * Copyright (c) 2008 Lukas Mejdrech
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
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
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup net
30
 *  @{
31
 */
32
 
33
/** @file
34
 */
35
 
36
#include <async.h>
37
#include <align.h>
38
#include <errno.h>
39
//#include <stdio.h>
40
#include <unistd.h>
41
 
42
#include <ipc/ipc.h>
43
#include <ipc/services.h>
44
#include <sys/mman.h>
45
 
46
#include "../../err.h"
47
#include "../../messages.h"
48
 
49
#include "packet.h"
50
#include "packet_header.h"
51
#include "packet_server.h"
52
 
53
#define IPC_GET_ID( call )          ( packet_id_t ) IPC_GET_ARG1( * call )
54
#define IPC_GET_OWNER( call )       ( services_t ) IPC_GET_ARG1( * call )
55
#define IPC_GET_CONTENT( call )     ( size_t ) IPC_GET_ARG2( * call )
56
#define IPC_GET_ADDR_LEN( call )    ( size_t ) IPC_GET_ARG3( * call )
57
#define IPC_GET_PREFIX( call )      ( size_t ) IPC_GET_ARG4( * call )
58
#define IPC_GET_SUFIX( call )       ( size_t ) IPC_GET_ARG5( * call )
59
 
60
#define FREE_QUEUES_COUNT   7
61
 
62
static struct{
63
    packet_t free[ FREE_QUEUES_COUNT ];
64
    int sizes[ FREE_QUEUES_COUNT ];
65
    unsigned int count;
66
} ps_globals = {
67
    { 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 },
69
 
70
};
71
 
72
void packet_release( packet_t packet );
73
packet_t packet_get( services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_sufix );
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 );
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 );
76
int packet_reply( const packet_t packet );
77
 
78
int packet_server_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
79
    packet_t packet;
80
 
81
    * answer_count = 0;
82
    switch( IPC_GET_METHOD( * call )){
83
        case IPC_M_PHONE_HUNGUP:
84
            return EOK;
85
        case NET_PACKET_CREATE_1:
86
            packet = packet_get( IPC_GET_OWNER( call ), 0, 0, IPC_GET_CONTENT( call ), 0 );
87
            if( ! packet ) return ENOMEM;
88
            * answer_count = 2;
89
            IPC_SET_ARG1( * answer, packet->packet_id );
90
            IPC_SET_ARG2( * answer, packet->length );
91
            return EOK;
92
        case NET_PACKET_CREATE_5:
93
            packet = packet_get( IPC_GET_OWNER( call ), IPC_GET_ADDR_LEN( call ), IPC_GET_PREFIX( call ), IPC_GET_CONTENT( call ), IPC_GET_SUFIX( call ));
94
            if( ! packet ) return ENOMEM;
95
            * answer_count = 2;
96
            IPC_SET_ARG1( * answer, packet->packet_id );
97
            IPC_SET_ARG2( * answer, packet->length );
98
            return EOK;
99
        case NET_PACKET_GET:
100
            packet = pm_find( IPC_GET_ID( call ));
101
            if( ! packet_is_valid( packet )) return ENOENT;
102
            return packet_reply( packet );
103
        case NET_PACKET_GET_SIZE:
104
            packet = pm_find( IPC_GET_ID( call ));
105
            if( ! packet_is_valid( packet )) return ENOENT;
106
            * answer_count = 1;
107
            IPC_SET_ARG1( * answer, packet->length );
108
            return EOK;
109
        case NET_PACKET_RELEASE:
110
            packet = pm_find( IPC_GET_ID( call ));
111
            if( ! packet_is_valid( packet )) return ENOENT;
112
            pq_destroy( packet, packet_release );
113
            return EOK;
114
    }
115
    return ENOTSUP;
116
}
117
 
118
void packet_release( packet_t packet ){
119
    int index;
120
 
121
    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 );
123
}
124
 
125
packet_t packet_get( services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_sufix ){
126
    int index;
127
    packet_t packet;
128
    size_t length;
129
 
130
    length = ALIGN_UP( sizeof( struct packet ) + 2 * addr_len + max_prefix + max_content + max_sufix, PAGE_SIZE );
131
    for( index = 0; index < FREE_QUEUES_COUNT - 1; ++ index ){
132
        if( length <= ps_globals.sizes[ index ] ){
133
            packet = ps_globals.free[ index ];
134
            while( packet_is_valid( packet ) && ( packet->length < length )){
135
                packet = pm_find( packet->next );
136
            }
137
            if( packet ){
138
                packet_init( packet, owner, addr_len, max_prefix, max_content, max_sufix );
139
                return packet;
140
            }
141
        }
142
    }
143
    return packet_create( length, owner, addr_len, max_prefix, max_content, max_sufix );
144
}
145
 
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 ){
147
    ERROR_DECLARE;
148
 
149
    packet_t    packet;
150
 
151
    packet = ( packet_t ) mmap( NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 );
152
    if( packet == MAP_FAILED ) return NULL;
153
    ++ ps_globals.count;
154
    packet->packet_id = ps_globals.count;
155
    packet->mode = PM_ONEWAY;
156
    packet->length = length;
157
    packet_init( packet, owner, addr_len, max_prefix, max_content, max_sufix );
158
    packet->magic_value = PACKET_MAGIC_VALUE;
159
    if( ERROR_OCCURED( pm_add( packet ))){
160
        munmap( packet, packet->length );
161
        return NULL;
162
    }
163
    packet_release( packet );
164
    return packet;
165
}
166
 
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 ){
168
    packet->owner = owner;
169
    packet->order = 0;
170
    packet->metric = 0;
171
    packet->previous = 0;
172
    packet->next = 0;
173
    packet->addr_len = addr_len;
174
    packet->src_addr = sizeof( struct packet );
175
    packet->dest_addr = packet->src_addr + packet->addr_len;
176
    packet->max_prefix = max_prefix;
177
    packet->max_content = max_content;
178
    packet->data_start = packet->dest_addr + packet->addr_len + packet->max_prefix;
179
    packet->data_end = packet->data_start;
180
}
181
 
182
int packet_reply( const packet_t packet ){
183
    ipc_callid_t    callid;
184
    size_t          size;
185
 
186
    if( ! packet_is_valid( packet )) return EINVAL;
187
    if( ipc_share_in_receive( & callid, & size ) <= 0 ) return EINVAL;
188
    if( size != packet->length ) return ENOMEM;
189
    return ipc_share_in_finalize( callid, packet, PROTO_READ | PROTO_WRITE );
190
}
191
 
192
/** @}
193
 */