Subversion Repositories HelenOS

Rev

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