Subversion Repositories HelenOS

Rev

Rev 4075 | Rev 4192 | 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
34
 */
35
 
36
#include <async.h>
37
#include <errno.h>
38
#include <stdio.h>
3846 mejdrech 39
 
3666 mejdrech 40
#include <ipc/ipc.h>
41
#include <ipc/services.h>
42
 
3886 mejdrech 43
#include "../../err.h"
44
#include "../../messages.h"
45
#include "../../modules.h"
3666 mejdrech 46
 
3886 mejdrech 47
#include "../../structures/measured_strings.h"
3901 mejdrech 48
#include "../../structures/packet/packet_client.h"
3666 mejdrech 49
 
3886 mejdrech 50
#include "../netif.h"
51
 
3666 mejdrech 52
#define DEFAULT_MTU 1500
53
 
54
#define NAME    "lo - loopback interface"
55
 
56
netif_globals_t netif_globals;
57
 
4163 mejdrech 58
static struct lo_globals{
59
    measured_string_ref addr;
60
    int mtu;
61
} lo_globals;
62
 
3846 mejdrech 63
int change_state_message( device_id_t device_id, device_state_t state );
64
int create( device_id_t device_id, device_ref * device );
65
int initialize( void );
3666 mejdrech 66
void    netif_print_name( void );
3846 mejdrech 67
int probe_auto_message( void );
68
int probe_message( device_id_t device_id, int irq, int io );
69
int send_message( device_id_t device_id, packet_t packet );
70
int start_message( device_id_t device_id );
71
int stop_message( device_id_t device_id );
4163 mejdrech 72
measured_string_ref get_addr_message( device_id_t device_id );
3666 mejdrech 73
 
4163 mejdrech 74
measured_string_ref get_addr_message( device_id_t device_id ){
75
    return lo_globals.addr;
76
}
77
 
3846 mejdrech 78
int change_state_message( device_id_t device_id, device_state_t state ){
3666 mejdrech 79
    ERROR_DECLARE;
80
 
3846 mejdrech 81
    device_ref  device;
3666 mejdrech 82
 
3846 mejdrech 83
    ERROR_PROPAGATE( find_device( device_id, & device ));
84
    if( device->state != state ){
85
        device->state = state;
3912 mejdrech 86
        nil_message( device, NET_NIL_DEVICE_STATE, device->state, NULL );
4163 mejdrech 87
        printf( "\nState changed to %s", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" );
3846 mejdrech 88
    }
3666 mejdrech 89
    return EOK;
90
}
91
 
3846 mejdrech 92
int create( device_id_t device_id, device_ref * device ){
3666 mejdrech 93
    ERROR_DECLARE;
94
 
3846 mejdrech 95
    if( device_map_count( & netif_globals.device_map ) > 0 ){
3666 mejdrech 96
        return EXDEV;
97
    }else{
3846 mejdrech 98
        * device = ( device_ref ) malloc( sizeof( device_t ));
3666 mejdrech 99
        if( !( * device )) return ENOMEM;
100
        ( ** device ).device_id = device_id;
3846 mejdrech 101
        ( ** device ).nil_phone = -1;
3685 mejdrech 102
        ( ** device ).specific = NULL;
3846 mejdrech 103
        null_device_stats( &(( ** device ).stats ));
3685 mejdrech 104
        ( ** device ).state = NETIF_STOPPED;
3912 mejdrech 105
        if( ERROR_OCCURRED( device_map_add( & netif_globals.device_map, ( ** device ).device_id, * device ))){
3666 mejdrech 106
            free( * device );
107
            * device = NULL;
108
            return ERROR_CODE;
109
        }
110
    }
111
    return EOK;
112
}
113
 
3846 mejdrech 114
int initialize( void ){
3685 mejdrech 115
    ipcarg_t    phonehash;
3666 mejdrech 116
 
4163 mejdrech 117
    lo_globals.addr = measured_string_create_bulk( "\0\0\0\0\0\0", 0 );
118
    if( ! lo_globals.addr ) return ENOMEM;
3666 mejdrech 119
    return REGISTER_ME( SERVICE_LO, & phonehash );
120
}
121
 
122
void netif_print_name( void ){
123
    printf( NAME );
124
}
125
 
3846 mejdrech 126
int probe_auto_message( void ){
3685 mejdrech 127
/*  ERROR_DECLARE;
3666 mejdrech 128
 
3846 mejdrech 129
    device_ref  device;
3666 mejdrech 130
 
3846 mejdrech 131
    ERROR_PROPAGATE( create( arg1, & device ));
132
    ipc_call_sync_3_3( netif_globals.networking_phone, NET_NET_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
3685 mejdrech 133
*/  return ENOTSUP;
3666 mejdrech 134
}
135
 
3846 mejdrech 136
int probe_message( device_id_t device_id, int irq, int io ){
3666 mejdrech 137
    ERROR_DECLARE;
138
 
3846 mejdrech 139
    device_ref      device;
3685 mejdrech 140
    aid_t           message;
141
    ipc_call_t      answer;
142
    measured_string_t   configuration[ 1 ] = {{ "MTU", 3 }};
143
    int         count = 1;
144
    measured_string_ref settings;
145
    char *          data;
3666 mejdrech 146
 
3685 mejdrech 147
    // create a new device
3846 mejdrech 148
    ERROR_PROPAGATE( create( device_id, & device ));
3685 mejdrech 149
    // get configuration
3846 mejdrech 150
    message = async_send_2( netif_globals.networking_phone, NET_NET_GET_DEVICE_CONF, device->device_id, count, & answer );
3685 mejdrech 151
    // send names and get settings
3912 mejdrech 152
    if( ERROR_OCCURRED( measured_strings_send( netif_globals.networking_phone, configuration, count ))
153
    || ERROR_OCCURRED( measured_strings_return( netif_globals.networking_phone, & settings, & data, count ))){
3685 mejdrech 154
        async_wait_for( message, NULL );
155
        return ERROR_CODE;
156
    }
157
    // MTU is the first one
158
    if( settings && ( settings[ 0 ].value )){
4163 mejdrech 159
        lo_globals.mtu = strtoul( settings[ 0 ].value, NULL, 0 );
3685 mejdrech 160
    }else{
4163 mejdrech 161
        lo_globals.mtu = DEFAULT_MTU;
3685 mejdrech 162
    }
163
    free( settings );
164
    free( data );
165
    // end request
166
    async_wait_for( message, NULL );
4163 mejdrech 167
    // print the settings
168
    printf("\nNew device registered:\n\tid\t= %d\n\tMTU\t= %d", device->device_id, lo_globals.mtu );
3666 mejdrech 169
    return EOK;
170
}
171
 
3846 mejdrech 172
int send_message( device_id_t device_id, packet_t packet ){
3666 mejdrech 173
    ERROR_DECLARE;
174
 
3846 mejdrech 175
    device_ref  device;
176
    size_t      length;
3991 mejdrech 177
    packet_t    next;
3666 mejdrech 178
 
3846 mejdrech 179
    ERROR_PROPAGATE( find_device( device_id, & device ));
180
    if( device->state != NETIF_ACTIVE ) return EPERM;
4075 mejdrech 181
    next = packet;
3991 mejdrech 182
    do{
183
        ++ device->stats.tx_packets;
184
        ++ device->stats.rx_packets;
4075 mejdrech 185
        length = packet_get_data_length( next );
3991 mejdrech 186
        device->stats.tx_bytes += length;
187
        device->stats.rx_bytes += length;
4075 mejdrech 188
        next = pq_next( next );
189
    }while( next );
190
    nil_message( device, NET_NIL_RECEIVED, packet_get_id( packet ), PACKET_SELF );
3846 mejdrech 191
    return EOK;
3666 mejdrech 192
}
193
 
3846 mejdrech 194
int start_message( device_id_t device_id ){
195
    return change_state_message( device_id, NETIF_ACTIVE );
3666 mejdrech 196
}
197
 
3846 mejdrech 198
int stop_message( device_id_t device_id ){
199
    return change_state_message( device_id, NETIF_STOPPED );
3666 mejdrech 200
}
201
 
202
/** @}
203
 */