Ignore:
File:
1 edited

Legend:

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

    r14f1db0 r25271006  
    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>
    3940#include <stdio.h>
    4041#include <str.h>
     
    4243#include <ipc/ipc.h>
    4344#include <ipc/services.h>
    44 
    45 #include <net_err.h>
    46 #include <net_messages.h>
    47 #include <net_modules.h>
     45#include <ipc/nil.h>
     46
     47#include <net/modules.h>
    4848#include <adt/measured_strings.h>
    49 #include <packet/packet_client.h>
    50 #include <net_device.h>
     49#include <packet_client.h>
     50#include <net/device.h>
    5151#include <nil_interface.h>
    52 #include <nil_messages.h>
    5352#include <netif_interface.h>
    5453#include <netif_local.h>
    5554
    56 /** Default hardware address.
    57  */
     55/** Default hardware address. */
    5856#define DEFAULT_ADDR            "\0\0\0\0\0\0"
    5957
    60 /** Default address length.
    61  */
     58/** Default address length. */
    6259#define DEFAULT_ADDR_LEN        (sizeof(DEFAULT_ADDR) / sizeof(char))
    6360
    64 /** Loopback module name.
    65  */
     61/** Loopback module name. */
    6662#define NAME  "lo"
    6763
    68 /** Network interface global data.
    69  */
     64/** Network interface global data. */
    7065netif_globals_t netif_globals;
    7166
    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  */
    78 int 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  */
    87 int create(device_id_t device_id, netif_device_t * * device);
    88 
    89 int netif_specific_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
     67int
     68netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
     69    ipc_call_t *answer, int *answer_count)
     70{
    9071        return ENOTSUP;
    9172}
    9273
    93 int netif_get_addr_message(device_id_t device_id, measured_string_ref address){
    94         if(! address){
     74int netif_get_addr_message(device_id_t device_id, measured_string_ref address)
     75{
     76        if (!address)
    9577                return EBADMEM;
    96         }
    9778        address->value = str_dup(DEFAULT_ADDR);
    9879        address->length = DEFAULT_ADDR_LEN;
     
    10081}
    10182
    102 int 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){
     83int 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)
    10890                return EBADMEM;
    109         }
    11091        ERROR_PROPAGATE(find_device(device_id, &device));
    111         memcpy(stats, (device_stats_ref) device->specific, sizeof(device_stats_t));
    112         return EOK;
    113 }
    114 
    115 int change_state_message(netif_device_t * device, device_state_t state)
     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 */
     104static int change_state_message(netif_device_t *device, device_state_t state)
    116105{
    117106        if (device->state != state) {
     
    127116}
    128117
    129 int create(device_id_t device_id, netif_device_t * * device){
     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 */
     126static int create(device_id_t device_id, netif_device_t **device)
     127{
    130128        int index;
    131129
    132         if(netif_device_map_count(&netif_globals.device_map) > 0){
     130        if (netif_device_map_count(&netif_globals.device_map) > 0)
    133131                return EXDEV;
    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 
    159 int netif_initialize(void){
     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
     157int netif_initialize(void)
     158{
    160159        ipcarg_t phonehash;
    161160
     
    163162}
    164163
    165 int netif_probe_message(device_id_t device_id, int irq, uintptr_t io){
    166         ERROR_DECLARE;
    167 
    168         netif_device_t * device;
     164int netif_probe_message(device_id_t device_id, int irq, uintptr_t io)
     165{
     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         ERROR_DECLARE;
    179 
    180         netif_device_t * device;
     177int netif_send_message(device_id_t device_id, packet_t packet, services_t sender)
     178{
     179        ERROR_DECLARE;
     180
     181        netif_device_t *device;
    181182        size_t length;
    182183        packet_t next;
     
    184185
    185186        ERROR_PROPAGATE(find_device(device_id, &device));
    186         if(device->state != NETIF_ACTIVE){
     187        if (device->state != NETIF_ACTIVE) {
    187188                netif_pq_release(packet_get_id(packet));
    188189                return EFORWARD;
    189190        }
    190191        next = packet;
    191         do{
    192                 ++ ((device_stats_ref) device->specific)->send_packets;
    193                 ++ ((device_stats_ref) device->specific)->receive_packets;
     192        do {
     193                ((device_stats_ref) device->specific)->send_packets++;
     194                ((device_stats_ref) device->specific)->receive_packets++;
    194195                length = packet_get_data_length(next);
    195196                ((device_stats_ref) device->specific)->send_bytes += length;
    196197                ((device_stats_ref) device->specific)->receive_bytes += length;
    197198                next = pq_next(next);
    198         }while(next);
     199        } while(next);
    199200        phone = device->nil_phone;
    200201        fibril_rwlock_write_unlock(&netif_globals.lock);
    201202        nil_received_msg(phone, device_id, packet, sender);
    202203        fibril_rwlock_write_lock(&netif_globals.lock);
    203         return EOK;
    204 }
    205 
    206 int netif_start_message(netif_device_t * device){
     204       
     205        return EOK;
     206}
     207
     208int netif_start_message(netif_device_t *device)
     209{
    207210        return change_state_message(device, NETIF_ACTIVE);
    208211}
    209212
    210 int netif_stop_message(netif_device_t * device){
     213int netif_stop_message(netif_device_t *device)
     214{
    211215        return change_state_message(device, NETIF_STOPPED);
    212216}
     
    214218/** Default thread for new connections.
    215219 *
    216  * @param[in] iid The initial message identifier.
    217  * @param[in] icall The initial message call structure.
    218  *
     220 * @param[in] iid       The initial message identifier.
     221 * @param[in] icall     The initial message call structure.
    219222 */
    220223static void netif_client_connection(ipc_callid_t iid, ipc_call_t *icall)
     
    226229        ipc_answer_0(iid, EOK);
    227230       
    228         while(true) {
     231        while (true) {
    229232                ipc_call_t answer;
    230233                int answer_count;
     
    241244                    &answer_count);
    242245               
    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))
     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))
    245252                        return;
    246253               
     
    255262       
    256263        /* Start the module */
    257         if (ERROR_OCCURRED(netif_module_start(netif_client_connection)))
    258                 return ERROR_CODE;
    259        
     264        ERROR_PROPAGATE(netif_module_start(netif_client_connection));
    260265        return EOK;
    261266}
Note: See TracChangeset for help on using the changeset viewer.