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