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