Subversion Repositories HelenOS

Rev

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

Rev 4696 Rev 4703
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 hardware address.
61
/** Default hardware address.
62
 */
62
 */
63
#define DEFAULT_ADDR        "\0\0\0\0\0\0"
63
#define DEFAULT_ADDR        "\0\0\0\0\0\0"
64
 
64
 
65
/** Default address length.
65
/** Default address length.
66
 */
66
 */
67
#define DEFAULT_ADDR_LEN    ( sizeof( DEFAULT_ADDR ) / sizeof( char ))
67
#define DEFAULT_ADDR_LEN    ( sizeof( DEFAULT_ADDR ) / sizeof( char ))
68
 
68
 
69
/** Loopback module name.
69
/** Loopback module name.
70
 */
70
 */
71
#define NAME    "lo - loopback interface"
71
#define NAME    "lo - loopback interface"
72
 
72
 
73
/** Network interface global data.
73
/** Network interface global data.
74
 */
74
 */
75
netif_globals_t netif_globals;
75
netif_globals_t netif_globals;
76
 
76
 
77
/** Changes the loopback state.
77
/** Changes the loopback state.
78
 *  @param device The device structure. Input parameter.
78
 *  @param device The device structure. Input parameter.
79
 *  @param state The new device state. Input parameter.
79
 *  @param state The new device state. Input parameter.
80
 *  @returns The new state if changed.
80
 *  @returns The new state if changed.
81
 *  @returns EOK otherwise.
81
 *  @returns EOK otherwise.
82
 */
82
 */
83
int change_state_message( device_ref device, device_state_t state );
83
int change_state_message( device_ref device, device_state_t state );
84
 
84
 
85
/** Creates and returns the loopback network interface structure.
85
/** Creates and returns the loopback network interface structure.
86
 *  @param device_id The new devce identifier. Input parameter.
86
 *  @param device_id The new devce identifier. Input parameter.
87
 *  @param device The device structure. Output parameter.
87
 *  @param device The device structure. Output parameter.
88
 *  @returns EOK on success.
88
 *  @returns EOK on success.
89
 *  @returns EXDEV if one loopback network interface already exists.
89
 *  @returns EXDEV if one loopback network interface already exists.
90
 *  @returns ENOMEM if there is not enough memory left.
90
 *  @returns ENOMEM if there is not enough memory left.
91
 */
91
 */
92
int create( device_id_t device_id, device_ref * device );
92
int create( device_id_t device_id, device_ref * device );
93
 
93
 
94
/** Prints the module name.
94
/** Prints the module name.
95
 *  @see NAME
95
 *  @see NAME
96
 */
96
 */
97
void    module_print_name( void );
97
void    module_print_name( void );
98
 
98
 
99
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 ){
100
    return ENOTSUP;
100
    return ENOTSUP;
101
}
101
}
102
 
102
 
103
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 ){
104
    if( ! address ) return EBADMEM;
104
    if( ! address ) return EBADMEM;
105
    address->value = DEFAULT_ADDR;
105
    address->value = DEFAULT_ADDR;
106
    address->length = DEFAULT_ADDR_LEN;
106
    address->length = DEFAULT_ADDR_LEN;
107
    return EOK;
107
    return EOK;
108
}
108
}
109
 
109
 
110
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 ){
111
    ERROR_DECLARE;
111
    ERROR_DECLARE;
112
 
112
 
113
    device_ref  device;
113
    device_ref  device;
114
 
114
 
115
    if( ! stats ) return EBADMEM;
115
    if( ! stats ) return EBADMEM;
116
    ERROR_PROPAGATE( find_device( device_id, & device ));
116
    ERROR_PROPAGATE( find_device( device_id, & device ));
117
    memcpy( stats, ( device_stats_ref ) device->specific, sizeof( device_stats_t ));
117
    memcpy( stats, ( device_stats_ref ) device->specific, sizeof( device_stats_t ));
118
    return EOK;
118
    return EOK;
119
}
119
}
120
 
120
 
121
int change_state_message( device_ref device, device_state_t state ){
121
int change_state_message( device_ref device, device_state_t state ){
122
    if( device->state != state ){
122
    if( device->state != state ){
123
        device->state = state;
123
        device->state = state;
124
        printf( "State changed to %s\n", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" );
124
        printf( "State changed to %s\n", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" );
125
        return state;
125
        return state;
126
    }
126
    }
127
    return EOK;
127
    return EOK;
128
}
128
}
129
 
129
 
130
int create( device_id_t device_id, device_ref * device ){
130
int create( device_id_t device_id, device_ref * device ){
131
    int index;
131
    int index;
132
 
132
 
133
    if( device_map_count( & netif_globals.device_map ) > 0 ){
133
    if( device_map_count( & netif_globals.device_map ) > 0 ){
134
        return EXDEV;
134
        return EXDEV;
135
    }else{
135
    }else{
136
        * device = ( device_ref ) malloc( sizeof( device_t ));
136
        * device = ( device_ref ) malloc( sizeof( device_t ));
137
        if( !( * device )) return ENOMEM;
137
        if( !( * device )) return ENOMEM;
138
        ( ** device ).specific = malloc( sizeof( device_stats_t ));
138
        ( ** device ).specific = malloc( sizeof( device_stats_t ));
139
        if( ! ( ** device ).specific ){
139
        if( ! ( ** device ).specific ){
140
            free( * device );
140
            free( * device );
141
            return ENOMEM;
141
            return ENOMEM;
142
        }
142
        }
143
        null_device_stats(( device_stats_ref )( ** device ).specific );
143
        null_device_stats(( device_stats_ref )( ** device ).specific );
144
        ( ** device ).device_id = device_id;
144
        ( ** device ).device_id = device_id;
145
        ( ** device ).nil_phone = -1;
145
        ( ** device ).nil_phone = -1;
146
        ( ** device ).state = NETIF_STOPPED;
146
        ( ** device ).state = NETIF_STOPPED;
147
        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 );
148
        if( index < 0 ){
148
        if( index < 0 ){
149
            free( * device );
149
            free( * device );
150
            free(( ** device ).specific );
150
            free(( ** device ).specific );
151
            * device = NULL;
151
            * device = NULL;
152
            return index;
152
            return index;
153
        }
153
        }
154
    }
154
    }
155
    return EOK;
155
    return EOK;
156
}
156
}
157
 
157
 
158
int netif_initialize( void ){
158
int netif_initialize( void ){
159
    ipcarg_t    phonehash;
159
    ipcarg_t    phonehash;
160
 
160
 
161
    return REGISTER_ME( SERVICE_LO, & phonehash );
161
    return REGISTER_ME( SERVICE_LO, & phonehash );
162
}
162
}
163
 
163
 
164
void module_print_name( void ){
164
void module_print_name( void ){
165
    printf( "%s", NAME );
165
    printf( "%s", NAME );
166
}
166
}
167
 
167
 
168
int netif_probe_auto_message( void ){
-
 
169
/*  ERROR_DECLARE;
-
 
170
 
-
 
171
    device_ref  device;
-
 
172
 
-
 
173
    ERROR_PROPAGATE( create( arg1, & device ));
-
 
174
    ipc_call_sync_3_3( netif_globals.net_phone, NET_NET_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
-
 
175
*/  return ENOTSUP;
-
 
176
}
-
 
177
 
-
 
178
int netif_probe_message( device_id_t device_id, int irq, int io ){
168
int netif_probe_message( device_id_t device_id, int irq, int io ){
179
    ERROR_DECLARE;
169
    ERROR_DECLARE;
180
 
170
 
181
    device_ref          device;
171
    device_ref          device;
182
 
172
 
183
    // create a new device
173
    // create a new device
184
    ERROR_PROPAGATE( create( device_id, & device ));
174
    ERROR_PROPAGATE( create( device_id, & device ));
185
    // print the settings
175
    // print the settings
186
    printf("New device created:\n\tid\t= %d\n", device->device_id );
176
    printf("New device created:\n\tid\t= %d\n", device->device_id );
187
    return EOK;
177
    return EOK;
188
}
178
}
189
 
179
 
190
int netif_send_message( device_id_t device_id, packet_t packet, services_t sender ){
180
int netif_send_message( device_id_t device_id, packet_t packet, services_t sender ){
191
    ERROR_DECLARE;
181
    ERROR_DECLARE;
192
 
182
 
193
    device_ref  device;
183
    device_ref  device;
194
    size_t      length;
184
    size_t      length;
195
    packet_t    next;
185
    packet_t    next;
196
    int         phone;
186
    int         phone;
197
 
187
 
198
    ERROR_PROPAGATE( find_device( device_id, & device ));
188
    ERROR_PROPAGATE( find_device( device_id, & device ));
199
    if( device->state != NETIF_ACTIVE ) return EPERM;
189
    if( device->state != NETIF_ACTIVE ) return EPERM;
200
    next = packet;
190
    next = packet;
201
    do{
191
    do{
202
        ++ (( device_stats_ref ) device->specific )->tx_packets;
192
        ++ (( device_stats_ref ) device->specific )->tx_packets;
203
        ++ (( device_stats_ref ) device->specific )->rx_packets;
193
        ++ (( device_stats_ref ) device->specific )->rx_packets;
204
        length = packet_get_data_length( next );
194
        length = packet_get_data_length( next );
205
        (( device_stats_ref ) device->specific )->tx_bytes += length;
195
        (( device_stats_ref ) device->specific )->tx_bytes += length;
206
        (( device_stats_ref ) device->specific )->rx_bytes += length;
196
        (( device_stats_ref ) device->specific )->rx_bytes += length;
207
        next = pq_next( next );
197
        next = pq_next( next );
208
    }while( next );
198
    }while( next );
209
    phone = device->nil_phone;
199
    phone = device->nil_phone;
210
    fibril_rwlock_write_unlock( & netif_globals.lock );
200
    fibril_rwlock_write_unlock( & netif_globals.lock );
211
    nil_received_msg( phone, device_id, packet, sender );
201
    nil_received_msg( phone, device_id, packet, sender );
212
    fibril_rwlock_write_lock( & netif_globals.lock );
202
    fibril_rwlock_write_lock( & netif_globals.lock );
213
    return EOK;
203
    return EOK;
214
}
204
}
215
 
205
 
216
int netif_start_message( device_ref device ){
206
int netif_start_message( device_ref device ){
217
    return change_state_message( device, NETIF_ACTIVE );
207
    return change_state_message( device, NETIF_ACTIVE );
218
}
208
}
219
 
209
 
220
int netif_stop_message( device_ref device ){
210
int netif_stop_message( device_ref device ){
221
    return change_state_message( device, NETIF_STOPPED );
211
    return change_state_message( device, NETIF_STOPPED );
222
}
212
}
223
 
213
 
224
/** @}
214
/** @}
225
 */
215
 */
226
 
216