Subversion Repositories HelenOS

Rev

Rev 4243 | Rev 4307 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4243 Rev 4261
1
/*
1
/*
2
 * Copyright (c) 2009 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:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
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 lo
29
/** @addtogroup lo
30
 *  @{
30
 *  @{
31
 */
31
 */
32
 
32
 
33
/** @file
33
/** @file
34
 */
34
 */
35
 
35
 
36
#include <async.h>
36
#include <async.h>
37
#include <errno.h>
37
#include <errno.h>
38
#include <stdio.h>
38
#include <stdio.h>
39
 
39
 
40
#include <ipc/ipc.h>
40
#include <ipc/ipc.h>
41
#include <ipc/services.h>
41
#include <ipc/services.h>
42
 
42
 
43
#include "../../err.h"
43
#include "../../err.h"
44
#include "../../messages.h"
44
#include "../../messages.h"
45
#include "../../modules.h"
45
#include "../../modules.h"
46
 
46
 
47
#include "../../structures/measured_strings.h"
47
#include "../../structures/measured_strings.h"
48
#include "../../structures/packet/packet_client.h"
48
#include "../../structures/packet/packet_client.h"
49
 
49
 
50
#include "../../include/device.h"
50
#include "../../include/device.h"
-
 
51
#include "../../include/nil_messages.h"
51
 
52
 
52
#include "../netif.h"
53
#include "../netif.h"
53
#include "../netif_interface.h"
54
#include "../netif_interface.h"
54
 
55
 
55
#define DEFAULT_MTU 1500
56
#define DEFAULT_MTU 1500
56
 
57
 
57
#define DEFAULT_ADDR        "\0\0\0\0\0\0"
58
#define DEFAULT_ADDR        "\0\0\0\0\0\0"
58
#define DEFAULT_ADDR_LEN    ( sizeof( DEFAULT_ADDR ) / sizeof( char ))
59
#define DEFAULT_ADDR_LEN    ( sizeof( DEFAULT_ADDR ) / sizeof( char ))
59
 
60
 
60
#define NAME    "lo - loopback interface"
61
#define NAME    "lo - loopback interface"
61
 
62
 
62
netif_globals_t netif_globals;
63
netif_globals_t netif_globals;
63
 
64
 
64
static struct lo_globals{
65
static struct lo_globals{
65
    int mtu;
66
    int mtu;
66
} lo_globals;
67
} lo_globals;
67
 
68
 
68
static int  change_state_message( device_id_t device_id, device_state_t state );
69
static int  change_state_message( device_ref device, device_state_t state );
69
static int  create( device_id_t device_id, device_ref * device );
70
static int  create( device_id_t device_id, device_ref * device );
70
void    netif_print_name( void );
71
void    netif_print_name( void );
71
 
72
 
72
int netif_specific_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
73
int netif_specific_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
73
    //TODO nil send message
74
    //TODO nil send message
74
    return ENOTSUP;
75
    return ENOTSUP;
75
}
76
}
76
 
77
 
77
int netif_get_addr_message( device_id_t device_id, measured_string_ref address ){
78
int netif_get_addr_message( device_id_t device_id, measured_string_ref address ){
78
    if( ! address ) return EBADMEM;
79
    if( ! address ) return EBADMEM;
79
    address->value = DEFAULT_ADDR;
80
    address->value = DEFAULT_ADDR;
80
    address->length = DEFAULT_ADDR_LEN;
81
    address->length = DEFAULT_ADDR_LEN;
81
    return EOK;
82
    return EOK;
82
}
83
}
83
 
84
 
84
int netif_get_device_stats( device_id_t device_id, device_stats_ref stats ){
85
int netif_get_device_stats( device_id_t device_id, device_stats_ref stats ){
85
    ERROR_DECLARE;
86
    ERROR_DECLARE;
86
 
87
 
87
    device_ref  device;
88
    device_ref  device;
88
 
89
 
89
    if( ! stats ) return EBADMEM;
90
    if( ! stats ) return EBADMEM;
90
    ERROR_PROPAGATE( find_device( device_id, & device ));
91
    ERROR_PROPAGATE( find_device( device_id, & device ));
91
    memcpy( stats, ( device_stats_ref ) device->specific, sizeof( device_stats_t ));
92
    memcpy( stats, ( device_stats_ref ) device->specific, sizeof( device_stats_t ));
92
    return EOK;
93
    return EOK;
93
}
94
}
94
 
95
 
95
static int change_state_message( device_id_t device_id, device_state_t state ){
96
static int change_state_message( device_ref device, device_state_t state ){
96
    ERROR_DECLARE;
-
 
97
 
-
 
98
    device_ref  device;
-
 
99
 
-
 
100
    ERROR_PROPAGATE( find_device( device_id, & device ));
-
 
101
    if( device->state != state ){
97
    if( device->state != state ){
102
        device->state = state;
98
        device->state = state;
103
        nil_message( device, NET_NIL_DEVICE_STATE, device->state, NULL );
-
 
104
        printf( "\nState changed to %s", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" );
99
        printf( "\nState changed to %s", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" );
-
 
100
        return state;
105
    }
101
    }
106
    return EOK;
102
    return EOK;
107
}
103
}
108
 
104
 
109
static int create( device_id_t device_id, device_ref * device ){
105
static int create( device_id_t device_id, device_ref * device ){
110
    int index;
106
    int index;
111
 
107
 
112
    if( device_map_count( & netif_globals.device_map ) > 0 ){
108
    if( device_map_count( & netif_globals.device_map ) > 0 ){
113
        return EXDEV;
109
        return EXDEV;
114
    }else{
110
    }else{
115
        * device = ( device_ref ) malloc( sizeof( device_t ));
111
        * device = ( device_ref ) malloc( sizeof( device_t ));
116
        if( !( * device )) return ENOMEM;
112
        if( !( * device )) return ENOMEM;
117
        ( ** device ).specific = malloc( sizeof( device_stats_t ));
113
        ( ** device ).specific = malloc( sizeof( device_stats_t ));
118
        if( ! ( ** device ).specific ){
114
        if( ! ( ** device ).specific ){
119
            free( * device );
115
            free( * device );
120
            return ENOMEM;
116
            return ENOMEM;
121
        }
117
        }
122
        null_device_stats(( device_stats_ref )( ** device ).specific );
118
        null_device_stats(( device_stats_ref )( ** device ).specific );
123
        ( ** device ).device_id = device_id;
119
        ( ** device ).device_id = device_id;
124
        ( ** device ).nil_phone = -1;
120
        ( ** device ).nil_phone = -1;
125
        ( ** device ).state = NETIF_STOPPED;
121
        ( ** device ).state = NETIF_STOPPED;
126
        index = device_map_add( & netif_globals.device_map, ( ** device ).device_id, * device );
122
        index = device_map_add( & netif_globals.device_map, ( ** device ).device_id, * device );
127
        if( index < 0 ){
123
        if( index < 0 ){
128
            free( * device );
124
            free( * device );
129
            free(( ** device ).specific );
125
            free(( ** device ).specific );
130
            * device = NULL;
126
            * device = NULL;
131
            return index;
127
            return index;
132
        }
128
        }
133
    }
129
    }
134
    return EOK;
130
    return EOK;
135
}
131
}
136
 
132
 
137
int netif_initialize( void ){
133
int netif_initialize( void ){
138
    ipcarg_t    phonehash;
134
    ipcarg_t    phonehash;
139
 
135
 
140
    return REGISTER_ME( SERVICE_LO, & phonehash );
136
    return REGISTER_ME( SERVICE_LO, & phonehash );
141
}
137
}
142
 
138
 
143
void netif_print_name( void ){
139
void netif_print_name( void ){
144
    printf( "%s", NAME );
140
    printf( "%s", NAME );
145
}
141
}
146
 
142
 
147
int netif_probe_auto_message( void ){
143
int netif_probe_auto_message( void ){
148
/*  ERROR_DECLARE;
144
/*  ERROR_DECLARE;
149
 
145
 
150
    device_ref  device;
146
    device_ref  device;
151
 
147
 
152
    ERROR_PROPAGATE( create( arg1, & device ));
148
    ERROR_PROPAGATE( create( arg1, & device ));
153
    ipc_call_sync_3_3( netif_globals.networking_phone, NET_NET_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
149
    ipc_call_sync_3_3( netif_globals.networking_phone, NET_NET_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
154
*/  return ENOTSUP;
150
*/  return ENOTSUP;
155
}
151
}
156
 
152
 
157
int netif_probe_message( device_id_t device_id, int irq, int io ){
153
int netif_probe_message( device_id_t device_id, int irq, int io ){
158
    ERROR_DECLARE;
154
    ERROR_DECLARE;
159
 
155
 
160
    device_ref      device;
156
    device_ref      device;
161
    aid_t           message;
157
    aid_t           message;
162
    ipc_call_t      answer;
158
    ipc_call_t      answer;
163
    measured_string_t   configuration[ 1 ] = {{ "MTU", 3 }};
159
    measured_string_t   configuration[ 1 ] = {{ "MTU", 3 }};
164
    int         count = 1;
160
    int         count = 1;
165
    measured_string_ref settings;
161
    measured_string_ref settings;
166
    char *          data;
162
    char *          data;
167
    ipcarg_t        result;
163
    ipcarg_t        result;
168
 
164
 
169
    // create a new device
165
    // create a new device
170
    ERROR_PROPAGATE( create( device_id, & device ));
166
    ERROR_PROPAGATE( create( device_id, & device ));
171
    // get configuration
167
    // get configuration
172
    message = async_send_2( netif_globals.networking_phone, NET_NET_GET_DEVICE_CONF, device->device_id, count, & answer );
168
    message = async_send_2( netif_globals.networking_phone, NET_NET_GET_DEVICE_CONF, device->device_id, count, & answer );
173
    // send names and get settings
169
    // send names and get settings
174
    if( ERROR_OCCURRED( measured_strings_send( netif_globals.networking_phone, configuration, count ))
170
    if( ERROR_OCCURRED( measured_strings_send( netif_globals.networking_phone, configuration, count ))
175
    || ERROR_OCCURRED( measured_strings_return( netif_globals.networking_phone, & settings, & data, count ))){
171
    || ERROR_OCCURRED( measured_strings_return( netif_globals.networking_phone, & settings, & data, count ))){
176
        async_wait_for( message, NULL );
172
        async_wait_for( message, NULL );
177
        return ERROR_CODE;
173
        return ERROR_CODE;
178
    }
174
    }
179
    // end request
175
    // end request
180
    async_wait_for( message, & result );
176
    async_wait_for( message, & result );
181
    if( ERROR_OCCURRED( result )){
177
    if( ERROR_OCCURRED( result )){
182
        if( settings ){
178
        if( settings ){
183
            free( settings );
179
            free( settings );
184
            free( data );
180
            free( data );
185
        }
181
        }
186
        return ERROR_CODE;
182
        return ERROR_CODE;
187
    }
183
    }
188
    // MTU is the first one
184
    // MTU is the first one
189
    if( settings && ( settings[ 0 ].value )){
185
    if( settings && ( settings[ 0 ].value )){
190
        lo_globals.mtu = strtoul( settings[ 0 ].value, NULL, 0 );
186
        lo_globals.mtu = strtoul( settings[ 0 ].value, NULL, 0 );
191
        free( settings );
187
        free( settings );
192
        free( data );
188
        free( data );
193
    }else{
189
    }else{
194
        lo_globals.mtu = DEFAULT_MTU;
190
        lo_globals.mtu = DEFAULT_MTU;
195
    }
191
    }
196
    // print the settings
192
    // print the settings
197
    printf("\nNew device registered:\n\tid\t= %d\n\tMTU\t= %d", device->device_id, lo_globals.mtu );
193
    printf("\nNew device registered:\n\tid\t= %d\n\tMTU\t= %d", device->device_id, lo_globals.mtu );
198
    return EOK;
194
    return EOK;
199
}
195
}
200
 
196
 
201
int netif_send_message( device_id_t device_id, packet_t packet ){
197
int netif_send_message( device_id_t device_id, packet_t packet, services_t sender ){
202
    ERROR_DECLARE;
198
    ERROR_DECLARE;
203
 
199
 
204
    device_ref  device;
200
    device_ref  device;
205
    size_t      length;
201
    size_t      length;
206
    packet_t    next;
202
    packet_t    next;
-
 
203
    int         phone;
207
 
204
 
208
    ERROR_PROPAGATE( find_device( device_id, & device ));
205
    ERROR_PROPAGATE( find_device( device_id, & device ));
209
    if( device->state != NETIF_ACTIVE ) return EPERM;
206
    if( device->state != NETIF_ACTIVE ) return EPERM;
210
    next = packet;
207
    next = packet;
211
    do{
208
    do{
212
        ++ (( device_stats_ref ) device->specific )->tx_packets;
209
        ++ (( device_stats_ref ) device->specific )->tx_packets;
213
        ++ (( device_stats_ref ) device->specific )->rx_packets;
210
        ++ (( device_stats_ref ) device->specific )->rx_packets;
214
        length = packet_get_data_length( next );
211
        length = packet_get_data_length( next );
215
        (( device_stats_ref ) device->specific )->tx_bytes += length;
212
        (( device_stats_ref ) device->specific )->tx_bytes += length;
216
        (( device_stats_ref ) device->specific )->rx_bytes += length;
213
        (( device_stats_ref ) device->specific )->rx_bytes += length;
217
        next = pq_next( next );
214
        next = pq_next( next );
218
    }while( next );
215
    }while( next );
-
 
216
    phone = device->nil_phone;
-
 
217
    rwlock_write_unlock( & netif_globals.lock );
219
    nil_message( device, NET_NIL_RECEIVED, packet_get_id( packet ), PACKET_SELF );
218
    nil_received_msg( phone, device_id, packet, sender );
-
 
219
    rwlock_write_lock( & netif_globals.lock );
220
    return EOK;
220
    return EOK;
221
}
221
}
222
 
222
 
223
int netif_start_message( device_id_t device_id ){
223
int netif_start_message( device_ref device ){
224
    return change_state_message( device_id, NETIF_ACTIVE );
224
    return change_state_message( device, NETIF_ACTIVE );
225
}
225
}
226
 
226
 
227
int netif_stop_message( device_id_t device_id ){
227
int netif_stop_message( device_ref device ){
228
    return change_state_message( device_id, NETIF_STOPPED );
228
    return change_state_message( device, NETIF_STOPPED );
229
}
229
}
230
 
230
 
231
/** @}
231
/** @}
232
 */
232
 */
233
 
233