Rev 4722 | Rev 4731 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3666 | mejdrech | 1 | /* |
3912 | mejdrech | 2 | * Copyright (c) 2009 Lukas Mejdrech |
3666 | mejdrech | 3 | * All rights reserved. |
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
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 |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
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 |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
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 |
||
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 |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
3912 | mejdrech | 29 | /** @addtogroup lo |
30 | * @{ |
||
3666 | mejdrech | 31 | */ |
32 | |||
33 | /** @file |
||
4350 | mejdrech | 34 | * Loopback network interface implementation. |
3666 | mejdrech | 35 | */ |
36 | |||
37 | #include <async.h> |
||
38 | #include <errno.h> |
||
39 | #include <stdio.h> |
||
4582 | mejdrech | 40 | #include <string.h> |
3846 | mejdrech | 41 | |
3666 | mejdrech | 42 | #include <ipc/ipc.h> |
43 | #include <ipc/services.h> |
||
44 | |||
3886 | mejdrech | 45 | #include "../../err.h" |
46 | #include "../../messages.h" |
||
47 | #include "../../modules.h" |
||
3666 | mejdrech | 48 | |
3886 | mejdrech | 49 | #include "../../structures/measured_strings.h" |
3901 | mejdrech | 50 | #include "../../structures/packet/packet_client.h" |
3666 | mejdrech | 51 | |
4243 | mejdrech | 52 | #include "../../include/device.h" |
4307 | mejdrech | 53 | #include "../../include/nil_interface.h" |
4243 | mejdrech | 54 | |
4498 | mejdrech | 55 | #include "../../nil/nil_messages.h" |
56 | |||
3886 | mejdrech | 57 | #include "../netif.h" |
4307 | mejdrech | 58 | #include "../netif_module.h" |
3886 | mejdrech | 59 | |
4350 | mejdrech | 60 | /** Default hardware address. |
61 | */ |
||
4192 | mejdrech | 62 | #define DEFAULT_ADDR "\0\0\0\0\0\0" |
4350 | mejdrech | 63 | |
64 | /** Default address length. |
||
65 | */ |
||
4192 | mejdrech | 66 | #define DEFAULT_ADDR_LEN ( sizeof( DEFAULT_ADDR ) / sizeof( char )) |
67 | |||
4350 | mejdrech | 68 | /** Loopback module name. |
69 | */ |
||
3666 | mejdrech | 70 | #define NAME "lo - loopback interface" |
71 | |||
4350 | mejdrech | 72 | /** Network interface global data. |
73 | */ |
||
3666 | mejdrech | 74 | netif_globals_t netif_globals; |
75 | |||
4350 | mejdrech | 76 | /** Changes the loopback state. |
77 | * @param device The device structure. Input parameter. |
||
78 | * @param state The new device state. Input parameter. |
||
79 | * @returns The new state if changed. |
||
80 | * @returns EOK otherwise. |
||
81 | */ |
||
82 | int change_state_message( device_ref device, device_state_t state ); |
||
83 | |||
84 | /** Creates and returns the loopback network interface structure. |
||
85 | * @param device_id The new devce identifier. Input parameter. |
||
86 | * @param device The device structure. Output parameter. |
||
87 | * @returns EOK on success. |
||
88 | * @returns EXDEV if one loopback network interface already exists. |
||
89 | * @returns ENOMEM if there is not enough memory left. |
||
90 | */ |
||
91 | int create( device_id_t device_id, device_ref * device ); |
||
92 | |||
93 | /** Prints the module name. |
||
94 | * @see NAME |
||
95 | */ |
||
4307 | mejdrech | 96 | void module_print_name( void ); |
3666 | mejdrech | 97 | |
4243 | mejdrech | 98 | int netif_specific_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
4696 | mejdrech | 99 | return ENOTSUP; |
4163 | mejdrech | 100 | } |
101 | |||
4243 | mejdrech | 102 | int netif_get_addr_message( device_id_t device_id, measured_string_ref address ){ |
4192 | mejdrech | 103 | if( ! address ) return EBADMEM; |
104 | address->value = DEFAULT_ADDR; |
||
105 | address->length = DEFAULT_ADDR_LEN; |
||
4243 | mejdrech | 106 | return EOK; |
4192 | mejdrech | 107 | } |
108 | |||
4243 | mejdrech | 109 | int netif_get_device_stats( device_id_t device_id, device_stats_ref stats ){ |
4192 | mejdrech | 110 | ERROR_DECLARE; |
111 | |||
112 | device_ref device; |
||
113 | |||
114 | if( ! stats ) return EBADMEM; |
||
115 | ERROR_PROPAGATE( find_device( device_id, & device )); |
||
116 | memcpy( stats, ( device_stats_ref ) device->specific, sizeof( device_stats_t )); |
||
117 | return EOK; |
||
118 | } |
||
119 | |||
4350 | mejdrech | 120 | int change_state_message( device_ref device, device_state_t state ){ |
3846 | mejdrech | 121 | if( device->state != state ){ |
122 | device->state = state; |
||
4307 | mejdrech | 123 | printf( "State changed to %s\n", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" ); |
4261 | mejdrech | 124 | return state; |
3846 | mejdrech | 125 | } |
3666 | mejdrech | 126 | return EOK; |
127 | } |
||
128 | |||
4350 | mejdrech | 129 | int create( device_id_t device_id, device_ref * device ){ |
4192 | mejdrech | 130 | int index; |
3666 | mejdrech | 131 | |
3846 | mejdrech | 132 | if( device_map_count( & netif_globals.device_map ) > 0 ){ |
3666 | mejdrech | 133 | return EXDEV; |
134 | }else{ |
||
3846 | mejdrech | 135 | * device = ( device_ref ) malloc( sizeof( device_t )); |
3666 | mejdrech | 136 | if( !( * device )) return ENOMEM; |
4192 | mejdrech | 137 | ( ** device ).specific = malloc( sizeof( device_stats_t )); |
138 | if( ! ( ** device ).specific ){ |
||
139 | free( * device ); |
||
140 | return ENOMEM; |
||
141 | } |
||
142 | null_device_stats(( device_stats_ref )( ** device ).specific ); |
||
3666 | mejdrech | 143 | ( ** device ).device_id = device_id; |
3846 | mejdrech | 144 | ( ** device ).nil_phone = -1; |
3685 | mejdrech | 145 | ( ** device ).state = NETIF_STOPPED; |
4192 | mejdrech | 146 | index = device_map_add( & netif_globals.device_map, ( ** device ).device_id, * device ); |
147 | if( index < 0 ){ |
||
3666 | mejdrech | 148 | free( * device ); |
4192 | mejdrech | 149 | free(( ** device ).specific ); |
3666 | mejdrech | 150 | * device = NULL; |
4192 | mejdrech | 151 | return index; |
3666 | mejdrech | 152 | } |
153 | } |
||
154 | return EOK; |
||
155 | } |
||
156 | |||
4243 | mejdrech | 157 | int netif_initialize( void ){ |
3685 | mejdrech | 158 | ipcarg_t phonehash; |
3666 | mejdrech | 159 | |
160 | return REGISTER_ME( SERVICE_LO, & phonehash ); |
||
161 | } |
||
162 | |||
4307 | mejdrech | 163 | void module_print_name( void ){ |
4197 | mejdrech | 164 | printf( "%s", NAME ); |
3666 | mejdrech | 165 | } |
166 | |||
4243 | mejdrech | 167 | int netif_probe_message( device_id_t device_id, int irq, int io ){ |
3666 | mejdrech | 168 | ERROR_DECLARE; |
169 | |||
4307 | mejdrech | 170 | device_ref device; |
3666 | mejdrech | 171 | |
3685 | mejdrech | 172 | // create a new device |
3846 | mejdrech | 173 | ERROR_PROPAGATE( create( device_id, & device )); |
4163 | mejdrech | 174 | // print the settings |
4696 | mejdrech | 175 | printf("New device created:\n\tid\t= %d\n", device->device_id ); |
3666 | mejdrech | 176 | return EOK; |
177 | } |
||
178 | |||
4261 | mejdrech | 179 | int netif_send_message( device_id_t device_id, packet_t packet, services_t sender ){ |
3666 | mejdrech | 180 | ERROR_DECLARE; |
181 | |||
3846 | mejdrech | 182 | device_ref device; |
183 | size_t length; |
||
3991 | mejdrech | 184 | packet_t next; |
4261 | mejdrech | 185 | int phone; |
3666 | mejdrech | 186 | |
3846 | mejdrech | 187 | ERROR_PROPAGATE( find_device( device_id, & device )); |
188 | if( device->state != NETIF_ACTIVE ) return EPERM; |
||
4075 | mejdrech | 189 | next = packet; |
3991 | mejdrech | 190 | do{ |
4728 | mejdrech | 191 | ++ (( device_stats_ref ) device->specific )->send_packets; |
192 | ++ (( device_stats_ref ) device->specific )->receive_packets; |
||
4075 | mejdrech | 193 | length = packet_get_data_length( next ); |
4728 | mejdrech | 194 | (( device_stats_ref ) device->specific )->send_bytes += length; |
195 | (( device_stats_ref ) device->specific )->receive_bytes += length; |
||
4075 | mejdrech | 196 | next = pq_next( next ); |
197 | }while( next ); |
||
4261 | mejdrech | 198 | phone = device->nil_phone; |
4582 | mejdrech | 199 | fibril_rwlock_write_unlock( & netif_globals.lock ); |
4261 | mejdrech | 200 | nil_received_msg( phone, device_id, packet, sender ); |
4582 | mejdrech | 201 | fibril_rwlock_write_lock( & netif_globals.lock ); |
3846 | mejdrech | 202 | return EOK; |
3666 | mejdrech | 203 | } |
204 | |||
4261 | mejdrech | 205 | int netif_start_message( device_ref device ){ |
206 | return change_state_message( device, NETIF_ACTIVE ); |
||
3666 | mejdrech | 207 | } |
208 | |||
4261 | mejdrech | 209 | int netif_stop_message( device_ref device ){ |
210 | return change_state_message( device, NETIF_STOPPED ); |
||
3666 | mejdrech | 211 | } |
212 | |||
213 | /** @} |
||
214 | */ |