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