Subversion Repositories HelenOS

Rev

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

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