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