Subversion Repositories HelenOS

Rev

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

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