Subversion Repositories HelenOS

Rev

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

Rev 3666 Rev 3685
1
/*
1
/*
2
 * Copyright (c) 2008 Lukas Mejdrech
2
 * Copyright (c) 2008 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 net
29
/** @addtogroup net
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/** @file
33
/** @file
34
 */
34
 */
35
 
35
 
36
#include <async.h>
36
#include <async.h>
37
#include <errno.h>
37
#include <errno.h>
38
#include <stdio.h>
38
#include <stdio.h>
39
#include <ipc/ipc.h>
39
#include <ipc/ipc.h>
40
#include <ipc/services.h>
40
#include <ipc/services.h>
41
//#include <sys/mman.h>
41
//#include <sys/mman.h>
42
 
42
 
43
#include "../err.h"
43
#include "../err.h"
-
 
44
#include "../measured_strings.h"
44
#include "../messages.h"
45
#include "../messages.h"
45
#include "../modules.h"
46
#include "../modules.h"
46
 
47
 
47
#include "netif.h"
48
#include "netif.h"
48
 
49
 
49
#define DEFAULT_MTU 1500
50
#define DEFAULT_MTU 1500
50
 
51
 
51
#define NAME    "lo - loopback interface"
52
#define NAME    "lo - loopback interface"
52
 
53
 
53
netif_globals_t netif_globals;
54
netif_globals_t netif_globals;
54
 
55
 
55
void    change_status( netif_device_ref device, netif_status_t status );
56
void    change_state( netif_device_ref device, netif_state_t state );
56
int change_status_message( netif_device_id_t device_id, netif_status_t status );
57
int change_state_message( netif_device_id_t device_id, netif_state_t state );
57
int netif_create( netif_device_id_t device_id, netif_device_ref * device );
58
int netif_create( netif_device_id_t device_id, netif_device_ref * device );
58
int netif_call( ipc_callid_t callid );
59
int netif_call( ipc_callid_t callid );
59
int netif_initialize( void );
60
int netif_initialize( void );
60
void    netif_print_name( void );
61
void    netif_print_name( void );
61
int netif_probe_auto_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
62
int netif_probe_auto_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
62
int netif_probe_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
63
int netif_probe_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
63
int netif_send_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
64
int netif_send_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
64
int netif_start_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
65
int netif_start_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
65
int netif_stop_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
66
int netif_stop_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
66
 
67
 
67
void change_status( netif_device_ref device, netif_status_t status ){
68
void change_state( netif_device_ref device, netif_state_t state ){
68
    device->status = status;
69
    device->state = state;
69
    ll_message( device, NET_LL_DEVICE_STATUS_CHANGED, device->status, NULL, NULL, NULL, NULL );
70
    ll_message( device, NET_LL_DEVICE_STATE_CHANGED, device->state, NULL, NULL, NULL, NULL );
70
}
71
}
71
 
72
 
72
int change_status_message( netif_device_id_t device_id, netif_status_t status ){
73
int change_state_message( netif_device_id_t device_id, netif_state_t state ){
73
    ERROR_DECLARE;
74
    ERROR_DECLARE;
74
 
75
 
75
    netif_device_ref    device;
76
    netif_device_ref    device;
76
 
77
 
77
    ERROR_PROPAGATE( netif_device_find( device_id, & device ));
78
    ERROR_PROPAGATE( netif_device_find( device_id, & device ));
78
    change_status( device, status );
79
    change_state( device, state );
79
    return EOK;
80
    return EOK;
80
}
81
}
81
 
82
 
82
int netif_create( netif_device_id_t device_id, netif_device_ref * device ){
83
int netif_create( netif_device_id_t device_id, netif_device_ref * device ){
83
    ERROR_DECLARE;
84
    ERROR_DECLARE;
84
 
85
 
85
    if( netif_device_map_count( & netif_globals.netif_device_map ) > 0 ){
86
    if( netif_device_map_count( & netif_globals.netif_device_map ) > 0 ){
86
        return EXDEV;
87
        return EXDEV;
87
    }else{
88
    }else{
88
        * device = ( netif_device_ref ) malloc( sizeof( netif_device_t ));
89
        * device = ( netif_device_ref ) malloc( sizeof( netif_device_t ));
89
        if( !( * device )) return ENOMEM;
90
        if( !( * device )) return ENOMEM;
90
//      ( ** device ).device_id = netif_device_id_generate( 1 );
-
 
91
        ( ** device ).device_id = device_id;
91
        ( ** device ).device_id = device_id;
92
        ( ** device ).ll_registered = NULL;
92
        ( ** device ).ll_registered = -1;
-
 
93
        ( ** device ).specific = NULL;
93
        netif_device_stats_null( &(( ** device ).stats ));
94
        netif_device_stats_null( &(( ** device ).stats ));
94
        ( ** device ).status = NETIF_STOPPED;
95
        ( ** device ).state = NETIF_STOPPED;
95
        ( ** device ).flags = NULL;
96
        ( ** device ).flags = NULL;
96
        ( ** device ).mtu = DEFAULT_MTU;
97
        ( ** device ).mtu = DEFAULT_MTU;
97
        if( ERROR_OCCURED( netif_device_map_add( & netif_globals.netif_device_map, ( ** device ).device_id, * device ))){
98
        if( ERROR_OCCURED( netif_device_map_add( & netif_globals.netif_device_map, ( ** device ).device_id, * device ))){
98
            free( * device );
99
            free( * device );
99
            * device = NULL;
100
            * device = NULL;
100
            return ERROR_CODE;
101
            return ERROR_CODE;
101
        }
102
        }
102
    }
103
    }
103
    return EOK;
104
    return EOK;
104
}
105
}
105
 
106
 
106
int netif_call( ipc_callid_t callid ){
107
int netif_call( ipc_callid_t callid ){
107
    return EOK;
108
    return EOK;
108
}
109
}
109
 
110
 
110
int netif_initialize( void ){
111
int netif_initialize( void ){
111
    int phonehash;
112
    ipcarg_t    phonehash;
112
 
113
 
113
    return REGISTER_ME( SERVICE_LO, & phonehash );
114
    return REGISTER_ME( SERVICE_LO, & phonehash );
114
}
115
}
115
 
116
 
116
void netif_print_name( void ){
117
void netif_print_name( void ){
117
    printf( NAME );
118
    printf( NAME );
118
}
119
}
119
 
120
 
120
int netif_probe_auto_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
121
int netif_probe_auto_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
121
    ERROR_DECLARE;
122
/*  ERROR_DECLARE;
122
 
123
 
123
    netif_device_ref    device;
124
    netif_device_ref    device;
124
 
125
 
125
    ERROR_PROPAGATE( netif_create( arg1, & device ));
126
    ERROR_PROPAGATE( netif_create( arg1, & device ));
126
    networking_message( NET_NETWORKING_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
127
    ipc_call_sync_3_3( netif_globals.networking_phone, NET_NETWORKING_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
127
    return EOK;
128
*/  return ENOTSUP;
128
}
129
}
129
 
130
 
130
int netif_probe_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
131
int netif_probe_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
131
    ERROR_DECLARE;
132
    ERROR_DECLARE;
132
 
133
 
133
    netif_device_ref    device;
134
    netif_device_ref    device;
-
 
135
    aid_t           message;
-
 
136
    ipc_call_t      answer;
-
 
137
    measured_string_t   configuration[ 1 ] = {{ "MTU", 3 }};
-
 
138
    int         count = 1;
-
 
139
    measured_string_ref settings;
-
 
140
    char *          data;
134
 
141
 
-
 
142
    // create a new device
135
    ERROR_PROPAGATE( netif_create( arg1, & device ));
143
    ERROR_PROPAGATE( netif_create( arg1, & device ));
-
 
144
    // get configuration
-
 
145
    message = async_send_2( netif_globals.networking_phone, NET_NETWORKING_GET_DEVICE_CONFIGURATION, device->device_id, count, & answer );
-
 
146
    // send names and get settings
-
 
147
    if( ERROR_OCCURED( measured_strings_send( netif_globals.networking_phone, configuration, count ))
-
 
148
    || ERROR_OCCURED( measured_strings_return( netif_globals.networking_phone, & settings, & data, count ))){
-
 
149
        async_wait_for( message, NULL );
-
 
150
        return ERROR_CODE;
-
 
151
    }
-
 
152
    // MTU is the first one
-
 
153
    if( settings && ( settings[ 0 ].value )){
-
 
154
        device->mtu = strtoul( settings[ 0 ].value, NULL, 0 );
-
 
155
    }else{
-
 
156
        device->mtu = DEFAULT_MTU;
-
 
157
    }
-
 
158
    // print the settings
-
 
159
    printf("\n -MTU=%d", device->mtu );
-
 
160
    free( settings );
-
 
161
    free( data );
-
 
162
    // end request
-
 
163
    async_wait_for( message, NULL );
136
    return EOK;
164
    return EOK;
137
}
165
}
138
 
166
 
139
int netif_send_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
167
int netif_send_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
140
    ERROR_DECLARE;
168
    ERROR_DECLARE;
141
 
169
 
142
    netif_device_ref    device;
170
    netif_device_ref    device;
143
 
171
 
144
    ERROR_PROPAGATE( netif_device_find( arg1, & device ));
172
    ERROR_PROPAGATE( netif_device_find( arg1, & device ));
145
    if( device->status == NETIF_ACTIVE ){
173
    if( device->state == NETIF_ACTIVE ){
146
        ++ device->stats.tx_packets;
174
        ++ device->stats.tx_packets;
147
        ++ device->stats.rx_packets;
175
        ++ device->stats.rx_packets;
148
        // TODO packet size
176
        // TODO packet size
149
        //device->stats->tx_bytes += ;
177
        //device->stats->tx_bytes += ;
150
        //device->stats->rx_bytes += ;
178
        //device->stats->rx_bytes += ;
151
        ll_message( device, NET_LL_RECEIVED, arg2, NULL, NULL, NULL, NULL );
179
        ll_message( device, NET_LL_RECEIVED, arg2, NULL, NULL, NULL, NULL );
152
        return EOK;
180
        return EOK;
153
    }else{
181
    }else{
154
        return EPERM;
182
        return EPERM;
155
    }
183
    }
156
}
184
}
157
 
185
 
158
int netif_start_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
186
int netif_start_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
159
    return change_status_message( arg1, NETIF_ACTIVE );
187
    return change_state_message( arg1, NETIF_ACTIVE );
160
}
188
}
161
 
189
 
162
int netif_stop_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
190
int netif_stop_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
163
    return change_status_message( arg1, NETIF_STOPPED );
191
    return change_state_message( arg1, NETIF_STOPPED );
164
}
192
}
165
 
193
 
166
/** @}
194
/** @}
167
 */
195
 */
168
 
196