Subversion Repositories HelenOS

Rev

Rev 4582 | Rev 4703 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4582 Rev 4696
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
 *  Loopback network interface implementation.
34
 *  Loopback network interface implementation.
35
 */
35
 */
36
 
36
 
37
#include <async.h>
37
#include <async.h>
38
#include <errno.h>
38
#include <errno.h>
39
#include <stdio.h>
39
#include <stdio.h>
40
#include <string.h>
40
#include <string.h>
41
 
41
 
42
#include <ipc/ipc.h>
42
#include <ipc/ipc.h>
43
#include <ipc/services.h>
43
#include <ipc/services.h>
44
 
44
 
45
#include "../../err.h"
45
#include "../../err.h"
46
#include "../../messages.h"
46
#include "../../messages.h"
47
#include "../../modules.h"
47
#include "../../modules.h"
48
 
48
 
49
#include "../../structures/measured_strings.h"
49
#include "../../structures/measured_strings.h"
50
#include "../../structures/packet/packet_client.h"
50
#include "../../structures/packet/packet_client.h"
51
 
51
 
52
#include "../../include/device.h"
52
#include "../../include/device.h"
53
#include "../../include/nil_interface.h"
53
#include "../../include/nil_interface.h"
54
#include "../../include/net_interface.h"
54
#include "../../include/net_interface.h"
55
 
55
 
56
#include "../../nil/nil_messages.h"
56
#include "../../nil/nil_messages.h"
57
 
57
 
58
#include "../netif.h"
58
#include "../netif.h"
59
#include "../netif_module.h"
59
#include "../netif_module.h"
60
 
60
 
61
/** Default maximum transmission unit.
-
 
62
 */
-
 
63
#define DEFAULT_MTU 1500
-
 
64
 
-
 
65
/** Default hardware address.
61
/** Default hardware address.
66
 */
62
 */
67
#define DEFAULT_ADDR        "\0\0\0\0\0\0"
63
#define DEFAULT_ADDR        "\0\0\0\0\0\0"
68
 
64
 
69
/** Default address length.
65
/** Default address length.
70
 */
66
 */
71
#define DEFAULT_ADDR_LEN    ( sizeof( DEFAULT_ADDR ) / sizeof( char ))
67
#define DEFAULT_ADDR_LEN    ( sizeof( DEFAULT_ADDR ) / sizeof( char ))
72
 
68
 
73
/** Loopback module name.
69
/** Loopback module name.
74
 */
70
 */
75
#define NAME    "lo - loopback interface"
71
#define NAME    "lo - loopback interface"
76
 
72
 
77
/** Network interface global data.
73
/** Network interface global data.
78
 */
74
 */
79
netif_globals_t netif_globals;
75
netif_globals_t netif_globals;
80
 
76
 
81
/** Loopback module global data.
-
 
82
 */
-
 
83
static struct lo_globals{
-
 
84
    unsigned int mtu;
-
 
85
} lo_globals;
-
 
86
 
-
 
87
/** Changes the loopback state.
77
/** Changes the loopback state.
88
 *  @param device The device structure. Input parameter.
78
 *  @param device The device structure. Input parameter.
89
 *  @param state The new device state. Input parameter.
79
 *  @param state The new device state. Input parameter.
90
 *  @returns The new state if changed.
80
 *  @returns The new state if changed.
91
 *  @returns EOK otherwise.
81
 *  @returns EOK otherwise.
92
 */
82
 */
93
int change_state_message( device_ref device, device_state_t state );
83
int change_state_message( device_ref device, device_state_t state );
94
 
84
 
95
/** Creates and returns the loopback network interface structure.
85
/** Creates and returns the loopback network interface structure.
96
 *  @param device_id The new devce identifier. Input parameter.
86
 *  @param device_id The new devce identifier. Input parameter.
97
 *  @param device The device structure. Output parameter.
87
 *  @param device The device structure. Output parameter.
98
 *  @returns EOK on success.
88
 *  @returns EOK on success.
99
 *  @returns EXDEV if one loopback network interface already exists.
89
 *  @returns EXDEV if one loopback network interface already exists.
100
 *  @returns ENOMEM if there is not enough memory left.
90
 *  @returns ENOMEM if there is not enough memory left.
101
 */
91
 */
102
int create( device_id_t device_id, device_ref * device );
92
int create( device_id_t device_id, device_ref * device );
103
 
93
 
104
/** Prints the module name.
94
/** Prints the module name.
105
 *  @see NAME
95
 *  @see NAME
106
 */
96
 */
107
void    module_print_name( void );
97
void    module_print_name( void );
108
 
98
 
109
int netif_specific_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
99
int netif_specific_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
110
    switch( IPC_GET_METHOD( * call )){
-
 
111
        case NET_NIL_PACKET_SPACE:
-
 
112
            * IPC_SET_ADDR( answer ) = 0;
-
 
113
            * IPC_SET_PREFIX( answer ) = 0;
-
 
114
            * IPC_SET_CONTENT( answer ) = lo_globals.mtu;
-
 
115
            * IPC_SET_SUFFIX( answer ) = 0;
-
 
116
            * answer_count = 4;
-
 
117
            return EOK;
-
 
118
        default:
-
 
119
            return ENOTSUP;
100
    return ENOTSUP;
120
    }
-
 
121
}
101
}
122
 
102
 
123
int netif_get_addr_message( device_id_t device_id, measured_string_ref address ){
103
int netif_get_addr_message( device_id_t device_id, measured_string_ref address ){
124
    if( ! address ) return EBADMEM;
104
    if( ! address ) return EBADMEM;
125
    address->value = DEFAULT_ADDR;
105
    address->value = DEFAULT_ADDR;
126
    address->length = DEFAULT_ADDR_LEN;
106
    address->length = DEFAULT_ADDR_LEN;
127
    return EOK;
107
    return EOK;
128
}
108
}
129
 
109
 
130
int netif_get_device_stats( device_id_t device_id, device_stats_ref stats ){
110
int netif_get_device_stats( device_id_t device_id, device_stats_ref stats ){
131
    ERROR_DECLARE;
111
    ERROR_DECLARE;
132
 
112
 
133
    device_ref  device;
113
    device_ref  device;
134
 
114
 
135
    if( ! stats ) return EBADMEM;
115
    if( ! stats ) return EBADMEM;
136
    ERROR_PROPAGATE( find_device( device_id, & device ));
116
    ERROR_PROPAGATE( find_device( device_id, & device ));
137
    memcpy( stats, ( device_stats_ref ) device->specific, sizeof( device_stats_t ));
117
    memcpy( stats, ( device_stats_ref ) device->specific, sizeof( device_stats_t ));
138
    return EOK;
118
    return EOK;
139
}
119
}
140
 
120
 
141
int change_state_message( device_ref device, device_state_t state ){
121
int change_state_message( device_ref device, device_state_t state ){
142
    if( device->state != state ){
122
    if( device->state != state ){
143
        device->state = state;
123
        device->state = state;
144
        printf( "State changed to %s\n", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" );
124
        printf( "State changed to %s\n", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" );
145
        return state;
125
        return state;
146
    }
126
    }
147
    return EOK;
127
    return EOK;
148
}
128
}
149
 
129
 
150
int create( device_id_t device_id, device_ref * device ){
130
int create( device_id_t device_id, device_ref * device ){
151
    int index;
131
    int index;
152
 
132
 
153
    if( device_map_count( & netif_globals.device_map ) > 0 ){
133
    if( device_map_count( & netif_globals.device_map ) > 0 ){
154
        return EXDEV;
134
        return EXDEV;
155
    }else{
135
    }else{
156
        * device = ( device_ref ) malloc( sizeof( device_t ));
136
        * device = ( device_ref ) malloc( sizeof( device_t ));
157
        if( !( * device )) return ENOMEM;
137
        if( !( * device )) return ENOMEM;
158
        ( ** device ).specific = malloc( sizeof( device_stats_t ));
138
        ( ** device ).specific = malloc( sizeof( device_stats_t ));
159
        if( ! ( ** device ).specific ){
139
        if( ! ( ** device ).specific ){
160
            free( * device );
140
            free( * device );
161
            return ENOMEM;
141
            return ENOMEM;
162
        }
142
        }
163
        null_device_stats(( device_stats_ref )( ** device ).specific );
143
        null_device_stats(( device_stats_ref )( ** device ).specific );
164
        ( ** device ).device_id = device_id;
144
        ( ** device ).device_id = device_id;
165
        ( ** device ).nil_phone = -1;
145
        ( ** device ).nil_phone = -1;
166
        ( ** device ).state = NETIF_STOPPED;
146
        ( ** device ).state = NETIF_STOPPED;
167
        index = device_map_add( & netif_globals.device_map, ( ** device ).device_id, * device );
147
        index = device_map_add( & netif_globals.device_map, ( ** device ).device_id, * device );
168
        if( index < 0 ){
148
        if( index < 0 ){
169
            free( * device );
149
            free( * device );
170
            free(( ** device ).specific );
150
            free(( ** device ).specific );
171
            * device = NULL;
151
            * device = NULL;
172
            return index;
152
            return index;
173
        }
153
        }
174
    }
154
    }
175
    return EOK;
155
    return EOK;
176
}
156
}
177
 
157
 
178
int netif_initialize( void ){
158
int netif_initialize( void ){
179
    ipcarg_t    phonehash;
159
    ipcarg_t    phonehash;
180
 
160
 
181
    return REGISTER_ME( SERVICE_LO, & phonehash );
161
    return REGISTER_ME( SERVICE_LO, & phonehash );
182
}
162
}
183
 
163
 
184
void module_print_name( void ){
164
void module_print_name( void ){
185
    printf( "%s", NAME );
165
    printf( "%s", NAME );
186
}
166
}
187
 
167
 
188
int netif_probe_auto_message( void ){
168
int netif_probe_auto_message( void ){
189
/*  ERROR_DECLARE;
169
/*  ERROR_DECLARE;
190
 
170
 
191
    device_ref  device;
171
    device_ref  device;
192
 
172
 
193
    ERROR_PROPAGATE( create( arg1, & device ));
173
    ERROR_PROPAGATE( create( arg1, & device ));
194
    ipc_call_sync_3_3( netif_globals.net_phone, NET_NET_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
174
    ipc_call_sync_3_3( netif_globals.net_phone, NET_NET_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
195
*/  return ENOTSUP;
175
*/  return ENOTSUP;
196
}
176
}
197
 
177
 
198
int netif_probe_message( device_id_t device_id, int irq, int io ){
178
int netif_probe_message( device_id_t device_id, int irq, int io ){
199
    ERROR_DECLARE;
179
    ERROR_DECLARE;
200
 
180
 
201
    device_ref          device;
181
    device_ref          device;
202
    measured_string_t   names[ 1 ] = {{ "MTU", 3 }};
-
 
203
    measured_string_ref configuration;
-
 
204
    size_t              count = sizeof( names ) / sizeof( measured_string_t );
-
 
205
    char *              data;
-
 
206
 
182
 
207
    configuration = & names[ 0 ];
-
 
208
    // create a new device
183
    // create a new device
209
    ERROR_PROPAGATE( create( device_id, & device ));
184
    ERROR_PROPAGATE( create( device_id, & device ));
210
    // get configuration
-
 
211
    ERROR_PROPAGATE( net_get_device_conf_req( netif_globals.net_phone, device->device_id, & configuration, count, & data ));
-
 
212
    // MTU is the first one
-
 
213
    if( configuration && configuration[ 0 ].value ){
-
 
214
        lo_globals.mtu = strtoul( configuration[ 0 ].value, NULL, 0 );
-
 
215
        net_free_settings( configuration, data );
-
 
216
    }else{
-
 
217
        lo_globals.mtu = DEFAULT_MTU;
-
 
218
    }
-
 
219
    // print the settings
185
    // print the settings
220
    printf("New device registered:\n\tid\t= %d\n\tMTU\t= %d\n", device->device_id, lo_globals.mtu );
186
    printf("New device created:\n\tid\t= %d\n", device->device_id );
221
    return EOK;
187
    return EOK;
222
}
188
}
223
 
189
 
224
int netif_send_message( device_id_t device_id, packet_t packet, services_t sender ){
190
int netif_send_message( device_id_t device_id, packet_t packet, services_t sender ){
225
    ERROR_DECLARE;
191
    ERROR_DECLARE;
226
 
192
 
227
    device_ref  device;
193
    device_ref  device;
228
    size_t      length;
194
    size_t      length;
229
    packet_t    next;
195
    packet_t    next;
230
    int         phone;
196
    int         phone;
231
 
197
 
232
    ERROR_PROPAGATE( find_device( device_id, & device ));
198
    ERROR_PROPAGATE( find_device( device_id, & device ));
233
    if( device->state != NETIF_ACTIVE ) return EPERM;
199
    if( device->state != NETIF_ACTIVE ) return EPERM;
234
    next = packet;
200
    next = packet;
235
    do{
201
    do{
236
        ++ (( device_stats_ref ) device->specific )->tx_packets;
202
        ++ (( device_stats_ref ) device->specific )->tx_packets;
237
        ++ (( device_stats_ref ) device->specific )->rx_packets;
203
        ++ (( device_stats_ref ) device->specific )->rx_packets;
238
        length = packet_get_data_length( next );
204
        length = packet_get_data_length( next );
239
        (( device_stats_ref ) device->specific )->tx_bytes += length;
205
        (( device_stats_ref ) device->specific )->tx_bytes += length;
240
        (( device_stats_ref ) device->specific )->rx_bytes += length;
206
        (( device_stats_ref ) device->specific )->rx_bytes += length;
241
        next = pq_next( next );
207
        next = pq_next( next );
242
    }while( next );
208
    }while( next );
243
    phone = device->nil_phone;
209
    phone = device->nil_phone;
244
    fibril_rwlock_write_unlock( & netif_globals.lock );
210
    fibril_rwlock_write_unlock( & netif_globals.lock );
245
    nil_received_msg( phone, device_id, packet, sender );
211
    nil_received_msg( phone, device_id, packet, sender );
246
    fibril_rwlock_write_lock( & netif_globals.lock );
212
    fibril_rwlock_write_lock( & netif_globals.lock );
247
    return EOK;
213
    return EOK;
248
}
214
}
249
 
215
 
250
int netif_start_message( device_ref device ){
216
int netif_start_message( device_ref device ){
251
    return change_state_message( device, NETIF_ACTIVE );
217
    return change_state_message( device, NETIF_ACTIVE );
252
}
218
}
253
 
219
 
254
int netif_stop_message( device_ref device ){
220
int netif_stop_message( device_ref device ){
255
    return change_state_message( device, NETIF_STOPPED );
221
    return change_state_message( device, NETIF_STOPPED );
256
}
222
}
257
 
223
 
258
/** @}
224
/** @}
259
 */
225
 */
260
 
226