Subversion Repositories HelenOS

Rev

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

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