Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/netif/lo/lo.c

    r25271006 r14f1db0  
    2828
    2929/** @addtogroup lo
    30  * @{
     30 *  @{
    3131 */
    3232
    3333/** @file
    34  * Loopback network interface implementation.
     34 *  Loopback network interface implementation.
    3535 */
    3636
    3737#include <async.h>
    3838#include <errno.h>
    39 #include <err.h>
    4039#include <stdio.h>
    4140#include <str.h>
     
    4342#include <ipc/ipc.h>
    4443#include <ipc/services.h>
    45 #include <ipc/nil.h>
    46 
    47 #include <net/modules.h>
     44
     45#include <net_err.h>
     46#include <net_messages.h>
     47#include <net_modules.h>
    4848#include <adt/measured_strings.h>
    49 #include <packet_client.h>
    50 #include <net/device.h>
     49#include <packet/packet_client.h>
     50#include <net_device.h>
    5151#include <nil_interface.h>
     52#include <nil_messages.h>
    5253#include <netif_interface.h>
    5354#include <netif_local.h>
    5455
    55 /** Default hardware address. */
     56/** Default hardware address.
     57 */
    5658#define DEFAULT_ADDR            "\0\0\0\0\0\0"
    5759
    58 /** Default address length. */
     60/** Default address length.
     61 */
    5962#define DEFAULT_ADDR_LEN        (sizeof(DEFAULT_ADDR) / sizeof(char))
    6063
    61 /** Loopback module name. */
     64/** Loopback module name.
     65 */
    6266#define NAME  "lo"
    6367
    64 /** Network interface global data. */
     68/** Network interface global data.
     69 */
    6570netif_globals_t netif_globals;
    6671
    67 int
    68 netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
    69     ipc_call_t *answer, int *answer_count)
    70 {
     72/** Changes the loopback state.
     73 *  @param[in] device The device structure.
     74 *  @param[in] state The new device state.
     75 *  @returns The new state if changed.
     76 *  @returns EOK otherwise.
     77 */
     78int change_state_message(netif_device_t * device, device_state_t state);
     79
     80/** Creates and returns the loopback network interface structure.
     81 *  @param[in] device_id The new devce identifier.
     82 *  @param[out] device The device structure.
     83 *  @returns EOK on success.
     84 *  @returns EXDEV if one loopback network interface already exists.
     85 *  @returns ENOMEM if there is not enough memory left.
     86 */
     87int create(device_id_t device_id, netif_device_t * * device);
     88
     89int netif_specific_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
    7190        return ENOTSUP;
    7291}
    7392
    74 int netif_get_addr_message(device_id_t device_id, measured_string_ref address)
    75 {
    76         if (!address)
     93int netif_get_addr_message(device_id_t device_id, measured_string_ref address){
     94        if(! address){
    7795                return EBADMEM;
     96        }
    7897        address->value = str_dup(DEFAULT_ADDR);
    7998        address->length = DEFAULT_ADDR_LEN;
     
    81100}
    82101
    83 int netif_get_device_stats(device_id_t device_id, device_stats_ref stats)
    84 {
    85         ERROR_DECLARE;
    86 
    87         netif_device_t *device;
    88 
    89         if (!stats)
     102int netif_get_device_stats(device_id_t device_id, device_stats_ref stats){
     103        ERROR_DECLARE;
     104
     105        netif_device_t * device;
     106
     107        if(! stats){
    90108                return EBADMEM;
     109        }
    91110        ERROR_PROPAGATE(find_device(device_id, &device));
    92         memcpy(stats, (device_stats_ref) device->specific,
    93             sizeof(device_stats_t));
    94         return EOK;
    95 }
    96 
    97 /** Changes the loopback state.
    98  *
    99  * @param[in] device    The device structure.
    100  * @param[in] state     The new device state.
    101  * @returns             The new state if changed.
    102  * @returns             EOK otherwise.
    103  */
    104 static int change_state_message(netif_device_t *device, device_state_t state)
     111        memcpy(stats, (device_stats_ref) device->specific, sizeof(device_stats_t));
     112        return EOK;
     113}
     114
     115int change_state_message(netif_device_t * device, device_state_t state)
    105116{
    106117        if (device->state != state) {
     
    116127}
    117128
    118 /** Creates and returns the loopback network interface structure.
    119  *
    120  * @param[in] device_id The new devce identifier.
    121  * @param[out] device   The device structure.
    122  * @returns             EOK on success.
    123  * @returns             EXDEV if one loopback network interface already exists.
    124  * @returns             ENOMEM if there is not enough memory left.
    125  */
    126 static int create(device_id_t device_id, netif_device_t **device)
    127 {
     129int create(device_id_t device_id, netif_device_t * * device){
    128130        int index;
    129131
    130         if (netif_device_map_count(&netif_globals.device_map) > 0)
     132        if(netif_device_map_count(&netif_globals.device_map) > 0){
    131133                return EXDEV;
    132 
    133         *device = (netif_device_t *) malloc(sizeof(netif_device_t));
    134         if (!*device)
    135                 return ENOMEM;
    136         (*device)->specific = (device_stats_t *) malloc(sizeof(device_stats_t));
    137         if (!(*device)->specific) {
    138                 free(*device);
    139                 return ENOMEM;
    140         }
    141         null_device_stats((device_stats_ref) (*device)->specific);
    142         (*device)->device_id = device_id;
    143         (*device)->nil_phone = -1;
    144         (*device)->state = NETIF_STOPPED;
    145         index = netif_device_map_add(&netif_globals.device_map,
    146             (*device)->device_id, *device);
    147         if (index < 0) {
    148                 free(*device);
    149                 free((*device)->specific);
    150                 *device = NULL;
    151                 return index;
    152         }
    153        
    154         return EOK;
    155 }
    156 
    157 int netif_initialize(void)
    158 {
     134        }else{
     135                *device = (netif_device_t *) malloc(sizeof(netif_device_t));
     136                if(!(*device)){
     137                        return ENOMEM;
     138                }
     139                (** device).specific = malloc(sizeof(device_stats_t));
     140                if(! (** device).specific){
     141                        free(*device);
     142                        return ENOMEM;
     143                }
     144                null_device_stats((device_stats_ref)(** device).specific);
     145                (** device).device_id = device_id;
     146                (** device).nil_phone = -1;
     147                (** device).state = NETIF_STOPPED;
     148                index = netif_device_map_add(&netif_globals.device_map, (** device).device_id, * device);
     149                if(index < 0){
     150                        free(*device);
     151                        free((** device).specific);
     152                        *device = NULL;
     153                        return index;
     154                }
     155        }
     156        return EOK;
     157}
     158
     159int netif_initialize(void){
    159160        ipcarg_t phonehash;
    160161
     
    162163}
    163164
    164 int netif_probe_message(device_id_t device_id, int irq, uintptr_t io)
    165 {
    166         ERROR_DECLARE;
    167 
    168         netif_device_t *device;
     165int netif_probe_message(device_id_t device_id, int irq, uintptr_t io){
     166        ERROR_DECLARE;
     167
     168        netif_device_t * device;
    169169
    170170        // create a new device
     
    175175}
    176176
    177 int netif_send_message(device_id_t device_id, packet_t packet, services_t sender)
    178 {
    179         ERROR_DECLARE;
    180 
    181         netif_device_t *device;
     177int netif_send_message(device_id_t device_id, packet_t packet, services_t sender){
     178        ERROR_DECLARE;
     179
     180        netif_device_t * device;
    182181        size_t length;
    183182        packet_t next;
     
    185184
    186185        ERROR_PROPAGATE(find_device(device_id, &device));
    187         if (device->state != NETIF_ACTIVE) {
     186        if(device->state != NETIF_ACTIVE){
    188187                netif_pq_release(packet_get_id(packet));
    189188                return EFORWARD;
    190189        }
    191190        next = packet;
    192         do {
    193                 ((device_stats_ref) device->specific)->send_packets++;
    194                 ((device_stats_ref) device->specific)->receive_packets++;
     191        do{
     192                ++ ((device_stats_ref) device->specific)->send_packets;
     193                ++ ((device_stats_ref) device->specific)->receive_packets;
    195194                length = packet_get_data_length(next);
    196195                ((device_stats_ref) device->specific)->send_bytes += length;
    197196                ((device_stats_ref) device->specific)->receive_bytes += length;
    198197                next = pq_next(next);
    199         } while(next);
     198        }while(next);
    200199        phone = device->nil_phone;
    201200        fibril_rwlock_write_unlock(&netif_globals.lock);
    202201        nil_received_msg(phone, device_id, packet, sender);
    203202        fibril_rwlock_write_lock(&netif_globals.lock);
    204        
    205         return EOK;
    206 }
    207 
    208 int netif_start_message(netif_device_t *device)
    209 {
     203        return EOK;
     204}
     205
     206int netif_start_message(netif_device_t * device){
    210207        return change_state_message(device, NETIF_ACTIVE);
    211208}
    212209
    213 int netif_stop_message(netif_device_t *device)
    214 {
     210int netif_stop_message(netif_device_t * device){
    215211        return change_state_message(device, NETIF_STOPPED);
    216212}
     
    218214/** Default thread for new connections.
    219215 *
    220  * @param[in] iid       The initial message identifier.
    221  * @param[in] icall     The initial message call structure.
     216 * @param[in] iid The initial message identifier.
     217 * @param[in] icall The initial message call structure.
     218 *
    222219 */
    223220static void netif_client_connection(ipc_callid_t iid, ipc_call_t *icall)
     
    229226        ipc_answer_0(iid, EOK);
    230227       
    231         while (true) {
     228        while(true) {
    232229                ipc_call_t answer;
    233230                int answer_count;
     
    244241                    &answer_count);
    245242               
    246                 /*
    247                  * End if told to either by the message or the processing
    248                  * result.
    249                  */
    250                 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||
    251                     (res == EHANGUP))
     243                /* End if said to either by the message or the processing result */
     244                if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
    252245                        return;
    253246               
     
    262255       
    263256        /* Start the module */
    264         ERROR_PROPAGATE(netif_module_start(netif_client_connection));
     257        if (ERROR_OCCURRED(netif_module_start(netif_client_connection)))
     258                return ERROR_CODE;
     259       
    265260        return EOK;
    266261}
Note: See TracChangeset for help on using the changeset viewer.